On 11/9/26 5:56 AM, Richard Biener wrote:
> On Fri, Nov 6, 2026 at 10:18 PM Andrew MacLeod <[email protected]> wrote:
>>
>> IPA currently tries to manually fix up function local addresses found
>> in a prange in two different locations.
>>
>> This patch provides a method for the range to remove such addresses, and
>> moves this removal to a central place (ipa_get_value_range).  Then removes
>> the special code from the other two locations.
>>
>> Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK?
> 
> https://gcc.sashiko.dev/#/patchset/ef49e64f-9909-428b-a11a-e6958129765f%40redhat.com


> diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc
> index 7d73d595907..184da086e65 100644
> --- a/gcc/tree-inline.cc
> +++ b/gcc/tree-inline.cc
[ ... ]
> @@ -275,6 +274,9 @@ remap_ssa_name (tree name, copy_body_data *id)
>        insert_decl_map (id, name, new_tree);
>        SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
>       = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
> +      /* Range info can be transferred, if local addresses are removed.  */
> +      if (SSA_NAME_RANGE_INFO (name))
> +     duplicate_ssa_name_range_info (new_tree, name, true);
>        /* At least IPA points-to info can be directly transferred.  */
>        if (id->src_cfun->gimple_df
>         && id->src_cfun->gimple_df->ipa_pta
> @@ -282,10 +284,6 @@ remap_ssa_name (tree name, copy_body_data *id)
>         && (pt = ssa_name_pt_solution (name))
>         && !pt->anything)
>       set_ssa_name_pt_solution (new_tree, *pt);
> -      /* So can range-info.  */
> -      if (!POINTER_TYPE_P (TREE_TYPE (name))
> -       && SSA_NAME_RANGE_INFO (name))
> -     duplicate_ssa_name_range_info (new_tree, name);
>        if (SSA_NAME_IS_DEFAULT_DEF (name))

> In remap_ssa_name in gcc/tree-inline.cc, SSA_NAME_RANGE_INFO (now including
> prange) is copied onto new_tree before checking SSA_NAME_IS_DEFAULT_DEF
> (name). Immediately below, if name is an uninitialized local default-def
> SSA_NAME with SSA_NAME_OCCURS_IN_ABNORMAL_PHI set (such as across
> transactional memory restart edges under -fgnu-tm or OpenMP/OpenACC abnormal
> edges) and is inlined into a basic block other than the single-predecessor
> entry successor, remap_ssa_name emits gimple_build_assign (new_tree, zero) to
> initialize new_tree to 0B. Note that this already affected integral ranges
> before this patch, and now extends to pointer ranges as well: if name
> acquired a non-null prange (for example [1B, +INF] from early VRP or
> set_ptr_nonnull), new_tree is defined as 0B while retaining global range
> info that excludes 0B, which can lead to wrong-code miscompilation. Should
> reset_flow_sensitive_info (new_tree) be called (or the range copy skipped)
> when emitting the zero-initialization assignment?


Sure, we can just do the copies afterwards, and not do the range work if a zero 
initialization is issued.  And pt_solution can still always be copied.

This must have existed with pointers before with nonzero ranges as they would 
have been copied by the pt_solution copy previously...

regardless, we can fix it all here.


> diff --git a/gcc/tree-ssanames.cc b/gcc/tree-ssanames.cc
> index b97738834f1..0411d2a5a75 100644
> --- a/gcc/tree-ssanames.cc
> +++ b/gcc/tree-ssanames.cc
[ ... ]
> @@ -998,6 +1002,10 @@ duplicate_ssa_name_range_info (tree name, tree src)
>  
>    value_range src_range (TREE_TYPE (src));
>    range_info_get_range (src, src_range);
> +
> +  /* Remove any local addresses from the range if present.  */
> +  if (remove_local_addr && is_a<prange> (src_range))
> +    as_a<prange> (src_range).clear_function_local_addr ();
>    range_info_set_range (name, src_range);
> In duplicate_ssa_name_range_info in gcc/tree-ssanames.cc (called from
> remap_ssa_name in gcc/tree-inline.cc), prange::clear_function_local_addr in
> gcc/value-range.cc checks !is_gimple_ip_invariant (m_addr) to strip
> function-local addresses. However, for a GNU C address-of-label expression
> (&&label), decl_address_ip_invariant_p in gcc/tree.cc unconditionally returns
> true for LABEL_DECL, even though copy_tree_body_r and copy_bb in
> gcc/tree-inline.cc remap each function-local LABEL_DECL to a new per-instance
> LABEL_DECL via remap_decl during inlining or cloning. When a function with a
> pointer SSA_NAME whose prange records m_addr == &label (after early VRP) is
> inlined, new_tree retains the unremapped LABEL_DECL from src_fn in its
> prange:
> 1. If the function is inlined multiple times into the same caller, the
>   remapped pointer SSA_NAMEs from distinct call sites both carry the same
>   m_addr (&src_label) despite pointing to distinct remapped labels at
>   runtime, causing operator_equal::fold_range (via addr_same_known_p) to>>   
> fold their equality comparison to true and
>  operator_pointer_diff::fold_range to fold their difference to 0
>   (wrong-code).
> 2. Downstream rvrp_folder::value_of_expr and value_on_edge in
>   gcc/tree-vrp.cc can substitute p.addr_known () (&src_label, whose
>   DECL_CONTEXT is still src_fn) directly into dst_fn's GIMPLE IR, causing a
>   GIMPLE verification ICE or wrong-code.
> prange::clear_function_local_addr (or duplicate_ssa_name_range_info) needs to
> treat function-local LABEL_DECL addresses as local and clear (or remap) them.


