From: Hendrik Greving Date: Thu, 12 Nov 2020 01:56:14 +0000 (-0800) Subject: Add MachineModuleInfo constructor with external MCContext X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=d4ba5e15f4f2f41f13625cc10e4df74b44620803;p=android-x86%2Fexternal-llvm-project.git Add MachineModuleInfo constructor with external MCContext Adds a constructor to MachineModuleInfo and MachineModuleInfoWapperPass that takes an external MCContext. If provided, the external context will be used throughout codegen instead of MMI's default one. This enables external drivers to take ownership of data put on the MMI's context during codegen. The internal context is used otherwise and destroyed upon finish. Differential Revision: https://reviews.llvm.org/D91313 --- diff --git a/llvm/include/llvm/CodeGen/MachineModuleInfo.h b/llvm/include/llvm/CodeGen/MachineModuleInfo.h index a37708c5537..fa900affb21 100644 --- a/llvm/include/llvm/CodeGen/MachineModuleInfo.h +++ b/llvm/include/llvm/CodeGen/MachineModuleInfo.h @@ -83,6 +83,9 @@ class MachineModuleInfo { /// This is the MCContext used for the entire code generator. MCContext Context; + // This is an external context, that if assigned, will be used instead of the + // internal context. + MCContext *ExternalContext = nullptr; /// This is the LLVM Module being worked on. const Module *TheModule; @@ -149,6 +152,9 @@ class MachineModuleInfo { public: explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr); + explicit MachineModuleInfo(const LLVMTargetMachine *TM, + MCContext *ExtContext); + MachineModuleInfo(MachineModuleInfo &&MMII); ~MachineModuleInfo(); @@ -158,8 +164,12 @@ public: const LLVMTargetMachine &getTarget() const { return TM; } - const MCContext &getContext() const { return Context; } - MCContext &getContext() { return Context; } + const MCContext &getContext() const { + return ExternalContext ? *ExternalContext : Context; + } + MCContext &getContext() { + return ExternalContext ? *ExternalContext : Context; + } const Module *getModule() const { return TheModule; } @@ -266,6 +276,9 @@ public: static char ID; // Pass identification, replacement for typeid explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr); + explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM, + MCContext *ExtContext); + // Initialization and Finalization bool doInitialization(Module &) override; bool doFinalization(Module &) override; diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp index be08f0ae311..f75acbb2494 100644 --- a/llvm/lib/CodeGen/MachineModuleInfo.cpp +++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp @@ -170,6 +170,7 @@ void MachineModuleInfo::finalize() { AddrLabelSymbols = nullptr; Context.reset(); + // We don't clear the ExternalContext. delete ObjFileMMI; ObjFileMMI = nullptr; @@ -187,6 +188,7 @@ MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI) HasSplitStack = MMI.HasSplitStack; HasNosplitStack = MMI.HasNosplitStack; AddrLabelSymbols = MMI.AddrLabelSymbols; + ExternalContext = MMI.ExternalContext; TheModule = MMI.TheModule; } @@ -196,6 +198,14 @@ MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM) initialize(); } +MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM, + MCContext *ExtContext) + : TM(*TM), Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(), + TM->getObjFileLowering(), nullptr, nullptr, false), + ExternalContext(ExtContext) { + initialize(); +} + MachineModuleInfo::~MachineModuleInfo() { finalize(); } //===- Address of Block Management ----------------------------------------===// @@ -204,7 +214,7 @@ ArrayRef MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) { // Lazily create AddrLabelSymbols. if (!AddrLabelSymbols) - AddrLabelSymbols = new MMIAddrLabelMap(Context); + AddrLabelSymbols = new MMIAddrLabelMap(getContext()); return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast(BB)); } @@ -296,6 +306,12 @@ MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); } +MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( + const LLVMTargetMachine *TM, MCContext *ExtContext) + : ImmutablePass(ID), MMI(TM, ExtContext) { + initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); +} + // Handle the Pass registration stuff necessary to use DataLayout's. INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo", "Machine Module Information", false, false)