LLVM API Documentation

MachineModuleInfo.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Collect meta information for a module.  This information should be in a
00011 // neutral form that can be used by different debugging and exception handling
00012 // schemes.
00013 //
00014 // The organization of information is primarily clustered around the source
00015 // compile units.  The main exception is source line correspondence where
00016 // inlining may interleave code from various compile units.
00017 //
00018 // The following information can be retrieved from the MachineModuleInfo.
00019 //
00020 //  -- Source directories - Directories are uniqued based on their canonical
00021 //     string and assigned a sequential numeric ID (base 1.)
00022 //  -- Source files - Files are also uniqued based on their name and directory
00023 //     ID.  A file ID is sequential number (base 1.)
00024 //  -- Source line correspondence - A vector of file ID, line#, column# triples.
00025 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
00026 //     corresponding to each entry in the source line list.  This allows a debug
00027 //     emitter to generate labels referenced by debug information tables.
00028 //
00029 //===----------------------------------------------------------------------===//
00030 
00031 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
00032 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
00033 
00034 #include "llvm/Support/Dwarf.h"
00035 #include "llvm/Support/DataTypes.h"
00036 #include "llvm/ADT/SmallVector.h"
00037 #include "llvm/ADT/DenseMap.h"
00038 #include "llvm/ADT/UniqueVector.h"
00039 #include "llvm/ADT/SmallPtrSet.h"
00040 #include "llvm/ADT/StringMap.h"
00041 #include "llvm/CodeGen/MachineLocation.h"
00042 #include "llvm/GlobalValue.h"
00043 #include "llvm/Pass.h"
00044 
00045 namespace llvm {
00046 
00047 //===----------------------------------------------------------------------===//
00048 // Forward declarations.
00049 class Constant;
00050 class GlobalVariable;
00051 class MachineBasicBlock;
00052 class MachineFunction;
00053 class Module;
00054 class PointerType;
00055 class StructType;
00056 
00057 //===----------------------------------------------------------------------===//
00058 /// SourceLineInfo - This class is used to record source line correspondence.
00059 ///
00060 class SourceLineInfo {
00061   unsigned Line;                        // Source line number.
00062   unsigned Column;                      // Source column.
00063   unsigned SourceID;                    // Source ID number.
00064   unsigned LabelID;                     // Label in code ID number.
00065 public:
00066   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
00067   : Line(L), Column(C), SourceID(S), LabelID(I) {}
00068   
00069   // Accessors
00070   unsigned getLine()     const { return Line; }
00071   unsigned getColumn()   const { return Column; }
00072   unsigned getSourceID() const { return SourceID; }
00073   unsigned getLabelID()  const { return LabelID; }
00074 };
00075 
00076 //===----------------------------------------------------------------------===//
00077 /// LandingPadInfo - This structure is used to retain landing pad info for
00078 /// the current function.
00079 ///
00080 struct LandingPadInfo {
00081   MachineBasicBlock *LandingPadBlock;   // Landing pad block.
00082   SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
00083   SmallVector<unsigned, 1> EndLabels;   // Labels after invoke.
00084   unsigned LandingPadLabel;             // Label at beginning of landing pad.
00085   Function *Personality;                // Personality function.
00086   std::vector<int> TypeIds;             // List of type ids (filters negative)
00087 
00088   explicit LandingPadInfo(MachineBasicBlock *MBB)
00089   : LandingPadBlock(MBB)
00090   , LandingPadLabel(0)
00091   , Personality(NULL)  
00092   {}
00093 };
00094 
00095 //===----------------------------------------------------------------------===//
00096 /// MachineModuleInfo - This class contains meta information specific to a
00097 /// module.  Queries can be made by different debugging and exception handling 
00098 /// schemes and reformated for specific use.
00099 ///
00100 class MachineModuleInfo : public ImmutablePass {
00101 private:
00102   // Lines - List of of source line correspondence.
00103   std::vector<SourceLineInfo> Lines;
00104   
00105   // LabelIDList - One entry per assigned label.  Normally the entry is equal to
00106   // the list index(+1).  If the entry is zero then the label has been deleted.
00107   // Any other value indicates the label has been deleted by is mapped to
00108   // another label.
00109   std::vector<unsigned> LabelIDList;
00110   
00111   // FrameMoves - List of moves done by a function's prolog.  Used to construct
00112   // frame maps by debug and exception handling consumers.
00113   std::vector<MachineMove> FrameMoves;
00114   
00115   // LandingPads - List of LandingPadInfo describing the landing pad information
00116   // in the current function.
00117   std::vector<LandingPadInfo> LandingPads;
00118   
00119   // TypeInfos - List of C++ TypeInfo used in the current function.
00120   //
00121   std::vector<GlobalVariable *> TypeInfos;
00122 
00123   // FilterIds - List of typeids encoding filters used in the current function.
00124   //
00125   std::vector<unsigned> FilterIds;
00126 
00127   // FilterEnds - List of the indices in FilterIds corresponding to filter
00128   // terminators.
00129   //
00130   std::vector<unsigned> FilterEnds;
00131 
00132   // Personalities - Vector of all personality functions ever seen. Used to emit
00133   // common EH frames.
00134   std::vector<Function *> Personalities;
00135 
00136   // UsedFunctions - the functions in the llvm.used list in a more easily
00137   // searchable format.
00138   SmallPtrSet<const Function *, 32> UsedFunctions;
00139 
00140   bool CallsEHReturn;
00141   bool CallsUnwindInit;
00142  
00143   /// DbgInfoAvailable - True if debugging information is available
00144   /// in this module.
00145   bool DbgInfoAvailable;
00146 public:
00147   static char ID; // Pass identification, replacement for typeid
00148 
00149   MachineModuleInfo();
00150   ~MachineModuleInfo();
00151   
00152   /// doInitialization - Initialize the state for a new module.
00153   ///
00154   bool doInitialization();
00155   
00156   /// doFinalization - Tear down the state after completion of a module.
00157   ///
00158   bool doFinalization();
00159   
00160   /// BeginFunction - Begin gathering function meta information.
00161   ///
00162   void BeginFunction(MachineFunction *MF);
00163   
00164   /// EndFunction - Discard function meta information.
00165   ///
00166   void EndFunction();
00167 
00168   /// AnalyzeModule - Scan the module for global debug information.
00169   ///
00170   void AnalyzeModule(Module &M);
00171   
00172   /// hasDebugInfo - Returns true if valid debug info is present.
00173   ///
00174   bool hasDebugInfo() const { return DbgInfoAvailable; }
00175   void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = true; }
00176 
00177   bool callsEHReturn() const { return CallsEHReturn; }
00178   void setCallsEHReturn(bool b) { CallsEHReturn = b; }
00179 
00180   bool callsUnwindInit() const { return CallsUnwindInit; }
00181   void setCallsUnwindInit(bool b) { CallsUnwindInit = b; }
00182   
00183   /// NextLabelID - Return the next unique label id.
00184   ///
00185   unsigned NextLabelID() {
00186     unsigned ID = (unsigned)LabelIDList.size() + 1;
00187     LabelIDList.push_back(ID);
00188     return ID;
00189   }
00190   
00191   /// RecordSourceLine - Records location information and associates it with a
00192   /// label.  Returns a unique label ID used to generate a label and 
00193   /// provide correspondence to the source line list.
00194   unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source);
00195   
00196   /// InvalidateLabel - Inhibit use of the specified label # from
00197   /// MachineModuleInfo, for example because the code was deleted.
00198   void InvalidateLabel(unsigned LabelID) {
00199     // Remap to zero to indicate deletion.
00200     RemapLabel(LabelID, 0);
00201   }
00202 
00203   /// RemapLabel - Indicate that a label has been merged into another.
00204   ///
00205   void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
00206     assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
00207           "Old label ID out of range.");
00208     assert(NewLabelID <= LabelIDList.size() &&
00209           "New label ID out of range.");
00210     LabelIDList[OldLabelID - 1] = NewLabelID;
00211   }
00212   
00213   /// MappedLabel - Find out the label's final ID.  Zero indicates deletion.
00214   /// ID != Mapped ID indicates that the label was folded into another label.
00215   unsigned MappedLabel(unsigned LabelID) const {
00216     assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
00217     return LabelID ? LabelIDList[LabelID - 1] : 0;
00218   }
00219 
00220   /// getSourceLines - Return a vector of source lines.
00221   ///
00222   const std::vector<SourceLineInfo> &getSourceLines() const {
00223     return Lines;
00224   }
00225   
00226   /// getFrameMoves - Returns a reference to a list of moves done in the current
00227   /// function's prologue.  Used to construct frame maps for debug and exception
00228   /// handling comsumers.
00229   std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
00230   
00231   //===-EH-----------------------------------------------------------------===//
00232 
00233   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
00234   /// specified MachineBasicBlock.
00235   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
00236 
00237   /// addInvoke - Provide the begin and end labels of an invoke style call and
00238   /// associate it with a try landing pad block.
00239   void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
00240                                                 unsigned EndLabel);
00241   
00242   /// addLandingPad - Add a new panding pad.  Returns the label ID for the 
00243   /// landing pad entry.
00244   unsigned addLandingPad(MachineBasicBlock *LandingPad);
00245   
00246   /// addPersonality - Provide the personality function for the exception
00247   /// information.
00248   void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
00249 
00250   /// getPersonalityIndex - Get index of the current personality function inside
00251   /// Personalitites array
00252   unsigned getPersonalityIndex() const;
00253 
00254   /// getPersonalities - Return array of personality functions ever seen.
00255   const std::vector<Function *>& getPersonalities() const {
00256     return Personalities;
00257   }
00258 
00259   // UsedFunctions - Return set of the functions in the llvm.used list.
00260   const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
00261     return UsedFunctions;
00262   }
00263 
00264   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
00265   ///
00266   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
00267                         std::vector<GlobalVariable *> &TyInfo);
00268 
00269   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
00270   ///
00271   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
00272                          std::vector<GlobalVariable *> &TyInfo);
00273 
00274   /// addCleanup - Add a cleanup action for a landing pad.
00275   ///
00276   void addCleanup(MachineBasicBlock *LandingPad);
00277 
00278   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is 
00279   /// function wide.
00280   unsigned getTypeIDFor(GlobalVariable *TI);
00281 
00282   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
00283   /// function wide.
00284   int getFilterIDFor(std::vector<unsigned> &TyIds);
00285 
00286   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
00287   /// pads.
00288   void TidyLandingPads();
00289                         
00290   /// getLandingPads - Return a reference to the landing pad info for the
00291   /// current function.
00292   const std::vector<LandingPadInfo> &getLandingPads() const {
00293     return LandingPads;
00294   }
00295   
00296   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
00297   /// function.
00298   const std::vector<GlobalVariable *> &getTypeInfos() const {
00299     return TypeInfos;
00300   }
00301 
00302   /// getFilterIds - Return a reference to the typeids encoding filters used in
00303   /// the current function.
00304   const std::vector<unsigned> &getFilterIds() const {
00305     return FilterIds;
00306   }
00307 
00308   /// getPersonality - Return a personality function if available.  The presence
00309   /// of one is required to emit exception handling info.
00310   Function *getPersonality() const;
00311 
00312 }; // End class MachineModuleInfo
00313 
00314 } // End llvm namespace
00315 
00316 #endif