Skip to content

Commit d4be333

Browse files
committed
[RISCV] Add matching of codegen patterns to RISCV Bit Manipulation Zbs asm instructions
This patch provides optimization of bit manipulation operations by enabling the +experimental-b target feature. It adds matching of single block patterns of instructions to specific bit-manip instructions from the single-bit subset (zbs subextension) of the experimental B extension of RISC-V. It adds also the correspondent codegen tests. This patch is based on Claire Wolf's proposal for the bit manipulation extension of RISCV: https://github.com/riscv/riscv-bitmanip/blob/master/bitmanip-0.92.pdf Differential Revision: https://reviews.llvm.org/D79874
1 parent 6144f0a commit d4be333

File tree

3 files changed

+649
-0
lines changed

3 files changed

+649
-0
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfoB.td

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,38 @@ def : Pat<(rotr GPR:$rs1, GPR:$rs2), (ROR GPR:$rs1, GPR:$rs2)>;
664664
def : Pat<(fshr GPR:$rs1, GPR:$rs1, GPR:$rs2), (ROR GPR:$rs1, GPR:$rs2)>;
665665
} // Predicates = [HasStdExtZbbOrZbp]
666666

667+
let Predicates = [HasStdExtZbs, IsRV32] in
668+
def : Pat<(and (xor (shl 1, (and GPR:$rs2, 31)), -1), GPR:$rs1),
669+
(SBCLR GPR:$rs1, GPR:$rs2)>;
670+
let Predicates = [HasStdExtZbs, IsRV64] in
671+
def : Pat<(and (xor (shl 1, (and GPR:$rs2, 63)), -1), GPR:$rs1),
672+
(SBCLR GPR:$rs1, GPR:$rs2)>;
673+
674+
let Predicates = [HasStdExtZbs] in
675+
def : Pat<(and (rotl -2, GPR:$rs2), GPR:$rs1), (SBCLR GPR:$rs1, GPR:$rs2)>;
676+
677+
let Predicates = [HasStdExtZbs, IsRV32] in
678+
def : Pat<(or (shl 1, (and GPR:$rs2, 31)), GPR:$rs1),
679+
(SBSET GPR:$rs1, GPR:$rs2)>;
680+
let Predicates = [HasStdExtZbs, IsRV64] in
681+
def : Pat<(or (shl 1, (and GPR:$rs2, 63)), GPR:$rs1),
682+
(SBSET GPR:$rs1, GPR:$rs2)>;
683+
684+
let Predicates = [HasStdExtZbs, IsRV32] in
685+
def : Pat<(xor (shl 1, (and GPR:$rs2, 31)), GPR:$rs1),
686+
(SBINV GPR:$rs1, GPR:$rs2)>;
687+
let Predicates = [HasStdExtZbs, IsRV64] in
688+
def : Pat<(xor (shl 1, (and GPR:$rs2, 63)), GPR:$rs1),
689+
(SBINV GPR:$rs1, GPR:$rs2)>;
690+
691+
let Predicates = [HasStdExtZbs, IsRV32] in
692+
def : Pat<(and (srl GPR:$rs1, (and GPR:$rs2, 31)), 1),
693+
(SBEXT GPR:$rs1, GPR:$rs2)>;
694+
695+
let Predicates = [HasStdExtZbs, IsRV64] in
696+
def : Pat<(and (srl GPR:$rs1, (and GPR:$rs2, 63)), 1),
697+
(SBEXT GPR:$rs1, GPR:$rs2)>;
698+
667699
let Predicates = [HasStdExtZbb] in {
668700
def : Pat<(SLOIPat GPR:$rs1, uimmlog2xlen:$shamt),
669701
(SLOI GPR:$rs1, uimmlog2xlen:$shamt)>;
@@ -678,6 +710,12 @@ let Predicates = [HasStdExtZbbOrZbp] in
678710
def : Pat<(RORIPat GPR:$rs1, uimmlog2xlen:$shamt),
679711
(RORI GPR:$rs1, uimmlog2xlen:$shamt)>;
680712

713+
// We don't pattern-match sbclri[w], sbseti[w], sbinvi[w] because they are
714+
// pattern-matched by simple andi, ori, and xori.
715+
let Predicates = [HasStdExtZbs] in
716+
def : Pat<(and (srl GPR:$rs1, uimmlog2xlen:$shamt), (XLenVT 1)),
717+
(SBEXTI GPR:$rs1, uimmlog2xlen:$shamt)>;
718+
681719
let Predicates = [HasStdExtZbp, IsRV32] in {
682720
def : Pat<(or (or (and (srl GPR:$rs1, (i32 1)), (i32 0x55555555)), GPR:$rs1),
683721
(and (shl GPR:$rs1, (i32 1)), (i32 0xAAAAAAAA))),
@@ -886,6 +924,21 @@ def : Pat<(or (riscv_sllw (assertsexti32 GPR:$rs1),
886924
(RORW GPR:$rs1, GPR:$rs2)>;
887925
} // Predicates = [HasStdExtZbbOrZbp, IsRV64]
888926

927+
let Predicates = [HasStdExtZbs, IsRV64] in {
928+
def : Pat<(and (xor (riscv_sllw 1, (assertsexti32 GPR:$rs2)), -1),
929+
(assertsexti32 GPR:$rs1)),
930+
(SBCLRW GPR:$rs1, GPR:$rs2)>;
931+
def : Pat<(or (riscv_sllw 1, (assertsexti32 GPR:$rs2)),
932+
(assertsexti32 GPR:$rs1)),
933+
(SBSETW GPR:$rs1, GPR:$rs2)>;
934+
def : Pat<(xor (riscv_sllw 1, (assertsexti32 GPR:$rs2)),
935+
(assertsexti32 GPR:$rs1)),
936+
(SBINVW GPR:$rs1, GPR:$rs2)>;
937+
def : Pat<(and (riscv_srlw (assertsexti32 GPR:$rs1), (assertsexti32 GPR:$rs2)),
938+
1),
939+
(SBEXTW GPR:$rs1, GPR:$rs2)>;
940+
} // Predicates = [HasStdExtZbs, IsRV64]
941+
889942
let Predicates = [HasStdExtZbb, IsRV64] in {
890943
def : Pat<(SLOIWPat GPR:$rs1, uimmlog2xlen:$shamt),
891944
(SLOIW GPR:$rs1, uimmlog2xlen:$shamt)>;

0 commit comments

Comments
 (0)