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 auto FuncName = F->getName();
54 Func = F;
55 run(DbgVariables&: DebugVariables, FuncName, Before);
56}
57
58void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
59 const Function *F, StringRef PassID, StringRef FuncOrModName,
60 StringRef PassLevel) {
61 Func = F;
62 StringRef FuncName = F->getName();
63 DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
64 calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
65 PassLevel, Func);
66}
67
68void DroppedVariableStatsIR::runOnModule(StringRef PassID, const Module *M,
69 bool Before) {
70 for (auto &F : *M) {
71 runOnFunction(PassID, F: &F, Before);
72 }
73}
74
75void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
76 const Module *M, StringRef PassID, StringRef FuncOrModName,
77 StringRef PassLevel) {
78 for (auto &F : *M) {
79 calculateDroppedVarStatsOnFunction(F: &F, PassID, FuncOrModName, PassLevel);
80 }
81}
82
83void DroppedVariableStatsIR::registerCallbacks(
84 PassInstrumentationCallbacks &PIC) {
85 if (!DroppedVariableStatsEnabled)
86 return;
87
88 PIC.registerBeforeNonSkippedPassCallback(
89 C: [this](StringRef P, IRUnitRef IR) { return runBeforePass(P, IR); });
90 PIC.registerAfterPassCallback(
91 C: [this](StringRef P, IRUnitRef IR, const PreservedAnalyses &PA) {
92 return runAfterPass(P, IR);
93 });
94 PIC.registerAfterPassInvalidatedCallback(
95 C: [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
96}
97
98void DroppedVariableStatsIR::visitEveryInstruction(
99 unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
100 VarID Var) {
101 const DIScope *DbgValScope = std::get<0>(t&: Var);
102 for (const auto &I : instructions(F: Func)) {
103 auto *DbgLoc = I.getDebugLoc().get();
104 if (!DbgLoc)
105 continue;
106 if (updateDroppedCount(DbgLoc, Scope: DbgLoc->getScope(), DbgValScope,
107 InlinedAtsMap, Var, DroppedCount))
108 break;
109 }
110}
111
112void DroppedVariableStatsIR::visitEveryDebugRecord(
113 DenseSet<VarID> &VarIDSet,
114 DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
115 StringRef FuncName, bool Before) {
116 for (const auto &I : instructions(F: Func)) {
117 for (DbgRecord &DR : I.getDbgRecordRange()) {
118 if (auto *Dbg = dyn_cast<DbgVariableRecord>(Val: &DR)) {
119 auto *DbgVar = Dbg->getVariable();
120 auto DbgLoc = DR.getDebugLoc();
121 populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
122 FuncName, Before);
123 }
124 }
125 }
126}
127