xref: /llvm-project/llvm/lib/Passes/DroppedVariableStatsIR.cpp (revision 12903fb3c73ad549c89585097f24d8b9952d849c)
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/Passes/DroppedVariableStatsIR.h"
15 
16 using namespace llvm;
17 
18 void DroppedVariableStatsIR::runOnFunction(StringRef PassID, const Function *F,
19                                            bool Before) {
20   auto &DebugVariables = DebugVariablesStack.back()[F];
21   auto FuncName = F->getName();
22   Func = F;
23   run(DebugVariables, FuncName, Before);
24 }
25 
26 void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
27     const Function *F, StringRef PassID, StringRef FuncOrModName,
28     StringRef PassLevel) {
29   Func = F;
30   StringRef FuncName = F->getName();
31   DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
32   calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
33                                 PassLevel, Func);
34 }
35 
36 void DroppedVariableStatsIR::runOnModule(StringRef PassID, const Module *M,
37                                          bool Before) {
38   for (auto &F : *M) {
39     runOnFunction(PassID, &F, Before);
40   }
41 }
42 
43 void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
44     const Module *M, StringRef PassID, StringRef FuncOrModName,
45     StringRef PassLevel) {
46   for (auto &F : *M) {
47     calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
48   }
49 }
50 
51 void DroppedVariableStatsIR::registerCallbacks(
52     PassInstrumentationCallbacks &PIC) {
53   if (!DroppedVariableStatsEnabled)
54     return;
55 
56   PIC.registerBeforeNonSkippedPassCallback(
57       [this](StringRef P, Any IR) { return runBeforePass(P, IR); });
58   PIC.registerAfterPassCallback(
59       [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
60         return runAfterPass(P, IR);
61       });
62   PIC.registerAfterPassInvalidatedCallback(
63       [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
64 }
65 
66 void DroppedVariableStatsIR::visitEveryInstruction(
67     unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
68     VarID Var) {
69   const DIScope *DbgValScope = std::get<0>(Var);
70   for (const auto &I : instructions(Func)) {
71     auto *DbgLoc = I.getDebugLoc().get();
72     if (!DbgLoc)
73       continue;
74     if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,
75                            InlinedAtsMap, Var, DroppedCount))
76       break;
77   }
78 }
79 
80 void DroppedVariableStatsIR::visitEveryDebugRecord(
81     DenseSet<VarID> &VarIDSet,
82     DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
83     StringRef FuncName, bool Before) {
84   for (const auto &I : instructions(Func)) {
85     for (DbgRecord &DR : I.getDbgRecordRange()) {
86       if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
87         auto *DbgVar = Dbg->getVariable();
88         auto DbgLoc = DR.getDebugLoc();
89         populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
90                                       FuncName, Before);
91       }
92     }
93   }
94 }
95