[PATCH] D23736: CodeGen: Remove MachineFunctionAnalysis => Enable (Machine)ModulePasses

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 22 09:50:03 PDT 2016


Matthias Braun <matze at braunis.de> writes:
> MatzeB created this revision.
> MatzeB added reviewers: jmolloy, chandlerc, hfinkel, bogner, rengolin,
> qcolombet.
> MatzeB added subscribers: llvm-commits, jpaquette.
> MatzeB set the repository for this revision to rL LLVM.
> Herald added a reviewer: tstellarAMD.
> Herald added subscribers: mcrosier, arsenm, MatzeB, jholewinski.
>
> This patch removes the MachineFunctionAnalysis. Instead we keep a
> map from IR Function to MachineFunction in the MachineModuleInfo.
>
> This allows the insertion of ModulePasses into the codegen pipeline
> without breaking it because the MachineFunctionAnalysis gets dropped
> before a module pass.
>
> Peak memory should stay unchanged without a ModulePass in the codegen
> pipeline: Previously the MachineFunction was freed at the end of a codegen
> function pipeline because the MachineFunctionAnalysis was dropped; With
> this patch the MachineFunction is freed after the AsmPrinter has
> finished.
>
>
> Note that this is not complete yet. It works nice without a ModulePass
> in the codegen pipeline. It works in many cases with a ModulePass, but
> still fails in some instances because have some metadata about the
> current function in MachineModuleInfo which needs to be moved into the
> MachineFunction class.

I like the idea. A few random thoughts/nitpicks below.

