LLVM 24.0.0git
DroppedVariableStatsIR.cpp
Go to the documentation of this file.
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
17#include "llvm/IR/Module.h"
19
20using namespace llvm;
21
23 setup();
24 if (const auto *M = dyn_cast<Module>(IR))
25 return this->runOnModule(P, M, true);
26 if (const auto *F = dyn_cast<Function>(IR))
27 return this->runOnFunction(P, F, true);
28}
29
31 if (const auto *M = dyn_cast<Module>(IR))
32 runAfterPassModule(P, M);
33 else if (const auto *F = dyn_cast<Function>(IR))
34 runAfterPassFunction(P, F);
35 cleanup();
36}
37
38void DroppedVariableStatsIR::runAfterPassFunction(StringRef PassID,
39 const Function *F) {
40 runOnFunction(PassID, F, false);
41 calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(), "Function");
42}
43
44void DroppedVariableStatsIR::runAfterPassModule(StringRef PassID,
45 const Module *M) {
46 runOnModule(PassID, M, false);
47 calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "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(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, 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, PassID, FuncOrModName, PassLevel);
80 }
81}
82
86 return;
87
88 PIC.registerBeforeNonSkippedPassCallback(
89 [this](StringRef P, IRUnitRef IR) { return runBeforePass(P, IR); });
90 PIC.registerAfterPassCallback(
91 [this](StringRef P, IRUnitRef IR, const PreservedAnalyses &PA) {
92 return runAfterPass(P, IR);
93 });
94 PIC.registerAfterPassInvalidatedCallback(
95 [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>(Var);
102 for (const auto &I : instructions(Func)) {
103 auto *DbgLoc = I.getDebugLoc().get();
104 if (!DbgLoc)
105 continue;
106 if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,
107 InlinedAtsMap, Var, DroppedCount))
108 break;
109 }
110}
111
112void DroppedVariableStatsIR::visitEveryDebugRecord(
113 DenseSet<VarID> &VarIDSet,
115 StringRef FuncName, bool Before) {
116 for (const auto &I : instructions(Func)) {
117 for (DbgRecord &DR : I.getDbgRecordRange()) {
118 if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
119 auto *DbgVar = Dbg->getVariable();
120 auto DbgLoc = DR.getDebugLoc();
121 populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
122 FuncName, Before);
123 }
124 }
125 }
126}
Expand Atomic instructions
===- DroppedVariableStatsIR.h - Opt Diagnostics -*- C++ -*-----------—===//
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
Base class for scope-like contexts.
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
void registerCallbacks(PassInstrumentationCallbacks &PIC)
void runAfterPass(StringRef P, IRUnitRef IR)
void runBeforePass(StringRef P, IRUnitRef IR)
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.
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
Definition IRUnitRef.h:60
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643