diff mbox series

[1/3] Simplify (A + CST cmp A -> CST cmp zero) for undefined overflow type

Message ID DB5PR0801MB2742FB8E536D63ACD8075576E7420@DB5PR0801MB2742.eurprd08.prod.outlook.com
State New
Headers show
Series [1/3] Simplify (A + CST cmp A -> CST cmp zero) for undefined overflow type | expand

Commit Message

Bin Cheng Oct. 19, 2017, 1:26 p.m. UTC
Hi,
This is a rework of patch set at https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01036.html
and https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01037.html.  The patch set improves niters
bound analysis for split loop.  Instead of feeding bound computation to generic folder, this
patch simplifies (A + CST cmp A  ->  CST cmp zero) for types with undefined overflow behavior.
Bootstrap and test for patch set on x86_64 and AArch64.  Comments?

Thanks,
bin
2017-10-16  Bin Cheng  <bin.cheng@arm.com>

	* match.pd (A + CST cmp A  ->  CST cmp zero): New simplification
	for undefined overflow types in (A + CST CMP A  ->  A CMP' CST').

Comments

Marc Glisse Oct. 19, 2017, 3:22 p.m. UTC | #1
On Thu, 19 Oct 2017, Bin Cheng wrote:

> 	* match.pd (A + CST cmp A  ->  CST cmp zero): New simplification
> 	for undefined overflow types in (A + CST CMP A  ->  A CMP' CST').

Could you check if you still need that? I recently added something very 
similar (search for "X + Y < Y" in match.pd).
Bin.Cheng Oct. 19, 2017, 4:17 p.m. UTC | #2
On Thu, Oct 19, 2017 at 4:22 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Thu, 19 Oct 2017, Bin Cheng wrote:
>
>>         * match.pd (A + CST cmp A  ->  CST cmp zero): New simplification
>>         for undefined overflow types in (A + CST CMP A  ->  A CMP' CST').
>
>
> Could you check if you still need that? I recently added something very
> similar (search for "X + Y < Y" in match.pd).
Ah, yes indeed, the two patterns are unnecessary now on TOT.  I will drop this.

Thanks,
bin
>
> --
> Marc Glisse
diff mbox series

Patch

From 9eb5d484235b97ed6e4e5f153dd7f159d7365f38 Mon Sep 17 00:00:00 2001
From: Bin Cheng <binche01@e108451-lin.cambridge.arm.com>
Date: Mon, 16 Oct 2017 14:24:10 +0100
Subject: [PATCH 1/3] simplify-AopCst-cmp-A-20171006.txt

---
 gcc/match.pd | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index f2c4373..64b023d 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3518,7 +3518,11 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* When one argument is a constant, overflow detection can be simplified.
    Currently restricted to single use so as not to interfere too much with
    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
-   A + CST CMP A  ->  A CMP' CST' */
+   A + CST CMP A  ->  A CMP' CST'
+
+   For type with undefined overflow behavior, the expression can also be
+   simplified by assuming overflow won't happen.
+   A + CST cmp A  -> CST cmp zero.  */
 (for cmp (lt le ge gt)
      out (gt gt le le)
  (simplify
@@ -3530,7 +3534,18 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
 			        wi::max_value (prec, UNSIGNED)
-				- wi::to_wide (@1)); })))))
+				- wi::to_wide (@1)); }))
+   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
+    (with
+     {
+       tree zero = build_zero_cst (TREE_TYPE (@1));
+
+       fold_overflow_warning (("assuming signed overflow does not occur "
+			       "when simplifying A + CST cmp A"),
+			      WARN_STRICT_OVERFLOW_CONDITIONAL);
+     }
+     (cmp @1 { zero; }))))))
 
 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
-- 
1.9.1