Author: Pawan Nirpal
Date: 2026-07-16T18:08:12Z
New Revision: 5aa15abead01fef21fee4706aa01f39e8ac18829

URL: 
https://github.com/llvm/llvm-project/commit/5aa15abead01fef21fee4706aa01f39e8ac18829
DIFF: 
https://github.com/llvm/llvm-project/commit/5aa15abead01fef21fee4706aa01f39e8ac18829.diff

LOG: [ARM] - Fix Thumb1InstrInfo::copyPhysReg crash on debug instructions 
during liveness walk. (#209478)

clang asserts when compiling with -mthumb -mcpu=arm926ej-s -O0 -g
(pre-v6 Thumb1, debug info enabled). The crash occurs in
LiveRegUnits::stepBackward which unconditionally asserts that it must
never be called on a debug instruction, but Thumb1InstrInfo::copyPhysReg
walks the basic block backward without skipping DBG_VALUE instructions.

Fixes: https://github.com/llvm/llvm-project/issues/209475
(cherry picked from commit 73615e792dc3116d5c66cb8175039dd6d4840f31)

Added: 
    llvm/test/CodeGen/ARM/thumb1-copy-physreg-dbg-value-crash.ll

Modified: 
    llvm/lib/Target/ARM/Thumb1InstrInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/ARM/Thumb1InstrInfo.cpp 
b/llvm/lib/Target/ARM/Thumb1InstrInfo.cpp
index c2345f588b937..ef67e1116af72 100644
--- a/llvm/lib/Target/ARM/Thumb1InstrInfo.cpp
+++ b/llvm/lib/Target/ARM/Thumb1InstrInfo.cpp
@@ -62,10 +62,14 @@ void Thumb1InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
     UsedRegs.addLiveOuts(MBB);
 
     auto InstUpToI = MBB.end();
-    while (InstUpToI != I)
+    while (InstUpToI != I) {
       // The pre-decrement is on purpose here.
       // We want to have the liveness right before I.
-      UsedRegs.stepBackward(*--InstUpToI);
+      --InstUpToI;
+      if (InstUpToI->isDebugInstr())
+        continue;
+      UsedRegs.stepBackward(*InstUpToI);
+    }
 
     if (UsedRegs.available(ARM::CPSR)) {
       BuildMI(MBB, I, DL, get(ARM::tMOVSr), DestReg)

diff  --git a/llvm/test/CodeGen/ARM/thumb1-copy-physreg-dbg-value-crash.ll 
b/llvm/test/CodeGen/ARM/thumb1-copy-physreg-dbg-value-crash.ll
new file mode 100644
index 0000000000000..0ff18f1f56d3c
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/thumb1-copy-physreg-dbg-value-crash.ll
@@ -0,0 +1,25 @@
+; RUN: llc < %s -mtriple=thumbv5e-none-linux-gnueabi --float-abi=soft 
-verify-machineinstrs -o /dev/null
+; This used to crash in Thumb1InstrInfo::copyPhysReg when computing liveness
+; for a pre-v6 low GPR copy. The backward liveness walk from MBB.end() to the
+; COPY instruction was calling LiveRegUnits::stepBackward on DBG_VALUE
+; instructions, which asserts that debug instructions must not affect liveness
+; calculation.
+
+define void @foo(ptr %res) !dbg !3 {
+entry:
+  #dbg_declare(ptr %res, !4, !DIExpression(), !5)
+  call void @llvm.memcpy.p0.p0.i32(ptr %res, ptr null, i32 1, i1 false)
+  ret void
+}
+
+declare void @llvm.memcpy.p0.p0.i32(ptr noalias writeonly captures(none), ptr 
noalias readonly captures(none), i32, i1 immarg)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: 
FullDebug)
+!1 = !DIFile(filename: "t.c", directory: "")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, type: 
!DISubroutineType(types: !{}), unit: !0)
+!4 = !DILocalVariable(scope: !3)
+!5 = !DILocation(line: 1, scope: !3)


        
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to