change the LabelSDNode to be EHLabelSDNode and make it hold
[oota-llvm.git] / lib / CodeGen / MachineModuleInfo.cpp
1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/CodeGen/MachineModuleInfo.h"
11
12 #include "llvm/Constants.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/GlobalVariable.h"
15 #include "llvm/Intrinsics.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Module.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 using namespace llvm;
30 using namespace llvm::dwarf;
31
32 // Handle the Pass registration stuff necessary to use TargetData's.
33 static RegisterPass<MachineModuleInfo>
34 X("machinemoduleinfo", "Machine Module Information");
35 char MachineModuleInfo::ID = 0;
36
37 // Out of line virtual method.
38 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
39
40 //===----------------------------------------------------------------------===//
41
42 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
43 : ImmutablePass(&ID), Context(MAI),
44   ObjFileMMI(0), NextLabelIDToReturn(1), 
45   CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){
46   // Always emit some info, by default "no personality" info.
47   Personalities.push_back(NULL);
48 }
49
50 MachineModuleInfo::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
57 MachineModuleInfo::~MachineModuleInfo() {
58   delete ObjFileMMI;
59 }
60
61 /// doInitialization - Initialize the state for a new module.
62 ///
63 bool MachineModuleInfo::doInitialization() {
64   return false;
65 }
66
67 /// doFinalization - Tear down the state after completion of a module.
68 ///
69 bool MachineModuleInfo::doFinalization() {
70   return false;
71 }
72
73 /// getLabelSym - Turn a label ID into a symbol.
74 MCSymbol *MachineModuleInfo::getLabelSym(unsigned ID) {
75   return Context.GetOrCreateTemporarySymbol
76     (Twine(Context.getAsmInfo().getPrivateGlobalPrefix()) + "label" +Twine(ID));
77 }
78
79 /// EndFunction - Discard function meta information.
80 ///
81 void MachineModuleInfo::EndFunction() {
82   // Clean up frame info.
83   FrameMoves.clear();
84
85   // Clean up exception info.
86   LandingPads.clear();
87   CallSiteMap.clear();
88   TypeInfos.clear();
89   FilterIds.clear();
90   FilterEnds.clear();
91   CallsEHReturn = 0;
92   CallsUnwindInit = 0;
93   VariableDbgInfo.clear();
94 }
95
96 /// AnalyzeModule - Scan the module for global debug information.
97 ///
98 void MachineModuleInfo::AnalyzeModule(Module &M) {
99   // Insert functions in the llvm.used array (but not llvm.compiler.used) into
100   // UsedFunctions.
101   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
108   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);
112 }
113
114 //===-EH-------------------------------------------------------------------===//
115
116 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
117 /// specified MachineBasicBlock.
118 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
119     (MachineBasicBlock *LandingPad) {
120   unsigned N = LandingPads.size();
121   for (unsigned i = 0; i < N; ++i) {
122     LandingPadInfo &LP = LandingPads[i];
123     if (LP.LandingPadBlock == LandingPad)
124       return LP;
125   }
126
127   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.
133 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
134                                   MCSymbol *BeginLabel, MCSymbol *EndLabel) {
135   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
136   LP.BeginLabels.push_back(BeginLabel);
137   LP.EndLabels.push_back(EndLabel);
138 }
139
140 /// addLandingPad - Provide the label of a try LandingPad block.
141 ///
142 MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
143   MCSymbol *LandingPadLabel = getLabelSym(NextLabelID());
144   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
145   LP.LandingPadLabel = LandingPadLabel;
146   return LandingPadLabel;
147 }
148
149 /// addPersonality - Provide the personality function for the exception
150 /// information.
151 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
152                                        Function *Personality) {
153   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
154   LP.Personality = Personality;
155
156   for (unsigned i = 0; i < Personalities.size(); ++i)
157     if (Personalities[i] == Personality)
158       return;
159
160   // If this is the first personality we're adding go
161   // ahead and add it at the beginning.
162   if (Personalities[0] == NULL)
163     Personalities[0] = Personality;
164   else
165     Personalities.push_back(Personality);
166 }
167
168 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
169 ///
170 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
171                                         std::vector<GlobalVariable *> &TyInfo) {
172   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
173   for (unsigned N = TyInfo.size(); N; --N)
174     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
175 }
176
177 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
178 ///
179 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
180                                         std::vector<GlobalVariable *> &TyInfo) {
181   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
182   std::vector<unsigned> IdsInFilter(TyInfo.size());
183   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
184     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
185   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
186 }
187
188 /// addCleanup - Add a cleanup action for a landing pad.
189 ///
190 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
191   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
192   LP.TypeIds.push_back(0);
193 }
194
195 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
196 /// pads.
197 void MachineModuleInfo::TidyLandingPads() {
198   for (unsigned i = 0; i != LandingPads.size(); ) {
199     LandingPadInfo &LandingPad = LandingPads[i];
200     if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined())
201       LandingPad.LandingPadLabel = 0;
202
203     // Special case: we *should* emit LPs with null LP MBB. This indicates
204     // "nounwind" case.
205     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
206       LandingPads.erase(LandingPads.begin() + i);
207       continue;
208     }
209
210     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
211       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
212       MCSymbol *EndLabel = LandingPad.EndLabels[j];
213       if (BeginLabel->isDefined() && EndLabel->isDefined()) continue;
214       
215       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
216       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
217       --j, --e;
218     }
219
220     // Remove landing pads with no try-ranges.
221     if (LandingPads[i].BeginLabels.empty()) {
222       LandingPads.erase(LandingPads.begin() + i);
223       continue;
224     }
225
226     // If there is no landing pad, ensure that the list of typeids is empty.
227     // If the only typeid is a cleanup, this is the same as having no typeids.
228     if (!LandingPad.LandingPadBlock ||
229         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
230       LandingPad.TypeIds.clear();
231     ++i;
232   }
233 }
234
235 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
236 /// function wide.
237 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
238   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
239     if (TypeInfos[i] == TI) return i + 1;
240
241   TypeInfos.push_back(TI);
242   return TypeInfos.size();
243 }
244
245 /// getFilterIDFor - Return the filter id for the specified typeinfos.  This is
246 /// function wide.
247 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
248   // If the new filter coincides with the tail of an existing filter, then
249   // re-use the existing filter.  Folding filters more than this requires
250   // re-ordering filters and/or their elements - probably not worth it.
251   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
252        E = FilterEnds.end(); I != E; ++I) {
253     unsigned i = *I, j = TyIds.size();
254
255     while (i && j)
256       if (FilterIds[--i] != TyIds[--j])
257         goto try_next;
258
259     if (!j)
260       // The new filter coincides with range [i, end) of the existing filter.
261       return -(1 + i);
262
263 try_next:;
264   }
265
266   // Add the new filter.
267   int FilterID = -(1 + FilterIds.size());
268   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
269   for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
270     FilterIds.push_back(TyIds[I]);
271   FilterEnds.push_back(FilterIds.size());
272   FilterIds.push_back(0); // terminator
273   return FilterID;
274 }
275
276 /// getPersonality - Return the personality function for the current function.
277 Function *MachineModuleInfo::getPersonality() const {
278   // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
279   // function
280   return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
281 }
282
283 /// getPersonalityIndex - Return unique index for current personality
284 /// function. NULL/first personality function should always get zero index.
285 unsigned MachineModuleInfo::getPersonalityIndex() const {
286   const Function* Personality = NULL;
287
288   // Scan landing pads. If there is at least one non-NULL personality - use it.
289   for (unsigned i = 0; i != LandingPads.size(); ++i)
290     if (LandingPads[i].Personality) {
291       Personality = LandingPads[i].Personality;
292       break;
293     }
294
295   for (unsigned i = 0; i < Personalities.size(); ++i) {
296     if (Personalities[i] == Personality)
297       return i;
298   }
299
300   // This will happen if the current personality function is
301   // in the zero index.
302   return 0;
303 }
304