blob: 171e1621889fbbe001a7e145e2dff718b76b4f51 [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
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
24namespace llvm {
25
26class DbgDeclareInst;
27class DbgValueInst;
28class Module;
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// Find subprogram that is enclosing this scope.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031DISubprogram *getDISubprogram(const MDNode *Scope);
32
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033/// Strip debug info in the module if it exists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034///
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.
38bool StripDebugInfo(Module &M);
39bool 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.
51bool stripNonLineTableDebugInfo(Module &M);
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053/// Return Debug Info Metadata Version by checking module flags.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054unsigned getDebugMetadataVersionFromModule(const Module &M);
55
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056/// Utility to find all debug info in a module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057///
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.
64class DebugInfoFinder {
65public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010066 /// Process entire module and collect debug info anchors.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010067 void processModule(const Module &M);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Process a single instruction and collect debug info anchors.
69 void processInstruction(const Module &M, const Instruction &I);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Process DbgDeclareInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010073 /// Process DbgValueInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 void processValue(const Module &M, const DbgValueInst *DVI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010075 /// Process debug info location.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010076 void processLocation(const Module &M, const DILocation *Loc);
77
Andrew Scullcdfcccc2018-10-05 20:58:37 +010078 /// Clear all lists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 void reset();
80
81private:
82 void InitializeTypeMap(const Module &M);
83
Andrew Scullcdfcccc2018-10-05 20:58:37 +010084 void processCompileUnit(DICompileUnit *CU);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085 void processScope(DIScope *Scope);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010086 void processSubprogram(DISubprogram *SP);
87 void processType(DIType *DT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088 bool addCompileUnit(DICompileUnit *CU);
89 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010090 bool addScope(DIScope *Scope);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 bool addSubprogram(DISubprogram *SP);
92 bool addType(DIType *DT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093
94public:
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
129private:
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