blob: eec984f53b90a49962caaf64a43bbd60705d0373 [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};
100}
101
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
Jim Laskey0bbdc552006-01-26 20:21:46 +0000321/// AnalyzeModule - Scan the module for global debug information.
322///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000323void MachineModuleInfo::AnalyzeModule(const Module &M) {
Chris Lattner58f9bb22009-07-20 06:14:25 +0000324 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
325 // UsedFunctions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000326 const GlobalVariable *GV = M.getGlobalVariable("llvm.used");
Dale Johannesened203662008-01-16 19:59:28 +0000327 if (!GV || !GV->hasInitializer()) return;
328
329 // Should be an array of 'i8*'.
Rafael Espindola74f2e462013-04-22 14:58:02 +0000330 const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
Dale Johannesened203662008-01-16 19:59:28 +0000331
Chris Lattner029380f2009-07-20 06:05:50 +0000332 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000333 if (const Function *F =
Chris Lattner029380f2009-07-20 06:05:50 +0000334 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
335 UsedFunctions.insert(F);
Jim Laskey0bbdc552006-01-26 20:21:46 +0000336}
337
Chris Lattner347a0eb2010-03-15 19:09:43 +0000338//===- Address of Block Management ----------------------------------------===//
339
340
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000341/// getAddrLabelSymbol - Return the symbol to be used for the specified basic
342/// block when its address is taken. This cannot be its normal LBB label
343/// because the block may be accessed outside its containing function.
344MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
Chris Lattner347a0eb2010-03-15 19:09:43 +0000345 // Lazily create AddrLabelSymbols.
Craig Topperc0196b12014-04-14 00:51:57 +0000346 if (!AddrLabelSymbols)
Chris Lattner347a0eb2010-03-15 19:09:43 +0000347 AddrLabelSymbols = new MMIAddrLabelMap(Context);
348 return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000349}
350
Chris Lattnerdb035a02010-03-16 00:29:39 +0000351/// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
352/// basic block when its address is taken. If other blocks were RAUW'd to
353/// this one, we may have to emit them as well, return the whole set.
354std::vector<MCSymbol*> MachineModuleInfo::
355getAddrLabelSymbolToEmit(const BasicBlock *BB) {
356 // Lazily create AddrLabelSymbols.
Craig Topperc0196b12014-04-14 00:51:57 +0000357 if (!AddrLabelSymbols)
Chris Lattnerdb035a02010-03-16 00:29:39 +0000358 AddrLabelSymbols = new MMIAddrLabelMap(Context);
359 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
360}
361
362
Chris Lattner561334a2010-03-15 20:39:00 +0000363/// takeDeletedSymbolsForFunction - If the specified function has had any
364/// references to address-taken blocks generated, but the block got deleted,
365/// return the symbol now so we can emit it. This prevents emitting a
366/// reference to a symbol that has no definition.
367void MachineModuleInfo::
368takeDeletedSymbolsForFunction(const Function *F,
369 std::vector<MCSymbol*> &Result) {
370 // If no blocks have had their addresses taken, we're done.
Craig Topperc0196b12014-04-14 00:51:57 +0000371 if (!AddrLabelSymbols) return;
Chris Lattner561334a2010-03-15 20:39:00 +0000372 return AddrLabelSymbols->
373 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
374}
Chris Lattner9efbbcb2010-03-14 17:53:23 +0000375
Chris Lattner347a0eb2010-03-15 19:09:43 +0000376//===- EH -----------------------------------------------------------------===//
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000377
378/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
379/// specified MachineBasicBlock.
Bill Wendling2e5068942008-07-03 22:53:42 +0000380LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
381 (MachineBasicBlock *LandingPad) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000382 unsigned N = LandingPads.size();
383 for (unsigned i = 0; i < N; ++i) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000384 LandingPadInfo &LP = LandingPads[i];
385 if (LP.LandingPadBlock == LandingPad)
386 return LP;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000387 }
Eric Christopherc2645712009-08-26 21:27:09 +0000388
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000389 LandingPads.push_back(LandingPadInfo(LandingPad));
390 return LandingPads[N];
391}
392
393/// addInvoke - Provide the begin and end labels of an invoke style call and
394/// associate it with a try landing pad block.
395void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattner34adc8d2010-03-14 01:41:15 +0000396 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000397 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000398 LP.BeginLabels.push_back(BeginLabel);
399 LP.EndLabels.push_back(EndLabel);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000400}
401
402/// addLandingPad - Provide the label of a try LandingPad block.
403///
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000404MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000405 MCSymbol *LandingPadLabel = Context.createTempSymbol();
Jim Laskey6458e6a2007-03-01 20:25:32 +0000406 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopherc2645712009-08-26 21:27:09 +0000407 LP.LandingPadLabel = LandingPadLabel;
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000408 return LandingPadLabel;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000409}
410
411/// addPersonality - Provide the personality function for the exception
412/// information.
413void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000414 const Function *Personality) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000415 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000416 LP.Personality = Personality;
Anton Korobeynikov13da1782007-05-12 22:36:25 +0000417
Bill Wendling2e5068942008-07-03 22:53:42 +0000418 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000419 if (Personalities[i] == Personality)
420 return;
Eric Christopherc2645712009-08-26 21:27:09 +0000421
Eric Christophera258c622009-08-26 21:30:49 +0000422 // If this is the first personality we're adding go
423 // ahead and add it at the beginning.
Craig Topperc0196b12014-04-14 00:51:57 +0000424 if (!Personalities[0])
Eric Christophera258c622009-08-26 21:30:49 +0000425 Personalities[0] = Personality;
426 else
427 Personalities.push_back(Personality);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000428}
429
David Majnemercde33032015-03-30 22:58:10 +0000430void MachineModuleInfo::addWinEHState(MachineBasicBlock *LandingPad,
431 int State) {
432 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
433 LP.WinEHState = State;
434}
435
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000436/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
437///
Bill Wendlingf8d95bc2011-07-28 21:25:33 +0000438void MachineModuleInfo::
439addCatchTypeInfo(MachineBasicBlock *LandingPad,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000440 ArrayRef<const GlobalValue *> TyInfo) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000441 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000442 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey6458e6a2007-03-01 20:25:32 +0000443 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000444}
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000445
446/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey6458e6a2007-03-01 20:25:32 +0000447///
Bill Wendlingf8d95bc2011-07-28 21:25:33 +0000448void MachineModuleInfo::
449addFilterTypeInfo(MachineBasicBlock *LandingPad,
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000450 ArrayRef<const GlobalValue *> TyInfo) {
Jim Laskey6458e6a2007-03-01 20:25:32 +0000451 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendlingb46e5162008-07-07 21:41:57 +0000452 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling2e5068942008-07-03 22:53:42 +0000453 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000454 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
455 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey6458e6a2007-03-01 20:25:32 +0000456}
457
Duncan Sandsef5a6542007-08-27 15:47:50 +0000458/// addCleanup - Add a cleanup action for a landing pad.
459///
460void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
461 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
462 LP.TypeIds.push_back(0);
463}
464
Reid Klecknerd2a1a512015-04-21 18:23:57 +0000465void MachineModuleInfo::addSEHCatchHandler(MachineBasicBlock *LandingPad,
466 const Function *Filter,
467 const BlockAddress *RecoverBA) {
468 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
469 SEHHandler Handler;
470 Handler.FilterOrFinally = Filter;
471 Handler.RecoverBA = RecoverBA;
472 LP.SEHHandlers.push_back(Handler);
473}
474
475void MachineModuleInfo::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
476 const Function *Cleanup) {
477 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
478 SEHHandler Handler;
479 Handler.FilterOrFinally = Cleanup;
480 Handler.RecoverBA = nullptr;
481 LP.SEHHandlers.push_back(Handler);
482}
483
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000484/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
485/// pads.
Bill Wendling929f3c02010-04-16 08:46:10 +0000486void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000487 for (unsigned i = 0; i != LandingPads.size(); ) {
488 LandingPadInfo &LandingPad = LandingPads[i];
Bill Wendling929f3c02010-04-16 08:46:10 +0000489 if (LandingPad.LandingPadLabel &&
490 !LandingPad.LandingPadLabel->isDefined() &&
491 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
Craig Topperc0196b12014-04-14 00:51:57 +0000492 LandingPad.LandingPadLabel = nullptr;
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000493
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000494 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands030bce72007-12-19 07:36:31 +0000495 // "nounwind" case.
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000496 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000497 LandingPads.erase(LandingPads.begin() + i);
498 continue;
499 }
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000500
Chris Lattner34adc8d2010-03-14 01:41:15 +0000501 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
502 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
503 MCSymbol *EndLabel = LandingPad.EndLabels[j];
Bill Wendling929f3c02010-04-16 08:46:10 +0000504 if ((BeginLabel->isDefined() ||
505 (LPMap && (*LPMap)[BeginLabel] != 0)) &&
506 (EndLabel->isDefined() ||
507 (LPMap && (*LPMap)[EndLabel] != 0))) continue;
Michael J. Spencerd3ea25e2010-10-16 08:25:21 +0000508
Chris Lattner34adc8d2010-03-14 01:41:15 +0000509 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
510 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
511 --j, --e;
Anton Korobeynikov96142de2007-05-10 22:34:59 +0000512 }
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000513
514 // Remove landing pads with no try-ranges.
Dan Gohman70de4cb2008-01-29 13:02:09 +0000515 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands3c1b7fc2007-09-05 11:27:52 +0000516 LandingPads.erase(LandingPads.begin() + i);
517 continue;
518 }
519
520 // If there is no landing pad, ensure that the list of typeids is empty.
521 // If the only typeid is a cleanup, this is the same as having no typeids.
522 if (!LandingPad.LandingPadBlock ||
523 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
524 LandingPad.TypeIds.clear();
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000525 ++i;
526 }
527}
528
Bill Wendlingc2d55b62011-10-05 22:20:38 +0000529/// setCallSiteLandingPad - Map the landing pad's EH symbol to the call site
530/// indexes.
531void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
532 ArrayRef<unsigned> Sites) {
Benjamin Kramer0e3791e2012-02-14 10:29:27 +0000533 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
Bill Wendlingc2d55b62011-10-05 22:20:38 +0000534}
535
Eric Christopherc2645712009-08-26 21:27:09 +0000536/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000537/// function wide.
Reid Kleckner283bc2e2014-11-14 00:35:50 +0000538unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
Bill Wendling2e5068942008-07-03 22:53:42 +0000539 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
540 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000541
542 TypeInfos.push_back(TI);
543 return TypeInfos.size();
544}
545
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000546/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
547/// function wide.
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000548int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands4836e3a2007-07-05 15:15:01 +0000549 // If the new filter coincides with the tail of an existing filter, then
550 // re-use the existing filter. Folding filters more than this requires
551 // re-ordering filters and/or their elements - probably not worth it.
552 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
553 E = FilterEnds.end(); I != E; ++I) {
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000554 unsigned i = *I, j = TyIds.size();
Duncan Sands4836e3a2007-07-05 15:15:01 +0000555
556 while (i && j)
557 if (FilterIds[--i] != TyIds[--j])
558 goto try_next;
559
560 if (!j)
561 // The new filter coincides with range [i, end) of the existing filter.
562 return -(1 + i);
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000563
Duncan Sands4836e3a2007-07-05 15:15:01 +0000564try_next:;
565 }
566
567 // Add the new filter.
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000568 int FilterID = -(1 + FilterIds.size());
569 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
Benjamin Kramer0e3791e2012-02-14 10:29:27 +0000570 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
Bill Wendlingfcbb9522008-06-27 01:27:56 +0000571 FilterEnds.push_back(FilterIds.size());
Duncan Sandsc063f5f2007-06-02 16:53:42 +0000572 FilterIds.push_back(0); // terminator
573 return FilterID;
574}
575
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000576/// getPersonality - Return the personality function for the current function.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000577const Function *MachineModuleInfo::getPersonality() const {
Reid Kleckner5cc15692015-01-23 18:49:01 +0000578 for (const LandingPadInfo &LPI : LandingPads)
579 if (LPI.Personality)
580 return LPI.Personality;
581 return nullptr;
582}
583
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000584EHPersonality MachineModuleInfo::getPersonalityType() {
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000585 if (PersonalityTypeCache == EHPersonality::Unknown) {
586 if (const Function *F = getPersonality())
587 PersonalityTypeCache = classifyEHPersonality(F);
588 }
Reid Kleckner5cc15692015-01-23 18:49:01 +0000589 return PersonalityTypeCache;
Jim Laskey88dd2fd2007-02-21 22:38:31 +0000590}
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000591
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000592/// getPersonalityIndex - Return unique index for current personality
Eric Christopher92335652009-08-26 21:44:57 +0000593/// function. NULL/first personality function should always get zero index.
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000594unsigned MachineModuleInfo::getPersonalityIndex() const {
Craig Topperc0196b12014-04-14 00:51:57 +0000595 const Function* Personality = nullptr;
Eric Christopherc2645712009-08-26 21:27:09 +0000596
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000597 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling05f73802012-02-13 23:45:26 +0000598 for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
Anton Korobeynikov3b327822007-05-23 11:08:31 +0000599 if (LandingPads[i].Personality) {
600 Personality = LandingPads[i].Personality;
601 break;
602 }
Eric Christopherc2645712009-08-26 21:27:09 +0000603
Bill Wendling05f73802012-02-13 23:45:26 +0000604 for (unsigned i = 0, e = Personalities.size(); i < e; ++i) {
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000605 if (Personalities[i] == Personality)
606 return i;
Bill Wendling2e5068942008-07-03 22:53:42 +0000607 }
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000608
Eric Christopher92335652009-08-26 21:44:57 +0000609 // This will happen if the current personality function is
610 // in the zero index.
Anton Korobeynikovbbaf5542007-05-13 15:42:26 +0000611 return 0;
612}
David Majnemercde33032015-03-30 22:58:10 +0000613
614const Function *MachineModuleInfo::getWinEHParent(const Function *F) const {
615 StringRef WinEHParentName =
616 F->getFnAttribute("wineh-parent").getValueAsString();
617 if (WinEHParentName.empty() || WinEHParentName == F->getName())
618 return F;
619 return F->getParent()->getFunction(WinEHParentName);
620}
621
622WinEHFuncInfo &MachineModuleInfo::getWinEHFuncInfo(const Function *F) {
623 auto &Ptr = FuncInfoMap[getWinEHParent(F)];
624 if (!Ptr)
625 Ptr.reset(new WinEHFuncInfo);
626 return *Ptr;
627}