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: 19447 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1827484wru; Sun, 13 Nov 2022 12:50:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf7Qryg2U4Vs/gDGi+X5p28qRIhSpMg8/C2Q9aT+6fzgwX9qt82E90ASeOjJgvSSv9fDL2lX X-Received: by 2002:aa7:c54a:0:b0:459:9e3:c0b0 with SMTP id s10-20020aa7c54a000000b0045909e3c0b0mr8714106edr.279.1668372611390; Sun, 13 Nov 2022 12:50:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668372611; cv=none; d=google.com; s=arc-20160816; b=srNFb7NBRfJf+MSyNwK9BuTW0z7oA/nUdvSiA1uwIzl8rq2zaC74E2Ffi909suayVo XAAx886NCxETHmq8gAgX7El3ebssGG8rS/T+ji9n6JHU36TmNy4qF7Eq0hdDZQQaxe3L gNSPRXqlwTBLt+RJTO2y37k9FkrGmz2ZEsTzHP1Eo0aitnPCfAN0hTZWaUK5qGQfTRCM Wy8E2juGa/SMVXJqN40/uB93anecSCFvWrejuzPB7Ok+VN8VoPoHZ1eppmG5DQ8RofAk gdHbISnpAmQQuEvJ9IxEEOxfmCfchkOXzOobq8lAJ8VYkPRnCBZ3S7v23q5gD+T5fusg x+zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=zzv4I44b43BK6ZRfKwyamNUK/0FhAkidEnHf49cxOg8=; b=RZILt3hHrCZd9H0lkOLeIu+4Llx/YsMqdyh0hkhjRx7BpTIIsQ5ItpMtwMJW2yR8oA WGWr0OfxrGx4CbJHP24zviGXhbElqfeiO0PDFwp1fBEsaPBLd0u872FW/Z0gETucBsNy Hwa9GO+s+cBE9hzaLz7otrA0ApB7+M3fAZTb3CHiQIfiXXhb76sgevu4/JpOBTjJ9Rpq 7vPQQBOlvMTZGlgLQZAHMH5FoYwKXm5jB7sPCuPrX2ET6nGb6sQ9FXSOml9l0exRPBHR 0KgPltgo/qN0ngVHAHos19ae5Vmg0HvgGVL1z6oncZBH8vY3hC3UBK3QV+zqfjLn3+8i SVfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=kf0Mg+XH; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j20-20020aa7ca54000000b00461dca37cd0si7069168edt.126.2022.11.13.12.50.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 12:50:11 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=kf0Mg+XH; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BCE123885C24 for ; Sun, 13 Nov 2022 20:49:09 +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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749415479074170302?= X-GMAIL-MSGID: =?utf-8?q?1749415479074170302?= 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 } } */