match.pd: Optimize a * !a to 0 [PR114009]
The following patch attempts to fix an optimization regression through adding a simple simplification. We already have the /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ (if (!canonicalize_math_p ()) (for cmp (tcc_comparison) (simplify (mult:c (convert (cmp@0 @1 @2)) @3) (if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (@0))) (cond @0 @3 { build_zero_cst (type); }))) optimization which otherwise triggers during the a * !a multiplication, but that is done only late and we aren't able through range assumptions optimize it yet anyway. The patch adds a specific simplification for it. If a is zero, then a * !a will be 0 * 1 (or for signed 1-bit 0 * -1) and so 0. If a is non-zero, then a * !a will be a * 0 and so again 0. THe pattern is valid for scalar integers, complex integers and vector types, but I think will actually trigger only for the scalar integers. For vector types I've added other two with VEC_COND_EXPR in it, for complex there are different GENERIC trees to match and it is something that likely would be never matched in GIMPLE, so I didn't handle that. 2024-03-07 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/114009 * genmatch.cc (decision_tree::gen): Emit ARG_UNUSED for captures argument even for GENERIC, not just for GIMPLE. * match.pd (a * !a -> 0): New simplifications. * gcc.dg/tree-ssa/pr114009.c: New test.
This commit is contained in:
parent
1cd8254eba
commit
95b6ee9634
3 changed files with 45 additions and 1 deletions
|
@ -4071,7 +4071,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
|
|||
for (unsigned i = 0;
|
||||
i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
|
||||
fp_decl (f, " tree ARG_UNUSED (_p%d),", i);
|
||||
fp_decl (f, " tree *captures");
|
||||
fp_decl (f, " tree *ARG_UNUSED (captures)");
|
||||
}
|
||||
for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
|
||||
{
|
||||
|
|
11
gcc/match.pd
11
gcc/match.pd
|
@ -1219,6 +1219,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
|
|||
&& tree_nop_conversion_p (type, TREE_TYPE (@1)))
|
||||
(lshift @0 @2)))
|
||||
|
||||
/* Fold a * !a into 0. */
|
||||
(simplify
|
||||
(mult:c @0 (convert? (eq @0 integer_zerop)))
|
||||
{ build_zero_cst (type); })
|
||||
(simplify
|
||||
(mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
|
||||
{ build_zero_cst (type); })
|
||||
(simplify
|
||||
(mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
|
||||
{ build_zero_cst (type); })
|
||||
|
||||
/* Shifts by precision or greater result in zero. */
|
||||
(for shift (lshift rshift)
|
||||
(simplify
|
||||
|
|
33
gcc/testsuite/gcc.dg/tree-ssa/pr114009.c
Normal file
33
gcc/testsuite/gcc.dg/tree-ssa/pr114009.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/* PR tree-optimization/114009 */
|
||||
/* { dg-do compile } */
|
||||
/* { dg-options "-O2 -Wno-psabi -fdump-tree-forwprop1" } */
|
||||
/* { dg-final { scan-tree-dump-times " return 0;" 3 "forwprop1" } } */
|
||||
/* { dg-final { scan-tree-dump-times " (?:return|<retval> =) { 0, 0, 0, 0 };" 1 "forwprop1" } } */
|
||||
|
||||
int
|
||||
foo (int x)
|
||||
{
|
||||
x = (x / 2) * 2;
|
||||
return (!x) * x;
|
||||
}
|
||||
|
||||
int
|
||||
bar (int x, int y)
|
||||
{
|
||||
(void) x;
|
||||
return y * !y;
|
||||
}
|
||||
|
||||
unsigned long long
|
||||
baz (unsigned long long x)
|
||||
{
|
||||
return (!x) * x;
|
||||
}
|
||||
|
||||
typedef int V __attribute__((vector_size (4 * sizeof (int))));
|
||||
|
||||
V
|
||||
qux (V x)
|
||||
{
|
||||
return x * (x == 0);
|
||||
}
|
Loading…
Add table
Reference in a new issue