aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ValueTracking.cpp
diff options
context:
space:
mode:
authorAndreas Jonson <andjo403@hotmail.com>2025-05-27 18:53:29 +0200
committerGitHub <noreply@github.com>2025-05-27 18:53:29 +0200
commit58ead2cee80edf6f16997ff8947ccb221f10c784 (patch)
treec7cae69ec11c84ac262d8f97c36f4f5bb64f7d56 /llvm/lib/Analysis/ValueTracking.cpp
parent5ab017a30f31f704fe6981a927ac64be139faa33 (diff)
downloadllvm-58ead2cee80edf6f16997ff8947ccb221f10c784.zip
llvm-58ead2cee80edf6f16997ff8947ccb221f10c784.tar.gz
llvm-58ead2cee80edf6f16997ff8947ccb221f10c784.tar.bz2
[ValueTracking] Support trunc nuw condition in isImpliedCondition (#141528)
Proof: https://alive2.llvm.org/ce/z/oqQyxC
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp33
1 files changed, 23 insertions, 10 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 5b9338b..b7084cf 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9165,15 +9165,13 @@ isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR,
/// is true. Return false if LHS implies RHS is false. Otherwise, return
/// std::nullopt if we can't infer anything.
static std::optional<bool>
-isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
- const Value *R1, const DataLayout &DL, bool LHSIsTrue) {
- Value *L0 = LHS->getOperand(0);
- Value *L1 = LHS->getOperand(1);
-
+isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
+ CmpPredicate RPred, const Value *R0, const Value *R1,
+ const DataLayout &DL, bool LHSIsTrue) {
// The rest of the logic assumes the LHS condition is true. If that's not the
// case, invert the predicate to make it so.
- CmpPredicate LPred =
- LHSIsTrue ? LHS->getCmpPredicate() : LHS->getInverseCmpPredicate();
+ if (!LHSIsTrue)
+ LPred = ICmpInst::getInverseCmpPredicate(LPred);
// We can have non-canonical operands, so try to normalize any common operand
// to L0/R0.
@@ -9314,9 +9312,15 @@ llvm::isImpliedCondition(const Value *LHS, CmpPredicate RHSPred,
LHSIsTrue = !LHSIsTrue;
// Both LHS and RHS are icmps.
- const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
- if (LHSCmp)
- return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
+ if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
+ return isImpliedCondICmps(LHSCmp->getCmpPredicate(), LHSCmp->getOperand(0),
+ LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
+ DL, LHSIsTrue);
+ const Value *V;
+ if (match(LHS, m_NUWTrunc(m_Value(V))))
+ return isImpliedCondICmps(CmpInst::ICMP_NE, V,
+ ConstantInt::get(V->getType(), 0), RHSPred,
+ RHSOp0, RHSOp1, DL, LHSIsTrue);
/// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
/// the RHS to be an icmp.
@@ -9354,6 +9358,15 @@ std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
return std::nullopt;
}
+ const Value *V;
+ if (match(RHS, m_NUWTrunc(m_Value(V)))) {
+ if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
+ ConstantInt::get(V->getType(), 0), DL,
+ LHSIsTrue, Depth))
+ return InvertRHS ? !*Implied : *Implied;
+ return std::nullopt;
+ }
+
if (Depth == MaxAnalysisRecursionDepth)
return std::nullopt;