1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/PostOrderIterator.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/TinyPtrVector.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineFunctionInitializer.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetMachine.h"
39 using namespace llvm::dwarf;
41 // Handle the Pass registration stuff necessary to use DataLayout's.
42 INITIALIZE_PASS(MachineModuleInfo, "machinemoduleinfo",
43 "Machine Module Information", false, false)
44 char MachineModuleInfo::ID = 0;
46 // Out of line virtual method.
47 MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
51 class MMIAddrLabelMapCallbackPtr final : CallbackVH {
52 MMIAddrLabelMap *Map = nullptr;
55 MMIAddrLabelMapCallbackPtr() = default;
56 MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V) {}
58 void setPtr(BasicBlock *BB) {
59 ValueHandleBase::operator=(BB);
62 void setMap(MMIAddrLabelMap *map) { Map = map; }
64 void deleted() override;
65 void allUsesReplacedWith(Value *V2) override;
68 class MMIAddrLabelMap {
70 struct AddrLabelSymEntry {
71 /// The symbols for the label.
72 TinyPtrVector<MCSymbol *> Symbols;
74 Function *Fn; // The containing function of the BasicBlock.
75 unsigned Index; // The index in BBCallbacks for the BasicBlock.
78 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
80 /// Callbacks for the BasicBlock's that we have entries for. We use this so
81 /// we get notified if a block is deleted or RAUWd.
82 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
84 /// This is a per-function list of symbols whose corresponding BasicBlock got
85 /// deleted. These symbols need to be emitted at some point in the file, so
86 /// AsmPrinter emits them after the function body.
87 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>
88 DeletedAddrLabelsNeedingEmission;
91 MMIAddrLabelMap(MCContext &context) : Context(context) {}
94 assert(DeletedAddrLabelsNeedingEmission.empty() &&
95 "Some labels for deleted blocks never got emitted");
98 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
100 void takeDeletedSymbolsForFunction(Function *F,
101 std::vector<MCSymbol*> &Result);
103 void UpdateForDeletedBlock(BasicBlock *BB);
104 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
107 } // end namespace llvm
109 ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
110 assert(BB->hasAddressTaken() &&
111 "Shouldn't get label for block without address taken");
112 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
114 // If we already had an entry for this block, just return it.
115 if (!Entry.Symbols.empty()) {
116 assert(BB->getParent() == Entry.Fn && "Parent changed");
117 return Entry.Symbols;
120 // Otherwise, this is a new entry, create a new symbol for it and add an
121 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
122 BBCallbacks.emplace_back(BB);
123 BBCallbacks.back().setMap(this);
124 Entry.Index = BBCallbacks.size() - 1;
125 Entry.Fn = BB->getParent();
126 Entry.Symbols.push_back(Context.createTempSymbol());
127 return Entry.Symbols;
130 /// If we have any deleted symbols for F, return them.
131 void MMIAddrLabelMap::
132 takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
133 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>::iterator I =
134 DeletedAddrLabelsNeedingEmission.find(F);
136 // If there are no entries for the function, just return.
137 if (I == DeletedAddrLabelsNeedingEmission.end()) return;
139 // Otherwise, take the list.
140 std::swap(Result, I->second);
141 DeletedAddrLabelsNeedingEmission.erase(I);
144 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
145 // If the block got deleted, there is no need for the symbol. If the symbol
146 // was already emitted, we can just forget about it, otherwise we need to
147 // queue it up for later emission when the function is output.
148 AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
149 AddrLabelSymbols.erase(BB);
150 assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
151 BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
153 assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
154 "Block/parent mismatch");
156 for (MCSymbol *Sym : Entry.Symbols) {
157 if (Sym->isDefined())
160 // If the block is not yet defined, we need to emit it at the end of the
161 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
162 // for the containing Function. Since the block is being deleted, its
163 // parent may already be removed, we have to get the function from 'Entry'.
164 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
168 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
169 // Get the entry for the RAUW'd block and remove it from our map.
170 AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
171 AddrLabelSymbols.erase(Old);
172 assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
174 AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
176 // If New is not address taken, just move our symbol over to it.
177 if (NewEntry.Symbols.empty()) {
178 BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
179 NewEntry = std::move(OldEntry); // Set New's entry.
183 BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
185 // Otherwise, we need to add the old symbols to the new block's set.
186 NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(),
187 OldEntry.Symbols.end());
190 void MMIAddrLabelMapCallbackPtr::deleted() {
191 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
194 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
195 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
198 MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM)
199 : ImmutablePass(ID), TM(*TM),
200 Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
201 TM->getObjFileLowering(), nullptr, false) {
202 initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
205 MachineModuleInfo::~MachineModuleInfo() = default;
207 bool MachineModuleInfo::doInitialization(Module &M) {
208 ObjFileMMI = nullptr;
210 DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
211 AddrLabelSymbols = nullptr;
217 bool MachineModuleInfo::doFinalization(Module &M) {
218 Personalities.clear();
220 delete AddrLabelSymbols;
221 AddrLabelSymbols = nullptr;
226 ObjFileMMI = nullptr;
231 //===- Address of Block Management ----------------------------------------===//
234 MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
235 // Lazily create AddrLabelSymbols.
236 if (!AddrLabelSymbols)
237 AddrLabelSymbols = new MMIAddrLabelMap(Context);
238 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
241 void MachineModuleInfo::
242 takeDeletedSymbolsForFunction(const Function *F,
243 std::vector<MCSymbol*> &Result) {
244 // If no blocks have had their addresses taken, we're done.
245 if (!AddrLabelSymbols) return;
246 return AddrLabelSymbols->
247 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
250 /// \name Exception Handling
253 void MachineModuleInfo::addPersonality(const Function *Personality) {
254 for (unsigned i = 0; i < Personalities.size(); ++i)
255 if (Personalities[i] == Personality)
257 Personalities.push_back(Personality);
262 MachineFunction &MachineModuleInfo::getMachineFunction(const Function &F) {
263 // Shortcut for the common case where a sequence of MachineFunctionPasses
264 // all query for the same Function.
265 if (LastRequest == &F)
268 auto I = MachineFunctions.insert(
269 std::make_pair(&F, std::unique_ptr<MachineFunction>()));
272 // No pre-existing machine function, create a new one.
273 MF = new MachineFunction(&F, TM, NextFnNum++, *this);
274 // Update the set entry.
275 I.first->second.reset(MF);
278 if (MFInitializer->initializeMachineFunction(*MF))
279 report_fatal_error("Unable to initialize machine function");
281 MF = I.first->second.get();
289 void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
290 MachineFunctions.erase(&F);
291 LastRequest = nullptr;
292 LastResult = nullptr;
297 /// This pass frees the MachineFunction object associated with a Function.
298 class FreeMachineFunction : public FunctionPass {
302 FreeMachineFunction() : FunctionPass(ID) {}
304 void getAnalysisUsage(AnalysisUsage &AU) const override {
305 AU.addRequired<MachineModuleInfo>();
306 AU.addPreserved<MachineModuleInfo>();
309 bool runOnFunction(Function &F) override {
310 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
311 MMI.deleteMachineFunctionFor(F);
315 StringRef getPassName() const override {
316 return "Free MachineFunction";
320 } // end anonymous namespace
322 char FreeMachineFunction::ID;
324 FunctionPass *llvm::createFreeMachineFunctionPass() {
325 return new FreeMachineFunction();
328 //===- MMI building helpers -----------------------------------------------===//
330 void llvm::computeUsesVAFloatArgument(const CallInst &I,
331 MachineModuleInfo &MMI) {
333 cast<FunctionType>(I.getCalledValue()->getType()->getContainedType(0));
334 if (FT->isVarArg() && !MMI.usesVAFloatArgument()) {
335 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
336 Type *T = I.getArgOperand(i)->getType();
337 for (auto i : post_order(T)) {
338 if (i->isFloatingPointTy()) {
339 MMI.setUsesVAFloatArgument(true);