From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1923) id E9F363AA88FD; Thu, 17 Nov 2022 15:21:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9F363AA88FD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668698463; bh=5eSDAwqxkalKR/SXriGaUCwly0M+ZeTrDvizLOXFrkE=; h=From:To:Subject:Date:From; b=h2ozO3c792mICZeIG/ABmwOCXEVfBw6x9AXi/gDG1pvlqPU9rVifLgV8wJD9mSxl9 pCLK5XVASmwInWDS9RFFKY+4SPvbuxbqtmPliO+LosPsn1iann+POWiJryLKeAka/E IMaOJd4ULVpngZF7nZgywPkmzPUbUMgaUTn7o0yE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Philipp Tomsich To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4128] RISC-V: bitmanip: add splitter to use bexti for "(a & (1 << BIT_NO)) ? 0 : -1" X-Act-Checkin: gcc X-Git-Author: Philipp Tomsich X-Git-Refname: refs/heads/master X-Git-Oldrev: 705bae2351a870dc3ff59db47fa0de6baaf829c8 X-Git-Newrev: 1957bedf29a1b2cc231972aba680fe80199d5498 Message-Id: <20221117152103.E9F363AA88FD@sourceware.org> Date: Thu, 17 Nov 2022 15:21:03 +0000 (GMT) List-Id: https://gcc.gnu.org/g:1957bedf29a1b2cc231972aba680fe80199d5498 commit r13-4128-g1957bedf29a1b2cc231972aba680fe80199d5498 Author: Philipp Tomsich Date: Tue Nov 9 18:54:54 2021 +0100 RISC-V: bitmanip: add splitter to use bexti for "(a & (1 << BIT_NO)) ? 0 : -1" Consider creating a polarity-reversed mask from a set-bit (i.e., if the bit is set, produce all-ones; otherwise: all-zeros). Using Zbb, this can be expressed as bexti, followed by an addi of minus-one. To enable the combiner to discover this opportunity, we need to split the canonical expression for "(a & (1 << BIT_NO)) ? 0 : -1" into a form combinable into bexti. Consider the function: long f(long a) { return (a & (1 << BIT_NO)) ? 0 : -1; } This produces the following sequence prior to this change: andi a0,a0,16 seqz a0,a0 neg a0,a0 ret Following this change, it results in: bexti a0,a0,4 addi a0,a0,-1 ret gcc/ChangeLog: * config/riscv/bitmanip.md: Add a splitter to generate polarity-reversed masks from a set bit using bexti + addi. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbs-bexti.c: New test. Diff: --- gcc/config/riscv/bitmanip.md | 13 +++++++++++++ gcc/testsuite/gcc.target/riscv/zbs-bexti.c | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index fb062f6eb43..0dd6ebbdd0b 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -429,3 +429,16 @@ (const_int 1) (match_dup 2))) (set (match_dup 0) (xor:X (match_dup 0) (const_int 1)))]) + +;; We can create a polarity-reversed mask (i.e. bit N -> { set = 0, clear = -1 }) +;; using a bext(i) followed by an addi instruction. +;; This splits the canonical representation of "(a & (1 << BIT_NO)) ? 0 : -1". +(define_split + [(set (match_operand:GPR 0 "register_operand") + (neg:GPR (eq:GPR (zero_extract:GPR (match_operand:GPR 1 "register_operand") + (const_int 1) + (match_operand 2)) + (const_int 0))))] + "TARGET_ZBS" + [(set (match_dup 0) (zero_extract:GPR (match_dup 1) (const_int 1) (match_dup 2))) + (set (match_dup 0) (plus:GPR (match_dup 0) (const_int -1)))]) diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bexti.c b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c index c15098eb6cc..d7a89638f46 100644 --- a/gcc/testsuite/gcc.target/riscv/zbs-bexti.c +++ b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c @@ -26,6 +26,6 @@ long bexti64_4(long a, char bitno) } /* { dg-final { scan-assembler-times "bexti\t" 4 } } */ -/* { dg-final { scan-assembler-times "xori\t|snez\t" 1 } } */ +/* { dg-final { scan-assembler-times "xori\t" 1 } } */ /* { dg-final { scan-assembler-times "addi\t" 1 } } */ /* { dg-final { scan-assembler-times "neg\t" 1 } } */