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/CodeGen/MachineModuleInfo.h"
11 #include "llvm/ADT/PointerUnion.h"
12 #include "llvm/ADT/PostOrderIterator.h"
13 #include "llvm/ADT/TinyPtrVector.h"
14 #include "llvm/Analysis/EHPersonalities.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionInitializer.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetMachine.h"
32 using namespace llvm::dwarf;
34 // Handle the Pass registration stuff necessary to use DataLayout's.
35 INITIALIZE_TM_PASS(MachineModuleInfo, "machinemoduleinfo",
36 "Machine Module Information", false, false)
37 char MachineModuleInfo::ID = 0;
39 // Out of line virtual method.
40 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
43 class MMIAddrLabelMapCallbackPtr final : CallbackVH {
46 MMIAddrLabelMapCallbackPtr() : Map(nullptr) {}
47 MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(nullptr) {}
49 void setPtr(BasicBlock *BB) {
50 ValueHandleBase::operator=(BB);
53 void setMap(MMIAddrLabelMap *map) { Map = map; }
55 void deleted() override;
56 void allUsesReplacedWith(Value *V2) override;
59 class MMIAddrLabelMap {
61 struct AddrLabelSymEntry {
62 /// The symbols for the label.
63 TinyPtrVector<MCSymbol *> Symbols;
65 Function *Fn; // The containing function of the BasicBlock.
66 unsigned Index; // The index in BBCallbacks for the BasicBlock.
69 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
71 /// Callbacks for the BasicBlock's that we have entries for. We use this so
72 /// we get notified if a block is deleted or RAUWd.
73 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
75 /// This is a per-function list of symbols whose corresponding BasicBlock got
76 /// deleted. These symbols need to be emitted at some point in the file, so
77 /// AsmPrinter emits them after the function body.
78 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
79 DeletedAddrLabelsNeedingEmission;
82 MMIAddrLabelMap(MCContext &context) : Context(context) {}
84 assert(DeletedAddrLabelsNeedingEmission.empty() &&
85 "Some labels for deleted blocks never got emitted");
88 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
90 void takeDeletedSymbolsForFunction(Function *F,
91 std::vector<MCSymbol*> &Result);
93 void UpdateForDeletedBlock(BasicBlock *BB);
94 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
98 ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
99 assert(BB->hasAddressTaken() &&
100 "Shouldn't get label for block without address taken");
101 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
103 // If we already had an entry for this block, just return it.
104 if (!Entry.Symbols.empty()) {
105 assert(BB->getParent() == Entry.Fn && "Parent changed");
106 return Entry.Symbols;
109 // Otherwise, this is a new entry, create a new symbol for it and add an
110 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
111 BBCallbacks.emplace_back(BB);
112 BBCallbacks.back().setMap(this);
113 Entry.Index = BBCallbacks.size() - 1;
114 Entry.Fn = BB->getParent();
115 Entry.Symbols.push_back(Context.createTempSymbol());
116 return Entry.Symbols;
119 /// If we have any deleted symbols for F, return them.
120 void MMIAddrLabelMap::
121 takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
122 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
123 DeletedAddrLabelsNeedingEmission.find(F);
125 // If there are no entries for the function, just return.
126 if (I == DeletedAddrLabelsNeedingEmission.end()) return;
128 // Otherwise, take the list.
129 std::swap(Result, I->second);
130 DeletedAddrLabelsNeedingEmission.erase(I);
134 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
135 // If the block got deleted, there is no need for the symbol. If the symbol
136 // was already emitted, we can just forget about it, otherwise we need to
137 // queue it up for later emission when the function is output.
138 AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
139 AddrLabelSymbols.erase(BB);
140 assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
141 BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
143 assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
144 "Block/parent mismatch");
146 for (MCSymbol *Sym : Entry.Symbols) {
147 if (Sym->isDefined())
150 // If the block is not yet defined, we need to emit it at the end of the
151 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
152 // for the containing Function. Since the block is being deleted, its
153 // parent may already be removed, we have to get the function from 'Entry'.
154 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
158 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
159 // Get the entry for the RAUW'd block and remove it from our map.
160 AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
161 AddrLabelSymbols.erase(Old);
162 assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
164 AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
166 // If New is not address taken, just move our symbol over to it.
167 if (NewEntry.Symbols.empty()) {
168 BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
169 NewEntry = std::move(OldEntry); // Set New's entry.
173 BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
175 // Otherwise, we need to add the old symbols to the new block's set.
176 NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(),
177 OldEntry.Symbols.end());
181 void MMIAddrLabelMapCallbackPtr::deleted() {
182 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
185 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
186 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
190 //===----------------------------------------------------------------------===//
192 MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM)
193 : ImmutablePass(ID), TM(*TM),
194 Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
195 TM->getObjFileLowering(), nullptr, false) {
196 initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
199 MachineModuleInfo::~MachineModuleInfo() {
202 bool MachineModuleInfo::doInitialization(Module &M) {
204 ObjFileMMI = nullptr;
206 DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
207 AddrLabelSymbols = nullptr;
213 bool MachineModuleInfo::doFinalization(Module &M) {
215 Personalities.clear();
217 delete AddrLabelSymbols;
218 AddrLabelSymbols = nullptr;
223 ObjFileMMI = nullptr;
228 //===- Address of Block Management ----------------------------------------===//
231 MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
232 // Lazily create AddrLabelSymbols.
233 if (!AddrLabelSymbols)
234 AddrLabelSymbols = new MMIAddrLabelMap(Context);
235 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
238 void MachineModuleInfo::
239 takeDeletedSymbolsForFunction(const Function *F,
240 std::vector<MCSymbol*> &Result) {
241 // If no blocks have had their addresses taken, we're done.
242 if (!AddrLabelSymbols) return;
243 return AddrLabelSymbols->
244 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
247 /// \name Exception Handling
250 void MachineModuleInfo::addPersonality(const Function *Personality) {
251 for (unsigned i = 0; i < Personalities.size(); ++i)
252 if (Personalities[i] == Personality)
254 Personalities.push_back(Personality);
259 MachineFunction &MachineModuleInfo::getMachineFunction(const Function &F) {
260 // Shortcut for the common case where a sequence of MachineFunctionPasses
261 // all query for the same Function.
262 if (LastRequest == &F)
265 auto I = MachineFunctions.insert(
266 std::make_pair(&F, std::unique_ptr<MachineFunction>()));
269 // No pre-existing machine function, create a new one.
270 MF = new MachineFunction(&F, TM, NextFnNum++, *this);
271 // Update the set entry.
272 I.first->second.reset(MF);
275 if (MFInitializer->initializeMachineFunction(*MF))
276 report_fatal_error("Unable to initialize machine function");
278 MF = I.first->second.get();
286 void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
287 MachineFunctions.erase(&F);
288 LastRequest = nullptr;
289 LastResult = nullptr;
293 /// This pass frees the MachineFunction object associated with a Function.
294 class FreeMachineFunction : public FunctionPass {
297 FreeMachineFunction() : FunctionPass(ID) {}
299 void getAnalysisUsage(AnalysisUsage &AU) const override {
300 AU.addRequired<MachineModuleInfo>();
301 AU.addPreserved<MachineModuleInfo>();
304 bool runOnFunction(Function &F) override {
305 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
306 MMI.deleteMachineFunctionFor(F);
310 StringRef getPassName() const override {
311 return "Free MachineFunction";
314 char FreeMachineFunction::ID;
315 } // end anonymous namespace
318 FunctionPass *createFreeMachineFunctionPass() {
319 return new FreeMachineFunction();
321 } // end namespace llvm
323 //===- MMI building helpers -----------------------------------------------===//
325 void llvm::computeUsesVAFloatArgument(const CallInst &I,
326 MachineModuleInfo &MMI) {
328 cast<FunctionType>(I.getCalledValue()->getType()->getContainedType(0));
329 if (FT->isVarArg() && !MMI.usesVAFloatArgument()) {
330 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
331 Type *T = I.getArgOperand(i)->getType();
332 for (auto i : post_order(T)) {
333 if (i->isFloatingPointTy()) {
334 MMI.setUsesVAFloatArgument(true);