Skip to content

Commit

Permalink
[RISCV] Add isel patterns to match sbset/sbclr/sbinv/sbext even if th…
Browse files Browse the repository at this point in the history
…e shift amount isn't masked.

This uses the shiftop PatFrags to handle the masked shift amount
and unmasked shift amount cases. That also checks XLen as part
of the masked amount check so we don't need separate RV32 and RV64
patterns.

Differential Revision: https://reviews.llvm.org/D91016
  • Loading branch information
topperc committed Nov 9, 2020
1 parent 2eccde4 commit c0dd22e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 72 deletions.
32 changes: 6 additions & 26 deletions llvm/lib/Target/RISCV/RISCVInstrInfoB.td
Expand Up @@ -675,37 +675,17 @@ def : Pat<(rotl GPR:$rs1, GPR:$rs2), (ROL GPR:$rs1, GPR:$rs2)>;
def : Pat<(rotr GPR:$rs1, GPR:$rs2), (ROR GPR:$rs1, GPR:$rs2)>;
} // Predicates = [HasStdExtZbbOrZbp]

let Predicates = [HasStdExtZbs, IsRV32] in
def : Pat<(and (not (shl 1, (and GPR:$rs2, 31))), GPR:$rs1),
(SBCLR GPR:$rs1, GPR:$rs2)>;
let Predicates = [HasStdExtZbs, IsRV64] in
def : Pat<(and (not (shl 1, (and GPR:$rs2, 63))), GPR:$rs1),
let Predicates = [HasStdExtZbs] in {
def : Pat<(and (not (shiftop<shl> 1, GPR:$rs2)), GPR:$rs1),
(SBCLR GPR:$rs1, GPR:$rs2)>;

let Predicates = [HasStdExtZbs] in
def : Pat<(and (rotl -2, GPR:$rs2), GPR:$rs1), (SBCLR GPR:$rs1, GPR:$rs2)>;

let Predicates = [HasStdExtZbs, IsRV32] in
def : Pat<(or (shl 1, (and GPR:$rs2, 31)), GPR:$rs1),
(SBSET GPR:$rs1, GPR:$rs2)>;
let Predicates = [HasStdExtZbs, IsRV64] in
def : Pat<(or (shl 1, (and GPR:$rs2, 63)), GPR:$rs1),
def : Pat<(or (shiftop<shl> 1, GPR:$rs2), GPR:$rs1),
(SBSET GPR:$rs1, GPR:$rs2)>;

let Predicates = [HasStdExtZbs, IsRV32] in
def : Pat<(xor (shl 1, (and GPR:$rs2, 31)), GPR:$rs1),
(SBINV GPR:$rs1, GPR:$rs2)>;
let Predicates = [HasStdExtZbs, IsRV64] in
def : Pat<(xor (shl 1, (and GPR:$rs2, 63)), GPR:$rs1),
def : Pat<(xor (shiftop<shl> 1, GPR:$rs2), GPR:$rs1),
(SBINV GPR:$rs1, GPR:$rs2)>;

let Predicates = [HasStdExtZbs, IsRV32] in
def : Pat<(and (srl GPR:$rs1, (and GPR:$rs2, 31)), 1),
(SBEXT GPR:$rs1, GPR:$rs2)>;

let Predicates = [HasStdExtZbs, IsRV64] in
def : Pat<(and (srl GPR:$rs1, (and GPR:$rs2, 63)), 1),
def : Pat<(and (shiftop<srl> GPR:$rs1, GPR:$rs2), 1),
(SBEXT GPR:$rs1, GPR:$rs2)>;
}

