From patchwork Sun Nov 13 20:48:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 1703338 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=vrull.eu header.i=@vrull.eu header.a=rsa-sha256 header.s=google header.b=NsUelwuO; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N9Pgt2zsNz1yhv for ; Mon, 14 Nov 2022 07:49:30 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76933393C842 for ; Sun, 13 Nov 2022 20:49:28 +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 167F33836029 for ; Sun, 13 Nov 2022 20:48:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 167F33836029 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 d20so10650108ljc.12 for ; Sun, 13 Nov 2022 12:48:54 -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=C9Yl+h268KO7VihatZQCiOynGa2WlOVqBWAmJfShU0U=; b=NsUelwuOMxMQFGVeNfWwdYi5OhVLDNkBiZi3qRLzGZQbnJNqhT7veSVQm/RfEzZJp9 BpMU1yW0qCzZy/r9449A9DIGsxWL+75922U4VRXENaE6fRZKPsLgay87an5N5s5Z4GnQ /i4B1AYxcN5UN5ajXpwFncNfSMd9KqNrH5RX/BkG4m5UN7j2vzA6V5qwB+Hik53Q5IWM SoSzYY0N6WskMDKP+kltj83spVZcewqJaroLoZ4yKSIDDHuVMZ8/kg1g5kKd+d7FFWWZ BcYMkAgXZdBrY/+WqF5hrxv4ZyXdvUSsBkgnvhftPebFabRQfJGksxtr/3x2856GC3xr kYag== 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=C9Yl+h268KO7VihatZQCiOynGa2WlOVqBWAmJfShU0U=; b=tIP/n7dBqRrvEWrc/UQhgw82z/bIPAhGae6gXIiTMeM78l1Gg4334y2mkHf6sKL2+Y oLQx9SYnCyXLoPvB/EKTO9YnruS5O9ytzbf83Egy1soCbo76MQNUDaZHrZNOuywH/bC9 151yyuKvYT+vBi60dTNtL9ruv0X41PDrTAE12pIIIcGLLkK5S1RV3pwlh0tD5iwSE+JL y/CtpdMYdrJRnwkWEKCxzusZNRTRGz/hHUIiEdBMb/Shq+swtOhy+zVd2f7P8kbfKJBh PmdACUWmxAJfms0rrSIYbJV+91ZA3yEMp2cvy9ax+GuyNAXlbB+DYtUCk9WqU3v71Ay7 VYQQ== X-Gm-Message-State: ANoB5pnxiAQfsX3rl8BFCp+ab1nszbuxL/KBo++B/8yV0bJLGed5D1kP jYg9jCbFIoiOh1BS1o6AlxwS27a6epAGxEXz X-Google-Smtp-Source: AA0mqf5kBfgkqMqlHQVFuCg7sqRnMzpvVqxtp9K1b61XnFg+QZyUuDKhgPdiCmfACOF706n0bqzDFg== X-Received: by 2002:a2e:a885:0:b0:26f:a9e1:9181 with SMTP id m5-20020a2ea885000000b0026fa9e19181mr3170653ljq.223.1668372532274; Sun, 13 Nov 2022 12:48:52 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id a3-20020a05651c030300b002772414817esm1644230ljp.1.2022.11.13.12.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Nov 2022 12:48:51 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Jeff Law , Palmer Dabbelt , Christoph Muellner , Kito Cheng , Vineet Gupta , Philipp Tomsich Subject: [PATCH] RISC-V: Split "(a & (1UL << bitno)) ? 0 : 1" to bext + xori Date: Sun, 13 Nov 2022 21:48:49 +0100 Message-Id: <20221113204849.4062129-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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" We avoid reassociating "(~(a >> BIT_NO)) & 1" into "((~a) >> BIT_NO) & 1" by splitting it into a zero-extraction (bext) and an xori. This both avoids burning a register on a temporary and generates a sequence that clearly captures 'extract bit, then invert bit'. This change improves the previously generated srl a0,a0,a1 not a0,a0 andi a0,a0,1 into bext a0,a0,a1 xori a0,a0,1 Signed-off-by: Philipp Tomsich gcc/ChangeLog: * config/riscv/bitmanip.md: Add split covering "(a & (1 << BIT_NO)) ? 0 : 1". gcc/testsuite/ChangeLog: * gcc.target/riscv/zbs-bext.c: Add testcases. * gcc.target/riscv/zbs-bexti.c: Add testcases. --- gcc/config/riscv/bitmanip.md | 13 +++++++++++++ gcc/testsuite/gcc.target/riscv/zbs-bext.c | 10 ++++++++-- gcc/testsuite/gcc.target/riscv/zbs-bexti.c | 10 ++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 8222c600ca5..7a8f4e35880 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -677,3 +677,16 @@ "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)))]) + +;; Split for "(a & (1 << BIT_NO)) ? 0 : 1": +;; We avoid reassociating "(~(a >> BIT_NO)) & 1" into "((~a) >> BIT_NO) & 1", +;; so we don't have to use a temporary. Instead we extract the bit and then +;; invert bit 0 ("a ^ 1") only. +(define_split + [(set (match_operand:X 0 "register_operand") + (and:X (not:X (lshiftrt:X (match_operand:X 1 "register_operand") + (subreg:QI (match_operand:X 2 "register_operand") 0))) + (const_int 1)))] + "TARGET_ZBS" + [(set (match_dup 0) (zero_extract:X (match_dup 1) (const_int 1) (match_dup 2))) + (set (match_dup 0) (xor:X (match_dup 0) (const_int 1)))]) diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bext.c b/gcc/testsuite/gcc.target/riscv/zbs-bext.c index 8de9c5a167c..a8aadb60390 100644 --- a/gcc/testsuite/gcc.target/riscv/zbs-bext.c +++ b/gcc/testsuite/gcc.target/riscv/zbs-bext.c @@ -23,16 +23,22 @@ long bext64_1(long a, char bitno) long bext64_2(long a, char bitno) { - return (a & (1UL << bitno)) ? 0 : -1; + return (a & (1UL << bitno)) ? 0 : 1; } long bext64_3(long a, char bitno) +{ + return (a & (1UL << bitno)) ? 0 : -1; +} + +long bext64_4(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" 4 } } */ +/* { dg-final { scan-assembler-times "bext\t" 5 } } */ +/* { dg-final { scan-assembler-times "xori\t|snez\t" 1 } } */ /* { 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 30b69c9bc3e..c15098eb6cc 100644 --- a/gcc/testsuite/gcc.target/riscv/zbs-bexti.c +++ b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c @@ -12,14 +12,20 @@ long bexti64_1(long a, char bitno) long bexti64_2(long a, char bitno) { - return (a & (1UL << BIT_NO)) ? 0 : -1; + return (a & (1UL << BIT_NO)) ? 0 : 1; } long bexti64_3(long a, char bitno) +{ + return (a & (1UL << BIT_NO)) ? 0 : -1; +} + +long bexti64_4(long a, char bitno) { return (a & (1UL << BIT_NO)) ? -1 : 0; } -/* { dg-final { scan-assembler-times "bexti\t" 3 } } */ +/* { dg-final { scan-assembler-times "bexti\t" 4 } } */ +/* { dg-final { scan-assembler-times "xori\t|snez\t" 1 } } */ /* { dg-final { scan-assembler-times "addi\t" 1 } } */ /* { dg-final { scan-assembler-times "neg\t" 1 } } */