Preliminary patch to improve dwarf EH generation - Hooks to return Personality /...
[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/System/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 #include "llvm/Metadata.h"
46 #include "llvm/Support/ValueHandle.h"
47
48 namespace llvm {
49
50 //===----------------------------------------------------------------------===//
51 // Forward declarations.
52 class Constant;
53 class MCSymbol;
54 class MDNode;
55 class GlobalVariable;
56 class MachineBasicBlock;
57 class MachineFunction;
58 class Module;
59 class PointerType;
60 class StructType;
61   
62   
63 /// MachineModuleInfoImpl - This class can be derived from and used by targets
64 /// to hold private target-specific information for each Module.  Objects of
65 /// type are accessed/created with MMI::getInfo and destroyed when the
66 /// MachineModuleInfo is destroyed.
67 class MachineModuleInfoImpl {
68 public:
69   virtual ~MachineModuleInfoImpl();
70
71   typedef std::vector<std::pair<MCSymbol*, MCSymbol*> >
72       SymbolListTy;
73 protected:
74     static SymbolListTy
75     GetSortedStubs(const DenseMap<MCSymbol*, MCSymbol*> &Map);
76 };
77   
78   
79
80 //===----------------------------------------------------------------------===//
81 /// LandingPadInfo - This structure is used to retain landing pad info for
82 /// the current function.
83 ///
84 struct LandingPadInfo {
85   MachineBasicBlock *LandingPadBlock;   // Landing pad block.
86   SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
87   SmallVector<unsigned, 1> EndLabels;   // Labels after invoke.
88   unsigned LandingPadLabel;             // Label at beginning of landing pad.
89   Function *Personality;                // Personality function.
90   std::vector<int> TypeIds;             // List of type ids (filters negative)
91
92   explicit LandingPadInfo(MachineBasicBlock *MBB)
93   : LandingPadBlock(MBB)
94   , LandingPadLabel(0)
95   , Personality(NULL)  
96   {}
97 };
98
99 //===----------------------------------------------------------------------===//
100 /// MachineModuleInfo - This class contains meta information specific to a
101 /// module.  Queries can be made by different debugging and exception handling 
102 /// schemes and reformated for specific use.
103 ///
104 class MachineModuleInfo : public ImmutablePass {
105   /// ObjFileMMI - This is the object-file-format-specific implementation of
106   /// MachineModuleInfoImpl, which lets targets accumulate whatever info they
107   /// want.
108   MachineModuleInfoImpl *ObjFileMMI;
109
110   // LabelIDList - One entry per assigned label.  Normally the entry is equal to
111   // the list index(+1).  If the entry is zero then the label has been deleted.
112   // Any other value indicates the label has been deleted by is mapped to
113   // another label.
114   std::vector<unsigned> LabelIDList;
115   
116   // FrameMoves - List of moves done by a function's prolog.  Used to construct
117   // frame maps by debug and exception handling consumers.
118   std::vector<MachineMove> FrameMoves;
119   
120   // LandingPads - List of LandingPadInfo describing the landing pad information
121   // in the current function.
122   std::vector<LandingPadInfo> LandingPads;
123
124   // Map of invoke call site index values to associated begin EH_LABEL for
125   // the current function.
126   DenseMap<unsigned, unsigned> CallSiteMap;
127
128   // The current call site index being processed, if any. 0 if none.
129   unsigned CurCallSite;
130
131   // TypeInfos - List of C++ TypeInfo used in the current function.
132   //
133   std::vector<GlobalVariable *> TypeInfos;
134
135   // FilterIds - List of typeids encoding filters used in the current function.
136   //
137   std::vector<unsigned> FilterIds;
138
139   // FilterEnds - List of the indices in FilterIds corresponding to filter
140   // terminators.
141   //
142   std::vector<unsigned> FilterEnds;
143
144   // Personalities - Vector of all personality functions ever seen. Used to emit
145   // common EH frames.
146   std::vector<Function *> Personalities;
147
148   /// UsedFunctions - The functions in the @llvm.used list in a more easily
149   /// searchable format.  This does not include the functions in
150   /// llvm.compiler.used.
151   SmallPtrSet<const Function *, 32> UsedFunctions;
152
153   bool CallsEHReturn;
154   bool CallsUnwindInit;
155  
156   /// DbgInfoAvailable - True if debugging information is available
157   /// in this module.
158   bool DbgInfoAvailable;
159
160 public:
161   static char ID; // Pass identification, replacement for typeid
162
163   typedef std::pair<unsigned, TrackingVH<MDNode> > UnsignedAndMDNodePair;
164   typedef SmallVector< std::pair<TrackingVH<MDNode>, UnsignedAndMDNodePair>, 4>
165     VariableDbgInfoMapTy;
166   VariableDbgInfoMapTy VariableDbgInfo;
167
168   MachineModuleInfo();
169   ~MachineModuleInfo();
170   
171   bool doInitialization();
172   bool doFinalization();
173
174   /// EndFunction - Discard function meta information.
175   ///
176   void EndFunction();
177
178   /// getInfo - Keep track of various per-function pieces of information for
179   /// backends that would like to do so.
180   ///
181   template<typename Ty>
182   Ty &getObjFileInfo() {
183     if (ObjFileMMI == 0)
184       ObjFileMMI = new Ty(*this);
185     return *static_cast<Ty*>(ObjFileMMI);
186   }
187   
188   template<typename Ty>
189   const Ty &getObjFileInfo() const {
190     return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
191   }
192   
193   /// AnalyzeModule - Scan the module for global debug information.
194   ///
195   void AnalyzeModule(Module &M);
196   
197   /// hasDebugInfo - Returns true if valid debug info is present.
198   ///
199   bool hasDebugInfo() const { return DbgInfoAvailable; }
200   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
201
202   bool callsEHReturn() const { return CallsEHReturn; }
203   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
204
205   bool callsUnwindInit() const { return CallsUnwindInit; }
206   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
207   
208   /// NextLabelID - Return the next unique label id.
209   ///
210   unsigned NextLabelID() {
211     unsigned ID = (unsigned)LabelIDList.size() + 1;
212     LabelIDList.push_back(ID);
213     return ID;
214   }
215   
216   /// InvalidateLabel - Inhibit use of the specified label # from
217   /// MachineModuleInfo, for example because the code was deleted.
218   void InvalidateLabel(unsigned LabelID) {
219     // Remap to zero to indicate deletion.
220     RemapLabel(LabelID, 0);
221   }
222
223   /// RemapLabel - Indicate that a label has been merged into another.
224   ///
225   void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
226     assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
227           "Old label ID out of range.");
228     assert(NewLabelID <= LabelIDList.size() &&
229           "New label ID out of range.");
230     LabelIDList[OldLabelID - 1] = NewLabelID;
231   }
232   
233   /// MappedLabel - Find out the label's final ID.  Zero indicates deletion.
234   /// ID != Mapped ID indicates that the label was folded into another label.
235   unsigned MappedLabel(unsigned LabelID) const {
236     assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
237     return LabelID ? LabelIDList[LabelID - 1] : 0;
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   /// setCallSiteBeginLabel - Map the begin label for a call site
313   void setCallSiteBeginLabel(unsigned BeginLabel, unsigned Site) {
314     CallSiteMap[BeginLabel] = Site;
315   }
316
317   /// getCallSiteBeginLabel - Get the call site number for a begin label
318   unsigned getCallSiteBeginLabel(unsigned BeginLabel) {
319     assert(CallSiteMap.count(BeginLabel) &&
320            "Missing call site number for EH_LABEL!");
321     return CallSiteMap[BeginLabel];
322   }
323
324   /// setCurrentCallSite - Set the call site currently being processed.
325   void setCurrentCallSite(unsigned Site) { CurCallSite = Site; }
326
327   /// getCurrentCallSite - Get the call site currently being processed, if any.
328   /// return zero if none.
329   unsigned getCurrentCallSite(void) { return CurCallSite; }
330
331   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
332   /// function.
333   const std::vector<GlobalVariable *> &getTypeInfos() const {
334     return TypeInfos;
335   }
336
337   /// getFilterIds - Return a reference to the typeids encoding filters used in
338   /// the current function.
339   const std::vector<unsigned> &getFilterIds() const {
340     return FilterIds;
341   }
342
343   /// getPersonality - Return a personality function if available.  The presence
344   /// of one is required to emit exception handling info.
345   Function *getPersonality() const;
346
347   /// setVariableDbgInfo - Collect information used to emit debugging information
348   /// of a variable.
349   void setVariableDbgInfo(MDNode *N, unsigned Slot, MDNode *Scope) {
350     VariableDbgInfo.push_back(std::make_pair(N, std::make_pair(Slot, Scope)));
351   }
352
353   VariableDbgInfoMapTy &getVariableDbgInfo() {  return VariableDbgInfo;  }
354
355 }; // End class MachineModuleInfo
356
357 } // End llvm namespace
358
359 #endif