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