match.pd: rewrite select to branchless expression

This patches transforms ((x & 0x1) == 0) ? y : z <op> y -into
(-(typeof(y))(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.

	* match.pd ((x & 0x1) == 0) ? y : z <op> y
	-> (-(typeof(y))(x & 0x1) & z) <op> y.

	* gcc.dg/tree-ssa/branchless-cond.c: New test.
This commit is contained in:
Michael Collison 2022-12-02 08:40:05 +01:00 committed by Richard Biener
parent 5b50850c3c
commit 6508d5e5a1
2 changed files with 50 additions and 0 deletions

View File

@ -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 <op> y -> (-(typeof(y))(x & 0x1) & z) <op> 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 <op> y : y -> (-(typeof(y))(x & 0x1) & z) <op> 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)

View File

@ -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" } } */