From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by sourceware.org (Postfix) with ESMTPS id 18E313858D39 for ; Fri, 11 Nov 2022 07:45:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 18E313858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-io1-xd31.google.com with SMTP id y6so3071886iof.9 for ; Thu, 10 Nov 2022 23:45:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; 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=WBKuVzQyQXx497j8bMLAysXPhWFMGee7d+rEwd/qfyA=; b=AQzywEw4HP6hrswH8RWrkKT+KbWfdJAjKmE1uuOu6QZYaZyVvtvzr/w38zU5hTdliA Z9RRi7uEDA82FeZoz5kshSFtTVp59GA+I85D9oQdnSLu7+13DWy/xWoXXOoGnY6N5ONs cvUbzxLljewOXaTd8ptrunSim32HjwjeEOXndemHpIHFzbxLMKvpsOWQGQwuMvvPSDtP tizDsADZ7JmX9rrzTj8AOUYfvhdZ3c1FL4DHu4F2cKuUGuzy6bnUovrc7Gn71WY1pFEA 1Db0in7frOAIpvcjHjfdQ3QP7f8cbjUCsaD6yDC92xfDZrOF9+4J+mmE2C95byAVMFlq iatg== 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=WBKuVzQyQXx497j8bMLAysXPhWFMGee7d+rEwd/qfyA=; b=BTYKd6diu3CAVrYPSHuXC15EKWyP48wr96zhM8u0bEBrIFZEE+lus2dhg3PljXDrdZ pMPEbi6+k5YKO70ZyJQMbz1EzW4neeeZLaURZZ2oslWuKMYcqQCyDhNN1gGREXTxd/jZ e83mDDz82anAJxr4ErU8nYDomL1xoMcHY5rAntIuy0+skN6Q8zli8ByPqPn1XO5j4hDL auVfmkEs94mGWah0EmNk8h7lmXNTphciBYrb92/M7OVwfhKOhRXvEc5ct3JHr+fI8FfA vIsP973KeUOeh3dm7IU9xdY60rRvM0PwzpLC87jiGXjtZLtSDhF6u14S2KTnp3AESIZH ItXw== X-Gm-Message-State: ANoB5pnBj+x8JC1YC212+2SGvMhXGtUViWBsyBq6bg8t83sG5syROpgM oCFNSx37jrZvqQEdMEcB69nLt9XS48SR0qTeO3oLhrk+wcA= X-Google-Smtp-Source: AA0mqf7WSe4J4+Umm+Mpm7kybSVUGGPJV/GXy83DVGyZk0M0gGbPTC2XLFNQkEErMuPq5EjmFSUvxkJyY/m3VMo+eJg= X-Received: by 2002:a02:9092:0:b0:375:ae19:858e with SMTP id x18-20020a029092000000b00375ae19858emr196354jaf.289.1668152716255; Thu, 10 Nov 2022 23:45:16 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Prathamesh Kulkarni Date: Fri, 11 Nov 2022 13:14:40 +0530 Message-ID: Subject: Re: [PATCH v2] match.pd: rewrite select to branchless expression To: Michael Collison Cc: gcc-patches@gcc.gnu.org, Richard Biener Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Fri, 11 Nov 2022 at 07:58, 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? > > 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; > +} Sorry to nitpick -- Since the pattern gates on INTEGRAL_TYPE_P, would it be a good idea to have these tests for other integral types too besides int like {char, short, long} ? Thanks, Prathamesh > + > +/* { 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 >