]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/ipa-cp.c
c++: Handle multiple aggregate overloads [PR95319].
[thirdparty/gcc.git] / gcc / ipa-cp.c
index e762abb896dfe59e1617f1f9b860cb4e7dd258e9..c64e9104a94411891a82367dc79cec2d9313fcd3 100644 (file)
@@ -1352,11 +1352,14 @@ ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
   gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
   if (TREE_CODE (input) == ADDR_EXPR)
     {
-      tree t = TREE_OPERAND (input, 0);
-      t = build_ref_for_offset (EXPR_LOCATION (t), t,
-                               ipa_get_jf_ancestor_offset (jfunc), false,
-                               ptr_type_node, NULL, false);
-      return build_fold_addr_expr (t);
+      gcc_checking_assert (is_gimple_ip_invariant_address (input));
+      poly_int64 off = ipa_get_jf_ancestor_offset (jfunc);
+      if (known_eq (off, 0))
+       return input;
+      poly_int64 byte_offset = exact_div (off, BITS_PER_UNIT);
+      return build1 (ADDR_EXPR, TREE_TYPE (input),
+                    fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (input)), input,
+                                 build_int_cst (ptr_type_node, byte_offset)));
     }
   else
     return NULL_TREE;
@@ -1850,7 +1853,7 @@ ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
          {
            ipcp_value_source<valtype> *s;
            for (s = val->sources; s; s = s->next)
-             if (s->cs == cs)
+             if (s->cs == cs && s->val == src_val)
                break;
            if (s)
              return false;
@@ -1897,8 +1900,8 @@ ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
 }
 
 /* Return true, if a ipcp_value VAL is orginated from parameter value of
-   self-feeding recursive function by applying non-passthrough arithmetic
-   transformation.  */
+   self-feeding recursive function via some kind of pass-through jump
+   function.  */
 
 static bool
 self_recursively_generated_p (ipcp_value<tree> *val)
@@ -1909,10 +1912,12 @@ self_recursively_generated_p (ipcp_value<tree> *val)
     {
       cgraph_edge *cs = src->cs;
 
-      if (!src->val || cs->caller != cs->callee->function_symbol ()
-         || src->val == val)
+      if (!src->val || cs->caller != cs->callee->function_symbol ())
        return false;
 
+      if (src->val == val)
+       continue;
+
       if (!info)
        info = IPA_NODE_REF (cs->caller);
 
@@ -4033,15 +4038,25 @@ edge_clone_summary_t::duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
   src_data->next_clone = dst_edge;
 }
 
-/* Return true is NODE is DEST or its clone for all contexts.  */
+/* Return true is CS calls DEST or its clone for all contexts.  When
+   ALLOW_RECURSION_TO_CLONE is false, also return false for self-recursive
+   edges from/to an all-context clone.  */
 
 static bool
-same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest)
+calls_same_node_or_its_all_contexts_clone_p (cgraph_edge *cs, cgraph_node *dest,
+                                            bool allow_recursion_to_clone)
 {
-  if (node == dest)
+  enum availability availability;
+  cgraph_node *callee = cs->callee->function_symbol (&availability);
+
+  if (availability <= AVAIL_INTERPOSABLE)
+    return false;
+  if (callee == dest)
     return true;
+  if (!allow_recursion_to_clone && cs->caller == callee)
+    return false;
 
-  class ipa_node_params *info = IPA_NODE_REF (node);
+  class ipa_node_params *info = IPA_NODE_REF (callee);
   return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
 }
 
@@ -4053,11 +4068,8 @@ cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
                            cgraph_node *dest, ipcp_value<tree> *dest_val)
 {
   class ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
-  enum availability availability;
-  cgraph_node *real_dest = cs->callee->function_symbol (&availability);
 
-  if (availability <= AVAIL_INTERPOSABLE
-      || !same_node_or_its_all_contexts_clone_p (real_dest, dest)
+  if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, !src->val)
       || caller_info->node_dead)
     return false;
 
