From patchwork Sun Jun 23 14:40:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 1951216 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=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=CqQDdpk6; dkim-atps=neutral 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=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 [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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W6Yft32Nyz20X6 for ; Mon, 24 Jun 2024 00:40:38 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EA81385E839 for ; Sun, 23 Jun 2024 14:40:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-yw1-x112e.google.com (mail-yw1-x112e.google.com [IPv6:2607:f8b0:4864:20::112e]) by sourceware.org (Postfix) with ESMTPS id 9380C385DDF1 for ; Sun, 23 Jun 2024 14:40:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9380C385DDF1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9380C385DDF1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719153611; cv=none; b=R6p/ZYSB2Ye40x69wVnIIZV2fjgxktsObbFI+iJevTghdjZzmAzL1YJqMihCNMzxqStN3sYKKW3qWECMtz/9Bnge5sPRT1Nf6L7ma/wmv1nkxSCduTF+PCmJFoSER2nPu6bNnXYrxzxWcZhdo7B/RxCs3VmThSTJ/bbTT/gKVmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719153611; c=relaxed/simple; bh=9f4VsxzZT1ole/3lN4v6XTi3dUJ+7/KRzMDMPTGag70=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=LV/1hpVnLQqfXEfaVCqbTrNPQBhN6eyOiazVC+BoUKplST14YbyTOAqZcyR6rsG5nqSL1JHCMGr24mjRF1uDPmvB2+Ysj4MM3+Llg/QcaIGa72Tp3UTNhu8mkaQpV9WDZOFMIWTus0Jg74hGyWEWV5jCpRedafHnANM+vnx7x0k= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-63bf211542cso28954797b3.1 for ; Sun, 23 Jun 2024 07:40:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719153607; x=1719758407; darn=gcc.gnu.org; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=m1E5t3J7b6wPI7iu9qM/K/Ev9OtVZVyjOSKFVlkPXfo=; b=CqQDdpk6wXyRODO9dChtfmOFjUw3M3rgtFieYDyJAAr1abCOiFlREYvrVUpLdDa6Dn D0tueu+pxSfR6EEXdkreNPb9jiLgvKa90EAAkTXiDsY0TPvlQK00vNzKTgwYdbCnLNIe CZBS2hpuOlIvv7erHU4tQOChWAYgztnXfsVlSmxtp4/7tTgWmgG2djY2HiJbpgmU694P ngORadeUioFDyynaPhON9nvNhlKKVPxh/bdHW09v/FemPcXFvmzbIV8y9iVL/wIsoHkK fNvKbpxebdaq/gkBlGsOnz5ABahnXXp1BGppuI27s/hNonP/IM3Xh6aetG9CJf/qtAha Ctsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719153607; x=1719758407; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=m1E5t3J7b6wPI7iu9qM/K/Ev9OtVZVyjOSKFVlkPXfo=; b=ML4iY/vtX1C6HEwSbRna0ibC4t1CmMZsbXRGU5re422qDH8VwffjFJp3fRJVnQcuoJ RddVcPSRyU2wS0CY1kNWk3qQhY68oRZbbqtj3fIhTJbVRqfb1TSjYVNDP1C4e8WI3sGb h2jxm2BcBZ7Z7VYTKUl1JOwugNJbzlbdOfO30WeOgmAIk6Y1FfWREQcBZzWoSrULUQJz RRFUBIHRizDQLilA0KH8KdFLKGKTfJGCMKgZIn6oBBZc4yqdvLlMyaPG/NS4P77qsefA fv1Yt3nBZsYa3B9MKf2JlbAEL9QMjtcJtG3RdEttgeA0uD+moLDZGAfPI5OQIZwsgCav HjZw== X-Gm-Message-State: AOJu0Yxh7QhTHRWgE8VrgYV1Oy0b5q00dLq3ZTaTYFKak3/UpTA36sn/ vgwN7di33O4EBznSuNrdiShqVxmEidVx4Vg1NZBcGusXMrEWCUGATnMqFQ== X-Google-Smtp-Source: AGHT+IH+Nu4qI3Reb2jnj9UmmiKjNDZZFN9cusSfuEDX8dgyxX1DJSoWgcb+W0pSfbJN1YP1ftJyEQ== X-Received: by 2002:a05:690c:4807:b0:632:a02e:1c22 with SMTP id 00721157ae682-64363e923d9mr28621337b3.34.1719153606977; Sun, 23 Jun 2024 07:40:06 -0700 (PDT) Received: from [172.31.0.109] ([136.36.72.243]) by smtp.gmail.com with ESMTPSA id 00721157ae682-63f154d601csm21079767b3.114.2024.06.23.07.40.05 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 23 Jun 2024 07:40:06 -0700 (PDT) Message-ID: Date: Sun, 23 Jun 2024 08:40:05 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Content-Language: en-US To: "gcc-patches@gcc.gnu.org" From: Jeff Law Subject: [to-be-committed][V2][RISC-V] Handle bit manipulation of SImode values X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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 Nothing actually changed, just attached the wrong patch last time... --- Last patch in this round of bitmanip work... At least I think I'm going to pause here and switch gears to other projects that need attention 🙂 This patch introduces the ability to generate bitmanip instructions for rv64 when operating on SI objects when we know something about the range of the bit position (due to masking of the position). I've got note that the (7-pos % 8) bit position form was discovered by RAU in 500.perl. I took that and expanded it to the simple (pos & mask) form as well as covering bset, binv and bclr. As far as the implementation is concerned.... This turns the recently added define_splits into define_insn_and_split constructs. This allows combine to "see" enough RTL to realize a sign extension is unnecessary. Otherwise we get undesirable sign extensions for the new testcases. Second it adds new patterns for the logical operations. Two patterns for IOR/XOR and two patterns for AND. I think a key concept to keep in mind is that once we determine a Zbs operation is safe to perform on a SI value, we can rewrite the RTL in 64bit form. If we were ever to try and use range information at expand time for this stuff (and we probably should investigate that), that's the path I'd suggest. This is notably cleaner than my original implementation which actually kept the more complex RTL form through final and emitted 2/3 instructions (mask the bit position, then the bset/bclr/binv). Tested in my tester, but waiting for pre-commit CI to report back before taking further action. Jeff gcc/ * config/riscv/bitmap.md (bset splitters): Turn into define_and_splits. Don't depend on combine splitting the "andn with constant" form. (bset, binv, bclr with masked bit position): New patterns. gcc/testsuite * gcc.target/riscv/binv-for-simode.c: New test. * gcc.target/riscv/bset-for-simode.c: New test. * gcc.target/riscv/bclr-for-simode.c: New test. diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 3eedabffca0..f403ba8dbba 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -615,37 +615,140 @@ (define_insn "*bsetdi_2" ;; shift constant. With the limited range we know the SImode sign ;; bit is never set, thus we can treat this as zero extending and ;; generate the bsetdi_2 pattern. -(define_split - [(set (match_operand:DI 0 "register_operand") +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") (any_extend:DI (ashift:SI (const_int 1) (subreg:QI - (and:DI (not:DI (match_operand:DI 1 "register_operand")) + (and:DI (not:DI (match_operand:DI 1 "register_operand" "r")) (match_operand 2 "const_int_operand")) 0)))) - (clobber (match_operand:DI 3 "register_operand"))] + (clobber (match_scratch:X 3 "=&r"))] "TARGET_64BIT && TARGET_ZBS && (TARGET_ZBB || TARGET_ZBKB) && (INTVAL (operands[2]) & 0x1f) != 0x1f" - [(set (match_dup 0) (and:DI (not:DI (match_dup 1)) (match_dup 2))) - (set (match_dup 0) (zero_extend:DI (ashift:SI - (const_int 1) - (subreg:QI (match_dup 0) 0))))]) + "#" + "&& reload_completed" + [(set (match_dup 3) (match_dup 2)) + (set (match_dup 3) (and:DI (not:DI (match_dup 1)) (match_dup 3))) + (set (match_dup 0) (zero_extend:DI + (ashift:SI (const_int 1) (match_dup 4))))] + { operands[4] = gen_lowpart (QImode, operands[3]); } + [(set_attr "type" "bitmanip")]) -(define_split - [(set (match_operand:DI 0 "register_operand") - (any_extend:DI +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") + (any_extend:DI (ashift:SI (const_int 1) (subreg:QI - (and:DI (match_operand:DI 1 "register_operand") + (and:DI (match_operand:DI 1 "register_operand" "r") (match_operand 2 "const_int_operand")) 0))))] "TARGET_64BIT && TARGET_ZBS && (INTVAL (operands[2]) & 0x1f) != 0x1f" - [(set (match_dup 0) (and:DI (match_dup 1) (match_dup 2))) - (set (match_dup 0) (zero_extend:DI (ashift:SI - (const_int 1) - (subreg:QI (match_dup 0) 0))))]) + "#" + "&& 1" + [(set (match_dup 0) (and:DI (match_dup 1) (match_dup 2))) + (set (match_dup 0) (zero_extend:DI (ashift:SI + (const_int 1) + (subreg:QI (match_dup 0) 0))))] + { } + [(set_attr "type" "bitmanip")]) + +;; Similarly two patterns for IOR/XOR generating bset/binv to +;; manipulate a bit in a register +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") + (any_or:DI + (any_extend:DI + (ashift:SI + (const_int 1) + (subreg:QI + (and:DI (not:DI (match_operand:DI 1 "register_operand" "r")) + (match_operand 2 "const_int_operand")) 0))) + (match_operand:DI 3 "register_operand" "r"))) + (clobber (match_scratch:X 4 "=&r"))] + "TARGET_64BIT + && TARGET_ZBS + && (TARGET_ZBB || TARGET_ZBKB) + && (INTVAL (operands[2]) & 0x1f) != 0x1f" + "#" + "&& reload_completed" + [(set (match_dup 4) (match_dup 2)) + (set (match_dup 4) (and:DI (not:DI (match_dup 4)) (match_dup 1))) + (set (match_dup 0) (any_or:DI (ashift:DI (const_int 1) (match_dup 5)) (match_dup 3)))] + { operands[5] = gen_lowpart (QImode, operands[4]); } + [(set_attr "type" "bitmanip")]) + +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") + (any_or:DI + (any_extend:DI + (ashift:SI + (const_int 1) + (subreg:QI + (and:DI (match_operand:DI 1 "register_operand" "r") + (match_operand 2 "const_int_operand")) 0))) + (match_operand:DI 3 "register_operand" "r"))) + (clobber (match_scratch:X 4 "=&r"))] + "TARGET_64BIT + && TARGET_ZBS + && (INTVAL (operands[2]) & 0x1f) != 0x1f" + "#" + "&& reload_completed" + [(set (match_dup 4) (and:DI (match_dup 1) (match_dup 2))) + (set (match_dup 0) (any_or:DI (ashift:DI (const_int 1) (subreg:QI (match_dup 4) 0)) (match_dup 3)))] + { } + [(set_attr "type" "bitmanip")]) + +;; Similarly two patterns for AND generating bclr to +;; manipulate a bit in a register +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI + (not:DI + (any_extend:DI + (ashift:SI + (const_int 1) + (subreg:QI + (and:DI (not:DI (match_operand:DI 1 "register_operand" "r")) + (match_operand 2 "const_int_operand")) 0)))) + (match_operand:DI 3 "register_operand" "r"))) + (clobber (match_scratch:X 4 "=&r"))] + "TARGET_64BIT + && TARGET_ZBS + && (TARGET_ZBB || TARGET_ZBKB) + && (INTVAL (operands[2]) & 0x1f) != 0x1f" + "#" + "&& reload_completed" + [(set (match_dup 4) (match_dup 2)) + (set (match_dup 4) (and:DI (not:DI (match_dup 1)) (match_dup 4))) + (set (match_dup 0) (and:DI (rotate:DI (const_int -2) (match_dup 5)) (match_dup 3)))] + { operands[5] = gen_lowpart (QImode, operands[4]); } + [(set_attr "type" "bitmanip")]) + + +(define_insn_and_split "" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI + (not:DI + (any_extend:DI + (ashift:SI + (const_int 1) + (subreg:QI + (and:DI (match_operand:DI 1 "register_operand" "r") + (match_operand 2 "const_int_operand")) 0)))) + (match_operand:DI 3 "register_operand" "r"))) + (clobber (match_scratch:X 4 "=&r"))] + "TARGET_64BIT + && TARGET_ZBS + && (INTVAL (operands[2]) & 0x1f) != 0x1f" + "#" + "&& reload_completed" + [(set (match_dup 4) (and:DI (match_dup 1) (match_dup 2))) + (set (match_dup 0) (and:DI (rotate:DI (const_int -2) (match_dup 5)) (match_dup 3)))] + { operands[5] = gen_lowpart (QImode, operands[4]); } + [(set_attr "type" "bitmanip")]) (define_insn "*bset_1_mask" [(set (match_operand:X 0 "register_operand" "=r") diff --git a/gcc/testsuite/gcc.target/riscv/bclr-for-simode-1.c b/gcc/testsuite/gcc.target/riscv/bclr-for-simode-1.c new file mode 100644 index 00000000000..ae9fc33bb34 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/bclr-for-simode-1.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb_zbs -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ + + +typedef unsigned int uint32_t; +uint32_t foo(uint32_t pos, uint32_t x) +{ + return x & ~(1 <<( pos & 0xf)); +} + +typedef unsigned int uint32_t; +uint32_t foo2(uint32_t pos, uint32_t x) +{ + return x & ~(1 <<(7-(pos) % 8)); +} + + + +/* { dg-final { scan-assembler-not "sll\t" } } */ +/* { dg-final { scan-assembler-times "bclr\t" 2 } } */ +/* { dg-final { scan-assembler-times "andi\t" 1 } } */ +/* { dg-final { scan-assembler-times "andn\t" 1 } } */ +/* { dg-final { scan-assembler-times "ret" 2 } } */ + diff --git a/gcc/testsuite/gcc.target/riscv/binv-for-simode-1.c b/gcc/testsuite/gcc.target/riscv/binv-for-simode-1.c new file mode 100644 index 00000000000..f96c2458ef1 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/binv-for-simode-1.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb_zbs -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ + + +typedef unsigned int uint32_t; +uint32_t foo(uint32_t pos, uint32_t x) +{ + return x ^ (1 <<( pos & 0xf)); +} + +typedef unsigned int uint32_t; +uint32_t foo2(uint32_t pos, uint32_t x) +{ + return x ^ (1 <<(7-(pos) % 8)); + +} + +/* { dg-final { scan-assembler-not "sll\t" } } */ +/* { dg-final { scan-assembler-times "binv\t" 2 } } */ +/* { dg-final { scan-assembler-times "andi\t" 1 } } */ +/* { dg-final { scan-assembler-times "andn\t" 1 } } */ +/* { dg-final { scan-assembler-times "ret" 2 } } */ + diff --git a/gcc/testsuite/gcc.target/riscv/bset-for-simode-1.c b/gcc/testsuite/gcc.target/riscv/bset-for-simode-1.c new file mode 100644 index 00000000000..24663a1c856 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/bset-for-simode-1.c @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb_zbs -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ + + +typedef unsigned int uint32_t; +uint32_t foo(uint32_t pos, uint32_t x) +{ + return x | (1 <<( pos & 0xf)); +} + +typedef unsigned int uint32_t; +uint32_t foo2(uint32_t pos, uint32_t x) +{ + return x | (1 <<(7-(pos) % 8)); + +} + +/* { dg-final { scan-assembler-not "sll\t" } } */ +/* { dg-final { scan-assembler-times "bset\t" 2 } } */ +/* { dg-final { scan-assembler-times "andi\t" 1 } } */ +/* { dg-final { scan-assembler-times "andn\t" 1 } } */ +/* { dg-final { scan-assembler-times "ret" 2 } } */ +