diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp --- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -344,12 +344,17 @@ LLVM_DEBUG(dbgs() << "FnSpecialization: Replacing " << *V << "\nFnSpecialization: with " << *Const << "\n"); - V->replaceAllUsesWith(Const); - - for (auto *U : Const->users()) + // Record uses of V to avoid visiting irrelevant uses of const later. + SmallVector UseInsts; + for (auto *U : V->users()) if (auto *I = dyn_cast(U)) if (Solver.isBlockExecutable(I->getParent())) - Solver.visit(I); + UseInsts.push_back(I); + + V->replaceAllUsesWith(Const); + + for (auto *I : UseInsts) + Solver.visit(I); // Remove the instruction from Block and Solver. if (auto *I = dyn_cast(V)) {