Skip to content

Commit 96dbb37

Browse files
committed
[SCEV] Use m_scev_Mul in a few more places. (NFC) (llvm#163364)
Add a new variant of m_scev_Mul that binds a SCEVMulExpr and use it in SCEVURem_match and also update 2 more places in ScalarEvolution.cpp that can use m_scev_Mul as well. PR: llvm#163364 (cherry picked from commit 7c54c82)
1 parent 711a060 commit 96dbb37

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ inline bind_ty<const SCEVAddExpr> m_scev_Add(const SCEVAddExpr *&V) {
9595
return V;
9696
}
9797

98+
inline bind_ty<const SCEVMulExpr> m_scev_Mul(const SCEVMulExpr *&V) {
99+
return V;
100+
}
101+
98102
/// Match a specified const SCEV *.
99103
struct specificscev_ty {
100104
const SCEV *Expr;
@@ -247,14 +251,10 @@ template <typename Op0_t, typename Op1_t> struct SCEVURem_match {
247251
<< SE.getTypeSizeInBits(TruncTy));
248252
return Op0.match(LHS) && Op1.match(RHS);
249253
}
250-
const auto *Add = dyn_cast<SCEVAddExpr>(Expr);
251-
if (Add == nullptr || Add->getNumOperands() != 2)
252-
return false;
253-
254-
const SCEV *A = Add->getOperand(1);
255-
const auto *Mul = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
256254

257-
if (Mul == nullptr)
255+
const SCEV *A;
256+
const SCEVMulExpr *Mul;
257+
if (!SCEVPatternMatch::match(Expr, m_scev_Add(m_scev_Mul(Mul), m_SCEV(A))))
258258
return false;
259259

260260
const auto MatchURemWithDivisor = [&](const SCEV *B) {

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,17 +4616,11 @@ const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
46164616

46174617
/// If Expr computes ~A, return A else return nullptr
46184618
static const SCEV *MatchNotExpr(const SCEV *Expr) {
4619-
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
4620-
if (!Add || Add->getNumOperands() != 2 ||
4621-
!Add->getOperand(0)->isAllOnesValue())
4622-
return nullptr;
4623-
4624-
const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
4625-
if (!AddRHS || AddRHS->getNumOperands() != 2 ||
4626-
!AddRHS->getOperand(0)->isAllOnesValue())
4627-
return nullptr;
4628-
4629-
return AddRHS->getOperand(1);
4619+
const SCEV *MulOp;
4620+
if (match(Expr, m_scev_Add(m_scev_AllOnes(),
4621+
m_scev_Mul(m_scev_AllOnes(), m_SCEV(MulOp)))))
4622+
return MulOp;
4623+
return nullptr;
46304624
}
46314625

46324626
/// Return a SCEV corresponding to ~V = -1-V
@@ -12186,12 +12180,11 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
1218612180
// Try to match a common constant multiply.
1218712181
auto MatchConstMul =
1218812182
[](const SCEV *S) -> std::optional<std::pair<const SCEV *, APInt>> {
12189-
auto *M = dyn_cast<SCEVMulExpr>(S);
12190-
if (!M || M->getNumOperands() != 2 ||
12191-
!isa<SCEVConstant>(M->getOperand(0)))
12192-
return std::nullopt;
12193-
return {
12194-
{M->getOperand(1), cast<SCEVConstant>(M->getOperand(0))->getAPInt()}};
12183+
const APInt *C;
12184+
const SCEV *Op;
12185+
if (match(S, m_scev_Mul(m_scev_APInt(C), m_SCEV(Op))))
12186+
return {{Op, *C}};
12187+
return std::nullopt;
1219512188
};
1219612189
if (auto MatchedMore = MatchConstMul(More)) {
1219712190
if (auto MatchedLess = MatchConstMul(Less)) {

0 commit comments

Comments
 (0)