diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp index ed9a0be6981fa..15e7172c6ce12 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -60,17 +60,18 @@ bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) { SmallVector Stack; SmallPtrSet Visited; Stack.push_back(&PN); + Visited.insert(&PN); while (!Stack.empty()) { PHINode *Phi = Stack.pop_back_val(); - if (!Visited.insert(Phi).second) - continue; - // Early stop if the set of PHIs is large - if (Visited.size() == 16) - return false; for (User *Use : Phi->users()) { - if (PHINode *PhiUse = dyn_cast(Use)) + if (PHINode *PhiUse = dyn_cast(Use)) { + if (!Visited.insert(PhiUse).second) + continue; + // Early stop if the set of PHIs is large + if (Visited.size() >= 16) + return false; Stack.push_back(PhiUse); - else + } else return false; } }