| 1 | ///===- DroppedVariableStats.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/IR/DroppedVariableStats.h" |
| 15 | #include "llvm/IR/DebugInfoMetadata.h" |
| 16 | #include "llvm/IR/DiagnosticInfo.h" |
| 17 | #include "llvm/IR/Function.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | DroppedVariableStats::DroppedVariableStats(bool DroppedVarStatsEnabled) |
| 22 | : DroppedVariableStatsEnabled(DroppedVarStatsEnabled) { |
| 23 | if (DroppedVarStatsEnabled) |
| 24 | llvm::outs() << "Pass Level, Pass Name, Num of Dropped Variables, Func or " |
| 25 | "Module Name\n" ; |
| 26 | } |
| 27 | |
| 28 | void DroppedVariableStats::setup() { |
| 29 | DebugVariablesStack.push_back(Elt: {DenseMap<const Function *, DebugVariables>()}); |
| 30 | } |
| 31 | |
| 32 | void DroppedVariableStats::cleanup() { |
| 33 | assert(!DebugVariablesStack.empty() && |
| 34 | "DebugVariablesStack shouldn't be empty!" ); |
| 35 | DebugVariablesStack.pop_back(); |
| 36 | } |
| 37 | |
| 38 | void DroppedVariableStats::calculateDroppedStatsAndPrint( |
| 39 | DebugVariables &DbgVariables, StringRef PassID, StringRef FuncOrModName, |
| 40 | StringRef PassLevel, const Function *Func) { |
| 41 | unsigned DroppedCount = 0; |
| 42 | DenseSet<VarID> &DebugVariablesBeforeSet = DbgVariables.DebugVariablesBefore; |
| 43 | DenseSet<VarID> &DebugVariablesAfterSet = DbgVariables.DebugVariablesAfter; |
| 44 | // A variable is considered dropped if an instruction remains at which a |
| 45 | // debugger could observe it. |
| 46 | for (VarID Var : DebugVariablesBeforeSet) { |
| 47 | if (DebugVariablesAfterSet.contains(V: Var)) |
| 48 | continue; |
| 49 | visitEveryInstruction(DroppedCount, Var); |
| 50 | removeVarFromAllSets(Var, F: Func); |
| 51 | } |
| 52 | if (DroppedCount > 0) { |
| 53 | llvm::outs() << PassLevel << ", " << PassID << ", " << DroppedCount << ", " |
| 54 | << FuncOrModName << "\n" ; |
| 55 | PassDroppedVariables = true; |
| 56 | } else |
| 57 | PassDroppedVariables = false; |
| 58 | } |
| 59 | |
| 60 | bool DroppedVariableStats::updateDroppedCount(const DILocation *DbgLoc, |
| 61 | VarID Var, |
| 62 | unsigned &DroppedCount) { |
| 63 | if (!isVarInScopeAt(Loc: DbgLoc, Var)) |
| 64 | return false; |
| 65 | // Found another instruction in the variable's scope, so there exists a break |
| 66 | // point at which the variable could be observed. Count it as dropped. |
| 67 | DroppedCount++; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | void DroppedVariableStats::run(DebugVariables &DbgVariables, bool Before) { |
| 72 | auto &VarIDSet = (Before ? DbgVariables.DebugVariablesBefore |
| 73 | : DbgVariables.DebugVariablesAfter); |
| 74 | VarIDSet = DenseSet<VarID>(); |
| 75 | visitEveryDebugRecord(VarIDSet); |
| 76 | } |
| 77 | |
| 78 | void DroppedVariableStats::populateVarIDSet(const DILocalVariable *DbgVar, |
| 79 | DebugLoc DbgLoc, |
| 80 | DenseSet<VarID> &VarIDSet) { |
| 81 | VarIDSet.insert(V: DebugVariableAggregate( |
| 82 | DebugVariable(DbgVar, std::nullopt, DbgLoc.getInlinedAt()))); |
| 83 | } |
| 84 | |
| 85 | void DroppedVariableStats::removeVarFromAllSets(VarID Var, const Function *F) { |
| 86 | // Do not remove Var from the last element, it will be popped from the |
| 87 | // stack. |
| 88 | for (auto &DebugVariablesMap : llvm::drop_end(RangeOrContainer&: DebugVariablesStack)) |
| 89 | DebugVariablesMap[F].DebugVariablesBefore.erase(V: Var); |
| 90 | } |
| 91 | |
| 92 | bool DroppedVariableStats::isScopeChildOfOrEqualTo(const DIScope *Scope, |
| 93 | const DIScope *VarScope) { |
| 94 | while (Scope != nullptr) { |
| 95 | if (Scope == VarScope) |
| 96 | return true; |
| 97 | Scope = Scope->getScope(); |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | bool DroppedVariableStats::isVarInScopeAt(const DILocation *Loc, VarID Var) { |
| 103 | // Loc and the call sites in its inlinedAt chain are the frames a debugger |
| 104 | // stopped at Loc can see. Var can only be in the frame of its own |
| 105 | // inlined instance. |
| 106 | for (const DILocation *Frame = Loc; Frame; Frame = Frame->getInlinedAt()) |
| 107 | if (Frame->getInlinedAt() == Var.getInlinedAt()) |
| 108 | return isScopeChildOfOrEqualTo(Scope: Frame->getScope(), |
| 109 | VarScope: Var.getVariable()->getScope()); |
| 110 | return false; |
| 111 | } |
| 112 | |