Initial support for carrying MachineInstrs in SUnits.
[oota-llvm.git] / include / llvm / CodeGen / MachineModuleInfo.h
index 25a74e8451711594b860adb6f41af2eacdfc61e9..46bf7904c74d214a3e55c6723133c1f3fbc1f3e0 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by James M. Laskey and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/DataTypes.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/UniqueVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/CodeGen/MachineLocation.h"
 #include "llvm/GlobalValue.h"
 #include "llvm/Pass.h"
-#include "llvm/Target/TargetOptions.h"
 
 namespace llvm {
 
@@ -45,8 +49,8 @@ namespace llvm {
 class Constant;
 class DebugInfoDesc;
 class GlobalVariable;
+class MachineBasicBlock;
 class MachineFunction;
-class MachineMove;
 class Module;
 class PointerType;
 class StructType;
@@ -96,7 +100,7 @@ private:
                                         // Dwarf writers.
   
 protected:
-  DebugInfoDesc(unsigned T) : Tag(T | LLVMDebugVersion) {}
+  explicit DebugInfoDesc(unsigned T) : Tag(T | LLVMDebugVersion) {}
   
 public:
   virtual ~DebugInfoDesc() {}
@@ -160,7 +164,7 @@ private:
   
 public:
   AnchorDesc();
-  AnchorDesc(AnchoredDesc *D);
+  explicit AnchorDesc(AnchoredDesc *D);
   
   // Accessors
   unsigned getAnchorTag() const { return AnchorTag; }
@@ -200,7 +204,7 @@ private:
 
 protected:
 
-  AnchoredDesc(unsigned T);
+  explicit AnchoredDesc(unsigned T);
 
 public:  
   // Accessors.
@@ -263,7 +267,7 @@ public:
   
   /// getAnchorString - Return a string used to label this descriptor's anchor.
   ///
-  static const char *AnchorString;
+  static const char *const AnchorString;
   virtual const char *getAnchorString() const;
     
 #ifndef NDEBUG
@@ -290,7 +294,7 @@ private:
   unsigned Flags;                       // Miscellaneous flags.
 
 public:
-  TypeDesc(unsigned T);
+  explicit TypeDesc(unsigned T);
 
   // Accessors
   DebugInfoDesc *getContext()                const { return Context; }
@@ -381,7 +385,7 @@ private:
   DebugInfoDesc *FromType;              // Type derived from.
 
 public:
-  DerivedTypeDesc(unsigned T);
+  explicit DerivedTypeDesc(unsigned T);
   
   // Accessors
   TypeDesc *getFromType() const {
@@ -420,7 +424,7 @@ private:
   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
 
 public:
-  CompositeTypeDesc(unsigned T);
+  explicit CompositeTypeDesc(unsigned T);
   
   // Accessors
   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
@@ -535,7 +539,7 @@ private:
   DebugInfoDesc *TyDesc;                // Type of variable.
 
 public:
-  VariableDesc(unsigned T);
+  explicit VariableDesc(unsigned T);
 
   // Accessors
   DebugInfoDesc *getContext()                const { return Context; }
@@ -594,7 +598,7 @@ private:
   bool IsDefinition;                    // Is the global defined in context.
   
 protected:
-  GlobalDesc(unsigned T);
+  explicit GlobalDesc(unsigned T);
 
 public:
   // Accessors
@@ -662,7 +666,7 @@ public:
   
   /// getAnchorString - Return a string used to label this descriptor's anchor.
   ///
-  static const char *AnchorString;
+  static const char *const AnchorString;
   virtual const char *getAnchorString() const;
     
 #ifndef NDEBUG
@@ -699,7 +703,7 @@ public:
   
   /// getAnchorString - Return a string used to label this descriptor's anchor.
   ///
-  static const char *AnchorString;
+  static const char *const AnchorString;
   virtual const char *getAnchorString() const;
     
 #ifndef NDEBUG
@@ -746,14 +750,13 @@ public:
 /// DIDeserializer - This class is responsible for casting GlobalVariables
 /// into DebugInfoDesc objects.
 class DIDeserializer {
-private:
-  std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
-                                        // Previously defined gloabls.
-  
+  // Previously defined gloabls.
+  std::map<GlobalVariable*, DebugInfoDesc*> GlobalDescs;
 public:
-  DIDeserializer() {}
-  ~DIDeserializer() {}
-  
+  const std::map<GlobalVariable *, DebugInfoDesc *> &getGlobalDescs() const {
+    return GlobalDescs;
+  }
+
   /// Deserialize - Reconstitute a GlobalVariable into it's component
   /// DebugInfoDesc objects.
   DebugInfoDesc *Deserialize(Value *V);
@@ -764,33 +767,29 @@ public:
 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
 /// into GlobalVariables.
 class DISerializer {
-private:
   Module *M;                            // Definition space module.
-  PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
+  PointerType *StrPtrTy;                // A "i8*" type.  Created lazily.
   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
+
+  // Types per Tag. Created lazily.
   std::map<unsigned, StructType *> TagTypes;
-                                        // Types per Tag.  Created lazily.
-  std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
-                                        // Previously defined descriptors.
-  std::map<const std::string, Constant *> StringCache;
-                                        // Previously defined strings.
-                                          
+
+  // Previously defined descriptors.
+  DenseMap<DebugInfoDesc *, GlobalVariable *> DescGlobals;
+
+  // Previously defined strings.
+  StringMap<Constant*> StringCache;
 public:
   DISerializer()
-  : M(NULL)
-  , StrPtrTy(NULL)
-  , EmptyStructPtrTy(NULL)
-  , TagTypes()
-  , DescGlobals()
-  , StringCache()
+    : M(NULL), StrPtrTy(NULL), EmptyStructPtrTy(NULL), TagTypes(),
+      DescGlobals(), StringCache()
   {}
-  ~DISerializer() {}
   
   // Accessors
   Module *getModule()        const { return M; };
   void setModule(Module *module)  { M = module; }
 
-  /// getStrPtrType - Return a "sbyte *" type.
+  /// getStrPtrType - Return a "i8*" type.
   ///
   const PointerType *getStrPtrType();
   
@@ -809,44 +808,45 @@ public:
   /// Serialize - Recursively cast the specified descriptor into a
   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
   GlobalVariable *Serialize(DebugInfoDesc *DD);
+
+  /// addDescriptor - Directly connect DD with existing GV.
+  void addDescriptor(DebugInfoDesc *DD, GlobalVariable *GV);
 };
 
 //===----------------------------------------------------------------------===//
 /// DIVerifier - This class is responsible for verifying the given network of
 /// GlobalVariables are valid as DebugInfoDesc objects.
 class DIVerifier {
-private:
   enum {
     Unknown = 0,
     Invalid,
     Valid
   };
-  std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
-  std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
-  
+  DenseMap<GlobalVariable *, unsigned> Validity; // Tracks prior results.
+  std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
 public:
   DIVerifier()
-  : Validity()
-  , Counts()
+    : Validity(), Counts()
   {}
-  ~DIVerifier() {}
   
   /// Verify - Return true if the GlobalVariable appears to be a valid
   /// serialization of a DebugInfoDesc.
   bool Verify(Value *V);
   bool Verify(GlobalVariable *GV);
+
+  /// isVerified - Return true if the specified GV has already been
+  /// verified as a debug information descriptor.
+  bool isVerified(GlobalVariable *GV);
 };
 
 //===----------------------------------------------------------------------===//
 /// SourceLineInfo - This class is used to record source line correspondence.
 ///
 class SourceLineInfo {
-private:
   unsigned Line;                        // Source line number.
   unsigned Column;                      // Source column.
   unsigned SourceID;                    // Source ID number.
   unsigned LabelID;                     // Label in code ID number.
-
 public:
   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
   : Line(L), Column(C), SourceID(S), LabelID(I) {}
@@ -862,10 +862,8 @@ public:
 /// SourceFileInfo - This class is used to track source information.
 ///
 class SourceFileInfo {
-private:
   unsigned DirectoryID;                 // Directory ID number.
   std::string Name;                     // File name (not including directory.)
-  
 public:
   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
             
@@ -949,6 +947,25 @@ public:
   void AddVariable(DebugVariable *V) { Variables.push_back(V); }
 };
 
+//===----------------------------------------------------------------------===//
+/// LandingPadInfo - This structure is used to retain landing pad info for
+/// the current function.
+///
+struct LandingPadInfo {
+  MachineBasicBlock *LandingPadBlock;   // Landing pad block.
+  SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
+  SmallVector<unsigned, 1> EndLabels;   // Labels after invoke.
+  unsigned LandingPadLabel;             // Label at beginning of landing pad.
+  Function *Personality;                // Personality function.
+  std::vector<int> TypeIds;             // List of type ids (filters negative)
+
+  explicit LandingPadInfo(MachineBasicBlock *MBB)
+  : LandingPadBlock(MBB)
+  , LandingPadLabel(0)
+  , Personality(NULL)  
+  {}
+};
+
 //===----------------------------------------------------------------------===//
 /// MachineModuleInfo - This class contains meta information specific to a
 /// module.  Queries can be made by different debugging and exception handling 
@@ -988,8 +1005,37 @@ private:
   // FrameMoves - List of moves done by a function's prolog.  Used to construct
   // frame maps by debug and exception handling consumers.
   std::vector<MachineMove> FrameMoves;
+  
+  // LandingPads - List of LandingPadInfo describing the landing pad information
+  // in the current function.
+  std::vector<LandingPadInfo> LandingPads;
+  
+  // TypeInfos - List of C++ TypeInfo used in the current function.
+  //
+  std::vector<GlobalVariable *> TypeInfos;
 
+  // FilterIds - List of typeids encoding filters used in the current function.
+  //
+  std::vector<unsigned> FilterIds;
+
+  // FilterEnds - List of the indices in FilterIds corresponding to filter
+  // terminators.
+  //
+  std::vector<unsigned> FilterEnds;
+
+  // Personalities - Vector of all personality functions ever seen. Used to emit
+  // common EH frames.
+  std::vector<Function *> Personalities;
+
+  // UsedFunctions - the functions in the llvm.used list in a more easily
+  // searchable format.
+  SmallPtrSet<const Function *, 32> UsedFunctions;
+
+  bool CallsEHReturn;
+  bool CallsUnwindInit;
 public:
+  static char ID; // Pass identification, replacement for typeid
+
   MachineModuleInfo();
   ~MachineModuleInfo();
   
@@ -1016,7 +1062,11 @@ public:
   
   /// Verify - Verify that a Value is debug information descriptor.
   ///
-  bool Verify(Value *V);
+  bool Verify(Value *V) { return VR.Verify(V); }
+
+  /// isVerified - Return true if the specified GV has already been
+  /// verified as a debug information descriptor.
+  bool isVerified(GlobalVariable *GV) { return VR.isVerified(GV); }
   
   /// AnalyzeModule - Scan the module for global debug information.
   ///
@@ -1026,22 +1076,24 @@ public:
   ///
   bool hasDebugInfo() const { return !CompileUnits.empty(); }
   
-  /// needsFrameInfo - Returns true if we need to gather callee-saved register
-  /// move info for the frame.
-  bool needsFrameInfo() const { return hasDebugInfo() || ExceptionHandling; }
+  bool callsEHReturn() const { return CallsEHReturn; }
+  void setCallsEHReturn(bool b) { CallsEHReturn = b; }
+
+  bool callsUnwindInit() const { return CallsUnwindInit; }
+  void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
   
   /// NextLabelID - Return the next unique label id.
   ///
   unsigned NextLabelID() {
-    unsigned ID = LabelIDList.size() + 1;
+    unsigned ID = (unsigned)LabelIDList.size() + 1;
     LabelIDList.push_back(ID);
     return ID;
   }
   
-  /// RecordLabel - Records location information and associates it with a
+  /// RecordSourceLine - Records location information and associates it with a
   /// label.  Returns a unique label ID used to generate a label and 
   /// provide correspondence to the source line list.
-  unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source);
+  unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
   
   /// InvalidateLabel - Inhibit use of the specified label # from
   /// MachineModuleInfo, for example because the code was deleted.
@@ -1091,11 +1143,6 @@ public:
     return Lines;
   }
   
-  // FIXME: nuke this.
-  void ClearLineInfo() {
-    Lines.clear();
-  }
-  
   /// SetupCompileUnits - Set up the unique vector of compile units.
   ///
   void SetupCompileUnits(Module &M);
@@ -1106,27 +1153,26 @@ public:
   
   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
   /// named GlobalVariable.
-  std::vector<GlobalVariable*>
-  getGlobalVariablesUsing(Module &M, const std::string &RootName);
+  void getGlobalVariablesUsing(Module &M, const std::string &RootName,
+                               std::vector<GlobalVariable*> &Result);
 
   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
   ///
-  template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
+  template <class T>
+  void getAnchoredDescriptors(Module &M, std::vector<T*> &AnchoredDescs) {
     T Desc;
-    std::vector<GlobalVariable *> Globals =
-                             getGlobalVariablesUsing(M, Desc.getAnchorString());
-    std::vector<T *> AnchoredDescs;
+    std::vector<GlobalVariable *> Globals;
+    getGlobalVariablesUsing(M, Desc.getAnchorString(), Globals);
+
     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
       GlobalVariable *GV = Globals[i];
-      
+
       // FIXME - In the short term, changes are too drastic to continue.
       if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() &&
           DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) {
         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
       }
     }
-
-    return AnchoredDescs;
   }
   
   /// RecordRegionStart - Indicate the start of a region.
@@ -1139,7 +1185,7 @@ public:
 
   /// RecordVariable - Indicate the declaration of  a local variable.
   ///
-  void RecordVariable(Value *V, unsigned FrameIndex);
+  void RecordVariable(GlobalValue *GV, unsigned FrameIndex);
   
   /// getRootScope - Return current functions root scope.
   ///
@@ -1153,7 +1199,89 @@ public:
   /// function's prologue.  Used to construct frame maps for debug and exception
   /// handling comsumers.
   std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
+  
+  //===-EH-----------------------------------------------------------------===//
+
+  /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
+  /// specified MachineBasicBlock.
+  LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
+
+  /// addInvoke - Provide the begin and end labels of an invoke style call and
+  /// associate it with a try landing pad block.
+  void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
+                                                unsigned EndLabel);
+  
+  /// addLandingPad - Add a new panding pad.  Returns the label ID for the 
+  /// landing pad entry.
+  unsigned addLandingPad(MachineBasicBlock *LandingPad);
+  
+  /// addPersonality - Provide the personality function for the exception
+  /// information.
+  void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
+
+  /// getPersonalityIndex - Get index of the current personality function inside
+  /// Personalitites array
+  unsigned getPersonalityIndex() const;
+
+  /// getPersonalities - Return array of personality functions ever seen.
+  const std::vector<Function *>& getPersonalities() const {
+    return Personalities;
+  }
+
+  // UsedFunctions - Return set of the functions in the llvm.used list.
+  const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
+    return UsedFunctions;
+  }
+
+  /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
+  ///
+  void addCatchTypeInfo(MachineBasicBlock *LandingPad,
+                        std::vector<GlobalVariable *> &TyInfo);
+
+  /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
+  ///
+  void addFilterTypeInfo(MachineBasicBlock *LandingPad,
+                         std::vector<GlobalVariable *> &TyInfo);
+
+  /// addCleanup - Add a cleanup action for a landing pad.
+  ///
+  void addCleanup(MachineBasicBlock *LandingPad);
+
+  /// getTypeIDFor - Return the type id for the specified typeinfo.  This is 
+  /// function wide.
+  unsigned getTypeIDFor(GlobalVariable *TI);
+
+  /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
+  /// function wide.
+  int getFilterIDFor(std::vector<unsigned> &TyIds);
+
+  /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
+  /// pads.
+  void TidyLandingPads();
+                        
+  /// getLandingPads - Return a reference to the landing pad info for the
+  /// current function.
+  const std::vector<LandingPadInfo> &getLandingPads() const {
+    return LandingPads;
+  }
+  
+  /// getTypeInfos - Return a reference to the C++ typeinfo for the current
+  /// function.
+  const std::vector<GlobalVariable *> &getTypeInfos() const {
+    return TypeInfos;
+  }
+
+  /// getFilterIds - Return a reference to the typeids encoding filters used in
+  /// the current function.
+  const std::vector<unsigned> &getFilterIds() const {
+    return FilterIds;
+  }
+
+  /// getPersonality - Return a personality function if available.  The presence
+  /// of one is required to emit exception handling info.
+  Function *getPersonality() const;
 
+  DIDeserializer *getDIDeserializer() { return &DR; }
 }; // End class MachineModuleInfo
 
 } // End llvm namespace