Skip to content

Commit 0d9466f

Browse files
adriwebcodex
andcommitted
[Z80][GISel] Fix eZ80 s16 shift-by-const selection from o16 sources.
The eZ80 G_SHL selector path for s16 shifts by 2..6 was constraining the source vreg to A16 before emitting: SUBREG_TO_REG COPY to UHL repeated ADD24aa COPY low 16 bits from HL That source constraint was unnecessary and rejects valid inputs such as o16 virtual registers, causing instruction selection failures like: cannot select: %...:o16(s16) = G_SHL %...:o16, %...:gpr(s8) Remove the unnecessary source constraint and constrain the fallback destination to R16 instead of A16 after copying the low 16-bit result out of HL. This fixes backend crashes seen in real code such as invert_palette from dino-run-ce, and adds a MIR regression test covering G_SHL on an o16 s16 input in eZ80 mode. Co-Authored-By: Codex CLI <codex@openai.com>
1 parent d189354 commit 0d9466f

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

llvm/lib/Target/Z80/GISel/Z80InstructionSelector.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,8 +2962,6 @@ bool Z80InstructionSelector::selectShift(MachineInstr &I,
29622962
.addImm(0) // upper bits undefined (don't care)
29632963
.addReg(SrcReg) // source s16
29642964
.addImm(Z80::sub_short);
2965-
if (!RBI.constrainGenericRegister(SrcReg, Z80::A16RegClass, MRI))
2966-
return false;
29672965

29682966
// copy to physical UHL for ADD24aa operations
29692967
BuildMI(MBB, InsertPt, DL, TII.get(TargetOpcode::COPY), Z80::UHL)
@@ -2979,7 +2977,7 @@ bool Z80InstructionSelector::selectShift(MachineInstr &I,
29792977
MIB.buildCopy(DstReg, Register(Z80::HL));
29802978

29812979
const TargetRegisterClass *DstRC = MRI.getRegClassOrNull(DstReg);
2982-
if (!DstRC && !RBI.constrainGenericRegister(DstReg, Z80::A16RegClass, MRI)) {
2980+
if (!DstRC && !RBI.constrainGenericRegister(DstReg, Z80::R16RegClass, MRI)) {
29832981
return false;
29842982
}
29852983
I.eraseFromParent();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# RUN: llc -O0 -mtriple=ez80-none-elf -run-pass=instruction-select -verify-machineinstrs %s -o - | FileCheck %s
2+
3+
--- |
4+
define i16 @shl_s16_o16(i16 %x) { entry: ret i16 0 }
5+
...
6+
7+
---
8+
name: shl_s16_o16
9+
alignment: 1
10+
legalized: true
11+
regBankSelected: true
12+
tracksRegLiveness: true
13+
body: |
14+
bb.0.entry:
15+
liveins: $de
16+
17+
; CHECK-LABEL: name: shl_s16_o16
18+
; CHECK: [[EXT:%[0-9]+]]:a24 = SUBREG_TO_REG 0, [[SRC:%[0-9]+]], %subreg.sub_short
19+
; CHECK: $uhl = COPY [[EXT]]
20+
; CHECK: $uhl = ADD24aa $uhl
21+
; CHECK: $uhl = ADD24aa $uhl
22+
; CHECK: $uhl = ADD24aa $uhl
23+
; CHECK: $uhl = ADD24aa $uhl
24+
; CHECK: $uhl = ADD24aa $uhl
25+
; CHECK: [[DST:%[0-9]+]]:o16 = COPY $hl
26+
; CHECK: $hl = COPY [[DST]]
27+
; CHECK: RET24 implicit-def $spl, implicit $spl
28+
%0:o16(s16) = COPY $de
29+
%1:gpr(s8) = G_CONSTANT i8 5
30+
%2:o16(s16) = G_SHL %0, %1(s8)
31+
$hl = COPY %2(s16)
32+
RET24 implicit-def $spl, implicit $spl
33+
...

0 commit comments

Comments
 (0)