blob: dc26d9144ffae8cbccbdaed14594b0757b90fb70 [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)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043, CallsEHReturn(0)
44, CallsUnwindInit(0)
Chris Lattner72ba6722009-09-15 22:44:26 +000045, DbgInfoAvailable(false) {
Eric Christopherc87d5ae2009-08-26 21:30:49 +000046 // Always emit some info, by default "no personality" info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 Personalities.push_back(NULL);
48}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
Chris Lattner72ba6722009-09-15 22:44:26 +000050MachineModuleInfo::~MachineModuleInfo() {
Chris Lattner16fcdf92009-09-16 05:26:00 +000051 delete ObjFileMMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052}
53
54/// doInitialization - Initialize the state for a new module.
55///
56bool MachineModuleInfo::doInitialization() {
57 return false;
58}
59
60/// doFinalization - Tear down the state after completion of a module.
61///
62bool MachineModuleInfo::doFinalization() {
63 return false;
64}
65
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066/// EndFunction - Discard function meta information.
67///
68void MachineModuleInfo::EndFunction() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 // Clean up frame info.
70 FrameMoves.clear();
Eric Christopher84603632009-08-26 21:27:09 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 // Clean up exception info.
73 LandingPads.clear();
Jim Grosbach1db5d982010-01-28 01:45:32 +000074 CallSiteMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 TypeInfos.clear();
76 FilterIds.clear();
77 FilterEnds.clear();
78 CallsEHReturn = 0;
79 CallsUnwindInit = 0;
Devang Patelf57e1062009-10-08 20:41:17 +000080 VariableDbgInfo.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081}
82
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083/// AnalyzeModule - Scan the module for global debug information.
84///
85void MachineModuleInfo::AnalyzeModule(Module &M) {
Chris Lattner1e0e0d12009-07-20 06:14:25 +000086 // Insert functions in the llvm.used array (but not llvm.compiler.used) into
87 // UsedFunctions.
Dale Johannesen3dadeb32008-01-16 19:59:28 +000088 GlobalVariable *GV = M.getGlobalVariable("llvm.used");
89 if (!GV || !GV->hasInitializer()) return;
90
91 // Should be an array of 'i8*'.
92 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
93 if (InitList == 0) return;
94
Chris Lattner26165602009-07-20 06:05:50 +000095 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
96 if (Function *F =
97 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
98 UsedFunctions.insert(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099}
100
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101//===-EH-------------------------------------------------------------------===//
102
103/// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
104/// specified MachineBasicBlock.
Bill Wendling4de8de52008-07-03 22:53:42 +0000105LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
106 (MachineBasicBlock *LandingPad) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 unsigned N = LandingPads.size();
108 for (unsigned i = 0; i < N; ++i) {
109 LandingPadInfo &LP = LandingPads[i];
110 if (LP.LandingPadBlock == LandingPad)
111 return LP;
112 }
Eric Christopher84603632009-08-26 21:27:09 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 LandingPads.push_back(LandingPadInfo(LandingPad));
115 return LandingPads[N];
116}
117
118/// addInvoke - Provide the begin and end labels of an invoke style call and
119/// associate it with a try landing pad block.
120void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
121 unsigned BeginLabel, unsigned EndLabel) {
122 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
123 LP.BeginLabels.push_back(BeginLabel);
124 LP.EndLabels.push_back(EndLabel);
125}
126
127/// addLandingPad - Provide the label of a try LandingPad block.
128///
129unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
130 unsigned LandingPadLabel = NextLabelID();
131 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Eric Christopher84603632009-08-26 21:27:09 +0000132 LP.LandingPadLabel = LandingPadLabel;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 return LandingPadLabel;
134}
135
136/// addPersonality - Provide the personality function for the exception
137/// information.
138void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
139 Function *Personality) {
140 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
141 LP.Personality = Personality;
142
Bill Wendling4de8de52008-07-03 22:53:42 +0000143 for (unsigned i = 0; i < Personalities.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 if (Personalities[i] == Personality)
145 return;
Eric Christopher84603632009-08-26 21:27:09 +0000146
Eric Christopherc87d5ae2009-08-26 21:30:49 +0000147 // If this is the first personality we're adding go
148 // ahead and add it at the beginning.
149 if (Personalities[0] == NULL)
150 Personalities[0] = Personality;
151 else
152 Personalities.push_back(Personality);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153}
154
155/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
156///
157void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
158 std::vector<GlobalVariable *> &TyInfo) {
159 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
160 for (unsigned N = TyInfo.size(); N; --N)
161 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
162}
163
164/// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
165///
166void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
167 std::vector<GlobalVariable *> &TyInfo) {
168 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
Bill Wendling993b3dd2008-07-07 21:41:57 +0000169 std::vector<unsigned> IdsInFilter(TyInfo.size());
Bill Wendling4de8de52008-07-03 22:53:42 +0000170 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
172 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
173}
174
Duncan Sands923fdb12007-08-27 15:47:50 +0000175/// addCleanup - Add a cleanup action for a landing pad.
176///
177void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
178 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
179 LP.TypeIds.push_back(0);
180}
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182/// TidyLandingPads - Remap landing pad labels and remove any deleted landing
183/// pads.
184void MachineModuleInfo::TidyLandingPads() {
185 for (unsigned i = 0; i != LandingPads.size(); ) {
186 LandingPadInfo &LandingPad = LandingPads[i];
187 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
188
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 // Special case: we *should* emit LPs with null LP MBB. This indicates
Duncan Sands4ff179f2007-12-19 07:36:31 +0000190 // "nounwind" case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
192 LandingPads.erase(LandingPads.begin() + i);
193 continue;
194 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000195
Bill Wendling4de8de52008-07-03 22:53:42 +0000196 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
198 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
Duncan Sands241a0c92007-09-05 11:27:52 +0000199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 if (!BeginLabel || !EndLabel) {
201 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
202 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
203 continue;
204 }
205
206 LandingPad.BeginLabels[j] = BeginLabel;
207 LandingPad.EndLabels[j] = EndLabel;
208 ++j;
209 }
Duncan Sands241a0c92007-09-05 11:27:52 +0000210
211 // Remove landing pads with no try-ranges.
Dan Gohman301f4052008-01-29 13:02:09 +0000212 if (LandingPads[i].BeginLabels.empty()) {
Duncan Sands241a0c92007-09-05 11:27:52 +0000213 LandingPads.erase(LandingPads.begin() + i);
214 continue;
215 }
216
217 // If there is no landing pad, ensure that the list of typeids is empty.
218 // If the only typeid is a cleanup, this is the same as having no typeids.
219 if (!LandingPad.LandingPadBlock ||
220 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
221 LandingPad.TypeIds.clear();
222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 ++i;
224 }
225}
226
Eric Christopher84603632009-08-26 21:27:09 +0000227/// getTypeIDFor - Return the type id for the specified typeinfo. This is
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228/// function wide.
229unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
Bill Wendling4de8de52008-07-03 22:53:42 +0000230 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
231 if (TypeInfos[i] == TI) return i + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
233 TypeInfos.push_back(TI);
234 return TypeInfos.size();
235}
236
237/// getFilterIDFor - Return the filter id for the specified typeinfos. This is
238/// function wide.
Bill Wendling0e679b82008-06-27 01:27:56 +0000239int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 // If the new filter coincides with the tail of an existing filter, then
241 // re-use the existing filter. Folding filters more than this requires
242 // re-ordering filters and/or their elements - probably not worth it.
243 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
244 E = FilterEnds.end(); I != E; ++I) {
Bill Wendling0e679b82008-06-27 01:27:56 +0000245 unsigned i = *I, j = TyIds.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246
247 while (i && j)
248 if (FilterIds[--i] != TyIds[--j])
249 goto try_next;
250
251 if (!j)
252 // The new filter coincides with range [i, end) of the existing filter.
253 return -(1 + i);
Bill Wendling0e679b82008-06-27 01:27:56 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255try_next:;
256 }
257
258 // Add the new filter.
Bill Wendling0e679b82008-06-27 01:27:56 +0000259 int FilterID = -(1 + FilterIds.size());
260 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
261 for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 FilterIds.push_back(TyIds[I]);
Bill Wendling0e679b82008-06-27 01:27:56 +0000263 FilterEnds.push_back(FilterIds.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 FilterIds.push_back(0); // terminator
265 return FilterID;
266}
267
268/// getPersonality - Return the personality function for the current function.
269Function *MachineModuleInfo::getPersonality() const {
270 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
271 // function
272 return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
273}
274
275/// getPersonalityIndex - Return unique index for current personality
Eric Christophere92cc8d2009-08-26 21:44:57 +0000276/// function. NULL/first personality function should always get zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277unsigned MachineModuleInfo::getPersonalityIndex() const {
278 const Function* Personality = NULL;
Eric Christopher84603632009-08-26 21:27:09 +0000279
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 // Scan landing pads. If there is at least one non-NULL personality - use it.
Bill Wendling4de8de52008-07-03 22:53:42 +0000281 for (unsigned i = 0; i != LandingPads.size(); ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 if (LandingPads[i].Personality) {
283 Personality = LandingPads[i].Personality;
284 break;
285 }
Eric Christopher84603632009-08-26 21:27:09 +0000286
Bill Wendling4de8de52008-07-03 22:53:42 +0000287 for (unsigned i = 0; i < Personalities.size(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 if (Personalities[i] == Personality)
289 return i;
Bill Wendling4de8de52008-07-03 22:53:42 +0000290 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291
Eric Christophere92cc8d2009-08-26 21:44:57 +0000292 // This will happen if the current personality function is
293 // in the zero index.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 return 0;
295}
296