7979#include "llvm/Analysis/LoopInfo.h"
8080#include "llvm/Analysis/MemoryBuiltins.h"
8181#include "llvm/Analysis/ScalarEvolutionExpressions.h"
82+ #include "llvm/Analysis/ScalarEvolutionPatternMatch.h"
8283#include "llvm/Analysis/TargetLibraryInfo.h"
8384#include "llvm/Analysis/ValueTracking.h"
8485#include "llvm/Config/llvm-config.h"
133134
134135using namespace llvm;
135136using namespace PatternMatch;
137+ using namespace SCEVPatternMatch;
136138
137139#define DEBUG_TYPE "scalar-evolution"
138140
@@ -3423,9 +3425,8 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
34233425 return S;
34243426
34253427 // 0 udiv Y == 0
3426- if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS))
3427- if (LHSC->getValue()->isZero())
3428- return LHS;
3428+ if (match(LHS, m_scev_Zero()))
3429+ return LHS;
34293430
34303431 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
34313432 if (RHSC->getValue()->isOne())
@@ -10593,7 +10594,6 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1059310594 // Get the initial value for the loop.
1059410595 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
1059510596 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
10596- const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
1059710597
1059810598 if (!isLoopInvariant(Step, L))
1059910599 return getCouldNotCompute();
@@ -10615,8 +10615,8 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1061510615 // Handle unitary steps, which cannot wraparound.
1061610616 // 1*N = -Start; -1*N = Start (mod 2^BW), so:
1061710617 // N = Distance (as unsigned)
10618- if (StepC &&
10619- (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne( ))) {
10618+
10619+ if (match(Step, m_CombineOr(m_scev_One(), m_scev_MinusOne() ))) {
1062010620 APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, Guards));
1062110621 MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance));
1062210622
@@ -10668,6 +10668,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
1066810668 }
1066910669
1067010670 // Solve the general equation.
10671+ const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
1067110672 if (!StepC || StepC->getValue()->isZero())
1067210673 return getCouldNotCompute();
1067310674 const SCEV *E = SolveLinEquationWithOverflow(
0 commit comments