1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines a bunch of datatypes that are useful for creating and 10 // walking debug info in LLVM IR form. They essentially provide wrappers around 11 // the information in the global variables that's needed when constructing the 12 // DWARF information. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_IR_DEBUGINFO_H 17 #define LLVM_IR_DEBUGINFO_H 18 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/iterator_range.h" 22 #include "llvm/IR/DebugInfoMetadata.h" 23 24 namespace llvm { 25 26 class DbgDeclareInst; 27 class DbgValueInst; 28 class Module; 29 30 /// Find subprogram that is enclosing this scope. 31 DISubprogram *getDISubprogram(const MDNode *Scope); 32 33 /// Strip debug info in the module if it exists. 34 /// 35 /// To do this, we remove all calls to the debugger intrinsics and any named 36 /// metadata for debugging. We also remove debug locations for instructions. 37 /// Return true if module is modified. 38 bool StripDebugInfo(Module &M); 39 bool stripDebugInfo(Function &F); 40 41 /// Downgrade the debug info in a module to contain only line table information. 42 /// 43 /// In order to convert debug info to what -gline-tables-only would have 44 /// created, this does the following: 45 /// 1) Delete all debug intrinsics. 46 /// 2) Delete all non-CU named metadata debug info nodes. 47 /// 3) Create new DebugLocs for each instruction. 48 /// 4) Create a new CU debug info, and similarly for every metadata node 49 /// that's reachable from the CU debug info. 50 /// All debug type metadata nodes are unreachable and garbage collected. 51 bool stripNonLineTableDebugInfo(Module &M); 52 53 /// Return Debug Info Metadata Version by checking module flags. 54 unsigned getDebugMetadataVersionFromModule(const Module &M); 55 56 /// Utility to find all debug info in a module. 57 /// 58 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To 59 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses 60 /// processDeclare, processValue and processLocation to handle DbgDeclareInst, 61 /// DbgValueInst and DbgLoc attached to instructions. processModule will go 62 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes 63 /// used by the CUs. 64 class DebugInfoFinder { 65 public: 66 /// Process entire module and collect debug info anchors. 67 void processModule(const Module &M); 68 /// Process a single instruction and collect debug info anchors. 69 void processInstruction(const Module &M, const Instruction &I); 70 71 /// Process DbgDeclareInst. 72 void processDeclare(const Module &M, const DbgDeclareInst *DDI); 73 /// Process DbgValueInst. 74 void processValue(const Module &M, const DbgValueInst *DVI); 75 /// Process debug info location. 76 void processLocation(const Module &M, const DILocation *Loc); 77 78 /// Clear all lists. 79 void reset(); 80 81 private: 82 void InitializeTypeMap(const Module &M); 83 84 void processCompileUnit(DICompileUnit *CU); 85 void processScope(DIScope *Scope); 86 void processSubprogram(DISubprogram *SP); 87 void processType(DIType *DT); 88 bool addCompileUnit(DICompileUnit *CU); 89 bool addGlobalVariable(DIGlobalVariableExpression *DIG); 90 bool addScope(DIScope *Scope); 91 bool addSubprogram(DISubprogram *SP); 92 bool addType(DIType *DT); 93 94 public: 95 using compile_unit_iterator = 96 SmallVectorImpl<DICompileUnit *>::const_iterator; 97 using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator; 98 using global_variable_expression_iterator = 99 SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator; 100 using type_iterator = SmallVectorImpl<DIType *>::const_iterator; 101 using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator; 102 compile_units()103 iterator_range<compile_unit_iterator> compile_units() const { 104 return make_range(CUs.begin(), CUs.end()); 105 } 106 subprograms()107 iterator_range<subprogram_iterator> subprograms() const { 108 return make_range(SPs.begin(), SPs.end()); 109 } 110 global_variables()111 iterator_range<global_variable_expression_iterator> global_variables() const { 112 return make_range(GVs.begin(), GVs.end()); 113 } 114 types()115 iterator_range<type_iterator> types() const { 116 return make_range(TYs.begin(), TYs.end()); 117 } 118 scopes()119 iterator_range<scope_iterator> scopes() const { 120 return make_range(Scopes.begin(), Scopes.end()); 121 } 122 compile_unit_count()123 unsigned compile_unit_count() const { return CUs.size(); } global_variable_count()124 unsigned global_variable_count() const { return GVs.size(); } subprogram_count()125 unsigned subprogram_count() const { return SPs.size(); } type_count()126 unsigned type_count() const { return TYs.size(); } scope_count()127 unsigned scope_count() const { return Scopes.size(); } 128 129 private: 130 SmallVector<DICompileUnit *, 8> CUs; 131 SmallVector<DISubprogram *, 8> SPs; 132 SmallVector<DIGlobalVariableExpression *, 8> GVs; 133 SmallVector<DIType *, 8> TYs; 134 SmallVector<DIScope *, 8> Scopes; 135 SmallPtrSet<const MDNode *, 32> NodesSeen; 136 }; 137 138 } // end namespace llvm 139 140 #endif // LLVM_IR_DEBUGINFO_H 141