public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/101031] [12 Regression] wrong code at -O2 on x86_64-linux-gnu
Date: Mon, 14 Jun 2021 07:35:54 +0000	[thread overview]
Message-ID: <bug-101031-4-gqqH6n6e9M@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-101031-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101031

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Interestingly the bogus store removal happens in the strlen pass.

> diff -u b/a-t3.c.190t.dom3 b/a-t3.c.191t.strlen1
--- b/a-t3.c.190t.dom3  2021-06-14 08:59:33.483709029 +0200
+++ b/a-t3.c.191t.strlen1       2021-06-14 08:59:33.487709082 +0200
@@ -78,7 +80,6 @@
   _25 = (long int) _24;
   d = _25;
   *c.0_14 = 1;
-  b = 0;
   f ();
   return;

in

#0  gsi_remove (i=0x7fffffffd830, remove_permanently=true)
    at /home/rguenther/src/gcc2/gcc/gimple-iterator.c:558
#1  0x0000000001619e63 in handle_store (gsi=0x7fffffffd830, 
    zero_write=0x7fffffffd7df, ptr_qry=...)
    at /home/rguenther/src/gcc2/gcc/tree-ssa-strlen.c:4852
#2  0x000000000161bb9a in check_and_optimize_stmt (gsi=0x7fffffffd830, 
    cleanup_eh=0x7fffffffd89f, ptr_qry=...)
    at /home/rguenther/src/gcc2/gcc/tree-ssa-strlen.c:5439
#3  0x000000000161c30c in strlen_dom_walker::before_dom_children (
    this=0x7fffffffd970, bb=<basic_block 0x7ffff6576270 (2)>)
    at /home/rguenther/src/gcc2/gcc/tree-ssa-strlen.c:5635
#4  0x000000000228e9b6 in dom_walker::walk (this=0x7fffffffd970, 
    bb=<basic_block 0x7ffff6576270 (2)>)
    at /home/rguenther/src/gcc2/gcc/domwalk.c:309

the *c stores have string info index 1 and the 'b' store have index 2.

Interestingly the first *c store is handled by handle_store but the
second is not and we return false from check_and_optimize_stmt.  In
particular we run into

      if (store_before_nul[1] > 0
          && storing_nonzero_p
          && lenrange[0] == lenrange[1]
          && lenrange[0] == lenrange[2]
          && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE)
        {
          /* Handle a store of one or more non-nul characters that ends
             before the terminating nul of the destination and so does
             not affect its length
             If si->nonzero_chars > OFFSET, we aren't overwriting '\0',
             and if we aren't storing '\0', we know that the length of
             the string and any other zero terminated string in memory
             remains the same.  In that case we move to the next gimple
             statement and return to signal the caller that it shouldn't
             invalidate anything.
...
          gsi_next (gsi);
          return false;
        }

and skip the invalidation of 'b'.

Now, we're not invalidating the first *c store at the point we stroe to b
either because maybe_invalidate has

      ao_ref r;
      tree size = NULL_TREE;
      if (si->nonzero_chars)
        {
          /* Include the terminating nul in the size of the string
             to consider when determining possible clobber.  */
          tree type = TREE_TYPE (si->nonzero_chars);
          size = fold_build2 (PLUS_EXPR, type, si->nonzero_chars,
                              build_int_cst (type, 1));
        }
      ao_ref_init_from_ptr_and_size (&r, si->ptr, size);
      if (stmt_may_clobber_ref_p_1 (stmt, &r))

and thus we ask the oracle whether b = 0; may clobber a store/load of
*c of size 2 which it can't (an access of size two cannot refer to 'b').

Looking at compare_nonzero_chars the si->nonzero_chars check should likely
be si->nonzero_chars && !integer_zerop (si->nonzero_chars) as well.

Now, adding 1 is done for correctness AFAIU but then extending the access
beyond the size of the object is invalid.  So IMHO we have to adjust
max_size to be 1 bigger than size instead.

The following fixes the testcase:

diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c
index 423075b2bd1..6add8c99032 100644
--- a/gcc/tree-ssa-strlen.c
+++ b/gcc/tree-ssa-strlen.c
@@ -1284,16 +1284,19 @@ maybe_invalidate (gimple *stmt, bool zero_write =
false)
        continue;

       ao_ref r;
-      tree size = NULL_TREE;
-      if (si->nonzero_chars)
+      tree size = si->nonzero_chars;
+      ao_ref_init_from_ptr_and_size (&r, si->ptr, size);
+      /* Include the terminating nul in the size of the string
+        to consider when determining possible clobber.  But do not
+        add it to 'size' since we don't know whether it would
+        actually fit the allocated area.  */
+      if (known_size_p (r.size))
        {
-         /* Include the terminating nul in the size of the string
-            to consider when determining possible clobber.  */
-         tree type = TREE_TYPE (si->nonzero_chars);
-         size = fold_build2 (PLUS_EXPR, type, si->nonzero_chars,
-                             build_int_cst (type, 1));
+         if (known_le (r.size, HOST_WIDE_INT_MAX - BITS_PER_UNIT))
+           r.max_size += BITS_PER_UNIT;
+         else
+           r.max_size = -1;
        }
-      ao_ref_init_from_ptr_and_size (&r, si->ptr, size);
       if (stmt_may_clobber_ref_p_1 (stmt, &r))
        {
          if (dump_file && (dump_flags & TDF_DETAILS))

  parent reply	other threads:[~2021-06-14  7:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-11 13:28 [Bug tree-optimization/101031] New: " qrzhang at gatech dot edu
2021-06-11 13:29 ` [Bug tree-optimization/101031] " qrzhang at gatech dot edu
2021-06-11 13:31 ` [Bug tree-optimization/101031] [12 Regression] " jakub at gcc dot gnu.org
2021-06-11 13:48 ` rguenth at gcc dot gnu.org
2021-06-11 13:52 ` rguenth at gcc dot gnu.org
2021-06-14  7:35 ` rguenth at gcc dot gnu.org [this message]
2021-06-14  9:14 ` cvs-commit at gcc dot gnu.org
2021-06-14 10:21 ` rguenth at gcc dot gnu.org

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=bug-101031-4-gqqH6n6e9M@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).