blob: e7c1d9a906777344a9de8dda252f93828dbebb19 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020019#include "llvm/ADT/STLExtras.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#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
25namespace llvm {
26
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020027class DbgVariableIntrinsic;
28class Instruction;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029class Module;
30
Andrew Scullcdfcccc2018-10-05 20:58:37 +010031/// Find subprogram that is enclosing this scope.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010032DISubprogram *getDISubprogram(const MDNode *Scope);
33
Andrew Scullcdfcccc2018-10-05 20:58:37 +010034/// Strip debug info in the module if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035///
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.
39bool StripDebugInfo(Module &M);
40bool 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.
52bool stripNonLineTableDebugInfo(Module &M);
53
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020054/// 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.
58void updateLoopMetadataDebugLocations(
59 Instruction &I, function_ref<DILocation *(const DILocation &)> Updater);
60
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061/// Return Debug Info Metadata Version by checking module flags.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062unsigned getDebugMetadataVersionFromModule(const Module &M);
63
Andrew Scullcdfcccc2018-10-05 20:58:37 +010064/// Utility to find all debug info in a module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065///
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.
72class DebugInfoFinder {
73public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 /// Process entire module and collect debug info anchors.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 void processModule(const Module &M);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076 /// Process a single instruction and collect debug info anchors.
77 void processInstruction(const Module &M, const Instruction &I);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020079 /// Process DbgVariableIntrinsic.
80 void processVariable(const Module &M, const DbgVariableIntrinsic &DVI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010081 /// Process debug info location.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010082 void processLocation(const Module &M, const DILocation *Loc);
83
Andrew Scullcdfcccc2018-10-05 20:58:37 +010084 /// Clear all lists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085 void reset();
86
87private:
88 void InitializeTypeMap(const Module &M);
89
Andrew Scullcdfcccc2018-10-05 20:58:37 +010090 void processCompileUnit(DICompileUnit *CU);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 void processScope(DIScope *Scope);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092 void processSubprogram(DISubprogram *SP);
93 void processType(DIType *DT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094 bool addCompileUnit(DICompileUnit *CU);
95 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010096 bool addScope(DIScope *Scope);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 bool addSubprogram(DISubprogram *SP);
98 bool addType(DIType *DT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099
100public:
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
135private:
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