PR c++/53989
[official-gcc.git] / gcc / ipa-cp.c
blob7ecd9e2a0d7f527ab31694898f14e65de5c31761
1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
6 <mjambor@suse.cz>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Interprocedural constant propagation (IPA-CP).
26 The goal of this transformation is to
28 1) discover functions which are always invoked with some arguments with the
29 same known constant values and modify the functions so that the
30 subsequent optimizations can take advantage of the knowledge, and
32 2) partial specialization - create specialized versions of functions
33 transformed in this way if some parameters are known constants only in
34 certain contexts but the estimated tradeoff between speedup and cost size
35 is deemed good.
37 The algorithm also propagates types and attempts to perform type based
38 devirtualization. Types are propagated much like constants.
40 The algorithm basically consists of three stages. In the first, functions
41 are analyzed one at a time and jump functions are constructed for all known
42 call-sites. In the second phase, the pass propagates information from the
43 jump functions across the call to reveal what values are available at what
44 call sites, performs estimations of effects of known values on functions and
45 their callees, and finally decides what specialized extra versions should be
46 created. In the third, the special versions materialize and appropriate
47 calls are redirected.
49 The algorithm used is to a certain extent based on "Interprocedural Constant
50 Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon,
51 Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D
52 Cooper, Mary W. Hall, and Ken Kennedy.
55 First stage - intraprocedural analysis
56 =======================================
58 This phase computes jump_function and modification flags.
60 A jump function for a call-site represents the values passed as an actual
61 arguments of a given call-site. In principle, there are three types of
62 values:
64 Pass through - the caller's formal parameter is passed as an actual
65 argument, plus an operation on it can be performed.
66 Constant - a constant is passed as an actual argument.
67 Unknown - neither of the above.
69 All jump function types are described in detail in ipa-prop.h, together with
70 the data structures that represent them and methods of accessing them.
72 ipcp_generate_summary() is the main function of the first stage.
74 Second stage - interprocedural analysis
75 ========================================
77 This stage is itself divided into two phases. In the first, we propagate
78 known values over the call graph, in the second, we make cloning decisions.
79 It uses a different algorithm than the original Callahan's paper.
81 First, we traverse the functions topologically from callers to callees and,
82 for each strongly connected component (SCC), we propagate constants
83 according to previously computed jump functions. We also record what known
84 values depend on other known values and estimate local effects. Finally, we
85 propagate cumulative information about these effects from dependent values
86 to those on which they depend.
88 Second, we again traverse the call graph in the same topological order and
89 make clones for functions which we know are called with the same values in
90 all contexts and decide about extra specialized clones of functions just for
91 some contexts - these decisions are based on both local estimates and
92 cumulative estimates propagated from callees.
94 ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
95 third stage.
97 Third phase - materialization of clones, call statement updates.
98 ============================================
100 This stage is currently performed by call graph code (mainly in cgraphunit.c
101 and tree-inline.c) according to instructions inserted to the call graph by
102 the second stage. */
104 #include "config.h"
105 #include "system.h"
106 #include "coretypes.h"
107 #include "tree.h"
108 #include "target.h"
109 #include "gimple.h"
110 #include "cgraph.h"
111 #include "ipa-prop.h"
112 #include "tree-flow.h"
113 #include "tree-pass.h"
114 #include "flags.h"
115 #include "diagnostic.h"
116 #include "tree-pretty-print.h"
117 #include "tree-inline.h"
118 #include "params.h"
119 #include "ipa-inline.h"
120 #include "ipa-utils.h"
122 struct ipcp_value;
124 /* Describes a particular source for an IPA-CP value. */
126 struct ipcp_value_source
128 /* The incoming edge that brought the value. */
129 struct cgraph_edge *cs;
130 /* If the jump function that resulted into his value was a pass-through or an
131 ancestor, this is the ipcp_value of the caller from which the described
132 value has been derived. Otherwise it is NULL. */
133 struct ipcp_value *val;
134 /* Next pointer in a linked list of sources of a value. */
135 struct ipcp_value_source *next;
136 /* If the jump function that resulted into his value was a pass-through or an
137 ancestor, this is the index of the parameter of the caller the jump
138 function references. */
139 int index;
142 /* Describes one particular value stored in struct ipcp_lattice. */
144 struct ipcp_value
146 /* The actual value for the given parameter. This is either an IPA invariant
147 or a TREE_BINFO describing a type that can be used for
148 devirtualization. */
149 tree value;
150 /* The list of sources from which this value originates. */
151 struct ipcp_value_source *sources;
152 /* Next pointers in a linked list of all values in a lattice. */
153 struct ipcp_value *next;
154 /* Next pointers in a linked list of values in a strongly connected component
155 of values. */
156 struct ipcp_value *scc_next;
157 /* Next pointers in a linked list of SCCs of values sorted topologically
158 according their sources. */
159 struct ipcp_value *topo_next;
160 /* A specialized node created for this value, NULL if none has been (so far)
161 created. */
162 struct cgraph_node *spec_node;
163 /* Depth first search number and low link for topological sorting of
164 values. */
165 int dfs, low_link;
166 /* Time benefit and size cost that specializing the function for this value
167 would bring about in this function alone. */
168 int local_time_benefit, local_size_cost;
169 /* Time benefit and size cost that specializing the function for this value
170 can bring about in it's callees (transitively). */
171 int prop_time_benefit, prop_size_cost;
172 /* True if this valye is currently on the topo-sort stack. */
173 bool on_stack;
176 /* Allocation pools for values and their sources in ipa-cp. */
178 alloc_pool ipcp_values_pool;
179 alloc_pool ipcp_sources_pool;
181 /* Lattice describing potential values of a formal parameter of a function and
182 some of their other properties. TOP is represented by a lattice with zero
183 values and with contains_variable and bottom flags cleared. BOTTOM is
184 represented by a lattice with the bottom flag set. In that case, values and
185 contains_variable flag should be disregarded. */
187 struct ipcp_lattice
189 /* The list of known values and types in this lattice. Note that values are
190 not deallocated if a lattice is set to bottom because there may be value
191 sources referencing them. */
192 struct ipcp_value *values;
193 /* Number of known values and types in this lattice. */
194 int values_count;
195 /* The lattice contains a variable component (in addition to values). */
196 bool contains_variable;
197 /* The value of the lattice is bottom (i.e. variable and unusable for any
198 propagation). */
199 bool bottom;
200 /* There is a virtual call based on this parameter. */
201 bool virt_call;
204 /* Maximal count found in program. */
206 static gcov_type max_count;
208 /* Original overall size of the program. */
210 static long overall_size, max_new_size;
212 /* Head of the linked list of topologically sorted values. */
214 static struct ipcp_value *values_topo;
216 /* Return the lattice corresponding to the Ith formal parameter of the function
217 described by INFO. */
218 static inline struct ipcp_lattice *
219 ipa_get_lattice (struct ipa_node_params *info, int i)
221 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
222 gcc_checking_assert (!info->ipcp_orig_node);
223 gcc_checking_assert (info->lattices);
224 return &(info->lattices[i]);
227 /* Return whether LAT is a lattice with a single constant and without an
228 undefined value. */
230 static inline bool
231 ipa_lat_is_single_const (struct ipcp_lattice *lat)
233 if (lat->bottom
234 || lat->contains_variable
235 || lat->values_count != 1)
236 return false;
237 else
238 return true;
241 /* Return true iff the CS is an edge within a strongly connected component as
242 computed by ipa_reduced_postorder. */
244 static inline bool
245 edge_within_scc (struct cgraph_edge *cs)
247 struct ipa_dfs_info *caller_dfs = (struct ipa_dfs_info *) cs->caller->symbol.aux;
248 struct ipa_dfs_info *callee_dfs;
249 struct cgraph_node *callee = cgraph_function_node (cs->callee, NULL);
251 callee_dfs = (struct ipa_dfs_info *) callee->symbol.aux;
252 return (caller_dfs
253 && callee_dfs
254 && caller_dfs->scc_no == callee_dfs->scc_no);
257 /* Print V which is extracted from a value in a lattice to F. */
259 static void
260 print_ipcp_constant_value (FILE * f, tree v)
262 if (TREE_CODE (v) == TREE_BINFO)
264 fprintf (f, "BINFO ");
265 print_generic_expr (f, BINFO_TYPE (v), 0);
267 else if (TREE_CODE (v) == ADDR_EXPR
268 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
270 fprintf (f, "& ");
271 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
273 else
274 print_generic_expr (f, v, 0);
277 /* Print all ipcp_lattices of all functions to F. */
279 static void
280 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
282 struct cgraph_node *node;
283 int i, count;
285 fprintf (f, "\nLattices:\n");
286 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
288 struct ipa_node_params *info;
290 info = IPA_NODE_REF (node);
291 fprintf (f, " Node: %s/%i:\n", cgraph_node_name (node), node->uid);
292 count = ipa_get_param_count (info);
293 for (i = 0; i < count; i++)
295 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
296 struct ipcp_value *val;
297 bool prev = false;
299 fprintf (f, " param [%d]: ", i);
300 if (lat->bottom)
302 fprintf (f, "BOTTOM\n");
303 continue;
306 if (!lat->values_count && !lat->contains_variable)
308 fprintf (f, "TOP\n");
309 continue;
312 if (lat->contains_variable)
314 fprintf (f, "VARIABLE");
315 prev = true;
316 if (dump_benefits)
317 fprintf (f, "\n");
320 for (val = lat->values; val; val = val->next)
322 if (dump_benefits && prev)
323 fprintf (f, " ");
324 else if (!dump_benefits && prev)
325 fprintf (f, ", ");
326 else
327 prev = true;
329 print_ipcp_constant_value (f, val->value);
331 if (dump_sources)
333 struct ipcp_value_source *s;
335 fprintf (f, " [from:");
336 for (s = val->sources; s; s = s->next)
337 fprintf (f, " %i(%i)", s->cs->caller->uid,s->cs->frequency);
338 fprintf (f, "]");
341 if (dump_benefits)
342 fprintf (f, " [loc_time: %i, loc_size: %i, "
343 "prop_time: %i, prop_size: %i]\n",
344 val->local_time_benefit, val->local_size_cost,
345 val->prop_time_benefit, val->prop_size_cost);
347 if (!dump_benefits)
348 fprintf (f, "\n");
353 /* Determine whether it is at all technically possible to create clones of NODE
354 and store this information in the ipa_node_params structure associated
355 with NODE. */
357 static void
358 determine_versionability (struct cgraph_node *node)
360 const char *reason = NULL;
362 /* There are a number of generic reasons functions cannot be versioned. We
363 also cannot remove parameters if there are type attributes such as fnspec
364 present. */
365 if (node->alias || node->thunk.thunk_p)
366 reason = "alias or thunk";
367 else if (!node->local.versionable)
368 reason = "not a tree_versionable_function";
369 else if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
370 reason = "insufficient body availability";
372 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
373 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
374 cgraph_node_name (node), node->uid, reason);
376 node->local.versionable = (reason == NULL);
379 /* Return true if it is at all technically possible to create clones of a
380 NODE. */
382 static bool
383 ipcp_versionable_function_p (struct cgraph_node *node)
385 return node->local.versionable;
388 /* Structure holding accumulated information about callers of a node. */
390 struct caller_statistics
392 gcov_type count_sum;
393 int n_calls, n_hot_calls, freq_sum;
396 /* Initialize fields of STAT to zeroes. */
398 static inline void
399 init_caller_stats (struct caller_statistics *stats)
401 stats->count_sum = 0;
402 stats->n_calls = 0;
403 stats->n_hot_calls = 0;
404 stats->freq_sum = 0;
407 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
408 non-thunk incoming edges to NODE. */
410 static bool
411 gather_caller_stats (struct cgraph_node *node, void *data)
413 struct caller_statistics *stats = (struct caller_statistics *) data;
414 struct cgraph_edge *cs;
416 for (cs = node->callers; cs; cs = cs->next_caller)
417 if (cs->caller->thunk.thunk_p)
418 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
419 stats, false);
420 else
422 stats->count_sum += cs->count;
423 stats->freq_sum += cs->frequency;
424 stats->n_calls++;
425 if (cgraph_maybe_hot_edge_p (cs))
426 stats->n_hot_calls ++;
428 return false;
432 /* Return true if this NODE is viable candidate for cloning. */
434 static bool
435 ipcp_cloning_candidate_p (struct cgraph_node *node)
437 struct caller_statistics stats;
439 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
441 if (!flag_ipa_cp_clone)
443 if (dump_file)
444 fprintf (dump_file, "Not considering %s for cloning; "
445 "-fipa-cp-clone disabled.\n",
446 cgraph_node_name (node));
447 return false;
450 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->symbol.decl)))
452 if (dump_file)
453 fprintf (dump_file, "Not considering %s for cloning; "
454 "optimizing it for size.\n",
455 cgraph_node_name (node));
456 return false;
459 init_caller_stats (&stats);
460 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
462 if (inline_summary (node)->self_size < stats.n_calls)
464 if (dump_file)
465 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
466 cgraph_node_name (node));
467 return true;
470 /* When profile is available and function is hot, propagate into it even if
471 calls seems cold; constant propagation can improve function's speed
472 significantly. */
473 if (max_count)
475 if (stats.count_sum > node->count * 90 / 100)
477 if (dump_file)
478 fprintf (dump_file, "Considering %s for cloning; "
479 "usually called directly.\n",
480 cgraph_node_name (node));
481 return true;
484 if (!stats.n_hot_calls)
486 if (dump_file)
487 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
488 cgraph_node_name (node));
489 return false;
491 if (dump_file)
492 fprintf (dump_file, "Considering %s for cloning.\n",
493 cgraph_node_name (node));
494 return true;
497 /* Arrays representing a topological ordering of call graph nodes and a stack
498 of noes used during constant propagation. */
500 struct topo_info
502 struct cgraph_node **order;
503 struct cgraph_node **stack;
504 int nnodes, stack_top;
507 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
509 static void
510 build_toporder_info (struct topo_info *topo)
512 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
513 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
514 topo->stack_top = 0;
515 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
518 /* Free information about strongly connected components and the arrays in
519 TOPO. */
521 static void
522 free_toporder_info (struct topo_info *topo)
524 ipa_free_postorder_info ();
525 free (topo->order);
526 free (topo->stack);
529 /* Add NODE to the stack in TOPO, unless it is already there. */
531 static inline void
532 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
534 struct ipa_node_params *info = IPA_NODE_REF (node);
535 if (info->node_enqueued)
536 return;
537 info->node_enqueued = 1;
538 topo->stack[topo->stack_top++] = node;
541 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
542 is empty. */
544 static struct cgraph_node *
545 pop_node_from_stack (struct topo_info *topo)
547 if (topo->stack_top)
549 struct cgraph_node *node;
550 topo->stack_top--;
551 node = topo->stack[topo->stack_top];
552 IPA_NODE_REF (node)->node_enqueued = 0;
553 return node;
555 else
556 return NULL;
559 /* Set lattice LAT to bottom and return true if it previously was not set as
560 such. */
562 static inline bool
563 set_lattice_to_bottom (struct ipcp_lattice *lat)
565 bool ret = !lat->bottom;
566 lat->bottom = true;
567 return ret;
570 /* Mark lattice as containing an unknown value and return true if it previously
571 was not marked as such. */
573 static inline bool
574 set_lattice_contains_variable (struct ipcp_lattice *lat)
576 bool ret = !lat->contains_variable;
577 lat->contains_variable = true;
578 return ret;
581 /* Initialize ipcp_lattices. */
583 static void
584 initialize_node_lattices (struct cgraph_node *node)
586 struct ipa_node_params *info = IPA_NODE_REF (node);
587 struct cgraph_edge *ie;
588 bool disable = false, variable = false;
589 int i;
591 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
592 if (!node->local.local)
594 /* When cloning is allowed, we can assume that externally visible
595 functions are not called. We will compensate this by cloning
596 later. */
597 if (ipcp_versionable_function_p (node)
598 && ipcp_cloning_candidate_p (node))
599 variable = true;
600 else
601 disable = true;
604 if (disable || variable)
606 for (i = 0; i < ipa_get_param_count (info) ; i++)
608 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
609 if (disable)
610 set_lattice_to_bottom (lat);
611 else
612 set_lattice_contains_variable (lat);
614 if (dump_file && (dump_flags & TDF_DETAILS)
615 && node->alias && node->thunk.thunk_p)
616 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
617 cgraph_node_name (node), node->uid,
618 disable ? "BOTTOM" : "VARIABLE");
621 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
622 if (ie->indirect_info->polymorphic)
624 gcc_checking_assert (ie->indirect_info->param_index >= 0);
625 ipa_get_lattice (info, ie->indirect_info->param_index)->virt_call = 1;
629 /* Return the result of a (possibly arithmetic) pass through jump function
630 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
631 determined or itself is considered an interprocedural invariant. */
633 static tree
634 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
636 tree restype, res;
638 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
639 return input;
640 else if (TREE_CODE (input) == TREE_BINFO)
641 return NULL_TREE;
643 gcc_checking_assert (is_gimple_ip_invariant (input));
644 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
645 == tcc_comparison)
646 restype = boolean_type_node;
647 else
648 restype = TREE_TYPE (input);
649 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
650 input, ipa_get_jf_pass_through_operand (jfunc));
652 if (res && !is_gimple_ip_invariant (res))
653 return NULL_TREE;
655 return res;
658 /* Return the result of an ancestor jump function JFUNC on the constant value
659 INPUT. Return NULL_TREE if that cannot be determined. */
661 static tree
662 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
664 if (TREE_CODE (input) == TREE_BINFO)
665 return get_binfo_at_offset (input,
666 ipa_get_jf_ancestor_offset (jfunc),
667 ipa_get_jf_ancestor_type (jfunc));
668 else if (TREE_CODE (input) == ADDR_EXPR)
670 tree t = TREE_OPERAND (input, 0);
671 t = build_ref_for_offset (EXPR_LOCATION (t), t,
672 ipa_get_jf_ancestor_offset (jfunc),
673 ipa_get_jf_ancestor_type (jfunc), NULL, false);
674 return build_fold_addr_expr (t);
676 else
677 return NULL_TREE;
680 /* Extract the acual BINFO being described by JFUNC which must be a known type
681 jump function. */
683 static tree
684 ipa_value_from_known_type_jfunc (struct ipa_jump_func *jfunc)
686 tree base_binfo = TYPE_BINFO (ipa_get_jf_known_type_base_type (jfunc));
687 if (!base_binfo)
688 return NULL_TREE;
689 return get_binfo_at_offset (base_binfo,
690 ipa_get_jf_known_type_offset (jfunc),
691 ipa_get_jf_known_type_component_type (jfunc));
694 /* Determine whether JFUNC evaluates to a known value (that is either a
695 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
696 describes the caller node so that pass-through jump functions can be
697 evaluated. */
699 tree
700 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
702 if (jfunc->type == IPA_JF_CONST)
703 return ipa_get_jf_constant (jfunc);
704 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
705 return ipa_value_from_known_type_jfunc (jfunc);
706 else if (jfunc->type == IPA_JF_PASS_THROUGH
707 || jfunc->type == IPA_JF_ANCESTOR)
709 tree input;
710 int idx;
712 if (jfunc->type == IPA_JF_PASS_THROUGH)
713 idx = ipa_get_jf_pass_through_formal_id (jfunc);
714 else
715 idx = ipa_get_jf_ancestor_formal_id (jfunc);
717 if (info->ipcp_orig_node)
718 input = VEC_index (tree, info->known_vals, idx);
719 else
721 struct ipcp_lattice *lat;
723 if (!info->lattices)
725 gcc_checking_assert (!flag_ipa_cp);
726 return NULL_TREE;
728 lat = ipa_get_lattice (info, idx);
729 if (!ipa_lat_is_single_const (lat))
730 return NULL_TREE;
731 input = lat->values->value;
734 if (!input)
735 return NULL_TREE;
737 if (jfunc->type == IPA_JF_PASS_THROUGH)
738 return ipa_get_jf_pass_through_result (jfunc, input);
739 else
740 return ipa_get_jf_ancestor_result (jfunc, input);
742 else
743 return NULL_TREE;
747 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
748 bottom, not containing a variable component and without any known value at
749 the same time. */
751 DEBUG_FUNCTION void
752 ipcp_verify_propagated_values (void)
754 struct cgraph_node *node;
756 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
758 struct ipa_node_params *info = IPA_NODE_REF (node);
759 int i, count = ipa_get_param_count (info);
761 for (i = 0; i < count; i++)
763 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
765 if (!lat->bottom
766 && !lat->contains_variable
767 && lat->values_count == 0)
769 if (dump_file)
771 fprintf (dump_file, "\nIPA lattices after constant "
772 "propagation:\n");
773 print_all_lattices (dump_file, true, false);
776 gcc_unreachable ();
782 /* Return true iff X and Y should be considered equal values by IPA-CP. */
784 static bool
785 values_equal_for_ipcp_p (tree x, tree y)
787 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
789 if (x == y)
790 return true;
792 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
793 return false;
795 if (TREE_CODE (x) == ADDR_EXPR
796 && TREE_CODE (y) == ADDR_EXPR
797 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
798 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
799 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
800 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
801 else
802 return operand_equal_p (x, y, 0);
805 /* Add a new value source to VAL, marking that a value comes from edge CS and
806 (if the underlying jump function is a pass-through or an ancestor one) from
807 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. */
809 static void
810 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
811 struct ipcp_value *src_val, int src_idx)
813 struct ipcp_value_source *src;
815 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
816 src->cs = cs;
817 src->val = src_val;
818 src->index = src_idx;
820 src->next = val->sources;
821 val->sources = src;
825 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
826 it. CS, SRC_VAL and SRC_INDEX are meant for add_value_source and have the
827 same meaning. */
829 static bool
830 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
831 struct cgraph_edge *cs, struct ipcp_value *src_val,
832 int src_idx)
834 struct ipcp_value *val;
836 if (lat->bottom)
837 return false;
840 for (val = lat->values; val; val = val->next)
841 if (values_equal_for_ipcp_p (val->value, newval))
843 if (edge_within_scc (cs))
845 struct ipcp_value_source *s;
846 for (s = val->sources; s ; s = s->next)
847 if (s->cs == cs)
848 break;
849 if (s)
850 return false;
853 add_value_source (val, cs, src_val, src_idx);
854 return false;
857 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
859 /* We can only free sources, not the values themselves, because sources
860 of other values in this this SCC might point to them. */
861 for (val = lat->values; val; val = val->next)
863 while (val->sources)
865 struct ipcp_value_source *src = val->sources;
866 val->sources = src->next;
867 pool_free (ipcp_sources_pool, src);
871 lat->values = NULL;
872 return set_lattice_to_bottom (lat);
875 lat->values_count++;
876 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
877 memset (val, 0, sizeof (*val));
879 add_value_source (val, cs, src_val, src_idx);
880 val->value = newval;
881 val->next = lat->values;
882 lat->values = val;
883 return true;
886 /* Propagate values through a pass-through jump function JFUNC associated with
887 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
888 is the index of the source parameter. */
890 static bool
891 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
892 struct ipa_jump_func *jfunc,
893 struct ipcp_lattice *src_lat,
894 struct ipcp_lattice *dest_lat,
895 int src_idx)
897 struct ipcp_value *src_val;
898 bool ret = false;
900 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
901 for (src_val = src_lat->values; src_val; src_val = src_val->next)
902 ret |= add_value_to_lattice (dest_lat, src_val->value, cs,
903 src_val, src_idx);
904 /* Do not create new values when propagating within an SCC because if there
905 are arithmetic functions with circular dependencies, there is infinite
906 number of them and we would just make lattices bottom. */
907 else if (edge_within_scc (cs))
908 ret = set_lattice_contains_variable (dest_lat);
909 else
910 for (src_val = src_lat->values; src_val; src_val = src_val->next)
912 tree cstval = src_val->value;
914 if (TREE_CODE (cstval) == TREE_BINFO)
916 ret |= set_lattice_contains_variable (dest_lat);
917 continue;
919 cstval = ipa_get_jf_pass_through_result (jfunc, cstval);
921 if (cstval)
922 ret |= add_value_to_lattice (dest_lat, cstval, cs, src_val, src_idx);
923 else
924 ret |= set_lattice_contains_variable (dest_lat);
927 return ret;
930 /* Propagate values through an ancestor jump function JFUNC associated with
931 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
932 is the index of the source parameter. */
934 static bool
935 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
936 struct ipa_jump_func *jfunc,
937 struct ipcp_lattice *src_lat,
938 struct ipcp_lattice *dest_lat,
939 int src_idx)
941 struct ipcp_value *src_val;
942 bool ret = false;
944 if (edge_within_scc (cs))
945 return set_lattice_contains_variable (dest_lat);
947 for (src_val = src_lat->values; src_val; src_val = src_val->next)
949 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
951 if (t)
952 ret |= add_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
953 else
954 ret |= set_lattice_contains_variable (dest_lat);
957 return ret;
960 /* Propagate values across jump function JFUNC that is associated with edge CS
961 and put the values into DEST_LAT. */
963 static bool
964 propagate_accross_jump_function (struct cgraph_edge *cs,
965 struct ipa_jump_func *jfunc,
966 struct ipcp_lattice *dest_lat)
968 if (dest_lat->bottom)
969 return false;
971 if (jfunc->type == IPA_JF_CONST
972 || jfunc->type == IPA_JF_KNOWN_TYPE)
974 tree val;
976 if (jfunc->type == IPA_JF_KNOWN_TYPE)
978 val = ipa_value_from_known_type_jfunc (jfunc);
979 if (!val)
980 return set_lattice_contains_variable (dest_lat);
982 else
983 val = ipa_get_jf_constant (jfunc);
984 return add_value_to_lattice (dest_lat, val, cs, NULL, 0);
986 else if (jfunc->type == IPA_JF_PASS_THROUGH
987 || jfunc->type == IPA_JF_ANCESTOR)
989 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
990 struct ipcp_lattice *src_lat;
991 int src_idx;
992 bool ret;
994 if (jfunc->type == IPA_JF_PASS_THROUGH)
995 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
996 else
997 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
999 src_lat = ipa_get_lattice (caller_info, src_idx);
1000 if (src_lat->bottom)
1001 return set_lattice_contains_variable (dest_lat);
1003 /* If we would need to clone the caller and cannot, do not propagate. */
1004 if (!ipcp_versionable_function_p (cs->caller)
1005 && (src_lat->contains_variable
1006 || (src_lat->values_count > 1)))
1007 return set_lattice_contains_variable (dest_lat);
1009 if (jfunc->type == IPA_JF_PASS_THROUGH)
1010 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1011 dest_lat, src_idx);
1012 else
1013 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1014 src_idx);
1016 if (src_lat->contains_variable)
1017 ret |= set_lattice_contains_variable (dest_lat);
1019 return ret;
1022 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1023 use it for indirect inlining), we should propagate them too. */
1024 return set_lattice_contains_variable (dest_lat);
1027 /* Propagate constants from the caller to the callee of CS. INFO describes the
1028 caller. */
1030 static bool
1031 propagate_constants_accross_call (struct cgraph_edge *cs)
1033 struct ipa_node_params *callee_info;
1034 enum availability availability;
1035 struct cgraph_node *callee, *alias_or_thunk;
1036 struct ipa_edge_args *args;
1037 bool ret = false;
1038 int i, args_count, parms_count;
1040 callee = cgraph_function_node (cs->callee, &availability);
1041 if (!callee->analyzed)
1042 return false;
1043 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1044 callee_info = IPA_NODE_REF (callee);
1046 args = IPA_EDGE_REF (cs);
1047 args_count = ipa_get_cs_argument_count (args);
1048 parms_count = ipa_get_param_count (callee_info);
1050 /* If this call goes through a thunk we must not propagate to the first (0th)
1051 parameter. However, we might need to uncover a thunk from below a series
1052 of aliases first. */
1053 alias_or_thunk = cs->callee;
1054 while (alias_or_thunk->alias)
1055 alias_or_thunk = cgraph_alias_aliased_node (alias_or_thunk);
1056 if (alias_or_thunk->thunk.thunk_p)
1058 ret |= set_lattice_contains_variable (ipa_get_lattice (callee_info, 0));
1059 i = 1;
1061 else
1062 i = 0;
1064 for (; (i < args_count) && (i < parms_count); i++)
1066 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1067 struct ipcp_lattice *dest_lat = ipa_get_lattice (callee_info, i);
1069 if (availability == AVAIL_OVERWRITABLE)
1070 ret |= set_lattice_contains_variable (dest_lat);
1071 else
1072 ret |= propagate_accross_jump_function (cs, jump_func, dest_lat);
1074 for (; i < parms_count; i++)
1075 ret |= set_lattice_contains_variable (ipa_get_lattice (callee_info, i));
1077 return ret;
1080 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1081 (which can contain both constants and binfos) or KNOWN_BINFOS (which can be
1082 NULL) return the destination. */
1084 tree
1085 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1086 VEC (tree, heap) *known_vals,
1087 VEC (tree, heap) *known_binfos)
1089 int param_index = ie->indirect_info->param_index;
1090 HOST_WIDE_INT token, anc_offset;
1091 tree otr_type;
1092 tree t;
1094 if (param_index == -1)
1095 return NULL_TREE;
1097 if (!ie->indirect_info->polymorphic)
1099 tree t = (VEC_length (tree, known_vals) > (unsigned int) param_index
1100 ? VEC_index (tree, known_vals, param_index) : NULL);
1101 if (t &&
1102 TREE_CODE (t) == ADDR_EXPR
1103 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1104 return TREE_OPERAND (t, 0);
1105 else
1106 return NULL_TREE;
1109 token = ie->indirect_info->otr_token;
1110 anc_offset = ie->indirect_info->anc_offset;
1111 otr_type = ie->indirect_info->otr_type;
1113 t = VEC_index (tree, known_vals, param_index);
1114 if (!t && known_binfos
1115 && VEC_length (tree, known_binfos) > (unsigned int) param_index)
1116 t = VEC_index (tree, known_binfos, param_index);
1117 if (!t)
1118 return NULL_TREE;
1120 if (TREE_CODE (t) != TREE_BINFO)
1122 tree binfo;
1123 binfo = gimple_extract_devirt_binfo_from_cst (t);
1124 if (!binfo)
1125 return NULL_TREE;
1126 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1127 if (!binfo)
1128 return NULL_TREE;
1129 return gimple_get_virt_method_for_binfo (token, binfo);
1131 else
1133 tree binfo;
1135 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1136 if (!binfo)
1137 return NULL_TREE;
1138 return gimple_get_virt_method_for_binfo (token, binfo);
1142 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1143 and KNOWN_BINFOS. */
1145 static int
1146 devirtualization_time_bonus (struct cgraph_node *node,
1147 VEC (tree, heap) *known_csts,
1148 VEC (tree, heap) *known_binfos)
1150 struct cgraph_edge *ie;
1151 int res = 0;
1153 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1155 struct cgraph_node *callee;
1156 struct inline_summary *isummary;
1157 tree target;
1159 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos);
1160 if (!target)
1161 continue;
1163 /* Only bare minimum benefit for clearly un-inlineable targets. */
1164 res += 1;
1165 callee = cgraph_get_node (target);
1166 if (!callee || !callee->analyzed)
1167 continue;
1168 isummary = inline_summary (callee);
1169 if (!isummary->inlinable)
1170 continue;
1172 /* FIXME: The values below need re-considering and perhaps also
1173 integrating into the cost metrics, at lest in some very basic way. */
1174 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1175 res += 31;
1176 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1177 res += 15;
1178 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1179 || DECL_DECLARED_INLINE_P (callee->symbol.decl))
1180 res += 7;
1183 return res;
1186 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1187 and SIZE_COST and with the sum of frequencies of incoming edges to the
1188 potential new clone in FREQUENCIES. */
1190 static bool
1191 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1192 int freq_sum, gcov_type count_sum, int size_cost)
1194 if (time_benefit == 0
1195 || !flag_ipa_cp_clone
1196 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->symbol.decl)))
1197 return false;
1199 gcc_assert (size_cost > 0);
1201 if (max_count)
1203 int factor = (count_sum * 1000) / max_count;
1204 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * factor)
1205 / size_cost);
1207 if (dump_file && (dump_flags & TDF_DETAILS))
1208 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1209 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1210 ") -> evaluation: " HOST_WIDEST_INT_PRINT_DEC
1211 ", threshold: %i\n",
1212 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1213 evaluation, 500);
1215 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1217 else
1219 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * freq_sum)
1220 / size_cost);
1222 if (dump_file && (dump_flags & TDF_DETAILS))
1223 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1224 "size: %i, freq_sum: %i) -> evaluation: "
1225 HOST_WIDEST_INT_PRINT_DEC ", threshold: %i\n",
1226 time_benefit, size_cost, freq_sum, evaluation,
1227 CGRAPH_FREQ_BASE /2);
1229 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1234 /* Allocate KNOWN_CSTS and KNOWN_BINFOS and populate them with values of
1235 parameters that are known independent of the context. INFO describes the
1236 function. If REMOVABLE_PARAMS_COST is non-NULL, the movement cost of all
1237 removable parameters will be stored in it. */
1239 static bool
1240 gather_context_independent_values (struct ipa_node_params *info,
1241 VEC (tree, heap) **known_csts,
1242 VEC (tree, heap) **known_binfos,
1243 int *removable_params_cost)
1245 int i, count = ipa_get_param_count (info);
1246 bool ret = false;
1248 *known_csts = NULL;
1249 *known_binfos = NULL;
1250 VEC_safe_grow_cleared (tree, heap, *known_csts, count);
1251 VEC_safe_grow_cleared (tree, heap, *known_binfos, count);
1253 if (removable_params_cost)
1254 *removable_params_cost = 0;
1256 for (i = 0; i < count ; i++)
1258 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1260 if (ipa_lat_is_single_const (lat))
1262 struct ipcp_value *val = lat->values;
1263 if (TREE_CODE (val->value) != TREE_BINFO)
1265 VEC_replace (tree, *known_csts, i, val->value);
1266 if (removable_params_cost)
1267 *removable_params_cost
1268 += estimate_move_cost (TREE_TYPE (val->value));
1269 ret = true;
1271 else if (lat->virt_call)
1273 VEC_replace (tree, *known_binfos, i, val->value);
1274 ret = true;
1276 else if (removable_params_cost
1277 && !ipa_is_param_used (info, i))
1278 *removable_params_cost
1279 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1281 else if (removable_params_cost
1282 && !ipa_is_param_used (info, i))
1283 *removable_params_cost
1284 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1287 return ret;
1290 /* Iterate over known values of parameters of NODE and estimate the local
1291 effects in terms of time and size they have. */
1293 static void
1294 estimate_local_effects (struct cgraph_node *node)
1296 struct ipa_node_params *info = IPA_NODE_REF (node);
1297 int i, count = ipa_get_param_count (info);
1298 VEC (tree, heap) *known_csts, *known_binfos;
1299 bool always_const;
1300 int base_time = inline_summary (node)->time;
1301 int removable_params_cost;
1303 if (!count || !ipcp_versionable_function_p (node))
1304 return;
1306 if (dump_file && (dump_flags & TDF_DETAILS))
1307 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1308 cgraph_node_name (node), node->uid, base_time);
1310 always_const = gather_context_independent_values (info, &known_csts,
1311 &known_binfos,
1312 &removable_params_cost);
1313 if (always_const)
1315 struct caller_statistics stats;
1316 int time, size;
1318 init_caller_stats (&stats);
1319 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1320 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1321 &size, &time);
1322 time -= devirtualization_time_bonus (node, known_csts, known_binfos);
1323 time -= removable_params_cost;
1324 size -= stats.n_calls * removable_params_cost;
1326 if (dump_file)
1327 fprintf (dump_file, " - context independent values, size: %i, "
1328 "time_benefit: %i\n", size, base_time - time);
1330 if (size <= 0
1331 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1333 info->clone_for_all_contexts = true;
1334 base_time = time;
1336 if (dump_file)
1337 fprintf (dump_file, " Decided to specialize for all "
1338 "known contexts, code not going to grow.\n");
1340 else if (good_cloning_opportunity_p (node, base_time - time,
1341 stats.freq_sum, stats.count_sum,
1342 size))
1344 if (size + overall_size <= max_new_size)
1346 info->clone_for_all_contexts = true;
1347 base_time = time;
1348 overall_size += size;
1350 if (dump_file)
1351 fprintf (dump_file, " Decided to specialize for all "
1352 "known contexts, growth deemed beneficial.\n");
1354 else if (dump_file && (dump_flags & TDF_DETAILS))
1355 fprintf (dump_file, " Not cloning for all contexts because "
1356 "max_new_size would be reached with %li.\n",
1357 size + overall_size);
1361 for (i = 0; i < count ; i++)
1363 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1364 struct ipcp_value *val;
1365 int emc;
1367 if (lat->bottom
1368 || !lat->values
1369 || VEC_index (tree, known_csts, i)
1370 || VEC_index (tree, known_binfos, i))
1371 continue;
1373 for (val = lat->values; val; val = val->next)
1375 int time, size, time_benefit;
1377 if (TREE_CODE (val->value) != TREE_BINFO)
1379 VEC_replace (tree, known_csts, i, val->value);
1380 VEC_replace (tree, known_binfos, i, NULL_TREE);
1381 emc = estimate_move_cost (TREE_TYPE (val->value));
1383 else if (lat->virt_call)
1385 VEC_replace (tree, known_csts, i, NULL_TREE);
1386 VEC_replace (tree, known_binfos, i, val->value);
1387 emc = 0;
1389 else
1390 continue;
1392 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1393 &size, &time);
1394 time_benefit = base_time - time
1395 + devirtualization_time_bonus (node, known_csts, known_binfos)
1396 + removable_params_cost + emc;
1398 gcc_checking_assert (size >=0);
1399 /* The inliner-heuristics based estimates may think that in certain
1400 contexts some functions do not have any size at all but we want
1401 all specializations to have at least a tiny cost, not least not to
1402 divide by zero. */
1403 if (size == 0)
1404 size = 1;
1406 if (dump_file && (dump_flags & TDF_DETAILS))
1408 fprintf (dump_file, " - estimates for value ");
1409 print_ipcp_constant_value (dump_file, val->value);
1410 fprintf (dump_file, " for parameter ");
1411 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
1412 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
1413 time_benefit, size);
1416 val->local_time_benefit = time_benefit;
1417 val->local_size_cost = size;
1421 VEC_free (tree, heap, known_csts);
1422 VEC_free (tree, heap, known_binfos);
1426 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
1427 topological sort of values. */
1429 static void
1430 add_val_to_toposort (struct ipcp_value *cur_val)
1432 static int dfs_counter = 0;
1433 static struct ipcp_value *stack;
1434 struct ipcp_value_source *src;
1436 if (cur_val->dfs)
1437 return;
1439 dfs_counter++;
1440 cur_val->dfs = dfs_counter;
1441 cur_val->low_link = dfs_counter;
1443 cur_val->topo_next = stack;
1444 stack = cur_val;
1445 cur_val->on_stack = true;
1447 for (src = cur_val->sources; src; src = src->next)
1448 if (src->val)
1450 if (src->val->dfs == 0)
1452 add_val_to_toposort (src->val);
1453 if (src->val->low_link < cur_val->low_link)
1454 cur_val->low_link = src->val->low_link;
1456 else if (src->val->on_stack
1457 && src->val->dfs < cur_val->low_link)
1458 cur_val->low_link = src->val->dfs;
1461 if (cur_val->dfs == cur_val->low_link)
1463 struct ipcp_value *v, *scc_list = NULL;
1467 v = stack;
1468 stack = v->topo_next;
1469 v->on_stack = false;
1471 v->scc_next = scc_list;
1472 scc_list = v;
1474 while (v != cur_val);
1476 cur_val->topo_next = values_topo;
1477 values_topo = cur_val;
1481 /* Add all values in lattices associated with NODE to the topological sort if
1482 they are not there yet. */
1484 static void
1485 add_all_node_vals_to_toposort (struct cgraph_node *node)
1487 struct ipa_node_params *info = IPA_NODE_REF (node);
1488 int i, count = ipa_get_param_count (info);
1490 for (i = 0; i < count ; i++)
1492 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1493 struct ipcp_value *val;
1495 if (lat->bottom || !lat->values)
1496 continue;
1497 for (val = lat->values; val; val = val->next)
1498 add_val_to_toposort (val);
1502 /* One pass of constants propagation along the call graph edges, from callers
1503 to callees (requires topological ordering in TOPO), iterate over strongly
1504 connected components. */
1506 static void
1507 propagate_constants_topo (struct topo_info *topo)
1509 int i;
1511 for (i = topo->nnodes - 1; i >= 0; i--)
1513 struct cgraph_node *v, *node = topo->order[i];
1514 struct ipa_dfs_info *node_dfs_info;
1516 if (!cgraph_function_with_gimple_body_p (node))
1517 continue;
1519 node_dfs_info = (struct ipa_dfs_info *) node->symbol.aux;
1520 /* First, iteratively propagate within the strongly connected component
1521 until all lattices stabilize. */
1522 v = node_dfs_info->next_cycle;
1523 while (v)
1525 push_node_to_stack (topo, v);
1526 v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle;
1529 v = node;
1530 while (v)
1532 struct cgraph_edge *cs;
1534 for (cs = v->callees; cs; cs = cs->next_callee)
1535 if (edge_within_scc (cs)
1536 && propagate_constants_accross_call (cs))
1537 push_node_to_stack (topo, cs->callee);
1538 v = pop_node_from_stack (topo);
1541 /* Afterwards, propagate along edges leading out of the SCC, calculates
1542 the local effects of the discovered constants and all valid values to
1543 their topological sort. */
1544 v = node;
1545 while (v)
1547 struct cgraph_edge *cs;
1549 estimate_local_effects (v);
1550 add_all_node_vals_to_toposort (v);
1551 for (cs = v->callees; cs; cs = cs->next_callee)
1552 if (!edge_within_scc (cs))
1553 propagate_constants_accross_call (cs);
1555 v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle;
1561 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
1562 the bigger one if otherwise. */
1564 static int
1565 safe_add (int a, int b)
1567 if (a > INT_MAX/2 || b > INT_MAX/2)
1568 return a > b ? a : b;
1569 else
1570 return a + b;
1574 /* Propagate the estimated effects of individual values along the topological
1575 from the dependent values to those they depend on. */
1577 static void
1578 propagate_effects (void)
1580 struct ipcp_value *base;
1582 for (base = values_topo; base; base = base->topo_next)
1584 struct ipcp_value_source *src;
1585 struct ipcp_value *val;
1586 int time = 0, size = 0;
1588 for (val = base; val; val = val->scc_next)
1590 time = safe_add (time,
1591 val->local_time_benefit + val->prop_time_benefit);
1592 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
1595 for (val = base; val; val = val->scc_next)
1596 for (src = val->sources; src; src = src->next)
1597 if (src->val
1598 && cgraph_maybe_hot_edge_p (src->cs))
1600 src->val->prop_time_benefit = safe_add (time,
1601 src->val->prop_time_benefit);
1602 src->val->prop_size_cost = safe_add (size,
1603 src->val->prop_size_cost);
1609 /* Propagate constants, binfos and their effects from the summaries
1610 interprocedurally. */
1612 static void
1613 ipcp_propagate_stage (struct topo_info *topo)
1615 struct cgraph_node *node;
1617 if (dump_file)
1618 fprintf (dump_file, "\n Propagating constants:\n\n");
1620 if (in_lto_p)
1621 ipa_update_after_lto_read ();
1624 FOR_EACH_DEFINED_FUNCTION (node)
1626 struct ipa_node_params *info = IPA_NODE_REF (node);
1628 determine_versionability (node);
1629 if (cgraph_function_with_gimple_body_p (node))
1631 info->lattices = XCNEWVEC (struct ipcp_lattice,
1632 ipa_get_param_count (info));
1633 initialize_node_lattices (node);
1635 if (node->count > max_count)
1636 max_count = node->count;
1637 overall_size += inline_summary (node)->self_size;
1640 max_new_size = overall_size;
1641 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1642 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1643 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1645 if (dump_file)
1646 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
1647 overall_size, max_new_size);
1649 propagate_constants_topo (topo);
1650 #ifdef ENABLE_CHECKING
1651 ipcp_verify_propagated_values ();
1652 #endif
1653 propagate_effects ();
1655 if (dump_file)
1657 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
1658 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
1662 /* Discover newly direct outgoing edges from NODE which is a new clone with
1663 known KNOWN_VALS and make them direct. */
1665 static void
1666 ipcp_discover_new_direct_edges (struct cgraph_node *node,
1667 VEC (tree, heap) *known_vals)
1669 struct cgraph_edge *ie, *next_ie;
1671 for (ie = node->indirect_calls; ie; ie = next_ie)
1673 tree target;
1675 next_ie = ie->next_callee;
1676 target = ipa_get_indirect_edge_target (ie, known_vals, NULL);
1677 if (target)
1678 ipa_make_edge_direct_to_target (ie, target);
1682 /* Vector of pointers which for linked lists of clones of an original crgaph
1683 edge. */
1685 static VEC (cgraph_edge_p, heap) *next_edge_clone;
1687 static inline void
1688 grow_next_edge_clone_vector (void)
1690 if (VEC_length (cgraph_edge_p, next_edge_clone)
1691 <= (unsigned) cgraph_edge_max_uid)
1692 VEC_safe_grow_cleared (cgraph_edge_p, heap, next_edge_clone,
1693 cgraph_edge_max_uid + 1);
1696 /* Edge duplication hook to grow the appropriate linked list in
1697 next_edge_clone. */
1699 static void
1700 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1701 __attribute__((unused)) void *data)
1703 grow_next_edge_clone_vector ();
1704 VEC_replace (cgraph_edge_p, next_edge_clone, dst->uid,
1705 VEC_index (cgraph_edge_p, next_edge_clone, src->uid));
1706 VEC_replace (cgraph_edge_p, next_edge_clone, src->uid, dst);
1709 /* Get the next clone in the linked list of clones of an edge. */
1711 static inline struct cgraph_edge *
1712 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
1714 return VEC_index (cgraph_edge_p, next_edge_clone, cs->uid);
1717 /* Return true if edge CS does bring about the value described by SRC. */
1719 static bool
1720 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
1721 struct ipcp_value_source *src)
1723 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1725 if (IPA_NODE_REF (cs->callee)->ipcp_orig_node
1726 || caller_info->node_dead)
1727 return false;
1728 if (!src->val)
1729 return true;
1731 if (caller_info->ipcp_orig_node)
1733 tree t = VEC_index (tree, caller_info->known_vals, src->index);
1734 return (t != NULL_TREE
1735 && values_equal_for_ipcp_p (src->val->value, t));
1737 else
1739 struct ipcp_lattice *lat = ipa_get_lattice (caller_info, src->index);
1740 if (ipa_lat_is_single_const (lat)
1741 && values_equal_for_ipcp_p (src->val->value, lat->values->value))
1742 return true;
1743 else
1744 return false;
1748 /* Given VAL, iterate over all its sources and if they still hold, add their
1749 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
1750 respectively. */
1752 static bool
1753 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
1754 gcov_type *count_sum, int *caller_count)
1756 struct ipcp_value_source *src;
1757 int freq = 0, count = 0;
1758 gcov_type cnt = 0;
1759 bool hot = false;
1761 for (src = val->sources; src; src = src->next)
1763 struct cgraph_edge *cs = src->cs;
1764 while (cs)
1766 if (cgraph_edge_brings_value_p (cs, src))
1768 count++;
1769 freq += cs->frequency;
1770 cnt += cs->count;
1771 hot |= cgraph_maybe_hot_edge_p (cs);
1773 cs = get_next_cgraph_edge_clone (cs);
1777 *freq_sum = freq;
1778 *count_sum = cnt;
1779 *caller_count = count;
1780 return hot;
1783 /* Return a vector of incoming edges that do bring value VAL. It is assumed
1784 their number is known and equal to CALLER_COUNT. */
1786 static VEC (cgraph_edge_p,heap) *
1787 gather_edges_for_value (struct ipcp_value *val, int caller_count)
1789 struct ipcp_value_source *src;
1790 VEC (cgraph_edge_p,heap) *ret;
1792 ret = VEC_alloc (cgraph_edge_p, heap, caller_count);
1793 for (src = val->sources; src; src = src->next)
1795 struct cgraph_edge *cs = src->cs;
1796 while (cs)
1798 if (cgraph_edge_brings_value_p (cs, src))
1799 VEC_quick_push (cgraph_edge_p, ret, cs);
1800 cs = get_next_cgraph_edge_clone (cs);
1804 return ret;
1807 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
1808 Return it or NULL if for some reason it cannot be created. */
1810 static struct ipa_replace_map *
1811 get_replacement_map (tree value, tree parm)
1813 tree req_type = TREE_TYPE (parm);
1814 struct ipa_replace_map *replace_map;
1816 if (!useless_type_conversion_p (req_type, TREE_TYPE (value)))
1818 if (fold_convertible_p (req_type, value))
1819 value = fold_build1 (NOP_EXPR, req_type, value);
1820 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (value)))
1821 value = fold_build1 (VIEW_CONVERT_EXPR, req_type, value);
1822 else
1824 if (dump_file)
1826 fprintf (dump_file, " const ");
1827 print_generic_expr (dump_file, value, 0);
1828 fprintf (dump_file, " can't be converted to param ");
1829 print_generic_expr (dump_file, parm, 0);
1830 fprintf (dump_file, "\n");
1832 return NULL;
1836 replace_map = ggc_alloc_ipa_replace_map ();
1837 if (dump_file)
1839 fprintf (dump_file, " replacing param ");
1840 print_generic_expr (dump_file, parm, 0);
1841 fprintf (dump_file, " with const ");
1842 print_generic_expr (dump_file, value, 0);
1843 fprintf (dump_file, "\n");
1845 replace_map->old_tree = parm;
1846 replace_map->new_tree = value;
1847 replace_map->replace_p = true;
1848 replace_map->ref_p = false;
1850 return replace_map;
1853 /* Dump new profiling counts */
1855 static void
1856 dump_profile_updates (struct cgraph_node *orig_node,
1857 struct cgraph_node *new_node)
1859 struct cgraph_edge *cs;
1861 fprintf (dump_file, " setting count of the specialized node to "
1862 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
1863 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1864 fprintf (dump_file, " edge to %s has count "
1865 HOST_WIDE_INT_PRINT_DEC "\n",
1866 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
1868 fprintf (dump_file, " setting count of the original node to "
1869 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
1870 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1871 fprintf (dump_file, " edge to %s is left with "
1872 HOST_WIDE_INT_PRINT_DEC "\n",
1873 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
1876 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
1877 their profile information to reflect this. */
1879 static void
1880 update_profiling_info (struct cgraph_node *orig_node,
1881 struct cgraph_node *new_node)
1883 struct cgraph_edge *cs;
1884 struct caller_statistics stats;
1885 gcov_type new_sum, orig_sum;
1886 gcov_type remainder, orig_node_count = orig_node->count;
1888 if (orig_node_count == 0)
1889 return;
1891 init_caller_stats (&stats);
1892 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
1893 orig_sum = stats.count_sum;
1894 init_caller_stats (&stats);
1895 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
1896 new_sum = stats.count_sum;
1898 if (orig_node_count < orig_sum + new_sum)
1900 if (dump_file)
1901 fprintf (dump_file, " Problem: node %s/%i has too low count "
1902 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
1903 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
1904 cgraph_node_name (orig_node), orig_node->uid,
1905 (HOST_WIDE_INT) orig_node_count,
1906 (HOST_WIDE_INT) (orig_sum + new_sum));
1908 orig_node_count = (orig_sum + new_sum) * 12 / 10;
1909 if (dump_file)
1910 fprintf (dump_file, " proceeding by pretending it was "
1911 HOST_WIDE_INT_PRINT_DEC "\n",
1912 (HOST_WIDE_INT) orig_node_count);
1915 new_node->count = new_sum;
1916 remainder = orig_node_count - new_sum;
1917 orig_node->count = remainder;
1919 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1920 if (cs->frequency)
1921 cs->count = cs->count * (new_sum * REG_BR_PROB_BASE
1922 / orig_node_count) / REG_BR_PROB_BASE;
1923 else
1924 cs->count = 0;
1926 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1927 cs->count = cs->count * (remainder * REG_BR_PROB_BASE
1928 / orig_node_count) / REG_BR_PROB_BASE;
1930 if (dump_file)
1931 dump_profile_updates (orig_node, new_node);
1934 /* Update the respective profile of specialized NEW_NODE and the original
1935 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
1936 have been redirected to the specialized version. */
1938 static void
1939 update_specialized_profile (struct cgraph_node *new_node,
1940 struct cgraph_node *orig_node,
1941 gcov_type redirected_sum)
1943 struct cgraph_edge *cs;
1944 gcov_type new_node_count, orig_node_count = orig_node->count;
1946 if (dump_file)
1947 fprintf (dump_file, " the sum of counts of redirected edges is "
1948 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
1949 if (orig_node_count == 0)
1950 return;
1952 gcc_assert (orig_node_count >= redirected_sum);
1954 new_node_count = new_node->count;
1955 new_node->count += redirected_sum;
1956 orig_node->count -= redirected_sum;
1958 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1959 if (cs->frequency)
1960 cs->count += cs->count * redirected_sum / new_node_count;
1961 else
1962 cs->count = 0;
1964 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1966 gcov_type dec = cs->count * (redirected_sum * REG_BR_PROB_BASE
1967 / orig_node_count) / REG_BR_PROB_BASE;
1968 if (dec < cs->count)
1969 cs->count -= dec;
1970 else
1971 cs->count = 0;
1974 if (dump_file)
1975 dump_profile_updates (orig_node, new_node);
1978 /* Create a specialized version of NODE with known constants and types of
1979 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
1981 static struct cgraph_node *
1982 create_specialized_node (struct cgraph_node *node,
1983 VEC (tree, heap) *known_vals,
1984 VEC (cgraph_edge_p,heap) *callers)
1986 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
1987 VEC (ipa_replace_map_p,gc)* replace_trees = NULL;
1988 struct cgraph_node *new_node;
1989 int i, count = ipa_get_param_count (info);
1990 bitmap args_to_skip;
1992 gcc_assert (!info->ipcp_orig_node);
1994 if (node->local.can_change_signature)
1996 args_to_skip = BITMAP_GGC_ALLOC ();
1997 for (i = 0; i < count; i++)
1999 tree t = VEC_index (tree, known_vals, i);
2001 if ((t && TREE_CODE (t) != TREE_BINFO)
2002 || !ipa_is_param_used (info, i))
2003 bitmap_set_bit (args_to_skip, i);
2006 else
2008 args_to_skip = NULL;
2009 if (dump_file && (dump_flags & TDF_DETAILS))
2010 fprintf (dump_file, " cannot change function signature\n");
2013 for (i = 0; i < count ; i++)
2015 tree t = VEC_index (tree, known_vals, i);
2016 if (t && TREE_CODE (t) != TREE_BINFO)
2018 struct ipa_replace_map *replace_map;
2020 replace_map = get_replacement_map (t, ipa_get_param (info, i));
2021 if (replace_map)
2022 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_map);
2026 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2027 args_to_skip, "constprop");
2028 if (dump_file && (dump_flags & TDF_DETAILS))
2029 fprintf (dump_file, " the new node is %s/%i.\n",
2030 cgraph_node_name (new_node), new_node->uid);
2031 gcc_checking_assert (ipa_node_params_vector
2032 && (VEC_length (ipa_node_params_t,
2033 ipa_node_params_vector)
2034 > (unsigned) cgraph_max_uid));
2035 update_profiling_info (node, new_node);
2036 new_info = IPA_NODE_REF (new_node);
2037 new_info->ipcp_orig_node = node;
2038 new_info->known_vals = known_vals;
2040 ipcp_discover_new_direct_edges (new_node, known_vals);
2042 VEC_free (cgraph_edge_p, heap, callers);
2043 return new_node;
2046 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2047 KNOWN_VALS with constants and types that are also known for all of the
2048 CALLERS. */
2050 static void
2051 find_more_values_for_callers_subset (struct cgraph_node *node,
2052 VEC (tree, heap) *known_vals,
2053 VEC (cgraph_edge_p,heap) *callers)
2055 struct ipa_node_params *info = IPA_NODE_REF (node);
2056 int i, count = ipa_get_param_count (info);
2058 for (i = 0; i < count ; i++)
2060 struct cgraph_edge *cs;
2061 tree newval = NULL_TREE;
2062 int j;
2064 if (ipa_get_lattice (info, i)->bottom
2065 || VEC_index (tree, known_vals, i))
2066 continue;
2068 FOR_EACH_VEC_ELT (cgraph_edge_p, callers, j, cs)
2070 struct ipa_jump_func *jump_func;
2071 tree t;
2073 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2075 newval = NULL_TREE;
2076 break;
2078 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2079 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2080 if (!t
2081 || (newval
2082 && !values_equal_for_ipcp_p (t, newval)))
2084 newval = NULL_TREE;
2085 break;
2087 else
2088 newval = t;
2091 if (newval)
2093 if (dump_file && (dump_flags & TDF_DETAILS))
2095 fprintf (dump_file, " adding an extra known value ");
2096 print_ipcp_constant_value (dump_file, newval);
2097 fprintf (dump_file, " for parameter ");
2098 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2099 fprintf (dump_file, "\n");
2102 VEC_replace (tree, known_vals, i, newval);
2107 /* Given an original NODE and a VAL for which we have already created a
2108 specialized clone, look whether there are incoming edges that still lead
2109 into the old node but now also bring the requested value and also conform to
2110 all other criteria such that they can be redirected the the special node.
2111 This function can therefore redirect the final edge in a SCC. */
2113 static void
2114 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
2116 struct ipa_node_params *dest_info = IPA_NODE_REF (val->spec_node);
2117 struct ipcp_value_source *src;
2118 int count = ipa_get_param_count (dest_info);
2119 gcov_type redirected_sum = 0;
2121 for (src = val->sources; src; src = src->next)
2123 struct cgraph_edge *cs = src->cs;
2124 while (cs)
2126 enum availability availability;
2127 bool insufficient = false;
2129 if (cgraph_function_node (cs->callee, &availability) == node
2130 && availability > AVAIL_OVERWRITABLE
2131 && cgraph_edge_brings_value_p (cs, src))
2133 struct ipa_node_params *caller_info;
2134 struct ipa_edge_args *args;
2135 int i;
2137 caller_info = IPA_NODE_REF (cs->caller);
2138 args = IPA_EDGE_REF (cs);
2139 for (i = 0; i < count; i++)
2141 struct ipa_jump_func *jump_func;
2142 tree val, t;
2144 val = VEC_index (tree, dest_info->known_vals, i);
2145 if (!val)
2146 continue;
2148 if (i >= ipa_get_cs_argument_count (args))
2150 insufficient = true;
2151 break;
2153 jump_func = ipa_get_ith_jump_func (args, i);
2154 t = ipa_value_from_jfunc (caller_info, jump_func);
2155 if (!t || !values_equal_for_ipcp_p (val, t))
2157 insufficient = true;
2158 break;
2162 if (!insufficient)
2164 if (dump_file)
2165 fprintf (dump_file, " - adding an extra caller %s/%i"
2166 " of %s/%i\n",
2167 xstrdup (cgraph_node_name (cs->caller)),
2168 cs->caller->uid,
2169 xstrdup (cgraph_node_name (val->spec_node)),
2170 val->spec_node->uid);
2172 cgraph_redirect_edge_callee (cs, val->spec_node);
2173 redirected_sum += cs->count;
2176 cs = get_next_cgraph_edge_clone (cs);
2180 if (redirected_sum)
2181 update_specialized_profile (val->spec_node, node, redirected_sum);
2185 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
2187 static void
2188 move_binfos_to_values (VEC (tree, heap) *known_vals,
2189 VEC (tree, heap) *known_binfos)
2191 tree t;
2192 int i;
2194 for (i = 0; VEC_iterate (tree, known_binfos, i, t); i++)
2195 if (t)
2196 VEC_replace (tree, known_vals, i, t);
2200 /* Decide whether and what specialized clones of NODE should be created. */
2202 static bool
2203 decide_whether_version_node (struct cgraph_node *node)
2205 struct ipa_node_params *info = IPA_NODE_REF (node);
2206 int i, count = ipa_get_param_count (info);
2207 VEC (tree, heap) *known_csts, *known_binfos;
2208 bool ret = false;
2210 if (count == 0)
2211 return false;
2213 if (dump_file && (dump_flags & TDF_DETAILS))
2214 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
2215 cgraph_node_name (node), node->uid);
2217 gather_context_independent_values (info, &known_csts, &known_binfos,
2218 NULL);
2220 for (i = 0; i < count ; i++)
2222 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
2223 struct ipcp_value *val;
2225 if (lat->bottom
2226 || VEC_index (tree, known_csts, i)
2227 || VEC_index (tree, known_binfos, i))
2228 continue;
2230 for (val = lat->values; val; val = val->next)
2232 int freq_sum, caller_count;
2233 gcov_type count_sum;
2234 VEC (cgraph_edge_p, heap) *callers;
2235 VEC (tree, heap) *kv;
2237 if (val->spec_node)
2239 perhaps_add_new_callers (node, val);
2240 continue;
2242 else if (val->local_size_cost + overall_size > max_new_size)
2244 if (dump_file && (dump_flags & TDF_DETAILS))
2245 fprintf (dump_file, " Ignoring candidate value because "
2246 "max_new_size would be reached with %li.\n",
2247 val->local_size_cost + overall_size);
2248 continue;
2250 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
2251 &caller_count))
2252 continue;
2254 if (dump_file && (dump_flags & TDF_DETAILS))
2256 fprintf (dump_file, " - considering value ");
2257 print_ipcp_constant_value (dump_file, val->value);
2258 fprintf (dump_file, " for parameter ");
2259 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2260 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
2264 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
2265 freq_sum, count_sum,
2266 val->local_size_cost)
2267 && !good_cloning_opportunity_p (node,
2268 val->local_time_benefit
2269 + val->prop_time_benefit,
2270 freq_sum, count_sum,
2271 val->local_size_cost
2272 + val->prop_size_cost))
2273 continue;
2275 if (dump_file)
2276 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
2277 cgraph_node_name (node), node->uid);
2279 callers = gather_edges_for_value (val, caller_count);
2280 kv = VEC_copy (tree, heap, known_csts);
2281 move_binfos_to_values (kv, known_binfos);
2282 VEC_replace (tree, kv, i, val->value);
2283 find_more_values_for_callers_subset (node, kv, callers);
2284 val->spec_node = create_specialized_node (node, kv, callers);
2285 overall_size += val->local_size_cost;
2286 info = IPA_NODE_REF (node);
2288 /* TODO: If for some lattice there is only one other known value
2289 left, make a special node for it too. */
2290 ret = true;
2292 VEC_replace (tree, kv, i, val->value);
2296 if (info->clone_for_all_contexts)
2298 VEC (cgraph_edge_p, heap) *callers;
2300 if (dump_file)
2301 fprintf (dump_file, " - Creating a specialized node of %s/%i "
2302 "for all known contexts.\n", cgraph_node_name (node),
2303 node->uid);
2305 callers = collect_callers_of_node (node);
2306 move_binfos_to_values (known_csts, known_binfos);
2307 create_specialized_node (node, known_csts, callers);
2308 info = IPA_NODE_REF (node);
2309 info->clone_for_all_contexts = false;
2310 ret = true;
2312 else
2313 VEC_free (tree, heap, known_csts);
2315 VEC_free (tree, heap, known_binfos);
2316 return ret;
2319 /* Transitively mark all callees of NODE within the same SCC as not dead. */
2321 static void
2322 spread_undeadness (struct cgraph_node *node)
2324 struct cgraph_edge *cs;
2326 for (cs = node->callees; cs; cs = cs->next_callee)
2327 if (edge_within_scc (cs))
2329 struct cgraph_node *callee;
2330 struct ipa_node_params *info;
2332 callee = cgraph_function_node (cs->callee, NULL);
2333 info = IPA_NODE_REF (callee);
2335 if (info->node_dead)
2337 info->node_dead = 0;
2338 spread_undeadness (callee);
2343 /* Return true if NODE has a caller from outside of its SCC that is not
2344 dead. Worker callback for cgraph_for_node_and_aliases. */
2346 static bool
2347 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
2348 void *data ATTRIBUTE_UNUSED)
2350 struct cgraph_edge *cs;
2352 for (cs = node->callers; cs; cs = cs->next_caller)
2353 if (cs->caller->thunk.thunk_p
2354 && cgraph_for_node_and_aliases (cs->caller,
2355 has_undead_caller_from_outside_scc_p,
2356 NULL, true))
2357 return true;
2358 else if (!edge_within_scc (cs)
2359 && !IPA_NODE_REF (cs->caller)->node_dead)
2360 return true;
2361 return false;
2365 /* Identify nodes within the same SCC as NODE which are no longer needed
2366 because of new clones and will be removed as unreachable. */
2368 static void
2369 identify_dead_nodes (struct cgraph_node *node)
2371 struct cgraph_node *v;
2372 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
2373 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
2374 && !cgraph_for_node_and_aliases (v,
2375 has_undead_caller_from_outside_scc_p,
2376 NULL, true))
2377 IPA_NODE_REF (v)->node_dead = 1;
2379 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
2380 if (!IPA_NODE_REF (v)->node_dead)
2381 spread_undeadness (v);
2383 if (dump_file && (dump_flags & TDF_DETAILS))
2385 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
2386 if (IPA_NODE_REF (v)->node_dead)
2387 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
2388 cgraph_node_name (v), v->uid);
2392 /* The decision stage. Iterate over the topological order of call graph nodes
2393 TOPO and make specialized clones if deemed beneficial. */
2395 static void
2396 ipcp_decision_stage (struct topo_info *topo)
2398 int i;
2400 if (dump_file)
2401 fprintf (dump_file, "\nIPA decision stage:\n\n");
2403 for (i = topo->nnodes - 1; i >= 0; i--)
2405 struct cgraph_node *node = topo->order[i];
2406 bool change = false, iterate = true;
2408 while (iterate)
2410 struct cgraph_node *v;
2411 iterate = false;
2412 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
2413 if (cgraph_function_with_gimple_body_p (v)
2414 && ipcp_versionable_function_p (v))
2415 iterate |= decide_whether_version_node (v);
2417 change |= iterate;
2419 if (change)
2420 identify_dead_nodes (node);
2424 /* The IPCP driver. */
2426 static unsigned int
2427 ipcp_driver (void)
2429 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
2430 struct topo_info topo;
2432 ipa_check_create_node_params ();
2433 ipa_check_create_edge_args ();
2434 grow_next_edge_clone_vector ();
2435 edge_duplication_hook_holder =
2436 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
2437 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
2438 sizeof (struct ipcp_value), 32);
2439 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
2440 sizeof (struct ipcp_value_source), 64);
2441 if (dump_file)
2443 fprintf (dump_file, "\nIPA structures before propagation:\n");
2444 if (dump_flags & TDF_DETAILS)
2445 ipa_print_all_params (dump_file);
2446 ipa_print_all_jump_functions (dump_file);
2449 /* Topological sort. */
2450 build_toporder_info (&topo);
2451 /* Do the interprocedural propagation. */
2452 ipcp_propagate_stage (&topo);
2453 /* Decide what constant propagation and cloning should be performed. */
2454 ipcp_decision_stage (&topo);
2456 /* Free all IPCP structures. */
2457 free_toporder_info (&topo);
2458 VEC_free (cgraph_edge_p, heap, next_edge_clone);
2459 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2460 ipa_free_all_structures_after_ipa_cp ();
2461 if (dump_file)
2462 fprintf (dump_file, "\nIPA constant propagation end\n");
2463 return 0;
2466 /* Initialization and computation of IPCP data structures. This is the initial
2467 intraprocedural analysis of functions, which gathers information to be
2468 propagated later on. */
2470 static void
2471 ipcp_generate_summary (void)
2473 struct cgraph_node *node;
2475 if (dump_file)
2476 fprintf (dump_file, "\nIPA constant propagation start:\n");
2477 ipa_register_cgraph_hooks ();
2479 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2481 node->local.versionable
2482 = tree_versionable_function_p (node->symbol.decl);
2483 ipa_analyze_node (node);
2487 /* Write ipcp summary for nodes in SET. */
2489 static void
2490 ipcp_write_summary (cgraph_node_set set,
2491 varpool_node_set vset ATTRIBUTE_UNUSED)
2493 ipa_prop_write_jump_functions (set);
2496 /* Read ipcp summary. */
2498 static void
2499 ipcp_read_summary (void)
2501 ipa_prop_read_jump_functions ();
2504 /* Gate for IPCP optimization. */
2506 static bool
2507 cgraph_gate_cp (void)
2509 /* FIXME: We should remove the optimize check after we ensure we never run
2510 IPA passes when not optimizing. */
2511 return flag_ipa_cp && optimize;
2514 struct ipa_opt_pass_d pass_ipa_cp =
2517 IPA_PASS,
2518 "cp", /* name */
2519 cgraph_gate_cp, /* gate */
2520 ipcp_driver, /* execute */
2521 NULL, /* sub */
2522 NULL, /* next */
2523 0, /* static_pass_number */
2524 TV_IPA_CONSTANT_PROP, /* tv_id */
2525 0, /* properties_required */
2526 0, /* properties_provided */
2527 0, /* properties_destroyed */
2528 0, /* todo_flags_start */
2529 TODO_dump_symtab |
2530 TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
2532 ipcp_generate_summary, /* generate_summary */
2533 ipcp_write_summary, /* write_summary */
2534 ipcp_read_summary, /* read_summary */
2535 NULL, /* write_optimization_summary */
2536 NULL, /* read_optimization_summary */
2537 NULL, /* stmt_fixup */
2538 0, /* TODOs */
2539 NULL, /* function_transform */
2540 NULL, /* variable_transform */