strength reduce MMI::MappedLabel to MMI::isLabelDeleted,
[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/Analysis/ValueTracking.h"
14 #include "llvm/CodeGen/MachineFunctionPass.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/Passes.h"
17 #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"
26 #include "llvm/Support/ErrorHandling.h"
27 using namespace llvm;
28 using namespace llvm::dwarf;
29
30 // Handle the Pass registration stuff necessary to use TargetData's.
31 static RegisterPass<MachineModuleInfo>
32 X("machinemoduleinfo", "Module Information");
33 char MachineModuleInfo::ID = 0;
34
35 // Out of line virtual method.
36 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
37
38 //===----------------------------------------------------------------------===//
39
40 MachineModuleInfo::MachineModuleInfo()
41 : ImmutablePass(&ID)
42 , ObjFileMMI(0)
43 , CurCallSite(0)
44 , CallsEHReturn(0)
45 , CallsUnwindInit(0)
46 , DbgInfoAvailable(false) {
47   // Always emit some info, by default "no personality" info.
48   Personalities.push_back(NULL);
49 }
50
51 MachineModuleInfo::~MachineModuleInfo() {
52   delete ObjFileMMI;
53 }
54
55 /// doInitialization - Initialize the state for a new module.
56 ///
57 bool MachineModuleInfo::doInitialization() {
58   return false;
59 }
60
61 /// doFinalization - Tear down the state after completion of a module.
62 ///
63 bool MachineModuleInfo::doFinalization() {
64   return false;
65 }
66
67 /// EndFunction - Discard function meta information.
68 ///
69 void MachineModuleInfo::EndFunction() {
70   // Clean up frame info.
71   FrameMoves.clear();
72
73   // Clean up exception info.
74   LandingPads.clear();
75   CallSiteMap.clear();
76   TypeInfos.clear();
77   FilterIds.clear();
78   FilterEnds.clear();
79   CallsEHReturn = 0;
80   CallsUnwindInit = 0;
81   VariableDbgInfo.clear();
82 }
83
84 /// AnalyzeModule - Scan the module for global debug information.
85 ///
86 void MachineModuleInfo::AnalyzeModule(Module &M) {
87   // Insert functions in the llvm.used array (but not llvm.compiler.used) into
88   // UsedFunctions.
89   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
96   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);
100 }
101
102 //===-EH-------------------------------------------------------------------===//
103
104 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
105 /// specified MachineBasicBlock.
106 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
107     (MachineBasicBlock *LandingPad) {
108   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   }
114
115   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.
121 void 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 ///
130 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
131   unsigned LandingPadLabel = NextLabelID();
132   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
133   LP.LandingPadLabel = LandingPadLabel;
134   return LandingPadLabel;
135 }
136
137 /// addPersonality - Provide the personality function for the exception
138 /// information.
139 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
140                                        Function *Personality) {
141   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
142   LP.Personality = Personality;
143
144   for (unsigned i = 0; i < Personalities.size(); ++i)
145     if (Personalities[i] == Personality)
146       return;
147
148   // 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);
154 }
155
156 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
157 ///
158 void 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 ///
167 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
168                                         std::vector<GlobalVariable *> &TyInfo) {
169   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
170   std::vector<unsigned> IdsInFilter(TyInfo.size());
171   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
172     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
173   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
174 }
175
176 /// addCleanup - Add a cleanup action for a landing pad.
177 ///
178 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
179   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
180   LP.TypeIds.push_back(0);
181 }
182
183 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
184 /// pads.
185 void MachineModuleInfo::TidyLandingPads() {
186   for (unsigned i = 0; i != LandingPads.size(); ) {
187     LandingPadInfo &LandingPad = LandingPads[i];
188     if (isLabelDeleted(LandingPad.LandingPadLabel))
189       LandingPad.LandingPadLabel = 0;
190
191     // Special case: we *should* emit LPs with null LP MBB. This indicates
192     // "nounwind" case.
193     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
194       LandingPads.erase(LandingPads.begin() + i);
195       continue;
196     }
197
198     for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
199       unsigned BeginLabel = LandingPad.BeginLabels[j];
200       unsigned EndLabel = LandingPad.EndLabels[j];
201       if (isLabelDeleted(BeginLabel) || isLabelDeleted(EndLabel)) {
202         LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
203         LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
204         continue;
205       }
206
207       ++j;
208     }
209
210     // Remove landing pads with no try-ranges.
211     if (LandingPads[i].BeginLabels.empty()) {
212       LandingPads.erase(LandingPads.begin() + i);
213       continue;
214     }
215
216     // If there is no landing pad, ensure that the list of typeids is empty.
217     // If the only typeid is a cleanup, this is the same as having no typeids.
218     if (!LandingPad.LandingPadBlock ||
219         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
220       LandingPad.TypeIds.clear();
221
222     ++i;
223   }
224 }
225
226 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
227 /// function wide.
228 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
229   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
230     if (TypeInfos[i] == TI) return i + 1;
231
232   TypeInfos.push_back(TI);
233   return TypeInfos.size();
234 }
235
236 /// getFilterIDFor - Return the filter id for the specified typeinfos.  This is
237 /// function wide.
238 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
239   // If the new filter coincides with the tail of an existing filter, then
240   // re-use the existing filter.  Folding filters more than this requires
241   // re-ordering filters and/or their elements - probably not worth it.
242   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
243        E = FilterEnds.end(); I != E; ++I) {
244     unsigned i = *I, j = TyIds.size();
245
246     while (i && j)
247       if (FilterIds[--i] != TyIds[--j])
248         goto try_next;
249
250     if (!j)
251       // The new filter coincides with range [i, end) of the existing filter.
252       return -(1 + i);
253
254 try_next:;
255   }
256
257   // Add the new filter.
258   int FilterID = -(1 + FilterIds.size());
259   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
260   for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
261     FilterIds.push_back(TyIds[I]);
262   FilterEnds.push_back(FilterIds.size());
263   FilterIds.push_back(0); // terminator
264   return FilterID;
265 }
266
267 /// getPersonality - Return the personality function for the current function.
268 Function *MachineModuleInfo::getPersonality() const {
269   // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
270   // function
271   return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
272 }
273
274 /// getPersonalityIndex - Return unique index for current personality
275 /// function. NULL/first personality function should always get zero index.
276 unsigned MachineModuleInfo::getPersonalityIndex() const {
277   const Function* Personality = NULL;
278
279   // Scan landing pads. If there is at least one non-NULL personality - use it.
280   for (unsigned i = 0; i != LandingPads.size(); ++i)
281     if (LandingPads[i].Personality) {
282       Personality = LandingPads[i].Personality;
283       break;
284     }
285
286   for (unsigned i = 0; i < Personalities.size(); ++i) {
287     if (Personalities[i] == Personality)
288       return i;
289   }
290
291   // This will happen if the current personality function is
292   // in the zero index.
293   return 0;
294 }
295