From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id AF2E03858C52 for ; Fri, 2 Dec 2022 08:23:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AF2E03858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12d.google.com with SMTP id g7so6315453lfv.5 for ; Fri, 02 Dec 2022 00:23:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pdl7kRZiCNdoEzdXKcVG1VQuVMdppSHNfnftDue3VEQ=; b=qM2P/wK10HLR+wdGsEFh1HZUklEkr+X2XMcYlX4pxGRcHkJO3tDJcWDwrqC4yZniNn 8/fr7+8dRgq9QlsO7e5Go/bhg1KgnU1vRARFG7I6OoY6UO0O5kG+3e+a/3jRvbN4tvgy yhTEvkt4pd77uofvJw1CSyDRnxGEjIfuWS8U9o8XvIJrIZ2xSM2N0zEXITxImqjwJ+MO dYqa+ZSlI272Hk++tQHFl19as82oto55pIbRhLS5AeZp9cM3RGCu6lnBbGz/m2RDnKL/ 0Ow1Rh8OEiHtm/vl6jF2W6UUUMv4p9K0itV14QoNoNQBX5MZ/rJuf/EkwfjIRcz6939y 3a+w== 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=pdl7kRZiCNdoEzdXKcVG1VQuVMdppSHNfnftDue3VEQ=; b=Qz+vZa6mLvrVQQRveKub0/81QW/SlNeFBNHGG//RbfAJoeLNqWYKRed2lspepDBFWx oqlwBtS8AmyhB8yJswLQeMkEOC7JfEucSGpurb6Cyxj0kGiOMUk7hHGI+JmiBs2dzF5b u+2a0/8vMW05sOPvJl2cxnwB9JIymFmmi5E1Wi+saAkNHcSEdEhErzoNiL39gh7uJTDr /gh3AIcxAuKUh0ElUw4coH2tup5gFvRMHAqFAMA+Yoi7qDo+0MgdFKIPa6DIrk6gh1LA 6X4c5nE2PYuqK2J9rhB+DfNhxUexF1AAZHhymfZ9gxjQMuIJrBWeAov5DJe+zfeoD3/X geXw== X-Gm-Message-State: ANoB5pmBg3pm6HjClZ04Xk96Kuqnqv2Ewa/uidVVEYXJj4y3zO4yAJJN 948wfq7ey4beRHfh76zIFr6uiDTdFlzJZfxtSHU= X-Google-Smtp-Source: AA0mqf51Z0p/FylSW+WWlAJK4QMP7IEY3V/6Az3ATM7FOtkL7pvf7Q/hj2mDzNqWlsMD7C49F7y6j3rRKUYzzbcj9hg= X-Received: by 2002:a05:6512:39d4:b0:4b3:b6db:8cb5 with SMTP id k20-20020a05651239d400b004b3b6db8cb5mr21321305lfu.599.1669969379856; Fri, 02 Dec 2022 00:22:59 -0800 (PST) MIME-Version: 1.0 References: <17f48447-7765-a47a-d1c4-0f4f2a3907bb@rivosinc.com> In-Reply-To: <17f48447-7765-a47a-d1c4-0f4f2a3907bb@rivosinc.com> From: Richard Biener Date: Fri, 2 Dec 2022 09:22:47 +0100 Message-ID: Subject: Re: [PATCH v2] match.pd: rewrite select to branchless expression To: Michael Collison Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,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, Dec 1, 2022 at 7:57 PM Michael Collison wrote: > > Richard, > > Can you submit this patch for me while I sort out git write access? Done. I had to apply the patch manually - in future please make sure to send patches that can be applied with git am. Thanks, Richard. > On 11/18/22 07:57, Richard Biener wrote: > > On Fri, Nov 11, 2022 at 3:28 AM Michael Collison wrote: > >> This patches transforms ((x & 0x1) == 0) ? y : z y -into > >> (-(typeof(y))(x & 0x1) & z) y, where op is a '^' or a '|'. It also > >> transforms (cond (and (x , 0x1) != 0), (z op y), y ) into (-(and (x , > >> 0x1)) & z ) op y. > >> > >> Matching this patterns allows GCC to generate branchless code for one of > >> the functions in coremark. > >> > >> Bootstrapped and tested on x86 and RISC-V. Okay? > > OK. > > > > Thanks, > > Richard. > > > >> Michael. > >> > >> 2022-11-10 Michael Collison > >> > >> * match.pd ((x & 0x1) == 0) ? y : z y > >> -> (-(typeof(y))(x & 0x1) & z) y. > >> > >> 2022-11-10 Michael Collison > >> > >> * gcc.dg/tree-ssa/branchless-cond.c: New test. > >> > >> --- > >> > >> Changes in v2: > >> > >> - Rewrite comment to use C syntax > >> > >> - Guard against 1-bit types > >> > >> - Simplify pattern by using zero_one_valued_p > >> > >> gcc/match.pd | 24 +++++++++++++++++ > >> .../gcc.dg/tree-ssa/branchless-cond.c | 26 +++++++++++++++++++ > >> 2 files changed, 50 insertions(+) > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c > >> > >> diff --git a/gcc/match.pd b/gcc/match.pd > >> index 194ba8f5188..258531e9046 100644 > >> --- a/gcc/match.pd > >> +++ b/gcc/match.pd > >> @@ -3486,6 +3486,30 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > >> (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) > >> (max @2 @1)) > >> > >> +/* ((x & 0x1) == 0) ? y : z y -> (-(typeof(y))(x & 0x1) & z) y */ > >> +(for op (bit_xor bit_ior) > >> + (simplify > >> + (cond (eq zero_one_valued_p@0 > >> + integer_zerop) > >> + @1 > >> + (op:c @2 @1)) > >> + (if (INTEGRAL_TYPE_P (type) > >> + && TYPE_PRECISION (type) > 1 > >> + && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) > >> + (op (bit_and (negate (convert:type @0)) @2) @1)))) > >> + > >> +/* ((x & 0x1) == 0) ? z y : y -> (-(typeof(y))(x & 0x1) & z) y */ > >> +(for op (bit_xor bit_ior) > >> + (simplify > >> + (cond (ne zero_one_valued_p@0 > >> + integer_zerop) > >> + (op:c @2 @1) > >> + @1) > >> + (if (INTEGRAL_TYPE_P (type) > >> + && TYPE_PRECISION (type) > 1 > >> + && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) > >> + (op (bit_and (negate (convert:type @0)) @2) @1)))) > >> + > >> /* Simplifications of shift and rotates. */ > >> > >> (for rotate (lrotate rrotate) > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c b/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c > >> new file mode 100644 > >> index 00000000000..68087ae6568 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c > >> @@ -0,0 +1,26 @@ > >> +/* { dg-do compile } */ > >> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >> + > >> +int f1(unsigned int x, unsigned int y, unsigned int z) > >> +{ > >> + return ((x & 1) == 0) ? y : z ^ y; > >> +} > >> + > >> +int f2(unsigned int x, unsigned int y, unsigned int z) > >> +{ > >> + return ((x & 1) != 0) ? z ^ y : y; > >> +} > >> + > >> +int f3(unsigned int x, unsigned int y, unsigned int z) > >> +{ > >> + return ((x & 1) == 0) ? y : z | y; > >> +} > >> + > >> +int f4(unsigned int x, unsigned int y, unsigned int z) > >> +{ > >> + return ((x & 1) != 0) ? z | y : y; > >> +} > >> + > >> +/* { dg-final { scan-tree-dump-times " -" 4 "optimized" } } */ > >> +/* { dg-final { scan-tree-dump-times " & " 8 "optimized" } } */ > >> +/* { dg-final { scan-tree-dump-not "if" "optimized" } } */ > >> -- > >> 2.34.1 > >>