From c59ec5d8b8750cfc7d11b56deab293837d201d02 Mon Sep 17 00:00:00 2001 From: Apochens Date: Mon, 7 Jul 2025 06:13:51 +0000 Subject: [PATCH 1/2] fix #147238 --- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp | 4 +++- .../LoopStrengthReduce/optimizemax_debugloc.ll | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 786e4516ace05..7b631dbb16571 100644 --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -2559,8 +2559,10 @@ ICmpInst *LSRInstance::OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse) { Instruction *Cmp = cast(Sel->getOperand(0)); Cond->eraseFromParent(); Sel->eraseFromParent(); - if (Cmp->use_empty()) + if (Cmp->use_empty()) { + salvageDebugInfo(*Cmp); Cmp->eraseFromParent(); + } return NewCond; } diff --git a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll index d2d5550c3d68f..eabcb0e1cb81c 100644 --- a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll +++ b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll @@ -1,7 +1,16 @@ ; RUN: opt < %s -loop-reduce -S 2>&1 | FileCheck %s + ;; This test case checks that whether the new icmp instruction preserves ;; the debug location of the original instruction for %exitcond + +;; This test case also checks that the debug value of the dead icmp +;; instruction is salvaged. + +; CHECK: #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]]) ; CHECK: icmp uge i32 %indvar.next, %n, !dbg ![[DBGLOC:[0-9]+]] + +; CHECK: ![[META1]] = !DILocalVariable(name: "1", +; CHECK: ![[META2]] = !DILocation(line: 1, column: 1, ; CHECK: ![[DBGLOC]] = !DILocation(line: 6, column: 1, scope ; ModuleID = 'simplified-dbg.bc' From 5f3346b3579e7259f897bcd6fbb8b38961c170ee Mon Sep 17 00:00:00 2001 From: Apochens Date: Tue, 8 Jul 2025 12:11:07 +0000 Subject: [PATCH 2/2] test nit --- .../Transforms/LoopStrengthReduce/optimizemax_debugloc.ll | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll index eabcb0e1cb81c..d3f68f2305165 100644 --- a/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll +++ b/llvm/test/Transforms/LoopStrengthReduce/optimizemax_debugloc.ll @@ -6,8 +6,10 @@ ;; This test case also checks that the debug value of the dead icmp ;; instruction is salvaged. -; CHECK: #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]]) -; CHECK: icmp uge i32 %indvar.next, %n, !dbg ![[DBGLOC:[0-9]+]] +; CHECK-LABEL: bb.nph: +; CHECK: #dbg_value(i32 %n, ![[META1:[0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), ![[META2:[0-9]+]]) +; CHECK-LABEL: bb: +; CHECK: icmp uge i32 %indvar.next, %n, !dbg ![[DBGLOC:[0-9]+]] ; CHECK: ![[META1]] = !DILocalVariable(name: "1", ; CHECK: ![[META2]] = !DILocation(line: 1, column: 1,