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>, Aldy Hernandez <aldyh@redhat.com>
Subject: [COMMITTED 16/16] Callers of irange_bitmask must normalize value/mask pairs.
Date: Sun, 28 Apr 2024 21:05:54 +0200	[thread overview]
Message-ID: <20240428190557.1209586-17-aldyh@redhat.com> (raw)
In-Reply-To: <20240428190557.1209586-1-aldyh@redhat.com>

As per the documentation, irange_bitmask must have the unknown bits in
the mask set to 0 in the value field.  Even though we say we must have
normalized value/mask bits, we don't enforce it, opting to normalize
on the fly in union and intersect.  Avoiding this lazy enforcing as
well as the extra saving/restoring involved in returning the changed
status, gives us a performance increase of 1.25% for VRP and 1.51% for
ipa-CP.

gcc/ChangeLog:

	* tree-ssa-ccp.cc (ccp_finalize): Normalize before calling
	set_bitmask.
	* value-range.cc (irange::intersect_bitmask): Calculate changed
	irange_bitmask bits on our own.
	(irange::union_bitmask): Same.
	(irange_bitmask::verify_mask): Verify that bits are normalized.
	* value-range.h (irange_bitmask::union_): Do not normalize.
	Remove return value.
	(irange_bitmask::intersect): Same.
---
 gcc/tree-ssa-ccp.cc |  1 +
 gcc/value-range.cc  |  7 +++++--
 gcc/value-range.h   | 24 ++++++------------------
 3 files changed, 12 insertions(+), 20 deletions(-)

diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
index f6a5cd0ee6e..3749126b5f7 100644
--- a/gcc/tree-ssa-ccp.cc
+++ b/gcc/tree-ssa-ccp.cc
@@ -1024,6 +1024,7 @@ ccp_finalize (bool nonzero_p)
 	  unsigned int precision = TYPE_PRECISION (TREE_TYPE (val->value));
 	  wide_int value = wi::to_wide (val->value);
 	  wide_int mask = wide_int::from (val->mask, precision, UNSIGNED);
+	  value = value & ~mask;
 	  set_bitmask (name, value, mask);
 	}
     }
diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index a27de5534e1..ca6d521c625 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -2067,7 +2067,8 @@ irange::intersect_bitmask (const irange &r)
 
   irange_bitmask bm = get_bitmask ();
   irange_bitmask save = bm;
-  if (!bm.intersect (r.get_bitmask ()))
+  bm.intersect (r.get_bitmask ());
+  if (save == bm)
     return false;
 
   m_bitmask = bm;
@@ -2099,7 +2100,8 @@ irange::union_bitmask (const irange &r)
 
   irange_bitmask bm = get_bitmask ();
   irange_bitmask save = bm;
-  if (!bm.union_ (r.get_bitmask ()))
+  bm.union_ (r.get_bitmask ());
+  if (save == bm)
     return false;
 
   m_bitmask = bm;
@@ -2133,6 +2135,7 @@ void
 irange_bitmask::verify_mask () const
 {
   gcc_assert (m_value.get_precision () == m_mask.get_precision ());
+  gcc_checking_assert (wi::bit_and (m_mask, m_value) == 0);
 }
 
 void
diff --git a/gcc/value-range.h b/gcc/value-range.h
index 0ab717697f0..11c73faca1b 100644
--- a/gcc/value-range.h
+++ b/gcc/value-range.h
@@ -139,8 +139,8 @@ public:
   void set_unknown (unsigned prec);
   bool unknown_p () const;
   unsigned get_precision () const;
-  bool union_ (const irange_bitmask &src);
-  bool intersect (const irange_bitmask &src);
+  void union_ (const irange_bitmask &src);
+  void intersect (const irange_bitmask &src);
   bool operator== (const irange_bitmask &src) const;
   bool operator!= (const irange_bitmask &src) const { return !(*this == src); }
   void verify_mask () const;
@@ -233,29 +233,18 @@ irange_bitmask::operator== (const irange_bitmask &src) const
   return m_value == src.m_value && m_mask == src.m_mask;
 }
 
-inline bool
-irange_bitmask::union_ (const irange_bitmask &orig_src)
+inline void
+irange_bitmask::union_ (const irange_bitmask &src)
 {
-  // Normalize mask.
-  irange_bitmask src (orig_src.m_value & ~orig_src.m_mask, orig_src.m_mask);
-  m_value &= ~m_mask;
-
-  irange_bitmask save (*this);
   m_mask = (m_mask | src.m_mask) | (m_value ^ src.m_value);
   m_value = m_value & src.m_value;
   if (flag_checking)
     verify_mask ();
-  return *this != save;
 }
 
-inline bool
-irange_bitmask::intersect (const irange_bitmask &orig_src)
+inline void
+irange_bitmask::intersect (const irange_bitmask &src)
 {
-  // Normalize mask.
-  irange_bitmask src (orig_src.m_value & ~orig_src.m_mask, orig_src.m_mask);
-  m_value &= ~m_mask;
-
-  irange_bitmask save (*this);
   // If we have two known bits that are incompatible, the resulting
   // bit is undefined.  It is unclear whether we should set the entire
   // range to UNDEFINED, or just a subset of it.  For now, set the
@@ -274,7 +263,6 @@ irange_bitmask::intersect (const irange_bitmask &orig_src)
     }
   if (flag_checking)
     verify_mask ();
-  return *this != save;
 }
 
 // An integer range without any storage.
-- 
2.44.0


      parent reply	other threads:[~2024-04-28 19:06 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-28 19:05 [PATCH 00/16] prange supporting patchset Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 01/16] Make vrange an abstract class Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 02/16] Add a virtual vrange destructor Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 03/16] Make some Value_Range's explicitly integer Aldy Hernandez
2024-04-30  7:46   ` Richard Biener
2024-04-30  8:38     ` Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 04/16] Add tree versions of lower and upper bounds to vrange Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 05/16] Move bitmask routines to vrange base class Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 06/16] Remove GTY support for vrange and derived classes Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 07/16] Make fold_cond_with_ops use a boolean type for range_true/range_false Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 08/16] Change range_includes_zero_p argument to a reference Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 09/16] Verify that reading back from vrange_storage doesn't drop bits Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 10/16] Accept a vrange in get_legacy_range Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 11/16] Move get_bitmask_from_range out of irange class Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 12/16] Make some integer specific ranges generic Value_Range's Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 13/16] Accept any vrange in range_includes_zero_p Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 14/16] Move print_irange_* out of vrange_printer class Aldy Hernandez
2024-04-28 19:05 ` [COMMITTED 15/16] Remove range_zero and range_nonzero Aldy Hernandez
2024-04-28 19:05 ` Aldy Hernandez [this message]

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=20240428190557.1209586-17-aldyh@redhat.com \
    --to=aldyh@redhat.com \
    --cc=amacleod@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).