[llvm] Hexagon: Fix constant propagation crash on a use with no reaching definition (PR #220297)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 1 09:55:56 PDT 2026


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

The machine constant propagator seeds a lattice cell for a virtual register
when it visits that register's definition. A register whose definition is
never visited (e.g. an undef use) therefore has no cell. The instruction
evaluators and rewriters assumed every register use has a cell and asserted
so, crashing on such uses. Bail out of evaluation and rewriting when an input
use has no cell, and treat a branch predicate with no cell as unknown.

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

>From ae6be7cf0f7d2621043a2aa7ed3851f80148be42 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 20 Aug 2026 00:50:47 +0200
Subject: [PATCH] Hexagon: Fix constant propagation crash on a use with no
 reaching definition

The machine constant propagator seeds a lattice cell for a virtual register
when it visits that register's definition. A register whose definition is
never visited (e.g. an undef use) therefore has no cell. The instruction
evaluators and rewriters assumed every register use has a cell and asserted
so, crashing on such uses. Bail out of evaluation and rewriting when an input
use has no cell, and treat a branch predicate with no cell as unknown.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>
---
 .../Hexagon/HexagonConstPropagation.cpp       |  17 +-
 .../test/CodeGen/Hexagon/const-prop-undef.mir | 314 ++++++++++++++++++
 2 files changed, 330 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/Hexagon/const-prop-undef.mir

diff --git a/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp b/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp
index 3498636000912..b77d13958c6e2 100644
--- a/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp
@@ -1935,6 +1935,12 @@ bool HexagonConstEvaluator::evaluate(const MachineInstr &MI,
   if (!DefR.Reg.isVirtual())
     return false;
 
+  // The evaluators below assume every register use has a cell.
+  for (const MachineOperand &MO : MI.uses()) {
+    if (MO.isReg() && MO.getReg().isVirtual() && !Inputs.has(MO.getReg()))
+      return false;
+  }
+
   if (MI.isCopy()) {
     LatticeCell RC;
     RegSubRegPair SrcR(getRegSubRegPair(MI.getOperand(1)));
@@ -2296,7 +2302,10 @@ bool HexagonConstEvaluator::evaluate(const MachineInstr &BrI,
     // we currently recognize.
     if (PR.SubReg)
       goto Undetermined;
-    assert(Inputs.has(PR.Reg));
+    // A predicate with no reaching definition (e.g. an undef operand) has no
+    // cell; its value is unknown.
+    if (!Inputs.has(PR.Reg))
+      goto Undetermined;
     const LatticeCell &PredC = Inputs.get(PR.Reg);
     if (PredC.isBottom())
       goto Undetermined;
@@ -2329,6 +2338,12 @@ bool HexagonConstEvaluator::rewrite(MachineInstr &MI, const CellMap &Inputs) {
   if (MI.isBranch())
     return rewriteHexBranch(MI, Inputs);
 
+  // The rewriters below assume every register use has a cell.
+  for (const MachineOperand &MO : MI.uses()) {
+    if (MO.isReg() && MO.getReg().isVirtual() && !Inputs.has(MO.getReg()))
+      return false;
+  }
+
   unsigned Opc = MI.getOpcode();
   switch (Opc) {
     default:
diff --git a/llvm/test/CodeGen/Hexagon/const-prop-undef.mir b/llvm/test/CodeGen/Hexagon/const-prop-undef.mir
new file mode 100644
index 0000000000000..3a9db3d65210a
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/const-prop-undef.mir
@@ -0,0 +1,314 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=hexagon -run-pass=hexagon-constp -o - %s | FileCheck %s
+
+# Constant propagation must not crash on a use with no reaching
+# definition. Each function feeds an undef register into a different
+# evaluator path.
+
+---
+name: cmp_rr
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: cmp_rr
+    ; CHECK: [[C2_cmpeq:%[0-9]+]]:predregs = C2_cmpeq undef %1:intregs, undef %2:intregs
+    ; CHECK-NEXT: [[C2_tfrpr:%[0-9]+]]:intregs = C2_tfrpr [[C2_cmpeq]]
+    ; CHECK-NEXT: $r0 = COPY [[C2_tfrpr]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:predregs = C2_cmpeq undef %1:intregs, undef %2:intregs
+    %3:intregs = C2_tfrpr %0
+    $r0 = COPY %3
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: cmp_rp
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: cmp_rp
+    ; CHECK: [[C2_cmpgtp:%[0-9]+]]:predregs = C2_cmpgtp undef %1:doubleregs, undef %2:doubleregs
+    ; CHECK-NEXT: [[C2_tfrpr:%[0-9]+]]:intregs = C2_tfrpr [[C2_cmpgtp]]
+    ; CHECK-NEXT: $r0 = COPY [[C2_tfrpr]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:predregs = C2_cmpgtp undef %1:doubleregs, undef %2:doubleregs
+    %3:intregs = C2_tfrpr %0
+    $r0 = COPY %3
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: logical_and
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: logical_and
+    ; CHECK: [[A2_and:%[0-9]+]]:intregs = A2_and undef %1:intregs, undef %2:intregs
+    ; CHECK-NEXT: $r0 = COPY [[A2_and]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = A2_and undef %1:intregs, undef %2:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: logical_andir
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: logical_andir
+    ; CHECK: [[A2_andir:%[0-9]+]]:intregs = A2_andir undef %1:intregs, 7
+    ; CHECK-NEXT: $r0 = COPY [[A2_andir]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = A2_andir undef %1:intregs, 7
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: logical_or
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: logical_or
+    ; CHECK: [[A2_or:%[0-9]+]]:intregs = A2_or undef %1:intregs, undef %2:intregs
+    ; CHECK-NEXT: $r0 = COPY [[A2_or]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = A2_or undef %1:intregs, undef %2:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: logical_xor
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: logical_xor
+    ; CHECK: [[A2_xor:%[0-9]+]]:intregs = A2_xor undef %1:intregs, undef %2:intregs
+    ; CHECK-NEXT: $r0 = COPY [[A2_xor]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = A2_xor undef %1:intregs, undef %2:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: condmove_mux
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: condmove_mux
+    ; CHECK: [[C2_mux:%[0-9]+]]:intregs = C2_mux undef %1:predregs, undef %2:intregs, undef %3:intregs
+    ; CHECK-NEXT: $r0 = COPY [[C2_mux]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = C2_mux undef %1:predregs, undef %2:intregs, undef %3:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: condmove_muxir
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: condmove_muxir
+    ; CHECK: [[C2_muxir:%[0-9]+]]:intregs = C2_muxir undef %1:predregs, undef %2:intregs, 5
+    ; CHECK-NEXT: $r0 = COPY [[C2_muxir]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = C2_muxir undef %1:predregs, undef %2:intregs, 5
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: ext_sxtb
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: ext_sxtb
+    ; CHECK: [[A2_sxtb:%[0-9]+]]:intregs = A2_sxtb undef %1:intregs
+    ; CHECK-NEXT: $r0 = COPY [[A2_sxtb]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = A2_sxtb undef %1:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: ext_zxth
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: ext_zxth
+    ; CHECK: [[A2_zxth:%[0-9]+]]:intregs = A2_zxth undef %1:intregs
+    ; CHECK-NEXT: $r0 = COPY [[A2_zxth]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = A2_zxth undef %1:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: ext_sxtw
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: ext_sxtw
+    ; CHECK: [[A2_sxtw:%[0-9]+]]:doubleregs = A2_sxtw undef %1:intregs
+    ; CHECK-NEXT: $d0 = COPY [[A2_sxtw]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $d0
+    %0:doubleregs = A2_sxtw undef %1:intregs
+    $d0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $d0
+...
+
+---
+name: rseq_combinew
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: rseq_combinew
+    ; CHECK: [[REG_SEQUENCE:%[0-9]+]]:doubleregs = REG_SEQUENCE undef %1:intregs, %subreg.isub_hi, undef %2:intregs, %subreg.isub_lo
+    ; CHECK-NEXT: $d0 = COPY [[REG_SEQUENCE]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $d0
+    %0:doubleregs = REG_SEQUENCE undef %1:intregs, %subreg.isub_hi, undef %2:intregs, %subreg.isub_lo
+    $d0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $d0
+...
+
+---
+name: vec1_clb
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: vec1_clb
+    ; CHECK: [[S2_clb:%[0-9]+]]:intregs = S2_clb undef %1:intregs
+    ; CHECK-NEXT: $r0 = COPY [[S2_clb]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = S2_clb undef %1:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: vec1_ct0
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: vec1_ct0
+    ; CHECK: [[S2_ct0_:%[0-9]+]]:intregs = S2_ct0 undef %1:intregs
+    ; CHECK-NEXT: $r0 = COPY [[S2_ct0_]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = S2_ct0 undef %1:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: vec1_extractu
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: vec1_extractu
+    ; CHECK: [[S2_extractu:%[0-9]+]]:intregs = S2_extractu undef %1:intregs, 5, 2
+    ; CHECK-NEXT: $r0 = COPY [[S2_extractu]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = S2_extractu undef %1:intregs, 5, 2
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: vec1_extract
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: vec1_extract
+    ; CHECK: [[S4_extract:%[0-9]+]]:intregs = S4_extract undef %1:intregs, 5, 2
+    ; CHECK-NEXT: $r0 = COPY [[S4_extract]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = S4_extract undef %1:intregs, 5, 2
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: vec1_vsplatrb
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: vec1_vsplatrb
+    ; CHECK: [[S2_vsplatrb:%[0-9]+]]:intregs = S2_vsplatrb undef %1:intregs
+    ; CHECK-NEXT: $r0 = COPY [[S2_vsplatrb]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = S2_vsplatrb undef %1:intregs
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: setbit
+tracksRegLiveness: true
+body: |
+  bb.0:
+    ; CHECK-LABEL: name: setbit
+    ; CHECK: [[S2_setbit_i:%[0-9]+]]:intregs = S2_setbit_i undef %1:intregs, 3
+    ; CHECK-NEXT: $r0 = COPY [[S2_setbit_i]]
+    ; CHECK-NEXT: J2_jumpr $r31, implicit-def $pc, implicit $r0
+    %0:intregs = S2_setbit_i undef %1:intregs, 3
+    $r0 = COPY %0
+    J2_jumpr $r31, implicit-def $pc, implicit $r0
+...
+
+---
+name: branch_jumpt
+tracksRegLiveness: true
+body: |
+  ; CHECK-LABEL: name: branch_jumpt
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.2(0x40000000), %bb.1(0x40000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   J2_jumpt undef %0:predregs, %bb.2, implicit-def $pc
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   J2_jumpr $r31, implicit-def $pc
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   J2_jumpr $r31, implicit-def $pc
+  bb.0:
+    successors: %bb.2, %bb.1
+    J2_jumpt undef %0:predregs, %bb.2, implicit-def $pc
+
+  bb.1:
+    J2_jumpr $r31, implicit-def $pc
+
+  bb.2:
+    J2_jumpr $r31, implicit-def $pc
+...
+
+---
+name: branch_jumpf
+tracksRegLiveness: true
+body: |
+  ; CHECK-LABEL: name: branch_jumpf
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.2(0x40000000), %bb.1(0x40000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   J2_jumpf undef %0:predregs, %bb.2, implicit-def $pc
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   J2_jumpr $r31, implicit-def $pc
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2:
+  ; CHECK-NEXT:   J2_jumpr $r31, implicit-def $pc
+  bb.0:
+    successors: %bb.2, %bb.1
+    J2_jumpf undef %0:predregs, %bb.2, implicit-def $pc
+
+  bb.1:
+    J2_jumpr $r31, implicit-def $pc
+
+  bb.2:
+    J2_jumpr $r31, implicit-def $pc
+...



More information about the llvm-commits mailing list