https://gcc.gnu.org/g:610d42e09ff8443b8e7e3d69e03e24bd0a4dd0ca

commit r17-6284-g610d42e09ff8443b8e7e3d69e03e24bd0a4dd0ca
Author: Jan Hubicka <[email protected]>
Date:   Sun Nov 15 08:37:52 2026 +0100

    Fold ancestor and arithmetic aggregate jump functions after inlining
    
    When update_jump_functions_after_inlining composes an outgoing jump
    function DST with an incoming constant jump function SRC (IPA_JF_CONST),
    it handles only simple cases:
    
    1) An IPA_JF_ANCESTOR DST fed by an IPA_JF_CONST pointer SRC is reset to
       IPA_JF_UNKNOWN. This can be fixed by computing
       updated value via ipa_get_jf_ancestor_result.
    
    2) An arithmetic IPA_JF_PASS_THROUGH item inside DST->agg.items handles
       only NOP_EXPR, but we can use ipa_get_jf_arith_result.
    
    gcc/ChangeLog:
    
            * ipa-cp.cc (ipa_get_jf_arith_result): Make non-static.
            (ipa_get_jf_ancestor_result): Make non-static.
            * ipa-prop.h (ipa_get_jf_arith_result): Declare.
            (ipa_get_jf_ancestor_result): Declare.
            * ipa-prop.cc (update_jump_functions_after_inlining): Fold
            IPA_JF_CONST inputs into IPA_JF_ANCESTOR jump functions and 
arithmetic
            IPA_JF_PASS_THROUGH aggregate jump-function items.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/ipa/iinline-cstagg-3.c: New test.
            * gcc.dg/ipa/iinline-cstagg-4.c: New test.
            Assisted-by: Google Carbon

Diff:
---
 gcc/ipa-cp.cc                               |   4 +-
 gcc/ipa-prop.cc                             |  46 ++++++++--
 gcc/ipa-prop.h                              |   2 +
 gcc/testsuite/gcc.dg/ipa/iinline-cstagg-3.c | 125 ++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/ipa/iinline-cstagg-4.c |  44 ++++++++++
 5 files changed, 212 insertions(+), 9 deletions(-)

diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index c1fcead4c12e..3eee412b4d0c 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -1581,7 +1581,7 @@ ipacp_value_safe_for_type (tree param_type, tree value)
    NULL_TREE if that cannot be determined or be considered an interprocedural
    invariant.  */
 
