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, 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, 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, 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 FinalizedMFs.insert(V: &F);
108 LastRequest = nullptr;
109 LastResult = nullptr;
110 auto Leader = MFDeletionGrouping.findLeader(V: &F);
111 if (Leader == MFDeletionGrouping.member_end()) {
112 MachineFunctions.erase(Val: &F);
113 return;
114 }
115
116 if (llvm::all_of(Range: MFDeletionGrouping.members(V: *Leader),
117 P: [this](const Function *Member) {
118 return FinalizedMFs.count(V: 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(V: *Leader)) {
123 MachineFunctions.erase(Val: Member);
124 }
125 }
126}
127
128void MachineModuleInfo::insertFunction(const Function &F,
129 std::unique_ptr<MachineFunction> &&MF) {
130 auto I = MachineFunctions.insert(KV: std::make_pair(x: &F, y: 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 {
145 AU.addRequired<MachineModuleInfoWrapperPass>();
146 AU.addPreserved<MachineModuleInfoWrapperPass>();
147 }
148
149 bool runOnFunction(Function &F) override {
150 MachineModuleInfo &MMI =
151 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
152 MMI.deleteMachineFunctionFor(F);
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
165FunctionPass *llvm::createFreeMachineFunctionPass() {
166 return new FreeMachineFunction();
167}
168
169MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
170 const TargetMachine *TM)
171 : ImmutablePass(ID), MMI(TM) {}
172
173MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
174 const TargetMachine *TM, MCContext *ExtContext)
175 : ImmutablePass(ID), MMI(TM, ExtContext) {}
176
177// Handle the Pass registration stuff necessary to use DataLayout's.
178INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
179 "Machine Module Information", false, false)
180char MachineModuleInfoWrapperPass::ID = 0;
181
182static uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
183 std::vector<const MDNode *> &LocInfos) {
184 // Look up a LocInfo for the buffer this diagnostic is coming from.
185 unsigned BufNum = SrcMgr.FindBufferContainingLoc(Loc: 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>(MD: LocInfo->getOperand(I: ErrorLine)))
201 LocCookie = CI->getZExtValue();
202 }
203
204 return LocCookie;
205}
206
207bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
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 DI: DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
220 });
221 MMI.getTarget().verifyOptionsConsistency(M);
222 return false;
223}
224
225bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
226 MMI.finalize();
227 return false;
228}
229
230AnalysisKey MachineModuleAnalysis::Key;
231
232MachineModuleAnalysis::Result
233MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
234 MMI.TheModule = &M;
235 LLVMContext &Ctx = M.getContext();
236 MMI.getContext().setDiagnosticHandler(
237 [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
238 const SourceMgr &SrcMgr,
239 std::vector<const MDNode *> &LocInfos) {
240 unsigned LocCookie = 0;
241 if (IsInlineAsm)
242 LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
243 Ctx.diagnose(
244 DI: DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
245 });
246 MMI.getTarget().verifyOptionsConsistency(M);
247 return Result(MMI);
248}
249