public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Aldy Hernandez <aldyh@redhat.com>
To: GCC patches <gcc-patches@gcc.gnu.org>
Cc: Andrew MacLeod <amacleod@redhat.com>,
	drepper@redhat.com, Aldy Hernandez <aldyh@redhat.com>
Subject: [COMMITTED] irange: keep better track of powers of 2.
Date: Tue, 27 Sep 2022 10:46:06 +0200	[thread overview]
Message-ID: <20220927084606.3409637-1-aldyh@redhat.com> (raw)

When setting the nonzero bits to a mask containing only one bit, set
the range immediately, as it can be devined from the mask.  This helps
us keep better track of powers of two.

For example, with this patch a nonzero mask of 0x8000 is set to a
range of [0,0][0x8000,0x8000] with a nonzero mask of 0x8000.

Tested on x86-64 Linux.

p.s. Thanks for the bug report Uli.

gcc/ChangeLog:

	* value-range.cc (irange::set_nonzero_bits): Set range when known.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/popcount6.c: New test.
---
 gcc/testsuite/gcc.dg/tree-ssa/popcount6.c | 12 ++++++++++++
 gcc/value-range.cc                        | 13 +++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/popcount6.c

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c
new file mode 100644
index 00000000000..1406ad9d33b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c
@@ -0,0 +1,12 @@
+// { dg-do compile }
+// { dg-options "-O2 -fdump-tree-evrp" }
+
+int g(int n)
+{
+  n &= 0x8000;
+  if (n == 0)
+    return 1;
+  return __builtin_popcount(n);
+}
+
+// { dg-final { scan-tree-dump "return 1;" "evrp" } }
diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index 754379add19..6154d73ccf5 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -2930,6 +2930,19 @@ irange::set_nonzero_bits (const wide_int_ref &bits)
       set_nonzero_bits (NULL);
       return;
     }
+  // If we have only one bit set in the mask, we can figure out the
+  // range immediately.
+  if (wi::popcount (bits) == 1)
+    {
+      bool has_zero = contains_p (build_zero_cst (type ()));
+      set (type (), bits, bits);
+      if (has_zero)
+	{
+	  int_range<2> zero;
+	  zero.set_zero (type ());
+	  union_ (zero);
+	}
+    }
   set_nonzero_bits (wide_int_to_tree (type (), bits));
 }
 
-- 
2.37.1


                 reply	other threads:[~2022-09-27  8:46 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=20220927084606.3409637-1-aldyh@redhat.com \
    --to=aldyh@redhat.com \
    --cc=amacleod@redhat.com \
    --cc=drepper@redhat.com \
    --cc=gcc-patches@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).