Skip to content

Commit 04cd0f2

Browse files
authored
[LSR] Clean up code using SCEVPatternMatch (NFC) (#145556)
1 parent 7ff9669 commit 04cd0f2

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

llvm/include/llvm/Analysis/ScalarEvolutionPatternMatch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ inline class_match<const SCEV> m_SCEV() { return class_match<const SCEV>(); }
6464
inline class_match<const SCEVConstant> m_SCEVConstant() {
6565
return class_match<const SCEVConstant>();
6666
}
67+
inline class_match<const SCEVVScale> m_SCEVVScale() {
68+
return class_match<const SCEVVScale>();
69+
}
6770

6871
template <typename Class> struct bind_ty {
6972
Class *&VR;

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,11 @@ static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
923923
/// If S involves the addition of a constant integer value, return that integer
924924
/// value, and mutate S to point to a new SCEV with that value excluded.
925925
static Immediate ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
926-
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
927-
if (C->getAPInt().getSignificantBits() <= 64) {
928-
S = SE.getConstant(C->getType(), 0);
929-
return Immediate::getFixed(C->getValue()->getSExtValue());
926+
const APInt *C;
927+
if (match(S, m_scev_APInt(C))) {
928+
if (C->getSignificantBits() <= 64) {
929+
S = SE.getConstant(S->getType(), 0);
930+
return Immediate::getFixed(C->getSExtValue());
930931
}
931932
} else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
932933
SmallVector<const SCEV *, 8> NewOps(Add->operands());
@@ -942,14 +943,10 @@ static Immediate ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
942943
// FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
943944
SCEV::FlagAnyWrap);
944945
return Result;
945-
} else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
946-
if (EnableVScaleImmediates && M->getNumOperands() == 2) {
947-
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
948-
if (isa<SCEVVScale>(M->getOperand(1))) {
949-
S = SE.getConstant(M->getType(), 0);
950-
return Immediate::getScalable(C->getValue()->getSExtValue());
951-
}
952-
}
946+
} else if (EnableVScaleImmediates &&
947+
match(S, m_scev_Mul(m_scev_APInt(C), m_SCEVVScale()))) {
948+
S = SE.getConstant(S->getType(), 0);
949+
return Immediate::getScalable(C->getSExtValue());
953950
}
954951
return Immediate::getZero();
955952
}
@@ -1133,23 +1130,22 @@ static bool isHighCostExpansion(const SCEV *S,
11331130
return false;
11341131
}
11351132

1136-
if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
1137-
if (Mul->getNumOperands() == 2) {
1138-
// Multiplication by a constant is ok
1139-
if (isa<SCEVConstant>(Mul->getOperand(0)))
1140-
return isHighCostExpansion(Mul->getOperand(1), Processed, SE);
1141-
1142-
// If we have the value of one operand, check if an existing
1143-
// multiplication already generates this expression.
1144-
if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Mul->getOperand(1))) {
1145-
Value *UVal = U->getValue();
1146-
for (User *UR : UVal->users()) {
1147-
// If U is a constant, it may be used by a ConstantExpr.
1148-
Instruction *UI = dyn_cast<Instruction>(UR);
1149-
if (UI && UI->getOpcode() == Instruction::Mul &&
1150-
SE.isSCEVable(UI->getType())) {
1151-
return SE.getSCEV(UI) == Mul;
1152-
}
1133+
const SCEV *Op0, *Op1;
1134+
if (match(S, m_scev_Mul(m_SCEV(Op0), m_SCEV(Op1)))) {
1135+
// Multiplication by a constant is ok
1136+
if (isa<SCEVConstant>(Op0))
1137+
return isHighCostExpansion(Op1, Processed, SE);
1138+
1139+
// If we have the value of one operand, check if an existing
1140+
// multiplication already generates this expression.
1141+
if (const auto *U = dyn_cast<SCEVUnknown>(Op1)) {
1142+
Value *UVal = U->getValue();
1143+
for (User *UR : UVal->users()) {
1144+
// If U is a constant, it may be used by a ConstantExpr.
1145+
Instruction *UI = dyn_cast<Instruction>(UR);
1146+
if (UI && UI->getOpcode() == Instruction::Mul &&
1147+
SE.isSCEVable(UI->getType())) {
1148+
return SE.getSCEV(UI) == S;
11531149
}
11541150
}
11551151
}
@@ -3333,14 +3329,11 @@ static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
33333329
IncOffset = Immediate::getFixed(IncConst->getValue()->getSExtValue());
33343330
} else {
33353331
// Look for mul(vscale, constant), to detect a scalable offset.
3336-
auto *IncVScale = dyn_cast<SCEVMulExpr>(IncExpr);
3337-
if (!IncVScale || IncVScale->getNumOperands() != 2 ||
3338-
!isa<SCEVVScale>(IncVScale->getOperand(1)))
3332+
const APInt *C;
3333+
if (!match(IncExpr, m_scev_Mul(m_scev_APInt(C), m_SCEVVScale())) ||
3334+
C->getSignificantBits() > 64)
33393335
return false;
3340-
auto *Scale = dyn_cast<SCEVConstant>(IncVScale->getOperand(0));
3341-
if (!Scale || Scale->getType()->getScalarSizeInBits() > 64)
3342-
return false;
3343-
IncOffset = Immediate::getScalable(Scale->getValue()->getSExtValue());
3336+
IncOffset = Immediate::getScalable(C->getSExtValue());
33443337
}
33453338

33463339
if (!isAddressUse(TTI, UserInst, Operand))
@@ -3818,6 +3811,8 @@ static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
38183811
return nullptr;
38193812
}
38203813
const SCEV *Start, *Step;
3814+
const SCEVConstant *Op0;
3815+
const SCEV *Op1;
38213816
if (match(S, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step)))) {
38223817
// Split a non-zero base out of an addrec.
38233818
if (Start->isZero())
@@ -3839,19 +3834,13 @@ static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
38393834
// FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
38403835
SCEV::FlagAnyWrap);
38413836
}
3842-
} else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3837+
} else if (match(S, m_scev_Mul(m_SCEVConstant(Op0), m_SCEV(Op1)))) {
38433838
// Break (C * (a + b + c)) into C*a + C*b + C*c.
3844-
if (Mul->getNumOperands() != 2)
3845-
return S;
3846-
if (const SCEVConstant *Op0 =
3847-
dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3848-
C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0;
3849-
const SCEV *Remainder =
3850-
CollectSubexprs(Mul->getOperand(1), C, Ops, L, SE, Depth+1);
3851-
if (Remainder)
3852-
Ops.push_back(SE.getMulExpr(C, Remainder));
3853-
return nullptr;
3854-
}
3839+
C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0;
3840+
const SCEV *Remainder = CollectSubexprs(Op1, C, Ops, L, SE, Depth + 1);
3841+
if (Remainder)
3842+
Ops.push_back(SE.getMulExpr(C, Remainder));
3843+
return nullptr;
38553844
}
38563845
return S;
38573846
}

0 commit comments

Comments
 (0)