aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ScalarEvolution.cpp
diff options
context:
space:
mode:
authorRamkumar Ramachandra <ramkumar.ramachandra@codasip.com>2025-02-06 18:14:54 +0000
committerGitHub <noreply@github.com>2025-02-06 18:14:54 +0000
commitf5d24e6cbe07902bad620df291f6172b50a7271e (patch)
tree11ea898bbc272b5d0f8cdf4aaa1b2d19b7045785 /llvm/lib/Analysis/ScalarEvolution.cpp
parentdcb124e820b2bf9dda60f66151591155a385580e (diff)
downloadllvm-f5d24e6cbe07902bad620df291f6172b50a7271e.zip
llvm-f5d24e6cbe07902bad620df291f6172b50a7271e.tar.gz
llvm-f5d24e6cbe07902bad620df291f6172b50a7271e.tar.bz2
SCEV: teach isImpliedViaOperations about samesign (#124270)
Use CmpPredicate::getMatching in isImpliedCondBalancedTypes to pass samesign information to isImpliedViaOperations, and teach it to call CmpPredicate::getPreferredSignedPredicate, effectively making it optimize with samesign information.
Diffstat (limited to 'llvm/lib/Analysis/ScalarEvolution.cpp')
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp33
1 files changed, 17 insertions, 16 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 0d7bbe3..f898871 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11860,15 +11860,13 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
}
// Check whether the found predicate is the same as the desired predicate.
- // FIXME: use CmpPredicate::getMatching here.
- if (FoundPred == static_cast<CmpInst::Predicate>(Pred))
- return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI);
+ if (auto P = CmpPredicate::getMatching(FoundPred, Pred))
+ return isImpliedCondOperands(*P, LHS, RHS, FoundLHS, FoundRHS, CtxI);
// Check whether swapping the found predicate makes it the same as the
// desired predicate.
- // FIXME: use CmpPredicate::getMatching here.
- if (ICmpInst::getSwappedCmpPredicate(FoundPred) ==
- static_cast<CmpInst::Predicate>(Pred)) {
+ if (auto P = CmpPredicate::getMatching(
+ ICmpInst::getSwappedCmpPredicate(FoundPred), Pred)) {
// We can write the implication
// 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS
// using one of the following ways:
@@ -11879,22 +11877,23 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
// Forms 1. and 2. require swapping the operands of one condition. Don't
// do this if it would break canonical constant/addrec ordering.
if (!isa<SCEVConstant>(RHS) && !isa<SCEVAddRecExpr>(LHS))
- return isImpliedCondOperands(FoundPred, RHS, LHS, FoundLHS, FoundRHS,
- CtxI);
+ return isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(*P), RHS,
+ LHS, FoundLHS, FoundRHS, CtxI);
if (!isa<SCEVConstant>(FoundRHS) && !isa<SCEVAddRecExpr>(FoundLHS))
- return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS, CtxI);
+ return isImpliedCondOperands(*P, LHS, RHS, FoundRHS, FoundLHS, CtxI);
// There's no clear preference between forms 3. and 4., try both. Avoid
// forming getNotSCEV of pointer values as the resulting subtract is
// not legal.
if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() &&
- isImpliedCondOperands(FoundPred, getNotSCEV(LHS), getNotSCEV(RHS),
- FoundLHS, FoundRHS, CtxI))
+ isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(*P),
+ getNotSCEV(LHS), getNotSCEV(RHS), FoundLHS,
+ FoundRHS, CtxI))
return true;
if (!FoundLHS->getType()->isPointerTy() &&
!FoundRHS->getType()->isPointerTy() &&
- isImpliedCondOperands(Pred, LHS, RHS, getNotSCEV(FoundLHS),
+ isImpliedCondOperands(*P, LHS, RHS, getNotSCEV(FoundLHS),
getNotSCEV(FoundRHS), CtxI))
return true;
@@ -12564,14 +12563,16 @@ bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
return false;
// We only want to work with GT comparison so far.
- if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) {
+ if (ICmpInst::isLT(Pred)) {
Pred = ICmpInst::getSwappedCmpPredicate(Pred);
std::swap(LHS, RHS);
std::swap(FoundLHS, FoundRHS);
}
+ CmpInst::Predicate P = Pred.getPreferredSignedPredicate();
+
// For unsigned, try to reduce it to corresponding signed comparison.
- if (Pred == ICmpInst::ICMP_UGT)
+ if (P == ICmpInst::ICMP_UGT)
// We can replace unsigned predicate with its signed counterpart if all
// involved values are non-negative.
// TODO: We could have better support for unsigned.
@@ -12584,10 +12585,10 @@ bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
FoundRHS) &&
isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS,
FoundRHS))
- Pred = ICmpInst::ICMP_SGT;
+ P = ICmpInst::ICMP_SGT;
}
- if (Pred != ICmpInst::ICMP_SGT)
+ if (P != ICmpInst::ICMP_SGT)
return false;
auto GetOpFromSExt = [&](const SCEV *S) {