[llvm] [DebugInfo][InstrRef] Copy instr-ref to replacement instrs in X86FixupSetCCPass (PR #159777)

Orlando Cazalet-Hyams via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 19 09:09:13 PDT 2025


https://github.com/OCHyams updated https://github.com/llvm/llvm-project/pull/159777

>From db89b6b5b5fd83ab5d4635fe8eab56451590949e Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Wed, 17 Sep 2025 18:22:10 +0100
Subject: [PATCH 1/2] [DebugInfo][InstrRef] Copy instr-ref to replacement
 instrs in X86FixupSetCCPass

...to preserve variable location coverage.

Partially fixes missing variable coverage mentioned in #49818 (the locations
go missing again later in regalloc in that example).
---
 llvm/lib/Target/X86/X86FixupSetCC.cpp         | 14 ++++--
 .../X86/x86fixupsetcc-debug-instr-num.mir     | 48 +++++++++++++++++++
 2 files changed, 57 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir

diff --git a/llvm/lib/Target/X86/X86FixupSetCC.cpp b/llvm/lib/Target/X86/X86FixupSetCC.cpp
index 2de89947c4519..e354bb49e1d7b 100644
--- a/llvm/lib/Target/X86/X86FixupSetCC.cpp
+++ b/llvm/lib/Target/X86/X86FixupSetCC.cpp
@@ -131,11 +131,15 @@ bool X86FixupSetCCPass::runOnMachineFunction(MachineFunction &MF) {
                 ZeroReg);
       }
 
-      BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
-              TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
-          .addReg(ZeroReg)
-          .addReg(Reg0)
-          .addImm(X86::sub_8bit);
+      MachineInstr *NewMI =
+          BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
+                  TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
+              .addReg(ZeroReg)
+              .addReg(Reg0)
+              .addImm(X86::sub_8bit);
+      // Copy the debug info instr-ref number from the zext to its replacement.
+      if (unsigned InstrNum = ZExt->peekDebugInstrNum())
+        NewMI->setDebugInstrNum(InstrNum);
       ToErase.push_back(ZExt);
     }
   }
diff --git a/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir b/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir
new file mode 100644
index 0000000000000..4b963ca1ea66a
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir
@@ -0,0 +1,48 @@
+# RUN: llc %s  --run-pass=x86-fixup-setcc -o - | FileCheck %s
+
+## Check the debug-isntr-number transfers from MOVZX32rr8
+## to its replacement INSERT_SUBREG.
+
+# CHECK: %[[#]]:gr32 = INSERT_SUBREG %[[#]], %[[#]], %subreg.sub_8bit,  debug-instr-number 1
+# CHECK-NEXT: DBG_INSTR_REF !4, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0)
+
+--- |
+  source_filename = "reduced.ll"
+  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+  target triple = "x86_64-unknown-linux-gnu"
+
+  define i32 @main(i32 %call2) {
+  entry:
+    %cmp12 = icmp sgt i32 %call2, 0
+    %conv13 = zext i1 %cmp12 to i32
+      #dbg_value(i32 %conv13, !4, !DIExpression(), !8)
+    ret i32 %conv13
+  }
+
+  !llvm.dbg.cu = !{!0}
+  !llvm.module.flags = !{!3}
+
+  !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 22.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !2, splitDebugInlining: false, nameTableKind: None)
+  !1 = !DIFile(filename: "test.c", directory: "/")
+  !2 = !{}
+  !3 = !{i32 2, !"Debug Info Version", i32 3}
+  !4 = !DILocalVariable(name: "v_3", scope: !5, file: !1, line: 10, type: !7)
+  !5 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 5, type: !6, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2, keyInstructions: true)
+  !6 = !DISubroutineType(types: !2)
+  !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+  !8 = !DILocation(line: 0, scope: !5)
+...
+---
+name:            main
+body:             |
+  bb.0.entry:
+    liveins: $edi
+
+    %0:gr32 = COPY $edi
+    TEST32rr %0, %0, implicit-def $eflags
+    %1:gr8 = SETCCr 15, implicit $eflags
+    %2:gr32 = MOVZX32rr8 killed %1,  debug-instr-number 1
+    DBG_INSTR_REF !4, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0),  debug-location !8
+    $eax = COPY %2
+    RET 0, $eax
+...

>From 013aa774603627ee06937386558b3da5b26b5f70 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams <orlando.hyams at sony.com>
Date: Fri, 19 Sep 2025 17:08:49 +0100
Subject: [PATCH 2/2] use subst

---
 llvm/lib/Target/X86/X86FixupSetCC.cpp           | 17 +++++++++--------
 .../X86/x86fixupsetcc-debug-instr-num.mir       |  9 +++++++--
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Target/X86/X86FixupSetCC.cpp b/llvm/lib/Target/X86/X86FixupSetCC.cpp
index e354bb49e1d7b..e99712249420a 100644
--- a/llvm/lib/Target/X86/X86FixupSetCC.cpp
+++ b/llvm/lib/Target/X86/X86FixupSetCC.cpp
@@ -131,15 +131,16 @@ bool X86FixupSetCCPass::runOnMachineFunction(MachineFunction &MF) {
                 ZeroReg);
       }
 
-      MachineInstr *NewMI =
-          BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
-                  TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
-              .addReg(ZeroReg)
-              .addReg(Reg0)
-              .addImm(X86::sub_8bit);
-      // Copy the debug info instr-ref number from the zext to its replacement.
+      BuildMI(*ZExt->getParent(), ZExt, ZExt->getDebugLoc(),
+              TII->get(X86::INSERT_SUBREG), ZExt->getOperand(0).getReg())
+          .addReg(ZeroReg)
+          .addReg(Reg0)
+          .addImm(X86::sub_8bit);
+
       if (unsigned InstrNum = ZExt->peekDebugInstrNum())
-        NewMI->setDebugInstrNum(InstrNum);
+        ZExt->getParent()->getParent()->makeDebugValueSubstitution(
+            {InstrNum, 0}, {MI.getDebugInstrNum(), 0});
+
       ToErase.push_back(ZExt);
     }
   }
diff --git a/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir b/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir
index 4b963ca1ea66a..17d24175cbeeb 100644
--- a/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir
+++ b/llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir
@@ -3,8 +3,13 @@
 ## Check the debug-isntr-number transfers from MOVZX32rr8
 ## to its replacement INSERT_SUBREG.
 
-# CHECK: %[[#]]:gr32 = INSERT_SUBREG %[[#]], %[[#]], %subreg.sub_8bit,  debug-instr-number 1
-# CHECK-NEXT: DBG_INSTR_REF !4, !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0)
+# CHECK: debugValueSubstitutions:
+# CHECK:  - { srcinst: 1, srcop: 0, dstinst: 2, dstop: 0, subreg: 0 }
+
+# CHECK: %[[#]]:gr8 = SETCCr 15, implicit $eflags,  debug-instr-number 2
+# CHECK: INSERT_SUBREG
+# CHECK-NOT: debug-instr-number
+# CHECK-NEXT: DBG_INSTR_REF ![[#]], !DIExpression(DW_OP_LLVM_arg, 0), dbg-instr-ref(1, 0)
 
 --- |
   source_filename = "reduced.ll"



More information about the llvm-commits mailing list