-static tree
+tree
 ipa_get_jf_arith_result (enum tree_code opcode, tree input, tree operand,
                         tree res_type)
 {
@@ -1614,7 +1614,7 @@ ipa_get_jf_arith_result (enum tree_code opcode, tree 
input, tree operand,
 /* Return the result of an ancestor jump function JFUNC on the constant value
    INPUT.  Return NULL_TREE if that cannot be determined.  */
 
-static tree
+tree
 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
 {
   gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index 9da18d60742d..61b2dbd5d6bd 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -3953,12 +3953,27 @@ update_jump_functions_after_inlining (struct 
cgraph_edge *cs,
              src = ipa_get_ith_jump_func (top, dst_fid);
              if (src->type == IPA_JF_CONST)
                {
-                 if (item->jftype == IPA_JF_PASS_THROUGH
-                     && item->value.pass_through.operation == NOP_EXPR)
+                 if (item->jftype == IPA_JF_PASS_THROUGH)
                    {
-                     item->jftype = IPA_JF_CONST;
-                     item->value.constant = src->value.constant.value;
-                     continue;
+                     if (item->value.pass_through.operation == NOP_EXPR)
+                       {
+                         item->jftype = IPA_JF_CONST;
+                         item->value.constant = src->value.constant.value;
+                         continue;
+                       }
+                     tree cst = ipa_get_jf_arith_result
+                                  (item->value.pass_through.operation,
+                                   src->value.constant.value,
+                                   item->value.pass_through.operand,
+                                   item->value.pass_through.op_type);
+                     cst = ipacp_value_safe_for_type (item->type, cst);
+                     if (cst)
+                       {
+                         item->jftype = IPA_JF_CONST;
+                         item->value.constant
+                           = unshare_expr_without_location (cst);
+                         continue;
+                       }
                    }
                }
              else if (src->type == IPA_JF_PASS_THROUGH
@@ -4058,8 +4073,25 @@ update_jump_functions_after_inlining (struct cgraph_edge 
*cs,
                item->offset -= dst->value.ancestor.offset;
            }
 
-         if (src->type == IPA_JF_PASS_THROUGH
-             && src->value.pass_through.operation == NOP_EXPR)
+         if (src->type == IPA_JF_CONST)
+           {
+             tree parm_type = ipa_get_callee_param_type (e, i);
+             tree cst = ipa_get_jf_ancestor_result (dst,
+                                                    ipa_get_jf_constant (src));
+             if (cst && parm_type)
+               cst = ipacp_value_safe_for_type (parm_type, cst);
+             if (cst)
+               {
+                 dst->type = IPA_JF_CONST;
+                 dst->value.constant.value
+                   = unshare_expr_without_location (cst);
+                 ipa_zap_jf_refdesc (dst);
+               }
+             else
+               ipa_set_jf_unknown (dst);
+           }
+         else if (src->type == IPA_JF_PASS_THROUGH
+                  && src->value.pass_through.operation == NOP_EXPR)
            {
              dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
              dst->value.ancestor.agg_preserved &=
diff --git a/gcc/ipa-prop.h b/gcc/ipa-prop.h
index 47b93404f9ef..627753ce7e87 100644
--- a/gcc/ipa-prop.h
+++ b/gcc/ipa-prop.h
@@ -1292,6 +1292,8 @@ void ipa_cp_cc_finalize (void);
 bool ipa_return_value_range (value_range &range, tree decl);
 void ipa_record_return_value_range (value_range val);
 bool ipa_jump_functions_equivalent_p (ipa_jump_func *jf1, ipa_jump_func *jf2);
+tree ipa_get_jf_arith_result (enum tree_code, tree, tree, tree);
+tree ipa_get_jf_ancestor_result (struct ipa_jump_func *, tree);
 
 
 #endif /* IPA_PROP_H */
diff --git a/gcc/testsuite/gcc.dg/ipa/iinline-cstagg-3.c 
b/gcc/testsuite/gcc.dg/ipa/iinline-cstagg-3.c
new file mode 100644
index 000000000000..6f5bea7441b7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/iinline-cstagg-3.c
@@ -0,0 +1,125 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -fdump-ipa-inline-details -fno-early-inlining 
-fno-ipa-sra -fno-ipa-cp" } */
+
+typedef struct S
+{
+  int add_offset;
+  int (*call)(int);
+} S;
+
+typedef struct Outer
+{
+  int dummy;
+  S inner;
+} Outer;
+
+extern int slow (int);
+extern void work (int);
+
+static int
+target_anc (int x)
+{
+  return x + 17;
+}
+
+static int
+target_arith (int x)
+{
+  return x * 3;
+}
+
+static const Outer o1 = {1, {32, target_anc}};
+
+static int
+leaf_anc (const S *f, int x)
+{
+  int r = f->call (x);
+  work (r);
+  work (r + 1);
+  work (r + 2);
+  work (r + 3);
+  work (r + 4);
+  return r;
+}
+
+int
+other_anc (const S *f, int x)
+{
+  return leaf_anc (f, x);
+}
+
+static int
+mid_anc (const Outer *p, int x)
+{
+  return leaf_anc (&p->inner, x);
+}
+
+int
+outer_anc (int x)
+{
+  return mid_anc (&o1, x);
+}
+
+static int
+leaf_arith (const S *b, int x)
+{
+  int (*fn)(int) = b->call;
+  if (b->add_offset != 42)
+    {
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+      x = slow (x);
+    }
+  return fn (x);
+}
+
+int
+other_arith (const S *b, int x)
+{
+  return leaf_arith (b, x);
+}
+
+static int
+mid_arith (int a, int x)
+{
+  S q;
+  q.add_offset = a + 5;
+  q.call = target_arith;
+  return leaf_arith (&q, x);
+}
+
+int
+outer_arith (int x)
+{
+  return mid_arith (37, x);
+}
+
+/* { dg-final { scan-ipa-dump "target_anc\[^\\n\]*inline copy in outer_anc" 
"inline" } } */
+/* { dg-final { scan-ipa-dump "target_arith\[^\\n\]*inline copy in 
outer_arith" "inline" } } */
diff --git a/gcc/testsuite/gcc.dg/ipa/iinline-cstagg-4.c 
b/gcc/testsuite/gcc.dg/ipa/iinline-cstagg-4.c
new file mode 100644
index 000000000000..85a14262ac60
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/iinline-cstagg-4.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fdump-ipa-inline-details -fno-early-inlining 
-fno-ipa-sra -fno-ipa-cp" } */
+
+void long_function (void);
+
+struct Outer
+{
+  int pad;
+  char str[4];
+};
+
+static const struct Outer empty_outer = { 1, "" };
+
+int
+ret_a (const char *a)
+{
+  if (a[0])
+    {
+      long_function ();
+      long_function ();
+      long_function ();
+      long_function ();
+      long_function ();
+      long_function ();
+      long_function ();
+      long_function ();
+    }
+  return a[0];
+}
+
+static int
+passthru (const struct Outer *p)
+{
+  long_function ();
+  return ret_a (p->str);
+}
+
+int
+test_os (void)
+{
+  return passthru (&empty_outer);
+}
+
+/* { dg-final { scan-ipa-dump "ret_a\[^\\n\]*inline copy in test_os" "inline" 
} } */

Reply via email to