blob: 5052af7bd8f04535cce680d256e32ebb26fbb0e4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/CodeGen/MachineModuleInfo.h"
11
12#include "llvm/Constants.h"
Evan Cheng833501d2008-06-30 07:31:25 +000013#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng1b42ac12008-09-22 22:21:38 +000016#include "llvm/CodeGen/Passes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Target/TargetInstrInfo.h"
18#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/TargetOptions.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/GlobalVariable.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/Instructions.h"
24#include "llvm/Module.h"
25#include "llvm/Support/Dwarf.h"
Edwin Török675d5622009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28using namespace llvm::dwarf;
29
30// Handle the Pass registration stuff necessary to use TargetData's.
Dan Gohman089efff2008-05-13 00:00:25 +000031static RegisterPass<MachineModuleInfo>
32X("machinemoduleinfo", "Module Information");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033char MachineModuleInfo::ID = 0;
34
Chris Lattner72ba6722009-09-15 22:44:26 +000035// Out of line virtual method.
36MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
37
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038//===----------------------------------------------------------------------===//
Eric Christopher84603632009-08-26 21:27:09 +000039
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040MachineModuleInfo::MachineModuleInfo()
Dan Gohman26f8c272008-09-04 17:05:41 +000041: ImmutablePass(&ID)
Chris Lattner16fcdf92009-09-16 05:26:00 +000042, ObjFileMMI(0)
Bob Wilsonf7cd4ca2010-02-06 05:55:20 +000043, CurCallSite(0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044, CallsEHReturn(0)
45, CallsUnwindInit(0)
Chris Lattner72ba6722009-09-15 22:44:26 +000046, DbgInfoAvailable(false) {
Eric Christopherc87d5ae2009-08-26 21:30:49 +000047 // Always emit some info, by default "no personality" info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 Personalities.push_back(NULL);
49}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
Chris Lattner72ba6722009-09-15 22:44:26 +000051MachineModuleInfo::~MachineModuleInfo() {
Chris Lattner16fcdf92009-09-16 05:26:00 +000052 delete ObjFileMMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053}
54
55/// doInitialization - Initialize the state for a new module.
56///
57bool MachineModuleInfo::doInitialization() {
58 return false;
59}
60
61/// doFinalization - Tear down the state after completion of a module.
62///
63bool MachineModuleInfo::doFinalization() {
64 return false;
65}
66
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067/// EndFunction - Discard function meta information.
68///
69void MachineModuleInfo::EndFunction() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 // Clean up frame info.
71 FrameMoves.clear();
Eric Christopher84603632009-08-26 21:27:09 +000072
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 // Clean up exception info.
74 LandingPads.clear();
Jim Grosbach1db5d982010-01-28 01:45:32 +000075 CallSiteMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 TypeInfos.clear();
77 FilterIds.clear();
78 FilterEnds.clear();
79 CallsEHReturn = 0;
80 CallsUnwindInit = 0;
Devang Patelf57e1062009-10-08 20:41:17 +000081 VariableDbgInfo.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082}
83
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084/// AnalyzeModule - Scan the module for global debug information.
85///
86void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner1e0e0d12009-07-20 06:14:25 +000087 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
88 // UsedFunctions.
Dale Johannesen3dadeb32008-01-16 19:59:28 +000089 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
90 if (!GV || !GV->hasInitializer()) return;
91
92 // Should be an array of 'i8*'.
93 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
94 if (InitList == 0) return;
95
Chris Lattner26165602009-07-20 06:05:50 +000096 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
97 if (Function *F =
98 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
99 UsedFunctions.insert(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102//===-EH-------------------------------------------------------------------===//
103
104/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
105/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000106LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
107 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 unsigned N = LandingPads.size();
109 for (unsigned i = 0; i < N; ++i) {
110 LandingPadInfo &LP = LandingPads[i];
111 if (LP.LandingPadBlock == LandingPad)
112 return LP;
113 }
Eric Christopher84603632009-08-26 21:27:09 +0000114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 LandingPads.push_back(LandingPadInfo(LandingPad));
116 return LandingPads[N];
117}
118
119/// addInvoke - Provide the begin and end labels of an invoke style call and
120/// associate it with a try landing pad block.
121void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
122 unsigned BeginLabel, unsigned EndLabel) {
123 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
124 LP.BeginLabels.push_back(BeginLabel);
125 LP.EndLabels.push_back(EndLabel);
126}
127
128/// addLandingPad - Provide the label of a try LandingPad block.
129///
130unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
131 unsigned LandingPadLabel = NextLabelID();
132 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopher84603632009-08-26 21:27:09 +0000133 LP.LandingPadLabel = LandingPadLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 return LandingPadLabel;
135}
136
137/// addPersonality - Provide the personality function for the exception
138/// information.
139void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
140 Function *Personality) {
141 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
142 LP.Personality = Personality;
143
Bill Wendling4de8de52008-07-03 22:53:42 +0000144 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 if (Personalities[i] == Personality)
146 return;
Eric Christopher84603632009-08-26 21:27:09 +0000147
Eric Christopherc87d5ae2009-08-26 21:30:49 +0000148 // If this is the first personality we're adding go
149 // ahead and add it at the beginning.
150 if (Personalities[0] == NULL)
151 Personalities[0] = Personality;
152 else
153 Personalities.push_back(Personality);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154}
155
156/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
157///
158void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
159 std::vector<GlobalVariable *> &TyInfo) {
160 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
161 for (unsigned N = TyInfo.size(); N; --N)
162 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
163}
164
165/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
166///
167void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
168 std::vector<GlobalVariable *> &TyInfo) {
169 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000170 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000171 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
173 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
174}
175
Duncan Sands923fdb12007-08-27 15:47:50 +0000176/// addCleanup - Add a cleanup action for a landing pad.
177///
178void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
179 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
180 LP.TypeIds.push_back(0);
181}
182
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
184/// pads.
185void MachineModuleInfo::TidyLandingPads() {
186 for (unsigned i = 0; i != LandingPads.size(); ) {
187 LandingPadInfo &LandingPad = LandingPads[i];
188 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
189
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000191 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
193 LandingPads.erase(LandingPads.begin() + i);
194 continue;
195 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000196
Bill Wendling4de8de52008-07-03 22:53:42 +0000197 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
199 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands241a0c92007-09-05 11:27:52 +0000200
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 if (!BeginLabel || !EndLabel) {
202 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
203 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
204 continue;
205 }
206
207 LandingPad.BeginLabels[j] = BeginLabel;
208 LandingPad.EndLabels[j] = EndLabel;
209 ++j;
210 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000211
212 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000213 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000214 LandingPads.erase(LandingPads.begin() + i);
215 continue;
216 }
217
218 // If there is no landing pad, ensure that the list of typeids is empty.
219 // If the only typeid is a cleanup, this is the same as having no typeids.
220 if (!LandingPad.LandingPadBlock ||
221 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
222 LandingPad.TypeIds.clear();
223
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 ++i;
225 }
226}
227
Eric Christopher84603632009-08-26 21:27:09 +0000228/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229/// function wide.
230unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000231 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
232 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233
234 TypeInfos.push_back(TI);
235 return TypeInfos.size();
236}
237
238/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
239/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000240int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 // If the new filter coincides with the tail of an existing filter, then
242 // re-use the existing filter. Folding filters more than this requires
243 // re-ordering filters and/or their elements - probably not worth it.
244 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
245 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000246 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
248 while (i && j)
249 if (FilterIds[--i] != TyIds[--j])
250 goto try_next;
251
252 if (!j)
253 // The new filter coincides with range [i, end) of the existing filter.
254 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256try_next:;
257 }
258
259 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000260 int FilterID = -(1 + FilterIds.size());
261 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
262 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000264 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 FilterIds.push_back(0); // terminator
266 return FilterID;
267}
268
269/// getPersonality - Return the personality function for the current function.
270Function *MachineModuleInfo::getPersonality() const {
271 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
272 // function
273 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
274}
275
276/// getPersonalityIndex - Return unique index for current personality
Eric Christophere92cc8d2009-08-26 21:44:57 +0000277/// function. NULL/first personality function should always get zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278unsigned MachineModuleInfo::getPersonalityIndex() const {
279 const Function* Personality = NULL;
Eric Christopher84603632009-08-26 21:27:09 +0000280
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000282 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 if (LandingPads[i].Personality) {
284 Personality = LandingPads[i].Personality;
285 break;
286 }
Eric Christopher84603632009-08-26 21:27:09 +0000287
Bill Wendling4de8de52008-07-03 22:53:42 +0000288 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 if (Personalities[i] == Personality)
290 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
Eric Christophere92cc8d2009-08-26 21:44:57 +0000293 // This will happen if the current personality function is
294 // in the zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 return 0;
296}
297