diff --git a/llvm/include/llvm/CodeGen/RemoveRedundantDebugValues.h b/llvm/include/llvm/CodeGen/RemoveRedundantDebugValues.h new file mode 100644 index 0000000000000..7d6dfa9bb830d --- /dev/null +++ b/llvm/include/llvm/CodeGen/RemoveRedundantDebugValues.h @@ -0,0 +1,25 @@ +//===- llvm/CodeGen/RemoveRedundantDebugValues.h ----------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CODEGEN_REMOVEREDUNDANTDEBUGVALUES_H +#define LLVM_CODEGEN_REMOVEREDUNDANTDEBUGVALUES_H + +#include "llvm/CodeGen/MachinePassManager.h" + +namespace llvm { + +class RemoveRedundantDebugValuesPass + : public PassInfoMixin { +public: + PreservedAnalyses run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM); +}; + +} // namespace llvm + +#endif // LLVM_CODEGEN_REMOVEREDUNDANTDEBUGVALUES_H diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index 0dfb46a210c7e..39e454adba57c 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -266,7 +266,7 @@ void initializeRegionPrinterPass(PassRegistry &); void initializeRegionViewerPass(PassRegistry &); void initializeRegisterCoalescerLegacyPass(PassRegistry &); void initializeRemoveLoadsIntoFakeUsesPass(PassRegistry &); -void initializeRemoveRedundantDebugValuesPass(PassRegistry &); +void initializeRemoveRedundantDebugValuesLegacyPass(PassRegistry &); void initializeRenameIndependentSubregsLegacyPass(PassRegistry &); void initializeReplaceWithVeclibLegacyPass(PassRegistry &); void initializeResetMachineFunctionPass(PassRegistry &); diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h index 30f0742fd2c26..9d998b0e4e8c6 100644 --- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h +++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h @@ -64,6 +64,7 @@ #include "llvm/CodeGen/RegUsageInfoPropagate.h" #include "llvm/CodeGen/RegisterCoalescerPass.h" #include "llvm/CodeGen/RegisterUsageInfo.h" +#include "llvm/CodeGen/RemoveRedundantDebugValues.h" #include "llvm/CodeGen/RenameIndependentSubregs.h" #include "llvm/CodeGen/ReplaceWithVeclib.h" #include "llvm/CodeGen/SafeStack.h" diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def index 67eab42b014c9..ce3f3b971d1bc 100644 --- a/llvm/include/llvm/Passes/MachinePassRegistry.def +++ b/llvm/include/llvm/Passes/MachinePassRegistry.def @@ -173,6 +173,7 @@ MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass()) MACHINE_FUNCTION_PASS("reg-usage-propagation", RegUsageInfoPropagationPass()) MACHINE_FUNCTION_PASS("register-coalescer", RegisterCoalescerPass()) MACHINE_FUNCTION_PASS("rename-independent-subregs", RenameIndependentSubregsPass()) +MACHINE_FUNCTION_PASS("remove-redundant-debug-values", RemoveRedundantDebugValuesPass()) MACHINE_FUNCTION_PASS("require-all-machine-function-properties", RequireAllMachineFunctionPropertiesPass()) MACHINE_FUNCTION_PASS("stack-coloring", StackColoringPass()) @@ -266,7 +267,6 @@ DUMMY_MACHINE_FUNCTION_PASS("regalloc", RegAllocPass) DUMMY_MACHINE_FUNCTION_PASS("regallocscoringpass", RegAllocScoringPass) DUMMY_MACHINE_FUNCTION_PASS("regbankselect", RegBankSelectPass) DUMMY_MACHINE_FUNCTION_PASS("remove-loads-into-fake-uses", RemoveLoadsIntoFakeUsesPass) -DUMMY_MACHINE_FUNCTION_PASS("removeredundantdebugvalues", RemoveRedundantDebugValuesPass) DUMMY_MACHINE_FUNCTION_PASS("reset-machine-function", ResetMachineFunctionPass) DUMMY_MACHINE_FUNCTION_PASS("shrink-wrap", ShrinkWrapPass) DUMMY_MACHINE_FUNCTION_PASS("stack-frame-layout", StackFrameLayoutAnalysisPass) diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp index 046a3ee42dd6b..c57953299590f 100644 --- a/llvm/lib/CodeGen/CodeGen.cpp +++ b/llvm/lib/CodeGen/CodeGen.cpp @@ -118,7 +118,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) { initializeRegUsageInfoPropagationLegacyPass(Registry); initializeRegisterCoalescerLegacyPass(Registry); initializeRemoveLoadsIntoFakeUsesPass(Registry); - initializeRemoveRedundantDebugValuesPass(Registry); + initializeRemoveRedundantDebugValuesLegacyPass(Registry); initializeRenameIndependentSubregsLegacyPass(Registry); initializeSafeStackLegacyPassPass(Registry); initializeSelectOptimizePass(Registry); diff --git a/llvm/lib/CodeGen/RemoveRedundantDebugValues.cpp b/llvm/lib/CodeGen/RemoveRedundantDebugValues.cpp index ba8dd49ba9291..e9ea50160b7df 100644 --- a/llvm/lib/CodeGen/RemoveRedundantDebugValues.cpp +++ b/llvm/lib/CodeGen/RemoveRedundantDebugValues.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/CodeGen/RemoveRedundantDebugValues.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallVector.h" @@ -33,14 +34,15 @@ STATISTIC(NumRemovedForward, "Number of DBG_VALUEs removed (forward scan)"); namespace { -class RemoveRedundantDebugValues : public MachineFunctionPass { +struct RemoveRedundantDebugValuesImpl { + bool reduceDbgValues(MachineFunction &MF); +}; + +class RemoveRedundantDebugValuesLegacy : public MachineFunctionPass { public: static char ID; - RemoveRedundantDebugValues(); - - bool reduceDbgValues(MachineFunction &MF); - + RemoveRedundantDebugValuesLegacy(); /// Remove redundant debug value MIs for the given machine function. bool runOnMachineFunction(MachineFunction &MF) override; @@ -56,17 +58,18 @@ class RemoveRedundantDebugValues : public MachineFunctionPass { // Implementation //===----------------------------------------------------------------------===// -char RemoveRedundantDebugValues::ID = 0; +char RemoveRedundantDebugValuesLegacy::ID = 0; -char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValues::ID; +char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValuesLegacy::ID; -INITIALIZE_PASS(RemoveRedundantDebugValues, DEBUG_TYPE, +INITIALIZE_PASS(RemoveRedundantDebugValuesLegacy, DEBUG_TYPE, "Remove Redundant DEBUG_VALUE analysis", false, false) /// Default construct and initialize the pass. -RemoveRedundantDebugValues::RemoveRedundantDebugValues() +RemoveRedundantDebugValuesLegacy::RemoveRedundantDebugValuesLegacy() : MachineFunctionPass(ID) { - initializeRemoveRedundantDebugValuesPass(*PassRegistry::getPassRegistry()); + initializeRemoveRedundantDebugValuesLegacyPass( + *PassRegistry::getPassRegistry()); } // This analysis aims to remove redundant DBG_VALUEs by going forward @@ -199,7 +202,7 @@ static bool reduceDbgValsBackwardScan(MachineBasicBlock &MBB) { return !DbgValsToBeRemoved.empty(); } -bool RemoveRedundantDebugValues::reduceDbgValues(MachineFunction &MF) { +bool RemoveRedundantDebugValuesImpl::reduceDbgValues(MachineFunction &MF) { LLVM_DEBUG(dbgs() << "\nDebug Value Reduction\n"); bool Changed = false; @@ -212,16 +215,32 @@ bool RemoveRedundantDebugValues::reduceDbgValues(MachineFunction &MF) { return Changed; } -bool RemoveRedundantDebugValues::runOnMachineFunction(MachineFunction &MF) { - // Skip functions without debugging information. - if (!MF.getFunction().getSubprogram()) +bool RemoveRedundantDebugValuesLegacy::runOnMachineFunction( + MachineFunction &MF) { + // Skip functions without debugging information or functions from NoDebug + // compilation units. + if (!MF.getFunction().getSubprogram() || + (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == + DICompileUnit::NoDebug)) return false; - // Skip functions from NoDebug compilation units. - if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == - DICompileUnit::NoDebug) - return false; + return RemoveRedundantDebugValuesImpl().reduceDbgValues(MF); +} - bool Changed = reduceDbgValues(MF); - return Changed; +PreservedAnalyses +RemoveRedundantDebugValuesPass::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + // Skip functions without debugging information or functions from NoDebug + // compilation units. + if (!MF.getFunction().getSubprogram() || + (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == + DICompileUnit::NoDebug)) + return PreservedAnalyses::all(); + + if (!RemoveRedundantDebugValuesImpl().reduceDbgValues(MF)) + return PreservedAnalyses::all(); + + auto PA = getMachineFunctionPassPreservedAnalyses(); + PA.preserveSet(); + return PA; } diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index c9825dfc89d3d..6134820370431 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -135,6 +135,7 @@ #include "llvm/CodeGen/RegUsageInfoPropagate.h" #include "llvm/CodeGen/RegisterCoalescerPass.h" #include "llvm/CodeGen/RegisterUsageInfo.h" +#include "llvm/CodeGen/RemoveRedundantDebugValues.h" #include "llvm/CodeGen/RenameIndependentSubregs.h" #include "llvm/CodeGen/SafeStack.h" #include "llvm/CodeGen/SelectOptimize.h" diff --git a/llvm/test/DebugInfo/MIR/X86/remove-redundant-dbg-vals.mir b/llvm/test/DebugInfo/MIR/X86/remove-redundant-dbg-vals.mir index 4817fe3115058..06ce18d8edaa7 100644 --- a/llvm/test/DebugInfo/MIR/X86/remove-redundant-dbg-vals.mir +++ b/llvm/test/DebugInfo/MIR/X86/remove-redundant-dbg-vals.mir @@ -1,4 +1,5 @@ # RUN: llc %s -o - -run-pass=removeredundantdebugvalues | FileCheck --implicit-check-not=DBG_VALUE %s +# RUN: llc %s -o - -passes=remove-redundant-debug-values | FileCheck --implicit-check-not=DBG_VALUE %s ## This checks that the RemoveRedundantDebugValues removes redundant ## DBG_VALUEs. The MIR was hand-written, and foo{[2-6]}() are just