Index: llvm/include/llvm/Analysis/ScalarEvolution.h =================================================================== --- llvm/include/llvm/Analysis/ScalarEvolution.h +++ llvm/include/llvm/Analysis/ScalarEvolution.h @@ -1886,8 +1886,8 @@ bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R, SCEV::NoWrapFlags &Flags); - /// Drop memoized information computed for S. - void forgetMemoizedResults(const SCEV *S); + /// Drop memoized information for all \p SCEVs. + void forgetMemoizedResults(ArrayRef SCEVs); void forgetMemoizedResultsImpl(const SCEV *S); Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -4447,6 +4447,7 @@ SmallPtrSet Visited; Visited.insert(PN); + SmallVector ToForget; while (!Worklist.empty()) { Instruction *I = Worklist.pop_back_val(); if (!Visited.insert(I).second) @@ -4472,12 +4473,13 @@ !isa(Old) || (I != PN && Old == SymName)) { eraseValueFromMap(It->first); - forgetMemoizedResults(Old); + ToForget.push_back(Old); } } PushDefUseChildren(I, Worklist); } + forgetMemoizedResults(ToForget); } namespace { @@ -7452,6 +7454,7 @@ PushLoopPHIs(L, Worklist); SmallPtrSet Discovered; + SmallVector ToForget; while (!Worklist.empty()) { Instruction *I = Worklist.pop_back_val(); @@ -7468,7 +7471,7 @@ // own when it gets to that point. if (!isa(I) || !isa(Old)) { eraseValueFromMap(It->first); - forgetMemoizedResults(Old); + ToForget.push_back(Old); } if (PHINode *PN = dyn_cast(I)) ConstantEvolutionLoopExitValue.erase(PN); @@ -7500,6 +7503,7 @@ Worklist.push_back(I); } } + forgetMemoizedResults(ToForget); } // Re-lookup the insert position, since the call to @@ -7536,6 +7540,7 @@ SmallVector LoopWorklist(1, L); SmallVector Worklist; SmallPtrSet Visited; + SmallVector ToForget; // Iterate over all the loops and sub-loops to drop SCEV information. while (!LoopWorklist.empty()) { @@ -7557,8 +7562,8 @@ auto LoopUsersItr = LoopUsers.find(CurrL); if (LoopUsersItr != LoopUsers.end()) { - for (auto *S : LoopUsersItr->second) - forgetMemoizedResults(S); + ToForget.insert(ToForget.end(), LoopUsersItr->second.begin(), + LoopUsersItr->second.end()); LoopUsers.erase(LoopUsersItr); } @@ -7574,7 +7579,7 @@ ValueExprMap.find_as(static_cast(I)); if (It != ValueExprMap.end()) { eraseValueFromMap(It->first); - forgetMemoizedResults(It->second); + ToForget.push_back(It->second); if (PHINode *PN = dyn_cast(I)) ConstantEvolutionLoopExitValue.erase(PN); } @@ -7587,6 +7592,8 @@ // ValuesAtScopes map. LoopWorklist.append(CurrL->begin(), CurrL->end()); } + + forgetMemoizedResults(ToForget); } void ScalarEvolution::forgetTopmostLoop(const Loop *L) { @@ -7604,6 +7611,7 @@ Worklist.push_back(I); SmallPtrSet Visited; + SmallVector ToForget; while (!Worklist.empty()) { I = Worklist.pop_back_val(); if (!Visited.insert(I).second) @@ -7613,13 +7621,14 @@ ValueExprMap.find_as(static_cast(I)); if (It != ValueExprMap.end()) { eraseValueFromMap(It->first); - forgetMemoizedResults(It->second); + ToForget.push_back(It->second); if (PHINode *PN = dyn_cast(I)) ConstantEvolutionLoopExitValue.erase(PN); } PushDefUseChildren(I, Worklist); } + forgetMemoizedResults(ToForget); } void ScalarEvolution::forgetLoopDispositions(const Loop *L) { @@ -12806,11 +12815,13 @@ return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; }); } -void ScalarEvolution::forgetMemoizedResults(const SCEV *S) { +void ScalarEvolution::forgetMemoizedResults(ArrayRef SCEVs) { SmallPtrSet Visited; SmallVector Worklist; - Visited.insert(S); - Worklist.push_back(S); + for (auto *S : SCEVs) + if (Visited.insert(S).second) + Worklist.push_back(S); + while (!Worklist.empty()) { const SCEV *Curr = Worklist.pop_back_val(); auto Users = SCEVUsers.find(Curr);