From patchwork Mon May 13 19:18:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 1934795 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=NCO1pKHa; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VdTnF68mzz1yfq for ; Tue, 14 May 2024 05:19:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 350FB38708D8 for ; Mon, 13 May 2024 19:19:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 5D31C3858432 for ; Mon, 13 May 2024 19:18:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D31C3858432 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ventanamicro.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5D31C3858432 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::531 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715627929; cv=none; b=nxRfRCPpcFYYtNy3yUVT4WHJhgR1FSFgBe6EK8csvtiAaA3FEh48tcYaMRKx1nwT5zFqOwgIx5E8kgC30T7yCDfTLxjwol2r2LHhUFJqWqnOpLMiwHz+O+nkKKXFYjnVXHaI+xjR7H1slH8/L0TghUr0w0t8NrU9Xz14h0XDz7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715627929; c=relaxed/simple; bh=9qt78uDrHZq6nGaKzZr5Z8zRzlMJniKo+rNMRx/3F7s=; h=DKIM-Signature:Message-ID:Date:MIME-Version:From:To:Subject; b=MClPD2hZiMEmCMXAbVS2QQAj3sDcXz/B+Dycoy7mPuYHMQ/rzYm0LSY3j9JkWlGX2zFd7Akw0MuqalIFnmx8mcY9JXI4KxOAg70xmXB9QnAdw3XKtPjmDiMJR5T0/E1d7ledMCcr8lJAbE9mBynMoDlUKzoNwEDQ0PVkFoba/Zw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-5e4f79007ffso3233680a12.2 for ; Mon, 13 May 2024 12:18:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1715627925; x=1716232725; darn=gcc.gnu.org; h=subject:to:from:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=zSMqjo6CraMeShEDfj+f14h7C4aSGeswz6JBZJWUjGw=; b=NCO1pKHa5idbpUK4AnvNb3hMGFaR0Xy1d8DJYgLtalHbHe2Em1dFhbOYvwUgP2phdE ngd0vyfhHTU86NFpaCZcN2TTGOzTpKfIOOP7sOOESH2bIhRjZ3u+T7Kp2064shaMXXTN UAc7qVKHAaEZyEgQDiphYk9TvLj+UpF765POOgoG9sLAm2VKXyJEM6ix6VDiRYQq+Raf ktz//SXYLL4eB+ngJOUpfUol4usTMjBFPBxvrhWgqnDaf1SzxxduFk42HnAwEvx6BOwX hkQetIo6lfpbd/mlWqQe60WA0phg78Y9ndPs6O+8OwXmyaJnRQC5hqeytuBfVf4LbKpG wR8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715627925; x=1716232725; h=subject:to:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zSMqjo6CraMeShEDfj+f14h7C4aSGeswz6JBZJWUjGw=; b=O1QiSzObEHUKWTXWyobTTKolEj7bUHP0qhLr+QE2UFrwz3wN0FmlS0eh59F/l3frvK Aas1813zaSTgR9DcOVE2ScGJkHdmul/nU09tw5QPZJMxAkbK/wNCrif3mQc20LdNM6Dg r5mvQ5xqZsBvyFtL1/Q2r/R+tUS2lQwbs2NZOqPP9ch86qosTV4ZAMGneLe5qr112/u0 BB3BzBMvopDbasakJ/FcXA247ornUhWQASQcOJH70uyJwOYefUvNQadgInIWt+YXq73L 46+WTWgxWy8E/tTvWeFRADFS4p/tvQbt8fdRnp7s1vXcDjpcrHzsnOieMKAuxe5McUNG IFTA== X-Gm-Message-State: AOJu0YxNcXfNKkY+TQnB9KryVvU9AsjymfWCQ717bFz6fZsE4p/PGFUf eV/e4PgOvUMSA20AYyvwXtmHx+J0/xAXHlh27qd9hwyrG3gkI/CPJWEUm7WpcGubs+6J2v+0xqP Q X-Google-Smtp-Source: AGHT+IGoR+ScDxrpDW3fwoCGOu/NPiMk1OrkY/ayCNP3Q8zrNiQieF2JJENjh37t1orMNmKCCD4CDQ== X-Received: by 2002:a05:6a20:6a22:b0:1af:8e8d:cefd with SMTP id adf61e73a8af0-1afde1ddafdmr14644139637.51.1715627925486; Mon, 13 May 2024 12:18:45 -0700 (PDT) Received: from [172.31.0.109] ([136.36.72.243]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-63411530b0esm8163036a12.88.2024.05.13.12.18.44 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 May 2024 12:18:44 -0700 (PDT) Message-ID: <8155a1f9-f6cf-41a4-abb3-a025ded33daf@ventanamicro.com> Date: Mon, 13 May 2024 13:18:43 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Content-Language: en-US From: Jeff Law To: "gcc-patches@gcc.gnu.org" Subject: [to-be-committed][RISC-V] Improve AND with some constants X-Spam-Status: No, score=-10.9 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 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+incoming=patchwork.ozlabs.org@gcc.gnu.org If we have an AND with a constant operand and the constant operand requires synthesis, then we may be able to generate more efficient code than we do now. Essentially the need for constant synthesis gives us a budget for alternative ways to clear bits, which zext.w can do for bits 32..63 trivially. So if we clear 32..63 via zext.w, the constant for the remaining bits to clear may be simple enough to use with andi or bseti. That will save us an instruction. This has tested in Ventana's CI system as well as my own. I'll wait for the upstream CI tester to report success before committing. Jeff gcc/ * config/riscv/bitmanip.md: Add new splitter for AND with a constant that masks off bits 32..63 and needs synthesis. gcc/testsuite/ * gcc.target/riscv/zba_zbs_and-1.c: New test. +++ b/gcc/testsuite/gcc.target/riscv/zba_zbs_and-1.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 724511b6df3..8769a6b818b 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -843,6 +843,40 @@ (define_insn_and_split "*andi_extrabit" } [(set_attr "type" "bitmanip")]) +;; If we have the ZBA extension, then we can clear the upper half of a 64 +;; bit object with a zext.w. So if we have AND where the constant would +;; require synthesis of two or more instructions, but 32->64 sign extension +;; of the constant is a simm12, then we can use zext.w+andi. If the adjusted +;; constant is a single bit constant, then we can use zext.w+bclri +;; +;; With the mvconst_internal pattern claiming a single insn to synthesize +;; constants, this must be a define_insn_and_split. +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI (match_operand:DI 1 "register_operand" "r") + (match_operand 2 "const_int_operand" "n")))] + "TARGET_64BIT + && TARGET_ZBA + && !paradoxical_subreg_p (operands[1]) + /* Only profitable if synthesis takes more than one insn. */ + && riscv_const_insns (operands[2]) != 1 + /* We need the upper half to be zero. */ + && (INTVAL (operands[2]) & HOST_WIDE_INT_C (0xffffffff00000000)) == 0 + /* And the the adjusted constant must either be something we can + implement with andi or bclri. */ + && ((SMALL_OPERAND (sext_hwi (INTVAL (operands[2]), 32)) + || (TARGET_ZBS && popcount_hwi (INTVAL (operands[2])) == 31)) + && INTVAL (operands[2]) != 0x7fffffff)" + "#" + "&& 1" + [(set (match_dup 0) (zero_extend:DI (match_dup 3))) + (set (match_dup 0) (and:DI (match_dup 0) (match_dup 2)))] + "{ + operands[3] = gen_lowpart (SImode, operands[1]); + operands[2] = GEN_INT (sext_hwi (INTVAL (operands[2]), 32)); + }" + [(set_attr "type" "bitmanip")]) + ;; IF_THEN_ELSE: test for 2 bits of opposite polarity (define_insn_and_split "*branch_mask_twobits_equals_singlebit" [(set (pc) diff --git a/gcc/testsuite/gcc.target/riscv/zba_zbs_and-1.c b/gcc/testsuite/gcc.target/riscv/zba_zbs_and-1.c new file mode 100644 index 00000000000..23fd769449e --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zba_zbs_and-1.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zba_zbb_zbs -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ + + +unsigned long long w32mem_1(unsigned long long w32) +{ + return w32 & ~(1U << 0); +} + +unsigned long long w32mem_2(unsigned long long w32) +{ + return w32 & ~(1U << 30); +} + +unsigned long long w32mem_3(unsigned long long w32) +{ + return w32 & ~(1U << 31); +} + +/* If we do synthesis, then we'd see an addi. */ +/* { dg-final { scan-assembler-not "addi\t" } } */