aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2020-12-31 11:08:15 +0100
committerJakub Jelinek <jakub@redhat.com>2020-12-31 11:08:15 +0100
commit8f12ce2ea3be12de4f83d3c419bdb1dc5036b202 (patch)
treeacdd88b6fe3f832de1cfc8febfc18dbf98a27043
parent9e603837f7ad886df62e02ac0cd395ec17b7d587 (diff)
match.pd: Add (-(X < 0) | 1) * X -> abs (X) etc. simplifications [PR94785]
The following patch adds two simplifications to recognize idioms for ABS_EXPR resp. ABSU_EXPR. 2020-12-31 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/94785 * match.pd ((-(X < 0) | 1) * X -> abs (X)): New simplification. ((-(X < 0) | 1U) * X -> absu (X)): Likewise. * gcc.dg/tree-ssa/pr94785.c: New test.
-rw-r--r--gcc/match.pd16
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/pr94785.c36
2 files changed, 52 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index c7ee57d4bc2..1055292d2e9 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1374,6 +1374,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(absu tree_expr_nonnegative_p@0)
(convert @0))
+/* Simplify (-(X < 0) | 1) * X into abs (X). */
+(simplify
+ (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
+ (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
+ (abs @0)))
+
+/* Similarly (-(X < 0) | 1U) * X into absu (X). */
+(simplify
+ (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
+ integer_onep) (nop_convert @0))
+ (if (INTEGRAL_TYPE_P (type)
+ && TYPE_UNSIGNED (type)
+ && INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (absu @0)))
+
/* A few cases of fold-const.c negate_expr_p predicate. */
(match negate_expr_p
INTEGER_CST
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr94785.c b/gcc/testsuite/gcc.dg/tree-ssa/pr94785.c
new file mode 100644
index 00000000000..923928424c5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr94785.c
@@ -0,0 +1,36 @@
+/* PR tree-optimization/94785 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-times " = ABS_EXPR <v_\[0-9]*\\\(D\\\)>;" 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times " = ABSU_EXPR <v_\[0-9]*\\\(D\\\)>;" 2 "optimized" } } */
+
+int
+f1 (int v)
+{
+ return (1 | -(v < 0)) * v;
+}
+
+unsigned
+f2 (int v)
+{
+ return (1U | -(v < 0)) * v;
+}
+
+int
+f3 (int v)
+{
+ int a = v < 0;
+ int b = -a;
+ int c = 1 | b;
+ return c * v;
+}
+
+unsigned
+f4 (int v)
+{
+ int a = v < 0;
+ int b = -a;
+ unsigned c = b;
+ unsigned d = c | 1;
+ return d * v;
+}