@@ -4076,9 +4088,6 @@ cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
     }
   else
     {
-      /* At the moment we do not propagate over arithmetic jump functions in
-        SCCs, so it is safe to detect self-feeding recursive calls in this
-        way.  */
       if (src->val == dest_val)
        return true;
 
@@ -4113,11 +4122,8 @@ cgraph_edge_brings_value_p (cgraph_edge *cs,
                            ipcp_value<ipa_polymorphic_call_context> *)
 {
   class ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
-  enum availability avail;
-  cgraph_node *real_dest = cs->callee->function_symbol (&avail);
 
-  if (avail <= AVAIL_INTERPOSABLE
-      || !same_node_or_its_all_contexts_clone_p (real_dest, dest)
+  if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, true)
       || caller_info->node_dead)
     return false;
   if (!src->val)
@@ -4204,6 +4210,33 @@ get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
   return hot;
 }
 
+/* Given a NODE, and a set of its CALLERS, try to adjust order of the callers
+   to let a non-self-recursive caller be the first element.  Thus, we can
+   simplify intersecting operations on values that arrive from all of these
+   callers, especially when there exists self-recursive call.  Return true if
+   this kind of adjustment is possible.  */
+
+static bool
+adjust_callers_for_value_intersection (vec<cgraph_edge *> callers,
+                                      cgraph_node *node)
+{
+  for (unsigned i = 0; i < callers.length (); i++)
+    {
+      cgraph_edge *cs = callers[i];
+
+      if (cs->caller != node)
+       {
+         if (i > 0)
+           {
+             callers[i] = callers[0];
+             callers[0] = cs;
+           }
+         return true;
+       }
+    }
+  return false;
+}
+
 /* Return a vector of incoming edges that do bring value VAL to node DEST.  It
    is assumed their number is known and equal to CALLER_COUNT.  */
 
@@ -4227,6 +4260,9 @@ gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
        }
     }
 
+  if (caller_count > 1)
+    adjust_callers_for_value_intersection (ret, dest);
+
   return ret;
 }
 
@@ -4238,7 +4274,6 @@ get_replacement_map (class ipa_node_params *info, tree value, int parm_num)
 {
   struct ipa_replace_map *replace_map;
 
-
   replace_map = ggc_alloc<ipa_replace_map> ();
   if (dump_file)
     {
@@ -4588,37 +4623,47 @@ create_specialized_node (struct cgraph_node *node,
   return new_node;
 }
 
-/* Return true, if JFUNC, which describes a i-th parameter of call CS, is a
-   simple no-operation pass-through function to itself.  */
+/* Return true if JFUNC, which describes a i-th parameter of call CS, is a
+   pass-through function to itself when the cgraph_node involved is not an
+   IPA-CP clone.  When SIMPLE is true, further check if JFUNC is a simple
+   no-operation pass-through.  */
 
 static bool
-self_recursive_pass_through_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i)
+self_recursive_pass_through_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i,
+                              bool simple = true)
 {
   enum availability availability;
   if (cs->caller == cs->callee->function_symbol (&availability)
       && availability > AVAIL_INTERPOSABLE
       && jfunc->type == IPA_JF_PASS_THROUGH
-      && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR
-      && ipa_get_jf_pass_through_formal_id (jfunc) == i)
+      && (!simple || ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
+      && ipa_get_jf_pass_through_formal_id (jfunc) == i
+      && IPA_NODE_REF (cs->caller)
+      && !IPA_NODE_REF (cs->caller)->ipcp_orig_node)
     return true;
   return false;
 }
 