let Predicates = [HasStdExtZbb] in {
def : Pat<(SLOIPat GPR:$rs1, uimmlog2xlen:$shamt),
Expand Down
43 changes: 17 additions & 26 deletions llvm/test/CodeGen/RISCV/rv32Zbs.ll
Expand Up @@ -47,10 +47,7 @@ define i32 @sbclr_i32_no_mask(i32 %a, i32 %b) nounwind {
;
; RV32IBS-LABEL: sbclr_i32_no_mask:
; RV32IBS: # %bb.0:
; RV32IBS-NEXT: addi a2, zero, 1
; RV32IBS-NEXT: sll a1, a2, a1
; RV32IBS-NEXT: not a1, a1
; RV32IBS-NEXT: and a0, a1, a0
; RV32IBS-NEXT: sbclr a0, a0, a1
; RV32IBS-NEXT: ret
%shl = shl nuw i32 1, %b
%neg = xor i32 %shl, -1
Expand Down Expand Up @@ -156,16 +153,12 @@ define i32 @sbset_i32_no_mask(i32 %a, i32 %b) nounwind {
;
; RV32IB-LABEL: sbset_i32_no_mask:
; RV32IB: # %bb.0:
; RV32IB-NEXT: addi a2, zero, 1
; RV32IB-NEXT: sll a1, a2, a1
; RV32IB-NEXT: or a0, a1, a0
; RV32IB-NEXT: sbset a0, a0, a1
; RV32IB-NEXT: ret
;
; RV32IBS-LABEL: sbset_i32_no_mask:
; RV32IBS: # %bb.0:
; RV32IBS-NEXT: addi a2, zero, 1
; RV32IBS-NEXT: sll a1, a2, a1
; RV32IBS-NEXT: or a0, a1, a0
; RV32IBS-NEXT: sbset a0, a0, a1
; RV32IBS-NEXT: ret
%shl = shl nuw i32 1, %b
%or = or i32 %shl, %a
Expand All @@ -190,18 +183,18 @@ define i64 @sbset_i64(i64 %a, i64 %b) nounwind {
; RV32IB-LABEL: sbset_i64:
; RV32IB: # %bb.0:
; RV32IB-NEXT: addi a3, zero, 1
; RV32IB-NEXT: sll a2, a3, a2
; RV32IB-NEXT: srai a3, a2, 31
; RV32IB-NEXT: or a0, a2, a0
; RV32IB-NEXT: sll a3, a3, a2
; RV32IB-NEXT: srai a3, a3, 31
; RV32IB-NEXT: sbset a0, a0, a2
; RV32IB-NEXT: or a1, a3, a1
; RV32IB-NEXT: ret
;
; RV32IBS-LABEL: sbset_i64:
; RV32IBS: # %bb.0:
; RV32IBS-NEXT: addi a3, zero, 1
; RV32IBS-NEXT: sll a2, a3, a2
; RV32IBS-NEXT: srai a3, a2, 31
; RV32IBS-NEXT: or a0, a2, a0
; RV32IBS-NEXT: sll a3, a3, a2
; RV32IBS-NEXT: srai a3, a3, 31
; RV32IBS-NEXT: sbset a0, a0, a2
; RV32IBS-NEXT: or a1, a3, a1
; RV32IBS-NEXT: ret
%1 = trunc i64 %b to i32
Expand Down Expand Up @@ -253,18 +246,18 @@ define i64 @sbinv_i64(i64 %a, i64 %b) nounwind {
; RV32IB-LABEL: sbinv_i64:
; RV32IB: # %bb.0:
; RV32IB-NEXT: addi a3, zero, 1
; RV32IB-NEXT: sll a2, a3, a2
; RV32IB-NEXT: srai a3, a2, 31
; RV32IB-NEXT: xor a0, a2, a0
; RV32IB-NEXT: sll a3, a3, a2
; RV32IB-NEXT: srai a3, a3, 31
; RV32IB-NEXT: sbinv a0, a0, a2
; RV32IB-NEXT: xor a1, a3, a1
; RV32IB-NEXT: ret
;
; RV32IBS-LABEL: sbinv_i64:
; RV32IBS: # %bb.0:
; RV32IBS-NEXT: addi a3, zero, 1
; RV32IBS-NEXT: sll a2, a3, a2
; RV32IBS-NEXT: srai a3, a2, 31
; RV32IBS-NEXT: xor a0, a2, a0
; RV32IBS-NEXT: sll a3, a3, a2
; RV32IBS-NEXT: srai a3, a3, 31
; RV32IBS-NEXT: sbinv a0, a0, a2
; RV32IBS-NEXT: xor a1, a3, a1
; RV32IBS-NEXT: ret
%1 = trunc i64 %b to i32
Expand Down Expand Up @@ -306,14 +299,12 @@ define i32 @sbext_i32_no_mask(i32 %a, i32 %b) nounwind {
;
; RV32IB-LABEL: sbext_i32_no_mask:
; RV32IB: # %bb.0:
; RV32IB-NEXT: srl a0, a0, a1
; RV32IB-NEXT: andi a0, a0, 1
; RV32IB-NEXT: sbext a0, a0, a1
; RV32IB-NEXT: ret
;
; RV32IBS-LABEL: sbext_i32_no_mask:
; RV32IBS: # %bb.0:
; RV32IBS-NEXT: srl a0, a0, a1
; RV32IBS-NEXT: andi a0, a0, 1
; RV32IBS-NEXT: sbext a0, a0, a1
; RV32IBS-NEXT: ret
%shr = lshr i32 %a, %b
%and1 = and i32 %shr, 1
Expand Down
27 changes: 7 additions & 20 deletions llvm/test/CodeGen/RISCV/rv64Zbs.ll
Expand Up @@ -96,10 +96,7 @@ define i64 @sbclr_i64_no_mask(i64 %a, i64 %b) nounwind {
;
; RV64IBS-LABEL: sbclr_i64_no_mask:
; RV64IBS: # %bb.0:
; RV64IBS-NEXT: addi a2, zero, 1
; RV64IBS-NEXT: sll a1, a2, a1
; RV64IBS-NEXT: not a1, a1
; RV64IBS-NEXT: and a0, a1, a0
; RV64IBS-NEXT: sbclr a0, a0, a1
; RV64IBS-NEXT: ret
%shl = shl i64 1, %b
%neg = xor i64 %shl, -1
Expand Down Expand Up @@ -185,16 +182,12 @@ define i64 @sbset_i64_no_mask(i64 %a, i64 %b) nounwind {
;
; RV64IB-LABEL: sbset_i64_no_mask:
; RV64IB: # %bb.0:
; RV64IB-NEXT: addi a2, zero, 1
; RV64IB-NEXT: sll a1, a2, a1
; RV64IB-NEXT: or a0, a1, a0
; RV64IB-NEXT: sbset a0, a0, a1
; RV64IB-NEXT: ret
;
; RV64IBS-LABEL: sbset_i64_no_mask:
; RV64IBS: # %bb.0:
; RV64IBS-NEXT: addi a2, zero, 1
; RV64IBS-NEXT: sll a1, a2, a1
; RV64IBS-NEXT: or a0, a1, a0
; RV64IBS-NEXT: sbset a0, a0, a1
; RV64IBS-NEXT: ret
%shl = shl i64 1, %b
%or = or i64 %shl, %a
Expand Down Expand Up @@ -279,16 +272,12 @@ define i64 @sbinv_i64_no_mask(i64 %a, i64 %b) nounwind {
;
; RV64IB-LABEL: sbinv_i64_no_mask:
; RV64IB: # %bb.0:
; RV64IB-NEXT: addi a2, zero, 1
; RV64IB-NEXT: sll a1, a2, a1
; RV64IB-NEXT: xor a0, a1, a0
; RV64IB-NEXT: sbinv a0, a0, a1
; RV64IB-NEXT: ret
;
; RV64IBS-LABEL: sbinv_i64_no_mask:
; RV64IBS: # %bb.0:
; RV64IBS-NEXT: addi a2, zero, 1
; RV64IBS-NEXT: sll a1, a2, a1
; RV64IBS-NEXT: xor a0, a1, a0
; RV64IBS-NEXT: sbinv a0, a0, a1
; RV64IBS-NEXT: ret
%shl = shl nuw i64 1, %b
%xor = xor i64 %shl, %a
Expand Down Expand Up @@ -369,14 +358,12 @@ define i64 @sbext_i64_no_mask(i64 %a, i64 %b) nounwind {
;
; RV64IB-LABEL: sbext_i64_no_mask:
; RV64IB: # %bb.0:
; RV64IB-NEXT: srl a0, a0, a1
; RV64IB-NEXT: andi a0, a0, 1
; RV64IB-NEXT: sbext a0, a0, a1
; RV64IB-NEXT: ret
;
; RV64IBS-LABEL: sbext_i64_no_mask:
; RV64IBS: # %bb.0:
; RV64IBS-NEXT: srl a0, a0, a1
; RV64IBS-NEXT: andi a0, a0, 1
; RV64IBS-NEXT: sbext a0, a0, a1
; RV64IBS-NEXT: ret
%shr = lshr i64 %a, %b
%and1 = and i64 %shr, 1
Expand Down

0 comments on commit c0dd22e

Please sign in to comment.