Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 30 | /// Find subprogram that is enclosing this scope. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | DISubprogram *getDISubprogram(const MDNode *Scope); |
| 32 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 33 | /// Strip debug info in the module if it exists. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 53 | /// Return Debug Info Metadata Version by checking module flags. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 54 | unsigned getDebugMetadataVersionFromModule(const Module &M); |
| 55 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 56 | /// Utility to find all debug info in a module. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 66 | /// Process entire module and collect debug info anchors. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | void processModule(const Module &M); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 68 | /// Process a single instruction and collect debug info anchors. |
| 69 | void processInstruction(const Module &M, const Instruction &I); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 71 | /// Process DbgDeclareInst. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | void processDeclare(const Module &M, const DbgDeclareInst *DDI); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 73 | /// Process DbgValueInst. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | void processValue(const Module &M, const DbgValueInst *DVI); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 75 | /// Process debug info location. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | void processLocation(const Module &M, const DILocation *Loc); |
| 77 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 78 | /// Clear all lists. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | void reset(); |
| 80 | |
| 81 | private: |
| 82 | void InitializeTypeMap(const Module &M); |
| 83 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 84 | void processCompileUnit(DICompileUnit *CU); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 85 | void processScope(DIScope *Scope); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 86 | void processSubprogram(DISubprogram *SP); |
| 87 | void processType(DIType *DT); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 88 | bool addCompileUnit(DICompileUnit *CU); |
| 89 | bool addGlobalVariable(DIGlobalVariableExpression *DIG); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 90 | bool addScope(DIScope *Scope); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 91 | bool addSubprogram(DISubprogram *SP); |
| 92 | bool addType(DIType *DT); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
| 103 | iterator_range<compile_unit_iterator> compile_units() const { |
| 104 | return make_range(CUs.begin(), CUs.end()); |
| 105 | } |
| 106 | |
| 107 | iterator_range<subprogram_iterator> subprograms() const { |
| 108 | return make_range(SPs.begin(), SPs.end()); |
| 109 | } |
| 110 | |
| 111 | iterator_range<global_variable_expression_iterator> global_variables() const { |
| 112 | return make_range(GVs.begin(), GVs.end()); |
| 113 | } |
| 114 | |
| 115 | iterator_range<type_iterator> types() const { |
| 116 | return make_range(TYs.begin(), TYs.end()); |
| 117 | } |
| 118 | |
| 119 | iterator_range<scope_iterator> scopes() const { |
| 120 | return make_range(Scopes.begin(), Scopes.end()); |
| 121 | } |
| 122 | |
| 123 | unsigned compile_unit_count() const { return CUs.size(); } |
| 124 | unsigned global_variable_count() const { return GVs.size(); } |
| 125 | unsigned subprogram_count() const { return SPs.size(); } |
| 126 | unsigned type_count() const { return TYs.size(); } |
| 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 |