Skip to content

Commit e7d8606

Browse files
committed
SCEV: teach isImpliedViaOperations about samesign
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.
1 parent c3199f7 commit e7d8606

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11863,15 +11863,13 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
1186311863
}
1186411864

1186511865
// Check whether the found predicate is the same as the desired predicate.
11866-
// FIXME: use CmpPredicate::getMatching here.
11867-
if (FoundPred == static_cast<CmpInst::Predicate>(Pred))
11868-
return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI);
11866+
if (auto P = CmpPredicate::getMatching(FoundPred, Pred))
11867+
return isImpliedCondOperands(*P, LHS, RHS, FoundLHS, FoundRHS, CtxI);
1186911868

1187011869
// Check whether swapping the found predicate makes it the same as the
1187111870
// desired predicate.
11872-
// FIXME: use CmpPredicate::getMatching here.
11873-
if (ICmpInst::getSwappedCmpPredicate(FoundPred) ==
11874-
static_cast<CmpInst::Predicate>(Pred)) {
11871+
if (auto P = CmpPredicate::getMatching(
11872+
ICmpInst::getSwappedCmpPredicate(FoundPred), Pred)) {
1187511873
// We can write the implication
1187611874
// 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS
1187711875
// using one of the following ways:
@@ -11882,22 +11880,23 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
1188211880
// Forms 1. and 2. require swapping the operands of one condition. Don't
1188311881
// do this if it would break canonical constant/addrec ordering.
1188411882
if (!isa<SCEVConstant>(RHS) && !isa<SCEVAddRecExpr>(LHS))
11885-
return isImpliedCondOperands(FoundPred, RHS, LHS, FoundLHS, FoundRHS,
11886-
CtxI);
11883+
return isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(*P), RHS,
11884+
LHS, FoundLHS, FoundRHS, CtxI);
1188711885
if (!isa<SCEVConstant>(FoundRHS) && !isa<SCEVAddRecExpr>(FoundLHS))
11888-
return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS, CtxI);
11886+
return isImpliedCondOperands(*P, LHS, RHS, FoundRHS, FoundLHS, CtxI);
1188911887

1189011888
// There's no clear preference between forms 3. and 4., try both. Avoid
1189111889
// forming getNotSCEV of pointer values as the resulting subtract is
1189211890
// not legal.
1189311891
if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() &&
11894-
isImpliedCondOperands(FoundPred, getNotSCEV(LHS), getNotSCEV(RHS),
11895-
FoundLHS, FoundRHS, CtxI))
11892+
isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(*P),
11893+
getNotSCEV(LHS), getNotSCEV(RHS), FoundLHS,
11894+
FoundRHS, CtxI))
1189611895
return true;
1189711896

1189811897
if (!FoundLHS->getType()->isPointerTy() &&
1189911898
!FoundRHS->getType()->isPointerTy() &&
11900-
isImpliedCondOperands(Pred, LHS, RHS, getNotSCEV(FoundLHS),
11899+
isImpliedCondOperands(*P, LHS, RHS, getNotSCEV(FoundLHS),
1190111900
getNotSCEV(FoundRHS), CtxI))
1190211901
return true;
1190311902

@@ -12567,14 +12566,16 @@ bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
1256712566
return false;
1256812567

1256912568
// We only want to work with GT comparison so far.
12570-
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) {
12569+
if (ICmpInst::isLT(Pred)) {
1257112570
Pred = ICmpInst::getSwappedCmpPredicate(Pred);
1257212571
std::swap(LHS, RHS);
1257312572
std::swap(FoundLHS, FoundRHS);
1257412573
}
1257512574

12575+
CmpInst::Predicate P = Pred.getPreferredSignedPredicate();
12576+
1257612577
// For unsigned, try to reduce it to corresponding signed comparison.
12577-
if (Pred == ICmpInst::ICMP_UGT)
12578+
if (P == ICmpInst::ICMP_UGT)
1257812579
// We can replace unsigned predicate with its signed counterpart if all
1257912580
// involved values are non-negative.
1258012581
// TODO: We could have better support for unsigned.
@@ -12587,10 +12588,10 @@ bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
1258712588
FoundRHS) &&
1258812589
isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS,
1258912590
FoundRHS))
12590-
Pred = ICmpInst::ICMP_SGT;
12591+
P = ICmpInst::ICMP_SGT;
1259112592
}
1259212593

12593-
if (Pred != ICmpInst::ICMP_SGT)
12594+
if (P != ICmpInst::ICMP_SGT)
1259412595
return false;
1259512596

1259612597
auto GetOpFromSExt = [&](const SCEV *S) {

llvm/test/Analysis/ScalarEvolution/implied-via-division.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ define void @implied1_samesign(i32 %n) {
3131
; Prove that (n > 1) ===> (n / 2 s> 0).
3232
; CHECK-LABEL: 'implied1_samesign'
3333
; CHECK-NEXT: Determining loop execution counts for: @implied1_samesign
34-
; CHECK-NEXT: Loop %header: backedge-taken count is (-1 + (1 smax %n.div.2))<nsw>
34+
; CHECK-NEXT: Loop %header: backedge-taken count is (-1 + %n.div.2)<nsw>
3535
; CHECK-NEXT: Loop %header: constant max backedge-taken count is i32 1073741822
36-
; CHECK-NEXT: Loop %header: symbolic max backedge-taken count is (-1 + (1 smax %n.div.2))<nsw>
36+
; CHECK-NEXT: Loop %header: symbolic max backedge-taken count is (-1 + %n.div.2)<nsw>
3737
; CHECK-NEXT: Loop %header: Trip multiple is 1
3838
;
3939
entry:

0 commit comments

Comments
 (0)