blob: a303426b420c28173ed683ff4affea6497faeca5 [file] [log] [blame]
Jim Laskey5a1df972007-01-26 21:38:26 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
Jim Laskey44317392006-01-04 13:36:38 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey44317392006-01-04 13:36:38 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey44317392006-01-04 13:36:38 +00009
Jim Laskey5a1df972007-01-26 21:38:26 +000010#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/PointerUnion.h"
Reid Kleckner2d5fb682015-02-14 00:21:02 +000012#include "llvm/Analysis/LibCallSemantics.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/Analysis/ValueTracking.h"
14#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/CodeGen/MachineFunctionPass.h"
16#include "llvm/CodeGen/Passes.h"
David Majnemercde33032015-03-30 22:58:10 +000017#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/GlobalVariable.h"
21#include "llvm/IR/Module.h"
Evan Cheng76792992011-07-20 05:58:47 +000022#include "llvm/MC/MCObjectFileInfo.h"
Chris Lattner34adc8d2010-03-14 01:41:15 +000023#include "llvm/MC/MCSymbol.h"
Jim Laskey0bbdc552006-01-26 20:21:46 +000024#include "llvm/Support/Dwarf.h"
Torok Edwin56d06592009-07-11 20:10:48 +000025#include "llvm/Support/ErrorHandling.h"
Jim Laskey44317392006-01-04 13:36:38 +000026using namespace llvm;
Jim Laskey4e71db12006-03-01 20:39:36 +000027using namespace llvm::dwarf;
Jim Laskey44317392006-01-04 13:36:38 +000028
Micah Villmowcdfe20b2012-10-08 16:38:25 +000029// Handle the Pass registration stuff necessary to use DataLayout's.
Owen Andersona57b97e2010-07-21 22:09:45 +000030INITIALIZE_PASS(MachineModuleInfo, "machinemoduleinfo",
Owen Andersondf7a4f22010-10-07 22:25:06 +000031 "Machine Module Information", false, false)
Devang Patel8c78a0b2007-05-03 01:11:54 +000032char MachineModuleInfo::ID = 0;
Jim Laskeyb9966022006-01-17 17:31:53 +000033
Chris Lattnerf2471ec2009-09-15 22:44:26 +000034// Out of line virtual method.
35MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
36
Chris Lattner347a0eb2010-03-15 19:09:43 +000037namespace llvm {
38class MMIAddrLabelMapCallbackPtr : CallbackVH {
39 MMIAddrLabelMap *Map;
40public:
Craig Topperc0196b12014-04-14 00:51:57 +000041 MMIAddrLabelMapCallbackPtr() : Map(nullptr) {}
42 MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(nullptr) {}
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000043
Chris Lattnerb1c4f622010-03-22 23:15:57 +000044 void setPtr(BasicBlock *BB) {
45 ValueHandleBase::operator=(BB);
46 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000047
Chris Lattner347a0eb2010-03-15 19:09:43 +000048 void setMap(MMIAddrLabelMap *map) { Map = map; }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000049
Craig Topper4584cd52014-03-07 09:26:03 +000050 void deleted() override;
51 void allUsesReplacedWith(Value *V2) override;
Chris Lattner347a0eb2010-03-15 19:09:43 +000052};
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000053
Chris Lattner347a0eb2010-03-15 19:09:43 +000054class MMIAddrLabelMap {
55 MCContext &Context;
56 struct AddrLabelSymEntry {
Chris Lattnerdb035a02010-03-16 00:29:39 +000057 /// Symbols - The symbols for the label. This is a pointer union that is
58 /// either one symbol (the common case) or a list of symbols.
59 PointerUnion<MCSymbol *, std::vector<MCSymbol*>*> Symbols;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000060
Chris Lattner561334a2010-03-15 20:39:00 +000061 Function *Fn; // The containing function of the BasicBlock.
62 unsigned Index; // The index in BBCallbacks for the BasicBlock.
Chris Lattner347a0eb2010-03-15 19:09:43 +000063 };
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000064
Chris Lattner347a0eb2010-03-15 19:09:43 +000065 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000066
Chris Lattner561334a2010-03-15 20:39:00 +000067 /// BBCallbacks - Callbacks for the BasicBlock's that we have entries for. We
68 /// use this so we get notified if a block is deleted or RAUWd.
Chris Lattner347a0eb2010-03-15 19:09:43 +000069 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
Chris Lattner561334a2010-03-15 20:39:00 +000070
71 /// DeletedAddrLabelsNeedingEmission - This is a per-function list of symbols
72 /// whose corresponding BasicBlock got deleted. These symbols need to be
73 /// emitted at some point in the file, so AsmPrinter emits them after the
74 /// function body.
75 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
76 DeletedAddrLabelsNeedingEmission;
Chris Lattner347a0eb2010-03-15 19:09:43 +000077public:
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000078
Chris Lattner347a0eb2010-03-15 19:09:43 +000079 MMIAddrLabelMap(MCContext &context) : Context(context) {}
Chris Lattner561334a2010-03-15 20:39:00 +000080 ~MMIAddrLabelMap() {
81 assert(DeletedAddrLabelsNeedingEmission.empty() &&
82 "Some labels for deleted blocks never got emitted");
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000083
Chris Lattnerdb035a02010-03-16 00:29:39 +000084 // Deallocate any of the 'list of symbols' case.
85 for (DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry>::iterator
86 I = AddrLabelSymbols.begin(), E = AddrLabelSymbols.end(); I != E; ++I)
87 if (I->second.Symbols.is<std::vector<MCSymbol*>*>())
88 delete I->second.Symbols.get<std::vector<MCSymbol*>*>();
Chris Lattner561334a2010-03-15 20:39:00 +000089 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000090
Chris Lattnerdb035a02010-03-16 00:29:39 +000091 MCSymbol *getAddrLabelSymbol(BasicBlock *BB);
92 std::vector<MCSymbol*> getAddrLabelSymbolToEmit(BasicBlock *BB);
93
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000094 void takeDeletedSymbolsForFunction(Function *F,
Chris Lattner561334a2010-03-15 20:39:00 +000095 std::vector<MCSymbol*> &Result);
96
Chris Lattner347a0eb2010-03-15 19:09:43 +000097 void UpdateForDeletedBlock(BasicBlock *BB);
98 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
99};
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000100} // namespace llvm
Chris Lattner347a0eb2010-03-15 19:09:43 +0000101
102MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
103 assert(BB->hasAddressTaken() &&
104 "Shouldn't get label for block without address taken");
105 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000106
Chris Lattner347a0eb2010-03-15 19:09:43 +0000107 // If we already had an entry for this block, just return it.
Chris Lattnerdb035a02010-03-16 00:29:39 +0000108 if (!Entry.Symbols.isNull()) {
Chris Lattner561334a2010-03-15 20:39:00 +0000109 assert(BB->getParent() == Entry.Fn && "Parent changed");
Chris Lattnerdb035a02010-03-16 00:29:39 +0000110 if (Entry.Symbols.is<MCSymbol*>())
111 return Entry.Symbols.get<MCSymbol*>();
112 return (*Entry.Symbols.get<std::vector<MCSymbol*>*>())[0];
Chris Lattner561334a2010-03-15 20:39:00 +0000113 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000114
Chris Lattner347a0eb2010-03-15 19:09:43 +0000115 // Otherwise, this is a new entry, create a new symbol for it and add an
116 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000117 BBCallbacks.emplace_back(BB);
Chris Lattner347a0eb2010-03-15 19:09:43 +0000118 BBCallbacks.back().setMap(this);
119 Entry.Index = BBCallbacks.size()-1;
Chris Lattner561334a2010-03-15 20:39:00 +0000120 Entry.Fn = BB->getParent();
Jim Grosbach6f482002015-05-18 18:43:14 +0000121 MCSymbol *Result = Context.createTempSymbol();
Chris Lattnerdb035a02010-03-16 00:29:39 +0000122 Entry.Symbols = Result;
123 return Result;
Chris Lattner347a0eb2010-03-15 19:09:43 +0000124}
125
Chris Lattnerdb035a02010-03-16 00:29:39 +0000126std::vector<MCSymbol*>
127MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
128 assert(BB->hasAddressTaken() &&
129 "Shouldn't get label for block without address taken");
130 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000131
Chris Lattnerdb035a02010-03-16 00:29:39 +0000132 std::vector<MCSymbol*> Result;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000133
Chris Lattnerdb035a02010-03-16 00:29:39 +0000134 // If we already had an entry for this block, just return it.
135 if (Entry.Symbols.isNull())
136 Result.push_back(getAddrLabelSymbol(BB));
137 else if (MCSymbol *Sym = Entry.Symbols.dyn_cast<MCSymbol*>())
138 Result.push_back(Sym);
139 else
140 Result = *Entry.Symbols.get<std::vector<MCSymbol*>*>();
141 return Result;
142}
143
144
Chris Lattner561334a2010-03-15 20:39:00 +0000145/// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
146/// them.
147void MMIAddrLabelMap::
148takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
149 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
150 DeletedAddrLabelsNeedingEmission.find(F);
151
152 // If there are no entries for the function, just return.
153 if (I == DeletedAddrLabelsNeedingEmission.end()) return;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000154
Chris Lattner561334a2010-03-15 20:39:00 +0000155 // Otherwise, take the list.
156 std::swap(Result, I->second);
157 DeletedAddrLabelsNeedingEmission.erase(I);
158}
159
160
Chris Lattner347a0eb2010-03-15 19:09:43 +0000161void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
162 // If the block got deleted, there is no need for the symbol. If the symbol
163 // was already emitted, we can just forget about it, otherwise we need to
164 // queue it up for later emission when the function is output.
165 AddrLabelSymEntry Entry = AddrLabelSymbols[BB];
166 AddrLabelSymbols.erase(BB);
Chris Lattnerdb035a02010-03-16 00:29:39 +0000167 assert(!Entry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
Craig Topperc0196b12014-04-14 00:51:57 +0000168 BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
Chris Lattner347a0eb2010-03-15 19:09:43 +0000169
Craig Topperc0196b12014-04-14 00:51:57 +0000170 assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
Chris Lattner561334a2010-03-15 20:39:00 +0000171 "Block/parent mismatch");
Chris Lattnerdb035a02010-03-16 00:29:39 +0000172
173 // Handle both the single and the multiple symbols cases.
174 if (MCSymbol *Sym = Entry.Symbols.dyn_cast<MCSymbol*>()) {
175 if (Sym->isDefined())
176 return;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000177
Chris Lattnerdb035a02010-03-16 00:29:39 +0000178 // If the block is not yet defined, we need to emit it at the end of the
179 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
180 // for the containing Function. Since the block is being deleted, its
181 // parent may already be removed, we have to get the function from 'Entry'.
182 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
183 } else {
184 std::vector<MCSymbol*> *Syms = Entry.Symbols.get<std::vector<MCSymbol*>*>();
185
186 for (unsigned i = 0, e = Syms->size(); i != e; ++i) {
187 MCSymbol *Sym = (*Syms)[i];
188 if (Sym->isDefined()) continue; // Ignore already emitted labels.
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000189
Chris Lattnerdb035a02010-03-16 00:29:39 +0000190 // If the block is not yet defined, we need to emit it at the end of the
191 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
192 // for the containing Function. Since the block is being deleted, its
193 // parent may already be removed, we have to get the function from
194 // 'Entry'.
195 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
196 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000197
Chris Lattnerdb035a02010-03-16 00:29:39 +0000198 // The entry is deleted, free the memory associated with the symbol list.
199 delete Syms;
200 }
Chris Lattner347a0eb2010-03-15 19:09:43 +0000201}
202
203void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
204 // Get the entry for the RAUW'd block and remove it from our map.
205 AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old];
206 AddrLabelSymbols.erase(Old);
Chris Lattnerdb035a02010-03-16 00:29:39 +0000207 assert(!OldEntry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
208
209 AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
210
Chris Lattner347a0eb2010-03-15 19:09:43 +0000211 // If New is not address taken, just move our symbol over to it.
Chris Lattnerdb035a02010-03-16 00:29:39 +0000212 if (NewEntry.Symbols.isNull()) {
Chris Lattnerb1c4f622010-03-22 23:15:57 +0000213 BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
Chris Lattnerdb035a02010-03-16 00:29:39 +0000214 NewEntry = OldEntry; // Set New's entry.
215 return;
Chris Lattner347a0eb2010-03-15 19:09:43 +0000216 }
Chris Lattnerdb035a02010-03-16 00:29:39 +0000217
Craig Topperc0196b12014-04-14 00:51:57 +0000218 BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
Chris Lattnerdb035a02010-03-16 00:29:39 +0000219
220 // Otherwise, we need to add the old symbol to the new block's set. If it is
221 // just a single entry, upgrade it to a symbol list.
222 if (MCSymbol *PrevSym = NewEntry.Symbols.dyn_cast<MCSymbol*>()) {
223 std::vector<MCSymbol*> *SymList = new std::vector<MCSymbol*>();
224 SymList->push_back(PrevSym);
225 NewEntry.Symbols = SymList;
226 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000227
Chris Lattnerdb035a02010-03-16 00:29:39 +0000228 std::vector<MCSymbol*> *SymList =
229 NewEntry.Symbols.get<std::vector<MCSymbol*>*>();
230
231 // If the old entry was a single symbol, add it.
232 if (MCSymbol *Sym = OldEntry.Symbols.dyn_cast<MCSymbol*>()) {
233 SymList->push_back(Sym);
234 return;
235 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000236
Chris Lattnerdb035a02010-03-16 00:29:39 +0000237 // Otherwise, concatenate the list.
238 std::vector<MCSymbol*> *Syms =OldEntry.Symbols.get<std::vector<MCSymbol*>*>();
239 SymList->insert(SymList->end(), Syms->begin(), Syms->end());
240 delete Syms;
Chris Lattner347a0eb2010-03-15 19:09:43 +0000241}
242
243
244void MMIAddrLabelMapCallbackPtr::deleted() {
245 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
246}
247
248void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
249 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
250}
251
252
Jim Laskey0bbdc552006-01-26 20:21:46 +0000253//===----------------------------------------------------------------------===//
Eric Christopherc2645712009-08-26 21:27:09 +0000254
Rafael Espindola0a017a62010-12-10 07:39:47 +0000255MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI,
Evan Chengd60fa58b2011-07-18 20:57:22 +0000256 const MCRegisterInfo &MRI,
Evan Chengbbf3b0d2011-07-20 19:50:42 +0000257 const MCObjectFileInfo *MOFI)
Craig Topperc0196b12014-04-14 00:51:57 +0000258 : ImmutablePass(ID), Context(&MAI, &MRI, MOFI, nullptr, false) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000259 initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000260}
Jim Laskey0bbdc552006-01-26 20:21:46 +0000261
Chris Lattnere468f882010-03-13 20:55:24 +0000262MachineModuleInfo::MachineModuleInfo()
Craig Topperc0196b12014-04-14 00:51:57 +0000263 : ImmutablePass(ID), Context(nullptr, nullptr, nullptr) {
Craig Topperee4dab52012-02-05 08:31:47 +0000264 llvm_unreachable("This MachineModuleInfo constructor should never be called, "
265 "MMI should always be explicitly constructed by "
266 "LLVMTargetMachine");
Chris Lattnere468f882010-03-13 20:55:24 +0000267}
268
Chris Lattnerf2471ec2009-09-15 22:44:26 +0000269MachineModuleInfo::~MachineModuleInfo() {
Pedro Artigas41b98842012-12-05 17:12:22 +0000270}
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000271
Pedro Artigas41b98842012-12-05 17:12:22 +0000272bool MachineModuleInfo::doInitialization(Module &M) {
Pedro Artigase84b13f2012-12-06 22:12:44 +0000273
Craig Topperc0196b12014-04-14 00:51:57 +0000274 ObjFileMMI = nullptr;
Pedro Artigas41b98842012-12-05 17:12:22 +0000275 CurCallSite = 0;
276 CallsEHReturn = 0;
277 CallsUnwindInit = 0;
Peter Collingbourne7ef497b2014-12-30 20:05:19 +0000278 DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
Pedro Artigas41b98842012-12-05 17:12:22 +0000279 // Always emit some info, by default "no personality" info.
Craig Topperc0196b12014-04-14 00:51:57 +0000280 Personalities.push_back(nullptr);
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000281 PersonalityTypeCache = EHPersonality::Unknown;
Craig Topperc0196b12014-04-14 00:51:57 +0000282 AddrLabelSymbols = nullptr;
283 TheModule = nullptr;
Pedro Artigas41b98842012-12-05 17:12:22 +0000284
285 return false;
286}
287
288bool MachineModuleInfo::doFinalization(Module &M) {
289
290 Personalities.clear();
291
Chris Lattner347a0eb2010-03-15 19:09:43 +0000292 delete AddrLabelSymbols;
Craig Topperc0196b12014-04-14 00:51:57 +0000293 AddrLabelSymbols = nullptr;
Pedro Artigas41b98842012-12-05 17:12:22 +0000294
Pedro Artigas7212ee42012-12-12 22:59:46 +0000295 Context.reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +0000296
Pedro Artigas33832252013-01-04 18:04:42 +0000297 delete ObjFileMMI;
Craig Topperc0196b12014-04-14 00:51:57 +0000298 ObjFileMMI = nullptr;
Pedro Artigas33832252013-01-04 18:04:42 +0000299
Pedro Artigas41b98842012-12-05 17:12:22 +0000300 return false;
Jim Laskey0bbdc552006-01-26 20:21:46 +0000301}
302
Jim Laskey5a1df972007-01-26 21:38:26 +0000303/// EndFunction - Discard function meta information.
Jim Laskey2d7298c2006-04-07 16:34:46 +0000304///
Jim Laskey5a1df972007-01-26 21:38:26 +0000305void MachineModuleInfo::EndFunction() {
Jim Laskey2d7298c2006-04-07 16:34:46 +0000306 // Clean up frame info.
Rafael Espindola227144c2013-05-13 01:16:13 +0000307 FrameInstructions.clear();
Eric Christopherc2645712009-08-26 21:27:09 +0000308
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000309 // Clean up exception info.
310 LandingPads.clear();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000311 PersonalityTypeCache = EHPersonality::Unknown;
Jim Grosbach54c05302010-01-28 01:45:32 +0000312 CallSiteMap.clear();
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000313 TypeInfos.clear();
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000314 FilterIds.clear();
Duncan Sands4836e3a2007-07-05 15:15:01 +0000315 FilterEnds.clear();
Anton Korobeynikov383a3242007-07-14 14:06:15 +0000316 CallsEHReturn = 0;
317 CallsUnwindInit = 0;
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +0000318 VariableDbgInfos.clear();
Jim Laskey2d7298c2006-04-07 16:34:46 +0000319}
320
Chris Lattner347a0eb2010-03-15 19:09:43 +0000321//===- Address of Block Management ----------------------------------------===//
322
323
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000324/// getAddrLabelSymbol - Return the symbol to be used for the specified basic
325/// block when its address is taken. This cannot be its normal LBB label
326/// because the block may be accessed outside its containing function.
327MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
Chris Lattner347a0eb2010-03-15 19:09:43 +0000328 // Lazily create AddrLabelSymbols.
Craig Topperc0196b12014-04-14 00:51:57 +0000329 if (!AddrLabelSymbols)
Chris Lattner347a0eb2010-03-15 19:09:43 +0000330 AddrLabelSymbols = new MMIAddrLabelMap(Context);
331 return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000332}
333
Chris Lattnerdb035a02010-03-16 00:29:39 +0000334/// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
335/// basic block when its address is taken. If other blocks were RAUW'd to
336/// this one, we may have to emit them as well, return the whole set.
337std::vector<MCSymbol*> MachineModuleInfo::
338getAddrLabelSymbolToEmit(const BasicBlock *BB) {
339 // Lazily create AddrLabelSymbols.
Craig Topperc0196b12014-04-14 00:51:57 +0000340 if (!AddrLabelSymbols)
Chris Lattnerdb035a02010-03-16 00:29:39 +0000341 AddrLabelSymbols = new MMIAddrLabelMap(Context);
342 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
343}
344
345
Chris Lattner561334a2010-03-15 20:39:00 +0000346/// takeDeletedSymbolsForFunction - If the specified function has had any
347/// references to address-taken blocks generated, but the block got deleted,
348/// return the symbol now so we can emit it. This prevents emitting a
349/// reference to a symbol that has no definition.
350void MachineModuleInfo::
351takeDeletedSymbolsForFunction(const Function *F,
352 std::vector<MCSymbol*> &Result) {
353 // If no blocks have had their addresses taken, we're done.
Craig Topperc0196b12014-04-14 00:51:57 +0000354 if (!AddrLabelSymbols) return;
Chris Lattner561334a2010-03-15 20:39:00 +0000355 return AddrLabelSymbols->
356 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
357}
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000358
Chris Lattner347a0eb2010-03-15 19:09:43 +0000359//===- EH -----------------------------------------------------------------===//
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000360
361/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
362/// specified MachineBasicBlock.
Bill Wendling2e5068942008-07-03 22:53:42 +0000363LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
364 (MachineBasicBlock *LandingPad) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000365 unsigned N = LandingPads.size();
366 for (unsigned i = 0; i < N; ++i) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000367 LandingPadInfo &LP = LandingPads[i];
368 if (LP.LandingPadBlock == LandingPad)
369 return LP;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000370 }
Eric Christopherc2645712009-08-26 21:27:09 +0000371
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000372 LandingPads.push_back(LandingPadInfo(LandingPad));
373 return LandingPads[N];
374}
375
376/// addInvoke - Provide the begin and end labels of an invoke style call and
377/// associate it with a try landing pad block.
378void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattner34adc8d2010-03-14 01:41:15 +0000379 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000380 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000381 LP.BeginLabels.push_back(BeginLabel);
382 LP.EndLabels.push_back(EndLabel);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000383}
384
385/// addLandingPad - Provide the label of a try LandingPad block.
386///
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000387MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000388 MCSymbol *LandingPadLabel = Context.createTempSymbol();
Jim Laskey6458e6a2007-03-01 20:25:32 +0000389 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopherc2645712009-08-26 21:27:09 +0000390 LP.LandingPadLabel = LandingPadLabel;
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000391 return LandingPadLabel;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000392}
393
394/// addPersonality - Provide the personality function for the exception
395/// information.
396void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000397 const Function *Personality) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000398 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000399 LP.Personality = Personality;
Anton Korobeynikov13da1782007-05-12 22:36:25 +0000400
Bill Wendling2e5068942008-07-03 22:53:42 +0000401 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000402 if (Personalities[i] == Personality)
403 return;
Eric Christopherc2645712009-08-26 21:27:09 +0000404
Eric Christophera258c622009-08-26 21:30:49 +0000405 // If this is the first personality we're adding go
406 // ahead and add it at the beginning.
Craig Topperc0196b12014-04-14 00:51:57 +0000407 if (!Personalities[0])
Eric Christophera258c622009-08-26 21:30:49 +0000408 Personalities[0] = Personality;
409 else
410 Personalities.push_back(Personality);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000411}
412
David Majnemercde33032015-03-30 22:58:10 +0000413void MachineModuleInfo::addWinEHState(MachineBasicBlock *LandingPad,
414 int State) {
415 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
416 LP.WinEHState = State;
417}
418
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000419/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
420///
Bill Wendlingf8d95bc2011-07-28 21:25:33 +0000421void MachineModuleInfo::
422addCatchTypeInfo(MachineBasicBlock *LandingPad,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000423 ArrayRef<const GlobalValue *> TyInfo) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000424 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000425 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey6458e6a2007-03-01 20:25:32 +0000426 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000427}
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000428
429/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey6458e6a2007-03-01 20:25:32 +0000430///
Bill Wendlingf8d95bc2011-07-28 21:25:33 +0000431void MachineModuleInfo::
432addFilterTypeInfo(MachineBasicBlock *LandingPad,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000433 ArrayRef<const GlobalValue *> TyInfo) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000434 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendlingb46e5162008-07-07 21:41:57 +0000435 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling2e5068942008-07-03 22:53:42 +0000436 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000437 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
438 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey6458e6a2007-03-01 20:25:32 +0000439}
440
Duncan Sandsef5a6542007-08-27 15:47:50 +0000441/// addCleanup - Add a cleanup action for a landing pad.
442///
443void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
444 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
445 LP.TypeIds.push_back(0);
446}
447
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000448void MachineModuleInfo::addSEHCatchHandler(MachineBasicBlock *LandingPad,
449 const Function *Filter,
450 const BlockAddress *RecoverBA) {
451 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
452 SEHHandler Handler;
453 Handler.FilterOrFinally = Filter;
454 Handler.RecoverBA = RecoverBA;
455 LP.SEHHandlers.push_back(Handler);
456}
457
458void MachineModuleInfo::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
459 const Function *Cleanup) {
460 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
461 SEHHandler Handler;
462 Handler.FilterOrFinally = Cleanup;
463 Handler.RecoverBA = nullptr;
464 LP.SEHHandlers.push_back(Handler);
465}
466
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000467/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
468/// pads.
Bill Wendling929f3c02010-04-16 08:46:10 +0000469void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000470 for (unsigned i = 0; i != LandingPads.size(); ) {
471 LandingPadInfo &LandingPad = LandingPads[i];
Bill Wendling929f3c02010-04-16 08:46:10 +0000472 if (LandingPad.LandingPadLabel &&
473 !LandingPad.LandingPadLabel->isDefined() &&
474 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
Craig Topperc0196b12014-04-14 00:51:57 +0000475 LandingPad.LandingPadLabel = nullptr;
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000476
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000477 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands030bce72007-12-19 07:36:31 +0000478 // "nounwind" case.
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000479 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000480 LandingPads.erase(LandingPads.begin() + i);
481 continue;
482 }
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000483
Chris Lattner34adc8d2010-03-14 01:41:15 +0000484 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
485 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
486 MCSymbol *EndLabel = LandingPad.EndLabels[j];
Bill Wendling929f3c02010-04-16 08:46:10 +0000487 if ((BeginLabel->isDefined() ||
488 (LPMap && (*LPMap)[BeginLabel] != 0)) &&
489 (EndLabel->isDefined() ||
490 (LPMap && (*LPMap)[EndLabel] != 0))) continue;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000491
Chris Lattner34adc8d2010-03-14 01:41:15 +0000492 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
493 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
494 --j, --e;
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000495 }
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000496
497 // Remove landing pads with no try-ranges.
Dan Gohman70de4cb2008-01-29 13:02:09 +0000498 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000499 LandingPads.erase(LandingPads.begin() + i);
500 continue;
501 }
502
503 // If there is no landing pad, ensure that the list of typeids is empty.
504 // If the only typeid is a cleanup, this is the same as having no typeids.
505 if (!LandingPad.LandingPadBlock ||
506 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
507 LandingPad.TypeIds.clear();
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000508 ++i;
509 }
510}
511
Bill Wendlingc2d55b62011-10-05 22:20:38 +0000512/// setCallSiteLandingPad - Map the landing pad's EH symbol to the call site
513/// indexes.
514void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
515 ArrayRef<unsigned> Sites) {
Benjamin Kramer0e3791e2012-02-14 10:29:27 +0000516 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
Bill Wendlingc2d55b62011-10-05 22:20:38 +0000517}
518
Eric Christopherc2645712009-08-26 21:27:09 +0000519/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000520/// function wide.
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000521unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
Bill Wendling2e5068942008-07-03 22:53:42 +0000522 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
523 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000524
525 TypeInfos.push_back(TI);
526 return TypeInfos.size();
527}
528
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000529/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
530/// function wide.
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000531int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands4836e3a2007-07-05 15:15:01 +0000532 // If the new filter coincides with the tail of an existing filter, then
533 // re-use the existing filter. Folding filters more than this requires
534 // re-ordering filters and/or their elements - probably not worth it.
535 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
536 E = FilterEnds.end(); I != E; ++I) {
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000537 unsigned i = *I, j = TyIds.size();
Duncan Sands4836e3a2007-07-05 15:15:01 +0000538
539 while (i && j)
540 if (FilterIds[--i] != TyIds[--j])
541 goto try_next;
542
543 if (!j)
544 // The new filter coincides with range [i, end) of the existing filter.
545 return -(1 + i);
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000546
Duncan Sands4836e3a2007-07-05 15:15:01 +0000547try_next:;
548 }
549
550 // Add the new filter.
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000551 int FilterID = -(1 + FilterIds.size());
552 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
Benjamin Kramer0e3791e2012-02-14 10:29:27 +0000553 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000554 FilterEnds.push_back(FilterIds.size());
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000555 FilterIds.push_back(0); // terminator
556 return FilterID;
557}
558
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000559/// getPersonality - Return the personality function for the current function.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000560const Function *MachineModuleInfo::getPersonality() const {
Reid Kleckner5cc15692015-01-23 18:49:01 +0000561 for (const LandingPadInfo &LPI : LandingPads)
562 if (LPI.Personality)
563 return LPI.Personality;
564 return nullptr;
565}
566
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000567EHPersonality MachineModuleInfo::getPersonalityType() {
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000568 if (PersonalityTypeCache == EHPersonality::Unknown) {
569 if (const Function *F = getPersonality())
570 PersonalityTypeCache = classifyEHPersonality(F);
571 }
Reid Kleckner5cc15692015-01-23 18:49:01 +0000572 return PersonalityTypeCache;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000573}
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000574
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000575/// getPersonalityIndex - Return unique index for current personality
Eric Christopher92335652009-08-26 21:44:57 +0000576/// function. NULL/first personality function should always get zero index.
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000577unsigned MachineModuleInfo::getPersonalityIndex() const {
Craig Topperc0196b12014-04-14 00:51:57 +0000578 const Function* Personality = nullptr;
Eric Christopherc2645712009-08-26 21:27:09 +0000579
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000580 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling05f73802012-02-13 23:45:26 +0000581 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000582 if (LandingPads[i].Personality) {
583 Personality = LandingPads[i].Personality;
584 break;
585 }
Eric Christopherc2645712009-08-26 21:27:09 +0000586
Bill Wendling05f73802012-02-13 23:45:26 +0000587 for (unsigned i = 0, e = Personalities.size(); i < e; ++i) {
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000588 if (Personalities[i] == Personality)
589 return i;
Bill Wendling2e5068942008-07-03 22:53:42 +0000590 }
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000591
Eric Christopher92335652009-08-26 21:44:57 +0000592 // This will happen if the current personality function is
593 // in the zero index.
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000594 return 0;
595}
David Majnemercde33032015-03-30 22:58:10 +0000596
597const Function *MachineModuleInfo::getWinEHParent(const Function *F) const {
598 StringRef WinEHParentName =
599 F->getFnAttribute("wineh-parent").getValueAsString();
600 if (WinEHParentName.empty() || WinEHParentName == F->getName())
601 return F;
602 return F->getParent()->getFunction(WinEHParentName);
603}
604
605WinEHFuncInfo &MachineModuleInfo::getWinEHFuncInfo(const Function *F) {
606 auto &Ptr = FuncInfoMap[getWinEHParent(F)];
607 if (!Ptr)
608 Ptr.reset(new WinEHFuncInfo);
609 return *Ptr;
610}