14
14
#ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATS_H
15
15
#define LLVM_CODEGEN_DROPPEDVARIABLESTATS_H
16
16
17
- #include " llvm/IR/DebugInfoMetadata.h"
18
- #include " llvm/IR/DiagnosticInfo.h"
19
- #include " llvm/IR/Function.h"
20
- #include " llvm/IR/PassInstrumentation.h"
17
+ #include " llvm/ADT/DenseSet.h"
18
+ #include " llvm/ADT/SmallVector.h"
19
+ #include < tuple>
21
20
22
21
namespace llvm {
23
22
23
+ class DIScope ;
24
+ class DILocalVariable ;
25
+ class Function ;
26
+ class DILocation ;
27
+ class DebugLoc ;
28
+ class StringRef ;
29
+
24
30
// / A unique key that represents a debug variable.
25
31
// / First const DIScope *: Represents the scope of the debug variable.
26
32
// / Second const DIScope *: Represents the InlinedAt scope of the debug
@@ -33,13 +39,7 @@ using VarID =
33
39
// / statistics.
34
40
class DroppedVariableStats {
35
41
public:
36
- DroppedVariableStats (bool DroppedVarStatsEnabled)
37
- : DroppedVariableStatsEnabled(DroppedVarStatsEnabled) {
38
- if (DroppedVarStatsEnabled)
39
- llvm::outs ()
40
- << " Pass Level, Pass Name, Num of Dropped Variables, Func or "
41
- " Module Name\n " ;
42
- };
42
+ DroppedVariableStats (bool DroppedVarStatsEnabled);
43
43
44
44
virtual ~DroppedVariableStats () {}
45
45
@@ -50,20 +50,9 @@ class DroppedVariableStats {
50
50
bool getPassDroppedVariables () { return PassDroppedVariables; }
51
51
52
52
protected:
53
- void setup () {
54
- DebugVariablesStack.push_back (
55
- {DenseMap<const Function *, DebugVariables>()});
56
- InlinedAts.push_back (
57
- {DenseMap<StringRef, DenseMap<VarID, DILocation *>>()});
58
- }
59
-
60
- void cleanup () {
61
- assert (!DebugVariablesStack.empty () &&
62
- " DebugVariablesStack shouldn't be empty!" );
63
- assert (!InlinedAts.empty () && " InlinedAts shouldn't be empty!" );
64
- DebugVariablesStack.pop_back ();
65
- InlinedAts.pop_back ();
66
- }
53
+ void setup ();
54
+
55
+ void cleanup ();
67
56
68
57
bool DroppedVariableStatsEnabled = false ;
69
58
struct DebugVariables {
@@ -73,7 +62,6 @@ class DroppedVariableStats {
73
62
DenseSet<VarID> DebugVariablesAfter;
74
63
};
75
64
76
- protected:
77
65
// / A stack of a DenseMap, that maps DebugVariables for every pass to an
78
66
// / llvm::Function. A stack is used because an optimization pass can call
79
67
// / other passes.
@@ -90,78 +78,27 @@ class DroppedVariableStats {
90
78
void calculateDroppedStatsAndPrint (DebugVariables &DbgVariables,
91
79
StringRef FuncName, StringRef PassID,
92
80
StringRef FuncOrModName,
93
- StringRef PassLevel,
94
- const Function *Func) {
95
- unsigned DroppedCount = 0 ;
96
- DenseSet<VarID> &DebugVariablesBeforeSet =
97
- DbgVariables.DebugVariablesBefore ;
98
- DenseSet<VarID> &DebugVariablesAfterSet = DbgVariables.DebugVariablesAfter ;
99
- auto It = InlinedAts.back ().find (FuncName);
100
- if (It == InlinedAts.back ().end ())
101
- return ;
102
- DenseMap<VarID, DILocation *> &InlinedAtsMap = It->second ;
103
- // Find an Instruction that shares the same scope as the dropped #dbg_value
104
- // or has a scope that is the child of the scope of the #dbg_value, and has
105
- // an inlinedAt equal to the inlinedAt of the #dbg_value or it's inlinedAt
106
- // chain contains the inlinedAt of the #dbg_value, if such an Instruction is
107
- // found, debug information is dropped.
108
- for (VarID Var : DebugVariablesBeforeSet) {
109
- if (DebugVariablesAfterSet.contains (Var))
110
- continue ;
111
- visitEveryInstruction (DroppedCount, InlinedAtsMap, Var);
112
- removeVarFromAllSets (Var, Func);
113
- }
114
- if (DroppedCount > 0 ) {
115
- llvm::outs () << PassLevel << " , " << PassID << " , " << DroppedCount
116
- << " , " << FuncOrModName << " \n " ;
117
- PassDroppedVariables = true ;
118
- } else
119
- PassDroppedVariables = false ;
120
- }
81
+ StringRef PassLevel, const Function *Func);
121
82
122
83
// / Check if a \p Var has been dropped or is a false positive. Also update the
123
84
// / \p DroppedCount if a debug variable is dropped.
124
85
bool updateDroppedCount (DILocation *DbgLoc, const DIScope *Scope,
125
86
const DIScope *DbgValScope,
126
87
DenseMap<VarID, DILocation *> &InlinedAtsMap,
127
- VarID Var, unsigned &DroppedCount) {
128
- // If the Scope is a child of, or equal to the DbgValScope and is inlined at
129
- // the Var's InlinedAt location, return true to signify that the Var has
130
- // been dropped.
131
- if (isScopeChildOfOrEqualTo (Scope, DbgValScope))
132
- if (isInlinedAtChildOfOrEqualTo (DbgLoc->getInlinedAt (),
133
- InlinedAtsMap[Var])) {
134
- // Found another instruction in the variable's scope, so there exists a
135
- // break point at which the variable could be observed. Count it as
136
- // dropped.
137
- DroppedCount++;
138
- return true ;
139
- }
140
- return false ;
141
- }
88
+ VarID Var, unsigned &DroppedCount);
89
+
142
90
// / Run code to populate relevant data structures over an llvm::Function or
143
91
// / llvm::MachineFunction.
144
- void run (DebugVariables &DbgVariables, StringRef FuncName, bool Before) {
145
- auto &VarIDSet = (Before ? DbgVariables.DebugVariablesBefore
146
- : DbgVariables.DebugVariablesAfter );
147
- auto &InlinedAtsMap = InlinedAts.back ();
148
- if (Before)
149
- InlinedAtsMap.try_emplace (FuncName, DenseMap<VarID, DILocation *>());
150
- VarIDSet = DenseSet<VarID>();
151
- visitEveryDebugRecord (VarIDSet, InlinedAtsMap, FuncName, Before);
152
- }
92
+ void run (DebugVariables &DbgVariables, StringRef FuncName, bool Before);
93
+
153
94
// / Populate the VarIDSet and InlinedAtMap with the relevant information
154
95
// / needed for before and after pass analysis to determine dropped variable
155
96
// / status.
156
97
void populateVarIDSetAndInlinedMap (
157
98
const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet<VarID> &VarIDSet,
158
99
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
159
- StringRef FuncName, bool Before) {
160
- VarID Key{DbgVar->getScope (), DbgLoc->getInlinedAtScope (), DbgVar};
161
- VarIDSet.insert (Key);
162
- if (Before)
163
- InlinedAtsMap[FuncName].try_emplace (Key, DbgLoc.getInlinedAt ());
164
- }
100
+ StringRef FuncName, bool Before);
101
+
165
102
// / Visit every llvm::Instruction or llvm::MachineInstruction and check if the
166
103
// / debug variable denoted by its ID \p Var may have been dropped by an
167
104
// / optimization pass.
@@ -179,47 +116,18 @@ class DroppedVariableStats {
179
116
private:
180
117
// / Remove a dropped debug variable's VarID from all Sets in the
181
118
// / DroppedVariablesBefore stack.
182
- void removeVarFromAllSets (VarID Var, const Function *F) {
183
- // Do not remove Var from the last element, it will be popped from the
184
- // stack.
185
- for (auto &DebugVariablesMap : llvm::drop_end (DebugVariablesStack))
186
- DebugVariablesMap[F].DebugVariablesBefore .erase (Var);
187
- }
119
+ void removeVarFromAllSets (VarID Var, const Function *F);
120
+
188
121
// / Return true if \p Scope is the same as \p DbgValScope or a child scope of
189
122
// / \p DbgValScope, return false otherwise.
190
123
bool isScopeChildOfOrEqualTo (const DIScope *Scope,
191
- const DIScope *DbgValScope) {
192
- while (Scope != nullptr ) {
193
- if (VisitedScope.find (Scope) == VisitedScope.end ()) {
194
- VisitedScope.insert (Scope);
195
- if (Scope == DbgValScope) {
196
- VisitedScope.clear ();
197
- return true ;
198
- }
199
- Scope = Scope->getScope ();
200
- } else {
201
- VisitedScope.clear ();
202
- return false ;
203
- }
204
- }
205
- return false ;
206
- }
124
+ const DIScope *DbgValScope);
125
+
207
126
// / Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
208
127
// / the InlinedAt chain, return false otherwise.
209
128
bool isInlinedAtChildOfOrEqualTo (const DILocation *InlinedAt,
210
- const DILocation *DbgValInlinedAt) {
211
- if (DbgValInlinedAt == InlinedAt)
212
- return true ;
213
- if (!DbgValInlinedAt)
214
- return false ;
215
- auto *IA = InlinedAt;
216
- while (IA) {
217
- if (IA == DbgValInlinedAt)
218
- return true ;
219
- IA = IA->getInlinedAt ();
220
- }
221
- return false ;
222
- }
129
+ const DILocation *DbgValInlinedAt);
130
+
223
131
bool PassDroppedVariables = false ;
224
132
};
225
133
0 commit comments