blob: 27c138789dc5af3b0cf4b7e5784bccfbcc1b9d3b [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/MCAsmInfo.h"
26#include "llvm/MC/MCSymbol.h"
Jim Laskeyb3e789a2006-01-26 20:21:46 +000027#include "llvm/Support/Dwarf.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Jim Laskey6af56812006-01-04 13:36:38 +000029using namespace llvm;
Jim Laskey9c4447a2006-03-01 20:39:36 +000030using namespace llvm::dwarf;
Jim Laskey6af56812006-01-04 13:36:38 +000031
32// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman844731a2008-05-13 00:00:25 +000033static RegisterPass<MachineModuleInfo>
Chris Lattner11d53c12010-03-13 20:55:24 +000034X("machinemoduleinfo", "Machine Module Information");
Devang Patel19974732007-05-03 01:11:54 +000035char MachineModuleInfo::ID = 0;
Jim Laskey063e7652006-01-17 17:31:53 +000036
Chris Lattnera70e2e32009-09-15 22:44:26 +000037// Out of line virtual method.
38MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
39
Jim Laskeyb3e789a2006-01-26 20:21:46 +000040//===----------------------------------------------------------------------===//
Eric Christophercf296972009-08-26 21:27:09 +000041
Chris Lattner11d53c12010-03-13 20:55:24 +000042MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
43: ImmutablePass(&ID), Context(MAI),
44 ObjFileMMI(0), CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0),
45 DbgInfoAvailable(false) {
Eric Christopherd44fff72009-08-26 21:30:49 +000046 // Always emit some info, by default "no personality" info.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +000047 Personalities.push_back(NULL);
48}
Jim Laskeyb3e789a2006-01-26 20:21:46 +000049
Chris Lattner11d53c12010-03-13 20:55:24 +000050MachineModuleInfo::MachineModuleInfo()
51: ImmutablePass(&ID), Context(*(MCAsmInfo*)0) {
52 assert(0 && "This MachineModuleInfo constructor should never be called, MMI "
53 "should always be explicitly constructed by LLVMTargetMachine");
54 abort();
55}
56
Chris Lattnera70e2e32009-09-15 22:44:26 +000057MachineModuleInfo::~MachineModuleInfo() {
Chris Lattnerf1854552009-09-16 05:26:00 +000058 delete ObjFileMMI;
Jim Laskeyb3e789a2006-01-26 20:21:46 +000059}
60
Jim Laskey6da18642007-01-26 21:38:26 +000061/// doInitialization - Initialize the state for a new module.
Jim Laskeyb2efb852006-01-04 22:28:25 +000062///
Jim Laskey6da18642007-01-26 21:38:26 +000063bool MachineModuleInfo::doInitialization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +000064 return false;
Jim Laskey6af56812006-01-04 13:36:38 +000065}
66
Jim Laskey6da18642007-01-26 21:38:26 +000067/// doFinalization - Tear down the state after completion of a module.
Jim Laskeyb2efb852006-01-04 22:28:25 +000068///
Jim Laskey6da18642007-01-26 21:38:26 +000069bool MachineModuleInfo::doFinalization() {
Jim Laskeyb2efb852006-01-04 22:28:25 +000070 return false;
71}
Jim Laskeyb3e789a2006-01-26 20:21:46 +000072
Chris Lattner16112732010-03-14 01:41:15 +000073/// getLabelSym - Turn a label ID into a symbol.
74MCSymbol *MachineModuleInfo::getLabelSym(unsigned ID) {
75 return Context.GetOrCreateTemporarySymbol
76 (Twine(Context.getAsmInfo().getPrivateGlobalPrefix()) + "Label" +Twine(ID));
77}
78
Jim Laskey6da18642007-01-26 21:38:26 +000079/// EndFunction - Discard function meta information.
Jim Laskey41886992006-04-07 16:34:46 +000080///
Jim Laskey6da18642007-01-26 21:38:26 +000081void MachineModuleInfo::EndFunction() {
Jim Laskey41886992006-04-07 16:34:46 +000082 // Clean up frame info.
Jim Laskey41886992006-04-07 16:34:46 +000083 FrameMoves.clear();
Eric Christophercf296972009-08-26 21:27:09 +000084
Jim Laskey59667fe2007-02-21 22:38:31 +000085 // Clean up exception info.
86 LandingPads.clear();
Jim Grosbachca752c92010-01-28 01:45:32 +000087 CallSiteMap.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +000088 TypeInfos.clear();
Duncan Sands73ef58a2007-06-02 16:53:42 +000089 FilterIds.clear();
Duncan Sands14da32a2007-07-05 15:15:01 +000090 FilterEnds.clear();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091 CallsEHReturn = 0;
92 CallsUnwindInit = 0;
Devang Patel85d29e22009-10-08 20:41:17 +000093 VariableDbgInfo.clear();
Jim Laskey41886992006-04-07 16:34:46 +000094}
95
Jim Laskeyb3e789a2006-01-26 20:21:46 +000096/// AnalyzeModule - Scan the module for global debug information.
97///
Jim Laskey6da18642007-01-26 21:38:26 +000098void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner401e10c2009-07-20 06:14:25 +000099 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
100 // UsedFunctions.
Dale Johannesen48ae02f2008-01-16 19:59:28 +0000101 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
102 if (!GV || !GV->hasInitializer()) return;
103
104 // Should be an array of 'i8*'.
105 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
106 if (InitList == 0) return;
107
Chris Lattner1d5c4932009-07-20 06:05:50 +0000108 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
109 if (Function *F =
110 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
111 UsedFunctions.insert(F);
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000112}
113
Jim Laskey59667fe2007-02-21 22:38:31 +0000114//===-EH-------------------------------------------------------------------===//
115
116/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
117/// specified MachineBasicBlock.
Bill Wendling10fff602008-07-03 22:53:42 +0000118LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
119 (MachineBasicBlock *LandingPad) {
Jim Laskey59667fe2007-02-21 22:38:31 +0000120 unsigned N = LandingPads.size();
121 for (unsigned i = 0; i < N; ++i) {
Jim Laskey59e84342007-03-01 20:25:32 +0000122 LandingPadInfo &LP = LandingPads[i];
123 if (LP.LandingPadBlock == LandingPad)
124 return LP;
Jim Laskey59667fe2007-02-21 22:38:31 +0000125 }
Eric Christophercf296972009-08-26 21:27:09 +0000126
Jim Laskey59667fe2007-02-21 22:38:31 +0000127 LandingPads.push_back(LandingPadInfo(LandingPad));
128 return LandingPads[N];
129}
130
131/// addInvoke - Provide the begin and end labels of an invoke style call and
132/// associate it with a try landing pad block.
133void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
Chris Lattner16112732010-03-14 01:41:15 +0000134 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
Jim Laskey59e84342007-03-01 20:25:32 +0000135 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000136 LP.BeginLabels.push_back(BeginLabel);
137 LP.EndLabels.push_back(EndLabel);
Jim Laskey59667fe2007-02-21 22:38:31 +0000138}
139
140/// addLandingPad - Provide the label of a try LandingPad block.
141///
142unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
Chris Lattner16112732010-03-14 01:41:15 +0000143 unsigned LandingPadID = NextLabelID();
144 MCSymbol *LandingPadLabel = getLabelSym(LandingPadID);
Jim Laskey59e84342007-03-01 20:25:32 +0000145 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christophercf296972009-08-26 21:27:09 +0000146 LP.LandingPadLabel = LandingPadLabel;
Chris Lattner16112732010-03-14 01:41:15 +0000147 return LandingPadID;
Jim Laskey59667fe2007-02-21 22:38:31 +0000148}
149
150/// addPersonality - Provide the personality function for the exception
151/// information.
152void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000153 Function *Personality) {
Jim Laskey59e84342007-03-01 20:25:32 +0000154 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000155 LP.Personality = Personality;
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000156
Bill Wendling10fff602008-07-03 22:53:42 +0000157 for (unsigned i = 0; i < Personalities.size(); ++i)
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000158 if (Personalities[i] == Personality)
159 return;
Eric Christophercf296972009-08-26 21:27:09 +0000160
Eric Christopherd44fff72009-08-26 21:30:49 +0000161 // If this is the first personality we're adding go
162 // ahead and add it at the beginning.
163 if (Personalities[0] == NULL)
164 Personalities[0] = Personality;
165 else
166 Personalities.push_back(Personality);
Jim Laskey59667fe2007-02-21 22:38:31 +0000167}
168
169/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
170///
171void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
172 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +0000173 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Jim Laskey59667fe2007-02-21 22:38:31 +0000174 for (unsigned N = TyInfo.size(); N; --N)
Jim Laskey59e84342007-03-01 20:25:32 +0000175 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
Jim Laskey59667fe2007-02-21 22:38:31 +0000176}
Duncan Sands73ef58a2007-06-02 16:53:42 +0000177
178/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
Jim Laskey59e84342007-03-01 20:25:32 +0000179///
Duncan Sands73ef58a2007-06-02 16:53:42 +0000180void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
181 std::vector<GlobalVariable *> &TyInfo) {
Jim Laskey59e84342007-03-01 20:25:32 +0000182 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling49255672008-07-07 21:41:57 +0000183 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling10fff602008-07-03 22:53:42 +0000184 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +0000185 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
186 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
Jim Laskey59e84342007-03-01 20:25:32 +0000187}
188
Duncan Sands6590b042007-08-27 15:47:50 +0000189/// addCleanup - Add a cleanup action for a landing pad.
190///
191void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
192 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
193 LP.TypeIds.push_back(0);
194}
195
Jim Laskey59667fe2007-02-21 22:38:31 +0000196/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
197/// pads.
198void MachineModuleInfo::TidyLandingPads() {
199 for (unsigned i = 0; i != LandingPads.size(); ) {
200 LandingPadInfo &LandingPad = LandingPads[i];
Chris Lattner16112732010-03-14 01:41:15 +0000201 if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined())
Chris Lattnera34ec2292010-03-09 01:51:43 +0000202 LandingPad.LandingPadLabel = 0;
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000203
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000204 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands481dc722007-12-19 07:36:31 +0000205 // "nounwind" case.
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000206 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
Jim Laskey59667fe2007-02-21 22:38:31 +0000207 LandingPads.erase(LandingPads.begin() + i);
208 continue;
209 }
Duncan Sands57810cd2007-09-05 11:27:52 +0000210
Chris Lattner16112732010-03-14 01:41:15 +0000211 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
212 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
213 MCSymbol *EndLabel = LandingPad.EndLabels[j];
214 if (BeginLabel->isDefined() && EndLabel->isDefined()) continue;
215
216 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
217 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
218 --j, --e;
Anton Korobeynikoveeb37e02007-05-10 22:34:59 +0000219 }
Duncan Sands57810cd2007-09-05 11:27:52 +0000220
221 // Remove landing pads with no try-ranges.
Dan Gohman30359592008-01-29 13:02:09 +0000222 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands57810cd2007-09-05 11:27:52 +0000223 LandingPads.erase(LandingPads.begin() + i);
224 continue;
225 }
226
227 // If there is no landing pad, ensure that the list of typeids is empty.
228 // If the only typeid is a cleanup, this is the same as having no typeids.
229 if (!LandingPad.LandingPadBlock ||
230 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
231 LandingPad.TypeIds.clear();
Jim Laskey59667fe2007-02-21 22:38:31 +0000232 ++i;
233 }
234}
235
Eric Christophercf296972009-08-26 21:27:09 +0000236/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Jim Laskey59667fe2007-02-21 22:38:31 +0000237/// function wide.
238unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling10fff602008-07-03 22:53:42 +0000239 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
240 if (TypeInfos[i] == TI) return i + 1;
Jim Laskey59667fe2007-02-21 22:38:31 +0000241
242 TypeInfos.push_back(TI);
243 return TypeInfos.size();
244}
245
Duncan Sands73ef58a2007-06-02 16:53:42 +0000246/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
247/// function wide.
Bill Wendling914c9702008-06-27 01:27:56 +0000248int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Duncan Sands14da32a2007-07-05 15:15:01 +0000249 // If the new filter coincides with the tail of an existing filter, then
250 // re-use the existing filter. Folding filters more than this requires
251 // re-ordering filters and/or their elements - probably not worth it.
252 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
253 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling914c9702008-06-27 01:27:56 +0000254 unsigned i = *I, j = TyIds.size();
Duncan Sands14da32a2007-07-05 15:15:01 +0000255
256 while (i && j)
257 if (FilterIds[--i] != TyIds[--j])
258 goto try_next;
259
260 if (!j)
261 // The new filter coincides with range [i, end) of the existing filter.
262 return -(1 + i);
Bill Wendling914c9702008-06-27 01:27:56 +0000263
Duncan Sands14da32a2007-07-05 15:15:01 +0000264try_next:;
265 }
266
267 // Add the new filter.
Bill Wendling914c9702008-06-27 01:27:56 +0000268 int FilterID = -(1 + FilterIds.size());
269 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
270 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Duncan Sands73ef58a2007-06-02 16:53:42 +0000271 FilterIds.push_back(TyIds[I]);
Bill Wendling914c9702008-06-27 01:27:56 +0000272 FilterEnds.push_back(FilterIds.size());
Duncan Sands73ef58a2007-06-02 16:53:42 +0000273 FilterIds.push_back(0); // terminator
274 return FilterID;
275}
276
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000277/// getPersonality - Return the personality function for the current function.
Jim Laskey59667fe2007-02-21 22:38:31 +0000278Function *MachineModuleInfo::getPersonality() const {
Anton Korobeynikov0ff3ca42007-05-12 22:36:25 +0000279 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000280 // function
281 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
Jim Laskey59667fe2007-02-21 22:38:31 +0000282}
283
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000284/// getPersonalityIndex - Return unique index for current personality
Eric Christopher5e365e22009-08-26 21:44:57 +0000285/// function. NULL/first personality function should always get zero index.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000286unsigned MachineModuleInfo::getPersonalityIndex() const {
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000287 const Function* Personality = NULL;
Eric Christophercf296972009-08-26 21:27:09 +0000288
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000289 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling10fff602008-07-03 22:53:42 +0000290 for (unsigned i = 0; i != LandingPads.size(); ++i)
Anton Korobeynikov070280e2007-05-23 11:08:31 +0000291 if (LandingPads[i].Personality) {
292 Personality = LandingPads[i].Personality;
293 break;
294 }
Eric Christophercf296972009-08-26 21:27:09 +0000295
Bill Wendling10fff602008-07-03 22:53:42 +0000296 for (unsigned i = 0; i < Personalities.size(); ++i) {
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000297 if (Personalities[i] == Personality)
298 return i;
Bill Wendling10fff602008-07-03 22:53:42 +0000299 }
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000300
Eric Christopher5e365e22009-08-26 21:44:57 +0000301 // This will happen if the current personality function is
302 // in the zero index.
Anton Korobeynikov8c7c1732007-05-13 15:42:26 +0000303 return 0;
304}
Jim Laskey59667fe2007-02-21 22:38:31 +0000305