rearrange MCContext ownership. Before LLVMTargetMachine created it
[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", "Machine Module Information");
33 char MachineModuleInfo::ID = 0;
34
35 // Out of line virtual method.
36 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
37
38 //===----------------------------------------------------------------------===//
39
40 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI)
41 : ImmutablePass(&ID), Context(MAI),
42   ObjFileMMI(0), CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0),
43   DbgInfoAvailable(false) {
44   // Always emit some info, by default "no personality" info.
45   Personalities.push_back(NULL);
46 }
47
48 MachineModuleInfo::MachineModuleInfo()
49 : ImmutablePass(&ID), Context(*(MCAsmInfo*)0) {
50   assert(0 && "This MachineModuleInfo constructor should never be called, MMI "
51          "should always be explicitly constructed by LLVMTargetMachine");
52   abort();
53 }
54
55 MachineModuleInfo::~MachineModuleInfo() {
56   delete ObjFileMMI;
57 }
58
59 /// doInitialization - Initialize the state for a new module.
60 ///
61 bool MachineModuleInfo::doInitialization() {
62   return false;
63 }
64
65 /// doFinalization - Tear down the state after completion of a module.
66 ///
67 bool MachineModuleInfo::doFinalization() {
68   return false;
69 }
70
71 /// EndFunction - Discard function meta information.
72 ///
73 void MachineModuleInfo::EndFunction() {
74   // Clean up frame info.
75   FrameMoves.clear();
76
77   // Clean up exception info.
78   LandingPads.clear();
79   CallSiteMap.clear();
80   TypeInfos.clear();
81   FilterIds.clear();
82   FilterEnds.clear();
83   CallsEHReturn = 0;
84   CallsUnwindInit = 0;
85   VariableDbgInfo.clear();
86 }
87
88 /// AnalyzeModule - Scan the module for global debug information.
89 ///
90 void MachineModuleInfo::AnalyzeModule(Module &M) {
91   // Insert functions in the llvm.used array (but not llvm.compiler.used) into
92   // UsedFunctions.
93   GlobalVariable *GV = M.getGlobalVariable("llvm.used");
94   if (!GV || !GV->hasInitializer()) return;
95
96   // Should be an array of 'i8*'.
97   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
98   if (InitList == 0) return;
99
100   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
101     if (Function *F =
102           dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
103       UsedFunctions.insert(F);
104 }
105
106 //===-EH-------------------------------------------------------------------===//
107
108 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
109 /// specified MachineBasicBlock.
110 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
111     (MachineBasicBlock *LandingPad) {
112   unsigned N = LandingPads.size();
113   for (unsigned i = 0; i < N; ++i) {
114     LandingPadInfo &LP = LandingPads[i];
115     if (LP.LandingPadBlock == LandingPad)
116       return LP;
117   }
118
119   LandingPads.push_back(LandingPadInfo(LandingPad));
120   return LandingPads[N];
121 }
122
123 /// addInvoke - Provide the begin and end labels of an invoke style call and
124 /// associate it with a try landing pad block.
125 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
126                                   unsigned BeginLabel, unsigned EndLabel) {
127   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
128   LP.BeginLabels.push_back(BeginLabel);
129   LP.EndLabels.push_back(EndLabel);
130 }
131
132 /// addLandingPad - Provide the label of a try LandingPad block.
133 ///
134 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
135   unsigned LandingPadLabel = NextLabelID();
136   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
137   LP.LandingPadLabel = LandingPadLabel;
138   return LandingPadLabel;
139 }
140
141 /// addPersonality - Provide the personality function for the exception
142 /// information.
143 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
144                                        Function *Personality) {
145   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
146   LP.Personality = Personality;
147
148   for (unsigned i = 0; i < Personalities.size(); ++i)
149     if (Personalities[i] == Personality)
150       return;
151
152   // If this is the first personality we're adding go
153   // ahead and add it at the beginning.
154   if (Personalities[0] == NULL)
155     Personalities[0] = Personality;
156   else
157     Personalities.push_back(Personality);
158 }
159
160 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
161 ///
162 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
163                                         std::vector<GlobalVariable *> &TyInfo) {
164   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
165   for (unsigned N = TyInfo.size(); N; --N)
166     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
167 }
168
169 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
170 ///
171 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
172                                         std::vector<GlobalVariable *> &TyInfo) {
173   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
174   std::vector<unsigned> IdsInFilter(TyInfo.size());
175   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
176     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
177   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
178 }
179
180 /// addCleanup - Add a cleanup action for a landing pad.
181 ///
182 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
183   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
184   LP.TypeIds.push_back(0);
185 }
186
187 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
188 /// pads.
189 void MachineModuleInfo::TidyLandingPads() {
190   for (unsigned i = 0; i != LandingPads.size(); ) {
191     LandingPadInfo &LandingPad = LandingPads[i];
192     if (isLabelDeleted(LandingPad.LandingPadLabel))
193       LandingPad.LandingPadLabel = 0;
194
195     // Special case: we *should* emit LPs with null LP MBB. This indicates
196     // "nounwind" case.
197     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
198       LandingPads.erase(LandingPads.begin() + i);
199       continue;
200     }
201
202     for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
203       unsigned BeginLabel = LandingPad.BeginLabels[j];
204       unsigned EndLabel = LandingPad.EndLabels[j];
205       if (isLabelDeleted(BeginLabel) || isLabelDeleted(EndLabel)) {
206         LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
207         LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
208         continue;
209       }
210
211       ++j;
212     }
213
214     // Remove landing pads with no try-ranges.
215     if (LandingPads[i].BeginLabels.empty()) {
216       LandingPads.erase(LandingPads.begin() + i);
217       continue;
218     }
219
220     // If there is no landing pad, ensure that the list of typeids is empty.
221     // If the only typeid is a cleanup, this is the same as having no typeids.
222     if (!LandingPad.LandingPadBlock ||
223         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
224       LandingPad.TypeIds.clear();
225
226     ++i;
227   }
228 }
229
230 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
231 /// function wide.
232 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
233   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
234     if (TypeInfos[i] == TI) return i + 1;
235
236   TypeInfos.push_back(TI);
237   return TypeInfos.size();
238 }
239
240 /// getFilterIDFor - Return the filter id for the specified typeinfos.  This is
241 /// function wide.
242 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
243   // If the new filter coincides with the tail of an existing filter, then
244   // re-use the existing filter.  Folding filters more than this requires
245   // re-ordering filters and/or their elements - probably not worth it.
246   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
247        E = FilterEnds.end(); I != E; ++I) {
248     unsigned i = *I, j = TyIds.size();
249
250     while (i && j)
251       if (FilterIds[--i] != TyIds[--j])
252         goto try_next;
253
254     if (!j)
255       // The new filter coincides with range [i, end) of the existing filter.
256       return -(1 + i);
257
258 try_next:;
259   }
260
261   // Add the new filter.
262   int FilterID = -(1 + FilterIds.size());
263   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
264   for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
265     FilterIds.push_back(TyIds[I]);
266   FilterEnds.push_back(FilterIds.size());
267   FilterIds.push_back(0); // terminator
268   return FilterID;
269 }
270
271 /// getPersonality - Return the personality function for the current function.
272 Function *MachineModuleInfo::getPersonality() const {
273   // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
274   // function
275   return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
276 }
277
278 /// getPersonalityIndex - Return unique index for current personality
279 /// function. NULL/first personality function should always get zero index.
280 unsigned MachineModuleInfo::getPersonalityIndex() const {
281   const Function* Personality = NULL;
282
283   // Scan landing pads. If there is at least one non-NULL personality - use it.
284   for (unsigned i = 0; i != LandingPads.size(); ++i)
285     if (LandingPads[i].Personality) {
286       Personality = LandingPads[i].Personality;
287       break;
288     }
289
290   for (unsigned i = 0; i < Personalities.size(); ++i) {
291     if (Personalities[i] == Personality)
292       return i;
293   }
294
295   // This will happen if the current personality function is
296   // in the zero index.
297   return 0;
298 }
299