LLVM 22.0.0git
DroppedVariableStatsMIR.cpp
Go to the documentation of this file.
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
16
17using namespace llvm;
18
20 MachineFunction *MF) {
21 if (PassID == "Debug Variable Analysis")
22 return;
23 setup();
24 return runOnMachineFunction(MF, true);
25}
26
28 MachineFunction *MF) {
29 if (PassID == "Debug Variable Analysis")
30 return;
31 runOnMachineFunction(MF, false);
32 calculateDroppedVarStatsOnMachineFunction(MF, PassID, 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(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 "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>(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,
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}
MachineBasicBlock & MBB
===- DroppedVariableStatsMIR.h - Opt Diagnostics -*- C++ -*----------—===//
IRTranslator LLVM IR MI
Base class for scope-like contexts.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
void runBeforePass(StringRef PassID, MachineFunction *MF)
void runAfterPass(StringRef PassID, MachineFunction *MF)
LLVM_ABI void calculateDroppedStatsAndPrint(DebugVariables &DbgVariables, StringRef FuncName, StringRef PassID, StringRef FuncOrModName, StringRef PassLevel, const Function *Func)
Calculate the number of dropped variables in an llvm::Function or llvm::MachineFunction and print the...
LLVM_ABI bool updateDroppedCount(DILocation *DbgLoc, const DIScope *Scope, const DIScope *DbgValScope, DenseMap< VarID, DILocation * > &InlinedAtsMap, VarID Var, unsigned &DroppedCount)
Check if a Var has been dropped or is a false positive.
LLVM_ABI void populateVarIDSetAndInlinedMap(const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet< VarID > &VarIDSet, DenseMap< StringRef, DenseMap< VarID, DILocation * > > &InlinedAtsMap, StringRef FuncName, bool Before)
Populate the VarIDSet and InlinedAtMap with the relevant information needed for before and after pass...
SmallVector< DenseMap< const Function *, DebugVariables > > DebugVariablesStack
A stack of a DenseMap, that maps DebugVariables for every pass to an llvm::Function.
LLVM_ABI void run(DebugVariables &DbgVariables, StringRef FuncName, bool Before)
Run code to populate relevant data structures over an llvm::Function or llvm::MachineFunction.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:233
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.