Skip to content

Commit 1d8731f

Browse files
committed
[SCEV] Add dedicated AffineAddRec matcher + loop matchers (NFC).
1 parent 0c42aef commit 1d8731f

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,48 @@ m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
196196
return m_scev_Binary<SCEVUDivExpr>(Op0, Op1);
197197
}
198198

199+
inline class_match<const Loop> m_Loop() { return class_match<const Loop>(); }
200+
201+
/// Match an affine SCEVAddRecExpr.
202+
template <typename Op0_t, typename Op1_t, typename Loop_t>
203+
struct SCEVAffineAddRec_match {
204+
SCEVBinaryExpr_match<SCEVAddRecExpr, Op0_t, Op1_t> Ops;
205+
Loop_t Loop;
206+
207+
SCEVAffineAddRec_match(Op0_t Op0, Op1_t Op1, Loop_t Loop)
208+
: Ops(Op0, Op1), Loop(Loop) {}
209+
210+
bool match(const SCEV *S) const {
211+
return Ops.match(S) && Loop.match(cast<SCEVAddRecExpr>(S)->getLoop());
212+
}
213+
};
214+
215+
/// Match a specified const Loop*.
216+
struct specificloop_ty {
217+
const Loop *L;
218+
219+
specificloop_ty(const Loop *L) : L(L) {}
220+
221+
bool match(const Loop *L) const { return L == this->L; }
222+
};
223+
224+
inline specificloop_ty m_SpecificLoop(const Loop *L) { return L; }
225+
226+
inline bind_ty<const Loop> m_Loop(const Loop *&L) { return L; }
227+
199228
template <typename Op0_t, typename Op1_t>
200-
inline SCEVBinaryExpr_match<SCEVAddRecExpr, Op0_t, Op1_t>
229+
inline SCEVAffineAddRec_match<Op0_t, Op1_t, class_match<const Loop>>
201230
m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1) {
202-
return m_scev_Binary<SCEVAddRecExpr>(Op0, Op1);
231+
return SCEVAffineAddRec_match<Op0_t, Op1_t, class_match<const Loop>>(
232+
Op0, Op1, m_Loop());
233+
}
234+
235+
template <typename Op0_t, typename Op1_t, typename Loop_t>
236+
inline SCEVAffineAddRec_match<Op0_t, Op1_t, Loop_t>
237+
m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1, const Loop_t &L) {
238+
return SCEVAffineAddRec_match<Op0_t, Op1_t, Loop_t>(Op0, Op1, L);
203239
}
240+
204241
} // namespace SCEVPatternMatch
205242
} // namespace llvm
206243

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12531,14 +12531,14 @@ static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
1253112531
return false;
1253212532

1253312533
const SCEV *LStart, *RStart, *Step;
12534-
if (!match(LHS, m_scev_AffineAddRec(m_SCEV(LStart), m_SCEV(Step))) ||
12535-
!match(RHS, m_scev_AffineAddRec(m_SCEV(RStart), m_scev_Specific(Step))))
12534+
const Loop *L;
12535+
if (!match(LHS,
12536+
m_scev_AffineAddRec(m_SCEV(LStart), m_SCEV(Step), m_Loop(L))) ||
12537+
!match(RHS, m_scev_AffineAddRec(m_SCEV(RStart), m_scev_Specific(Step),
12538+
m_SpecificLoop(L))))
1253612539
return false;
1253712540
const SCEVAddRecExpr *LAR = cast<SCEVAddRecExpr>(LHS);
1253812541
const SCEVAddRecExpr *RAR = cast<SCEVAddRecExpr>(RHS);
12539-
if (LAR->getLoop() != RAR->getLoop())
12540-
return false;
12541-
1254212542
SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ?
1254312543
SCEV::FlagNSW : SCEV::FlagNUW;
1254412544
if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,8 @@ static bool isLoopCounter(PHINode* Phi, Loop *L,
808808
return false;
809809

810810
const SCEV *S = SE->getSCEV(Phi);
811-
if (!match(S, m_scev_AffineAddRec(m_SCEV(), m_scev_One())) ||
811+
if (!match(S,
812+
m_scev_AffineAddRec(m_SCEV(), m_scev_One(), m_SpecificLoop(L))) ||
812813
cast<SCEVAddRecExpr>(S)->getLoop() != L)
813814
return false;
814815

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,14 @@ static void DoInitialMatch(const SCEV *S, Loop *L,
557557

558558
// Look at addrec operands.
559559
const SCEV *Start, *Step;
560-
if (match(S, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step))) &&
560+
const Loop *ARLoop;
561+
if (match(S,
562+
m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step), m_Loop(ARLoop))) &&
561563
!Start->isZero()) {
562564
DoInitialMatch(Start, L, Good, Bad, SE);
563565
DoInitialMatch(SE.getAddRecExpr(SE.getConstant(S->getType(), 0), Step,
564566
// FIXME: AR->getNoWrapFlags()
565-
cast<SCEVAddRecExpr>(S)->getLoop(),
566-
SCEV::FlagAnyWrap),
567+
ARLoop, SCEV::FlagAnyWrap),
567568
L, Good, Bad, SE);
568569
return;
569570
}

0 commit comments

Comments
 (0)