Home | History | Annotate | Download | only in IR
      1 //===- DebugInfo.h - Debug Information Helpers ------------------*- 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 // This file defines a bunch of datatypes that are useful for creating and
     11 // walking debug info in LLVM IR form. They essentially provide wrappers around
     12 // the information in the global variables that's needed when constructing the
     13 // DWARF information.
     14 //
     15 //===----------------------------------------------------------------------===//
     16 
     17 #ifndef LLVM_IR_DEBUGINFO_H
     18 #define LLVM_IR_DEBUGINFO_H
     19 
     20 #include "llvm/ADT/DenseMap.h"
     21 #include "llvm/ADT/SmallPtrSet.h"
     22 #include "llvm/ADT/SmallVector.h"
     23 #include "llvm/ADT/StringRef.h"
     24 #include "llvm/ADT/iterator_range.h"
     25 #include "llvm/IR/DebugInfoMetadata.h"
     26 #include "llvm/Support/Casting.h"
     27 #include "llvm/Support/Dwarf.h"
     28 #include "llvm/Support/ErrorHandling.h"
     29 #include <iterator>
     30 
     31 namespace llvm {
     32 class Module;
     33 class DbgDeclareInst;
     34 class DbgValueInst;
     35 
     36 /// \brief Maps from type identifier to the actual MDNode.
     37 typedef DenseMap<const MDString *, DIType *> DITypeIdentifierMap;
     38 
     39 /// \brief Find subprogram that is enclosing this scope.
     40 DISubprogram *getDISubprogram(const MDNode *Scope);
     41 
     42 /// \brief Find debug info for a given function.
     43 ///
     44 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
     45 DISubprogram *getDISubprogram(const Function *F);
     46 
     47 /// \brief Generate map by visiting all retained types.
     48 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
     49 
     50 /// \brief Strip debug info in the module if it exists.
     51 ///
     52 /// To do this, we remove all calls to the debugger intrinsics and any named
     53 /// metadata for debugging. We also remove debug locations for instructions.
     54 /// Return true if module is modified.
     55 bool StripDebugInfo(Module &M);
     56 bool stripDebugInfo(Function &F);
     57 
     58 /// \brief Return Debug Info Metadata Version by checking module flags.
     59 unsigned getDebugMetadataVersionFromModule(const Module &M);
     60 
     61 /// \brief Utility to find all debug info in a module.
     62 ///
     63 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
     64 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
     65 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
     66 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
     67 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
     68 /// used by the CUs.
     69 class DebugInfoFinder {
     70 public:
     71   DebugInfoFinder() : TypeMapInitialized(false) {}
     72 
     73   /// \brief Process entire module and collect debug info anchors.
     74   void processModule(const Module &M);
     75 
     76   /// \brief Process DbgDeclareInst.
     77   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
     78   /// \brief Process DbgValueInst.
     79   void processValue(const Module &M, const DbgValueInst *DVI);
     80   /// \brief Process debug info location.
     81   void processLocation(const Module &M, const DILocation *Loc);
     82 
     83   /// \brief Clear all lists.
     84   void reset();
     85 
     86 private:
     87   void InitializeTypeMap(const Module &M);
     88 
     89   void processType(DIType *DT);
     90   void processSubprogram(DISubprogram *SP);
     91   void processScope(DIScope *Scope);
     92   bool addCompileUnit(DICompileUnit *CU);
     93   bool addGlobalVariable(DIGlobalVariable *DIG);
     94   bool addSubprogram(DISubprogram *SP);
     95   bool addType(DIType *DT);
     96   bool addScope(DIScope *Scope);
     97 
     98 public:
     99   typedef SmallVectorImpl<DICompileUnit *>::const_iterator
    100       compile_unit_iterator;
    101   typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator;
    102   typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator
    103       global_variable_iterator;
    104   typedef SmallVectorImpl<DIType *>::const_iterator type_iterator;
    105   typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator;
    106 
    107   iterator_range<compile_unit_iterator> compile_units() const {
    108     return make_range(CUs.begin(), CUs.end());
    109   }
    110 
    111   iterator_range<subprogram_iterator> subprograms() const {
    112     return make_range(SPs.begin(), SPs.end());
    113   }
    114 
    115   iterator_range<global_variable_iterator> global_variables() const {
    116     return make_range(GVs.begin(), GVs.end());
    117   }
    118 
    119   iterator_range<type_iterator> types() const {
    120     return make_range(TYs.begin(), TYs.end());
    121   }
    122 
    123   iterator_range<scope_iterator> scopes() const {
    124     return make_range(Scopes.begin(), Scopes.end());
    125   }
    126 
    127   unsigned compile_unit_count() const { return CUs.size(); }
    128   unsigned global_variable_count() const { return GVs.size(); }
    129   unsigned subprogram_count() const { return SPs.size(); }
    130   unsigned type_count() const { return TYs.size(); }
    131   unsigned scope_count() const { return Scopes.size(); }
    132 
    133 private:
    134   SmallVector<DICompileUnit *, 8> CUs;
    135   SmallVector<DISubprogram *, 8> SPs;
    136   SmallVector<DIGlobalVariable *, 8> GVs;
    137   SmallVector<DIType *, 8> TYs;
    138   SmallVector<DIScope *, 8> Scopes;
    139   SmallPtrSet<const MDNode *, 64> NodesSeen;
    140   DITypeIdentifierMap TypeIdentifierMap;
    141 
    142   /// \brief Specify if TypeIdentifierMap is initialized.
    143   bool TypeMapInitialized;
    144 };
    145 
    146 } // end namespace llvm
    147 
    148 #endif
    149