diff options
author | Arthur Eubanks <aeubanks@google.com> | 2022-12-20 12:55:05 -0800 |
---|---|---|
committer | Arthur Eubanks <aeubanks@google.com> | 2022-12-20 12:57:35 -0800 |
commit | 2118b9d39b91e93c0146611235072cd6ca0f27b1 (patch) | |
tree | 310b7a57a85a91fcb2d3b55f1eb941ccc2451b0b /llvm/lib | |
parent | 1b79bed8f53231144ad6e14f8cc3e8673c7fb469 (diff) | |
download | llvm-2118b9d39b91e93c0146611235072cd6ca0f27b1.zip llvm-2118b9d39b91e93c0146611235072cd6ca0f27b1.tar.gz llvm-2118b9d39b91e93c0146611235072cd6ca0f27b1.tar.bz2 |
[llvm-extract] Use new pass manager instead of legacy pass manager
Removes some legacy passes specific to llvm-extract
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Passes/PassRegistry.def | 2 | ||||
-rw-r--r-- | llvm/lib/Transforms/IPO/BlockExtractor.cpp | 73 | ||||
-rw-r--r-- | llvm/lib/Transforms/IPO/ExtractGV.cpp | 174 | ||||
-rw-r--r-- | llvm/lib/Transforms/IPO/IPO.cpp | 1 | ||||
-rw-r--r-- | llvm/lib/Transforms/IPO/StripSymbols.cpp | 1 |
5 files changed, 87 insertions, 164 deletions
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index ee41084..4aec1b8 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -56,7 +56,7 @@ MODULE_PASS("deadargelim", DeadArgumentEliminationPass()) MODULE_PASS("debugify", NewPMDebugifyPass()) MODULE_PASS("dot-callgraph", CallGraphDOTPrinterPass()) MODULE_PASS("elim-avail-extern", EliminateAvailableExternallyPass()) -MODULE_PASS("extract-blocks", BlockExtractorPass()) +MODULE_PASS("extract-blocks", BlockExtractorPass({}, false)) MODULE_PASS("forceattrs", ForceFunctionAttrsPass()) MODULE_PASS("function-import", FunctionImportPass()) MODULE_PASS("globaldce", GlobalDCEPass()) diff --git a/llvm/lib/Transforms/IPO/BlockExtractor.cpp b/llvm/lib/Transforms/IPO/BlockExtractor.cpp index dfadfdd..a68cf7d 100644 --- a/llvm/lib/Transforms/IPO/BlockExtractor.cpp +++ b/llvm/lib/Transforms/IPO/BlockExtractor.cpp @@ -45,20 +45,15 @@ class BlockExtractor { public: BlockExtractor(bool EraseFunctions) : EraseFunctions(EraseFunctions) {} bool runOnModule(Module &M); - void init(const SmallVectorImpl<SmallVector<BasicBlock *, 16>> - &GroupsOfBlocksToExtract) { - for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks : - GroupsOfBlocksToExtract) { - SmallVector<BasicBlock *, 16> NewGroup; - NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end()); - GroupsOfBlocks.emplace_back(NewGroup); - } + void + init(const std::vector<std::vector<BasicBlock *>> &GroupsOfBlocksToExtract) { + GroupsOfBlocks = GroupsOfBlocksToExtract; if (!BlockExtractorFile.empty()) loadFile(); } private: - SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupsOfBlocks; + std::vector<std::vector<BasicBlock *>> GroupsOfBlocks; bool EraseFunctions; /// Map a function name to groups of blocks. SmallVector<std::pair<std::string, SmallVector<std::string, 4>>, 4> @@ -68,56 +63,8 @@ private: void splitLandingPadPreds(Function &F); }; -class BlockExtractorLegacyPass : public ModulePass { - BlockExtractor BE; - bool runOnModule(Module &M) override; - -public: - static char ID; - BlockExtractorLegacyPass(const SmallVectorImpl<BasicBlock *> &BlocksToExtract, - bool EraseFunctions) - : ModulePass(ID), BE(EraseFunctions) { - // We want one group per element of the input list. - SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks; - for (BasicBlock *BB : BlocksToExtract) { - SmallVector<BasicBlock *, 16> NewGroup; - NewGroup.push_back(BB); - MassagedGroupsOfBlocks.push_back(NewGroup); - } - BE.init(MassagedGroupsOfBlocks); - } - - BlockExtractorLegacyPass(const SmallVectorImpl<SmallVector<BasicBlock *, 16>> - &GroupsOfBlocksToExtract, - bool EraseFunctions) - : ModulePass(ID), BE(EraseFunctions) { - BE.init(GroupsOfBlocksToExtract); - } - - BlockExtractorLegacyPass() - : BlockExtractorLegacyPass(SmallVector<BasicBlock *, 0>(), false) {} -}; - } // end anonymous namespace -char BlockExtractorLegacyPass::ID = 0; -INITIALIZE_PASS(BlockExtractorLegacyPass, "extract-blocks", - "Extract basic blocks from module", false, false) - -ModulePass *llvm::createBlockExtractorPass() { - return new BlockExtractorLegacyPass(); -} -ModulePass *llvm::createBlockExtractorPass( - const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) { - return new BlockExtractorLegacyPass(BlocksToExtract, EraseFunctions); -} -ModulePass *llvm::createBlockExtractorPass( - const SmallVectorImpl<SmallVector<BasicBlock *, 16>> - &GroupsOfBlocksToExtract, - bool EraseFunctions) { - return new BlockExtractorLegacyPass(GroupsOfBlocksToExtract, EraseFunctions); -} - /// Gets all of the blocks specified in the input file. void BlockExtractor::loadFile() { auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile); @@ -179,7 +126,6 @@ void BlockExtractor::splitLandingPadPreds(Function &F) { } bool BlockExtractor::runOnModule(Module &M) { - bool Changed = false; // Get all the functions. @@ -251,14 +197,15 @@ bool BlockExtractor::runOnModule(Module &M) { return Changed; } -bool BlockExtractorLegacyPass::runOnModule(Module &M) { - return BE.runOnModule(M); -} +BlockExtractorPass::BlockExtractorPass( + std::vector<std::vector<BasicBlock *>> &&GroupsOfBlocks, + bool EraseFunctions) + : GroupsOfBlocks(GroupsOfBlocks), EraseFunctions(EraseFunctions) {} PreservedAnalyses BlockExtractorPass::run(Module &M, ModuleAnalysisManager &AM) { - BlockExtractor BE(false); - BE.init(SmallVector<SmallVector<BasicBlock *, 16>, 0>()); + BlockExtractor BE(EraseFunctions); + BE.init(GroupsOfBlocks); return BE.runOnModule(M) ? PreservedAnalyses::none() : PreservedAnalyses::all(); } diff --git a/llvm/lib/Transforms/IPO/ExtractGV.cpp b/llvm/lib/Transforms/IPO/ExtractGV.cpp index 8428078..d5073ee 100644 --- a/llvm/lib/Transforms/IPO/ExtractGV.cpp +++ b/llvm/lib/Transforms/IPO/ExtractGV.cpp @@ -10,11 +10,11 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/SetVector.h" +#include "llvm/Transforms/IPO/ExtractGV.h" #include "llvm/IR/Module.h" -#include "llvm/Pass.h" -#include "llvm/Transforms/IPO.h" +#include "llvm/IR/PassManager.h" #include <algorithm> + using namespace llvm; /// Make sure GV is visible from both modules. Delete is true if it is @@ -48,110 +48,86 @@ static void makeVisible(GlobalValue &GV, bool Delete) { } } -namespace { - /// A pass to extract specific global values and their dependencies. - class GVExtractorPass : public ModulePass { - SetVector<GlobalValue *> Named; - bool deleteStuff; - bool keepConstInit; - public: - static char ID; // Pass identification, replacement for typeid /// If deleteS is true, this pass deletes the specified global values. /// Otherwise, it deletes as much of the module as possible, except for the /// global values specified. - explicit GVExtractorPass(std::vector<GlobalValue*> &GVs, - bool deleteS = true, bool keepConstInit = false) - : ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS), - keepConstInit(keepConstInit) {} - - bool runOnModule(Module &M) override { - if (skipModule(M)) - return false; - - // Visit the global inline asm. - if (!deleteStuff) - M.setModuleInlineAsm(""); - - // For simplicity, just give all GlobalValues ExternalLinkage. A trickier - // implementation could figure out which GlobalValues are actually - // referenced by the Named set, and which GlobalValues in the rest of - // the module are referenced by the NamedSet, and get away with leaving - // more internal and private things internal and private. But for now, - // be conservative and simple. - - // Visit the GlobalVariables. - for (GlobalVariable &GV : M.globals()) { - bool Delete = deleteStuff == (bool)Named.count(&GV) && - !GV.isDeclaration() && - (!GV.isConstant() || !keepConstInit); - if (!Delete) { - if (GV.hasAvailableExternallyLinkage()) - continue; - if (GV.getName() == "llvm.global_ctors") - continue; - } - - makeVisible(GV, Delete); - - if (Delete) { - // Make this a declaration and drop it's comdat. - GV.setInitializer(nullptr); - GV.setComdat(nullptr); - } - } +ExtractGVPass::ExtractGVPass(std::vector<GlobalValue *> &GVs, bool deleteS, + bool keepConstInit) + : Named(GVs.begin(), GVs.end()), deleteStuff(deleteS), + keepConstInit(keepConstInit) {} + +PreservedAnalyses ExtractGVPass::run(Module &M, ModuleAnalysisManager &) { + // Visit the global inline asm. + if (!deleteStuff) + M.setModuleInlineAsm(""); + + // For simplicity, just give all GlobalValues ExternalLinkage. A trickier + // implementation could figure out which GlobalValues are actually + // referenced by the Named set, and which GlobalValues in the rest of + // the module are referenced by the NamedSet, and get away with leaving + // more internal and private things internal and private. But for now, + // be conservative and simple. + + // Visit the GlobalVariables. + for (GlobalVariable &GV : M.globals()) { + bool Delete = deleteStuff == (bool)Named.count(&GV) && + !GV.isDeclaration() && (!GV.isConstant() || !keepConstInit); + if (!Delete) { + if (GV.hasAvailableExternallyLinkage()) + continue; + if (GV.getName() == "llvm.global_ctors") + continue; + } - // Visit the Functions. - for (Function &F : M) { - bool Delete = - deleteStuff == (bool)Named.count(&F) && !F.isDeclaration(); - if (!Delete) { - if (F.hasAvailableExternallyLinkage()) - continue; - } - - makeVisible(F, Delete); - - if (Delete) { - // Make this a declaration and drop it's comdat. - F.deleteBody(); - F.setComdat(nullptr); - } - } + makeVisible(GV, Delete); - // Visit the Aliases. - for (GlobalAlias &GA : llvm::make_early_inc_range(M.aliases())) { - bool Delete = deleteStuff == (bool)Named.count(&GA); - makeVisible(GA, Delete); - - if (Delete) { - Type *Ty = GA.getValueType(); - - GA.removeFromParent(); - llvm::Value *Declaration; - if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { - Declaration = - Function::Create(FTy, GlobalValue::ExternalLinkage, - GA.getAddressSpace(), GA.getName(), &M); - - } else { - Declaration = - new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage, - nullptr, GA.getName()); - } - GA.replaceAllUsesWith(Declaration); - delete &GA; - } - } + if (Delete) { + // Make this a declaration and drop it's comdat. + GV.setInitializer(nullptr); + GV.setComdat(nullptr); + } + } - return true; + // Visit the Functions. + for (Function &F : M) { + bool Delete = deleteStuff == (bool)Named.count(&F) && !F.isDeclaration(); + if (!Delete) { + if (F.hasAvailableExternallyLinkage()) + continue; } - }; - char GVExtractorPass::ID = 0; -} + makeVisible(F, Delete); + + if (Delete) { + // Make this a declaration and drop it's comdat. + F.deleteBody(); + F.setComdat(nullptr); + } + } + + // Visit the Aliases. + for (GlobalAlias &GA : llvm::make_early_inc_range(M.aliases())) { + bool Delete = deleteStuff == (bool)Named.count(&GA); + makeVisible(GA, Delete); + + if (Delete) { + Type *Ty = GA.getValueType(); + + GA.removeFromParent(); + llvm::Value *Declaration; + if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { + Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage, + GA.getAddressSpace(), GA.getName(), &M); + + } else { + Declaration = new GlobalVariable( + M, Ty, false, GlobalValue::ExternalLinkage, nullptr, GA.getName()); + } + GA.replaceAllUsesWith(Declaration); + delete &GA; + } + } -ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs, - bool deleteFn, bool keepConstInit) { - return new GVExtractorPass(GVs, deleteFn, keepConstInit); + return PreservedAnalyses::none(); } diff --git a/llvm/lib/Transforms/IPO/IPO.cpp b/llvm/lib/Transforms/IPO/IPO.cpp index 8bd92b5..4163c44 100644 --- a/llvm/lib/Transforms/IPO/IPO.cpp +++ b/llvm/lib/Transforms/IPO/IPO.cpp @@ -40,7 +40,6 @@ void llvm::initializeIPO(PassRegistry &Registry) { initializeInferFunctionAttrsLegacyPassPass(Registry); initializeInternalizeLegacyPassPass(Registry); initializeLoopExtractorLegacyPassPass(Registry); - initializeBlockExtractorLegacyPassPass(Registry); initializeSingleLoopExtractorPass(Registry); initializeMergeFunctionsLegacyPassPass(Registry); initializePartialInlinerLegacyPassPass(Registry); diff --git a/llvm/lib/Transforms/IPO/StripSymbols.cpp b/llvm/lib/Transforms/IPO/StripSymbols.cpp index 159f0b2..34f8c43 100644 --- a/llvm/lib/Transforms/IPO/StripSymbols.cpp +++ b/llvm/lib/Transforms/IPO/StripSymbols.cpp @@ -33,6 +33,7 @@ #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/IPO/StripSymbols.h" #include "llvm/Transforms/Utils/Local.h" using namespace llvm; |