> Repository:
>   rL LLVM
>
> https://reviews.llvm.org/D23736
>
> Files:
>   include/llvm/CodeGen/AsmPrinter.h
>   include/llvm/CodeGen/MachineFunctionAnalysis.h
>   include/llvm/CodeGen/MachineModuleInfo.h
>   include/llvm/Target/TargetMachine.h
>   lib/CodeGen/AsmPrinter/AsmPrinter.cpp
>   lib/CodeGen/CMakeLists.txt
>   lib/CodeGen/LLVMTargetMachine.cpp
>   lib/CodeGen/MachineFunctionAnalysis.cpp
>   lib/CodeGen/MachineFunctionPass.cpp
>   lib/CodeGen/MachineModuleInfo.cpp
>   lib/CodeGen/StackMapLivenessAnalysis.cpp
>   lib/Target/AMDGPU/AMDILCFGStructurizer.cpp
>   lib/Target/Hexagon/HexagonCommonGEP.cpp
>   lib/Target/Hexagon/HexagonGenExtract.cpp
>   lib/Target/Hexagon/HexagonNewValueJump.cpp
>   lib/Target/Hexagon/HexagonOptimizeSZextends.cpp
>   lib/Target/Hexagon/HexagonVLIWPacketizer.cpp
>   lib/Target/NVPTX/NVPTXAllocaHoisting.cpp
>   lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
>   lib/Target/NVPTX/NVPTXLowerAggrCopies.cpp
>   lib/Target/NVPTX/NVPTXTargetMachine.cpp
>   test/CodeGen/Generic/stop-after.ll
>   test/CodeGen/X86/hidden-vis-pic.ll
>   tools/llc/llc.cpp
>   unittests/MI/LiveIntervalTest.cpp
>
> Index: unittests/MI/LiveIntervalTest.cpp
> ===================================================================
> --- unittests/MI/LiveIntervalTest.cpp
> +++ unittests/MI/LiveIntervalTest.cpp
> @@ -70,8 +70,7 @@
>      return nullptr;
>  
>    const LLVMTargetMachine &LLVMTM = static_cast<const LLVMTargetMachine&>(TM);
> -  LLVMTM.addMachineModuleInfo(PM);
> -  LLVMTM.addMachineFunctionAnalysis(PM, MIR.get());
> +  LLVMTM.addMachineModuleInfo(PM, MIR.get());
>  
>    return M;
>  }
> Index: tools/llc/llc.cpp
> ===================================================================
> --- tools/llc/llc.cpp
> +++ tools/llc/llc.cpp
> @@ -450,8 +450,7 @@
>        LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine&>(*Target);
>        TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM);
>        PM.add(&TPC);
> -      LLVMTM.addMachineModuleInfo(PM);
> -      LLVMTM.addMachineFunctionAnalysis(PM, MIR.get());
> +      LLVMTM.addMachineModuleInfo(PM, MIR.get());
>        TPC.printAndVerify("");
>  
>        for (const std::string &RunPassName : *RunPassNames) {
> Index: test/CodeGen/X86/hidden-vis-pic.ll
> ===================================================================
> --- test/CodeGen/X86/hidden-vis-pic.ll
> +++ test/CodeGen/X86/hidden-vis-pic.ll
> @@ -17,7 +17,7 @@
>  
>  ; This must use movl of the stub, not an lea, since the function isn't being
>  ; emitted here.
> -; CHECK: movl L__ZNSbIcED1Ev$non_lazy_ptr-L1$pb(
> +; CHECK: movl L__ZNSbIcED1Ev$non_lazy_ptr-L0$pb(

This change seems a bit suspect. What's going on here?

>  
> Index: test/CodeGen/Generic/stop-after.ll
> ===================================================================
> --- test/CodeGen/Generic/stop-after.ll
> +++ test/CodeGen/Generic/stop-after.ll
> @@ -3,7 +3,6 @@
>  
>  ; STOP: -loop-reduce
>  ; STOP: Loop Strength Reduction
> -; STOP-NEXT: Machine Function Analysis
>  ; STOP-NEXT: MIR Printing Pass
>  
>  ; START: -machine-branch-prob -pre-isel-intrinsic-lowering
> Index: lib/Target/NVPTX/NVPTXTargetMachine.cpp
> ===================================================================
> --- lib/Target/NVPTX/NVPTXTargetMachine.cpp
> +++ lib/Target/NVPTX/NVPTXTargetMachine.cpp
> @@ -20,7 +20,6 @@
>  #include "NVPTXTargetTransformInfo.h"
>  #include "llvm/Analysis/Passes.h"
>  #include "llvm/CodeGen/AsmPrinter.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/MachineModuleInfo.h"
>  #include "llvm/CodeGen/Passes.h"
>  #include "llvm/CodeGen/TargetPassConfig.h"
> Index: lib/Target/NVPTX/NVPTXLowerAggrCopies.cpp
> ===================================================================
> --- lib/Target/NVPTX/NVPTXLowerAggrCopies.cpp
> +++ lib/Target/NVPTX/NVPTXLowerAggrCopies.cpp
> @@ -14,7 +14,6 @@
>  //===----------------------------------------------------------------------===//
>  
>  #include "NVPTXLowerAggrCopies.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/StackProtector.h"
>  #include "llvm/IR/Constants.h"
>  #include "llvm/IR/DataLayout.h"
> @@ -41,7 +40,6 @@
>    NVPTXLowerAggrCopies() : FunctionPass(ID) {}
>  
>    void getAnalysisUsage(AnalysisUsage &AU) const override {
> -    AU.addPreserved<MachineFunctionAnalysis>();
>      AU.addPreserved<StackProtector>();
>    }
>  
> Index: lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
> ===================================================================
> --- lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
> +++ lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
> @@ -15,7 +15,6 @@
>  #include "NVPTX.h"
>  #include "MCTargetDesc/NVPTXBaseInfo.h"
>  #include "NVPTXUtilities.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/ValueTypes.h"
>  #include "llvm/IR/Constants.h"
>  #include "llvm/IR/DerivedTypes.h"
> Index: lib/Target/NVPTX/NVPTXAllocaHoisting.cpp
> ===================================================================
> --- lib/Target/NVPTX/NVPTXAllocaHoisting.cpp
> +++ lib/Target/NVPTX/NVPTXAllocaHoisting.cpp
> @@ -12,7 +12,6 @@
>  //===----------------------------------------------------------------------===//
>  
>  #include "NVPTXAllocaHoisting.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/StackProtector.h"
>  #include "llvm/IR/Constants.h"
>  #include "llvm/IR/Function.h"
> @@ -28,7 +27,6 @@
>    NVPTXAllocaHoisting() : FunctionPass(ID) {}
>  
>    void getAnalysisUsage(AnalysisUsage &AU) const override {
> -    AU.addPreserved<MachineFunctionAnalysis>();
>      AU.addPreserved<StackProtector>();
>    }
>  
> Index: lib/Target/Hexagon/HexagonVLIWPacketizer.cpp
> ===================================================================
> --- lib/Target/Hexagon/HexagonVLIWPacketizer.cpp
> +++ lib/Target/Hexagon/HexagonVLIWPacketizer.cpp
> @@ -22,7 +22,6 @@
>  #include "HexagonVLIWPacketizer.h"
>  #include "llvm/Analysis/AliasAnalysis.h"
>  #include "llvm/CodeGen/MachineDominators.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/MachineFunctionPass.h"
>  #include "llvm/CodeGen/MachineLoopInfo.h"
>  #include "llvm/CodeGen/MachineRegisterInfo.h"
> Index: lib/Target/Hexagon/HexagonOptimizeSZextends.cpp
> ===================================================================
> --- lib/Target/Hexagon/HexagonOptimizeSZextends.cpp
> +++ lib/Target/Hexagon/HexagonOptimizeSZextends.cpp
> @@ -12,7 +12,6 @@
>  //
>  //===----------------------------------------------------------------------===//
>  
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/StackProtector.h"
>  #include "llvm/IR/Function.h"
>  #include "llvm/IR/Instructions.h"
> @@ -43,8 +42,6 @@
>      }
>  
>      void getAnalysisUsage(AnalysisUsage &AU) const override {
> -      AU.addRequired<MachineFunctionAnalysis>();
> -      AU.addPreserved<MachineFunctionAnalysis>();
>        AU.addPreserved<StackProtector>();
>        FunctionPass::getAnalysisUsage(AU);
>      }
> Index: lib/Target/Hexagon/HexagonNewValueJump.cpp
> ===================================================================
> --- lib/Target/Hexagon/HexagonNewValueJump.cpp
> +++ lib/Target/Hexagon/HexagonNewValueJump.cpp
> @@ -29,7 +29,6 @@
>  #include "HexagonTargetMachine.h"
>  #include "llvm/ADT/Statistic.h"
>  #include "llvm/CodeGen/LiveVariables.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/MachineFunctionPass.h"
>  #include "llvm/CodeGen/MachineInstrBuilder.h"
>  #include "llvm/CodeGen/MachineRegisterInfo.h"
> Index: lib/Target/Hexagon/HexagonGenExtract.cpp
> ===================================================================
> --- lib/Target/Hexagon/HexagonGenExtract.cpp
> +++ lib/Target/Hexagon/HexagonGenExtract.cpp
> @@ -8,7 +8,6 @@
>  //===----------------------------------------------------------------------===//
>  
>  #include "llvm/ADT/STLExtras.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/IR/Constants.h"
>  #include "llvm/IR/Dominators.h"
>  #include "llvm/IR/Function.h"
> @@ -60,7 +59,6 @@
>      virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
>        AU.addRequired<DominatorTreeWrapperPass>();
>        AU.addPreserved<DominatorTreeWrapperPass>();
> -      AU.addPreserved<MachineFunctionAnalysis>();
>        FunctionPass::getAnalysisUsage(AU);
>      }
>    private:
> Index: lib/Target/Hexagon/HexagonCommonGEP.cpp
> ===================================================================
> --- lib/Target/Hexagon/HexagonCommonGEP.cpp
> +++ lib/Target/Hexagon/HexagonCommonGEP.cpp
> @@ -14,7 +14,6 @@
>  #include "llvm/ADT/STLExtras.h"
>  #include "llvm/Analysis/LoopInfo.h"
>  #include "llvm/Analysis/PostDominators.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/IR/Constants.h"
>  #include "llvm/IR/Dominators.h"
>  #include "llvm/IR/Function.h"
> Index: lib/Target/AMDGPU/AMDILCFGStructurizer.cpp
> ===================================================================
> --- lib/Target/AMDGPU/AMDILCFGStructurizer.cpp
> +++ lib/Target/AMDGPU/AMDILCFGStructurizer.cpp
> @@ -18,7 +18,6 @@
>  #include "llvm/ADT/Statistic.h"
>  #include "llvm/CodeGen/MachineDominators.h"
>  #include "llvm/CodeGen/MachineFunction.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/MachineFunctionPass.h"
>  #include "llvm/CodeGen/MachineInstrBuilder.h"
>  #include "llvm/CodeGen/MachineJumpTableInfo.h"
> @@ -144,11 +143,10 @@
>    }
>  
>    void getAnalysisUsage(AnalysisUsage &AU) const override {
> -    AU.addPreserved<MachineFunctionAnalysis>();
> -    AU.addRequired<MachineFunctionAnalysis>();
>      AU.addRequired<MachineDominatorTree>();
>      AU.addRequired<MachinePostDominatorTree>();
>      AU.addRequired<MachineLoopInfo>();
> +    MachineFunctionPass::getAnalysisUsage(AU);
>    }
>  
>    /// Perform the CFG structurization
> Index: lib/CodeGen/StackMapLivenessAnalysis.cpp
> ===================================================================
> --- lib/CodeGen/StackMapLivenessAnalysis.cpp
> +++ lib/CodeGen/StackMapLivenessAnalysis.cpp
> @@ -17,7 +17,6 @@
>  #include "llvm/CodeGen/LivePhysRegs.h"
>  #include "llvm/CodeGen/MachineFrameInfo.h"
>  #include "llvm/CodeGen/MachineFunction.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/MachineFunctionPass.h"
>  #include "llvm/CodeGen/Passes.h"
>  #include "llvm/Support/CommandLine.h"
> Index: lib/CodeGen/MachineModuleInfo.cpp
> ===================================================================
> --- lib/CodeGen/MachineModuleInfo.cpp
> +++ lib/CodeGen/MachineModuleInfo.cpp
> @@ -13,6 +13,7 @@
>  #include "llvm/Analysis/EHPersonalities.h"
>  #include "llvm/Analysis/ValueTracking.h"
>  #include "llvm/CodeGen/MachineFunction.h"
> +#include "llvm/CodeGen/MachineFunctionInitializer.h"
>  #include "llvm/CodeGen/MachineFunctionPass.h"
>  #include "llvm/CodeGen/Passes.h"
>  #include "llvm/IR/Constants.h"
> @@ -186,15 +187,19 @@
>  
>  //===----------------------------------------------------------------------===//
>  
> -MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI,
> +MachineModuleInfo::MachineModuleInfo(const TargetMachine &TM,
> +                                     const MCAsmInfo &MAI,
>                                       const MCRegisterInfo &MRI,
> -                                     const MCObjectFileInfo *MOFI)
> -  : ImmutablePass(ID), Context(&MAI, &MRI, MOFI, nullptr, false) {
> +                                     const MCObjectFileInfo *MOFI,
> +                                     MachineFunctionInitializer *MFI)
> +  : ImmutablePass(ID), TM(TM), Context(&MAI, &MRI, MOFI, nullptr, false),
> +    MFInitializer(MFI) {
>    initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
>  }
>  
>  MachineModuleInfo::MachineModuleInfo()
> -  : ImmutablePass(ID), Context(nullptr, nullptr, nullptr) {
> +  : ImmutablePass(ID), TM(*((TargetMachine*)nullptr)),
> +    Context(nullptr, nullptr, nullptr) {
>    llvm_unreachable("This MachineModuleInfo constructor should never be called, "
>                     "MMI should always be explicitly constructed by "
>                     "LLVMTargetMachine");

Why does this constructor even exist? I'm not really a fan of the
very-undefined-behaviour of storing null in a reference you've added
(though since it's unreachable it obviously doesn't come up)

> @@ -213,7 +218,7 @@
>    DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
>    PersonalityTypeCache = EHPersonality::Unknown;
>    AddrLabelSymbols = nullptr;
> -  TheModule = nullptr;
> +  TheModule = &M;
>  
>    return false;
>  }
> @@ -461,3 +466,40 @@
>    FilterIds.push_back(0); // terminator
>    return FilterID;
>  }
> +
> +MachineFunction &MachineModuleInfo::getMachineFunction(const Function &F) {
> +  // Shortcut for the common case where a sequence of MachineFunctionPasses
> +  // all query for the same Function.
> +  if (LastRequest == &F)
> +    return *LastResult;
> +
> +  auto I = MachineFunctions.insert(
> +      std::make_pair(&F, std::unique_ptr<MachineFunction>()));
> +  MachineFunction *MF;
> +  if (I.second) {
> +    unsigned FunctionNum = (unsigned)MachineFunctions.size() - 1;
> +    MF = new MachineFunction(&F, TM, FunctionNum, *this);
> +    I.first->second.reset(MF);

I think assigning to the unique_ptr and then doing MF = <...>.get()
would be marginally clearer about what's going on here, but maybe the
boilerplate to reach into the pairs would end up being a bit much.

> +
> +    if (MFInitializer)
> +      if (MFInitializer->initializeMachineFunction(*MF))
> +        report_fatal_error("Unable to initialize machine function");
> +  } else {
> +    MF = I.first->second.get();
> +  }
> +
> +  LastRequest = &F;
> +  LastResult = MF;
> +  return *MF;
> +}
> +
> +void MachineModuleInfo::deleteMachineFunction(MachineFunction &MF) {
> +  if (LastResult == &MF) {
> +    LastRequest = nullptr;
> +    LastResult = nullptr;
> +  }
> +
> +  auto I = MachineFunctions.find(MF.getFunction());
> +  assert(I != MachineFunctions.end() && "MachineFunction is known");
> +  I->second.reset(nullptr);
> +}
> Index: lib/CodeGen/MachineFunctionPass.cpp
> ===================================================================
> --- lib/CodeGen/MachineFunctionPass.cpp
> +++ lib/CodeGen/MachineFunctionPass.cpp
> @@ -22,7 +22,7 @@
>  #include "llvm/Analysis/ScalarEvolution.h"
>  #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
>  #include "llvm/CodeGen/MachineFunction.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
> +#include "llvm/CodeGen/MachineModuleInfo.h"
>  #include "llvm/CodeGen/Passes.h"
>  #include "llvm/CodeGen/StackProtector.h"
>  #include "llvm/IR/Dominators.h"
> @@ -41,7 +41,9 @@
>    if (F.hasAvailableExternallyLinkage())
>      return false;
>  
> -  MachineFunction &MF = getAnalysis<MachineFunctionAnalysis>().getMF();
> +  MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
> +  MachineFunction &MF = MMI.getMachineFunction(F);
> +
>    MachineFunctionProperties &MFProps = MF.getProperties();
>  
>  #ifndef NDEBUG
> @@ -65,8 +67,8 @@
>  }
>  
>  void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
> -  AU.addRequired<MachineFunctionAnalysis>();
> -  AU.addPreserved<MachineFunctionAnalysis>();
> +  AU.addRequired<MachineModuleInfo>();
> +  AU.addPreserved<MachineModuleInfo>();
>  
>    // MachineFunctionPass preserves all LLVM IR passes, but there's no
>    // high-level way to express this. Instead, just list a bunch of
> Index: lib/CodeGen/MachineFunctionAnalysis.cpp
> ===================================================================
> --- lib/CodeGen/MachineFunctionAnalysis.cpp
> +++ /dev/null
> @@ -1,62 +0,0 @@
> -//===-- MachineFunctionAnalysis.cpp ---------------------------------------===//
> -//
> -//                     The LLVM Compiler Infrastructure
> -//
> -// This file is distributed under the University of Illinois Open Source
> -// License. See LICENSE.TXT for details.
> -//
> -//===----------------------------------------------------------------------===//
> -//
> -// This file contains the definitions of the MachineFunctionAnalysis members.
> -//
> -//===----------------------------------------------------------------------===//
> -
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
> -#include "llvm/CodeGen/GCMetadata.h"
> -#include "llvm/CodeGen/MachineFunction.h"
> -#include "llvm/CodeGen/MachineModuleInfo.h"
> -#include "llvm/CodeGen/MachineFunctionInitializer.h"
> -using namespace llvm;
> -
> -char MachineFunctionAnalysis::ID = 0;
> -
> -MachineFunctionAnalysis::MachineFunctionAnalysis(
> -    const TargetMachine &tm, MachineFunctionInitializer *MFInitializer)
> -    : FunctionPass(ID), TM(tm), MF(nullptr), MFInitializer(MFInitializer) {
> -  initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
> -}
> -
> -MachineFunctionAnalysis::~MachineFunctionAnalysis() {
> -  releaseMemory();
> -  assert(!MF && "MachineFunctionAnalysis left initialized!");
> -}
> -
> -void MachineFunctionAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
> -  AU.setPreservesAll();
> -  AU.addRequired<MachineModuleInfo>();
> -}
> -
> -bool MachineFunctionAnalysis::doInitialization(Module &M) {
> -  MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
> -  assert(MMI && "MMI not around yet??");
> -  MMI->setModule(&M);
> -  NextFnNum = 0;
> -  return false;
> -}
> -
> -
> -bool MachineFunctionAnalysis::runOnFunction(Function &F) {
> -  assert(!MF && "MachineFunctionAnalysis already initialized!");
> -  MF = new MachineFunction(&F, TM, NextFnNum++,
> -                           getAnalysis<MachineModuleInfo>());
> -  if (MFInitializer) {
> -    if (MFInitializer->initializeMachineFunction(*MF))
> -      report_fatal_error("Unable to initialize machine function");
> -  }
> -  return false;
> -}
> -
> -void MachineFunctionAnalysis::releaseMemory() {
> -  delete MF;
> -  MF = nullptr;
> -}
> Index: lib/CodeGen/LLVMTargetMachine.cpp
> ===================================================================
> --- lib/CodeGen/LLVMTargetMachine.cpp
> +++ lib/CodeGen/LLVMTargetMachine.cpp
> @@ -15,7 +15,6 @@
>  #include "llvm/Analysis/Passes.h"
>  #include "llvm/CodeGen/AsmPrinter.h"
>  #include "llvm/CodeGen/BasicTTIImpl.h"
> -#include "llvm/CodeGen/MachineFunctionAnalysis.h"
>  #include "llvm/CodeGen/MachineModuleInfo.h"
>  #include "llvm/CodeGen/Passes.h"
>  #include "llvm/CodeGen/TargetPassConfig.h"
> @@ -103,19 +102,15 @@
>  }
>  
>  MachineModuleInfo &
> -LLVMTargetMachine::addMachineModuleInfo(PassManagerBase &PM) const {
> -  MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
> +LLVMTargetMachine::addMachineModuleInfo(PassManagerBase &PM,
> +                                        MachineFunctionInitializer *MFI) const {
> +  MachineModuleInfo *MMI = new MachineModuleInfo(*this, *getMCAsmInfo(),
>                                                   *getMCRegisterInfo(),
> -                                                 getObjFileLowering());
> +                                                 getObjFileLowering(), MFI);
>    PM.add(MMI);
>    return *MMI;
>  }
>  
> -void LLVMTargetMachine::addMachineFunctionAnalysis(PassManagerBase &PM,
> -    MachineFunctionInitializer *MFInitializer) const {
> -  PM.add(new MachineFunctionAnalysis(*this, MFInitializer));
> -}
> -
>  /// addPassesToX helper drives creation and initialization of TargetPassConfig.
>  static MCContext *
>  addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
> @@ -150,8 +145,7 @@
>  
>    PassConfig->addISelPrepare();
>  
> -  MachineModuleInfo &MMI = TM->addMachineModuleInfo(PM);
> -  TM->addMachineFunctionAnalysis(PM, MFInitializer);
> +  MachineModuleInfo &MMI = TM->addMachineModuleInfo(PM, MFInitializer);
>  
>    // Enable FastISel with -fast, but allow that to be overridden.
>    TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
> Index: lib/CodeGen/CMakeLists.txt
> ===================================================================
> --- lib/CodeGen/CMakeLists.txt
> +++ lib/CodeGen/CMakeLists.txt
> @@ -59,7 +59,6 @@
>    MachineCSE.cpp
>    MachineDominanceFrontier.cpp
>    MachineDominators.cpp
> -  MachineFunctionAnalysis.cpp
>    MachineFunction.cpp
>    MachineFunctionPass.cpp
>    MachineFunctionPrinterPass.cpp
> Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp
> ===================================================================
> --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp
> +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp
> @@ -2571,7 +2571,14 @@
>    return true;
>  }
>  
> +bool AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
> +  SetupMachineFunction(MF);
> +  EmitFunctionBody();
>  
> +  MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
> +  MMI.deleteMachineFunction(MF);
> +  return false;
> +}
>  
>  GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy &S) {
>    if (!S.usesMetadata())
> Index: include/llvm/Target/TargetMachine.h
> ===================================================================
> --- include/llvm/Target/TargetMachine.h
> +++ include/llvm/Target/TargetMachine.h
> @@ -314,11 +314,8 @@
>                           bool DisableVerify = true) override;
>  
>    /// Add MachineModuleInfo pass to pass manager.
> -  MachineModuleInfo &addMachineModuleInfo(PassManagerBase &PM) const;
> -
> -  /// Add MachineFunctionAnalysis pass to pass manager.
> -  void addMachineFunctionAnalysis(PassManagerBase &PM,
> -      MachineFunctionInitializer *MFInitializer) const;
> +  MachineModuleInfo &addMachineModuleInfo(PassManagerBase &PM,
> +      MachineFunctionInitializer *MFI = nullptr) const;
>  };
>  
>  } // End llvm namespace
> Index: include/llvm/CodeGen/MachineModuleInfo.h
> ===================================================================
> --- include/llvm/CodeGen/MachineModuleInfo.h
> +++ include/llvm/CodeGen/MachineModuleInfo.h
> @@ -54,6 +54,7 @@
>  class MDNode;
>  class MMIAddrLabelMap;
>  class MachineBasicBlock;
> +class MachineFunctionInitializer;
>  class MachineFunction;
>  class Module;
>  class PointerType;
> @@ -107,6 +108,8 @@
>  /// schemes and reformated for specific use.
>  ///
>  class MachineModuleInfo : public ImmutablePass {
> +  const TargetMachine &TM;
> +
>    /// Context - This is the MCContext used for the entire code generator.
>    MCContext Context;
>  
> @@ -184,6 +187,12 @@
>  
>    EHPersonality PersonalityTypeCache;
>  
> +  MachineFunctionInitializer *MFInitializer;
> +  /// Maps IR Functions to their corresponding MachineFunctions.
> +  DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
> +  const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
> +  MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
> +
>  public:
>    static char ID; // Pass identification, replacement for typeid
>  
> @@ -202,8 +211,9 @@
>  
>    MachineModuleInfo();  // DUMMY CONSTRUCTOR, DO NOT CALL.
>    // Real constructor.
> -  MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
> -                    const MCObjectFileInfo *MOFI);
> +  MachineModuleInfo(const TargetMachine &TM, const MCAsmInfo &MAI,
> +                    const MCRegisterInfo &MRI, const MCObjectFileInfo *MOFI,
> +                    MachineFunctionInitializer *MFInitializer = nullptr);
>    ~MachineModuleInfo() override;
>  
>    // Initialization and Finalization
> @@ -220,6 +230,15 @@
>    void setModule(const Module *M) { TheModule = M; }
>    const Module *getModule() const { return TheModule; }
>  
> +  /// Returns the MachineFunction constructed for the IR function \p F.
> +  /// Creates a new MachineFunction and runs the MachineFunctionInitializer
> +  /// if none exists yet.
> +  MachineFunction &getMachineFunction(const Function &F);
> +
> +  /// \brief Delete the MachineFunction \p MF and reset the link in the IR
> +  /// Function to Machine Function map.
> +  void deleteMachineFunction(MachineFunction &MF);
> +
>    /// getInfo - Keep track of various per-function pieces of information for
>    /// backends that would like to do so.
>    ///
> Index: include/llvm/CodeGen/MachineFunctionAnalysis.h
> ===================================================================
> --- include/llvm/CodeGen/MachineFunctionAnalysis.h
> +++ /dev/null
> @@ -1,55 +0,0 @@
> -//===-- MachineFunctionAnalysis.h - Owner of MachineFunctions ----*-C++ -*-===//
> -//
> -//                     The LLVM Compiler Infrastructure
> -//
> -// This file is distributed under the University of Illinois Open Source
> -// License. See LICENSE.TXT for details.
> -//
> -//===----------------------------------------------------------------------===//
> -//
> -// This file declares the MachineFunctionAnalysis class.
> -//
> -//===----------------------------------------------------------------------===//
> -
> -#ifndef LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS_H
> -#define LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS_H
> -
> -#include "llvm/Pass.h"
> -
> -namespace llvm {
> -
> -class MachineFunction;
> -class MachineFunctionInitializer;
> -class TargetMachine;
> -
> -/// MachineFunctionAnalysis - This class is a Pass that manages a
> -/// MachineFunction object.
> -struct MachineFunctionAnalysis : public FunctionPass {
> -private:
> -  const TargetMachine &TM;
> -  MachineFunction *MF;
> -  unsigned NextFnNum;
> -  MachineFunctionInitializer *MFInitializer;
> -
> -public:
> -  static char ID;
> -  explicit MachineFunctionAnalysis(const TargetMachine &tm,
> -                                   MachineFunctionInitializer *MFInitializer);
> -  ~MachineFunctionAnalysis() override;
> -
> -  MachineFunction &getMF() const { return *MF; }
> -
> -  const char* getPassName() const override {
> -    return "Machine Function Analysis";
> -  }
> -
> -private:
> -  bool doInitialization(Module &M) override;
> -  bool runOnFunction(Function &F) override;
> -  void releaseMemory() override;
> -  void getAnalysisUsage(AnalysisUsage &AU) const override;
> -};
> -
> -} // End llvm namespace
> -
> -#endif
> Index: include/llvm/CodeGen/AsmPrinter.h
> ===================================================================
> --- include/llvm/CodeGen/AsmPrinter.h
> +++ include/llvm/CodeGen/AsmPrinter.h
> @@ -204,11 +204,7 @@
>    bool doFinalization(Module &M) override;
>  
>    /// Emit the specified function out to the OutStreamer.
> -  bool runOnMachineFunction(MachineFunction &MF) override {
> -    SetupMachineFunction(MF);
> -    EmitFunctionBody();
> -    return false;
> -  }
> +  bool runOnMachineFunction(MachineFunction &MF) override;
>  
>    //===------------------------------------------------------------------===//
>    // Coarse grained IR lowering routines.


More information about the llvm-commits mailing list