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
14#include "llvm/IR/DroppedVariableStatsIR.h"
15#include "llvm/IR/DebugInfoMetadata.h"
16#include "llvm/IR/InstIterator.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/PassInstrumentation.h"
19
20using namespace llvm;
21
22void DroppedVariableStatsIR::runBeforePass(StringRef P, IRUnitRef IR) {
23 setup();
24 if (const auto *M = dyn_cast<Module>(Val&: IR))
25 return this->runOnModule(PassID: P, M, Before: true);
26 if (const auto *F = dyn_cast<Function>(Val&: IR))
27 return this->runOnFunction(PassID: P, F, Before: true);
28}
29
30void DroppedVariableStatsIR::runAfterPass(StringRef P, IRUnitRef IR) {
31 if (const auto *M = dyn_cast<Module>(Val&: IR))
32 runAfterPassModule(PassID: P, M);
33 else if (const auto *F = dyn_cast<Function>(Val&: IR))
34 runAfterPassFunction(PassID: P, F);
35 cleanup();
36}
37
38void DroppedVariableStatsIR::runAfterPassFunction(StringRef PassID,
39 const Function *F) {
40 runOnFunction(PassID, F, Before: false);
41 calculateDroppedVarStatsOnFunction(F, PassID, FuncOrModName: F->getName().str(), PassLevel: "Function");
42}
43
44void DroppedVariableStatsIR::runAfterPassModule(StringRef PassID,
45 const Module *M) {
46 runOnModule(PassID, M, Before: false);
47 calculateDroppedVarStatsOnModule(M, PassID, FuncOrModName: M->getName().str(), PassLevel: "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(DbgVariables&: 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: &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: &F, PassID, FuncOrModName, PassLevel);
78 }
79}
80
81void DroppedVariableStatsIR::registerCallbacks(
82 PassInstrumentationCallbacks &PIC) {
83 if (!DroppedVariableStatsEnabled)
84 return;
85
86 PIC.registerBeforeNonSkippedPassCallback(
87 C: [this](StringRef P, IRUnitRef IR) { return runBeforePass(P, IR); });
88 PIC.registerAfterPassCallback(
89 C: [this](StringRef P, IRUnitRef IR, const PreservedAnalyses &PA) {
90 return runAfterPass(P, IR);
91 });
92 PIC.registerAfterPassInvalidatedCallback(
93 C: [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
94}
95
96void DroppedVariableStatsIR::visitEveryInstruction(unsigned &DroppedCount,
97 VarID Var) {
98 for (const auto &I : instructions(F: 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(F: Func)) {
109 for (DbgRecord &DR : I.getDbgRecordRange()) {
110 if (auto *Dbg = dyn_cast<DbgVariableRecord>(Val: &DR)) {
111 auto *DbgVar = Dbg->getVariable();
112 auto DbgLoc = DR.getDebugLoc();
113 populateVarIDSet(DbgVar, DbgLoc, VarIDSet);
114 }
115 }
116 }
117}
118