LLVM 24.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
23 setup();
24 if (const auto *M = dyn_cast<Module>(IR))
25 return this->runOnModule(P, M, true);
26 if (const auto *F = dyn_cast<Function>(IR))
27 return this->runOnFunction(P, F, true);
28}
29
31 if (const auto *M = dyn_cast<Module>(IR))
32 runAfterPassModule(P, M);
33 else if (const auto *F = dyn_cast<Function>(IR))
34 runAfterPassFunction(P, F);
35 cleanup();
36}
37
38void DroppedVariableStatsIR::runAfterPassFunction(StringRef PassID,
39 const Function *F) {
40 runOnFunction(PassID, F, false);
41 calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(), "Function");
42}
43
44void DroppedVariableStatsIR::runAfterPassModule(StringRef PassID,
45 const Module *M) {
46 runOnModule(PassID, M, false);
47 calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");
48}
49
50void DroppedVariableStatsIR::runOnFunction(StringRef PassID, const Function *F,
51 bool Before) {
52 auto &DebugVariables = DebugVariablesStack.back()[F];
53 Func = F;
54 run(DebugVariables, Before);
55}
56
57void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
58 const Function *F, StringRef PassID, StringRef FuncOrModName,
59 StringRef PassLevel) {
60 Func = F;
61 DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
62 calculateDroppedStatsAndPrint(DbgVariables, PassID, FuncOrModName, PassLevel,
63 Func);
64}
65
66void DroppedVariableStatsIR::runOnModule(StringRef PassID, const Module *M,
67 bool Before) {
68 for (auto &F : *M) {
69 runOnFunction(PassID, &F, Before);
70 }
71}
72
73void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
74 const Module *M, StringRef PassID, StringRef FuncOrModName,
75 StringRef PassLevel) {
76 for (auto &F : *M) {
77 calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
78 }
79}
80
84 return;
85
86 PIC.registerBeforeNonSkippedPassCallback(
87 [this](StringRef P, IRUnitRef IR) { return runBeforePass(P, IR); });
88 PIC.registerAfterPassCallback(
89 [this](StringRef P, IRUnitRef IR, const PreservedAnalyses &PA) {
90 return runAfterPass(P, IR);
91 });
92 PIC.registerAfterPassInvalidatedCallback(
93 [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
94}
95
96void DroppedVariableStatsIR::visitEveryInstruction(unsigned &DroppedCount,
97 VarID Var) {
98 for (const auto &I : instructions(Func)) {
99 auto *DbgLoc = I.getDebugLoc().get();
100 if (!DbgLoc)
101 continue;
102 if (updateDroppedCount(DbgLoc, Var, DroppedCount))
103 break;
104 }
105}
106
107void DroppedVariableStatsIR::visitEveryDebugRecord(DenseSet<VarID> &VarIDSet) {
108 for (const auto &I : instructions(Func)) {
109 for (DbgRecord &DR : I.getDbgRecordRange()) {
110 if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
111 auto *DbgVar = Dbg->getVariable();
112 auto DbgLoc = DR.getDebugLoc();
113 populateVarIDSet(DbgVar, DbgLoc, VarIDSet);
114 }
115 }
116 }
117}
Expand Atomic instructions
===- DroppedVariableStatsIR.h - Opt Diagnostics -*- C++ -*-----------—===//
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:85
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
void registerCallbacks(PassInstrumentationCallbacks &PIC)
void runAfterPass(StringRef P, IRUnitRef IR)
void runBeforePass(StringRef P, IRUnitRef IR)
LLVM_ABI void populateVarIDSet(const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet< VarID > &VarIDSet)
Add the VarID of the debug record of DbgVar at DbgLoc to VarIDSet.
LLVM_ABI void run(DebugVariables &DbgVariables, bool Before)
Run code to populate relevant data structures over an llvm::Function or llvm::MachineFunction.
LLVM_ABI void calculateDroppedStatsAndPrint(DebugVariables &DbgVariables, 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...
SmallVector< DenseMap< const Function *, DebugVariables > > DebugVariablesStack
A stack of a DenseMap, that maps DebugVariables for every pass to an llvm::Function.
LLVM_ABI bool updateDroppedCount(const DILocation *DbgLoc, VarID Var, unsigned &DroppedCount)
Check if a Var has been dropped or is a false positive.
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
Definition IRUnitRef.h:60
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
DebugVariableAggregate VarID
A unique key that represents one instance of a debug variable.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643