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