Clear variable debug info map at the end of the function.
[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 , CallsEHReturn(0)
44 , CallsUnwindInit(0)
45 , DbgInfoAvailable(false) {
46   // Always emit some info, by default "no personality" info.
47   Personalities.push_back(NULL);
48 }
49
50 MachineModuleInfo::~MachineModuleInfo() {
51   delete ObjFileMMI;
52 }
53
54 /// doInitialization - Initialize the state for a new module.
55 ///
56 bool MachineModuleInfo::doInitialization() {
57   return false;
58 }
59
60 /// doFinalization - Tear down the state after completion of a module.
61 ///
62 bool MachineModuleInfo::doFinalization() {
63   return false;
64 }
65
66 /// EndFunction - Discard function meta information.
67 ///
68 void MachineModuleInfo::EndFunction() {
69   // Clean up frame info.
70   FrameMoves.clear();
71
72   // Clean up exception info.
73   LandingPads.clear();
74   TypeInfos.clear();
75   FilterIds.clear();
76   FilterEnds.clear();
77   CallsEHReturn = 0;
78   CallsUnwindInit = 0;
79 #ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
80   VariableDbgInfo.clear();
81 #endif
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     LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
189
190     // Special case: we *should* emit LPs with null LP MBB. This indicates
191     // "nounwind" case.
192     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
193       LandingPads.erase(LandingPads.begin() + i);
194       continue;
195     }
196
197     for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
198       unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
199       unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
200
201       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     }
211
212     // Remove landing pads with no try-ranges.
213     if (LandingPads[i].BeginLabels.empty()) {
214       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
224     ++i;
225   }
226 }
227
228 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
229 /// function wide.
230 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
231   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
232     if (TypeInfos[i] == TI) return i + 1;
233
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.
240 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
241   // 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) {
246     unsigned i = *I, j = TyIds.size();
247
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);
255
256 try_next:;
257   }
258
259   // Add the new filter.
260   int FilterID = -(1 + FilterIds.size());
261   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
262   for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
263     FilterIds.push_back(TyIds[I]);
264   FilterEnds.push_back(FilterIds.size());
265   FilterIds.push_back(0); // terminator
266   return FilterID;
267 }
268
269 /// getPersonality - Return the personality function for the current function.
270 Function *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
277 /// function. NULL/first personality function should always get zero index.
278 unsigned MachineModuleInfo::getPersonalityIndex() const {
279   const Function* Personality = NULL;
280
281   // Scan landing pads. If there is at least one non-NULL personality - use it.
282   for (unsigned i = 0; i != LandingPads.size(); ++i)
283     if (LandingPads[i].Personality) {
284       Personality = LandingPads[i].Personality;
285       break;
286     }
287
288   for (unsigned i = 0; i < Personalities.size(); ++i) {
289     if (Personalities[i] == Personality)
290       return i;
291   }
292
293   // This will happen if the current personality function is
294   // in the zero index.
295   return 0;
296 }
297
298 //===----------------------------------------------------------------------===//
299 /// DebugLabelFolding pass - This pass prunes out redundant labels.  This allows
300 /// a info consumer to determine if the range of two labels is empty, by seeing
301 /// if the labels map to the same reduced label.
302
303 namespace llvm {
304
305 struct DebugLabelFolder : public MachineFunctionPass {
306   static char ID;
307   DebugLabelFolder() : MachineFunctionPass(&ID) {}
308
309   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
310     AU.setPreservesCFG();
311     AU.addPreservedID(MachineLoopInfoID);
312     AU.addPreservedID(MachineDominatorsID);
313     MachineFunctionPass::getAnalysisUsage(AU);
314   }
315
316   virtual bool runOnMachineFunction(MachineFunction &MF);
317   virtual const char *getPassName() const { return "Label Folder"; }
318 };
319
320 char DebugLabelFolder::ID = 0;
321
322 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
323   // Get machine module info.
324   MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
325   if (!MMI) return false;
326
327   // Track if change is made.
328   bool MadeChange = false;
329   // No prior label to begin.
330   unsigned PriorLabel = 0;
331
332   // Iterate through basic blocks.
333   for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
334        BB != E; ++BB) {
335     // Iterate through instructions.
336     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
337       // Is it a label.
338       if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
339         // The label ID # is always operand #0, an immediate.
340         unsigned NextLabel = I->getOperand(0).getImm();
341
342         // If there was an immediate prior label.
343         if (PriorLabel) {
344           // Remap the current label to prior label.
345           MMI->RemapLabel(NextLabel, PriorLabel);
346           // Delete the current label.
347           I = BB->erase(I);
348           // Indicate a change has been made.
349           MadeChange = true;
350           continue;
351         } else {
352           // Start a new round.
353           PriorLabel = NextLabel;
354         }
355        } else {
356         // No consecutive labels.
357         PriorLabel = 0;
358       }
359
360       ++I;
361     }
362   }
363
364   return MadeChange;
365 }
366
367 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
368
369 }