blob: 01178af3c9ff58f0861772ef8f15c2abfa7e292b [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a bunch of datatypes that are useful for creating and
11// walking debug info in LLVM IR form. They essentially provide wrappers around
12// the information in the global variables that's needed when constructing the
13// DWARF information.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_IR_DEBUGINFO_H
18#define LLVM_IR_DEBUGINFO_H
19
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
25namespace llvm {
26
27class DbgDeclareInst;
28class DbgValueInst;
29class 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
Andrew Scullcdfcccc2018-10-05 20:58:37 +010054/// Return Debug Info Metadata Version by checking module flags.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055unsigned getDebugMetadataVersionFromModule(const Module &M);
56
Andrew Scullcdfcccc2018-10-05 20:58:37 +010057/// Utility to find all debug info in a module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058///
59/// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
60/// list debug info MDNodes used by an instruction, DebugInfoFinder uses
61/// processDeclare, processValue and processLocation to handle DbgDeclareInst,
62/// DbgValueInst and DbgLoc attached to instructions. processModule will go
63/// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
64/// used by the CUs.
65class DebugInfoFinder {
66public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010067 /// Process entire module and collect debug info anchors.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010068 void processModule(const Module &M);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010069 /// Process a single instruction and collect debug info anchors.
70 void processInstruction(const Module &M, const Instruction &I);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// Process DbgDeclareInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 void processDeclare(const Module &M, const DbgDeclareInst *DDI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010074 /// Process DbgValueInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075 void processValue(const Module &M, const DbgValueInst *DVI);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076 /// Process debug info location.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010077 void processLocation(const Module &M, const DILocation *Loc);
78
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 /// Clear all lists.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010080 void reset();
81
82private:
83 void InitializeTypeMap(const Module &M);
84
Andrew Scullcdfcccc2018-10-05 20:58:37 +010085 void processCompileUnit(DICompileUnit *CU);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086 void processScope(DIScope *Scope);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010087 void processSubprogram(DISubprogram *SP);
88 void processType(DIType *DT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010089 bool addCompileUnit(DICompileUnit *CU);
90 bool addGlobalVariable(DIGlobalVariableExpression *DIG);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010091 bool addScope(DIScope *Scope);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 bool addSubprogram(DISubprogram *SP);
93 bool addType(DIType *DT);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094
95public:
96 using compile_unit_iterator =
97 SmallVectorImpl<DICompileUnit *>::const_iterator;
98 using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
99 using global_variable_expression_iterator =
100 SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
101 using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
102 using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
103
104 iterator_range<compile_unit_iterator> compile_units() const {
105 return make_range(CUs.begin(), CUs.end());
106 }
107
108 iterator_range<subprogram_iterator> subprograms() const {
109 return make_range(SPs.begin(), SPs.end());
110 }
111
112 iterator_range<global_variable_expression_iterator> global_variables() const {
113 return make_range(GVs.begin(), GVs.end());
114 }
115
116 iterator_range<type_iterator> types() const {
117 return make_range(TYs.begin(), TYs.end());
118 }
119
120 iterator_range<scope_iterator> scopes() const {
121 return make_range(Scopes.begin(), Scopes.end());
122 }
123
124 unsigned compile_unit_count() const { return CUs.size(); }
125 unsigned global_variable_count() const { return GVs.size(); }
126 unsigned subprogram_count() const { return SPs.size(); }
127 unsigned type_count() const { return TYs.size(); }
128 unsigned scope_count() const { return Scopes.size(); }
129
130private:
131 SmallVector<DICompileUnit *, 8> CUs;
132 SmallVector<DISubprogram *, 8> SPs;
133 SmallVector<DIGlobalVariableExpression *, 8> GVs;
134 SmallVector<DIType *, 8> TYs;
135 SmallVector<DIScope *, 8> Scopes;
136 SmallPtrSet<const MDNode *, 32> NodesSeen;
137};
138
139} // end namespace llvm
140
141#endif // LLVM_IR_DEBUGINFO_H