Fix PR97315 (part 2 of 2)
authorAldy Hernandez <aldyh@redhat.com>
Thu, 8 Oct 2020 09:33:30 +0000 (11:33 +0200)
committerAldy Hernandez <aldyh@redhat.com>
Thu, 8 Oct 2020 09:33:30 +0000 (11:33 +0200)
gcc/ChangeLog:

PR tree-optimization/97315
* range-op.cc (value_range_with_overflow): Change any
non-overflow calculation in which both bounds are
overflow/underflow to be undefined.

gcc/testsuite/ChangeLog:

* gcc.dg/pr97315-2.c: New test.

gcc/range-op.cc
gcc/testsuite/gcc.dg/pr97315-2.c [new file with mode: 0644]

index 87c6d82f2ac1bbdeb9f79f9780cad67871721d72..22bc23c1bbfe3cf2313c02fd1df545ea8f6ef331 100644 (file)
@@ -287,6 +287,15 @@ value_range_with_overflow (irange &r, tree type,
     }
   else
     {
+      // If both bounds either underflowed or overflowed, then the result
+      // is undefined.
+      if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
+         || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
+       {
+         r.set_undefined ();
+         return;
+       }
+
       // If overflow does not wrap, saturate to [MIN, MAX].
       wide_int new_lb, new_ub;
       if (min_ovf == wi::OVF_UNDERFLOW)
diff --git a/gcc/testsuite/gcc.dg/pr97315-2.c b/gcc/testsuite/gcc.dg/pr97315-2.c
new file mode 100644 (file)
index 0000000..5dd1b6a
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+void c(int);
+
+int a;
+void b()
+{
+  if (a >= 2147483647)
+    c(a + 1);
+}