Skip to content

Commit 1a37147

Browse files
authored
[SCEV] Match both (-1)b + a and a + (-1)b as a - b (#84247)
In our analysis of guarding conditions, we were converting a-b == 0 into a == b alternate form, but we were only checking for one of the two forms for the sub. There's no requirement that the multiply only be on the LHS of the add.
1 parent 6fc5dc3 commit 1a37147

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10577,6 +10577,25 @@ static bool HasSameValue(const SCEV *A, const SCEV *B) {
1057710577
return false;
1057810578
}
1057910579

10580+
static bool MatchBinarySub(const SCEV *S, const SCEV *&LHS, const SCEV *&RHS) {
10581+
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S);
10582+
if (!Add || Add->getNumOperands() != 2)
10583+
return false;
10584+
if (auto *ME = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
10585+
ME && ME->getNumOperands() == 2 && ME->getOperand(0)->isAllOnesValue()) {
10586+
LHS = Add->getOperand(1);
10587+
RHS = ME->getOperand(1);
10588+
return true;
10589+
}
10590+
if (auto *ME = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
10591+
ME && ME->getNumOperands() == 2 && ME->getOperand(0)->isAllOnesValue()) {
10592+
LHS = Add->getOperand(0);
10593+
RHS = ME->getOperand(1);
10594+
return true;
10595+
}
10596+
return false;
10597+
}
10598+
1058010599
bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
1058110600
const SCEV *&LHS, const SCEV *&RHS,
1058210601
unsigned Depth) {
@@ -10652,19 +10671,10 @@ bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
1065210671
case ICmpInst::ICMP_EQ:
1065310672
case ICmpInst::ICMP_NE:
1065410673
// Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
10655-
if (!RA)
10656-
if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
10657-
if (const SCEVMulExpr *ME =
10658-
dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
10659-
if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
10660-
ME->getOperand(0)->isAllOnesValue()) {
10661-
RHS = AE->getOperand(1);
10662-
LHS = ME->getOperand(1);
10663-
Changed = true;
10664-
}
10674+
if (RA.isZero() && MatchBinarySub(LHS, LHS, RHS))
10675+
Changed = true;
1066510676
break;
1066610677

10667-
1066810678
// The "Should have been caught earlier!" messages refer to the fact
1066910679
// that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
1067010680
// should have fired on the corresponding cases, and canonicalized the

llvm/test/Analysis/ScalarEvolution/trip-count.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ define void @dual_sext_ne_with_slt_guard(i8 %s, i8 %n) {
145145
; CHECK-LABEL: 'dual_sext_ne_with_slt_guard'
146146
; CHECK-NEXT: Determining loop execution counts for: @dual_sext_ne_with_slt_guard
147147
; CHECK-NEXT: Loop %for.body: backedge-taken count is (-1 + (sext i8 %n to i64) + (-1 * (sext i8 %s to i64))<nsw>)
148-
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i64 -1
148+
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i64 -2
149149
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (-1 + (sext i8 %n to i64) + (-1 * (sext i8 %s to i64))<nsw>)
150150
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
151151
;

0 commit comments

Comments
 (0)