Skip to content

Commit 7b3d307

Browse files
committed
[RISCV] Add isel patterns for grevi, shfli, and unshfli to brev8/zip/unzip instructions.
Zbkb supports some encodings of the general grevi, shfli, and unshfli instructions legal, so we added separate instructions for those encodings to improve the diagnostics for assembler and disassembler. To be consistent we should always use these separate instructions whenever those specific encodings of grevi/shfli/unshfli occur. So this patch adds specific isel patterns to override the generic isel patterns for these cases. Similar was done for rev8 and zext.h for Zbb previously.
1 parent 7ee1c16 commit 7b3d307

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfoZb.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ def : InstAlias<"zip4 $rd, $rs", (SHFLI GPR:$rd, GPR:$rs, 0b1100)>;
687687
def : InstAlias<"unzip4 $rd, $rs", (UNSHFLI GPR:$rd, GPR:$rs, 0b1100)>;
688688
def : InstAlias<"zip2 $rd, $rs", (SHFLI GPR:$rd, GPR:$rs, 0b1110)>;
689689
def : InstAlias<"unzip2 $rd, $rs", (UNSHFLI GPR:$rd, GPR:$rs, 0b1110)>;
690+
// zip and unzip are considered instructions rather than an alias.
690691

691692
def : InstAlias<"orc16 $rd, $rs", (GORCI GPR:$rd, GPR:$rs, 0b10000)>;
692693
def : InstAlias<"orc8 $rd, $rs", (GORCI GPR:$rd, GPR:$rs, 0b11000)>;
@@ -865,6 +866,9 @@ def : PatGprImm<riscv_shfl, SHFLI, shfl_uimm>;
865866
def : PatGprImm<riscv_unshfl, UNSHFLI, shfl_uimm>;
866867
def : PatGprImm<riscv_grev, GREVI, uimmlog2xlen>;
867868
def : PatGprImm<riscv_gorc, GORCI, uimmlog2xlen>;
869+
870+
// We treat brev8 as a separate instruction, so match it directly.
871+
def : Pat<(riscv_grev GPR:$rs1, 7), (BREV8 GPR:$rs1)>;
868872
} // Predicates = [HasStdExtZbp]
869873

870874
let Predicates = [HasStdExtZbp, IsRV64] in
@@ -876,6 +880,10 @@ def : Pat<(i32 (rotl (riscv_grev GPR:$rs1, 24), (i32 16))), (GREVI GPR:$rs1, 8)>
876880

877881
// We treat rev8 as a separate instruction, so match it directly.
878882
def : Pat<(i32 (riscv_grev GPR:$rs1, 24)), (REV8_RV32 GPR:$rs1)>;
883+
884+
// We treat zip and unzip as separate instructions, so match it directly.
885+
def : Pat<(i32 (riscv_shfl GPR:$rs1, 15)), (ZIP_RV32 GPR:$rs1)>;
886+
def : Pat<(i32 (riscv_unshfl GPR:$rs1, 15)), (UNZIP_RV32 GPR:$rs1)>;
879887
} // Predicates = [HasStdExtZbp, IsRV32]
880888

