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 MFunc = MF;
40 run(DbgVariables&: DebugVariables, Before);
41}
42
43void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
44 const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
45 MFunc = MF;
46 const Function *Func = &MF->getFunction();
47 DebugVariables &DbgVariables = DebugVariablesStack.back()[Func];
48 calculateDroppedStatsAndPrint(DbgVariables, PassID, FuncOrModName,
49 PassLevel: "MachineFunction", Func);
50}
51
52void DroppedVariableStatsMIR::visitEveryInstruction(unsigned &DroppedCount,
53 VarID Var) {
54 unsigned PrevDroppedCount = DroppedCount;
55 for (const auto &MBB : *MFunc) {
56 for (const auto &MI : MBB) {
57 if (!MI.isDebugInstr()) {
58 auto *DbgLoc = MI.getDebugLoc().get();
59 if (!DbgLoc)
60 continue;
61
62 if (updateDroppedCount(DbgLoc, Var, DroppedCount))
63 break;
64 }
65 }
66 if (PrevDroppedCount != DroppedCount) {
67 PrevDroppedCount = DroppedCount;
68 break;
69 }
70 }
71}
72
73void DroppedVariableStatsMIR::visitEveryDebugRecord(DenseSet<VarID> &VarIDSet) {
74 for (const auto &MBB : *MFunc) {
75 for (const auto &MI : MBB) {
76 if (MI.isDebugValueLike()) {
77 auto *DbgVar = MI.getDebugVariable();
78 if (!DbgVar)
79 continue;
80 auto DbgLoc = MI.getDebugLoc();
81 populateVarIDSet(DbgVar, DbgLoc, VarIDSet);
82 }
83 }
84 }
85}
86