Index: llvm/lib/Target/RISCV/RISCVInstrInfoB.td =================================================================== --- llvm/lib/Target/RISCV/RISCVInstrInfoB.td +++ llvm/lib/Target/RISCV/RISCVInstrInfoB.td @@ -653,10 +653,6 @@ (SLO GPR:$rs1, GPR:$rs2)>; def : Pat<(xor (srl (xor GPR:$rs1, -1), GPR:$rs2), -1), (SRO GPR:$rs1, GPR:$rs2)>; -def : Pat<(SLOIPat GPR:$rs1, uimmlog2xlen:$shamt), - (SLOI GPR:$rs1, uimmlog2xlen:$shamt)>; -def : Pat<(SROIPat GPR:$rs1, uimmlog2xlen:$shamt), - (SROI GPR:$rs1, uimmlog2xlen:$shamt)>; } // Predicates = [HasStdExtZbb] let Predicates = [HasStdExtZbbOrZbp] in { @@ -666,6 +662,51 @@ def : Pat<(fshr GPR:$rs1, GPR:$rs1, GPR:$rs2), (ROR GPR:$rs1, GPR:$rs2)>; } // Predicates = [HasStdExtZbbOrZbp] +let Predicates = [HasStdExtZbs, IsRV32] in +def : Pat<(and (xor (shl 1, (and GPR:$rs2, 31)), -1), GPR:$rs1), + (SBCLR GPR:$rs1, GPR:$rs2)>; +let Predicates = [HasStdExtZbs, IsRV64] in +def : Pat<(and (xor (shl 1, (and GPR:$rs2, 63)), -1), 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), + (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), + (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), + (SBEXT GPR:$rs1, GPR:$rs2)>; + +let Predicates = [HasStdExtZbb] in { +def : Pat<(SLOIPat GPR:$rs1, uimmlog2xlen:$shamt), + (SLOI GPR:$rs1, uimmlog2xlen:$shamt)>; +def : Pat<(SROIPat GPR:$rs1, uimmlog2xlen:$shamt), + (SROI GPR:$rs1, uimmlog2xlen:$shamt)>; +} // Predicates = [HasStdExtZbb] + +// We don't pattern-match sbclri, sbseti, sbinvi because they get easily +// selected already as andi, ori, and xori. +let Predicates = [HasStdExtZbs] in +def : Pat<(and (srl GPR:$rs1, uimmlog2xlen:$shamt), (XLenVT 1)), + (SBEXTI GPR:$rs1, uimmlog2xlen:$shamt)>; + let Predicates = [HasStdExtZbp, IsRV32] in { def : Pat<(or (or (and (srl GPR:$rs1, (i32 1)), (i32 0x55555555)), GPR:$rs1), (and (shl GPR:$rs1, (i32 1)), (i32 0xAAAAAAAA))), @@ -878,6 +919,21 @@ (RORW GPR:$rs1, GPR:$rs2)>; } // Predicates = [HasStdExtZbbOrZbp, IsRV64] +let Predicates = [HasStdExtZbs, IsRV64] in { +def : Pat<(and (xor (riscv_sllw 1, (assertsexti32 GPR:$rs2)), -1), + (assertsexti32 GPR:$rs1)), + (SBCLRW GPR:$rs1, GPR:$rs2)>; +def : Pat<(or (riscv_sllw 1, (assertsexti32 GPR:$rs2)), + (assertsexti32 GPR:$rs1)), + (SBSETW GPR:$rs1, GPR:$rs2)>; +def : Pat<(xor (riscv_sllw 1, (assertsexti32 GPR:$rs2)), + (assertsexti32 GPR:$rs1)), + (SBINVW GPR:$rs1, GPR:$rs2)>; +def : Pat<(and (riscv_srlw (assertsexti32 GPR:$rs1), (assertsexti32 GPR:$rs2)), + 1), + (SBEXTW GPR:$rs1, GPR:$rs2)>; +} // Predicates = [HasStdExtZbs, IsRV64] + let Predicates = [HasStdExtZbp, IsRV64] in { def : Pat<(sext_inreg (or (or (and (srl GPR:$rs1, (i64 1)), (i64 0x55555555)), GPR:$rs1), Index: llvm/test/CodeGen/RISCV/rv32Zbs.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/RISCV/rv32Zbs.ll @@ -0,0 +1,217 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \ +; RUN: | FileCheck %s -check-prefix=RV32I +; RUN: llc -mtriple=riscv32 -mattr=+experimental-b -verify-machineinstrs < %s \ +; RUN: | FileCheck %s -check-prefix=RV32IB +; RUN: llc -mtriple=riscv32 -mattr=+experimental-zbs -verify-machineinstrs < %s \ +; RUN: | FileCheck %s -check-prefix=RV32IB + +define i32 @sbclr_i32(i32 %a, i32 %b) nounwind { +; RV32I-LABEL: sbclr_i32: +; RV32I: # %bb.0: +; RV32I-NEXT: addi a2, zero, 1 +; RV32I-NEXT: sll a1, a2, a1 +; RV32I-NEXT: not a1, a1 +; RV32I-NEXT: and a0, a1, a0 +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbclr_i32: +; RV32IB: # %bb.0: +; RV32IB-NEXT: sbclr a0, a0, a1 +; RV32IB-NEXT: ret + %and = and i32 %b, 31 + %shl = shl nuw i32 1, %and + %neg = xor i32 %shl, -1 + %and1 = and i32 %neg, %a + ret i32 %and1 +} + +; This pattern-matching of sbclr_i64 would generate an asm conflict if tested +; on RV32. Until b the extension is ratified or new patterns can be matched to +; this corner case we'll skip it. + + +define i32 @sbset_i32(i32 %a, i32 %b) nounwind { +; RV32I-LABEL: sbset_i32: +; RV32I: # %bb.0: +; RV32I-NEXT: addi a2, zero, 1 +; RV32I-NEXT: sll a1, a2, a1 +; RV32I-NEXT: or a0, a1, a0 +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbset_i32: +; RV32IB: # %bb.0: +; RV32IB-NEXT: sbset a0, a0, a1 +; RV32IB-NEXT: ret + %and = and i32 %b, 31 + %shl = shl nuw i32 1, %and + %or = or i32 %shl, %a + ret i32 %or +} + +define i64 @sbset_i64(i64 %a, i64 %b) nounwind { +; RV32I-LABEL: sbset_i64: +; RV32I: # %bb.0: +; RV32I-NEXT: addi a3, zero, 1 +; RV32I-NEXT: sll a2, a3, a2 +; RV32I-NEXT: srai a3, a2, 31 +; RV32I-NEXT: or a0, a2, a0 +; RV32I-NEXT: or a1, a3, a1 +; RV32I-NEXT: ret +; +; 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: or a1, a3, a1 +; RV32IB-NEXT: ret + %1 = trunc i64 %b to i32 + %conv = and i32 %1, 63 + %shl = shl nuw i32 1, %conv + %conv1 = sext i32 %shl to i64 + %or = or i64 %conv1, %a + ret i64 %or +} + +define i32 @sbinv_i32(i32 %a, i32 %b) nounwind { +; RV32I-LABEL: sbinv_i32: +; RV32I: # %bb.0: +; RV32I-NEXT: addi a2, zero, 1 +; RV32I-NEXT: sll a1, a2, a1 +; RV32I-NEXT: xor a0, a1, a0 +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbinv_i32: +; RV32IB: # %bb.0: +; RV32IB-NEXT: sbinv a0, a0, a1 +; RV32IB-NEXT: ret + %and = and i32 %b, 31 + %shl = shl nuw i32 1, %and + %xor = xor i32 %shl, %a + ret i32 %xor +} + +define i64 @sbinv_i64(i64 %a, i64 %b) nounwind { +; RV32I-LABEL: sbinv_i64: +; RV32I: # %bb.0: +; RV32I-NEXT: addi a3, zero, 1 +; RV32I-NEXT: sll a2, a3, a2 +; RV32I-NEXT: srai a3, a2, 31 +; RV32I-NEXT: xor a0, a2, a0 +; RV32I-NEXT: xor a1, a3, a1 +; RV32I-NEXT: ret +; +; 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: xor a1, a3, a1 +; RV32IB-NEXT: ret + %1 = trunc i64 %b to i32 + %conv = and i32 %1, 63 + %shl = shl nuw i32 1, %conv + %conv1 = sext i32 %shl to i64 + %xor = xor i64 %conv1, %a + ret i64 %xor +} + +define i32 @sbext_i32(i32 %a, i32 %b) nounwind { +; RV32I-LABEL: sbext_i32: +; RV32I: # %bb.0: +; RV32I-NEXT: srl a0, a0, a1 +; RV32I-NEXT: andi a0, a0, 1 +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbext_i32: +; RV32IB: # %bb.0: +; RV32IB-NEXT: sbext a0, a0, a1 +; RV32IB-NEXT: ret + %and = and i32 %b, 31 + %shr = lshr i32 %a, %and + %and1 = and i32 %shr, 1 + ret i32 %and1 +} + +define i64 @sbext_i64(i64 %a, i64 %b) nounwind { +; RV32I-LABEL: sbext_i64: +; RV32I: # %bb.0: +; RV32I-NEXT: andi a3, a2, 63 +; RV32I-NEXT: addi a4, a3, -32 +; RV32I-NEXT: bltz a4, .LBB6_2 +; RV32I-NEXT: # %bb.1: +; RV32I-NEXT: srl a0, a1, a4 +; RV32I-NEXT: j .LBB6_3 +; RV32I-NEXT: .LBB6_2: +; RV32I-NEXT: srl a0, a0, a2 +; RV32I-NEXT: addi a2, zero, 31 +; RV32I-NEXT: sub a2, a2, a3 +; RV32I-NEXT: slli a1, a1, 1 +; RV32I-NEXT: sll a1, a1, a2 +; RV32I-NEXT: or a0, a0, a1 +; RV32I-NEXT: .LBB6_3: +; RV32I-NEXT: andi a0, a0, 1 +; RV32I-NEXT: mv a1, zero +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbext_i64: +; RV32IB: # %bb.0: +; RV32IB-NEXT: andi a3, a2, 63 +; RV32IB-NEXT: addi a4, a3, -32 +; RV32IB-NEXT: bltz a4, .LBB6_2 +; RV32IB-NEXT: # %bb.1: +; RV32IB-NEXT: srl a0, a1, a4 +; RV32IB-NEXT: j .LBB6_3 +; RV32IB-NEXT: .LBB6_2: +; RV32IB-NEXT: srl a0, a0, a2 +; RV32IB-NEXT: addi a2, zero, 31 +; RV32IB-NEXT: sub a2, a2, a3 +; RV32IB-NEXT: slli a1, a1, 1 +; RV32IB-NEXT: sll a1, a1, a2 +; RV32IB-NEXT: or a0, a0, a1 +; RV32IB-NEXT: .LBB6_3: +; RV32IB-NEXT: andi a0, a0, 1 +; RV32IB-NEXT: mv a1, zero +; RV32IB-NEXT: ret + %conv = and i64 %b, 63 + %shr = lshr i64 %a, %conv + %and1 = and i64 %shr, 1 + ret i64 %and1 +} + +define i32 @sbexti_i32(i32 %a) nounwind { +; RV32I-LABEL: sbexti_i32: +; RV32I: # %bb.0: +; RV32I-NEXT: srli a0, a0, 5 +; RV32I-NEXT: andi a0, a0, 1 +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbexti_i32: +; RV32IB: # %bb.0: +; RV32IB-NEXT: sbexti a0, a0, 5 +; RV32IB-NEXT: ret + %shr = lshr i32 %a, 5 + %and = and i32 %shr, 1 + ret i32 %and +} + +define i64 @sbexti_i64(i64 %a) nounwind { +; RV32I-LABEL: sbexti_i64: +; RV32I: # %bb.0: +; RV32I-NEXT: srli a0, a0, 5 +; RV32I-NEXT: andi a0, a0, 1 +; RV32I-NEXT: mv a1, zero +; RV32I-NEXT: ret +; +; RV32IB-LABEL: sbexti_i64: +; RV32IB: # %bb.0: +; RV32IB-NEXT: sbexti a0, a0, 5 +; RV32IB-NEXT: mv a1, zero +; RV32IB-NEXT: ret + %shr = lshr i64 %a, 5 + %and = and i64 %shr, 1 + ret i64 %and +} Index: llvm/test/CodeGen/RISCV/rv64Zbs.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/RISCV/rv64Zbs.ll @@ -0,0 +1,185 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \ +; RUN: | FileCheck %s -check-prefix=RV64I +; RUN: llc -mtriple=riscv64 -mattr=+experimental-b -verify-machineinstrs < %s \ +; RUN: | FileCheck %s -check-prefix=RV64IB +; RUN: llc -mtriple=riscv64 -mattr=+experimental-zbs -verify-machineinstrs < %s \ +; RUN: | FileCheck %s -check-prefix=RV64IB + +define signext i32 @sbclr_i32(i32 signext %a, i32 signext %b) nounwind { +; RV64I-LABEL: sbclr_i32: +; RV64I: # %bb.0: +; RV64I-NEXT: addi a2, zero, 1 +; RV64I-NEXT: sllw a1, a2, a1 +; RV64I-NEXT: not a1, a1 +; RV64I-NEXT: and a0, a1, a0 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbclr_i32: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbclrw a0, a0, a1 +; RV64IB-NEXT: ret + %and = and i32 %b, 31 + %shl = shl nuw i32 1, %and + %neg = xor i32 %shl, -1 + %and1 = and i32 %neg, %a + ret i32 %and1 +} + +define i64 @sbclr_i64(i64 %a, i64 %b) nounwind { +; RV64I-LABEL: sbclr_i64: +; RV64I: # %bb.0: +; RV64I-NEXT: addi a2, zero, 1 +; RV64I-NEXT: sll a1, a2, a1 +; RV64I-NEXT: not a1, a1 +; RV64I-NEXT: and a0, a1, a0 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbclr_i64: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbclr a0, a0, a1 +; RV64IB-NEXT: ret + %and = and i64 %b, 63 + %shl = shl nuw i64 1, %and + %neg = xor i64 %shl, -1 + %and1 = and i64 %neg, %a + ret i64 %and1 +} + +define signext i32 @sbset_i32(i32 signext %a, i32 signext %b) nounwind { +; RV64I-LABEL: sbset_i32: +; RV64I: # %bb.0: +; RV64I-NEXT: addi a2, zero, 1 +; RV64I-NEXT: sllw a1, a2, a1 +; RV64I-NEXT: or a0, a1, a0 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbset_i32: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbsetw a0, a0, a1 +; RV64IB-NEXT: ret + %and = and i32 %b, 31 + %shl = shl nuw i32 1, %and + %or = or i32 %shl, %a + ret i32 %or +} + +define i64 @sbset_i64(i64 %a, i64 %b) nounwind { +; RV64I-LABEL: sbset_i64: +; RV64I: # %bb.0: +; RV64I-NEXT: addi a2, zero, 1 +; RV64I-NEXT: sll a1, a2, a1 +; RV64I-NEXT: or a0, a1, a0 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbset_i64: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbset a0, a0, a1 +; RV64IB-NEXT: ret + %conv = and i64 %b, 63 + %shl = shl nuw i64 1, %conv + %or = or i64 %shl, %a + ret i64 %or +} + +define signext i32 @sbinv_i32(i32 signext %a, i32 signext %b) nounwind { +; RV64I-LABEL: sbinv_i32: +; RV64I: # %bb.0: +; RV64I-NEXT: addi a2, zero, 1 +; RV64I-NEXT: sllw a1, a2, a1 +; RV64I-NEXT: xor a0, a1, a0 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbinv_i32: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbinvw a0, a0, a1 +; RV64IB-NEXT: ret + %and = and i32 %b, 31 + %shl = shl nuw i32 1, %and + %xor = xor i32 %shl, %a + ret i32 %xor +} + +define i64 @sbinv_i64(i64 %a, i64 %b) nounwind { +; RV64I-LABEL: sbinv_i64: +; RV64I: # %bb.0: +; RV64I-NEXT: addi a2, zero, 1 +; RV64I-NEXT: sll a1, a2, a1 +; RV64I-NEXT: xor a0, a1, a0 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbinv_i64: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbinv a0, a0, a1 +; RV64IB-NEXT: ret + %conv = and i64 %b, 63 + %shl = shl nuw i64 1, %conv + %xor = xor i64 %shl, %a + ret i64 %xor +} + +define signext i32 @sbext_i32(i32 signext %a, i32 signext %b) nounwind { +; RV64I-LABEL: sbext_i32: +; RV64I: # %bb.0: +; RV64I-NEXT: srlw a0, a0, a1 +; RV64I-NEXT: andi a0, a0, 1 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbext_i32: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbextw a0, a0, a1 +; RV64IB-NEXT: ret + %and = and i32 %b, 31 + %shr = lshr i32 %a, %and + %and1 = and i32 %shr, 1 + ret i32 %and1 +} + +define i64 @sbext_i64(i64 %a, i64 %b) nounwind { +; RV64I-LABEL: sbext_i64: +; RV64I: # %bb.0: +; RV64I-NEXT: srl a0, a0, a1 +; RV64I-NEXT: andi a0, a0, 1 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbext_i64: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbext a0, a0, a1 +; RV64IB-NEXT: ret + %conv = and i64 %b, 63 + %shr = lshr i64 %a, %conv + %and1 = and i64 %shr, 1 + ret i64 %and1 +} + +define signext i32 @sbexti_i32(i32 signext %a) nounwind { +; RV64I-LABEL: sbexti_i32: +; RV64I: # %bb.0: +; RV64I-NEXT: srli a0, a0, 5 +; RV64I-NEXT: andi a0, a0, 1 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbexti_i32: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbexti a0, a0, 5 +; RV64IB-NEXT: ret + %shr = lshr i32 %a, 5 + %and = and i32 %shr, 1 + ret i32 %and +} + +define i64 @sbexti_i64(i64 %a) nounwind { +; RV64I-LABEL: sbexti_i64: +; RV64I: # %bb.0: +; RV64I-NEXT: srli a0, a0, 5 +; RV64I-NEXT: andi a0, a0, 1 +; RV64I-NEXT: ret +; +; RV64IB-LABEL: sbexti_i64: +; RV64IB: # %bb.0: +; RV64IB-NEXT: sbexti a0, a0, 5 +; RV64IB-NEXT: ret + %shr = lshr i64 %a, 5 + %and = and i64 %shr, 1 + ret i64 %and +}