LLVM  17.0.0git
MachineModuleInfo.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/StringRef.h"
13 #include "llvm/CodeGen/Passes.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/DiagnosticInfo.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Pass.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <memory>
28 #include <utility>
29 #include <vector>
30 
31 using namespace llvm;
32 using namespace llvm::dwarf;
33 
34 static cl::opt<bool>
35  DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
36  cl::desc("Disable debug info printing"));
37 
38 // Out of line virtual method.
40 
42  ObjFileMMI = nullptr;
43  CurCallSite = 0;
44  NextFnNum = 0;
45  UsesMSVCFloatingPoint = false;
46  DbgInfoAvailable = false;
47 }
48 
50  Context.reset();
51  // We don't clear the ExternalContext.
52 
53  delete ObjFileMMI;
54  ObjFileMMI = nullptr;
55 }
56 
58  : TM(std::move(MMI.TM)),
59  Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
60  MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
61  &MMI.TM.Options.MCOptions, false),
62  MachineFunctions(std::move(MMI.MachineFunctions)) {
63  Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
64  ObjFileMMI = MMI.ObjFileMMI;
65  CurCallSite = MMI.CurCallSite;
66  ExternalContext = MMI.ExternalContext;
67  TheModule = MMI.TheModule;
68 }
69 
71  : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
72  TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
73  nullptr, &TM->Options.MCOptions, false) {
74  Context.setObjectFileInfo(TM->getObjFileLowering());
75  initialize();
76 }
77 
79  MCContext *ExtContext)
80  : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
81  TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
82  nullptr, &TM->Options.MCOptions, false),
83  ExternalContext(ExtContext) {
84  Context.setObjectFileInfo(TM->getObjFileLowering());
85  initialize();
86 }
87 
89 
92  auto I = MachineFunctions.find(&F);
93  return I != MachineFunctions.end() ? I->second.get() : nullptr;
94 }
95 
97  // Shortcut for the common case where a sequence of MachineFunctionPasses
98  // all query for the same Function.
99  if (LastRequest == &F)
100  return *LastResult;
101 
102  auto I = MachineFunctions.insert(
103  std::make_pair(&F, std::unique_ptr<MachineFunction>()));
104  MachineFunction *MF;
105  if (I.second) {
106  // No pre-existing machine function, create a new one.
107  const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
108  MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
110  // Update the set entry.
111  I.first->second.reset(MF);
112  } else {
113  MF = I.first->second.get();
114  }
115 
116  LastRequest = &F;
117  LastResult = MF;
118  return *MF;
119 }
120 
122  MachineFunctions.erase(&F);
123  LastRequest = nullptr;
124  LastResult = nullptr;
125 }
126 
128  std::unique_ptr<MachineFunction> &&MF) {
129  auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
130  assert(I.second && "machine function already mapped");
131  (void)I;
132 }
133 
134 namespace {
135 
136 /// This pass frees the MachineFunction object associated with a Function.
137 class FreeMachineFunction : public FunctionPass {
138 public:
139  static char ID;
140 
141  FreeMachineFunction() : FunctionPass(ID) {}
142 
143  void getAnalysisUsage(AnalysisUsage &AU) const override {
146  }
147 
148  bool runOnFunction(Function &F) override {
149  MachineModuleInfo &MMI =
150  getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
152  return true;
153  }
154 
155  StringRef getPassName() const override {
156  return "Free MachineFunction";
157  }
158 };
159 
160 } // end anonymous namespace
161 
163 
165  return new FreeMachineFunction();
166 }
167 
169  const LLVMTargetMachine *TM)
170  : ImmutablePass(ID), MMI(TM) {
172 }
173 
175  const LLVMTargetMachine *TM, MCContext *ExtContext)
176  : ImmutablePass(ID), MMI(TM, ExtContext) {
178 }
179 
180 // Handle the Pass registration stuff necessary to use DataLayout's.
181 INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
182  "Machine Module Information", false, false)
184 
186  std::vector<const MDNode *> &LocInfos) {
187  // Look up a LocInfo for the buffer this diagnostic is coming from.
188  unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
189  const MDNode *LocInfo = nullptr;
190  if (BufNum > 0 && BufNum <= LocInfos.size())
191  LocInfo = LocInfos[BufNum - 1];
192 
193  // If the inline asm had metadata associated with it, pull out a location
194  // cookie corresponding to which line the error occurred on.
195  unsigned LocCookie = 0;
196  if (LocInfo) {
197  unsigned ErrorLine = SMD.getLineNo() - 1;
198  if (ErrorLine >= LocInfo->getNumOperands())
199  ErrorLine = 0;
200 
201  if (LocInfo->getNumOperands() != 0)
202  if (const ConstantInt *CI =
203  mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
204  LocCookie = CI->getZExtValue();
205  }
206 
207  return LocCookie;
208 }
209 
211  MMI.initialize();
212  MMI.TheModule = &M;
213  // FIXME: Do this for new pass manager.
214  LLVMContext &Ctx = M.getContext();
216  [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
217  const SourceMgr &SrcMgr,
218  std::vector<const MDNode *> &LocInfos) {
219  unsigned LocCookie = 0;
220  if (IsInlineAsm)
221  LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
222  Ctx.diagnose(
223  DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
224  });
225  MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
226  !M.debug_compile_units().empty();
227  return false;
228 }
229 
231  MMI.finalize();
232  return false;
233 }
234 
235 AnalysisKey MachineModuleAnalysis::Key;
236 
239  MachineModuleInfo MMI(TM);
240  MMI.TheModule = &M;
241  MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
242  !M.debug_compile_units().empty();
243  return MMI;
244 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::dwarf
Definition: Dwarf.h:34
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:824
llvm::MachineModuleInfoWrapperPass::doFinalization
bool doFinalization(Module &) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: MachineModuleInfo.cpp:230
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:139
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:279
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::Function
Definition: Function.h:59
StringRef.h
Pass.h
ErrorHandling.h
llvm::MachineModuleInfo::insertFunction
void insertFunction(const Function &F, std::unique_ptr< MachineFunction > &&MF)
Add an externally created MachineFunction MF for F.
Definition: MachineModuleInfo.cpp:127
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:138
DenseMap.h
Module.h
llvm::MachineModuleInfo::~MachineModuleInfo
~MachineModuleInfo()
Definition: MachineModuleInfo.cpp:88
llvm::MachineModuleInfo::initialize
void initialize()
Definition: MachineModuleInfo.cpp:41
llvm::MachineModuleInfoImpl::~MachineModuleInfoImpl
virtual ~MachineModuleInfoImpl()
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1297
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
CommandLine.h
llvm::MachineModuleAnalysis::run
MachineModuleInfo run(Module &M, ModuleAnalysisManager &)
Run the analysis pass and produce machine module information.
Definition: MachineModuleInfo.cpp:237
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
TargetMachine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
Constants.h
MCContext.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
llvm::MCContext::setObjectFileInfo
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:444
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::SMDiagnostic
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
false
Definition: StackSlotColoring.cpp:141
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:74
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::MachineModuleInfo::MachineModuleInfo
MachineModuleInfo(const LLVMTargetMachine *TM=nullptr)
Definition: MachineModuleInfo.cpp:70
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MCContext::setDiagnosticHandler
void setDiagnosticHandler(DiagHandlerTy DiagHandler)
Definition: MCContext.h:440
Passes.h
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1291
llvm::cl::opt< bool >
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:203
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::MachineModuleInfo::deleteMachineFunctionFor
void deleteMachineFunctionFor(Function &F)
Delete the MachineFunction MF and reset the link in the IR Function to Machine Function map.
Definition: MachineModuleInfo.cpp:121
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:69
I
#define I(x, y, z)
Definition: MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1862
DisableDebugInfoPrinting
static cl::opt< bool > DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, cl::desc("Disable debug info printing"))
MachineModuleInfo.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MDNode
Metadata node.
Definition: Metadata.h:943
llvm::MachineFunction
Definition: MachineFunction.h:258
llvm::createFreeMachineFunctionPass
FunctionPass * createFreeMachineFunctionPass()
This pass frees the memory occupied by the MachineFunction.
Definition: MachineModuleInfo.cpp:164
llvm::MachineFunction::initTargetMachineFunctionInfo
void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI)
Initialize the target specific MachineFunctionInfo.
Definition: MachineFunction.cpp:236
llvm::SourceMgr::FindBufferContainingLoc
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:73
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::MachineModuleInfoWrapperPass::doInitialization
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: MachineModuleInfo.cpp:210
TargetLoweringObjectFile.h
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
llvm::MachineModuleInfo::getOrCreateMachineFunction
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
Definition: MachineModuleInfo.cpp:96
llvm::TargetSubtargetInfo
TargetSubtargetInfo - Generic base class for all target subtargets.
Definition: TargetSubtargetInfo.h:62
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:85
llvm::LLVMContext::diagnose
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Definition: LLVMContext.cpp:248
std
Definition: BitVector.h:851
getLocCookie
static INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo", "Machine Module Information", false, false) char MachineModuleInfoWrapperPass unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr, std::vector< const MDNode * > &LocInfos)
Definition: MachineModuleInfo.cpp:185
llvm::MachineModuleInfo::getMachineFunction
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
Definition: MachineModuleInfo.cpp:91
llvm::DiagnosticInfoSrcMgr
Diagnostic information for SMDiagnostic reporting.
Definition: DiagnosticInfo.h:1073
DiagnosticInfo.h
llvm::MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass
MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM=nullptr)
Definition: MachineModuleInfo.cpp:168
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:417
llvm::TargetMachine::getSubtargetImpl
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
Definition: TargetMachine.h:134
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::initializeMachineModuleInfoWrapperPassPass
void initializeMachineModuleInfoWrapperPassPass(PassRegistry &)
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
LLVMContext.h
llvm::MachineModuleInfo::finalize
void finalize()
Definition: MachineModuleInfo.cpp:49
llvm::cl::desc
Definition: CommandLine.h:411
MachineFunction.h
InitializePasses.h