-/* Return true, if JFUNC, which describes a part of an aggregate represented
-   or pointed to by the i-th parameter of call CS, is a simple no-operation
-   pass-through function to itself.  */
+/* Return true if JFUNC, which describes a part of an aggregate represented or
+   pointed to by the i-th parameter of call CS, is a pass-through function to
+   itself when the cgraph_node involved is not an IPA-CP clone..  When
+   SIMPLE is true, further check if JFUNC is a simple no-operation
+   pass-through.  */
 
 static bool
 self_recursive_agg_pass_through_p (cgraph_edge *cs, ipa_agg_jf_item *jfunc,
-                                  int i)
+                                  int i, bool simple = true)
 {
   enum availability availability;
   if (cs->caller == cs->callee->function_symbol (&availability)
       && availability > AVAIL_INTERPOSABLE
       && jfunc->jftype == IPA_JF_LOAD_AGG
       && jfunc->offset == jfunc->value.load_agg.offset
-      && jfunc->value.pass_through.operation == NOP_EXPR
-      && jfunc->value.pass_through.formal_id == i)
+      && (!simple || jfunc->value.pass_through.operation == NOP_EXPR)
+      && jfunc->value.pass_through.formal_id == i
+      && useless_type_conversion_p (jfunc->value.load_agg.type, jfunc->type)
+      && IPA_NODE_REF (cs->caller)
+      && !IPA_NODE_REF (cs->caller)->ipcp_orig_node)
     return true;
   return false;
 }
@@ -4650,9 +4695,6 @@ find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
          struct ipa_jump_func *jump_func;
          tree t;
 
