LLVM 24.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, false),
44 MachineFunctions(std::move(MMI.MachineFunctions)) {
45 Context.setObjectFileInfo(TM.getObjFileLowering());
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, 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, 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.
93 TM.registerMachineRegisterInfoCallback(*MF);
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 FinalizedMFs.insert(&F);
108 LastRequest = nullptr;
109 LastResult = nullptr;
110 auto Leader = MFDeletionGrouping.findLeader(&F);
111 if (Leader == MFDeletionGrouping.member_end()) {
112 MachineFunctions.erase(&F);
113 return;
114 }
115
116 if (llvm::all_of(MFDeletionGrouping.members(*Leader),
117 [this](const Function *Member) {
118 return FinalizedMFs.count(Member);
119 })) {
120 // All functions in the same deletion grouping have been finalized,
121 // so delete all of them.
122 for (const Function *Member : MFDeletionGrouping.members(*Leader)) {
123 MachineFunctions.erase(Member);
124 }
125 }
126}
127
129 std::unique_ptr<MachineFunction> &&MF) {
130 auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
131 assert(I.second && "machine function already mapped");
132 (void)I;
133}
134
135namespace {
136
137/// This pass frees the MachineFunction object associated with a Function.
138class FreeMachineFunction : public FunctionPass {
139public:
140 static char ID;
141
142 FreeMachineFunction() : FunctionPass(ID) {}
143
144 void getAnalysisUsage(AnalysisUsage &AU) const override {
147 }
148
149 bool runOnFunction(Function &F) override {
150 MachineModuleInfo &MMI =
151 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
153 return true;
154 }
155
156 StringRef getPassName() const override {
157 return "Free MachineFunction";
158 }
159};
160
161} // end anonymous namespace
162
163char FreeMachineFunction::ID;
164
166 return new FreeMachineFunction();
167}
168
172
176
177// Handle the Pass registration stuff necessary to use DataLayout's.
179 "Machine Module Information", false, false)
181
183 std::vector<const MDNode *> &LocInfos) {
184 // Look up a LocInfo for the buffer this diagnostic is coming from.
185 unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
186 const MDNode *LocInfo = nullptr;
187 if (BufNum > 0 && BufNum <= LocInfos.size())
188 LocInfo = LocInfos[BufNum - 1];
189
190 // If the inline asm had metadata associated with it, pull out a location
191 // cookie corresponding to which line the error occurred on.
192 uint64_t LocCookie = 0;
193 if (LocInfo) {
194 unsigned ErrorLine = SMD.getLineNo() - 1;
195 if (ErrorLine >= LocInfo->getNumOperands())
196 ErrorLine = 0;
197
198 if (LocInfo->getNumOperands() != 0)
199 if (const ConstantInt *CI =
200 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
201 LocCookie = CI->getZExtValue();
202 }
203
204 return LocCookie;
205}
206
208 MMI.initialize();
209 MMI.TheModule = &M;
210 LLVMContext &Ctx = M.getContext();
211 MMI.getContext().setDiagnosticHandler(
212 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
213 const SourceMgr &SrcMgr,
214 std::vector<const MDNode *> &LocInfos) {
215 uint64_t LocCookie = 0;
216 if (IsInlineAsm)
217 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
218 Ctx.diagnose(
219 DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
220 });
221 return false;
222}
223
225 MMI.finalize();
226 return false;
227}
228
229AnalysisKey MachineModuleAnalysis::Key;
230
233 MMI.TheModule = &M;
234 LLVMContext &Ctx = M.getContext();
235 MMI.getContext().setDiagnosticHandler(
236 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
237 const SourceMgr &SrcMgr,
238 std::vector<const MDNode *> &LocInfos) {
239 unsigned LocCookie = 0;
240 if (IsInlineAsm)
241 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
242 Ctx.diagnose(
243 DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
244 });
245 return Result(MMI);
246}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
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)
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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:87
Diagnostic information for SMDiagnostic reporting.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
ImmutablePass(char &pid)
Definition Pass.h:287
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void setDiagnosticHandler(std::unique_ptr< DiagnosticHandler > &&DH, bool RespectFilters=false)
setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler to provide custom d...
Context object for machine code objects.
Definition MCContext.h:83
Metadata node.
Definition Metadata.h:1069
void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI)
Initialize the target specific MachineFunctionInfo.
void insert(iterator MBBI, MachineBasicBlock *MBB)
LLVM_ABI 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 TargetMachine *TM=nullptr)
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
LLVM_ABI MachineModuleInfo(const TargetMachine *TM=nullptr)
LLVM_ABI void insertFunction(const Function &F, std::unique_ptr< MachineFunction > &&MF)
Add an externally created MachineFunction MF for F.
const MCContext & getContext() const
LLVM_ABI MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
LLVM_ABI MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
LLVM_ABI 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:67
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:308
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
Primary interface to the complete machine description for the target machine.
TargetSubtargetInfo - Generic base class for all target subtargets.
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:696
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI 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:1917
LLVM_ABI FunctionPass * createFreeMachineFunctionPass()
This pass frees the memory occupied by the MachineFunction.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29