Skip to content

Commit 544830a

Browse files
authored
[RISCV] Rework zext.h handling for Zbkb again. (#96957)
Use the Zbb zext.h nstructions only when Zbb is enabled. In both the assembler and codegen. Use pack/packw for zext.h when Zbkb is enabled, but Zbb is not. This is accomplished with extra isel patterns for CodeGen and InstAliases for the assembler that are used with Zbkb and not Zbb. This fixes the quirk that the assembler and disassembler printed something different for pack rd, rs1, x0.
1 parent 897489b commit 544830a

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

llvm/lib/Target/RISCV/RISCVFeatures.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ def FeatureStdExtZbb
458458
def HasStdExtZbb : Predicate<"Subtarget->hasStdExtZbb()">,
459459
AssemblerPredicate<(all_of FeatureStdExtZbb),
460460
"'Zbb' (Basic Bit-Manipulation)">;
461+
def NoStdExtZbb : Predicate<"!Subtarget->hasStdExtZbb()">,
462+
AssemblerPredicate<(all_of (not FeatureStdExtZbb))>;
461463

462464
def FeatureStdExtZbc
463465
: RISCVExtension<"zbc", 1, 0,

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,22 @@ let Predicates = [HasStdExtZbkb, IsRV64], IsSignExtendingOpW = 1 in
415415
def PACKW : ALUW_rr<0b0000100, 0b100, "packw">,
416416
Sched<[WritePACK32, ReadPACK32, ReadPACK32]>;
417417

418-
let Predicates = [HasStdExtZbbOrZbkb, IsRV32] in {
418+
let Predicates = [HasStdExtZbb, IsRV32] in {
419419
def ZEXT_H_RV32 : RVBUnary<0b000010000000, 0b100, OPC_OP, "zext.h">,
420420
Sched<[WriteIALU, ReadIALU]>;
421+
} // Predicates = [HasStdExtZbb, IsRV32]
422+
423+
let Predicates = [HasStdExtZbb, IsRV64], IsSignExtendingOpW = 1 in {
424+
def ZEXT_H_RV64 : RVBUnary<0b000010000000, 0b100, OPC_OP_32, "zext.h">,
425+
Sched<[WriteIALU, ReadIALU]>;
426+
} // Predicates = [HasStdExtZbb, IsRV64]
427+
428+
let Predicates = [HasStdExtZbbOrZbkb, IsRV32] in {
421429
def REV8_RV32 : RVBUnary<0b011010011000, 0b101, OPC_OP_IMM, "rev8">,
422430
Sched<[WriteREV8, ReadREV8]>;
423431
} // Predicates = [HasStdExtZbbOrZbkb, IsRV32]
424432

425433
let Predicates = [HasStdExtZbbOrZbkb, IsRV64] in {
426-
let IsSignExtendingOpW = 1 in
427-
def ZEXT_H_RV64 : RVBUnary<0b000010000000, 0b100, OPC_OP_32, "zext.h">,
428-
Sched<[WriteIALU, ReadIALU]>;
429434
def REV8_RV64 : RVBUnary<0b011010111000, 0b101, OPC_OP_IMM, "rev8">,
430435
Sched<[WriteREV8, ReadREV8]>;
431436
} // Predicates = [HasStdExtZbbOrZbkb, IsRV64]
@@ -476,6 +481,14 @@ def : InstAlias<"bext $rd, $rs1, $shamt",
476481
(BEXTI GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt), 0>;
477482
} // Predicates = [HasStdExtZbs]
478483

484+
let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV32] in {
485+
def : InstAlias<"zext.h $rd, $rs", (PACK GPR:$rd, GPR:$rs, X0)>;
486+
} // Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV32]
487+
488+
let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in {
489+
def : InstAlias<"zext.h $rd, $rs", (PACKW GPR:$rd, GPR:$rs, X0)>;
490+
} // Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64]
491+
479492
//===----------------------------------------------------------------------===//
480493
// Codegen patterns
481494
//===----------------------------------------------------------------------===//
@@ -632,11 +645,16 @@ def : Pat<(i64 (or (sext_inreg (shl GPR:$rs2, (i64 16)), i32),
632645
(PACKW GPR:$rs1, GPR:$rs2)>;
633646
} // Predicates = [HasStdExtZbkb, IsRV64]
634647

635-
let Predicates = [HasStdExtZbbOrZbkb, IsRV32] in
648+
let Predicates = [HasStdExtZbb, IsRV32] in
636649
def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (ZEXT_H_RV32 GPR:$rs)>;
637-
let Predicates = [HasStdExtZbbOrZbkb, IsRV64] in
650+
let Predicates = [HasStdExtZbb, IsRV64] in
638651
def : Pat<(i64 (and GPR:$rs, 0xFFFF)), (ZEXT_H_RV64 GPR:$rs)>;
639652

653+
let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV32] in
654+
def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (PACK GPR:$rs, (XLenVT X0))>;
655+
let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in
656+
def : Pat<(i64 (and GPR:$rs, 0xFFFF)), (PACKW GPR:$rs, (XLenVT X0))>;
657+
640658
let Predicates = [HasStdExtZba] in {
641659

642660
foreach i = {1,2,3} in {
@@ -743,11 +761,13 @@ def : PatGpr<ctpop, CPOPW, i32>;
743761

744762
def : Pat<(i32 (sext_inreg GPR:$rs1, i8)), (SEXT_B GPR:$rs1)>;
745763
def : Pat<(i32 (sext_inreg GPR:$rs1, i16)), (SEXT_H GPR:$rs1)>;
746-
} // Predicates = [HasStdExtZbb, IsRV64]
747764

748-
let Predicates = [HasStdExtZbbOrZbkb, IsRV64] in {
749765
def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (ZEXT_H_RV64 GPR:$rs)>;
750-
} // Predicates = [HasStdExtZbbOrZbkb, IsRV64]
766+
} // Predicates = [HasStdExtZbb, IsRV64]
767+
768+
let Predicates = [HasStdExtZbkb, NoStdExtZbb, IsRV64] in {
769+
def : Pat<(i32 (and GPR:$rs, 0xFFFF)), (PACKW GPR:$rs, (XLenVT X0))>;
770+
}
751771

