LLVM  15.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  Personalities.clear();
51 
52  Context.reset();
53  // We don't clear the ExternalContext.
54 
55  delete ObjFileMMI;
56  ObjFileMMI = nullptr;
57 }
58 
60  : TM(std::move(MMI.TM)),
61  Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
62  MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
63  nullptr, false),
64  MachineFunctions(std::move(MMI.MachineFunctions)) {
65  Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
66  ObjFileMMI = MMI.ObjFileMMI;
67  CurCallSite = MMI.CurCallSite;
68  ExternalContext = MMI.ExternalContext;
69  TheModule = MMI.TheModule;
70 }
71 
73  : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
74  TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
75  nullptr, nullptr, false) {
76  Context.setObjectFileInfo(TM->getObjFileLowering());
77  initialize();
78 }
79 
81  MCContext *ExtContext)
82  : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
83  TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
84  nullptr, nullptr, false),
85  ExternalContext(ExtContext) {
86  Context.setObjectFileInfo(TM->getObjFileLowering());
87  initialize();
88 }
89 
91 
92 /// \name Exception Handling
93 /// \{
94 
95 void MachineModuleInfo::addPersonality(const Function *Personality) {
96  if (!llvm::is_contained(Personalities, Personality))
97  Personalities.push_back(Personality);
98 }
99 
100 /// \}
101 
104  auto I = MachineFunctions.find(&F);
105  return I != MachineFunctions.end() ? I->second.get() : nullptr;
106 }
107 
109  // Shortcut for the common case where a sequence of MachineFunctionPasses
110  // all query for the same Function.
111  if (LastRequest == &F)
112  return *LastResult;
113 
114  auto I = MachineFunctions.insert(
115  std::make_pair(&F, std::unique_ptr<MachineFunction>()));
116  MachineFunction *MF;
117  if (I.second) {
118  // No pre-existing machine function, create a new one.
119  const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
120  MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
121  // Update the set entry.
122  I.first->second.reset(MF);
123  } else {
124  MF = I.first->second.get();
125  }
126 
127  LastRequest = &F;
128  LastResult = MF;
129  return *MF;
130 }
131 
133  MachineFunctions.erase(&F);
134  LastRequest = nullptr;
135  LastResult = nullptr;
136 }
137 
139  std::unique_ptr<MachineFunction> &&MF) {
140  auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
141  assert(I.second && "machine function already mapped");
142  (void)I;
143 }
144 
145 namespace {
146 
147 /// This pass frees the MachineFunction object associated with a Function.
148 class FreeMachineFunction : public FunctionPass {
149 public:
150  static char ID;
151 
152  FreeMachineFunction() : FunctionPass(ID) {}
153 
154  void getAnalysisUsage(AnalysisUsage &AU) const override {
157  }
158 
159  bool runOnFunction(Function &F) override {
160  MachineModuleInfo &MMI =
161  getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
163  return true;
164  }
165 
166  StringRef getPassName() const override {
167  return "Free MachineFunction";
168  }
169 };
170 
171 } // end anonymous namespace
172 
174 
176  return new FreeMachineFunction();
177 }
178 
180  const LLVMTargetMachine *TM)
181  : ImmutablePass(ID), MMI(TM) {
183 }
184 
186  const LLVMTargetMachine *TM, MCContext *ExtContext)
187  : ImmutablePass(ID), MMI(TM, ExtContext) {
189 }
190 
191 // Handle the Pass registration stuff necessary to use DataLayout's.
192 INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
193  "Machine Module Information", false, false)
195 
197  std::vector<const MDNode *> &LocInfos) {
198  // Look up a LocInfo for the buffer this diagnostic is coming from.
199  unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
200  const MDNode *LocInfo = nullptr;
201  if (BufNum > 0 && BufNum <= LocInfos.size())
202  LocInfo = LocInfos[BufNum - 1];
203 
204  // If the inline asm had metadata associated with it, pull out a location
205  // cookie corresponding to which line the error occurred on.
206  unsigned LocCookie = 0;
207  if (LocInfo) {
208  unsigned ErrorLine = SMD.getLineNo() - 1;
209  if (ErrorLine >= LocInfo->getNumOperands())
210  ErrorLine = 0;
211 
212  if (LocInfo->getNumOperands() != 0)
213  if (const ConstantInt *CI =
214  mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
215  LocCookie = CI->getZExtValue();
216  }
217 
218  return LocCookie;
219 }
220 
222  MMI.initialize();
223  MMI.TheModule = &M;
224  // FIXME: Do this for new pass manager.
225  LLVMContext &Ctx = M.getContext();
227  [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
228  const SourceMgr &SrcMgr,
229  std::vector<const MDNode *> &LocInfos) {
230  unsigned LocCookie = 0;
231  if (IsInlineAsm)
232  LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
233  Ctx.diagnose(
234  DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
235  });
236  MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
237  !M.debug_compile_units().empty();
238  return false;
239 }
240 
242  MMI.finalize();
243  return false;
244 }
245 
246 AnalysisKey MachineModuleAnalysis::Key;
247 
250  MachineModuleInfo MMI(TM);
251  MMI.TheModule = &M;
252  MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
253  !M.debug_compile_units().empty();
254  return MMI;
255 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:35
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:241
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:144
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:74
llvm::Function
Definition: Function.h:60
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:138
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
DenseMap.h
Module.h
llvm::MachineModuleInfo::~MachineModuleInfo
~MachineModuleInfo()
Definition: MachineModuleInfo.cpp:90
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:1199
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:248
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
TargetMachine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Constants.h
MCContext.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
llvm::MCContext::setObjectFileInfo
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:437
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:75
llvm::MachineModuleInfo::MachineModuleInfo
MachineModuleInfo(const LLVMTargetMachine *TM=nullptr)
Definition: MachineModuleInfo.cpp:72
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:433
Passes.h
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1193
llvm::cl::opt< bool >
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:215
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:132
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:68
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
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1670
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:1663
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:926
llvm::MachineFunction
Definition: MachineFunction.h:241
llvm::createFreeMachineFunctionPass
FunctionPass * createFreeMachineFunctionPass()
This pass frees the memory occupied by the MachineFunction.
Definition: MachineModuleInfo.cpp:175
llvm::SourceMgr::FindBufferContainingLoc
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:69
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MachineModuleInfoWrapperPass::doInitialization
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: MachineModuleInfo.cpp:221
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:108
llvm::TargetSubtargetInfo
TargetSubtargetInfo - Generic base class for all target subtargets.
Definition: TargetSubtargetInfo.h:60
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::LLVMContext::diagnose
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Definition: LLVMContext.cpp:243
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:196
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:103
llvm::DiagnosticInfoSrcMgr
Diagnostic information for SMDiagnostic reporting.
Definition: DiagnosticInfo.h:1074
DiagnosticInfo.h
llvm::MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass
MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM=nullptr)
Definition: MachineModuleInfo.cpp:179
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:405
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:133
llvm::MachineModuleInfo::addPersonality
void addPersonality(const Function *Personality)
Provide the personality function for the exception information.
Definition: MachineModuleInfo.cpp:95
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:405
MachineFunction.h
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37