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

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 19 06:21:58 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)

<details>
<summary>Changes</summary>

...to preserve variable location coverage.

Partially fixes missing variable coverage mentioned in #<!-- -->49818 (the locations go missing again later in regalloc in that example).

---
Full diff: https://github.com/llvm/llvm-project/pull/159777.diff


2 Files Affected:

- (modified) llvm/lib/Target/X86/X86FixupSetCC.cpp (+9-5) 
- (added) llvm/test/DebugInfo/X86/x86fixupsetcc-debug-instr-num.mir (+48) 


``````````diff
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
+...

``````````

</details>


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


More information about the llvm-commits mailing list