From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 3AB143858C52; Fri, 2 Dec 2022 08:22:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3AB143858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669969345; bh=n0C4DmxIoGwzsLDUh//rLgrc3HBEh+ncxlX7TSQ4U+I=; h=From:To:Subject:Date:From; b=c0Sg8IPVR6DP8EcewAzURd7SL//ZPRyXyX5+MQMNW+8wY+Fp/sKvRmGOhVy482Dw0 VahkQiRq8Q0JTVLbhHcDj/aNytsGDSgycfLpaW7+YGdj2huR3q9SI/sd4b+MXICFd2 WDY8A54Z2HEKEReP4BHm5gjPB02c2YNQgSYD56PU= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4459] match.pd: rewrite select to branchless expression X-Act-Checkin: gcc X-Git-Author: Michael Collison X-Git-Refname: refs/heads/master X-Git-Oldrev: 5b50850c3c6f2eceb8012dcc8d3cd5ddd94fac6c X-Git-Newrev: 6508d5e5a1a8c08bebf83d258fad525eadc9f5d1 Message-Id: <20221202082225.3AB143858C52@sourceware.org> Date: Fri, 2 Dec 2022 08:22:25 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6508d5e5a1a8c08bebf83d258fad525eadc9f5d1 commit r13-4459-g6508d5e5a1a8c08bebf83d258fad525eadc9f5d1 Author: Michael Collison Date: Fri Dec 2 08:40:05 2022 +0100 match.pd: rewrite select to branchless expression 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. * match.pd ((x & 0x1) == 0) ? y : z y -> (-(typeof(y))(x & 0x1) & z) y. * gcc.dg/tree-ssa/branchless-cond.c: New test. Diff: --- gcc/match.pd | 24 +++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/branchless-cond.c | 26 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index f8610e37011..04d4c4c054a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3492,6 +3492,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" } } */