1///===- DroppedVariableStatsMIR.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_VALUEs that get dropped due to an optimization pass.
11///
12///===---------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/DroppedVariableStatsMIR.h"
15#include "llvm/IR/DebugInfoMetadata.h"
16
17using namespace llvm;
18
19void DroppedVariableStatsMIR::runBeforePass(StringRef PassID,
20 MachineFunction *MF) {
21 if (PassID == "Debug Variable Analysis")
22 return;
23 setup();
24 return runOnMachineFunction(MF, Before: true);
25}
26
27void DroppedVariableStatsMIR::runAfterPass(StringRef PassID,
28 MachineFunction *MF) {
29 if (PassID == "Debug Variable Analysis")
30 return;
31 runOnMachineFunction(MF, Before: false);
32 calculateDroppedVarStatsOnMachineFunction(MF, PassID, FuncOrModName: MF->getName().str());
33 cleanup();
34}
35
36void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
37 bool Before) {
38 auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
39 auto FuncName = MF->getName();
40 MFunc = MF;
41 run(DbgVariables&: DebugVariables, FuncName, Before);
42}
43
44void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
45 const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
46 MFunc = MF;
47 StringRef FuncName = MF->getName();
48 const Function *Func = &MF->getFunction();
49 DebugVariables &DbgVariables = DebugVariablesStack.back()[Func];
50 calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
51 PassLevel: "MachineFunction", Func);
52}
53
54void DroppedVariableStatsMIR::visitEveryInstruction(
55 unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
56 VarID Var) {
57 unsigned PrevDroppedCount = DroppedCount;
58 const DIScope *DbgValScope = std::get<0>(t&: Var);
59 for (const auto &MBB : *MFunc) {
60 for (const auto &MI : MBB) {
61 if (!MI.isDebugInstr()) {
62 auto *DbgLoc = MI.getDebugLoc().get();
63 if (!DbgLoc)
64 continue;
65
66 auto *Scope = DbgLoc->getScope();
67 if (updateDroppedCount(DbgLoc, Scope, DbgValScope, InlinedAtsMap, Var,
68 DroppedCount))
69 break;
70 }
71 }
72 if (PrevDroppedCount != DroppedCount) {
73 PrevDroppedCount = DroppedCount;
74 break;
75 }
76 }
77}
78
79void DroppedVariableStatsMIR::visitEveryDebugRecord(
80 DenseSet<VarID> &VarIDSet,
81 DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
82 StringRef FuncName, bool Before) {
83 for (const auto &MBB : *MFunc) {
84 for (const auto &MI : MBB) {
85 if (MI.isDebugValueLike()) {
86 auto *DbgVar = MI.getDebugVariable();
87 if (!DbgVar)
88 continue;
89 auto DbgLoc = MI.getDebugLoc();
90 populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
91 FuncName, Before);
92 }
93 }
94 }
95}
96