2014-02-20 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-cp.c
blobf7cf7164fe11e0c18c1d3f082eb1f86c68470671
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2014 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";
433 else if (!opt_for_fn (node->decl, optimize)
434 || !opt_for_fn (node->decl, flag_ipa_cp))
435 reason = "non-optimized function";
436 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
438 /* Ideally we should clone the SIMD clones themselves and create
439 vector copies of them, so IPA-cp and SIMD clones can happily
440 coexist, but that may not be worth the effort. */
441 reason = "function has SIMD clones";
443 /* Don't clone decls local to a comdat group; it breaks and for C++
444 decloned constructors, inlining is always better anyway. */
445 else if (symtab_comdat_local_p (node))
446 reason = "comdat-local function";
448 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
449 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
450 node->name (), node->order, reason);
452 node->local.versionable = (reason == NULL);
455 /* Return true if it is at all technically possible to create clones of a
456 NODE. */
458 static bool
459 ipcp_versionable_function_p (struct cgraph_node *node)
461 return node->local.versionable;
464 /* Structure holding accumulated information about callers of a node. */
466 struct caller_statistics
468 gcov_type count_sum;
469 int n_calls, n_hot_calls, freq_sum;
472 /* Initialize fields of STAT to zeroes. */
474 static inline void
475 init_caller_stats (struct caller_statistics *stats)
477 stats->count_sum = 0;
478 stats->n_calls = 0;
479 stats->n_hot_calls = 0;
480 stats->freq_sum = 0;
483 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
484 non-thunk incoming edges to NODE. */
486 static bool
487 gather_caller_stats (struct cgraph_node *node, void *data)
489 struct caller_statistics *stats = (struct caller_statistics *) data;
490 struct cgraph_edge *cs;
492 for (cs = node->callers; cs; cs = cs->next_caller)
493 if (cs->caller->thunk.thunk_p)
494 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
495 stats, false);
496 else
498 stats->count_sum += cs->count;
499 stats->freq_sum += cs->frequency;
500 stats->n_calls++;
501 if (cgraph_maybe_hot_edge_p (cs))
502 stats->n_hot_calls ++;
504 return false;
508 /* Return true if this NODE is viable candidate for cloning. */
510 static bool
511 ipcp_cloning_candidate_p (struct cgraph_node *node)
513 struct caller_statistics stats;
515 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
517 if (!flag_ipa_cp_clone)
519 if (dump_file)
520 fprintf (dump_file, "Not considering %s for cloning; "
521 "-fipa-cp-clone disabled.\n",
522 node->name ());
523 return false;
526 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
528 if (dump_file)
529 fprintf (dump_file, "Not considering %s for cloning; "
530 "optimizing it for size.\n",
531 node->name ());
532 return false;
535 init_caller_stats (&stats);
536 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
538 if (inline_summary (node)->self_size < stats.n_calls)
540 if (dump_file)
541 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
542 node->name ());
543 return true;
546 /* When profile is available and function is hot, propagate into it even if
547 calls seems cold; constant propagation can improve function's speed
548 significantly. */
549 if (max_count)
551 if (stats.count_sum > node->count * 90 / 100)
553 if (dump_file)
554 fprintf (dump_file, "Considering %s for cloning; "
555 "usually called directly.\n",
556 node->name ());
557 return true;
560 if (!stats.n_hot_calls)
562 if (dump_file)
563 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
564 node->name ());
565 return false;
567 if (dump_file)
568 fprintf (dump_file, "Considering %s for cloning.\n",
569 node->name ());
570 return true;
573 /* Arrays representing a topological ordering of call graph nodes and a stack
574 of noes used during constant propagation. */
576 struct topo_info
578 struct cgraph_node **order;
579 struct cgraph_node **stack;
580 int nnodes, stack_top;
583 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
585 static void
586 build_toporder_info (struct topo_info *topo)
588 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
589 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
590 topo->stack_top = 0;
591 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
594 /* Free information about strongly connected components and the arrays in
595 TOPO. */
597 static void
598 free_toporder_info (struct topo_info *topo)
600 ipa_free_postorder_info ();
601 free (topo->order);
602 free (topo->stack);
605 /* Add NODE to the stack in TOPO, unless it is already there. */
607 static inline void
608 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
610 struct ipa_node_params *info = IPA_NODE_REF (node);
611 if (info->node_enqueued)
612 return;
613 info->node_enqueued = 1;
614 topo->stack[topo->stack_top++] = node;
617 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
618 is empty. */
620 static struct cgraph_node *
621 pop_node_from_stack (struct topo_info *topo)
623 if (topo->stack_top)
625 struct cgraph_node *node;
626 topo->stack_top--;
627 node = topo->stack[topo->stack_top];
628 IPA_NODE_REF (node)->node_enqueued = 0;
629 return node;
631 else
632 return NULL;
635 /* Set lattice LAT to bottom and return true if it previously was not set as
636 such. */
638 static inline bool
639 set_lattice_to_bottom (struct ipcp_lattice *lat)
641 bool ret = !lat->bottom;
642 lat->bottom = true;
643 return ret;
646 /* Mark lattice as containing an unknown value and return true if it previously
647 was not marked as such. */
649 static inline bool
650 set_lattice_contains_variable (struct ipcp_lattice *lat)
652 bool ret = !lat->contains_variable;
653 lat->contains_variable = true;
654 return ret;
657 /* Set all aggegate lattices in PLATS to bottom and return true if they were
658 not previously set as such. */
660 static inline bool
661 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
663 bool ret = !plats->aggs_bottom;
664 plats->aggs_bottom = true;
665 return ret;
668 /* Mark all aggegate lattices in PLATS as containing an unknown value and
669 return true if they were not previously marked as such. */
671 static inline bool
672 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
674 bool ret = !plats->aggs_contain_variable;
675 plats->aggs_contain_variable = true;
676 return ret;
679 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
680 return true is any of them has not been marked as such so far. */
682 static inline bool
683 set_all_contains_variable (struct ipcp_param_lattices *plats)
685 bool ret = !plats->itself.contains_variable || !plats->aggs_contain_variable;
686 plats->itself.contains_variable = true;
687 plats->aggs_contain_variable = true;
688 return ret;
691 /* Initialize ipcp_lattices. */
693 static void
694 initialize_node_lattices (struct cgraph_node *node)
696 struct ipa_node_params *info = IPA_NODE_REF (node);
697 struct cgraph_edge *ie;
698 bool disable = false, variable = false;
699 int i;
701 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
702 if (!node->local.local)
704 /* When cloning is allowed, we can assume that externally visible
705 functions are not called. We will compensate this by cloning
706 later. */
707 if (ipcp_versionable_function_p (node)
708 && ipcp_cloning_candidate_p (node))
709 variable = true;
710 else
711 disable = true;
714 if (disable || variable)
716 for (i = 0; i < ipa_get_param_count (info) ; i++)
718 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
719 if (disable)
721 set_lattice_to_bottom (&plats->itself);
722 set_agg_lats_to_bottom (plats);
724 else
725 set_all_contains_variable (plats);
727 if (dump_file && (dump_flags & TDF_DETAILS)
728 && !node->alias && !node->thunk.thunk_p)
729 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
730 node->name (), node->order,
731 disable ? "BOTTOM" : "VARIABLE");
734 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
735 if (ie->indirect_info->polymorphic
736 && ie->indirect_info->param_index >= 0)
738 gcc_checking_assert (ie->indirect_info->param_index >= 0);
739 ipa_get_parm_lattices (info,
740 ie->indirect_info->param_index)->virt_call = 1;
744 /* Return the result of a (possibly arithmetic) pass through jump function
745 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
746 determined or be considered an interprocedural invariant. */
748 static tree
749 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
751 tree restype, res;
753 if (TREE_CODE (input) == TREE_BINFO)
755 if (ipa_get_jf_pass_through_type_preserved (jfunc))
757 gcc_checking_assert (ipa_get_jf_pass_through_operation (jfunc)
758 == NOP_EXPR);
759 return input;
761 return NULL_TREE;
764 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
765 return input;
767 gcc_checking_assert (is_gimple_ip_invariant (input));
768 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
769 == tcc_comparison)
770 restype = boolean_type_node;
771 else
772 restype = TREE_TYPE (input);
773 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
774 input, ipa_get_jf_pass_through_operand (jfunc));
776 if (res && !is_gimple_ip_invariant (res))
777 return NULL_TREE;
779 return res;
782 /* Return the result of an ancestor jump function JFUNC on the constant value
783 INPUT. Return NULL_TREE if that cannot be determined. */
785 static tree
786 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
788 if (TREE_CODE (input) == TREE_BINFO)
790 if (!ipa_get_jf_ancestor_type_preserved (jfunc))
791 return NULL;
792 return get_binfo_at_offset (input,
793 ipa_get_jf_ancestor_offset (jfunc),
794 ipa_get_jf_ancestor_type (jfunc));
796 else if (TREE_CODE (input) == ADDR_EXPR)
798 tree t = TREE_OPERAND (input, 0);
799 t = build_ref_for_offset (EXPR_LOCATION (t), t,
800 ipa_get_jf_ancestor_offset (jfunc),
801 ipa_get_jf_ancestor_type (jfunc)
802 ? ipa_get_jf_ancestor_type (jfunc)
803 : ptr_type_node, NULL, false);
804 return build_fold_addr_expr (t);
806 else
807 return NULL_TREE;
810 /* Determine whether JFUNC evaluates to a known value (that is either a
811 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
812 describes the caller node so that pass-through jump functions can be
813 evaluated. */
815 tree
816 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
818 if (jfunc->type == IPA_JF_CONST)
819 return ipa_get_jf_constant (jfunc);
820 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
821 return ipa_binfo_from_known_type_jfunc (jfunc);
822 else if (jfunc->type == IPA_JF_PASS_THROUGH
823 || jfunc->type == IPA_JF_ANCESTOR)
825 tree input;
826 int idx;
828 if (jfunc->type == IPA_JF_PASS_THROUGH)
829 idx = ipa_get_jf_pass_through_formal_id (jfunc);
830 else
831 idx = ipa_get_jf_ancestor_formal_id (jfunc);
833 if (info->ipcp_orig_node)
834 input = info->known_vals[idx];
835 else
837 struct ipcp_lattice *lat;
839 if (!info->lattices)
841 gcc_checking_assert (!flag_ipa_cp);
842 return NULL_TREE;
844 lat = ipa_get_scalar_lat (info, idx);
845 if (!ipa_lat_is_single_const (lat))
846 return NULL_TREE;
847 input = lat->values->value;
850 if (!input)
851 return NULL_TREE;
853 if (jfunc->type == IPA_JF_PASS_THROUGH)
854 return ipa_get_jf_pass_through_result (jfunc, input);
855 else
856 return ipa_get_jf_ancestor_result (jfunc, input);
858 else
859 return NULL_TREE;
863 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
864 bottom, not containing a variable component and without any known value at
865 the same time. */
867 DEBUG_FUNCTION void
868 ipcp_verify_propagated_values (void)
870 struct cgraph_node *node;
872 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
874 struct ipa_node_params *info = IPA_NODE_REF (node);
875 int i, count = ipa_get_param_count (info);
877 for (i = 0; i < count; i++)
879 struct ipcp_lattice *lat = ipa_get_scalar_lat (info, i);
881 if (!lat->bottom
882 && !lat->contains_variable
883 && lat->values_count == 0)
885 if (dump_file)
887 fprintf (dump_file, "\nIPA lattices after constant "
888 "propagation:\n");
889 print_all_lattices (dump_file, true, false);
892 gcc_unreachable ();
898 /* Return true iff X and Y should be considered equal values by IPA-CP. */
900 static bool
901 values_equal_for_ipcp_p (tree x, tree y)
903 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
905 if (x == y)
906 return true;
908 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
909 return false;
911 if (TREE_CODE (x) == ADDR_EXPR
912 && TREE_CODE (y) == ADDR_EXPR
913 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
914 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
915 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
916 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
917 else
918 return operand_equal_p (x, y, 0);
921 /* Add a new value source to VAL, marking that a value comes from edge CS and
922 (if the underlying jump function is a pass-through or an ancestor one) from
923 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. OFFSET
924 is negative if the source was the scalar value of the parameter itself or
925 the offset within an aggregate. */
927 static void
928 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
929 struct ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset)
931 struct ipcp_value_source *src;
933 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
934 src->offset = offset;
935 src->cs = cs;
936 src->val = src_val;
937 src->index = src_idx;
939 src->next = val->sources;
940 val->sources = src;
943 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
944 it. CS, SRC_VAL SRC_INDEX and OFFSET are meant for add_value_source and
945 have the same meaning. */
947 static bool
948 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
949 struct cgraph_edge *cs, struct ipcp_value *src_val,
950 int src_idx, HOST_WIDE_INT offset)
952 struct ipcp_value *val;
954 if (lat->bottom)
955 return false;
957 for (val = lat->values; val; val = val->next)
958 if (values_equal_for_ipcp_p (val->value, newval))
960 if (ipa_edge_within_scc (cs))
962 struct ipcp_value_source *s;
963 for (s = val->sources; s ; s = s->next)
964 if (s->cs == cs)
965 break;
966 if (s)
967 return false;
970 add_value_source (val, cs, src_val, src_idx, offset);
971 return false;
974 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
976 /* We can only free sources, not the values themselves, because sources
977 of other values in this this SCC might point to them. */
978 for (val = lat->values; val; val = val->next)
980 while (val->sources)
982 struct ipcp_value_source *src = val->sources;
983 val->sources = src->next;
984 pool_free (ipcp_sources_pool, src);
988 lat->values = NULL;
989 return set_lattice_to_bottom (lat);
992 lat->values_count++;
993 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
994 memset (val, 0, sizeof (*val));
996 add_value_source (val, cs, src_val, src_idx, offset);
997 val->value = newval;
998 val->next = lat->values;
999 lat->values = val;
1000 return true;
1003 /* Like above but passes a special value of offset to distinguish that the
1004 origin is the scalar value of the parameter rather than a part of an
1005 aggregate. */
1007 static inline bool
1008 add_scalar_value_to_lattice (struct ipcp_lattice *lat, tree newval,
1009 struct cgraph_edge *cs,
1010 struct ipcp_value *src_val, int src_idx)
1012 return add_value_to_lattice (lat, newval, cs, src_val, src_idx, -1);
1015 /* Propagate values through a pass-through jump function JFUNC associated with
1016 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1017 is the index of the source parameter. */
1019 static bool
1020 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
1021 struct ipa_jump_func *jfunc,
1022 struct ipcp_lattice *src_lat,
1023 struct ipcp_lattice *dest_lat,
1024 int src_idx)
1026 struct ipcp_value *src_val;
1027 bool ret = false;
1029 /* Do not create new values when propagating within an SCC because if there
1030 are arithmetic functions with circular dependencies, there is infinite
1031 number of them and we would just make lattices bottom. */
1032 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1033 && ipa_edge_within_scc (cs))
1034 ret = set_lattice_contains_variable (dest_lat);
1035 else
1036 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1038 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1040 if (cstval)
1041 ret |= add_scalar_value_to_lattice (dest_lat, cstval, cs, src_val,
1042 src_idx);
1043 else
1044 ret |= set_lattice_contains_variable (dest_lat);
1047 return ret;
1050 /* Propagate values through an ancestor jump function JFUNC associated with
1051 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1052 is the index of the source parameter. */
1054 static bool
1055 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1056 struct ipa_jump_func *jfunc,
1057 struct ipcp_lattice *src_lat,
1058 struct ipcp_lattice *dest_lat,
1059 int src_idx)
1061 struct ipcp_value *src_val;
1062 bool ret = false;
1064 if (ipa_edge_within_scc (cs))
1065 return set_lattice_contains_variable (dest_lat);
1067 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1069 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1071 if (t)
1072 ret |= add_scalar_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
1073 else
1074 ret |= set_lattice_contains_variable (dest_lat);
1077 return ret;
1080 /* Propagate scalar values across jump function JFUNC that is associated with
1081 edge CS and put the values into DEST_LAT. */
1083 static bool
1084 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1085 struct ipa_jump_func *jfunc,
1086 struct ipcp_lattice *dest_lat)
1088 if (dest_lat->bottom)
1089 return false;
1091 if (jfunc->type == IPA_JF_CONST
1092 || jfunc->type == IPA_JF_KNOWN_TYPE)
1094 tree val;
1096 if (jfunc->type == IPA_JF_KNOWN_TYPE)
1098 val = ipa_binfo_from_known_type_jfunc (jfunc);
1099 if (!val)
1100 return set_lattice_contains_variable (dest_lat);
1102 else
1103 val = ipa_get_jf_constant (jfunc);
1104 return add_scalar_value_to_lattice (dest_lat, val, cs, NULL, 0);
1106 else if (jfunc->type == IPA_JF_PASS_THROUGH
1107 || jfunc->type == IPA_JF_ANCESTOR)
1109 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1110 struct ipcp_lattice *src_lat;
1111 int src_idx;
1112 bool ret;
1114 if (jfunc->type == IPA_JF_PASS_THROUGH)
1115 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1116 else
1117 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1119 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1120 if (src_lat->bottom)
1121 return set_lattice_contains_variable (dest_lat);
1123 /* If we would need to clone the caller and cannot, do not propagate. */
1124 if (!ipcp_versionable_function_p (cs->caller)
1125 && (src_lat->contains_variable
1126 || (src_lat->values_count > 1)))
1127 return set_lattice_contains_variable (dest_lat);
1129 if (jfunc->type == IPA_JF_PASS_THROUGH)
1130 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1131 dest_lat, src_idx);
1132 else
1133 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1134 src_idx);
1136 if (src_lat->contains_variable)
1137 ret |= set_lattice_contains_variable (dest_lat);
1139 return ret;
1142 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1143 use it for indirect inlining), we should propagate them too. */
1144 return set_lattice_contains_variable (dest_lat);
1147 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1148 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1149 other cases, return false). If there are no aggregate items, set
1150 aggs_by_ref to NEW_AGGS_BY_REF. */
1152 static bool
1153 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1154 bool new_aggs_by_ref)
1156 if (dest_plats->aggs)
1158 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1160 set_agg_lats_to_bottom (dest_plats);
1161 return true;
1164 else
1165 dest_plats->aggs_by_ref = new_aggs_by_ref;
1166 return false;
1169 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1170 already existing lattice for the given OFFSET and SIZE, marking all skipped
1171 lattices as containing variable and checking for overlaps. If there is no
1172 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1173 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1174 unless there are too many already. If there are two many, return false. If
1175 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1176 skipped lattices were newly marked as containing variable, set *CHANGE to
1177 true. */
1179 static bool
1180 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1181 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1182 struct ipcp_agg_lattice ***aglat,
1183 bool pre_existing, bool *change)
1185 gcc_checking_assert (offset >= 0);
1187 while (**aglat && (**aglat)->offset < offset)
1189 if ((**aglat)->offset + (**aglat)->size > offset)
1191 set_agg_lats_to_bottom (dest_plats);
1192 return false;
1194 *change |= set_lattice_contains_variable (**aglat);
1195 *aglat = &(**aglat)->next;
1198 if (**aglat && (**aglat)->offset == offset)
1200 if ((**aglat)->size != val_size
1201 || ((**aglat)->next
1202 && (**aglat)->next->offset < offset + val_size))
1204 set_agg_lats_to_bottom (dest_plats);
1205 return false;
1207 gcc_checking_assert (!(**aglat)->next
1208 || (**aglat)->next->offset >= offset + val_size);
1209 return true;
1211 else
1213 struct ipcp_agg_lattice *new_al;
1215 if (**aglat && (**aglat)->offset < offset + val_size)
1217 set_agg_lats_to_bottom (dest_plats);
1218 return false;
1220 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1221 return false;
1222 dest_plats->aggs_count++;
1223 new_al = (struct ipcp_agg_lattice *) pool_alloc (ipcp_agg_lattice_pool);
1224 memset (new_al, 0, sizeof (*new_al));
1226 new_al->offset = offset;
1227 new_al->size = val_size;
1228 new_al->contains_variable = pre_existing;
1230 new_al->next = **aglat;
1231 **aglat = new_al;
1232 return true;
1236 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1237 containing an unknown value. */
1239 static bool
1240 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1242 bool ret = false;
1243 while (aglat)
1245 ret |= set_lattice_contains_variable (aglat);
1246 aglat = aglat->next;
1248 return ret;
1251 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1252 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1253 parameter used for lattice value sources. Return true if DEST_PLATS changed
1254 in any way. */
1256 static bool
1257 merge_aggregate_lattices (struct cgraph_edge *cs,
1258 struct ipcp_param_lattices *dest_plats,
1259 struct ipcp_param_lattices *src_plats,
1260 int src_idx, HOST_WIDE_INT offset_delta)
1262 bool pre_existing = dest_plats->aggs != NULL;
1263 struct ipcp_agg_lattice **dst_aglat;
1264 bool ret = false;
1266 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1267 return true;
1268 if (src_plats->aggs_bottom)
1269 return set_agg_lats_contain_variable (dest_plats);
1270 if (src_plats->aggs_contain_variable)
1271 ret |= set_agg_lats_contain_variable (dest_plats);
1272 dst_aglat = &dest_plats->aggs;
1274 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1275 src_aglat;
1276 src_aglat = src_aglat->next)
1278 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1280 if (new_offset < 0)
1281 continue;
1282 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1283 &dst_aglat, pre_existing, &ret))
1285 struct ipcp_agg_lattice *new_al = *dst_aglat;
1287 dst_aglat = &(*dst_aglat)->next;
1288 if (src_aglat->bottom)
1290 ret |= set_lattice_contains_variable (new_al);
1291 continue;
1293 if (src_aglat->contains_variable)
1294 ret |= set_lattice_contains_variable (new_al);
1295 for (struct ipcp_value *val = src_aglat->values;
1296 val;
1297 val = val->next)
1298 ret |= add_value_to_lattice (new_al, val->value, cs, val, src_idx,
1299 src_aglat->offset);
1301 else if (dest_plats->aggs_bottom)
1302 return true;
1304 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1305 return ret;
1308 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1309 pass-through JFUNC and if so, whether it has conform and conforms to the
1310 rules about propagating values passed by reference. */
1312 static bool
1313 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1314 struct ipa_jump_func *jfunc)
1316 return src_plats->aggs
1317 && (!src_plats->aggs_by_ref
1318 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1321 /* Propagate scalar values across jump function JFUNC that is associated with
1322 edge CS and put the values into DEST_LAT. */
1324 static bool
1325 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1326 struct ipa_jump_func *jfunc,
1327 struct ipcp_param_lattices *dest_plats)
1329 bool ret = false;
1331 if (dest_plats->aggs_bottom)
1332 return false;
1334 if (jfunc->type == IPA_JF_PASS_THROUGH
1335 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1337 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1338 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1339 struct ipcp_param_lattices *src_plats;
1341 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1342 if (agg_pass_through_permissible_p (src_plats, jfunc))
1344 /* Currently we do not produce clobber aggregate jump
1345 functions, replace with merging when we do. */
1346 gcc_assert (!jfunc->agg.items);
1347 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1348 src_idx, 0);
1350 else
1351 ret |= set_agg_lats_contain_variable (dest_plats);
1353 else if (jfunc->type == IPA_JF_ANCESTOR
1354 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1356 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1357 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1358 struct ipcp_param_lattices *src_plats;
1360 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1361 if (src_plats->aggs && src_plats->aggs_by_ref)
1363 /* Currently we do not produce clobber aggregate jump
1364 functions, replace with merging when we do. */
1365 gcc_assert (!jfunc->agg.items);
1366 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1367 ipa_get_jf_ancestor_offset (jfunc));
1369 else if (!src_plats->aggs_by_ref)
1370 ret |= set_agg_lats_to_bottom (dest_plats);
1371 else
1372 ret |= set_agg_lats_contain_variable (dest_plats);
1374 else if (jfunc->agg.items)
1376 bool pre_existing = dest_plats->aggs != NULL;
1377 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1378 struct ipa_agg_jf_item *item;
1379 int i;
1381 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1382 return true;
1384 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1386 HOST_WIDE_INT val_size;
1388 if (item->offset < 0)
1389 continue;
1390 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1391 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1393 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1394 &aglat, pre_existing, &ret))
1396 ret |= add_value_to_lattice (*aglat, item->value, cs, NULL, 0, 0);
1397 aglat = &(*aglat)->next;
1399 else if (dest_plats->aggs_bottom)
1400 return true;
1403 ret |= set_chain_of_aglats_contains_variable (*aglat);
1405 else
1406 ret |= set_agg_lats_contain_variable (dest_plats);
1408 return ret;
1411 /* Propagate constants from the caller to the callee of CS. INFO describes the
1412 caller. */
1414 static bool
1415 propagate_constants_accross_call (struct cgraph_edge *cs)
1417 struct ipa_node_params *callee_info;
1418 enum availability availability;
1419 struct cgraph_node *callee, *alias_or_thunk;
1420 struct ipa_edge_args *args;
1421 bool ret = false;
1422 int i, args_count, parms_count;
1424 callee = cgraph_function_node (cs->callee, &availability);
1425 if (!callee->definition)
1426 return false;
1427 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1428 callee_info = IPA_NODE_REF (callee);
1430 args = IPA_EDGE_REF (cs);
1431 args_count = ipa_get_cs_argument_count (args);
1432 parms_count = ipa_get_param_count (callee_info);
1434 /* If this call goes through a thunk we must not propagate to the first (0th)
1435 parameter. However, we might need to uncover a thunk from below a series
1436 of aliases first. */
1437 alias_or_thunk = cs->callee;
1438 while (alias_or_thunk->alias)
1439 alias_or_thunk = cgraph_alias_target (alias_or_thunk);
1440 if (alias_or_thunk->thunk.thunk_p)
1442 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1443 0));
1444 i = 1;
1446 else
1447 i = 0;
1449 for (; (i < args_count) && (i < parms_count); i++)
1451 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1452 struct ipcp_param_lattices *dest_plats;
1454 dest_plats = ipa_get_parm_lattices (callee_info, i);
1455 if (availability == AVAIL_OVERWRITABLE)
1456 ret |= set_all_contains_variable (dest_plats);
1457 else
1459 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1460 &dest_plats->itself);
1461 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1462 dest_plats);
1465 for (; i < parms_count; i++)
1466 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1468 return ret;
1471 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1472 (which can contain both constants and binfos), KNOWN_BINFOS, KNOWN_AGGS or
1473 AGG_REPS return the destination. The latter three can be NULL. If AGG_REPS
1474 is not NULL, KNOWN_AGGS is ignored. */
1476 static tree
1477 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1478 vec<tree> known_vals,
1479 vec<tree> known_binfos,
1480 vec<ipa_agg_jump_function_p> known_aggs,
1481 struct ipa_agg_replacement_value *agg_reps)
1483 int param_index = ie->indirect_info->param_index;
1484 HOST_WIDE_INT token, anc_offset;
1485 tree otr_type;
1486 tree t;
1487 tree target = NULL;
1489 if (param_index == -1
1490 || known_vals.length () <= (unsigned int) param_index)
1491 return NULL_TREE;
1493 if (!ie->indirect_info->polymorphic)
1495 tree t;
1497 if (ie->indirect_info->agg_contents)
1499 if (agg_reps)
1501 t = NULL;
1502 while (agg_reps)
1504 if (agg_reps->index == param_index
1505 && agg_reps->offset == ie->indirect_info->offset
1506 && agg_reps->by_ref == ie->indirect_info->by_ref)
1508 t = agg_reps->value;
1509 break;
1511 agg_reps = agg_reps->next;
1514 else if (known_aggs.length () > (unsigned int) param_index)
1516 struct ipa_agg_jump_function *agg;
1517 agg = known_aggs[param_index];
1518 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1519 ie->indirect_info->by_ref);
1521 else
1522 t = NULL;
1524 else
1525 t = known_vals[param_index];
1527 if (t &&
1528 TREE_CODE (t) == ADDR_EXPR
1529 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1530 return TREE_OPERAND (t, 0);
1531 else
1532 return NULL_TREE;
1535 if (!flag_devirtualize)
1536 return NULL_TREE;
1538 gcc_assert (!ie->indirect_info->agg_contents);
1539 token = ie->indirect_info->otr_token;
1540 anc_offset = ie->indirect_info->offset;
1541 otr_type = ie->indirect_info->otr_type;
1543 t = NULL;
1545 /* Try to work out value of virtual table pointer value in replacemnets. */
1546 if (!t && agg_reps && !ie->indirect_info->by_ref)
1548 while (agg_reps)
1550 if (agg_reps->index == param_index
1551 && agg_reps->offset == ie->indirect_info->offset
1552 && agg_reps->by_ref)
1554 t = agg_reps->value;
1555 break;
1557 agg_reps = agg_reps->next;
1561 /* Try to work out value of virtual table pointer value in known
1562 aggregate values. */
1563 if (!t && known_aggs.length () > (unsigned int) param_index
1564 && !ie->indirect_info->by_ref)
1566 struct ipa_agg_jump_function *agg;
1567 agg = known_aggs[param_index];
1568 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1569 true);
1572 /* If we found the virtual table pointer, lookup the target. */
1573 if (t)
1575 tree vtable;
1576 unsigned HOST_WIDE_INT offset;
1577 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
1579 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
1580 vtable, offset);
1581 if (target)
1583 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
1584 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
1585 || !possible_polymorphic_call_target_p
1586 (ie, cgraph_get_node (target)))
1588 if (dump_file)
1589 fprintf (dump_file,
1590 "Type inconsident devirtualization: %s/%i->%s\n",
1591 ie->caller->name (), ie->caller->order,
1592 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target)));
1593 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
1594 cgraph_get_create_node (target);
1596 return target;
1601 /* Did we work out BINFO via type propagation? */
1602 if (!t && known_binfos.length () > (unsigned int) param_index)
1603 t = known_binfos[param_index];
1604 /* Or do we know the constant value of pointer? */
1605 if (!t)
1606 t = known_vals[param_index];
1607 if (!t)
1608 return NULL_TREE;
1610 if (TREE_CODE (t) != TREE_BINFO)
1612 ipa_polymorphic_call_context context;
1613 vec <cgraph_node *>targets;
1614 bool final;
1616 if (!get_polymorphic_call_info_from_invariant
1617 (&context, t, ie->indirect_info->otr_type,
1618 anc_offset))
1619 return NULL_TREE;
1620 targets = possible_polymorphic_call_targets
1621 (ie->indirect_info->otr_type,
1622 ie->indirect_info->otr_token,
1623 context, &final);
1624 if (!final || targets.length () > 1)
1625 return NULL_TREE;
1626 if (targets.length () == 1)
1627 target = targets[0]->decl;
1628 else
1629 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
1631 else
1633 tree binfo;
1635 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1636 if (!binfo)
1637 return NULL_TREE;
1638 target = gimple_get_virt_method_for_binfo (token, binfo);
1640 #ifdef ENABLE_CHECKING
1641 if (target)
1642 gcc_assert (possible_polymorphic_call_target_p
1643 (ie, cgraph_get_node (target)));
1644 #endif
1646 return target;
1650 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1651 (which can contain both constants and binfos), KNOWN_BINFOS (which can be
1652 NULL) or KNOWN_AGGS (which also can be NULL) return the destination. */
1654 tree
1655 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1656 vec<tree> known_vals,
1657 vec<tree> known_binfos,
1658 vec<ipa_agg_jump_function_p> known_aggs)
1660 return ipa_get_indirect_edge_target_1 (ie, known_vals, known_binfos,
1661 known_aggs, NULL);
1664 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1665 and KNOWN_BINFOS. */
1667 static int
1668 devirtualization_time_bonus (struct cgraph_node *node,
1669 vec<tree> known_csts,
1670 vec<tree> known_binfos,
1671 vec<ipa_agg_jump_function_p> known_aggs)
1673 struct cgraph_edge *ie;
1674 int res = 0;
1676 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1678 struct cgraph_node *callee;
1679 struct inline_summary *isummary;
1680 tree target;
1682 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos,
1683 known_aggs);
1684 if (!target)
1685 continue;
1687 /* Only bare minimum benefit for clearly un-inlineable targets. */
1688 res += 1;
1689 callee = cgraph_get_node (target);
1690 if (!callee || !callee->definition)
1691 continue;
1692 isummary = inline_summary (callee);
1693 if (!isummary->inlinable)
1694 continue;
1696 /* FIXME: The values below need re-considering and perhaps also
1697 integrating into the cost metrics, at lest in some very basic way. */
1698 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1699 res += 31;
1700 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1701 res += 15;
1702 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1703 || DECL_DECLARED_INLINE_P (callee->decl))
1704 res += 7;
1707 return res;
1710 /* Return time bonus incurred because of HINTS. */
1712 static int
1713 hint_time_bonus (inline_hints hints)
1715 int result = 0;
1716 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
1717 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
1718 if (hints & INLINE_HINT_array_index)
1719 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
1720 return result;
1723 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1724 and SIZE_COST and with the sum of frequencies of incoming edges to the
1725 potential new clone in FREQUENCIES. */
1727 static bool
1728 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1729 int freq_sum, gcov_type count_sum, int size_cost)
1731 if (time_benefit == 0
1732 || !flag_ipa_cp_clone
1733 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
1734 return false;
1736 gcc_assert (size_cost > 0);
1738 if (max_count)
1740 int factor = (count_sum * 1000) / max_count;
1741 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * factor)
1742 / size_cost);
1744 if (dump_file && (dump_flags & TDF_DETAILS))
1745 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1746 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1747 ") -> evaluation: " HOST_WIDEST_INT_PRINT_DEC
1748 ", threshold: %i\n",
1749 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1750 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1752 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1754 else
1756 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * freq_sum)
1757 / size_cost);
1759 if (dump_file && (dump_flags & TDF_DETAILS))
1760 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1761 "size: %i, freq_sum: %i) -> evaluation: "
1762 HOST_WIDEST_INT_PRINT_DEC ", threshold: %i\n",
1763 time_benefit, size_cost, freq_sum, evaluation,
1764 PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1766 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1770 /* Return all context independent values from aggregate lattices in PLATS in a
1771 vector. Return NULL if there are none. */
1773 static vec<ipa_agg_jf_item, va_gc> *
1774 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
1776 vec<ipa_agg_jf_item, va_gc> *res = NULL;
1778 if (plats->aggs_bottom
1779 || plats->aggs_contain_variable
1780 || plats->aggs_count == 0)
1781 return NULL;
1783 for (struct ipcp_agg_lattice *aglat = plats->aggs;
1784 aglat;
1785 aglat = aglat->next)
1786 if (ipa_lat_is_single_const (aglat))
1788 struct ipa_agg_jf_item item;
1789 item.offset = aglat->offset;
1790 item.value = aglat->values->value;
1791 vec_safe_push (res, item);
1793 return res;
1796 /* Allocate KNOWN_CSTS, KNOWN_BINFOS and, if non-NULL, KNOWN_AGGS and populate
1797 them with values of parameters that are known independent of the context.
1798 INFO describes the function. If REMOVABLE_PARAMS_COST is non-NULL, the
1799 movement cost of all removable parameters will be stored in it. */
1801 static bool
1802 gather_context_independent_values (struct ipa_node_params *info,
1803 vec<tree> *known_csts,
1804 vec<tree> *known_binfos,
1805 vec<ipa_agg_jump_function> *known_aggs,
1806 int *removable_params_cost)
1808 int i, count = ipa_get_param_count (info);
1809 bool ret = false;
1811 known_csts->create (0);
1812 known_binfos->create (0);
1813 known_csts->safe_grow_cleared (count);
1814 known_binfos->safe_grow_cleared (count);
1815 if (known_aggs)
1817 known_aggs->create (0);
1818 known_aggs->safe_grow_cleared (count);
1821 if (removable_params_cost)
1822 *removable_params_cost = 0;
1824 for (i = 0; i < count ; i++)
1826 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1827 struct ipcp_lattice *lat = &plats->itself;
1829 if (ipa_lat_is_single_const (lat))
1831 struct ipcp_value *val = lat->values;
1832 if (TREE_CODE (val->value) != TREE_BINFO)
1834 (*known_csts)[i] = val->value;
1835 if (removable_params_cost)
1836 *removable_params_cost
1837 += estimate_move_cost (TREE_TYPE (val->value));
1838 ret = true;
1840 else if (plats->virt_call)
1842 (*known_binfos)[i] = val->value;
1843 ret = true;
1845 else if (removable_params_cost
1846 && !ipa_is_param_used (info, i))
1847 *removable_params_cost += ipa_get_param_move_cost (info, i);
1849 else if (removable_params_cost
1850 && !ipa_is_param_used (info, i))
1851 *removable_params_cost
1852 += ipa_get_param_move_cost (info, i);
1854 if (known_aggs)
1856 vec<ipa_agg_jf_item, va_gc> *agg_items;
1857 struct ipa_agg_jump_function *ajf;
1859 agg_items = context_independent_aggregate_values (plats);
1860 ajf = &(*known_aggs)[i];
1861 ajf->items = agg_items;
1862 ajf->by_ref = plats->aggs_by_ref;
1863 ret |= agg_items != NULL;
1867 return ret;
1870 /* The current interface in ipa-inline-analysis requires a pointer vector.
1871 Create it.
1873 FIXME: That interface should be re-worked, this is slightly silly. Still,
1874 I'd like to discuss how to change it first and this demonstrates the
1875 issue. */
1877 static vec<ipa_agg_jump_function_p>
1878 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
1880 vec<ipa_agg_jump_function_p> ret;
1881 struct ipa_agg_jump_function *ajf;
1882 int i;
1884 ret.create (known_aggs.length ());
1885 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
1886 ret.quick_push (ajf);
1887 return ret;
1890 /* Iterate over known values of parameters of NODE and estimate the local
1891 effects in terms of time and size they have. */
1893 static void
1894 estimate_local_effects (struct cgraph_node *node)
1896 struct ipa_node_params *info = IPA_NODE_REF (node);
1897 int i, count = ipa_get_param_count (info);
1898 vec<tree> known_csts, known_binfos;
1899 vec<ipa_agg_jump_function> known_aggs;
1900 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
1901 bool always_const;
1902 int base_time = inline_summary (node)->time;
1903 int removable_params_cost;
1905 if (!count || !ipcp_versionable_function_p (node))
1906 return;
1908 if (dump_file && (dump_flags & TDF_DETAILS))
1909 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1910 node->name (), node->order, base_time);
1912 always_const = gather_context_independent_values (info, &known_csts,
1913 &known_binfos, &known_aggs,
1914 &removable_params_cost);
1915 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
1916 if (always_const)
1918 struct caller_statistics stats;
1919 inline_hints hints;
1920 int time, size;
1922 init_caller_stats (&stats);
1923 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1924 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1925 known_aggs_ptrs, &size, &time, &hints);
1926 time -= devirtualization_time_bonus (node, known_csts, known_binfos,
1927 known_aggs_ptrs);
1928 time -= hint_time_bonus (hints);
1929 time -= removable_params_cost;
1930 size -= stats.n_calls * removable_params_cost;
1932 if (dump_file)
1933 fprintf (dump_file, " - context independent values, size: %i, "
1934 "time_benefit: %i\n", size, base_time - time);
1936 if (size <= 0
1937 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1939 info->do_clone_for_all_contexts = true;
1940 base_time = time;
1942 if (dump_file)
1943 fprintf (dump_file, " Decided to specialize for all "
1944 "known contexts, code not going to grow.\n");
1946 else if (good_cloning_opportunity_p (node, base_time - time,
1947 stats.freq_sum, stats.count_sum,
1948 size))
1950 if (size + overall_size <= max_new_size)
1952 info->do_clone_for_all_contexts = true;
1953 base_time = time;
1954 overall_size += size;
1956 if (dump_file)
1957 fprintf (dump_file, " Decided to specialize for all "
1958 "known contexts, growth deemed beneficial.\n");
1960 else if (dump_file && (dump_flags & TDF_DETAILS))
1961 fprintf (dump_file, " Not cloning for all contexts because "
1962 "max_new_size would be reached with %li.\n",
1963 size + overall_size);
1967 for (i = 0; i < count ; i++)
1969 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1970 struct ipcp_lattice *lat = &plats->itself;
1971 struct ipcp_value *val;
1972 int emc;
1974 if (lat->bottom
1975 || !lat->values
1976 || known_csts[i]
1977 || known_binfos[i])
1978 continue;
1980 for (val = lat->values; val; val = val->next)
1982 int time, size, time_benefit;
1983 inline_hints hints;
1985 if (TREE_CODE (val->value) != TREE_BINFO)
1987 known_csts[i] = val->value;
1988 known_binfos[i] = NULL_TREE;
1989 emc = estimate_move_cost (TREE_TYPE (val->value));
1991 else if (plats->virt_call)
1993 known_csts[i] = NULL_TREE;
1994 known_binfos[i] = val->value;
1995 emc = 0;
1997 else
1998 continue;
2000 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
2001 known_aggs_ptrs, &size, &time,
2002 &hints);
2003 time_benefit = base_time - time
2004 + devirtualization_time_bonus (node, known_csts, known_binfos,
2005 known_aggs_ptrs)
2006 + hint_time_bonus (hints)
2007 + removable_params_cost + emc;
2009 gcc_checking_assert (size >=0);
2010 /* The inliner-heuristics based estimates may think that in certain
2011 contexts some functions do not have any size at all but we want
2012 all specializations to have at least a tiny cost, not least not to
2013 divide by zero. */
2014 if (size == 0)
2015 size = 1;
2017 if (dump_file && (dump_flags & TDF_DETAILS))
2019 fprintf (dump_file, " - estimates for value ");
2020 print_ipcp_constant_value (dump_file, val->value);
2021 fprintf (dump_file, " for ");
2022 ipa_dump_param (dump_file, info, i);
2023 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2024 time_benefit, size);
2027 val->local_time_benefit = time_benefit;
2028 val->local_size_cost = size;
2030 known_binfos[i] = NULL_TREE;
2031 known_csts[i] = NULL_TREE;
2034 for (i = 0; i < count ; i++)
2036 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2037 struct ipa_agg_jump_function *ajf;
2038 struct ipcp_agg_lattice *aglat;
2040 if (plats->aggs_bottom || !plats->aggs)
2041 continue;
2043 ajf = &known_aggs[i];
2044 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2046 struct ipcp_value *val;
2047 if (aglat->bottom || !aglat->values
2048 /* If the following is true, the one value is in known_aggs. */
2049 || (!plats->aggs_contain_variable
2050 && ipa_lat_is_single_const (aglat)))
2051 continue;
2053 for (val = aglat->values; val; val = val->next)
2055 int time, size, time_benefit;
2056 struct ipa_agg_jf_item item;
2057 inline_hints hints;
2059 item.offset = aglat->offset;
2060 item.value = val->value;
2061 vec_safe_push (ajf->items, item);
2063 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
2064 known_aggs_ptrs, &size, &time,
2065 &hints);
2066 time_benefit = base_time - time
2067 + devirtualization_time_bonus (node, known_csts, known_binfos,
2068 known_aggs_ptrs)
2069 + hint_time_bonus (hints);
2070 gcc_checking_assert (size >=0);
2071 if (size == 0)
2072 size = 1;
2074 if (dump_file && (dump_flags & TDF_DETAILS))
2076 fprintf (dump_file, " - estimates for value ");
2077 print_ipcp_constant_value (dump_file, val->value);
2078 fprintf (dump_file, " for ");
2079 ipa_dump_param (dump_file, info, i);
2080 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2081 "]: time_benefit: %i, size: %i\n",
2082 plats->aggs_by_ref ? "ref " : "",
2083 aglat->offset, time_benefit, size);
2086 val->local_time_benefit = time_benefit;
2087 val->local_size_cost = size;
2088 ajf->items->pop ();
2093 for (i = 0; i < count ; i++)
2094 vec_free (known_aggs[i].items);
2096 known_csts.release ();
2097 known_binfos.release ();
2098 known_aggs.release ();
2099 known_aggs_ptrs.release ();
2103 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2104 topological sort of values. */
2106 static void
2107 add_val_to_toposort (struct ipcp_value *cur_val)
2109 static int dfs_counter = 0;
2110 static struct ipcp_value *stack;
2111 struct ipcp_value_source *src;
2113 if (cur_val->dfs)
2114 return;
2116 dfs_counter++;
2117 cur_val->dfs = dfs_counter;
2118 cur_val->low_link = dfs_counter;
2120 cur_val->topo_next = stack;
2121 stack = cur_val;
2122 cur_val->on_stack = true;
2124 for (src = cur_val->sources; src; src = src->next)
2125 if (src->val)
2127 if (src->val->dfs == 0)
2129 add_val_to_toposort (src->val);
2130 if (src->val->low_link < cur_val->low_link)
2131 cur_val->low_link = src->val->low_link;
2133 else if (src->val->on_stack
2134 && src->val->dfs < cur_val->low_link)
2135 cur_val->low_link = src->val->dfs;
2138 if (cur_val->dfs == cur_val->low_link)
2140 struct ipcp_value *v, *scc_list = NULL;
2144 v = stack;
2145 stack = v->topo_next;
2146 v->on_stack = false;
2148 v->scc_next = scc_list;
2149 scc_list = v;
2151 while (v != cur_val);
2153 cur_val->topo_next = values_topo;
2154 values_topo = cur_val;
2158 /* Add all values in lattices associated with NODE to the topological sort if
2159 they are not there yet. */
2161 static void
2162 add_all_node_vals_to_toposort (struct cgraph_node *node)
2164 struct ipa_node_params *info = IPA_NODE_REF (node);
2165 int i, count = ipa_get_param_count (info);
2167 for (i = 0; i < count ; i++)
2169 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2170 struct ipcp_lattice *lat = &plats->itself;
2171 struct ipcp_agg_lattice *aglat;
2172 struct ipcp_value *val;
2174 if (!lat->bottom)
2175 for (val = lat->values; val; val = val->next)
2176 add_val_to_toposort (val);
2178 if (!plats->aggs_bottom)
2179 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2180 if (!aglat->bottom)
2181 for (val = aglat->values; val; val = val->next)
2182 add_val_to_toposort (val);
2186 /* One pass of constants propagation along the call graph edges, from callers
2187 to callees (requires topological ordering in TOPO), iterate over strongly
2188 connected components. */
2190 static void
2191 propagate_constants_topo (struct topo_info *topo)
2193 int i;
2195 for (i = topo->nnodes - 1; i >= 0; i--)
2197 unsigned j;
2198 struct cgraph_node *v, *node = topo->order[i];
2199 vec<cgraph_node_ptr> cycle_nodes = ipa_get_nodes_in_cycle (node);
2201 /* First, iteratively propagate within the strongly connected component
2202 until all lattices stabilize. */
2203 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2204 if (cgraph_function_with_gimple_body_p (v))
2205 push_node_to_stack (topo, v);
2207 v = pop_node_from_stack (topo);
2208 while (v)
2210 struct cgraph_edge *cs;
2212 for (cs = v->callees; cs; cs = cs->next_callee)
2213 if (ipa_edge_within_scc (cs)
2214 && propagate_constants_accross_call (cs))
2215 push_node_to_stack (topo, cs->callee);
2216 v = pop_node_from_stack (topo);
2219 /* Afterwards, propagate along edges leading out of the SCC, calculates
2220 the local effects of the discovered constants and all valid values to
2221 their topological sort. */
2222 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2223 if (cgraph_function_with_gimple_body_p (v))
2225 struct cgraph_edge *cs;
2227 estimate_local_effects (v);
2228 add_all_node_vals_to_toposort (v);
2229 for (cs = v->callees; cs; cs = cs->next_callee)
2230 if (!ipa_edge_within_scc (cs))
2231 propagate_constants_accross_call (cs);
2233 cycle_nodes.release ();
2238 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2239 the bigger one if otherwise. */
2241 static int
2242 safe_add (int a, int b)
2244 if (a > INT_MAX/2 || b > INT_MAX/2)
2245 return a > b ? a : b;
2246 else
2247 return a + b;
2251 /* Propagate the estimated effects of individual values along the topological
2252 from the dependent values to those they depend on. */
2254 static void
2255 propagate_effects (void)
2257 struct ipcp_value *base;
2259 for (base = values_topo; base; base = base->topo_next)
2261 struct ipcp_value_source *src;
2262 struct ipcp_value *val;
2263 int time = 0, size = 0;
2265 for (val = base; val; val = val->scc_next)
2267 time = safe_add (time,
2268 val->local_time_benefit + val->prop_time_benefit);
2269 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2272 for (val = base; val; val = val->scc_next)
2273 for (src = val->sources; src; src = src->next)
2274 if (src->val
2275 && cgraph_maybe_hot_edge_p (src->cs))
2277 src->val->prop_time_benefit = safe_add (time,
2278 src->val->prop_time_benefit);
2279 src->val->prop_size_cost = safe_add (size,
2280 src->val->prop_size_cost);
2286 /* Propagate constants, binfos and their effects from the summaries
2287 interprocedurally. */
2289 static void
2290 ipcp_propagate_stage (struct topo_info *topo)
2292 struct cgraph_node *node;
2294 if (dump_file)
2295 fprintf (dump_file, "\n Propagating constants:\n\n");
2297 if (in_lto_p)
2298 ipa_update_after_lto_read ();
2301 FOR_EACH_DEFINED_FUNCTION (node)
2303 struct ipa_node_params *info = IPA_NODE_REF (node);
2305 determine_versionability (node);
2306 if (cgraph_function_with_gimple_body_p (node))
2308 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2309 ipa_get_param_count (info));
2310 initialize_node_lattices (node);
2312 if (node->definition && !node->alias)
2313 overall_size += inline_summary (node)->self_size;
2314 if (node->count > max_count)
2315 max_count = node->count;
2318 max_new_size = overall_size;
2319 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2320 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2321 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2323 if (dump_file)
2324 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2325 overall_size, max_new_size);
2327 propagate_constants_topo (topo);
2328 #ifdef ENABLE_CHECKING
2329 ipcp_verify_propagated_values ();
2330 #endif
2331 propagate_effects ();
2333 if (dump_file)
2335 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2336 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2340 /* Discover newly direct outgoing edges from NODE which is a new clone with
2341 known KNOWN_VALS and make them direct. */
2343 static void
2344 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2345 vec<tree> known_vals,
2346 struct ipa_agg_replacement_value *aggvals)
2348 struct cgraph_edge *ie, *next_ie;
2349 bool found = false;
2351 for (ie = node->indirect_calls; ie; ie = next_ie)
2353 tree target;
2355 next_ie = ie->next_callee;
2356 target = ipa_get_indirect_edge_target_1 (ie, known_vals, vNULL, vNULL,
2357 aggvals);
2358 if (target)
2360 bool agg_contents = ie->indirect_info->agg_contents;
2361 bool polymorphic = ie->indirect_info->polymorphic;
2362 int param_index = ie->indirect_info->param_index;
2363 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target);
2364 found = true;
2366 if (cs && !agg_contents && !polymorphic)
2368 struct ipa_node_params *info = IPA_NODE_REF (node);
2369 int c = ipa_get_controlled_uses (info, param_index);
2370 if (c != IPA_UNDESCRIBED_USE)
2372 struct ipa_ref *to_del;
2374 c--;
2375 ipa_set_controlled_uses (info, param_index, c);
2376 if (dump_file && (dump_flags & TDF_DETAILS))
2377 fprintf (dump_file, " controlled uses count of param "
2378 "%i bumped down to %i\n", param_index, c);
2379 if (c == 0
2380 && (to_del = ipa_find_reference (node,
2381 cs->callee,
2382 NULL, 0)))
2384 if (dump_file && (dump_flags & TDF_DETAILS))
2385 fprintf (dump_file, " and even removing its "
2386 "cloning-created reference\n");
2387 ipa_remove_reference (to_del);
2393 /* Turning calls to direct calls will improve overall summary. */
2394 if (found)
2395 inline_update_overall_summary (node);
2398 /* Vector of pointers which for linked lists of clones of an original crgaph
2399 edge. */
2401 static vec<cgraph_edge_p> next_edge_clone;
2402 static vec<cgraph_edge_p> prev_edge_clone;
2404 static inline void
2405 grow_edge_clone_vectors (void)
2407 if (next_edge_clone.length ()
2408 <= (unsigned) cgraph_edge_max_uid)
2409 next_edge_clone.safe_grow_cleared (cgraph_edge_max_uid + 1);
2410 if (prev_edge_clone.length ()
2411 <= (unsigned) cgraph_edge_max_uid)
2412 prev_edge_clone.safe_grow_cleared (cgraph_edge_max_uid + 1);
2415 /* Edge duplication hook to grow the appropriate linked list in
2416 next_edge_clone. */
2418 static void
2419 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2420 void *)
2422 grow_edge_clone_vectors ();
2424 struct cgraph_edge *old_next = next_edge_clone[src->uid];
2425 if (old_next)
2426 prev_edge_clone[old_next->uid] = dst;
2427 prev_edge_clone[dst->uid] = src;
2429 next_edge_clone[dst->uid] = old_next;
2430 next_edge_clone[src->uid] = dst;
2433 /* Hook that is called by cgraph.c when an edge is removed. */
2435 static void
2436 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
2438 grow_edge_clone_vectors ();
2440 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
2441 struct cgraph_edge *next = next_edge_clone[cs->uid];
2442 if (prev)
2443 next_edge_clone[prev->uid] = next;
2444 if (next)
2445 prev_edge_clone[next->uid] = prev;
2448 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2449 parameter with the given INDEX. */
2451 static tree
2452 get_clone_agg_value (struct cgraph_node *node, HOST_WIDEST_INT offset,
2453 int index)
2455 struct ipa_agg_replacement_value *aggval;
2457 aggval = ipa_get_agg_replacements_for_node (node);
2458 while (aggval)
2460 if (aggval->offset == offset
2461 && aggval->index == index)
2462 return aggval->value;
2463 aggval = aggval->next;
2465 return NULL_TREE;
2468 /* Return true if edge CS does bring about the value described by SRC. */
2470 static bool
2471 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
2472 struct ipcp_value_source *src)
2474 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2475 struct ipa_node_params *dst_info = IPA_NODE_REF (cs->callee);
2477 if ((dst_info->ipcp_orig_node && !dst_info->is_all_contexts_clone)
2478 || caller_info->node_dead)
2479 return false;
2480 if (!src->val)
2481 return true;
2483 if (caller_info->ipcp_orig_node)
2485 tree t;
2486 if (src->offset == -1)
2487 t = caller_info->known_vals[src->index];
2488 else
2489 t = get_clone_agg_value (cs->caller, src->offset, src->index);
2490 return (t != NULL_TREE
2491 && values_equal_for_ipcp_p (src->val->value, t));
2493 else
2495 struct ipcp_agg_lattice *aglat;
2496 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
2497 src->index);
2498 if (src->offset == -1)
2499 return (ipa_lat_is_single_const (&plats->itself)
2500 && values_equal_for_ipcp_p (src->val->value,
2501 plats->itself.values->value));
2502 else
2504 if (plats->aggs_bottom || plats->aggs_contain_variable)
2505 return false;
2506 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2507 if (aglat->offset == src->offset)
2508 return (ipa_lat_is_single_const (aglat)
2509 && values_equal_for_ipcp_p (src->val->value,
2510 aglat->values->value));
2512 return false;
2516 /* Get the next clone in the linked list of clones of an edge. */
2518 static inline struct cgraph_edge *
2519 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
2521 return next_edge_clone[cs->uid];
2524 /* Given VAL, iterate over all its sources and if they still hold, add their
2525 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
2526 respectively. */
2528 static bool
2529 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
2530 gcov_type *count_sum, int *caller_count)
2532 struct ipcp_value_source *src;
2533 int freq = 0, count = 0;
2534 gcov_type cnt = 0;
2535 bool hot = false;
2537 for (src = val->sources; src; src = src->next)
2539 struct cgraph_edge *cs = src->cs;
2540 while (cs)
2542 if (cgraph_edge_brings_value_p (cs, src))
2544 count++;
2545 freq += cs->frequency;
2546 cnt += cs->count;
2547 hot |= cgraph_maybe_hot_edge_p (cs);
2549 cs = get_next_cgraph_edge_clone (cs);
2553 *freq_sum = freq;
2554 *count_sum = cnt;
2555 *caller_count = count;
2556 return hot;
2559 /* Return a vector of incoming edges that do bring value VAL. It is assumed
2560 their number is known and equal to CALLER_COUNT. */
2562 static vec<cgraph_edge_p>
2563 gather_edges_for_value (struct ipcp_value *val, int caller_count)
2565 struct ipcp_value_source *src;
2566 vec<cgraph_edge_p> ret;
2568 ret.create (caller_count);
2569 for (src = val->sources; src; src = src->next)
2571 struct cgraph_edge *cs = src->cs;
2572 while (cs)
2574 if (cgraph_edge_brings_value_p (cs, src))
2575 ret.quick_push (cs);
2576 cs = get_next_cgraph_edge_clone (cs);
2580 return ret;
2583 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
2584 Return it or NULL if for some reason it cannot be created. */
2586 static struct ipa_replace_map *
2587 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
2589 struct ipa_replace_map *replace_map;
2592 replace_map = ggc_alloc_ipa_replace_map ();
2593 if (dump_file)
2595 fprintf (dump_file, " replacing ");
2596 ipa_dump_param (dump_file, info, parm_num);
2598 fprintf (dump_file, " with const ");
2599 print_generic_expr (dump_file, value, 0);
2600 fprintf (dump_file, "\n");
2602 replace_map->old_tree = NULL;
2603 replace_map->parm_num = parm_num;
2604 replace_map->new_tree = value;
2605 replace_map->replace_p = true;
2606 replace_map->ref_p = false;
2608 return replace_map;
2611 /* Dump new profiling counts */
2613 static void
2614 dump_profile_updates (struct cgraph_node *orig_node,
2615 struct cgraph_node *new_node)
2617 struct cgraph_edge *cs;
2619 fprintf (dump_file, " setting count of the specialized node to "
2620 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
2621 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2622 fprintf (dump_file, " edge to %s has count "
2623 HOST_WIDE_INT_PRINT_DEC "\n",
2624 cs->callee->name (), (HOST_WIDE_INT) cs->count);
2626 fprintf (dump_file, " setting count of the original node to "
2627 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
2628 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2629 fprintf (dump_file, " edge to %s is left with "
2630 HOST_WIDE_INT_PRINT_DEC "\n",
2631 cs->callee->name (), (HOST_WIDE_INT) cs->count);
2634 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
2635 their profile information to reflect this. */
2637 static void
2638 update_profiling_info (struct cgraph_node *orig_node,
2639 struct cgraph_node *new_node)
2641 struct cgraph_edge *cs;
2642 struct caller_statistics stats;
2643 gcov_type new_sum, orig_sum;
2644 gcov_type remainder, orig_node_count = orig_node->count;
2646 if (orig_node_count == 0)
2647 return;
2649 init_caller_stats (&stats);
2650 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
2651 orig_sum = stats.count_sum;
2652 init_caller_stats (&stats);
2653 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
2654 new_sum = stats.count_sum;
2656 if (orig_node_count < orig_sum + new_sum)
2658 if (dump_file)
2659 fprintf (dump_file, " Problem: node %s/%i has too low count "
2660 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
2661 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
2662 orig_node->name (), orig_node->order,
2663 (HOST_WIDE_INT) orig_node_count,
2664 (HOST_WIDE_INT) (orig_sum + new_sum));
2666 orig_node_count = (orig_sum + new_sum) * 12 / 10;
2667 if (dump_file)
2668 fprintf (dump_file, " proceeding by pretending it was "
2669 HOST_WIDE_INT_PRINT_DEC "\n",
2670 (HOST_WIDE_INT) orig_node_count);
2673 new_node->count = new_sum;
2674 remainder = orig_node_count - new_sum;
2675 orig_node->count = remainder;
2677 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2678 if (cs->frequency)
2679 cs->count = apply_probability (cs->count,
2680 GCOV_COMPUTE_SCALE (new_sum,
2681 orig_node_count));
2682 else
2683 cs->count = 0;
2685 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2686 cs->count = apply_probability (cs->count,
2687 GCOV_COMPUTE_SCALE (remainder,
2688 orig_node_count));
2690 if (dump_file)
2691 dump_profile_updates (orig_node, new_node);
2694 /* Update the respective profile of specialized NEW_NODE and the original
2695 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
2696 have been redirected to the specialized version. */
2698 static void
2699 update_specialized_profile (struct cgraph_node *new_node,
2700 struct cgraph_node *orig_node,
2701 gcov_type redirected_sum)
2703 struct cgraph_edge *cs;
2704 gcov_type new_node_count, orig_node_count = orig_node->count;
2706 if (dump_file)
2707 fprintf (dump_file, " the sum of counts of redirected edges is "
2708 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
2709 if (orig_node_count == 0)
2710 return;
2712 gcc_assert (orig_node_count >= redirected_sum);
2714 new_node_count = new_node->count;
2715 new_node->count += redirected_sum;
2716 orig_node->count -= redirected_sum;
2718 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2719 if (cs->frequency)
2720 cs->count += apply_probability (cs->count,
2721 GCOV_COMPUTE_SCALE (redirected_sum,
2722 new_node_count));
2723 else
2724 cs->count = 0;
2726 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2728 gcov_type dec = apply_probability (cs->count,
2729 GCOV_COMPUTE_SCALE (redirected_sum,
2730 orig_node_count));
2731 if (dec < cs->count)
2732 cs->count -= dec;
2733 else
2734 cs->count = 0;
2737 if (dump_file)
2738 dump_profile_updates (orig_node, new_node);
2741 /* Create a specialized version of NODE with known constants and types of
2742 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
2744 static struct cgraph_node *
2745 create_specialized_node (struct cgraph_node *node,
2746 vec<tree> known_vals,
2747 struct ipa_agg_replacement_value *aggvals,
2748 vec<cgraph_edge_p> callers)
2750 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
2751 vec<ipa_replace_map_p, va_gc> *replace_trees = NULL;
2752 struct ipa_agg_replacement_value *av;
2753 struct cgraph_node *new_node;
2754 int i, count = ipa_get_param_count (info);
2755 bitmap args_to_skip;
2757 gcc_assert (!info->ipcp_orig_node);
2759 if (node->local.can_change_signature)
2761 args_to_skip = BITMAP_GGC_ALLOC ();
2762 for (i = 0; i < count; i++)
2764 tree t = known_vals[i];
2766 if ((t && TREE_CODE (t) != TREE_BINFO)
2767 || !ipa_is_param_used (info, i))
2768 bitmap_set_bit (args_to_skip, i);
2771 else
2773 args_to_skip = NULL;
2774 if (dump_file && (dump_flags & TDF_DETAILS))
2775 fprintf (dump_file, " cannot change function signature\n");
2778 for (i = 0; i < count ; i++)
2780 tree t = known_vals[i];
2781 if (t && TREE_CODE (t) != TREE_BINFO)
2783 struct ipa_replace_map *replace_map;
2785 replace_map = get_replacement_map (info, t, i);
2786 if (replace_map)
2787 vec_safe_push (replace_trees, replace_map);
2791 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2792 args_to_skip, "constprop");
2793 ipa_set_node_agg_value_chain (new_node, aggvals);
2794 for (av = aggvals; av; av = av->next)
2795 ipa_maybe_record_reference (new_node, av->value,
2796 IPA_REF_ADDR, NULL);
2798 if (dump_file && (dump_flags & TDF_DETAILS))
2800 fprintf (dump_file, " the new node is %s/%i.\n",
2801 new_node->name (), new_node->order);
2802 if (aggvals)
2803 ipa_dump_agg_replacement_values (dump_file, aggvals);
2805 gcc_checking_assert (ipa_node_params_vector.exists ()
2806 && (ipa_node_params_vector.length ()
2807 > (unsigned) cgraph_max_uid));
2808 update_profiling_info (node, new_node);
2809 new_info = IPA_NODE_REF (new_node);
2810 new_info->ipcp_orig_node = node;
2811 new_info->known_vals = known_vals;
2813 ipcp_discover_new_direct_edges (new_node, known_vals, aggvals);
2815 callers.release ();
2816 return new_node;
2819 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2820 KNOWN_VALS with constants and types that are also known for all of the
2821 CALLERS. */
2823 static void
2824 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
2825 vec<tree> known_vals,
2826 vec<cgraph_edge_p> callers)
2828 struct ipa_node_params *info = IPA_NODE_REF (node);
2829 int i, count = ipa_get_param_count (info);
2831 for (i = 0; i < count ; i++)
2833 struct cgraph_edge *cs;
2834 tree newval = NULL_TREE;
2835 int j;
2837 if (ipa_get_scalar_lat (info, i)->bottom || known_vals[i])
2838 continue;
2840 FOR_EACH_VEC_ELT (callers, j, cs)
2842 struct ipa_jump_func *jump_func;
2843 tree t;
2845 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2847 newval = NULL_TREE;
2848 break;
2850 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2851 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2852 if (!t
2853 || (newval
2854 && !values_equal_for_ipcp_p (t, newval)))
2856 newval = NULL_TREE;
2857 break;
2859 else
2860 newval = t;
2863 if (newval)
2865 if (dump_file && (dump_flags & TDF_DETAILS))
2867 fprintf (dump_file, " adding an extra known scalar value ");
2868 print_ipcp_constant_value (dump_file, newval);
2869 fprintf (dump_file, " for ");
2870 ipa_dump_param (dump_file, info, i);
2871 fprintf (dump_file, "\n");
2874 known_vals[i] = newval;
2879 /* Go through PLATS and create a vector of values consisting of values and
2880 offsets (minus OFFSET) of lattices that contain only a single value. */
2882 static vec<ipa_agg_jf_item>
2883 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
2885 vec<ipa_agg_jf_item> res = vNULL;
2887 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2888 return vNULL;
2890 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
2891 if (ipa_lat_is_single_const (aglat))
2893 struct ipa_agg_jf_item ti;
2894 ti.offset = aglat->offset - offset;
2895 ti.value = aglat->values->value;
2896 res.safe_push (ti);
2898 return res;
2901 /* Intersect all values in INTER with single value lattices in PLATS (while
2902 subtracting OFFSET). */
2904 static void
2905 intersect_with_plats (struct ipcp_param_lattices *plats,
2906 vec<ipa_agg_jf_item> *inter,
2907 HOST_WIDE_INT offset)
2909 struct ipcp_agg_lattice *aglat;
2910 struct ipa_agg_jf_item *item;
2911 int k;
2913 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2915 inter->release ();
2916 return;
2919 aglat = plats->aggs;
2920 FOR_EACH_VEC_ELT (*inter, k, item)
2922 bool found = false;
2923 if (!item->value)
2924 continue;
2925 while (aglat)
2927 if (aglat->offset - offset > item->offset)
2928 break;
2929 if (aglat->offset - offset == item->offset)
2931 gcc_checking_assert (item->value);
2932 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
2933 found = true;
2934 break;
2936 aglat = aglat->next;
2938 if (!found)
2939 item->value = NULL_TREE;
2943 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
2944 vector result while subtracting OFFSET from the individual value offsets. */
2946 static vec<ipa_agg_jf_item>
2947 agg_replacements_to_vector (struct cgraph_node *node, int index,
2948 HOST_WIDE_INT offset)
2950 struct ipa_agg_replacement_value *av;
2951 vec<ipa_agg_jf_item> res = vNULL;
2953 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
2954 if (av->index == index
2955 && (av->offset - offset) >= 0)
2957 struct ipa_agg_jf_item item;
2958 gcc_checking_assert (av->value);
2959 item.offset = av->offset - offset;
2960 item.value = av->value;
2961 res.safe_push (item);
2964 return res;
2967 /* Intersect all values in INTER with those that we have already scheduled to
2968 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
2969 (while subtracting OFFSET). */
2971 static void
2972 intersect_with_agg_replacements (struct cgraph_node *node, int index,
2973 vec<ipa_agg_jf_item> *inter,
2974 HOST_WIDE_INT offset)
2976 struct ipa_agg_replacement_value *srcvals;
2977 struct ipa_agg_jf_item *item;
2978 int i;
2980 srcvals = ipa_get_agg_replacements_for_node (node);
2981 if (!srcvals)
2983 inter->release ();
2984 return;
2987 FOR_EACH_VEC_ELT (*inter, i, item)
2989 struct ipa_agg_replacement_value *av;
2990 bool found = false;
2991 if (!item->value)
2992 continue;
2993 for (av = srcvals; av; av = av->next)
2995 gcc_checking_assert (av->value);
2996 if (av->index == index
2997 && av->offset - offset == item->offset)
2999 if (values_equal_for_ipcp_p (item->value, av->value))
3000 found = true;
3001 break;
3004 if (!found)
3005 item->value = NULL_TREE;
3009 /* Intersect values in INTER with aggregate values that come along edge CS to
3010 parameter number INDEX and return it. If INTER does not actually exist yet,
3011 copy all incoming values to it. If we determine we ended up with no values
3012 whatsoever, return a released vector. */
3014 static vec<ipa_agg_jf_item>
3015 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3016 vec<ipa_agg_jf_item> inter)
3018 struct ipa_jump_func *jfunc;
3019 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3020 if (jfunc->type == IPA_JF_PASS_THROUGH
3021 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3023 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3024 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3026 if (caller_info->ipcp_orig_node)
3028 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3029 struct ipcp_param_lattices *orig_plats;
3030 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3031 src_idx);
3032 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3034 if (!inter.exists ())
3035 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3036 else
3037 intersect_with_agg_replacements (cs->caller, src_idx,
3038 &inter, 0);
3041 else
3043 struct ipcp_param_lattices *src_plats;
3044 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3045 if (agg_pass_through_permissible_p (src_plats, jfunc))
3047 /* Currently we do not produce clobber aggregate jump
3048 functions, adjust when we do. */
3049 gcc_checking_assert (!jfunc->agg.items);
3050 if (!inter.exists ())
3051 inter = copy_plats_to_inter (src_plats, 0);
3052 else
3053 intersect_with_plats (src_plats, &inter, 0);
3057 else if (jfunc->type == IPA_JF_ANCESTOR
3058 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3060 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3061 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3062 struct ipcp_param_lattices *src_plats;
3063 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3065 if (caller_info->ipcp_orig_node)
3067 if (!inter.exists ())
3068 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3069 else
3070 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3071 delta);
3073 else
3075 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3076 /* Currently we do not produce clobber aggregate jump
3077 functions, adjust when we do. */
3078 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3079 if (!inter.exists ())
3080 inter = copy_plats_to_inter (src_plats, delta);
3081 else
3082 intersect_with_plats (src_plats, &inter, delta);
3085 else if (jfunc->agg.items)
3087 struct ipa_agg_jf_item *item;
3088 int k;
3090 if (!inter.exists ())
3091 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3092 inter.safe_push ((*jfunc->agg.items)[i]);
3093 else
3094 FOR_EACH_VEC_ELT (inter, k, item)
3096 int l = 0;
3097 bool found = false;;
3099 if (!item->value)
3100 continue;
3102 while ((unsigned) l < jfunc->agg.items->length ())
3104 struct ipa_agg_jf_item *ti;
3105 ti = &(*jfunc->agg.items)[l];
3106 if (ti->offset > item->offset)
3107 break;
3108 if (ti->offset == item->offset)
3110 gcc_checking_assert (ti->value);
3111 if (values_equal_for_ipcp_p (item->value,
3112 ti->value))
3113 found = true;
3114 break;
3116 l++;
3118 if (!found)
3119 item->value = NULL;
3122 else
3124 inter.release ();
3125 return vec<ipa_agg_jf_item>();
3127 return inter;
3130 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3131 from all of them. */
3133 static struct ipa_agg_replacement_value *
3134 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3135 vec<cgraph_edge_p> callers)
3137 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3138 struct ipa_agg_replacement_value *res = NULL;
3139 struct cgraph_edge *cs;
3140 int i, j, count = ipa_get_param_count (dest_info);
3142 FOR_EACH_VEC_ELT (callers, j, cs)
3144 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3145 if (c < count)
3146 count = c;
3149 for (i = 0; i < count ; i++)
3151 struct cgraph_edge *cs;
3152 vec<ipa_agg_jf_item> inter = vNULL;
3153 struct ipa_agg_jf_item *item;
3154 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3155 int j;
3157 /* Among other things, the following check should deal with all by_ref
3158 mismatches. */
3159 if (plats->aggs_bottom)
3160 continue;
3162 FOR_EACH_VEC_ELT (callers, j, cs)
3164 inter = intersect_aggregates_with_edge (cs, i, inter);
3166 if (!inter.exists ())
3167 goto next_param;
3170 FOR_EACH_VEC_ELT (inter, j, item)
3172 struct ipa_agg_replacement_value *v;
3174 if (!item->value)
3175 continue;
3177 v = ggc_alloc_ipa_agg_replacement_value ();
3178 v->index = i;
3179 v->offset = item->offset;
3180 v->value = item->value;
3181 v->by_ref = plats->aggs_by_ref;
3182 v->next = res;
3183 res = v;
3186 next_param:
3187 if (inter.exists ())
3188 inter.release ();
3190 return res;
3193 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3195 static struct ipa_agg_replacement_value *
3196 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3198 struct ipa_agg_replacement_value *res = NULL;
3199 struct ipa_agg_jump_function *aggjf;
3200 struct ipa_agg_jf_item *item;
3201 int i, j;
3203 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3204 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3206 struct ipa_agg_replacement_value *v;
3207 v = ggc_alloc_ipa_agg_replacement_value ();
3208 v->index = i;
3209 v->offset = item->offset;
3210 v->value = item->value;
3211 v->by_ref = aggjf->by_ref;
3212 v->next = res;
3213 res = v;
3215 return res;
3218 /* Determine whether CS also brings all scalar values that the NODE is
3219 specialized for. */
3221 static bool
3222 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3223 struct cgraph_node *node)
3225 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3226 int count = ipa_get_param_count (dest_info);
3227 struct ipa_node_params *caller_info;
3228 struct ipa_edge_args *args;
3229 int i;
3231 caller_info = IPA_NODE_REF (cs->caller);
3232 args = IPA_EDGE_REF (cs);
3233 for (i = 0; i < count; i++)
3235 struct ipa_jump_func *jump_func;
3236 tree val, t;
3238 val = dest_info->known_vals[i];
3239 if (!val)
3240 continue;
3242 if (i >= ipa_get_cs_argument_count (args))
3243 return false;
3244 jump_func = ipa_get_ith_jump_func (args, i);
3245 t = ipa_value_from_jfunc (caller_info, jump_func);
3246 if (!t || !values_equal_for_ipcp_p (val, t))
3247 return false;
3249 return true;
3252 /* Determine whether CS also brings all aggregate values that NODE is
3253 specialized for. */
3254 static bool
3255 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3256 struct cgraph_node *node)
3258 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3259 struct ipa_agg_replacement_value *aggval;
3260 int i, ec, count;
3262 aggval = ipa_get_agg_replacements_for_node (node);
3263 if (!aggval)
3264 return true;
3266 count = ipa_get_param_count (IPA_NODE_REF (node));
3267 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3268 if (ec < count)
3269 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3270 if (aggval->index >= ec)
3271 return false;
3273 if (orig_caller_info->ipcp_orig_node)
3274 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3276 for (i = 0; i < count; i++)
3278 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
3279 struct ipcp_param_lattices *plats;
3280 bool interesting = false;
3281 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3282 if (aggval->index == i)
3284 interesting = true;
3285 break;
3287 if (!interesting)
3288 continue;
3290 plats = ipa_get_parm_lattices (orig_caller_info, aggval->index);
3291 if (plats->aggs_bottom)
3292 return false;
3294 values = intersect_aggregates_with_edge (cs, i, values);
3295 if (!values.exists ())
3296 return false;
3298 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3299 if (aggval->index == i)
3301 struct ipa_agg_jf_item *item;
3302 int j;
3303 bool found = false;
3304 FOR_EACH_VEC_ELT (values, j, item)
3305 if (item->value
3306 && item->offset == av->offset
3307 && values_equal_for_ipcp_p (item->value, av->value))
3309 found = true;
3310 break;
3312 if (!found)
3314 values.release ();
3315 return false;
3319 return true;
3322 /* Given an original NODE and a VAL for which we have already created a
3323 specialized clone, look whether there are incoming edges that still lead
3324 into the old node but now also bring the requested value and also conform to
3325 all other criteria such that they can be redirected the the special node.
3326 This function can therefore redirect the final edge in a SCC. */
3328 static void
3329 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
3331 struct ipcp_value_source *src;
3332 gcov_type redirected_sum = 0;
3334 for (src = val->sources; src; src = src->next)
3336 struct cgraph_edge *cs = src->cs;
3337 while (cs)
3339 enum availability availability;
3340 struct cgraph_node *dst = cgraph_function_node (cs->callee,
3341 &availability);
3342 if ((dst == node || IPA_NODE_REF (dst)->is_all_contexts_clone)
3343 && availability > AVAIL_OVERWRITABLE
3344 && cgraph_edge_brings_value_p (cs, src))
3346 if (cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3347 && cgraph_edge_brings_all_agg_vals_for_node (cs,
3348 val->spec_node))
3350 if (dump_file)
3351 fprintf (dump_file, " - adding an extra caller %s/%i"
3352 " of %s/%i\n",
3353 xstrdup (cs->caller->name ()),
3354 cs->caller->order,
3355 xstrdup (val->spec_node->name ()),
3356 val->spec_node->order);
3358 cgraph_redirect_edge_callee (cs, val->spec_node);
3359 redirected_sum += cs->count;
3362 cs = get_next_cgraph_edge_clone (cs);
3366 if (redirected_sum)
3367 update_specialized_profile (val->spec_node, node, redirected_sum);
3371 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
3373 static void
3374 move_binfos_to_values (vec<tree> known_vals,
3375 vec<tree> known_binfos)
3377 tree t;
3378 int i;
3380 for (i = 0; known_binfos.iterate (i, &t); i++)
3381 if (t)
3382 known_vals[i] = t;
3385 /* Return true if there is a replacement equivalent to VALUE, INDEX and OFFSET
3386 among those in the AGGVALS list. */
3388 DEBUG_FUNCTION bool
3389 ipcp_val_in_agg_replacements_p (struct ipa_agg_replacement_value *aggvals,
3390 int index, HOST_WIDE_INT offset, tree value)
3392 while (aggvals)
3394 if (aggvals->index == index
3395 && aggvals->offset == offset
3396 && values_equal_for_ipcp_p (aggvals->value, value))
3397 return true;
3398 aggvals = aggvals->next;
3400 return false;
3403 /* Decide wheter to create a special version of NODE for value VAL of parameter
3404 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
3405 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
3406 KNOWN_BINFOS and KNOWN_AGGS describe the other already known values. */
3408 static bool
3409 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
3410 struct ipcp_value *val, vec<tree> known_csts,
3411 vec<tree> known_binfos)
3413 struct ipa_agg_replacement_value *aggvals;
3414 int freq_sum, caller_count;
3415 gcov_type count_sum;
3416 vec<cgraph_edge_p> callers;
3417 vec<tree> kv;
3419 if (val->spec_node)
3421 perhaps_add_new_callers (node, val);
3422 return false;
3424 else if (val->local_size_cost + overall_size > max_new_size)
3426 if (dump_file && (dump_flags & TDF_DETAILS))
3427 fprintf (dump_file, " Ignoring candidate value because "
3428 "max_new_size would be reached with %li.\n",
3429 val->local_size_cost + overall_size);
3430 return false;
3432 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
3433 &caller_count))
3434 return false;
3436 if (dump_file && (dump_flags & TDF_DETAILS))
3438 fprintf (dump_file, " - considering value ");
3439 print_ipcp_constant_value (dump_file, val->value);
3440 fprintf (dump_file, " for ");
3441 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
3442 if (offset != -1)
3443 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
3444 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
3447 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
3448 freq_sum, count_sum,
3449 val->local_size_cost)
3450 && !good_cloning_opportunity_p (node,
3451 val->local_time_benefit
3452 + val->prop_time_benefit,
3453 freq_sum, count_sum,
3454 val->local_size_cost
3455 + val->prop_size_cost))
3456 return false;
3458 if (dump_file)
3459 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
3460 node->name (), node->order);
3462 callers = gather_edges_for_value (val, caller_count);
3463 kv = known_csts.copy ();
3464 move_binfos_to_values (kv, known_binfos);
3465 if (offset == -1)
3466 kv[index] = val->value;
3467 find_more_scalar_values_for_callers_subset (node, kv, callers);
3468 aggvals = find_aggregate_values_for_callers_subset (node, callers);
3469 gcc_checking_assert (offset == -1
3470 || ipcp_val_in_agg_replacements_p (aggvals, index,
3471 offset, val->value));
3472 val->spec_node = create_specialized_node (node, kv, aggvals, callers);
3473 overall_size += val->local_size_cost;
3475 /* TODO: If for some lattice there is only one other known value
3476 left, make a special node for it too. */
3478 return true;
3481 /* Decide whether and what specialized clones of NODE should be created. */
3483 static bool
3484 decide_whether_version_node (struct cgraph_node *node)
3486 struct ipa_node_params *info = IPA_NODE_REF (node);
3487 int i, count = ipa_get_param_count (info);
3488 vec<tree> known_csts, known_binfos;
3489 vec<ipa_agg_jump_function> known_aggs = vNULL;
3490 bool ret = false;
3492 if (count == 0)
3493 return false;
3495 if (dump_file && (dump_flags & TDF_DETAILS))
3496 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
3497 node->name (), node->order);
3499 gather_context_independent_values (info, &known_csts, &known_binfos,
3500 info->do_clone_for_all_contexts ? &known_aggs
3501 : NULL, NULL);
3503 for (i = 0; i < count ;i++)
3505 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3506 struct ipcp_lattice *lat = &plats->itself;
3507 struct ipcp_value *val;
3509 if (!lat->bottom
3510 && !known_csts[i]
3511 && !known_binfos[i])
3512 for (val = lat->values; val; val = val->next)
3513 ret |= decide_about_value (node, i, -1, val, known_csts,
3514 known_binfos);
3516 if (!plats->aggs_bottom)
3518 struct ipcp_agg_lattice *aglat;
3519 struct ipcp_value *val;
3520 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3521 if (!aglat->bottom && aglat->values
3522 /* If the following is false, the one value is in
3523 known_aggs. */
3524 && (plats->aggs_contain_variable
3525 || !ipa_lat_is_single_const (aglat)))
3526 for (val = aglat->values; val; val = val->next)
3527 ret |= decide_about_value (node, i, aglat->offset, val,
3528 known_csts, known_binfos);
3530 info = IPA_NODE_REF (node);
3533 if (info->do_clone_for_all_contexts)
3535 struct cgraph_node *clone;
3536 vec<cgraph_edge_p> callers;
3538 if (dump_file)
3539 fprintf (dump_file, " - Creating a specialized node of %s/%i "
3540 "for all known contexts.\n", node->name (),
3541 node->order);
3543 callers = collect_callers_of_node (node);
3544 move_binfos_to_values (known_csts, known_binfos);
3545 clone = create_specialized_node (node, known_csts,
3546 known_aggs_to_agg_replacement_list (known_aggs),
3547 callers);
3548 info = IPA_NODE_REF (node);
3549 info->do_clone_for_all_contexts = false;
3550 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
3551 for (i = 0; i < count ; i++)
3552 vec_free (known_aggs[i].items);
3553 known_aggs.release ();
3554 ret = true;
3556 else
3557 known_csts.release ();
3559 known_binfos.release ();
3560 return ret;
3563 /* Transitively mark all callees of NODE within the same SCC as not dead. */
3565 static void
3566 spread_undeadness (struct cgraph_node *node)
3568 struct cgraph_edge *cs;
3570 for (cs = node->callees; cs; cs = cs->next_callee)
3571 if (ipa_edge_within_scc (cs))
3573 struct cgraph_node *callee;
3574 struct ipa_node_params *info;
3576 callee = cgraph_function_node (cs->callee, NULL);
3577 info = IPA_NODE_REF (callee);
3579 if (info->node_dead)
3581 info->node_dead = 0;
3582 spread_undeadness (callee);
3587 /* Return true if NODE has a caller from outside of its SCC that is not
3588 dead. Worker callback for cgraph_for_node_and_aliases. */
3590 static bool
3591 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
3592 void *data ATTRIBUTE_UNUSED)
3594 struct cgraph_edge *cs;
3596 for (cs = node->callers; cs; cs = cs->next_caller)
3597 if (cs->caller->thunk.thunk_p
3598 && cgraph_for_node_and_aliases (cs->caller,
3599 has_undead_caller_from_outside_scc_p,
3600 NULL, true))
3601 return true;
3602 else if (!ipa_edge_within_scc (cs)
3603 && !IPA_NODE_REF (cs->caller)->node_dead)
3604 return true;
3605 return false;
3609 /* Identify nodes within the same SCC as NODE which are no longer needed
3610 because of new clones and will be removed as unreachable. */
3612 static void
3613 identify_dead_nodes (struct cgraph_node *node)
3615 struct cgraph_node *v;
3616 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3617 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
3618 && !cgraph_for_node_and_aliases (v,
3619 has_undead_caller_from_outside_scc_p,
3620 NULL, true))
3621 IPA_NODE_REF (v)->node_dead = 1;
3623 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3624 if (!IPA_NODE_REF (v)->node_dead)
3625 spread_undeadness (v);
3627 if (dump_file && (dump_flags & TDF_DETAILS))
3629 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3630 if (IPA_NODE_REF (v)->node_dead)
3631 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
3632 v->name (), v->order);
3636 /* The decision stage. Iterate over the topological order of call graph nodes
3637 TOPO and make specialized clones if deemed beneficial. */
3639 static void
3640 ipcp_decision_stage (struct topo_info *topo)
3642 int i;
3644 if (dump_file)
3645 fprintf (dump_file, "\nIPA decision stage:\n\n");
3647 for (i = topo->nnodes - 1; i >= 0; i--)
3649 struct cgraph_node *node = topo->order[i];
3650 bool change = false, iterate = true;
3652 while (iterate)
3654 struct cgraph_node *v;
3655 iterate = false;
3656 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3657 if (cgraph_function_with_gimple_body_p (v)
3658 && ipcp_versionable_function_p (v))
3659 iterate |= decide_whether_version_node (v);
3661 change |= iterate;
3663 if (change)
3664 identify_dead_nodes (node);
3668 /* The IPCP driver. */
3670 static unsigned int
3671 ipcp_driver (void)
3673 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
3674 struct cgraph_edge_hook_list *edge_removal_hook_holder;
3675 struct topo_info topo;
3677 ipa_check_create_node_params ();
3678 ipa_check_create_edge_args ();
3679 grow_edge_clone_vectors ();
3680 edge_duplication_hook_holder =
3681 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
3682 edge_removal_hook_holder =
3683 cgraph_add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
3685 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
3686 sizeof (struct ipcp_value), 32);
3687 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
3688 sizeof (struct ipcp_value_source), 64);
3689 ipcp_agg_lattice_pool = create_alloc_pool ("IPA_CP aggregate lattices",
3690 sizeof (struct ipcp_agg_lattice),
3691 32);
3692 if (dump_file)
3694 fprintf (dump_file, "\nIPA structures before propagation:\n");
3695 if (dump_flags & TDF_DETAILS)
3696 ipa_print_all_params (dump_file);
3697 ipa_print_all_jump_functions (dump_file);
3700 /* Topological sort. */
3701 build_toporder_info (&topo);
3702 /* Do the interprocedural propagation. */
3703 ipcp_propagate_stage (&topo);
3704 /* Decide what constant propagation and cloning should be performed. */
3705 ipcp_decision_stage (&topo);
3707 /* Free all IPCP structures. */
3708 free_toporder_info (&topo);
3709 next_edge_clone.release ();
3710 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3711 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3712 ipa_free_all_structures_after_ipa_cp ();
3713 if (dump_file)
3714 fprintf (dump_file, "\nIPA constant propagation end\n");
3715 return 0;
3718 /* Initialization and computation of IPCP data structures. This is the initial
3719 intraprocedural analysis of functions, which gathers information to be
3720 propagated later on. */
3722 static void
3723 ipcp_generate_summary (void)
3725 struct cgraph_node *node;
3727 if (dump_file)
3728 fprintf (dump_file, "\nIPA constant propagation start:\n");
3729 ipa_register_cgraph_hooks ();
3731 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
3733 node->local.versionable
3734 = tree_versionable_function_p (node->decl);
3735 ipa_analyze_node (node);
3739 /* Write ipcp summary for nodes in SET. */
3741 static void
3742 ipcp_write_summary (void)
3744 ipa_prop_write_jump_functions ();
3747 /* Read ipcp summary. */
3749 static void
3750 ipcp_read_summary (void)
3752 ipa_prop_read_jump_functions ();
3755 /* Gate for IPCP optimization. */
3757 static bool
3758 cgraph_gate_cp (void)
3760 /* FIXME: We should remove the optimize check after we ensure we never run
3761 IPA passes when not optimizing. */
3762 return flag_ipa_cp && optimize;
3765 namespace {
3767 const pass_data pass_data_ipa_cp =
3769 IPA_PASS, /* type */
3770 "cp", /* name */
3771 OPTGROUP_NONE, /* optinfo_flags */
3772 true, /* has_gate */
3773 true, /* has_execute */
3774 TV_IPA_CONSTANT_PROP, /* tv_id */
3775 0, /* properties_required */
3776 0, /* properties_provided */
3777 0, /* properties_destroyed */
3778 0, /* todo_flags_start */
3779 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
3782 class pass_ipa_cp : public ipa_opt_pass_d
3784 public:
3785 pass_ipa_cp (gcc::context *ctxt)
3786 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
3787 ipcp_generate_summary, /* generate_summary */
3788 ipcp_write_summary, /* write_summary */
3789 ipcp_read_summary, /* read_summary */
3790 ipa_prop_write_all_agg_replacement, /*
3791 write_optimization_summary */
3792 ipa_prop_read_all_agg_replacement, /*
3793 read_optimization_summary */
3794 NULL, /* stmt_fixup */
3795 0, /* function_transform_todo_flags_start */
3796 ipcp_transform_function, /* function_transform */
3797 NULL) /* variable_transform */
3800 /* opt_pass methods: */
3801 bool gate () { return cgraph_gate_cp (); }
3802 unsigned int execute () { return ipcp_driver (); }
3804 }; // class pass_ipa_cp
3806 } // anon namespace
3808 ipa_opt_pass_d *
3809 make_pass_ipa_cp (gcc::context *ctxt)
3811 return new pass_ipa_cp (ctxt);