2013-11-27 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-cp.c
blobc1634a3c69cdcd47612b3e0b3db0af47b831ba92
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
5 <mjambor@suse.cz>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Interprocedural constant propagation (IPA-CP).
25 The goal of this transformation is to
27 1) discover functions which are always invoked with some arguments with the
28 same known constant values and modify the functions so that the
29 subsequent optimizations can take advantage of the knowledge, and
31 2) partial specialization - create specialized versions of functions
32 transformed in this way if some parameters are known constants only in
33 certain contexts but the estimated tradeoff between speedup and cost size
34 is deemed good.
36 The algorithm also propagates types and attempts to perform type based
37 devirtualization. Types are propagated much like constants.
39 The algorithm basically consists of three stages. In the first, functions
40 are analyzed one at a time and jump functions are constructed for all known
41 call-sites. In the second phase, the pass propagates information from the
42 jump functions across the call to reveal what values are available at what
43 call sites, performs estimations of effects of known values on functions and
44 their callees, and finally decides what specialized extra versions should be
45 created. In the third, the special versions materialize and appropriate
46 calls are redirected.
48 The algorithm used is to a certain extent based on "Interprocedural Constant
49 Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon,
50 Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D
51 Cooper, Mary W. Hall, and Ken Kennedy.
54 First stage - intraprocedural analysis
55 =======================================
57 This phase computes jump_function and modification flags.
59 A jump function for a call-site represents the values passed as an actual
60 arguments of a given call-site. In principle, there are three types of
61 values:
63 Pass through - the caller's formal parameter is passed as an actual
64 argument, plus an operation on it can be performed.
65 Constant - a constant is passed as an actual argument.
66 Unknown - neither of the above.
68 All jump function types are described in detail in ipa-prop.h, together with
69 the data structures that represent them and methods of accessing them.
71 ipcp_generate_summary() is the main function of the first stage.
73 Second stage - interprocedural analysis
74 ========================================
76 This stage is itself divided into two phases. In the first, we propagate
77 known values over the call graph, in the second, we make cloning decisions.
78 It uses a different algorithm than the original Callahan's paper.
80 First, we traverse the functions topologically from callers to callees and,
81 for each strongly connected component (SCC), we propagate constants
82 according to previously computed jump functions. We also record what known
83 values depend on other known values and estimate local effects. Finally, we
84 propagate cumulative information about these effects from dependent values
85 to those on which they depend.
87 Second, we again traverse the call graph in the same topological order and
88 make clones for functions which we know are called with the same values in
89 all contexts and decide about extra specialized clones of functions just for
90 some contexts - these decisions are based on both local estimates and
91 cumulative estimates propagated from callees.
93 ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
94 third stage.
96 Third phase - materialization of clones, call statement updates.
97 ============================================
99 This stage is currently performed by call graph code (mainly in cgraphunit.c
100 and tree-inline.c) according to instructions inserted to the call graph by
101 the second stage. */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "tree.h"
107 #include "gimple-fold.h"
108 #include "gimple-expr.h"
109 #include "target.h"
110 #include "ipa-prop.h"
111 #include "bitmap.h"
112 #include "tree-pass.h"
113 #include "flags.h"
114 #include "diagnostic.h"
115 #include "tree-pretty-print.h"
116 #include "tree-inline.h"
117 #include "params.h"
118 #include "ipa-inline.h"
119 #include "ipa-utils.h"
121 struct ipcp_value;
123 /* Describes a particular source for an IPA-CP value. */
125 struct ipcp_value_source
127 /* Aggregate offset of the source, negative if the source is scalar value of
128 the argument itself. */
129 HOST_WIDE_INT offset;
130 /* The incoming edge that brought the value. */
131 struct cgraph_edge *cs;
132 /* If the jump function that resulted into his value was a pass-through or an
133 ancestor, this is the ipcp_value of the caller from which the described
134 value has been derived. Otherwise it is NULL. */
135 struct ipcp_value *val;
136 /* Next pointer in a linked list of sources of a value. */
137 struct ipcp_value_source *next;
138 /* If the jump function that resulted into his value was a pass-through or an
139 ancestor, this is the index of the parameter of the caller the jump
140 function references. */
141 int index;
144 /* Describes one particular value stored in struct ipcp_lattice. */
146 struct ipcp_value
148 /* The actual value for the given parameter. This is either an IPA invariant
149 or a TREE_BINFO describing a type that can be used for
150 devirtualization. */
151 tree value;
152 /* The list of sources from which this value originates. */
153 struct ipcp_value_source *sources;
154 /* Next pointers in a linked list of all values in a lattice. */
155 struct ipcp_value *next;
156 /* Next pointers in a linked list of values in a strongly connected component
157 of values. */
158 struct ipcp_value *scc_next;
159 /* Next pointers in a linked list of SCCs of values sorted topologically
160 according their sources. */
161 struct ipcp_value *topo_next;
162 /* A specialized node created for this value, NULL if none has been (so far)
163 created. */
164 struct cgraph_node *spec_node;
165 /* Depth first search number and low link for topological sorting of
166 values. */
167 int dfs, low_link;
168 /* Time benefit and size cost that specializing the function for this value
169 would bring about in this function alone. */
170 int local_time_benefit, local_size_cost;
171 /* Time benefit and size cost that specializing the function for this value
172 can bring about in it's callees (transitively). */
173 int prop_time_benefit, prop_size_cost;
174 /* True if this valye is currently on the topo-sort stack. */
175 bool on_stack;
178 /* Lattice describing potential values of a formal parameter of a function, or
179 a part of an aggreagate. TOP is represented by a lattice with zero values
180 and with contains_variable and bottom flags cleared. BOTTOM is represented
181 by a lattice with the bottom flag set. In that case, values and
182 contains_variable flag should be disregarded. */
184 struct ipcp_lattice
186 /* The list of known values and types in this lattice. Note that values are
187 not deallocated if a lattice is set to bottom because there may be value
188 sources referencing them. */
189 struct ipcp_value *values;
190 /* Number of known values and types in this lattice. */
191 int values_count;
192 /* The lattice contains a variable component (in addition to values). */
193 bool contains_variable;
194 /* The value of the lattice is bottom (i.e. variable and unusable for any
195 propagation). */
196 bool bottom;
199 /* Lattice with an offset to describe a part of an aggregate. */
201 struct ipcp_agg_lattice : public ipcp_lattice
203 /* Offset that is being described by this lattice. */
204 HOST_WIDE_INT offset;
205 /* Size so that we don't have to re-compute it every time we traverse the
206 list. Must correspond to TYPE_SIZE of all lat values. */
207 HOST_WIDE_INT size;
208 /* Next element of the linked list. */
209 struct ipcp_agg_lattice *next;
212 /* Structure containing lattices for a parameter itself and for pieces of
213 aggregates that are passed in the parameter or by a reference in a parameter
214 plus some other useful flags. */
216 struct ipcp_param_lattices
218 /* Lattice describing the value of the parameter itself. */
219 struct ipcp_lattice itself;
220 /* Lattices describing aggregate parts. */
221 struct ipcp_agg_lattice *aggs;
222 /* Number of aggregate lattices */
223 int aggs_count;
224 /* True if aggregate data were passed by reference (as opposed to by
225 value). */
226 bool aggs_by_ref;
227 /* All aggregate lattices contain a variable component (in addition to
228 values). */
229 bool aggs_contain_variable;
230 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
231 for any propagation). */
232 bool aggs_bottom;
234 /* There is a virtual call based on this parameter. */
235 bool virt_call;
238 /* Allocation pools for values and their sources in ipa-cp. */
240 alloc_pool ipcp_values_pool;
241 alloc_pool ipcp_sources_pool;
242 alloc_pool ipcp_agg_lattice_pool;
244 /* Maximal count found in program. */
246 static gcov_type max_count;
248 /* Original overall size of the program. */
250 static long overall_size, max_new_size;
252 /* Head of the linked list of topologically sorted values. */
254 static struct ipcp_value *values_topo;
256 /* Return the param lattices structure corresponding to the Ith formal
257 parameter of the function described by INFO. */
258 static inline struct ipcp_param_lattices *
259 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
261 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
262 gcc_checking_assert (!info->ipcp_orig_node);
263 gcc_checking_assert (info->lattices);
264 return &(info->lattices[i]);
267 /* Return the lattice corresponding to the scalar value of the Ith formal
268 parameter of the function described by INFO. */
269 static inline struct ipcp_lattice *
270 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
272 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
273 return &plats->itself;
276 /* Return whether LAT is a lattice with a single constant and without an
277 undefined value. */
279 static inline bool
280 ipa_lat_is_single_const (struct ipcp_lattice *lat)
282 if (lat->bottom
283 || lat->contains_variable
284 || lat->values_count != 1)
285 return false;
286 else
287 return true;
290 /* Print V which is extracted from a value in a lattice to F. */
292 static void
293 print_ipcp_constant_value (FILE * f, tree v)
295 if (TREE_CODE (v) == TREE_BINFO)
297 fprintf (f, "BINFO ");
298 print_generic_expr (f, BINFO_TYPE (v), 0);
300 else if (TREE_CODE (v) == ADDR_EXPR
301 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
303 fprintf (f, "& ");
304 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
306 else
307 print_generic_expr (f, v, 0);
310 /* Print a lattice LAT to F. */
312 static void
313 print_lattice (FILE * f, struct ipcp_lattice *lat,
314 bool dump_sources, bool dump_benefits)
316 struct ipcp_value *val;
317 bool prev = false;
319 if (lat->bottom)
321 fprintf (f, "BOTTOM\n");
322 return;
325 if (!lat->values_count && !lat->contains_variable)
327 fprintf (f, "TOP\n");
328 return;
331 if (lat->contains_variable)
333 fprintf (f, "VARIABLE");
334 prev = true;
335 if (dump_benefits)
336 fprintf (f, "\n");
339 for (val = lat->values; val; val = val->next)
341 if (dump_benefits && prev)
342 fprintf (f, " ");
343 else if (!dump_benefits && prev)
344 fprintf (f, ", ");
345 else
346 prev = true;
348 print_ipcp_constant_value (f, val->value);
350 if (dump_sources)
352 struct ipcp_value_source *s;
354 fprintf (f, " [from:");
355 for (s = val->sources; s; s = s->next)
356 fprintf (f, " %i(%i)", s->cs->caller->order,
357 s->cs->frequency);
358 fprintf (f, "]");
361 if (dump_benefits)
362 fprintf (f, " [loc_time: %i, loc_size: %i, "
363 "prop_time: %i, prop_size: %i]\n",
364 val->local_time_benefit, val->local_size_cost,
365 val->prop_time_benefit, val->prop_size_cost);
367 if (!dump_benefits)
368 fprintf (f, "\n");
371 /* Print all ipcp_lattices of all functions to F. */
373 static void
374 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
376 struct cgraph_node *node;
377 int i, count;
379 fprintf (f, "\nLattices:\n");
380 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
382 struct ipa_node_params *info;
384 info = IPA_NODE_REF (node);
385 fprintf (f, " Node: %s/%i:\n", node->name (),
386 node->order);
387 count = ipa_get_param_count (info);
388 for (i = 0; i < count; i++)
390 struct ipcp_agg_lattice *aglat;
391 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
392 fprintf (f, " param [%d]: ", i);
393 print_lattice (f, &plats->itself, dump_sources, dump_benefits);
395 if (plats->virt_call)
396 fprintf (f, " virt_call flag set\n");
398 if (plats->aggs_bottom)
400 fprintf (f, " AGGS BOTTOM\n");
401 continue;
403 if (plats->aggs_contain_variable)
404 fprintf (f, " AGGS VARIABLE\n");
405 for (aglat = plats->aggs; aglat; aglat = aglat->next)
407 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
408 plats->aggs_by_ref ? "ref " : "", aglat->offset);
409 print_lattice (f, aglat, dump_sources, dump_benefits);
415 /* Determine whether it is at all technically possible to create clones of NODE
416 and store this information in the ipa_node_params structure associated
417 with NODE. */
419 static void
420 determine_versionability (struct cgraph_node *node)
422 const char *reason = NULL;
424 /* There are a number of generic reasons functions cannot be versioned. We
425 also cannot remove parameters if there are type attributes such as fnspec
426 present. */
427 if (node->alias || node->thunk.thunk_p)
428 reason = "alias or thunk";
429 else if (!node->local.versionable)
430 reason = "not a tree_versionable_function";
431 else if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
432 reason = "insufficient body availability";
434 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
435 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
436 node->name (), node->order, reason);
438 node->local.versionable = (reason == NULL);
441 /* Return true if it is at all technically possible to create clones of a
442 NODE. */
444 static bool
445 ipcp_versionable_function_p (struct cgraph_node *node)
447 return node->local.versionable;
450 /* Structure holding accumulated information about callers of a node. */
452 struct caller_statistics
454 gcov_type count_sum;
455 int n_calls, n_hot_calls, freq_sum;
458 /* Initialize fields of STAT to zeroes. */
460 static inline void
461 init_caller_stats (struct caller_statistics *stats)
463 stats->count_sum = 0;
464 stats->n_calls = 0;
465 stats->n_hot_calls = 0;
466 stats->freq_sum = 0;
469 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
470 non-thunk incoming edges to NODE. */
472 static bool
473 gather_caller_stats (struct cgraph_node *node, void *data)
475 struct caller_statistics *stats = (struct caller_statistics *) data;
476 struct cgraph_edge *cs;
478 for (cs = node->callers; cs; cs = cs->next_caller)
479 if (cs->caller->thunk.thunk_p)
480 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
481 stats, false);
482 else
484 stats->count_sum += cs->count;
485 stats->freq_sum += cs->frequency;
486 stats->n_calls++;
487 if (cgraph_maybe_hot_edge_p (cs))
488 stats->n_hot_calls ++;
490 return false;
494 /* Return true if this NODE is viable candidate for cloning. */
496 static bool
497 ipcp_cloning_candidate_p (struct cgraph_node *node)
499 struct caller_statistics stats;
501 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
503 if (!flag_ipa_cp_clone)
505 if (dump_file)
506 fprintf (dump_file, "Not considering %s for cloning; "
507 "-fipa-cp-clone disabled.\n",
508 node->name ());
509 return false;
512 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
514 if (dump_file)
515 fprintf (dump_file, "Not considering %s for cloning; "
516 "optimizing it for size.\n",
517 node->name ());
518 return false;
521 init_caller_stats (&stats);
522 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
524 if (inline_summary (node)->self_size < stats.n_calls)
526 if (dump_file)
527 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
528 node->name ());
529 return true;
532 /* When profile is available and function is hot, propagate into it even if
533 calls seems cold; constant propagation can improve function's speed
534 significantly. */
535 if (max_count)
537 if (stats.count_sum > node->count * 90 / 100)
539 if (dump_file)
540 fprintf (dump_file, "Considering %s for cloning; "
541 "usually called directly.\n",
542 node->name ());
543 return true;
546 if (!stats.n_hot_calls)
548 if (dump_file)
549 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
550 node->name ());
551 return false;
553 if (dump_file)
554 fprintf (dump_file, "Considering %s for cloning.\n",
555 node->name ());
556 return true;
559 /* Arrays representing a topological ordering of call graph nodes and a stack
560 of noes used during constant propagation. */
562 struct topo_info
564 struct cgraph_node **order;
565 struct cgraph_node **stack;
566 int nnodes, stack_top;
569 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
571 static void
572 build_toporder_info (struct topo_info *topo)
574 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
575 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
576 topo->stack_top = 0;
577 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
580 /* Free information about strongly connected components and the arrays in
581 TOPO. */
583 static void
584 free_toporder_info (struct topo_info *topo)
586 ipa_free_postorder_info ();
587 free (topo->order);
588 free (topo->stack);
591 /* Add NODE to the stack in TOPO, unless it is already there. */
593 static inline void
594 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
596 struct ipa_node_params *info = IPA_NODE_REF (node);
597 if (info->node_enqueued)
598 return;
599 info->node_enqueued = 1;
600 topo->stack[topo->stack_top++] = node;
603 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
604 is empty. */
606 static struct cgraph_node *
607 pop_node_from_stack (struct topo_info *topo)
609 if (topo->stack_top)
611 struct cgraph_node *node;
612 topo->stack_top--;
613 node = topo->stack[topo->stack_top];
614 IPA_NODE_REF (node)->node_enqueued = 0;
615 return node;
617 else
618 return NULL;
621 /* Set lattice LAT to bottom and return true if it previously was not set as
622 such. */
624 static inline bool
625 set_lattice_to_bottom (struct ipcp_lattice *lat)
627 bool ret = !lat->bottom;
628 lat->bottom = true;
629 return ret;
632 /* Mark lattice as containing an unknown value and return true if it previously
633 was not marked as such. */
635 static inline bool
636 set_lattice_contains_variable (struct ipcp_lattice *lat)
638 bool ret = !lat->contains_variable;
639 lat->contains_variable = true;
640 return ret;
643 /* Set all aggegate lattices in PLATS to bottom and return true if they were
644 not previously set as such. */
646 static inline bool
647 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
649 bool ret = !plats->aggs_bottom;
650 plats->aggs_bottom = true;
651 return ret;
654 /* Mark all aggegate lattices in PLATS as containing an unknown value and
655 return true if they were not previously marked as such. */
657 static inline bool
658 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
660 bool ret = !plats->aggs_contain_variable;
661 plats->aggs_contain_variable = true;
662 return ret;
665 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
666 return true is any of them has not been marked as such so far. */
668 static inline bool
669 set_all_contains_variable (struct ipcp_param_lattices *plats)
671 bool ret = !plats->itself.contains_variable || !plats->aggs_contain_variable;
672 plats->itself.contains_variable = true;
673 plats->aggs_contain_variable = true;
674 return ret;
677 /* Initialize ipcp_lattices. */
679 static void
680 initialize_node_lattices (struct cgraph_node *node)
682 struct ipa_node_params *info = IPA_NODE_REF (node);
683 struct cgraph_edge *ie;
684 bool disable = false, variable = false;
685 int i;
687 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
688 if (!node->local.local)
690 /* When cloning is allowed, we can assume that externally visible
691 functions are not called. We will compensate this by cloning
692 later. */
693 if (ipcp_versionable_function_p (node)
694 && ipcp_cloning_candidate_p (node))
695 variable = true;
696 else
697 disable = true;
700 if (disable || variable)
702 for (i = 0; i < ipa_get_param_count (info) ; i++)
704 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
705 if (disable)
707 set_lattice_to_bottom (&plats->itself);
708 set_agg_lats_to_bottom (plats);
710 else
711 set_all_contains_variable (plats);
713 if (dump_file && (dump_flags & TDF_DETAILS)
714 && !node->alias && !node->thunk.thunk_p)
715 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
716 node->name (), node->order,
717 disable ? "BOTTOM" : "VARIABLE");
720 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
721 if (ie->indirect_info->polymorphic
722 && ie->indirect_info->param_index >= 0)
724 gcc_checking_assert (ie->indirect_info->param_index >= 0);
725 ipa_get_parm_lattices (info,
726 ie->indirect_info->param_index)->virt_call = 1;
730 /* Return the result of a (possibly arithmetic) pass through jump function
731 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
732 determined or be considered an interprocedural invariant. */
734 static tree
735 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
737 tree restype, res;
739 if (TREE_CODE (input) == TREE_BINFO)
741 if (ipa_get_jf_pass_through_type_preserved (jfunc))
743 gcc_checking_assert (ipa_get_jf_pass_through_operation (jfunc)
744 == NOP_EXPR);
745 return input;
747 return NULL_TREE;
750 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
751 return input;
753 gcc_checking_assert (is_gimple_ip_invariant (input));
754 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
755 == tcc_comparison)
756 restype = boolean_type_node;
757 else
758 restype = TREE_TYPE (input);
759 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
760 input, ipa_get_jf_pass_through_operand (jfunc));
762 if (res && !is_gimple_ip_invariant (res))
763 return NULL_TREE;
765 return res;
768 /* Return the result of an ancestor jump function JFUNC on the constant value
769 INPUT. Return NULL_TREE if that cannot be determined. */
771 static tree
772 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
774 if (TREE_CODE (input) == TREE_BINFO)
776 if (!ipa_get_jf_ancestor_type_preserved (jfunc))
777 return NULL;
778 return get_binfo_at_offset (input,
779 ipa_get_jf_ancestor_offset (jfunc),
780 ipa_get_jf_ancestor_type (jfunc));
782 else if (TREE_CODE (input) == ADDR_EXPR)
784 tree t = TREE_OPERAND (input, 0);
785 t = build_ref_for_offset (EXPR_LOCATION (t), t,
786 ipa_get_jf_ancestor_offset (jfunc),
787 ipa_get_jf_ancestor_type (jfunc), NULL, false);
788 return build_fold_addr_expr (t);
790 else
791 return NULL_TREE;
794 /* Determine whether JFUNC evaluates to a known value (that is either a
795 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
796 describes the caller node so that pass-through jump functions can be
797 evaluated. */
799 tree
800 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
802 if (jfunc->type == IPA_JF_CONST)
803 return ipa_get_jf_constant (jfunc);
804 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
805 return ipa_binfo_from_known_type_jfunc (jfunc);
806 else if (jfunc->type == IPA_JF_PASS_THROUGH
807 || jfunc->type == IPA_JF_ANCESTOR)
809 tree input;
810 int idx;
812 if (jfunc->type == IPA_JF_PASS_THROUGH)
813 idx = ipa_get_jf_pass_through_formal_id (jfunc);
814 else
815 idx = ipa_get_jf_ancestor_formal_id (jfunc);
817 if (info->ipcp_orig_node)
818 input = info->known_vals[idx];
819 else
821 struct ipcp_lattice *lat;
823 if (!info->lattices)
825 gcc_checking_assert (!flag_ipa_cp);
826 return NULL_TREE;
828 lat = ipa_get_scalar_lat (info, idx);
829 if (!ipa_lat_is_single_const (lat))
830 return NULL_TREE;
831 input = lat->values->value;
834 if (!input)
835 return NULL_TREE;
837 if (jfunc->type == IPA_JF_PASS_THROUGH)
838 return ipa_get_jf_pass_through_result (jfunc, input);
839 else
840 return ipa_get_jf_ancestor_result (jfunc, input);
842 else
843 return NULL_TREE;
847 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
848 bottom, not containing a variable component and without any known value at
849 the same time. */
851 DEBUG_FUNCTION void
852 ipcp_verify_propagated_values (void)
854 struct cgraph_node *node;
856 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
858 struct ipa_node_params *info = IPA_NODE_REF (node);
859 int i, count = ipa_get_param_count (info);
861 for (i = 0; i < count; i++)
863 struct ipcp_lattice *lat = ipa_get_scalar_lat (info, i);
865 if (!lat->bottom
866 && !lat->contains_variable
867 && lat->values_count == 0)
869 if (dump_file)
871 fprintf (dump_file, "\nIPA lattices after constant "
872 "propagation:\n");
873 print_all_lattices (dump_file, true, false);
876 gcc_unreachable ();
882 /* Return true iff X and Y should be considered equal values by IPA-CP. */
884 static bool
885 values_equal_for_ipcp_p (tree x, tree y)
887 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
889 if (x == y)
890 return true;
892 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
893 return false;
895 if (TREE_CODE (x) == ADDR_EXPR
896 && TREE_CODE (y) == ADDR_EXPR
897 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
898 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
899 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
900 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
901 else
902 return operand_equal_p (x, y, 0);
905 /* Add a new value source to VAL, marking that a value comes from edge CS and
906 (if the underlying jump function is a pass-through or an ancestor one) from
907 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. OFFSET
908 is negative if the source was the scalar value of the parameter itself or
909 the offset within an aggregate. */
911 static void
912 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
913 struct ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset)
915 struct ipcp_value_source *src;
917 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
918 src->offset = offset;
919 src->cs = cs;
920 src->val = src_val;
921 src->index = src_idx;
923 src->next = val->sources;
924 val->sources = src;
927 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
928 it. CS, SRC_VAL SRC_INDEX and OFFSET are meant for add_value_source and
929 have the same meaning. */
931 static bool
932 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
933 struct cgraph_edge *cs, struct ipcp_value *src_val,
934 int src_idx, HOST_WIDE_INT offset)
936 struct ipcp_value *val;
938 if (lat->bottom)
939 return false;
941 for (val = lat->values; val; val = val->next)
942 if (values_equal_for_ipcp_p (val->value, newval))
944 if (ipa_edge_within_scc (cs))
946 struct ipcp_value_source *s;
947 for (s = val->sources; s ; s = s->next)
948 if (s->cs == cs)
949 break;
950 if (s)
951 return false;
954 add_value_source (val, cs, src_val, src_idx, offset);
955 return false;
958 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
960 /* We can only free sources, not the values themselves, because sources
961 of other values in this this SCC might point to them. */
962 for (val = lat->values; val; val = val->next)
964 while (val->sources)
966 struct ipcp_value_source *src = val->sources;
967 val->sources = src->next;
968 pool_free (ipcp_sources_pool, src);
972 lat->values = NULL;
973 return set_lattice_to_bottom (lat);
976 lat->values_count++;
977 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
978 memset (val, 0, sizeof (*val));
980 add_value_source (val, cs, src_val, src_idx, offset);
981 val->value = newval;
982 val->next = lat->values;
983 lat->values = val;
984 return true;
987 /* Like above but passes a special value of offset to distinguish that the
988 origin is the scalar value of the parameter rather than a part of an
989 aggregate. */
991 static inline bool
992 add_scalar_value_to_lattice (struct ipcp_lattice *lat, tree newval,
993 struct cgraph_edge *cs,
994 struct ipcp_value *src_val, int src_idx)
996 return add_value_to_lattice (lat, newval, cs, src_val, src_idx, -1);
999 /* Propagate values through a pass-through jump function JFUNC associated with
1000 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1001 is the index of the source parameter. */
1003 static bool
1004 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
1005 struct ipa_jump_func *jfunc,
1006 struct ipcp_lattice *src_lat,
1007 struct ipcp_lattice *dest_lat,
1008 int src_idx)
1010 struct ipcp_value *src_val;
1011 bool ret = false;
1013 /* Do not create new values when propagating within an SCC because if there
1014 are arithmetic functions with circular dependencies, there is infinite
1015 number of them and we would just make lattices bottom. */
1016 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1017 && ipa_edge_within_scc (cs))
1018 ret = set_lattice_contains_variable (dest_lat);
1019 else
1020 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1022 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1024 if (cstval)
1025 ret |= add_scalar_value_to_lattice (dest_lat, cstval, cs, src_val,
1026 src_idx);
1027 else
1028 ret |= set_lattice_contains_variable (dest_lat);
1031 return ret;
1034 /* Propagate values through an ancestor jump function JFUNC associated with
1035 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1036 is the index of the source parameter. */
1038 static bool
1039 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1040 struct ipa_jump_func *jfunc,
1041 struct ipcp_lattice *src_lat,
1042 struct ipcp_lattice *dest_lat,
1043 int src_idx)
1045 struct ipcp_value *src_val;
1046 bool ret = false;
1048 if (ipa_edge_within_scc (cs))
1049 return set_lattice_contains_variable (dest_lat);
1051 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1053 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1055 if (t)
1056 ret |= add_scalar_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
1057 else
1058 ret |= set_lattice_contains_variable (dest_lat);
1061 return ret;
1064 /* Propagate scalar values across jump function JFUNC that is associated with
1065 edge CS and put the values into DEST_LAT. */
1067 static bool
1068 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1069 struct ipa_jump_func *jfunc,
1070 struct ipcp_lattice *dest_lat)
1072 if (dest_lat->bottom)
1073 return false;
1075 if (jfunc->type == IPA_JF_CONST
1076 || jfunc->type == IPA_JF_KNOWN_TYPE)
1078 tree val;
1080 if (jfunc->type == IPA_JF_KNOWN_TYPE)
1082 val = ipa_binfo_from_known_type_jfunc (jfunc);
1083 if (!val)
1084 return set_lattice_contains_variable (dest_lat);
1086 else
1087 val = ipa_get_jf_constant (jfunc);
1088 return add_scalar_value_to_lattice (dest_lat, val, cs, NULL, 0);
1090 else if (jfunc->type == IPA_JF_PASS_THROUGH
1091 || jfunc->type == IPA_JF_ANCESTOR)
1093 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1094 struct ipcp_lattice *src_lat;
1095 int src_idx;
1096 bool ret;
1098 if (jfunc->type == IPA_JF_PASS_THROUGH)
1099 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1100 else
1101 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1103 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1104 if (src_lat->bottom)
1105 return set_lattice_contains_variable (dest_lat);
1107 /* If we would need to clone the caller and cannot, do not propagate. */
1108 if (!ipcp_versionable_function_p (cs->caller)
1109 && (src_lat->contains_variable
1110 || (src_lat->values_count > 1)))
1111 return set_lattice_contains_variable (dest_lat);
1113 if (jfunc->type == IPA_JF_PASS_THROUGH)
1114 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1115 dest_lat, src_idx);
1116 else
1117 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1118 src_idx);
1120 if (src_lat->contains_variable)
1121 ret |= set_lattice_contains_variable (dest_lat);
1123 return ret;
1126 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1127 use it for indirect inlining), we should propagate them too. */
1128 return set_lattice_contains_variable (dest_lat);
1131 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1132 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1133 other cases, return false). If there are no aggregate items, set
1134 aggs_by_ref to NEW_AGGS_BY_REF. */
1136 static bool
1137 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1138 bool new_aggs_by_ref)
1140 if (dest_plats->aggs)
1142 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1144 set_agg_lats_to_bottom (dest_plats);
1145 return true;
1148 else
1149 dest_plats->aggs_by_ref = new_aggs_by_ref;
1150 return false;
1153 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1154 already existing lattice for the given OFFSET and SIZE, marking all skipped
1155 lattices as containing variable and checking for overlaps. If there is no
1156 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1157 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1158 unless there are too many already. If there are two many, return false. If
1159 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1160 skipped lattices were newly marked as containing variable, set *CHANGE to
1161 true. */
1163 static bool
1164 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1165 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1166 struct ipcp_agg_lattice ***aglat,
1167 bool pre_existing, bool *change)
1169 gcc_checking_assert (offset >= 0);
1171 while (**aglat && (**aglat)->offset < offset)
1173 if ((**aglat)->offset + (**aglat)->size > offset)
1175 set_agg_lats_to_bottom (dest_plats);
1176 return false;
1178 *change |= set_lattice_contains_variable (**aglat);
1179 *aglat = &(**aglat)->next;
1182 if (**aglat && (**aglat)->offset == offset)
1184 if ((**aglat)->size != val_size
1185 || ((**aglat)->next
1186 && (**aglat)->next->offset < offset + val_size))
1188 set_agg_lats_to_bottom (dest_plats);
1189 return false;
1191 gcc_checking_assert (!(**aglat)->next
1192 || (**aglat)->next->offset >= offset + val_size);
1193 return true;
1195 else
1197 struct ipcp_agg_lattice *new_al;
1199 if (**aglat && (**aglat)->offset < offset + val_size)
1201 set_agg_lats_to_bottom (dest_plats);
1202 return false;
1204 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1205 return false;
1206 dest_plats->aggs_count++;
1207 new_al = (struct ipcp_agg_lattice *) pool_alloc (ipcp_agg_lattice_pool);
1208 memset (new_al, 0, sizeof (*new_al));
1210 new_al->offset = offset;
1211 new_al->size = val_size;
1212 new_al->contains_variable = pre_existing;
1214 new_al->next = **aglat;
1215 **aglat = new_al;
1216 return true;
1220 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1221 containing an unknown value. */
1223 static bool
1224 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1226 bool ret = false;
1227 while (aglat)
1229 ret |= set_lattice_contains_variable (aglat);
1230 aglat = aglat->next;
1232 return ret;
1235 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1236 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1237 parameter used for lattice value sources. Return true if DEST_PLATS changed
1238 in any way. */
1240 static bool
1241 merge_aggregate_lattices (struct cgraph_edge *cs,
1242 struct ipcp_param_lattices *dest_plats,
1243 struct ipcp_param_lattices *src_plats,
1244 int src_idx, HOST_WIDE_INT offset_delta)
1246 bool pre_existing = dest_plats->aggs != NULL;
1247 struct ipcp_agg_lattice **dst_aglat;
1248 bool ret = false;
1250 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1251 return true;
1252 if (src_plats->aggs_bottom)
1253 return set_agg_lats_contain_variable (dest_plats);
1254 if (src_plats->aggs_contain_variable)
1255 ret |= set_agg_lats_contain_variable (dest_plats);
1256 dst_aglat = &dest_plats->aggs;
1258 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1259 src_aglat;
1260 src_aglat = src_aglat->next)
1262 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1264 if (new_offset < 0)
1265 continue;
1266 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1267 &dst_aglat, pre_existing, &ret))
1269 struct ipcp_agg_lattice *new_al = *dst_aglat;
1271 dst_aglat = &(*dst_aglat)->next;
1272 if (src_aglat->bottom)
1274 ret |= set_lattice_contains_variable (new_al);
1275 continue;
1277 if (src_aglat->contains_variable)
1278 ret |= set_lattice_contains_variable (new_al);
1279 for (struct ipcp_value *val = src_aglat->values;
1280 val;
1281 val = val->next)
1282 ret |= add_value_to_lattice (new_al, val->value, cs, val, src_idx,
1283 src_aglat->offset);
1285 else if (dest_plats->aggs_bottom)
1286 return true;
1288 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1289 return ret;
1292 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1293 pass-through JFUNC and if so, whether it has conform and conforms to the
1294 rules about propagating values passed by reference. */
1296 static bool
1297 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1298 struct ipa_jump_func *jfunc)
1300 return src_plats->aggs
1301 && (!src_plats->aggs_by_ref
1302 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1305 /* Propagate scalar values across jump function JFUNC that is associated with
1306 edge CS and put the values into DEST_LAT. */
1308 static bool
1309 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1310 struct ipa_jump_func *jfunc,
1311 struct ipcp_param_lattices *dest_plats)
1313 bool ret = false;
1315 if (dest_plats->aggs_bottom)
1316 return false;
1318 if (jfunc->type == IPA_JF_PASS_THROUGH
1319 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1321 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1322 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1323 struct ipcp_param_lattices *src_plats;
1325 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1326 if (agg_pass_through_permissible_p (src_plats, jfunc))
1328 /* Currently we do not produce clobber aggregate jump
1329 functions, replace with merging when we do. */
1330 gcc_assert (!jfunc->agg.items);
1331 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1332 src_idx, 0);
1334 else
1335 ret |= set_agg_lats_contain_variable (dest_plats);
1337 else if (jfunc->type == IPA_JF_ANCESTOR
1338 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1340 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1341 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1342 struct ipcp_param_lattices *src_plats;
1344 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1345 if (src_plats->aggs && src_plats->aggs_by_ref)
1347 /* Currently we do not produce clobber aggregate jump
1348 functions, replace with merging when we do. */
1349 gcc_assert (!jfunc->agg.items);
1350 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1351 ipa_get_jf_ancestor_offset (jfunc));
1353 else if (!src_plats->aggs_by_ref)
1354 ret |= set_agg_lats_to_bottom (dest_plats);
1355 else
1356 ret |= set_agg_lats_contain_variable (dest_plats);
1358 else if (jfunc->agg.items)
1360 bool pre_existing = dest_plats->aggs != NULL;
1361 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1362 struct ipa_agg_jf_item *item;
1363 int i;
1365 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1366 return true;
1368 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1370 HOST_WIDE_INT val_size;
1372 if (item->offset < 0)
1373 continue;
1374 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1375 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1377 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1378 &aglat, pre_existing, &ret))
1380 ret |= add_value_to_lattice (*aglat, item->value, cs, NULL, 0, 0);
1381 aglat = &(*aglat)->next;
1383 else if (dest_plats->aggs_bottom)
1384 return true;
1387 ret |= set_chain_of_aglats_contains_variable (*aglat);
1389 else
1390 ret |= set_agg_lats_contain_variable (dest_plats);
1392 return ret;
1395 /* Propagate constants from the caller to the callee of CS. INFO describes the
1396 caller. */
1398 static bool
1399 propagate_constants_accross_call (struct cgraph_edge *cs)
1401 struct ipa_node_params *callee_info;
1402 enum availability availability;
1403 struct cgraph_node *callee, *alias_or_thunk;
1404 struct ipa_edge_args *args;
1405 bool ret = false;
1406 int i, args_count, parms_count;
1408 callee = cgraph_function_node (cs->callee, &availability);
1409 if (!callee->definition)
1410 return false;
1411 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1412 callee_info = IPA_NODE_REF (callee);
1414 args = IPA_EDGE_REF (cs);
1415 args_count = ipa_get_cs_argument_count (args);
1416 parms_count = ipa_get_param_count (callee_info);
1418 /* If this call goes through a thunk we must not propagate to the first (0th)
1419 parameter. However, we might need to uncover a thunk from below a series
1420 of aliases first. */
1421 alias_or_thunk = cs->callee;
1422 while (alias_or_thunk->alias)
1423 alias_or_thunk = cgraph_alias_target (alias_or_thunk);
1424 if (alias_or_thunk->thunk.thunk_p)
1426 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1427 0));
1428 i = 1;
1430 else
1431 i = 0;
1433 for (; (i < args_count) && (i < parms_count); i++)
1435 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1436 struct ipcp_param_lattices *dest_plats;
1438 dest_plats = ipa_get_parm_lattices (callee_info, i);
1439 if (availability == AVAIL_OVERWRITABLE)
1440 ret |= set_all_contains_variable (dest_plats);
1441 else
1443 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1444 &dest_plats->itself);
1445 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1446 dest_plats);
1449 for (; i < parms_count; i++)
1450 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1452 return ret;
1455 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1456 (which can contain both constants and binfos), KNOWN_BINFOS, KNOWN_AGGS or
1457 AGG_REPS return the destination. The latter three can be NULL. If AGG_REPS
1458 is not NULL, KNOWN_AGGS is ignored. */
1460 static tree
1461 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1462 vec<tree> known_vals,
1463 vec<tree> known_binfos,
1464 vec<ipa_agg_jump_function_p> known_aggs,
1465 struct ipa_agg_replacement_value *agg_reps)
1467 int param_index = ie->indirect_info->param_index;
1468 HOST_WIDE_INT token, anc_offset;
1469 tree otr_type;
1470 tree t;
1471 tree target;
1473 if (param_index == -1
1474 || known_vals.length () <= (unsigned int) param_index)
1475 return NULL_TREE;
1477 if (!ie->indirect_info->polymorphic)
1479 tree t;
1481 if (ie->indirect_info->agg_contents)
1483 if (agg_reps)
1485 t = NULL;
1486 while (agg_reps)
1488 if (agg_reps->index == param_index
1489 && agg_reps->offset == ie->indirect_info->offset
1490 && agg_reps->by_ref == ie->indirect_info->by_ref)
1492 t = agg_reps->value;
1493 break;
1495 agg_reps = agg_reps->next;
1498 else if (known_aggs.length () > (unsigned int) param_index)
1500 struct ipa_agg_jump_function *agg;
1501 agg = known_aggs[param_index];
1502 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1503 ie->indirect_info->by_ref);
1505 else
1506 t = NULL;
1508 else
1509 t = known_vals[param_index];
1511 if (t &&
1512 TREE_CODE (t) == ADDR_EXPR
1513 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1514 return TREE_OPERAND (t, 0);
1515 else
1516 return NULL_TREE;
1519 gcc_assert (!ie->indirect_info->agg_contents);
1520 token = ie->indirect_info->otr_token;
1521 anc_offset = ie->indirect_info->offset;
1522 otr_type = ie->indirect_info->otr_type;
1524 t = known_vals[param_index];
1525 if (!t && known_binfos.length () > (unsigned int) param_index)
1526 t = known_binfos[param_index];
1527 if (!t)
1528 return NULL_TREE;
1530 if (TREE_CODE (t) != TREE_BINFO)
1532 tree binfo;
1533 binfo = gimple_extract_devirt_binfo_from_cst
1534 (t, ie->indirect_info->otr_type);
1535 if (!binfo)
1536 return NULL_TREE;
1537 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1538 if (!binfo)
1539 return NULL_TREE;
1540 target = gimple_get_virt_method_for_binfo (token, binfo);
1542 else
1544 tree binfo;
1546 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1547 if (!binfo)
1548 return NULL_TREE;
1549 target = gimple_get_virt_method_for_binfo (token, binfo);
1551 #ifdef ENABLE_CHECKING
1552 if (target)
1553 gcc_assert (possible_polymorphic_call_target_p
1554 (ie, cgraph_get_node (target)));
1555 #endif
1557 return target;
1561 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1562 (which can contain both constants and binfos), KNOWN_BINFOS (which can be
1563 NULL) or KNOWN_AGGS (which also can be NULL) return the destination. */
1565 tree
1566 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1567 vec<tree> known_vals,
1568 vec<tree> known_binfos,
1569 vec<ipa_agg_jump_function_p> known_aggs)
1571 return ipa_get_indirect_edge_target_1 (ie, known_vals, known_binfos,
1572 known_aggs, NULL);
1575 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1576 and KNOWN_BINFOS. */
1578 static int
1579 devirtualization_time_bonus (struct cgraph_node *node,
1580 vec<tree> known_csts,
1581 vec<tree> known_binfos,
1582 vec<ipa_agg_jump_function_p> known_aggs)
1584 struct cgraph_edge *ie;
1585 int res = 0;
1587 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1589 struct cgraph_node *callee;
1590 struct inline_summary *isummary;
1591 tree target;
1593 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos,
1594 known_aggs);
1595 if (!target)
1596 continue;
1598 /* Only bare minimum benefit for clearly un-inlineable targets. */
1599 res += 1;
1600 callee = cgraph_get_node (target);
1601 if (!callee || !callee->definition)
1602 continue;
1603 isummary = inline_summary (callee);
1604 if (!isummary->inlinable)
1605 continue;
1607 /* FIXME: The values below need re-considering and perhaps also
1608 integrating into the cost metrics, at lest in some very basic way. */
1609 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1610 res += 31;
1611 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1612 res += 15;
1613 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1614 || DECL_DECLARED_INLINE_P (callee->decl))
1615 res += 7;
1618 return res;
1621 /* Return time bonus incurred because of HINTS. */
1623 static int
1624 hint_time_bonus (inline_hints hints)
1626 int result = 0;
1627 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
1628 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
1629 if (hints & INLINE_HINT_array_index)
1630 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
1631 return result;
1634 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1635 and SIZE_COST and with the sum of frequencies of incoming edges to the
1636 potential new clone in FREQUENCIES. */
1638 static bool
1639 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1640 int freq_sum, gcov_type count_sum, int size_cost)
1642 if (time_benefit == 0
1643 || !flag_ipa_cp_clone
1644 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
1645 return false;
1647 gcc_assert (size_cost > 0);
1649 if (max_count)
1651 int factor = (count_sum * 1000) / max_count;
1652 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * factor)
1653 / size_cost);
1655 if (dump_file && (dump_flags & TDF_DETAILS))
1656 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1657 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1658 ") -> evaluation: " HOST_WIDEST_INT_PRINT_DEC
1659 ", threshold: %i\n",
1660 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1661 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1663 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1665 else
1667 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * freq_sum)
1668 / size_cost);
1670 if (dump_file && (dump_flags & TDF_DETAILS))
1671 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1672 "size: %i, freq_sum: %i) -> evaluation: "
1673 HOST_WIDEST_INT_PRINT_DEC ", threshold: %i\n",
1674 time_benefit, size_cost, freq_sum, evaluation,
1675 PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1677 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1681 /* Return all context independent values from aggregate lattices in PLATS in a
1682 vector. Return NULL if there are none. */
1684 static vec<ipa_agg_jf_item_t, va_gc> *
1685 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
1687 vec<ipa_agg_jf_item_t, va_gc> *res = NULL;
1689 if (plats->aggs_bottom
1690 || plats->aggs_contain_variable
1691 || plats->aggs_count == 0)
1692 return NULL;
1694 for (struct ipcp_agg_lattice *aglat = plats->aggs;
1695 aglat;
1696 aglat = aglat->next)
1697 if (ipa_lat_is_single_const (aglat))
1699 struct ipa_agg_jf_item item;
1700 item.offset = aglat->offset;
1701 item.value = aglat->values->value;
1702 vec_safe_push (res, item);
1704 return res;
1707 /* Allocate KNOWN_CSTS, KNOWN_BINFOS and, if non-NULL, KNOWN_AGGS and populate
1708 them with values of parameters that are known independent of the context.
1709 INFO describes the function. If REMOVABLE_PARAMS_COST is non-NULL, the
1710 movement cost of all removable parameters will be stored in it. */
1712 static bool
1713 gather_context_independent_values (struct ipa_node_params *info,
1714 vec<tree> *known_csts,
1715 vec<tree> *known_binfos,
1716 vec<ipa_agg_jump_function_t> *known_aggs,
1717 int *removable_params_cost)
1719 int i, count = ipa_get_param_count (info);
1720 bool ret = false;
1722 known_csts->create (0);
1723 known_binfos->create (0);
1724 known_csts->safe_grow_cleared (count);
1725 known_binfos->safe_grow_cleared (count);
1726 if (known_aggs)
1728 known_aggs->create (0);
1729 known_aggs->safe_grow_cleared (count);
1732 if (removable_params_cost)
1733 *removable_params_cost = 0;
1735 for (i = 0; i < count ; i++)
1737 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1738 struct ipcp_lattice *lat = &plats->itself;
1740 if (ipa_lat_is_single_const (lat))
1742 struct ipcp_value *val = lat->values;
1743 if (TREE_CODE (val->value) != TREE_BINFO)
1745 (*known_csts)[i] = val->value;
1746 if (removable_params_cost)
1747 *removable_params_cost
1748 += estimate_move_cost (TREE_TYPE (val->value));
1749 ret = true;
1751 else if (plats->virt_call)
1753 (*known_binfos)[i] = val->value;
1754 ret = true;
1756 else if (removable_params_cost
1757 && !ipa_is_param_used (info, i))
1758 *removable_params_cost += ipa_get_param_move_cost (info, i);
1760 else if (removable_params_cost
1761 && !ipa_is_param_used (info, i))
1762 *removable_params_cost
1763 += ipa_get_param_move_cost (info, i);
1765 if (known_aggs)
1767 vec<ipa_agg_jf_item_t, va_gc> *agg_items;
1768 struct ipa_agg_jump_function *ajf;
1770 agg_items = context_independent_aggregate_values (plats);
1771 ajf = &(*known_aggs)[i];
1772 ajf->items = agg_items;
1773 ajf->by_ref = plats->aggs_by_ref;
1774 ret |= agg_items != NULL;
1778 return ret;
1781 /* The current interface in ipa-inline-analysis requires a pointer vector.
1782 Create it.
1784 FIXME: That interface should be re-worked, this is slightly silly. Still,
1785 I'd like to discuss how to change it first and this demonstrates the
1786 issue. */
1788 static vec<ipa_agg_jump_function_p>
1789 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function_t> known_aggs)
1791 vec<ipa_agg_jump_function_p> ret;
1792 struct ipa_agg_jump_function *ajf;
1793 int i;
1795 ret.create (known_aggs.length ());
1796 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
1797 ret.quick_push (ajf);
1798 return ret;
1801 /* Iterate over known values of parameters of NODE and estimate the local
1802 effects in terms of time and size they have. */
1804 static void
1805 estimate_local_effects (struct cgraph_node *node)
1807 struct ipa_node_params *info = IPA_NODE_REF (node);
1808 int i, count = ipa_get_param_count (info);
1809 vec<tree> known_csts, known_binfos;
1810 vec<ipa_agg_jump_function_t> known_aggs;
1811 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
1812 bool always_const;
1813 int base_time = inline_summary (node)->time;
1814 int removable_params_cost;
1816 if (!count || !ipcp_versionable_function_p (node))
1817 return;
1819 if (dump_file && (dump_flags & TDF_DETAILS))
1820 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1821 node->name (), node->order, base_time);
1823 always_const = gather_context_independent_values (info, &known_csts,
1824 &known_binfos, &known_aggs,
1825 &removable_params_cost);
1826 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
1827 if (always_const)
1829 struct caller_statistics stats;
1830 inline_hints hints;
1831 int time, size;
1833 init_caller_stats (&stats);
1834 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1835 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1836 known_aggs_ptrs, &size, &time, &hints);
1837 time -= devirtualization_time_bonus (node, known_csts, known_binfos,
1838 known_aggs_ptrs);
1839 time -= hint_time_bonus (hints);
1840 time -= removable_params_cost;
1841 size -= stats.n_calls * removable_params_cost;
1843 if (dump_file)
1844 fprintf (dump_file, " - context independent values, size: %i, "
1845 "time_benefit: %i\n", size, base_time - time);
1847 if (size <= 0
1848 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1850 info->do_clone_for_all_contexts = true;
1851 base_time = time;
1853 if (dump_file)
1854 fprintf (dump_file, " Decided to specialize for all "
1855 "known contexts, code not going to grow.\n");
1857 else if (good_cloning_opportunity_p (node, base_time - time,
1858 stats.freq_sum, stats.count_sum,
1859 size))
1861 if (size + overall_size <= max_new_size)
1863 info->do_clone_for_all_contexts = true;
1864 base_time = time;
1865 overall_size += size;
1867 if (dump_file)
1868 fprintf (dump_file, " Decided to specialize for all "
1869 "known contexts, growth deemed beneficial.\n");
1871 else if (dump_file && (dump_flags & TDF_DETAILS))
1872 fprintf (dump_file, " Not cloning for all contexts because "
1873 "max_new_size would be reached with %li.\n",
1874 size + overall_size);
1878 for (i = 0; i < count ; i++)
1880 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1881 struct ipcp_lattice *lat = &plats->itself;
1882 struct ipcp_value *val;
1883 int emc;
1885 if (lat->bottom
1886 || !lat->values
1887 || known_csts[i]
1888 || known_binfos[i])
1889 continue;
1891 for (val = lat->values; val; val = val->next)
1893 int time, size, time_benefit;
1894 inline_hints hints;
1896 if (TREE_CODE (val->value) != TREE_BINFO)
1898 known_csts[i] = val->value;
1899 known_binfos[i] = NULL_TREE;
1900 emc = estimate_move_cost (TREE_TYPE (val->value));
1902 else if (plats->virt_call)
1904 known_csts[i] = NULL_TREE;
1905 known_binfos[i] = val->value;
1906 emc = 0;
1908 else
1909 continue;
1911 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1912 known_aggs_ptrs, &size, &time,
1913 &hints);
1914 time_benefit = base_time - time
1915 + devirtualization_time_bonus (node, known_csts, known_binfos,
1916 known_aggs_ptrs)
1917 + hint_time_bonus (hints)
1918 + removable_params_cost + emc;
1920 gcc_checking_assert (size >=0);
1921 /* The inliner-heuristics based estimates may think that in certain
1922 contexts some functions do not have any size at all but we want
1923 all specializations to have at least a tiny cost, not least not to
1924 divide by zero. */
1925 if (size == 0)
1926 size = 1;
1928 if (dump_file && (dump_flags & TDF_DETAILS))
1930 fprintf (dump_file, " - estimates for value ");
1931 print_ipcp_constant_value (dump_file, val->value);
1932 fprintf (dump_file, " for ");
1933 ipa_dump_param (dump_file, info, i);
1934 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
1935 time_benefit, size);
1938 val->local_time_benefit = time_benefit;
1939 val->local_size_cost = size;
1941 known_binfos[i] = NULL_TREE;
1942 known_csts[i] = NULL_TREE;
1945 for (i = 0; i < count ; i++)
1947 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1948 struct ipa_agg_jump_function *ajf;
1949 struct ipcp_agg_lattice *aglat;
1951 if (plats->aggs_bottom || !plats->aggs)
1952 continue;
1954 ajf = &known_aggs[i];
1955 for (aglat = plats->aggs; aglat; aglat = aglat->next)
1957 struct ipcp_value *val;
1958 if (aglat->bottom || !aglat->values
1959 /* If the following is true, the one value is in known_aggs. */
1960 || (!plats->aggs_contain_variable
1961 && ipa_lat_is_single_const (aglat)))
1962 continue;
1964 for (val = aglat->values; val; val = val->next)
1966 int time, size, time_benefit;
1967 struct ipa_agg_jf_item item;
1968 inline_hints hints;
1970 item.offset = aglat->offset;
1971 item.value = val->value;
1972 vec_safe_push (ajf->items, item);
1974 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1975 known_aggs_ptrs, &size, &time,
1976 &hints);
1977 time_benefit = base_time - time
1978 + devirtualization_time_bonus (node, known_csts, known_binfos,
1979 known_aggs_ptrs)
1980 + hint_time_bonus (hints);
1981 gcc_checking_assert (size >=0);
1982 if (size == 0)
1983 size = 1;
1985 if (dump_file && (dump_flags & TDF_DETAILS))
1987 fprintf (dump_file, " - estimates for value ");
1988 print_ipcp_constant_value (dump_file, val->value);
1989 fprintf (dump_file, " for ");
1990 ipa_dump_param (dump_file, info, i);
1991 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
1992 "]: time_benefit: %i, size: %i\n",
1993 plats->aggs_by_ref ? "ref " : "",
1994 aglat->offset, time_benefit, size);
1997 val->local_time_benefit = time_benefit;
1998 val->local_size_cost = size;
1999 ajf->items->pop ();
2004 for (i = 0; i < count ; i++)
2005 vec_free (known_aggs[i].items);
2007 known_csts.release ();
2008 known_binfos.release ();
2009 known_aggs.release ();
2010 known_aggs_ptrs.release ();
2014 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2015 topological sort of values. */
2017 static void
2018 add_val_to_toposort (struct ipcp_value *cur_val)
2020 static int dfs_counter = 0;
2021 static struct ipcp_value *stack;
2022 struct ipcp_value_source *src;
2024 if (cur_val->dfs)
2025 return;
2027 dfs_counter++;
2028 cur_val->dfs = dfs_counter;
2029 cur_val->low_link = dfs_counter;
2031 cur_val->topo_next = stack;
2032 stack = cur_val;
2033 cur_val->on_stack = true;
2035 for (src = cur_val->sources; src; src = src->next)
2036 if (src->val)
2038 if (src->val->dfs == 0)
2040 add_val_to_toposort (src->val);
2041 if (src->val->low_link < cur_val->low_link)
2042 cur_val->low_link = src->val->low_link;
2044 else if (src->val->on_stack
2045 && src->val->dfs < cur_val->low_link)
2046 cur_val->low_link = src->val->dfs;
2049 if (cur_val->dfs == cur_val->low_link)
2051 struct ipcp_value *v, *scc_list = NULL;
2055 v = stack;
2056 stack = v->topo_next;
2057 v->on_stack = false;
2059 v->scc_next = scc_list;
2060 scc_list = v;
2062 while (v != cur_val);
2064 cur_val->topo_next = values_topo;
2065 values_topo = cur_val;
2069 /* Add all values in lattices associated with NODE to the topological sort if
2070 they are not there yet. */
2072 static void
2073 add_all_node_vals_to_toposort (struct cgraph_node *node)
2075 struct ipa_node_params *info = IPA_NODE_REF (node);
2076 int i, count = ipa_get_param_count (info);
2078 for (i = 0; i < count ; i++)
2080 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2081 struct ipcp_lattice *lat = &plats->itself;
2082 struct ipcp_agg_lattice *aglat;
2083 struct ipcp_value *val;
2085 if (!lat->bottom)
2086 for (val = lat->values; val; val = val->next)
2087 add_val_to_toposort (val);
2089 if (!plats->aggs_bottom)
2090 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2091 if (!aglat->bottom)
2092 for (val = aglat->values; val; val = val->next)
2093 add_val_to_toposort (val);
2097 /* One pass of constants propagation along the call graph edges, from callers
2098 to callees (requires topological ordering in TOPO), iterate over strongly
2099 connected components. */
2101 static void
2102 propagate_constants_topo (struct topo_info *topo)
2104 int i;
2106 for (i = topo->nnodes - 1; i >= 0; i--)
2108 unsigned j;
2109 struct cgraph_node *v, *node = topo->order[i];
2110 vec<cgraph_node_ptr> cycle_nodes = ipa_get_nodes_in_cycle (node);
2112 /* First, iteratively propagate within the strongly connected component
2113 until all lattices stabilize. */
2114 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2115 if (cgraph_function_with_gimple_body_p (v))
2116 push_node_to_stack (topo, v);
2118 v = pop_node_from_stack (topo);
2119 while (v)
2121 struct cgraph_edge *cs;
2123 for (cs = v->callees; cs; cs = cs->next_callee)
2124 if (ipa_edge_within_scc (cs)
2125 && propagate_constants_accross_call (cs))
2126 push_node_to_stack (topo, cs->callee);
2127 v = pop_node_from_stack (topo);
2130 /* Afterwards, propagate along edges leading out of the SCC, calculates
2131 the local effects of the discovered constants and all valid values to
2132 their topological sort. */
2133 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2134 if (cgraph_function_with_gimple_body_p (v))
2136 struct cgraph_edge *cs;
2138 estimate_local_effects (v);
2139 add_all_node_vals_to_toposort (v);
2140 for (cs = v->callees; cs; cs = cs->next_callee)
2141 if (!ipa_edge_within_scc (cs))
2142 propagate_constants_accross_call (cs);
2144 cycle_nodes.release ();
2149 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2150 the bigger one if otherwise. */
2152 static int
2153 safe_add (int a, int b)
2155 if (a > INT_MAX/2 || b > INT_MAX/2)
2156 return a > b ? a : b;
2157 else
2158 return a + b;
2162 /* Propagate the estimated effects of individual values along the topological
2163 from the dependent values to those they depend on. */
2165 static void
2166 propagate_effects (void)
2168 struct ipcp_value *base;
2170 for (base = values_topo; base; base = base->topo_next)
2172 struct ipcp_value_source *src;
2173 struct ipcp_value *val;
2174 int time = 0, size = 0;
2176 for (val = base; val; val = val->scc_next)
2178 time = safe_add (time,
2179 val->local_time_benefit + val->prop_time_benefit);
2180 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2183 for (val = base; val; val = val->scc_next)
2184 for (src = val->sources; src; src = src->next)
2185 if (src->val
2186 && cgraph_maybe_hot_edge_p (src->cs))
2188 src->val->prop_time_benefit = safe_add (time,
2189 src->val->prop_time_benefit);
2190 src->val->prop_size_cost = safe_add (size,
2191 src->val->prop_size_cost);
2197 /* Propagate constants, binfos and their effects from the summaries
2198 interprocedurally. */
2200 static void
2201 ipcp_propagate_stage (struct topo_info *topo)
2203 struct cgraph_node *node;
2205 if (dump_file)
2206 fprintf (dump_file, "\n Propagating constants:\n\n");
2208 if (in_lto_p)
2209 ipa_update_after_lto_read ();
2212 FOR_EACH_DEFINED_FUNCTION (node)
2214 struct ipa_node_params *info = IPA_NODE_REF (node);
2216 determine_versionability (node);
2217 if (cgraph_function_with_gimple_body_p (node))
2219 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2220 ipa_get_param_count (info));
2221 initialize_node_lattices (node);
2223 if (node->definition && !node->alias)
2224 overall_size += inline_summary (node)->self_size;
2225 if (node->count > max_count)
2226 max_count = node->count;
2229 max_new_size = overall_size;
2230 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2231 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2232 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2234 if (dump_file)
2235 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2236 overall_size, max_new_size);
2238 propagate_constants_topo (topo);
2239 #ifdef ENABLE_CHECKING
2240 ipcp_verify_propagated_values ();
2241 #endif
2242 propagate_effects ();
2244 if (dump_file)
2246 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2247 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2251 /* Discover newly direct outgoing edges from NODE which is a new clone with
2252 known KNOWN_VALS and make them direct. */
2254 static void
2255 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2256 vec<tree> known_vals,
2257 struct ipa_agg_replacement_value *aggvals)
2259 struct cgraph_edge *ie, *next_ie;
2260 bool found = false;
2262 for (ie = node->indirect_calls; ie; ie = next_ie)
2264 tree target;
2266 next_ie = ie->next_callee;
2267 target = ipa_get_indirect_edge_target_1 (ie, known_vals, vNULL, vNULL,
2268 aggvals);
2269 if (target)
2271 bool agg_contents = ie->indirect_info->agg_contents;
2272 bool polymorphic = ie->indirect_info->polymorphic;
2273 bool param_index = ie->indirect_info->param_index;
2274 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target);
2275 found = true;
2277 if (cs && !agg_contents && !polymorphic)
2279 struct ipa_node_params *info = IPA_NODE_REF (node);
2280 int c = ipa_get_controlled_uses (info, param_index);
2281 if (c != IPA_UNDESCRIBED_USE)
2283 struct ipa_ref *to_del;
2285 c--;
2286 ipa_set_controlled_uses (info, param_index, c);
2287 if (dump_file && (dump_flags & TDF_DETAILS))
2288 fprintf (dump_file, " controlled uses count of param "
2289 "%i bumped down to %i\n", param_index, c);
2290 if (c == 0
2291 && (to_del = ipa_find_reference (node,
2292 cs->callee,
2293 NULL, 0)))
2295 if (dump_file && (dump_flags & TDF_DETAILS))
2296 fprintf (dump_file, " and even removing its "
2297 "cloning-created reference\n");
2298 ipa_remove_reference (to_del);
2304 /* Turning calls to direct calls will improve overall summary. */
2305 if (found)
2306 inline_update_overall_summary (node);
2309 /* Vector of pointers which for linked lists of clones of an original crgaph
2310 edge. */
2312 static vec<cgraph_edge_p> next_edge_clone;
2314 static inline void
2315 grow_next_edge_clone_vector (void)
2317 if (next_edge_clone.length ()
2318 <= (unsigned) cgraph_edge_max_uid)
2319 next_edge_clone.safe_grow_cleared (cgraph_edge_max_uid + 1);
2322 /* Edge duplication hook to grow the appropriate linked list in
2323 next_edge_clone. */
2325 static void
2326 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2327 __attribute__((unused)) void *data)
2329 grow_next_edge_clone_vector ();
2330 next_edge_clone[dst->uid] = next_edge_clone[src->uid];
2331 next_edge_clone[src->uid] = dst;
2334 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2335 parameter with the given INDEX. */
2337 static tree
2338 get_clone_agg_value (struct cgraph_node *node, HOST_WIDEST_INT offset,
2339 int index)
2341 struct ipa_agg_replacement_value *aggval;
2343 aggval = ipa_get_agg_replacements_for_node (node);
2344 while (aggval)
2346 if (aggval->offset == offset
2347 && aggval->index == index)
2348 return aggval->value;
2349 aggval = aggval->next;
2351 return NULL_TREE;
2354 /* Return true if edge CS does bring about the value described by SRC. */
2356 static bool
2357 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
2358 struct ipcp_value_source *src)
2360 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2361 struct ipa_node_params *dst_info = IPA_NODE_REF (cs->callee);
2363 if ((dst_info->ipcp_orig_node && !dst_info->is_all_contexts_clone)
2364 || caller_info->node_dead)
2365 return false;
2366 if (!src->val)
2367 return true;
2369 if (caller_info->ipcp_orig_node)
2371 tree t;
2372 if (src->offset == -1)
2373 t = caller_info->known_vals[src->index];
2374 else
2375 t = get_clone_agg_value (cs->caller, src->offset, src->index);
2376 return (t != NULL_TREE
2377 && values_equal_for_ipcp_p (src->val->value, t));
2379 else
2381 struct ipcp_agg_lattice *aglat;
2382 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
2383 src->index);
2384 if (src->offset == -1)
2385 return (ipa_lat_is_single_const (&plats->itself)
2386 && values_equal_for_ipcp_p (src->val->value,
2387 plats->itself.values->value));
2388 else
2390 if (plats->aggs_bottom || plats->aggs_contain_variable)
2391 return false;
2392 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2393 if (aglat->offset == src->offset)
2394 return (ipa_lat_is_single_const (aglat)
2395 && values_equal_for_ipcp_p (src->val->value,
2396 aglat->values->value));
2398 return false;
2402 /* Get the next clone in the linked list of clones of an edge. */
2404 static inline struct cgraph_edge *
2405 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
2407 return next_edge_clone[cs->uid];
2410 /* Given VAL, iterate over all its sources and if they still hold, add their
2411 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
2412 respectively. */
2414 static bool
2415 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
2416 gcov_type *count_sum, int *caller_count)
2418 struct ipcp_value_source *src;
2419 int freq = 0, count = 0;
2420 gcov_type cnt = 0;
2421 bool hot = false;
2423 for (src = val->sources; src; src = src->next)
2425 struct cgraph_edge *cs = src->cs;
2426 while (cs)
2428 if (cgraph_edge_brings_value_p (cs, src))
2430 count++;
2431 freq += cs->frequency;
2432 cnt += cs->count;
2433 hot |= cgraph_maybe_hot_edge_p (cs);
2435 cs = get_next_cgraph_edge_clone (cs);
2439 *freq_sum = freq;
2440 *count_sum = cnt;
2441 *caller_count = count;
2442 return hot;
2445 /* Return a vector of incoming edges that do bring value VAL. It is assumed
2446 their number is known and equal to CALLER_COUNT. */
2448 static vec<cgraph_edge_p>
2449 gather_edges_for_value (struct ipcp_value *val, int caller_count)
2451 struct ipcp_value_source *src;
2452 vec<cgraph_edge_p> ret;
2454 ret.create (caller_count);
2455 for (src = val->sources; src; src = src->next)
2457 struct cgraph_edge *cs = src->cs;
2458 while (cs)
2460 if (cgraph_edge_brings_value_p (cs, src))
2461 ret.quick_push (cs);
2462 cs = get_next_cgraph_edge_clone (cs);
2466 return ret;
2469 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
2470 Return it or NULL if for some reason it cannot be created. */
2472 static struct ipa_replace_map *
2473 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
2475 struct ipa_replace_map *replace_map;
2478 replace_map = ggc_alloc_ipa_replace_map ();
2479 if (dump_file)
2481 fprintf (dump_file, " replacing ");
2482 ipa_dump_param (dump_file, info, parm_num);
2484 fprintf (dump_file, " with const ");
2485 print_generic_expr (dump_file, value, 0);
2486 fprintf (dump_file, "\n");
2488 replace_map->old_tree = NULL;
2489 replace_map->parm_num = parm_num;
2490 replace_map->new_tree = value;
2491 replace_map->replace_p = true;
2492 replace_map->ref_p = false;
2494 return replace_map;
2497 /* Dump new profiling counts */
2499 static void
2500 dump_profile_updates (struct cgraph_node *orig_node,
2501 struct cgraph_node *new_node)
2503 struct cgraph_edge *cs;
2505 fprintf (dump_file, " setting count of the specialized node to "
2506 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
2507 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2508 fprintf (dump_file, " edge to %s has count "
2509 HOST_WIDE_INT_PRINT_DEC "\n",
2510 cs->callee->name (), (HOST_WIDE_INT) cs->count);
2512 fprintf (dump_file, " setting count of the original node to "
2513 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
2514 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2515 fprintf (dump_file, " edge to %s is left with "
2516 HOST_WIDE_INT_PRINT_DEC "\n",
2517 cs->callee->name (), (HOST_WIDE_INT) cs->count);
2520 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
2521 their profile information to reflect this. */
2523 static void
2524 update_profiling_info (struct cgraph_node *orig_node,
2525 struct cgraph_node *new_node)
2527 struct cgraph_edge *cs;
2528 struct caller_statistics stats;
2529 gcov_type new_sum, orig_sum;
2530 gcov_type remainder, orig_node_count = orig_node->count;
2532 if (orig_node_count == 0)
2533 return;
2535 init_caller_stats (&stats);
2536 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
2537 orig_sum = stats.count_sum;
2538 init_caller_stats (&stats);
2539 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
2540 new_sum = stats.count_sum;
2542 if (orig_node_count < orig_sum + new_sum)
2544 if (dump_file)
2545 fprintf (dump_file, " Problem: node %s/%i has too low count "
2546 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
2547 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
2548 orig_node->name (), orig_node->order,
2549 (HOST_WIDE_INT) orig_node_count,
2550 (HOST_WIDE_INT) (orig_sum + new_sum));
2552 orig_node_count = (orig_sum + new_sum) * 12 / 10;
2553 if (dump_file)
2554 fprintf (dump_file, " proceeding by pretending it was "
2555 HOST_WIDE_INT_PRINT_DEC "\n",
2556 (HOST_WIDE_INT) orig_node_count);
2559 new_node->count = new_sum;
2560 remainder = orig_node_count - new_sum;
2561 orig_node->count = remainder;
2563 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2564 if (cs->frequency)
2565 cs->count = apply_probability (cs->count,
2566 GCOV_COMPUTE_SCALE (new_sum,
2567 orig_node_count));
2568 else
2569 cs->count = 0;
2571 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2572 cs->count = apply_probability (cs->count,
2573 GCOV_COMPUTE_SCALE (remainder,
2574 orig_node_count));
2576 if (dump_file)
2577 dump_profile_updates (orig_node, new_node);
2580 /* Update the respective profile of specialized NEW_NODE and the original
2581 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
2582 have been redirected to the specialized version. */
2584 static void
2585 update_specialized_profile (struct cgraph_node *new_node,
2586 struct cgraph_node *orig_node,
2587 gcov_type redirected_sum)
2589 struct cgraph_edge *cs;
2590 gcov_type new_node_count, orig_node_count = orig_node->count;
2592 if (dump_file)
2593 fprintf (dump_file, " the sum of counts of redirected edges is "
2594 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
2595 if (orig_node_count == 0)
2596 return;
2598 gcc_assert (orig_node_count >= redirected_sum);
2600 new_node_count = new_node->count;
2601 new_node->count += redirected_sum;
2602 orig_node->count -= redirected_sum;
2604 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2605 if (cs->frequency)
2606 cs->count += apply_probability (cs->count,
2607 GCOV_COMPUTE_SCALE (redirected_sum,
2608 new_node_count));
2609 else
2610 cs->count = 0;
2612 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2614 gcov_type dec = apply_probability (cs->count,
2615 GCOV_COMPUTE_SCALE (redirected_sum,
2616 orig_node_count));
2617 if (dec < cs->count)
2618 cs->count -= dec;
2619 else
2620 cs->count = 0;
2623 if (dump_file)
2624 dump_profile_updates (orig_node, new_node);
2627 /* Create a specialized version of NODE with known constants and types of
2628 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
2630 static struct cgraph_node *
2631 create_specialized_node (struct cgraph_node *node,
2632 vec<tree> known_vals,
2633 struct ipa_agg_replacement_value *aggvals,
2634 vec<cgraph_edge_p> callers)
2636 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
2637 vec<ipa_replace_map_p, va_gc> *replace_trees = NULL;
2638 struct ipa_agg_replacement_value *av;
2639 struct cgraph_node *new_node;
2640 int i, count = ipa_get_param_count (info);
2641 bitmap args_to_skip;
2643 gcc_assert (!info->ipcp_orig_node);
2645 if (node->local.can_change_signature)
2647 args_to_skip = BITMAP_GGC_ALLOC ();
2648 for (i = 0; i < count; i++)
2650 tree t = known_vals[i];
2652 if ((t && TREE_CODE (t) != TREE_BINFO)
2653 || !ipa_is_param_used (info, i))
2654 bitmap_set_bit (args_to_skip, i);
2657 else
2659 args_to_skip = NULL;
2660 if (dump_file && (dump_flags & TDF_DETAILS))
2661 fprintf (dump_file, " cannot change function signature\n");
2664 for (i = 0; i < count ; i++)
2666 tree t = known_vals[i];
2667 if (t && TREE_CODE (t) != TREE_BINFO)
2669 struct ipa_replace_map *replace_map;
2671 replace_map = get_replacement_map (info, t, i);
2672 if (replace_map)
2673 vec_safe_push (replace_trees, replace_map);
2677 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2678 args_to_skip, "constprop");
2679 ipa_set_node_agg_value_chain (new_node, aggvals);
2680 for (av = aggvals; av; av = av->next)
2681 ipa_maybe_record_reference (new_node, av->value,
2682 IPA_REF_ADDR, NULL);
2684 if (dump_file && (dump_flags & TDF_DETAILS))
2686 fprintf (dump_file, " the new node is %s/%i.\n",
2687 new_node->name (), new_node->order);
2688 if (aggvals)
2689 ipa_dump_agg_replacement_values (dump_file, aggvals);
2691 gcc_checking_assert (ipa_node_params_vector.exists ()
2692 && (ipa_node_params_vector.length ()
2693 > (unsigned) cgraph_max_uid));
2694 update_profiling_info (node, new_node);
2695 new_info = IPA_NODE_REF (new_node);
2696 new_info->ipcp_orig_node = node;
2697 new_info->known_vals = known_vals;
2699 ipcp_discover_new_direct_edges (new_node, known_vals, aggvals);
2701 callers.release ();
2702 return new_node;
2705 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2706 KNOWN_VALS with constants and types that are also known for all of the
2707 CALLERS. */
2709 static void
2710 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
2711 vec<tree> known_vals,
2712 vec<cgraph_edge_p> callers)
2714 struct ipa_node_params *info = IPA_NODE_REF (node);
2715 int i, count = ipa_get_param_count (info);
2717 for (i = 0; i < count ; i++)
2719 struct cgraph_edge *cs;
2720 tree newval = NULL_TREE;
2721 int j;
2723 if (ipa_get_scalar_lat (info, i)->bottom || known_vals[i])
2724 continue;
2726 FOR_EACH_VEC_ELT (callers, j, cs)
2728 struct ipa_jump_func *jump_func;
2729 tree t;
2731 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2733 newval = NULL_TREE;
2734 break;
2736 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2737 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2738 if (!t
2739 || (newval
2740 && !values_equal_for_ipcp_p (t, newval)))
2742 newval = NULL_TREE;
2743 break;
2745 else
2746 newval = t;
2749 if (newval)
2751 if (dump_file && (dump_flags & TDF_DETAILS))
2753 fprintf (dump_file, " adding an extra known scalar value ");
2754 print_ipcp_constant_value (dump_file, newval);
2755 fprintf (dump_file, " for ");
2756 ipa_dump_param (dump_file, info, i);
2757 fprintf (dump_file, "\n");
2760 known_vals[i] = newval;
2765 /* Go through PLATS and create a vector of values consisting of values and
2766 offsets (minus OFFSET) of lattices that contain only a single value. */
2768 static vec<ipa_agg_jf_item_t>
2769 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
2771 vec<ipa_agg_jf_item_t> res = vNULL;
2773 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2774 return vNULL;
2776 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
2777 if (ipa_lat_is_single_const (aglat))
2779 struct ipa_agg_jf_item ti;
2780 ti.offset = aglat->offset - offset;
2781 ti.value = aglat->values->value;
2782 res.safe_push (ti);
2784 return res;
2787 /* Intersect all values in INTER with single value lattices in PLATS (while
2788 subtracting OFFSET). */
2790 static void
2791 intersect_with_plats (struct ipcp_param_lattices *plats,
2792 vec<ipa_agg_jf_item_t> *inter,
2793 HOST_WIDE_INT offset)
2795 struct ipcp_agg_lattice *aglat;
2796 struct ipa_agg_jf_item *item;
2797 int k;
2799 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2801 inter->release ();
2802 return;
2805 aglat = plats->aggs;
2806 FOR_EACH_VEC_ELT (*inter, k, item)
2808 bool found = false;
2809 if (!item->value)
2810 continue;
2811 while (aglat)
2813 if (aglat->offset - offset > item->offset)
2814 break;
2815 if (aglat->offset - offset == item->offset)
2817 gcc_checking_assert (item->value);
2818 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
2819 found = true;
2820 break;
2822 aglat = aglat->next;
2824 if (!found)
2825 item->value = NULL_TREE;
2829 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
2830 vector result while subtracting OFFSET from the individual value offsets. */
2832 static vec<ipa_agg_jf_item_t>
2833 agg_replacements_to_vector (struct cgraph_node *node, int index,
2834 HOST_WIDE_INT offset)
2836 struct ipa_agg_replacement_value *av;
2837 vec<ipa_agg_jf_item_t> res = vNULL;
2839 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
2840 if (av->index == index
2841 && (av->offset - offset) >= 0)
2843 struct ipa_agg_jf_item item;
2844 gcc_checking_assert (av->value);
2845 item.offset = av->offset - offset;
2846 item.value = av->value;
2847 res.safe_push (item);
2850 return res;
2853 /* Intersect all values in INTER with those that we have already scheduled to
2854 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
2855 (while subtracting OFFSET). */
2857 static void
2858 intersect_with_agg_replacements (struct cgraph_node *node, int index,
2859 vec<ipa_agg_jf_item_t> *inter,
2860 HOST_WIDE_INT offset)
2862 struct ipa_agg_replacement_value *srcvals;
2863 struct ipa_agg_jf_item *item;
2864 int i;
2866 srcvals = ipa_get_agg_replacements_for_node (node);
2867 if (!srcvals)
2869 inter->release ();
2870 return;
2873 FOR_EACH_VEC_ELT (*inter, i, item)
2875 struct ipa_agg_replacement_value *av;
2876 bool found = false;
2877 if (!item->value)
2878 continue;
2879 for (av = srcvals; av; av = av->next)
2881 gcc_checking_assert (av->value);
2882 if (av->index == index
2883 && av->offset - offset == item->offset)
2885 if (values_equal_for_ipcp_p (item->value, av->value))
2886 found = true;
2887 break;
2890 if (!found)
2891 item->value = NULL_TREE;
2895 /* Intersect values in INTER with aggregate values that come along edge CS to
2896 parameter number INDEX and return it. If INTER does not actually exist yet,
2897 copy all incoming values to it. If we determine we ended up with no values
2898 whatsoever, return a released vector. */
2900 static vec<ipa_agg_jf_item_t>
2901 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
2902 vec<ipa_agg_jf_item_t> inter)
2904 struct ipa_jump_func *jfunc;
2905 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
2906 if (jfunc->type == IPA_JF_PASS_THROUGH
2907 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2909 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2910 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2912 if (caller_info->ipcp_orig_node)
2914 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
2915 struct ipcp_param_lattices *orig_plats;
2916 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
2917 src_idx);
2918 if (agg_pass_through_permissible_p (orig_plats, jfunc))
2920 if (!inter.exists ())
2921 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
2922 else
2923 intersect_with_agg_replacements (cs->caller, src_idx,
2924 &inter, 0);
2927 else
2929 struct ipcp_param_lattices *src_plats;
2930 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
2931 if (agg_pass_through_permissible_p (src_plats, jfunc))
2933 /* Currently we do not produce clobber aggregate jump
2934 functions, adjust when we do. */
2935 gcc_checking_assert (!jfunc->agg.items);
2936 if (!inter.exists ())
2937 inter = copy_plats_to_inter (src_plats, 0);
2938 else
2939 intersect_with_plats (src_plats, &inter, 0);
2943 else if (jfunc->type == IPA_JF_ANCESTOR
2944 && ipa_get_jf_ancestor_agg_preserved (jfunc))
2946 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2947 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2948 struct ipcp_param_lattices *src_plats;
2949 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
2951 if (caller_info->ipcp_orig_node)
2953 if (!inter.exists ())
2954 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
2955 else
2956 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
2957 delta);
2959 else
2961 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
2962 /* Currently we do not produce clobber aggregate jump
2963 functions, adjust when we do. */
2964 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
2965 if (!inter.exists ())
2966 inter = copy_plats_to_inter (src_plats, delta);
2967 else
2968 intersect_with_plats (src_plats, &inter, delta);
2971 else if (jfunc->agg.items)
2973 struct ipa_agg_jf_item *item;
2974 int k;
2976 if (!inter.exists ())
2977 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
2978 inter.safe_push ((*jfunc->agg.items)[i]);
2979 else
2980 FOR_EACH_VEC_ELT (inter, k, item)
2982 int l = 0;
2983 bool found = false;;
2985 if (!item->value)
2986 continue;
2988 while ((unsigned) l < jfunc->agg.items->length ())
2990 struct ipa_agg_jf_item *ti;
2991 ti = &(*jfunc->agg.items)[l];
2992 if (ti->offset > item->offset)
2993 break;
2994 if (ti->offset == item->offset)
2996 gcc_checking_assert (ti->value);
2997 if (values_equal_for_ipcp_p (item->value,
2998 ti->value))
2999 found = true;
3000 break;
3002 l++;
3004 if (!found)
3005 item->value = NULL;
3008 else
3010 inter.release ();
3011 return vec<ipa_agg_jf_item_t>();
3013 return inter;
3016 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3017 from all of them. */
3019 static struct ipa_agg_replacement_value *
3020 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3021 vec<cgraph_edge_p> callers)
3023 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3024 struct ipa_agg_replacement_value *res = NULL;
3025 struct cgraph_edge *cs;
3026 int i, j, count = ipa_get_param_count (dest_info);
3028 FOR_EACH_VEC_ELT (callers, j, cs)
3030 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3031 if (c < count)
3032 count = c;
3035 for (i = 0; i < count ; i++)
3037 struct cgraph_edge *cs;
3038 vec<ipa_agg_jf_item_t> inter = vNULL;
3039 struct ipa_agg_jf_item *item;
3040 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3041 int j;
3043 /* Among other things, the following check should deal with all by_ref
3044 mismatches. */
3045 if (plats->aggs_bottom)
3046 continue;
3048 FOR_EACH_VEC_ELT (callers, j, cs)
3050 inter = intersect_aggregates_with_edge (cs, i, inter);
3052 if (!inter.exists ())
3053 goto next_param;
3056 FOR_EACH_VEC_ELT (inter, j, item)
3058 struct ipa_agg_replacement_value *v;
3060 if (!item->value)
3061 continue;
3063 v = ggc_alloc_ipa_agg_replacement_value ();
3064 v->index = i;
3065 v->offset = item->offset;
3066 v->value = item->value;
3067 v->by_ref = plats->aggs_by_ref;
3068 v->next = res;
3069 res = v;
3072 next_param:
3073 if (inter.exists ())
3074 inter.release ();
3076 return res;
3079 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3081 static struct ipa_agg_replacement_value *
3082 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function_t> known_aggs)
3084 struct ipa_agg_replacement_value *res = NULL;
3085 struct ipa_agg_jump_function *aggjf;
3086 struct ipa_agg_jf_item *item;
3087 int i, j;
3089 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3090 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3092 struct ipa_agg_replacement_value *v;
3093 v = ggc_alloc_ipa_agg_replacement_value ();
3094 v->index = i;
3095 v->offset = item->offset;
3096 v->value = item->value;
3097 v->by_ref = aggjf->by_ref;
3098 v->next = res;
3099 res = v;
3101 return res;
3104 /* Determine whether CS also brings all scalar values that the NODE is
3105 specialized for. */
3107 static bool
3108 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3109 struct cgraph_node *node)
3111 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3112 int count = ipa_get_param_count (dest_info);
3113 struct ipa_node_params *caller_info;
3114 struct ipa_edge_args *args;
3115 int i;
3117 caller_info = IPA_NODE_REF (cs->caller);
3118 args = IPA_EDGE_REF (cs);
3119 for (i = 0; i < count; i++)
3121 struct ipa_jump_func *jump_func;
3122 tree val, t;
3124 val = dest_info->known_vals[i];
3125 if (!val)
3126 continue;
3128 if (i >= ipa_get_cs_argument_count (args))
3129 return false;
3130 jump_func = ipa_get_ith_jump_func (args, i);
3131 t = ipa_value_from_jfunc (caller_info, jump_func);
3132 if (!t || !values_equal_for_ipcp_p (val, t))
3133 return false;
3135 return true;
3138 /* Determine whether CS also brings all aggregate values that NODE is
3139 specialized for. */
3140 static bool
3141 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3142 struct cgraph_node *node)
3144 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3145 struct ipa_agg_replacement_value *aggval;
3146 int i, ec, count;
3148 aggval = ipa_get_agg_replacements_for_node (node);
3149 if (!aggval)
3150 return true;
3152 count = ipa_get_param_count (IPA_NODE_REF (node));
3153 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3154 if (ec < count)
3155 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3156 if (aggval->index >= ec)
3157 return false;
3159 if (orig_caller_info->ipcp_orig_node)
3160 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3162 for (i = 0; i < count; i++)
3164 static vec<ipa_agg_jf_item_t> values = vec<ipa_agg_jf_item_t>();
3165 struct ipcp_param_lattices *plats;
3166 bool interesting = false;
3167 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3168 if (aggval->index == i)
3170 interesting = true;
3171 break;
3173 if (!interesting)
3174 continue;
3176 plats = ipa_get_parm_lattices (orig_caller_info, aggval->index);
3177 if (plats->aggs_bottom)
3178 return false;
3180 values = intersect_aggregates_with_edge (cs, i, values);
3181 if (!values.exists ())
3182 return false;
3184 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3185 if (aggval->index == i)
3187 struct ipa_agg_jf_item *item;
3188 int j;
3189 bool found = false;
3190 FOR_EACH_VEC_ELT (values, j, item)
3191 if (item->value
3192 && item->offset == av->offset
3193 && values_equal_for_ipcp_p (item->value, av->value))
3195 found = true;
3196 break;
3198 if (!found)
3200 values.release ();
3201 return false;
3205 return true;
3208 /* Given an original NODE and a VAL for which we have already created a
3209 specialized clone, look whether there are incoming edges that still lead
3210 into the old node but now also bring the requested value and also conform to
3211 all other criteria such that they can be redirected the the special node.
3212 This function can therefore redirect the final edge in a SCC. */
3214 static void
3215 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
3217 struct ipcp_value_source *src;
3218 gcov_type redirected_sum = 0;
3220 for (src = val->sources; src; src = src->next)
3222 struct cgraph_edge *cs = src->cs;
3223 while (cs)
3225 enum availability availability;
3226 struct cgraph_node *dst = cgraph_function_node (cs->callee,
3227 &availability);
3228 if ((dst == node || IPA_NODE_REF (dst)->is_all_contexts_clone)
3229 && availability > AVAIL_OVERWRITABLE
3230 && cgraph_edge_brings_value_p (cs, src))
3232 if (cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3233 && cgraph_edge_brings_all_agg_vals_for_node (cs,
3234 val->spec_node))
3236 if (dump_file)
3237 fprintf (dump_file, " - adding an extra caller %s/%i"
3238 " of %s/%i\n",
3239 xstrdup (cs->caller->name ()),
3240 cs->caller->order,
3241 xstrdup (val->spec_node->name ()),
3242 val->spec_node->order);
3244 cgraph_redirect_edge_callee (cs, val->spec_node);
3245 redirected_sum += cs->count;
3248 cs = get_next_cgraph_edge_clone (cs);
3252 if (redirected_sum)
3253 update_specialized_profile (val->spec_node, node, redirected_sum);
3257 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
3259 static void
3260 move_binfos_to_values (vec<tree> known_vals,
3261 vec<tree> known_binfos)
3263 tree t;
3264 int i;
3266 for (i = 0; known_binfos.iterate (i, &t); i++)
3267 if (t)
3268 known_vals[i] = t;
3271 /* Return true if there is a replacement equivalent to VALUE, INDEX and OFFSET
3272 among those in the AGGVALS list. */
3274 DEBUG_FUNCTION bool
3275 ipcp_val_in_agg_replacements_p (struct ipa_agg_replacement_value *aggvals,
3276 int index, HOST_WIDE_INT offset, tree value)
3278 while (aggvals)
3280 if (aggvals->index == index
3281 && aggvals->offset == offset
3282 && values_equal_for_ipcp_p (aggvals->value, value))
3283 return true;
3284 aggvals = aggvals->next;
3286 return false;
3289 /* Decide wheter to create a special version of NODE for value VAL of parameter
3290 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
3291 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
3292 KNOWN_BINFOS and KNOWN_AGGS describe the other already known values. */
3294 static bool
3295 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
3296 struct ipcp_value *val, vec<tree> known_csts,
3297 vec<tree> known_binfos)
3299 struct ipa_agg_replacement_value *aggvals;
3300 int freq_sum, caller_count;
3301 gcov_type count_sum;
3302 vec<cgraph_edge_p> callers;
3303 vec<tree> kv;
3305 if (val->spec_node)
3307 perhaps_add_new_callers (node, val);
3308 return false;
3310 else if (val->local_size_cost + overall_size > max_new_size)
3312 if (dump_file && (dump_flags & TDF_DETAILS))
3313 fprintf (dump_file, " Ignoring candidate value because "
3314 "max_new_size would be reached with %li.\n",
3315 val->local_size_cost + overall_size);
3316 return false;
3318 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
3319 &caller_count))
3320 return false;
3322 if (dump_file && (dump_flags & TDF_DETAILS))
3324 fprintf (dump_file, " - considering value ");
3325 print_ipcp_constant_value (dump_file, val->value);
3326 fprintf (dump_file, " for ");
3327 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
3328 if (offset != -1)
3329 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
3330 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
3333 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
3334 freq_sum, count_sum,
3335 val->local_size_cost)
3336 && !good_cloning_opportunity_p (node,
3337 val->local_time_benefit
3338 + val->prop_time_benefit,
3339 freq_sum, count_sum,
3340 val->local_size_cost
3341 + val->prop_size_cost))
3342 return false;
3344 if (dump_file)
3345 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
3346 node->name (), node->order);
3348 callers = gather_edges_for_value (val, caller_count);
3349 kv = known_csts.copy ();
3350 move_binfos_to_values (kv, known_binfos);
3351 if (offset == -1)
3352 kv[index] = val->value;
3353 find_more_scalar_values_for_callers_subset (node, kv, callers);
3354 aggvals = find_aggregate_values_for_callers_subset (node, callers);
3355 gcc_checking_assert (offset == -1
3356 || ipcp_val_in_agg_replacements_p (aggvals, index,
3357 offset, val->value));
3358 val->spec_node = create_specialized_node (node, kv, aggvals, callers);
3359 overall_size += val->local_size_cost;
3361 /* TODO: If for some lattice there is only one other known value
3362 left, make a special node for it too. */
3364 return true;
3367 /* Decide whether and what specialized clones of NODE should be created. */
3369 static bool
3370 decide_whether_version_node (struct cgraph_node *node)
3372 struct ipa_node_params *info = IPA_NODE_REF (node);
3373 int i, count = ipa_get_param_count (info);
3374 vec<tree> known_csts, known_binfos;
3375 vec<ipa_agg_jump_function_t> known_aggs = vNULL;
3376 bool ret = false;
3378 if (count == 0)
3379 return false;
3381 if (dump_file && (dump_flags & TDF_DETAILS))
3382 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
3383 node->name (), node->order);
3385 gather_context_independent_values (info, &known_csts, &known_binfos,
3386 info->do_clone_for_all_contexts ? &known_aggs
3387 : NULL, NULL);
3389 for (i = 0; i < count ;i++)
3391 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3392 struct ipcp_lattice *lat = &plats->itself;
3393 struct ipcp_value *val;
3395 if (!lat->bottom
3396 && !known_csts[i]
3397 && !known_binfos[i])
3398 for (val = lat->values; val; val = val->next)
3399 ret |= decide_about_value (node, i, -1, val, known_csts,
3400 known_binfos);
3402 if (!plats->aggs_bottom)
3404 struct ipcp_agg_lattice *aglat;
3405 struct ipcp_value *val;
3406 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3407 if (!aglat->bottom && aglat->values
3408 /* If the following is false, the one value is in
3409 known_aggs. */
3410 && (plats->aggs_contain_variable
3411 || !ipa_lat_is_single_const (aglat)))
3412 for (val = aglat->values; val; val = val->next)
3413 ret |= decide_about_value (node, i, aglat->offset, val,
3414 known_csts, known_binfos);
3416 info = IPA_NODE_REF (node);
3419 if (info->do_clone_for_all_contexts)
3421 struct cgraph_node *clone;
3422 vec<cgraph_edge_p> callers;
3424 if (dump_file)
3425 fprintf (dump_file, " - Creating a specialized node of %s/%i "
3426 "for all known contexts.\n", node->name (),
3427 node->order);
3429 callers = collect_callers_of_node (node);
3430 move_binfos_to_values (known_csts, known_binfos);
3431 clone = create_specialized_node (node, known_csts,
3432 known_aggs_to_agg_replacement_list (known_aggs),
3433 callers);
3434 info = IPA_NODE_REF (node);
3435 info->do_clone_for_all_contexts = false;
3436 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
3437 for (i = 0; i < count ; i++)
3438 vec_free (known_aggs[i].items);
3439 known_aggs.release ();
3440 ret = true;
3442 else
3443 known_csts.release ();
3445 known_binfos.release ();
3446 return ret;
3449 /* Transitively mark all callees of NODE within the same SCC as not dead. */
3451 static void
3452 spread_undeadness (struct cgraph_node *node)
3454 struct cgraph_edge *cs;
3456 for (cs = node->callees; cs; cs = cs->next_callee)
3457 if (ipa_edge_within_scc (cs))
3459 struct cgraph_node *callee;
3460 struct ipa_node_params *info;
3462 callee = cgraph_function_node (cs->callee, NULL);
3463 info = IPA_NODE_REF (callee);
3465 if (info->node_dead)
3467 info->node_dead = 0;
3468 spread_undeadness (callee);
3473 /* Return true if NODE has a caller from outside of its SCC that is not
3474 dead. Worker callback for cgraph_for_node_and_aliases. */
3476 static bool
3477 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
3478 void *data ATTRIBUTE_UNUSED)
3480 struct cgraph_edge *cs;
3482 for (cs = node->callers; cs; cs = cs->next_caller)
3483 if (cs->caller->thunk.thunk_p
3484 && cgraph_for_node_and_aliases (cs->caller,
3485 has_undead_caller_from_outside_scc_p,
3486 NULL, true))
3487 return true;
3488 else if (!ipa_edge_within_scc (cs)
3489 && !IPA_NODE_REF (cs->caller)->node_dead)
3490 return true;
3491 return false;
3495 /* Identify nodes within the same SCC as NODE which are no longer needed
3496 because of new clones and will be removed as unreachable. */
3498 static void
3499 identify_dead_nodes (struct cgraph_node *node)
3501 struct cgraph_node *v;
3502 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3503 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
3504 && !cgraph_for_node_and_aliases (v,
3505 has_undead_caller_from_outside_scc_p,
3506 NULL, true))
3507 IPA_NODE_REF (v)->node_dead = 1;
3509 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3510 if (!IPA_NODE_REF (v)->node_dead)
3511 spread_undeadness (v);
3513 if (dump_file && (dump_flags & TDF_DETAILS))
3515 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3516 if (IPA_NODE_REF (v)->node_dead)
3517 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
3518 v->name (), v->order);
3522 /* The decision stage. Iterate over the topological order of call graph nodes
3523 TOPO and make specialized clones if deemed beneficial. */
3525 static void
3526 ipcp_decision_stage (struct topo_info *topo)
3528 int i;
3530 if (dump_file)
3531 fprintf (dump_file, "\nIPA decision stage:\n\n");
3533 for (i = topo->nnodes - 1; i >= 0; i--)
3535 struct cgraph_node *node = topo->order[i];
3536 bool change = false, iterate = true;
3538 while (iterate)
3540 struct cgraph_node *v;
3541 iterate = false;
3542 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3543 if (cgraph_function_with_gimple_body_p (v)
3544 && ipcp_versionable_function_p (v))
3545 iterate |= decide_whether_version_node (v);
3547 change |= iterate;
3549 if (change)
3550 identify_dead_nodes (node);
3554 /* The IPCP driver. */
3556 static unsigned int
3557 ipcp_driver (void)
3559 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
3560 struct topo_info topo;
3562 ipa_check_create_node_params ();
3563 ipa_check_create_edge_args ();
3564 grow_next_edge_clone_vector ();
3565 edge_duplication_hook_holder =
3566 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
3567 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
3568 sizeof (struct ipcp_value), 32);
3569 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
3570 sizeof (struct ipcp_value_source), 64);
3571 ipcp_agg_lattice_pool = create_alloc_pool ("IPA_CP aggregate lattices",
3572 sizeof (struct ipcp_agg_lattice),
3573 32);
3574 if (dump_file)
3576 fprintf (dump_file, "\nIPA structures before propagation:\n");
3577 if (dump_flags & TDF_DETAILS)
3578 ipa_print_all_params (dump_file);
3579 ipa_print_all_jump_functions (dump_file);
3582 /* Topological sort. */
3583 build_toporder_info (&topo);
3584 /* Do the interprocedural propagation. */
3585 ipcp_propagate_stage (&topo);
3586 /* Decide what constant propagation and cloning should be performed. */
3587 ipcp_decision_stage (&topo);
3589 /* Free all IPCP structures. */
3590 free_toporder_info (&topo);
3591 next_edge_clone.release ();
3592 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3593 ipa_free_all_structures_after_ipa_cp ();
3594 if (dump_file)
3595 fprintf (dump_file, "\nIPA constant propagation end\n");
3596 return 0;
3599 /* Initialization and computation of IPCP data structures. This is the initial
3600 intraprocedural analysis of functions, which gathers information to be
3601 propagated later on. */
3603 static void
3604 ipcp_generate_summary (void)
3606 struct cgraph_node *node;
3608 if (dump_file)
3609 fprintf (dump_file, "\nIPA constant propagation start:\n");
3610 ipa_register_cgraph_hooks ();
3612 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
3614 node->local.versionable
3615 = tree_versionable_function_p (node->decl);
3616 ipa_analyze_node (node);
3620 /* Write ipcp summary for nodes in SET. */
3622 static void
3623 ipcp_write_summary (void)
3625 ipa_prop_write_jump_functions ();
3628 /* Read ipcp summary. */
3630 static void
3631 ipcp_read_summary (void)
3633 ipa_prop_read_jump_functions ();
3636 /* Gate for IPCP optimization. */
3638 static bool
3639 cgraph_gate_cp (void)
3641 /* FIXME: We should remove the optimize check after we ensure we never run
3642 IPA passes when not optimizing. */
3643 return flag_ipa_cp && optimize;
3646 namespace {
3648 const pass_data pass_data_ipa_cp =
3650 IPA_PASS, /* type */
3651 "cp", /* name */
3652 OPTGROUP_NONE, /* optinfo_flags */
3653 true, /* has_gate */
3654 true, /* has_execute */
3655 TV_IPA_CONSTANT_PROP, /* tv_id */
3656 0, /* properties_required */
3657 0, /* properties_provided */
3658 0, /* properties_destroyed */
3659 0, /* todo_flags_start */
3660 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
3663 class pass_ipa_cp : public ipa_opt_pass_d
3665 public:
3666 pass_ipa_cp (gcc::context *ctxt)
3667 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
3668 ipcp_generate_summary, /* generate_summary */
3669 ipcp_write_summary, /* write_summary */
3670 ipcp_read_summary, /* read_summary */
3671 ipa_prop_write_all_agg_replacement, /*
3672 write_optimization_summary */
3673 ipa_prop_read_all_agg_replacement, /*
3674 read_optimization_summary */
3675 NULL, /* stmt_fixup */
3676 0, /* function_transform_todo_flags_start */
3677 ipcp_transform_function, /* function_transform */
3678 NULL) /* variable_transform */
3681 /* opt_pass methods: */
3682 bool gate () { return cgraph_gate_cp (); }
3683 unsigned int execute () { return ipcp_driver (); }
3685 }; // class pass_ipa_cp
3687 } // anon namespace
3689 ipa_opt_pass_d *
3690 make_pass_ipa_cp (gcc::context *ctxt)
3692 return new pass_ipa_cp (ctxt);