Change use to type-based pool allocator in ipa-prop.c
[official-gcc.git] / gcc / ipa-cp.c
blob9f812fa5f5d266fd82c5a2312da330ba88c4a851
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 pool_allocator<ipcp_value<tree> > ipcp_cst_values_pool
295 ("IPA-CP constant values", 32);
297 pool_allocator<ipcp_value<ipa_polymorphic_call_context> >
298 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts", 32);
300 pool_allocator<ipcp_value_source<tree> > ipcp_sources_pool
301 ("IPA-CP value sources", 64);
303 pool_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
304 ("IPA_CP aggregate lattices", 32);
306 /* Maximal count found in program. */
308 static gcov_type max_count;
310 /* Original overall size of the program. */
312 static long overall_size, max_new_size;
314 /* Return the param lattices structure corresponding to the Ith formal
315 parameter of the function described by INFO. */
316 static inline struct ipcp_param_lattices *
317 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
319 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
320 gcc_checking_assert (!info->ipcp_orig_node);
321 gcc_checking_assert (info->lattices);
322 return &(info->lattices[i]);
325 /* Return the lattice corresponding to the scalar value of the Ith formal
326 parameter of the function described by INFO. */
327 static inline ipcp_lattice<tree> *
328 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
330 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
331 return &plats->itself;
334 /* Return the lattice corresponding to the scalar value of the Ith formal
335 parameter of the function described by INFO. */
336 static inline ipcp_lattice<ipa_polymorphic_call_context> *
337 ipa_get_poly_ctx_lat (struct ipa_node_params *info, int i)
339 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
340 return &plats->ctxlat;
343 /* Return whether LAT is a lattice with a single constant and without an
344 undefined value. */
346 template <typename valtype>
347 inline bool
348 ipcp_lattice<valtype>::is_single_const ()
350 if (bottom || contains_variable || values_count != 1)
351 return false;
352 else
353 return true;
356 /* Print V which is extracted from a value in a lattice to F. */
358 static void
359 print_ipcp_constant_value (FILE * f, tree v)
361 if (TREE_CODE (v) == ADDR_EXPR
362 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
364 fprintf (f, "& ");
365 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
367 else
368 print_generic_expr (f, v, 0);
371 /* Print V which is extracted from a value in a lattice to F. */
373 static void
374 print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
376 v.dump(f, false);
379 /* Print a lattice LAT to F. */
381 template <typename valtype>
382 void
383 ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
385 ipcp_value<valtype> *val;
386 bool prev = false;
388 if (bottom)
390 fprintf (f, "BOTTOM\n");
391 return;
394 if (!values_count && !contains_variable)
396 fprintf (f, "TOP\n");
397 return;
400 if (contains_variable)
402 fprintf (f, "VARIABLE");
403 prev = true;
404 if (dump_benefits)
405 fprintf (f, "\n");
408 for (val = values; val; val = val->next)
410 if (dump_benefits && prev)
411 fprintf (f, " ");
412 else if (!dump_benefits && prev)
413 fprintf (f, ", ");
414 else
415 prev = true;
417 print_ipcp_constant_value (f, val->value);
419 if (dump_sources)
421 ipcp_value_source<valtype> *s;
423 fprintf (f, " [from:");
424 for (s = val->sources; s; s = s->next)
425 fprintf (f, " %i(%i)", s->cs->caller->order,
426 s->cs->frequency);
427 fprintf (f, "]");
430 if (dump_benefits)
431 fprintf (f, " [loc_time: %i, loc_size: %i, "
432 "prop_time: %i, prop_size: %i]\n",
433 val->local_time_benefit, val->local_size_cost,
434 val->prop_time_benefit, val->prop_size_cost);
436 if (!dump_benefits)
437 fprintf (f, "\n");
440 /* Print all ipcp_lattices of all functions to F. */
442 static void
443 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
445 struct cgraph_node *node;
446 int i, count;
448 fprintf (f, "\nLattices:\n");
449 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
451 struct ipa_node_params *info;
453 info = IPA_NODE_REF (node);
454 fprintf (f, " Node: %s/%i:\n", node->name (),
455 node->order);
456 count = ipa_get_param_count (info);
457 for (i = 0; i < count; i++)
459 struct ipcp_agg_lattice *aglat;
460 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
461 fprintf (f, " param [%d]: ", i);
462 plats->itself.print (f, dump_sources, dump_benefits);
463 fprintf (f, " ctxs: ");
464 plats->ctxlat.print (f, dump_sources, dump_benefits);
465 if (plats->alignment.known && plats->alignment.align > 0)
466 fprintf (f, " Alignment %u, misalignment %u\n",
467 plats->alignment.align, plats->alignment.misalign);
468 else if (plats->alignment.known)
469 fprintf (f, " Alignment unusable\n");
470 else
471 fprintf (f, " Alignment unknown\n");
472 if (plats->virt_call)
473 fprintf (f, " virt_call flag set\n");
475 if (plats->aggs_bottom)
477 fprintf (f, " AGGS BOTTOM\n");
478 continue;
480 if (plats->aggs_contain_variable)
481 fprintf (f, " AGGS VARIABLE\n");
482 for (aglat = plats->aggs; aglat; aglat = aglat->next)
484 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
485 plats->aggs_by_ref ? "ref " : "", aglat->offset);
486 aglat->print (f, dump_sources, dump_benefits);
492 /* Determine whether it is at all technically possible to create clones of NODE
493 and store this information in the ipa_node_params structure associated
494 with NODE. */
496 static void
497 determine_versionability (struct cgraph_node *node)
499 const char *reason = NULL;
501 /* There are a number of generic reasons functions cannot be versioned. We
502 also cannot remove parameters if there are type attributes such as fnspec
503 present. */
504 if (node->alias || node->thunk.thunk_p)
505 reason = "alias or thunk";
506 else if (!node->local.versionable)
507 reason = "not a tree_versionable_function";
508 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
509 reason = "insufficient body availability";
510 else if (!opt_for_fn (node->decl, optimize)
511 || !opt_for_fn (node->decl, flag_ipa_cp))
512 reason = "non-optimized function";
513 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
515 /* Ideally we should clone the SIMD clones themselves and create
516 vector copies of them, so IPA-cp and SIMD clones can happily
517 coexist, but that may not be worth the effort. */
518 reason = "function has SIMD clones";
520 /* Don't clone decls local to a comdat group; it breaks and for C++
521 decloned constructors, inlining is always better anyway. */
522 else if (node->comdat_local_p ())
523 reason = "comdat-local function";
525 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
526 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
527 node->name (), node->order, reason);
529 node->local.versionable = (reason == NULL);
532 /* Return true if it is at all technically possible to create clones of a
533 NODE. */
535 static bool
536 ipcp_versionable_function_p (struct cgraph_node *node)
538 return node->local.versionable;
541 /* Structure holding accumulated information about callers of a node. */
543 struct caller_statistics
545 gcov_type count_sum;
546 int n_calls, n_hot_calls, freq_sum;
549 /* Initialize fields of STAT to zeroes. */
551 static inline void
552 init_caller_stats (struct caller_statistics *stats)
554 stats->count_sum = 0;
555 stats->n_calls = 0;
556 stats->n_hot_calls = 0;
557 stats->freq_sum = 0;
560 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
561 non-thunk incoming edges to NODE. */
563 static bool
564 gather_caller_stats (struct cgraph_node *node, void *data)
566 struct caller_statistics *stats = (struct caller_statistics *) data;
567 struct cgraph_edge *cs;
569 for (cs = node->callers; cs; cs = cs->next_caller)
570 if (!cs->caller->thunk.thunk_p)
572 stats->count_sum += cs->count;
573 stats->freq_sum += cs->frequency;
574 stats->n_calls++;
575 if (cs->maybe_hot_p ())
576 stats->n_hot_calls ++;
578 return false;
582 /* Return true if this NODE is viable candidate for cloning. */
584 static bool
585 ipcp_cloning_candidate_p (struct cgraph_node *node)
587 struct caller_statistics stats;
589 gcc_checking_assert (node->has_gimple_body_p ());
591 if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
593 if (dump_file)
594 fprintf (dump_file, "Not considering %s for cloning; "
595 "-fipa-cp-clone disabled.\n",
596 node->name ());
597 return false;
600 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
602 if (dump_file)
603 fprintf (dump_file, "Not considering %s for cloning; "
604 "optimizing it for size.\n",
605 node->name ());
606 return false;
609 init_caller_stats (&stats);
610 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
612 if (inline_summaries->get (node)->self_size < stats.n_calls)
614 if (dump_file)
615 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
616 node->name ());
617 return true;
620 /* When profile is available and function is hot, propagate into it even if
621 calls seems cold; constant propagation can improve function's speed
622 significantly. */
623 if (max_count)
625 if (stats.count_sum > node->count * 90 / 100)
627 if (dump_file)
628 fprintf (dump_file, "Considering %s for cloning; "
629 "usually called directly.\n",
630 node->name ());
631 return true;
634 if (!stats.n_hot_calls)
636 if (dump_file)
637 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
638 node->name ());
639 return false;
641 if (dump_file)
642 fprintf (dump_file, "Considering %s for cloning.\n",
643 node->name ());
644 return true;
647 template <typename valtype>
648 class value_topo_info
650 public:
651 /* Head of the linked list of topologically sorted values. */
652 ipcp_value<valtype> *values_topo;
653 /* Stack for creating SCCs, represented by a linked list too. */
654 ipcp_value<valtype> *stack;
655 /* Counter driving the algorithm in add_val_to_toposort. */
656 int dfs_counter;
658 value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
660 void add_val (ipcp_value<valtype> *cur_val);
661 void propagate_effects ();
664 /* Arrays representing a topological ordering of call graph nodes and a stack
665 of nodes used during constant propagation and also data required to perform
666 topological sort of values and propagation of benefits in the determined
667 order. */
669 class ipa_topo_info
671 public:
672 /* Array with obtained topological order of cgraph nodes. */
673 struct cgraph_node **order;
674 /* Stack of cgraph nodes used during propagation within SCC until all values
675 in the SCC stabilize. */
676 struct cgraph_node **stack;
677 int nnodes, stack_top;
679 value_topo_info<tree> constants;
680 value_topo_info<ipa_polymorphic_call_context> contexts;
682 ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
683 constants ()
687 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
689 static void
690 build_toporder_info (struct ipa_topo_info *topo)
692 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
693 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
695 gcc_checking_assert (topo->stack_top == 0);
696 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
699 /* Free information about strongly connected components and the arrays in
700 TOPO. */
702 static void
703 free_toporder_info (struct ipa_topo_info *topo)
705 ipa_free_postorder_info ();
706 free (topo->order);
707 free (topo->stack);
710 /* Add NODE to the stack in TOPO, unless it is already there. */
712 static inline void
713 push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node)
715 struct ipa_node_params *info = IPA_NODE_REF (node);
716 if (info->node_enqueued)
717 return;
718 info->node_enqueued = 1;
719 topo->stack[topo->stack_top++] = node;
722 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
723 is empty. */
725 static struct cgraph_node *
726 pop_node_from_stack (struct ipa_topo_info *topo)
728 if (topo->stack_top)
730 struct cgraph_node *node;
731 topo->stack_top--;
732 node = topo->stack[topo->stack_top];
733 IPA_NODE_REF (node)->node_enqueued = 0;
734 return node;
736 else
737 return NULL;
740 /* Set lattice LAT to bottom and return true if it previously was not set as
741 such. */
743 template <typename valtype>
744 inline bool
745 ipcp_lattice<valtype>::set_to_bottom ()
747 bool ret = !bottom;
748 bottom = true;
749 return ret;
752 /* Mark lattice as containing an unknown value and return true if it previously
753 was not marked as such. */
755 template <typename valtype>
756 inline bool
757 ipcp_lattice<valtype>::set_contains_variable ()
759 bool ret = !contains_variable;
760 contains_variable = true;
761 return ret;
764 /* Set all aggegate lattices in PLATS to bottom and return true if they were
765 not previously set as such. */
767 static inline bool
768 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
770 bool ret = !plats->aggs_bottom;
771 plats->aggs_bottom = true;
772 return ret;
775 /* Mark all aggegate lattices in PLATS as containing an unknown value and
776 return true if they were not previously marked as such. */
778 static inline bool
779 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
781 bool ret = !plats->aggs_contain_variable;
782 plats->aggs_contain_variable = true;
783 return ret;
786 /* Return true if alignment information in PLATS is known to be unusable. */
788 static inline bool
789 alignment_bottom_p (ipcp_param_lattices *plats)
791 return plats->alignment.known && (plats->alignment.align == 0);
794 /* Set alignment information in PLATS to unusable. Return true if it
795 previously was usable or unknown. */
797 static inline bool
798 set_alignment_to_bottom (ipcp_param_lattices *plats)
800 if (alignment_bottom_p (plats))
801 return false;
802 plats->alignment.known = true;
803 plats->alignment.align = 0;
804 return true;
807 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
808 return true is any of them has not been marked as such so far. */
810 static inline bool
811 set_all_contains_variable (struct ipcp_param_lattices *plats)
813 bool ret;
814 ret = plats->itself.set_contains_variable ();
815 ret |= plats->ctxlat.set_contains_variable ();
816 ret |= set_agg_lats_contain_variable (plats);
817 ret |= set_alignment_to_bottom (plats);
818 return ret;
821 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
822 points to by the number of callers to NODE. */
824 static bool
825 count_callers (cgraph_node *node, void *data)
827 int *caller_count = (int *) data;
829 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
830 /* Local thunks can be handled transparently, but if the thunk can not
831 be optimized out, count it as a real use. */
832 if (!cs->caller->thunk.thunk_p || !cs->caller->local.local)
833 ++*caller_count;
834 return false;
837 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
838 the one caller of some other node. Set the caller's corresponding flag. */
840 static bool
841 set_single_call_flag (cgraph_node *node, void *)
843 cgraph_edge *cs = node->callers;
844 /* Local thunks can be handled transparently, skip them. */
845 while (cs && cs->caller->thunk.thunk_p && cs->caller->local.local)
846 cs = cs->next_caller;
847 if (cs)
849 IPA_NODE_REF (cs->caller)->node_calling_single_call = true;
850 return true;
852 return false;
855 /* Initialize ipcp_lattices. */
857 static void
858 initialize_node_lattices (struct cgraph_node *node)
860 struct ipa_node_params *info = IPA_NODE_REF (node);
861 struct cgraph_edge *ie;
862 bool disable = false, variable = false;
863 int i;
865 gcc_checking_assert (node->has_gimple_body_p ());
866 if (cgraph_local_p (node))
868 int caller_count = 0;
869 node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
870 true);
871 gcc_checking_assert (caller_count > 0);
872 if (caller_count == 1)
873 node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
874 NULL, true);
876 else
878 /* When cloning is allowed, we can assume that externally visible
879 functions are not called. We will compensate this by cloning
880 later. */
881 if (ipcp_versionable_function_p (node)
882 && ipcp_cloning_candidate_p (node))
883 variable = true;
884 else
885 disable = true;
888 if (disable || variable)
890 for (i = 0; i < ipa_get_param_count (info) ; i++)
892 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
893 if (disable)
895 plats->itself.set_to_bottom ();
896 plats->ctxlat.set_to_bottom ();
897 set_agg_lats_to_bottom (plats);
898 set_alignment_to_bottom (plats);
900 else
901 set_all_contains_variable (plats);
903 if (dump_file && (dump_flags & TDF_DETAILS)
904 && !node->alias && !node->thunk.thunk_p)
905 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
906 node->name (), node->order,
907 disable ? "BOTTOM" : "VARIABLE");
910 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
911 if (ie->indirect_info->polymorphic
912 && ie->indirect_info->param_index >= 0)
914 gcc_checking_assert (ie->indirect_info->param_index >= 0);
915 ipa_get_parm_lattices (info,
916 ie->indirect_info->param_index)->virt_call = 1;
920 /* Return the result of a (possibly arithmetic) pass through jump function
921 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
922 determined or be considered an interprocedural invariant. */
924 static tree
925 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
927 tree restype, res;
929 gcc_checking_assert (is_gimple_ip_invariant (input));
930 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
931 return input;
933 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
934 == tcc_comparison)
935 restype = boolean_type_node;
936 else
937 restype = TREE_TYPE (input);
938 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
939 input, ipa_get_jf_pass_through_operand (jfunc));
941 if (res && !is_gimple_ip_invariant (res))
942 return NULL_TREE;
944 return res;
947 /* Return the result of an ancestor jump function JFUNC on the constant value
948 INPUT. Return NULL_TREE if that cannot be determined. */
950 static tree
951 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
953 gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
954 if (TREE_CODE (input) == ADDR_EXPR)
956 tree t = TREE_OPERAND (input, 0);
957 t = build_ref_for_offset (EXPR_LOCATION (t), t,
958 ipa_get_jf_ancestor_offset (jfunc),
959 ptr_type_node, NULL, false);
960 return build_fold_addr_expr (t);
962 else
963 return NULL_TREE;
966 /* Determine whether JFUNC evaluates to a single known constant value and if
967 so, return it. Otherwise return NULL. INFO describes the caller node or
968 the one it is inlined to, so that pass-through jump functions can be
969 evaluated. */
971 tree
972 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
974 if (jfunc->type == IPA_JF_CONST)
975 return ipa_get_jf_constant (jfunc);
976 else if (jfunc->type == IPA_JF_PASS_THROUGH
977 || jfunc->type == IPA_JF_ANCESTOR)
979 tree input;
980 int idx;
982 if (jfunc->type == IPA_JF_PASS_THROUGH)
983 idx = ipa_get_jf_pass_through_formal_id (jfunc);
984 else
985 idx = ipa_get_jf_ancestor_formal_id (jfunc);
987 if (info->ipcp_orig_node)
988 input = info->known_csts[idx];
989 else
991 ipcp_lattice<tree> *lat;
993 if (!info->lattices
994 || idx >= ipa_get_param_count (info))
995 return NULL_TREE;
996 lat = ipa_get_scalar_lat (info, idx);
997 if (!lat->is_single_const ())
998 return NULL_TREE;
999 input = lat->values->value;
1002 if (!input)
1003 return NULL_TREE;
1005 if (jfunc->type == IPA_JF_PASS_THROUGH)
1006 return ipa_get_jf_pass_through_result (jfunc, input);
1007 else
1008 return ipa_get_jf_ancestor_result (jfunc, input);
1010 else
1011 return NULL_TREE;
1014 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1015 that INFO describes the caller node or the one it is inlined to, CS is the
1016 call graph edge corresponding to JFUNC and CSIDX index of the described
1017 parameter. */
1019 ipa_polymorphic_call_context
1020 ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1021 ipa_jump_func *jfunc)
1023 ipa_edge_args *args = IPA_EDGE_REF (cs);
1024 ipa_polymorphic_call_context ctx;
1025 ipa_polymorphic_call_context *edge_ctx
1026 = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1028 if (edge_ctx && !edge_ctx->useless_p ())
1029 ctx = *edge_ctx;
1031 if (jfunc->type == IPA_JF_PASS_THROUGH
1032 || jfunc->type == IPA_JF_ANCESTOR)
1034 ipa_polymorphic_call_context srcctx;
1035 int srcidx;
1036 bool type_preserved = true;
1037 if (jfunc->type == IPA_JF_PASS_THROUGH)
1039 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1040 return ctx;
1041 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1042 srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1044 else
1046 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1047 srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1049 if (info->ipcp_orig_node)
1051 if (info->known_contexts.exists ())
1052 srcctx = info->known_contexts[srcidx];
1054 else
1056 if (!info->lattices
1057 || srcidx >= ipa_get_param_count (info))
1058 return ctx;
1059 ipcp_lattice<ipa_polymorphic_call_context> *lat;
1060 lat = ipa_get_poly_ctx_lat (info, srcidx);
1061 if (!lat->is_single_const ())
1062 return ctx;
1063 srcctx = lat->values->value;
1065 if (srcctx.useless_p ())
1066 return ctx;
1067 if (jfunc->type == IPA_JF_ANCESTOR)
1068 srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1069 if (!type_preserved)
1070 srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1071 srcctx.combine_with (ctx);
1072 return srcctx;
1075 return ctx;
1078 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1079 bottom, not containing a variable component and without any known value at
1080 the same time. */
1082 DEBUG_FUNCTION void
1083 ipcp_verify_propagated_values (void)
1085 struct cgraph_node *node;
1087 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1089 struct ipa_node_params *info = IPA_NODE_REF (node);
1090 int i, count = ipa_get_param_count (info);
1092 for (i = 0; i < count; i++)
1094 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
1096 if (!lat->bottom
1097 && !lat->contains_variable
1098 && lat->values_count == 0)
1100 if (dump_file)
1102 symtab_node::dump_table (dump_file);
1103 fprintf (dump_file, "\nIPA lattices after constant "
1104 "propagation, before gcc_unreachable:\n");
1105 print_all_lattices (dump_file, true, false);
1108 gcc_unreachable ();
1114 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1116 static bool
1117 values_equal_for_ipcp_p (tree x, tree y)
1119 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
1121 if (x == y)
1122 return true;
1124 if (TREE_CODE (x) == ADDR_EXPR
1125 && TREE_CODE (y) == ADDR_EXPR
1126 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
1127 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
1128 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
1129 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
1130 else
1131 return operand_equal_p (x, y, 0);
1134 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1136 static bool
1137 values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
1138 ipa_polymorphic_call_context y)
1140 return x.equal_to (y);
1144 /* Add a new value source to the value represented by THIS, marking that a
1145 value comes from edge CS and (if the underlying jump function is a
1146 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1147 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1148 scalar value of the parameter itself or the offset within an aggregate. */
1150 template <typename valtype>
1151 void
1152 ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
1153 int src_idx, HOST_WIDE_INT offset)
1155 ipcp_value_source<valtype> *src;
1157 src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
1158 src->offset = offset;
1159 src->cs = cs;
1160 src->val = src_val;
1161 src->index = src_idx;
1163 src->next = sources;
1164 sources = src;
1167 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1168 SOURCE and clear all other fields. */
1170 static ipcp_value<tree> *
1171 allocate_and_init_ipcp_value (tree source)
1173 ipcp_value<tree> *val;
1175 val = ipcp_cst_values_pool.allocate ();
1176 memset (val, 0, sizeof (*val));
1177 val->value = source;
1178 return val;
1181 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1182 value to SOURCE and clear all other fields. */
1184 static ipcp_value<ipa_polymorphic_call_context> *
1185 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source)
1187 ipcp_value<ipa_polymorphic_call_context> *val;
1189 // TODO
1190 val = ipcp_poly_ctx_values_pool.allocate ();
1191 memset (val, 0, sizeof (*val));
1192 val->value = source;
1193 return val;
1196 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1197 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1198 meaning. OFFSET -1 means the source is scalar and not a part of an
1199 aggregate. */
1201 template <typename valtype>
1202 bool
1203 ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
1204 ipcp_value<valtype> *src_val,
1205 int src_idx, HOST_WIDE_INT offset)
1207 ipcp_value<valtype> *val;
1209 if (bottom)
1210 return false;
1212 for (val = values; val; val = val->next)
1213 if (values_equal_for_ipcp_p (val->value, newval))
1215 if (ipa_edge_within_scc (cs))
1217 ipcp_value_source<valtype> *s;
1218 for (s = val->sources; s ; s = s->next)
1219 if (s->cs == cs)
1220 break;
1221 if (s)
1222 return false;
1225 val->add_source (cs, src_val, src_idx, offset);
1226 return false;
1229 if (values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
1231 /* We can only free sources, not the values themselves, because sources
1232 of other values in this this SCC might point to them. */
1233 for (val = values; val; val = val->next)
1235 while (val->sources)
1237 ipcp_value_source<valtype> *src = val->sources;
1238 val->sources = src->next;
1239 ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
1243 values = NULL;
1244 return set_to_bottom ();
1247 values_count++;
1248 val = allocate_and_init_ipcp_value (newval);
1249 val->add_source (cs, src_val, src_idx, offset);
1250 val->next = values;
1251 values = val;
1252 return true;
1255 /* Propagate values through a pass-through jump function JFUNC associated with
1256 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1257 is the index of the source parameter. */
1259 static bool
1260 propagate_vals_accross_pass_through (cgraph_edge *cs,
1261 ipa_jump_func *jfunc,
1262 ipcp_lattice<tree> *src_lat,
1263 ipcp_lattice<tree> *dest_lat,
1264 int src_idx)
1266 ipcp_value<tree> *src_val;
1267 bool ret = false;
1269 /* Do not create new values when propagating within an SCC because if there
1270 are arithmetic functions with circular dependencies, there is infinite
1271 number of them and we would just make lattices bottom. */
1272 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1273 && ipa_edge_within_scc (cs))
1274 ret = dest_lat->set_contains_variable ();
1275 else
1276 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1278 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1280 if (cstval)
1281 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx);
1282 else
1283 ret |= dest_lat->set_contains_variable ();
1286 return ret;
1289 /* Propagate values through an ancestor jump function JFUNC associated with
1290 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1291 is the index of the source parameter. */
1293 static bool
1294 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1295 struct ipa_jump_func *jfunc,
1296 ipcp_lattice<tree> *src_lat,
1297 ipcp_lattice<tree> *dest_lat,
1298 int src_idx)
1300 ipcp_value<tree> *src_val;
1301 bool ret = false;
1303 if (ipa_edge_within_scc (cs))
1304 return dest_lat->set_contains_variable ();
1306 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1308 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1310 if (t)
1311 ret |= dest_lat->add_value (t, cs, src_val, src_idx);
1312 else
1313 ret |= dest_lat->set_contains_variable ();
1316 return ret;
1319 /* Propagate scalar values across jump function JFUNC that is associated with
1320 edge CS and put the values into DEST_LAT. */
1322 static bool
1323 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1324 struct ipa_jump_func *jfunc,
1325 ipcp_lattice<tree> *dest_lat)
1327 if (dest_lat->bottom)
1328 return false;
1330 if (jfunc->type == IPA_JF_CONST)
1332 tree val = ipa_get_jf_constant (jfunc);
1333 return dest_lat->add_value (val, cs, NULL, 0);
1335 else if (jfunc->type == IPA_JF_PASS_THROUGH
1336 || jfunc->type == IPA_JF_ANCESTOR)
1338 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1339 ipcp_lattice<tree> *src_lat;
1340 int src_idx;
1341 bool ret;
1343 if (jfunc->type == IPA_JF_PASS_THROUGH)
1344 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1345 else
1346 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1348 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1349 if (src_lat->bottom)
1350 return dest_lat->set_contains_variable ();
1352 /* If we would need to clone the caller and cannot, do not propagate. */
1353 if (!ipcp_versionable_function_p (cs->caller)
1354 && (src_lat->contains_variable
1355 || (src_lat->values_count > 1)))
1356 return dest_lat->set_contains_variable ();
1358 if (jfunc->type == IPA_JF_PASS_THROUGH)
1359 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1360 dest_lat, src_idx);
1361 else
1362 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1363 src_idx);
1365 if (src_lat->contains_variable)
1366 ret |= dest_lat->set_contains_variable ();
1368 return ret;
1371 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1372 use it for indirect inlining), we should propagate them too. */
1373 return dest_lat->set_contains_variable ();
1376 /* Propagate scalar values across jump function JFUNC that is associated with
1377 edge CS and describes argument IDX and put the values into DEST_LAT. */
1379 static bool
1380 propagate_context_accross_jump_function (cgraph_edge *cs,
1381 ipa_jump_func *jfunc, int idx,
1382 ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
1384 ipa_edge_args *args = IPA_EDGE_REF (cs);
1385 if (dest_lat->bottom)
1386 return false;
1387 bool ret = false;
1388 bool added_sth = false;
1389 bool type_preserved = true;
1391 ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
1392 = ipa_get_ith_polymorhic_call_context (args, idx);
1394 if (edge_ctx_ptr)
1395 edge_ctx = *edge_ctx_ptr;
1397 if (jfunc->type == IPA_JF_PASS_THROUGH
1398 || jfunc->type == IPA_JF_ANCESTOR)
1400 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1401 int src_idx;
1402 ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
1404 /* TODO: Once we figure out how to propagate speculations, it will
1405 probably be a good idea to switch to speculation if type_preserved is
1406 not set instead of punting. */
1407 if (jfunc->type == IPA_JF_PASS_THROUGH)
1409 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1410 goto prop_fail;
1411 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1412 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1414 else
1416 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1417 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1420 src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
1421 /* If we would need to clone the caller and cannot, do not propagate. */
1422 if (!ipcp_versionable_function_p (cs->caller)
1423 && (src_lat->contains_variable
1424 || (src_lat->values_count > 1)))
1425 goto prop_fail;
1427 ipcp_value<ipa_polymorphic_call_context> *src_val;
1428 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1430 ipa_polymorphic_call_context cur = src_val->value;
1432 if (!type_preserved)
1433 cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1434 if (jfunc->type == IPA_JF_ANCESTOR)
1435 cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1436 /* TODO: In cases we know how the context is going to be used,
1437 we can improve the result by passing proper OTR_TYPE. */
1438 cur.combine_with (edge_ctx);
1439 if (!cur.useless_p ())
1441 if (src_lat->contains_variable
1442 && !edge_ctx.equal_to (cur))
1443 ret |= dest_lat->set_contains_variable ();
1444 ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
1445 added_sth = true;
1451 prop_fail:
1452 if (!added_sth)
1454 if (!edge_ctx.useless_p ())
1455 ret |= dest_lat->add_value (edge_ctx, cs);
1456 else
1457 ret |= dest_lat->set_contains_variable ();
1460 return ret;
1463 /* Propagate alignments across jump function JFUNC that is associated with
1464 edge CS and update DEST_LAT accordingly. */
1466 static bool
1467 propagate_alignment_accross_jump_function (struct cgraph_edge *cs,
1468 struct ipa_jump_func *jfunc,
1469 struct ipcp_param_lattices *dest_lat)
1471 if (alignment_bottom_p (dest_lat))
1472 return false;
1474 ipa_alignment cur;
1475 cur.known = false;
1476 if (jfunc->alignment.known)
1477 cur = jfunc->alignment;
1478 else if (jfunc->type == IPA_JF_PASS_THROUGH
1479 || jfunc->type == IPA_JF_ANCESTOR)
1481 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1482 struct ipcp_param_lattices *src_lats;
1483 HOST_WIDE_INT offset = 0;
1484 int src_idx;
1486 if (jfunc->type == IPA_JF_PASS_THROUGH)
1488 enum tree_code op = ipa_get_jf_pass_through_operation (jfunc);
1489 if (op != NOP_EXPR)
1491 if (op != POINTER_PLUS_EXPR
1492 && op != PLUS_EXPR)
1493 goto prop_fail;
1494 tree operand = ipa_get_jf_pass_through_operand (jfunc);
1495 if (!tree_fits_shwi_p (operand))
1496 goto prop_fail;
1497 offset = tree_to_shwi (operand);
1499 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1501 else
1503 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1504 offset = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;;
1507 src_lats = ipa_get_parm_lattices (caller_info, src_idx);
1508 if (!src_lats->alignment.known
1509 || alignment_bottom_p (src_lats))
1510 goto prop_fail;
1512 cur = src_lats->alignment;
1513 cur.misalign = (cur.misalign + offset) % cur.align;
1516 if (cur.known)
1518 if (!dest_lat->alignment.known)
1520 dest_lat->alignment = cur;
1521 return true;
1523 else if (dest_lat->alignment.align == cur.align
1524 && dest_lat->alignment.misalign == cur.misalign)
1525 return false;
1528 prop_fail:
1529 set_alignment_to_bottom (dest_lat);
1530 return true;
1533 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1534 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1535 other cases, return false). If there are no aggregate items, set
1536 aggs_by_ref to NEW_AGGS_BY_REF. */
1538 static bool
1539 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1540 bool new_aggs_by_ref)
1542 if (dest_plats->aggs)
1544 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1546 set_agg_lats_to_bottom (dest_plats);
1547 return true;
1550 else
1551 dest_plats->aggs_by_ref = new_aggs_by_ref;
1552 return false;
1555 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1556 already existing lattice for the given OFFSET and SIZE, marking all skipped
1557 lattices as containing variable and checking for overlaps. If there is no
1558 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1559 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1560 unless there are too many already. If there are two many, return false. If
1561 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1562 skipped lattices were newly marked as containing variable, set *CHANGE to
1563 true. */
1565 static bool
1566 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1567 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1568 struct ipcp_agg_lattice ***aglat,
1569 bool pre_existing, bool *change)
1571 gcc_checking_assert (offset >= 0);
1573 while (**aglat && (**aglat)->offset < offset)
1575 if ((**aglat)->offset + (**aglat)->size > offset)
1577 set_agg_lats_to_bottom (dest_plats);
1578 return false;
1580 *change |= (**aglat)->set_contains_variable ();
1581 *aglat = &(**aglat)->next;
1584 if (**aglat && (**aglat)->offset == offset)
1586 if ((**aglat)->size != val_size
1587 || ((**aglat)->next
1588 && (**aglat)->next->offset < offset + val_size))
1590 set_agg_lats_to_bottom (dest_plats);
1591 return false;
1593 gcc_checking_assert (!(**aglat)->next
1594 || (**aglat)->next->offset >= offset + val_size);
1595 return true;
1597 else
1599 struct ipcp_agg_lattice *new_al;
1601 if (**aglat && (**aglat)->offset < offset + val_size)
1603 set_agg_lats_to_bottom (dest_plats);
1604 return false;
1606 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1607 return false;
1608 dest_plats->aggs_count++;
1609 new_al = ipcp_agg_lattice_pool.allocate ();
1610 memset (new_al, 0, sizeof (*new_al));
1612 new_al->offset = offset;
1613 new_al->size = val_size;
1614 new_al->contains_variable = pre_existing;
1616 new_al->next = **aglat;
1617 **aglat = new_al;
1618 return true;
1622 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1623 containing an unknown value. */
1625 static bool
1626 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1628 bool ret = false;
1629 while (aglat)
1631 ret |= aglat->set_contains_variable ();
1632 aglat = aglat->next;
1634 return ret;
1637 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1638 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1639 parameter used for lattice value sources. Return true if DEST_PLATS changed
1640 in any way. */
1642 static bool
1643 merge_aggregate_lattices (struct cgraph_edge *cs,
1644 struct ipcp_param_lattices *dest_plats,
1645 struct ipcp_param_lattices *src_plats,
1646 int src_idx, HOST_WIDE_INT offset_delta)
1648 bool pre_existing = dest_plats->aggs != NULL;
1649 struct ipcp_agg_lattice **dst_aglat;
1650 bool ret = false;
1652 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1653 return true;
1654 if (src_plats->aggs_bottom)
1655 return set_agg_lats_contain_variable (dest_plats);
1656 if (src_plats->aggs_contain_variable)
1657 ret |= set_agg_lats_contain_variable (dest_plats);
1658 dst_aglat = &dest_plats->aggs;
1660 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1661 src_aglat;
1662 src_aglat = src_aglat->next)
1664 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1666 if (new_offset < 0)
1667 continue;
1668 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1669 &dst_aglat, pre_existing, &ret))
1671 struct ipcp_agg_lattice *new_al = *dst_aglat;
1673 dst_aglat = &(*dst_aglat)->next;
1674 if (src_aglat->bottom)
1676 ret |= new_al->set_contains_variable ();
1677 continue;
1679 if (src_aglat->contains_variable)
1680 ret |= new_al->set_contains_variable ();
1681 for (ipcp_value<tree> *val = src_aglat->values;
1682 val;
1683 val = val->next)
1684 ret |= new_al->add_value (val->value, cs, val, src_idx,
1685 src_aglat->offset);
1687 else if (dest_plats->aggs_bottom)
1688 return true;
1690 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1691 return ret;
1694 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1695 pass-through JFUNC and if so, whether it has conform and conforms to the
1696 rules about propagating values passed by reference. */
1698 static bool
1699 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1700 struct ipa_jump_func *jfunc)
1702 return src_plats->aggs
1703 && (!src_plats->aggs_by_ref
1704 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1707 /* Propagate scalar values across jump function JFUNC that is associated with
1708 edge CS and put the values into DEST_LAT. */
1710 static bool
1711 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1712 struct ipa_jump_func *jfunc,
1713 struct ipcp_param_lattices *dest_plats)
1715 bool ret = false;
1717 if (dest_plats->aggs_bottom)
1718 return false;
1720 if (jfunc->type == IPA_JF_PASS_THROUGH
1721 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1723 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1724 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1725 struct ipcp_param_lattices *src_plats;
1727 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1728 if (agg_pass_through_permissible_p (src_plats, jfunc))
1730 /* Currently we do not produce clobber aggregate jump
1731 functions, replace with merging when we do. */
1732 gcc_assert (!jfunc->agg.items);
1733 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1734 src_idx, 0);
1736 else
1737 ret |= set_agg_lats_contain_variable (dest_plats);
1739 else if (jfunc->type == IPA_JF_ANCESTOR
1740 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1742 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1743 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1744 struct ipcp_param_lattices *src_plats;
1746 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1747 if (src_plats->aggs && src_plats->aggs_by_ref)
1749 /* Currently we do not produce clobber aggregate jump
1750 functions, replace with merging when we do. */
1751 gcc_assert (!jfunc->agg.items);
1752 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1753 ipa_get_jf_ancestor_offset (jfunc));
1755 else if (!src_plats->aggs_by_ref)
1756 ret |= set_agg_lats_to_bottom (dest_plats);
1757 else
1758 ret |= set_agg_lats_contain_variable (dest_plats);
1760 else if (jfunc->agg.items)
1762 bool pre_existing = dest_plats->aggs != NULL;
1763 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1764 struct ipa_agg_jf_item *item;
1765 int i;
1767 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1768 return true;
1770 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1772 HOST_WIDE_INT val_size;
1774 if (item->offset < 0)
1775 continue;
1776 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1777 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1779 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1780 &aglat, pre_existing, &ret))
1782 ret |= (*aglat)->add_value (item->value, cs, NULL, 0, 0);
1783 aglat = &(*aglat)->next;
1785 else if (dest_plats->aggs_bottom)
1786 return true;
1789 ret |= set_chain_of_aglats_contains_variable (*aglat);
1791 else
1792 ret |= set_agg_lats_contain_variable (dest_plats);
1794 return ret;
1797 /* Propagate constants from the caller to the callee of CS. INFO describes the
1798 caller. */
1800 static bool
1801 propagate_constants_accross_call (struct cgraph_edge *cs)
1803 struct ipa_node_params *callee_info;
1804 enum availability availability;
1805 struct cgraph_node *callee, *alias_or_thunk;
1806 struct ipa_edge_args *args;
1807 bool ret = false;
1808 int i, args_count, parms_count;
1810 callee = cs->callee->function_symbol (&availability);
1811 if (!callee->definition)
1812 return false;
1813 gcc_checking_assert (callee->has_gimple_body_p ());
1814 callee_info = IPA_NODE_REF (callee);
1816 args = IPA_EDGE_REF (cs);
1817 args_count = ipa_get_cs_argument_count (args);
1818 parms_count = ipa_get_param_count (callee_info);
1819 if (parms_count == 0)
1820 return false;
1822 /* No propagation through instrumentation thunks is available yet.
1823 It should be possible with proper mapping of call args and
1824 instrumented callee params in the propagation loop below. But
1825 this case mostly occurs when legacy code calls instrumented code
1826 and it is not a primary target for optimizations.
1827 We detect instrumentation thunks in aliases and thunks chain by
1828 checking instrumentation_clone flag for chain source and target.
1829 Going through instrumentation thunks we always have it changed
1830 from 0 to 1 and all other nodes do not change it. */
1831 if (!cs->callee->instrumentation_clone
1832 && callee->instrumentation_clone)
1834 for (i = 0; i < parms_count; i++)
1835 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1836 i));
1837 return ret;
1840 /* If this call goes through a thunk we must not propagate to the first (0th)
1841 parameter. However, we might need to uncover a thunk from below a series
1842 of aliases first. */
1843 alias_or_thunk = cs->callee;
1844 while (alias_or_thunk->alias)
1845 alias_or_thunk = alias_or_thunk->get_alias_target ();
1846 if (alias_or_thunk->thunk.thunk_p)
1848 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1849 0));
1850 i = 1;
1852 else
1853 i = 0;
1855 for (; (i < args_count) && (i < parms_count); i++)
1857 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1858 struct ipcp_param_lattices *dest_plats;
1860 dest_plats = ipa_get_parm_lattices (callee_info, i);
1861 if (availability == AVAIL_INTERPOSABLE)
1862 ret |= set_all_contains_variable (dest_plats);
1863 else
1865 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1866 &dest_plats->itself);
1867 ret |= propagate_context_accross_jump_function (cs, jump_func, i,
1868 &dest_plats->ctxlat);
1869 ret |= propagate_alignment_accross_jump_function (cs, jump_func,
1870 dest_plats);
1871 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1872 dest_plats);
1875 for (; i < parms_count; i++)
1876 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1878 return ret;
1881 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1882 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
1883 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
1885 static tree
1886 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1887 vec<tree> known_csts,
1888 vec<ipa_polymorphic_call_context> known_contexts,
1889 vec<ipa_agg_jump_function_p> known_aggs,
1890 struct ipa_agg_replacement_value *agg_reps,
1891 bool *speculative)
1893 int param_index = ie->indirect_info->param_index;
1894 HOST_WIDE_INT anc_offset;
1895 tree t;
1896 tree target = NULL;
1898 *speculative = false;
1900 if (param_index == -1
1901 || known_csts.length () <= (unsigned int) param_index)
1902 return NULL_TREE;
1904 if (!ie->indirect_info->polymorphic)
1906 tree t;
1908 if (ie->indirect_info->agg_contents)
1910 if (agg_reps)
1912 t = NULL;
1913 while (agg_reps)
1915 if (agg_reps->index == param_index
1916 && agg_reps->offset == ie->indirect_info->offset
1917 && agg_reps->by_ref == ie->indirect_info->by_ref)
1919 t = agg_reps->value;
1920 break;
1922 agg_reps = agg_reps->next;
1925 else if (known_aggs.length () > (unsigned int) param_index)
1927 struct ipa_agg_jump_function *agg;
1928 agg = known_aggs[param_index];
1929 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1930 ie->indirect_info->by_ref);
1932 else
1933 t = NULL;
1935 else
1936 t = known_csts[param_index];
1938 if (t &&
1939 TREE_CODE (t) == ADDR_EXPR
1940 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1941 return TREE_OPERAND (t, 0);
1942 else
1943 return NULL_TREE;
1946 if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
1947 return NULL_TREE;
1949 gcc_assert (!ie->indirect_info->agg_contents);
1950 anc_offset = ie->indirect_info->offset;
1952 t = NULL;
1954 /* Try to work out value of virtual table pointer value in replacemnets. */
1955 if (!t && agg_reps && !ie->indirect_info->by_ref)
1957 while (agg_reps)
1959 if (agg_reps->index == param_index
1960 && agg_reps->offset == ie->indirect_info->offset
1961 && agg_reps->by_ref)
1963 t = agg_reps->value;
1964 break;
1966 agg_reps = agg_reps->next;
1970 /* Try to work out value of virtual table pointer value in known
1971 aggregate values. */
1972 if (!t && known_aggs.length () > (unsigned int) param_index
1973 && !ie->indirect_info->by_ref)
1975 struct ipa_agg_jump_function *agg;
1976 agg = known_aggs[param_index];
1977 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1978 true);
1981 /* If we found the virtual table pointer, lookup the target. */
1982 if (t)
1984 tree vtable;
1985 unsigned HOST_WIDE_INT offset;
1986 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
1988 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
1989 vtable, offset);
1990 if (target)
1992 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
1993 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
1994 || !possible_polymorphic_call_target_p
1995 (ie, cgraph_node::get (target)))
1996 target = ipa_impossible_devirt_target (ie, target);
1997 *speculative = ie->indirect_info->vptr_changed;
1998 if (!*speculative)
1999 return target;
2004 /* Do we know the constant value of pointer? */
2005 if (!t)
2006 t = known_csts[param_index];
2008 gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
2010 ipa_polymorphic_call_context context;
2011 if (known_contexts.length () > (unsigned int) param_index)
2013 context = known_contexts[param_index];
2014 context.offset_by (anc_offset);
2015 if (ie->indirect_info->vptr_changed)
2016 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2017 ie->indirect_info->otr_type);
2018 if (t)
2020 ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
2021 (t, ie->indirect_info->otr_type, anc_offset);
2022 if (!ctx2.useless_p ())
2023 context.combine_with (ctx2, ie->indirect_info->otr_type);
2026 else if (t)
2028 context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
2029 anc_offset);
2030 if (ie->indirect_info->vptr_changed)
2031 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2032 ie->indirect_info->otr_type);
2034 else
2035 return NULL_TREE;
2037 vec <cgraph_node *>targets;
2038 bool final;
2040 targets = possible_polymorphic_call_targets
2041 (ie->indirect_info->otr_type,
2042 ie->indirect_info->otr_token,
2043 context, &final);
2044 if (!final || targets.length () > 1)
2046 struct cgraph_node *node;
2047 if (*speculative)
2048 return target;
2049 if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
2050 || ie->speculative || !ie->maybe_hot_p ())
2051 return NULL;
2052 node = try_speculative_devirtualization (ie->indirect_info->otr_type,
2053 ie->indirect_info->otr_token,
2054 context);
2055 if (node)
2057 *speculative = true;
2058 target = node->decl;
2060 else
2061 return NULL;
2063 else
2065 *speculative = false;
2066 if (targets.length () == 1)
2067 target = targets[0]->decl;
2068 else
2069 target = ipa_impossible_devirt_target (ie, NULL_TREE);
2072 if (target && !possible_polymorphic_call_target_p (ie,
2073 cgraph_node::get (target)))
2074 target = ipa_impossible_devirt_target (ie, target);
2076 return target;
2080 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2081 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2082 return the destination. */
2084 tree
2085 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
2086 vec<tree> known_csts,
2087 vec<ipa_polymorphic_call_context> known_contexts,
2088 vec<ipa_agg_jump_function_p> known_aggs,
2089 bool *speculative)
2091 return ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2092 known_aggs, NULL, speculative);
2095 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2096 and KNOWN_CONTEXTS. */
2098 static int
2099 devirtualization_time_bonus (struct cgraph_node *node,
2100 vec<tree> known_csts,
2101 vec<ipa_polymorphic_call_context> known_contexts,
2102 vec<ipa_agg_jump_function_p> known_aggs)
2104 struct cgraph_edge *ie;
2105 int res = 0;
2107 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
2109 struct cgraph_node *callee;
2110 struct inline_summary *isummary;
2111 enum availability avail;
2112 tree target;
2113 bool speculative;
2115 target = ipa_get_indirect_edge_target (ie, known_csts, known_contexts,
2116 known_aggs, &speculative);
2117 if (!target)
2118 continue;
2120 /* Only bare minimum benefit for clearly un-inlineable targets. */
2121 res += 1;
2122 callee = cgraph_node::get (target);
2123 if (!callee || !callee->definition)
2124 continue;
2125 callee = callee->function_symbol (&avail);
2126 if (avail < AVAIL_AVAILABLE)
2127 continue;
2128 isummary = inline_summaries->get (callee);
2129 if (!isummary->inlinable)
2130 continue;
2132 /* FIXME: The values below need re-considering and perhaps also
2133 integrating into the cost metrics, at lest in some very basic way. */
2134 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
2135 res += 31 / ((int)speculative + 1);
2136 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
2137 res += 15 / ((int)speculative + 1);
2138 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
2139 || DECL_DECLARED_INLINE_P (callee->decl))
2140 res += 7 / ((int)speculative + 1);
2143 return res;
2146 /* Return time bonus incurred because of HINTS. */
2148 static int
2149 hint_time_bonus (inline_hints hints)
2151 int result = 0;
2152 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
2153 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
2154 if (hints & INLINE_HINT_array_index)
2155 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
2156 return result;
2159 /* If there is a reason to penalize the function described by INFO in the
2160 cloning goodness evaluation, do so. */
2162 static inline int64_t
2163 incorporate_penalties (ipa_node_params *info, int64_t evaluation)
2165 if (info->node_within_scc)
2166 evaluation = (evaluation
2167 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY))) / 100;
2169 if (info->node_calling_single_call)
2170 evaluation = (evaluation
2171 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY)))
2172 / 100;
2174 return evaluation;
2177 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2178 and SIZE_COST and with the sum of frequencies of incoming edges to the
2179 potential new clone in FREQUENCIES. */
2181 static bool
2182 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
2183 int freq_sum, gcov_type count_sum, int size_cost)
2185 if (time_benefit == 0
2186 || !opt_for_fn (node->decl, flag_ipa_cp_clone)
2187 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
2188 return false;
2190 gcc_assert (size_cost > 0);
2192 struct ipa_node_params *info = IPA_NODE_REF (node);
2193 if (max_count)
2195 int factor = (count_sum * 1000) / max_count;
2196 int64_t evaluation = (((int64_t) time_benefit * factor)
2197 / size_cost);
2198 evaluation = incorporate_penalties (info, evaluation);
2200 if (dump_file && (dump_flags & TDF_DETAILS))
2201 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2202 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
2203 "%s%s) -> evaluation: " "%" PRId64
2204 ", threshold: %i\n",
2205 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
2206 info->node_within_scc ? ", scc" : "",
2207 info->node_calling_single_call ? ", single_call" : "",
2208 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2210 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2212 else
2214 int64_t evaluation = (((int64_t) time_benefit * freq_sum)
2215 / size_cost);
2216 evaluation = incorporate_penalties (info, evaluation);
2218 if (dump_file && (dump_flags & TDF_DETAILS))
2219 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2220 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2221 "%" PRId64 ", threshold: %i\n",
2222 time_benefit, size_cost, freq_sum,
2223 info->node_within_scc ? ", scc" : "",
2224 info->node_calling_single_call ? ", single_call" : "",
2225 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2227 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2231 /* Return all context independent values from aggregate lattices in PLATS in a
2232 vector. Return NULL if there are none. */
2234 static vec<ipa_agg_jf_item, va_gc> *
2235 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
2237 vec<ipa_agg_jf_item, va_gc> *res = NULL;
2239 if (plats->aggs_bottom
2240 || plats->aggs_contain_variable
2241 || plats->aggs_count == 0)
2242 return NULL;
2244 for (struct ipcp_agg_lattice *aglat = plats->aggs;
2245 aglat;
2246 aglat = aglat->next)
2247 if (aglat->is_single_const ())
2249 struct ipa_agg_jf_item item;
2250 item.offset = aglat->offset;
2251 item.value = aglat->values->value;
2252 vec_safe_push (res, item);
2254 return res;
2257 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2258 populate them with values of parameters that are known independent of the
2259 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2260 non-NULL, the movement cost of all removable parameters will be stored in
2261 it. */
2263 static bool
2264 gather_context_independent_values (struct ipa_node_params *info,
2265 vec<tree> *known_csts,
2266 vec<ipa_polymorphic_call_context>
2267 *known_contexts,
2268 vec<ipa_agg_jump_function> *known_aggs,
2269 int *removable_params_cost)
2271 int i, count = ipa_get_param_count (info);
2272 bool ret = false;
2274 known_csts->create (0);
2275 known_contexts->create (0);
2276 known_csts->safe_grow_cleared (count);
2277 known_contexts->safe_grow_cleared (count);
2278 if (known_aggs)
2280 known_aggs->create (0);
2281 known_aggs->safe_grow_cleared (count);
2284 if (removable_params_cost)
2285 *removable_params_cost = 0;
2287 for (i = 0; i < count ; i++)
2289 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2290 ipcp_lattice<tree> *lat = &plats->itself;
2292 if (lat->is_single_const ())
2294 ipcp_value<tree> *val = lat->values;
2295 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2296 (*known_csts)[i] = val->value;
2297 if (removable_params_cost)
2298 *removable_params_cost
2299 += estimate_move_cost (TREE_TYPE (val->value), false);
2300 ret = true;
2302 else if (removable_params_cost
2303 && !ipa_is_param_used (info, i))
2304 *removable_params_cost
2305 += ipa_get_param_move_cost (info, i);
2307 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2308 if (ctxlat->is_single_const ())
2310 (*known_contexts)[i] = ctxlat->values->value;
2311 ret = true;
2314 if (known_aggs)
2316 vec<ipa_agg_jf_item, va_gc> *agg_items;
2317 struct ipa_agg_jump_function *ajf;
2319 agg_items = context_independent_aggregate_values (plats);
2320 ajf = &(*known_aggs)[i];
2321 ajf->items = agg_items;
2322 ajf->by_ref = plats->aggs_by_ref;
2323 ret |= agg_items != NULL;
2327 return ret;
2330 /* The current interface in ipa-inline-analysis requires a pointer vector.
2331 Create it.
2333 FIXME: That interface should be re-worked, this is slightly silly. Still,
2334 I'd like to discuss how to change it first and this demonstrates the
2335 issue. */
2337 static vec<ipa_agg_jump_function_p>
2338 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
2340 vec<ipa_agg_jump_function_p> ret;
2341 struct ipa_agg_jump_function *ajf;
2342 int i;
2344 ret.create (known_aggs.length ());
2345 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
2346 ret.quick_push (ajf);
2347 return ret;
2350 /* Perform time and size measurement of NODE with the context given in
2351 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2352 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2353 all context-independent removable parameters and EST_MOVE_COST of estimated
2354 movement of the considered parameter and store it into VAL. */
2356 static void
2357 perform_estimation_of_a_value (cgraph_node *node, vec<tree> known_csts,
2358 vec<ipa_polymorphic_call_context> known_contexts,
2359 vec<ipa_agg_jump_function_p> known_aggs_ptrs,
2360 int base_time, int removable_params_cost,
2361 int est_move_cost, ipcp_value_base *val)
2363 int time, size, time_benefit;
2364 inline_hints hints;
2366 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2367 known_aggs_ptrs, &size, &time,
2368 &hints);
2369 time_benefit = base_time - time
2370 + devirtualization_time_bonus (node, known_csts, known_contexts,
2371 known_aggs_ptrs)
2372 + hint_time_bonus (hints)
2373 + removable_params_cost + est_move_cost;
2375 gcc_checking_assert (size >=0);
2376 /* The inliner-heuristics based estimates may think that in certain
2377 contexts some functions do not have any size at all but we want
2378 all specializations to have at least a tiny cost, not least not to
2379 divide by zero. */
2380 if (size == 0)
2381 size = 1;
2383 val->local_time_benefit = time_benefit;
2384 val->local_size_cost = size;
2387 /* Iterate over known values of parameters of NODE and estimate the local
2388 effects in terms of time and size they have. */
2390 static void
2391 estimate_local_effects (struct cgraph_node *node)
2393 struct ipa_node_params *info = IPA_NODE_REF (node);
2394 int i, count = ipa_get_param_count (info);
2395 vec<tree> known_csts;
2396 vec<ipa_polymorphic_call_context> known_contexts;
2397 vec<ipa_agg_jump_function> known_aggs;
2398 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
2399 bool always_const;
2400 int base_time = inline_summaries->get (node)->time;
2401 int removable_params_cost;
2403 if (!count || !ipcp_versionable_function_p (node))
2404 return;
2406 if (dump_file && (dump_flags & TDF_DETAILS))
2407 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
2408 node->name (), node->order, base_time);
2410 always_const = gather_context_independent_values (info, &known_csts,
2411 &known_contexts, &known_aggs,
2412 &removable_params_cost);
2413 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
2414 if (always_const)
2416 struct caller_statistics stats;
2417 inline_hints hints;
2418 int time, size;
2420 init_caller_stats (&stats);
2421 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2422 false);
2423 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2424 known_aggs_ptrs, &size, &time, &hints);
2425 time -= devirtualization_time_bonus (node, known_csts, known_contexts,
2426 known_aggs_ptrs);
2427 time -= hint_time_bonus (hints);
2428 time -= removable_params_cost;
2429 size -= stats.n_calls * removable_params_cost;
2431 if (dump_file)
2432 fprintf (dump_file, " - context independent values, size: %i, "
2433 "time_benefit: %i\n", size, base_time - time);
2435 if (size <= 0
2436 || node->will_be_removed_from_program_if_no_direct_calls_p ())
2438 info->do_clone_for_all_contexts = true;
2439 base_time = time;
2441 if (dump_file)
2442 fprintf (dump_file, " Decided to specialize for all "
2443 "known contexts, code not going to grow.\n");
2445 else if (good_cloning_opportunity_p (node, base_time - time,
2446 stats.freq_sum, stats.count_sum,
2447 size))
2449 if (size + overall_size <= max_new_size)
2451 info->do_clone_for_all_contexts = true;
2452 base_time = time;
2453 overall_size += size;
2455 if (dump_file)
2456 fprintf (dump_file, " Decided to specialize for all "
2457 "known contexts, growth deemed beneficial.\n");
2459 else if (dump_file && (dump_flags & TDF_DETAILS))
2460 fprintf (dump_file, " Not cloning for all contexts because "
2461 "max_new_size would be reached with %li.\n",
2462 size + overall_size);
2466 for (i = 0; i < count ; i++)
2468 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2469 ipcp_lattice<tree> *lat = &plats->itself;
2470 ipcp_value<tree> *val;
2472 if (lat->bottom
2473 || !lat->values
2474 || known_csts[i])
2475 continue;
2477 for (val = lat->values; val; val = val->next)
2479 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2480 known_csts[i] = val->value;
2482 int emc = estimate_move_cost (TREE_TYPE (val->value), true);
2483 perform_estimation_of_a_value (node, known_csts, known_contexts,
2484 known_aggs_ptrs, base_time,
2485 removable_params_cost, emc, val);
2487 if (dump_file && (dump_flags & TDF_DETAILS))
2489 fprintf (dump_file, " - estimates for value ");
2490 print_ipcp_constant_value (dump_file, val->value);
2491 fprintf (dump_file, " for ");
2492 ipa_dump_param (dump_file, info, i);
2493 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2494 val->local_time_benefit, val->local_size_cost);
2497 known_csts[i] = NULL_TREE;
2500 for (i = 0; i < count; i++)
2502 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2504 if (!plats->virt_call)
2505 continue;
2507 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2508 ipcp_value<ipa_polymorphic_call_context> *val;
2510 if (ctxlat->bottom
2511 || !ctxlat->values
2512 || !known_contexts[i].useless_p ())
2513 continue;
2515 for (val = ctxlat->values; val; val = val->next)
2517 known_contexts[i] = val->value;
2518 perform_estimation_of_a_value (node, known_csts, known_contexts,
2519 known_aggs_ptrs, base_time,
2520 removable_params_cost, 0, val);
2522 if (dump_file && (dump_flags & TDF_DETAILS))
2524 fprintf (dump_file, " - estimates for polymorphic context ");
2525 print_ipcp_constant_value (dump_file, val->value);
2526 fprintf (dump_file, " for ");
2527 ipa_dump_param (dump_file, info, i);
2528 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2529 val->local_time_benefit, val->local_size_cost);
2532 known_contexts[i] = ipa_polymorphic_call_context ();
2535 for (i = 0; i < count ; i++)
2537 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2538 struct ipa_agg_jump_function *ajf;
2539 struct ipcp_agg_lattice *aglat;
2541 if (plats->aggs_bottom || !plats->aggs)
2542 continue;
2544 ajf = &known_aggs[i];
2545 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2547 ipcp_value<tree> *val;
2548 if (aglat->bottom || !aglat->values
2549 /* If the following is true, the one value is in known_aggs. */
2550 || (!plats->aggs_contain_variable
2551 && aglat->is_single_const ()))
2552 continue;
2554 for (val = aglat->values; val; val = val->next)
2556 struct ipa_agg_jf_item item;
2558 item.offset = aglat->offset;
2559 item.value = val->value;
2560 vec_safe_push (ajf->items, item);
2562 perform_estimation_of_a_value (node, known_csts, known_contexts,
2563 known_aggs_ptrs, base_time,
2564 removable_params_cost, 0, val);
2566 if (dump_file && (dump_flags & TDF_DETAILS))
2568 fprintf (dump_file, " - estimates for value ");
2569 print_ipcp_constant_value (dump_file, val->value);
2570 fprintf (dump_file, " for ");
2571 ipa_dump_param (dump_file, info, i);
2572 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2573 "]: time_benefit: %i, size: %i\n",
2574 plats->aggs_by_ref ? "ref " : "",
2575 aglat->offset,
2576 val->local_time_benefit, val->local_size_cost);
2579 ajf->items->pop ();
2584 for (i = 0; i < count ; i++)
2585 vec_free (known_aggs[i].items);
2587 known_csts.release ();
2588 known_contexts.release ();
2589 known_aggs.release ();
2590 known_aggs_ptrs.release ();
2594 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2595 topological sort of values. */
2597 template <typename valtype>
2598 void
2599 value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
2601 ipcp_value_source<valtype> *src;
2603 if (cur_val->dfs)
2604 return;
2606 dfs_counter++;
2607 cur_val->dfs = dfs_counter;
2608 cur_val->low_link = dfs_counter;
2610 cur_val->topo_next = stack;
2611 stack = cur_val;
2612 cur_val->on_stack = true;
2614 for (src = cur_val->sources; src; src = src->next)
2615 if (src->val)
2617 if (src->val->dfs == 0)
2619 add_val (src->val);
2620 if (src->val->low_link < cur_val->low_link)
2621 cur_val->low_link = src->val->low_link;
2623 else if (src->val->on_stack
2624 && src->val->dfs < cur_val->low_link)
2625 cur_val->low_link = src->val->dfs;
2628 if (cur_val->dfs == cur_val->low_link)
2630 ipcp_value<valtype> *v, *scc_list = NULL;
2634 v = stack;
2635 stack = v->topo_next;
2636 v->on_stack = false;
2638 v->scc_next = scc_list;
2639 scc_list = v;
2641 while (v != cur_val);
2643 cur_val->topo_next = values_topo;
2644 values_topo = cur_val;
2648 /* Add all values in lattices associated with NODE to the topological sort if
2649 they are not there yet. */
2651 static void
2652 add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
2654 struct ipa_node_params *info = IPA_NODE_REF (node);
2655 int i, count = ipa_get_param_count (info);
2657 for (i = 0; i < count ; i++)
2659 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2660 ipcp_lattice<tree> *lat = &plats->itself;
2661 struct ipcp_agg_lattice *aglat;
2663 if (!lat->bottom)
2665 ipcp_value<tree> *val;
2666 for (val = lat->values; val; val = val->next)
2667 topo->constants.add_val (val);
2670 if (!plats->aggs_bottom)
2671 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2672 if (!aglat->bottom)
2674 ipcp_value<tree> *val;
2675 for (val = aglat->values; val; val = val->next)
2676 topo->constants.add_val (val);
2679 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2680 if (!ctxlat->bottom)
2682 ipcp_value<ipa_polymorphic_call_context> *ctxval;
2683 for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
2684 topo->contexts.add_val (ctxval);
2689 /* One pass of constants propagation along the call graph edges, from callers
2690 to callees (requires topological ordering in TOPO), iterate over strongly
2691 connected components. */
2693 static void
2694 propagate_constants_topo (struct ipa_topo_info *topo)
2696 int i;
2698 for (i = topo->nnodes - 1; i >= 0; i--)
2700 unsigned j;
2701 struct cgraph_node *v, *node = topo->order[i];
2702 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
2704 /* First, iteratively propagate within the strongly connected component
2705 until all lattices stabilize. */
2706 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2707 if (v->has_gimple_body_p ())
2708 push_node_to_stack (topo, v);
2710 v = pop_node_from_stack (topo);
2711 while (v)
2713 struct cgraph_edge *cs;
2715 for (cs = v->callees; cs; cs = cs->next_callee)
2716 if (ipa_edge_within_scc (cs))
2718 IPA_NODE_REF (v)->node_within_scc = true;
2719 if (propagate_constants_accross_call (cs))
2720 push_node_to_stack (topo, cs->callee->function_symbol ());
2722 v = pop_node_from_stack (topo);
2725 /* Afterwards, propagate along edges leading out of the SCC, calculates
2726 the local effects of the discovered constants and all valid values to
2727 their topological sort. */
2728 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2729 if (v->has_gimple_body_p ())
2731 struct cgraph_edge *cs;
2733 estimate_local_effects (v);
2734 add_all_node_vals_to_toposort (v, topo);
2735 for (cs = v->callees; cs; cs = cs->next_callee)
2736 if (!ipa_edge_within_scc (cs))
2737 propagate_constants_accross_call (cs);
2739 cycle_nodes.release ();
2744 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2745 the bigger one if otherwise. */
2747 static int
2748 safe_add (int a, int b)
2750 if (a > INT_MAX/2 || b > INT_MAX/2)
2751 return a > b ? a : b;
2752 else
2753 return a + b;
2757 /* Propagate the estimated effects of individual values along the topological
2758 from the dependent values to those they depend on. */
2760 template <typename valtype>
2761 void
2762 value_topo_info<valtype>::propagate_effects ()
2764 ipcp_value<valtype> *base;
2766 for (base = values_topo; base; base = base->topo_next)
2768 ipcp_value_source<valtype> *src;
2769 ipcp_value<valtype> *val;
2770 int time = 0, size = 0;
2772 for (val = base; val; val = val->scc_next)
2774 time = safe_add (time,
2775 val->local_time_benefit + val->prop_time_benefit);
2776 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2779 for (val = base; val; val = val->scc_next)
2780 for (src = val->sources; src; src = src->next)
2781 if (src->val
2782 && src->cs->maybe_hot_p ())
2784 src->val->prop_time_benefit = safe_add (time,
2785 src->val->prop_time_benefit);
2786 src->val->prop_size_cost = safe_add (size,
2787 src->val->prop_size_cost);
2793 /* Propagate constants, polymorphic contexts and their effects from the
2794 summaries interprocedurally. */
2796 static void
2797 ipcp_propagate_stage (struct ipa_topo_info *topo)
2799 struct cgraph_node *node;
2801 if (dump_file)
2802 fprintf (dump_file, "\n Propagating constants:\n\n");
2804 if (in_lto_p)
2805 ipa_update_after_lto_read ();
2808 FOR_EACH_DEFINED_FUNCTION (node)
2810 struct ipa_node_params *info = IPA_NODE_REF (node);
2812 determine_versionability (node);
2813 if (node->has_gimple_body_p ())
2815 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2816 ipa_get_param_count (info));
2817 initialize_node_lattices (node);
2819 if (node->definition && !node->alias)
2820 overall_size += inline_summaries->get (node)->self_size;
2821 if (node->count > max_count)
2822 max_count = node->count;
2825 max_new_size = overall_size;
2826 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2827 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2828 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2830 if (dump_file)
2831 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2832 overall_size, max_new_size);
2834 propagate_constants_topo (topo);
2835 #ifdef ENABLE_CHECKING
2836 ipcp_verify_propagated_values ();
2837 #endif
2838 topo->constants.propagate_effects ();
2839 topo->contexts.propagate_effects ();
2841 if (dump_file)
2843 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2844 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2848 /* Discover newly direct outgoing edges from NODE which is a new clone with
2849 known KNOWN_CSTS and make them direct. */
2851 static void
2852 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2853 vec<tree> known_csts,
2854 vec<ipa_polymorphic_call_context>
2855 known_contexts,
2856 struct ipa_agg_replacement_value *aggvals)
2858 struct cgraph_edge *ie, *next_ie;
2859 bool found = false;
2861 for (ie = node->indirect_calls; ie; ie = next_ie)
2863 tree target;
2864 bool speculative;
2866 next_ie = ie->next_callee;
2867 target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2868 vNULL, aggvals, &speculative);
2869 if (target)
2871 bool agg_contents = ie->indirect_info->agg_contents;
2872 bool polymorphic = ie->indirect_info->polymorphic;
2873 int param_index = ie->indirect_info->param_index;
2874 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
2875 speculative);
2876 found = true;
2878 if (cs && !agg_contents && !polymorphic)
2880 struct ipa_node_params *info = IPA_NODE_REF (node);
2881 int c = ipa_get_controlled_uses (info, param_index);
2882 if (c != IPA_UNDESCRIBED_USE)
2884 struct ipa_ref *to_del;
2886 c--;
2887 ipa_set_controlled_uses (info, param_index, c);
2888 if (dump_file && (dump_flags & TDF_DETAILS))
2889 fprintf (dump_file, " controlled uses count of param "
2890 "%i bumped down to %i\n", param_index, c);
2891 if (c == 0
2892 && (to_del = node->find_reference (cs->callee, NULL, 0)))
2894 if (dump_file && (dump_flags & TDF_DETAILS))
2895 fprintf (dump_file, " and even removing its "
2896 "cloning-created reference\n");
2897 to_del->remove_reference ();
2903 /* Turning calls to direct calls will improve overall summary. */
2904 if (found)
2905 inline_update_overall_summary (node);
2908 /* Vector of pointers which for linked lists of clones of an original crgaph
2909 edge. */
2911 static vec<cgraph_edge *> next_edge_clone;
2912 static vec<cgraph_edge *> prev_edge_clone;
2914 static inline void
2915 grow_edge_clone_vectors (void)
2917 if (next_edge_clone.length ()
2918 <= (unsigned) symtab->edges_max_uid)
2919 next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2920 if (prev_edge_clone.length ()
2921 <= (unsigned) symtab->edges_max_uid)
2922 prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2925 /* Edge duplication hook to grow the appropriate linked list in
2926 next_edge_clone. */
2928 static void
2929 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2930 void *)
2932 grow_edge_clone_vectors ();
2934 struct cgraph_edge *old_next = next_edge_clone[src->uid];
2935 if (old_next)
2936 prev_edge_clone[old_next->uid] = dst;
2937 prev_edge_clone[dst->uid] = src;
2939 next_edge_clone[dst->uid] = old_next;
2940 next_edge_clone[src->uid] = dst;
2943 /* Hook that is called by cgraph.c when an edge is removed. */
2945 static void
2946 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
2948 grow_edge_clone_vectors ();
2950 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
2951 struct cgraph_edge *next = next_edge_clone[cs->uid];
2952 if (prev)
2953 next_edge_clone[prev->uid] = next;
2954 if (next)
2955 prev_edge_clone[next->uid] = prev;
2958 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2959 parameter with the given INDEX. */
2961 static tree
2962 get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset,
2963 int index)
2965 struct ipa_agg_replacement_value *aggval;
2967 aggval = ipa_get_agg_replacements_for_node (node);
2968 while (aggval)
2970 if (aggval->offset == offset
2971 && aggval->index == index)
2972 return aggval->value;
2973 aggval = aggval->next;
2975 return NULL_TREE;
2978 /* Return true is NODE is DEST or its clone for all contexts. */
2980 static bool
2981 same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest)
2983 if (node == dest)
2984 return true;
2986 struct ipa_node_params *info = IPA_NODE_REF (node);
2987 return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
2990 /* Return true if edge CS does bring about the value described by SRC to node
2991 DEST or its clone for all contexts. */
2993 static bool
2994 cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
2995 cgraph_node *dest)
2997 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2998 enum availability availability;
2999 cgraph_node *real_dest = cs->callee->function_symbol (&availability);
3001 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3002 || availability <= AVAIL_INTERPOSABLE
3003 || caller_info->node_dead)
3004 return false;
3005 if (!src->val)
3006 return true;
3008 if (caller_info->ipcp_orig_node)
3010 tree t;
3011 if (src->offset == -1)
3012 t = caller_info->known_csts[src->index];
3013 else
3014 t = get_clone_agg_value (cs->caller, src->offset, src->index);
3015 return (t != NULL_TREE
3016 && values_equal_for_ipcp_p (src->val->value, t));
3018 else
3020 struct ipcp_agg_lattice *aglat;
3021 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3022 src->index);
3023 if (src->offset == -1)
3024 return (plats->itself.is_single_const ()
3025 && values_equal_for_ipcp_p (src->val->value,
3026 plats->itself.values->value));
3027 else
3029 if (plats->aggs_bottom || plats->aggs_contain_variable)
3030 return false;
3031 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3032 if (aglat->offset == src->offset)
3033 return (aglat->is_single_const ()
3034 && values_equal_for_ipcp_p (src->val->value,
3035 aglat->values->value));
3037 return false;
3041 /* Return true if edge CS does bring about the value described by SRC to node
3042 DEST or its clone for all contexts. */
3044 static bool
3045 cgraph_edge_brings_value_p (cgraph_edge *cs,
3046 ipcp_value_source<ipa_polymorphic_call_context> *src,
3047 cgraph_node *dest)
3049 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3050 cgraph_node *real_dest = cs->callee->function_symbol ();
3052 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3053 || caller_info->node_dead)
3054 return false;
3055 if (!src->val)
3056 return true;
3058 if (caller_info->ipcp_orig_node)
3059 return (caller_info->known_contexts.length () > (unsigned) src->index)
3060 && values_equal_for_ipcp_p (src->val->value,
3061 caller_info->known_contexts[src->index]);
3063 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3064 src->index);
3065 return plats->ctxlat.is_single_const ()
3066 && values_equal_for_ipcp_p (src->val->value,
3067 plats->ctxlat.values->value);
3070 /* Get the next clone in the linked list of clones of an edge. */
3072 static inline struct cgraph_edge *
3073 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
3075 return next_edge_clone[cs->uid];
3078 /* Given VAL that is intended for DEST, iterate over all its sources and if
3079 they still hold, add their edge frequency and their number into *FREQUENCY
3080 and *CALLER_COUNT respectively. */
3082 template <typename valtype>
3083 static bool
3084 get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
3085 int *freq_sum,
3086 gcov_type *count_sum, int *caller_count)
3088 ipcp_value_source<valtype> *src;
3089 int freq = 0, count = 0;
3090 gcov_type cnt = 0;
3091 bool hot = false;
3093 for (src = val->sources; src; src = src->next)
3095 struct cgraph_edge *cs = src->cs;
3096 while (cs)
3098 if (cgraph_edge_brings_value_p (cs, src, dest))
3100 count++;
3101 freq += cs->frequency;
3102 cnt += cs->count;
3103 hot |= cs->maybe_hot_p ();
3105 cs = get_next_cgraph_edge_clone (cs);
3109 *freq_sum = freq;
3110 *count_sum = cnt;
3111 *caller_count = count;
3112 return hot;
3115 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3116 is assumed their number is known and equal to CALLER_COUNT. */
3118 template <typename valtype>
3119 static vec<cgraph_edge *>
3120 gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
3121 int caller_count)
3123 ipcp_value_source<valtype> *src;
3124 vec<cgraph_edge *> ret;
3126 ret.create (caller_count);
3127 for (src = val->sources; src; src = src->next)
3129 struct cgraph_edge *cs = src->cs;
3130 while (cs)
3132 if (cgraph_edge_brings_value_p (cs, src, dest))
3133 ret.quick_push (cs);
3134 cs = get_next_cgraph_edge_clone (cs);
3138 return ret;
3141 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3142 Return it or NULL if for some reason it cannot be created. */
3144 static struct ipa_replace_map *
3145 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
3147 struct ipa_replace_map *replace_map;
3150 replace_map = ggc_alloc<ipa_replace_map> ();
3151 if (dump_file)
3153 fprintf (dump_file, " replacing ");
3154 ipa_dump_param (dump_file, info, parm_num);
3156 fprintf (dump_file, " with const ");
3157 print_generic_expr (dump_file, value, 0);
3158 fprintf (dump_file, "\n");
3160 replace_map->old_tree = NULL;
3161 replace_map->parm_num = parm_num;
3162 replace_map->new_tree = value;
3163 replace_map->replace_p = true;
3164 replace_map->ref_p = false;
3166 return replace_map;
3169 /* Dump new profiling counts */
3171 static void
3172 dump_profile_updates (struct cgraph_node *orig_node,
3173 struct cgraph_node *new_node)
3175 struct cgraph_edge *cs;
3177 fprintf (dump_file, " setting count of the specialized node to "
3178 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
3179 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3180 fprintf (dump_file, " edge to %s has count "
3181 HOST_WIDE_INT_PRINT_DEC "\n",
3182 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3184 fprintf (dump_file, " setting count of the original node to "
3185 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
3186 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3187 fprintf (dump_file, " edge to %s is left with "
3188 HOST_WIDE_INT_PRINT_DEC "\n",
3189 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3192 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3193 their profile information to reflect this. */
3195 static void
3196 update_profiling_info (struct cgraph_node *orig_node,
3197 struct cgraph_node *new_node)
3199 struct cgraph_edge *cs;
3200 struct caller_statistics stats;
3201 gcov_type new_sum, orig_sum;
3202 gcov_type remainder, orig_node_count = orig_node->count;
3204 if (orig_node_count == 0)
3205 return;
3207 init_caller_stats (&stats);
3208 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3209 false);
3210 orig_sum = stats.count_sum;
3211 init_caller_stats (&stats);
3212 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3213 false);
3214 new_sum = stats.count_sum;
3216 if (orig_node_count < orig_sum + new_sum)
3218 if (dump_file)
3219 fprintf (dump_file, " Problem: node %s/%i has too low count "
3220 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
3221 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
3222 orig_node->name (), orig_node->order,
3223 (HOST_WIDE_INT) orig_node_count,
3224 (HOST_WIDE_INT) (orig_sum + new_sum));
3226 orig_node_count = (orig_sum + new_sum) * 12 / 10;
3227 if (dump_file)
3228 fprintf (dump_file, " proceeding by pretending it was "
3229 HOST_WIDE_INT_PRINT_DEC "\n",
3230 (HOST_WIDE_INT) orig_node_count);
3233 new_node->count = new_sum;
3234 remainder = orig_node_count - new_sum;
3235 orig_node->count = remainder;
3237 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3238 if (cs->frequency)
3239 cs->count = apply_probability (cs->count,
3240 GCOV_COMPUTE_SCALE (new_sum,
3241 orig_node_count));
3242 else
3243 cs->count = 0;
3245 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3246 cs->count = apply_probability (cs->count,
3247 GCOV_COMPUTE_SCALE (remainder,
3248 orig_node_count));
3250 if (dump_file)
3251 dump_profile_updates (orig_node, new_node);
3254 /* Update the respective profile of specialized NEW_NODE and the original
3255 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3256 have been redirected to the specialized version. */
3258 static void
3259 update_specialized_profile (struct cgraph_node *new_node,
3260 struct cgraph_node *orig_node,
3261 gcov_type redirected_sum)
3263 struct cgraph_edge *cs;
3264 gcov_type new_node_count, orig_node_count = orig_node->count;
3266 if (dump_file)
3267 fprintf (dump_file, " the sum of counts of redirected edges is "
3268 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
3269 if (orig_node_count == 0)
3270 return;
3272 gcc_assert (orig_node_count >= redirected_sum);
3274 new_node_count = new_node->count;
3275 new_node->count += redirected_sum;
3276 orig_node->count -= redirected_sum;
3278 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3279 if (cs->frequency)
3280 cs->count += apply_probability (cs->count,
3281 GCOV_COMPUTE_SCALE (redirected_sum,
3282 new_node_count));
3283 else
3284 cs->count = 0;
3286 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3288 gcov_type dec = apply_probability (cs->count,
3289 GCOV_COMPUTE_SCALE (redirected_sum,
3290 orig_node_count));
3291 if (dec < cs->count)
3292 cs->count -= dec;
3293 else
3294 cs->count = 0;
3297 if (dump_file)
3298 dump_profile_updates (orig_node, new_node);
3301 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3302 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3303 redirect all edges in CALLERS to it. */
3305 static struct cgraph_node *
3306 create_specialized_node (struct cgraph_node *node,
3307 vec<tree> known_csts,
3308 vec<ipa_polymorphic_call_context> known_contexts,
3309 struct ipa_agg_replacement_value *aggvals,
3310 vec<cgraph_edge *> callers)
3312 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
3313 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
3314 struct ipa_agg_replacement_value *av;
3315 struct cgraph_node *new_node;
3316 int i, count = ipa_get_param_count (info);
3317 bitmap args_to_skip;
3319 gcc_assert (!info->ipcp_orig_node);
3321 if (node->local.can_change_signature)
3323 args_to_skip = BITMAP_GGC_ALLOC ();
3324 for (i = 0; i < count; i++)
3326 tree t = known_csts[i];
3328 if (t || !ipa_is_param_used (info, i))
3329 bitmap_set_bit (args_to_skip, i);
3332 else
3334 args_to_skip = NULL;
3335 if (dump_file && (dump_flags & TDF_DETAILS))
3336 fprintf (dump_file, " cannot change function signature\n");
3339 for (i = 0; i < count ; i++)
3341 tree t = known_csts[i];
3342 if (t)
3344 struct ipa_replace_map *replace_map;
3346 gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
3347 replace_map = get_replacement_map (info, t, i);
3348 if (replace_map)
3349 vec_safe_push (replace_trees, replace_map);
3353 new_node = node->create_virtual_clone (callers, replace_trees,
3354 args_to_skip, "constprop");
3355 ipa_set_node_agg_value_chain (new_node, aggvals);
3356 for (av = aggvals; av; av = av->next)
3357 new_node->maybe_create_reference (av->value, IPA_REF_ADDR, NULL);
3359 if (dump_file && (dump_flags & TDF_DETAILS))
3361 fprintf (dump_file, " the new node is %s/%i.\n",
3362 new_node->name (), new_node->order);
3363 if (known_contexts.exists ())
3365 for (i = 0; i < count ; i++)
3366 if (!known_contexts[i].useless_p ())
3368 fprintf (dump_file, " known ctx %i is ", i);
3369 known_contexts[i].dump (dump_file);
3372 if (aggvals)
3373 ipa_dump_agg_replacement_values (dump_file, aggvals);
3375 ipa_check_create_node_params ();
3376 update_profiling_info (node, new_node);
3377 new_info = IPA_NODE_REF (new_node);
3378 new_info->ipcp_orig_node = node;
3379 new_info->known_csts = known_csts;
3380 new_info->known_contexts = known_contexts;
3382 ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts, aggvals);
3384 callers.release ();
3385 return new_node;
3388 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3389 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3391 static void
3392 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
3393 vec<tree> known_csts,
3394 vec<cgraph_edge *> callers)
3396 struct ipa_node_params *info = IPA_NODE_REF (node);
3397 int i, count = ipa_get_param_count (info);
3399 for (i = 0; i < count ; i++)
3401 struct cgraph_edge *cs;
3402 tree newval = NULL_TREE;
3403 int j;
3404 bool first = true;
3406 if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
3407 continue;
3409 FOR_EACH_VEC_ELT (callers, j, cs)
3411 struct ipa_jump_func *jump_func;
3412 tree t;
3414 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3416 newval = NULL_TREE;
3417 break;
3419 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
3420 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
3421 if (!t
3422 || (newval
3423 && !values_equal_for_ipcp_p (t, newval))
3424 || (!first && !newval))
3426 newval = NULL_TREE;
3427 break;
3429 else
3430 newval = t;
3431 first = false;
3434 if (newval)
3436 if (dump_file && (dump_flags & TDF_DETAILS))
3438 fprintf (dump_file, " adding an extra known scalar value ");
3439 print_ipcp_constant_value (dump_file, newval);
3440 fprintf (dump_file, " for ");
3441 ipa_dump_param (dump_file, info, i);
3442 fprintf (dump_file, "\n");
3445 known_csts[i] = newval;
3450 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3451 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3452 CALLERS. */
3454 static void
3455 find_more_contexts_for_caller_subset (cgraph_node *node,
3456 vec<ipa_polymorphic_call_context>
3457 *known_contexts,
3458 vec<cgraph_edge *> callers)
3460 ipa_node_params *info = IPA_NODE_REF (node);
3461 int i, count = ipa_get_param_count (info);
3463 for (i = 0; i < count ; i++)
3465 cgraph_edge *cs;
3467 if (ipa_get_poly_ctx_lat (info, i)->bottom
3468 || (known_contexts->exists ()
3469 && !(*known_contexts)[i].useless_p ()))
3470 continue;
3472 ipa_polymorphic_call_context newval;
3473 bool first = true;
3474 int j;
3476 FOR_EACH_VEC_ELT (callers, j, cs)
3478 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3479 return;
3480 ipa_jump_func *jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs),
3482 ipa_polymorphic_call_context ctx;
3483 ctx = ipa_context_from_jfunc (IPA_NODE_REF (cs->caller), cs, i,
3484 jfunc);
3485 if (first)
3487 newval = ctx;
3488 first = false;
3490 else
3491 newval.meet_with (ctx);
3492 if (newval.useless_p ())
3493 break;
3496 if (!newval.useless_p ())
3498 if (dump_file && (dump_flags & TDF_DETAILS))
3500 fprintf (dump_file, " adding an extra known polymorphic "
3501 "context ");
3502 print_ipcp_constant_value (dump_file, newval);
3503 fprintf (dump_file, " for ");
3504 ipa_dump_param (dump_file, info, i);
3505 fprintf (dump_file, "\n");
3508 if (!known_contexts->exists ())
3509 known_contexts->safe_grow_cleared (ipa_get_param_count (info));
3510 (*known_contexts)[i] = newval;
3516 /* Go through PLATS and create a vector of values consisting of values and
3517 offsets (minus OFFSET) of lattices that contain only a single value. */
3519 static vec<ipa_agg_jf_item>
3520 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
3522 vec<ipa_agg_jf_item> res = vNULL;
3524 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3525 return vNULL;
3527 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3528 if (aglat->is_single_const ())
3530 struct ipa_agg_jf_item ti;
3531 ti.offset = aglat->offset - offset;
3532 ti.value = aglat->values->value;
3533 res.safe_push (ti);
3535 return res;
3538 /* Intersect all values in INTER with single value lattices in PLATS (while
3539 subtracting OFFSET). */
3541 static void
3542 intersect_with_plats (struct ipcp_param_lattices *plats,
3543 vec<ipa_agg_jf_item> *inter,
3544 HOST_WIDE_INT offset)
3546 struct ipcp_agg_lattice *aglat;
3547 struct ipa_agg_jf_item *item;
3548 int k;
3550 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3552 inter->release ();
3553 return;
3556 aglat = plats->aggs;
3557 FOR_EACH_VEC_ELT (*inter, k, item)
3559 bool found = false;
3560 if (!item->value)
3561 continue;
3562 while (aglat)
3564 if (aglat->offset - offset > item->offset)
3565 break;
3566 if (aglat->offset - offset == item->offset)
3568 gcc_checking_assert (item->value);
3569 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
3570 found = true;
3571 break;
3573 aglat = aglat->next;
3575 if (!found)
3576 item->value = NULL_TREE;
3580 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
3581 vector result while subtracting OFFSET from the individual value offsets. */
3583 static vec<ipa_agg_jf_item>
3584 agg_replacements_to_vector (struct cgraph_node *node, int index,
3585 HOST_WIDE_INT offset)
3587 struct ipa_agg_replacement_value *av;
3588 vec<ipa_agg_jf_item> res = vNULL;
3590 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
3591 if (av->index == index
3592 && (av->offset - offset) >= 0)
3594 struct ipa_agg_jf_item item;
3595 gcc_checking_assert (av->value);
3596 item.offset = av->offset - offset;
3597 item.value = av->value;
3598 res.safe_push (item);
3601 return res;
3604 /* Intersect all values in INTER with those that we have already scheduled to
3605 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
3606 (while subtracting OFFSET). */
3608 static void
3609 intersect_with_agg_replacements (struct cgraph_node *node, int index,
3610 vec<ipa_agg_jf_item> *inter,
3611 HOST_WIDE_INT offset)
3613 struct ipa_agg_replacement_value *srcvals;
3614 struct ipa_agg_jf_item *item;
3615 int i;
3617 srcvals = ipa_get_agg_replacements_for_node (node);
3618 if (!srcvals)
3620 inter->release ();
3621 return;
3624 FOR_EACH_VEC_ELT (*inter, i, item)
3626 struct ipa_agg_replacement_value *av;
3627 bool found = false;
3628 if (!item->value)
3629 continue;
3630 for (av = srcvals; av; av = av->next)
3632 gcc_checking_assert (av->value);
3633 if (av->index == index
3634 && av->offset - offset == item->offset)
3636 if (values_equal_for_ipcp_p (item->value, av->value))
3637 found = true;
3638 break;
3641 if (!found)
3642 item->value = NULL_TREE;
3646 /* Intersect values in INTER with aggregate values that come along edge CS to
3647 parameter number INDEX and return it. If INTER does not actually exist yet,
3648 copy all incoming values to it. If we determine we ended up with no values
3649 whatsoever, return a released vector. */
3651 static vec<ipa_agg_jf_item>
3652 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3653 vec<ipa_agg_jf_item> inter)
3655 struct ipa_jump_func *jfunc;
3656 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3657 if (jfunc->type == IPA_JF_PASS_THROUGH
3658 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3660 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3661 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3663 if (caller_info->ipcp_orig_node)
3665 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3666 struct ipcp_param_lattices *orig_plats;
3667 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3668 src_idx);
3669 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3671 if (!inter.exists ())
3672 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3673 else
3674 intersect_with_agg_replacements (cs->caller, src_idx,
3675 &inter, 0);
3677 else
3679 inter.release ();
3680 return vNULL;
3683 else
3685 struct ipcp_param_lattices *src_plats;
3686 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3687 if (agg_pass_through_permissible_p (src_plats, jfunc))
3689 /* Currently we do not produce clobber aggregate jump
3690 functions, adjust when we do. */
3691 gcc_checking_assert (!jfunc->agg.items);
3692 if (!inter.exists ())
3693 inter = copy_plats_to_inter (src_plats, 0);
3694 else
3695 intersect_with_plats (src_plats, &inter, 0);
3697 else
3699 inter.release ();
3700 return vNULL;
3704 else if (jfunc->type == IPA_JF_ANCESTOR
3705 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3707 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3708 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3709 struct ipcp_param_lattices *src_plats;
3710 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3712 if (caller_info->ipcp_orig_node)
3714 if (!inter.exists ())
3715 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3716 else
3717 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3718 delta);
3720 else
3722 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3723 /* Currently we do not produce clobber aggregate jump
3724 functions, adjust when we do. */
3725 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3726 if (!inter.exists ())
3727 inter = copy_plats_to_inter (src_plats, delta);
3728 else
3729 intersect_with_plats (src_plats, &inter, delta);
3732 else if (jfunc->agg.items)
3734 struct ipa_agg_jf_item *item;
3735 int k;
3737 if (!inter.exists ())
3738 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3739 inter.safe_push ((*jfunc->agg.items)[i]);
3740 else
3741 FOR_EACH_VEC_ELT (inter, k, item)
3743 int l = 0;
3744 bool found = false;;
3746 if (!item->value)
3747 continue;
3749 while ((unsigned) l < jfunc->agg.items->length ())
3751 struct ipa_agg_jf_item *ti;
3752 ti = &(*jfunc->agg.items)[l];
3753 if (ti->offset > item->offset)
3754 break;
3755 if (ti->offset == item->offset)
3757 gcc_checking_assert (ti->value);
3758 if (values_equal_for_ipcp_p (item->value,
3759 ti->value))
3760 found = true;
3761 break;
3763 l++;
3765 if (!found)
3766 item->value = NULL;
3769 else
3771 inter.release ();
3772 return vec<ipa_agg_jf_item>();
3774 return inter;
3777 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3778 from all of them. */
3780 static struct ipa_agg_replacement_value *
3781 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3782 vec<cgraph_edge *> callers)
3784 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3785 struct ipa_agg_replacement_value *res;
3786 struct ipa_agg_replacement_value **tail = &res;
3787 struct cgraph_edge *cs;
3788 int i, j, count = ipa_get_param_count (dest_info);
3790 FOR_EACH_VEC_ELT (callers, j, cs)
3792 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3793 if (c < count)
3794 count = c;
3797 for (i = 0; i < count ; i++)
3799 struct cgraph_edge *cs;
3800 vec<ipa_agg_jf_item> inter = vNULL;
3801 struct ipa_agg_jf_item *item;
3802 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3803 int j;
3805 /* Among other things, the following check should deal with all by_ref
3806 mismatches. */
3807 if (plats->aggs_bottom)
3808 continue;
3810 FOR_EACH_VEC_ELT (callers, j, cs)
3812 inter = intersect_aggregates_with_edge (cs, i, inter);
3814 if (!inter.exists ())
3815 goto next_param;
3818 FOR_EACH_VEC_ELT (inter, j, item)
3820 struct ipa_agg_replacement_value *v;
3822 if (!item->value)
3823 continue;
3825 v = ggc_alloc<ipa_agg_replacement_value> ();
3826 v->index = i;
3827 v->offset = item->offset;
3828 v->value = item->value;
3829 v->by_ref = plats->aggs_by_ref;
3830 *tail = v;
3831 tail = &v->next;
3834 next_param:
3835 if (inter.exists ())
3836 inter.release ();
3838 *tail = NULL;
3839 return res;
3842 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3844 static struct ipa_agg_replacement_value *
3845 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3847 struct ipa_agg_replacement_value *res;
3848 struct ipa_agg_replacement_value **tail = &res;
3849 struct ipa_agg_jump_function *aggjf;
3850 struct ipa_agg_jf_item *item;
3851 int i, j;
3853 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3854 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3856 struct ipa_agg_replacement_value *v;
3857 v = ggc_alloc<ipa_agg_replacement_value> ();
3858 v->index = i;
3859 v->offset = item->offset;
3860 v->value = item->value;
3861 v->by_ref = aggjf->by_ref;
3862 *tail = v;
3863 tail = &v->next;
3865 *tail = NULL;
3866 return res;
3869 /* Determine whether CS also brings all scalar values that the NODE is
3870 specialized for. */
3872 static bool
3873 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3874 struct cgraph_node *node)
3876 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3877 int count = ipa_get_param_count (dest_info);
3878 struct ipa_node_params *caller_info;
3879 struct ipa_edge_args *args;
3880 int i;
3882 caller_info = IPA_NODE_REF (cs->caller);
3883 args = IPA_EDGE_REF (cs);
3884 for (i = 0; i < count; i++)
3886 struct ipa_jump_func *jump_func;
3887 tree val, t;
3889 val = dest_info->known_csts[i];
3890 if (!val)
3891 continue;
3893 if (i >= ipa_get_cs_argument_count (args))
3894 return false;
3895 jump_func = ipa_get_ith_jump_func (args, i);
3896 t = ipa_value_from_jfunc (caller_info, jump_func);
3897 if (!t || !values_equal_for_ipcp_p (val, t))
3898 return false;
3900 return true;
3903 /* Determine whether CS also brings all aggregate values that NODE is
3904 specialized for. */
3905 static bool
3906 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3907 struct cgraph_node *node)
3909 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3910 struct ipa_node_params *orig_node_info;
3911 struct ipa_agg_replacement_value *aggval;
3912 int i, ec, count;
3914 aggval = ipa_get_agg_replacements_for_node (node);
3915 if (!aggval)
3916 return true;
3918 count = ipa_get_param_count (IPA_NODE_REF (node));
3919 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3920 if (ec < count)
3921 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3922 if (aggval->index >= ec)
3923 return false;
3925 orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node);
3926 if (orig_caller_info->ipcp_orig_node)
3927 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3929 for (i = 0; i < count; i++)
3931 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
3932 struct ipcp_param_lattices *plats;
3933 bool interesting = false;
3934 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3935 if (aggval->index == i)
3937 interesting = true;
3938 break;
3940 if (!interesting)
3941 continue;
3943 plats = ipa_get_parm_lattices (orig_node_info, aggval->index);
3944 if (plats->aggs_bottom)
3945 return false;
3947 values = intersect_aggregates_with_edge (cs, i, values);
3948 if (!values.exists ())
3949 return false;
3951 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3952 if (aggval->index == i)
3954 struct ipa_agg_jf_item *item;
3955 int j;
3956 bool found = false;
3957 FOR_EACH_VEC_ELT (values, j, item)
3958 if (item->value
3959 && item->offset == av->offset
3960 && values_equal_for_ipcp_p (item->value, av->value))
3962 found = true;
3963 break;
3965 if (!found)
3967 values.release ();
3968 return false;
3972 return true;
3975 /* Given an original NODE and a VAL for which we have already created a
3976 specialized clone, look whether there are incoming edges that still lead
3977 into the old node but now also bring the requested value and also conform to
3978 all other criteria such that they can be redirected the the special node.
3979 This function can therefore redirect the final edge in a SCC. */
3981 template <typename valtype>
3982 static void
3983 perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
3985 ipcp_value_source<valtype> *src;
3986 gcov_type redirected_sum = 0;
3988 for (src = val->sources; src; src = src->next)
3990 struct cgraph_edge *cs = src->cs;
3991 while (cs)
3993 if (cgraph_edge_brings_value_p (cs, src, node)
3994 && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3995 && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
3997 if (dump_file)
3998 fprintf (dump_file, " - adding an extra caller %s/%i"
3999 " of %s/%i\n",
4000 xstrdup_for_dump (cs->caller->name ()),
4001 cs->caller->order,
4002 xstrdup_for_dump (val->spec_node->name ()),
4003 val->spec_node->order);
4005 cs->redirect_callee_duplicating_thunks (val->spec_node);
4006 val->spec_node->expand_all_artificial_thunks ();
4007 redirected_sum += cs->count;
4009 cs = get_next_cgraph_edge_clone (cs);
4013 if (redirected_sum)
4014 update_specialized_profile (val->spec_node, node, redirected_sum);
4017 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4019 static bool
4020 known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
4022 ipa_polymorphic_call_context *ctx;
4023 int i;
4025 FOR_EACH_VEC_ELT (known_contexts, i, ctx)
4026 if (!ctx->useless_p ())
4027 return true;
4028 return false;
4031 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4033 static vec<ipa_polymorphic_call_context>
4034 copy_useful_known_contexts (vec<ipa_polymorphic_call_context> known_contexts)
4036 if (known_contexts_useful_p (known_contexts))
4037 return known_contexts.copy ();
4038 else
4039 return vNULL;
4042 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4043 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4045 static void
4046 modify_known_vectors_with_val (vec<tree> *known_csts,
4047 vec<ipa_polymorphic_call_context> *known_contexts,
4048 ipcp_value<tree> *val,
4049 int index)
4051 *known_csts = known_csts->copy ();
4052 *known_contexts = copy_useful_known_contexts (*known_contexts);
4053 (*known_csts)[index] = val->value;
4056 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4057 copy according to VAL and INDEX. */
4059 static void
4060 modify_known_vectors_with_val (vec<tree> *known_csts,
4061 vec<ipa_polymorphic_call_context> *known_contexts,
4062 ipcp_value<ipa_polymorphic_call_context> *val,
4063 int index)
4065 *known_csts = known_csts->copy ();
4066 *known_contexts = known_contexts->copy ();
4067 (*known_contexts)[index] = val->value;
4070 /* Return true if OFFSET indicates this was not an aggregate value or there is
4071 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4072 AGGVALS list. */
4074 DEBUG_FUNCTION bool
4075 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *aggvals,
4076 int index, HOST_WIDE_INT offset, tree value)
4078 if (offset == -1)
4079 return true;
4081 while (aggvals)
4083 if (aggvals->index == index
4084 && aggvals->offset == offset
4085 && values_equal_for_ipcp_p (aggvals->value, value))
4086 return true;
4087 aggvals = aggvals->next;
4089 return false;
4092 /* Return true if offset is minus one because source of a polymorphic contect
4093 cannot be an aggregate value. */
4095 DEBUG_FUNCTION bool
4096 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *,
4097 int , HOST_WIDE_INT offset,
4098 ipa_polymorphic_call_context)
4100 return offset == -1;
4103 /* Decide wheter to create a special version of NODE for value VAL of parameter
4104 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4105 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4106 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4108 template <typename valtype>
4109 static bool
4110 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
4111 ipcp_value<valtype> *val, vec<tree> known_csts,
4112 vec<ipa_polymorphic_call_context> known_contexts)
4114 struct ipa_agg_replacement_value *aggvals;
4115 int freq_sum, caller_count;
4116 gcov_type count_sum;
4117 vec<cgraph_edge *> callers;
4119 if (val->spec_node)
4121 perhaps_add_new_callers (node, val);
4122 return false;
4124 else if (val->local_size_cost + overall_size > max_new_size)
4126 if (dump_file && (dump_flags & TDF_DETAILS))
4127 fprintf (dump_file, " Ignoring candidate value because "
4128 "max_new_size would be reached with %li.\n",
4129 val->local_size_cost + overall_size);
4130 return false;
4132 else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum,
4133 &caller_count))
4134 return false;
4136 if (dump_file && (dump_flags & TDF_DETAILS))
4138 fprintf (dump_file, " - considering value ");
4139 print_ipcp_constant_value (dump_file, val->value);
4140 fprintf (dump_file, " for ");
4141 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
4142 if (offset != -1)
4143 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
4144 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
4147 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
4148 freq_sum, count_sum,
4149 val->local_size_cost)
4150 && !good_cloning_opportunity_p (node,
4151 val->local_time_benefit
4152 + val->prop_time_benefit,
4153 freq_sum, count_sum,
4154 val->local_size_cost
4155 + val->prop_size_cost))
4156 return false;
4158 if (dump_file)
4159 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
4160 node->name (), node->order);
4162 callers = gather_edges_for_value (val, node, caller_count);
4163 if (offset == -1)
4164 modify_known_vectors_with_val (&known_csts, &known_contexts, val, index);
4165 else
4167 known_csts = known_csts.copy ();
4168 known_contexts = copy_useful_known_contexts (known_contexts);
4170 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
4171 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
4172 aggvals = find_aggregate_values_for_callers_subset (node, callers);
4173 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
4174 offset, val->value));
4175 val->spec_node = create_specialized_node (node, known_csts, known_contexts,
4176 aggvals, callers);
4177 overall_size += val->local_size_cost;
4179 /* TODO: If for some lattice there is only one other known value
4180 left, make a special node for it too. */
4182 return true;
4185 /* Decide whether and what specialized clones of NODE should be created. */
4187 static bool
4188 decide_whether_version_node (struct cgraph_node *node)
4190 struct ipa_node_params *info = IPA_NODE_REF (node);
4191 int i, count = ipa_get_param_count (info);
4192 vec<tree> known_csts;
4193 vec<ipa_polymorphic_call_context> known_contexts;
4194 vec<ipa_agg_jump_function> known_aggs = vNULL;
4195 bool ret = false;
4197 if (count == 0)
4198 return false;
4200 if (dump_file && (dump_flags & TDF_DETAILS))
4201 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
4202 node->name (), node->order);
4204 gather_context_independent_values (info, &known_csts, &known_contexts,
4205 info->do_clone_for_all_contexts ? &known_aggs
4206 : NULL, NULL);
4208 for (i = 0; i < count ;i++)
4210 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4211 ipcp_lattice<tree> *lat = &plats->itself;
4212 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
4214 if (!lat->bottom
4215 && !known_csts[i])
4217 ipcp_value<tree> *val;
4218 for (val = lat->values; val; val = val->next)
4219 ret |= decide_about_value (node, i, -1, val, known_csts,
4220 known_contexts);
4223 if (!plats->aggs_bottom)
4225 struct ipcp_agg_lattice *aglat;
4226 ipcp_value<tree> *val;
4227 for (aglat = plats->aggs; aglat; aglat = aglat->next)
4228 if (!aglat->bottom && aglat->values
4229 /* If the following is false, the one value is in
4230 known_aggs. */
4231 && (plats->aggs_contain_variable
4232 || !aglat->is_single_const ()))
4233 for (val = aglat->values; val; val = val->next)
4234 ret |= decide_about_value (node, i, aglat->offset, val,
4235 known_csts, known_contexts);
4238 if (!ctxlat->bottom
4239 && known_contexts[i].useless_p ())
4241 ipcp_value<ipa_polymorphic_call_context> *val;
4242 for (val = ctxlat->values; val; val = val->next)
4243 ret |= decide_about_value (node, i, -1, val, known_csts,
4244 known_contexts);
4247 info = IPA_NODE_REF (node);
4250 if (info->do_clone_for_all_contexts)
4252 struct cgraph_node *clone;
4253 vec<cgraph_edge *> callers;
4255 if (dump_file)
4256 fprintf (dump_file, " - Creating a specialized node of %s/%i "
4257 "for all known contexts.\n", node->name (),
4258 node->order);
4260 callers = node->collect_callers ();
4262 if (!known_contexts_useful_p (known_contexts))
4264 known_contexts.release ();
4265 known_contexts = vNULL;
4267 clone = create_specialized_node (node, known_csts, known_contexts,
4268 known_aggs_to_agg_replacement_list (known_aggs),
4269 callers);
4270 info = IPA_NODE_REF (node);
4271 info->do_clone_for_all_contexts = false;
4272 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
4273 for (i = 0; i < count ; i++)
4274 vec_free (known_aggs[i].items);
4275 known_aggs.release ();
4276 ret = true;
4278 else
4280 known_csts.release ();
4281 known_contexts.release ();
4284 return ret;
4287 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4289 static void
4290 spread_undeadness (struct cgraph_node *node)
4292 struct cgraph_edge *cs;
4294 for (cs = node->callees; cs; cs = cs->next_callee)
4295 if (ipa_edge_within_scc (cs))
4297 struct cgraph_node *callee;
4298 struct ipa_node_params *info;
4300 callee = cs->callee->function_symbol (NULL);
4301 info = IPA_NODE_REF (callee);
4303 if (info->node_dead)
4305 info->node_dead = 0;
4306 spread_undeadness (callee);
4311 /* Return true if NODE has a caller from outside of its SCC that is not
4312 dead. Worker callback for cgraph_for_node_and_aliases. */
4314 static bool
4315 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
4316 void *data ATTRIBUTE_UNUSED)
4318 struct cgraph_edge *cs;
4320 for (cs = node->callers; cs; cs = cs->next_caller)
4321 if (cs->caller->thunk.thunk_p
4322 && cs->caller->call_for_symbol_thunks_and_aliases
4323 (has_undead_caller_from_outside_scc_p, NULL, true))
4324 return true;
4325 else if (!ipa_edge_within_scc (cs)
4326 && !IPA_NODE_REF (cs->caller)->node_dead)
4327 return true;
4328 return false;
4332 /* Identify nodes within the same SCC as NODE which are no longer needed
4333 because of new clones and will be removed as unreachable. */
4335 static void
4336 identify_dead_nodes (struct cgraph_node *node)
4338 struct cgraph_node *v;
4339 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4340 if (v->will_be_removed_from_program_if_no_direct_calls_p ()
4341 && !v->call_for_symbol_thunks_and_aliases
4342 (has_undead_caller_from_outside_scc_p, NULL, true))
4343 IPA_NODE_REF (v)->node_dead = 1;
4345 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4346 if (!IPA_NODE_REF (v)->node_dead)
4347 spread_undeadness (v);
4349 if (dump_file && (dump_flags & TDF_DETAILS))
4351 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4352 if (IPA_NODE_REF (v)->node_dead)
4353 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
4354 v->name (), v->order);
4358 /* The decision stage. Iterate over the topological order of call graph nodes
4359 TOPO and make specialized clones if deemed beneficial. */
4361 static void
4362 ipcp_decision_stage (struct ipa_topo_info *topo)
4364 int i;
4366 if (dump_file)
4367 fprintf (dump_file, "\nIPA decision stage:\n\n");
4369 for (i = topo->nnodes - 1; i >= 0; i--)
4371 struct cgraph_node *node = topo->order[i];
4372 bool change = false, iterate = true;
4374 while (iterate)
4376 struct cgraph_node *v;
4377 iterate = false;
4378 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4379 if (v->has_gimple_body_p ()
4380 && ipcp_versionable_function_p (v))
4381 iterate |= decide_whether_version_node (v);
4383 change |= iterate;
4385 if (change)
4386 identify_dead_nodes (node);
4390 /* Look up all alignment information that we have discovered and copy it over
4391 to the transformation summary. */
4393 static void
4394 ipcp_store_alignment_results (void)
4396 cgraph_node *node;
4398 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4400 ipa_node_params *info = IPA_NODE_REF (node);
4401 bool dumped_sth = false;
4402 bool found_useful_result = false;
4404 if (!opt_for_fn (node->decl, flag_ipa_cp_alignment))
4406 if (dump_file)
4407 fprintf (dump_file, "Not considering %s for alignment discovery "
4408 "and propagate; -fipa-cp-alignment: disabled.\n",
4409 node->name ());
4410 continue;
4413 if (info->ipcp_orig_node)
4414 info = IPA_NODE_REF (info->ipcp_orig_node);
4416 unsigned count = ipa_get_param_count (info);
4417 for (unsigned i = 0; i < count ; i++)
4419 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4420 if (plats->alignment.known
4421 && plats->alignment.align > 0)
4423 found_useful_result = true;
4424 break;
4427 if (!found_useful_result)
4428 continue;
4430 ipcp_grow_transformations_if_necessary ();
4431 ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node);
4432 vec_safe_reserve_exact (ts->alignments, count);
4434 for (unsigned i = 0; i < count ; i++)
4436 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4438 if (plats->alignment.align == 0)
4439 plats->alignment.known = false;
4441 ts->alignments->quick_push (plats->alignment);
4442 if (!dump_file || !plats->alignment.known)
4443 continue;
4444 if (!dumped_sth)
4446 fprintf (dump_file, "Propagated alignment info for function %s/%i:\n",
4447 node->name (), node->order);
4448 dumped_sth = true;
4450 fprintf (dump_file, " param %i: align: %u, misalign: %u\n",
4451 i, plats->alignment.align, plats->alignment.misalign);
4456 /* The IPCP driver. */
4458 static unsigned int
4459 ipcp_driver (void)
4461 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
4462 struct cgraph_edge_hook_list *edge_removal_hook_holder;
4463 struct ipa_topo_info topo;
4465 ipa_check_create_node_params ();
4466 ipa_check_create_edge_args ();
4467 grow_edge_clone_vectors ();
4468 edge_duplication_hook_holder =
4469 symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
4470 edge_removal_hook_holder =
4471 symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
4473 if (dump_file)
4475 fprintf (dump_file, "\nIPA structures before propagation:\n");
4476 if (dump_flags & TDF_DETAILS)
4477 ipa_print_all_params (dump_file);
4478 ipa_print_all_jump_functions (dump_file);
4481 /* Topological sort. */
4482 build_toporder_info (&topo);
4483 /* Do the interprocedural propagation. */
4484 ipcp_propagate_stage (&topo);
4485 /* Decide what constant propagation and cloning should be performed. */
4486 ipcp_decision_stage (&topo);
4487 /* Store results of alignment propagation. */
4488 ipcp_store_alignment_results ();
4490 /* Free all IPCP structures. */
4491 free_toporder_info (&topo);
4492 next_edge_clone.release ();
4493 prev_edge_clone.release ();
4494 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4495 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4496 ipa_free_all_structures_after_ipa_cp ();
4497 if (dump_file)
4498 fprintf (dump_file, "\nIPA constant propagation end\n");
4499 return 0;
4502 /* Initialization and computation of IPCP data structures. This is the initial
4503 intraprocedural analysis of functions, which gathers information to be
4504 propagated later on. */
4506 static void
4507 ipcp_generate_summary (void)
4509 struct cgraph_node *node;
4511 if (dump_file)
4512 fprintf (dump_file, "\nIPA constant propagation start:\n");
4513 ipa_register_cgraph_hooks ();
4515 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4517 node->local.versionable
4518 = tree_versionable_function_p (node->decl);
4519 ipa_analyze_node (node);
4523 /* Write ipcp summary for nodes in SET. */
4525 static void
4526 ipcp_write_summary (void)
4528 ipa_prop_write_jump_functions ();
4531 /* Read ipcp summary. */
4533 static void
4534 ipcp_read_summary (void)
4536 ipa_prop_read_jump_functions ();
4539 namespace {
4541 const pass_data pass_data_ipa_cp =
4543 IPA_PASS, /* type */
4544 "cp", /* name */
4545 OPTGROUP_NONE, /* optinfo_flags */
4546 TV_IPA_CONSTANT_PROP, /* tv_id */
4547 0, /* properties_required */
4548 0, /* properties_provided */
4549 0, /* properties_destroyed */
4550 0, /* todo_flags_start */
4551 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
4554 class pass_ipa_cp : public ipa_opt_pass_d
4556 public:
4557 pass_ipa_cp (gcc::context *ctxt)
4558 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
4559 ipcp_generate_summary, /* generate_summary */
4560 ipcp_write_summary, /* write_summary */
4561 ipcp_read_summary, /* read_summary */
4562 ipcp_write_transformation_summaries, /*
4563 write_optimization_summary */
4564 ipcp_read_transformation_summaries, /*
4565 read_optimization_summary */
4566 NULL, /* stmt_fixup */
4567 0, /* function_transform_todo_flags_start */
4568 ipcp_transform_function, /* function_transform */
4569 NULL) /* variable_transform */
4572 /* opt_pass methods: */
4573 virtual bool gate (function *)
4575 /* FIXME: We should remove the optimize check after we ensure we never run
4576 IPA passes when not optimizing. */
4577 return (flag_ipa_cp && optimize) || in_lto_p;
4580 virtual unsigned int execute (function *) { return ipcp_driver (); }
4582 }; // class pass_ipa_cp
4584 } // anon namespace
4586 ipa_opt_pass_d *
4587 make_pass_ipa_cp (gcc::context *ctxt)
4589 return new pass_ipa_cp (ctxt);
4592 /* Reset all state within ipa-cp.c so that we can rerun the compiler
4593 within the same process. For use by toplev::finalize. */
4595 void
4596 ipa_cp_c_finalize (void)
4598 max_count = 0;
4599 overall_size = 0;
4600 max_new_size = 0;