From patchwork Sun Nov 13 20:48:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 60538 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4043A389EC5E for ; Sun, 13 Nov 2022 20:49:02 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id 243D3388A026 for ; Sun, 13 Nov 2022 20:48:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 243D3388A026 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-lj1-x22a.google.com with SMTP id t10so10723207ljj.0 for ; Sun, 13 Nov 2022 12:48:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zzv4I44b43BK6ZRfKwyamNUK/0FhAkidEnHf49cxOg8=; b=kf0Mg+XHN0iQ3nEt/E0TNYNHyKII5iN7RI91JGvm0MyHQMd4rM9JlGjQR/wYhWD54u 5bHol+XDmvw6UDBPM+1f7We7EdyuEIneVXEYzGPh0lvhHaLALuK0MFoo4hNDfKj+lb6K lbRiSpjFWm+zHm26HwaASIRBzOUL47SSpVhRoU8exTthbNVALqAVlVJ+a//U5ThZM22p TA/kfpVtcn/i0rroo9We2jK07npfE5o4RcQefayw1N3q910bj9xZLCrJItN6zkXbB9Fj Dn2SQjPkbm+gWPr03btELYmT/2e8jRlbVeXad9tyyPS/FLyEG6rspiFoHgwiPSkpEVis 8lkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zzv4I44b43BK6ZRfKwyamNUK/0FhAkidEnHf49cxOg8=; b=f2dOzdcBhmkST8+4w5mH9MmiprNok/VI7ZhRydbL+BzLbPAcqHfB2jQkdGPVRQA3P2 50sDIHi77dtPjgBYwM4rqomWDL63JjJfIBjHzlYod+lFPMrWinfcu2amRm2j9vTVRXiR 3z12RL4DnG5Klc6jUbwMnKAXxcbXF3o4FOPB+1i5JppOKS6UbZM21wB5ZLpXy9YeLJ4W HM08FneELAONh6YjCYP4VtVGtanSjg2JVA5wfYONUl56ymYDutebZraE6BSgLrrjtSpY 5BgFvF93ZoL3SJ1AxbiC57iYiZISIO4hXALhrx7YEYi9r2a2hg9eZS0xtkj6bgytF/KM SIcQ== X-Gm-Message-State: ANoB5pk1xoa3bbFzI3hVv3NLS7C6xdOxFBObxKD12DChUebPcIWkAssz 0HuKv1B/Fq4AUobtlts5ME1+U8uFbTxn1ORn X-Google-Smtp-Source: AA0mqf5IYOoRtnyl1kg/2utac0FbnqmnR4NRvPhGN7op6u5oX/fAQ2h/B9mtQ5EMW3gtQXNZ+GvATw== X-Received: by 2002:a05:651c:2203:b0:26f:bda0:cf0d with SMTP id y3-20020a05651c220300b0026fbda0cf0dmr3183424ljq.227.1668372523364; Sun, 13 Nov 2022 12:48:43 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id bg20-20020a05651c0b9400b00277129b4a10sm1641431ljb.86.2022.11.13.12.48.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 12:48:42 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Vineet Gupta , Kito Cheng , Christoph Muellner , Jeff Law , Palmer Dabbelt , Philipp Tomsich Subject: [PATCH] RISC-V: Split "(a & (1UL << bitno)) ? 0 : -1" to bext + addi Date: Sun, 13 Nov 2022 21:48:40 +0100 Message-Id: <20221113204840.4062092-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" For a straightforward application of bext for the following function long bext64(long a, char bitno) { return (a & (1UL << bitno)) ? 0 : -1; } we generate srl a0,a0,a1 # 7 [c=4 l=4] lshrdi3 andi a0,a0,1 # 8 [c=4 l=4] anddi3/1 addi a0,a0,-1 # 14 [c=4 l=4] adddi3/1 due to the following failed match at combine time: (set (reg:DI 82) (zero_extract:DI (reg:DI 83) (const_int 1 [0x1]) (reg:DI 84))) The existing pattern for bext requires the 3rd argument to zero_extract to be a QImode register wrapped in a zero_extension. This adds an additional pattern that allows an Xmode argument. With this change, the testcase compiles to bext a0,a0,a1 # 8 [c=4 l=4] *bextdi addi a0,a0,-1 # 14 [c=4 l=4] adddi3/1 gcc/ChangeLog: * config/riscv/bitmanip.md (*bext): Add an additional pattern that allows the 3rd argument to zero_extract to be an Xmode register operand. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbs-bext.c: Add testcases. * gcc.target/riscv/zbs-bexti.c: Add testcases. Signed-off-by: Philipp Tomsich --- gcc/config/riscv/bitmanip.md | 12 +++++++++++ gcc/testsuite/gcc.target/riscv/zbs-bext.c | 23 +++++++++++++++++--- gcc/testsuite/gcc.target/riscv/zbs-bexti.c | 25 ++++++++++++++++------ 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index f1d8f24c2d3..8222c600ca5 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -644,6 +644,18 @@ "bext\t%0,%1,%2" [(set_attr "type" "bitmanip")]) +;; When performing `(a & (1UL << bitno)) ? 0 : -1` the combiner +;; usually has the `bitno` typed as X-mode (i.e. no further +;; zero-extension is performed around the bitno). +(define_insn "*bext" + [(set (match_operand:X 0 "register_operand" "=r") + (zero_extract:X (match_operand:X 1 "register_operand" "r") + (const_int 1) + (match_operand:X 2 "register_operand" "r")))] + "TARGET_ZBS" + "bext\t%0,%1,%2" + [(set_attr "type" "bitmanip")]) + (define_insn "*bexti" [(set (match_operand:X 0 "register_operand" "=r") (zero_extract:X (match_operand:X 1 "register_operand" "r") diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bext.c b/gcc/testsuite/gcc.target/riscv/zbs-bext.c index 47982396119..8de9c5a167c 100644 --- a/gcc/testsuite/gcc.target/riscv/zbs-bext.c +++ b/gcc/testsuite/gcc.target/riscv/zbs-bext.c @@ -1,6 +1,6 @@ /* { dg-do compile } */ /* { dg-options "-march=rv64gc_zbs -mabi=lp64" } */ -/* { dg-skip-if "" { *-*-* } { "-O0" } } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ /* bext */ long @@ -16,6 +16,23 @@ foo1 (long i) return 1L & (i >> 20); } +long bext64_1(long a, char bitno) +{ + return (a & (1UL << bitno)) ? 1 : 0; +} + +long bext64_2(long a, char bitno) +{ + return (a & (1UL << bitno)) ? 0 : -1; +} + +long bext64_3(long a, char bitno) +{ + return (a & (1UL << bitno)) ? -1 : 0; +} + /* { dg-final { scan-assembler-times "bexti\t" 1 } } */ -/* { dg-final { scan-assembler-times "bext\t" 1 } } */ -/* { dg-final { scan-assembler-not "andi" } } */ +/* { dg-final { scan-assembler-times "bext\t" 4 } } */ +/* { dg-final { scan-assembler-times "addi\t" 1 } } */ +/* { dg-final { scan-assembler-times "neg\t" 1 } } */ +/* { dg-final { scan-assembler-not "andi" } } */ \ No newline at end of file diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bexti.c b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c index 99e3b58309c..30b69c9bc3e 100644 --- a/gcc/testsuite/gcc.target/riscv/zbs-bexti.c +++ b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c @@ -1,14 +1,25 @@ /* { dg-do compile } */ -/* { dg-options "-march=rv64gc_zbs -mabi=lp64 -O2" } */ +/* { dg-options "-march=rv64gc_zbs -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ /* bexti */ -#define BIT_NO 4 +#define BIT_NO 21 -long -foo0 (long a) +long bexti64_1(long a, char bitno) { - return (a & (1 << BIT_NO)) ? 0 : -1; + return (a & (1UL << BIT_NO)) ? 1 : 0; } -/* { dg-final { scan-assembler "bexti" } } */ -/* { dg-final { scan-assembler "addi" } } */ +long bexti64_2(long a, char bitno) +{ + return (a & (1UL << BIT_NO)) ? 0 : -1; +} + +long bexti64_3(long a, char bitno) +{ + return (a & (1UL << BIT_NO)) ? -1 : 0; +} + +/* { dg-final { scan-assembler-times "bexti\t" 3 } } */ +/* { dg-final { scan-assembler-times "addi\t" 1 } } */ +/* { dg-final { scan-assembler-times "neg\t" 1 } } */