Movatterモバイル変換


[0]ホーム

URL:


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
9#include "llvm/CodeGen/MachineModuleInfo.h"
10#include "llvm/CodeGen/MachineFunction.h"
11#include "llvm/CodeGen/Passes.h"
12#include "llvm/IR/Constants.h"
13#include "llvm/IR/DiagnosticInfo.h"
14#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
16#include "llvm/InitializePasses.h"
17#include "llvm/Target/TargetLoweringObjectFile.h"
18#include "llvm/Target/TargetMachine.h"
19#include <cassert>
20
21using namespacellvm;
22using namespacellvm::dwarf;
23
24// Out of line virtual method.
25MachineModuleInfoImpl::~MachineModuleInfoImpl() =default;
26
27voidMachineModuleInfo::initialize() {
28 ObjFileMMI =nullptr;
29 NextFnNum = 0;
30}
31
32voidMachineModuleInfo::finalize() {
33 Context.reset();
34// We don't clear the ExternalContext.
35
36delete ObjFileMMI;
37 ObjFileMMI =nullptr;
38}
39
40MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
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)) {
45 Context.setObjectFileInfo(TM.getObjFileLowering());
46 ObjFileMMI = MMI.ObjFileMMI;
47 ExternalContext = MMI.ExternalContext;
48 TheModule = MMI.TheModule;
49}
50
51MachineModuleInfo::MachineModuleInfo(constTargetMachine *TM)
52 : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
53 TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
54 nullptr, &TM->Options.MCOptions,false) {
55 Context.setObjectFileInfo(TM->getObjFileLowering());
56initialize();
57}
58
59MachineModuleInfo::MachineModuleInfo(constTargetMachine *TM,
60MCContext *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());
66initialize();
67}
68
69MachineModuleInfo::~MachineModuleInfo() {finalize(); }
70
71MachineFunction *
72MachineModuleInfo::getMachineFunction(constFunction &F) const{
73autoI = MachineFunctions.find(&F);
74returnI != MachineFunctions.end() ?I->second.get() :nullptr;
75}
76
77MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F) {
78// Shortcut for the common case where a sequence of MachineFunctionPasses
79// all query for the same Function.
80if (LastRequest == &F)
81return *LastResult;
82
83autoI = MachineFunctions.insert(
84 std::make_pair(&F, std::unique_ptr<MachineFunction>()));
85MachineFunction *MF;
86if (I.second) {
87// No pre-existing machine function, create a new one.
88constTargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
89 MF =newMachineFunction(F, TM, STI,getContext(), NextFnNum++);
90 MF->initTargetMachineFunctionInfo(STI);
91
92// MRI callback for target specific initializations.
93 TM.registerMachineRegisterInfoCallback(*MF);
94
95// Update the set entry.
96I.first->second.reset(MF);
97 }else {
98 MF =I.first->second.get();
99 }
100
101 LastRequest = &F;
102 LastResult = MF;
103return *MF;
104}
105
106voidMachineModuleInfo::deleteMachineFunctionFor(Function &F) {
107 MachineFunctions.erase(&F);
108 LastRequest =nullptr;
109 LastResult =nullptr;
110}
111
112voidMachineModuleInfo::insertFunction(constFunction &F,
113 std::unique_ptr<MachineFunction> &&MF) {
114autoI = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
115assert(I.second &&"machine function already mapped");
116 (void)I;
117}
118
119namespace{
120
121/// This pass frees the MachineFunction object associated with a Function.
122classFreeMachineFunction :publicFunctionPass {
123public:
124staticcharID;
125
126 FreeMachineFunction() :FunctionPass(ID) {}
127
128void getAnalysisUsage(AnalysisUsage &AU) const override{
129 AU.addRequired<MachineModuleInfoWrapperPass>();
130 AU.addPreserved<MachineModuleInfoWrapperPass>();
131 }
132
133boolrunOnFunction(Function &F) override{
134MachineModuleInfo &MMI =
135 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
136 MMI.deleteMachineFunctionFor(F);
137returntrue;
138 }
139
140StringRef getPassName() const override{
141return"Free MachineFunction";
142 }
143};
144
145}// end anonymous namespace
146
147char FreeMachineFunction::ID;
148
149FunctionPass *llvm::createFreeMachineFunctionPass() {
150returnnew FreeMachineFunction();
151}
152
153MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
154constTargetMachine *TM)
155 :ImmutablePass(ID), MMI(TM) {
156initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
157}
158
159MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
160constTargetMachine *TM,MCContext *ExtContext)
161 :ImmutablePass(ID), MMI(TM, ExtContext) {
162initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
163}
164
165// Handle the Pass registration stuff necessary to use DataLayout's.
166INITIALIZE_PASS(MachineModuleInfoWrapperPass,"machinemoduleinfo",
167"Machine Module Information",false,false)
168charMachineModuleInfoWrapperPass::ID = 0;
169
170staticuint64_tgetLocCookie(constSMDiagnostic &SMD,constSourceMgr &SrcMgr,
171std::vector<constMDNode *> &LocInfos) {
172// Look up a LocInfo for the buffer this diagnostic is coming from.
173unsigned BufNum =SrcMgr.FindBufferContainingLoc(SMD.getLoc());
174constMDNode *LocInfo =nullptr;
175if (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.
180uint64_t LocCookie = 0;
181if (LocInfo) {
182unsigned ErrorLine = SMD.getLineNo() - 1;
183if (ErrorLine >= LocInfo->getNumOperands())
184 ErrorLine = 0;
185
186if (LocInfo->getNumOperands() != 0)
187if (constConstantInt *CI =
188 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
189 LocCookie = CI->getZExtValue();
190 }
191
192return LocCookie;
193}
194
195boolMachineModuleInfoWrapperPass::doInitialization(Module &M) {
196 MMI.initialize();
197 MMI.TheModule = &M;
198LLVMContext &Ctx = M.getContext();
199 MMI.getContext().setDiagnosticHandler(
200 [&Ctx, &M](constSMDiagnostic &SMD,bool IsInlineAsm,
201constSourceMgr &SrcMgr,
202 std::vector<const MDNode *> &LocInfos) {
203uint64_t LocCookie = 0;
204if (IsInlineAsm)
205 LocCookie =getLocCookie(SMD,SrcMgr, LocInfos);
206 Ctx.diagnose(
207DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
208 });
209returnfalse;
210}
211
212boolMachineModuleInfoWrapperPass::doFinalization(Module &M) {
213 MMI.finalize();
214returnfalse;
215}
216
217AnalysisKey MachineModuleAnalysis::Key;
218
219MachineModuleAnalysis::Result
220MachineModuleAnalysis::run(Module &M,ModuleAnalysisManager &) {
221 MMI.TheModule = &M;
222LLVMContext &Ctx = M.getContext();
223 MMI.getContext().setDiagnosticHandler(
224 [&Ctx, &M](constSMDiagnostic &SMD,bool IsInlineAsm,
225constSourceMgr &SrcMgr,
226 std::vector<const MDNode *> &LocInfos) {
227unsigned LocCookie = 0;
228if (IsInlineAsm)
229 LocCookie =getLocCookie(SMD,SrcMgr, LocInfos);
230 Ctx.diagnose(
231DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
232 });
233returnResult(MMI);
234}
const
aarch64 promote const
Definition:AArch64PromoteConstant.cpp:230
vector
AMDGPU promote alloca to vector or false DEBUG_TYPE to vector
Definition:AMDGPUPromoteAlloca.cpp:214
Passes.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DiagnosticInfo.h
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition:EntryExitInstrumenter.cpp:98
Module.h
Module.h This file contains the declarations for the Module class.
InitializePasses.h
LLVMContext.h
Options
static LVOptions Options
Definition:LVOptions.cpp:25
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
MachineFunction.h
getLocCookie
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)
Definition:MachineModuleInfo.cpp:170
MachineModuleInfo.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:38
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
TargetLoweringObjectFile.h
char
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition:PassManager.h:253
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition:PassAnalysisSupport.h:75
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition:PassAnalysisSupport.h:98
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::DiagnosticInfoSrcMgr
Diagnostic information for SMDiagnostic reporting.
Definition:DiagnosticInfo.h:1162
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition:Pass.h:310
llvm::Function
Definition:Function.h:63
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition:Pass.h:281
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::LLVMContext::diagnose
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Definition:LLVMContext.cpp:245
llvm::MCContext
Context object for machine code objects.
Definition:MCContext.h:83
llvm::MCContext::setObjectFileInfo
void setObjectFileInfo(const MCObjectFileInfo *Mofi)
Definition:MCContext.h:410
llvm::MCContext::setDiagnosticHandler
void setDiagnosticHandler(DiagHandlerTy DiagHandler)
Definition:MCContext.h:406
llvm::MCContext::reset
void reset()
reset - return object to right after construction state to prepare to process a new module
Definition:MCContext.cpp:135
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition:Metadata.h:1434
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition:Metadata.h:1440
llvm::MachineFunction
Definition:MachineFunction.h:267
llvm::MachineFunction::initTargetMachineFunctionInfo
void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI)
Initialize the target specific MachineFunctionInfo.
Definition:MachineFunction.cpp:253
llvm::MachineModuleAnalysis::Result
Definition:MachineModuleInfo.h:200
llvm::MachineModuleAnalysis::run
Result run(Module &M, ModuleAnalysisManager &)
Run the analysis pass and produce machine module information.
Definition:MachineModuleInfo.cpp:220
llvm::MachineModuleInfoImpl::~MachineModuleInfoImpl
virtual ~MachineModuleInfoImpl()
llvm::MachineModuleInfoWrapperPass
Definition:MachineModuleInfo.h:170
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:212
llvm::MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass
MachineModuleInfoWrapperPass(const TargetMachine *TM=nullptr)
Definition:MachineModuleInfo.cpp:153
llvm::MachineModuleInfoWrapperPass::doInitialization
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition:MachineModuleInfo.cpp:195
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition:MachineModuleInfo.h:82
llvm::MachineModuleInfo::MachineModuleInfo
MachineModuleInfo(const TargetMachine *TM=nullptr)
Definition:MachineModuleInfo.cpp:51
llvm::MachineModuleInfo::initialize
void initialize()
Definition:MachineModuleInfo.cpp:27
llvm::MachineModuleInfo::insertFunction
void insertFunction(const Function &F, std::unique_ptr< MachineFunction > &&MF)
Add an externally created MachineFunction MF for F.
Definition:MachineModuleInfo.cpp:112
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition:MachineModuleInfo.h:125
llvm::MachineModuleInfo::getOrCreateMachineFunction
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
Definition:MachineModuleInfo.cpp:77
llvm::MachineModuleInfo::~MachineModuleInfo
~MachineModuleInfo()
Definition:MachineModuleInfo.cpp:69
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:72
llvm::MachineModuleInfo::finalize
void finalize()
Definition:MachineModuleInfo.cpp:32
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:106
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition:PassRegistry.cpp:24
llvm::SMDiagnostic
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition:SourceMgr.h:281
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition:SourceMgr.h:31
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:51
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition:TargetMachine.h:77
llvm::TargetMachine::getObjFileLowering
virtual TargetLoweringObjectFile * getObjFileLowering() const
Definition:TargetMachine.h:136
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::TargetMachine::registerMachineRegisterInfoCallback
virtual void registerMachineRegisterInfoCallback(MachineFunction &MF) const
Definition:TargetMachine.h:489
llvm::TargetSubtargetInfo
TargetSubtargetInfo - Generic base class for all target subtargets.
Definition:TargetSubtargetInfo.h:63
uint64_t
unsigned
TargetMachine.h
false
Definition:StackSlotColoring.cpp:193
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition:CallingConv.h:24
llvm::dwarf
Calculates the starting offsets for various sections within the .debug_names section.
Definition:Dwarf.h:34
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::initializeMachineModuleInfoWrapperPassPass
void initializeMachineModuleInfoWrapperPassPass(PassRegistry &)
llvm::SrcMgr
SourceMgr SrcMgr
Definition:Error.cpp:24
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:1873
llvm::createFreeMachineFunctionPass
FunctionPass * createFreeMachineFunctionPass()
This pass frees the memory occupied by the MachineFunction.
Definition:MachineModuleInfo.cpp:149
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition:Analysis.h:28

Generated on Fri Jul 18 2025 10:46:31 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp