
This patch attempts to reland https://github.com/llvm/llvm-project/pull/120780 while addressing the issues that caused the patch to be reverted. Namely: 1. The patch had included code from the llvm/Passes directory in the llvm/CodeGen directory. 2. The patch increased the backend compile time by 2% due to adding a very expensive include in MachineFunctionPass.h The patch has been re-structured so that there is no dependency between the llvm/Passes and llvm/CodeGen directory, by moving the base class, `class DroppedVariableStats` to the llvm/IR directory. The expensive include in MachineFunctionPass.h has been changed to contain forward declarations instead of other header includes which was pulling a ton of code into MachineFunctionPass.h and should resolve any issues when it comes to compile time increase.
96 lines
3.3 KiB
C++
96 lines
3.3 KiB
C++
///===- DroppedVariableStatsMIR.cpp ---------------------------------------===//
|
|
///
|
|
/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
|
|
/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
|
|
/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
///
|
|
///===---------------------------------------------------------------------===//
|
|
/// \file
|
|
/// Dropped Variable Statistics for Debug Information. Reports any number
|
|
/// of DBG_VALUEs that get dropped due to an optimization pass.
|
|
///
|
|
///===---------------------------------------------------------------------===//
|
|
|
|
#include "llvm/CodeGen/DroppedVariableStatsMIR.h"
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
|
|
|
using namespace llvm;
|
|
|
|
void DroppedVariableStatsMIR::runBeforePass(StringRef PassID,
|
|
MachineFunction *MF) {
|
|
if (PassID == "Debug Variable Analysis")
|
|
return;
|
|
setup();
|
|
return runOnMachineFunction(MF, true);
|
|
}
|
|
|
|
void DroppedVariableStatsMIR::runAfterPass(StringRef PassID,
|
|
MachineFunction *MF) {
|
|
if (PassID == "Debug Variable Analysis")
|
|
return;
|
|
runOnMachineFunction(MF, false);
|
|
calculateDroppedVarStatsOnMachineFunction(MF, PassID, MF->getName().str());
|
|
cleanup();
|
|
}
|
|
|
|
void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
|
|
bool Before) {
|
|
auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
|
|
auto FuncName = MF->getName();
|
|
MFunc = MF;
|
|
run(DebugVariables, FuncName, Before);
|
|
}
|
|
|
|
void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
|
|
const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
|
|
MFunc = MF;
|
|
StringRef FuncName = MF->getName();
|
|
const Function *Func = &MF->getFunction();
|
|
DebugVariables &DbgVariables = DebugVariablesStack.back()[Func];
|
|
calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
|
|
"MachineFunction", Func);
|
|
}
|
|
|
|
void DroppedVariableStatsMIR::visitEveryInstruction(
|
|
unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
|
|
VarID Var) {
|
|
unsigned PrevDroppedCount = DroppedCount;
|
|
const DIScope *DbgValScope = std::get<0>(Var);
|
|
for (const auto &MBB : *MFunc) {
|
|
for (const auto &MI : MBB) {
|
|
if (!MI.isDebugInstr()) {
|
|
auto *DbgLoc = MI.getDebugLoc().get();
|
|
if (!DbgLoc)
|
|
continue;
|
|
|
|
auto *Scope = DbgLoc->getScope();
|
|
if (updateDroppedCount(DbgLoc, Scope, DbgValScope, InlinedAtsMap, Var,
|
|
DroppedCount))
|
|
break;
|
|
}
|
|
}
|
|
if (PrevDroppedCount != DroppedCount) {
|
|
PrevDroppedCount = DroppedCount;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DroppedVariableStatsMIR::visitEveryDebugRecord(
|
|
DenseSet<VarID> &VarIDSet,
|
|
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
|
|
StringRef FuncName, bool Before) {
|
|
for (const auto &MBB : *MFunc) {
|
|
for (const auto &MI : MBB) {
|
|
if (MI.isDebugValueLike()) {
|
|
auto *DbgVar = MI.getDebugVariable();
|
|
if (!DbgVar)
|
|
continue;
|
|
auto DbgLoc = MI.getDebugLoc();
|
|
populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
|
|
FuncName, Before);
|
|
}
|
|
}
|
|
}
|
|
}
|