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/STLExtras.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/TinyPtrVector.h" 23 #include "llvm/ADT/iterator_range.h" 24 #include "llvm/IR/DebugInfoMetadata.h" 25 26 namespace llvm { 27 28 class DbgDeclareInst; 29 class DbgValueInst; 30 class DbgVariableIntrinsic; 31 class Instruction; 32 class Module; 33 34 /// Finds all intrinsics declaring local variables as living in the memory that 35 /// 'V' points to. This may include a mix of dbg.declare and 36 /// dbg.addr intrinsics. 37 TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V); 38 39 /// Like \c FindDbgAddrUses, but only returns dbg.declare intrinsics, not 40 /// dbg.addr. 41 TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V); 42 43 /// Finds the llvm.dbg.value intrinsics describing a value. 44 void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V); 45 46 /// Finds the debug info intrinsics describing a value. 47 void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V); 48 49 /// Find subprogram that is enclosing this scope. 50 DISubprogram *getDISubprogram(const MDNode *Scope); 51 52 /// Strip debug info in the module if it exists. 53 /// 54 /// To do this, we remove all calls to the debugger intrinsics and any named 55 /// metadata for debugging. We also remove debug locations for instructions. 56 /// Return true if module is modified. 57 bool StripDebugInfo(Module &M); 58 bool stripDebugInfo(Function &F); 59 60 /// Downgrade the debug info in a module to contain only line table information. 61 /// 62 /// In order to convert debug info to what -gline-tables-only would have 63 /// created, this does the following: 64 /// 1) Delete all debug intrinsics. 65 /// 2) Delete all non-CU named metadata debug info nodes. 66 /// 3) Create new DebugLocs for each instruction. 67 /// 4) Create a new CU debug info, and similarly for every metadata node 68 /// that's reachable from the CU debug info. 69 /// All debug type metadata nodes are unreachable and garbage collected. 70 bool stripNonLineTableDebugInfo(Module &M); 71 72 /// Update the debug locations contained within the MD_loop metadata attached 73 /// to the instruction \p I, if one exists. \p Updater is applied to Metadata 74 /// operand in the MD_loop metadata: the returned value is included in the 75 /// updated loop metadata node if it is non-null. 76 void updateLoopMetadataDebugLocations( 77 Instruction &I, function_ref<Metadata *(Metadata *)> Updater); 78 79 /// Return Debug Info Metadata Version by checking module flags. 80 unsigned getDebugMetadataVersionFromModule(const Module &M); 81 82 /// Utility to find all debug info in a module. 83 /// 84 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To 85 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses 86 /// processDeclare, processValue and processLocation to handle DbgDeclareInst, 87 /// DbgValueInst and DbgLoc attached to instructions. processModule will go 88 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes 89 /// used by the CUs. 90 class DebugInfoFinder { 91 public: 92 /// Process entire module and collect debug info anchors. 93 void processModule(const Module &M); 94 /// Process a single instruction and collect debug info anchors. 95 void processInstruction(const Module &M, const Instruction &I); 96 97 /// Process DbgVariableIntrinsic. 98 void processVariable(const Module &M, const DbgVariableIntrinsic &DVI); 99 /// Process debug info location. 100 void processLocation(const Module &M, const DILocation *Loc); 101 102 /// Process subprogram. 103 void processSubprogram(DISubprogram *SP); 104 105 /// Clear all lists. 106 void reset(); 107 108 private: 109 void processCompileUnit(DICompileUnit *CU); 110 void processScope(DIScope *Scope); 111 void processType(DIType *DT); 112 bool addCompileUnit(DICompileUnit *CU); 113 bool addGlobalVariable(DIGlobalVariableExpression *DIG); 114 bool addScope(DIScope *Scope); 115 bool addSubprogram(DISubprogram *SP); 116 bool addType(DIType *DT); 117 118 public: 119 using compile_unit_iterator = 120 SmallVectorImpl<DICompileUnit *>::const_iterator; 121 using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator; 122 using global_variable_expression_iterator = 123 SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator; 124 using type_iterator = SmallVectorImpl<DIType *>::const_iterator; 125 using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator; 126 127 iterator_range<compile_unit_iterator> compile_units() const { 128 return make_range(CUs.begin(), CUs.end()); 129 } 130 131 iterator_range<subprogram_iterator> subprograms() const { 132 return make_range(SPs.begin(), SPs.end()); 133 } 134 135 iterator_range<global_variable_expression_iterator> global_variables() const { 136 return make_range(GVs.begin(), GVs.end()); 137 } 138 139 iterator_range<type_iterator> types() const { 140 return make_range(TYs.begin(), TYs.end()); 141 } 142 143 iterator_range<scope_iterator> scopes() const { 144 return make_range(Scopes.begin(), Scopes.end()); 145 } 146 147 unsigned compile_unit_count() const { return CUs.size(); } 148 unsigned global_variable_count() const { return GVs.size(); } 149 unsigned subprogram_count() const { return SPs.size(); } 150 unsigned type_count() const { return TYs.size(); } 151 unsigned scope_count() const { return Scopes.size(); } 152 153 private: 154 SmallVector<DICompileUnit *, 8> CUs; 155 SmallVector<DISubprogram *, 8> SPs; 156 SmallVector<DIGlobalVariableExpression *, 8> GVs; 157 SmallVector<DIType *, 8> TYs; 158 SmallVector<DIScope *, 8> Scopes; 159 SmallPtrSet<const MDNode *, 32> NodesSeen; 160 }; 161 162 } // end namespace llvm 163 164 #endif // LLVM_IR_DEBUGINFO_H 165