diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -4410,24 +4410,24 @@ } /// 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 @@ } } - PushDefUseChildren(I, Worklist); + PushDefUseChildren(I, Worklist, Visited); } } @@ -7370,13 +7370,15 @@ } /// 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 @@ // 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 @@ } // 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 @@ ConstantEvolutionLoopExitValue.erase(PN); } - PushDefUseChildren(I, Worklist); + PushDefUseChildren(I, Worklist, Visited); } LoopPropertiesCache.erase(CurrL); @@ -7589,14 +7588,12 @@ // 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 @@ ConstantEvolutionLoopExitValue.erase(PN); } - PushDefUseChildren(I, Worklist); + PushDefUseChildren(I, Worklist, Visited); } }