diff --git a/llvm/include/llvm/Transforms/Scalar/DCE.h b/llvm/include/llvm/Transforms/Scalar/DCE.h --- a/llvm/include/llvm/Transforms/Scalar/DCE.h +++ b/llvm/include/llvm/Transforms/Scalar/DCE.h @@ -23,6 +23,12 @@ public: PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); }; + +class RedundantDbgInstEliminationPass + : public PassInfoMixin { +public: + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; } #endif // LLVM_TRANSFORMS_SCALAR_DCE_H diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -285,6 +285,7 @@ FUNCTION_PASS("print-predicateinfo", PredicateInfoPrinterPass(dbgs())) FUNCTION_PASS("print-mustexecute", MustExecutePrinterPass(dbgs())) FUNCTION_PASS("reassociate", ReassociatePass()) +FUNCTION_PASS("redundant-dbg-inst-elim", RedundantDbgInstEliminationPass()) FUNCTION_PASS("reg2mem", RegToMemPass()) FUNCTION_PASS("scalarizer", ScalarizerPass()) FUNCTION_PASS("separate-const-offset-from-gep", SeparateConstOffsetFromGEPPass()) diff --git a/llvm/lib/Transforms/Scalar/DCE.cpp b/llvm/lib/Transforms/Scalar/DCE.cpp --- a/llvm/lib/Transforms/Scalar/DCE.cpp +++ b/llvm/lib/Transforms/Scalar/DCE.cpp @@ -69,6 +69,18 @@ return new RedundantDbgInstElimination(); } +PreservedAnalyses +RedundantDbgInstEliminationPass::run(Function &F, FunctionAnalysisManager &AM) { + bool Changed = false; + for (auto &BB : F) + Changed |= RemoveRedundantDbgInstrs(&BB); + if (!Changed) + return PreservedAnalyses::all(); + PreservedAnalyses PA; + PA.preserveSet(); + return PA; +} + //===--------------------------------------------------------------------===// // DeadCodeElimination pass implementation // diff --git a/llvm/test/Transforms/DCE/dbg-value-removal.ll b/llvm/test/Transforms/DCE/dbg-value-removal.ll --- a/llvm/test/Transforms/DCE/dbg-value-removal.ll +++ b/llvm/test/Transforms/DCE/dbg-value-removal.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -S -redundant-dbg-inst-elim | FileCheck %s +; RUN: opt < %s -S -passes=redundant-dbg-inst-elim | FileCheck %s ; All dbg.value with location "!dbg !19" are redundant in the input. ; FIXME: We do not handle non-overlapping/overlapping fragments perfectly yet.