set_addr requires get_inner_reference_aff () to produce a base with a constant 
size as well as a constant offset, and a LABEL_DECL has no size. So we should 
never see a label pointed to.    At least at the moment.   Doesn't mean 
something wont change in the future, so there is no harm in having the clear 
routine also check for labels.

How about this, Im testing the following.   Is the 3 patch set OK if it 
bootstraps on x86_64-pc-linux-gnu with no regressions?
(Im also doing a regression run on aarch64 as well since an autotester whined 
about something there)

Andrew


Inlining previously ignored pointer ranges.  This patch allows them to
be inlined just like a normal range.  Any function local address
referred to in the range will be removed making it safe.

This restores any information that was lost when it was carried in a
ptr_info_def. This includes the alignment (now from the bitmask) and the
non-null property (now carried by the range).

        * tree-inline.cc (remap_ssa_name): Drop local addresses from a
        prange, and inline pointers.
        * tree-ssanames.cc (duplicate_ssa_name_range_info): Remove
        function local addresses if requested.
        * tree-ssanames.h (duplicate_ssa_name_range_info): Adjust prototype.
        * value-range.cc (prange::clear_function_local_addr): Remove
        LABEL_DECLs as well.
---
 gcc/tree-inline.cc   | 33 +++++++++++++++++----------------
 gcc/tree-ssanames.cc | 12 ++++++++++--
 gcc/tree-ssanames.h  |  3 ++-
 gcc/value-range.cc   |  8 +++++++-
 4 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc
index 7d73d595907..6a3ba7b8483 100644
--- a/gcc/tree-inline.cc
+++ b/gcc/tree-inline.cc
@@ -241,6 +241,9 @@ remap_ssa_name (tree name, copy_body_data *id)
       insert_decl_map (id, name, new_tree);
       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
        = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
+      /* Range info can be transferred, if local addresses are removed.  */
+      if (SSA_NAME_RANGE_INFO (name))
+       duplicate_ssa_name_range_info (new_tree, name, true);
       /* At least IPA points-to info can be directly transferred.  */
       if (id->src_cfun->gimple_df
          && id->src_cfun->gimple_df->ipa_pta
@@ -248,10 +251,6 @@ remap_ssa_name (tree name, copy_body_data *id)
          && (pt = ssa_name_pt_solution (name))
          && !pt->anything)
        set_ssa_name_pt_solution (new_tree, *pt);
-      /* So can range-info.  */
-      if (!POINTER_TYPE_P (TREE_TYPE (name))
-         && SSA_NAME_RANGE_INFO (name))
-       duplicate_ssa_name_range_info (new_tree, name);
       return new_tree;
     }
 
@@ -275,17 +274,8 @@ remap_ssa_name (tree name, copy_body_data *id)
       insert_decl_map (id, name, new_tree);
       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
        = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
