@@ -4437,8 +4437,7 @@ const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
4437
4437
return getGEPExpr(GEP, IndexExprs);
4438
4438
}
4439
4439
4440
- uint32_t
4441
- ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
4440
+ uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
4442
4441
if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
4443
4442
return C->getAPInt().countTrailingZeros();
4444
4443
@@ -4448,14 +4447,16 @@ ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
4448
4447
4449
4448
if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
4450
4449
uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
4451
- return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
4452
- getTypeSizeInBits(E->getType()) : OpRes;
4450
+ return OpRes == getTypeSizeInBits(E->getOperand()->getType())
4451
+ ? getTypeSizeInBits(E->getType())
4452
+ : OpRes;
4453
4453
}
4454
4454
4455
4455
if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
4456
4456
uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
4457
- return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
4458
- getTypeSizeInBits(E->getType()) : OpRes;
4457
+ return OpRes == getTypeSizeInBits(E->getOperand()->getType())
4458
+ ? getTypeSizeInBits(E->getType())
4459
+ : OpRes;
4459
4460
}
4460
4461
4461
4462
if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
@@ -4472,8 +4473,8 @@ ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
4472
4473
uint32_t BitWidth = getTypeSizeInBits(M->getType());
4473
4474
for (unsigned i = 1, e = M->getNumOperands();
4474
4475
SumOpRes != BitWidth && i != e; ++i)
4475
- SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
4476
- BitWidth);
4476
+ SumOpRes =
4477
+ std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
4477
4478
return SumOpRes;
4478
4479
}
4479
4480
@@ -4514,6 +4515,17 @@ ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
4514
4515
return 0;
4515
4516
}
4516
4517
4518
+ uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
4519
+ auto I = MinTrailingZerosCache.find(S);
4520
+ if (I != MinTrailingZerosCache.end())
4521
+ return I->second;
4522
+
4523
+ uint32_t Result = GetMinTrailingZerosImpl(S);
4524
+ auto InsertPair = MinTrailingZerosCache.insert({S, Result});
4525
+ assert(InsertPair.second && "Should insert a new key");
4526
+ return InsertPair.first->second;
4527
+ }
4528
+
4517
4529
/// Helper method to assign a range to V from metadata present in the IR.
4518
4530
static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
4519
4531
if (Instruction *I = dyn_cast<Instruction>(V))
@@ -9510,6 +9522,7 @@ ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
9510
9522
ValueExprMap(std::move(Arg.ValueExprMap)),
9511
9523
PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
9512
9524
WalkingBEDominatingConds(false), ProvingSplitPredicate(false),
9525
+ MinTrailingZerosCache(std::move(Arg.MinTrailingZerosCache)),
9513
9526
BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
9514
9527
PredicatedBackedgeTakenCounts(
9515
9528
std::move(Arg.PredicatedBackedgeTakenCounts)),
@@ -9915,6 +9928,7 @@ void ScalarEvolution::forgetMemoizedResults(const SCEV *S) {
9915
9928
SignedRanges.erase(S);
9916
9929
ExprValueMap.erase(S);
9917
9930
HasRecMap.erase(S);
9931
+ MinTrailingZerosCache.erase(S);
9918
9932
9919
9933
auto RemoveSCEVFromBackedgeMap =
9920
9934
[S, this](DenseMap<const Loop *, BackedgeTakenInfo> &Map) {
0 commit comments