If location info is attached with an instruction then keep track of alloca slots...
[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 MDNode;
52 class GlobalVariable;
53 class MachineBasicBlock;
54 class MachineFunction;
55 class Module;
56 class PointerType;
57 class StructType;
58   
59   
60 /// MachineModuleInfoImpl - This class can be derived from and used by targets
61 /// to hold private target-specific information for each Module.  Objects of
62 /// type are accessed/created with MMI::getInfo and destroyed when the
63 /// MachineModuleInfo is destroyed.
64 class MachineModuleInfoImpl {
65 public:
66   virtual ~MachineModuleInfoImpl();
67 };
68   
69   
70
71 //===----------------------------------------------------------------------===//
72 /// LandingPadInfo - This structure is used to retain landing pad info for
73 /// the current function.
74 ///
75 struct LandingPadInfo {
76   MachineBasicBlock *LandingPadBlock;   // Landing pad block.
77   SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
78   SmallVector<unsigned, 1> EndLabels;   // Labels after invoke.
79   unsigned LandingPadLabel;             // Label at beginning of landing pad.
80   Function *Personality;                // Personality function.
81   std::vector<int> TypeIds;             // List of type ids (filters negative)
82
83   explicit LandingPadInfo(MachineBasicBlock *MBB)
84   : LandingPadBlock(MBB)
85   , LandingPadLabel(0)
86   , Personality(NULL)  
87   {}
88 };
89
90 //===----------------------------------------------------------------------===//
91 /// MachineModuleInfo - This class contains meta information specific to a
92 /// module.  Queries can be made by different debugging and exception handling 
93 /// schemes and reformated for specific use.
94 ///
95 class MachineModuleInfo : public ImmutablePass {
96   /// ObjFileMMI - This is the object-file-format-specific implementation of
97   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
98   /// want.
99   MachineModuleInfoImpl *ObjFileMMI;
100
101   // LabelIDList - One entry per assigned label.  Normally the entry is equal to
102   // the list index(+1).  If the entry is zero then the label has been deleted.
103   // Any other value indicates the label has been deleted by is mapped to
104   // another label.
105   std::vector<unsigned> LabelIDList;
106   
107   // FrameMoves - List of moves done by a function's prolog.  Used to construct
108   // frame maps by debug and exception handling consumers.
109   std::vector<MachineMove> FrameMoves;
110   
111   // LandingPads - List of LandingPadInfo describing the landing pad information
112   // in the current function.
113   std::vector<LandingPadInfo> LandingPads;
114   
115   // TypeInfos - List of C++ TypeInfo used in the current function.
116   //
117   std::vector<GlobalVariable *> TypeInfos;
118
119   // FilterIds - List of typeids encoding filters used in the current function.
120   //
121   std::vector<unsigned> FilterIds;
122
123   // FilterEnds - List of the indices in FilterIds corresponding to filter
124   // terminators.
125   //
126   std::vector<unsigned> FilterEnds;
127
128   // Personalities - Vector of all personality functions ever seen. Used to emit
129   // common EH frames.
130   std::vector<Function *> Personalities;
131
132   /// UsedFunctions - The functions in the @llvm.used list in a more easily
133   /// searchable format.  This does not include the functions in
134   /// llvm.compiler.used.
135   SmallPtrSet<const Function *, 32> UsedFunctions;
136
137   /// UsedDbgLabels - labels are used by debug info entries.
138   SmallSet<unsigned, 8> UsedDbgLabels;
139
140   bool CallsEHReturn;
141   bool CallsUnwindInit;
142  
143   /// DbgInfoAvailable - True if debugging information is available
144   /// in this module.
145   bool DbgInfoAvailable;
146
147 public:
148   static char ID; // Pass identification, replacement for typeid
149
150   typedef DenseMap<MDNode *, std::pair<MDNode *, unsigned> > VariableDbgInfoMapTy;
151   VariableDbgInfoMapTy VariableDbgInfo;
152
153   MachineModuleInfo();
154   ~MachineModuleInfo();
155   
156   bool doInitialization();
157   bool doFinalization();
158
159   /// BeginFunction - Begin gathering function meta information.
160   ///
161   void BeginFunction(MachineFunction *) {}
162   
163   /// EndFunction - Discard function meta information.
164   ///
165   void EndFunction();
166
167   /// getInfo - Keep track of various per-function pieces of information for
168   /// backends that would like to do so.
169   ///
170   template<typename Ty>
171   Ty &getObjFileInfo() {
172     if (ObjFileMMI == 0)
173       ObjFileMMI = new Ty(*this);
174     
175     assert((void*)dynamic_cast<Ty*>(ObjFileMMI) == (void*)ObjFileMMI &&
176            "Invalid concrete type or multiple inheritence for getInfo");
177     return *static_cast<Ty*>(ObjFileMMI);
178   }
179   
180   template<typename Ty>
181   const Ty &getObjFileInfo() const {
182     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
183   }
184   
185   /// AnalyzeModule - Scan the module for global debug information.
186   ///
187   void AnalyzeModule(Module &M);
188   
189   /// hasDebugInfo - Returns true if valid debug info is present.
190   ///
191   bool hasDebugInfo() const { return DbgInfoAvailable; }
192   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
193
194   bool callsEHReturn() const { return CallsEHReturn; }
195   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
196
197   bool callsUnwindInit() const { return CallsUnwindInit; }
198   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
199   
200   /// NextLabelID - Return the next unique label id.
201   ///
202   unsigned NextLabelID() {
203     unsigned ID = (unsigned)LabelIDList.size() + 1;
204     LabelIDList.push_back(ID);
205     return ID;
206   }
207   
208   /// InvalidateLabel - Inhibit use of the specified label # from
209   /// MachineModuleInfo, for example because the code was deleted.
210   void InvalidateLabel(unsigned LabelID) {
211     // Remap to zero to indicate deletion.
212     RemapLabel(LabelID, 0);
213   }
214
215   /// RemapLabel - Indicate that a label has been merged into another.
216   ///
217   void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
218     assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
219           "Old label ID out of range.");
220     assert(NewLabelID <= LabelIDList.size() &&
221           "New label ID out of range.");
222     LabelIDList[OldLabelID - 1] = NewLabelID;
223   }
224   
225   /// MappedLabel - Find out the label's final ID.  Zero indicates deletion.
226   /// ID != Mapped ID indicates that the label was folded into another label.
227   unsigned MappedLabel(unsigned LabelID) const {
228     assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
229     return LabelID ? LabelIDList[LabelID - 1] : 0;
230   }
231
232   /// isDbgLabelUsed - Return true if label with LabelID is used by
233   /// DwarfWriter.
234   bool isDbgLabelUsed(unsigned LabelID) {
235     return UsedDbgLabels.count(LabelID);
236   }
237   
238   /// RecordUsedDbgLabel - Mark label with LabelID as used. This is used
239   /// by DwarfWriter to inform DebugLabelFolder that certain labels are
240   /// not to be deleted.
241   void RecordUsedDbgLabel(unsigned LabelID) {
242     UsedDbgLabels.insert(LabelID);
243   }
244
245   /// getFrameMoves - Returns a reference to a list of moves done in the current
246   /// function's prologue.  Used to construct frame maps for debug and exception
247   /// handling comsumers.
248   std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
249   
250   //===-EH-----------------------------------------------------------------===//
251
252   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
253   /// specified MachineBasicBlock.
254   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
255
256   /// addInvoke - Provide the begin and end labels of an invoke style call and
257   /// associate it with a try landing pad block.
258   void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
259                                                 unsigned EndLabel);
260   
261   /// addLandingPad - Add a new panding pad.  Returns the label ID for the 
262   /// landing pad entry.
263   unsigned addLandingPad(MachineBasicBlock *LandingPad);
264   
265   /// addPersonality - Provide the personality function for the exception
266   /// information.
267   void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
268
269   /// getPersonalityIndex - Get index of the current personality function inside
270   /// Personalitites array
271   unsigned getPersonalityIndex() const;
272
273   /// getPersonalities - Return array of personality functions ever seen.
274   const std::vector<Function *>& getPersonalities() const {
275     return Personalities;
276   }
277
278   /// isUsedFunction - Return true if the functions in the llvm.used list.  This
279   /// does not return true for things in llvm.compiler.used unless they are also
280   /// in llvm.used.
281   bool isUsedFunction(const Function *F) {
282     return UsedFunctions.count(F);
283   }
284
285   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
286   ///
287   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
288                         std::vector<GlobalVariable *> &TyInfo);
289
290   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
291   ///
292   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
293                          std::vector<GlobalVariable *> &TyInfo);
294
295   /// addCleanup - Add a cleanup action for a landing pad.
296   ///
297   void addCleanup(MachineBasicBlock *LandingPad);
298
299   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is 
300   /// function wide.
301   unsigned getTypeIDFor(GlobalVariable *TI);
302
303   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
304   /// function wide.
305   int getFilterIDFor(std::vector<unsigned> &TyIds);
306
307   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
308   /// pads.
309   void TidyLandingPads();
310                         
311   /// getLandingPads - Return a reference to the landing pad info for the
312   /// current function.
313   const std::vector<LandingPadInfo> &getLandingPads() const {
314     return LandingPads;
315   }
316   
317   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
318   /// function.
319   const std::vector<GlobalVariable *> &getTypeInfos() const {
320     return TypeInfos;
321   }
322
323   /// getFilterIds - Return a reference to the typeids encoding filters used in
324   /// the current function.
325   const std::vector<unsigned> &getFilterIds() const {
326     return FilterIds;
327   }
328
329   /// getPersonality - Return a personality function if available.  The presence
330   /// of one is required to emit exception handling info.
331   Function *getPersonality() const;
332
333   /// setVariableDbgInfo - Collect information used to emit debugging information
334   /// of a variable.
335   void setVariableDbgInfo(MDNode *N, MDNode *L, unsigned S) {
336     if (N && L)
337       VariableDbgInfo[N] = std::make_pair(L, S);
338   }
339
340   VariableDbgInfoMapTy &getVariableDbgInfo() {  return VariableDbgInfo;  }
341
342 }; // End class MachineModuleInfo
343
344 } // End llvm namespace
345
346 #endif