diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h index 8407be99a82b1..5f06c2926f06d 100644 --- a/llvm/include/llvm/Analysis/ScalarEvolution.h +++ b/llvm/include/llvm/Analysis/ScalarEvolution.h @@ -2053,6 +2053,12 @@ class ScalarEvolution { std::tuple findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef Ops); + /// Push PHI nodes in the header of the given loop onto the given Worklist + /// if they have a cached SCEV expression. If no expression is cached, then + /// there also aren't any dependent expressions to invalidate. + void pushCachedLoopPHIs(const Loop *L, + SmallVectorImpl &Worklist) const; + FoldingSet UniqueSCEVs; FoldingSet UniquePreds; BumpPtrAllocator SCEVAllocator; diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 4630c5562623c..446e5e3651a78 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -7020,13 +7020,16 @@ bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) { } /// Push PHI nodes in the header of the given loop onto the given Worklist. -static void -PushLoopPHIs(const Loop *L, SmallVectorImpl &Worklist) { +/// Only push PHIs for which a SCEV expression has been cached, otherwise there +/// cannot be any dependent expressions to invalidate. +void ScalarEvolution::pushCachedLoopPHIs( + const Loop *L, SmallVectorImpl &Worklist) const { BasicBlock *Header = L->getHeader(); - - // Push all Loop-header PHIs onto the Worklist stack. - for (PHINode &PN : Header->phis()) - Worklist.push_back(&PN); + for (PHINode &PN : Header->phis()) { + auto It = ValueExprMap.find_as(static_cast(&PN)); + if (It != ValueExprMap.end()) + Worklist.push_back(&PN); + } } const ScalarEvolution::BackedgeTakenInfo & @@ -7087,7 +7090,7 @@ ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { // it handles SCEVUnknown PHI nodes specially. if (Result.hasAnyInfo()) { SmallVector Worklist; - PushLoopPHIs(L, Worklist); + pushCachedLoopPHIs(L, Worklist); SmallPtrSet Discovered; while (!Worklist.empty()) { @@ -7210,7 +7213,7 @@ void ScalarEvolution::forgetLoop(const Loop *L) { } // Drop information about expressions based on loop-header PHIs. - PushLoopPHIs(CurrL, Worklist); + pushCachedLoopPHIs(CurrL, Worklist); while (!Worklist.empty()) { Instruction *I = Worklist.pop_back_val();