Hi, Please find attached the patch that optimizes some patterns in maxmin on same variabes with constants.
Bootstrapped and Regression tested on x86_64 & aarch64-thunder-linux. Please review the patch and let us know if its okay? 2016-12-15 Andrew Pinski <apin...@cavium.com> Naveen H.S <naveen.hurugalaw...@cavium.com> gcc * match.pd (max:c @0 (plus@2 @0 INTEGER_CST@1)): New Pattern. (min:c @0 (plus@2 @0 INTEGER_CST@1)) : New Pattern. gcc/testsuite * gcc.dg/max.c: New Testcase. * gcc.dg/min.c: New Testcase.
diff --git a/gcc/match.pd b/gcc/match.pd index f4cc2d8..ff5e97b 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1324,6 +1324,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ +/* max (a, a + CST) -> a + CST where CST is positive. */ +/* max (a, a + CST) -> a where CST is negative. */ +(simplify + (max:c @0 (plus@2 @0 INTEGER_CST@1)) + (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) + (if (tree_int_cst_sgn (@1) > 0) + @2 + @0))) + +/* min (a, a + CST) -> a where CST is positive. */ +/* min (a, a + CST) -> a + CST where CST is negative. */ +(simplify + (min:c @0 (plus@2 @0 INTEGER_CST@1)) + (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) + (if (tree_int_cst_sgn (@1) > 0) + @0 + @2))) + (for minmax (min max FMIN FMAX) (simplify (minmax @0 @0) diff --git a/gcc/testsuite/gcc.dg/max.c b/gcc/testsuite/gcc.dg/max.c new file mode 100644 index 0000000..e979810 --- /dev/null +++ b/gcc/testsuite/gcc.dg/max.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +static inline int +max (int a, int b) +{ + return a < b ? b : a; +} + +int +test_00 (int a) +{ + return max (a, a + 8); +} + +int +test_01 (int a) +{ + return max (a, a - 8); +} + +/* { dg-final { scan-tree-dump-not "MAX_EXPR" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/min.c b/gcc/testsuite/gcc.dg/min.c new file mode 100644 index 0000000..d847270 --- /dev/null +++ b/gcc/testsuite/gcc.dg/min.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +static inline int +min (int a, int b) +{ + return a < b ? a : b; +} + +int +test_00 (int a) +{ + return min (a, a + 8); +} + +int +test_01 (int a) +{ + return min (a, a - 8); +} + +/* { dg-final { scan-tree-dump-not "MIN_EXPR" "optimized" } } */