The following adds a pattern to optimize MAX (a, -a) to abs (a).

Bootstrap / regtest pending on x86_64-unknown-linux-gnu.

Richard.

2016-09-28  Richard Biener  <rguent...@suse.de>

        PR middle-end/55152
        * match.pd: Add max(a,-a) -> abs(a) pattern.

        * gcc.dg/pr55152.c: New testcase.

Index: gcc/match.pd
===================================================================
--- gcc/match.pd        (revision 240565)
+++ gcc/match.pd        (working copy)
@@ -1271,6 +1284,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (simplify
  (max:c (min:c @0 @1) @1)
  @1)
+/* max(a,-a) -> abs(a).  */
+(simplify
+ (max:c @0 (negate @0))
+ (if (TREE_CODE (type) != COMPLEX_TYPE
+      && (! ANY_INTEGRAL_TYPE_P (type)
+         || TYPE_OVERFLOW_UNDEFINED (type)))
+  (abs @0)))
 (simplify
  (min @0 @1)
  (switch
Index: gcc/testsuite/gcc.dg/pr55152.c
===================================================================
--- gcc/testsuite/gcc.dg/pr55152.c      (revision 0)
+++ gcc/testsuite/gcc.dg/pr55152.c      (working copy)
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O -ffinite-math-only -fstrict-overflow 
-fdump-tree-optimized" } */
+
+double g (double a)
+{
+  return (a>=-a)?a:-a;
+}
+int f(int a)
+{
+  return (a>=-a)?a:-a;
+}
+
+/* { dg-final { scan-tree-dump-times "ABS_EXPR" 2 "optimized" } } */

Reply via email to