Index: llvm/trunk/include/llvm/Analysis/ScalarEvolution.h =================================================================== --- llvm/trunk/include/llvm/Analysis/ScalarEvolution.h +++ llvm/trunk/include/llvm/Analysis/ScalarEvolution.h @@ -1769,6 +1769,11 @@ const SCEV *getOrCreateMulExpr(SmallVectorImpl &Ops, SCEV::NoWrapFlags Flags); + /// Find all of the loops transitively used in \p S, and fill \p LoopsUsed. + /// A loop is considered "used" by an expression if it contains + /// an add rec on said loop. + void getUsedLoops(const SCEV *S, SmallPtrSetImpl &LoopsUsed); + /// Find all of the loops transitively used in \p S, and update \c LoopUsers /// accordingly. void addToLoopUseLists(const SCEV *S); Index: llvm/trunk/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/trunk/lib/Analysis/ScalarEvolution.cpp +++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp @@ -11341,9 +11341,13 @@ RemoveSCEVFromBackedgeMap(PredicatedBackedgeTakenCounts); } -void ScalarEvolution::addToLoopUseLists(const SCEV *S) { +void +ScalarEvolution::getUsedLoops(const SCEV *S, + SmallPtrSetImpl &LoopsUsed) { struct FindUsedLoops { - SmallPtrSet LoopsUsed; + FindUsedLoops(SmallPtrSetImpl &LoopsUsed) + : LoopsUsed(LoopsUsed) {} + SmallPtrSetImpl &LoopsUsed; bool follow(const SCEV *S) { if (auto *AR = dyn_cast(S)) LoopsUsed.insert(AR->getLoop()); @@ -11353,10 +11357,14 @@ bool isDone() const { return false; } }; - FindUsedLoops F; + FindUsedLoops F(LoopsUsed); SCEVTraversal(F).visitAll(S); +} - for (auto *L : F.LoopsUsed) +void ScalarEvolution::addToLoopUseLists(const SCEV *S) { + SmallPtrSet LoopsUsed; + getUsedLoops(S, LoopsUsed); + for (auto *L : LoopsUsed) LoopUsers[L].push_back(S); }