Skip to content

[SCEV] Match both (-1)b + a and a + (-1)b as a - b #84247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10577,6 +10577,25 @@ static bool HasSameValue(const SCEV *A, const SCEV *B) {
return false;
}

static bool MatchBinarySub(const SCEV *S, const SCEV *&LHS, const SCEV *&RHS) {
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S);
if (!Add || Add->getNumOperands() != 2)
return false;
if (auto *ME = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
ME && ME->getNumOperands() == 2 && ME->getOperand(0)->isAllOnesValue()) {
LHS = Add->getOperand(1);
RHS = ME->getOperand(1);
return true;
}
if (auto *ME = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
ME && ME->getNumOperands() == 2 && ME->getOperand(0)->isAllOnesValue()) {
LHS = Add->getOperand(0);
RHS = ME->getOperand(1);
return true;
}
return false;
}

bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
const SCEV *&LHS, const SCEV *&RHS,
unsigned Depth) {
Expand Down Expand Up @@ -10652,19 +10671,10 @@ bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_NE:
// Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
if (!RA)
if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
if (const SCEVMulExpr *ME =
dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
ME->getOperand(0)->isAllOnesValue()) {
RHS = AE->getOperand(1);
LHS = ME->getOperand(1);
Changed = true;
}
if (RA.isZero() && MatchBinarySub(LHS, LHS, RHS))
Changed = true;
break;


// The "Should have been caught earlier!" messages refer to the fact
// that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
// should have fired on the corresponding cases, and canonicalized the
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Analysis/ScalarEvolution/trip-count.ll
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ define void @dual_sext_ne_with_slt_guard(i8 %s, i8 %n) {
; CHECK-LABEL: 'dual_sext_ne_with_slt_guard'
; CHECK-NEXT: Determining loop execution counts for: @dual_sext_ne_with_slt_guard
; CHECK-NEXT: Loop %for.body: backedge-taken count is (-1 + (sext i8 %n to i64) + (-1 * (sext i8 %s to i64))<nsw>)
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i64 -1
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i64 -2
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (-1 + (sext i8 %n to i64) + (-1 * (sext i8 %s to i64))<nsw>)
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
Expand Down