blob: 244e3fbc4e8f4286a0b2eed43930e83423bec556 [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 Klecknerc20276d2015-11-17 21:10:25 +000012#include "llvm/ADT/TinyPtrVector.h"
David Majnemer70497c62015-12-02 23:06:39 +000013#include "llvm/Analysis/EHPersonalities.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Analysis/ValueTracking.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/Passes.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 {
David Blaikie774b5842015-08-03 22:30:24 +000038class MMIAddrLabelMapCallbackPtr final : CallbackVH {
Chris Lattner347a0eb2010-03-15 19:09:43 +000039 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 {
Benjamin Kramer6fe4e792015-06-29 20:21:55 +000057 /// Symbols - The symbols for the label.
58 TinyPtrVector<MCSymbol *> Symbols;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000059
Chris Lattner561334a2010-03-15 20:39:00 +000060 Function *Fn; // The containing function of the BasicBlock.
61 unsigned Index; // The index in BBCallbacks for the BasicBlock.
Chris Lattner347a0eb2010-03-15 19:09:43 +000062 };
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000063
Chris Lattner347a0eb2010-03-15 19:09:43 +000064 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000065
Chris Lattner561334a2010-03-15 20:39:00 +000066 /// BBCallbacks - Callbacks for the BasicBlock's that we have entries for. We
67 /// use this so we get notified if a block is deleted or RAUWd.
Chris Lattner347a0eb2010-03-15 19:09:43 +000068 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
Chris Lattner561334a2010-03-15 20:39:00 +000069
70 /// DeletedAddrLabelsNeedingEmission - This is a per-function list of symbols
71 /// whose corresponding BasicBlock got deleted. These symbols need to be
72 /// emitted at some point in the file, so AsmPrinter emits them after the
73 /// function body.
74 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
75 DeletedAddrLabelsNeedingEmission;
Chris Lattner347a0eb2010-03-15 19:09:43 +000076public:
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000077
Chris Lattner347a0eb2010-03-15 19:09:43 +000078 MMIAddrLabelMap(MCContext &context) : Context(context) {}
Chris Lattner561334a2010-03-15 20:39:00 +000079 ~MMIAddrLabelMap() {
80 assert(DeletedAddrLabelsNeedingEmission.empty() &&
81 "Some labels for deleted blocks never got emitted");
82 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000083
Benjamin Kramer6fe4e792015-06-29 20:21:55 +000084 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
Chris Lattnerdb035a02010-03-16 00:29:39 +000085
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000086 void takeDeletedSymbolsForFunction(Function *F,
Chris Lattner561334a2010-03-15 20:39:00 +000087 std::vector<MCSymbol*> &Result);
88
Chris Lattner347a0eb2010-03-15 19:09:43 +000089 void UpdateForDeletedBlock(BasicBlock *BB);
90 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
91};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000092}
Chris Lattner347a0eb2010-03-15 19:09:43 +000093
Benjamin Kramer6fe4e792015-06-29 20:21:55 +000094ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
Chris Lattner347a0eb2010-03-15 19:09:43 +000095 assert(BB->hasAddressTaken() &&
96 "Shouldn't get label for block without address taken");
97 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +000098
Chris Lattner347a0eb2010-03-15 19:09:43 +000099 // If we already had an entry for this block, just return it.
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000100 if (!Entry.Symbols.empty()) {
Chris Lattner561334a2010-03-15 20:39:00 +0000101 assert(BB->getParent() == Entry.Fn && "Parent changed");
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000102 return Entry.Symbols;
Chris Lattner561334a2010-03-15 20:39:00 +0000103 }
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000104
Chris Lattner347a0eb2010-03-15 19:09:43 +0000105 // Otherwise, this is a new entry, create a new symbol for it and add an
106 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000107 BBCallbacks.emplace_back(BB);
Chris Lattner347a0eb2010-03-15 19:09:43 +0000108 BBCallbacks.back().setMap(this);
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000109 Entry.Index = BBCallbacks.size() - 1;
Chris Lattner561334a2010-03-15 20:39:00 +0000110 Entry.Fn = BB->getParent();
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000111 Entry.Symbols.push_back(Context.createTempSymbol());
112 return Entry.Symbols;
Chris Lattner347a0eb2010-03-15 19:09:43 +0000113}
114
Chris Lattner561334a2010-03-15 20:39:00 +0000115/// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
116/// them.
117void MMIAddrLabelMap::
118takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
119 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
120 DeletedAddrLabelsNeedingEmission.find(F);
121
122 // If there are no entries for the function, just return.
123 if (I == DeletedAddrLabelsNeedingEmission.end()) return;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000124
Chris Lattner561334a2010-03-15 20:39:00 +0000125 // Otherwise, take the list.
126 std::swap(Result, I->second);
127 DeletedAddrLabelsNeedingEmission.erase(I);
128}
129
130
Chris Lattner347a0eb2010-03-15 19:09:43 +0000131void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
132 // If the block got deleted, there is no need for the symbol. If the symbol
133 // was already emitted, we can just forget about it, otherwise we need to
134 // queue it up for later emission when the function is output.
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000135 AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
Chris Lattner347a0eb2010-03-15 19:09:43 +0000136 AddrLabelSymbols.erase(BB);
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000137 assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
Craig Topperc0196b12014-04-14 00:51:57 +0000138 BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
Chris Lattner347a0eb2010-03-15 19:09:43 +0000139
Craig Topperc0196b12014-04-14 00:51:57 +0000140 assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
Chris Lattner561334a2010-03-15 20:39:00 +0000141 "Block/parent mismatch");
Chris Lattnerdb035a02010-03-16 00:29:39 +0000142
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000143 for (MCSymbol *Sym : Entry.Symbols) {
Chris Lattnerdb035a02010-03-16 00:29:39 +0000144 if (Sym->isDefined())
145 return;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000146
Chris Lattnerdb035a02010-03-16 00:29:39 +0000147 // If the block is not yet defined, we need to emit it at the end of the
148 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
149 // for the containing Function. Since the block is being deleted, its
150 // parent may already be removed, we have to get the function from 'Entry'.
151 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
Chris Lattnerdb035a02010-03-16 00:29:39 +0000152 }
Chris Lattner347a0eb2010-03-15 19:09:43 +0000153}
154
155void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
156 // Get the entry for the RAUW'd block and remove it from our map.
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000157 AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
Chris Lattner347a0eb2010-03-15 19:09:43 +0000158 AddrLabelSymbols.erase(Old);
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000159 assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
Chris Lattnerdb035a02010-03-16 00:29:39 +0000160
161 AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
162
Chris Lattner347a0eb2010-03-15 19:09:43 +0000163 // If New is not address taken, just move our symbol over to it.
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000164 if (NewEntry.Symbols.empty()) {
Chris Lattnerb1c4f622010-03-22 23:15:57 +0000165 BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000166 NewEntry = std::move(OldEntry); // Set New's entry.
Chris Lattnerdb035a02010-03-16 00:29:39 +0000167 return;
Chris Lattner347a0eb2010-03-15 19:09:43 +0000168 }
Chris Lattnerdb035a02010-03-16 00:29:39 +0000169
Craig Topperc0196b12014-04-14 00:51:57 +0000170 BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
Chris Lattnerdb035a02010-03-16 00:29:39 +0000171
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000172 // Otherwise, we need to add the old symbols to the new block's set.
173 NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(),
174 OldEntry.Symbols.end());
Chris Lattner347a0eb2010-03-15 19:09:43 +0000175}
176
177
178void MMIAddrLabelMapCallbackPtr::deleted() {
179 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
180}
181
182void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
183 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
184}
185
186
Jim Laskey0bbdc552006-01-26 20:21:46 +0000187//===----------------------------------------------------------------------===//
Eric Christopherc2645712009-08-26 21:27:09 +0000188
Richard Smith8c3fbdc2016-08-23 22:08:27 +0000189MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI,
Evan Chengd60fa58b2011-07-18 20:57:22 +0000190 const MCRegisterInfo &MRI,
Richard Smith8c3fbdc2016-08-23 22:08:27 +0000191 const MCObjectFileInfo *MOFI)
192 : ImmutablePass(ID), Context(&MAI, &MRI, MOFI, nullptr, false) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000193 initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000194}
Jim Laskey0bbdc552006-01-26 20:21:46 +0000195
Chris Lattnere468f882010-03-13 20:55:24 +0000196MachineModuleInfo::MachineModuleInfo()
Richard Smith8c3fbdc2016-08-23 22:08:27 +0000197 : ImmutablePass(ID), Context(nullptr, nullptr, nullptr) {
Craig Topperee4dab52012-02-05 08:31:47 +0000198 llvm_unreachable("This MachineModuleInfo constructor should never be called, "
199 "MMI should always be explicitly constructed by "
200 "LLVMTargetMachine");
Chris Lattnere468f882010-03-13 20:55:24 +0000201}
202
Chris Lattnerf2471ec2009-09-15 22:44:26 +0000203MachineModuleInfo::~MachineModuleInfo() {
Pedro Artigas41b98842012-12-05 17:12:22 +0000204}
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000205
Pedro Artigas41b98842012-12-05 17:12:22 +0000206bool MachineModuleInfo::doInitialization(Module &M) {
Pedro Artigase84b13f2012-12-06 22:12:44 +0000207
Craig Topperc0196b12014-04-14 00:51:57 +0000208 ObjFileMMI = nullptr;
Pedro Artigas41b98842012-12-05 17:12:22 +0000209 CurCallSite = 0;
Yaron Keren5dbf3462015-07-03 07:56:24 +0000210 CallsEHReturn = false;
211 CallsUnwindInit = false;
Reid Kleckner0e288232015-08-27 23:27:47 +0000212 HasEHFunclets = false;
Peter Collingbourne7ef497b2014-12-30 20:05:19 +0000213 DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000214 PersonalityTypeCache = EHPersonality::Unknown;
Craig Topperc0196b12014-04-14 00:51:57 +0000215 AddrLabelSymbols = nullptr;
Richard Smith8c3fbdc2016-08-23 22:08:27 +0000216 TheModule = nullptr;
Pedro Artigas41b98842012-12-05 17:12:22 +0000217
218 return false;
219}
220
221bool MachineModuleInfo::doFinalization(Module &M) {
222
223 Personalities.clear();
224
Chris Lattner347a0eb2010-03-15 19:09:43 +0000225 delete AddrLabelSymbols;
Craig Topperc0196b12014-04-14 00:51:57 +0000226 AddrLabelSymbols = nullptr;
Pedro Artigas41b98842012-12-05 17:12:22 +0000227
Pedro Artigas7212ee42012-12-12 22:59:46 +0000228 Context.reset();
Pedro Artigase84b13f2012-12-06 22:12:44 +0000229
Pedro Artigas33832252013-01-04 18:04:42 +0000230 delete ObjFileMMI;
Craig Topperc0196b12014-04-14 00:51:57 +0000231 ObjFileMMI = nullptr;
Pedro Artigas33832252013-01-04 18:04:42 +0000232
Pedro Artigas41b98842012-12-05 17:12:22 +0000233 return false;
Jim Laskey0bbdc552006-01-26 20:21:46 +0000234}
235
Jim Laskey5a1df972007-01-26 21:38:26 +0000236/// EndFunction - Discard function meta information.
Jim Laskey2d7298c2006-04-07 16:34:46 +0000237///
Jim Laskey5a1df972007-01-26 21:38:26 +0000238void MachineModuleInfo::EndFunction() {
Jim Laskey2d7298c2006-04-07 16:34:46 +0000239 // Clean up frame info.
Rafael Espindola227144c2013-05-13 01:16:13 +0000240 FrameInstructions.clear();
Eric Christopherc2645712009-08-26 21:27:09 +0000241
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000242 // Clean up exception info.
243 LandingPads.clear();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000244 PersonalityTypeCache = EHPersonality::Unknown;
Jim Grosbach54c05302010-01-28 01:45:32 +0000245 CallSiteMap.clear();
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000246 TypeInfos.clear();
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000247 FilterIds.clear();
Duncan Sands4836e3a2007-07-05 15:15:01 +0000248 FilterEnds.clear();
Yaron Keren5dbf3462015-07-03 07:56:24 +0000249 CallsEHReturn = false;
250 CallsUnwindInit = false;
Reid Kleckner0e288232015-08-27 23:27:47 +0000251 HasEHFunclets = false;
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +0000252 VariableDbgInfos.clear();
Jim Laskey2d7298c2006-04-07 16:34:46 +0000253}
254
Chris Lattner347a0eb2010-03-15 19:09:43 +0000255//===- Address of Block Management ----------------------------------------===//
256
Chris Lattnerdb035a02010-03-16 00:29:39 +0000257/// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
258/// basic block when its address is taken. If other blocks were RAUW'd to
259/// this one, we may have to emit them as well, return the whole set.
Benjamin Kramer6fe4e792015-06-29 20:21:55 +0000260ArrayRef<MCSymbol *>
261MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
Chris Lattnerdb035a02010-03-16 00:29:39 +0000262 // Lazily create AddrLabelSymbols.
Craig Topperc0196b12014-04-14 00:51:57 +0000263 if (!AddrLabelSymbols)
Chris Lattnerdb035a02010-03-16 00:29:39 +0000264 AddrLabelSymbols = new MMIAddrLabelMap(Context);
265 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
266}
267
268
Chris Lattner561334a2010-03-15 20:39:00 +0000269/// takeDeletedSymbolsForFunction - If the specified function has had any
270/// references to address-taken blocks generated, but the block got deleted,
271/// return the symbol now so we can emit it. This prevents emitting a
272/// reference to a symbol that has no definition.
273void MachineModuleInfo::
274takeDeletedSymbolsForFunction(const Function *F,
275 std::vector<MCSymbol*> &Result) {
276 // If no blocks have had their addresses taken, we're done.
Craig Topperc0196b12014-04-14 00:51:57 +0000277 if (!AddrLabelSymbols) return;
Chris Lattner561334a2010-03-15 20:39:00 +0000278 return AddrLabelSymbols->
279 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
280}
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000281
Chris Lattner347a0eb2010-03-15 19:09:43 +0000282//===- EH -----------------------------------------------------------------===//
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000283
284/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
285/// specified MachineBasicBlock.
Bill Wendling2e5068942008-07-03 22:53:42 +0000286LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
287 (MachineBasicBlock *LandingPad) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000288 unsigned N = LandingPads.size();
289 for (unsigned i = 0; i < N; ++i) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000290 LandingPadInfo &LP = LandingPads[i];
291 if (LP.LandingPadBlock == LandingPad)
292 return LP;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000293 }
Eric Christopherc2645712009-08-26 21:27:09 +0000294
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000295 LandingPads.push_back(LandingPadInfo(LandingPad));
296 return LandingPads[N];
297}
298
299/// addInvoke - Provide the begin and end labels of an invoke style call and
300/// associate it with a try landing pad block.
301void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattner34adc8d2010-03-14 01:41:15 +0000302 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000303 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000304 LP.BeginLabels.push_back(BeginLabel);
305 LP.EndLabels.push_back(EndLabel);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000306}
307
308/// addLandingPad - Provide the label of a try LandingPad block.
309///
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000310MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000311 MCSymbol *LandingPadLabel = Context.createTempSymbol();
Jim Laskey6458e6a2007-03-01 20:25:32 +0000312 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopherc2645712009-08-26 21:27:09 +0000313 LP.LandingPadLabel = LandingPadLabel;
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000314 return LandingPadLabel;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000315}
316
Keno Fischeraff703a2015-07-14 19:22:51 +0000317void MachineModuleInfo::addPersonality(const Function *Personality) {
Bill Wendling2e5068942008-07-03 22:53:42 +0000318 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000319 if (Personalities[i] == Personality)
320 return;
Reid Klecknere00faf82015-08-31 20:02:16 +0000321 Personalities.push_back(Personality);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000322}
323
324/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
325///
Bill Wendlingf8d95bc2011-07-28 21:25:33 +0000326void MachineModuleInfo::
327addCatchTypeInfo(MachineBasicBlock *LandingPad,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000328 ArrayRef<const GlobalValue *> TyInfo) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000329 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000330 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey6458e6a2007-03-01 20:25:32 +0000331 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000332}
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000333
334/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey6458e6a2007-03-01 20:25:32 +0000335///
Bill Wendlingf8d95bc2011-07-28 21:25:33 +0000336void MachineModuleInfo::
337addFilterTypeInfo(MachineBasicBlock *LandingPad,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000338 ArrayRef<const GlobalValue *> TyInfo) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000339 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendlingb46e5162008-07-07 21:41:57 +0000340 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling2e5068942008-07-03 22:53:42 +0000341 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000342 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
343 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey6458e6a2007-03-01 20:25:32 +0000344}
345
Duncan Sandsef5a6542007-08-27 15:47:50 +0000346/// addCleanup - Add a cleanup action for a landing pad.
347///
348void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
349 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
350 LP.TypeIds.push_back(0);
351}
352
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000353void MachineModuleInfo::addSEHCatchHandler(MachineBasicBlock *LandingPad,
354 const Function *Filter,
355 const BlockAddress *RecoverBA) {
356 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
357 SEHHandler Handler;
358 Handler.FilterOrFinally = Filter;
359 Handler.RecoverBA = RecoverBA;
360 LP.SEHHandlers.push_back(Handler);
361}
362
363void MachineModuleInfo::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
364 const Function *Cleanup) {
365 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
366 SEHHandler Handler;
367 Handler.FilterOrFinally = Cleanup;
368 Handler.RecoverBA = nullptr;
369 LP.SEHHandlers.push_back(Handler);
370}
371
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000372/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
373/// pads.
Bill Wendling929f3c02010-04-16 08:46:10 +0000374void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000375 for (unsigned i = 0; i != LandingPads.size(); ) {
376 LandingPadInfo &LandingPad = LandingPads[i];
Bill Wendling929f3c02010-04-16 08:46:10 +0000377 if (LandingPad.LandingPadLabel &&
378 !LandingPad.LandingPadLabel->isDefined() &&
379 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
Craig Topperc0196b12014-04-14 00:51:57 +0000380 LandingPad.LandingPadLabel = nullptr;
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000381
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000382 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands030bce72007-12-19 07:36:31 +0000383 // "nounwind" case.
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000384 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000385 LandingPads.erase(LandingPads.begin() + i);
386 continue;
387 }
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000388
Chris Lattner34adc8d2010-03-14 01:41:15 +0000389 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
390 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
391 MCSymbol *EndLabel = LandingPad.EndLabels[j];
Bill Wendling929f3c02010-04-16 08:46:10 +0000392 if ((BeginLabel->isDefined() ||
393 (LPMap && (*LPMap)[BeginLabel] != 0)) &&
394 (EndLabel->isDefined() ||
395 (LPMap && (*LPMap)[EndLabel] != 0))) continue;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000396
Chris Lattner34adc8d2010-03-14 01:41:15 +0000397 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
398 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
Richard Trieu7a083812016-02-18 22:09:30 +0000399 --j;
400 --e;
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000401 }
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000402
403 // Remove landing pads with no try-ranges.
Dan Gohman70de4cb2008-01-29 13:02:09 +0000404 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000405 LandingPads.erase(LandingPads.begin() + i);
406 continue;
407 }
408
409 // If there is no landing pad, ensure that the list of typeids is empty.
410 // If the only typeid is a cleanup, this is the same as having no typeids.
411 if (!LandingPad.LandingPadBlock ||
412 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
413 LandingPad.TypeIds.clear();
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000414 ++i;
415 }
416}
417
Bill Wendlingc2d55b62011-10-05 22:20:38 +0000418/// setCallSiteLandingPad - Map the landing pad's EH symbol to the call site
419/// indexes.
420void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
421 ArrayRef<unsigned> Sites) {
Benjamin Kramer0e3791e2012-02-14 10:29:27 +0000422 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
Bill Wendlingc2d55b62011-10-05 22:20:38 +0000423}
424
Eric Christopherc2645712009-08-26 21:27:09 +0000425/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000426/// function wide.
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000427unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
Bill Wendling2e5068942008-07-03 22:53:42 +0000428 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
429 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000430
431 TypeInfos.push_back(TI);
432 return TypeInfos.size();
433}
434
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000435/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
436/// function wide.
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000437int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands4836e3a2007-07-05 15:15:01 +0000438 // If the new filter coincides with the tail of an existing filter, then
439 // re-use the existing filter. Folding filters more than this requires
440 // re-ordering filters and/or their elements - probably not worth it.
441 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
442 E = FilterEnds.end(); I != E; ++I) {
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000443 unsigned i = *I, j = TyIds.size();
Duncan Sands4836e3a2007-07-05 15:15:01 +0000444
445 while (i && j)
446 if (FilterIds[--i] != TyIds[--j])
447 goto try_next;
448
449 if (!j)
450 // The new filter coincides with range [i, end) of the existing filter.
451 return -(1 + i);
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000452
Duncan Sands4836e3a2007-07-05 15:15:01 +0000453try_next:;
454 }
455
456 // Add the new filter.
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000457 int FilterID = -(1 + FilterIds.size());
458 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
Benjamin Kramer0e3791e2012-02-14 10:29:27 +0000459 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000460 FilterEnds.push_back(FilterIds.size());
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000461 FilterIds.push_back(0); // terminator
462 return FilterID;
463}