https://gcc.gnu.org/g:b9d46c62015e8cb2e9b896a1d663cd85a0b4690b
commit r17-6480-gb9d46c62015e8cb2e9b896a1d663cd85a0b4690b Author: Jiawei <[email protected]> Date: Thu Nov 19 20:56:23 2026 +0800 RISC-V: Use inverted logical instructions for constants with Zbkb Zbkb provides andn, orn and xnor. Consider the complemented constant when synthesizing a logical operation and charge the same cost as for Zbb. A mask such as ~0xf000UL then needs only a lui and an andn. gcc/ChangeLog: * config/riscv/bitmanip.md: Enable the constant IOR/XOR splitter for Zbkb. * config/riscv/riscv.cc (riscv_rtx_costs, synthesize_ior_xor, synthesize_and): Handle inverted logical operations with Zbkb. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbkb-logic-imm.c: New test. Diff: --- gcc/config/riscv/bitmanip.md | 2 +- gcc/config/riscv/riscv.cc | 14 +++++++------- gcc/testsuite/gcc.target/riscv/zbkb-logic-imm.c | 11 +++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 2871515d5436..33bf84e5d846 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -1535,7 +1535,7 @@ (any_or:X (match_operand:X 1 "register_operand") (match_operand:X 2 "const_int_operand"))) (clobber (match_operand:X 3 "register_operand"))] - "TARGET_ZBB + "(TARGET_ZBB || TARGET_ZBKB) && (riscv_const_insns (operands[2], true) > riscv_const_insns (GEN_INT (~INTVAL (operands[2])), true))" [(const_int 0)] diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 4d3ce2643012..7036c641abfe 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -4870,8 +4870,8 @@ riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UN return true; } - /* orn, andn and xorn pattern for zbb. */ - if (TARGET_ZBB + /* orn, andn and xnor patterns for Zbb or Zbkb. */ + if ((TARGET_ZBB || TARGET_ZBKB) && GET_CODE (XEXP (x, 0)) == NOT) { *total = riscv_binary_cost (x, 1, 2); @@ -16506,9 +16506,9 @@ synthesize_ior_xor (rtx_code code, rtx operands[3]) if (budget < 0) { /* We're going to have to synthesize the constant. However, if - we have Zbb, then we have XNOR and ORN. So if the inverted constant - is cheaper, invert it and use XNOR/ORN. */ - if (TARGET_ZBB + we have Zbb or Zbkb, then we have XNOR and ORN. If the inverted + constant is cheaper, invert it and use XNOR/ORN. */ + if ((TARGET_ZBB || TARGET_ZBKB) && (riscv_integer_cost (INTVAL (operands[2]), true) > riscv_integer_cost (~UINTVAL (operands[2]), true))) { @@ -16811,9 +16811,9 @@ synthesize_and (rtx operands[3]) if (ival || budget < 0) { /* We're going to have to synthesize the constant. However, if - we have Zbb, then we have ANDN. So if the inverted constant + we have Zbb or Zbkb, then we have ANDN. If the inverted constant is cheaper, invert it and use ANDN. */ - if (TARGET_ZBB + if ((TARGET_ZBB || TARGET_ZBKB) && riscv_integer_cost (~UINTVAL (operands[2]), true) > 0 && (riscv_integer_cost (UINTVAL (operands[2]), true) > riscv_integer_cost (~UINTVAL (operands[2]), true))) diff --git a/gcc/testsuite/gcc.target/riscv/zbkb-logic-imm.c b/gcc/testsuite/gcc.target/riscv/zbkb-logic-imm.c new file mode 100644 index 000000000000..b723ff6ec00b --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbkb-logic-imm.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc_zbkb -mabi=ilp32d" { target rv32 } } */ +/* { dg-options "-O2 -march=rv64gc_zbkb -mabi=lp64d" { target rv64 } } */ + +unsigned long f_and (unsigned long x) { return x & ~0xf000UL; } +unsigned long f_or (unsigned long x) { return x | ~0xf000UL; } +unsigned long f_xor (unsigned long x) { return x ^ ~0xf000UL; } + +/* { dg-final { scan-assembler-times {\mandn\M} 1 } } */ +/* { dg-final { scan-assembler-times {\morn\M} 1 } } */ +/* { dg-final { scan-assembler-times {\mxnor\M} 1 } } */
