Skip to content

[RISCV] Simplify one of the RV32 PACK isel patterns. #152045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,15 @@ def : Pat<(binop_allhusers<or> (shl GPR:$rs2, (XLenVT 8)),
let Predicates = [HasStdExtZbkb, IsRV32] in {
def : Pat<(i32 (or (zexti16 (i32 GPR:$rs1)), (shl GPR:$rs2, (i32 16)))),
(PACK GPR:$rs1, GPR:$rs2)>;
def : Pat<(or (or
(shl (zexti8 (XLenVT GPR:$op1rs2)), (XLenVT 24)),

// Match a pattern of 2 bytes being inserted into bits [31:16], with bits
// bits [15:0] coming from a zero extended value. We can use pack with packh for
// bits [31:16]. If bits [15:0] can also be a packh, it can be matched
// separately.
def : Pat<(or (or (shl (zexti8 (XLenVT GPR:$op1rs2)), (XLenVT 24)),
(shl (zexti8 (XLenVT GPR:$op1rs1)), (XLenVT 16))),
(or
(shl (zexti8 (XLenVT GPR:$op0rs2)), (XLenVT 8)),
(zexti8 (XLenVT GPR:$op0rs1)))),
(PACK (XLenVT (PACKH GPR:$op0rs1, GPR:$op0rs2)),
(zexti16 (XLenVT GPR:$rs1))),
(PACK (XLenVT GPR:$rs1),
(XLenVT (PACKH GPR:$op1rs1, GPR:$op1rs2)))>;
}

Expand Down
80 changes: 80 additions & 0 deletions llvm/test/CodeGen/RISCV/rv32zbkb.ll
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,83 @@ define i64 @zext_i16_to_i64(i16 %a) nounwind {
%1 = zext i16 %a to i64
ret i64 %1
}

define i32 @pack_lo_packh_hi_packh(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) nounwind {
; RV32I-LABEL: pack_lo_packh_hi_packh:
; RV32I: # %bb.0:
; RV32I-NEXT: slli a1, a1, 8
; RV32I-NEXT: slli a2, a2, 16
; RV32I-NEXT: slli a3, a3, 24
; RV32I-NEXT: or a0, a0, a1
; RV32I-NEXT: or a2, a2, a3
; RV32I-NEXT: or a0, a0, a2
; RV32I-NEXT: ret
;
; RV32ZBKB-LABEL: pack_lo_packh_hi_packh:
; RV32ZBKB: # %bb.0:
; RV32ZBKB-NEXT: packh a0, a0, a1
; RV32ZBKB-NEXT: packh a1, a2, a3
; RV32ZBKB-NEXT: pack a0, a0, a1
; RV32ZBKB-NEXT: ret
%a = zext i8 %0 to i32
%b = zext i8 %1 to i32
%c = zext i8 %2 to i32
%d = zext i8 %3 to i32
%e = shl i32 %b, 8
%f = shl i32 %c, 16
%g = shl i32 %d, 24
%h = or i32 %a, %e
%i = or i32 %h, %f
%j = or i32 %i, %g
ret i32 %j
}

define i32 @pack_lo_zext_hi_packh(i16 zeroext %0, i8 zeroext %1, i8 zeroext %2) nounwind {
; RV32I-LABEL: pack_lo_zext_hi_packh:
; RV32I: # %bb.0:
; RV32I-NEXT: slli a1, a2, 16
; RV32I-NEXT: slli a2, a2, 24
; RV32I-NEXT: or a1, a2, a1
; RV32I-NEXT: or a0, a1, a0
; RV32I-NEXT: ret
;
; RV32ZBKB-LABEL: pack_lo_zext_hi_packh:
; RV32ZBKB: # %bb.0:
; RV32ZBKB-NEXT: packh a1, a2, a2
; RV32ZBKB-NEXT: pack a0, a0, a1
; RV32ZBKB-NEXT: ret
%a = zext i16 %0 to i32
%b = zext i8 %1 to i32
%c = zext i8 %2 to i32
%d = shl i32 %c, 8
%e = or i32 %c, %d
%f = shl i32 %e, 16
%g = or i32 %f, %a
ret i32 %g
}

; Negative test, %a isn't extended so we can't use pack for the outer or, but
; we can use packh for the high half.
define i32 @pack_lo_noext_hi_packh(i32 %a, i8 zeroext %1, i8 zeroext %2) nounwind {
; RV32I-LABEL: pack_lo_noext_hi_packh:
; RV32I: # %bb.0:
; RV32I-NEXT: slli a1, a2, 16
; RV32I-NEXT: slli a2, a2, 24
; RV32I-NEXT: or a1, a2, a1
; RV32I-NEXT: or a0, a1, a0
; RV32I-NEXT: ret
;
; RV32ZBKB-LABEL: pack_lo_noext_hi_packh:
; RV32ZBKB: # %bb.0:
; RV32ZBKB-NEXT: packh a1, a2, a2
; RV32ZBKB-NEXT: slli a1, a1, 16
; RV32ZBKB-NEXT: or a0, a1, a0
; RV32ZBKB-NEXT: ret
%b = zext i8 %1 to i32
%c = zext i8 %2 to i32
%d = shl i32 %c, 8
%e = or i32 %c, %d
%f = shl i32 %e, 16
%g = or i32 %f, %a
ret i32 %g
}