2323
2424namespace llvm {
2525
26- // / A class to collect and print dropped debug information variable statistics.
27- // / After every LLVM IR pass is run, it will print how many #dbg_values were
28- // / dropped due to that pass.
26+ // / A unique key that represents a #dbg_value.
27+ using VarID =
28+ std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
29+
30+ // / A base class to collect and print dropped debug information variable
31+ // / statistics.
2932class DroppedVariableStats {
3033public:
3134 DroppedVariableStats (bool DroppedVarStatsEnabled)
@@ -39,18 +42,25 @@ class DroppedVariableStats {
3942 DroppedVariableStats (const DroppedVariableStats &) = delete ;
4043 void operator =(const DroppedVariableStats &) = delete ;
4144
42- void registerCallbacks (PassInstrumentationCallbacks &PIC);
43- void runBeforePass (StringRef PassID, Any IR);
44- void runAfterPass (StringRef PassID, Any IR, const PreservedAnalyses &PA);
45- void runAfterPassInvalidated (StringRef PassID, const PreservedAnalyses &PA);
4645 bool getPassDroppedVariables () { return PassDroppedVariables; }
4746
48- private:
47+ protected:
48+ void setup () {
49+ DebugVariablesStack.push_back (
50+ {DenseMap<const Function *, DebugVariables>()});
51+ InlinedAts.push_back (
52+ {DenseMap<StringRef, DenseMap<VarID, DILocation *>>()});
53+ return ;
54+ }
55+
56+ void cleanup () {
57+ DebugVariablesStack.pop_back ();
58+ InlinedAts.pop_back ();
59+ return ;
60+ }
61+
4962 bool PassDroppedVariables = false ;
5063 bool DroppedVariableStatsEnabled = false ;
51- // / A unique key that represents a #dbg_value.
52- using VarID =
53- std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
5464
5565 struct DebugVariables {
5666 // / DenseSet of VarIDs before an optimization pass has run.
@@ -70,36 +80,80 @@ class DroppedVariableStats {
7080 // / DenseMap of VarIDs and their inlinedAt locations before an optimization
7181 // / pass has run.
7282 SmallVector<DenseMap<StringRef, DenseMap<VarID, DILocation *>>> InlinedAts;
83+ // / Remove a dropped #dbg_value VarID from all Sets in the
84+ // / DroppedVariablesBefore stack.
85+ void removeVarFromAllSets (VarID Var, const Function *F) {
86+ // Do not remove Var from the last element, it will be popped from the
87+ // stack.
88+ for (auto &DebugVariablesMap : llvm::drop_end (DebugVariablesStack))
89+ DebugVariablesMap[F].DebugVariablesBefore .erase (Var);
90+ }
91+ // / Return true if \p Scope is the same as \p DbgValScope or a child scope of
92+ // / \p DbgValScope, return false otherwise.
93+ bool isScopeChildOfOrEqualTo (DIScope *Scope, const DIScope *DbgValScope);
94+ // / Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
95+ // / the InlinedAt chain, return false otherwise.
96+ bool isInlinedAtChildOfOrEqualTo (const DILocation *InlinedAt,
97+ const DILocation *DbgValInlinedAt);
98+ };
7399
74- // / Iterate over all Functions in a Module and report any dropped debug
75- // / information. Will call calculateDroppedVarStatsOnFunction on every
76- // / Function.
77- void calculateDroppedVarStatsOnModule (const Module *M, StringRef PassID,
78- std::string FuncOrModName,
79- std::string PassLevel);
100+ // / A class to collect and print dropped debug information due to LLVM IR
101+ // / optimization passes. After every LLVM IR pass is run, it will print how many
102+ // / #dbg_values were dropped due to that pass.
103+ class DroppedVariableStatsIR : public DroppedVariableStats {
104+ public:
105+ DroppedVariableStatsIR (bool DroppedVarStatsEnabled)
106+ : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
107+
108+ void runBeforePass (Any IR) {
109+ setup ();
110+ if (const auto *M = unwrapIR<Module>(IR))
111+ return this ->runOnModule (M, true );
112+ if (const auto *F = unwrapIR<Function>(IR))
113+ return this ->runOnFunction (F, true );
114+ }
115+
116+ void runAfterPass (StringRef P, Any IR) {
117+ if (const auto *M = unwrapIR<Module>(IR))
118+ runAfterPassModule (P, M);
119+ else if (const auto *F = unwrapIR<Function>(IR))
120+ runAfterPassFunction (P, F);
121+ return cleanup ();
122+ }
123+
124+ void registerCallbacks (PassInstrumentationCallbacks &PIC);
125+
126+ private:
127+ void runAfterPassFunction (StringRef PassID, const Function *F) {
128+ runOnFunction (F, false );
129+ calculateDroppedVarStatsOnFunction (F, PassID, F->getName ().str (),
130+ " Function" );
131+ }
132+
133+ void runAfterPassModule (StringRef PassID, const Module *M) {
134+ runOnModule (M, false );
135+ calculateDroppedVarStatsOnModule (M, PassID, M->getName ().str (), " Module" );
136+ }
137+ // / Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
138+ // / after a pass has run to facilitate dropped variable calculation for an
139+ // / llvm::Function.
140+ void runOnFunction (const Function *F, bool Before);
80141 // / Iterate over all Instructions in a Function and report any dropped debug
81142 // / information.
82143 void calculateDroppedVarStatsOnFunction (const Function *F, StringRef PassID,
83144 std::string FuncOrModName,
84145 std::string PassLevel);
85146 // / Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
86147 // / after a pass has run to facilitate dropped variable calculation for an
87- // / llvm::Function.
88- void runOnFunction (const Function *F, bool Before);
89- // / Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
90- // / after a pass has run to facilitate dropped variable calculation for an
91148 // / llvm::Module. Calls runOnFunction on every Function in the Module.
92149 void runOnModule (const Module *M, bool Before);
93- // / Remove a dropped #dbg_value VarID from all Sets in the
94- // / DroppedVariablesBefore stack.
95- void removeVarFromAllSets (VarID Var, const Function *F);
96- // / Return true if \p Scope is the same as \p DbgValScope or a child scope of
97- // / \p DbgValScope, return false otherwise.
98- bool isScopeChildOfOrEqualTo (DIScope *Scope, const DIScope *DbgValScope);
99- // / Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
100- // / the InlinedAt chain, return false otherwise.
101- bool isInlinedAtChildOfOrEqualTo (const DILocation *InlinedAt,
102- const DILocation *DbgValInlinedAt);
150+ // / Iterate over all Functions in a Module and report any dropped debug
151+ // / information. Will call calculateDroppedVarStatsOnFunction on every
152+ // / Function.
153+ void calculateDroppedVarStatsOnModule (const Module *M, StringRef PassID,
154+ std::string FuncOrModName,
155+ std::string PassLevel);
156+
103157 template <typename IRUnitT> static const IRUnitT *unwrapIR (Any IR) {
104158 const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
105159 return IRPtr ? *IRPtr : nullptr ;
@@ -108,4 +162,4 @@ class DroppedVariableStats {
108162
109163} // namespace llvm
110164
111- #endif
165+ #endif
0 commit comments