From cf063dd2c8c1ba44ad416c0a1b088c65ae02ff03 Mon Sep 17 00:00:00 2001 From: Johannes Doerfert Date: Tue, 27 Aug 2019 04:57:54 +0000 Subject: [PATCH] [Attributor] Introduce an API to delete stuff Summary: During the fixpoint iteration, including the manifest stage, we should not delete stuff as other abstract attributes might have a reference to the value. Through the API this can now be done safely at the very end. Reviewers: uenoku, sstefan1 Subscribers: hiraditya, bollu, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D66779 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@370014 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/IPO/Attributor.h | 17 +++++++++++++++++ lib/Transforms/IPO/Attributor.cpp | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/llvm/Transforms/IPO/Attributor.h b/include/llvm/Transforms/IPO/Attributor.h index bea369a6b51..54c996e0da5 100644 --- a/include/llvm/Transforms/IPO/Attributor.h +++ b/include/llvm/Transforms/IPO/Attributor.h @@ -675,6 +675,15 @@ struct Attributor { 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. @@ -765,6 +774,14 @@ private: /// 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/lib/Transforms/IPO/Attributor.cpp b/lib/Transforms/IPO/Attributor.cpp index a6e8e528fd0..a2d470affc0 100644 --- a/lib/Transforms/IPO/Attributor.cpp +++ b/lib/Transforms/IPO/Attributor.cpp @@ -2641,6 +2641,26 @@ ChangeStatus Attributor::run() { 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; } -- 2.50.1