This patch moves the  ptr_info_def pointer from the tree union into a prange_storage.

The primary difference this makes is that there can never be a ptr_info_def object without an appropriate prange-storage object created.  A few places can be cleaned up based on this assumption and the ssa-name tree type no longer needs the union which excluded pointer ranges.

Bootstrapped on x86_64-pc-linux-gnu with  no regressions. OK?

Andrew
From d3f36934e8f7712d007f231ba83d32fffb79ebfe Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <[email protected]>
Date: Wed, 7 Oct 2026 16:06:58 -0400
Subject: [PATCH 1/9] Move ptr_info_def pointer into prange_storage

Move the ptr_info_def pointer from the tree union into a prange_storage.
This allows a full prange to be stored in an SSA_NAME rather than
just the non-null attribute.

	* ipa-icf.cc (sem_item_optimizer::merge_classes): Do not special
	case pointer ranges.
	* print-tree.cc (print_node): Print all range addresses, not
	just pointers.
	* tree-core.h (union ssa_name_info_type): Remove.
	* tree-ssanames.cc (range_info_alloc): Change return type.
	(ssa_name_ptr_info): New.
	(range_info_set_range): Preserve ptr_info_def pointer.
	(make_ssa_name_fn): Do not special case pointers.
	(set_ssa_name_ptr_info): New.
	(get_ptr_info): Use set_ssa_name_ptr_info.
	(duplicate_ssa_name_ptr_info): Likewise.
	* tree-ssanames.h (ssa_name_ptr_info): Add prototype.
	(SSA_NAME_PTR_INFO): Relocate from tree.h and adjust.
	* tree.h (SSA_NAME_PTR_INFO): Move to tree-ssanames.h.
	(SSA_NAME_RANGE_INFO): Adjust.
	* value-range-storage.cc (prange_storage::prange_storage): Set
	ptr_info field.
	* value-range-storage.h (prange_storage::ptr_info): New member.
---
 gcc/ipa-icf.cc             | 12 ++-------
 gcc/print-tree.cc          |  6 ++---
 gcc/tree-core.h            | 12 +++------
 gcc/tree-ssanames.cc       | 51 ++++++++++++++++++++++++++++++++------
 gcc/tree-ssanames.h        |  3 +++
 gcc/tree.h                 | 10 +++-----
 gcc/value-range-storage.cc |  4 +++
 gcc/value-range-storage.h  |  7 +++---
 8 files changed, 65 insertions(+), 40 deletions(-)

