This patch is split into 4 parts in case one of them causes any issues, it'll be easier to track.

In an attempt to make bitmasks on ranges a bit more consistent, get_bitmask_from_range has uses outside value-range.cc.  This patch simply moves the static function into a constructor for irange_bitmask.

Bootstrapped on x86_64-pc-linux-gnu with no regressions.  pushed.
From e4c6a0214c3ea8aa73e50b1496eb7a8aa5eda635 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacl...@redhat.com>
Date: Tue, 13 May 2025 13:23:16 -0400
Subject: [PATCH 1/4] Turn get_bitmask_from_range into an irange_bitmask
 constructor.

There are other places where this is interesting, so move the static
function into a constructor for class irange_bitmask.

	* value-range.cc (irange_bitmask::irange_bitmask): Rename from
	get_bitmask_from_range and tweak.
	(prange::set): Use new constructor.
	(prange::intersect): Use new constructor.
	(irange::get_bitmask): Likewise.
	* value-range.h (irange_bitmask): New constructor prototype.
---
 gcc/value-range.cc | 32 ++++++++++++++++----------------
 gcc/value-range.h  |  2 ++
 2 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index d2c14e7900d..48a1521b81e 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -31,25 +31,26 @@ along with GCC; see the file COPYING3.  If not see
 #include "fold-const.h"
 #include "gimple-range.h"
 
-// Return the bitmask inherent in a range.
+// Return the bitmask inherent in a range :   TYPE [MIN, MAX].
+// This use to be get_bitmask_from_range ().
 
-static irange_bitmask
-get_bitmask_from_range (tree type,
-			const wide_int &min, const wide_int &max)
+irange_bitmask::irange_bitmask (tree type,
+				const wide_int &min, const wide_int &max)
 {
   unsigned prec = TYPE_PRECISION (type);
-
   // All the bits of a singleton are known.
   if (min == max)
     {
-      wide_int mask = wi::zero (prec);
-      wide_int value = min;
-      return irange_bitmask (value, mask);
+      m_mask = wi::zero (prec);
+      m_value = min;
+    }
+  else
+    {
+      wide_int xorv = min ^ max;
+      xorv = wi::mask (prec - wi::clz (xorv), false, prec);
+      m_value = wi::zero (prec);
+      m_mask = min | xorv;
     }
-
-  wide_int xorv = min ^ max;
-  xorv = wi::mask (prec - wi::clz (xorv), false, prec);
-  return irange_bitmask (wi::zero (prec), min | xorv);
 }
 
 void
@@ -469,7 +470,7 @@ prange::set (tree type, const wide_int &min, const wide_int &max,
     }
 
   m_kind = VR_RANGE;
-  m_bitmask = get_bitmask_from_range (type, min, max);
+  m_bitmask = irange_bitmask (type, min, max);
   if (flag_checking)
     verify_range ();
 }
@@ -583,7 +584,7 @@ prange::intersect (const vrange &v)
     }
 
   // Intersect all bitmasks: the old one, the new one, and the other operand's.
-  irange_bitmask new_bitmask = get_bitmask_from_range (m_type, m_min, m_max);
+  irange_bitmask new_bitmask (m_type, m_min, m_max);
   m_bitmask.intersect (new_bitmask);
   m_bitmask.intersect (r.m_bitmask);
   if (varying_compatible_p ())
@@ -2396,8 +2397,7 @@ irange::get_bitmask () const
   // in the mask.
   //
   // See also the note in irange_bitmask::intersect.
-  irange_bitmask bm
-    = get_bitmask_from_range (type (), lower_bound (), upper_bound ());
+  irange_bitmask bm (type (), lower_bound (), upper_bound ());
   if (!m_bitmask.unknown_p ())
     bm.intersect (m_bitmask);
   return bm;
diff --git a/gcc/value-range.h b/gcc/value-range.h
index f6942989a6f..74cdf29ddcb 100644
--- a/gcc/value-range.h
+++ b/gcc/value-range.h
@@ -136,6 +136,8 @@ public:
   irange_bitmask () { /* uninitialized */ }
   irange_bitmask (unsigned prec) { set_unknown (prec); }
   irange_bitmask (const wide_int &value, const wide_int &mask);
+  irange_bitmask (tree type, const wide_int &min, const wide_int &max);
+
   wide_int value () const { return m_value; }
   wide_int mask () const { return m_mask; }
   void set_unknown (unsigned prec);
-- 
2.45.0

Reply via email to