LLVM 20.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
11#include "llvm/CodeGen/Passes.h"
12#include "llvm/IR/Constants.h"
14#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
19#include <cassert>
20
21using namespace llvm;
22using namespace llvm::dwarf;
23
24// Out of line virtual method.
26
28 ObjFileMMI = nullptr;
29 NextFnNum = 0;
30}
31
33 Context.reset();
34 // We don't clear the ExternalContext.
35
36 delete ObjFileMMI;
37 ObjFileMMI = nullptr;
38}
39
41 : TM(std::move(MMI.TM)),
42 Context(TM.getTargetTriple(), TM.getMCAsmInfo(), TM.getMCRegisterInfo(),
43 TM.getMCSubtargetInfo(), nullptr, &TM.Options.MCOptions, false),
44 MachineFunctions(std::move(MMI.MachineFunctions)) {
46 ObjFileMMI = MMI.ObjFileMMI;
47 ExternalContext = MMI.ExternalContext;
48 TheModule = MMI.TheModule;
49}
50
52 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
53 TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
54 nullptr, &TM->Options.MCOptions, false) {
55 Context.setObjectFileInfo(TM->getObjFileLowering());
56 initialize();
57}
58
60 MCContext *ExtContext)
61 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
62 TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
63 nullptr, &TM->Options.MCOptions, false),
64 ExternalContext(ExtContext) {
65 Context.setObjectFileInfo(TM->getObjFileLowering());
66 initialize();
67}
68
70
73 auto I = MachineFunctions.find(&F);
74 return I != MachineFunctions.end() ? I->second.get() : nullptr;
75}
76
78 // Shortcut for the common case where a sequence of MachineFunctionPasses
79 // all query for the same Function.
80 if (LastRequest == &F)
81 return *LastResult;
82
83 auto I = MachineFunctions.insert(
84 std::make_pair(&F, std::unique_ptr<MachineFunction>()));
86 if (I.second) {
87 // No pre-existing machine function, create a new one.
88 const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
89 MF = new MachineFunction(F, TM, STI, getContext(), NextFnNum++);
91
92 // MRI callback for target specific initializations.
94
95 // Update the set entry.
96 I.first->second.reset(MF);
97 } else {
98 MF = I.first->second.get();
99 }
100
101 LastRequest = &F;
102 LastResult = MF;
103 return *MF;
104}
105
107 MachineFunctions.erase(&F);
108 LastRequest = nullptr;
109 LastResult = nullptr;
110}
111
113 std::unique_ptr<MachineFunction> &&MF) {
114 auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
115 assert(I.second && "machine function already mapped");
116 (void)I;
117}
118
119namespace {
120
121/// This pass frees the MachineFunction object associated with a Function.
122class FreeMachineFunction : public FunctionPass {
123public:
124 static char ID;
125
126 FreeMachineFunction() : FunctionPass(ID) {}
127
128 void getAnalysisUsage(AnalysisUsage &AU) const override {
131 }
132
133 bool runOnFunction(Function &F) override {
134 MachineModuleInfo &MMI =
135 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
137 return true;
138 }
139
140 StringRef getPassName() const override {
141 return "Free MachineFunction";
142 }
143};
144
145} // end anonymous namespace
146
147char FreeMachineFunction::ID;
148
150 return new FreeMachineFunction();
151}
152
154 const LLVMTargetMachine *TM)
155 : ImmutablePass(ID), MMI(TM) {
157}
158
160 const LLVMTargetMachine *TM, MCContext *ExtContext)
161 : ImmutablePass(ID), MMI(TM, ExtContext) {
163}
164
165// Handle the Pass registration stuff necessary to use DataLayout's.
167 "Machine Module Information", false, false)
169
171 std::vector<const MDNode *> &LocInfos) {
172 // Look up a LocInfo for the buffer this diagnostic is coming from.
173 unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
174 const MDNode *LocInfo = nullptr;
175 if (BufNum > 0 && BufNum <= LocInfos.size())
176 LocInfo = LocInfos[BufNum - 1];
177
178 // If the inline asm had metadata associated with it, pull out a location
179 // cookie corresponding to which line the error occurred on.
180 uint64_t LocCookie = 0;
181 if (LocInfo) {
182 unsigned ErrorLine = SMD.getLineNo() - 1;
183 if (ErrorLine >= LocInfo->getNumOperands())
184 ErrorLine = 0;
185
186 if (LocInfo->getNumOperands() != 0)
187 if (const ConstantInt *CI =
188 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
189 LocCookie = CI->getZExtValue();
190 }
191
192 return LocCookie;
193}
194
196 MMI.initialize();
197 MMI.TheModule = &M;
198 LLVMContext &Ctx = M.getContext();
200 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
201 const SourceMgr &SrcMgr,
202 std::vector<const MDNode *> &LocInfos) {
203 uint64_t LocCookie = 0;
204 if (IsInlineAsm)
205 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
206 Ctx.diagnose(
207 DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
208 });
209 return false;
210}
211
213 MMI.finalize();
214 return false;
215}
216
217AnalysisKey MachineModuleAnalysis::Key;
218
221 MMI.TheModule = &M;
222 LLVMContext &Ctx = M.getContext();
224 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
225 const SourceMgr &SrcMgr,
226 std::vector<const MDNode *> &LocInfos) {
227 unsigned LocCookie = 0;
228 if (IsInlineAsm)
229 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
230 Ctx.diagnose(
231 DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
232 });
233 return Result(MMI);
234}
aarch64 promote const
AMDGPU promote alloca to vector or false DEBUG_TYPE to vector
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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 uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr, std::vector< const MDNode * > &LocInfos)
Module.h This file contains the declarations for the Module class.
#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:253
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:81
Diagnostic information for SMDiagnostic reporting.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:281
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:83
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition: MCContext.h:410
void setDiagnosticHandler(DiagHandlerTy DiagHandler)
Definition: MCContext.h:406
void reset()
reset - return object to right after construction state to prepare to process a new module
Definition: MCContext.cpp:136
Metadata node.
Definition: Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1430
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1436
void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI)
Initialize the target specific MachineFunctionInfo.
Result 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
Calculates the starting offsets for various sections within the .debug_names section.
Definition: Dwarf.h:34
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:1856
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: Analysis.h:28