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 namespace llvm;
22using namespace llvm::dwarf;
23
24// Out of line virtual method.
25MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
26
27void MachineModuleInfo::initialize() {
28 ObjFileMMI = nullptr;
29 NextFnNum = 0;
30}
31
32void MachineModuleInfo::finalize() {
33 Context.reset();
34 // We don't clear the ExternalContext.
35
36 delete 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(const TargetMachine *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());
56 initialize();
57}
58
59MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM,
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
69MachineModuleInfo::~MachineModuleInfo() { finalize(); }
70
71MachineFunction *
72MachineModuleInfo::getMachineFunction(const Function &F) const {
73 auto I = MachineFunctions.find(Val: &F);
74 return I != 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.
80 if (LastRequest == &F)
81 return *LastResult;
82
83 auto I = MachineFunctions.insert(
84 KV: std::make_pair(x: &F, y: std::unique_ptr<MachineFunction>()));
85 MachineFunction *MF;
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++);
90 MF->initTargetMachineFunctionInfo(STI);
91
92 // MRI callback for target specific initializations.
93 TM.registerMachineRegisterInfoCallback(MF&: *MF);
94
95 // Update the set entry.
96 I.first->second.reset(p: MF);
97 } else {
98 MF = I.first->second.get();
99 }
100
101 LastRequest = &F;
102 LastResult = MF;
103 return *MF;
104}
105
106void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
107 MachineFunctions.erase(Val: &F);
108 LastRequest = nullptr;
109 LastResult = nullptr;
110}
111
112void MachineModuleInfo::insertFunction(const Function &F,
113 std::unique_ptr<MachineFunction> &&MF) {
114 auto I = MachineFunctions.insert(KV: std::make_pair(x: &F, y: 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 {
129 AU.addRequired<MachineModuleInfoWrapperPass>();
130 AU.addPreserved<MachineModuleInfoWrapperPass>();
131 }
132
133 bool runOnFunction(Function &F) override {
134 MachineModuleInfo &MMI =
135 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
136 MMI.deleteMachineFunctionFor(F);
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
149FunctionPass *llvm::createFreeMachineFunctionPass() {
150 return new FreeMachineFunction();
151}
152
153MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
154 const TargetMachine *TM)
155 : ImmutablePass(ID), MMI(TM) {}
156
157MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
158 const TargetMachine *TM, MCContext *ExtContext)
159 : ImmutablePass(ID), MMI(TM, ExtContext) {}
160
161// Handle the Pass registration stuff necessary to use DataLayout's.
162INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
163 "Machine Module Information", false, false)
164char MachineModuleInfoWrapperPass::ID = 0;
165
166static uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
167 std::vector<const MDNode *> &LocInfos) {
168 // Look up a LocInfo for the buffer this diagnostic is coming from.
169 unsigned BufNum = SrcMgr.FindBufferContainingLoc(Loc: SMD.getLoc());
170 const MDNode *LocInfo = nullptr;
171 if (BufNum > 0 && BufNum <= LocInfos.size())
172 LocInfo = LocInfos[BufNum - 1];
173
174 // If the inline asm had metadata associated with it, pull out a location
175 // cookie corresponding to which line the error occurred on.
176 uint64_t LocCookie = 0;
177 if (LocInfo) {
178 unsigned ErrorLine = SMD.getLineNo() - 1;
179 if (ErrorLine >= LocInfo->getNumOperands())
180 ErrorLine = 0;
181
182 if (LocInfo->getNumOperands() != 0)
183 if (const ConstantInt *CI =
184 mdconst::dyn_extract<ConstantInt>(MD: LocInfo->getOperand(I: ErrorLine)))
185 LocCookie = CI->getZExtValue();
186 }
187
188 return LocCookie;
189}
190
191bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
192 MMI.initialize();
193 MMI.TheModule = &M;
194 LLVMContext &Ctx = M.getContext();
195 MMI.getContext().setDiagnosticHandler(
196 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
197 const SourceMgr &SrcMgr,
198 std::vector<const MDNode *> &LocInfos) {
199 uint64_t LocCookie = 0;
200 if (IsInlineAsm)
201 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
202 Ctx.diagnose(
203 DI: DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
204 });
205 return false;
206}
207
208bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
209 MMI.finalize();
210 return false;
211}
212
213AnalysisKey MachineModuleAnalysis::Key;
214
215MachineModuleAnalysis::Result
216MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
217 MMI.TheModule = &M;
218 LLVMContext &Ctx = M.getContext();
219 MMI.getContext().setDiagnosticHandler(
220 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
221 const SourceMgr &SrcMgr,
222 std::vector<const MDNode *> &LocInfos) {
223 unsigned LocCookie = 0;
224 if (IsInlineAsm)
225 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
226 Ctx.diagnose(
227 DI: DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
228 });
229 return Result(MMI);
230}
231