diff --git a/llvm/lib/CodeGen/TypePromotion.cpp b/llvm/lib/CodeGen/TypePromotion.cpp --- a/llvm/lib/CodeGen/TypePromotion.cpp +++ b/llvm/lib/CodeGen/TypePromotion.cpp @@ -106,9 +106,9 @@ SetVector &Sources; SetVector &Sinks; SmallPtrSetImpl &SafeWrap; + SmallPtrSetImpl &InstsToRemove; IntegerType *ExtTy = nullptr; SmallPtrSet NewInsts; - SmallPtrSet InstsToRemove; DenseMap> TruncTysMap; SmallPtrSet Promoted; @@ -120,12 +120,12 @@ void Cleanup(); public: - IRPromoter(LLVMContext &C, unsigned Width, - SetVector &visited, SetVector &sources, - SetVector &sinks, - SmallPtrSetImpl &wrap) - : Ctx(C), PromotedWidth(Width), Visited(visited), - Sources(sources), Sinks(sinks), SafeWrap(wrap) { + IRPromoter(LLVMContext &C, unsigned Width, SetVector &visited, + SetVector &sources, SetVector &sinks, + SmallPtrSetImpl &wrap, + SmallPtrSetImpl &instsToRemove) + : Ctx(C), PromotedWidth(Width), Visited(visited), Sources(sources), + Sinks(sinks), SafeWrap(wrap), InstsToRemove(instsToRemove) { ExtTy = IntegerType::get(Ctx, PromotedWidth); } @@ -139,6 +139,7 @@ SmallPtrSet AllVisited; SmallPtrSet SafeToPromote; SmallPtrSet SafeWrap; + SmallPtrSet InstsToRemove; // Does V have the same size result type as TypeSize. bool EqualTypeSize(Value *V); @@ -604,7 +605,6 @@ for (auto *I : InstsToRemove) { LLVM_DEBUG(dbgs() << "IR Promotion: Removing " << *I << "\n"); I->dropAllReferences(); - I->eraseFromParent(); } } @@ -876,7 +876,7 @@ return false; IRPromoter Promoter(*Ctx, PromotedWidth, CurrentVisited, Sources, Sinks, - SafeWrap); + SafeWrap, InstsToRemove); Promoter.Mutate(); return true; } @@ -942,6 +942,11 @@ } } } + if (!InstsToRemove.empty()) { + for (auto *I : InstsToRemove) + I->eraseFromParent(); + InstsToRemove.clear(); + } } AllVisited.clear();