public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-2944] Special case -TYPE_MIN_VALUE for flag_wrapv in operator_abs::op1_range.
@ 2021-08-17  9:39 Aldy Hernandez
  0 siblings, 0 replies; only message in thread
From: Aldy Hernandez @ 2021-08-17  9:39 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:891bdbf2b0432b4aa3d3e76923617fcb4fd33cf6

commit r12-2944-g891bdbf2b0432b4aa3d3e76923617fcb4fd33cf6
Author: Andrew MacLeod <amacleod@redhat.com>
Date:   Tue Aug 17 10:50:56 2021 +0200

    Special case -TYPE_MIN_VALUE for flag_wrapv in operator_abs::op1_range.
    
    With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
    unrepresentable.  We currently special case this in the ABS folding
    routine, but are missing similar treatment in operator_abs::op1_range.
    
    Tested on x86-64 Linux.
    
            PR tree-optimization/101938
    
    gcc/ChangeLog:
    
            * range-op.cc (operator_abs::op1_range): Special case
            -TYPE_MIN_VALUE for flag_wrapv.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/pr101938.c: New test.

Diff:
---
 gcc/range-op.cc                 |  6 ++++++
 gcc/testsuite/gcc.dg/pr101938.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index eb66e12677f..56eccf471a2 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -3642,6 +3642,12 @@ operator_abs::op1_range (irange &r, tree type,
     r.union_ (int_range<1> (type,
 			    -positives.upper_bound (i),
 			    -positives.lower_bound (i)));
+  // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
+  // unrepresentable.  Add -TYPE_MIN_VALUE in this case.
+  wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
+  wide_int lb = lhs.lower_bound ();
+  if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
+    r.union_ (int_range<2> (type, lb, lb));
   return true;
 }
 
diff --git a/gcc/testsuite/gcc.dg/pr101938.c b/gcc/testsuite/gcc.dg/pr101938.c
new file mode 100644
index 00000000000..82777554eaf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101938.c
@@ -0,0 +1,28 @@
+// { dg-do run }
+// { dg-require-effective-target lp64 }
+// { dg-options "-O2 -fwrapv" }
+
+typedef long long int int64;
+#define INT64CONST(x) (x##LL)
+/* -9223372036854775808ULL */
+#define INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
+
+static void __attribute__((noipa)) foo(int64 arg1, int64 arg2) {
+  int64 a1 = -arg1;
+  int64 a2 = (arg2 < 0) ? arg2 : -arg2;
+
+  if (a1 > a2) {
+    int64 swap = arg1;
+    arg1 = arg2;
+    arg2 = swap;
+  }
+
+  if (arg1 == INT64_MIN && arg2 == -1) return;
+
+  __builtin_abort();
+}
+
+int main() {
+  foo(-1, INT64_MIN);
+  return 0;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-08-17  9:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-17  9:39 [gcc r12-2944] Special case -TYPE_MIN_VALUE for flag_wrapv in operator_abs::op1_range Aldy Hernandez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).