public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Roger Sayle <sayle@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-6621] PR middle-end/109031: Fix final value replacement from narrower IVs.
Date: Sun, 12 Mar 2023 22:55:43 +0000 (GMT)	[thread overview]
Message-ID: <20230312225543.CDA78385841A@sourceware.org> (raw)

https://gcc.gnu.org/g:eb1d8df792f990574cbb695b55c92ee2684fc96b

commit r13-6621-geb1d8df792f990574cbb695b55c92ee2684fc96b
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Sun Mar 12 22:52:41 2023 +0000

    PR middle-end/109031: Fix final value replacement from narrower IVs.
    
    This patch fixes a P1 regression, a problem with my February 2022 patch
    to improve folding for final value replacement:
    https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590618.html
    
    The motivation for the original patch is that because we know the number
    of loop iterations can't be negative, final value expressions such as
    (int) ((unsigned int) x + 4294967295) + 1 can be simplified to x,
    as this is effectively ((x - 1) + 1) without overflow.
    
    The bug/oversight is that using integer_all_onesp to check for the
    implicit tree constant -1 it didn't consider that the inner (unsigned)
    type might be narrower than hthe outer result type.  For the case in the
    PR, (int)((unsigned char)x + 255) + 1 gets simplified to (int)x, but
    when x is originally zero, the correct result should be 256.
    
    The fix is to check that the inner type's precision (the width of the
    subtraction) is at least as wide as the result type (that of the addition).
    
    I've also added a test for signed types, but without -fwrapv this
    invokes undefined behaviour, and with -fwrapv it doesn't exhibit the
    problem in the PR.
    
    2023-03-12  Roger Sayle  <roger@nextmovesoftware.com>
    
    gcc/ChangeLog
            PR middle-end/109031
            * tree-chrec.cc (chrec_apply): When folding "{a, +, a} (x-1)",
            ensure that the type of x is as wide or wider than the type of a.
    
    gcc/testsuite/ChangeLog
            PR middle-end/109031
            * gcc.dg/tree-ssa/pr109031-1.c: New test case.
            * gcc.dg/tree-ssa/pr109031-2.c: Likewise.

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c | 39 ++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c | 39 ++++++++++++++++++++++++++++++
 gcc/tree-chrec.cc                          |  4 ++-
 3 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c
new file mode 100644
index 00000000000..84e1a08be29
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c
@@ -0,0 +1,39 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+unsigned char uc;
+unsigned short us;
+
+void testuc() {
+  unsigned int g = 0;
+  unsigned int *p1 = &g;
+  unsigned char *p2 = &uc;
+
+  do {
+    (*p1)++;
+    (*p2)--;
+  } while (uc);
+
+  if (g != 256)
+    __builtin_abort();
+}
+
+void testus() {
+  unsigned int g = 0;
+  unsigned int *p1 = &g;
+  unsigned short *p2 = &us;
+
+  do {
+    (*p1)++;
+    (*p2)--;
+  } while (us);
+
+  if (g != 65536)
+    __builtin_abort();
+}
+
+int main() {
+  testuc();
+  testus();
+  return 0;
+}
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c
new file mode 100644
index 00000000000..6f28b3b5ed8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c
@@ -0,0 +1,39 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fwrapv" } */
+signed char sc;
+signed short ss;
+
+void testsc() {
+  unsigned int g = 0;
+  unsigned int *p1 = &g;
+  signed char *p2 = &sc;
+
+  do {
+    (*p1)++;
+    (*p2)--;
+  } while (sc);
+
+  if (g != 256)
+    __builtin_abort();
+}
+
+void testss() {
+  unsigned int g = 0;
+  unsigned int *p1 = &g;
+  signed short *p2 = &ss;
+
+  do {
+    (*p1)++;
+    (*p2)--;
+  } while (ss);
+
+  if (g != 65536)
+    __builtin_abort();
+}
+
+int main() {
+  testsc();
+  testss();
+  return 0;
+}
+
diff --git a/gcc/tree-chrec.cc b/gcc/tree-chrec.cc
index f93d8dc406c..2f67581591a 100644
--- a/gcc/tree-chrec.cc
+++ b/gcc/tree-chrec.cc
@@ -623,7 +623,9 @@ chrec_apply (unsigned var,
 	  else if (operand_equal_p (CHREC_LEFT (chrec), chrecr)
 		   && TREE_CODE (x) == PLUS_EXPR
 		   && integer_all_onesp (TREE_OPERAND (x, 1))
-		   && !POINTER_TYPE_P (type))
+		   && !POINTER_TYPE_P (type)
+		   && TYPE_PRECISION (TREE_TYPE (x))
+		      >= TYPE_PRECISION (type))
 	    {
 	      /* We know the number of iterations can't be negative.
 		 So {a, +, a} (x-1) -> "a*x".  */

                 reply	other threads:[~2023-03-12 22:55 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=20230312225543.CDA78385841A@sourceware.org \
    --to=sayle@gcc.gnu.org \
    --cc=gcc-cvs@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).