171e01f
2012-11-08  Jakub Jelinek  <jakub@redhat.com>
171e01f
171e01f
	PR tree-optimization/55236
171e01f
	* fold-const.c (make_range_step) <case NEGATE_EXPR>: For -fwrapv
171e01f
	and signed ARG0_TYPE, force low and high to be non-NULL.
171e01f
171e01f
	* gcc.dg/pr55236.c: New test.
171e01f
171e01f
--- gcc/fold-const.c.jj	2012-11-07 11:24:34.000000000 +0100
171e01f
+++ gcc/fold-const.c	2012-11-08 16:54:38.978339040 +0100
171e01f
@@ -3880,6 +3880,17 @@ make_range_step (location_t loc, enum tr
171e01f
       return arg0;
171e01f
 
171e01f
     case NEGATE_EXPR:
171e01f
+      /* If flag_wrapv and ARG0_TYPE is signed, make sure
171e01f
+	 low and high are non-NULL, then normalize will DTRT.  */
171e01f
+      if (!TYPE_UNSIGNED (arg0_type)
171e01f
+	  && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
171e01f
+	{
171e01f
+	  if (low == NULL_TREE)
171e01f
+	    low = TYPE_MIN_VALUE (arg0_type);
171e01f
+	  if (high == NULL_TREE)
171e01f
+	    high = TYPE_MAX_VALUE (arg0_type);
171e01f
+	}
171e01f
+
171e01f
       /* (-x) IN [a,b] -> x in [-b, -a]  */
171e01f
       n_low = range_binop (MINUS_EXPR, exp_type,
171e01f
 			   build_int_cst (exp_type, 0),
171e01f
--- gcc/testsuite/gcc.dg/pr55236.c.jj	2012-11-08 16:40:49.171781137 +0100
171e01f
+++ gcc/testsuite/gcc.dg/pr55236.c	2012-11-08 16:41:20.044578919 +0100
171e01f
@@ -0,0 +1,31 @@
171e01f
+/* PR tree-optimization/55236 */
171e01f
+/* { dg-do run } */
171e01f
+/* { dg-options "-O2 -fwrapv" } */
171e01f
+
171e01f
+extern void abort ();
171e01f
+
171e01f
+__attribute__((noinline, noclone)) void
171e01f
+foo (int i)
171e01f
+{
171e01f
+  if (i > 0)
171e01f
+    abort ();
171e01f
+  i = -i;
171e01f
+  if (i < 0)
171e01f
+    return;
171e01f
+  abort ();
171e01f
+}
171e01f
+
171e01f
+__attribute__((noinline, noclone)) void
171e01f
+bar (int i)
171e01f
+{
171e01f
+  if (i > 0 || (-i) >= 0)
171e01f
+    abort ();
171e01f
+}
171e01f
+
171e01f
+int
171e01f
+main ()
171e01f
+{
171e01f
+  foo (-__INT_MAX__ - 1);
171e01f
+  bar (-__INT_MAX__ - 1);
171e01f
+  return 0;
171e01f
+}