Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -276,6 +276,7 @@ std::function GetTLI; SmallPtrSet SpecializedFuncs; + SmallVector ReplacedWithConstant; public: FunctionSpecializer(SCCPSolver &Solver, @@ -320,6 +321,12 @@ return Changed; } + void removeDeadInstructions() { + for (auto *I : ReplacedWithConstant) + I->eraseFromParent(); + ReplacedWithConstant.clear(); + } + bool tryToReplaceWithConstant(Value *V) { if (!V->getType()->isSingleValueType() || isa(V) || V->user_empty()) @@ -340,7 +347,7 @@ // Remove the instruction from Block and Solver. if (auto *I = dyn_cast(V)) { if (I->isSafeToRemove()) { - I->eraseFromParent(); + ReplacedWithConstant.push_back(I); Solver.removeLatticeValueFor(I); } } @@ -886,7 +893,8 @@ Changed = true; } - // Clean up the IR by removing ssa_copy intrinsics. + // Clean up the IR by removing dead instructions and ssa_copy intrinsics. + FS.removeDeadInstructions(); removeSSACopy(M); return Changed; }