From a79b75d19249d15c09fcd16c72aa10f747e03cd7 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 10 Apr 2021 23:50:23 +0200 Subject: [PATCH] [SCEV] Don't walk uses of phis without SCEV expression when forgetting I've run into some cases where a large fraction of compile-time is spent invalidating SCEV. One of the causes is forgetLoop(), which walks all values that are def-use reachable from the loop header phis. When invalidating a topmost loop, that might be close to all values in a function. Additionally, it's fairly common for there to not actually be anything to invalidate, but we'll still be performing this walk again and again. My first thought was that we don't need to continue walking the uses if the current value doesn't have a SCEV expression. However, this isn't quite right, because SCEV construction can skip over values (e.g. for a chain of adds, we might only create a SCEV expression for the final value). What this patch does instead is to only walk the (full) def-use chain of loop phis that have a SCEV expression. If there's no expression for a phi, then we also don't have any dependent expressions to invalidate. Differential Revision: https://reviews.llvm.org/D100264 --- include/llvm/Analysis/ScalarEvolution.h | 6 ++++++ lib/Analysis/ScalarEvolution.cpp | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/include/llvm/Analysis/ScalarEvolution.h b/include/llvm/Analysis/ScalarEvolution.h index 8407be99a82..5f06c2926f0 100644 --- a/include/llvm/Analysis/ScalarEvolution.h +++ b/include/llvm/Analysis/ScalarEvolution.h @@ -2053,6 +2053,12 @@ private: 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/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index 4630c556262..446e5e3651a 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/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();