public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-4990] vrp: Handle pointers in maybe_set_nonzero_bits [PR108253]
Date: Wed,  4 Jan 2023 11:17:07 +0000 (GMT)	[thread overview]
Message-ID: <20230104111707.3F5643858D1E@sourceware.org> (raw)

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

commit r13-4990-gebc449119442501c927ede0e83697eaece72223e
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jan 4 12:16:22 2023 +0100

    vrp: Handle pointers in maybe_set_nonzero_bits [PR108253]
    
    maybe_set_nonzero_bits calls set_nonzero_bits which asserts that
    var doesn't have pointer type.  While we could punt for those
    cases, I think we can handle at least some easy cases.
    Earlier in maybe_set_nonzero_bits we've checked this is on
    (var & cst) == 0
    edge and the other edge is __builtin_unreachable, so if cst
    is say 3 as in the testcase, we want to turn it into 4 byte alignment
    of the pointer.
    
    2023-01-04  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/108253
            * tree-vrp.cc (maybe_set_nonzero_bits): Handle var with pointer
            types.
    
            * g++.dg/opt/pr108253.C: New test.

Diff:
---
 gcc/testsuite/g++.dg/opt/pr108253.C | 48 +++++++++++++++++++++++++++++++++++++
 gcc/tree-vrp.cc                     | 18 ++++++++++++--
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/g++.dg/opt/pr108253.C b/gcc/testsuite/g++.dg/opt/pr108253.C
new file mode 100644
index 00000000000..0409121f2c7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/pr108253.C
@@ -0,0 +1,48 @@
+// PR tree-optimization/108253
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2" }
+
+struct S
+{
+  int *s;
+  S () : s (new int) {}
+  S (const S &r) noexcept : s (r.s) { __atomic_fetch_add (r.s, 1, 4); }
+};
+struct T
+{
+  explicit T (const S &x) : t (x) {}
+  const S t;
+};
+struct U
+{
+  operator int () const { new T (u); return 0; }
+  S u;
+};
+bool foo (int matcher);
+unsigned long bar (unsigned long pos, unsigned long end_pos);
+struct V
+{
+  alignas (4) char v[4];
+};
+struct W
+{
+  void baz ()
+  {
+    if (!w) __builtin_abort ();
+    if (reinterpret_cast <__UINTPTR_TYPE__> (w->v) % 4 != 0) __builtin_abort ();
+    __builtin_unreachable ();
+  }
+  [[gnu::noinline]] void qux (unsigned long) { if (!w) bar (0, x); } 
+  V *w = nullptr;
+  unsigned x = 0;
+};
+
+void
+test ()
+{
+  W w;
+  U t;
+  if (!foo (t))
+    w.baz ();
+  w.qux (0);
+}
diff --git a/gcc/tree-vrp.cc b/gcc/tree-vrp.cc
index 0dfea5ace06..3c431760a16 100644
--- a/gcc/tree-vrp.cc
+++ b/gcc/tree-vrp.cc
@@ -789,8 +789,22 @@ maybe_set_nonzero_bits (edge e, tree var)
 	return;
     }
   cst = gimple_assign_rhs2 (stmt);
-  set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
-					  wi::to_wide (cst)));
+  if (POINTER_TYPE_P (TREE_TYPE (var)))
+    {
+      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (var);
+      if (pi && pi->misalign)
+	return;
+      wide_int w = wi::bit_not (wi::to_wide (cst));
+      unsigned int bits = wi::ctz (w);
+      if (bits == 0 || bits >= HOST_BITS_PER_INT)
+	return;
+      unsigned int align = 1U << bits;
+      if (pi == NULL || pi->align < align)
+	set_ptr_info_alignment (get_ptr_info (var), align, 0);
+    }
+  else
+    set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
+					    wi::to_wide (cst)));
 }
 
 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL

                 reply	other threads:[~2023-01-04 11:17 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=20230104111707.3F5643858D1E@sourceware.org \
    --to=jakub@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).