public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew MacLeod <amacleod@redhat.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [COMMITTED] tree-optimization/101223 - Fix build_gt and build_lt for signed 1 bit values.
Date: Fri, 2 Jul 2021 11:36:14 -0400	[thread overview]
Message-ID: <9bcaf476-156c-84b3-6dea-b4788e9db2cc@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 520 bytes --]

These 2 routines were adding and subtracting 1 to a range bound, and 
checking for overflow.

Signed 1 bit values have a range of [-1, 0]. Adding or subtracting 1 
cannot be properly represented resulting in the overflow being set.  
This caused us to set UNDEFINED when we shouldn't.

This patch changes it such that signed values always have -1 added or 
subtracted as appropriate, allowing for proper representation and 
overflow setting.

Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed.

Andrew


[-- Attachment #2: pr232.patch --]
[-- Type: text/x-patch, Size: 2549 bytes --]

commit 84f7bab89279ca1234fef88929c74caeda8cb55e
Author: Andrew MacLeod <amacleod@redhat.com>
Date:   Wed Jun 30 14:15:53 2021 -0400

    Fix build_gt and build_lt for signed 1 bit values.
    
    Signed 1 bit values have a range of [-1, 0] but neither (0 - 1) nor (-1 + 1)
    can be represented.  For signed values, add or subtract -1 as appropriate.
    
            PR tree-optimization/101223
            gcc/
            * range-op.cc (build_lt): Add -1 for signed values.
            (built_gt): Subtract -1 for signed values.
    
            gcc/testsuite/
            * gcc.dg/pr101223.c: New.

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 97b9843e095..f8e4c6d4e49 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -687,7 +687,14 @@ static void
 build_lt (irange &r, tree type, const wide_int &val)
 {
   wi::overflow_type ov;
-  wide_int lim = wi::sub (val, 1, TYPE_SIGN (type), &ov);
+  wide_int lim;
+  signop sgn = TYPE_SIGN (type);
+
+  // Signed 1 bit cannot represent 1 for subtraction.
+  if (sgn == SIGNED)
+    lim = wi::add (val, -1, sgn, &ov);
+  else
+    lim = wi::sub (val, 1, sgn, &ov);
 
   // If val - 1 underflows, check if X < MIN, which is an empty range.
   if (ov)
@@ -710,7 +717,14 @@ static void
 build_gt (irange &r, tree type, const wide_int &val)
 {
   wi::overflow_type ov;
-  wide_int lim = wi::add (val, 1, TYPE_SIGN (type), &ov);
+  wide_int lim;
+  signop sgn = TYPE_SIGN (type);
+
+  // Signed 1 bit cannot represent 1 for addition.
+  if (sgn == SIGNED)
+    lim = wi::sub (val, -1, sgn, &ov);
+  else
+    lim = wi::add (val, 1, sgn, &ov);
   // If val + 1 overflows, check is for X > MAX, which is an empty range.
   if (ov)
     r.set_undefined ();
diff --git a/gcc/testsuite/gcc.dg/pr101223.c b/gcc/testsuite/gcc.dg/pr101223.c
new file mode 100644
index 00000000000..6d5a247fa6c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101223.c
@@ -0,0 +1,44 @@
+/* PR tree-optimization/101223 */
+/* { dg-do run } */
+/* { dg-options "-O2 " } */
+
+struct {
+  int a : 1;
+} b;
+int c = 1, d;
+int foo1() {
+  for (; d < 2; d++) {
+    int e = ~c, f = 0, g;
+    if (e) {
+      f = c;
+      g = b.a;
+      b.a = f;
+      if (b.a >= g)
+        __builtin_abort();
+    }
+    c = f;
+    b.a = g;
+  }
+  return 0;
+}
+
+int foo2() {
+  for (; d < 2; d++) {
+    int e = ~c, f = 0, g;
+    if (e) {
+      f = c;
+      g = b.a;
+      b.a = f;
+      if (g <= b.a)
+        __builtin_abort();
+    }
+    c = f;
+    b.a = g;
+  }
+  return 0;
+}
+int main ()
+{
+  return foo1() + foo2();
+}
+  

                 reply	other threads:[~2021-07-02 15:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9bcaf476-156c-84b3-6dea-b4788e9db2cc@redhat.com \
    --to=amacleod@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).