diff mbox

[Match,&,Simplify] Optimize some minmax patterns

Message ID CO2PR07MB269402165A70BA41A32350A6839D0@CO2PR07MB2694.namprd07.prod.outlook.com
State New
Headers show

Commit Message

Hurugalawadi, Naveen Dec. 15, 2016, 11:01 a.m. UTC
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.

Comments

Richard Biener Dec. 16, 2016, 1:25 p.m. UTC | #1
On Thu, Dec 15, 2016 at 12:01 PM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@cavium.com> wrote:
> 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?

Please put the patterns after the initial trivial ones, for example
after

(simplify
 (max @0 @1)
 (switch
  (if (INTEGRAL_TYPE_P (type)
       && TYPE_MAX_VALUE (type)
       && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
   @1)
  (if (INTEGRAL_TYPE_P (type)
       && TYPE_MIN_VALUE (type)
       && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
   @0)))

ok with that change.

RIchard.



> 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 mbox

Patch

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" } } */