Skip to content

Commit 95b6ee9

Browse files
committed
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 <[email protected]> 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.
1 parent 1cd8254 commit 95b6ee9

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

gcc/genmatch.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -4071,7 +4071,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
40714071
for (unsigned i = 0;
40724072
i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
40734073
fp_decl (f, " tree ARG_UNUSED (_p%d),", i);
4074-
fp_decl (f, " tree *captures");
4074+
fp_decl (f, " tree *ARG_UNUSED (captures)");
40754075
}
40764076
for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
40774077
{

gcc/match.pd

+11
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
12191219
&& tree_nop_conversion_p (type, TREE_TYPE (@1)))
12201220
(lshift @0 @2)))
12211221

1222+
/* Fold a * !a into 0. */
1223+
(simplify
1224+
(mult:c @0 (convert? (eq @0 integer_zerop)))
1225+
{ build_zero_cst (type); })
1226+
(simplify
1227+
(mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
1228+
{ build_zero_cst (type); })
1229+
(simplify
1230+
(mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
1231+
{ build_zero_cst (type); })
1232+
12221233
/* Shifts by precision or greater result in zero. */
12231234
(for shift (lshift rshift)
12241235
(simplify
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* PR tree-optimization/114009 */
2+
/* { dg-do compile } */
3+
/* { dg-options "-O2 -Wno-psabi -fdump-tree-forwprop1" } */
4+
/* { dg-final { scan-tree-dump-times " return 0;" 3 "forwprop1" } } */
5+
/* { dg-final { scan-tree-dump-times " (?:return|<retval> =) { 0, 0, 0, 0 };" 1 "forwprop1" } } */
6+
7+
int
8+
foo (int x)
9+
{
10+
x = (x / 2) * 2;
11+
return (!x) * x;
12+
}
13+
14+
int
15+
bar (int x, int y)
16+
{
17+
(void) x;
18+
return y * !y;
19+
}
20+
21+
unsigned long long
22+
baz (unsigned long long x)
23+
{
24+
return (!x) * x;
25+
}
26+
27+
typedef int V __attribute__((vector_size (4 * sizeof (int))));
28+
29+
V
30+
qux (V x)
31+
{
32+
return x * (x == 0);
33+
}

0 commit comments

Comments
 (0)