881889
let Predicates = [HasStdExtZbp, IsRV64] in {

llvm/test/CodeGen/RISCV/rv32zbp-intrinsic.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ define i32 @shfli32(i32 %a) nounwind {
9292
ret i32 %tmp
9393
}
9494

95+
define i32 @zipi32(i32 %a) nounwind {
96+
; RV32ZBP-LABEL: zipi32:
97+
; RV32ZBP: # %bb.0:
98+
; RV32ZBP-NEXT: zip a0, a0
99+
; RV32ZBP-NEXT: ret
100+
%tmp = call i32 @llvm.riscv.shfl.i32(i32 %a, i32 15)
101+
ret i32 %tmp
102+
}
103+
95104
declare i32 @llvm.riscv.unshfl.i32(i32 %a, i32 %b)
96105

97106
define i32 @unshfl32(i32 %a, i32 %b) nounwind {
@@ -122,6 +131,15 @@ define i32 @unshfli32(i32 %a) nounwind {
122131
ret i32 %tmp
123132
}
124133

134+
define i32 @unzipi32(i32 %a) nounwind {
135+
; RV32ZBP-LABEL: unzipi32:
136+
; RV32ZBP: # %bb.0:
137+
; RV32ZBP-NEXT: unzip a0, a0
138+
; RV32ZBP-NEXT: ret
139+
%tmp = call i32 @llvm.riscv.unshfl.i32(i32 %a, i32 15)
140+
ret i32 %tmp
141+
}
142+
125143
declare i32 @llvm.riscv.xperm.n.i32(i32 %a, i32 %b)
126144

127145
define i32 @xpermn32(i32 %a, i32 %b) nounwind {

llvm/test/CodeGen/RISCV/rv32zbp.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ define i32 @grev7_i32(i32 %a) nounwind {
14911491
;
14921492
; RV32ZBP-LABEL: grev7_i32:
14931493
; RV32ZBP: # %bb.0:
1494-
; RV32ZBP-NEXT: grevi a0, a0, 7
1494+
; RV32ZBP-NEXT: rev.b a0, a0
14951495
; RV32ZBP-NEXT: ret
14961496
%and1 = shl i32 %a, 1
14971497
%shl1 = and i32 %and1, -1431655766
@@ -1560,8 +1560,8 @@ define i64 @grev7_i64(i64 %a) nounwind {
15601560
;
15611561
; RV32ZBP-LABEL: grev7_i64:
15621562
; RV32ZBP: # %bb.0:
1563-
; RV32ZBP-NEXT: grevi a0, a0, 7
1564-
; RV32ZBP-NEXT: grevi a1, a1, 7
1563+
; RV32ZBP-NEXT: rev.b a0, a0
1564+
; RV32ZBP-NEXT: rev.b a1, a1
15651565
; RV32ZBP-NEXT: ret
15661566
%and1 = shl i64 %a, 1
15671567
%shl1 = and i64 %and1, -6148914691236517206
@@ -2175,7 +2175,7 @@ define zeroext i8 @bitreverse_i8(i8 zeroext %a) nounwind {
21752175
;
21762176
; RV32ZBP-LABEL: bitreverse_i8:
21772177
; RV32ZBP: # %bb.0:
2178-
; RV32ZBP-NEXT: grevi a0, a0, 7
2178+
; RV32ZBP-NEXT: rev.b a0, a0
21792179
; RV32ZBP-NEXT: ret
21802180
%1 = tail call i8 @llvm.bitreverse.i8(i8 %a)
21812181
ret i8 %1
@@ -2450,7 +2450,7 @@ define i32 @bitreverse_bswap_i32(i32 %a) {
24502450
;
24512451
; RV32ZBP-LABEL: bitreverse_bswap_i32:
24522452
; RV32ZBP: # %bb.0:
2453-
; RV32ZBP-NEXT: grevi a0, a0, 7
2453+
; RV32ZBP-NEXT: rev.b a0, a0
24542454
; RV32ZBP-NEXT: ret
24552455
%1 = call i32 @llvm.bitreverse.i32(i32 %a)
24562456
%2 = call i32 @llvm.bswap.i32(i32 %1)
@@ -2539,8 +2539,8 @@ define i64 @bitreverse_bswap_i64(i64 %a) {
25392539
;
25402540
; RV32ZBP-LABEL: bitreverse_bswap_i64:
25412541
; RV32ZBP: # %bb.0:
2542-
; RV32ZBP-NEXT: grevi a0, a0, 7
2543-
; RV32ZBP-NEXT: grevi a1, a1, 7
2542+
; RV32ZBP-NEXT: rev.b a0, a0
2543+
; RV32ZBP-NEXT: rev.b a1, a1
25442544
; RV32ZBP-NEXT: ret
25452545
%1 = call i64 @llvm.bitreverse.i64(i64 %a)
25462546
%2 = call i64 @llvm.bswap.i64(i64 %1)

llvm/test/CodeGen/RISCV/rv64zbp.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ define i64 @grev7_i64(i64 %a) nounwind {
14381438
;
14391439
; RV64ZBP-LABEL: grev7_i64:
14401440
; RV64ZBP: # %bb.0:
1441-
; RV64ZBP-NEXT: grevi a0, a0, 7
1441+
; RV64ZBP-NEXT: rev.b a0, a0
14421442
; RV64ZBP-NEXT: ret
14431443
%and1 = shl i64 %a, 1
14441444
%shl1 = and i64 %and1, -6148914691236517206
@@ -2481,7 +2481,7 @@ define i64 @bitreverse_bswap_i64(i64 %a) {
24812481
;
24822482
; RV64ZBP-LABEL: bitreverse_bswap_i64:
24832483
; RV64ZBP: # %bb.0:
2484-
; RV64ZBP-NEXT: grevi a0, a0, 7
2484+
; RV64ZBP-NEXT: rev.b a0, a0
24852485
; RV64ZBP-NEXT: ret
24862486
%1 = call i64 @llvm.bitreverse.i64(i64 %a)
24872487
%2 = call i64 @llvm.bswap.i64(i64 %1)

0 commit comments

Comments
 (0)