public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Richard Biener <rguenth@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-4790] tree-optimization/111445 - simple_iv simplification fault
Date: Fri, 20 Oct 2023 13:53:54 +0000 (GMT)	[thread overview]
Message-ID: <20231020135354.5D6793858D20@sourceware.org> (raw)

https://gcc.gnu.org/g:9692309ed6b625f0fb358c0e230404b5603f69a6

commit r14-4790-g9692309ed6b625f0fb358c0e230404b5603f69a6
Author: Richard Biener <rguenther@suse.de>
Date:   Fri Oct 20 15:08:49 2023 +0200

    tree-optimization/111445 - simple_iv simplification fault
    
    The following fixes a missed check in the simple_iv attempt
    to simplify (signed T)((unsigned T) base + step) where it
    allows a truncating inner conversion leading to wrong code.
    
            PR tree-optimization/111445
            * tree-scalar-evolution.cc (simple_iv_with_niters):
            Add missing check for a sign-conversion.
    
            * gcc.dg/torture/pr111445.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr111445.c | 29 +++++++++++++++++++++++++++++
 gcc/tree-scalar-evolution.cc            |  3 ++-
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr111445.c b/gcc/testsuite/gcc.dg/torture/pr111445.c
new file mode 100644
index 000000000000..320e0b906754
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr111445.c
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+
+extern void abort (void);
+short a, b;
+unsigned char c = 255;
+unsigned cnt;
+void __attribute__((noipa))
+check (int x)
+{
+  if (x != 0)
+    abort ();
+  cnt++;
+}
+int main()
+{
+  int d;
+  unsigned char e;
+  d = 0;
+  for (; a >= 0; a--) {
+    int *f = &d;
+    *f = c;
+  }
+  e = 0;
+  for (; (unsigned char)(d - 255) + e <= 1; e++)
+    check (b);
+  if (cnt != 2)
+    abort ();
+  return 0;
+}
diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
index 7cafe5ce5760..95a15fe09881 100644
--- a/gcc/tree-scalar-evolution.cc
+++ b/gcc/tree-scalar-evolution.cc
@@ -3286,7 +3286,8 @@ simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop,
 
   type = TREE_TYPE (iv->base);
   e = TREE_OPERAND (iv->base, 0);
-  if (TREE_CODE (e) != PLUS_EXPR
+  if (!tree_nop_conversion_p (type, TREE_TYPE (e))
+      || TREE_CODE (e) != PLUS_EXPR
       || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
       || !tree_int_cst_equal (iv->step,
 			      fold_convert (type, TREE_OPERAND (e, 1))))

                 reply	other threads:[~2023-10-20 13:53 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=20231020135354.5D6793858D20@sourceware.org \
    --to=rguenth@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).