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 r12-1419] tree-optimization/101031 - fix strlen opt invalidation logic
Date: Mon, 14 Jun 2021 09:14:06 +0000 (GMT)	[thread overview]
Message-ID: <20210614091406.78F9A3851C22@sourceware.org> (raw)

https://gcc.gnu.org/g:08ce1f4c5091b80b680d15c53a17237544a3cca8

commit r12-1419-g08ce1f4c5091b80b680d15c53a17237544a3cca8
Author: Richard Biener <rguenther@suse.de>
Date:   Mon Jun 14 09:37:24 2021 +0200

    tree-optimization/101031 - fix strlen opt invalidation logic
    
    strlen opt uses ao_ref_init_from_ptr_and_size to prepare alias
    queries to invalidate its knowledge about strings.  It constrains
    the size using the number of known-nonzero chars and adds one
    for a terminating nul - without knowing whether such nul exists
    or even fits the object.  The latter is now a problem since the
    oracle disambiguates an access of size two (as built so) against
    a store to a plain char variable (where a terminating nul does not
    fit).  The fix is to instead increment max_size but leave size to
    the number of chars we know are accessed.
    
    2021-06-14  Richard Biener  <rguenther@suse.de>
    
            PR tree-optimization/101031
            * tree-ssa-strlen.c (maybe_invalidate): Increment max_size
            instead of size when accounting for a possibly string
            terminating nul.
    
            * gcc.dg/torture/pr101031.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr101031.c | 28 ++++++++++++++++++++++++++++
 gcc/tree-ssa-strlen.c                   | 19 +++++++++++--------
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr101031.c b/gcc/testsuite/gcc.dg/torture/pr101031.c
new file mode 100644
index 00000000000..daf3bcf44eb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr101031.c
@@ -0,0 +1,28 @@
+/* { dg-do run } */
+
+int a;
+char b, e;
+static char *c = &b;
+static long d;
+void f(void);
+void __attribute__((noipa)) h() {
+  int g = 0;
+  for (; g < 2; ++g) {
+    d = *c;
+    *c = 1;
+    b = 0;
+  }
+  f();
+}
+void __attribute__((noipa)) f() {
+  if (d++)
+    c = &e;
+  for (; a;)
+    ;
+}
+int main() {
+  h();
+  if (b != 0)
+    __builtin_abort ();
+  return 0;
+}
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))


                 reply	other threads:[~2021-06-14  9:14 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=20210614091406.78F9A3851C22@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).