Path: blob/main/contrib/llvm-project/llvm/lib/IR/DroppedVariableStatsIR.cpp
213766 views
///===- DroppedVariableStatsIR.cpp ----------------------------------------===//1///2/// Part of the LLVM Project, under the Apache License v2.0 with LLVM3/// Exceptions. See https://llvm.org/LICENSE.txt for license information.4/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5///6///===---------------------------------------------------------------------===//7/// \file8/// Dropped Variable Statistics for Debug Information. Reports any number9/// of #dbg_value that get dropped due to an optimization pass.10///11///===---------------------------------------------------------------------===//1213#include "llvm/IR/DroppedVariableStatsIR.h"14#include "llvm/IR/DebugInfoMetadata.h"15#include "llvm/IR/InstIterator.h"16#include "llvm/IR/Module.h"17#include "llvm/IR/PassInstrumentation.h"1819using namespace llvm;2021template <typename IRUnitT>22const IRUnitT *DroppedVariableStatsIR::unwrapIR(Any IR) {23const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);24return IRPtr ? *IRPtr : nullptr;25}2627void DroppedVariableStatsIR::runBeforePass(StringRef P, Any IR) {28setup();29if (const auto *M = unwrapIR<Module>(IR))30return this->runOnModule(P, M, true);31if (const auto *F = unwrapIR<Function>(IR))32return this->runOnFunction(P, F, true);33}3435void DroppedVariableStatsIR::runAfterPass(StringRef P, Any IR) {36if (const auto *M = unwrapIR<Module>(IR))37runAfterPassModule(P, M);38else if (const auto *F = unwrapIR<Function>(IR))39runAfterPassFunction(P, F);40cleanup();41}4243void DroppedVariableStatsIR::runAfterPassFunction(StringRef PassID,44const Function *F) {45runOnFunction(PassID, F, false);46calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(), "Function");47}4849void DroppedVariableStatsIR::runAfterPassModule(StringRef PassID,50const Module *M) {51runOnModule(PassID, M, false);52calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");53}5455void DroppedVariableStatsIR::runOnFunction(StringRef PassID, const Function *F,56bool Before) {57auto &DebugVariables = DebugVariablesStack.back()[F];58auto FuncName = F->getName();59Func = F;60run(DebugVariables, FuncName, Before);61}6263void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(64const Function *F, StringRef PassID, StringRef FuncOrModName,65StringRef PassLevel) {66Func = F;67StringRef FuncName = F->getName();68DebugVariables &DbgVariables = DebugVariablesStack.back()[F];69calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,70PassLevel, Func);71}7273void DroppedVariableStatsIR::runOnModule(StringRef PassID, const Module *M,74bool Before) {75for (auto &F : *M) {76runOnFunction(PassID, &F, Before);77}78}7980void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(81const Module *M, StringRef PassID, StringRef FuncOrModName,82StringRef PassLevel) {83for (auto &F : *M) {84calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);85}86}8788void DroppedVariableStatsIR::registerCallbacks(89PassInstrumentationCallbacks &PIC) {90if (!DroppedVariableStatsEnabled)91return;9293PIC.registerBeforeNonSkippedPassCallback(94[this](StringRef P, Any IR) { return runBeforePass(P, IR); });95PIC.registerAfterPassCallback(96[this](StringRef P, Any IR, const PreservedAnalyses &PA) {97return runAfterPass(P, IR);98});99PIC.registerAfterPassInvalidatedCallback(100[this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });101}102103void DroppedVariableStatsIR::visitEveryInstruction(104unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,105VarID Var) {106const DIScope *DbgValScope = std::get<0>(Var);107for (const auto &I : instructions(Func)) {108auto *DbgLoc = I.getDebugLoc().get();109if (!DbgLoc)110continue;111if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,112InlinedAtsMap, Var, DroppedCount))113break;114}115}116117void DroppedVariableStatsIR::visitEveryDebugRecord(118DenseSet<VarID> &VarIDSet,119DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,120StringRef FuncName, bool Before) {121for (const auto &I : instructions(Func)) {122for (DbgRecord &DR : I.getDbgRecordRange()) {123if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {124auto *DbgVar = Dbg->getVariable();125auto DbgLoc = DR.getDebugLoc();126populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,127FuncName, Before);128}129}130}131}132133134