[llvm] CodeGen: Fix lookThruCopyLike crash on an undef register (PR #216598)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 12:43:46 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/216598

Found by AI while working on something else.

Co-authored-by: Claude (Opus 4.8) <noreply at anthropic.com>

>From 4a96a45c95fc8fed085a931087e467fbbe1ea375 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sun, 16 Aug 2026 10:52:42 +0200
Subject: [PATCH] CodeGen: Fix lookThruCopyLike crash on an undef register

Found by AI while working on something else.

Co-authored-by: Claude (Opus 4.8) <noreply at anthropic.com>
---
 llvm/lib/CodeGen/TargetRegisterInfo.cpp       |  2 +-
 .../X86/machinelicm-lookthru-undef.mir        | 39 +++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/X86/machinelicm-lookthru-undef.mir

diff --git a/llvm/lib/CodeGen/TargetRegisterInfo.cpp b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
index e9aeb457c7294..351fae37ab891 100644
--- a/llvm/lib/CodeGen/TargetRegisterInfo.cpp
+++ b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
@@ -608,7 +608,7 @@ TargetRegisterInfo::lookThruCopyLike(Register SrcReg,
                                      const MachineRegisterInfo *MRI) const {
   while (true) {
     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
-    if (!MI->isCopyLike())
+    if (!MI || !MI->isCopyLike())
       return SrcReg;
 
     Register CopySrcReg;
diff --git a/llvm/test/CodeGen/X86/machinelicm-lookthru-undef.mir b/llvm/test/CodeGen/X86/machinelicm-lookthru-undef.mir
new file mode 100644
index 0000000000000..1f2e9dfb161c4
--- /dev/null
+++ b/llvm/test/CodeGen/X86/machinelicm-lookthru-undef.mir
@@ -0,0 +1,39 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=x86_64-- -run-pass=early-machinelicm -hoist-const-stores -o - %s | FileCheck %s
+# A store operand that is a copy of an undef register with no def must not crash
+# MachineLICM while it looks through the copy to check for an invariant store.
+
+---
+name: lookthru_undef
+tracksRegLiveness: true
+body: |
+  ; CHECK-LABEL: name: lookthru_undef
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[COPY:%[0-9]+]]:gr64 = COPY undef %1:gr64
+  ; CHECK-NEXT:   JMP_1 %bb.1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   successors: %bb.1(0x40000000), %bb.2(0x40000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   MOV32mr [[COPY]], 1, $noreg, 0, $noreg, undef %2:gr32 :: (store (s32))
+  ; CHECK-NEXT:   JCC_1 %bb.1, 5, implicit undef $eflags
+  ; CHECK-NEXT:   JMP_1 %bb.2
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   RET64
+  bb.0:
+    successors: %bb.1
+    %0:gr64 = COPY undef %1:gr64
+    JMP_1 %bb.1
+
+  bb.1:
+    successors: %bb.1, %bb.2
+    MOV32mr %0, 1, $noreg, 0, $noreg, undef %2:gr32 :: (store (s32))
+    JCC_1 %bb.1, 5, implicit undef $eflags
+    JMP_1 %bb.2
+
+  bb.2:
+    RET64
+...



More information about the llvm-commits mailing list