752772
let Predicates = [HasStdExtZbbOrZbkb, IsRV64] in {
753773
def : Pat<(i32 (and GPR:$rs1, (not GPR:$rs2))), (ANDN GPR:$rs1, GPR:$rs2)>;

llvm/test/MC/RISCV/rv32zbkb-only-valid.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: | FileCheck -check-prefixes=CHECK-ASM,CHECK-ASM-AND-OBJ %s
33
# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+zbkb < %s \
44
# RUN: | llvm-objdump --mattr=+zbkb -d -r - \
5-
# RUN: | FileCheck --check-prefixes=CHECK-OBJ,CHECK-ASM-AND-OBJ %s
5+
# RUN: | FileCheck --check-prefix=CHECK-ASM-AND-OBJ %s
66

77
# CHECK-ASM-AND-OBJ: rev8 t0, t1
88
# CHECK-ASM: encoding: [0x93,0x52,0x83,0x69]
@@ -16,8 +16,7 @@ zip t0, t1
1616
unzip t0, t1
1717

1818
# Test the encoding used for zext.h for RV32.
19-
# CHECK-ASM: pack t0, t1, zero
20-
# CHECK-OBJ: zext.h t0, t1
19+
# CHECK-ASM-AND-OBJ: zext.h t0, t1
2120
# CHECK-ASM: encoding: [0xb3,0x42,0x03,0x08]
2221
pack t0, t1, x0
2322

llvm/test/MC/RISCV/rv64zbkb-valid.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: | FileCheck -check-prefixes=CHECK-ASM,CHECK-ASM-AND-OBJ %s
33
# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+zbkb < %s \
44
# RUN: | llvm-objdump --mattr=+zbkb --no-print-imm-hex -d -r - \
5-
# RUN: | FileCheck --check-prefixes=CHECK-OBJ,CHECK-ASM-AND-OBJ %s
5+
# RUN: | FileCheck --check-prefix=CHECK-ASM-AND-OBJ %s
66

77
# CHECK-ASM-AND-OBJ: rev8 t0, t1
88
# CHECK-ASM: encoding: [0x93,0x52,0x83,0x6b]
@@ -29,8 +29,7 @@ roriw t0, t1, 0
2929
packw t0, t1, t2
3030

3131
# Test the encoding used for zext.h on RV64
32-
# CHECK-ASM: packw t0, t1, zero
33-
# CHECK-OBJ: zext.h t0, t1
32+
# CHECK-ASM-AND-OBJ: zext.h t0, t1
3433
# CHECK-ASM: encoding: [0xbb,0x42,0x03,0x08]
3534
packw t0, t1, zero
3635

0 commit comments

Comments
 (0)