LLVM 18.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"
16#include "llvm/IR/LLVMContext.h"
17#include "llvm/IR/Module.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
31using namespace llvm;
32using namespace llvm::dwarf;
33
34static 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(TM.getTargetTriple(), TM.getMCAsmInfo(), TM.getMCRegisterInfo(),
60 TM.getMCSubtargetInfo(), nullptr, &TM.Options.MCOptions, false),
61 MachineFunctions(std::move(MMI.MachineFunctions)) {
63 ObjFileMMI = MMI.ObjFileMMI;
64 CurCallSite = MMI.CurCallSite;
65 ExternalContext = MMI.ExternalContext;
66 TheModule = MMI.TheModule;
67}
68
70 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
71 TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
72 nullptr, &TM->Options.MCOptions, false) {
73 Context.setObjectFileInfo(TM->getObjFileLowering());
74 initialize();
75}
76
78 MCContext *ExtContext)
79 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
80 TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
81 nullptr, &TM->Options.MCOptions, false),
82 ExternalContext(ExtContext) {
83 Context.setObjectFileInfo(TM->getObjFileLowering());
84 initialize();
85}
86
88
91 auto I = MachineFunctions.find(&F);
92 return I != MachineFunctions.end() ? I->second.get() : nullptr;
93}
94
96 // Shortcut for the common case where a sequence of MachineFunctionPasses
97 // all query for the same Function.
98 if (LastRequest == &F)
99 return *LastResult;
100
101 auto I = MachineFunctions.insert(
102 std::make_pair(&F, std::unique_ptr<MachineFunction>()));
103 MachineFunction *MF;
104 if (I.second) {
105 // No pre-existing machine function, create a new one.
106 const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
107 MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
109
110 // MRI callback for target specific initializations.
112
113 // Update the set entry.
114 I.first->second.reset(MF);
115 } else {
116 MF = I.first->second.get();
117 }
118
119 LastRequest = &F;
120 LastResult = MF;
121 return *MF;
122}
123
125 MachineFunctions.erase(&F);
126 LastRequest = nullptr;
127 LastResult = nullptr;
128}
129
131 std::unique_ptr<MachineFunction> &&MF) {
132 auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
133 assert(I.second && "machine function already mapped");
134 (void)I;
135}
136
137namespace {
138
139/// This pass frees the MachineFunction object associated with a Function.
140class FreeMachineFunction : public FunctionPass {
141public:
142 static char ID;
143
144 FreeMachineFunction() : FunctionPass(ID) {}
145
146 void getAnalysisUsage(AnalysisUsage &AU) const override {
149 }
150
151 bool runOnFunction(Function &F) override {
152 MachineModuleInfo &MMI =
153 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
155 return true;
156 }
157
158 StringRef getPassName() const override {
159 return "Free MachineFunction";
160 }
161};
162
163} // end anonymous namespace
164
165char FreeMachineFunction::ID;
166
168 return new FreeMachineFunction();
169}
170
172 const LLVMTargetMachine *TM)
173 : ImmutablePass(ID), MMI(TM) {
175}
176
178 const LLVMTargetMachine *TM, MCContext *ExtContext)
179 : ImmutablePass(ID), MMI(TM, ExtContext) {
181}
182
183// Handle the Pass registration stuff necessary to use DataLayout's.
185 "Machine Module Information", false, false)
187
189 std::vector<const MDNode *> &LocInfos) {
190 // Look up a LocInfo for the buffer this diagnostic is coming from.
191 unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
192 const MDNode *LocInfo = nullptr;
193 if (BufNum > 0 && BufNum <= LocInfos.size())
194 LocInfo = LocInfos[BufNum - 1];
195
196 // If the inline asm had metadata associated with it, pull out a location
197 // cookie corresponding to which line the error occurred on.
198 unsigned LocCookie = 0;
199 if (LocInfo) {
200 unsigned ErrorLine = SMD.getLineNo() - 1;
201 if (ErrorLine >= LocInfo->getNumOperands())
202 ErrorLine = 0;
203
204 if (LocInfo->getNumOperands() != 0)
205 if (const ConstantInt *CI =
206 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
207 LocCookie = CI->getZExtValue();
208 }
209
210 return LocCookie;
211}
212
214 MMI.initialize();
215 MMI.TheModule = &M;
216 // FIXME: Do this for new pass manager.
217 LLVMContext &Ctx = M.getContext();
219 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
220 const SourceMgr &SrcMgr,
221 std::vector<const MDNode *> &LocInfos) {
222 unsigned LocCookie = 0;
223 if (IsInlineAsm)
224 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
225 Ctx.diagnose(
226 DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
227 });
228 MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
229 !M.debug_compile_units().empty();
230 return false;
231}
232
234 MMI.finalize();
235 return false;
236}
237
238AnalysisKey MachineModuleAnalysis::Key;
239
242 MachineModuleInfo MMI(TM);
243 MMI.TheModule = &M;
244 MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
245 !M.debug_compile_units().empty();
246 return MMI;
247}
aarch64 promote const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
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)
static cl::opt< bool > DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, cl::desc("Disable debug info printing"))
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:649
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
Diagnostic information for SMDiagnostic reporting.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:282
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
This class describes a target machine that is implemented with the LLVM target-independent code gener...
virtual void registerMachineRegisterInfoCallback(MachineFunction &MF) const
Context object for machine code objects.
Definition: MCContext.h:76
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:444
void setDiagnosticHandler(DiagHandlerTy DiagHandler)
Definition: MCContext.h:440
void reset()
reset - return object to right after construction state to prepare to process a new module
Definition: MCContext.cpp:134
Metadata node.
Definition: Metadata.h:1037
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1391
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1397
void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI)
Initialize the target specific MachineFunctionInfo.
MachineModuleInfo run(Module &M, ModuleAnalysisManager &)
Run the analysis pass and produce machine module information.
bool doFinalization(Module &) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM=nullptr)
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
This class contains meta information specific to a module.
MachineModuleInfo(const LLVMTargetMachine *TM=nullptr)
void insertFunction(const Function &F, std::unique_ptr< MachineFunction > &&MF)
Add an externally created MachineFunction MF for F.
const MCContext & getContext() const
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
void deleteMachineFunctionFor(Function &F)
Delete the MachineFunction MF and reset the link in the IR Function to Machine Function map.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition: SourceMgr.cpp:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
virtual TargetLoweringObjectFile * getObjFileLowering() const
virtual const TargetSubtargetInfo * getSubtargetImpl(const Function &) const
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
TargetSubtargetInfo - Generic base class for all target subtargets.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeMachineModuleInfoWrapperPassPass(PassRegistry &)
SourceMgr SrcMgr
Definition: Error.cpp:24
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:1853
FunctionPass * createFreeMachineFunctionPass()
This pass frees the memory occupied by the MachineFunction.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:89