[llvm] PowerPC: Fix MI peephole crash on an undef forwarding operand (PR #216685)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 17 03:27:49 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-powerpc

Author: Matt Arsenault (arsenm)

<details>
<summary>Changes</summary>

Found by AI while working on something else.

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

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


2 Files Affected:

- (modified) llvm/lib/Target/PowerPC/PPCInstrInfo.cpp (+2) 
- (added) llvm/test/CodeGen/PowerPC/mi-peephole-forwarding-undef.mir (+25) 


``````````diff
diff --git a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
index 85301285a8237..d10e8b1c974f4 100644
--- a/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
+++ b/llvm/lib/Target/PowerPC/PPCInstrInfo.cpp
@@ -3501,6 +3501,8 @@ MachineInstr *PPCInstrInfo::getForwardingDefMI(
       Register TrueReg = RI.lookThruCopyLike(Reg, MRI);
       if (TrueReg.isVirtual()) {
         MachineInstr *DefMIForTrueReg = MRI->getVRegDef(TrueReg);
+        if (!DefMIForTrueReg)
+          continue;
         if (DefMIForTrueReg->getOpcode() == PPC::LI ||
             DefMIForTrueReg->getOpcode() == PPC::LI8 ||
             DefMIForTrueReg->getOpcode() == PPC::ADDI ||
diff --git a/llvm/test/CodeGen/PowerPC/mi-peephole-forwarding-undef.mir b/llvm/test/CodeGen/PowerPC/mi-peephole-forwarding-undef.mir
new file mode 100644
index 0000000000000..8009bc6255097
--- /dev/null
+++ b/llvm/test/CodeGen/PowerPC/mi-peephole-forwarding-undef.mir
@@ -0,0 +1,25 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=powerpc64le-unknown-linux-gnu -run-pass=ppc-mi-peepholes -o - %s | FileCheck %s
+
+# The MI peephole must not crash when a forwarding-eligible operand is an undef
+# register with no def.
+---
+name: forwarding_undef
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $x3
+    ; CHECK-LABEL: name: forwarding_undef
+    ; CHECK: liveins: $x3
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:g8rc = COPY killed $x3
+    ; CHECK-NEXT: [[ADDI8_:%[0-9]+]]:g8rc = ADDI8 undef %2:g8rc_and_g8rc_nox0, 4
+    ; CHECK-NEXT: [[ADD8_:%[0-9]+]]:g8rc = ADD8 killed [[COPY]], killed [[ADDI8_]]
+    ; CHECK-NEXT: $x3 = COPY killed [[ADD8_]]
+    ; CHECK-NEXT: BLR8 implicit $lr8, implicit $rm, implicit killed $x3
+    %0:g8rc = COPY $x3
+    %1:g8rc = ADDI8 undef %2:g8rc_and_g8rc_nox0, 4
+    %3:g8rc = ADD8 %0, %1
+    $x3 = COPY %3
+    BLR8 implicit $lr8, implicit $rm, implicit $x3
+...

``````````

</details>


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


More information about the llvm-commits mailing list