1414#ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATS_H
1515#define LLVM_CODEGEN_DROPPEDVARIABLESTATS_H
1616
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>
2120
2221namespace llvm {
2322
23+ class DIScope ;
24+ class DILocalVariable ;
25+ class Function ;
26+ class DILocation ;
27+ class DebugLoc ;
28+ class StringRef ;
29+
2430// / A unique key that represents a debug variable.
2531// / First const DIScope *: Represents the scope of the debug variable.
2632// / Second const DIScope *: Represents the InlinedAt scope of the debug
@@ -33,13 +39,7 @@ using VarID =
3339// / statistics.
3440class DroppedVariableStats {
3541public:
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);
4343
4444 virtual ~DroppedVariableStats () {}
4545
@@ -50,20 +50,9 @@ class DroppedVariableStats {
5050 bool getPassDroppedVariables () { return PassDroppedVariables; }
5151
5252protected:
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 ();
6756
6857 bool DroppedVariableStatsEnabled = false ;
6958 struct DebugVariables {
@@ -73,7 +62,6 @@ class DroppedVariableStats {
7362 DenseSet<VarID> DebugVariablesAfter;
7463 };
7564
76- protected:
7765 // / A stack of a DenseMap, that maps DebugVariables for every pass to an
7866 // / llvm::Function. A stack is used because an optimization pass can call
7967 // / other passes.
@@ -90,75 +78,27 @@ class DroppedVariableStats {
9078 void calculateDroppedStatsAndPrint (DebugVariables &DbgVariables,
9179 StringRef FuncName, StringRef PassID,
9280 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- DenseMap<VarID, DILocation *> &InlinedAtsMap = InlinedAts.back ()[FuncName];
100- // Find an Instruction that shares the same scope as the dropped #dbg_value
101- // or has a scope that is the child of the scope of the #dbg_value, and has
102- // an inlinedAt equal to the inlinedAt of the #dbg_value or it's inlinedAt
103- // chain contains the inlinedAt of the #dbg_value, if such an Instruction is
104- // found, debug information is dropped.
105- for (VarID Var : DebugVariablesBeforeSet) {
106- if (DebugVariablesAfterSet.contains (Var))
107- continue ;
108- visitEveryInstruction (DroppedCount, InlinedAtsMap, Var);
109- removeVarFromAllSets (Var, Func);
110- }
111- if (DroppedCount > 0 ) {
112- llvm::outs () << PassLevel << " , " << PassID << " , " << DroppedCount
113- << " , " << FuncOrModName << " \n " ;
114- PassDroppedVariables = true ;
115- } else
116- PassDroppedVariables = false ;
117- }
81+ StringRef PassLevel, const Function *Func);
11882
11983 // / Check if a \p Var has been dropped or is a false positive. Also update the
12084 // / \p DroppedCount if a debug variable is dropped.
12185 bool updateDroppedCount (DILocation *DbgLoc, const DIScope *Scope,
12286 const DIScope *DbgValScope,
12387 DenseMap<VarID, DILocation *> &InlinedAtsMap,
124- VarID Var, unsigned &DroppedCount) {
125- // If the Scope is a child of, or equal to the DbgValScope and is inlined at
126- // the Var's InlinedAt location, return true to signify that the Var has
127- // been dropped.
128- if (isScopeChildOfOrEqualTo (Scope, DbgValScope))
129- if (isInlinedAtChildOfOrEqualTo (DbgLoc->getInlinedAt (),
130- InlinedAtsMap[Var])) {
131- // Found another instruction in the variable's scope, so there exists a
132- // break point at which the variable could be observed. Count it as
133- // dropped.
134- DroppedCount++;
135- return true ;
136- }
137- return false ;
138- }
88+ VarID Var, unsigned &DroppedCount);
89+
13990 // / Run code to populate relevant data structures over an llvm::Function or
14091 // / llvm::MachineFunction.
141- void run (DebugVariables &DbgVariables, StringRef FuncName, bool Before) {
142- auto &VarIDSet = (Before ? DbgVariables.DebugVariablesBefore
143- : DbgVariables.DebugVariablesAfter );
144- auto &InlinedAtsMap = InlinedAts.back ();
145- if (Before)
146- InlinedAtsMap.try_emplace (FuncName, DenseMap<VarID, DILocation *>());
147- VarIDSet = DenseSet<VarID>();
148- visitEveryDebugRecord (VarIDSet, InlinedAtsMap, FuncName, Before);
149- }
92+ void run (DebugVariables &DbgVariables, StringRef FuncName, bool Before);
93+
15094 // / Populate the VarIDSet and InlinedAtMap with the relevant information
15195 // / needed for before and after pass analysis to determine dropped variable
15296 // / status.
15397 void populateVarIDSetAndInlinedMap (
15498 const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet<VarID> &VarIDSet,
15599 DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
156- StringRef FuncName, bool Before) {
157- VarID Key{DbgVar->getScope (), DbgLoc->getInlinedAtScope (), DbgVar};
158- VarIDSet.insert (Key);
159- if (Before)
160- InlinedAtsMap[FuncName].try_emplace (Key, DbgLoc.getInlinedAt ());
161- }
100+ StringRef FuncName, bool Before);
101+
162102 // / Visit every llvm::Instruction or llvm::MachineInstruction and check if the
163103 // / debug variable denoted by its ID \p Var may have been dropped by an
164104 // / optimization pass.
@@ -176,47 +116,18 @@ class DroppedVariableStats {
176116private:
177117 // / Remove a dropped debug variable's VarID from all Sets in the
178118 // / DroppedVariablesBefore stack.
179- void removeVarFromAllSets (VarID Var, const Function *F) {
180- // Do not remove Var from the last element, it will be popped from the
181- // stack.
182- for (auto &DebugVariablesMap : llvm::drop_end (DebugVariablesStack))
183- DebugVariablesMap[F].DebugVariablesBefore .erase (Var);
184- }
119+ void removeVarFromAllSets (VarID Var, const Function *F);
120+
185121 // / Return true if \p Scope is the same as \p DbgValScope or a child scope of
186122 // / \p DbgValScope, return false otherwise.
187123 bool isScopeChildOfOrEqualTo (const DIScope *Scope,
188- const DIScope *DbgValScope) {
189- while (Scope != nullptr ) {
190- if (VisitedScope.find (Scope) == VisitedScope.end ()) {
191- VisitedScope.insert (Scope);
192- if (Scope == DbgValScope) {
193- VisitedScope.clear ();
194- return true ;
195- }
196- Scope = Scope->getScope ();
197- } else {
198- VisitedScope.clear ();
199- return false ;
200- }
201- }
202- return false ;
203- }
124+ const DIScope *DbgValScope);
125+
204126 // / Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
205127 // / the InlinedAt chain, return false otherwise.
206128 bool isInlinedAtChildOfOrEqualTo (const DILocation *InlinedAt,
207- const DILocation *DbgValInlinedAt) {
208- if (DbgValInlinedAt == InlinedAt)
209- return true ;
210- if (!DbgValInlinedAt)
211- return false ;
212- auto *IA = InlinedAt;
213- while (IA) {
214- if (IA == DbgValInlinedAt)
215- return true ;
216- IA = IA->getInlinedAt ();
217- }
218- return false ;
219- }
129+ const DILocation *DbgValInlinedAt);
130+
220131 bool PassDroppedVariables = false ;
221132};
222133
0 commit comments