From fe1e7d0e9b7479f13b84670ac3b56cf5244d877c Mon Sep 17 00:00:00 2001
From: Eric Botcazou <ebotcazou@adacore.com>
Date: Wed, 19 Apr 2017 19:27:09 +0000
Subject: [PATCH] re PR tree-optimization/80426 (wrong manipulation of range
 based on INT_MIN)

	PR tree-optimization/80426
	* tree-vrp.c (extract_range_from_binary_expr_1): For an additive
	operation on symbolic operands, also compute the overflow for the
	invariant part when the operation degenerates into a negation.

	PR tree-optimization/80426
	* gcc.c-torture/execute/20170419-1.c: New test.

Co-Authored-By: Jakub Jelinek <jakub@redhat.com>

From-SVN: r247007
---
 gcc/ChangeLog                                 |  8 +++++
 gcc/testsuite/ChangeLog                       |  5 ++++
 .../gcc.c-torture/execute/20170429-1.c        | 24 +++++++++++++++
 gcc/tree-vrp.c                                | 30 +++++++++++++++++--
 4 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/20170429-1.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 6b17e9a8b877..de76f8a83f46 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@
+2017-04-19  Eric Botcazou  <ebotcazou@adacore.com>
+            Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/80426
+	* tree-vrp.c (extract_range_from_binary_expr_1): For an additive
+	operation on symbolic operands, also compute the overflow for the
+	invariant part when the operation degenerates into a negation.
+
 2017-04-19  Jakub Jelinek  <jakub@redhat.com>
 
 	PR debug/80461
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 048541d31074..7c306a683e9c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2017-04-19  Eric Botcazou  <ebotcazou@adacore.com>
+
+	PR tree-optimization/80426
+	* gcc.c-torture/execute/20170419-1.c: New test.
+
 2017-04-19  Jakub Jelinek  <jakub@redhat.com>
 
 	PR debug/80461
diff --git a/gcc/testsuite/gcc.c-torture/execute/20170429-1.c b/gcc/testsuite/gcc.c-torture/execute/20170429-1.c
new file mode 100644
index 000000000000..f59dbc7a0578
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/20170429-1.c
@@ -0,0 +1,24 @@
+/* PR tree-optimization/80426 */
+/* Testcase by <ishiura-compiler@ml.kwansei.ac.jp> */
+
+#define INT_MAX 0x7fffffff
+#define INT_MIN (-INT_MAX-1)
+
+int x;
+
+int main (void)
+{
+  volatile int a = 0;
+  volatile int b = -INT_MAX;
+  int j;
+
+  for(j = 0; j < 18; j += 1) {
+    x = ( (a == 0) != (b - (int)(INT_MIN) ) );
+  }
+
+  if (x != 0)
+    __builtin_abort ();
+
+  return 0;
+}
+
diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c
index 6d802de29a80..697cd88502eb 100644
--- a/gcc/tree-vrp.c
+++ b/gcc/tree-vrp.c
@@ -2461,7 +2461,20 @@ extract_range_from_binary_expr_1 (value_range *vr,
 	  else if (min_op0)
 	    wmin = min_op0;
 	  else if (min_op1)
-	    wmin = minus_p ? wi::neg (min_op1) : min_op1;
+	    {
+	      if (minus_p)
+		{
+		  wmin = wi::neg (min_op1);
+
+		  /* Check for overflow.  */
+		  if (sgn == SIGNED && wi::neg_p (min_op1) && wi::neg_p (wmin))
+		    min_ovf = 1;
+		  else if (sgn == UNSIGNED && wi::ne_p (min_op1, 0))
+		    min_ovf = -1;
+		}
+	      else
+		wmin = min_op1;
+	    }
 	  else
 	    wmin = wi::shwi (0, prec);
 
@@ -2489,7 +2502,20 @@ extract_range_from_binary_expr_1 (value_range *vr,
 	  else if (max_op0)
 	    wmax = max_op0;
 	  else if (max_op1)
-	    wmax = minus_p ? wi::neg (max_op1) : max_op1;
+	    {
+	      if (minus_p)
+		{
+		  wmax = wi::neg (max_op1);
+
+		  /* Check for overflow.  */
+		  if (sgn == SIGNED && wi::neg_p (max_op1) && wi::neg_p (wmax))
+		    max_ovf = 1;
+		  else if (sgn == UNSIGNED && wi::ne_p (max_op1, 0))
+		    max_ovf = -1;
+		}
+	      else
+		wmax = max_op1;
+	    }
 	  else
 	    wmax = wi::shwi (0, prec);
 
-- 
GitLab