OSDN Git Service

* ipa-cp.c (ipa_get_indirect_edge_target): Add typecasts when
[pf3gnuchains/gcc-fork.git] / gcc / ipa-cp.c
index 10e1834..8a4f992 100644 (file)
@@ -1112,7 +1112,8 @@ ipa_get_indirect_edge_target (struct cgraph_edge *ie,
 
   if (!ie->indirect_info->polymorphic)
     {
-      tree t = VEC_index (tree, known_vals, param_index);
+      tree t = (VEC_length (tree, known_vals) > (unsigned int) param_index
+               ? VEC_index (tree, known_vals, param_index) : NULL);
       if (t &&
          TREE_CODE (t) == ADDR_EXPR
          && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
@@ -1126,7 +1127,8 @@ ipa_get_indirect_edge_target (struct cgraph_edge *ie,
   otr_type = ie->indirect_info->otr_type;
 
   t = VEC_index (tree, known_vals, param_index);
-  if (!t && known_binfos)
+  if (!t && known_binfos
+      && VEC_length (tree, known_binfos) > (unsigned int) param_index)
     t = VEC_index (tree, known_binfos, param_index);
   if (!t)
     return NULL_TREE;
@@ -1210,19 +1212,19 @@ good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
       || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
     return false;
 
-  gcc_checking_assert (size_cost >= 0);
+  gcc_assert (size_cost > 0);
 
-  /* FIXME:  These decisions need tuning.  */
   if (max_count)
     {
-      int evaluation, factor = (count_sum * 1000) / max_count;
-
-      evaluation = (time_benefit * factor) / size_cost;
+      int factor = (count_sum * 1000) / max_count;
+      HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * factor)
+                                   / size_cost);
 
       if (dump_file && (dump_flags & TDF_DETAILS))
        fprintf (dump_file, "     good_cloning_opportunity_p (time: %i, "
                 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
-                ") -> evaluation: %i, threshold: %i\n",
+                ") -> evaluation: " HOST_WIDEST_INT_PRINT_DEC
+                ", threshold: %i\n",
                 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
                 evaluation, 500);
 
@@ -1230,11 +1232,13 @@ good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
     }
   else
     {
-      int evaluation = (time_benefit * freq_sum) / size_cost;
+      HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * freq_sum)
+                                   / size_cost);
 
       if (dump_file && (dump_flags & TDF_DETAILS))
        fprintf (dump_file, "     good_cloning_opportunity_p (time: %i, "
-                "size: %i, freq_sum: %i) -> evaluation: %i, threshold: %i\n",
+                "size: %i, freq_sum: %i) -> evaluation: "
+                HOST_WIDEST_INT_PRINT_DEC ", threshold: %i\n",
                 time_benefit, size_cost, freq_sum, evaluation,
                 CGRAPH_FREQ_BASE /2);
 
@@ -1407,6 +1411,14 @@ estimate_local_effects (struct cgraph_node *node)
            + devirtualization_time_bonus (node, known_csts, known_binfos)
            + removable_params_cost + emc;
 
+         gcc_checking_assert (size >=0);
+         /* The inliner-heuristics based estimates may think that in certain
+            contexts some functions do not have any size at all but we want
+            all specializations to have at least a tiny cost, not least not to
+            divide by zero.  */
+         if (size == 0)
+           size = 1;
+
          if (dump_file && (dump_flags & TDF_DETAILS))
            {
              fprintf (dump_file, " - estimates for value ");
@@ -1561,6 +1573,20 @@ propagate_constants_topo (struct topo_info *topo)
     }
 }
 
+
+/* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
+   the bigger one if otherwise.  */
+
+static int
+safe_add (int a, int b)
+{
+  if (a > INT_MAX/2 || b > INT_MAX/2)
+    return a > b ? a : b;
+  else
+    return a + b;
+}
+
+
 /* Propagate the estimated effects of individual values along the topological
    from the dependant values to those they depend on.  */
 
@@ -1577,8 +1603,9 @@ propagate_effects (void)
 
       for (val = base; val; val = val->scc_next)
        {
-         time += val->local_time_benefit + val->prop_time_benefit;
-         size += val->local_size_cost + val->prop_size_cost;
+         time = safe_add (time,
+                          val->local_time_benefit + val->prop_time_benefit);
+         size = safe_add (size, val->local_size_cost + val->prop_size_cost);
        }
 
       for (val = base; val; val = val->scc_next)
@@ -1586,8 +1613,10 @@ propagate_effects (void)
          if (src->val
              && cgraph_maybe_hot_edge_p (src->cs))
            {
-             src->val->prop_time_benefit += time;
-             src->val->prop_size_cost += size;
+             src->val->prop_time_benefit = safe_add (time,
+                                               src->val->prop_time_benefit);
+             src->val->prop_size_cost = safe_add (size,
+                                                  src->val->prop_size_cost);
            }
     }
 }