LLVM 22.0.0git
DroppedVariableStatsIR.cpp
Go to the documentation of this file.
1///===- DroppedVariableStatsIR.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"
19
20using namespace llvm;
21
22template <typename IRUnitT>
23const IRUnitT *DroppedVariableStatsIR::unwrapIR(Any IR) {
24 const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
25 return IRPtr ? *IRPtr : nullptr;
26}
27
29 setup();
30 if (const auto *M = unwrapIR<Module>(IR))
31 return this->runOnModule(P, M, true);
32 if (const auto *F = unwrapIR<Function>(IR))
33 return this->runOnFunction(P, F, true);
34}
35
37 if (const auto *M = unwrapIR<Module>(IR))
38 runAfterPassModule(P, M);
39 else if (const auto *F = unwrapIR<Function>(IR))
40 runAfterPassFunction(P, F);
41 cleanup();
42}
43
44void DroppedVariableStatsIR::runAfterPassFunction(StringRef PassID,
45 const Function *F) {
46 runOnFunction(PassID, F, false);
47 calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(), "Function");
48}
49
50void DroppedVariableStatsIR::runAfterPassModule(StringRef PassID,
51 const Module *M) {
52 runOnModule(PassID, M, false);
53 calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");
54}
55
56void DroppedVariableStatsIR::runOnFunction(StringRef PassID, const Function *F,
57 bool Before) {
58 auto &DebugVariables = DebugVariablesStack.back()[F];
59 auto FuncName = F->getName();
60 Func = F;
61 run(DebugVariables, FuncName, Before);
62}
63
64void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
65 const Function *F, StringRef PassID, StringRef FuncOrModName,
66 StringRef PassLevel) {
67 Func = F;
68 StringRef FuncName = F->getName();
69 DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
70 calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
71 PassLevel, Func);
72}
73
74void DroppedVariableStatsIR::runOnModule(StringRef PassID, const Module *M,
75 bool Before) {
76 for (auto &F : *M) {
77 runOnFunction(PassID, &F, Before);
78 }
79}
80
81void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
82 const Module *M, StringRef PassID, StringRef FuncOrModName,
83 StringRef PassLevel) {
84 for (auto &F : *M) {
85 calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
86 }
87}
88
92 return;
93
95 [this](StringRef P, Any IR) { return runBeforePass(P, IR); });
97 [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
98 return runAfterPass(P, IR);
99 });
101 [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
102}
103
104void DroppedVariableStatsIR::visitEveryInstruction(
105 unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
106 VarID Var) {
107 const DIScope *DbgValScope = std::get<0>(Var);
108 for (const auto &I : instructions(Func)) {
109 auto *DbgLoc = I.getDebugLoc().get();
110 if (!DbgLoc)
111 continue;
112 if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,
113 InlinedAtsMap, Var, DroppedCount))
114 break;
115 }
116}
117
118void DroppedVariableStatsIR::visitEveryDebugRecord(
119 DenseSet<VarID> &VarIDSet,
121 StringRef FuncName, bool Before) {
122 for (const auto &I : instructions(Func)) {
123 for (DbgRecord &DR : I.getDbgRecordRange()) {
124 if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
125 auto *DbgVar = Dbg->getVariable();
126 auto DbgLoc = DR.getDebugLoc();
127 populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
128 FuncName, Before);
129 }
130 }
131 }
132}
Expand Atomic instructions
===- DroppedVariableStatsIR.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
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
Definition: Any.h:28
Base class for scope-like contexts.
Base class for non-instruction debug metadata records that have positions within IR.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
void registerCallbacks(PassInstrumentationCallbacks &PIC)
void runAfterPass(StringRef P, Any IR)
void runBeforePass(StringRef P, Any IR)
LLVM_ABI 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...
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI 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:67
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.
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)