Daily bump.
[official-gcc.git] / gcc / ipa-cp.c
blobd9aa92ed5b5a74950a6e3dbdfe880e272dbff7a4
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2015 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 "hash-set.h"
107 #include "machmode.h"
108 #include "vec.h"
109 #include "hash-map.h"
110 #include "double-int.h"
111 #include "input.h"
112 #include "alias.h"
113 #include "symtab.h"
114 #include "options.h"
115 #include "wide-int.h"
116 #include "inchash.h"
117 #include "tree.h"
118 #include "fold-const.h"
119 #include "gimple-fold.h"
120 #include "gimple-expr.h"
121 #include "target.h"
122 #include "predict.h"
123 #include "basic-block.h"
124 #include "is-a.h"
125 #include "plugin-api.h"
126 #include "tm.h"
127 #include "hard-reg-set.h"
128 #include "input.h"
129 #include "function.h"
130 #include "ipa-ref.h"
131 #include "cgraph.h"
132 #include "alloc-pool.h"
133 #include "symbol-summary.h"
134 #include "ipa-prop.h"
135 #include "bitmap.h"
136 #include "tree-pass.h"
137 #include "flags.h"
138 #include "diagnostic.h"
139 #include "tree-pretty-print.h"
140 #include "tree-inline.h"
141 #include "params.h"
142 #include "ipa-inline.h"
143 #include "ipa-utils.h"
145 template <typename valtype> class ipcp_value;
147 /* Describes a particular source for an IPA-CP value. */
149 template <typename valtype>
150 class ipcp_value_source
152 public:
153 /* Aggregate offset of the source, negative if the source is scalar value of
154 the argument itself. */
155 HOST_WIDE_INT offset;
156 /* The incoming edge that brought the value. */
157 cgraph_edge *cs;
158 /* If the jump function that resulted into his value was a pass-through or an
159 ancestor, this is the ipcp_value of the caller from which the described
160 value has been derived. Otherwise it is NULL. */
161 ipcp_value<valtype> *val;
162 /* Next pointer in a linked list of sources of a value. */
163 ipcp_value_source *next;
164 /* If the jump function that resulted into his value was a pass-through or an
165 ancestor, this is the index of the parameter of the caller the jump
166 function references. */
167 int index;
170 /* Common ancestor for all ipcp_value instantiations. */
172 class ipcp_value_base
174 public:
175 /* Time benefit and size cost that specializing the function for this value
176 would bring about in this function alone. */
177 int local_time_benefit, local_size_cost;
178 /* Time benefit and size cost that specializing the function for this value
179 can bring about in it's callees (transitively). */
180 int prop_time_benefit, prop_size_cost;
183 /* Describes one particular value stored in struct ipcp_lattice. */
185 template <typename valtype>
186 class ipcp_value : public ipcp_value_base
188 public:
189 /* The actual value for the given parameter. */
190 valtype value;
191 /* The list of sources from which this value originates. */
192 ipcp_value_source <valtype> *sources;
193 /* Next pointers in a linked list of all values in a lattice. */
194 ipcp_value *next;
195 /* Next pointers in a linked list of values in a strongly connected component
196 of values. */
197 ipcp_value *scc_next;
198 /* Next pointers in a linked list of SCCs of values sorted topologically
199 according their sources. */
200 ipcp_value *topo_next;
201 /* A specialized node created for this value, NULL if none has been (so far)
202 created. */
203 cgraph_node *spec_node;
204 /* Depth first search number and low link for topological sorting of
205 values. */
206 int dfs, low_link;
207 /* True if this valye is currently on the topo-sort stack. */
208 bool on_stack;
210 void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx,
211 HOST_WIDE_INT offset);
214 /* Lattice describing potential values of a formal parameter of a function, or
215 a part of an aggreagate. TOP is represented by a lattice with zero values
216 and with contains_variable and bottom flags cleared. BOTTOM is represented
217 by a lattice with the bottom flag set. In that case, values and
218 contains_variable flag should be disregarded. */
220 template <typename valtype>
221 class ipcp_lattice
223 public:
224 /* The list of known values and types in this lattice. Note that values are
225 not deallocated if a lattice is set to bottom because there may be value
226 sources referencing them. */
227 ipcp_value<valtype> *values;
228 /* Number of known values and types in this lattice. */
229 int values_count;
230 /* The lattice contains a variable component (in addition to values). */
231 bool contains_variable;
232 /* The value of the lattice is bottom (i.e. variable and unusable for any
233 propagation). */
234 bool bottom;
236 inline bool is_single_const ();
237 inline bool set_to_bottom ();
238 inline bool set_contains_variable ();
239 bool add_value (valtype newval, cgraph_edge *cs,
240 ipcp_value<valtype> *src_val = NULL,
241 int src_idx = 0, HOST_WIDE_INT offset = -1);
242 void print (FILE * f, bool dump_sources, bool dump_benefits);
245 /* Lattice of tree values with an offset to describe a part of an
246 aggregate. */
248 class ipcp_agg_lattice : public ipcp_lattice<tree>
250 public:
251 /* Offset that is being described by this lattice. */
252 HOST_WIDE_INT offset;
253 /* Size so that we don't have to re-compute it every time we traverse the
254 list. Must correspond to TYPE_SIZE of all lat values. */
255 HOST_WIDE_INT size;
256 /* Next element of the linked list. */
257 struct ipcp_agg_lattice *next;
260 /* Structure containing lattices for a parameter itself and for pieces of
261 aggregates that are passed in the parameter or by a reference in a parameter
262 plus some other useful flags. */
264 class ipcp_param_lattices
266 public:
267 /* Lattice describing the value of the parameter itself. */
268 ipcp_lattice<tree> itself;
269 /* Lattice describing the the polymorphic contexts of a parameter. */
270 ipcp_lattice<ipa_polymorphic_call_context> ctxlat;
271 /* Lattices describing aggregate parts. */
272 ipcp_agg_lattice *aggs;
273 /* Alignment information. Very basic one value lattice where !known means
274 TOP and zero alignment bottom. */
275 ipa_alignment alignment;
276 /* Number of aggregate lattices */
277 int aggs_count;
278 /* True if aggregate data were passed by reference (as opposed to by
279 value). */
280 bool aggs_by_ref;
281 /* All aggregate lattices contain a variable component (in addition to
282 values). */
283 bool aggs_contain_variable;
284 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
285 for any propagation). */
286 bool aggs_bottom;
288 /* There is a virtual call based on this parameter. */
289 bool virt_call;
292 /* Allocation pools for values and their sources in ipa-cp. */
294 alloc_pool ipcp_cst_values_pool;
295 alloc_pool ipcp_poly_ctx_values_pool;
296 alloc_pool ipcp_sources_pool;
297 alloc_pool ipcp_agg_lattice_pool;
299 /* Maximal count found in program. */
301 static gcov_type max_count;
303 /* Original overall size of the program. */
305 static long overall_size, max_new_size;
307 /* Return the param lattices structure corresponding to the Ith formal
308 parameter of the function described by INFO. */
309 static inline struct ipcp_param_lattices *
310 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
312 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
313 gcc_checking_assert (!info->ipcp_orig_node);
314 gcc_checking_assert (info->lattices);
315 return &(info->lattices[i]);
318 /* Return the lattice corresponding to the scalar value of the Ith formal
319 parameter of the function described by INFO. */
320 static inline ipcp_lattice<tree> *
321 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
323 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
324 return &plats->itself;
327 /* Return the lattice corresponding to the scalar value of the Ith formal
328 parameter of the function described by INFO. */
329 static inline ipcp_lattice<ipa_polymorphic_call_context> *
330 ipa_get_poly_ctx_lat (struct ipa_node_params *info, int i)
332 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
333 return &plats->ctxlat;
336 /* Return whether LAT is a lattice with a single constant and without an
337 undefined value. */
339 template <typename valtype>
340 inline bool
341 ipcp_lattice<valtype>::is_single_const ()
343 if (bottom || contains_variable || values_count != 1)
344 return false;
345 else
346 return true;
349 /* Print V which is extracted from a value in a lattice to F. */
351 static void
352 print_ipcp_constant_value (FILE * f, tree v)
354 if (TREE_CODE (v) == ADDR_EXPR
355 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
357 fprintf (f, "& ");
358 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
360 else
361 print_generic_expr (f, v, 0);
364 /* Print V which is extracted from a value in a lattice to F. */
366 static void
367 print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
369 v.dump(f, false);
372 /* Print a lattice LAT to F. */
374 template <typename valtype>
375 void
376 ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
378 ipcp_value<valtype> *val;
379 bool prev = false;
381 if (bottom)
383 fprintf (f, "BOTTOM\n");
384 return;
387 if (!values_count && !contains_variable)
389 fprintf (f, "TOP\n");
390 return;
393 if (contains_variable)
395 fprintf (f, "VARIABLE");
396 prev = true;
397 if (dump_benefits)
398 fprintf (f, "\n");
401 for (val = values; val; val = val->next)
403 if (dump_benefits && prev)
404 fprintf (f, " ");
405 else if (!dump_benefits && prev)
406 fprintf (f, ", ");
407 else
408 prev = true;
410 print_ipcp_constant_value (f, val->value);
412 if (dump_sources)
414 ipcp_value_source<valtype> *s;
416 fprintf (f, " [from:");
417 for (s = val->sources; s; s = s->next)
418 fprintf (f, " %i(%i)", s->cs->caller->order,
419 s->cs->frequency);
420 fprintf (f, "]");
423 if (dump_benefits)
424 fprintf (f, " [loc_time: %i, loc_size: %i, "
425 "prop_time: %i, prop_size: %i]\n",
426 val->local_time_benefit, val->local_size_cost,
427 val->prop_time_benefit, val->prop_size_cost);
429 if (!dump_benefits)
430 fprintf (f, "\n");
433 /* Print all ipcp_lattices of all functions to F. */
435 static void
436 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
438 struct cgraph_node *node;
439 int i, count;
441 fprintf (f, "\nLattices:\n");
442 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
444 struct ipa_node_params *info;
446 info = IPA_NODE_REF (node);
447 fprintf (f, " Node: %s/%i:\n", node->name (),
448 node->order);
449 count = ipa_get_param_count (info);
450 for (i = 0; i < count; i++)
452 struct ipcp_agg_lattice *aglat;
453 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
454 fprintf (f, " param [%d]: ", i);
455 plats->itself.print (f, dump_sources, dump_benefits);
456 fprintf (f, " ctxs: ");
457 plats->ctxlat.print (f, dump_sources, dump_benefits);
458 if (plats->alignment.known && plats->alignment.align > 0)
459 fprintf (f, " Alignment %u, misalignment %u\n",
460 plats->alignment.align, plats->alignment.misalign);
461 else if (plats->alignment.known)
462 fprintf (f, " Alignment unusable\n");
463 else
464 fprintf (f, " Alignment unknown\n");
465 if (plats->virt_call)
466 fprintf (f, " virt_call flag set\n");
468 if (plats->aggs_bottom)
470 fprintf (f, " AGGS BOTTOM\n");
471 continue;
473 if (plats->aggs_contain_variable)
474 fprintf (f, " AGGS VARIABLE\n");
475 for (aglat = plats->aggs; aglat; aglat = aglat->next)
477 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
478 plats->aggs_by_ref ? "ref " : "", aglat->offset);
479 aglat->print (f, dump_sources, dump_benefits);
485 /* Determine whether it is at all technically possible to create clones of NODE
486 and store this information in the ipa_node_params structure associated
487 with NODE. */
489 static void
490 determine_versionability (struct cgraph_node *node)
492 const char *reason = NULL;
494 /* There are a number of generic reasons functions cannot be versioned. We
495 also cannot remove parameters if there are type attributes such as fnspec
496 present. */
497 if (node->alias || node->thunk.thunk_p)
498 reason = "alias or thunk";
499 else if (!node->local.versionable)
500 reason = "not a tree_versionable_function";
501 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
502 reason = "insufficient body availability";
503 else if (!opt_for_fn (node->decl, optimize)
504 || !opt_for_fn (node->decl, flag_ipa_cp))
505 reason = "non-optimized function";
506 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
508 /* Ideally we should clone the SIMD clones themselves and create
509 vector copies of them, so IPA-cp and SIMD clones can happily
510 coexist, but that may not be worth the effort. */
511 reason = "function has SIMD clones";
513 /* Don't clone decls local to a comdat group; it breaks and for C++
514 decloned constructors, inlining is always better anyway. */
515 else if (node->comdat_local_p ())
516 reason = "comdat-local function";
518 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
519 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
520 node->name (), node->order, reason);
522 node->local.versionable = (reason == NULL);
525 /* Return true if it is at all technically possible to create clones of a
526 NODE. */
528 static bool
529 ipcp_versionable_function_p (struct cgraph_node *node)
531 return node->local.versionable;
534 /* Structure holding accumulated information about callers of a node. */
536 struct caller_statistics
538 gcov_type count_sum;
539 int n_calls, n_hot_calls, freq_sum;
542 /* Initialize fields of STAT to zeroes. */
544 static inline void
545 init_caller_stats (struct caller_statistics *stats)
547 stats->count_sum = 0;
548 stats->n_calls = 0;
549 stats->n_hot_calls = 0;
550 stats->freq_sum = 0;
553 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
554 non-thunk incoming edges to NODE. */
556 static bool
557 gather_caller_stats (struct cgraph_node *node, void *data)
559 struct caller_statistics *stats = (struct caller_statistics *) data;
560 struct cgraph_edge *cs;
562 for (cs = node->callers; cs; cs = cs->next_caller)
563 if (!cs->caller->thunk.thunk_p)
565 stats->count_sum += cs->count;
566 stats->freq_sum += cs->frequency;
567 stats->n_calls++;
568 if (cs->maybe_hot_p ())
569 stats->n_hot_calls ++;
571 return false;
575 /* Return true if this NODE is viable candidate for cloning. */
577 static bool
578 ipcp_cloning_candidate_p (struct cgraph_node *node)
580 struct caller_statistics stats;
582 gcc_checking_assert (node->has_gimple_body_p ());
584 if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
586 if (dump_file)
587 fprintf (dump_file, "Not considering %s for cloning; "
588 "-fipa-cp-clone disabled.\n",
589 node->name ());
590 return false;
593 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
595 if (dump_file)
596 fprintf (dump_file, "Not considering %s for cloning; "
597 "optimizing it for size.\n",
598 node->name ());
599 return false;
602 init_caller_stats (&stats);
603 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
605 if (inline_summaries->get (node)->self_size < stats.n_calls)
607 if (dump_file)
608 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
609 node->name ());
610 return true;
613 /* When profile is available and function is hot, propagate into it even if
614 calls seems cold; constant propagation can improve function's speed
615 significantly. */
616 if (max_count)
618 if (stats.count_sum > node->count * 90 / 100)
620 if (dump_file)
621 fprintf (dump_file, "Considering %s for cloning; "
622 "usually called directly.\n",
623 node->name ());
624 return true;
627 if (!stats.n_hot_calls)
629 if (dump_file)
630 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
631 node->name ());
632 return false;
634 if (dump_file)
635 fprintf (dump_file, "Considering %s for cloning.\n",
636 node->name ());
637 return true;
640 template <typename valtype>
641 class value_topo_info
643 public:
644 /* Head of the linked list of topologically sorted values. */
645 ipcp_value<valtype> *values_topo;
646 /* Stack for creating SCCs, represented by a linked list too. */
647 ipcp_value<valtype> *stack;
648 /* Counter driving the algorithm in add_val_to_toposort. */
649 int dfs_counter;
651 value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
653 void add_val (ipcp_value<valtype> *cur_val);
654 void propagate_effects ();
657 /* Arrays representing a topological ordering of call graph nodes and a stack
658 of nodes used during constant propagation and also data required to perform
659 topological sort of values and propagation of benefits in the determined
660 order. */
662 class ipa_topo_info
664 public:
665 /* Array with obtained topological order of cgraph nodes. */
666 struct cgraph_node **order;
667 /* Stack of cgraph nodes used during propagation within SCC until all values
668 in the SCC stabilize. */
669 struct cgraph_node **stack;
670 int nnodes, stack_top;
672 value_topo_info<tree> constants;
673 value_topo_info<ipa_polymorphic_call_context> contexts;
675 ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
676 constants ()
680 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
682 static void
683 build_toporder_info (struct ipa_topo_info *topo)
685 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
686 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
688 gcc_checking_assert (topo->stack_top == 0);
689 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
692 /* Free information about strongly connected components and the arrays in
693 TOPO. */
695 static void
696 free_toporder_info (struct ipa_topo_info *topo)
698 ipa_free_postorder_info ();
699 free (topo->order);
700 free (topo->stack);
703 /* Add NODE to the stack in TOPO, unless it is already there. */
705 static inline void
706 push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node)
708 struct ipa_node_params *info = IPA_NODE_REF (node);
709 if (info->node_enqueued)
710 return;
711 info->node_enqueued = 1;
712 topo->stack[topo->stack_top++] = node;
715 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
716 is empty. */
718 static struct cgraph_node *
719 pop_node_from_stack (struct ipa_topo_info *topo)
721 if (topo->stack_top)
723 struct cgraph_node *node;
724 topo->stack_top--;
725 node = topo->stack[topo->stack_top];
726 IPA_NODE_REF (node)->node_enqueued = 0;
727 return node;
729 else
730 return NULL;
733 /* Set lattice LAT to bottom and return true if it previously was not set as
734 such. */
736 template <typename valtype>
737 inline bool
738 ipcp_lattice<valtype>::set_to_bottom ()
740 bool ret = !bottom;
741 bottom = true;
742 return ret;
745 /* Mark lattice as containing an unknown value and return true if it previously
746 was not marked as such. */
748 template <typename valtype>
749 inline bool
750 ipcp_lattice<valtype>::set_contains_variable ()
752 bool ret = !contains_variable;
753 contains_variable = true;
754 return ret;
757 /* Set all aggegate lattices in PLATS to bottom and return true if they were
758 not previously set as such. */
760 static inline bool
761 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
763 bool ret = !plats->aggs_bottom;
764 plats->aggs_bottom = true;
765 return ret;
768 /* Mark all aggegate lattices in PLATS as containing an unknown value and
769 return true if they were not previously marked as such. */
771 static inline bool
772 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
774 bool ret = !plats->aggs_contain_variable;
775 plats->aggs_contain_variable = true;
776 return ret;
779 /* Return true if alignment information in PLATS is known to be unusable. */
781 static inline bool
782 alignment_bottom_p (ipcp_param_lattices *plats)
784 return plats->alignment.known && (plats->alignment.align == 0);
787 /* Set alignment information in PLATS to unusable. Return true if it
788 previously was usable or unknown. */
790 static inline bool
791 set_alignment_to_bottom (ipcp_param_lattices *plats)
793 if (alignment_bottom_p (plats))
794 return false;
795 plats->alignment.known = true;
796 plats->alignment.align = 0;
797 return true;
800 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
801 return true is any of them has not been marked as such so far. */
803 static inline bool
804 set_all_contains_variable (struct ipcp_param_lattices *plats)
806 bool ret;
807 ret = plats->itself.set_contains_variable ();
808 ret |= plats->ctxlat.set_contains_variable ();
809 ret |= set_agg_lats_contain_variable (plats);
810 ret |= set_alignment_to_bottom (plats);
811 return ret;
814 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
815 points to by the number of callers to NODE. */
817 static bool
818 count_callers (cgraph_node *node, void *data)
820 int *caller_count = (int *) data;
822 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
823 /* Local thunks can be handled transparently, but if the thunk can not
824 be optimized out, count it as a real use. */
825 if (!cs->caller->thunk.thunk_p || !cs->caller->local.local)
826 ++*caller_count;
827 return false;
830 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
831 the one caller of some other node. Set the caller's corresponding flag. */
833 static bool
834 set_single_call_flag (cgraph_node *node, void *)
836 cgraph_edge *cs = node->callers;
837 /* Local thunks can be handled transparently, skip them. */
838 while (cs && cs->caller->thunk.thunk_p && cs->caller->local.local)
839 cs = cs->next_caller;
840 if (cs)
842 gcc_assert (!cs->next_caller);
843 IPA_NODE_REF (cs->caller)->node_calling_single_call = true;
844 return true;
846 return false;
849 /* Initialize ipcp_lattices. */
851 static void
852 initialize_node_lattices (struct cgraph_node *node)
854 struct ipa_node_params *info = IPA_NODE_REF (node);
855 struct cgraph_edge *ie;
856 bool disable = false, variable = false;
857 int i;
859 gcc_checking_assert (node->has_gimple_body_p ());
860 if (cgraph_local_p (node))
862 int caller_count = 0;
863 node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
864 true);
865 gcc_checking_assert (caller_count > 0);
866 if (caller_count == 1)
867 node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
868 NULL, true);
870 else
872 /* When cloning is allowed, we can assume that externally visible
873 functions are not called. We will compensate this by cloning
874 later. */
875 if (ipcp_versionable_function_p (node)
876 && ipcp_cloning_candidate_p (node))
877 variable = true;
878 else
879 disable = true;
882 if (disable || variable)
884 for (i = 0; i < ipa_get_param_count (info) ; i++)
886 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
887 if (disable)
889 plats->itself.set_to_bottom ();
890 plats->ctxlat.set_to_bottom ();
891 set_agg_lats_to_bottom (plats);
892 set_alignment_to_bottom (plats);
894 else
895 set_all_contains_variable (plats);
897 if (dump_file && (dump_flags & TDF_DETAILS)
898 && !node->alias && !node->thunk.thunk_p)
899 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
900 node->name (), node->order,
901 disable ? "BOTTOM" : "VARIABLE");
904 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
905 if (ie->indirect_info->polymorphic
906 && ie->indirect_info->param_index >= 0)
908 gcc_checking_assert (ie->indirect_info->param_index >= 0);
909 ipa_get_parm_lattices (info,
910 ie->indirect_info->param_index)->virt_call = 1;
914 /* Return the result of a (possibly arithmetic) pass through jump function
915 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
916 determined or be considered an interprocedural invariant. */
918 static tree
919 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
921 tree restype, res;
923 gcc_checking_assert (is_gimple_ip_invariant (input));
924 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
925 return input;
927 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
928 == tcc_comparison)
929 restype = boolean_type_node;
930 else
931 restype = TREE_TYPE (input);
932 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
933 input, ipa_get_jf_pass_through_operand (jfunc));
935 if (res && !is_gimple_ip_invariant (res))
936 return NULL_TREE;
938 return res;
941 /* Return the result of an ancestor jump function JFUNC on the constant value
942 INPUT. Return NULL_TREE if that cannot be determined. */
944 static tree
945 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
947 gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
948 if (TREE_CODE (input) == ADDR_EXPR)
950 tree t = TREE_OPERAND (input, 0);
951 t = build_ref_for_offset (EXPR_LOCATION (t), t,
952 ipa_get_jf_ancestor_offset (jfunc),
953 ptr_type_node, NULL, false);
954 return build_fold_addr_expr (t);
956 else
957 return NULL_TREE;
960 /* Determine whether JFUNC evaluates to a single known constant value and if
961 so, return it. Otherwise return NULL. INFO describes the caller node or
962 the one it is inlined to, so that pass-through jump functions can be
963 evaluated. */
965 tree
966 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
968 if (jfunc->type == IPA_JF_CONST)
969 return ipa_get_jf_constant (jfunc);
970 else if (jfunc->type == IPA_JF_PASS_THROUGH
971 || jfunc->type == IPA_JF_ANCESTOR)
973 tree input;
974 int idx;
976 if (jfunc->type == IPA_JF_PASS_THROUGH)
977 idx = ipa_get_jf_pass_through_formal_id (jfunc);
978 else
979 idx = ipa_get_jf_ancestor_formal_id (jfunc);
981 if (info->ipcp_orig_node)
982 input = info->known_csts[idx];
983 else
985 ipcp_lattice<tree> *lat;
987 if (!info->lattices
988 || idx >= ipa_get_param_count (info))
989 return NULL_TREE;
990 lat = ipa_get_scalar_lat (info, idx);
991 if (!lat->is_single_const ())
992 return NULL_TREE;
993 input = lat->values->value;
996 if (!input)
997 return NULL_TREE;
999 if (jfunc->type == IPA_JF_PASS_THROUGH)
1000 return ipa_get_jf_pass_through_result (jfunc, input);
1001 else
1002 return ipa_get_jf_ancestor_result (jfunc, input);
1004 else
1005 return NULL_TREE;
1008 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1009 that INFO describes the caller node or the one it is inlined to, CS is the
1010 call graph edge corresponding to JFUNC and CSIDX index of the described
1011 parameter. */
1013 ipa_polymorphic_call_context
1014 ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1015 ipa_jump_func *jfunc)
1017 ipa_edge_args *args = IPA_EDGE_REF (cs);
1018 ipa_polymorphic_call_context ctx;
1019 ipa_polymorphic_call_context *edge_ctx
1020 = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1022 if (edge_ctx && !edge_ctx->useless_p ())
1023 ctx = *edge_ctx;
1025 if (jfunc->type == IPA_JF_PASS_THROUGH
1026 || jfunc->type == IPA_JF_ANCESTOR)
1028 ipa_polymorphic_call_context srcctx;
1029 int srcidx;
1030 bool type_preserved = true;
1031 if (jfunc->type == IPA_JF_PASS_THROUGH)
1033 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1034 return ctx;
1035 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1036 srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1038 else
1040 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1041 srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1043 if (info->ipcp_orig_node)
1045 if (info->known_contexts.exists ())
1046 srcctx = info->known_contexts[srcidx];
1048 else
1050 if (!info->lattices
1051 || srcidx >= ipa_get_param_count (info))
1052 return ctx;
1053 ipcp_lattice<ipa_polymorphic_call_context> *lat;
1054 lat = ipa_get_poly_ctx_lat (info, srcidx);
1055 if (!lat->is_single_const ())
1056 return ctx;
1057 srcctx = lat->values->value;
1059 if (srcctx.useless_p ())
1060 return ctx;
1061 if (jfunc->type == IPA_JF_ANCESTOR)
1062 srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1063 if (!type_preserved)
1064 srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1065 srcctx.combine_with (ctx);
1066 return srcctx;
1069 return ctx;
1072 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1073 bottom, not containing a variable component and without any known value at
1074 the same time. */
1076 DEBUG_FUNCTION void
1077 ipcp_verify_propagated_values (void)
1079 struct cgraph_node *node;
1081 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1083 struct ipa_node_params *info = IPA_NODE_REF (node);
1084 int i, count = ipa_get_param_count (info);
1086 for (i = 0; i < count; i++)
1088 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
1090 if (!lat->bottom
1091 && !lat->contains_variable
1092 && lat->values_count == 0)
1094 if (dump_file)
1096 symtab_node::dump_table (dump_file);
1097 fprintf (dump_file, "\nIPA lattices after constant "
1098 "propagation, before gcc_unreachable:\n");
1099 print_all_lattices (dump_file, true, false);
1102 gcc_unreachable ();
1108 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1110 static bool
1111 values_equal_for_ipcp_p (tree x, tree y)
1113 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
1115 if (x == y)
1116 return true;
1118 if (TREE_CODE (x) == ADDR_EXPR
1119 && TREE_CODE (y) == ADDR_EXPR
1120 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
1121 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
1122 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
1123 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
1124 else
1125 return operand_equal_p (x, y, 0);
1128 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1130 static bool
1131 values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
1132 ipa_polymorphic_call_context y)
1134 return x.equal_to (y);
1138 /* Add a new value source to the value represented by THIS, marking that a
1139 value comes from edge CS and (if the underlying jump function is a
1140 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1141 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1142 scalar value of the parameter itself or the offset within an aggregate. */
1144 template <typename valtype>
1145 void
1146 ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
1147 int src_idx, HOST_WIDE_INT offset)
1149 ipcp_value_source<valtype> *src;
1151 src = new (pool_alloc (ipcp_sources_pool)) ipcp_value_source<valtype>;
1152 src->offset = offset;
1153 src->cs = cs;
1154 src->val = src_val;
1155 src->index = src_idx;
1157 src->next = sources;
1158 sources = src;
1161 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1162 SOURCE and clear all other fields. */
1164 static ipcp_value<tree> *
1165 allocate_and_init_ipcp_value (tree source)
1167 ipcp_value<tree> *val;
1169 val = new (pool_alloc (ipcp_cst_values_pool)) ipcp_value<tree>;
1170 memset (val, 0, sizeof (*val));
1171 val->value = source;
1172 return val;
1175 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1176 value to SOURCE and clear all other fields. */
1178 static ipcp_value<ipa_polymorphic_call_context> *
1179 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source)
1181 ipcp_value<ipa_polymorphic_call_context> *val;
1183 val = new (pool_alloc (ipcp_poly_ctx_values_pool))
1184 ipcp_value<ipa_polymorphic_call_context>;
1185 memset (val, 0, sizeof (*val));
1186 val->value = source;
1187 return val;
1190 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1191 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1192 meaning. OFFSET -1 means the source is scalar and not a part of an
1193 aggregate. */
1195 template <typename valtype>
1196 bool
1197 ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
1198 ipcp_value<valtype> *src_val,
1199 int src_idx, HOST_WIDE_INT offset)
1201 ipcp_value<valtype> *val;
1203 if (bottom)
1204 return false;
1206 for (val = values; val; val = val->next)
1207 if (values_equal_for_ipcp_p (val->value, newval))
1209 if (ipa_edge_within_scc (cs))
1211 ipcp_value_source<valtype> *s;
1212 for (s = val->sources; s ; s = s->next)
1213 if (s->cs == cs)
1214 break;
1215 if (s)
1216 return false;
1219 val->add_source (cs, src_val, src_idx, offset);
1220 return false;
1223 if (values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
1225 /* We can only free sources, not the values themselves, because sources
1226 of other values in this this SCC might point to them. */
1227 for (val = values; val; val = val->next)
1229 while (val->sources)
1231 ipcp_value_source<valtype> *src = val->sources;
1232 val->sources = src->next;
1233 pool_free (ipcp_sources_pool, src);
1237 values = NULL;
1238 return set_to_bottom ();
1241 values_count++;
1242 val = allocate_and_init_ipcp_value (newval);
1243 val->add_source (cs, src_val, src_idx, offset);
1244 val->next = values;
1245 values = val;
1246 return true;
1249 /* Propagate values through a pass-through jump function JFUNC associated with
1250 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1251 is the index of the source parameter. */
1253 static bool
1254 propagate_vals_accross_pass_through (cgraph_edge *cs,
1255 ipa_jump_func *jfunc,
1256 ipcp_lattice<tree> *src_lat,
1257 ipcp_lattice<tree> *dest_lat,
1258 int src_idx)
1260 ipcp_value<tree> *src_val;
1261 bool ret = false;
1263 /* Do not create new values when propagating within an SCC because if there
1264 are arithmetic functions with circular dependencies, there is infinite
1265 number of them and we would just make lattices bottom. */
1266 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1267 && ipa_edge_within_scc (cs))
1268 ret = dest_lat->set_contains_variable ();
1269 else
1270 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1272 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1274 if (cstval)
1275 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx);
1276 else
1277 ret |= dest_lat->set_contains_variable ();
1280 return ret;
1283 /* Propagate values through an ancestor jump function JFUNC associated with
1284 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1285 is the index of the source parameter. */
1287 static bool
1288 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1289 struct ipa_jump_func *jfunc,
1290 ipcp_lattice<tree> *src_lat,
1291 ipcp_lattice<tree> *dest_lat,
1292 int src_idx)
1294 ipcp_value<tree> *src_val;
1295 bool ret = false;
1297 if (ipa_edge_within_scc (cs))
1298 return dest_lat->set_contains_variable ();
1300 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1302 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1304 if (t)
1305 ret |= dest_lat->add_value (t, cs, src_val, src_idx);
1306 else
1307 ret |= dest_lat->set_contains_variable ();
1310 return ret;
1313 /* Propagate scalar values across jump function JFUNC that is associated with
1314 edge CS and put the values into DEST_LAT. */
1316 static bool
1317 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1318 struct ipa_jump_func *jfunc,
1319 ipcp_lattice<tree> *dest_lat)
1321 if (dest_lat->bottom)
1322 return false;
1324 if (jfunc->type == IPA_JF_CONST)
1326 tree val = ipa_get_jf_constant (jfunc);
1327 return dest_lat->add_value (val, cs, NULL, 0);
1329 else if (jfunc->type == IPA_JF_PASS_THROUGH
1330 || jfunc->type == IPA_JF_ANCESTOR)
1332 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1333 ipcp_lattice<tree> *src_lat;
1334 int src_idx;
1335 bool ret;
1337 if (jfunc->type == IPA_JF_PASS_THROUGH)
1338 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1339 else
1340 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1342 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1343 if (src_lat->bottom)
1344 return dest_lat->set_contains_variable ();
1346 /* If we would need to clone the caller and cannot, do not propagate. */
1347 if (!ipcp_versionable_function_p (cs->caller)
1348 && (src_lat->contains_variable
1349 || (src_lat->values_count > 1)))
1350 return dest_lat->set_contains_variable ();
1352 if (jfunc->type == IPA_JF_PASS_THROUGH)
1353 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1354 dest_lat, src_idx);
1355 else
1356 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1357 src_idx);
1359 if (src_lat->contains_variable)
1360 ret |= dest_lat->set_contains_variable ();
1362 return ret;
1365 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1366 use it for indirect inlining), we should propagate them too. */
1367 return dest_lat->set_contains_variable ();
1370 /* Propagate scalar values across jump function JFUNC that is associated with
1371 edge CS and describes argument IDX and put the values into DEST_LAT. */
1373 static bool
1374 propagate_context_accross_jump_function (cgraph_edge *cs,
1375 ipa_jump_func *jfunc, int idx,
1376 ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
1378 ipa_edge_args *args = IPA_EDGE_REF (cs);
1379 if (dest_lat->bottom)
1380 return false;
1381 bool ret = false;
1382 bool added_sth = false;
1383 bool type_preserved = true;
1385 ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
1386 = ipa_get_ith_polymorhic_call_context (args, idx);
1388 if (edge_ctx_ptr)
1389 edge_ctx = *edge_ctx_ptr;
1391 if (jfunc->type == IPA_JF_PASS_THROUGH
1392 || jfunc->type == IPA_JF_ANCESTOR)
1394 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1395 int src_idx;
1396 ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
1398 /* TODO: Once we figure out how to propagate speculations, it will
1399 probably be a good idea to switch to speculation if type_preserved is
1400 not set instead of punting. */
1401 if (jfunc->type == IPA_JF_PASS_THROUGH)
1403 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1404 goto prop_fail;
1405 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1406 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1408 else
1410 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1411 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1414 src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
1415 /* If we would need to clone the caller and cannot, do not propagate. */
1416 if (!ipcp_versionable_function_p (cs->caller)
1417 && (src_lat->contains_variable
1418 || (src_lat->values_count > 1)))
1419 goto prop_fail;
1421 ipcp_value<ipa_polymorphic_call_context> *src_val;
1422 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1424 ipa_polymorphic_call_context cur = src_val->value;
1426 if (!type_preserved)
1427 cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1428 if (jfunc->type == IPA_JF_ANCESTOR)
1429 cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1430 /* TODO: In cases we know how the context is going to be used,
1431 we can improve the result by passing proper OTR_TYPE. */
1432 cur.combine_with (edge_ctx);
1433 if (!cur.useless_p ())
1435 if (src_lat->contains_variable
1436 && !edge_ctx.equal_to (cur))
1437 ret |= dest_lat->set_contains_variable ();
1438 ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
1439 added_sth = true;
1445 prop_fail:
1446 if (!added_sth)
1448 if (!edge_ctx.useless_p ())
1449 ret |= dest_lat->add_value (edge_ctx, cs);
1450 else
1451 ret |= dest_lat->set_contains_variable ();
1454 return ret;
1457 /* Propagate alignments across jump function JFUNC that is associated with
1458 edge CS and update DEST_LAT accordingly. */
1460 static bool
1461 propagate_alignment_accross_jump_function (struct cgraph_edge *cs,
1462 struct ipa_jump_func *jfunc,
1463 struct ipcp_param_lattices *dest_lat)
1465 if (alignment_bottom_p (dest_lat))
1466 return false;
1468 ipa_alignment cur;
1469 cur.known = false;
1470 if (jfunc->alignment.known)
1471 cur = jfunc->alignment;
1472 else if (jfunc->type == IPA_JF_PASS_THROUGH
1473 || jfunc->type == IPA_JF_ANCESTOR)
1475 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1476 struct ipcp_param_lattices *src_lats;
1477 HOST_WIDE_INT offset = 0;
1478 int src_idx;
1480 if (jfunc->type == IPA_JF_PASS_THROUGH)
1482 enum tree_code op = ipa_get_jf_pass_through_operation (jfunc);
1483 if (op != NOP_EXPR)
1485 if (op != POINTER_PLUS_EXPR
1486 && op != PLUS_EXPR)
1487 goto prop_fail;
1488 tree operand = ipa_get_jf_pass_through_operand (jfunc);
1489 if (!tree_fits_shwi_p (operand))
1490 goto prop_fail;
1491 offset = tree_to_shwi (operand);
1493 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1495 else
1497 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1498 offset = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;;
1501 src_lats = ipa_get_parm_lattices (caller_info, src_idx);
1502 if (!src_lats->alignment.known
1503 || alignment_bottom_p (src_lats))
1504 goto prop_fail;
1506 cur = src_lats->alignment;
1507 cur.misalign = (cur.misalign + offset) % cur.align;
1510 if (cur.known)
1512 if (!dest_lat->alignment.known)
1514 dest_lat->alignment = cur;
1515 return true;
1517 else if (dest_lat->alignment.align == cur.align
1518 && dest_lat->alignment.misalign == cur.misalign)
1519 return false;
1522 prop_fail:
1523 set_alignment_to_bottom (dest_lat);
1524 return true;
1527 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1528 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1529 other cases, return false). If there are no aggregate items, set
1530 aggs_by_ref to NEW_AGGS_BY_REF. */
1532 static bool
1533 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1534 bool new_aggs_by_ref)
1536 if (dest_plats->aggs)
1538 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1540 set_agg_lats_to_bottom (dest_plats);
1541 return true;
1544 else
1545 dest_plats->aggs_by_ref = new_aggs_by_ref;
1546 return false;
1549 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1550 already existing lattice for the given OFFSET and SIZE, marking all skipped
1551 lattices as containing variable and checking for overlaps. If there is no
1552 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1553 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1554 unless there are too many already. If there are two many, return false. If
1555 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1556 skipped lattices were newly marked as containing variable, set *CHANGE to
1557 true. */
1559 static bool
1560 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1561 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1562 struct ipcp_agg_lattice ***aglat,
1563 bool pre_existing, bool *change)
1565 gcc_checking_assert (offset >= 0);
1567 while (**aglat && (**aglat)->offset < offset)
1569 if ((**aglat)->offset + (**aglat)->size > offset)
1571 set_agg_lats_to_bottom (dest_plats);
1572 return false;
1574 *change |= (**aglat)->set_contains_variable ();
1575 *aglat = &(**aglat)->next;
1578 if (**aglat && (**aglat)->offset == offset)
1580 if ((**aglat)->size != val_size
1581 || ((**aglat)->next
1582 && (**aglat)->next->offset < offset + val_size))
1584 set_agg_lats_to_bottom (dest_plats);
1585 return false;
1587 gcc_checking_assert (!(**aglat)->next
1588 || (**aglat)->next->offset >= offset + val_size);
1589 return true;
1591 else
1593 struct ipcp_agg_lattice *new_al;
1595 if (**aglat && (**aglat)->offset < offset + val_size)
1597 set_agg_lats_to_bottom (dest_plats);
1598 return false;
1600 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1601 return false;
1602 dest_plats->aggs_count++;
1603 new_al = (struct ipcp_agg_lattice *) pool_alloc (ipcp_agg_lattice_pool);
1604 memset (new_al, 0, sizeof (*new_al));
1606 new_al->offset = offset;
1607 new_al->size = val_size;
1608 new_al->contains_variable = pre_existing;
1610 new_al->next = **aglat;
1611 **aglat = new_al;
1612 return true;
1616 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1617 containing an unknown value. */
1619 static bool
1620 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1622 bool ret = false;
1623 while (aglat)
1625 ret |= aglat->set_contains_variable ();
1626 aglat = aglat->next;
1628 return ret;
1631 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1632 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1633 parameter used for lattice value sources. Return true if DEST_PLATS changed
1634 in any way. */
1636 static bool
1637 merge_aggregate_lattices (struct cgraph_edge *cs,
1638 struct ipcp_param_lattices *dest_plats,
1639 struct ipcp_param_lattices *src_plats,
1640 int src_idx, HOST_WIDE_INT offset_delta)
1642 bool pre_existing = dest_plats->aggs != NULL;
1643 struct ipcp_agg_lattice **dst_aglat;
1644 bool ret = false;
1646 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1647 return true;
1648 if (src_plats->aggs_bottom)
1649 return set_agg_lats_contain_variable (dest_plats);
1650 if (src_plats->aggs_contain_variable)
1651 ret |= set_agg_lats_contain_variable (dest_plats);
1652 dst_aglat = &dest_plats->aggs;
1654 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1655 src_aglat;
1656 src_aglat = src_aglat->next)
1658 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1660 if (new_offset < 0)
1661 continue;
1662 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1663 &dst_aglat, pre_existing, &ret))
1665 struct ipcp_agg_lattice *new_al = *dst_aglat;
1667 dst_aglat = &(*dst_aglat)->next;
1668 if (src_aglat->bottom)
1670 ret |= new_al->set_contains_variable ();
1671 continue;
1673 if (src_aglat->contains_variable)
1674 ret |= new_al->set_contains_variable ();
1675 for (ipcp_value<tree> *val = src_aglat->values;
1676 val;
1677 val = val->next)
1678 ret |= new_al->add_value (val->value, cs, val, src_idx,
1679 src_aglat->offset);
1681 else if (dest_plats->aggs_bottom)
1682 return true;
1684 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1685 return ret;
1688 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1689 pass-through JFUNC and if so, whether it has conform and conforms to the
1690 rules about propagating values passed by reference. */
1692 static bool
1693 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1694 struct ipa_jump_func *jfunc)
1696 return src_plats->aggs
1697 && (!src_plats->aggs_by_ref
1698 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1701 /* Propagate scalar values across jump function JFUNC that is associated with
1702 edge CS and put the values into DEST_LAT. */
1704 static bool
1705 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1706 struct ipa_jump_func *jfunc,
1707 struct ipcp_param_lattices *dest_plats)
1709 bool ret = false;
1711 if (dest_plats->aggs_bottom)
1712 return false;
1714 if (jfunc->type == IPA_JF_PASS_THROUGH
1715 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1717 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1718 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1719 struct ipcp_param_lattices *src_plats;
1721 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1722 if (agg_pass_through_permissible_p (src_plats, jfunc))
1724 /* Currently we do not produce clobber aggregate jump
1725 functions, replace with merging when we do. */
1726 gcc_assert (!jfunc->agg.items);
1727 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1728 src_idx, 0);
1730 else
1731 ret |= set_agg_lats_contain_variable (dest_plats);
1733 else if (jfunc->type == IPA_JF_ANCESTOR
1734 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1736 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1737 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1738 struct ipcp_param_lattices *src_plats;
1740 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1741 if (src_plats->aggs && src_plats->aggs_by_ref)
1743 /* Currently we do not produce clobber aggregate jump
1744 functions, replace with merging when we do. */
1745 gcc_assert (!jfunc->agg.items);
1746 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1747 ipa_get_jf_ancestor_offset (jfunc));
1749 else if (!src_plats->aggs_by_ref)
1750 ret |= set_agg_lats_to_bottom (dest_plats);
1751 else
1752 ret |= set_agg_lats_contain_variable (dest_plats);
1754 else if (jfunc->agg.items)
1756 bool pre_existing = dest_plats->aggs != NULL;
1757 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1758 struct ipa_agg_jf_item *item;
1759 int i;
1761 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1762 return true;
1764 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1766 HOST_WIDE_INT val_size;
1768 if (item->offset < 0)
1769 continue;
1770 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1771 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1773 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1774 &aglat, pre_existing, &ret))
1776 ret |= (*aglat)->add_value (item->value, cs, NULL, 0, 0);
1777 aglat = &(*aglat)->next;
1779 else if (dest_plats->aggs_bottom)
1780 return true;
1783 ret |= set_chain_of_aglats_contains_variable (*aglat);
1785 else
1786 ret |= set_agg_lats_contain_variable (dest_plats);
1788 return ret;
1791 /* Propagate constants from the caller to the callee of CS. INFO describes the
1792 caller. */
1794 static bool
1795 propagate_constants_accross_call (struct cgraph_edge *cs)
1797 struct ipa_node_params *callee_info;
1798 enum availability availability;
1799 struct cgraph_node *callee, *alias_or_thunk;
1800 struct ipa_edge_args *args;
1801 bool ret = false;
1802 int i, args_count, parms_count;
1804 callee = cs->callee->function_symbol (&availability);
1805 if (!callee->definition)
1806 return false;
1807 gcc_checking_assert (callee->has_gimple_body_p ());
1808 callee_info = IPA_NODE_REF (callee);
1810 args = IPA_EDGE_REF (cs);
1811 args_count = ipa_get_cs_argument_count (args);
1812 parms_count = ipa_get_param_count (callee_info);
1813 if (parms_count == 0)
1814 return false;
1816 /* No propagation through instrumentation thunks is available yet.
1817 It should be possible with proper mapping of call args and
1818 instrumented callee params in the propagation loop below. But
1819 this case mostly occurs when legacy code calls instrumented code
1820 and it is not a primary target for optimizations.
1821 We detect instrumentation thunks in aliases and thunks chain by
1822 checking instrumentation_clone flag for chain source and target.
1823 Going through instrumentation thunks we always have it changed
1824 from 0 to 1 and all other nodes do not change it. */
1825 if (!cs->callee->instrumentation_clone
1826 && callee->instrumentation_clone)
1828 for (i = 0; i < parms_count; i++)
1829 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1830 i));
1831 return ret;
1834 /* If this call goes through a thunk we must not propagate to the first (0th)
1835 parameter. However, we might need to uncover a thunk from below a series
1836 of aliases first. */
1837 alias_or_thunk = cs->callee;
1838 while (alias_or_thunk->alias)
1839 alias_or_thunk = alias_or_thunk->get_alias_target ();
1840 if (alias_or_thunk->thunk.thunk_p)
1842 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1843 0));
1844 i = 1;
1846 else
1847 i = 0;
1849 for (; (i < args_count) && (i < parms_count); i++)
1851 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1852 struct ipcp_param_lattices *dest_plats;
1854 dest_plats = ipa_get_parm_lattices (callee_info, i);
1855 if (availability == AVAIL_INTERPOSABLE)
1856 ret |= set_all_contains_variable (dest_plats);
1857 else
1859 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1860 &dest_plats->itself);
1861 ret |= propagate_context_accross_jump_function (cs, jump_func, i,
1862 &dest_plats->ctxlat);
1863 ret |= propagate_alignment_accross_jump_function (cs, jump_func,
1864 dest_plats);
1865 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1866 dest_plats);
1869 for (; i < parms_count; i++)
1870 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1872 return ret;
1875 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1876 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
1877 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
1879 static tree
1880 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1881 vec<tree> known_csts,
1882 vec<ipa_polymorphic_call_context> known_contexts,
1883 vec<ipa_agg_jump_function_p> known_aggs,
1884 struct ipa_agg_replacement_value *agg_reps,
1885 bool *speculative)
1887 int param_index = ie->indirect_info->param_index;
1888 HOST_WIDE_INT anc_offset;
1889 tree t;
1890 tree target = NULL;
1892 *speculative = false;
1894 if (param_index == -1
1895 || known_csts.length () <= (unsigned int) param_index)
1896 return NULL_TREE;
1898 if (!ie->indirect_info->polymorphic)
1900 tree t;
1902 if (ie->indirect_info->agg_contents)
1904 if (agg_reps)
1906 t = NULL;
1907 while (agg_reps)
1909 if (agg_reps->index == param_index
1910 && agg_reps->offset == ie->indirect_info->offset
1911 && agg_reps->by_ref == ie->indirect_info->by_ref)
1913 t = agg_reps->value;
1914 break;
1916 agg_reps = agg_reps->next;
1919 else if (known_aggs.length () > (unsigned int) param_index)
1921 struct ipa_agg_jump_function *agg;
1922 agg = known_aggs[param_index];
1923 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1924 ie->indirect_info->by_ref);
1926 else
1927 t = NULL;
1929 else
1930 t = known_csts[param_index];
1932 if (t &&
1933 TREE_CODE (t) == ADDR_EXPR
1934 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1935 return TREE_OPERAND (t, 0);
1936 else
1937 return NULL_TREE;
1940 if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
1941 return NULL_TREE;
1943 gcc_assert (!ie->indirect_info->agg_contents);
1944 anc_offset = ie->indirect_info->offset;
1946 t = NULL;
1948 /* Try to work out value of virtual table pointer value in replacemnets. */
1949 if (!t && agg_reps && !ie->indirect_info->by_ref)
1951 while (agg_reps)
1953 if (agg_reps->index == param_index
1954 && agg_reps->offset == ie->indirect_info->offset
1955 && agg_reps->by_ref)
1957 t = agg_reps->value;
1958 break;
1960 agg_reps = agg_reps->next;
1964 /* Try to work out value of virtual table pointer value in known
1965 aggregate values. */
1966 if (!t && known_aggs.length () > (unsigned int) param_index
1967 && !ie->indirect_info->by_ref)
1969 struct ipa_agg_jump_function *agg;
1970 agg = known_aggs[param_index];
1971 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1972 true);
1975 /* If we found the virtual table pointer, lookup the target. */
1976 if (t)
1978 tree vtable;
1979 unsigned HOST_WIDE_INT offset;
1980 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
1982 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
1983 vtable, offset);
1984 if (target)
1986 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
1987 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
1988 || !possible_polymorphic_call_target_p
1989 (ie, cgraph_node::get (target)))
1990 target = ipa_impossible_devirt_target (ie, target);
1991 *speculative = ie->indirect_info->vptr_changed;
1992 if (!*speculative)
1993 return target;
1998 /* Do we know the constant value of pointer? */
1999 if (!t)
2000 t = known_csts[param_index];
2002 gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
2004 ipa_polymorphic_call_context context;
2005 if (known_contexts.length () > (unsigned int) param_index)
2007 context = known_contexts[param_index];
2008 context.offset_by (anc_offset);
2009 if (ie->indirect_info->vptr_changed)
2010 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2011 ie->indirect_info->otr_type);
2012 if (t)
2014 ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
2015 (t, ie->indirect_info->otr_type, anc_offset);
2016 if (!ctx2.useless_p ())
2017 context.combine_with (ctx2, ie->indirect_info->otr_type);
2020 else if (t)
2022 context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
2023 anc_offset);
2024 if (ie->indirect_info->vptr_changed)
2025 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2026 ie->indirect_info->otr_type);
2028 else
2029 return NULL_TREE;
2031 vec <cgraph_node *>targets;
2032 bool final;
2034 targets = possible_polymorphic_call_targets
2035 (ie->indirect_info->otr_type,
2036 ie->indirect_info->otr_token,
2037 context, &final);
2038 if (!final || targets.length () > 1)
2040 struct cgraph_node *node;
2041 if (*speculative)
2042 return target;
2043 if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
2044 || ie->speculative || !ie->maybe_hot_p ())
2045 return NULL;
2046 node = try_speculative_devirtualization (ie->indirect_info->otr_type,
2047 ie->indirect_info->otr_token,
2048 context);
2049 if (node)
2051 *speculative = true;
2052 target = node->decl;
2054 else
2055 return NULL;
2057 else
2059 *speculative = false;
2060 if (targets.length () == 1)
2061 target = targets[0]->decl;
2062 else
2063 target = ipa_impossible_devirt_target (ie, NULL_TREE);
2066 if (target && !possible_polymorphic_call_target_p (ie,
2067 cgraph_node::get (target)))
2068 target = ipa_impossible_devirt_target (ie, target);
2070 return target;
2074 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2075 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2076 return the destination. */
2078 tree
2079 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
2080 vec<tree> known_csts,
2081 vec<ipa_polymorphic_call_context> known_contexts,
2082 vec<ipa_agg_jump_function_p> known_aggs,
2083 bool *speculative)
2085 return ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2086 known_aggs, NULL, speculative);
2089 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2090 and KNOWN_CONTEXTS. */
2092 static int
2093 devirtualization_time_bonus (struct cgraph_node *node,
2094 vec<tree> known_csts,
2095 vec<ipa_polymorphic_call_context> known_contexts,
2096 vec<ipa_agg_jump_function_p> known_aggs)
2098 struct cgraph_edge *ie;
2099 int res = 0;
2101 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
2103 struct cgraph_node *callee;
2104 struct inline_summary *isummary;
2105 enum availability avail;
2106 tree target;
2107 bool speculative;
2109 target = ipa_get_indirect_edge_target (ie, known_csts, known_contexts,
2110 known_aggs, &speculative);
2111 if (!target)
2112 continue;
2114 /* Only bare minimum benefit for clearly un-inlineable targets. */
2115 res += 1;
2116 callee = cgraph_node::get (target);
2117 if (!callee || !callee->definition)
2118 continue;
2119 callee = callee->function_symbol (&avail);
2120 if (avail < AVAIL_AVAILABLE)
2121 continue;
2122 isummary = inline_summaries->get (callee);
2123 if (!isummary->inlinable)
2124 continue;
2126 /* FIXME: The values below need re-considering and perhaps also
2127 integrating into the cost metrics, at lest in some very basic way. */
2128 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
2129 res += 31 / ((int)speculative + 1);
2130 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
2131 res += 15 / ((int)speculative + 1);
2132 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
2133 || DECL_DECLARED_INLINE_P (callee->decl))
2134 res += 7 / ((int)speculative + 1);
2137 return res;
2140 /* Return time bonus incurred because of HINTS. */
2142 static int
2143 hint_time_bonus (inline_hints hints)
2145 int result = 0;
2146 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
2147 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
2148 if (hints & INLINE_HINT_array_index)
2149 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
2150 return result;
2153 /* If there is a reason to penalize the function described by INFO in the
2154 cloning goodness evaluation, do so. */
2156 static inline int64_t
2157 incorporate_penalties (ipa_node_params *info, int64_t evaluation)
2159 if (info->node_within_scc)
2160 evaluation = (evaluation
2161 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY))) / 100;
2163 if (info->node_calling_single_call)
2164 evaluation = (evaluation
2165 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY)))
2166 / 100;
2168 return evaluation;
2171 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2172 and SIZE_COST and with the sum of frequencies of incoming edges to the
2173 potential new clone in FREQUENCIES. */
2175 static bool
2176 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
2177 int freq_sum, gcov_type count_sum, int size_cost)
2179 if (time_benefit == 0
2180 || !opt_for_fn (node->decl, flag_ipa_cp_clone)
2181 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
2182 return false;
2184 gcc_assert (size_cost > 0);
2186 struct ipa_node_params *info = IPA_NODE_REF (node);
2187 if (max_count)
2189 int factor = (count_sum * 1000) / max_count;
2190 int64_t evaluation = (((int64_t) time_benefit * factor)
2191 / size_cost);
2192 evaluation = incorporate_penalties (info, evaluation);
2194 if (dump_file && (dump_flags & TDF_DETAILS))
2195 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2196 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
2197 "%s%s) -> evaluation: " "%"PRId64
2198 ", threshold: %i\n",
2199 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
2200 info->node_within_scc ? ", scc" : "",
2201 info->node_calling_single_call ? ", single_call" : "",
2202 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2204 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2206 else
2208 int64_t evaluation = (((int64_t) time_benefit * freq_sum)
2209 / size_cost);
2210 evaluation = incorporate_penalties (info, evaluation);
2212 if (dump_file && (dump_flags & TDF_DETAILS))
2213 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2214 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2215 "%"PRId64 ", threshold: %i\n",
2216 time_benefit, size_cost, freq_sum,
2217 info->node_within_scc ? ", scc" : "",
2218 info->node_calling_single_call ? ", single_call" : "",
2219 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2221 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2225 /* Return all context independent values from aggregate lattices in PLATS in a
2226 vector. Return NULL if there are none. */
2228 static vec<ipa_agg_jf_item, va_gc> *
2229 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
2231 vec<ipa_agg_jf_item, va_gc> *res = NULL;
2233 if (plats->aggs_bottom
2234 || plats->aggs_contain_variable
2235 || plats->aggs_count == 0)
2236 return NULL;
2238 for (struct ipcp_agg_lattice *aglat = plats->aggs;
2239 aglat;
2240 aglat = aglat->next)
2241 if (aglat->is_single_const ())
2243 struct ipa_agg_jf_item item;
2244 item.offset = aglat->offset;
2245 item.value = aglat->values->value;
2246 vec_safe_push (res, item);
2248 return res;
2251 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2252 populate them with values of parameters that are known independent of the
2253 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2254 non-NULL, the movement cost of all removable parameters will be stored in
2255 it. */
2257 static bool
2258 gather_context_independent_values (struct ipa_node_params *info,
2259 vec<tree> *known_csts,
2260 vec<ipa_polymorphic_call_context>
2261 *known_contexts,
2262 vec<ipa_agg_jump_function> *known_aggs,
2263 int *removable_params_cost)
2265 int i, count = ipa_get_param_count (info);
2266 bool ret = false;
2268 known_csts->create (0);
2269 known_contexts->create (0);
2270 known_csts->safe_grow_cleared (count);
2271 known_contexts->safe_grow_cleared (count);
2272 if (known_aggs)
2274 known_aggs->create (0);
2275 known_aggs->safe_grow_cleared (count);
2278 if (removable_params_cost)
2279 *removable_params_cost = 0;
2281 for (i = 0; i < count ; i++)
2283 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2284 ipcp_lattice<tree> *lat = &plats->itself;
2286 if (lat->is_single_const ())
2288 ipcp_value<tree> *val = lat->values;
2289 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2290 (*known_csts)[i] = val->value;
2291 if (removable_params_cost)
2292 *removable_params_cost
2293 += estimate_move_cost (TREE_TYPE (val->value), false);
2294 ret = true;
2296 else if (removable_params_cost
2297 && !ipa_is_param_used (info, i))
2298 *removable_params_cost
2299 += ipa_get_param_move_cost (info, i);
2301 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2302 if (ctxlat->is_single_const ())
2304 (*known_contexts)[i] = ctxlat->values->value;
2305 ret = true;
2308 if (known_aggs)
2310 vec<ipa_agg_jf_item, va_gc> *agg_items;
2311 struct ipa_agg_jump_function *ajf;
2313 agg_items = context_independent_aggregate_values (plats);
2314 ajf = &(*known_aggs)[i];
2315 ajf->items = agg_items;
2316 ajf->by_ref = plats->aggs_by_ref;
2317 ret |= agg_items != NULL;
2321 return ret;
2324 /* The current interface in ipa-inline-analysis requires a pointer vector.
2325 Create it.
2327 FIXME: That interface should be re-worked, this is slightly silly. Still,
2328 I'd like to discuss how to change it first and this demonstrates the
2329 issue. */
2331 static vec<ipa_agg_jump_function_p>
2332 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
2334 vec<ipa_agg_jump_function_p> ret;
2335 struct ipa_agg_jump_function *ajf;
2336 int i;
2338 ret.create (known_aggs.length ());
2339 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
2340 ret.quick_push (ajf);
2341 return ret;
2344 /* Perform time and size measurement of NODE with the context given in
2345 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2346 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2347 all context-independent removable parameters and EST_MOVE_COST of estimated
2348 movement of the considered parameter and store it into VAL. */
2350 static void
2351 perform_estimation_of_a_value (cgraph_node *node, vec<tree> known_csts,
2352 vec<ipa_polymorphic_call_context> known_contexts,
2353 vec<ipa_agg_jump_function_p> known_aggs_ptrs,
2354 int base_time, int removable_params_cost,
2355 int est_move_cost, ipcp_value_base *val)
2357 int time, size, time_benefit;
2358 inline_hints hints;
2360 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2361 known_aggs_ptrs, &size, &time,
2362 &hints);
2363 time_benefit = base_time - time
2364 + devirtualization_time_bonus (node, known_csts, known_contexts,
2365 known_aggs_ptrs)
2366 + hint_time_bonus (hints)
2367 + removable_params_cost + est_move_cost;
2369 gcc_checking_assert (size >=0);
2370 /* The inliner-heuristics based estimates may think that in certain
2371 contexts some functions do not have any size at all but we want
2372 all specializations to have at least a tiny cost, not least not to
2373 divide by zero. */
2374 if (size == 0)
2375 size = 1;
2377 val->local_time_benefit = time_benefit;
2378 val->local_size_cost = size;
2381 /* Iterate over known values of parameters of NODE and estimate the local
2382 effects in terms of time and size they have. */
2384 static void
2385 estimate_local_effects (struct cgraph_node *node)
2387 struct ipa_node_params *info = IPA_NODE_REF (node);
2388 int i, count = ipa_get_param_count (info);
2389 vec<tree> known_csts;
2390 vec<ipa_polymorphic_call_context> known_contexts;
2391 vec<ipa_agg_jump_function> known_aggs;
2392 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
2393 bool always_const;
2394 int base_time = inline_summaries->get (node)->time;
2395 int removable_params_cost;
2397 if (!count || !ipcp_versionable_function_p (node))
2398 return;
2400 if (dump_file && (dump_flags & TDF_DETAILS))
2401 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
2402 node->name (), node->order, base_time);
2404 always_const = gather_context_independent_values (info, &known_csts,
2405 &known_contexts, &known_aggs,
2406 &removable_params_cost);
2407 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
2408 if (always_const)
2410 struct caller_statistics stats;
2411 inline_hints hints;
2412 int time, size;
2414 init_caller_stats (&stats);
2415 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2416 false);
2417 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2418 known_aggs_ptrs, &size, &time, &hints);
2419 time -= devirtualization_time_bonus (node, known_csts, known_contexts,
2420 known_aggs_ptrs);
2421 time -= hint_time_bonus (hints);
2422 time -= removable_params_cost;
2423 size -= stats.n_calls * removable_params_cost;
2425 if (dump_file)
2426 fprintf (dump_file, " - context independent values, size: %i, "
2427 "time_benefit: %i\n", size, base_time - time);
2429 if (size <= 0
2430 || node->will_be_removed_from_program_if_no_direct_calls_p ())
2432 info->do_clone_for_all_contexts = true;
2433 base_time = time;
2435 if (dump_file)
2436 fprintf (dump_file, " Decided to specialize for all "
2437 "known contexts, code not going to grow.\n");
2439 else if (good_cloning_opportunity_p (node, base_time - time,
2440 stats.freq_sum, stats.count_sum,
2441 size))
2443 if (size + overall_size <= max_new_size)
2445 info->do_clone_for_all_contexts = true;
2446 base_time = time;
2447 overall_size += size;
2449 if (dump_file)
2450 fprintf (dump_file, " Decided to specialize for all "
2451 "known contexts, growth deemed beneficial.\n");
2453 else if (dump_file && (dump_flags & TDF_DETAILS))
2454 fprintf (dump_file, " Not cloning for all contexts because "
2455 "max_new_size would be reached with %li.\n",
2456 size + overall_size);
2460 for (i = 0; i < count ; i++)
2462 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2463 ipcp_lattice<tree> *lat = &plats->itself;
2464 ipcp_value<tree> *val;
2466 if (lat->bottom
2467 || !lat->values
2468 || known_csts[i])
2469 continue;
2471 for (val = lat->values; val; val = val->next)
2473 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2474 known_csts[i] = val->value;
2476 int emc = estimate_move_cost (TREE_TYPE (val->value), true);
2477 perform_estimation_of_a_value (node, known_csts, known_contexts,
2478 known_aggs_ptrs, base_time,
2479 removable_params_cost, emc, val);
2481 if (dump_file && (dump_flags & TDF_DETAILS))
2483 fprintf (dump_file, " - estimates for value ");
2484 print_ipcp_constant_value (dump_file, val->value);
2485 fprintf (dump_file, " for ");
2486 ipa_dump_param (dump_file, info, i);
2487 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2488 val->local_time_benefit, val->local_size_cost);
2491 known_csts[i] = NULL_TREE;
2494 for (i = 0; i < count; i++)
2496 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2498 if (!plats->virt_call)
2499 continue;
2501 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2502 ipcp_value<ipa_polymorphic_call_context> *val;
2504 if (ctxlat->bottom
2505 || !ctxlat->values
2506 || !known_contexts[i].useless_p ())
2507 continue;
2509 for (val = ctxlat->values; val; val = val->next)
2511 known_contexts[i] = val->value;
2512 perform_estimation_of_a_value (node, known_csts, known_contexts,
2513 known_aggs_ptrs, base_time,
2514 removable_params_cost, 0, val);
2516 if (dump_file && (dump_flags & TDF_DETAILS))
2518 fprintf (dump_file, " - estimates for polymorphic context ");
2519 print_ipcp_constant_value (dump_file, val->value);
2520 fprintf (dump_file, " for ");
2521 ipa_dump_param (dump_file, info, i);
2522 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2523 val->local_time_benefit, val->local_size_cost);
2526 known_contexts[i] = ipa_polymorphic_call_context ();
2529 for (i = 0; i < count ; i++)
2531 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2532 struct ipa_agg_jump_function *ajf;
2533 struct ipcp_agg_lattice *aglat;
2535 if (plats->aggs_bottom || !plats->aggs)
2536 continue;
2538 ajf = &known_aggs[i];
2539 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2541 ipcp_value<tree> *val;
2542 if (aglat->bottom || !aglat->values
2543 /* If the following is true, the one value is in known_aggs. */
2544 || (!plats->aggs_contain_variable
2545 && aglat->is_single_const ()))
2546 continue;
2548 for (val = aglat->values; val; val = val->next)
2550 struct ipa_agg_jf_item item;
2552 item.offset = aglat->offset;
2553 item.value = val->value;
2554 vec_safe_push (ajf->items, item);
2556 perform_estimation_of_a_value (node, known_csts, known_contexts,
2557 known_aggs_ptrs, base_time,
2558 removable_params_cost, 0, val);
2560 if (dump_file && (dump_flags & TDF_DETAILS))
2562 fprintf (dump_file, " - estimates for value ");
2563 print_ipcp_constant_value (dump_file, val->value);
2564 fprintf (dump_file, " for ");
2565 ipa_dump_param (dump_file, info, i);
2566 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2567 "]: time_benefit: %i, size: %i\n",
2568 plats->aggs_by_ref ? "ref " : "",
2569 aglat->offset,
2570 val->local_time_benefit, val->local_size_cost);
2573 ajf->items->pop ();
2578 for (i = 0; i < count ; i++)
2579 vec_free (known_aggs[i].items);
2581 known_csts.release ();
2582 known_contexts.release ();
2583 known_aggs.release ();
2584 known_aggs_ptrs.release ();
2588 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2589 topological sort of values. */
2591 template <typename valtype>
2592 void
2593 value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
2595 ipcp_value_source<valtype> *src;
2597 if (cur_val->dfs)
2598 return;
2600 dfs_counter++;
2601 cur_val->dfs = dfs_counter;
2602 cur_val->low_link = dfs_counter;
2604 cur_val->topo_next = stack;
2605 stack = cur_val;
2606 cur_val->on_stack = true;
2608 for (src = cur_val->sources; src; src = src->next)
2609 if (src->val)
2611 if (src->val->dfs == 0)
2613 add_val (src->val);
2614 if (src->val->low_link < cur_val->low_link)
2615 cur_val->low_link = src->val->low_link;
2617 else if (src->val->on_stack
2618 && src->val->dfs < cur_val->low_link)
2619 cur_val->low_link = src->val->dfs;
2622 if (cur_val->dfs == cur_val->low_link)
2624 ipcp_value<valtype> *v, *scc_list = NULL;
2628 v = stack;
2629 stack = v->topo_next;
2630 v->on_stack = false;
2632 v->scc_next = scc_list;
2633 scc_list = v;
2635 while (v != cur_val);
2637 cur_val->topo_next = values_topo;
2638 values_topo = cur_val;
2642 /* Add all values in lattices associated with NODE to the topological sort if
2643 they are not there yet. */
2645 static void
2646 add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
2648 struct ipa_node_params *info = IPA_NODE_REF (node);
2649 int i, count = ipa_get_param_count (info);
2651 for (i = 0; i < count ; i++)
2653 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2654 ipcp_lattice<tree> *lat = &plats->itself;
2655 struct ipcp_agg_lattice *aglat;
2657 if (!lat->bottom)
2659 ipcp_value<tree> *val;
2660 for (val = lat->values; val; val = val->next)
2661 topo->constants.add_val (val);
2664 if (!plats->aggs_bottom)
2665 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2666 if (!aglat->bottom)
2668 ipcp_value<tree> *val;
2669 for (val = aglat->values; val; val = val->next)
2670 topo->constants.add_val (val);
2673 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2674 if (!ctxlat->bottom)
2676 ipcp_value<ipa_polymorphic_call_context> *ctxval;
2677 for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
2678 topo->contexts.add_val (ctxval);
2683 /* One pass of constants propagation along the call graph edges, from callers
2684 to callees (requires topological ordering in TOPO), iterate over strongly
2685 connected components. */
2687 static void
2688 propagate_constants_topo (struct ipa_topo_info *topo)
2690 int i;
2692 for (i = topo->nnodes - 1; i >= 0; i--)
2694 unsigned j;
2695 struct cgraph_node *v, *node = topo->order[i];
2696 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
2698 /* First, iteratively propagate within the strongly connected component
2699 until all lattices stabilize. */
2700 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2701 if (v->has_gimple_body_p ())
2702 push_node_to_stack (topo, v);
2704 v = pop_node_from_stack (topo);
2705 while (v)
2707 struct cgraph_edge *cs;
2709 for (cs = v->callees; cs; cs = cs->next_callee)
2710 if (ipa_edge_within_scc (cs))
2712 IPA_NODE_REF (v)->node_within_scc = true;
2713 if (propagate_constants_accross_call (cs))
2714 push_node_to_stack (topo, cs->callee->function_symbol ());
2716 v = pop_node_from_stack (topo);
2719 /* Afterwards, propagate along edges leading out of the SCC, calculates
2720 the local effects of the discovered constants and all valid values to
2721 their topological sort. */
2722 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2723 if (v->has_gimple_body_p ())
2725 struct cgraph_edge *cs;
2727 estimate_local_effects (v);
2728 add_all_node_vals_to_toposort (v, topo);
2729 for (cs = v->callees; cs; cs = cs->next_callee)
2730 if (!ipa_edge_within_scc (cs))
2731 propagate_constants_accross_call (cs);
2733 cycle_nodes.release ();
2738 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2739 the bigger one if otherwise. */
2741 static int
2742 safe_add (int a, int b)
2744 if (a > INT_MAX/2 || b > INT_MAX/2)
2745 return a > b ? a : b;
2746 else
2747 return a + b;
2751 /* Propagate the estimated effects of individual values along the topological
2752 from the dependent values to those they depend on. */
2754 template <typename valtype>
2755 void
2756 value_topo_info<valtype>::propagate_effects ()
2758 ipcp_value<valtype> *base;
2760 for (base = values_topo; base; base = base->topo_next)
2762 ipcp_value_source<valtype> *src;
2763 ipcp_value<valtype> *val;
2764 int time = 0, size = 0;
2766 for (val = base; val; val = val->scc_next)
2768 time = safe_add (time,
2769 val->local_time_benefit + val->prop_time_benefit);
2770 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2773 for (val = base; val; val = val->scc_next)
2774 for (src = val->sources; src; src = src->next)
2775 if (src->val
2776 && src->cs->maybe_hot_p ())
2778 src->val->prop_time_benefit = safe_add (time,
2779 src->val->prop_time_benefit);
2780 src->val->prop_size_cost = safe_add (size,
2781 src->val->prop_size_cost);
2787 /* Propagate constants, polymorphic contexts and their effects from the
2788 summaries interprocedurally. */
2790 static void
2791 ipcp_propagate_stage (struct ipa_topo_info *topo)
2793 struct cgraph_node *node;
2795 if (dump_file)
2796 fprintf (dump_file, "\n Propagating constants:\n\n");
2798 if (in_lto_p)
2799 ipa_update_after_lto_read ();
2802 FOR_EACH_DEFINED_FUNCTION (node)
2804 struct ipa_node_params *info = IPA_NODE_REF (node);
2806 determine_versionability (node);
2807 if (node->has_gimple_body_p ())
2809 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2810 ipa_get_param_count (info));
2811 initialize_node_lattices (node);
2813 if (node->definition && !node->alias)
2814 overall_size += inline_summaries->get (node)->self_size;
2815 if (node->count > max_count)
2816 max_count = node->count;
2819 max_new_size = overall_size;
2820 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2821 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2822 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2824 if (dump_file)
2825 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2826 overall_size, max_new_size);
2828 propagate_constants_topo (topo);
2829 #ifdef ENABLE_CHECKING
2830 ipcp_verify_propagated_values ();
2831 #endif
2832 topo->constants.propagate_effects ();
2833 topo->contexts.propagate_effects ();
2835 if (dump_file)
2837 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2838 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2842 /* Discover newly direct outgoing edges from NODE which is a new clone with
2843 known KNOWN_CSTS and make them direct. */
2845 static void
2846 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2847 vec<tree> known_csts,
2848 vec<ipa_polymorphic_call_context>
2849 known_contexts,
2850 struct ipa_agg_replacement_value *aggvals)
2852 struct cgraph_edge *ie, *next_ie;
2853 bool found = false;
2855 for (ie = node->indirect_calls; ie; ie = next_ie)
2857 tree target;
2858 bool speculative;
2860 next_ie = ie->next_callee;
2861 target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2862 vNULL, aggvals, &speculative);
2863 if (target)
2865 bool agg_contents = ie->indirect_info->agg_contents;
2866 bool polymorphic = ie->indirect_info->polymorphic;
2867 int param_index = ie->indirect_info->param_index;
2868 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
2869 speculative);
2870 found = true;
2872 if (cs && !agg_contents && !polymorphic)
2874 struct ipa_node_params *info = IPA_NODE_REF (node);
2875 int c = ipa_get_controlled_uses (info, param_index);
2876 if (c != IPA_UNDESCRIBED_USE)
2878 struct ipa_ref *to_del;
2880 c--;
2881 ipa_set_controlled_uses (info, param_index, c);
2882 if (dump_file && (dump_flags & TDF_DETAILS))
2883 fprintf (dump_file, " controlled uses count of param "
2884 "%i bumped down to %i\n", param_index, c);
2885 if (c == 0
2886 && (to_del = node->find_reference (cs->callee, NULL, 0)))
2888 if (dump_file && (dump_flags & TDF_DETAILS))
2889 fprintf (dump_file, " and even removing its "
2890 "cloning-created reference\n");
2891 to_del->remove_reference ();
2897 /* Turning calls to direct calls will improve overall summary. */
2898 if (found)
2899 inline_update_overall_summary (node);
2902 /* Vector of pointers which for linked lists of clones of an original crgaph
2903 edge. */
2905 static vec<cgraph_edge *> next_edge_clone;
2906 static vec<cgraph_edge *> prev_edge_clone;
2908 static inline void
2909 grow_edge_clone_vectors (void)
2911 if (next_edge_clone.length ()
2912 <= (unsigned) symtab->edges_max_uid)
2913 next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2914 if (prev_edge_clone.length ()
2915 <= (unsigned) symtab->edges_max_uid)
2916 prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2919 /* Edge duplication hook to grow the appropriate linked list in
2920 next_edge_clone. */
2922 static void
2923 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2924 void *)
2926 grow_edge_clone_vectors ();
2928 struct cgraph_edge *old_next = next_edge_clone[src->uid];
2929 if (old_next)
2930 prev_edge_clone[old_next->uid] = dst;
2931 prev_edge_clone[dst->uid] = src;
2933 next_edge_clone[dst->uid] = old_next;
2934 next_edge_clone[src->uid] = dst;
2937 /* Hook that is called by cgraph.c when an edge is removed. */
2939 static void
2940 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
2942 grow_edge_clone_vectors ();
2944 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
2945 struct cgraph_edge *next = next_edge_clone[cs->uid];
2946 if (prev)
2947 next_edge_clone[prev->uid] = next;
2948 if (next)
2949 prev_edge_clone[next->uid] = prev;
2952 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2953 parameter with the given INDEX. */
2955 static tree
2956 get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset,
2957 int index)
2959 struct ipa_agg_replacement_value *aggval;
2961 aggval = ipa_get_agg_replacements_for_node (node);
2962 while (aggval)
2964 if (aggval->offset == offset
2965 && aggval->index == index)
2966 return aggval->value;
2967 aggval = aggval->next;
2969 return NULL_TREE;
2972 /* Return true is NODE is DEST or its clone for all contexts. */
2974 static bool
2975 same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest)
2977 if (node == dest)
2978 return true;
2980 struct ipa_node_params *info = IPA_NODE_REF (node);
2981 return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
2984 /* Return true if edge CS does bring about the value described by SRC to node
2985 DEST or its clone for all contexts. */
2987 static bool
2988 cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
2989 cgraph_node *dest)
2991 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2992 enum availability availability;
2993 cgraph_node *real_dest = cs->callee->function_symbol (&availability);
2995 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
2996 || availability <= AVAIL_INTERPOSABLE
2997 || caller_info->node_dead)
2998 return false;
2999 if (!src->val)
3000 return true;
3002 if (caller_info->ipcp_orig_node)
3004 tree t;
3005 if (src->offset == -1)
3006 t = caller_info->known_csts[src->index];
3007 else
3008 t = get_clone_agg_value (cs->caller, src->offset, src->index);
3009 return (t != NULL_TREE
3010 && values_equal_for_ipcp_p (src->val->value, t));
3012 else
3014 struct ipcp_agg_lattice *aglat;
3015 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3016 src->index);
3017 if (src->offset == -1)
3018 return (plats->itself.is_single_const ()
3019 && values_equal_for_ipcp_p (src->val->value,
3020 plats->itself.values->value));
3021 else
3023 if (plats->aggs_bottom || plats->aggs_contain_variable)
3024 return false;
3025 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3026 if (aglat->offset == src->offset)
3027 return (aglat->is_single_const ()
3028 && values_equal_for_ipcp_p (src->val->value,
3029 aglat->values->value));
3031 return false;
3035 /* Return true if edge CS does bring about the value described by SRC to node
3036 DEST or its clone for all contexts. */
3038 static bool
3039 cgraph_edge_brings_value_p (cgraph_edge *cs,
3040 ipcp_value_source<ipa_polymorphic_call_context> *src,
3041 cgraph_node *dest)
3043 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3044 cgraph_node *real_dest = cs->callee->function_symbol ();
3046 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3047 || caller_info->node_dead)
3048 return false;
3049 if (!src->val)
3050 return true;
3052 if (caller_info->ipcp_orig_node)
3053 return (caller_info->known_contexts.length () > (unsigned) src->index)
3054 && values_equal_for_ipcp_p (src->val->value,
3055 caller_info->known_contexts[src->index]);
3057 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3058 src->index);
3059 return plats->ctxlat.is_single_const ()
3060 && values_equal_for_ipcp_p (src->val->value,
3061 plats->ctxlat.values->value);
3064 /* Get the next clone in the linked list of clones of an edge. */
3066 static inline struct cgraph_edge *
3067 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
3069 return next_edge_clone[cs->uid];
3072 /* Given VAL that is intended for DEST, iterate over all its sources and if
3073 they still hold, add their edge frequency and their number into *FREQUENCY
3074 and *CALLER_COUNT respectively. */
3076 template <typename valtype>
3077 static bool
3078 get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
3079 int *freq_sum,
3080 gcov_type *count_sum, int *caller_count)
3082 ipcp_value_source<valtype> *src;
3083 int freq = 0, count = 0;
3084 gcov_type cnt = 0;
3085 bool hot = false;
3087 for (src = val->sources; src; src = src->next)
3089 struct cgraph_edge *cs = src->cs;
3090 while (cs)
3092 if (cgraph_edge_brings_value_p (cs, src, dest))
3094 count++;
3095 freq += cs->frequency;
3096 cnt += cs->count;
3097 hot |= cs->maybe_hot_p ();
3099 cs = get_next_cgraph_edge_clone (cs);
3103 *freq_sum = freq;
3104 *count_sum = cnt;
3105 *caller_count = count;
3106 return hot;
3109 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3110 is assumed their number is known and equal to CALLER_COUNT. */
3112 template <typename valtype>
3113 static vec<cgraph_edge *>
3114 gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
3115 int caller_count)
3117 ipcp_value_source<valtype> *src;
3118 vec<cgraph_edge *> ret;
3120 ret.create (caller_count);
3121 for (src = val->sources; src; src = src->next)
3123 struct cgraph_edge *cs = src->cs;
3124 while (cs)
3126 if (cgraph_edge_brings_value_p (cs, src, dest))
3127 ret.quick_push (cs);
3128 cs = get_next_cgraph_edge_clone (cs);
3132 return ret;
3135 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3136 Return it or NULL if for some reason it cannot be created. */
3138 static struct ipa_replace_map *
3139 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
3141 struct ipa_replace_map *replace_map;
3144 replace_map = ggc_alloc<ipa_replace_map> ();
3145 if (dump_file)
3147 fprintf (dump_file, " replacing ");
3148 ipa_dump_param (dump_file, info, parm_num);
3150 fprintf (dump_file, " with const ");
3151 print_generic_expr (dump_file, value, 0);
3152 fprintf (dump_file, "\n");
3154 replace_map->old_tree = NULL;
3155 replace_map->parm_num = parm_num;
3156 replace_map->new_tree = value;
3157 replace_map->replace_p = true;
3158 replace_map->ref_p = false;
3160 return replace_map;
3163 /* Dump new profiling counts */
3165 static void
3166 dump_profile_updates (struct cgraph_node *orig_node,
3167 struct cgraph_node *new_node)
3169 struct cgraph_edge *cs;
3171 fprintf (dump_file, " setting count of the specialized node to "
3172 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
3173 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3174 fprintf (dump_file, " edge to %s has count "
3175 HOST_WIDE_INT_PRINT_DEC "\n",
3176 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3178 fprintf (dump_file, " setting count of the original node to "
3179 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
3180 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3181 fprintf (dump_file, " edge to %s is left with "
3182 HOST_WIDE_INT_PRINT_DEC "\n",
3183 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3186 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3187 their profile information to reflect this. */
3189 static void
3190 update_profiling_info (struct cgraph_node *orig_node,
3191 struct cgraph_node *new_node)
3193 struct cgraph_edge *cs;
3194 struct caller_statistics stats;
3195 gcov_type new_sum, orig_sum;
3196 gcov_type remainder, orig_node_count = orig_node->count;
3198 if (orig_node_count == 0)
3199 return;
3201 init_caller_stats (&stats);
3202 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3203 false);
3204 orig_sum = stats.count_sum;
3205 init_caller_stats (&stats);
3206 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3207 false);
3208 new_sum = stats.count_sum;
3210 if (orig_node_count < orig_sum + new_sum)
3212 if (dump_file)
3213 fprintf (dump_file, " Problem: node %s/%i has too low count "
3214 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
3215 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
3216 orig_node->name (), orig_node->order,
3217 (HOST_WIDE_INT) orig_node_count,
3218 (HOST_WIDE_INT) (orig_sum + new_sum));
3220 orig_node_count = (orig_sum + new_sum) * 12 / 10;
3221 if (dump_file)
3222 fprintf (dump_file, " proceeding by pretending it was "
3223 HOST_WIDE_INT_PRINT_DEC "\n",
3224 (HOST_WIDE_INT) orig_node_count);
3227 new_node->count = new_sum;
3228 remainder = orig_node_count - new_sum;
3229 orig_node->count = remainder;
3231 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3232 if (cs->frequency)
3233 cs->count = apply_probability (cs->count,
3234 GCOV_COMPUTE_SCALE (new_sum,
3235 orig_node_count));
3236 else
3237 cs->count = 0;
3239 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3240 cs->count = apply_probability (cs->count,
3241 GCOV_COMPUTE_SCALE (remainder,
3242 orig_node_count));
3244 if (dump_file)
3245 dump_profile_updates (orig_node, new_node);
3248 /* Update the respective profile of specialized NEW_NODE and the original
3249 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3250 have been redirected to the specialized version. */
3252 static void
3253 update_specialized_profile (struct cgraph_node *new_node,
3254 struct cgraph_node *orig_node,
3255 gcov_type redirected_sum)
3257 struct cgraph_edge *cs;
3258 gcov_type new_node_count, orig_node_count = orig_node->count;
3260 if (dump_file)
3261 fprintf (dump_file, " the sum of counts of redirected edges is "
3262 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
3263 if (orig_node_count == 0)
3264 return;
3266 gcc_assert (orig_node_count >= redirected_sum);
3268 new_node_count = new_node->count;
3269 new_node->count += redirected_sum;
3270 orig_node->count -= redirected_sum;
3272 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3273 if (cs->frequency)
3274 cs->count += apply_probability (cs->count,
3275 GCOV_COMPUTE_SCALE (redirected_sum,
3276 new_node_count));
3277 else
3278 cs->count = 0;
3280 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3282 gcov_type dec = apply_probability (cs->count,
3283 GCOV_COMPUTE_SCALE (redirected_sum,
3284 orig_node_count));
3285 if (dec < cs->count)
3286 cs->count -= dec;
3287 else
3288 cs->count = 0;
3291 if (dump_file)
3292 dump_profile_updates (orig_node, new_node);
3295 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3296 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3297 redirect all edges in CALLERS to it. */
3299 static struct cgraph_node *
3300 create_specialized_node (struct cgraph_node *node,
3301 vec<tree> known_csts,
3302 vec<ipa_polymorphic_call_context> known_contexts,
3303 struct ipa_agg_replacement_value *aggvals,
3304 vec<cgraph_edge *> callers)
3306 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
3307 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
3308 struct ipa_agg_replacement_value *av;
3309 struct cgraph_node *new_node;
3310 int i, count = ipa_get_param_count (info);
3311 bitmap args_to_skip;
3313 gcc_assert (!info->ipcp_orig_node);
3315 if (node->local.can_change_signature)
3317 args_to_skip = BITMAP_GGC_ALLOC ();
3318 for (i = 0; i < count; i++)
3320 tree t = known_csts[i];
3322 if (t || !ipa_is_param_used (info, i))
3323 bitmap_set_bit (args_to_skip, i);
3326 else
3328 args_to_skip = NULL;
3329 if (dump_file && (dump_flags & TDF_DETAILS))
3330 fprintf (dump_file, " cannot change function signature\n");
3333 for (i = 0; i < count ; i++)
3335 tree t = known_csts[i];
3336 if (t)
3338 struct ipa_replace_map *replace_map;
3340 gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
3341 replace_map = get_replacement_map (info, t, i);
3342 if (replace_map)
3343 vec_safe_push (replace_trees, replace_map);
3347 new_node = node->create_virtual_clone (callers, replace_trees,
3348 args_to_skip, "constprop");
3349 ipa_set_node_agg_value_chain (new_node, aggvals);
3350 for (av = aggvals; av; av = av->next)
3351 new_node->maybe_create_reference (av->value, IPA_REF_ADDR, NULL);
3353 if (dump_file && (dump_flags & TDF_DETAILS))
3355 fprintf (dump_file, " the new node is %s/%i.\n",
3356 new_node->name (), new_node->order);
3357 if (known_contexts.exists ())
3359 for (i = 0; i < count ; i++)
3360 if (!known_contexts[i].useless_p ())
3362 fprintf (dump_file, " known ctx %i is ", i);
3363 known_contexts[i].dump (dump_file);
3366 if (aggvals)
3367 ipa_dump_agg_replacement_values (dump_file, aggvals);
3369 ipa_check_create_node_params ();
3370 update_profiling_info (node, new_node);
3371 new_info = IPA_NODE_REF (new_node);
3372 new_info->ipcp_orig_node = node;
3373 new_info->known_csts = known_csts;
3374 new_info->known_contexts = known_contexts;
3376 ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts, aggvals);
3378 callers.release ();
3379 return new_node;
3382 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3383 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3385 static void
3386 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
3387 vec<tree> known_csts,
3388 vec<cgraph_edge *> callers)
3390 struct ipa_node_params *info = IPA_NODE_REF (node);
3391 int i, count = ipa_get_param_count (info);
3393 for (i = 0; i < count ; i++)
3395 struct cgraph_edge *cs;
3396 tree newval = NULL_TREE;
3397 int j;
3398 bool first = true;
3400 if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
3401 continue;
3403 FOR_EACH_VEC_ELT (callers, j, cs)
3405 struct ipa_jump_func *jump_func;
3406 tree t;
3408 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3410 newval = NULL_TREE;
3411 break;
3413 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
3414 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
3415 if (!t
3416 || (newval
3417 && !values_equal_for_ipcp_p (t, newval))
3418 || (!first && !newval))
3420 newval = NULL_TREE;
3421 break;
3423 else
3424 newval = t;
3425 first = false;
3428 if (newval)
3430 if (dump_file && (dump_flags & TDF_DETAILS))
3432 fprintf (dump_file, " adding an extra known scalar value ");
3433 print_ipcp_constant_value (dump_file, newval);
3434 fprintf (dump_file, " for ");
3435 ipa_dump_param (dump_file, info, i);
3436 fprintf (dump_file, "\n");
3439 known_csts[i] = newval;
3444 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3445 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3446 CALLERS. */
3448 static void
3449 find_more_contexts_for_caller_subset (cgraph_node *node,
3450 vec<ipa_polymorphic_call_context>
3451 *known_contexts,
3452 vec<cgraph_edge *> callers)
3454 ipa_node_params *info = IPA_NODE_REF (node);
3455 int i, count = ipa_get_param_count (info);
3457 for (i = 0; i < count ; i++)
3459 cgraph_edge *cs;
3461 if (ipa_get_poly_ctx_lat (info, i)->bottom
3462 || (known_contexts->exists ()
3463 && !(*known_contexts)[i].useless_p ()))
3464 continue;
3466 ipa_polymorphic_call_context newval;
3467 bool first = true;
3468 int j;
3470 FOR_EACH_VEC_ELT (callers, j, cs)
3472 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3473 return;
3474 ipa_jump_func *jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs),
3476 ipa_polymorphic_call_context ctx;
3477 ctx = ipa_context_from_jfunc (IPA_NODE_REF (cs->caller), cs, i,
3478 jfunc);
3479 if (first)
3481 newval = ctx;
3482 first = false;
3484 else
3485 newval.meet_with (ctx);
3486 if (newval.useless_p ())
3487 break;
3490 if (!newval.useless_p ())
3492 if (dump_file && (dump_flags & TDF_DETAILS))
3494 fprintf (dump_file, " adding an extra known polymorphic "
3495 "context ");
3496 print_ipcp_constant_value (dump_file, newval);
3497 fprintf (dump_file, " for ");
3498 ipa_dump_param (dump_file, info, i);
3499 fprintf (dump_file, "\n");
3502 if (!known_contexts->exists ())
3503 known_contexts->safe_grow_cleared (ipa_get_param_count (info));
3504 (*known_contexts)[i] = newval;
3510 /* Go through PLATS and create a vector of values consisting of values and
3511 offsets (minus OFFSET) of lattices that contain only a single value. */
3513 static vec<ipa_agg_jf_item>
3514 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
3516 vec<ipa_agg_jf_item> res = vNULL;
3518 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3519 return vNULL;
3521 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3522 if (aglat->is_single_const ())
3524 struct ipa_agg_jf_item ti;
3525 ti.offset = aglat->offset - offset;
3526 ti.value = aglat->values->value;
3527 res.safe_push (ti);
3529 return res;
3532 /* Intersect all values in INTER with single value lattices in PLATS (while
3533 subtracting OFFSET). */
3535 static void
3536 intersect_with_plats (struct ipcp_param_lattices *plats,
3537 vec<ipa_agg_jf_item> *inter,
3538 HOST_WIDE_INT offset)
3540 struct ipcp_agg_lattice *aglat;
3541 struct ipa_agg_jf_item *item;
3542 int k;
3544 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3546 inter->release ();
3547 return;
3550 aglat = plats->aggs;
3551 FOR_EACH_VEC_ELT (*inter, k, item)
3553 bool found = false;
3554 if (!item->value)
3555 continue;
3556 while (aglat)
3558 if (aglat->offset - offset > item->offset)
3559 break;
3560 if (aglat->offset - offset == item->offset)
3562 gcc_checking_assert (item->value);
3563 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
3564 found = true;
3565 break;
3567 aglat = aglat->next;
3569 if (!found)
3570 item->value = NULL_TREE;
3574 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
3575 vector result while subtracting OFFSET from the individual value offsets. */
3577 static vec<ipa_agg_jf_item>
3578 agg_replacements_to_vector (struct cgraph_node *node, int index,
3579 HOST_WIDE_INT offset)
3581 struct ipa_agg_replacement_value *av;
3582 vec<ipa_agg_jf_item> res = vNULL;
3584 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
3585 if (av->index == index
3586 && (av->offset - offset) >= 0)
3588 struct ipa_agg_jf_item item;
3589 gcc_checking_assert (av->value);
3590 item.offset = av->offset - offset;
3591 item.value = av->value;
3592 res.safe_push (item);
3595 return res;
3598 /* Intersect all values in INTER with those that we have already scheduled to
3599 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
3600 (while subtracting OFFSET). */
3602 static void
3603 intersect_with_agg_replacements (struct cgraph_node *node, int index,
3604 vec<ipa_agg_jf_item> *inter,
3605 HOST_WIDE_INT offset)
3607 struct ipa_agg_replacement_value *srcvals;
3608 struct ipa_agg_jf_item *item;
3609 int i;
3611 srcvals = ipa_get_agg_replacements_for_node (node);
3612 if (!srcvals)
3614 inter->release ();
3615 return;
3618 FOR_EACH_VEC_ELT (*inter, i, item)
3620 struct ipa_agg_replacement_value *av;
3621 bool found = false;
3622 if (!item->value)
3623 continue;
3624 for (av = srcvals; av; av = av->next)
3626 gcc_checking_assert (av->value);
3627 if (av->index == index
3628 && av->offset - offset == item->offset)
3630 if (values_equal_for_ipcp_p (item->value, av->value))
3631 found = true;
3632 break;
3635 if (!found)
3636 item->value = NULL_TREE;
3640 /* Intersect values in INTER with aggregate values that come along edge CS to
3641 parameter number INDEX and return it. If INTER does not actually exist yet,
3642 copy all incoming values to it. If we determine we ended up with no values
3643 whatsoever, return a released vector. */
3645 static vec<ipa_agg_jf_item>
3646 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3647 vec<ipa_agg_jf_item> inter)
3649 struct ipa_jump_func *jfunc;
3650 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3651 if (jfunc->type == IPA_JF_PASS_THROUGH
3652 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3654 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3655 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3657 if (caller_info->ipcp_orig_node)
3659 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3660 struct ipcp_param_lattices *orig_plats;
3661 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3662 src_idx);
3663 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3665 if (!inter.exists ())
3666 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3667 else
3668 intersect_with_agg_replacements (cs->caller, src_idx,
3669 &inter, 0);
3671 else
3673 inter.release ();
3674 return vNULL;
3677 else
3679 struct ipcp_param_lattices *src_plats;
3680 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3681 if (agg_pass_through_permissible_p (src_plats, jfunc))
3683 /* Currently we do not produce clobber aggregate jump
3684 functions, adjust when we do. */
3685 gcc_checking_assert (!jfunc->agg.items);
3686 if (!inter.exists ())
3687 inter = copy_plats_to_inter (src_plats, 0);
3688 else
3689 intersect_with_plats (src_plats, &inter, 0);
3691 else
3693 inter.release ();
3694 return vNULL;
3698 else if (jfunc->type == IPA_JF_ANCESTOR
3699 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3701 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3702 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3703 struct ipcp_param_lattices *src_plats;
3704 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3706 if (caller_info->ipcp_orig_node)
3708 if (!inter.exists ())
3709 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3710 else
3711 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3712 delta);
3714 else
3716 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3717 /* Currently we do not produce clobber aggregate jump
3718 functions, adjust when we do. */
3719 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3720 if (!inter.exists ())
3721 inter = copy_plats_to_inter (src_plats, delta);
3722 else
3723 intersect_with_plats (src_plats, &inter, delta);
3726 else if (jfunc->agg.items)
3728 struct ipa_agg_jf_item *item;
3729 int k;
3731 if (!inter.exists ())
3732 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3733 inter.safe_push ((*jfunc->agg.items)[i]);
3734 else
3735 FOR_EACH_VEC_ELT (inter, k, item)
3737 int l = 0;
3738 bool found = false;;
3740 if (!item->value)
3741 continue;
3743 while ((unsigned) l < jfunc->agg.items->length ())
3745 struct ipa_agg_jf_item *ti;
3746 ti = &(*jfunc->agg.items)[l];
3747 if (ti->offset > item->offset)
3748 break;
3749 if (ti->offset == item->offset)
3751 gcc_checking_assert (ti->value);
3752 if (values_equal_for_ipcp_p (item->value,
3753 ti->value))
3754 found = true;
3755 break;
3757 l++;
3759 if (!found)
3760 item->value = NULL;
3763 else
3765 inter.release ();
3766 return vec<ipa_agg_jf_item>();
3768 return inter;
3771 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3772 from all of them. */
3774 static struct ipa_agg_replacement_value *
3775 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3776 vec<cgraph_edge *> callers)
3778 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3779 struct ipa_agg_replacement_value *res;
3780 struct ipa_agg_replacement_value **tail = &res;
3781 struct cgraph_edge *cs;
3782 int i, j, count = ipa_get_param_count (dest_info);
3784 FOR_EACH_VEC_ELT (callers, j, cs)
3786 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3787 if (c < count)
3788 count = c;
3791 for (i = 0; i < count ; i++)
3793 struct cgraph_edge *cs;
3794 vec<ipa_agg_jf_item> inter = vNULL;
3795 struct ipa_agg_jf_item *item;
3796 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3797 int j;
3799 /* Among other things, the following check should deal with all by_ref
3800 mismatches. */
3801 if (plats->aggs_bottom)
3802 continue;
3804 FOR_EACH_VEC_ELT (callers, j, cs)
3806 inter = intersect_aggregates_with_edge (cs, i, inter);
3808 if (!inter.exists ())
3809 goto next_param;
3812 FOR_EACH_VEC_ELT (inter, j, item)
3814 struct ipa_agg_replacement_value *v;
3816 if (!item->value)
3817 continue;
3819 v = ggc_alloc<ipa_agg_replacement_value> ();
3820 v->index = i;
3821 v->offset = item->offset;
3822 v->value = item->value;
3823 v->by_ref = plats->aggs_by_ref;
3824 *tail = v;
3825 tail = &v->next;
3828 next_param:
3829 if (inter.exists ())
3830 inter.release ();
3832 *tail = NULL;
3833 return res;
3836 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3838 static struct ipa_agg_replacement_value *
3839 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3841 struct ipa_agg_replacement_value *res;
3842 struct ipa_agg_replacement_value **tail = &res;
3843 struct ipa_agg_jump_function *aggjf;
3844 struct ipa_agg_jf_item *item;
3845 int i, j;
3847 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3848 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3850 struct ipa_agg_replacement_value *v;
3851 v = ggc_alloc<ipa_agg_replacement_value> ();
3852 v->index = i;
3853 v->offset = item->offset;
3854 v->value = item->value;
3855 v->by_ref = aggjf->by_ref;
3856 *tail = v;
3857 tail = &v->next;
3859 *tail = NULL;
3860 return res;
3863 /* Determine whether CS also brings all scalar values that the NODE is
3864 specialized for. */
3866 static bool
3867 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3868 struct cgraph_node *node)
3870 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3871 int count = ipa_get_param_count (dest_info);
3872 struct ipa_node_params *caller_info;
3873 struct ipa_edge_args *args;
3874 int i;
3876 caller_info = IPA_NODE_REF (cs->caller);
3877 args = IPA_EDGE_REF (cs);
3878 for (i = 0; i < count; i++)
3880 struct ipa_jump_func *jump_func;
3881 tree val, t;
3883 val = dest_info->known_csts[i];
3884 if (!val)
3885 continue;
3887 if (i >= ipa_get_cs_argument_count (args))
3888 return false;
3889 jump_func = ipa_get_ith_jump_func (args, i);
3890 t = ipa_value_from_jfunc (caller_info, jump_func);
3891 if (!t || !values_equal_for_ipcp_p (val, t))
3892 return false;
3894 return true;
3897 /* Determine whether CS also brings all aggregate values that NODE is
3898 specialized for. */
3899 static bool
3900 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3901 struct cgraph_node *node)
3903 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3904 struct ipa_node_params *orig_node_info;
3905 struct ipa_agg_replacement_value *aggval;
3906 int i, ec, count;
3908 aggval = ipa_get_agg_replacements_for_node (node);
3909 if (!aggval)
3910 return true;
3912 count = ipa_get_param_count (IPA_NODE_REF (node));
3913 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3914 if (ec < count)
3915 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3916 if (aggval->index >= ec)
3917 return false;
3919 orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node);
3920 if (orig_caller_info->ipcp_orig_node)
3921 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3923 for (i = 0; i < count; i++)
3925 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
3926 struct ipcp_param_lattices *plats;
3927 bool interesting = false;
3928 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3929 if (aggval->index == i)
3931 interesting = true;
3932 break;
3934 if (!interesting)
3935 continue;
3937 plats = ipa_get_parm_lattices (orig_node_info, aggval->index);
3938 if (plats->aggs_bottom)
3939 return false;
3941 values = intersect_aggregates_with_edge (cs, i, values);
3942 if (!values.exists ())
3943 return false;
3945 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3946 if (aggval->index == i)
3948 struct ipa_agg_jf_item *item;
3949 int j;
3950 bool found = false;
3951 FOR_EACH_VEC_ELT (values, j, item)
3952 if (item->value
3953 && item->offset == av->offset
3954 && values_equal_for_ipcp_p (item->value, av->value))
3956 found = true;
3957 break;
3959 if (!found)
3961 values.release ();
3962 return false;
3966 return true;
3969 /* Given an original NODE and a VAL for which we have already created a
3970 specialized clone, look whether there are incoming edges that still lead
3971 into the old node but now also bring the requested value and also conform to
3972 all other criteria such that they can be redirected the the special node.
3973 This function can therefore redirect the final edge in a SCC. */
3975 template <typename valtype>
3976 static void
3977 perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
3979 ipcp_value_source<valtype> *src;
3980 gcov_type redirected_sum = 0;
3982 for (src = val->sources; src; src = src->next)
3984 struct cgraph_edge *cs = src->cs;
3985 while (cs)
3987 if (cgraph_edge_brings_value_p (cs, src, node)
3988 && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3989 && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
3991 if (dump_file)
3992 fprintf (dump_file, " - adding an extra caller %s/%i"
3993 " of %s/%i\n",
3994 xstrdup_for_dump (cs->caller->name ()),
3995 cs->caller->order,
3996 xstrdup_for_dump (val->spec_node->name ()),
3997 val->spec_node->order);
3999 cs->redirect_callee_duplicating_thunks (val->spec_node);
4000 val->spec_node->expand_all_artificial_thunks ();
4001 redirected_sum += cs->count;
4003 cs = get_next_cgraph_edge_clone (cs);
4007 if (redirected_sum)
4008 update_specialized_profile (val->spec_node, node, redirected_sum);
4011 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4013 static bool
4014 known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
4016 ipa_polymorphic_call_context *ctx;
4017 int i;
4019 FOR_EACH_VEC_ELT (known_contexts, i, ctx)
4020 if (!ctx->useless_p ())
4021 return true;
4022 return false;
4025 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4027 static vec<ipa_polymorphic_call_context>
4028 copy_useful_known_contexts (vec<ipa_polymorphic_call_context> known_contexts)
4030 if (known_contexts_useful_p (known_contexts))
4031 return known_contexts.copy ();
4032 else
4033 return vNULL;
4036 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4037 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4039 static void
4040 modify_known_vectors_with_val (vec<tree> *known_csts,
4041 vec<ipa_polymorphic_call_context> *known_contexts,
4042 ipcp_value<tree> *val,
4043 int index)
4045 *known_csts = known_csts->copy ();
4046 *known_contexts = copy_useful_known_contexts (*known_contexts);
4047 (*known_csts)[index] = val->value;
4050 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4051 copy according to VAL and INDEX. */
4053 static void
4054 modify_known_vectors_with_val (vec<tree> *known_csts,
4055 vec<ipa_polymorphic_call_context> *known_contexts,
4056 ipcp_value<ipa_polymorphic_call_context> *val,
4057 int index)
4059 *known_csts = known_csts->copy ();
4060 *known_contexts = known_contexts->copy ();
4061 (*known_contexts)[index] = val->value;
4064 /* Return true if OFFSET indicates this was not an aggregate value or there is
4065 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4066 AGGVALS list. */
4068 DEBUG_FUNCTION bool
4069 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *aggvals,
4070 int index, HOST_WIDE_INT offset, tree value)
4072 if (offset == -1)
4073 return true;
4075 while (aggvals)
4077 if (aggvals->index == index
4078 && aggvals->offset == offset
4079 && values_equal_for_ipcp_p (aggvals->value, value))
4080 return true;
4081 aggvals = aggvals->next;
4083 return false;
4086 /* Return true if offset is minus one because source of a polymorphic contect
4087 cannot be an aggregate value. */
4089 DEBUG_FUNCTION bool
4090 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *,
4091 int , HOST_WIDE_INT offset,
4092 ipa_polymorphic_call_context)
4094 return offset == -1;
4097 /* Decide wheter to create a special version of NODE for value VAL of parameter
4098 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4099 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4100 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4102 template <typename valtype>
4103 static bool
4104 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
4105 ipcp_value<valtype> *val, vec<tree> known_csts,
4106 vec<ipa_polymorphic_call_context> known_contexts)
4108 struct ipa_agg_replacement_value *aggvals;
4109 int freq_sum, caller_count;
4110 gcov_type count_sum;
4111 vec<cgraph_edge *> callers;
4113 if (val->spec_node)
4115 perhaps_add_new_callers (node, val);
4116 return false;
4118 else if (val->local_size_cost + overall_size > max_new_size)
4120 if (dump_file && (dump_flags & TDF_DETAILS))
4121 fprintf (dump_file, " Ignoring candidate value because "
4122 "max_new_size would be reached with %li.\n",
4123 val->local_size_cost + overall_size);
4124 return false;
4126 else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum,
4127 &caller_count))
4128 return false;
4130 if (dump_file && (dump_flags & TDF_DETAILS))
4132 fprintf (dump_file, " - considering value ");
4133 print_ipcp_constant_value (dump_file, val->value);
4134 fprintf (dump_file, " for ");
4135 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
4136 if (offset != -1)
4137 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
4138 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
4141 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
4142 freq_sum, count_sum,
4143 val->local_size_cost)
4144 && !good_cloning_opportunity_p (node,
4145 val->local_time_benefit
4146 + val->prop_time_benefit,
4147 freq_sum, count_sum,
4148 val->local_size_cost
4149 + val->prop_size_cost))
4150 return false;
4152 if (dump_file)
4153 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
4154 node->name (), node->order);
4156 callers = gather_edges_for_value (val, node, caller_count);
4157 if (offset == -1)
4158 modify_known_vectors_with_val (&known_csts, &known_contexts, val, index);
4159 else
4161 known_csts = known_csts.copy ();
4162 known_contexts = copy_useful_known_contexts (known_contexts);
4164 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
4165 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
4166 aggvals = find_aggregate_values_for_callers_subset (node, callers);
4167 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
4168 offset, val->value));
4169 val->spec_node = create_specialized_node (node, known_csts, known_contexts,
4170 aggvals, callers);
4171 overall_size += val->local_size_cost;
4173 /* TODO: If for some lattice there is only one other known value
4174 left, make a special node for it too. */
4176 return true;
4179 /* Decide whether and what specialized clones of NODE should be created. */
4181 static bool
4182 decide_whether_version_node (struct cgraph_node *node)
4184 struct ipa_node_params *info = IPA_NODE_REF (node);
4185 int i, count = ipa_get_param_count (info);
4186 vec<tree> known_csts;
4187 vec<ipa_polymorphic_call_context> known_contexts;
4188 vec<ipa_agg_jump_function> known_aggs = vNULL;
4189 bool ret = false;
4191 if (count == 0)
4192 return false;
4194 if (dump_file && (dump_flags & TDF_DETAILS))
4195 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
4196 node->name (), node->order);
4198 gather_context_independent_values (info, &known_csts, &known_contexts,
4199 info->do_clone_for_all_contexts ? &known_aggs
4200 : NULL, NULL);
4202 for (i = 0; i < count ;i++)
4204 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4205 ipcp_lattice<tree> *lat = &plats->itself;
4206 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
4208 if (!lat->bottom
4209 && !known_csts[i])
4211 ipcp_value<tree> *val;
4212 for (val = lat->values; val; val = val->next)
4213 ret |= decide_about_value (node, i, -1, val, known_csts,
4214 known_contexts);
4217 if (!plats->aggs_bottom)
4219 struct ipcp_agg_lattice *aglat;
4220 ipcp_value<tree> *val;
4221 for (aglat = plats->aggs; aglat; aglat = aglat->next)
4222 if (!aglat->bottom && aglat->values
4223 /* If the following is false, the one value is in
4224 known_aggs. */
4225 && (plats->aggs_contain_variable
4226 || !aglat->is_single_const ()))
4227 for (val = aglat->values; val; val = val->next)
4228 ret |= decide_about_value (node, i, aglat->offset, val,
4229 known_csts, known_contexts);
4232 if (!ctxlat->bottom
4233 && known_contexts[i].useless_p ())
4235 ipcp_value<ipa_polymorphic_call_context> *val;
4236 for (val = ctxlat->values; val; val = val->next)
4237 ret |= decide_about_value (node, i, -1, val, known_csts,
4238 known_contexts);
4241 info = IPA_NODE_REF (node);
4244 if (info->do_clone_for_all_contexts)
4246 struct cgraph_node *clone;
4247 vec<cgraph_edge *> callers;
4249 if (dump_file)
4250 fprintf (dump_file, " - Creating a specialized node of %s/%i "
4251 "for all known contexts.\n", node->name (),
4252 node->order);
4254 callers = node->collect_callers ();
4256 if (!known_contexts_useful_p (known_contexts))
4258 known_contexts.release ();
4259 known_contexts = vNULL;
4261 clone = create_specialized_node (node, known_csts, known_contexts,
4262 known_aggs_to_agg_replacement_list (known_aggs),
4263 callers);
4264 info = IPA_NODE_REF (node);
4265 info->do_clone_for_all_contexts = false;
4266 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
4267 for (i = 0; i < count ; i++)
4268 vec_free (known_aggs[i].items);
4269 known_aggs.release ();
4270 ret = true;
4272 else
4274 known_csts.release ();
4275 known_contexts.release ();
4278 return ret;
4281 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4283 static void
4284 spread_undeadness (struct cgraph_node *node)
4286 struct cgraph_edge *cs;
4288 for (cs = node->callees; cs; cs = cs->next_callee)
4289 if (ipa_edge_within_scc (cs))
4291 struct cgraph_node *callee;
4292 struct ipa_node_params *info;
4294 callee = cs->callee->function_symbol (NULL);
4295 info = IPA_NODE_REF (callee);
4297 if (info->node_dead)
4299 info->node_dead = 0;
4300 spread_undeadness (callee);
4305 /* Return true if NODE has a caller from outside of its SCC that is not
4306 dead. Worker callback for cgraph_for_node_and_aliases. */
4308 static bool
4309 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
4310 void *data ATTRIBUTE_UNUSED)
4312 struct cgraph_edge *cs;
4314 for (cs = node->callers; cs; cs = cs->next_caller)
4315 if (cs->caller->thunk.thunk_p
4316 && cs->caller->call_for_symbol_thunks_and_aliases
4317 (has_undead_caller_from_outside_scc_p, NULL, true))
4318 return true;
4319 else if (!ipa_edge_within_scc (cs)
4320 && !IPA_NODE_REF (cs->caller)->node_dead)
4321 return true;
4322 return false;
4326 /* Identify nodes within the same SCC as NODE which are no longer needed
4327 because of new clones and will be removed as unreachable. */
4329 static void
4330 identify_dead_nodes (struct cgraph_node *node)
4332 struct cgraph_node *v;
4333 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4334 if (v->will_be_removed_from_program_if_no_direct_calls_p ()
4335 && !v->call_for_symbol_thunks_and_aliases
4336 (has_undead_caller_from_outside_scc_p, NULL, true))
4337 IPA_NODE_REF (v)->node_dead = 1;
4339 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4340 if (!IPA_NODE_REF (v)->node_dead)
4341 spread_undeadness (v);
4343 if (dump_file && (dump_flags & TDF_DETAILS))
4345 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4346 if (IPA_NODE_REF (v)->node_dead)
4347 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
4348 v->name (), v->order);
4352 /* The decision stage. Iterate over the topological order of call graph nodes
4353 TOPO and make specialized clones if deemed beneficial. */
4355 static void
4356 ipcp_decision_stage (struct ipa_topo_info *topo)
4358 int i;
4360 if (dump_file)
4361 fprintf (dump_file, "\nIPA decision stage:\n\n");
4363 for (i = topo->nnodes - 1; i >= 0; i--)
4365 struct cgraph_node *node = topo->order[i];
4366 bool change = false, iterate = true;
4368 while (iterate)
4370 struct cgraph_node *v;
4371 iterate = false;
4372 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4373 if (v->has_gimple_body_p ()
4374 && ipcp_versionable_function_p (v))
4375 iterate |= decide_whether_version_node (v);
4377 change |= iterate;
4379 if (change)
4380 identify_dead_nodes (node);
4384 /* Look up all alignment information that we have discovered and copy it over
4385 to the transformation summary. */
4387 static void
4388 ipcp_store_alignment_results (void)
4390 cgraph_node *node;
4392 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4394 ipa_node_params *info = IPA_NODE_REF (node);
4395 bool dumped_sth = false;
4396 bool found_useful_result = false;
4398 if (!opt_for_fn (node->decl, flag_ipa_cp_alignment))
4400 if (dump_file)
4401 fprintf (dump_file, "Not considering %s for alignment discovery "
4402 "and propagate; -fipa-cp-alignment: disabled.\n",
4403 node->name ());
4404 continue;
4407 if (info->ipcp_orig_node)
4408 info = IPA_NODE_REF (info->ipcp_orig_node);
4410 unsigned count = ipa_get_param_count (info);
4411 for (unsigned i = 0; i < count ; i++)
4413 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4414 if (plats->alignment.known
4415 && plats->alignment.align > 0)
4417 found_useful_result = true;
4418 break;
4421 if (!found_useful_result)
4422 continue;
4424 ipcp_grow_transformations_if_necessary ();
4425 ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node);
4426 vec_safe_reserve_exact (ts->alignments, count);
4428 for (unsigned i = 0; i < count ; i++)
4430 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4432 if (plats->alignment.align == 0)
4433 plats->alignment.known = false;
4435 ts->alignments->quick_push (plats->alignment);
4436 if (!dump_file || !plats->alignment.known)
4437 continue;
4438 if (!dumped_sth)
4440 fprintf (dump_file, "Propagated alignment info for function %s/%i:\n",
4441 node->name (), node->order);
4442 dumped_sth = true;
4444 fprintf (dump_file, " param %i: align: %u, misalign: %u\n",
4445 i, plats->alignment.align, plats->alignment.misalign);
4450 /* The IPCP driver. */
4452 static unsigned int
4453 ipcp_driver (void)
4455 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
4456 struct cgraph_edge_hook_list *edge_removal_hook_holder;
4457 struct ipa_topo_info topo;
4459 ipa_check_create_node_params ();
4460 ipa_check_create_edge_args ();
4461 grow_edge_clone_vectors ();
4462 edge_duplication_hook_holder =
4463 symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
4464 edge_removal_hook_holder =
4465 symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
4467 ipcp_cst_values_pool = create_alloc_pool ("IPA-CP constant values",
4468 sizeof (ipcp_value<tree>), 32);
4469 ipcp_poly_ctx_values_pool = create_alloc_pool
4470 ("IPA-CP polymorphic contexts",
4471 sizeof (ipcp_value<ipa_polymorphic_call_context>), 32);
4472 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
4473 sizeof (ipcp_value_source<tree>), 64);
4474 ipcp_agg_lattice_pool = create_alloc_pool ("IPA_CP aggregate lattices",
4475 sizeof (struct ipcp_agg_lattice),
4476 32);
4477 if (dump_file)
4479 fprintf (dump_file, "\nIPA structures before propagation:\n");
4480 if (dump_flags & TDF_DETAILS)
4481 ipa_print_all_params (dump_file);
4482 ipa_print_all_jump_functions (dump_file);
4485 /* Topological sort. */
4486 build_toporder_info (&topo);
4487 /* Do the interprocedural propagation. */
4488 ipcp_propagate_stage (&topo);
4489 /* Decide what constant propagation and cloning should be performed. */
4490 ipcp_decision_stage (&topo);
4491 /* Store results of alignment propagation. */
4492 ipcp_store_alignment_results ();
4494 /* Free all IPCP structures. */
4495 free_toporder_info (&topo);
4496 next_edge_clone.release ();
4497 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4498 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4499 ipa_free_all_structures_after_ipa_cp ();
4500 if (dump_file)
4501 fprintf (dump_file, "\nIPA constant propagation end\n");
4502 return 0;
4505 /* Initialization and computation of IPCP data structures. This is the initial
4506 intraprocedural analysis of functions, which gathers information to be
4507 propagated later on. */
4509 static void
4510 ipcp_generate_summary (void)
4512 struct cgraph_node *node;
4514 if (dump_file)
4515 fprintf (dump_file, "\nIPA constant propagation start:\n");
4516 ipa_register_cgraph_hooks ();
4518 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4520 node->local.versionable
4521 = tree_versionable_function_p (node->decl);
4522 ipa_analyze_node (node);
4526 /* Write ipcp summary for nodes in SET. */
4528 static void
4529 ipcp_write_summary (void)
4531 ipa_prop_write_jump_functions ();
4534 /* Read ipcp summary. */
4536 static void
4537 ipcp_read_summary (void)
4539 ipa_prop_read_jump_functions ();
4542 namespace {
4544 const pass_data pass_data_ipa_cp =
4546 IPA_PASS, /* type */
4547 "cp", /* name */
4548 OPTGROUP_NONE, /* optinfo_flags */
4549 TV_IPA_CONSTANT_PROP, /* tv_id */
4550 0, /* properties_required */
4551 0, /* properties_provided */
4552 0, /* properties_destroyed */
4553 0, /* todo_flags_start */
4554 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
4557 class pass_ipa_cp : public ipa_opt_pass_d
4559 public:
4560 pass_ipa_cp (gcc::context *ctxt)
4561 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
4562 ipcp_generate_summary, /* generate_summary */
4563 ipcp_write_summary, /* write_summary */
4564 ipcp_read_summary, /* read_summary */
4565 ipcp_write_transformation_summaries, /*
4566 write_optimization_summary */
4567 ipcp_read_transformation_summaries, /*
4568 read_optimization_summary */
4569 NULL, /* stmt_fixup */
4570 0, /* function_transform_todo_flags_start */
4571 ipcp_transform_function, /* function_transform */
4572 NULL) /* variable_transform */
4575 /* opt_pass methods: */
4576 virtual bool gate (function *)
4578 /* FIXME: We should remove the optimize check after we ensure we never run
4579 IPA passes when not optimizing. */
4580 return (flag_ipa_cp && optimize) || in_lto_p;
4583 virtual unsigned int execute (function *) { return ipcp_driver (); }
4585 }; // class pass_ipa_cp
4587 } // anon namespace
4589 ipa_opt_pass_d *
4590 make_pass_ipa_cp (gcc::context *ctxt)
4592 return new pass_ipa_cp (ctxt);
4595 /* Reset all state within ipa-cp.c so that we can rerun the compiler
4596 within the same process. For use by toplev::finalize. */
4598 void
4599 ipa_cp_c_finalize (void)
4601 max_count = 0;
4602 overall_size = 0;
4603 max_new_size = 0;