-         if (IPA_NODE_REF (cs->caller) && IPA_NODE_REF (cs->caller)->node_dead)
-           continue;
-
          if (!IPA_EDGE_REF (cs)
              || i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
              || (i == 0
@@ -4662,10 +4704,30 @@ find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
              break;
            }
          jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
-         if (self_recursive_pass_through_p (cs, jump_func, i))
-           continue;
 
-         t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func, type);
+         /* Besides simple pass-through jump function, arithmetic jump
+            function could also introduce argument-direct-pass-through for
+            self-feeding recursive call.  For example,
+
+               fn (int i)
+               {
+                 fn (i & 1);
+               }
+
+            Given that i is 0, recursive propagation via (i & 1) also gets
+            0.  */
+         if (self_recursive_pass_through_p (cs, jump_func, i, false))
+           {
+             gcc_assert (newval);
+             t = ipa_get_jf_arith_result (
+                               ipa_get_jf_pass_through_operation (jump_func),
+                               newval,
+                               ipa_get_jf_pass_through_operand (jump_func),
+                               type);
+           }
+         else
+           t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func,
+                                     type);
          if (!t
              || (newval
                  && !values_equal_for_ipcp_p (t, newval))
@@ -4814,19 +4876,12 @@ intersect_with_plats (class ipcp_param_lattices *plats,
            break;
          if (aglat->offset - offset == item->offset)
            {
-             gcc_checking_assert (item->value);
              if (aglat->is_single_const ())
                {
                  tree value = aglat->values->value;
 
                  if (values_equal_for_ipcp_p (item->value, value))
                    found = true;
-                 else if (item->value == error_mark_node)
-                   {
-                     /* Replace unknown place holder value with real one.  */
-                     item->value = value;
-                     found = true;
-                   }
                }
              break;
            }
@@ -4895,12 +4950,6 @@ intersect_with_agg_replacements (struct cgraph_node *node, int index,
            {
              if (values_equal_for_ipcp_p (item->value, av->value))
                found = true;
-             else if (item->value == error_mark_node)
-               {
-                 /* Replace place holder value with real one.  */
-                 item->value = av->value;
-                 found = true;
-               }
              break;
            }
        }
@@ -5005,31 +5054,16 @@ intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
        for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
          {
            struct ipa_agg_jf_item *agg_item = &(*jfunc->agg.items)[i];
-           struct ipa_agg_value agg_value;
-
-           if (self_recursive_agg_pass_through_p (cs, agg_item, index))
+           tree value = ipa_agg_value_from_node (caller_info, cs->caller,
+                                                 agg_item);
+           if (value)
              {
-               /* For a self-recursive call, if aggregate jump function is a
-                  simple pass-through, the exact value that it stands for is
-                  not known at this point, which must comes from other call
-                  sites.  But we still need to add a place holder in value
-                  sets to indicate it, here we use error_mark_node to
-                  represent the special unknown value, which will be replaced
-                  with real one during later intersecting operations.  */
-               agg_value.value = error_mark_node;
-             }
-           else
-             {
-               tree value = ipa_agg_value_from_node (caller_info, cs->caller,
-                                                     agg_item);
-               if (!value)
-                 continue;
+               struct ipa_agg_value agg_value;
 
                agg_value.value = value;
+               agg_value.offset = agg_item->offset;
+               inter.safe_push (agg_value);
              }
-
-           agg_value.offset = agg_item->offset;
-           inter.safe_push (agg_value);
          }
       else
        FOR_EACH_VEC_ELT (inter, k, item)
@@ -5050,25 +5084,32 @@ intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
                  {
                    tree value;
 
-                   if (self_recursive_agg_pass_through_p (cs, ti, index))
-                     {
-                       /* A simple aggregate pass-through in self-recursive
-                          call should lead to same value.  */
-                       found = true;
-                     }
-                   else if ((value = ipa_agg_value_from_node (caller_info,
-                                                            cs->caller, ti)))
-                     {
-                       if (values_equal_for_ipcp_p (item->value, value))
-                         found = true;
-                       else if (item->value == error_mark_node)
-                         {
-                           /* Replace unknown place holder value with real
-                              one.  */
-                           item->value = value;
-                           found = true;
-                         }
-                     }
+                   /* Besides simple pass-through aggregate jump function,
+                      arithmetic aggregate jump function could also bring
+                      same aggregate value as parameter passed-in for
+                      self-feeding recursive call.  For example,
+
+                        fn (int *i)
+                          {
+                            int j = *i & 1;
+                            fn (&j);
+                          }
+
+                      Given that *i is 0, recursive propagation via (*i & 1)
+                      also gets 0.  */
+                   if (self_recursive_agg_pass_through_p (cs, ti, index,
+                                                          false))
+                     value = ipa_get_jf_arith_result (
+                                       ti->value.pass_through.operation,
+                                       item->value,
+                                       ti->value.pass_through.operand,
+                                       ti->type);
+                   else
+                     value = ipa_agg_value_from_node (caller_info,
+                                                      cs->caller, ti);
+
+                   if (value && values_equal_for_ipcp_p (item->value, value))
+                     found = true;
                    break;
                  }
                l++;
@@ -5144,9 +5185,6 @@ find_aggregate_values_for_callers_subset (struct cgraph_node *node,
          if (!item->value)
            continue;
 
-         /* All values must be real values, not unknown place holders.  */
-         gcc_assert (item->value != error_mark_node);
-
          v = ggc_alloc<ipa_agg_replacement_value> ();
          v->index = i;
          v->offset = item->offset;
@@ -5542,13 +5580,33 @@ decide_whether_version_node (struct cgraph_node *node)
   if (info->do_clone_for_all_contexts)
     {
       struct cgraph_node *clone;
-      vec<cgraph_edge *> callers;
+      vec<cgraph_edge *> callers = node->collect_callers ();
+
+      for (int i = callers.length () - 1; i >= 0; i--)
+       {
+         cgraph_edge *cs = callers[i];
+         class ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
+
+         if (caller_info && caller_info->node_dead)
+           callers.unordered_remove (i);
+       }
+
+      if (!adjust_callers_for_value_intersection (callers, node))
+       {
+         /* If node is not called by anyone, or all its caller edges are
+            self-recursive, the node is not really be in use, no need to
+            do cloning.  */
+         callers.release ();
+         known_csts.release ();
+         known_contexts.release ();
+         info->do_clone_for_all_contexts = false;
+         return ret;
+       }
 
       if (dump_file)
        fprintf (dump_file, " - Creating a specialized node of %s "
                 "for all known contexts.\n", node->dump_name ());
 
-      callers = node->collect_callers ();
       find_more_scalar_values_for_callers_subset (node, known_csts, callers);
       find_more_contexts_for_caller_subset (node, &known_contexts, callers);
       ipa_agg_replacement_value *aggvals