[llvm] AArch64: Fix optimizeCondBranch crash on an undef register (PR #216593)

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


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

Found by AI while working on something else.

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

>From 2c6acb4367360fb8c0df25f57f63f0a10ffa1fe9 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sun, 16 Aug 2026 12:23:29 +0200
Subject: [PATCH] AArch64: Fix optimizeCondBranch 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/Target/AArch64/AArch64InstrInfo.cpp  |  2 ++
 .../AArch64/optimize-cond-branch-undef.mir    | 32 +++++++++++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/optimize-cond-branch-undef.mir

diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 2f31686b4f19b..bd4cacc737fd7 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -10090,6 +10090,8 @@ bool AArch64InstrInfo::optimizeCondBranch(MachineInstr &MI) const {
     return false;
 
   MachineInstr *DefMI = MRI->getVRegDef(VReg);
+  if (!DefMI)
+    return false;
 
   // Look through COPY instructions to find definition.
   while (DefMI->isCopy()) {
diff --git a/llvm/test/CodeGen/AArch64/optimize-cond-branch-undef.mir b/llvm/test/CodeGen/AArch64/optimize-cond-branch-undef.mir
new file mode 100644
index 0000000000000..7a4d9b98ca1a1
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/optimize-cond-branch-undef.mir
@@ -0,0 +1,32 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=aarch64-- -run-pass=peephole-opt -o - %s | FileCheck %s
+# A conditional branch on an undef register with no def must not crash the
+# peephole optimizer while it looks through the copy chain for the definition.
+
+---
+name: cbz_undef
+tracksRegLiveness: true
+body: |
+  ; CHECK-LABEL: name: cbz_undef
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x40000000), %bb.2(0x40000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   CBZW undef %0:gpr32, %bb.2
+  ; CHECK-NEXT:   B %bb.1
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   RET_ReallyLR
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   RET_ReallyLR
+  bb.0:
+    successors: %bb.1, %bb.2
+    CBZW undef %0:gpr32, %bb.2
+    B %bb.1
+
+  bb.1:
+    RET_ReallyLR
+
+  bb.2:
+    RET_ReallyLR
+...



More information about the llvm-commits mailing list