10b57cec5SDimitry Andric //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
120b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
130b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
140b57cec5SDimitry Andric #include "llvm/ADT/TinyPtrVector.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
170b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
180b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
19*5f7ddb14SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
200b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
21*5f7ddb14SDimitry Andric #include "llvm/IR/LLVMContext.h"
220b57cec5SDimitry Andric #include "llvm/IR/Module.h"
230b57cec5SDimitry Andric #include "llvm/IR/Value.h"
240b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h"
25480093f4SDimitry Andric #include "llvm/InitializePasses.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
28480093f4SDimitry Andric #include "llvm/MC/MCSymbolXCOFF.h"
290b57cec5SDimitry Andric #include "llvm/Pass.h"
300b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
310b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
320b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
330b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
340b57cec5SDimitry Andric #include <algorithm>
350b57cec5SDimitry Andric #include <cassert>
360b57cec5SDimitry Andric #include <memory>
370b57cec5SDimitry Andric #include <utility>
380b57cec5SDimitry Andric #include <vector>
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric using namespace llvm;
410b57cec5SDimitry Andric using namespace llvm::dwarf;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric // Out of line virtual method.
440b57cec5SDimitry Andric MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric namespace llvm {
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric class MMIAddrLabelMapCallbackPtr final : CallbackVH {
490b57cec5SDimitry Andric   MMIAddrLabelMap *Map = nullptr;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric public:
520b57cec5SDimitry Andric   MMIAddrLabelMapCallbackPtr() = default;
MMIAddrLabelMapCallbackPtr(Value * V)530b57cec5SDimitry Andric   MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V) {}
540b57cec5SDimitry Andric 
setPtr(BasicBlock * BB)550b57cec5SDimitry Andric   void setPtr(BasicBlock *BB) {
560b57cec5SDimitry Andric     ValueHandleBase::operator=(BB);
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric 
setMap(MMIAddrLabelMap * map)590b57cec5SDimitry Andric   void setMap(MMIAddrLabelMap *map) { Map = map; }
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   void deleted() override;
620b57cec5SDimitry Andric   void allUsesReplacedWith(Value *V2) override;
630b57cec5SDimitry Andric };
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric class MMIAddrLabelMap {
660b57cec5SDimitry Andric   MCContext &Context;
670b57cec5SDimitry Andric   struct AddrLabelSymEntry {
680b57cec5SDimitry Andric     /// The symbols for the label.
690b57cec5SDimitry Andric     TinyPtrVector<MCSymbol *> Symbols;
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric     Function *Fn;   // The containing function of the BasicBlock.
720b57cec5SDimitry Andric     unsigned Index; // The index in BBCallbacks for the BasicBlock.
730b57cec5SDimitry Andric   };
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   /// Callbacks for the BasicBlock's that we have entries for.  We use this so
780b57cec5SDimitry Andric   /// we get notified if a block is deleted or RAUWd.
790b57cec5SDimitry Andric   std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
800b57cec5SDimitry Andric 
81*5f7ddb14SDimitry Andric   /// This is a per-function list of symbols whose corresponding BasicBlock got
82*5f7ddb14SDimitry Andric   /// deleted.  These symbols need to be emitted at some point in the file, so
83*5f7ddb14SDimitry Andric   /// AsmPrinter emits them after the function body.
84*5f7ddb14SDimitry Andric   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>
85*5f7ddb14SDimitry Andric     DeletedAddrLabelsNeedingEmission;
86*5f7ddb14SDimitry Andric 
870b57cec5SDimitry Andric public:
MMIAddrLabelMap(MCContext & context)880b57cec5SDimitry Andric   MMIAddrLabelMap(MCContext &context) : Context(context) {}
890b57cec5SDimitry Andric 
~MMIAddrLabelMap()90*5f7ddb14SDimitry Andric   ~MMIAddrLabelMap() {
91*5f7ddb14SDimitry Andric     assert(DeletedAddrLabelsNeedingEmission.empty() &&
92*5f7ddb14SDimitry Andric            "Some labels for deleted blocks never got emitted");
93*5f7ddb14SDimitry Andric   }
94*5f7ddb14SDimitry Andric 
950b57cec5SDimitry Andric   ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
960b57cec5SDimitry Andric 
97*5f7ddb14SDimitry Andric   void takeDeletedSymbolsForFunction(Function *F,
98*5f7ddb14SDimitry Andric                                      std::vector<MCSymbol*> &Result);
99*5f7ddb14SDimitry Andric 
1000b57cec5SDimitry Andric   void UpdateForDeletedBlock(BasicBlock *BB);
1010b57cec5SDimitry Andric   void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
1020b57cec5SDimitry Andric };
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric } // end namespace llvm
1050b57cec5SDimitry Andric 
getAddrLabelSymbolToEmit(BasicBlock * BB)1060b57cec5SDimitry Andric ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
1070b57cec5SDimitry Andric   assert(BB->hasAddressTaken() &&
1080b57cec5SDimitry Andric          "Shouldn't get label for block without address taken");
1090b57cec5SDimitry Andric   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   // If we already had an entry for this block, just return it.
1120b57cec5SDimitry Andric   if (!Entry.Symbols.empty()) {
1130b57cec5SDimitry Andric     assert(BB->getParent() == Entry.Fn && "Parent changed");
1140b57cec5SDimitry Andric     return Entry.Symbols;
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // Otherwise, this is a new entry, create a new symbol for it and add an
1180b57cec5SDimitry Andric   // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
1190b57cec5SDimitry Andric   BBCallbacks.emplace_back(BB);
1200b57cec5SDimitry Andric   BBCallbacks.back().setMap(this);
1210b57cec5SDimitry Andric   Entry.Index = BBCallbacks.size() - 1;
1220b57cec5SDimitry Andric   Entry.Fn = BB->getParent();
123af732203SDimitry Andric   MCSymbol *Sym = BB->hasAddressTaken() ? Context.createNamedTempSymbol()
124af732203SDimitry Andric                                         : Context.createTempSymbol();
125480093f4SDimitry Andric   Entry.Symbols.push_back(Sym);
1260b57cec5SDimitry Andric   return Entry.Symbols;
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric 
129*5f7ddb14SDimitry Andric /// If we have any deleted symbols for F, return them.
130*5f7ddb14SDimitry Andric void MMIAddrLabelMap::
takeDeletedSymbolsForFunction(Function * F,std::vector<MCSymbol * > & Result)131*5f7ddb14SDimitry Andric takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
132*5f7ddb14SDimitry Andric   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>::iterator I =
133*5f7ddb14SDimitry Andric     DeletedAddrLabelsNeedingEmission.find(F);
134*5f7ddb14SDimitry Andric 
135*5f7ddb14SDimitry Andric   // If there are no entries for the function, just return.
136*5f7ddb14SDimitry Andric   if (I == DeletedAddrLabelsNeedingEmission.end()) return;
137*5f7ddb14SDimitry Andric 
138*5f7ddb14SDimitry Andric   // Otherwise, take the list.
139*5f7ddb14SDimitry Andric   std::swap(Result, I->second);
140*5f7ddb14SDimitry Andric   DeletedAddrLabelsNeedingEmission.erase(I);
141*5f7ddb14SDimitry Andric }
142*5f7ddb14SDimitry Andric 
UpdateForDeletedBlock(BasicBlock * BB)1430b57cec5SDimitry Andric void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
1440b57cec5SDimitry Andric   // If the block got deleted, there is no need for the symbol.  If the symbol
1450b57cec5SDimitry Andric   // was already emitted, we can just forget about it, otherwise we need to
1460b57cec5SDimitry Andric   // queue it up for later emission when the function is output.
1470b57cec5SDimitry Andric   AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
1480b57cec5SDimitry Andric   AddrLabelSymbols.erase(BB);
1490b57cec5SDimitry Andric   assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
1500b57cec5SDimitry Andric   BBCallbacks[Entry.Index] = nullptr;  // Clear the callback.
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
1530b57cec5SDimitry Andric          "Block/parent mismatch");
1540b57cec5SDimitry Andric 
155*5f7ddb14SDimitry Andric   for (MCSymbol *Sym : Entry.Symbols) {
156*5f7ddb14SDimitry Andric     if (Sym->isDefined())
157*5f7ddb14SDimitry Andric       return;
158*5f7ddb14SDimitry Andric 
159*5f7ddb14SDimitry Andric     // If the block is not yet defined, we need to emit it at the end of the
160*5f7ddb14SDimitry Andric     // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
161*5f7ddb14SDimitry Andric     // for the containing Function.  Since the block is being deleted, its
162*5f7ddb14SDimitry Andric     // parent may already be removed, we have to get the function from 'Entry'.
163*5f7ddb14SDimitry Andric     DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
164*5f7ddb14SDimitry Andric   }
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
UpdateForRAUWBlock(BasicBlock * Old,BasicBlock * New)1670b57cec5SDimitry Andric void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
1680b57cec5SDimitry Andric   // Get the entry for the RAUW'd block and remove it from our map.
1690b57cec5SDimitry Andric   AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
1700b57cec5SDimitry Andric   AddrLabelSymbols.erase(Old);
1710b57cec5SDimitry Andric   assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   // If New is not address taken, just move our symbol over to it.
1760b57cec5SDimitry Andric   if (NewEntry.Symbols.empty()) {
1770b57cec5SDimitry Andric     BBCallbacks[OldEntry.Index].setPtr(New);    // Update the callback.
1780b57cec5SDimitry Andric     NewEntry = std::move(OldEntry);             // Set New's entry.
1790b57cec5SDimitry Andric     return;
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   BBCallbacks[OldEntry.Index] = nullptr;    // Update the callback.
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   // Otherwise, we need to add the old symbols to the new block's set.
185af732203SDimitry Andric   llvm::append_range(NewEntry.Symbols, OldEntry.Symbols);
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
deleted()1880b57cec5SDimitry Andric void MMIAddrLabelMapCallbackPtr::deleted() {
1890b57cec5SDimitry Andric   Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
1900b57cec5SDimitry Andric }
1910b57cec5SDimitry Andric 
allUsesReplacedWith(Value * V2)1920b57cec5SDimitry Andric void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
1930b57cec5SDimitry Andric   Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
initialize()1968bcb0991SDimitry Andric void MachineModuleInfo::initialize() {
1970b57cec5SDimitry Andric   ObjFileMMI = nullptr;
1980b57cec5SDimitry Andric   CurCallSite = 0;
199*5f7ddb14SDimitry Andric   NextFnNum = 0;
2000b57cec5SDimitry Andric   UsesMSVCFloatingPoint = UsesMorestackAddr = false;
2010b57cec5SDimitry Andric   HasSplitStack = HasNosplitStack = false;
2020b57cec5SDimitry Andric   AddrLabelSymbols = nullptr;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
finalize()2058bcb0991SDimitry Andric void MachineModuleInfo::finalize() {
2060b57cec5SDimitry Andric   Personalities.clear();
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   delete AddrLabelSymbols;
2090b57cec5SDimitry Andric   AddrLabelSymbols = nullptr;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   Context.reset();
212af732203SDimitry Andric   // We don't clear the ExternalContext.
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   delete ObjFileMMI;
2150b57cec5SDimitry Andric   ObjFileMMI = nullptr;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
MachineModuleInfo(MachineModuleInfo && MMI)2188bcb0991SDimitry Andric MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
2198bcb0991SDimitry Andric     : TM(std::move(MMI.TM)),
220*5f7ddb14SDimitry Andric       Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
221*5f7ddb14SDimitry Andric               MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
222*5f7ddb14SDimitry Andric               nullptr, false),
223af732203SDimitry Andric       MachineFunctions(std::move(MMI.MachineFunctions)) {
224*5f7ddb14SDimitry Andric   Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
2258bcb0991SDimitry Andric   ObjFileMMI = MMI.ObjFileMMI;
2268bcb0991SDimitry Andric   CurCallSite = MMI.CurCallSite;
2278bcb0991SDimitry Andric   UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint;
2288bcb0991SDimitry Andric   UsesMorestackAddr = MMI.UsesMorestackAddr;
2298bcb0991SDimitry Andric   HasSplitStack = MMI.HasSplitStack;
2308bcb0991SDimitry Andric   HasNosplitStack = MMI.HasNosplitStack;
2318bcb0991SDimitry Andric   AddrLabelSymbols = MMI.AddrLabelSymbols;
232af732203SDimitry Andric   ExternalContext = MMI.ExternalContext;
2338bcb0991SDimitry Andric   TheModule = MMI.TheModule;
2348bcb0991SDimitry Andric }
2358bcb0991SDimitry Andric 
MachineModuleInfo(const LLVMTargetMachine * TM)2368bcb0991SDimitry Andric MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
237*5f7ddb14SDimitry Andric     : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
238*5f7ddb14SDimitry Andric                        TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
239*5f7ddb14SDimitry Andric                        nullptr, nullptr, false) {
240*5f7ddb14SDimitry Andric   Context.setObjectFileInfo(TM->getObjFileLowering());
2418bcb0991SDimitry Andric   initialize();
2428bcb0991SDimitry Andric }
2438bcb0991SDimitry Andric 
MachineModuleInfo(const LLVMTargetMachine * TM,MCContext * ExtContext)244af732203SDimitry Andric MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
245af732203SDimitry Andric                                      MCContext *ExtContext)
246*5f7ddb14SDimitry Andric     : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
247*5f7ddb14SDimitry Andric                        TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
248*5f7ddb14SDimitry Andric                        nullptr, nullptr, false),
249af732203SDimitry Andric       ExternalContext(ExtContext) {
250*5f7ddb14SDimitry Andric   Context.setObjectFileInfo(TM->getObjFileLowering());
251af732203SDimitry Andric   initialize();
252af732203SDimitry Andric }
253af732203SDimitry Andric 
~MachineModuleInfo()2548bcb0991SDimitry Andric MachineModuleInfo::~MachineModuleInfo() { finalize(); }
2558bcb0991SDimitry Andric 
2560b57cec5SDimitry Andric //===- Address of Block Management ----------------------------------------===//
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric ArrayRef<MCSymbol *>
getAddrLabelSymbolToEmit(const BasicBlock * BB)2590b57cec5SDimitry Andric MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
2600b57cec5SDimitry Andric   // Lazily create AddrLabelSymbols.
2610b57cec5SDimitry Andric   if (!AddrLabelSymbols)
262af732203SDimitry Andric     AddrLabelSymbols = new MMIAddrLabelMap(getContext());
2630b57cec5SDimitry Andric  return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
266*5f7ddb14SDimitry Andric void MachineModuleInfo::
takeDeletedSymbolsForFunction(const Function * F,std::vector<MCSymbol * > & Result)267*5f7ddb14SDimitry Andric takeDeletedSymbolsForFunction(const Function *F,
268*5f7ddb14SDimitry Andric                               std::vector<MCSymbol*> &Result) {
269*5f7ddb14SDimitry Andric   // If no blocks have had their addresses taken, we're done.
270*5f7ddb14SDimitry Andric   if (!AddrLabelSymbols) return;
271*5f7ddb14SDimitry Andric   return AddrLabelSymbols->
272*5f7ddb14SDimitry Andric      takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
273*5f7ddb14SDimitry Andric }
274*5f7ddb14SDimitry Andric 
2750b57cec5SDimitry Andric /// \name Exception Handling
2760b57cec5SDimitry Andric /// \{
2770b57cec5SDimitry Andric 
addPersonality(const Function * Personality)2780b57cec5SDimitry Andric void MachineModuleInfo::addPersonality(const Function *Personality) {
279*5f7ddb14SDimitry Andric   if (!llvm::is_contained(Personalities, Personality))
2800b57cec5SDimitry Andric     Personalities.push_back(Personality);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric /// \}
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric MachineFunction *
getMachineFunction(const Function & F) const2860b57cec5SDimitry Andric MachineModuleInfo::getMachineFunction(const Function &F) const {
2870b57cec5SDimitry Andric   auto I = MachineFunctions.find(&F);
2880b57cec5SDimitry Andric   return I != MachineFunctions.end() ? I->second.get() : nullptr;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
getOrCreateMachineFunction(Function & F)2915ffd83dbSDimitry Andric MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F) {
2920b57cec5SDimitry Andric   // Shortcut for the common case where a sequence of MachineFunctionPasses
2930b57cec5SDimitry Andric   // all query for the same Function.
2940b57cec5SDimitry Andric   if (LastRequest == &F)
2950b57cec5SDimitry Andric     return *LastResult;
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   auto I = MachineFunctions.insert(
2980b57cec5SDimitry Andric       std::make_pair(&F, std::unique_ptr<MachineFunction>()));
2990b57cec5SDimitry Andric   MachineFunction *MF;
3000b57cec5SDimitry Andric   if (I.second) {
3010b57cec5SDimitry Andric     // No pre-existing machine function, create a new one.
3020b57cec5SDimitry Andric     const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
3030b57cec5SDimitry Andric     MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
3040b57cec5SDimitry Andric     // Update the set entry.
3050b57cec5SDimitry Andric     I.first->second.reset(MF);
3060b57cec5SDimitry Andric   } else {
3070b57cec5SDimitry Andric     MF = I.first->second.get();
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   LastRequest = &F;
3110b57cec5SDimitry Andric   LastResult = MF;
3120b57cec5SDimitry Andric   return *MF;
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
deleteMachineFunctionFor(Function & F)3150b57cec5SDimitry Andric void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
3160b57cec5SDimitry Andric   MachineFunctions.erase(&F);
3170b57cec5SDimitry Andric   LastRequest = nullptr;
3180b57cec5SDimitry Andric   LastResult = nullptr;
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric namespace {
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric /// This pass frees the MachineFunction object associated with a Function.
3240b57cec5SDimitry Andric class FreeMachineFunction : public FunctionPass {
3250b57cec5SDimitry Andric public:
3260b57cec5SDimitry Andric   static char ID;
3270b57cec5SDimitry Andric 
FreeMachineFunction()3280b57cec5SDimitry Andric   FreeMachineFunction() : FunctionPass(ID) {}
3290b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const3300b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
3318bcb0991SDimitry Andric     AU.addRequired<MachineModuleInfoWrapperPass>();
3328bcb0991SDimitry Andric     AU.addPreserved<MachineModuleInfoWrapperPass>();
3330b57cec5SDimitry Andric   }
3340b57cec5SDimitry Andric 
runOnFunction(Function & F)3350b57cec5SDimitry Andric   bool runOnFunction(Function &F) override {
3368bcb0991SDimitry Andric     MachineModuleInfo &MMI =
3378bcb0991SDimitry Andric         getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
3380b57cec5SDimitry Andric     MMI.deleteMachineFunctionFor(F);
3390b57cec5SDimitry Andric     return true;
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric 
getPassName() const3420b57cec5SDimitry Andric   StringRef getPassName() const override {
3430b57cec5SDimitry Andric     return "Free MachineFunction";
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric };
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric } // end anonymous namespace
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric char FreeMachineFunction::ID;
3500b57cec5SDimitry Andric 
createFreeMachineFunctionPass()3510b57cec5SDimitry Andric FunctionPass *llvm::createFreeMachineFunctionPass() {
3520b57cec5SDimitry Andric   return new FreeMachineFunction();
3530b57cec5SDimitry Andric }
3548bcb0991SDimitry Andric 
MachineModuleInfoWrapperPass(const LLVMTargetMachine * TM)3558bcb0991SDimitry Andric MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
3568bcb0991SDimitry Andric     const LLVMTargetMachine *TM)
3578bcb0991SDimitry Andric     : ImmutablePass(ID), MMI(TM) {
3588bcb0991SDimitry Andric   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
3598bcb0991SDimitry Andric }
3608bcb0991SDimitry Andric 
MachineModuleInfoWrapperPass(const LLVMTargetMachine * TM,MCContext * ExtContext)361af732203SDimitry Andric MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
362af732203SDimitry Andric     const LLVMTargetMachine *TM, MCContext *ExtContext)
363af732203SDimitry Andric     : ImmutablePass(ID), MMI(TM, ExtContext) {
364af732203SDimitry Andric   initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
365af732203SDimitry Andric }
366af732203SDimitry Andric 
3678bcb0991SDimitry Andric // Handle the Pass registration stuff necessary to use DataLayout's.
3688bcb0991SDimitry Andric INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
3698bcb0991SDimitry Andric                 "Machine Module Information", false, false)
3708bcb0991SDimitry Andric char MachineModuleInfoWrapperPass::ID = 0;
3718bcb0991SDimitry Andric 
getLocCookie(const SMDiagnostic & SMD,const SourceMgr & SrcMgr,std::vector<const MDNode * > & LocInfos)372*5f7ddb14SDimitry Andric static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
373*5f7ddb14SDimitry Andric                              std::vector<const MDNode *> &LocInfos) {
374*5f7ddb14SDimitry Andric   // Look up a LocInfo for the buffer this diagnostic is coming from.
375*5f7ddb14SDimitry Andric   unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
376*5f7ddb14SDimitry Andric   const MDNode *LocInfo = nullptr;
377*5f7ddb14SDimitry Andric   if (BufNum > 0 && BufNum <= LocInfos.size())
378*5f7ddb14SDimitry Andric     LocInfo = LocInfos[BufNum - 1];
379*5f7ddb14SDimitry Andric 
380*5f7ddb14SDimitry Andric   // If the inline asm had metadata associated with it, pull out a location
381*5f7ddb14SDimitry Andric   // cookie corresponding to which line the error occurred on.
382*5f7ddb14SDimitry Andric   unsigned LocCookie = 0;
383*5f7ddb14SDimitry Andric   if (LocInfo) {
384*5f7ddb14SDimitry Andric     unsigned ErrorLine = SMD.getLineNo() - 1;
385*5f7ddb14SDimitry Andric     if (ErrorLine >= LocInfo->getNumOperands())
386*5f7ddb14SDimitry Andric       ErrorLine = 0;
387*5f7ddb14SDimitry Andric 
388*5f7ddb14SDimitry Andric     if (LocInfo->getNumOperands() != 0)
389*5f7ddb14SDimitry Andric       if (const ConstantInt *CI =
390*5f7ddb14SDimitry Andric               mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
391*5f7ddb14SDimitry Andric         LocCookie = CI->getZExtValue();
392*5f7ddb14SDimitry Andric   }
393*5f7ddb14SDimitry Andric 
394*5f7ddb14SDimitry Andric   return LocCookie;
395*5f7ddb14SDimitry Andric }
396*5f7ddb14SDimitry Andric 
doInitialization(Module & M)3978bcb0991SDimitry Andric bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
3988bcb0991SDimitry Andric   MMI.initialize();
3998bcb0991SDimitry Andric   MMI.TheModule = &M;
400*5f7ddb14SDimitry Andric   // FIXME: Do this for new pass manager.
401*5f7ddb14SDimitry Andric   LLVMContext &Ctx = M.getContext();
402*5f7ddb14SDimitry Andric   MMI.getContext().setDiagnosticHandler(
403*5f7ddb14SDimitry Andric       [&Ctx](const SMDiagnostic &SMD, bool IsInlineAsm, const SourceMgr &SrcMgr,
404*5f7ddb14SDimitry Andric              std::vector<const MDNode *> &LocInfos) {
405*5f7ddb14SDimitry Andric         unsigned LocCookie = 0;
406*5f7ddb14SDimitry Andric         if (IsInlineAsm)
407*5f7ddb14SDimitry Andric           LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
408*5f7ddb14SDimitry Andric         Ctx.diagnose(DiagnosticInfoSrcMgr(SMD, IsInlineAsm, LocCookie));
409*5f7ddb14SDimitry Andric       });
4108bcb0991SDimitry Andric   MMI.DbgInfoAvailable = !M.debug_compile_units().empty();
4118bcb0991SDimitry Andric   return false;
4128bcb0991SDimitry Andric }
4138bcb0991SDimitry Andric 
doFinalization(Module & M)4148bcb0991SDimitry Andric bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
4158bcb0991SDimitry Andric   MMI.finalize();
4168bcb0991SDimitry Andric   return false;
4178bcb0991SDimitry Andric }
4188bcb0991SDimitry Andric 
4198bcb0991SDimitry Andric AnalysisKey MachineModuleAnalysis::Key;
4208bcb0991SDimitry Andric 
run(Module & M,ModuleAnalysisManager &)4218bcb0991SDimitry Andric MachineModuleInfo MachineModuleAnalysis::run(Module &M,
4228bcb0991SDimitry Andric                                              ModuleAnalysisManager &) {
4238bcb0991SDimitry Andric   MachineModuleInfo MMI(TM);
4248bcb0991SDimitry Andric   MMI.TheModule = &M;
4258bcb0991SDimitry Andric   MMI.DbgInfoAvailable = !M.debug_compile_units().empty();
4268bcb0991SDimitry Andric   return MMI;
4278bcb0991SDimitry Andric }
428