From 29e6d3ae144a4e4e26cd641bc21b446e0e3470be Mon Sep 17 00:00:00 2001 From: William Huang Date: Tue, 21 Jan 2025 20:17:49 -0500 Subject: [PATCH] Move RemoveRedundantDbgInstrs outside of inner loop in JumpThreading (#123008) This cleanup action only needs to be performed once when the entire optimization is converged. Doing it in every iteration has a very high time-complexity, as it queries every dbg value in a dense map Compare before and after for one internal source file with many basic blocks ![image](https://github.com/user-attachments/assets/1dac76a9-a974-4068-9aa1-4041f963fa8e) ![image](https://github.com/user-attachments/assets/73ea2ef1-d1f4-4064-8826-8c13fb539b8d) >90% reduction in this extreme case. --- llvm/lib/Transforms/Scalar/JumpThreading.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp index f41eaed2e3e7..e2bda38b81db 100644 --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -429,11 +429,6 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, while (processBlock(&BB)) // Thread all of the branches we can over BB. Changed = true; - // Jump threading may have introduced redundant debug values into BB - // which should be removed. - if (Changed) - RemoveRedundantDbgInstrs(&BB); - // Stop processing BB if it's the entry or is now deleted. The following // routines attempt to eliminate BB and locating a suitable replacement // for the entry is non-trivial. @@ -465,7 +460,6 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, // detect and transform nested loops later. !LoopHeaders.count(&BB) && !LoopHeaders.count(Succ) && TryToSimplifyUncondBranchFromEmptyBlock(&BB, DTU)) { - RemoveRedundantDbgInstrs(Succ); // BB is valid for cleanup here because we passed in DTU. F remains // BB's parent until a DTU->getDomTree() event. LVI->eraseBlock(&BB); @@ -476,6 +470,13 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_, EverChanged |= Changed; } while (Changed); + // Jump threading may have introduced redundant debug values into F which + // should be removed. + if (EverChanged) + for (auto &BB : F) { + RemoveRedundantDbgInstrs(&BB); + } + LoopHeaders.clear(); return EverChanged; } -- Gitee