public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [GCC 11] [COMMITTED] Fix build_gt and build_lt for signed 1 bit values.
@ 2021-07-14 22:15 Andrew MacLeod
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew MacLeod @ 2021-07-14 22:15 UTC (permalink / raw)
  To: gcc-patches

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

Cherry picked from 84f7bab89279ca1234fef88929c74caeda8cb55e

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

Andrew


[-- Attachment #2: 0008-Fix-build_gt-and-build_lt-for-signed-1-bit-values.patch --]
[-- Type: text/x-patch, Size: 2813 bytes --]

From b977e6b29c67be81df882d1f5cc7eb6a5d8c98a0 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Wed, 30 Jun 2021 14:15:53 -0400
Subject: [PATCH 8/8] 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.

(cherry picked from commit 84f7bab89279ca1234fef88929c74caeda8cb55e)
---
 gcc/range-op.cc                 | 18 ++++++++++++--
 gcc/testsuite/gcc.dg/pr101223.c | 44 +++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr101223.c

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 3a35a2fb25b..c33f4edc2c1 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -562,7 +562,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)
@@ -585,7 +592,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();
+}
+  
-- 
2.17.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [GCC 11] [COMMITTED] Fix build_gt and build_lt for signed 1 bit values.
@ 2021-07-14 22:15 Andrew MacLeod
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew MacLeod @ 2021-07-14 22:15 UTC (permalink / raw)
  To: gcc-patches

Cherry picked from 84f7bab89279ca1234fef88929c74caeda8cb55e

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

Andrew


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-14 22:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-14 22:15 [GCC 11] [COMMITTED] Fix build_gt and build_lt for signed 1 bit values Andrew MacLeod
  -- strict thread matches above, loose matches on Subject: below --
2021-07-14 22:15 Andrew MacLeod

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).