1 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_DEBUGINFO_H
17 #define LLVM_IR_DEBUGINFO_H
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"
30 /// Find subprogram that is enclosing this scope.
31 DISubprogram
*getDISubprogram(const MDNode
*Scope
);
33 /// Strip debug info in the module if it exists.
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
);
41 /// Downgrade the debug info in a module to contain only line table information.
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
);
53 /// Return Debug Info Metadata Version by checking module flags.
54 unsigned getDebugMetadataVersionFromModule(const Module
&M
);
56 /// Utility to find all debug info in a module.
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
64 class DebugInfoFinder
{
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
);
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
);
82 void InitializeTypeMap(const Module
&M
);
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
);
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
;
103 iterator_range
<compile_unit_iterator
> compile_units() const {
104 return make_range(CUs
.begin(), CUs
.end());
107 iterator_range
<subprogram_iterator
> subprograms() const {
108 return make_range(SPs
.begin(), SPs
.end());
111 iterator_range
<global_variable_expression_iterator
> global_variables() const {
112 return make_range(GVs
.begin(), GVs
.end());
115 iterator_range
<type_iterator
> types() const {
116 return make_range(TYs
.begin(), TYs
.end());
119 iterator_range
<scope_iterator
> scopes() const {
120 return make_range(Scopes
.begin(), Scopes
.end());
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(); }
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
;
138 } // end namespace llvm
140 #endif // LLVM_IR_DEBUGINFO_H