Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:22

0001 ///===- DroppedVariableStatsIR.h - Opt Diagnostics -*- C++ -*--------------===//
0002 ///
0003 /// Part of the LLVM Project, under the Apache License v2.0 with LLVM
0004 /// Exceptions. See https://llvm.org/LICENSE.txt for license information.
0005 /// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 ///
0007 ///===---------------------------------------------------------------------===//
0008 /// \file
0009 /// Dropped Variable Statistics for Debug Information. Reports any number
0010 /// of #dbg_value that get dropped due to an optimization pass.
0011 ///
0012 ///===---------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATSIR_H
0015 #define LLVM_CODEGEN_DROPPEDVARIABLESTATSIR_H
0016 
0017 #include "llvm/IR/InstIterator.h"
0018 #include "llvm/IR/Module.h"
0019 #include "llvm/Passes/DroppedVariableStats.h"
0020 
0021 namespace llvm {
0022 
0023 /// A class to collect and print dropped debug information due to LLVM IR
0024 /// optimization passes. After every LLVM IR pass is run, it will print how many
0025 /// #dbg_values were dropped due to that pass.
0026 class DroppedVariableStatsIR : public DroppedVariableStats {
0027 public:
0028   DroppedVariableStatsIR(bool DroppedVarStatsEnabled)
0029       : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
0030 
0031   void runBeforePass(StringRef P, Any IR) {
0032     setup();
0033     if (const auto *M = unwrapIR<Module>(IR))
0034       return this->runOnModule(P, M, true);
0035     if (const auto *F = unwrapIR<Function>(IR))
0036       return this->runOnFunction(P, F, true);
0037   }
0038 
0039   void runAfterPass(StringRef P, Any IR) {
0040     if (const auto *M = unwrapIR<Module>(IR))
0041       runAfterPassModule(P, M);
0042     else if (const auto *F = unwrapIR<Function>(IR))
0043       runAfterPassFunction(P, F);
0044     cleanup();
0045   }
0046 
0047   void registerCallbacks(PassInstrumentationCallbacks &PIC);
0048 
0049 private:
0050   const Function *Func;
0051 
0052   void runAfterPassFunction(StringRef PassID, const Function *F) {
0053     runOnFunction(PassID, F, false);
0054     calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(),
0055                                        "Function");
0056   }
0057 
0058   void runAfterPassModule(StringRef PassID, const Module *M) {
0059     runOnModule(PassID, M, false);
0060     calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");
0061   }
0062   /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
0063   /// after a pass has run to facilitate dropped variable calculation for an
0064   /// llvm::Function.
0065   void runOnFunction(StringRef PassID, const Function *F, bool Before);
0066   /// Iterate over all Instructions in a Function and report any dropped debug
0067   /// information.
0068   void calculateDroppedVarStatsOnFunction(const Function *F, StringRef PassID,
0069                                           StringRef FuncOrModName,
0070                                           StringRef PassLevel);
0071   /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
0072   /// after a pass has run to facilitate dropped variable calculation for an
0073   /// llvm::Module. Calls runOnFunction on every Function in the Module.
0074   void runOnModule(StringRef PassID, const Module *M, bool Before);
0075   /// Iterate over all Functions in a Module and report any dropped debug
0076   /// information. Will call calculateDroppedVarStatsOnFunction on every
0077   /// Function.
0078   void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID,
0079                                         StringRef FuncOrModName,
0080                                         StringRef PassLevel);
0081   /// Override base class method to run on an llvm::Function specifically.
0082   virtual void
0083   visitEveryInstruction(unsigned &DroppedCount,
0084                         DenseMap<VarID, DILocation *> &InlinedAtsMap,
0085                         VarID Var) override;
0086 
0087   /// Override base class method to run on #dbg_values specifically.
0088   virtual void visitEveryDebugRecord(
0089       DenseSet<VarID> &VarIDSet,
0090       DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
0091       StringRef FuncName, bool Before) override;
0092 
0093   template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
0094     const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
0095     return IRPtr ? *IRPtr : nullptr;
0096   }
0097 };
0098 
0099 } // namespace llvm
0100 
0101 #endif