diff --git a/gcc/ipa-icf.cc b/gcc/ipa-icf.cc
index 64cc722bd7a..9ef2149e3aa 100644
--- a/gcc/ipa-icf.cc
+++ b/gcc/ipa-icf.cc
@@ -3495,19 +3495,11 @@ sem_item_optimizer::merge_classes (unsigned int prev_class_count,
 	    tree name;
 	    FOR_EACH_SSA_NAME (i, name, DECL_STRUCT_FUNCTION (source->decl))
 	      {
-		/* We need to either merge or reset SSA_NAME_*_INFO.
+		/* We need to either merge or reset SSA_NAME_RANGE_INFO.
 		   For merging we don't preserve the mapping between
 		   original and alias SSA_NAMEs from successful equals
 		   calls.  */
-		if (POINTER_TYPE_P (TREE_TYPE (name)))
-		  {
-		    if (SSA_NAME_PTR_INFO (name))
-		      {
-			gcc_checking_assert (!flag_wpa);
-			SSA_NAME_PTR_INFO (name) = NULL;
-		      }
-		  }
-		else if (SSA_NAME_RANGE_INFO (name))
+		if (SSA_NAME_RANGE_INFO (name))
 		  {
 		    gcc_checking_assert (!flag_wpa);
 		    SSA_NAME_RANGE_INFO (name) = NULL;
diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc
index d48f42d635f..379e0081ba5 100644
--- a/gcc/print-tree.cc
+++ b/gcc/print-tree.cc
@@ -976,11 +976,11 @@ print_node (FILE *file, const char *prefix, tree node, int indent,
 	  if (SSA_NAME_IN_FREE_LIST (node))
 	    fprintf (file, " in-free-list");
 
-	  if (SSA_NAME_PTR_INFO (node))
+	  /* Print the storage address for any range-info.  */
+	  if (SSA_NAME_RANGE_INFO (node))
 	    {
 	      indent_to (file, indent + 3);
-	      if (SSA_NAME_PTR_INFO (node))
-		dump_addr (file, " ptr-info ", SSA_NAME_PTR_INFO (node));
+	      dump_addr (file, " range-info ", SSA_NAME_RANGE_INFO (node));
 	    }
 	  break;
 
diff --git a/gcc/tree-core.h b/gcc/tree-core.h
index c252cd28bf6..80d0540c763 100644
--- a/gcc/tree-core.h
+++ b/gcc/tree-core.h
@@ -1737,14 +1737,10 @@ struct GTY(()) tree_ssa_name {
   /* Statement that defines this SSA name.  */
   gimple *def_stmt;
 
-  /* Value range information.  */
-  union ssa_name_info_type {
-    /* Range and aliasing info for pointers.  */
-    struct GTY ((tag ("0"))) ptr_info_def *ptr_info;
-    /* Range info for everything else.  */
-    struct GTY ((tag ("1"))) vrange_storage * range_info;
-  } GTY ((desc ("%1.typed.type ?" \
-		"!POINTER_TYPE_P (TREE_TYPE ((tree)&%1)) : 2"))) info;
+  /* Value range information for every type of SSA name.
+     The aliasing information for a prange is stored in prange_storage.
+     See SSA_NAME_PTR_INFO in tree-ssanames.h.  */
+  struct GTY (()) vrange_storage *range_info;
   /* Immediate uses list for this SSA_NAME.  */
   struct ssa_use_operand_t imm_uses;
 #if defined ENABLE_GIMPLE_CHECKING
diff --git a/gcc/tree-ssanames.cc b/gcc/tree-ssanames.cc
index 8eac57f42cb..01ba0573efe 100644
--- a/gcc/tree-ssanames.cc
+++ b/gcc/tree-ssanames.cc
@@ -94,7 +94,7 @@ range_info_fits_p (tree name, const vrange &r)
 /* Allocate a new global range for NAME and set it to R.  Return the
    allocation slot.  */
 
-inline void *
+inline vrange_storage *
 range_info_alloc (tree name, const vrange &r)
 {
   vrange_storage *mem = ggc_alloc_vrange_storage (r);
@@ -111,6 +111,18 @@ range_info_free (tree name)
   ggc_free (mem);
 }
 
+/* Return the aliasing information for ssa-name NAME, or NULL if there
+   is none.  Return NULL for non pointer types.  */
+
+struct ptr_info_def *
+ssa_name_ptr_info (const_tree name)
+{
+  vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
+  if (!mem || mem->m_discriminator != VR_PRANGE)
+    return NULL;
+  return static_cast <prange_storage *> (mem)->ptr_info;
+}
+
 /* Return the global range for NAME in R.  */
 
 inline void
@@ -127,10 +139,20 @@ range_info_set_range (tree name, const vrange &r)
 {
   if (!range_info_p (name) || !range_info_fits_p (name, r))
     {
+      // Aliasing info belongs to NAME, not to the prange storage object which
+      // might get reallocated.  ptr_info_def addresses may be cached
+      // elsewhere, so save and copy it back after any reallocation.
+      ptr_info_def *pi = SSA_NAME_PTR_INFO (name);
       if (range_info_p (name))
 	range_info_free (name);
 
-      return range_info_alloc (name, r);
+      vrange_storage *mem = range_info_alloc (name, r);
+      if (pi && mem)
+	{
+	  gcc_checking_assert (mem->m_discriminator == VR_PRANGE);
+	  static_cast <prange_storage *> (mem)->ptr_info = pi;
+	}
+      return mem != NULL;
     }
   else
     {
@@ -398,10 +420,7 @@ make_ssa_name_fn (struct function *fn, tree var, gimple *stmt,
       SET_SSA_NAME_VAR_OR_IDENTIFIER (t, var);
     }
   SSA_NAME_DEF_STMT (t) = stmt;
-  if (POINTER_TYPE_P (TREE_TYPE (t)))
-    SSA_NAME_PTR_INFO (t) = NULL;
-  else
-    SSA_NAME_RANGE_INFO (t) = NULL;
+  SSA_NAME_RANGE_INFO (t) = NULL;
 
   SSA_NAME_IN_FREE_LIST (t) = 0;
   SSA_NAME_IS_DEFAULT_DEF (t) = 0;
@@ -787,6 +806,22 @@ adjust_ptr_info_misalignment (struct ptr_info_def *pi, poly_uint64 increment)
     }
 }
 
+/* Make PI the aliasing information for NAME.  Allocate a VARYING range if
+   NAME does not already have a range.  */
+
+static void
+set_ssa_name_ptr_info (tree name, struct ptr_info_def *pi)
+{
+  gcc_checking_assert (POINTER_TYPE_P (TREE_TYPE (name)));
+
+  if (!range_info_p (name))
+    SSA_NAME_RANGE_INFO (name) = ggc_alloc_vrange_storage (TREE_TYPE (name));
+
+  vrange_storage *mem = SSA_NAME_RANGE_INFO (name);
+  gcc_checking_assert (mem && mem->m_discriminator == VR_PRANGE);
+  static_cast <prange_storage *> (mem)->ptr_info = pi;
+}
+
 /* Return the alias information associated with pointer T.  It creates a
    new instance if none existed.  */
 
@@ -803,7 +838,7 @@ get_ptr_info (tree t)
       pi = ggc_cleared_alloc<ptr_info_def> ();
       pt_solution_reset (&pi->pt);
       mark_ptr_info_alignment_unknown (pi);
-      SSA_NAME_PTR_INFO (t) = pi;
+      set_ssa_name_ptr_info (t, pi);
     }
 
   return pi;
@@ -847,7 +882,7 @@ duplicate_ssa_name_ptr_info (tree name, struct ptr_info_def *ptr_info)
   new_ptr_info = ggc_alloc<ptr_info_def> ();
   *new_ptr_info = *ptr_info;
 
-  SSA_NAME_PTR_INFO (name) = new_ptr_info;
+  set_ssa_name_ptr_info (name, new_ptr_info);
 }
 
 void
diff --git a/gcc/tree-ssanames.h b/gcc/tree-ssanames.h
index a7b564dd4e9..ea38a3bc3c5 100644
--- a/gcc/tree-ssanames.h
+++ b/gcc/tree-ssanames.h
@@ -45,6 +45,9 @@ struct GTY(()) ptr_info_def
   unsigned int misalign;
 };
 
+/* Aliasing attributes for SSA_NAMEs for pointer-type variables.  */
+struct ptr_info_def *ssa_name_ptr_info (const_tree name);
+#define SSA_NAME_PTR_INFO(N) (ssa_name_ptr_info (N))
 
 #define SSANAMES(fun) (fun)->gimple_df->ssa_names
 #define DEFAULT_DEFS(fun) (fun)->gimple_df->default_defs
diff --git a/gcc/tree.h b/gcc/tree.h
index fb82b1750dd..73c7d25a22c 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -2322,13 +2322,9 @@ class auto_suppress_location_wrappers
 #define SSA_NAME_POINTS_TO_READONLY_MEMORY(NODE) \
     SSA_NAME_CHECK (NODE)->base.deprecated_flag
 
-/* Attributes for SSA_NAMEs for pointer-type variables.  */
-#define SSA_NAME_PTR_INFO(N) \
-   SSA_NAME_CHECK (N)->ssa_name.info.ptr_info
-
-/* Value range info attributes for SSA_NAMEs of non pointer-type variables.  */
-#define SSA_NAME_RANGE_INFO(N) \
-    SSA_NAME_CHECK (N)->ssa_name.info.range_info
+/* Value range info for an SSA_NAME, of any type.  */
+#define SSA_NAME_RANGE_INFO(N)  \
+    SSA_NAME_CHECK (N)->ssa_name.range_info
 
 /* Return the immediate_use information for an SSA_NAME. */
 #define SSA_NAME_IMM_USE_NODE(NODE) SSA_NAME_CHECK (NODE)->ssa_name.imm_uses
diff --git a/gcc/value-range-storage.cc b/gcc/value-range-storage.cc
index 08c85a8880b..b121ffac754 100644
--- a/gcc/value-range-storage.cc
+++ b/gcc/value-range-storage.cc
@@ -638,6 +638,10 @@ prange_storage::alloc (vrange_internal_alloc &allocator, const prange &r,
 
 prange_storage::prange_storage (const prange &r) : vrange_storage (VR_PRANGE)
 {
+  // Aliasing info is not part of the range, so it is never copied from one.
+  // Only set_ssa_name_ptr_info() in tree-ssanames.cc ever makes this non-null.
+  ptr_info = NULL;
+
   unsigned num_words;
   enum prange_kind kind = prange_format (r, num_words);
   unsigned short prec = (kind == PR_UNDEFINED) ? 0 : TYPE_PRECISION (r.type ());
diff --git a/gcc/value-range-storage.h b/gcc/value-range-storage.h
index 6c2838eecc8..9eb03fc8e73 100644
--- a/gcc/value-range-storage.h
+++ b/gcc/value-range-storage.h
@@ -41,10 +41,6 @@ private:
 };
 
 // Efficient memory storage for a vrange.
-//
-// The GTY marker here does nothing but get gengtype to generate the
-// ggc_test_and_set_mark calls.  We ignore the derived classes, since
-// they don't contain any pointers.
 
 class GTY((desc ("%h.m_discriminator"), tag("VR_UNKNOWN"))) vrange_storage
 {
@@ -130,6 +126,9 @@ public:
   bool fits_p (const prange &r) const;
   void dump () const;
 
+  // Pointer aliasing information.  Managed seperately via SSA_NAME_PTR_INFO.
+  struct ptr_info_def *ptr_info;
+
 private:
   DISABLE_COPY_AND_ASSIGN (prange_storage);
   prange_storage (const prange &r);
-- 
2.56.0

Reply via email to