blob: b18e34c0d8d56dda4e8a907347c1a3ab7557a31d [file] [log] [blame]
Jim Laskey6da18642007-01-26 21:38:26 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
Jim Laskey6af56812006-01-04 13:36:38 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Laskey6af56812006-01-04 13:36:38 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey6af56812006-01-04 13:36:38 +00009
Jim Laskey6da18642007-01-26 21:38:26 +000010#include "llvm/CodeGen/MachineModuleInfo.h"
Jim Laskey6af56812006-01-04 13:36:38 +000011
Jim Laskeyb3e789a2006-01-26 20:21:46 +000012#include "llvm/Constants.h"
Chris Lattner16112732010-03-14 01:41:15 +000013#include "llvm/DerivedTypes.h"
14#include "llvm/GlobalVariable.h"
15#include "llvm/Intrinsics.h"
16#include "llvm/Instructions.h"
17#include "llvm/Module.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000018#include "llvm/Analysis/ValueTracking.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng8b56a902008-09-22 22:21:38 +000021#include "llvm/CodeGen/Passes.h"
Jim Laskey9d4209f2006-11-07 19:33:46 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Jim Laskeyc1c47c32007-01-29 23:40:33 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattner16112732010-03-14 01:41:15 +000025#include "llvm/MC/MCSymbol.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000026#include "llvm/Support/Dwarf.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Jim Laskey6af56812006-01-04 13:36:38 +000028using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000029using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000030
31// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman844731a2008-05-13 00:00:25 +000032static RegisterPass<MachineModuleInfo>
Chris Lattner11d53c12010-03-13 20:55:24 +000033X("machinemoduleinfo", "Machine Module Information");
Devang Patel19974732007-05-03 01:11:54 +000034char MachineModuleInfo::ID = 0;
Jim Laskey063e7652006-01-17 17:31:53 +000035
Chris Lattnera70e2e32009-09-15 22:44:26 +000036// Out of line virtual method.
37MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
38
Chris Lattner0220ba72010-03-15 19:09:43 +000039namespace llvm {
40class MMIAddrLabelMapCallbackPtr : CallbackVH {
41 MMIAddrLabelMap *Map;
42public:
43 MMIAddrLabelMapCallbackPtr() : Map(0) {}
44 MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(0) {}
45
46 void setMap(MMIAddrLabelMap *map) { Map = map; }
47
48 virtual void deleted();
49 virtual void allUsesReplacedWith(Value *V2);
50};
51
52class MMIAddrLabelMap {
53 MCContext &Context;
54 struct AddrLabelSymEntry {
Chris Lattner9cc0da92010-03-15 20:39:00 +000055 MCSymbol *Sym; // The symbol for the label.
56 Function *Fn; // The containing function of the BasicBlock.
57 unsigned Index; // The index in BBCallbacks for the BasicBlock.
Chris Lattner0220ba72010-03-15 19:09:43 +000058 };
59
60 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
61
Chris Lattner9cc0da92010-03-15 20:39:00 +000062 /// BBCallbacks - Callbacks for the BasicBlock's that we have entries for. We
63 /// use this so we get notified if a block is deleted or RAUWd.
Chris Lattner0220ba72010-03-15 19:09:43 +000064 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
Chris Lattner9cc0da92010-03-15 20:39:00 +000065
66 /// DeletedAddrLabelsNeedingEmission - This is a per-function list of symbols
67 /// whose corresponding BasicBlock got deleted. These symbols need to be
68 /// emitted at some point in the file, so AsmPrinter emits them after the
69 /// function body.
70 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
71 DeletedAddrLabelsNeedingEmission;
Chris Lattner0220ba72010-03-15 19:09:43 +000072public:
73
74 MMIAddrLabelMap(MCContext &context) : Context(context) {}
Chris Lattner9cc0da92010-03-15 20:39:00 +000075 ~MMIAddrLabelMap() {
76 assert(DeletedAddrLabelsNeedingEmission.empty() &&
77 "Some labels for deleted blocks never got emitted");
78 }
Chris Lattner0220ba72010-03-15 19:09:43 +000079
80 MCSymbol *getAddrLabelSymbol(BasicBlock *BB);
Chris Lattner9cc0da92010-03-15 20:39:00 +000081 void takeDeletedSymbolsForFunction(Function *F,
82 std::vector<MCSymbol*> &Result);
83
Chris Lattner0220ba72010-03-15 19:09:43 +000084 void UpdateForDeletedBlock(BasicBlock *BB);
85 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
86};
87}
88
89MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
90 assert(BB->hasAddressTaken() &&
91 "Shouldn't get label for block without address taken");
92 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
93
94 // If we already had an entry for this block, just return it.
Chris Lattner9cc0da92010-03-15 20:39:00 +000095 if (Entry.Sym) {
96 assert(BB->getParent() == Entry.Fn && "Parent changed");
97 return Entry.Sym;
98 }
Chris Lattner0220ba72010-03-15 19:09:43 +000099
100 // Otherwise, this is a new entry, create a new symbol for it and add an
101 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
102 BBCallbacks.push_back(BB);
103 BBCallbacks.back().setMap(this);
104 Entry.Index = BBCallbacks.size()-1;
Chris Lattner9cc0da92010-03-15 20:39:00 +0000105 Entry.Fn = BB->getParent();
Chris Lattner0220ba72010-03-15 19:09:43 +0000106 return Entry.Sym = Context.CreateTempSymbol();
107}
108
Chris Lattner9cc0da92010-03-15 20:39:00 +0000109/// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
110/// them.
111void MMIAddrLabelMap::
112takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
113 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
114 DeletedAddrLabelsNeedingEmission.find(F);
115
116 // If there are no entries for the function, just return.
117 if (I == DeletedAddrLabelsNeedingEmission.end()) return;
118
119 // Otherwise, take the list.
120 std::swap(Result, I->second);
121 DeletedAddrLabelsNeedingEmission.erase(I);
122}
123
124
Chris Lattner0220ba72010-03-15 19:09:43 +0000125void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
126 // If the block got deleted, there is no need for the symbol. If the symbol
127 // was already emitted, we can just forget about it, otherwise we need to
128 // queue it up for later emission when the function is output.
129 AddrLabelSymEntry Entry = AddrLabelSymbols[BB];
130 AddrLabelSymbols.erase(BB);
131 assert(Entry.Sym && "Didn't have a symbol, why a callback?");
132 BBCallbacks[Entry.Index] = 0; // Clear the callback.
133
134 if (Entry.Sym->isDefined())
135 return;
136
137 // If the block is not yet defined, we need to emit it at the end of the
Chris Lattner9cc0da92010-03-15 20:39:00 +0000138 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list for
139 // the containing Function. Since the block is being deleted, its parent may
140 // already be removed, we have to get the function from 'Entry'.
141 assert((BB->getParent() == 0 || BB->getParent() == Entry.Fn) &&
142 "Block/parent mismatch");
143
144 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Entry.Sym);
Chris Lattner0220ba72010-03-15 19:09:43 +0000145}
146
147void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
148 // Get the entry for the RAUW'd block and remove it from our map.
149 AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old];
150 AddrLabelSymbols.erase(Old);
151 assert(OldEntry.Sym && "Didn't have a symbol, why a callback?");
152
153 // If New is not address taken, just move our symbol over to it.
154 if (!AddrLabelSymbols.count(New)) {
155 BBCallbacks[OldEntry.Index] = New; // Update the callback.
156 AddrLabelSymbols[New] = OldEntry; // Set New's entry.
157 } else {
158 assert(0 && "Case not handled yet!");
159 abort();
160 }
161}
162
163
164void MMIAddrLabelMapCallbackPtr::deleted() {
165 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
166}
167
168void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
169 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
170}
171
172
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000173//===----------------------------------------------------------------------===//
Eric Christophercf296972009-08-26 21:27:09 +0000174
Chris Lattner11d53c12010-03-13 20:55:24 +0000175MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
176: ImmutablePass(&ID), Context(MAI),
Chris Lattner63d78362010-03-14 08:36:50 +0000177 ObjFileMMI(0),
Chris Lattner18589de2010-03-14 02:24:55 +0000178 CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){
Eric Christopherd44fff72009-08-26 21:30:49 +0000179 // Always emit some info, by default "no personality" info.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000180 Personalities.push_back(NULL);
Chris Lattner0220ba72010-03-15 19:09:43 +0000181 AddrLabelSymbols = 0;
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000182}
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000183
Chris Lattner11d53c12010-03-13 20:55:24 +0000184MachineModuleInfo::MachineModuleInfo()
185: ImmutablePass(&ID), Context(*(MCAsmInfo*)0) {
186 assert(0 && "This MachineModuleInfo constructor should never be called, MMI "
187 "should always be explicitly constructed by LLVMTargetMachine");
188 abort();
189}
190
Chris Lattnera70e2e32009-09-15 22:44:26 +0000191MachineModuleInfo::~MachineModuleInfo() {
Chris Lattnerf1854552009-09-16 05:26:00 +0000192 delete ObjFileMMI;
Chris Lattner0220ba72010-03-15 19:09:43 +0000193
194 // FIXME: Why isn't doFinalization being called??
195 //assert(AddrLabelSymbols == 0 && "doFinalization not called");
196 delete AddrLabelSymbols;
197 AddrLabelSymbols = 0;
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000198}
199
Jim Laskey6da18642007-01-26 21:38:26 +0000200/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000201///
Jim Laskey6da18642007-01-26 21:38:26 +0000202bool MachineModuleInfo::doInitialization() {
Chris Lattner0220ba72010-03-15 19:09:43 +0000203 assert(AddrLabelSymbols == 0 && "Improperly initialized");
Jim Laskeyb2efb852006-01-04 22:28:25 +0000204 return false;
Jim Laskey6af56812006-01-04 13:36:38 +0000205}
206
Jim Laskey6da18642007-01-26 21:38:26 +0000207/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +0000208///
Jim Laskey6da18642007-01-26 21:38:26 +0000209bool MachineModuleInfo::doFinalization() {
Chris Lattner0220ba72010-03-15 19:09:43 +0000210 delete AddrLabelSymbols;
211 AddrLabelSymbols = 0;
Jim Laskeyb2efb852006-01-04 22:28:25 +0000212 return false;
213}
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000214
Jim Laskey6da18642007-01-26 21:38:26 +0000215/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +0000216///
Jim Laskey6da18642007-01-26 21:38:26 +0000217void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +0000218 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +0000219 FrameMoves.clear();
Eric Christophercf296972009-08-26 21:27:09 +0000220
Jim Laskey59667fe2007-02-21 22:38:31 +0000221 // Clean up exception info.
222 LandingPads.clear();
Jim Grosbachca752c92010-01-28 01:45:32 +0000223 CallSiteMap.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +0000224 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +0000225 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +0000226 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000227 CallsEHReturn = 0;
228 CallsUnwindInit = 0;
Devang Patel85d29e22009-10-08 20:41:17 +0000229 VariableDbgInfo.clear();
Jim Laskey41886992006-04-07 16:34:46 +0000230}
231
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000232/// AnalyzeModule - Scan the module for global debug information.
233///
Jim Laskey6da18642007-01-26 21:38:26 +0000234void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner401e10c2009-07-20 06:14:25 +0000235 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
236 // UsedFunctions.
Dale Johannesen48ae02f2008-01-16 19:59:28 +0000237 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
238 if (!GV || !GV->hasInitializer()) return;
239
240 // Should be an array of 'i8*'.
241 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
242 if (InitList == 0) return;
243
Chris Lattner1d5c4932009-07-20 06:05:50 +0000244 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
245 if (Function *F =
246 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
247 UsedFunctions.insert(F);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000248}
249
Chris Lattner0220ba72010-03-15 19:09:43 +0000250//===- Address of Block Management ----------------------------------------===//
251
252
Chris Lattner3b9d6212010-03-14 17:53:23 +0000253/// getAddrLabelSymbol - Return the symbol to be used for the specified basic
254/// block when its address is taken. This cannot be its normal LBB label
255/// because the block may be accessed outside its containing function.
256MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
Chris Lattner0220ba72010-03-15 19:09:43 +0000257 // Lazily create AddrLabelSymbols.
258 if (AddrLabelSymbols == 0)
259 AddrLabelSymbols = new MMIAddrLabelMap(Context);
260 return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
Chris Lattner3b9d6212010-03-14 17:53:23 +0000261}
262
Chris Lattner9cc0da92010-03-15 20:39:00 +0000263/// takeDeletedSymbolsForFunction - If the specified function has had any
264/// references to address-taken blocks generated, but the block got deleted,
265/// return the symbol now so we can emit it. This prevents emitting a
266/// reference to a symbol that has no definition.
267void MachineModuleInfo::
268takeDeletedSymbolsForFunction(const Function *F,
269 std::vector<MCSymbol*> &Result) {
270 // If no blocks have had their addresses taken, we're done.
271 if (AddrLabelSymbols == 0) return;
272 return AddrLabelSymbols->
273 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
274}
Chris Lattner3b9d6212010-03-14 17:53:23 +0000275
Chris Lattner0220ba72010-03-15 19:09:43 +0000276//===- EH -----------------------------------------------------------------===//
Jim Laskey59667fe2007-02-21 22:38:31 +0000277
278/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
279/// specified MachineBasicBlock.
Bill Wendling10fff602008-07-03 22:53:42 +0000280LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
281 (MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +0000282 unsigned N = LandingPads.size();
283 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +0000284 LandingPadInfo &LP = LandingPads[i];
285 if (LP.LandingPadBlock == LandingPad)
286 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +0000287 }
Eric Christophercf296972009-08-26 21:27:09 +0000288
Jim Laskey59667fe2007-02-21 22:38:31 +0000289 LandingPads.push_back(LandingPadInfo(LandingPad));
290 return LandingPads[N];
291}
292
293/// addInvoke - Provide the begin and end labels of an invoke style call and
294/// associate it with a try landing pad block.
295void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattner16112732010-03-14 01:41:15 +0000296 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +0000297 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000298 LP.BeginLabels.push_back(BeginLabel);
299 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +0000300}
301
302/// addLandingPad - Provide the label of a try LandingPad block.
303///
Chris Lattner7561d482010-03-14 02:33:54 +0000304MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Chris Lattner63d78362010-03-14 08:36:50 +0000305 MCSymbol *LandingPadLabel = Context.CreateTempSymbol();
Jim Laskey59e84342007-03-01 20:25:32 +0000306 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christophercf296972009-08-26 21:27:09 +0000307 LP.LandingPadLabel = LandingPadLabel;
Chris Lattner7561d482010-03-14 02:33:54 +0000308 return LandingPadLabel;
Jim Laskey59667fe2007-02-21 22:38:31 +0000309}
310
311/// addPersonality - Provide the personality function for the exception
312/// information.
313void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000314 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +0000315 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000316 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000317
Bill Wendling10fff602008-07-03 22:53:42 +0000318 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000319 if (Personalities[i] == Personality)
320 return;
Eric Christophercf296972009-08-26 21:27:09 +0000321
Eric Christopherd44fff72009-08-26 21:30:49 +0000322 // If this is the first personality we're adding go
323 // ahead and add it at the beginning.
324 if (Personalities[0] == NULL)
325 Personalities[0] = Personality;
326 else
327 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +0000328}
329
330/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
331///
332void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
333 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +0000334 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +0000335 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +0000336 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +0000337}
Duncan Sands73ef58a2007-06-02 16:53:42 +0000338
339/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +0000340///
Duncan Sands73ef58a2007-06-02 16:53:42 +0000341void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
342 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +0000343 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling49255672008-07-07 21:41:57 +0000344 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling10fff602008-07-03 22:53:42 +0000345 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +0000346 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
347 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +0000348}
349
Duncan Sands6590b042007-08-27 15:47:50 +0000350/// addCleanup - Add a cleanup action for a landing pad.
351///
352void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
353 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
354 LP.TypeIds.push_back(0);
355}
356
Jim Laskey59667fe2007-02-21 22:38:31 +0000357/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
358/// pads.
359void MachineModuleInfo::TidyLandingPads() {
360 for (unsigned i = 0; i != LandingPads.size(); ) {
361 LandingPadInfo &LandingPad = LandingPads[i];
Chris Lattner16112732010-03-14 01:41:15 +0000362 if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined())
Chris Lattnera34ec2292010-03-09 01:51:43 +0000363 LandingPad.LandingPadLabel = 0;
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000364
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000365 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +0000366 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000367 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +0000368 LandingPads.erase(LandingPads.begin() + i);
369 continue;
370 }
Duncan Sands57810cd2007-09-05 11:27:52 +0000371
Chris Lattner16112732010-03-14 01:41:15 +0000372 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
373 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
374 MCSymbol *EndLabel = LandingPad.EndLabels[j];
375 if (BeginLabel->isDefined() && EndLabel->isDefined()) continue;
376
377 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
378 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
379 --j, --e;
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000380 }
Duncan Sands57810cd2007-09-05 11:27:52 +0000381
382 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +0000383 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +0000384 LandingPads.erase(LandingPads.begin() + i);
385 continue;
386 }
387
388 // If there is no landing pad, ensure that the list of typeids is empty.
389 // If the only typeid is a cleanup, this is the same as having no typeids.
390 if (!LandingPad.LandingPadBlock ||
391 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
392 LandingPad.TypeIds.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +0000393 ++i;
394 }
395}
396
Eric Christophercf296972009-08-26 21:27:09 +0000397/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Jim Laskey59667fe2007-02-21 22:38:31 +0000398/// function wide.
399unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling10fff602008-07-03 22:53:42 +0000400 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
401 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +0000402
403 TypeInfos.push_back(TI);
404 return TypeInfos.size();
405}
406
Duncan Sands73ef58a2007-06-02 16:53:42 +0000407/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
408/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +0000409int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +0000410 // If the new filter coincides with the tail of an existing filter, then
411 // re-use the existing filter. Folding filters more than this requires
412 // re-ordering filters and/or their elements - probably not worth it.
413 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
414 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +0000415 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +0000416
417 while (i && j)
418 if (FilterIds[--i] != TyIds[--j])
419 goto try_next;
420
421 if (!j)
422 // The new filter coincides with range [i, end) of the existing filter.
423 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +0000424
Duncan Sands14da32a2007-07-05 15:15:01 +0000425try_next:;
426 }
427
428 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +0000429 int FilterID = -(1 + FilterIds.size());
430 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
431 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +0000432 FilterIds.push_back(TyIds[I]);
Bill Wendling914c9702008-06-27 01:27:56 +0000433 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +0000434 FilterIds.push_back(0); // terminator
435 return FilterID;
436}
437
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000438/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +0000439Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000440 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000441 // function
442 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +0000443}
444
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000445/// getPersonalityIndex - Return unique index for current personality
Eric Christopher5e365e22009-08-26 21:44:57 +0000446/// function. NULL/first personality function should always get zero index.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000447unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000448 const Function* Personality = NULL;
Eric Christophercf296972009-08-26 21:27:09 +0000449
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000450 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling10fff602008-07-03 22:53:42 +0000451 for (unsigned i = 0; i != LandingPads.size(); ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000452 if (LandingPads[i].Personality) {
453 Personality = LandingPads[i].Personality;
454 break;
455 }
Eric Christophercf296972009-08-26 21:27:09 +0000456
Bill Wendling10fff602008-07-03 22:53:42 +0000457 for (unsigned i = 0; i < Personalities.size(); ++i) {
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000458 if (Personalities[i] == Personality)
459 return i;
Bill Wendling10fff602008-07-03 22:53:42 +0000460 }
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000461
Eric Christopher5e365e22009-08-26 21:44:57 +0000462 // This will happen if the current personality function is
463 // in the zero index.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000464 return 0;
465}
Jim Laskey59667fe2007-02-21 22:38:31 +0000466