@@ -923,10 +923,12 @@ static const SCEV *getExactSDiv(const SCEV *LHS, const SCEV *RHS,
923
923
// / If S involves the addition of a constant integer value, return that integer
924
924
// / value, and mutate S to point to a new SCEV with that value excluded.
925
925
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 ());
930
932
}
931
933
} else if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
932
934
SmallVector<const SCEV *, 8 > NewOps (Add->operands ());
@@ -942,14 +944,11 @@ static Immediate ExtractImmediate(const SCEV *&S, ScalarEvolution &SE) {
942
944
// FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
943
945
SCEV::FlagAnyWrap);
944
946
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 ());
953
952
}
954
953
return Immediate::getZero ();
955
954
}
@@ -1133,23 +1132,22 @@ static bool isHighCostExpansion(const SCEV *S,
1133
1132
return false ;
1134
1133
}
1135
1134
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;
1153
1151
}
1154
1152
}
1155
1153
}
@@ -3333,14 +3331,12 @@ static bool canFoldIVIncExpr(const SCEV *IncExpr, Instruction *UserInst,
3333
3331
IncOffset = Immediate::getFixed (IncConst->getValue ()->getSExtValue ());
3334
3332
} else {
3335
3333
// 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 )
3342
3338
return false ;
3343
- IncOffset = Immediate::getScalable (Scale-> getValue () ->getSExtValue ());
3339
+ IncOffset = Immediate::getScalable (C ->getSExtValue ());
3344
3340
}
3345
3341
3346
3342
if (!isAddressUse (TTI, UserInst, Operand))
@@ -3818,6 +3814,8 @@ static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
3818
3814
return nullptr ;
3819
3815
}
3820
3816
const SCEV *Start, *Step;
3817
+ const SCEVConstant *Op0;
3818
+ const SCEV *Op1;
3821
3819
if (match (S, m_scev_AffineAddRec (m_SCEV (Start), m_SCEV (Step)))) {
3822
3820
// Split a non-zero base out of an addrec.
3823
3821
if (Start->isZero ())
@@ -3839,19 +3837,13 @@ static const SCEV *CollectSubexprs(const SCEV *S, const SCEVConstant *C,
3839
3837
// FIXME: AR->getNoWrapFlags(SCEV::FlagNW)
3840
3838
SCEV::FlagAnyWrap);
3841
3839
}
3842
- } else if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S )) {
3840
+ } else if (match (S, m_scev_Mul ( m_SCEVConstant (Op0), m_SCEV (Op1)) )) {
3843
3841
// 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 ;
3855
3847
}
3856
3848
return S;
3857
3849
}
@@ -6478,13 +6470,10 @@ struct SCEVDbgValueBuilder {
6478
6470
// / Components of the expression are omitted if they are an identity function.
6479
6471
// / Chain (non-affine) SCEVs are not supported.
6480
6472
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" );
6488
6477
6489
6478
// Skip pushing arithmetic noops.
6490
6479
if (!isIdentityFunction (llvm::dwarf::DW_OP_mul, Stride)) {
@@ -6549,14 +6538,10 @@ struct SCEVDbgValueBuilder {
6549
6538
// / Components of the expression are omitted if they are an identity function.
6550
6539
bool SCEVToIterCountExpr (const llvm::SCEVAddRecExpr &SAR,
6551
6540
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" );
6560
6545
6561
6546
// Skip pushing arithmetic noops.
6562
6547
if (!isIdentityFunction (llvm::dwarf::DW_OP_minus, Start)) {
0 commit comments