From 1202d280c6d96d6ad85d7e3d9f0968a7331f4322 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Fri, 15 Oct 2021 10:19:15 +0700 Subject: [PATCH] [SCEV][NFC] Reduce memory footprint & compile time via DFS refactoring Current implementations of DFS in SCEV check unique-visited of traversed values on pop, and not on push. As result, the same value may be pushed multiple times just to be thrown away when popped. These operations are meaningless and only waste time and increase memory footprint of the worklist. This patch reworks the DFS strategy to check uniqueness before push. Should be NFC. Differential Revision: https://reviews.llvm.org/D111774 Reviewed By: nikic, reames --- llvm/lib/Analysis/ScalarEvolution.cpp | 45 ++++++++++++++++------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index ec55c92..a0faf7d 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -4410,24 +4410,24 @@ const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) { } /// Push users of the given Instruction onto the given Worklist. -static void -PushDefUseChildren(Instruction *I, - SmallVectorImpl &Worklist) { +static void PushDefUseChildren(Instruction *I, + SmallVectorImpl &Worklist, + SmallPtrSetImpl &Visited) { // Push the def-use children onto the Worklist stack. - for (User *U : I->users()) - Worklist.push_back(cast(U)); + for (User *U : I->users()) { + auto *UserInsn = cast(U); + if (Visited.insert(UserInsn).second) + Worklist.push_back(UserInsn); + } } void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) { SmallVector Worklist; - PushDefUseChildren(PN, Worklist); - SmallPtrSet Visited; Visited.insert(PN); + Worklist.push_back(PN); while (!Worklist.empty()) { Instruction *I = Worklist.pop_back_val(); - if (!Visited.insert(I).second) - continue; auto It = ValueExprMap.find_as(static_cast(I)); if (It != ValueExprMap.end()) { @@ -4453,7 +4453,7 @@ void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) { } } - PushDefUseChildren(I, Worklist); + PushDefUseChildren(I, Worklist, Visited); } } @@ -7370,13 +7370,15 @@ 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) { +static void PushLoopPHIs(const Loop *L, + SmallVectorImpl &Worklist, + SmallPtrSetImpl &Visited) { BasicBlock *Header = L->getHeader(); // Push all Loop-header PHIs onto the Worklist stack. for (PHINode &PN : Header->phis()) - Worklist.push_back(&PN); + if (Visited.insert(&PN).second) + Worklist.push_back(&PN); } const ScalarEvolution::BackedgeTakenInfo & @@ -7437,9 +7439,8 @@ ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { // it handles SCEVUnknown PHI nodes specially. if (Result.hasAnyInfo()) { SmallVector Worklist; - PushLoopPHIs(L, Worklist); - SmallPtrSet Discovered; + PushLoopPHIs(L, Worklist, Discovered); while (!Worklist.empty()) { Instruction *I = Worklist.pop_back_val(); @@ -7551,12 +7552,10 @@ void ScalarEvolution::forgetLoop(const Loop *L) { } // Drop information about expressions based on loop-header PHIs. - PushLoopPHIs(CurrL, Worklist); + PushLoopPHIs(CurrL, Worklist, Visited); while (!Worklist.empty()) { Instruction *I = Worklist.pop_back_val(); - if (!Visited.insert(I).second) - continue; ValueExprMapType::iterator It = ValueExprMap.find_as(static_cast(I)); @@ -7567,7 +7566,7 @@ void ScalarEvolution::forgetLoop(const Loop *L) { ConstantEvolutionLoopExitValue.erase(PN); } - PushDefUseChildren(I, Worklist); + PushDefUseChildren(I, Worklist, Visited); } LoopPropertiesCache.erase(CurrL); @@ -7589,14 +7588,12 @@ void ScalarEvolution::forgetValue(Value *V) { // Drop information about expressions based on loop-header PHIs. SmallVector Worklist; + SmallPtrSet Visited; Worklist.push_back(I); + Visited.insert(I); - SmallPtrSet Visited; while (!Worklist.empty()) { I = Worklist.pop_back_val(); - if (!Visited.insert(I).second) - continue; - ValueExprMapType::iterator It = ValueExprMap.find_as(static_cast(I)); if (It != ValueExprMap.end()) { @@ -7606,7 +7603,7 @@ void ScalarEvolution::forgetValue(Value *V) { ConstantEvolutionLoopExitValue.erase(PN); } - PushDefUseChildren(I, Worklist); + PushDefUseChildren(I, Worklist, Visited); } } -- 2.7.4