-      /* At least IPA points-to info can be directly transferred.  */
-      if (id->src_cfun->gimple_df
-         && id->src_cfun->gimple_df->ipa_pta
-         && POINTER_TYPE_P (TREE_TYPE (name))
-         && (pt = ssa_name_pt_solution (name))
-         && !pt->anything)
-       set_ssa_name_pt_solution (new_tree, *pt);
-      /* So can range-info.  */
-      if (!POINTER_TYPE_P (TREE_TYPE (name))
-         && SSA_NAME_RANGE_INFO (name))
-       duplicate_ssa_name_range_info (new_tree, name);
+
+      tree zero = NULL_TREE;
       if (SSA_NAME_IS_DEFAULT_DEF (name))
        {
          /* By inlining function having uninitialized variable, we might
@@ -307,7 +297,7 @@ remap_ssa_name (tree name, copy_body_data *id)
            {
              gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
              gimple *init_stmt;
-             tree zero = build_zero_cst (TREE_TYPE (new_tree));
+             zero = build_zero_cst (TREE_TYPE (new_tree));
 
              init_stmt = gimple_build_assign (new_tree, zero);
              gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
@@ -319,6 +309,17 @@ remap_ssa_name (tree name, copy_body_data *id)
              set_ssa_default_def (cfun, SSA_NAME_VAR (new_tree), new_tree);
            }
        }
+      /* Range info can be transferred if local addresses are removed
+        and no uninitialized name was processed and set to zero.  */
+      if (!zero && SSA_NAME_RANGE_INFO (name))
+       duplicate_ssa_name_range_info (new_tree, name, true);
+      /* At least IPA points-to info can be directly transferred.  */
+      if (id->src_cfun->gimple_df
+         && id->src_cfun->gimple_df->ipa_pta
+         && POINTER_TYPE_P (TREE_TYPE (name))
+         && (pt = ssa_name_pt_solution (name))
+         && !pt->anything)
+       set_ssa_name_pt_solution (new_tree, *pt);
     }
   else
     insert_decl_map (id, name, new_tree);
diff --git a/gcc/tree-ssanames.cc b/gcc/tree-ssanames.cc
index b97738834f1..77d8a9af601 100644
--- a/gcc/tree-ssanames.cc
+++ b/gcc/tree-ssanames.cc
@@ -986,10 +986,14 @@ duplicate_ssa_name_pt_solution (tree dest, tree src)
     duplicate_ssa_name_pt_solution (dest, pt);
 }
 
-/* Copy any global range information from SRC to NAME.  */
+/* Copy any global range information from SRC to NAME.  If REMOVE_LOCAL_ADDR
+   is true, then any specified address that a prange may refer to is checked
+   to see if it's local to the function and removed if so.
+   The default is FALSE, which is the previous default behaviour and copies
+   the entire range as is.  */
 
 void
-duplicate_ssa_name_range_info (tree name, tree src)
+duplicate_ssa_name_range_info (tree name, tree src, bool remove_local_addr)
 {
   gcc_checking_assert (!SSA_NAME_RANGE_INFO (name));
 
@@ -998,6 +1002,10 @@ duplicate_ssa_name_range_info (tree name, tree src)
 
   value_range src_range (TREE_TYPE (src));
   range_info_get_range (src, src_range);
+
+  /* Remove any local addresses from the range if present.  */
+  if (remove_local_addr && is_a <prange> (src_range))
+    as_a <prange> (src_range).clear_function_local_addr ();
   range_info_set_range (name, src_range);
 
   unsigned align, misalign;
diff --git a/gcc/tree-ssanames.h b/gcc/tree-ssanames.h
index 9798df7d04d..7f0b2f1f657 100644
--- a/gcc/tree-ssanames.h
+++ b/gcc/tree-ssanames.h
@@ -88,7 +88,8 @@ extern tree copy_ssa_name_fn (struct function *, tree, gimple 
*);
 extern tree duplicate_ssa_name_fn (struct function *, tree, gimple *);
 extern void duplicate_ssa_name_pt_solution (tree dest, const pt_solution *);
 extern void duplicate_ssa_name_pt_solution (tree dest, tree src);
-extern void duplicate_ssa_name_range_info (tree dest, tree src);
+extern void duplicate_ssa_name_range_info (tree dest, tree src,
+                                          bool remove_local_addr = false);
 extern void maybe_duplicate_ssa_info_at_copy (tree dest, tree src);
 extern void reset_flow_sensitive_info (tree);
 extern void reset_flow_sensitive_info_in_bb (basic_block);
diff --git a/gcc/value-range.cc b/gcc/value-range.cc
index bfc196f8ad2..a7bd9db6bd4 100644
--- a/gcc/value-range.cc
+++ b/gcc/value-range.cc
@@ -585,7 +585,13 @@ prange::set_addr (tree expr, bool known_p)
 bool
 prange::clear_function_local_addr ()
 {
-  if (m_addr && !is_gimple_ip_invariant (m_addr))
+  if (!m_addr)
+    return false;
+
+  /* is_gimple_ip_invariant () accepts the address of a LABEL_DECL, but
+     inlining and cloning could remap it, so remove it as well.  */
+  if (!is_gimple_ip_invariant (m_addr)
+      || TREE_CODE (TREE_OPERAND (m_addr, 0)) == LABEL_DECL)
     {
       set_addr_unknown ();
       return true;
-- 
2.56.0


Reply via email to