1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
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
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
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
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
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
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 dependant 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
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
106 #include "coretypes.h"
111 #include "ipa-prop.h"
112 #include "tree-flow.h"
113 #include "tree-pass.h"
116 #include "diagnostic.h"
117 #include "tree-pretty-print.h"
118 #include "tree-dump.h"
119 #include "tree-inline.h"
122 #include "ipa-inline.h"
123 #include "ipa-utils.h"
127 /* Describes a particular source for an IPA-CP value. */
129 struct ipcp_value_source
131 /* The incoming edge that brought the value. */
132 struct cgraph_edge *cs;
133 /* If the jump function that resulted into his value was a pass-through or an
134 ancestor, this is the ipcp_value of the caller from which the described
135 value has been derived. Otherwise it is NULL. */
136 struct ipcp_value *val;
137 /* Next pointer in a linked list of sources of a value. */
138 struct ipcp_value_source *next;
139 /* If the jump function that resulted into his value was a pass-through or an
140 ancestor, this is the index of the parameter of the caller the jump
141 function references. */
145 /* Describes one particular value stored in struct ipcp_lattice. */
149 /* The actual value for the given parameter. This is either an IPA invariant
150 or a TREE_BINFO describing a type that can be used for
153 /* The list of sources from which this value originates. */
154 struct ipcp_value_source *sources;
155 /* Next pointers in a linked list of all values in a lattice. */
156 struct ipcp_value *next;
157 /* Next pointers in a linked list of values in a strongly connected component
159 struct ipcp_value *scc_next;
160 /* Next pointers in a linked list of SCCs of values sorted topologically
161 according their sources. */
162 struct ipcp_value *topo_next;
163 /* A specialized node created for this value, NULL if none has been (so far)
165 struct cgraph_node *spec_node;
166 /* Depth first search number and low link for topological sorting of
169 /* Time benefit and size cost that specializing the function for this value
170 would bring about in this function alone. */
171 int local_time_benefit, local_size_cost;
172 /* Time benefit and size cost that specializing the function for this value
173 can bring about in it's callees (transitively). */
174 int prop_time_benefit, prop_size_cost;
175 /* True if this valye is currently on the topo-sort stack. */
179 /* Allocation pools for values and their sources in ipa-cp. */
181 alloc_pool ipcp_values_pool;
182 alloc_pool ipcp_sources_pool;
184 /* Lattice describing potential values of a formal parameter of a function and
185 some of their other properties. TOP is represented by a lattice with zero
186 values and with contains_variable and bottom flags cleared. BOTTOM is
187 represented by a lattice with the bottom flag set. In that case, values and
188 contains_variable flag should be disregarded. */
192 /* The list of known values and types in this lattice. Note that values are
193 not deallocated if a lattice is set to bottom because there may be value
194 sources referencing them. */
195 struct ipcp_value *values;
196 /* Number of known values and types in this lattice. */
198 /* The lattice contains a variable component (in addition to values). */
199 bool contains_variable;
200 /* The value of the lattice is bottom (i.e. variable and unusable for any
203 /* There is a virtual call based on this parameter. */
207 /* Maximal count found in program. */
209 static gcov_type max_count;
211 /* Original overall size of the program. */
213 static long overall_size, max_new_size;
215 /* Head of the linked list of topologically sorted values. */
217 static struct ipcp_value *values_topo;
219 /* Return the lattice corresponding to the Ith formal parameter of the function
220 described by INFO. */
221 static inline struct ipcp_lattice *
222 ipa_get_lattice (struct ipa_node_params *info, int i)
224 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
225 gcc_checking_assert (!info->ipcp_orig_node);
226 gcc_checking_assert (info->lattices);
227 return &(info->lattices[i]);
230 /* Return whether LAT is a lattice with a single constant and without an
234 ipa_lat_is_single_const (struct ipcp_lattice *lat)
237 || lat->contains_variable
238 || lat->values_count != 1)
244 /* Return true iff the CS is an edge within a strongly connected component as
245 computed by ipa_reduced_postorder. */
248 edge_within_scc (struct cgraph_edge *cs)
250 struct ipa_dfs_info *caller_dfs = (struct ipa_dfs_info *) cs->caller->aux;
251 struct ipa_dfs_info *callee_dfs;
252 struct cgraph_node *callee = cgraph_function_node (cs->callee, NULL);
254 callee_dfs = (struct ipa_dfs_info *) callee->aux;
257 && caller_dfs->scc_no == callee_dfs->scc_no);
260 /* Print V which is extracted from a value in a lattice to F. */
263 print_ipcp_constant_value (FILE * f, tree v)
265 if (TREE_CODE (v) == TREE_BINFO)
267 fprintf (f, "BINFO ");
268 print_generic_expr (f, BINFO_TYPE (v), 0);
270 else if (TREE_CODE (v) == ADDR_EXPR
271 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
274 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
277 print_generic_expr (f, v, 0);
280 /* Print all ipcp_lattices of all functions to F. */
283 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
285 struct cgraph_node *node;
288 fprintf (f, "\nLattices:\n");
289 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
291 struct ipa_node_params *info;
293 info = IPA_NODE_REF (node);
294 fprintf (f, " Node: %s/%i:\n", cgraph_node_name (node), node->uid);
295 count = ipa_get_param_count (info);
296 for (i = 0; i < count; i++)
298 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
299 struct ipcp_value *val;
302 fprintf (f, " param [%d]: ", i);
305 fprintf (f, "BOTTOM\n");
309 if (!lat->values_count && !lat->contains_variable)
311 fprintf (f, "TOP\n");
315 if (lat->contains_variable)
317 fprintf (f, "VARIABLE");
323 for (val = lat->values; val; val = val->next)
325 if (dump_benefits && prev)
327 else if (!dump_benefits && prev)
332 print_ipcp_constant_value (f, val->value);
336 struct ipcp_value_source *s;
338 fprintf (f, " [from:");
339 for (s = val->sources; s; s = s->next)
340 fprintf (f, " %i(%i)", s->cs->caller->uid,s->cs->frequency);
345 fprintf (f, " [loc_time: %i, loc_size: %i, "
346 "prop_time: %i, prop_size: %i]\n",
347 val->local_time_benefit, val->local_size_cost,
348 val->prop_time_benefit, val->prop_size_cost);
356 /* Determine whether it is at all technically possible to create clones of NODE
357 and store this information in the ipa_node_params structure associated
361 determine_versionability (struct cgraph_node *node)
363 const char *reason = NULL;
365 /* There are a number of generic reasons functions cannot be versioned. We
366 also cannot remove parameters if there are type attributes such as fnspec
368 if (node->alias || node->thunk.thunk_p)
369 reason = "alias or thunk";
370 else if (!node->local.versionable)
371 reason = "not a tree_versionable_function";
372 else if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
373 reason = "insufficient body availability";
375 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
376 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
377 cgraph_node_name (node), node->uid, reason);
379 node->local.versionable = (reason == NULL);
382 /* Return true if it is at all technically possible to create clones of a
386 ipcp_versionable_function_p (struct cgraph_node *node)
388 return node->local.versionable;
391 /* Structure holding accumulated information about callers of a node. */
393 struct caller_statistics
396 int n_calls, n_hot_calls, freq_sum;
399 /* Initialize fields of STAT to zeroes. */
402 init_caller_stats (struct caller_statistics *stats)
404 stats->count_sum = 0;
406 stats->n_hot_calls = 0;
410 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
411 non-thunk incoming edges to NODE. */
414 gather_caller_stats (struct cgraph_node *node, void *data)
416 struct caller_statistics *stats = (struct caller_statistics *) data;
417 struct cgraph_edge *cs;
419 for (cs = node->callers; cs; cs = cs->next_caller)
420 if (cs->caller->thunk.thunk_p)
421 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
425 stats->count_sum += cs->count;
426 stats->freq_sum += cs->frequency;
428 if (cgraph_maybe_hot_edge_p (cs))
429 stats->n_hot_calls ++;
435 /* Return true if this NODE is viable candidate for cloning. */
438 ipcp_cloning_candidate_p (struct cgraph_node *node)
440 struct caller_statistics stats;
442 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
444 if (!flag_ipa_cp_clone)
447 fprintf (dump_file, "Not considering %s for cloning; "
448 "-fipa-cp-clone disabled.\n",
449 cgraph_node_name (node));
453 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
456 fprintf (dump_file, "Not considering %s for cloning; "
457 "optimizing it for size.\n",
458 cgraph_node_name (node));
462 init_caller_stats (&stats);
463 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
465 if (inline_summary (node)->self_size < stats.n_calls)
468 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
469 cgraph_node_name (node));
473 /* When profile is available and function is hot, propagate into it even if
474 calls seems cold; constant propagation can improve function's speed
478 if (stats.count_sum > node->count * 90 / 100)
481 fprintf (dump_file, "Considering %s for cloning; "
482 "usually called directly.\n",
483 cgraph_node_name (node));
487 if (!stats.n_hot_calls)
490 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
491 cgraph_node_name (node));
495 fprintf (dump_file, "Considering %s for cloning.\n",
496 cgraph_node_name (node));
500 /* Arrays representing a topological ordering of call graph nodes and a stack
501 of noes used during constant propagation. */
505 struct cgraph_node **order;
506 struct cgraph_node **stack;
507 int nnodes, stack_top;
510 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
513 build_toporder_info (struct topo_info *topo)
515 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
516 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
518 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
521 /* Free information about strongly connected components and the arrays in
525 free_toporder_info (struct topo_info *topo)
527 ipa_free_postorder_info ();
532 /* Add NODE to the stack in TOPO, unless it is already there. */
535 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
537 struct ipa_node_params *info = IPA_NODE_REF (node);
538 if (info->node_enqueued)
540 info->node_enqueued = 1;
541 topo->stack[topo->stack_top++] = node;
544 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
547 static struct cgraph_node *
548 pop_node_from_stack (struct topo_info *topo)
552 struct cgraph_node *node;
554 node = topo->stack[topo->stack_top];
555 IPA_NODE_REF (node)->node_enqueued = 0;
562 /* Set lattice LAT to bottom and return true if it previously was not set as
566 set_lattice_to_bottom (struct ipcp_lattice *lat)
568 bool ret = !lat->bottom;
573 /* Mark lattice as containing an unknown value and return true if it previously
574 was not marked as such. */
577 set_lattice_contains_variable (struct ipcp_lattice *lat)
579 bool ret = !lat->contains_variable;
580 lat->contains_variable = true;
584 /* Initialize ipcp_lattices. */
587 initialize_node_lattices (struct cgraph_node *node)
589 struct ipa_node_params *info = IPA_NODE_REF (node);
590 struct cgraph_edge *ie;
591 bool disable = false, variable = false;
594 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
595 if (!node->local.local)
597 /* When cloning is allowed, we can assume that externally visible
598 functions are not called. We will compensate this by cloning
600 if (ipcp_versionable_function_p (node)
601 && ipcp_cloning_candidate_p (node))
607 if (disable || variable)
609 for (i = 0; i < ipa_get_param_count (info) ; i++)
611 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
613 set_lattice_to_bottom (lat);
615 set_lattice_contains_variable (lat);
617 if (dump_file && (dump_flags & TDF_DETAILS)
618 && node->alias && node->thunk.thunk_p)
619 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
620 cgraph_node_name (node), node->uid,
621 disable ? "BOTTOM" : "VARIABLE");
624 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
625 if (ie->indirect_info->polymorphic)
627 gcc_checking_assert (ie->indirect_info->param_index >= 0);
628 ipa_get_lattice (info, ie->indirect_info->param_index)->virt_call = 1;
632 /* Return the result of a (possibly arithmetic) pass through jump function
633 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
634 determined or itself is considered an interprocedural invariant. */
637 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
641 gcc_checking_assert (is_gimple_ip_invariant (input));
642 if (jfunc->value.pass_through.operation == NOP_EXPR)
645 if (TREE_CODE_CLASS (jfunc->value.pass_through.operation)
647 restype = boolean_type_node;
649 restype = TREE_TYPE (input);
650 res = fold_binary (jfunc->value.pass_through.operation, restype,
651 input, jfunc->value.pass_through.operand);
653 if (res && !is_gimple_ip_invariant (res))
659 /* Return the result of an ancestor jump function JFUNC on the constant value
660 INPUT. Return NULL_TREE if that cannot be determined. */
663 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
665 if (TREE_CODE (input) == ADDR_EXPR)
667 tree t = TREE_OPERAND (input, 0);
668 t = build_ref_for_offset (EXPR_LOCATION (t), t,
669 jfunc->value.ancestor.offset,
670 jfunc->value.ancestor.type, NULL, false);
671 return build_fold_addr_expr (t);
677 /* Extract the acual BINFO being described by JFUNC which must be a known type
681 ipa_value_from_known_type_jfunc (struct ipa_jump_func *jfunc)
683 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
686 return get_binfo_at_offset (base_binfo,
687 jfunc->value.known_type.offset,
688 jfunc->value.known_type.component_type);
691 /* Determine whether JFUNC evaluates to a known value (that is either a
692 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
693 describes the caller node so that pass-through jump functions can be
697 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
699 if (jfunc->type == IPA_JF_CONST)
700 return jfunc->value.constant;
701 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
702 return ipa_value_from_known_type_jfunc (jfunc);
703 else if (jfunc->type == IPA_JF_PASS_THROUGH
704 || jfunc->type == IPA_JF_ANCESTOR)
709 if (jfunc->type == IPA_JF_PASS_THROUGH)
710 idx = jfunc->value.pass_through.formal_id;
712 idx = jfunc->value.ancestor.formal_id;
714 if (info->ipcp_orig_node)
715 input = VEC_index (tree, info->known_vals, idx);
718 struct ipcp_lattice *lat;
722 gcc_checking_assert (!flag_ipa_cp);
725 lat = ipa_get_lattice (info, idx);
726 if (!ipa_lat_is_single_const (lat))
728 input = lat->values->value;
734 if (jfunc->type == IPA_JF_PASS_THROUGH)
736 if (jfunc->value.pass_through.operation == NOP_EXPR)
738 else if (TREE_CODE (input) == TREE_BINFO)
741 return ipa_get_jf_pass_through_result (jfunc, input);
745 if (TREE_CODE (input) == TREE_BINFO)
746 return get_binfo_at_offset (input, jfunc->value.ancestor.offset,
747 jfunc->value.ancestor.type);
749 return ipa_get_jf_ancestor_result (jfunc, input);
757 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
758 bottom, not containing a variable component and without any known value at
762 ipcp_verify_propagated_values (void)
764 struct cgraph_node *node;
766 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
768 struct ipa_node_params *info = IPA_NODE_REF (node);
769 int i, count = ipa_get_param_count (info);
771 for (i = 0; i < count; i++)
773 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
776 && !lat->contains_variable
777 && lat->values_count == 0)
781 fprintf (dump_file, "\nIPA lattices after constant "
783 print_all_lattices (dump_file, true, false);
792 /* Return true iff X and Y should be considered equal values by IPA-CP. */
795 values_equal_for_ipcp_p (tree x, tree y)
797 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
802 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
805 if (TREE_CODE (x) == ADDR_EXPR
806 && TREE_CODE (y) == ADDR_EXPR
807 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
808 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
809 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
810 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
812 return operand_equal_p (x, y, 0);
815 /* Add a new value source to VAL, marking that a value comes from edge CS and
816 (if the underlying jump function is a pass-through or an ancestor one) from
817 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. */
820 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
821 struct ipcp_value *src_val, int src_idx)
823 struct ipcp_value_source *src;
825 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
828 src->index = src_idx;
830 src->next = val->sources;
835 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
836 it. CS, SRC_VAL and SRC_INDEX are meant for add_value_source and have the
840 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
841 struct cgraph_edge *cs, struct ipcp_value *src_val,
844 struct ipcp_value *val;
850 for (val = lat->values; val; val = val->next)
851 if (values_equal_for_ipcp_p (val->value, newval))
853 if (edge_within_scc (cs))
855 struct ipcp_value_source *s;
856 for (s = val->sources; s ; s = s->next)
863 add_value_source (val, cs, src_val, src_idx);
867 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
869 /* We can only free sources, not the values themselves, because sources
870 of other values in this this SCC might point to them. */
871 for (val = lat->values; val; val = val->next)
875 struct ipcp_value_source *src = val->sources;
876 val->sources = src->next;
877 pool_free (ipcp_sources_pool, src);
882 return set_lattice_to_bottom (lat);
886 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
887 memset (val, 0, sizeof (*val));
889 add_value_source (val, cs, src_val, src_idx);
891 val->next = lat->values;
896 /* Propagate values through a pass-through jump function JFUNC associated with
897 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
898 is the index of the source parameter. */
901 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
902 struct ipa_jump_func *jfunc,
903 struct ipcp_lattice *src_lat,
904 struct ipcp_lattice *dest_lat,
907 struct ipcp_value *src_val;
910 if (jfunc->value.pass_through.operation == NOP_EXPR)
911 for (src_val = src_lat->values; src_val; src_val = src_val->next)
912 ret |= add_value_to_lattice (dest_lat, src_val->value, cs,
914 /* Do not create new values when propagating within an SCC because if there
915 arithmetic functions with circular dependencies, there is infinite number
916 of them and we would just make lattices bottom. */
917 else if (edge_within_scc (cs))
918 ret = set_lattice_contains_variable (dest_lat);
920 for (src_val = src_lat->values; src_val; src_val = src_val->next)
922 tree cstval = src_val->value;
924 if (TREE_CODE (cstval) == TREE_BINFO)
926 ret |= set_lattice_contains_variable (dest_lat);
929 cstval = ipa_get_jf_pass_through_result (jfunc, cstval);
932 ret |= add_value_to_lattice (dest_lat, cstval, cs, src_val, src_idx);
934 ret |= set_lattice_contains_variable (dest_lat);
940 /* Propagate values through an ancestor jump function JFUNC associated with
941 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
942 is the index of the source parameter. */
945 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
946 struct ipa_jump_func *jfunc,
947 struct ipcp_lattice *src_lat,
948 struct ipcp_lattice *dest_lat,
951 struct ipcp_value *src_val;
954 if (edge_within_scc (cs))
955 return set_lattice_contains_variable (dest_lat);
957 for (src_val = src_lat->values; src_val; src_val = src_val->next)
959 tree t = src_val->value;
961 if (TREE_CODE (t) == TREE_BINFO)
962 t = get_binfo_at_offset (t, jfunc->value.ancestor.offset,
963 jfunc->value.ancestor.type);
965 t = ipa_get_jf_ancestor_result (jfunc, t);
968 ret |= add_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
970 ret |= set_lattice_contains_variable (dest_lat);
976 /* Propagate values across jump function JFUNC that is associated with edge CS
977 and put the values into DEST_LAT. */
980 propagate_accross_jump_function (struct cgraph_edge *cs,
981 struct ipa_jump_func *jfunc,
982 struct ipcp_lattice *dest_lat)
984 if (dest_lat->bottom)
987 if (jfunc->type == IPA_JF_CONST
988 || jfunc->type == IPA_JF_KNOWN_TYPE)
992 if (jfunc->type == IPA_JF_KNOWN_TYPE)
994 val = ipa_value_from_known_type_jfunc (jfunc);
996 return set_lattice_contains_variable (dest_lat);
999 val = jfunc->value.constant;
1000 return add_value_to_lattice (dest_lat, val, cs, NULL, 0);
1002 else if (jfunc->type == IPA_JF_PASS_THROUGH
1003 || jfunc->type == IPA_JF_ANCESTOR)
1005 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1006 struct ipcp_lattice *src_lat;
1010 if (jfunc->type == IPA_JF_PASS_THROUGH)
1011 src_idx = jfunc->value.pass_through.formal_id;
1013 src_idx = jfunc->value.ancestor.formal_id;
1015 src_lat = ipa_get_lattice (caller_info, src_idx);
1016 if (src_lat->bottom)
1017 return set_lattice_contains_variable (dest_lat);
1019 /* If we would need to clone the caller and cannot, do not propagate. */
1020 if (!ipcp_versionable_function_p (cs->caller)
1021 && (src_lat->contains_variable
1022 || (src_lat->values_count > 1)))
1023 return set_lattice_contains_variable (dest_lat);
1025 if (jfunc->type == IPA_JF_PASS_THROUGH)
1026 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1029 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1032 if (src_lat->contains_variable)
1033 ret |= set_lattice_contains_variable (dest_lat);
1038 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1039 use it for indirect inlining), we should propagate them too. */
1040 return set_lattice_contains_variable (dest_lat);
1043 /* Propagate constants from the caller to the callee of CS. INFO describes the
1047 propagate_constants_accross_call (struct cgraph_edge *cs)
1049 struct ipa_node_params *callee_info;
1050 enum availability availability;
1051 struct cgraph_node *callee, *alias_or_thunk;
1052 struct ipa_edge_args *args;
1054 int i, args_count, parms_count;
1056 callee = cgraph_function_node (cs->callee, &availability);
1057 if (!callee->analyzed)
1059 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1060 callee_info = IPA_NODE_REF (callee);
1062 args = IPA_EDGE_REF (cs);
1063 args_count = ipa_get_cs_argument_count (args);
1064 parms_count = ipa_get_param_count (callee_info);
1066 /* If this call goes through a thunk we must not propagate to the first (0th)
1067 parameter. However, we might need to uncover a thunk from below a series
1068 of aliases first. */
1069 alias_or_thunk = cs->callee;
1070 while (alias_or_thunk->alias)
1071 alias_or_thunk = cgraph_alias_aliased_node (alias_or_thunk);
1072 if (alias_or_thunk->thunk.thunk_p)
1074 ret |= set_lattice_contains_variable (ipa_get_lattice (callee_info, 0));
1080 for (; (i < args_count) && (i < parms_count); i++)
1082 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1083 struct ipcp_lattice *dest_lat = ipa_get_lattice (callee_info, i);
1085 if (availability == AVAIL_OVERWRITABLE)
1086 ret |= set_lattice_contains_variable (dest_lat);
1088 ret |= propagate_accross_jump_function (cs, jump_func, dest_lat);
1090 for (; i < parms_count; i++)
1091 ret |= set_lattice_contains_variable (ipa_get_lattice (callee_info, i));
1096 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1097 (which can contain both constants and binfos) or KNOWN_BINFOS (which can be
1098 NULL) return the destination. */
1101 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1102 VEC (tree, heap) *known_vals,
1103 VEC (tree, heap) *known_binfos)
1105 int param_index = ie->indirect_info->param_index;
1106 HOST_WIDE_INT token, anc_offset;
1110 if (param_index == -1)
1113 if (!ie->indirect_info->polymorphic)
1115 tree t = VEC_index (tree, known_vals, param_index);
1117 TREE_CODE (t) == ADDR_EXPR
1118 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1119 return TREE_OPERAND (t, 0);
1124 token = ie->indirect_info->otr_token;
1125 anc_offset = ie->indirect_info->anc_offset;
1126 otr_type = ie->indirect_info->otr_type;
1128 t = VEC_index (tree, known_vals, param_index);
1129 if (!t && known_binfos)
1130 t = VEC_index (tree, known_binfos, param_index);
1134 if (TREE_CODE (t) != TREE_BINFO)
1137 binfo = gimple_extract_devirt_binfo_from_cst (t);
1140 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1143 return gimple_get_virt_method_for_binfo (token, binfo);
1149 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1152 return gimple_get_virt_method_for_binfo (token, binfo);
1156 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1157 and KNOWN_BINFOS. */
1160 devirtualization_time_bonus (struct cgraph_node *node,
1161 VEC (tree, heap) *known_csts,
1162 VEC (tree, heap) *known_binfos)
1164 struct cgraph_edge *ie;
1167 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1169 struct cgraph_node *callee;
1170 struct inline_summary *isummary;
1173 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos);
1177 /* Only bare minimum benefit for clearly un-inlineable targets. */
1179 callee = cgraph_get_node (target);
1180 if (!callee || !callee->analyzed)
1182 isummary = inline_summary (callee);
1183 if (!isummary->inlinable)
1186 /* FIXME: The values below need re-considering and perhaps also
1187 integrating into the cost metrics, at lest in some very basic way. */
1188 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1190 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1192 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1193 || DECL_DECLARED_INLINE_P (callee->decl))
1200 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1201 and SIZE_COST and with the sum of frequencies of incoming edges to the
1202 potential new clone in FREQUENCIES. */
1205 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1206 int freq_sum, gcov_type count_sum, int size_cost)
1208 if (time_benefit == 0
1209 || !flag_ipa_cp_clone
1210 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
1213 gcc_checking_assert (size_cost >= 0);
1215 /* FIXME: These decisions need tuning. */
1218 int evaluation, factor = (count_sum * 1000) / max_count;
1220 evaluation = (time_benefit * factor) / size_cost;
1222 if (dump_file && (dump_flags & TDF_DETAILS))
1223 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1224 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1225 ") -> evaluation: %i, threshold: %i\n",
1226 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1229 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1233 int evaluation = (time_benefit * freq_sum) / size_cost;
1235 if (dump_file && (dump_flags & TDF_DETAILS))
1236 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1237 "size: %i, freq_sum: %i) -> evaluation: %i, threshold: %i\n",
1238 time_benefit, size_cost, freq_sum, evaluation,
1239 CGRAPH_FREQ_BASE /2);
1241 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1246 /* Allocate KNOWN_CSTS and KNOWN_BINFOS and populate them with values of
1247 parameters that are known independent of the context. INFO describes the
1248 function. If REMOVABLE_PARAMS_COST is non-NULL, the movement cost of all
1249 removable parameters will be stored in it. */
1252 gather_context_independent_values (struct ipa_node_params *info,
1253 VEC (tree, heap) **known_csts,
1254 VEC (tree, heap) **known_binfos,
1255 int *removable_params_cost)
1257 int i, count = ipa_get_param_count (info);
1261 *known_binfos = NULL;
1262 VEC_safe_grow_cleared (tree, heap, *known_csts, count);
1263 VEC_safe_grow_cleared (tree, heap, *known_binfos, count);
1265 if (removable_params_cost)
1266 *removable_params_cost = 0;
1268 for (i = 0; i < count ; i++)
1270 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1272 if (ipa_lat_is_single_const (lat))
1274 struct ipcp_value *val = lat->values;
1275 if (TREE_CODE (val->value) != TREE_BINFO)
1277 VEC_replace (tree, *known_csts, i, val->value);
1278 if (removable_params_cost)
1279 *removable_params_cost
1280 += estimate_move_cost (TREE_TYPE (val->value));
1283 else if (lat->virt_call)
1285 VEC_replace (tree, *known_binfos, i, val->value);
1288 else if (removable_params_cost
1289 && !ipa_is_param_used (info, i))
1290 *removable_params_cost
1291 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1293 else if (removable_params_cost
1294 && !ipa_is_param_used (info, i))
1295 *removable_params_cost
1296 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1302 /* Iterate over known values of parameters of NODE and estimate the local
1303 effects in terms of time and size they have. */
1306 estimate_local_effects (struct cgraph_node *node)
1308 struct ipa_node_params *info = IPA_NODE_REF (node);
1309 int i, count = ipa_get_param_count (info);
1310 VEC (tree, heap) *known_csts, *known_binfos;
1312 int base_time = inline_summary (node)->time;
1313 int removable_params_cost;
1315 if (!count || !ipcp_versionable_function_p (node))
1318 if (dump_file && (dump_flags & TDF_DETAILS))
1319 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1320 cgraph_node_name (node), node->uid, base_time);
1322 always_const = gather_context_independent_values (info, &known_csts,
1324 &removable_params_cost);
1327 struct caller_statistics stats;
1330 init_caller_stats (&stats);
1331 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1332 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1334 time -= devirtualization_time_bonus (node, known_csts, known_binfos);
1335 time -= removable_params_cost;
1336 size -= stats.n_calls * removable_params_cost;
1339 fprintf (dump_file, " - context independent values, size: %i, "
1340 "time_benefit: %i\n", size, base_time - time);
1343 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1345 info->clone_for_all_contexts = true;
1349 fprintf (dump_file, " Decided to specialize for all "
1350 "known contexts, code not going to grow.\n");
1352 else if (good_cloning_opportunity_p (node, base_time - time,
1353 stats.freq_sum, stats.count_sum,
1356 if (size + overall_size <= max_new_size)
1358 info->clone_for_all_contexts = true;
1360 overall_size += size;
1363 fprintf (dump_file, " Decided to specialize for all "
1364 "known contexts, growth deemed beneficial.\n");
1366 else if (dump_file && (dump_flags & TDF_DETAILS))
1367 fprintf (dump_file, " Not cloning for all contexts because "
1368 "max_new_size would be reached with %li.\n",
1369 size + overall_size);
1373 for (i = 0; i < count ; i++)
1375 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1376 struct ipcp_value *val;
1381 || VEC_index (tree, known_csts, i)
1382 || VEC_index (tree, known_binfos, i))
1385 for (val = lat->values; val; val = val->next)
1387 int time, size, time_benefit;
1389 if (TREE_CODE (val->value) != TREE_BINFO)
1391 VEC_replace (tree, known_csts, i, val->value);
1392 VEC_replace (tree, known_binfos, i, NULL_TREE);
1393 emc = estimate_move_cost (TREE_TYPE (val->value));
1395 else if (lat->virt_call)
1397 VEC_replace (tree, known_csts, i, NULL_TREE);
1398 VEC_replace (tree, known_binfos, i, val->value);
1404 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1406 time_benefit = base_time - time
1407 + devirtualization_time_bonus (node, known_csts, known_binfos)
1408 + removable_params_cost + emc;
1410 if (dump_file && (dump_flags & TDF_DETAILS))
1412 fprintf (dump_file, " - estimates for value ");
1413 print_ipcp_constant_value (dump_file, val->value);
1414 fprintf (dump_file, " for parameter ");
1415 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
1416 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
1417 time_benefit, size);
1420 val->local_time_benefit = time_benefit;
1421 val->local_size_cost = size;
1425 VEC_free (tree, heap, known_csts);
1426 VEC_free (tree, heap, known_binfos);
1430 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
1431 topological sort of values. */
1434 add_val_to_toposort (struct ipcp_value *cur_val)
1436 static int dfs_counter = 0;
1437 static struct ipcp_value *stack;
1438 struct ipcp_value_source *src;
1444 cur_val->dfs = dfs_counter;
1445 cur_val->low_link = dfs_counter;
1447 cur_val->topo_next = stack;
1449 cur_val->on_stack = true;
1451 for (src = cur_val->sources; src; src = src->next)
1454 if (src->val->dfs == 0)
1456 add_val_to_toposort (src->val);
1457 if (src->val->low_link < cur_val->low_link)
1458 cur_val->low_link = src->val->low_link;
1460 else if (src->val->on_stack
1461 && src->val->dfs < cur_val->low_link)
1462 cur_val->low_link = src->val->dfs;
1465 if (cur_val->dfs == cur_val->low_link)
1467 struct ipcp_value *v, *scc_list = NULL;
1472 stack = v->topo_next;
1473 v->on_stack = false;
1475 v->scc_next = scc_list;
1478 while (v != cur_val);
1480 cur_val->topo_next = values_topo;
1481 values_topo = cur_val;
1485 /* Add all values in lattices associated with NODE to the topological sort if
1486 they are not there yet. */
1489 add_all_node_vals_to_toposort (struct cgraph_node *node)
1491 struct ipa_node_params *info = IPA_NODE_REF (node);
1492 int i, count = ipa_get_param_count (info);
1494 for (i = 0; i < count ; i++)
1496 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1497 struct ipcp_value *val;
1499 if (lat->bottom || !lat->values)
1501 for (val = lat->values; val; val = val->next)
1502 add_val_to_toposort (val);
1506 /* One pass of constants propagation along the call graph edges, from callers
1507 to callees (requires topological ordering in TOPO), iterate over strongly
1508 connected components. */
1511 propagate_constants_topo (struct topo_info *topo)
1515 for (i = topo->nnodes - 1; i >= 0; i--)
1517 struct cgraph_node *v, *node = topo->order[i];
1518 struct ipa_dfs_info *node_dfs_info;
1520 if (!cgraph_function_with_gimple_body_p (node))
1523 node_dfs_info = (struct ipa_dfs_info *) node->aux;
1524 /* First, iteratively propagate within the strongly connected component
1525 until all lattices stabilize. */
1526 v = node_dfs_info->next_cycle;
1529 push_node_to_stack (topo, v);
1530 v = ((struct ipa_dfs_info *) v->aux)->next_cycle;
1536 struct cgraph_edge *cs;
1538 for (cs = v->callees; cs; cs = cs->next_callee)
1539 if (edge_within_scc (cs)
1540 && propagate_constants_accross_call (cs))
1541 push_node_to_stack (topo, cs->callee);
1542 v = pop_node_from_stack (topo);
1545 /* Afterwards, propagate along edges leading out of the SCC, calculates
1546 the local effects of the discovered constants and all valid values to
1547 their topological sort. */
1551 struct cgraph_edge *cs;
1553 estimate_local_effects (v);
1554 add_all_node_vals_to_toposort (v);
1555 for (cs = v->callees; cs; cs = cs->next_callee)
1556 if (!edge_within_scc (cs))
1557 propagate_constants_accross_call (cs);
1559 v = ((struct ipa_dfs_info *) v->aux)->next_cycle;
1564 /* Propagate the estimated effects of individual values along the topological
1565 from the dependant values to those they depend on. */
1568 propagate_effects (void)
1570 struct ipcp_value *base;
1572 for (base = values_topo; base; base = base->topo_next)
1574 struct ipcp_value_source *src;
1575 struct ipcp_value *val;
1576 int time = 0, size = 0;
1578 for (val = base; val; val = val->scc_next)
1580 time += val->local_time_benefit + val->prop_time_benefit;
1581 size += val->local_size_cost + val->prop_size_cost;
1584 for (val = base; val; val = val->scc_next)
1585 for (src = val->sources; src; src = src->next)
1587 && cgraph_maybe_hot_edge_p (src->cs))
1589 src->val->prop_time_benefit += time;
1590 src->val->prop_size_cost += size;
1596 /* Propagate constants, binfos and their effects from the summaries
1597 interprocedurally. */
1600 ipcp_propagate_stage (struct topo_info *topo)
1602 struct cgraph_node *node;
1605 fprintf (dump_file, "\n Propagating constants:\n\n");
1608 ipa_update_after_lto_read ();
1611 FOR_EACH_DEFINED_FUNCTION (node)
1613 struct ipa_node_params *info = IPA_NODE_REF (node);
1615 determine_versionability (node);
1616 if (cgraph_function_with_gimple_body_p (node))
1618 info->lattices = XCNEWVEC (struct ipcp_lattice,
1619 ipa_get_param_count (info));
1620 initialize_node_lattices (node);
1622 if (node->count > max_count)
1623 max_count = node->count;
1624 overall_size += inline_summary (node)->self_size;
1627 max_new_size = overall_size;
1628 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1629 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1630 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1633 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
1634 overall_size, max_new_size);
1636 propagate_constants_topo (topo);
1637 #ifdef ENABLE_CHECKING
1638 ipcp_verify_propagated_values ();
1640 propagate_effects ();
1644 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
1645 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
1649 /* Discover newly direct outgoing edges from NODE which is a new clone with
1650 known KNOWN_VALS and make them direct. */
1653 ipcp_discover_new_direct_edges (struct cgraph_node *node,
1654 VEC (tree, heap) *known_vals)
1656 struct cgraph_edge *ie, *next_ie;
1658 for (ie = node->indirect_calls; ie; ie = next_ie)
1662 next_ie = ie->next_callee;
1663 target = ipa_get_indirect_edge_target (ie, known_vals, NULL);
1665 ipa_make_edge_direct_to_target (ie, target);
1669 /* Vector of pointers which for linked lists of clones of an original crgaph
1672 static VEC (cgraph_edge_p, heap) *next_edge_clone;
1675 grow_next_edge_clone_vector (void)
1677 if (VEC_length (cgraph_edge_p, next_edge_clone)
1678 <= (unsigned) cgraph_edge_max_uid)
1679 VEC_safe_grow_cleared (cgraph_edge_p, heap, next_edge_clone,
1680 cgraph_edge_max_uid + 1);
1683 /* Edge duplication hook to grow the appropriate linked list in
1687 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1688 __attribute__((unused)) void *data)
1690 grow_next_edge_clone_vector ();
1691 VEC_replace (cgraph_edge_p, next_edge_clone, dst->uid,
1692 VEC_index (cgraph_edge_p, next_edge_clone, src->uid));
1693 VEC_replace (cgraph_edge_p, next_edge_clone, src->uid, dst);
1696 /* Get the next clone in the linked list of clones of an edge. */
1698 static inline struct cgraph_edge *
1699 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
1701 return VEC_index (cgraph_edge_p, next_edge_clone, cs->uid);
1704 /* Return true if edge CS does bring about the value described by SRC. */
1707 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
1708 struct ipcp_value_source *src)
1710 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1712 if (IPA_NODE_REF (cs->callee)->ipcp_orig_node
1713 || caller_info->node_dead)
1718 if (caller_info->ipcp_orig_node)
1720 tree t = VEC_index (tree, caller_info->known_vals, src->index);
1721 return (t != NULL_TREE
1722 && values_equal_for_ipcp_p (src->val->value, t));
1726 struct ipcp_lattice *lat = ipa_get_lattice (caller_info, src->index);
1727 if (ipa_lat_is_single_const (lat)
1728 && values_equal_for_ipcp_p (src->val->value, lat->values->value))
1735 /* Given VAL, iterate over all its sources and if they still hold, add their
1736 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
1740 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
1741 gcov_type *count_sum, int *caller_count)
1743 struct ipcp_value_source *src;
1744 int freq = 0, count = 0;
1748 for (src = val->sources; src; src = src->next)
1750 struct cgraph_edge *cs = src->cs;
1753 if (cgraph_edge_brings_value_p (cs, src))
1756 freq += cs->frequency;
1758 hot |= cgraph_maybe_hot_edge_p (cs);
1760 cs = get_next_cgraph_edge_clone (cs);
1766 *caller_count = count;
1770 /* Return a vector of incoming edges that do bring value VAL. It is assumed
1771 their number is known and equal to CALLER_COUNT. */
1773 static VEC (cgraph_edge_p,heap) *
1774 gather_edges_for_value (struct ipcp_value *val, int caller_count)
1776 struct ipcp_value_source *src;
1777 VEC (cgraph_edge_p,heap) *ret;
1779 ret = VEC_alloc (cgraph_edge_p, heap, caller_count);
1780 for (src = val->sources; src; src = src->next)
1782 struct cgraph_edge *cs = src->cs;
1785 if (cgraph_edge_brings_value_p (cs, src))
1786 VEC_quick_push (cgraph_edge_p, ret, cs);
1787 cs = get_next_cgraph_edge_clone (cs);
1794 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
1795 Return it or NULL if for some reason it cannot be created. */
1797 static struct ipa_replace_map *
1798 get_replacement_map (tree value, tree parm)
1800 tree req_type = TREE_TYPE (parm);
1801 struct ipa_replace_map *replace_map;
1803 if (!useless_type_conversion_p (req_type, TREE_TYPE (value)))
1805 if (fold_convertible_p (req_type, value))
1806 value = fold_build1 (NOP_EXPR, req_type, value);
1807 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (value)))
1808 value = fold_build1 (VIEW_CONVERT_EXPR, req_type, value);
1813 fprintf (dump_file, " const ");
1814 print_generic_expr (dump_file, value, 0);
1815 fprintf (dump_file, " can't be converted to param ");
1816 print_generic_expr (dump_file, parm, 0);
1817 fprintf (dump_file, "\n");
1823 replace_map = ggc_alloc_ipa_replace_map ();
1826 fprintf (dump_file, " replacing param ");
1827 print_generic_expr (dump_file, parm, 0);
1828 fprintf (dump_file, " with const ");
1829 print_generic_expr (dump_file, value, 0);
1830 fprintf (dump_file, "\n");
1832 replace_map->old_tree = parm;
1833 replace_map->new_tree = value;
1834 replace_map->replace_p = true;
1835 replace_map->ref_p = false;
1840 /* Dump new profiling counts */
1843 dump_profile_updates (struct cgraph_node *orig_node,
1844 struct cgraph_node *new_node)
1846 struct cgraph_edge *cs;
1848 fprintf (dump_file, " setting count of the specialized node to "
1849 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
1850 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1851 fprintf (dump_file, " edge to %s has count "
1852 HOST_WIDE_INT_PRINT_DEC "\n",
1853 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
1855 fprintf (dump_file, " setting count of the original node to "
1856 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
1857 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1858 fprintf (dump_file, " edge to %s is left with "
1859 HOST_WIDE_INT_PRINT_DEC "\n",
1860 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
1863 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
1864 their profile information to reflect this. */
1867 update_profiling_info (struct cgraph_node *orig_node,
1868 struct cgraph_node *new_node)
1870 struct cgraph_edge *cs;
1871 struct caller_statistics stats;
1872 gcov_type new_sum, orig_sum;
1873 gcov_type remainder, orig_node_count = orig_node->count;
1875 if (orig_node_count == 0)
1878 init_caller_stats (&stats);
1879 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
1880 orig_sum = stats.count_sum;
1881 init_caller_stats (&stats);
1882 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
1883 new_sum = stats.count_sum;
1885 if (orig_node_count < orig_sum + new_sum)
1888 fprintf (dump_file, " Problem: node %s/%i has too low count "
1889 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
1890 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
1891 cgraph_node_name (orig_node), orig_node->uid,
1892 (HOST_WIDE_INT) orig_node_count,
1893 (HOST_WIDE_INT) (orig_sum + new_sum));
1895 orig_node_count = (orig_sum + new_sum) * 12 / 10;
1897 fprintf (dump_file, " proceeding by pretending it was "
1898 HOST_WIDE_INT_PRINT_DEC "\n",
1899 (HOST_WIDE_INT) orig_node_count);
1902 new_node->count = new_sum;
1903 remainder = orig_node_count - new_sum;
1904 orig_node->count = remainder;
1906 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1908 cs->count = cs->count * (new_sum * REG_BR_PROB_BASE
1909 / orig_node_count) / REG_BR_PROB_BASE;
1913 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1914 cs->count = cs->count * (remainder * REG_BR_PROB_BASE
1915 / orig_node_count) / REG_BR_PROB_BASE;
1918 dump_profile_updates (orig_node, new_node);
1921 /* Update the respective profile of specialized NEW_NODE and the original
1922 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
1923 have been redirected to the specialized version. */
1926 update_specialized_profile (struct cgraph_node *new_node,
1927 struct cgraph_node *orig_node,
1928 gcov_type redirected_sum)
1930 struct cgraph_edge *cs;
1931 gcov_type new_node_count, orig_node_count = orig_node->count;
1934 fprintf (dump_file, " the sum of counts of redirected edges is "
1935 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
1936 if (orig_node_count == 0)
1939 gcc_assert (orig_node_count >= redirected_sum);
1941 new_node_count = new_node->count;
1942 new_node->count += redirected_sum;
1943 orig_node->count -= redirected_sum;
1945 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1947 cs->count += cs->count * redirected_sum / new_node_count;
1951 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1953 gcov_type dec = cs->count * (redirected_sum * REG_BR_PROB_BASE
1954 / orig_node_count) / REG_BR_PROB_BASE;
1955 if (dec < cs->count)
1962 dump_profile_updates (orig_node, new_node);
1965 /* Create a specialized version of NODE with known constants and types of
1966 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
1968 static struct cgraph_node *
1969 create_specialized_node (struct cgraph_node *node,
1970 VEC (tree, heap) *known_vals,
1971 VEC (cgraph_edge_p,heap) *callers)
1973 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
1974 VEC (ipa_replace_map_p,gc)* replace_trees = NULL;
1975 struct cgraph_node *new_node;
1976 int i, count = ipa_get_param_count (info);
1977 bitmap args_to_skip;
1979 gcc_assert (!info->ipcp_orig_node);
1981 if (node->local.can_change_signature)
1983 args_to_skip = BITMAP_GGC_ALLOC ();
1984 for (i = 0; i < count; i++)
1986 tree t = VEC_index (tree, known_vals, i);
1988 if ((t && TREE_CODE (t) != TREE_BINFO)
1989 || !ipa_is_param_used (info, i))
1990 bitmap_set_bit (args_to_skip, i);
1995 args_to_skip = NULL;
1996 if (dump_file && (dump_flags & TDF_DETAILS))
1997 fprintf (dump_file, " cannot change function signature\n");
2000 for (i = 0; i < count ; i++)
2002 tree t = VEC_index (tree, known_vals, i);
2003 if (t && TREE_CODE (t) != TREE_BINFO)
2005 struct ipa_replace_map *replace_map;
2007 replace_map = get_replacement_map (t, ipa_get_param (info, i));
2009 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_map);
2013 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2014 args_to_skip, "constprop");
2015 if (dump_file && (dump_flags & TDF_DETAILS))
2016 fprintf (dump_file, " the new node is %s/%i.\n",
2017 cgraph_node_name (new_node), new_node->uid);
2018 gcc_checking_assert (ipa_node_params_vector
2019 && (VEC_length (ipa_node_params_t,
2020 ipa_node_params_vector)
2021 > (unsigned) cgraph_max_uid));
2022 update_profiling_info (node, new_node);
2023 new_info = IPA_NODE_REF (new_node);
2024 new_info->ipcp_orig_node = node;
2025 new_info->known_vals = known_vals;
2027 ipcp_discover_new_direct_edges (new_node, known_vals);
2029 VEC_free (cgraph_edge_p, heap, callers);
2033 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2034 KNOWN_VALS with constants and types that are also known for all of the
2038 find_more_values_for_callers_subset (struct cgraph_node *node,
2039 VEC (tree, heap) *known_vals,
2040 VEC (cgraph_edge_p,heap) *callers)
2042 struct ipa_node_params *info = IPA_NODE_REF (node);
2043 int i, count = ipa_get_param_count (info);
2045 for (i = 0; i < count ; i++)
2047 struct cgraph_edge *cs;
2048 tree newval = NULL_TREE;
2051 if (ipa_get_lattice (info, i)->bottom
2052 || VEC_index (tree, known_vals, i))
2055 FOR_EACH_VEC_ELT (cgraph_edge_p, callers, j, cs)
2057 struct ipa_jump_func *jump_func;
2060 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2065 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2066 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2069 && !values_equal_for_ipcp_p (t, newval)))
2080 if (dump_file && (dump_flags & TDF_DETAILS))
2082 fprintf (dump_file, " adding an extra known value ");
2083 print_ipcp_constant_value (dump_file, newval);
2084 fprintf (dump_file, " for parameter ");
2085 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2086 fprintf (dump_file, "\n");
2089 VEC_replace (tree, known_vals, i, newval);
2094 /* Given an original NODE and a VAL for which we have already created a
2095 specialized clone, look whether there are incoming edges that still lead
2096 into the old node but now also bring the requested value and also conform to
2097 all other criteria such that they can be redirected the the special node.
2098 This function can therefore redirect the final edge in a SCC. */
2101 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
2103 struct ipa_node_params *dest_info = IPA_NODE_REF (val->spec_node);
2104 struct ipcp_value_source *src;
2105 int count = ipa_get_param_count (dest_info);
2106 gcov_type redirected_sum = 0;
2108 for (src = val->sources; src; src = src->next)
2110 struct cgraph_edge *cs = src->cs;
2113 enum availability availability;
2114 bool insufficient = false;
2116 if (cgraph_function_node (cs->callee, &availability) == node
2117 && availability > AVAIL_OVERWRITABLE
2118 && cgraph_edge_brings_value_p (cs, src))
2120 struct ipa_node_params *caller_info;
2121 struct ipa_edge_args *args;
2124 caller_info = IPA_NODE_REF (cs->caller);
2125 args = IPA_EDGE_REF (cs);
2126 for (i = 0; i < count; i++)
2128 struct ipa_jump_func *jump_func;
2131 val = VEC_index (tree, dest_info->known_vals, i);
2135 if (i >= ipa_get_cs_argument_count (args))
2137 insufficient = true;
2140 jump_func = ipa_get_ith_jump_func (args, i);
2141 t = ipa_value_from_jfunc (caller_info, jump_func);
2142 if (!t || !values_equal_for_ipcp_p (val, t))
2144 insufficient = true;
2152 fprintf (dump_file, " - adding an extra caller %s/%i"
2154 cgraph_node_name (cs->caller), cs->caller->uid,
2155 cgraph_node_name (val->spec_node),
2156 val->spec_node->uid);
2158 cgraph_redirect_edge_callee (cs, val->spec_node);
2159 redirected_sum += cs->count;
2162 cs = get_next_cgraph_edge_clone (cs);
2167 update_specialized_profile (val->spec_node, node, redirected_sum);
2171 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
2174 move_binfos_to_values (VEC (tree, heap) *known_vals,
2175 VEC (tree, heap) *known_binfos)
2180 for (i = 0; VEC_iterate (tree, known_binfos, i, t); i++)
2182 VEC_replace (tree, known_vals, i, t);
2186 /* Decide whether and what specialized clones of NODE should be created. */
2189 decide_whether_version_node (struct cgraph_node *node)
2191 struct ipa_node_params *info = IPA_NODE_REF (node);
2192 int i, count = ipa_get_param_count (info);
2193 VEC (tree, heap) *known_csts, *known_binfos;
2199 if (dump_file && (dump_flags & TDF_DETAILS))
2200 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
2201 cgraph_node_name (node), node->uid);
2203 gather_context_independent_values (info, &known_csts, &known_binfos,
2206 for (i = 0; i < count ; i++)
2208 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
2209 struct ipcp_value *val;
2212 || VEC_index (tree, known_csts, i)
2213 || VEC_index (tree, known_binfos, i))
2216 for (val = lat->values; val; val = val->next)
2218 int freq_sum, caller_count;
2219 gcov_type count_sum;
2220 VEC (cgraph_edge_p, heap) *callers;
2221 VEC (tree, heap) *kv;
2225 perhaps_add_new_callers (node, val);
2228 else if (val->local_size_cost + overall_size > max_new_size)
2230 if (dump_file && (dump_flags & TDF_DETAILS))
2231 fprintf (dump_file, " Ignoring candidate value because "
2232 "max_new_size would be reached with %li.\n",
2233 val->local_size_cost + overall_size);
2236 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
2240 if (dump_file && (dump_flags & TDF_DETAILS))
2242 fprintf (dump_file, " - considering value ");
2243 print_ipcp_constant_value (dump_file, val->value);
2244 fprintf (dump_file, " for parameter ");
2245 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2246 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
2250 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
2251 freq_sum, count_sum,
2252 val->local_size_cost)
2253 && !good_cloning_opportunity_p (node,
2254 val->local_time_benefit
2255 + val->prop_time_benefit,
2256 freq_sum, count_sum,
2257 val->local_size_cost
2258 + val->prop_size_cost))
2262 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
2263 cgraph_node_name (node), node->uid);
2265 callers = gather_edges_for_value (val, caller_count);
2266 kv = VEC_copy (tree, heap, known_csts);
2267 move_binfos_to_values (kv, known_binfos);
2268 VEC_replace (tree, kv, i, val->value);
2269 find_more_values_for_callers_subset (node, kv, callers);
2270 val->spec_node = create_specialized_node (node, kv, callers);
2271 overall_size += val->local_size_cost;
2272 info = IPA_NODE_REF (node);
2274 /* TODO: If for some lattice there is only one other known value
2275 left, make a special node for it too. */
2278 VEC_replace (tree, kv, i, val->value);
2282 if (info->clone_for_all_contexts)
2284 VEC (cgraph_edge_p, heap) *callers;
2287 fprintf (dump_file, " - Creating a specialized node of %s/%i "
2288 "for all known contexts.\n", cgraph_node_name (node),
2291 callers = collect_callers_of_node (node);
2292 move_binfos_to_values (known_csts, known_binfos);
2293 create_specialized_node (node, known_csts, callers);
2294 info = IPA_NODE_REF (node);
2295 info->clone_for_all_contexts = false;
2299 VEC_free (tree, heap, known_csts);
2301 VEC_free (tree, heap, known_binfos);
2305 /* Transitively mark all callees of NODE within the same SCC as not dead. */
2308 spread_undeadness (struct cgraph_node *node)
2310 struct cgraph_edge *cs;
2312 for (cs = node->callees; cs; cs = cs->next_callee)
2313 if (edge_within_scc (cs))
2315 struct cgraph_node *callee;
2316 struct ipa_node_params *info;
2318 callee = cgraph_function_node (cs->callee, NULL);
2319 info = IPA_NODE_REF (callee);
2321 if (info->node_dead)
2323 info->node_dead = 0;
2324 spread_undeadness (callee);
2329 /* Return true if NODE has a caller from outside of its SCC that is not
2330 dead. Worker callback for cgraph_for_node_and_aliases. */
2333 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
2334 void *data ATTRIBUTE_UNUSED)
2336 struct cgraph_edge *cs;
2338 for (cs = node->callers; cs; cs = cs->next_caller)
2339 if (cs->caller->thunk.thunk_p
2340 && cgraph_for_node_and_aliases (cs->caller,
2341 has_undead_caller_from_outside_scc_p,
2344 else if (!edge_within_scc (cs)
2345 && !IPA_NODE_REF (cs->caller)->node_dead)
2351 /* Identify nodes within the same SCC as NODE which are no longer needed
2352 because of new clones and will be removed as unreachable. */
2355 identify_dead_nodes (struct cgraph_node *node)
2357 struct cgraph_node *v;
2358 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2359 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
2360 && !cgraph_for_node_and_aliases (v,
2361 has_undead_caller_from_outside_scc_p,
2363 IPA_NODE_REF (v)->node_dead = 1;
2365 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2366 if (!IPA_NODE_REF (v)->node_dead)
2367 spread_undeadness (v);
2369 if (dump_file && (dump_flags & TDF_DETAILS))
2371 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2372 if (IPA_NODE_REF (v)->node_dead)
2373 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
2374 cgraph_node_name (v), v->uid);
2378 /* The decision stage. Iterate over the topological order of call graph nodes
2379 TOPO and make specialized clones if deemed beneficial. */
2382 ipcp_decision_stage (struct topo_info *topo)
2387 fprintf (dump_file, "\nIPA decision stage:\n\n");
2389 for (i = topo->nnodes - 1; i >= 0; i--)
2391 struct cgraph_node *node = topo->order[i];
2392 bool change = false, iterate = true;
2396 struct cgraph_node *v;
2398 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2399 if (cgraph_function_with_gimple_body_p (v)
2400 && ipcp_versionable_function_p (v))
2401 iterate |= decide_whether_version_node (v);
2406 identify_dead_nodes (node);
2410 /* The IPCP driver. */
2415 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
2416 struct topo_info topo;
2418 cgraph_remove_unreachable_nodes (true,dump_file);
2419 ipa_check_create_node_params ();
2420 ipa_check_create_edge_args ();
2421 grow_next_edge_clone_vector ();
2422 edge_duplication_hook_holder =
2423 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
2424 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
2425 sizeof (struct ipcp_value), 32);
2426 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
2427 sizeof (struct ipcp_value_source), 64);
2430 fprintf (dump_file, "\nIPA structures before propagation:\n");
2431 if (dump_flags & TDF_DETAILS)
2432 ipa_print_all_params (dump_file);
2433 ipa_print_all_jump_functions (dump_file);
2436 /* Topological sort. */
2437 build_toporder_info (&topo);
2438 /* Do the interprocedural propagation. */
2439 ipcp_propagate_stage (&topo);
2440 /* Decide what constant propagation and cloning should be performed. */
2441 ipcp_decision_stage (&topo);
2443 /* Free all IPCP structures. */
2444 free_toporder_info (&topo);
2445 VEC_free (cgraph_edge_p, heap, next_edge_clone);
2446 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2447 ipa_free_all_structures_after_ipa_cp ();
2449 fprintf (dump_file, "\nIPA constant propagation end\n");
2453 /* Initialization and computation of IPCP data structures. This is the initial
2454 intraprocedural analysis of functions, which gathers information to be
2455 propagated later on. */
2458 ipcp_generate_summary (void)
2460 struct cgraph_node *node;
2463 fprintf (dump_file, "\nIPA constant propagation start:\n");
2464 ipa_register_cgraph_hooks ();
2466 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2468 /* Unreachable nodes should have been eliminated before ipcp. */
2469 gcc_assert (node->needed || node->reachable);
2470 node->local.versionable = tree_versionable_function_p (node->decl);
2471 ipa_analyze_node (node);
2475 /* Write ipcp summary for nodes in SET. */
2478 ipcp_write_summary (cgraph_node_set set,
2479 varpool_node_set vset ATTRIBUTE_UNUSED)
2481 ipa_prop_write_jump_functions (set);
2484 /* Read ipcp summary. */
2487 ipcp_read_summary (void)
2489 ipa_prop_read_jump_functions ();
2492 /* Gate for IPCP optimization. */
2495 cgraph_gate_cp (void)
2497 /* FIXME: We should remove the optimize check after we ensure we never run
2498 IPA passes when not optimizing. */
2499 return flag_ipa_cp && optimize;
2502 struct ipa_opt_pass_d pass_ipa_cp =
2507 cgraph_gate_cp, /* gate */
2508 ipcp_driver, /* execute */
2511 0, /* static_pass_number */
2512 TV_IPA_CONSTANT_PROP, /* tv_id */
2513 0, /* properties_required */
2514 0, /* properties_provided */
2515 0, /* properties_destroyed */
2516 0, /* todo_flags_start */
2518 TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
2520 ipcp_generate_summary, /* generate_summary */
2521 ipcp_write_summary, /* write_summary */
2522 ipcp_read_summary, /* read_summary */
2523 NULL, /* write_optimization_summary */
2524 NULL, /* read_optimization_summary */
2525 NULL, /* stmt_fixup */
2527 NULL, /* function_transform */
2528 NULL, /* variable_transform */