Skip to content

Commit 0074f5d

Browse files
committed
[LSR] Clean up code using SCEVPatternMatch (NFC)
1 parent fba63e3 commit 0074f5d

File tree

1 file changed

+48
-63
lines changed

1 file changed

+48
-63
lines changed

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,12 @@ 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+
const SCEV *Op1;
928+
if (match(S, m_scev_APInt(C))) {
929+
if (C->getSignificantBits() <= 64) {
930+
S = SE.getConstant(S->getType(), 0);
931+
return Immediate::getFixed(C->getSExtValue());
930932
}
931933
} else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
932934
SmallVector<const SCEV *, 8> NewOps(Add->operands());
@@ -942,14 +944,11 @@ static Immediate ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
942944
// FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
943945
SCEV::FlagAnyWrap);
944946
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-
}
947+
} else if (EnableVScaleImmediates &&
948+
match(S, m_scev_Mul(m_scev_APInt(C), m_SCEV(Op1))) &&
949+
isa<SCEVVScale>(Op1)) {
950+
S = SE.getConstant(S->getType(), 0);
951+
return Immediate::getScalable(C->getSExtValue());
953952
}
954953
return Immediate::getZero();
955954
}
@@ -1133,23 +1132,22 @@ static bool isHighCostExpansion(const SCEV *S,
11331132
return false;
11341133
}
11351134

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-
}
1135+
const SCEV *Op0, *Op1;
1136+
if (match(S, m_scev_Mul(m_SCEV(Op0), m_SCEV(Op1)))) {
1137+
// Multiplication by a constant is ok
1138+
if (isa<SCEVConstant>(Op0))
1139+
return isHighCostExpansion(Op1, Processed, SE);
1140+
1141+
// If we have the value of one operand, check if an existing
1142+
// multiplication already generates this expression.
1143+
if (const auto *U = dyn_cast<SCEVUnknown>(Op1)) {
1144+
Value *UVal = U->getValue();
1145+
for (User *UR : UVal->users()) {
1146+
// If U is a constant, it may be used by a ConstantExpr.
1147+
Instruction *UI = dyn_cast<Instruction>(UR);
1148+
if (UI && UI->getOpcode() == Instruction::Mul &&
1149+
SE.isSCEVable(UI->getType())) {
1150+
return SE.getSCEV(UI) == S;
11531151
}
11541152
}
11551153
}
@@ -3333,14 +3331,12 @@ static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
33333331
IncOffset = Immediate::getFixed(IncConst->getValue()->getSExtValue());
33343332
} else {
33353333
// 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)))
3339-
return false;
3340-
auto *Scale = dyn_cast<SCEVConstant>(IncVScale->getOperand(0));
3341-
if (!Scale || Scale->getType()->getScalarSizeInBits() > 64)
3334+
const APInt *C;
3335+
const SCEV *Op1;
3336+
if (!match(IncExpr, m_scev_Mul(m_scev_APInt(C), m_SCEV(Op1))) ||
3337+
!isa<SCEVVScale>(Op1) || C->getSignificantBits() > 64)
33423338
return false;
3343-
IncOffset = Immediate::getScalable(Scale->getValue()->getSExtValue());
3339+
IncOffset = Immediate::getScalable(C->getSExtValue());
33443340
}
33453341

33463342
if (!isAddressUse(TTI, UserInst, Operand))
@@ -3818,6 +3814,8 @@ static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
38183814
return nullptr;
38193815
}
38203816
const SCEV *Start, *Step;
3817+
const SCEVConstant *Op0;
3818+
const SCEV *Op1;
38213819
if (match(S, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step)))) {
38223820
// Split a non-zero base out of an addrec.
38233821
if (Start->isZero())
@@ -3839,19 +3837,13 @@ static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
38393837
// FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
38403838
SCEV::FlagAnyWrap);
38413839
}
3842-
} else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3840+
} else if (match(S, m_scev_Mul(m_SCEVConstant(Op0), m_SCEV(Op1)))) {
38433841
// 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-
}
3842+
C = C ? cast<SCEVConstant>(SE.getMulExpr(C, Op0)) : Op0;
3843+
const SCEV *Remainder = CollectSubexprs(Op1, C, Ops, L, SE, Depth + 1);
3844+
if (Remainder)
3845+
Ops.push_back(SE.getMulExpr(C, Remainder));
3846+
return nullptr;
38553847
}
38563848
return S;
38573849
}
@@ -6478,13 +6470,10 @@ struct SCEVDbgValueBuilder {
64786470
/// Components of the expression are omitted if they are an identity function.
64796471
/// Chain (non-affine) SCEVs are not supported.
64806472
bool SCEVToValueExpr(const llvm::SCEVAddRecExpr &SAR, ScalarEvolution &SE) {
6481-
assert(SAR.isAffine() && "Expected affine SCEV");
6482-
// TODO: Is this check needed?
6483-
if (isa<SCEVAddRecExpr>(SAR.getStart()))
6484-
return false;
6485-
6486-
const SCEV *Start = SAR.getStart();
6487-
const SCEV *Stride = SAR.getStepRecurrence(SE);
6473+
const SCEV *Start, *Stride;
6474+
[[maybe_unused]] bool Match =
6475+
match(&SAR, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Stride)));
6476+
assert(Match && "Expected affine SCEV");
64886477

64896478
// Skip pushing arithmetic noops.
64906479
if (!isIdentityFunction(llvm::dwarf::DW_OP_mul, Stride)) {
@@ -6549,14 +6538,10 @@ struct SCEVDbgValueBuilder {
65496538
/// Components of the expression are omitted if they are an identity function.
65506539
bool SCEVToIterCountExpr(const llvm::SCEVAddRecExpr &SAR,
65516540
ScalarEvolution &SE) {
6552-
assert(SAR.isAffine() && "Expected affine SCEV");
6553-
if (isa<SCEVAddRecExpr>(SAR.getStart())) {
6554-
LLVM_DEBUG(dbgs() << "scev-salvage: IV SCEV. Unsupported nested AddRec: "
6555-
<< SAR << '\n');
6556-
return false;
6557-
}
6558-
const SCEV *Start = SAR.getStart();
6559-
const SCEV *Stride = SAR.getStepRecurrence(SE);
6541+
const SCEV *Start, *Stride;
6542+
[[maybe_unused]] bool Match =
6543+
match(&SAR, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Stride)));
6544+
assert(Match && "Expected affine SCEV");
65606545

65616546
// Skip pushing arithmetic noops.
65626547
if (!isIdentityFunction(llvm::dwarf::DW_OP_minus, Start)) {

0 commit comments

Comments
 (0)