From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by sourceware.org (Postfix) with ESMTPS id 99BF13853D49 for ; Thu, 17 Nov 2022 18:56:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 99BF13853D49 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-qt1-x82e.google.com with SMTP id fz10so1711097qtb.3 for ; Thu, 17 Nov 2022 10:56:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=+3bj2h0Lf0c2fveSzS47L+TSZPl7nmipzGoVWW04F5A=; b=RZMPfh2fwWOqYCLTet3EAH6AdDMlIkS930k1s/7j9jwx/gkdETYkMLzdU/Z9wlxqPu BtXGd5I8FWqtnCy2aJs+RitBEay0j7x6R8tFHXPWE1hugbztF6qL84fqPLNGodjGc3ja xsIR5djvi2JhnOvB1AoHEs2t06nF8RQXlzUcrDam0TJjSlOQuTpw+9DIvKM+Q5Y/2Rc2 3z2AKAOTi6Pcb+P6sqQPnBCrvviPaZ40MpCBS5eNUy7LAAnpIjSaCI9WZcaddYWoYeaU Twse9SEsWxENHhk+STpZd0TRkwpd/FznU3VwK8SMgDFbRQj+/XiPBI2VlRUpS7/XvECD hp5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+3bj2h0Lf0c2fveSzS47L+TSZPl7nmipzGoVWW04F5A=; b=lN7roOuA1jE8x2ZN9tIsQ7exXw1vWtHr0Llvprz72pDXh0xEWhDVS4wMkjYhGR+T6d ZWjmy4xW7+gECXY6qbILDEeRkJRDgFhS/n/izYTs45XfGJoP3Sag3tADAXwXXnH7SqlQ eSaHDbWaY4X5JZhn3se/sfH8LUC+o+w0uZe3A7xF0GsGUZoZlCY1PZ+7pPBugQWsC2aE AFQT6LGUCgIW4sAD8wAUJ/zbuVd5ZkV2ORA3iuMFV90xjdyVPBDcy7YxGpta7UuRP+M2 mK+T9RV8JvmzcIFto6nO/yl7YlW734zl+SmZP+ZnIC/pEb1XPa/Rzjo1m8kZbMXcATBp CYMA== X-Gm-Message-State: ANoB5pkv05fnJeVQ+uyht4c0AGQ8GLxmf0ivRDHGf9pH00u6MMTXvg/k Y8F+Pq6m0pBbwpjiF6QC1qRGLQg9tlYj/U3Y0Ce5qVfHsQMO1UyRA/EjhZBcB2okLzRChwoEon1 tCgSwzS+LHWZtyy9aPgggqeJxFH3kUfNZJADtfi6518K/XFaL89mjj6/t5jaI+kfDspw7 X-Google-Smtp-Source: AA0mqf7F0yh/ghxDFDr3B5bIvGo1rnjZvh2Ppd8WcuM6D5eayDqKa0yqTkJ/HBXit+b96JywhZdIRQ== X-Received: by 2002:ac8:488f:0:b0:3a6:fa9:bcca with SMTP id i15-20020ac8488f000000b003a60fa9bccamr3544205qtq.652.1668711373649; Thu, 17 Nov 2022 10:56:13 -0800 (PST) Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com. [209.85.219.178]) by smtp.gmail.com with ESMTPSA id cf11-20020a05622a400b00b0039ee562799csm767315qtb.59.2022.11.17.10.56.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Nov 2022 10:56:13 -0800 (PST) Received: by mail-yb1-f178.google.com with SMTP id k84so2986045ybk.3 for ; Thu, 17 Nov 2022 10:56:13 -0800 (PST) X-Received: by 2002:a25:dcc:0:b0:6d3:4f69:dc56 with SMTP id 195-20020a250dcc000000b006d34f69dc56mr3351477ybn.251.1668711372805; Thu, 17 Nov 2022 10:56:12 -0800 (PST) MIME-Version: 1.0 References: <20221113204858.4062163-1-philipp.tomsich@vrull.eu> In-Reply-To: From: Andrew Waterman Date: Thu, 17 Nov 2022 10:56:01 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] RISC-V: Handle "(a & twobits) == singlebit" in branches using Zbs To: Philipp Tomsich Cc: gcc-patches@gcc.gnu.org, Christoph Muellner , Kito Cheng , Vineet Gupta , Jeff Law , Palmer Dabbelt Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-10.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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, Nov 17, 2022 at 10:52 AM Philipp Tomsich wrote: > > On Thu, 17 Nov 2022 at 19:33, Andrew Waterman wrote: > > > > Am I wrong to worry that this will increase dynamic instruction count > > when used in a loop? The obvious code is more efficient when the > > constant loads can be hoisted out of a loop. Or does the cost model > > account for this somehow? > > With this change merged, GCC still hoists the constants out of the > loop (just checked with a quick test case). > So the cost model seems correct (whether intentionally or accidentally). Cool, thanks for checking. > > Thanks, > Philipp. > > > > > > > On Sun, Nov 13, 2022 at 12:50 PM Philipp Tomsich > > wrote: > > > > > > Use Zbs when generating a sequence for "if ((a & twobits) == singlebit) ..." > > > that can be expressed as bexti + bexti + andn. > > > > > > gcc/ChangeLog: > > > > > > * config/riscv/bitmanip.md (*branch_mask_twobits_equals_singlebit): > > > Handle "if ((a & T) == C)" using Zbs, when T has 2 bits set and C has one > > > of these tow bits set. > > > * config/riscv/predicates.md (const_twobits_operand): New predicate. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * gcc.target/riscv/zbs-if_then_else-01.c: New test. > > > > > > Signed-off-by: Philipp Tomsich > > > --- > > > > > > gcc/config/riscv/bitmanip.md | 42 +++++++++++++++++++ > > > gcc/config/riscv/predicates.md | 5 +++ > > > .../gcc.target/riscv/zbs-if_then_else-01.c | 20 +++++++++ > > > 3 files changed, 67 insertions(+) > > > create mode 100644 gcc/testsuite/gcc.target/riscv/zbs-if_then_else-01.c > > > > > > diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md > > > index 7a8f4e35880..2cea394671f 100644 > > > --- a/gcc/config/riscv/bitmanip.md > > > +++ b/gcc/config/riscv/bitmanip.md > > > @@ -690,3 +690,45 @@ > > > "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)))]) > > > + > > > +;; IF_THEN_ELSE: test for 2 bits of opposite polarity > > > +(define_insn_and_split "*branch_mask_twobits_equals_singlebit" > > > + [(set (pc) > > > + (if_then_else (match_operator 1 "equality_operator" > > > + [(and:X (match_operand:X 2 "register_operand" "r") > > > + (match_operand:X 3 "const_twobits_operand" "i")) > > > + (match_operand:X 4 "single_bit_mask_operand" "i")]) > > > + (label_ref (match_operand 0 "" "")) > > > + (pc))) > > > + (clobber (match_scratch:X 5 "=&r")) > > > + (clobber (match_scratch:X 6 "=&r"))] > > > + "TARGET_ZBS && TARGET_ZBB && !SMALL_OPERAND (INTVAL (operands[3]))" > > > + "#" > > > + "&& reload_completed" > > > + [(set (match_dup 5) (zero_extract:X (match_dup 2) > > > + (const_int 1) > > > + (match_dup 8))) > > > + (set (match_dup 6) (zero_extract:X (match_dup 2) > > > + (const_int 1) > > > + (match_dup 9))) > > > + (set (match_dup 6) (and:X (not:X (match_dup 6)) (match_dup 5))) > > > + (set (pc) (if_then_else (match_op_dup 1 [(match_dup 6) (const_int 0)]) > > > + (label_ref (match_dup 0)) > > > + (pc)))] > > > +{ > > > + unsigned HOST_WIDE_INT twobits_mask = UINTVAL (operands[3]); > > > + unsigned HOST_WIDE_INT singlebit_mask = UINTVAL (operands[4]); > > > + > > > + /* Make sure that the reference value has one of the bits of the mask set */ > > > + if ((twobits_mask & singlebit_mask) == 0) > > > + FAIL; > > > + > > > + int setbit = ctz_hwi (singlebit_mask); > > > + int clearbit = ctz_hwi (twobits_mask & ~singlebit_mask); > > > + > > > + operands[1] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == NE ? EQ : NE, > > > + mode, operands[6], GEN_INT(0)); > > > + > > > + operands[8] = GEN_INT (setbit); > > > + operands[9] = GEN_INT (clearbit); > > > +}) > > > diff --git a/gcc/config/riscv/predicates.md b/gcc/config/riscv/predicates.md > > > index 490bff688a7..6e34829a59b 100644 > > > --- a/gcc/config/riscv/predicates.md > > > +++ b/gcc/config/riscv/predicates.md > > > @@ -321,6 +321,11 @@ > > > (and (match_code "const_int") > > > (match_test "popcount_hwi (~UINTVAL (op)) == 2"))) > > > > > > +;; A CONST_INT operand that has exactly two bits set. > > > +(define_predicate "const_twobits_operand" > > > + (and (match_code "const_int") > > > + (match_test "popcount_hwi (UINTVAL (op)) == 2"))) > > > + > > > ;; A CONST_INT operand that fits into the unsigned half of a > > > ;; signed-immediate after the top bit has been cleared. > > > (define_predicate "uimm_extra_bit_operand" > > > diff --git a/gcc/testsuite/gcc.target/riscv/zbs-if_then_else-01.c b/gcc/testsuite/gcc.target/riscv/zbs-if_then_else-01.c > > > new file mode 100644 > > > index 00000000000..d249a841ff9 > > > --- /dev/null > > > +++ b/gcc/testsuite/gcc.target/riscv/zbs-if_then_else-01.c > > > @@ -0,0 +1,20 @@ > > > +/* { dg-do compile } */ > > > +/* { dg-options "-march=rv64gc_zbb_zbs -mabi=lp64" } */ > > > +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-O1" } } */ > > > + > > > +void g(); > > > + > > > +void f1 (long a) > > > +{ > > > + if ((a & ((1ul << 33) | (1 << 4))) == (1ul << 33)) > > > + g(); > > > +} > > > + > > > +void f2 (long a) > > > +{ > > > + if ((a & 0x12) == 0x10) > > > + g(); > > > +} > > > + > > > +/* { dg-final { scan-assembler-times "bexti\t" 2 } } */ > > > +/* { dg-final { scan-assembler-times "andn\t" 1 } } */ > > > -- > > > 2.34.1 > > >