DebugLabelFolder ruthlessly deletes redundant labels. However, sometimes the redundan...
[oota-llvm.git] / include / llvm / CodeGen / MachineModuleInfo.h
1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- 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 // Collect meta information for a module.  This information should be in a
11 // neutral form that can be used by different debugging and exception handling
12 // schemes.
13 //
14 // The organization of information is primarily clustered around the source
15 // compile units.  The main exception is source line correspondence where
16 // inlining may interleave code from various compile units.
17 //
18 // The following information can be retrieved from the MachineModuleInfo.
19 //
20 //  -- Source directories - Directories are uniqued based on their canonical
21 //     string and assigned a sequential numeric ID (base 1.)
22 //  -- Source files - Files are also uniqued based on their name and directory
23 //     ID.  A file ID is sequential number (base 1.)
24 //  -- Source line correspondence - A vector of file ID, line#, column# triples.
25 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
26 //     corresponding to each entry in the source line list.  This allows a debug
27 //     emitter to generate labels referenced by debug information tables.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
33
34 #include "llvm/Support/Dwarf.h"
35 #include "llvm/Support/DataTypes.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/DenseMap.h"
38 #include "llvm/ADT/UniqueVector.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/SmallSet.h"
41 #include "llvm/ADT/StringMap.h"
42 #include "llvm/CodeGen/MachineLocation.h"
43 #include "llvm/GlobalValue.h"
44 #include "llvm/Pass.h"
45
46 namespace llvm {
47
48 //===----------------------------------------------------------------------===//
49 // Forward declarations.
50 class Constant;
51 class GlobalVariable;
52 class MachineBasicBlock;
53 class MachineFunction;
54 class Module;
55 class PointerType;
56 class StructType;
57
58 //===----------------------------------------------------------------------===//
59 /// LandingPadInfo - This structure is used to retain landing pad info for
60 /// the current function.
61 ///
62 struct LandingPadInfo {
63   MachineBasicBlock *LandingPadBlock;   // Landing pad block.
64   SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
65   SmallVector<unsigned, 1> EndLabels;   // Labels after invoke.
66   unsigned LandingPadLabel;             // Label at beginning of landing pad.
67   Function *Personality;                // Personality function.
68   std::vector<int> TypeIds;             // List of type ids (filters negative)
69
70   explicit LandingPadInfo(MachineBasicBlock *MBB)
71   : LandingPadBlock(MBB)
72   , LandingPadLabel(0)
73   , Personality(NULL)  
74   {}
75 };
76
77 //===----------------------------------------------------------------------===//
78 /// MachineModuleInfo - This class contains meta information specific to a
79 /// module.  Queries can be made by different debugging and exception handling 
80 /// schemes and reformated for specific use.
81 ///
82 class MachineModuleInfo : public ImmutablePass {
83 private:
84   // LabelIDList - One entry per assigned label.  Normally the entry is equal to
85   // the list index(+1).  If the entry is zero then the label has been deleted.
86   // Any other value indicates the label has been deleted by is mapped to
87   // another label.
88   std::vector<unsigned> LabelIDList;
89   
90   // FrameMoves - List of moves done by a function's prolog.  Used to construct
91   // frame maps by debug and exception handling consumers.
92   std::vector<MachineMove> FrameMoves;
93   
94   // LandingPads - List of LandingPadInfo describing the landing pad information
95   // in the current function.
96   std::vector<LandingPadInfo> LandingPads;
97   
98   // TypeInfos - List of C++ TypeInfo used in the current function.
99   //
100   std::vector<GlobalVariable *> TypeInfos;
101
102   // FilterIds - List of typeids encoding filters used in the current function.
103   //
104   std::vector<unsigned> FilterIds;
105
106   // FilterEnds - List of the indices in FilterIds corresponding to filter
107   // terminators.
108   //
109   std::vector<unsigned> FilterEnds;
110
111   // Personalities - Vector of all personality functions ever seen. Used to emit
112   // common EH frames.
113   std::vector<Function *> Personalities;
114
115   // UsedFunctions - the functions in the llvm.used list in a more easily
116   // searchable format.
117   SmallPtrSet<const Function *, 32> UsedFunctions;
118
119   /// UsedDbgLabels - labels are used by debug info entries.
120   SmallSet<unsigned, 8> UsedDbgLabels;
121
122   bool CallsEHReturn;
123   bool CallsUnwindInit;
124  
125   /// DbgInfoAvailable - True if debugging information is available
126   /// in this module.
127   bool DbgInfoAvailable;
128 public:
129   static char ID; // Pass identification, replacement for typeid
130
131   MachineModuleInfo();
132   ~MachineModuleInfo();
133   
134   /// doInitialization - Initialize the state for a new module.
135   ///
136   bool doInitialization();
137   
138   /// doFinalization - Tear down the state after completion of a module.
139   ///
140   bool doFinalization();
141   
142   /// BeginFunction - Begin gathering function meta information.
143   ///
144   void BeginFunction(MachineFunction *MF);
145   
146   /// EndFunction - Discard function meta information.
147   ///
148   void EndFunction();
149
150   /// AnalyzeModule - Scan the module for global debug information.
151   ///
152   void AnalyzeModule(Module &M);
153   
154   /// hasDebugInfo - Returns true if valid debug info is present.
155   ///
156   bool hasDebugInfo() const { return DbgInfoAvailable; }
157   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
158
159   bool callsEHReturn() const { return CallsEHReturn; }
160   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
161
162   bool callsUnwindInit() const { return CallsUnwindInit; }
163   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
164   
165   /// NextLabelID - Return the next unique label id.
166   ///
167   unsigned NextLabelID() {
168     unsigned ID = (unsigned)LabelIDList.size() + 1;
169     LabelIDList.push_back(ID);
170     return ID;
171   }
172   
173   /// RecordSourceLine - Records location information and associates it with a
174   /// label.  Returns a unique label ID used to generate a label and 
175   /// provide correspondence to the source line list.
176   unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
177   
178   /// InvalidateLabel - Inhibit use of the specified label # from
179   /// MachineModuleInfo, for example because the code was deleted.
180   void InvalidateLabel(unsigned LabelID) {
181     // Remap to zero to indicate deletion.
182     RemapLabel(LabelID, 0);
183   }
184
185   /// RemapLabel - Indicate that a label has been merged into another.
186   ///
187   void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
188     assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
189           "Old label ID out of range.");
190     assert(NewLabelID <= LabelIDList.size() &&
191           "New label ID out of range.");
192     LabelIDList[OldLabelID - 1] = NewLabelID;
193   }
194   
195   /// MappedLabel - Find out the label's final ID.  Zero indicates deletion.
196   /// ID != Mapped ID indicates that the label was folded into another label.
197   unsigned MappedLabel(unsigned LabelID) const {
198     assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
199     return LabelID ? LabelIDList[LabelID - 1] : 0;
200   }
201
202   /// isDbgLabelUsed - Return true if label with LabelID is used by
203   /// DwarfWriter.
204   bool isDbgLabelUsed(unsigned LabelID) {
205     return UsedDbgLabels.count(LabelID);
206   }
207   
208   /// RecordUsedDbgLabel - Mark label with LabelID as used. This is used
209   /// by DwarfWriter to inform DebugLabelFolder that certain labels are
210   /// not to be deleted.
211   void RecordUsedDbgLabel(unsigned LabelID) {
212     UsedDbgLabels.insert(LabelID);
213   }
214
215   /// getFrameMoves - Returns a reference to a list of moves done in the current
216   /// function's prologue.  Used to construct frame maps for debug and exception
217   /// handling comsumers.
218   std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
219   
220   //===-EH-----------------------------------------------------------------===//
221
222   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
223   /// specified MachineBasicBlock.
224   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
225
226   /// addInvoke - Provide the begin and end labels of an invoke style call and
227   /// associate it with a try landing pad block.
228   void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
229                                                 unsigned EndLabel);
230   
231   /// addLandingPad - Add a new panding pad.  Returns the label ID for the 
232   /// landing pad entry.
233   unsigned addLandingPad(MachineBasicBlock *LandingPad);
234   
235   /// addPersonality - Provide the personality function for the exception
236   /// information.
237   void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
238
239   /// getPersonalityIndex - Get index of the current personality function inside
240   /// Personalitites array
241   unsigned getPersonalityIndex() const;
242
243   /// getPersonalities - Return array of personality functions ever seen.
244   const std::vector<Function *>& getPersonalities() const {
245     return Personalities;
246   }
247
248   // UsedFunctions - Return set of the functions in the llvm.used list.
249   const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
250     return UsedFunctions;
251   }
252
253   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
254   ///
255   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
256                         std::vector<GlobalVariable *> &TyInfo);
257
258   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
259   ///
260   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
261                          std::vector<GlobalVariable *> &TyInfo);
262
263   /// addCleanup - Add a cleanup action for a landing pad.
264   ///
265   void addCleanup(MachineBasicBlock *LandingPad);
266
267   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is 
268   /// function wide.
269   unsigned getTypeIDFor(GlobalVariable *TI);
270
271   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
272   /// function wide.
273   int getFilterIDFor(std::vector<unsigned> &TyIds);
274
275   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
276   /// pads.
277   void TidyLandingPads();
278                         
279   /// getLandingPads - Return a reference to the landing pad info for the
280   /// current function.
281   const std::vector<LandingPadInfo> &getLandingPads() const {
282     return LandingPads;
283   }
284   
285   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
286   /// function.
287   const std::vector<GlobalVariable *> &getTypeInfos() const {
288     return TypeInfos;
289   }
290
291   /// getFilterIds - Return a reference to the typeids encoding filters used in
292   /// the current function.
293   const std::vector<unsigned> &getFilterIds() const {
294     return FilterIds;
295   }
296
297   /// getPersonality - Return a personality function if available.  The presence
298   /// of one is required to emit exception handling info.
299   Function *getPersonality() const;
300
301 }; // End class MachineModuleInfo
302
303 } // End llvm namespace
304
305 #endif