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 Find underlying composite type. 48 DICompositeTypeBase *getDICompositeType(DIType *T); 49 50 /// \brief Generate map by visiting all retained types. 51 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes); 52 53 /// \brief Strip debug info in the module if it exists. 54 /// 55 /// To do this, we remove all calls to the debugger intrinsics and any named 56 /// metadata for debugging. We also remove debug locations for instructions. 57 /// Return true if module is modified. 58 bool StripDebugInfo(Module &M); 59 bool stripDebugInfo(Function &F); 60 61 /// \brief Return Debug Info Metadata Version by checking module flags. 62 unsigned getDebugMetadataVersionFromModule(const Module &M); 63 64 /// \brief Utility to find all debug info in a module. 65 /// 66 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To 67 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses 68 /// processDeclare, processValue and processLocation to handle DbgDeclareInst, 69 /// DbgValueInst and DbgLoc attached to instructions. processModule will go 70 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes 71 /// used by the CUs. 72 class DebugInfoFinder { 73 public: DebugInfoFinder()74 DebugInfoFinder() : TypeMapInitialized(false) {} 75 76 /// \brief Process entire module and collect debug info anchors. 77 void processModule(const Module &M); 78 79 /// \brief Process DbgDeclareInst. 80 void processDeclare(const Module &M, const DbgDeclareInst *DDI); 81 /// \brief Process DbgValueInst. 82 void processValue(const Module &M, const DbgValueInst *DVI); 83 /// \brief Process debug info location. 84 void processLocation(const Module &M, const DILocation *Loc); 85 86 /// \brief Clear all lists. 87 void reset(); 88 89 private: 90 void InitializeTypeMap(const Module &M); 91 92 void processType(DIType *DT); 93 void processSubprogram(DISubprogram *SP); 94 void processScope(DIScope *Scope); 95 bool addCompileUnit(DICompileUnit *CU); 96 bool addGlobalVariable(DIGlobalVariable *DIG); 97 bool addSubprogram(DISubprogram *SP); 98 bool addType(DIType *DT); 99 bool addScope(DIScope *Scope); 100 101 public: 102 typedef SmallVectorImpl<DICompileUnit *>::const_iterator 103 compile_unit_iterator; 104 typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator; 105 typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator 106 global_variable_iterator; 107 typedef SmallVectorImpl<DIType *>::const_iterator type_iterator; 108 typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator; 109 compile_units()110 iterator_range<compile_unit_iterator> compile_units() const { 111 return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end()); 112 } 113 subprograms()114 iterator_range<subprogram_iterator> subprograms() const { 115 return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end()); 116 } 117 global_variables()118 iterator_range<global_variable_iterator> global_variables() const { 119 return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end()); 120 } 121 types()122 iterator_range<type_iterator> types() const { 123 return iterator_range<type_iterator>(TYs.begin(), TYs.end()); 124 } 125 scopes()126 iterator_range<scope_iterator> scopes() const { 127 return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end()); 128 } 129 compile_unit_count()130 unsigned compile_unit_count() const { return CUs.size(); } global_variable_count()131 unsigned global_variable_count() const { return GVs.size(); } subprogram_count()132 unsigned subprogram_count() const { return SPs.size(); } type_count()133 unsigned type_count() const { return TYs.size(); } scope_count()134 unsigned scope_count() const { return Scopes.size(); } 135 136 private: 137 SmallVector<DICompileUnit *, 8> CUs; 138 SmallVector<DISubprogram *, 8> SPs; 139 SmallVector<DIGlobalVariable *, 8> GVs; 140 SmallVector<DIType *, 8> TYs; 141 SmallVector<DIScope *, 8> Scopes; 142 SmallPtrSet<const MDNode *, 64> NodesSeen; 143 DITypeIdentifierMap TypeIdentifierMap; 144 145 /// \brief Specify if TypeIdentifierMap is initialized. 146 bool TypeMapInitialized; 147 }; 148 149 DenseMap<const Function *, DISubprogram *> makeSubprogramMap(const Module &M); 150 151 } // end namespace llvm 152 153 #endif 154