Skip to content

Commit 7c54c82

Browse files
authored
[SCEV] Use m_scev_Mul in a few more places. (NFC) (#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: #163364
1 parent 35cd291 commit 7c54c82

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;
@@ -284,14 +288,10 @@ template <typename Op0_t, typename Op1_t> struct SCEVURem_match {
284288
<< SE.getTypeSizeInBits(TruncTy));
285289
return Op0.match(LHS) && Op1.match(RHS);
286290
}
287-
const auto *Add = dyn_cast<SCEVAddExpr>(Expr);
288-
if (Add == nullptr || Add->getNumOperands() != 2)
289-
return false;
290-
291-
const SCEV *A = Add->getOperand(1);
292-
const auto *Mul = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
293291

294-
if (Mul == nullptr)
292+
const SCEV *A;
293+
const SCEVMulExpr *Mul;
294+
if (!SCEVPatternMatch::match(Expr, m_scev_Add(m_scev_Mul(Mul), m_SCEV(A))))
295295
return false;
296296

297297
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
@@ -4623,17 +4623,11 @@ const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
46234623

46244624
/// If Expr computes ~A, return A else return nullptr
46254625
static const SCEV *MatchNotExpr(const SCEV *Expr) {
4626-
const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
4627-
if (!Add || Add->getNumOperands() != 2 ||
4628-
!Add->getOperand(0)->isAllOnesValue())
4629-
return nullptr;
4630-
4631-
const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
4632-
if (!AddRHS || AddRHS->getNumOperands() != 2 ||
4633-
!AddRHS->getOperand(0)->isAllOnesValue())
4634-
return nullptr;
4635-
4636-
return AddRHS->getOperand(1);
4626+
const SCEV *MulOp;
4627+
if (match(Expr, m_scev_Add(m_scev_AllOnes(),
4628+
m_scev_Mul(m_scev_AllOnes(), m_SCEV(MulOp)))))
4629+
return MulOp;
4630+
return nullptr;
46374631
}
46384632

46394633
/// Return a SCEV corresponding to ~V = -1-V
@@ -12220,12 +12214,11 @@ ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
1222012214
// Try to match a common constant multiply.
1222112215
auto MatchConstMul =
1222212216
[](const SCEV *S) -> std::optional<std::pair<const SCEV *, APInt>> {
12223-
auto *M = dyn_cast<SCEVMulExpr>(S);
12224-
if (!M || M->getNumOperands() != 2 ||
12225-
!isa<SCEVConstant>(M->getOperand(0)))
12226-
return std::nullopt;
12227-
return {
12228-
{M->getOperand(1), cast<SCEVConstant>(M->getOperand(0))->getAPInt()}};
12217+
const APInt *C;
12218+
const SCEV *Op;
12219+
if (match(S, m_scev_Mul(m_scev_APInt(C), m_SCEV(Op))))
12220+
return {{Op, *C}};
12221+
return std::nullopt;
1222912222
};
1223012223
if (auto MatchedMore = MatchConstMul(More)) {
1223112224
if (auto MatchedLess = MatchConstMul(Less)) {

0 commit comments

Comments
 (0)