xref: /llvm-project/llvm/lib/CodeGen/MachineModuleInfo.cpp (revision d7938b1a817006388f95de5ea2ee74daa7cde892)
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/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/TinyPtrVector.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ValueHandle.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <memory>
35 #include <utility>
36 #include <vector>
37 
38 using namespace llvm;
39 using namespace llvm::dwarf;
40 
41 static cl::opt<bool>
42     DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
43                              cl::desc("Disable debug info printing"));
44 
45 // Out of line virtual method.
46 MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
47 
48 namespace llvm {
49 
50 class MMIAddrLabelMapCallbackPtr final : CallbackVH {
51   MMIAddrLabelMap *Map = nullptr;
52 
53 public:
54   MMIAddrLabelMapCallbackPtr() = default;
55   MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V) {}
56 
57   void setPtr(BasicBlock *BB) {
58     ValueHandleBase::operator=(BB);
59   }
60 
61   void setMap(MMIAddrLabelMap *map) { Map = map; }
62 
63   void deleted() override;
64   void allUsesReplacedWith(Value *V2) override;
65 };
66 
67 class MMIAddrLabelMap {
68   MCContext &Context;
69   struct AddrLabelSymEntry {
70     /// The symbols for the label.
71     TinyPtrVector<MCSymbol *> Symbols;
72 
73     Function *Fn;   // The containing function of the BasicBlock.
74     unsigned Index; // The index in BBCallbacks for the BasicBlock.
75   };
76 
77   DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
78 
79   /// Callbacks for the BasicBlock's that we have entries for.  We use this so
80   /// we get notified if a block is deleted or RAUWd.
81   std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
82 
83   /// This is a per-function list of symbols whose corresponding BasicBlock got
84   /// deleted.  These symbols need to be emitted at some point in the file, so
85   /// AsmPrinter emits them after the function body.
86   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>
87     DeletedAddrLabelsNeedingEmission;
88 
89 public:
90   MMIAddrLabelMap(MCContext &context) : Context(context) {}
91 
92   ~MMIAddrLabelMap() {
93     assert(DeletedAddrLabelsNeedingEmission.empty() &&
94            "Some labels for deleted blocks never got emitted");
95   }
96 
97   ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
98 
99   void takeDeletedSymbolsForFunction(Function *F,
100                                      std::vector<MCSymbol*> &Result);
101 
102   void UpdateForDeletedBlock(BasicBlock *BB);
103   void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
104 };
105 
106 } // end namespace llvm
107 
108 ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
109   assert(BB->hasAddressTaken() &&
110          "Shouldn't get label for block without address taken");
111   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
112 
113   // If we already had an entry for this block, just return it.
114   if (!Entry.Symbols.empty()) {
115     assert(BB->getParent() == Entry.Fn && "Parent changed");
116     return Entry.Symbols;
117   }
118 
119   // Otherwise, this is a new entry, create a new symbol for it and add an
120   // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
121   BBCallbacks.emplace_back(BB);
122   BBCallbacks.back().setMap(this);
123   Entry.Index = BBCallbacks.size() - 1;
124   Entry.Fn = BB->getParent();
125   MCSymbol *Sym = BB->hasAddressTaken() ? Context.createNamedTempSymbol()
126                                         : Context.createTempSymbol();
127   Entry.Symbols.push_back(Sym);
128   return Entry.Symbols;
129 }
130 
131 /// If we have any deleted symbols for F, return them.
132 void MMIAddrLabelMap::
133 takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
134   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>::iterator I =
135     DeletedAddrLabelsNeedingEmission.find(F);
136 
137   // If there are no entries for the function, just return.
138   if (I == DeletedAddrLabelsNeedingEmission.end()) return;
139 
140   // Otherwise, take the list.
141   std::swap(Result, I->second);
142   DeletedAddrLabelsNeedingEmission.erase(I);
143 }
144 
145 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
146   // If the block got deleted, there is no need for the symbol.  If the symbol
147   // was already emitted, we can just forget about it, otherwise we need to
148   // queue it up for later emission when the function is output.
149   AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
150   AddrLabelSymbols.erase(BB);
151   assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
152   BBCallbacks[Entry.Index] = nullptr;  // Clear the callback.
153 
154 #if !LLVM_MEMORY_SANITIZER_BUILD
155   // BasicBlock is destroyed already, so this access is UB detectable by msan.
156   assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
157          "Block/parent mismatch");
158 #endif
159 
160   for (MCSymbol *Sym : Entry.Symbols) {
161     if (Sym->isDefined())
162       return;
163 
164     // If the block is not yet defined, we need to emit it at the end of the
165     // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
166     // for the containing Function.  Since the block is being deleted, its
167     // parent may already be removed, we have to get the function from 'Entry'.
168     DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
169   }
170 }
171 
172 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
173   // Get the entry for the RAUW'd block and remove it from our map.
174   AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
175   AddrLabelSymbols.erase(Old);
176   assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
177 
178   AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
179 
180   // If New is not address taken, just move our symbol over to it.
181   if (NewEntry.Symbols.empty()) {
182     BBCallbacks[OldEntry.Index].setPtr(New);    // Update the callback.
183     NewEntry = std::move(OldEntry);             // Set New's entry.
184     return;
185   }
186 
187   BBCallbacks[OldEntry.Index] = nullptr;    // Update the callback.
188 
189   // Otherwise, we need to add the old symbols to the new block's set.
190   llvm::append_range(NewEntry.Symbols, OldEntry.Symbols);
191 }
192 
193 void MMIAddrLabelMapCallbackPtr::deleted() {
194   Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
195 }
196 
197 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
198   Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
199 }
200 
201 void MachineModuleInfo::initialize() {
202   ObjFileMMI = nullptr;
203   CurCallSite = 0;
204   NextFnNum = 0;
205   UsesMSVCFloatingPoint = UsesMorestackAddr = false;
206   AddrLabelSymbols = nullptr;
207   DbgInfoAvailable = false;
208 }
209 
210 void MachineModuleInfo::finalize() {
211   Personalities.clear();
212 
213   delete AddrLabelSymbols;
214   AddrLabelSymbols = nullptr;
215 
216   Context.reset();
217   // We don't clear the ExternalContext.
218 
219   delete ObjFileMMI;
220   ObjFileMMI = nullptr;
221 }
222 
223 MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
224     : TM(std::move(MMI.TM)),
225       Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
226               MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
227               nullptr, false),
228       MachineFunctions(std::move(MMI.MachineFunctions)) {
229   Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
230   ObjFileMMI = MMI.ObjFileMMI;
231   CurCallSite = MMI.CurCallSite;
232   UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint;
233   UsesMorestackAddr = MMI.UsesMorestackAddr;
234   AddrLabelSymbols = MMI.AddrLabelSymbols;
235   ExternalContext = MMI.ExternalContext;
236   TheModule = MMI.TheModule;
237 }
238 
239 MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
240     : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
241                        TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
242                        nullptr, nullptr, false) {
243   Context.setObjectFileInfo(TM->getObjFileLowering());
244   initialize();
245 }
246 
247 MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
248                                      MCContext *ExtContext)
249     : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
250                        TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
251                        nullptr, nullptr, false),
252       ExternalContext(ExtContext) {
253   Context.setObjectFileInfo(TM->getObjFileLowering());
254   initialize();
255 }
256 
257 MachineModuleInfo::~MachineModuleInfo() { finalize(); }
258 
259 //===- Address of Block Management ----------------------------------------===//
260 
261 ArrayRef<MCSymbol *>
262 MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
263   // Lazily create AddrLabelSymbols.
264   if (!AddrLabelSymbols)
265     AddrLabelSymbols = new MMIAddrLabelMap(getContext());
266  return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
267 }
268 
269 void MachineModuleInfo::
270 takeDeletedSymbolsForFunction(const Function *F,
271                               std::vector<MCSymbol*> &Result) {
272   // If no blocks have had their addresses taken, we're done.
273   if (!AddrLabelSymbols) return;
274   return AddrLabelSymbols->
275      takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
276 }
277 
278 /// \name Exception Handling
279 /// \{
280 
281 void MachineModuleInfo::addPersonality(const Function *Personality) {
282   if (!llvm::is_contained(Personalities, Personality))
283     Personalities.push_back(Personality);
284 }
285 
286 /// \}
287 
288 MachineFunction *
289 MachineModuleInfo::getMachineFunction(const Function &F) const {
290   auto I = MachineFunctions.find(&F);
291   return I != MachineFunctions.end() ? I->second.get() : nullptr;
292 }
293 
294 MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F) {
295   // Shortcut for the common case where a sequence of MachineFunctionPasses
296   // all query for the same Function.
297   if (LastRequest == &F)
298     return *LastResult;
299 
300   auto I = MachineFunctions.insert(
301       std::make_pair(&F, std::unique_ptr<MachineFunction>()));
302   MachineFunction *MF;
303   if (I.second) {
304     // No pre-existing machine function, create a new one.
305     const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
306     MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
307     // Update the set entry.
308     I.first->second.reset(MF);
309   } else {
310     MF = I.first->second.get();
311   }
312 
313   LastRequest = &F;
314   LastResult = MF;
315   return *MF;
316 }
317 
318 void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
319   MachineFunctions.erase(&F);
320   LastRequest = nullptr;
321   LastResult = nullptr;
322 }
323 
324 namespace {
325 
326 /// This pass frees the MachineFunction object associated with a Function.
327 class FreeMachineFunction : public FunctionPass {
328 public:
329   static char ID;
330 
331   FreeMachineFunction() : FunctionPass(ID) {}
332 
333   void getAnalysisUsage(AnalysisUsage &AU) const override {
334     AU.addRequired<MachineModuleInfoWrapperPass>();
335     AU.addPreserved<MachineModuleInfoWrapperPass>();
336   }
337 
338   bool runOnFunction(Function &F) override {
339     MachineModuleInfo &MMI =
340         getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
341     MMI.deleteMachineFunctionFor(F);
342     return true;
343   }
344 
345   StringRef getPassName() const override {
346     return "Free MachineFunction";
347   }
348 };
349 
350 } // end anonymous namespace
351 
352 char FreeMachineFunction::ID;
353 
354 FunctionPass *llvm::createFreeMachineFunctionPass() {
355   return new FreeMachineFunction();
356 }
357 
358 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
359     const LLVMTargetMachine *TM)
360     : ImmutablePass(ID), MMI(TM) {
361   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
362 }
363 
364 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
365     const LLVMTargetMachine *TM, MCContext *ExtContext)
366     : ImmutablePass(ID), MMI(TM, ExtContext) {
367   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
368 }
369 
370 // Handle the Pass registration stuff necessary to use DataLayout's.
371 INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
372                 "Machine Module Information", false, false)
373 char MachineModuleInfoWrapperPass::ID = 0;
374 
375 static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
376                              std::vector<const MDNode *> &LocInfos) {
377   // Look up a LocInfo for the buffer this diagnostic is coming from.
378   unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
379   const MDNode *LocInfo = nullptr;
380   if (BufNum > 0 && BufNum <= LocInfos.size())
381     LocInfo = LocInfos[BufNum - 1];
382 
383   // If the inline asm had metadata associated with it, pull out a location
384   // cookie corresponding to which line the error occurred on.
385   unsigned LocCookie = 0;
386   if (LocInfo) {
387     unsigned ErrorLine = SMD.getLineNo() - 1;
388     if (ErrorLine >= LocInfo->getNumOperands())
389       ErrorLine = 0;
390 
391     if (LocInfo->getNumOperands() != 0)
392       if (const ConstantInt *CI =
393               mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
394         LocCookie = CI->getZExtValue();
395   }
396 
397   return LocCookie;
398 }
399 
400 bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
401   MMI.initialize();
402   MMI.TheModule = &M;
403   // FIXME: Do this for new pass manager.
404   LLVMContext &Ctx = M.getContext();
405   MMI.getContext().setDiagnosticHandler(
406       [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
407                  const SourceMgr &SrcMgr,
408                  std::vector<const MDNode *> &LocInfos) {
409         unsigned LocCookie = 0;
410         if (IsInlineAsm)
411           LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
412         Ctx.diagnose(
413             DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
414       });
415   MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
416                          !M.debug_compile_units().empty();
417   return false;
418 }
419 
420 bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
421   MMI.finalize();
422   return false;
423 }
424 
425 AnalysisKey MachineModuleAnalysis::Key;
426 
427 MachineModuleInfo MachineModuleAnalysis::run(Module &M,
428                                              ModuleAnalysisManager &) {
429   MachineModuleInfo MMI(TM);
430   MMI.TheModule = &M;
431   MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
432                          !M.debug_compile_units().empty();
433   return MMI;
434 }
435