Skip to content

Commit e3fc88c

Browse files
committed
[RISCV] Select NDS_BFOZ for and with trailing ones mask
Select NDS_BFOS for and operation with trailing ones mask. The msb operand is calculated as the number of trailing ones minus one.
1 parent 5fb9e9b commit e3fc88c

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,11 @@ def XLenSubTrailingOnes : SDNodeXForm<imm, [{
564564
def GIXLenSubTrailingOnes : GICustomOperandRenderer<"renderXLenSubTrailingOnes">,
565565
GISDNodeXFormEquiv<XLenSubTrailingOnes>;
566566

567+
def TrailingOnesSubOne : SDNodeXForm<imm, [{
568+
return CurDAG->getTargetConstant(llvm::countr_one(N->getZExtValue()) - 1,
569+
SDLoc(N), N->getValueType(0));
570+
}]>;
571+
567572
// Checks if this mask is a non-empty sequence of ones starting at the
568573
// most/least significant bit with the remainder zero and exceeds simm32/simm12.
569574
def LeadingOnesMask : ImmLeaf<XLenVT, [{

llvm/lib/Target/RISCV/RISCVInstrInfoXAndes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ def NDS_VD4DOTSU_VV : NDSRVInstVD4DOT<0b000101, "nds.vd4dotsu">;
466466

467467
let Predicates = [HasVendorXAndesPerf] in {
468468

469+
def : Pat<(XLenVT (and GPR:$rs, TrailingOnesMask:$mask)),
470+
(NDS_BFOZ $rs, (TrailingOnesSubOne imm:$mask), 0)>;
471+
469472
def : Pat<(sext_inreg (XLenVT GPR:$rs1), i16), (NDS_BFOS GPR:$rs1, 15, 0)>;
470473
def : Pat<(sext_inreg (XLenVT GPR:$rs1), i8), (NDS_BFOS GPR:$rs1, 7, 0)>;
471474
def : Pat<(sext_inreg (XLenVT GPR:$rs1), i1), (NDS_BFOS GPR:$rs1, 0, 0)>;

llvm/test/CodeGen/RISCV/rv32xandesperf.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
define i32 @and32_0xfff(i32 %x) {
66
; CHECK-LABEL: and32_0xfff:
77
; CHECK: # %bb.0:
8-
; CHECK-NEXT: slli a0, a0, 20
9-
; CHECK-NEXT: srli a0, a0, 20
8+
; CHECK-NEXT: nds.bfoz a0, a0, 11, 0
109
; CHECK-NEXT: ret
1110
%a = and i32 %x, 4095
1211
ret i32 %a
@@ -16,8 +15,7 @@ define i64 @and64_0xfff(i64 %x) {
1615
; CHECK-LABEL: and64_0xfff:
1716
; CHECK: # %bb.0:
1817
; CHECK-NEXT: # kill: def $x11 killed $x10
19-
; CHECK-NEXT: slli a0, a0, 20
20-
; CHECK-NEXT: srli a0, a0, 20
18+
; CHECK-NEXT: nds.bfoz a0, a0, 11, 0
2119
; CHECK-NEXT: li a1, 0
2220
; CHECK-NEXT: ret
2321
%a = and i64 %x, 4095

llvm/test/CodeGen/RISCV/rv64xandesperf.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
define i32 @and32_0xfff(i32 %x) {
66
; CHECK-LABEL: and32_0xfff:
77
; CHECK: # %bb.0:
8-
; CHECK-NEXT: slli a0, a0, 52
9-
; CHECK-NEXT: srli a0, a0, 52
8+
; CHECK-NEXT: nds.bfoz a0, a0, 11, 0
109
; CHECK-NEXT: ret
1110
%a = and i32 %x, 4095
1211
ret i32 %a
@@ -15,8 +14,7 @@ define i32 @and32_0xfff(i32 %x) {
1514
define i64 @and64_0xfff(i64 %x) {
1615
; CHECK-LABEL: and64_0xfff:
1716
; CHECK: # %bb.0:
18-
; CHECK-NEXT: slli a0, a0, 52
19-
; CHECK-NEXT: srli a0, a0, 52
17+
; CHECK-NEXT: nds.bfoz a0, a0, 11, 0
2018
; CHECK-NEXT: ret
2119
%a = and i64 %x, 4095
2220
ret i64 %a

0 commit comments

Comments
 (0)