LLVM 20.0.0git
DroppedVariableStats.cpp
Go to the documentation of this file.
1///===- DroppedVariableStats.cpp ------------------------------------------===//
2///
3/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6///
7///===---------------------------------------------------------------------===//
8/// \file
9/// Dropped Variable Statistics for Debug Information. Reports any number
10/// of #dbg_value that get dropped due to an optimization pass.
11///
12///===---------------------------------------------------------------------===//
13
17#include "llvm/IR/Module.h"
18
19using namespace llvm;
20
21bool DroppedVariableStats::isScopeChildOfOrEqualTo(const DIScope *Scope,
22 const DIScope *DbgValScope) {
23 while (Scope != nullptr) {
24 if (VisitedScope.find(Scope) == VisitedScope.end()) {
25 VisitedScope.insert(Scope);
26 if (Scope == DbgValScope) {
27 VisitedScope.clear();
28 return true;
29 }
30 Scope = Scope->getScope();
31 } else {
32 VisitedScope.clear();
33 return false;
34 }
35 }
36 return false;
37}
38
39bool DroppedVariableStats::isInlinedAtChildOfOrEqualTo(
40 const DILocation *InlinedAt, const DILocation *DbgValInlinedAt) {
41 if (DbgValInlinedAt == InlinedAt)
42 return true;
43 if (!DbgValInlinedAt)
44 return false;
45 auto *IA = InlinedAt;
46 while (IA) {
47 if (IA == DbgValInlinedAt)
48 return true;
49 IA = IA->getInlinedAt();
50 }
51 return false;
52}
53
55 DebugVariables &DbgVariables, StringRef FuncName, StringRef PassID,
56 StringRef FuncOrModName, StringRef PassLevel, const Function *Func) {
57 unsigned DroppedCount = 0;
58 DenseSet<VarID> &DebugVariablesBeforeSet = DbgVariables.DebugVariablesBefore;
59 DenseSet<VarID> &DebugVariablesAfterSet = DbgVariables.DebugVariablesAfter;
60 DenseMap<VarID, DILocation *> &InlinedAtsMap = InlinedAts.back()[FuncName];
61 // Find an Instruction that shares the same scope as the dropped #dbg_value or
62 // has a scope that is the child of the scope of the #dbg_value, and has an
63 // inlinedAt equal to the inlinedAt of the #dbg_value or it's inlinedAt chain
64 // contains the inlinedAt of the #dbg_value, if such an Instruction is found,
65 // debug information is dropped.
66 for (VarID Var : DebugVariablesBeforeSet) {
67 if (DebugVariablesAfterSet.contains(Var))
68 continue;
69 visitEveryInstruction(DroppedCount, InlinedAtsMap, Var);
70 removeVarFromAllSets(Var, Func);
71 }
72 if (DroppedCount > 0) {
73 llvm::outs() << PassLevel << ", " << PassID << ", " << DroppedCount << ", "
74 << FuncOrModName << "\n";
75 PassDroppedVariables = true;
76 } else
77 PassDroppedVariables = false;
78}
79
81 DILocation *DbgLoc, const DIScope *Scope, const DIScope *DbgValScope,
82 DenseMap<VarID, DILocation *> &InlinedAtsMap, VarID Var,
83 unsigned &DroppedCount) {
84
85 // If the Scope is a child of, or equal to the DbgValScope and is inlined at
86 // the Var's InlinedAt location, return true to signify that the Var has been
87 // dropped.
88 if (isScopeChildOfOrEqualTo(Scope, DbgValScope))
89 if (isInlinedAtChildOfOrEqualTo(DbgLoc->getInlinedAt(),
90 InlinedAtsMap[Var])) {
91 // Found another instruction in the variable's scope, so there exists a
92 // break point at which the variable could be observed. Count it as
93 // dropped.
94 DroppedCount++;
95 return true;
96 }
97 return false;
98}
99
101 bool Before) {
102 auto &VarIDSet = (Before ? DbgVariables.DebugVariablesBefore
103 : DbgVariables.DebugVariablesAfter);
104 auto &InlinedAtsMap = InlinedAts.back();
105 if (Before)
106 InlinedAtsMap.try_emplace(FuncName, DenseMap<VarID, DILocation *>());
107 VarIDSet = DenseSet<VarID>();
108 visitEveryDebugRecord(VarIDSet, InlinedAtsMap, FuncName, Before);
109}
110
112 const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet<VarID> &VarIDSet,
114 StringRef FuncName, bool Before) {
115 VarID Key{DbgVar->getScope(), DbgLoc->getInlinedAtScope(), DbgVar};
116 VarIDSet.insert(Key);
117 if (Before)
118 InlinedAtsMap[FuncName].try_emplace(Key, DbgLoc.getInlinedAt());
119}
120
121void DroppedVariableStatsIR::runOnFunction(const Function *F, bool Before) {
122 auto &DebugVariables = DebugVariablesStack.back()[F];
123 auto FuncName = F->getName();
124 Func = F;
125 run(DebugVariables, FuncName, Before);
126}
127
128void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
129 const Function *F, StringRef PassID, StringRef FuncOrModName,
130 StringRef PassLevel) {
131 Func = F;
132 StringRef FuncName = F->getName();
133 DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
134 calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
135 PassLevel, Func);
136}
137
138void DroppedVariableStatsIR::runOnModule(const Module *M, bool Before) {
139 for (auto &F : *M)
140 runOnFunction(&F, Before);
141}
142
143void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
144 const Module *M, StringRef PassID, StringRef FuncOrModName,
145 StringRef PassLevel) {
146 for (auto &F : *M) {
147 calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
148 }
149}
150
154 return;
155
157 [this](StringRef P, Any IR) { return runBeforePass(IR); });
159 [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
160 return runAfterPass(P, IR);
161 });
163 [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
164}
165
166void DroppedVariableStatsIR::visitEveryInstruction(
167 unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
168 VarID Var) {
169 const DIScope *DbgValScope = std::get<0>(Var);
170 for (const auto &I : instructions(Func)) {
171 auto *DbgLoc = I.getDebugLoc().get();
172 if (!DbgLoc)
173 continue;
174 if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,
175 InlinedAtsMap, Var, DroppedCount))
176 break;
177 }
178}
179
180void DroppedVariableStatsIR::visitEveryDebugRecord(
181 DenseSet<VarID> &VarIDSet,
183 StringRef FuncName, bool Before) {
184 for (const auto &I : instructions(Func)) {
185 for (DbgRecord &DR : I.getDbgRecordRange()) {
186 if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
187 auto *DbgVar = Dbg->getVariable();
188 auto DbgLoc = DR.getDebugLoc();
189 populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
190 FuncName, Before);
191 }
192 }
193 }
194}
Expand Atomic instructions
===- DroppedVariableStats.h - Opt Diagnostics -*- C++ -*-------------—===//
Module.h This file contains the declarations for the Module class.
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:80
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
PassInstrumentationCallbacks PIC
Definition: Any.h:28
DILocalScope * getScope() const
Get the local scope for this variable.
Debug location.
Base class for scope-like contexts.
Base class for non-instruction debug metadata records that have positions within IR.
A debug info location.
Definition: DebugLoc.h:33
DILocation * getInlinedAt() const
Definition: DebugLoc.cpp:39
Implements a dense probed hash-table based set.
Definition: DenseSet.h:278
void registerCallbacks(PassInstrumentationCallbacks &PIC)
void runAfterPass(StringRef P, Any IR)
DenseSet< const DIScope * > VisitedScope
A DenseSet tracking whether a scope was visited before.
void calculateDroppedStatsAndPrint(DebugVariables &DbgVariables, StringRef FuncName, StringRef PassID, StringRef FuncOrModName, StringRef PassLevel, const Function *Func)
Calculate the number of dropped variables in an llvm::Function or llvm::MachineFunction and print the...
virtual void visitEveryInstruction(unsigned &DroppedCount, DenseMap< VarID, DILocation * > &InlinedAtsMap, VarID Var)=0
Visit every llvm::Instruction or llvm::MachineInstruction and check if the debug variable denoted by ...
SmallVector< DenseMap< StringRef, DenseMap< VarID, DILocation * > > > InlinedAts
A stack of DenseMaps, which map the name of an llvm::Function to a DenseMap of VarIDs and their inlin...
bool updateDroppedCount(DILocation *DbgLoc, const DIScope *Scope, const DIScope *DbgValScope, DenseMap< VarID, DILocation * > &InlinedAtsMap, VarID Var, unsigned &DroppedCount)
Check if a Var has been dropped or is a false positive.
virtual void visitEveryDebugRecord(DenseSet< VarID > &VarIDSet, DenseMap< StringRef, DenseMap< VarID, DILocation * > > &InlinedAtsMap, StringRef FuncName, bool Before)=0
Visit every debug record in an llvm::Function or llvm::MachineFunction and call populateVarIDSetAndIn...
void populateVarIDSetAndInlinedMap(const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet< VarID > &VarIDSet, DenseMap< StringRef, DenseMap< VarID, DILocation * > > &InlinedAtsMap, StringRef FuncName, bool Before)
Populate the VarIDSet and InlinedAtMap with the relevant information needed for before and after pass...
SmallVector< DenseMap< const Function *, DebugVariables > > DebugVariablesStack
A stack of a DenseMap, that maps DebugVariables for every pass to an llvm::Function.
void run(DebugVariables &DbgVariables, StringRef FuncName, bool Before)
Run code to populate relevant data structures over an llvm::Function or llvm::MachineFunction.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:213
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:193
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.
DenseSet< VarID > DebugVariablesAfter
DenseSet of VarIDs after an optimization pass has run.
DenseSet< VarID > DebugVariablesBefore
DenseSet of VarIDs before an optimization pass has run.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
void registerAfterPassInvalidatedCallback(CallableT C, bool ToFront=false)
void registerBeforeNonSkippedPassCallback(CallableT C)
void registerAfterPassCallback(CallableT C, bool ToFront=false)