diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -722,6 +722,15 @@ void identifyDefaultAbstractAttributes( Function &F, DenseSet *Whitelist = nullptr); + /// Record that \p I is deleted after information was manifested. + void deleteAfterManifest(Instruction &I) { ToBeDeletedInsts.insert(&I); } + + /// Record that \p BB is deleted after information was manifested. + void deleteAfterManifest(BasicBlock &BB) { ToBeDeletedBlocks.insert(&BB); } + + /// Record that \p F is deleted after information was manifested. + void deleteAfterManifest(Function &F) { ToBeDeletedFunctions.insert(&F); } + /// Return true if \p AA (or its context instruction) is assumed dead. /// /// If \p LivenessAA is not provided it is queried. @@ -812,6 +821,14 @@ /// The information cache that holds pre-processed (LLVM-IR) information. InformationCache &InfoCache; + + /// Functions, blocks, and instructions we delete after manifest is done. + /// + ///{ + SmallPtrSet ToBeDeletedFunctions; + SmallPtrSet ToBeDeletedBlocks; + SmallPtrSet ToBeDeletedInsts; + ///} }; /// An interface to query the internal state of an abstract attribute. diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -3767,6 +3767,26 @@ assert( NumFinalAAs == AllAbstractAttributes.size() && "Expected the final number of abstract attributes to remain unchanged!"); + + // Delete stuff at the end to avoid invalid references and a nice order. + LLVM_DEBUG(dbgs() << "\n[Attributor] Delete " << ToBeDeletedFunctions.size() + << " functions and " << ToBeDeletedBlocks.size() + << " blocks and " << ToBeDeletedInsts.size() + << " instructions\n"); + for (Instruction *I : ToBeDeletedInsts) { + if (I->hasNUsesOrMore(1)) + I->replaceAllUsesWith(UndefValue::get(I->getType())); + I->eraseFromParent(); + } + for (BasicBlock *BB : ToBeDeletedBlocks) { + // TODO: Check if we need to replace users (PHIs, indirect branches?) + BB->eraseFromParent(); + } + for (Function *Fn : ToBeDeletedFunctions) { + Fn->replaceAllUsesWith(UndefValue::get(Fn->getType())); + Fn->eraseFromParent(); + } + return ManifestChange; }