This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] [Match & Simplify] Optimize some minmax patterns
- From: "Hurugalawadi, Naveen" <Naveen dot Hurugalawadi at cavium dot com>
- To: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Cc: "Pinski, Andrew" <Andrew dot Pinski at cavium dot com>, Richard Biener <richard dot guenther at gmail dot com>, "marc dot glisse at inria dot fr" <marc dot glisse at inria dot fr>
- Date: Thu, 15 Dec 2016 11:01:45 +0000
- Subject: [PATCH] [Match & Simplify] Optimize some minmax patterns
- Authentication-results: sourceware.org; auth=none
- Authentication-results: spf=none (sender IP is ) smtp.mailfrom=Naveen dot Hurugalawadi at cavium dot com;
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
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 <apinski@cavium.com>
Naveen H.S <Naveen.Hurugalawadi@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" } } */