From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id 60E2F385842A for ; Tue, 8 Nov 2022 20:16:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 60E2F385842A 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-pg1-x52a.google.com with SMTP id h193so14316990pgc.10 for ; Tue, 08 Nov 2022 12:16:03 -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=K18o7jR7/xisxaOX3cFEhQbd4S/BotZ/J5De5l+czPw=; b=RJxW7xKhiYOKR00WgQNWlmuIj1hLm9xytpHmm5GCxlZLmqVEIGAs7PAKgNhjhgWyJt vhUpeEfDm451TmPF5Nix0+GW9Gh2OmU01dUMWmrxdYpryFbvmq8bBtdnP3FyviJ9CJ0Q wEiqoc5BUkYUUvDUGFh6Z0cqmfb5YooNmDug811sZpGiWaZGW/A87RWuyTlKE++mr2LH 8PB+gb9kp8a7J3TfjJ5Pg/4VwOER6+togKV+nZGJBpumduioJu+uXGfubib6W1CtjGgN T5NSGpVVpwspvT6dqqkP5b1gnDd0WWd7cDyUde7ox+NIMGTJZGwrmFGihNbTzi3KbmHN 58JQ== 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=K18o7jR7/xisxaOX3cFEhQbd4S/BotZ/J5De5l+czPw=; b=jWrRbWMPHmZ0RMkvB+fu7eOQ2UI38XtQPoWqtrPL2eN8RIfiq+1I+yyqIUUWg40f23 IrN/ytcqWLWv7Tm1wbHzAzTPepu866pZZJgK7fkrVBRXuAvBE6xXkZI686qHZVJywEoy +MpQMGiiZ0aW/nWdczyK+Kvf7b7mgnuSzyF5YQ3S+k33OoSK8zqdZ04WtpzfSIO1d+M0 x/uuyOqXSePrB8dItnvEklNnNQopjUIzx4zdFzS53wi883Bc6XbxagipUrGOb/WUk/JR +vI4KJ0B30AtQFzFr+S0TXpYjJ7OUUDT6iB6v3cVknKM5xsmH3T7H/KPcIWSwhdkRCZf uJZQ== X-Gm-Message-State: ACrzQf36vPVecqLEQDwaZVswn4WBb00GAgLyOA5ClNh/tFciVMn7Vz+s Fvc+skt7Oh0Kef155amQ/cjCvupJd/OhdwcRfwQ= X-Google-Smtp-Source: AMsMyM4b2w+kC2RTSoQQd0bH3sXFUSF+3xz8br3j7ke9cGo3JygoVWSuwCL64Mzl8DZxU5LC1lwoQW8LQ8QDAcQcAXg= X-Received: by 2002:a63:66c3:0:b0:470:8fd:7bae with SMTP id a186-20020a6366c3000000b0047008fd7baemr31107311pgc.277.1667938562311; Tue, 08 Nov 2022 12:16:02 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrew Pinski Date: Tue, 8 Nov 2022 12:15:49 -0800 Message-ID: Subject: Re: [PATCH] match.pd: rewrite select to branchless expression To: Michael Collison Cc: gcc-patches@gcc.gnu.org, Jeff Law , "jakub@redhat.com >> Jakub Jelinek" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.8 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 List-Id: On Tue, Nov 8, 2022 at 12:02 PM Michael Collison wrote: > > This patches transforms (cond (and (x , 0x1) == 0), y, (z op y)) into > (-(and (x , 0x1)) & z ) op 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? This seems like a (much) reduced (simplified?) version of https://gcc.gnu.org/pipermail/gcc-patches/2021-November/584411.html . I have not had time for the last year to go through the comments on that patch and resubmit it though. It seems like you are aiming for one specific case in coremarks rather than a more generic fix too. Thanks, Andrew Pinski > > Michael. > > 2022-11-08 Michael Collison > > * match.pd ((cond (and (x , 0x1) == 0), y, (z op y) ) > -> (-(and (x , 0x1)) & z ) op y) > > 2022-11-08 Michael Collison > > * gcc.dg/tree-ssa/branchless-cond.c: New test. > > --- > gcc/match.pd | 22 ++++++++++++++++ > .../gcc.dg/tree-ssa/branchless-cond.c | 26 +++++++++++++++++++ > 2 files changed, 48 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..722f517ac6d 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -3486,6 +3486,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) > (max @2 @1)) > > +/* (cond (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) > ^ y */ > +(for op (bit_xor bit_ior) > + (simplify > + (cond (eq (bit_and @0 integer_onep@1) > + integer_zerop) > + @2 > + (op:c @3 @2)) > + (if (INTEGRAL_TYPE_P (type) > + && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) > + (op (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2)))) > + > +/* (cond (and (x , 0x1) != 0), (z ^ y), y ) -> (-(and (x , 0x1)) & z ) > ^ y */ > +(for op (bit_xor bit_ior) > + (simplify > + (cond (ne (bit_and @0 integer_onep@1) > + integer_zerop) > + (op:c @3 @2) > + @2) > + (if (INTEGRAL_TYPE_P (type) > + && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) > + (op (bit_and (negate (convert:type (bit_and @0 @1))) @3) @2)))) > + > /* 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 > > > >