From 9637b045e6eee71cd34f0f2f80a2ae1ad2a71129 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 24 Sep 2021 17:16:59 -0700 Subject: [PATCH] Improve the effectiveness of ADCE's debug info salvaging This patch improves the effectiveness of ADCE's debug info salvaging by processing the instructions in reverse order and delaying dropAllReferences until after debug info salvaging. This allows salvaging of entire chains of deleted instructions! Previously we would remove all references from an instruction, which would make it impossible to use that instruction to salvage a later instruction in the instruction stream, because its operands were already removed. Differential Revision: https://reviews.llvm.org/D110462 --- llvm/lib/Transforms/Scalar/ADCE.cpp | 6 ++++-- llvm/test/Transforms/Util/salvage-debuginfo.ll | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/ADCE.cpp b/llvm/lib/Transforms/Scalar/ADCE.cpp index 6f3fdb8..b693acc 100644 --- a/llvm/lib/Transforms/Scalar/ADCE.cpp +++ b/llvm/lib/Transforms/Scalar/ADCE.cpp @@ -538,7 +538,7 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() { // that have no side effects and do not influence the control flow or return // value of the function, and may therefore be deleted safely. // NOTE: We reuse the Worklist vector here for memory efficiency. - for (Instruction &I : instructions(F)) { + for (Instruction &I : llvm::reverse(instructions(F))) { // Check if the instruction is alive. if (isLive(&I)) continue; @@ -554,9 +554,11 @@ bool AggressiveDeadCodeElimination::removeDeadInstructions() { // Prepare to delete. Worklist.push_back(&I); salvageDebugInfo(I); - I.dropAllReferences(); } + for (Instruction *&I : Worklist) + I->dropAllReferences(); + for (Instruction *&I : Worklist) { ++NumRemoved; I->eraseFromParent(); diff --git a/llvm/test/Transforms/Util/salvage-debuginfo.ll b/llvm/test/Transforms/Util/salvage-debuginfo.ll index a1f6f36..ed1baa5 100644 --- a/llvm/test/Transforms/Util/salvage-debuginfo.ll +++ b/llvm/test/Transforms/Util/salvage-debuginfo.ll @@ -5,8 +5,10 @@ define void @f(i32) !dbg !8 { entry: %p_x = inttoptr i32 %0 to i8* %i_x = ptrtoint i8* %p_x to i32 - ; CHECK: call void @llvm.dbg.value(metadata i8* undef, - ; CHECK-SAME: !DIExpression(DW_OP_LLVM_convert, 64, DW_ATE_unsigned, + ; CHECK: call void @llvm.dbg.value(metadata i32 %0, + ; CHECK-SAME: !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, + ; CHECK-SAME: DW_OP_LLVM_convert, 64, DW_ATE_unsigned, + ; CHECK-SAME: DW_OP_LLVM_convert, 64, DW_ATE_unsigned, ; CHECK-SAME: DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_stack_value)) call void @llvm.dbg.value(metadata i32 %i_x, metadata !11, metadata !DIExpression()), !dbg !13 ret void, !dbg !13 -- 2.7.4