Skip to content

Commit ab9b23c

Browse files
committed
[SCEV] Use pattern match to check ZExt(Add()). (NFC)
Follow-up to #151227 (review) to check the inner expression is an Add before calling getTruncateExpr. Adds a new matcher that just matches and captures SCEVAddExpr, to support matching a SCEVAddExpr with arbitrary number of operands.
1 parent 2c00df3 commit ab9b23c

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ inline bind_ty<const SCEVUnknown> m_SCEVUnknown(const SCEVUnknown *&V) {
9191
return V;
9292
}
9393

94+
inline bind_ty<const SCEVAddExpr> m_scev_Add(const SCEVAddExpr *&V) {
95+
return V;
96+
}
97+
9498
/// Match a specified const SCEV *.
9599
struct specificscev_ty {
96100
const SCEV *Expr;

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,16 +2685,15 @@ const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
26852685

26862686
// Try to push the constant operand into a ZExt: A + zext (-A + B) -> zext
26872687
// (B), if trunc (A) + -A + B does not unsigned-wrap.
2688-
if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Ops[1])) {
2689-
const SCEV *B = ZExt->getOperand(0);
2690-
const SCEV *NarrowA = getTruncateExpr(A, B->getType());
2691-
if (isa<SCEVAddExpr>(B) &&
2692-
NarrowA == getNegativeSCEV(cast<SCEVAddExpr>(B)->getOperand(0)) &&
2693-
getZeroExtendExpr(NarrowA, ZExt->getType()) == A &&
2694-
hasFlags(StrengthenNoWrapFlags(this, scAddExpr, {NarrowA, B},
2688+
const SCEVAddExpr *InnerAdd;
2689+
if (match(B, m_scev_ZExt(m_scev_Add(InnerAdd)))) {
2690+
const SCEV *NarrowA = getTruncateExpr(A, InnerAdd->getType());
2691+
if (NarrowA == getNegativeSCEV(InnerAdd->getOperand(0)) &&
2692+
getZeroExtendExpr(NarrowA, B->getType()) == A &&
2693+
hasFlags(StrengthenNoWrapFlags(this, scAddExpr, {NarrowA, InnerAdd},
26952694
SCEV::FlagAnyWrap),
26962695
SCEV::FlagNUW)) {
2697-
return getZeroExtendExpr(getAddExpr(NarrowA, B), ZExt->getType());
2696+
return getZeroExtendExpr(getAddExpr(NarrowA, InnerAdd), B->getType());
26982697
}
26992698
}
27002699
}

0 commit comments

Comments
 (0)