From patchwork Tue Feb 20 04:26:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 203408 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp187107dyc; Mon, 19 Feb 2024 20:27:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXbPpXB/omlaCTHB7RqQ86ML7Utqt+LUYb7u7aPkBvCCYNz27OP+t9U/xvC1j5asF2nYOe2+EI1Xjc9+8C9Evgd9O4g6g== X-Google-Smtp-Source: AGHT+IF4XL6sczBCsXVn3ID4XNJ8MwfpJ3LfvesEqEHVB3X0eN4/c7AI4MKL0gAs51/KRBOxZD6n X-Received: by 2002:a0c:f04b:0:b0:68f:2b1e:5116 with SMTP id b11-20020a0cf04b000000b0068f2b1e5116mr11029021qvl.65.1708403253957; Mon, 19 Feb 2024 20:27:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708403253; cv=pass; d=google.com; s=arc-20160816; b=eVbsU7mUryU7JxrP3cnSaY9PyiBp69TWp65bRz0HiO6/JMcmdgWs3ETXm/7y0DN594 rePcYG0nFrGUFwENQy1vgXaPPT/78gCCuHxbyr88ReR6M06s4QgOZDvRyahdYXMI2A3K tOz1x9e5KRWMiPjyRQuETQOUgbVmxlEiC45CzKXeWQwMo+DpHfEEZ9wzhJhEsrXZtTLf Rrgv6lPuGd4d4OG6u8PYHJ+WzyjgMpmWzHdnecTuKsBhqWUe764Dc+X/nboZMbk6i/xv Zrxqup+FCP6jNu35EKTmAitPoNWBPeymiPw6XRWANxADkzLTJ2H2W+H8jlXZOQsSbHAh 9h7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :message-id:date:organization:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=wVJn1dGKfldpTVQ6JcG9k/6H+5rq4qZbByVF/VGgWls=; fh=SNQ2dhuUT4dRFKVgjFufmtJY/UvUEnG1jlWE2QciTlg=; b=WyewsLNCvISBoZATO4xr9HbzOVT4jQpim/S3OFiN+RwE2PNc5WKBCuWTGEW4HbBjd4 S1wgShtOZ42fKdD4FzqzKhGRlmKVqhtn1LkSZamz5XnamOzqA2TOsr3MdNJAqQlVUHYS b9SUmgYxgxCwvYj6RXN0SGpTip1j92034eHQnAPVwrKnVtSn6uF9lNrCIBFRTPo79I9W Fofl8xgrPgX+QhuNPikE2/n34qvu7j13TA7sRfKVWnglxJ31xYAmjr/m+HmRXUQ24HaL W+wDZSyctBhAmw4h4/bMpuGOhHM8QP+EGMPjrQDcVOxPGgAHD1eyAcNrbn/LFEwsaeTG xU5g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=QxmTt3Ld; arc=pass (i=1); 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"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gm11-20020a056214268b00b0068c8a605eb5si7543449qvb.96.2024.02.19.20.27.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 20:27:33 -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=@adacore.com header.s=google header.b=QxmTt3Ld; arc=pass (i=1); 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"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9E08D3858D38 for ; Tue, 20 Feb 2024 04:27:33 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by sourceware.org (Postfix) with ESMTPS id C6D953858D1E for ; Tue, 20 Feb 2024 04:26:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6D953858D1E Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C6D953858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::232 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708403214; cv=none; b=CIGJxPrEa/nVcFqzRcxr+7syRoeNlTy3Wc0oxHQGIoCueOIjIadUzxOtv/m643XQjBkdRiM5XTjfRKaI4oeCeFV2cjIajuoojZ0MZgT6lOLPzeWlhkInRORNOMmLBnNpXfBo9Bd43fyg5gsa/7uIddV6aknfRs+2oNjtLBTjFH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708403214; c=relaxed/simple; bh=76ISG6R/RxAFBIOC056tcRy4xr7EmftakQ6VKZl0Z9Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hD62j3h/xc71UVWbdbpPYNcgPDohn+xeTPu5WobbeolJvlscUu7K4IpQ9UCdX78Z/VN6vOsIz8yMD1K13ucoJfinXEtd7t4JPafSu8hJwg1dNGcJkONmwYUEwuIi6sn8XRd9+VDd97KAHaEPmNr3mlTb7hNaUhQ4d0th105nwCc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3bbbc6e51d0so2991861b6e.3 for ; Mon, 19 Feb 2024 20:26:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1708403211; x=1709008011; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=wVJn1dGKfldpTVQ6JcG9k/6H+5rq4qZbByVF/VGgWls=; b=QxmTt3LdUfazfDHLUyWk2o9wxS4D72wwfOfQ3j5hb4KREgZIIiCUZGbxrI8zkoP/x3 bDcSmhmGzfgKImFwkNNemmei++uUdQsc8vsAhiFBnvXvK/R2Dxu1da6vFOGbRoRq/HR0 VciKbCNBaBDl4cg5geVW6QzsltCc916nXi/ccwqsBriTaNRA8uti7tdmfAdpNO5deXvb tmxYXXj0yfHJA6oLf/IPFOlzmQ6SFqGqJKb0vXcfftSA6OOVmqZDyZC4Rze4CRBVGaAH eDMebJn1NeXSMyt3029m8PXLQG/Em+L4urFN2vzt7pkcKJNzdpXgNpDV7bvV0eqkGjOd 6PPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708403211; x=1709008011; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wVJn1dGKfldpTVQ6JcG9k/6H+5rq4qZbByVF/VGgWls=; b=sAXnHXbWEahHDeKz2k46km4FcCIWBx2SD7OCnhXlyL8eZRgazy7RurrNw1VuBEvbBa XCwMGmrW9B5jQerwi0k+eWloT8q8psnjZFmvpdoVxtO512/FdNKtey/9RWip84ByuJyS vdH6gJVMwIqpTCNk9n7flAGMCRcONuAlN9hu6Jd9QUlR3cu4sNLAZ5+GjKAEXEjDdyc3 9EotExjtytYn3Q5BesZChyPcSLmWVyPnQyajKukLNipHrIAvjzcNuPfOTzvQuo+yL7Rz JRJB9MhHDn1UwnZMjHXMe0I98HQtTIwwqpE63UytPqtHsvUs10i90XSofKVGGrUG73pk 0Z3w== X-Gm-Message-State: AOJu0YzP5KSpLha+m1uML/8TWpk4o+31+LOTDoR2o5/DYDAqNv2Hdqoo JjKKAHtJS4Sb+btZxxVHCAiRtzAnp9dpTSrAENFfzVr+4Unh9ZVwg/A0zDXLdP19f9Ibz6BYLzT YeA== X-Received: by 2002:a05:6808:bd4:b0:3c1:559b:4223 with SMTP id o20-20020a0568080bd400b003c1559b4223mr6933750oik.42.1708403211139; Mon, 19 Feb 2024 20:26:51 -0800 (PST) Received: from free.home ([2804:7f1:218a:c88b:e868:4eaf:8258:c30b]) by smtp.gmail.com with ESMTPSA id u21-20020aa78395000000b006e0ad616be3sm5737290pfm.110.2024.02.19.20.26.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 20:26:50 -0800 (PST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 41K4QdQG006076 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 20 Feb 2024 01:26:39 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Cc: Kito Cheng , Palmer Dabbelt , Andrew Waterman , Jim Wilson , Raphael Moreira Zinsly Subject: [PATCH] RISC-V: Fix CTZ unnecessary sign extension [PR #106888] Organization: Free thinker, does not speak for AdaCore Date: Tue, 20 Feb 2024 01:26:39 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, WEIRD_QUOTING 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.30 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791390650662102344 X-GMAIL-MSGID: 1791390650662102344 This backport for gcc-13 is required for pr90838.c to get the expected count of andi instructions on riscv64-elf, rather than fail because of two extra andi insns in functions where it is not necessary. (On riscv32-elf, it passes). Regstrapped on x86_64-linux-gnu, along with other backports, and tested manually on riscv64-elf. Ok to install? From: Raphael Moreira Zinsly Changes since v1: - Remove subreg from operand 1. -- >8 -- We were not able to match the CTZ sign extend pattern on RISC-V because it gets optimized to zero extend and/or to ANDI patterns. For the ANDI case, combine scrambles the RTL and generates the extension by using subregs. gcc/ChangeLog: PR target/106888 * config/riscv/bitmanip.md (disi2): Match with any_extend. (disi2_sext): New pattern to match with sign extend using an ANDI instruction. gcc/testsuite/ChangeLog: PR target/106888 * gcc.target/riscv/pr106888.c: New test. * gcc.target/riscv/zbbw.c: Check for ANDI. (cherry picked from commit 9000da00dd70988f30d43806bae33b22ee6b9904) --- gcc/config/riscv/bitmanip.md | 13 ++++++++++++- gcc/testsuite/gcc.target/riscv/pr106888.c | 12 ++++++++++++ gcc/testsuite/gcc.target/riscv/zbbw.c | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/riscv/pr106888.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 7aa591689ba87..cc55ca133c3fe 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -246,13 +246,24 @@ (define_insn "*si2" (define_insn "*disi2" [(set (match_operand:DI 0 "register_operand" "=r") - (sign_extend:DI + (any_extend:DI (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r"))))] "TARGET_64BIT && TARGET_ZBB" "w\t%0,%1" [(set_attr "type" "bitmanip") (set_attr "mode" "SI")]) +;; A SImode clz_ctz_pcnt may be extended to DImode via subreg. +(define_insn "*disi2_sext" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI (subreg:DI + (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r")) 0) + (match_operand:DI 2 "const_int_operand")))] + "TARGET_64BIT && TARGET_ZBB && ((INTVAL (operands[2]) & 0x3f) == 0x3f)" + "w\t%0,%1" + [(set_attr "type" "bitmanip") + (set_attr "mode" "SI")]) + (define_insn "*di2" [(set (match_operand:DI 0 "register_operand" "=r") (clz_ctz_pcnt:DI (match_operand:DI 1 "register_operand" "r")))] diff --git a/gcc/testsuite/gcc.target/riscv/pr106888.c b/gcc/testsuite/gcc.target/riscv/pr106888.c new file mode 100644 index 0000000000000..77fb8e5b79c6b --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/pr106888.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb -mabi=lp64" } */ + +int +ctz (int i) +{ + int res = __builtin_ctz (i); + return res&0xffff; +} + +/* { dg-final { scan-assembler-times "ctzw" 1 } } */ +/* { dg-final { scan-assembler-not "andi" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/zbbw.c b/gcc/testsuite/gcc.target/riscv/zbbw.c index 709743c3b6807..f7b2b63853f40 100644 --- a/gcc/testsuite/gcc.target/riscv/zbbw.c +++ b/gcc/testsuite/gcc.target/riscv/zbbw.c @@ -23,3 +23,4 @@ popcount (int i) /* { dg-final { scan-assembler-times "clzw" 1 } } */ /* { dg-final { scan-assembler-times "ctzw" 1 } } */ /* { dg-final { scan-assembler-times "cpopw" 1 } } */ +/* { dg-final { scan-assembler-not "andi\t" } } */