Implement C _FloatN, _FloatNx types.
[official-gcc.git] / gcc / ipa-cp.c
blob5b6cb9a143f711e5fde0e133a94d6651a2de1d6d
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2016 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 "backend.h"
107 #include "tree.h"
108 #include "gimple-expr.h"
109 #include "predict.h"
110 #include "alloc-pool.h"
111 #include "tree-pass.h"
112 #include "cgraph.h"
113 #include "diagnostic.h"
114 #include "fold-const.h"
115 #include "gimple-fold.h"
116 #include "symbol-summary.h"
117 #include "ipa-prop.h"
118 #include "tree-pretty-print.h"
119 #include "tree-inline.h"
120 #include "params.h"
121 #include "ipa-inline.h"
122 #include "ipa-utils.h"
124 template <typename valtype> class ipcp_value;
126 /* Describes a particular source for an IPA-CP value. */
128 template <typename valtype>
129 class ipcp_value_source
131 public:
132 /* Aggregate offset of the source, negative if the source is scalar value of
133 the argument itself. */
134 HOST_WIDE_INT offset;
135 /* The incoming edge that brought the value. */
136 cgraph_edge *cs;
137 /* If the jump function that resulted into his value was a pass-through or an
138 ancestor, this is the ipcp_value of the caller from which the described
139 value has been derived. Otherwise it is NULL. */
140 ipcp_value<valtype> *val;
141 /* Next pointer in a linked list of sources of a value. */
142 ipcp_value_source *next;
143 /* If the jump function that resulted into his value was a pass-through or an
144 ancestor, this is the index of the parameter of the caller the jump
145 function references. */
146 int index;
149 /* Common ancestor for all ipcp_value instantiations. */
151 class ipcp_value_base
153 public:
154 /* Time benefit and size cost that specializing the function for this value
155 would bring about in this function alone. */
156 int local_time_benefit, local_size_cost;
157 /* Time benefit and size cost that specializing the function for this value
158 can bring about in it's callees (transitively). */
159 int prop_time_benefit, prop_size_cost;
162 /* Describes one particular value stored in struct ipcp_lattice. */
164 template <typename valtype>
165 class ipcp_value : public ipcp_value_base
167 public:
168 /* The actual value for the given parameter. */
169 valtype value;
170 /* The list of sources from which this value originates. */
171 ipcp_value_source <valtype> *sources;
172 /* Next pointers in a linked list of all values in a lattice. */
173 ipcp_value *next;
174 /* Next pointers in a linked list of values in a strongly connected component
175 of values. */
176 ipcp_value *scc_next;
177 /* Next pointers in a linked list of SCCs of values sorted topologically
178 according their sources. */
179 ipcp_value *topo_next;
180 /* A specialized node created for this value, NULL if none has been (so far)
181 created. */
182 cgraph_node *spec_node;
183 /* Depth first search number and low link for topological sorting of
184 values. */
185 int dfs, low_link;
186 /* True if this valye is currently on the topo-sort stack. */
187 bool on_stack;
189 void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx,
190 HOST_WIDE_INT offset);
193 /* Lattice describing potential values of a formal parameter of a function, or
194 a part of an aggreagate. TOP is represented by a lattice with zero values
195 and with contains_variable and bottom flags cleared. BOTTOM is represented
196 by a lattice with the bottom flag set. In that case, values and
197 contains_variable flag should be disregarded. */
199 template <typename valtype>
200 class ipcp_lattice
202 public:
203 /* The list of known values and types in this lattice. Note that values are
204 not deallocated if a lattice is set to bottom because there may be value
205 sources referencing them. */
206 ipcp_value<valtype> *values;
207 /* Number of known values and types in this lattice. */
208 int values_count;
209 /* The lattice contains a variable component (in addition to values). */
210 bool contains_variable;
211 /* The value of the lattice is bottom (i.e. variable and unusable for any
212 propagation). */
213 bool bottom;
215 inline bool is_single_const ();
216 inline bool set_to_bottom ();
217 inline bool set_contains_variable ();
218 bool add_value (valtype newval, cgraph_edge *cs,
219 ipcp_value<valtype> *src_val = NULL,
220 int src_idx = 0, HOST_WIDE_INT offset = -1);
221 void print (FILE * f, bool dump_sources, bool dump_benefits);
224 /* Lattice of tree values with an offset to describe a part of an
225 aggregate. */
227 class ipcp_agg_lattice : public ipcp_lattice<tree>
229 public:
230 /* Offset that is being described by this lattice. */
231 HOST_WIDE_INT offset;
232 /* Size so that we don't have to re-compute it every time we traverse the
233 list. Must correspond to TYPE_SIZE of all lat values. */
234 HOST_WIDE_INT size;
235 /* Next element of the linked list. */
236 struct ipcp_agg_lattice *next;
239 /* Lattice of pointer alignment. Unlike the previous types of lattices, this
240 one is only capable of holding one value. */
242 class ipcp_alignment_lattice
244 public:
245 /* If bottom and top are both false, these two fields hold values as given by
246 ptr_info_def and get_pointer_alignment_1. */
247 unsigned align;
248 unsigned misalign;
250 inline bool bottom_p () const;
251 inline bool top_p () const;
252 inline bool set_to_bottom ();
253 bool meet_with (unsigned new_align, unsigned new_misalign);
254 bool meet_with (const ipcp_alignment_lattice &other, HOST_WIDE_INT offset);
255 void print (FILE * f);
256 private:
257 /* If set, this lattice is bottom and all other fields should be
258 disregarded. */
259 bool bottom;
260 /* If bottom and not_top are false, the lattice is TOP. If not_top is true,
261 the known alignment is stored in the fields align and misalign. The field
262 is negated so that memset to zero initializes the lattice to TOP
263 state. */
264 bool not_top;
266 bool meet_with_1 (unsigned new_align, unsigned new_misalign);
269 /* Structure containing lattices for a parameter itself and for pieces of
270 aggregates that are passed in the parameter or by a reference in a parameter
271 plus some other useful flags. */
273 class ipcp_param_lattices
275 public:
276 /* Lattice describing the value of the parameter itself. */
277 ipcp_lattice<tree> itself;
278 /* Lattice describing the polymorphic contexts of a parameter. */
279 ipcp_lattice<ipa_polymorphic_call_context> ctxlat;
280 /* Lattices describing aggregate parts. */
281 ipcp_agg_lattice *aggs;
282 /* Lattice describing known alignment. */
283 ipcp_alignment_lattice alignment;
284 /* Number of aggregate lattices */
285 int aggs_count;
286 /* True if aggregate data were passed by reference (as opposed to by
287 value). */
288 bool aggs_by_ref;
289 /* All aggregate lattices contain a variable component (in addition to
290 values). */
291 bool aggs_contain_variable;
292 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
293 for any propagation). */
294 bool aggs_bottom;
296 /* There is a virtual call based on this parameter. */
297 bool virt_call;
300 /* Allocation pools for values and their sources in ipa-cp. */
302 object_allocator<ipcp_value<tree> > ipcp_cst_values_pool
303 ("IPA-CP constant values");
305 object_allocator<ipcp_value<ipa_polymorphic_call_context> >
306 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
308 object_allocator<ipcp_value_source<tree> > ipcp_sources_pool
309 ("IPA-CP value sources");
311 object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
312 ("IPA_CP aggregate lattices");
314 /* Maximal count found in program. */
316 static gcov_type max_count;
318 /* Original overall size of the program. */
320 static long overall_size, max_new_size;
322 /* Return the param lattices structure corresponding to the Ith formal
323 parameter of the function described by INFO. */
324 static inline struct ipcp_param_lattices *
325 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
327 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
328 gcc_checking_assert (!info->ipcp_orig_node);
329 gcc_checking_assert (info->lattices);
330 return &(info->lattices[i]);
333 /* Return the lattice corresponding to the scalar value of the Ith formal
334 parameter of the function described by INFO. */
335 static inline ipcp_lattice<tree> *
336 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
338 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
339 return &plats->itself;
342 /* Return the lattice corresponding to the scalar value of the Ith formal
343 parameter of the function described by INFO. */
344 static inline ipcp_lattice<ipa_polymorphic_call_context> *
345 ipa_get_poly_ctx_lat (struct ipa_node_params *info, int i)
347 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
348 return &plats->ctxlat;
351 /* Return whether LAT is a lattice with a single constant and without an
352 undefined value. */
354 template <typename valtype>
355 inline bool
356 ipcp_lattice<valtype>::is_single_const ()
358 if (bottom || contains_variable || values_count != 1)
359 return false;
360 else
361 return true;
364 /* Print V which is extracted from a value in a lattice to F. */
366 static void
367 print_ipcp_constant_value (FILE * f, tree v)
369 if (TREE_CODE (v) == ADDR_EXPR
370 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
372 fprintf (f, "& ");
373 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
375 else
376 print_generic_expr (f, v, 0);
379 /* Print V which is extracted from a value in a lattice to F. */
381 static void
382 print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
384 v.dump(f, false);
387 /* Print a lattice LAT to F. */
389 template <typename valtype>
390 void
391 ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
393 ipcp_value<valtype> *val;
394 bool prev = false;
396 if (bottom)
398 fprintf (f, "BOTTOM\n");
399 return;
402 if (!values_count && !contains_variable)
404 fprintf (f, "TOP\n");
405 return;
408 if (contains_variable)
410 fprintf (f, "VARIABLE");
411 prev = true;
412 if (dump_benefits)
413 fprintf (f, "\n");
416 for (val = values; val; val = val->next)
418 if (dump_benefits && prev)
419 fprintf (f, " ");
420 else if (!dump_benefits && prev)
421 fprintf (f, ", ");
422 else
423 prev = true;
425 print_ipcp_constant_value (f, val->value);
427 if (dump_sources)
429 ipcp_value_source<valtype> *s;
431 fprintf (f, " [from:");
432 for (s = val->sources; s; s = s->next)
433 fprintf (f, " %i(%i)", s->cs->caller->order,
434 s->cs->frequency);
435 fprintf (f, "]");
438 if (dump_benefits)
439 fprintf (f, " [loc_time: %i, loc_size: %i, "
440 "prop_time: %i, prop_size: %i]\n",
441 val->local_time_benefit, val->local_size_cost,
442 val->prop_time_benefit, val->prop_size_cost);
444 if (!dump_benefits)
445 fprintf (f, "\n");
448 /* Print alignment lattice to F. */
450 void
451 ipcp_alignment_lattice::print (FILE * f)
453 if (top_p ())
454 fprintf (f, " Alignment unknown (TOP)\n");
455 else if (bottom_p ())
456 fprintf (f, " Alignment unusable (BOTTOM)\n");
457 else
458 fprintf (f, " Alignment %u, misalignment %u\n", align, misalign);
461 /* Print all ipcp_lattices of all functions to F. */
463 static void
464 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
466 struct cgraph_node *node;
467 int i, count;
469 fprintf (f, "\nLattices:\n");
470 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
472 struct ipa_node_params *info;
474 info = IPA_NODE_REF (node);
475 fprintf (f, " Node: %s/%i:\n", node->name (),
476 node->order);
477 count = ipa_get_param_count (info);
478 for (i = 0; i < count; i++)
480 struct ipcp_agg_lattice *aglat;
481 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
482 fprintf (f, " param [%d]: ", i);
483 plats->itself.print (f, dump_sources, dump_benefits);
484 fprintf (f, " ctxs: ");
485 plats->ctxlat.print (f, dump_sources, dump_benefits);
486 plats->alignment.print (f);
487 if (plats->virt_call)
488 fprintf (f, " virt_call flag set\n");
490 if (plats->aggs_bottom)
492 fprintf (f, " AGGS BOTTOM\n");
493 continue;
495 if (plats->aggs_contain_variable)
496 fprintf (f, " AGGS VARIABLE\n");
497 for (aglat = plats->aggs; aglat; aglat = aglat->next)
499 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
500 plats->aggs_by_ref ? "ref " : "", aglat->offset);
501 aglat->print (f, dump_sources, dump_benefits);
507 /* Determine whether it is at all technically possible to create clones of NODE
508 and store this information in the ipa_node_params structure associated
509 with NODE. */
511 static void
512 determine_versionability (struct cgraph_node *node,
513 struct ipa_node_params *info)
515 const char *reason = NULL;
517 /* There are a number of generic reasons functions cannot be versioned. We
518 also cannot remove parameters if there are type attributes such as fnspec
519 present. */
520 if (node->alias || node->thunk.thunk_p)
521 reason = "alias or thunk";
522 else if (!node->local.versionable)
523 reason = "not a tree_versionable_function";
524 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
525 reason = "insufficient body availability";
526 else if (!opt_for_fn (node->decl, optimize)
527 || !opt_for_fn (node->decl, flag_ipa_cp))
528 reason = "non-optimized function";
529 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
531 /* Ideally we should clone the SIMD clones themselves and create
532 vector copies of them, so IPA-cp and SIMD clones can happily
533 coexist, but that may not be worth the effort. */
534 reason = "function has SIMD clones";
536 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (node->decl)))
538 /* Ideally we should clone the target clones themselves and create
539 copies of them, so IPA-cp and target clones can happily
540 coexist, but that may not be worth the effort. */
541 reason = "function target_clones attribute";
543 /* Don't clone decls local to a comdat group; it breaks and for C++
544 decloned constructors, inlining is always better anyway. */
545 else if (node->comdat_local_p ())
546 reason = "comdat-local function";
548 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
549 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
550 node->name (), node->order, reason);
552 info->versionable = (reason == NULL);
555 /* Return true if it is at all technically possible to create clones of a
556 NODE. */
558 static bool
559 ipcp_versionable_function_p (struct cgraph_node *node)
561 return IPA_NODE_REF (node)->versionable;
564 /* Structure holding accumulated information about callers of a node. */
566 struct caller_statistics
568 gcov_type count_sum;
569 int n_calls, n_hot_calls, freq_sum;
572 /* Initialize fields of STAT to zeroes. */
574 static inline void
575 init_caller_stats (struct caller_statistics *stats)
577 stats->count_sum = 0;
578 stats->n_calls = 0;
579 stats->n_hot_calls = 0;
580 stats->freq_sum = 0;
583 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
584 non-thunk incoming edges to NODE. */
586 static bool
587 gather_caller_stats (struct cgraph_node *node, void *data)
589 struct caller_statistics *stats = (struct caller_statistics *) data;
590 struct cgraph_edge *cs;
592 for (cs = node->callers; cs; cs = cs->next_caller)
593 if (!cs->caller->thunk.thunk_p)
595 stats->count_sum += cs->count;
596 stats->freq_sum += cs->frequency;
597 stats->n_calls++;
598 if (cs->maybe_hot_p ())
599 stats->n_hot_calls ++;
601 return false;
605 /* Return true if this NODE is viable candidate for cloning. */
607 static bool
608 ipcp_cloning_candidate_p (struct cgraph_node *node)
610 struct caller_statistics stats;
612 gcc_checking_assert (node->has_gimple_body_p ());
614 if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
616 if (dump_file)
617 fprintf (dump_file, "Not considering %s for cloning; "
618 "-fipa-cp-clone disabled.\n",
619 node->name ());
620 return false;
623 if (node->optimize_for_size_p ())
625 if (dump_file)
626 fprintf (dump_file, "Not considering %s for cloning; "
627 "optimizing it for size.\n",
628 node->name ());
629 return false;
632 init_caller_stats (&stats);
633 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
635 if (inline_summaries->get (node)->self_size < stats.n_calls)
637 if (dump_file)
638 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
639 node->name ());
640 return true;
643 /* When profile is available and function is hot, propagate into it even if
644 calls seems cold; constant propagation can improve function's speed
645 significantly. */
646 if (max_count)
648 if (stats.count_sum > node->count * 90 / 100)
650 if (dump_file)
651 fprintf (dump_file, "Considering %s for cloning; "
652 "usually called directly.\n",
653 node->name ());
654 return true;
657 if (!stats.n_hot_calls)
659 if (dump_file)
660 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
661 node->name ());
662 return false;
664 if (dump_file)
665 fprintf (dump_file, "Considering %s for cloning.\n",
666 node->name ());
667 return true;
670 template <typename valtype>
671 class value_topo_info
673 public:
674 /* Head of the linked list of topologically sorted values. */
675 ipcp_value<valtype> *values_topo;
676 /* Stack for creating SCCs, represented by a linked list too. */
677 ipcp_value<valtype> *stack;
678 /* Counter driving the algorithm in add_val_to_toposort. */
679 int dfs_counter;
681 value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
683 void add_val (ipcp_value<valtype> *cur_val);
684 void propagate_effects ();
687 /* Arrays representing a topological ordering of call graph nodes and a stack
688 of nodes used during constant propagation and also data required to perform
689 topological sort of values and propagation of benefits in the determined
690 order. */
692 class ipa_topo_info
694 public:
695 /* Array with obtained topological order of cgraph nodes. */
696 struct cgraph_node **order;
697 /* Stack of cgraph nodes used during propagation within SCC until all values
698 in the SCC stabilize. */
699 struct cgraph_node **stack;
700 int nnodes, stack_top;
702 value_topo_info<tree> constants;
703 value_topo_info<ipa_polymorphic_call_context> contexts;
705 ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
706 constants ()
710 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
712 static void
713 build_toporder_info (struct ipa_topo_info *topo)
715 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
716 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
718 gcc_checking_assert (topo->stack_top == 0);
719 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
722 /* Free information about strongly connected components and the arrays in
723 TOPO. */
725 static void
726 free_toporder_info (struct ipa_topo_info *topo)
728 ipa_free_postorder_info ();
729 free (topo->order);
730 free (topo->stack);
733 /* Add NODE to the stack in TOPO, unless it is already there. */
735 static inline void
736 push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node)
738 struct ipa_node_params *info = IPA_NODE_REF (node);
739 if (info->node_enqueued)
740 return;
741 info->node_enqueued = 1;
742 topo->stack[topo->stack_top++] = node;
745 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
746 is empty. */
748 static struct cgraph_node *
749 pop_node_from_stack (struct ipa_topo_info *topo)
751 if (topo->stack_top)
753 struct cgraph_node *node;
754 topo->stack_top--;
755 node = topo->stack[topo->stack_top];
756 IPA_NODE_REF (node)->node_enqueued = 0;
757 return node;
759 else
760 return NULL;
763 /* Set lattice LAT to bottom and return true if it previously was not set as
764 such. */
766 template <typename valtype>
767 inline bool
768 ipcp_lattice<valtype>::set_to_bottom ()
770 bool ret = !bottom;
771 bottom = true;
772 return ret;
775 /* Mark lattice as containing an unknown value and return true if it previously
776 was not marked as such. */
778 template <typename valtype>
779 inline bool
780 ipcp_lattice<valtype>::set_contains_variable ()
782 bool ret = !contains_variable;
783 contains_variable = true;
784 return ret;
787 /* Set all aggegate lattices in PLATS to bottom and return true if they were
788 not previously set as such. */
790 static inline bool
791 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
793 bool ret = !plats->aggs_bottom;
794 plats->aggs_bottom = true;
795 return ret;
798 /* Mark all aggegate lattices in PLATS as containing an unknown value and
799 return true if they were not previously marked as such. */
801 static inline bool
802 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
804 bool ret = !plats->aggs_contain_variable;
805 plats->aggs_contain_variable = true;
806 return ret;
809 /* Return true if alignment information in the lattice is yet unknown. */
811 bool
812 ipcp_alignment_lattice::top_p () const
814 return !bottom && !not_top;
817 /* Return true if alignment information in the lattice is known to be
818 unusable. */
820 bool
821 ipcp_alignment_lattice::bottom_p () const
823 return bottom;
826 /* Set alignment information in the lattice to bottom. Return true if it
827 previously was in a different state. */
829 bool
830 ipcp_alignment_lattice::set_to_bottom ()
832 if (bottom_p ())
833 return false;
834 bottom = true;
835 return true;
838 /* Meet the current value of the lattice with alignment described by NEW_ALIGN
839 and NEW_MISALIGN, assuming that we know the current value is neither TOP nor
840 BOTTOM. Return true if the value of lattice has changed. */
842 bool
843 ipcp_alignment_lattice::meet_with_1 (unsigned new_align, unsigned new_misalign)
845 gcc_checking_assert (new_align != 0);
846 if (align == new_align && misalign == new_misalign)
847 return false;
849 bool changed = false;
850 if (align > new_align)
852 align = new_align;
853 misalign = misalign % new_align;
854 changed = true;
856 if (misalign != (new_misalign % align))
858 int diff = abs ((int) misalign - (int) (new_misalign % align));
859 align = (unsigned) diff & -diff;
860 if (align)
861 misalign = misalign % align;
862 else
863 set_to_bottom ();
864 changed = true;
866 gcc_checking_assert (bottom_p () || align != 0);
867 return changed;
870 /* Meet the current value of the lattice with alignment described by NEW_ALIGN
871 and NEW_MISALIGN. Return true if the value of lattice has changed. */
873 bool
874 ipcp_alignment_lattice::meet_with (unsigned new_align, unsigned new_misalign)
876 gcc_assert (new_align != 0);
877 if (bottom_p ())
878 return false;
879 if (top_p ())
881 not_top = true;
882 align = new_align;
883 misalign = new_misalign;
884 return true;
886 return meet_with_1 (new_align, new_misalign);
889 /* Meet the current value of the lattice with OTHER, taking into account that
890 OFFSET has been added to the pointer value. Return true if the value of
891 lattice has changed. */
893 bool
894 ipcp_alignment_lattice::meet_with (const ipcp_alignment_lattice &other,
895 HOST_WIDE_INT offset)
897 if (other.bottom_p ())
898 return set_to_bottom ();
899 if (bottom_p () || other.top_p ())
900 return false;
902 unsigned adjusted_misalign = (other.misalign + offset) % other.align;
903 if (top_p ())
905 not_top = true;
906 align = other.align;
907 misalign = adjusted_misalign;
908 return true;
911 return meet_with_1 (other.align, adjusted_misalign);
914 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
915 return true is any of them has not been marked as such so far. */
917 static inline bool
918 set_all_contains_variable (struct ipcp_param_lattices *plats)
920 bool ret;
921 ret = plats->itself.set_contains_variable ();
922 ret |= plats->ctxlat.set_contains_variable ();
923 ret |= set_agg_lats_contain_variable (plats);
924 ret |= plats->alignment.set_to_bottom ();
925 return ret;
928 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
929 points to by the number of callers to NODE. */
931 static bool
932 count_callers (cgraph_node *node, void *data)
934 int *caller_count = (int *) data;
936 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
937 /* Local thunks can be handled transparently, but if the thunk can not
938 be optimized out, count it as a real use. */
939 if (!cs->caller->thunk.thunk_p || !cs->caller->local.local)
940 ++*caller_count;
941 return false;
944 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
945 the one caller of some other node. Set the caller's corresponding flag. */
947 static bool
948 set_single_call_flag (cgraph_node *node, void *)
950 cgraph_edge *cs = node->callers;
951 /* Local thunks can be handled transparently, skip them. */
952 while (cs && cs->caller->thunk.thunk_p && cs->caller->local.local)
953 cs = cs->next_caller;
954 if (cs)
956 IPA_NODE_REF (cs->caller)->node_calling_single_call = true;
957 return true;
959 return false;
962 /* Initialize ipcp_lattices. */
964 static void
965 initialize_node_lattices (struct cgraph_node *node)
967 struct ipa_node_params *info = IPA_NODE_REF (node);
968 struct cgraph_edge *ie;
969 bool disable = false, variable = false;
970 int i;
972 gcc_checking_assert (node->has_gimple_body_p ());
973 if (cgraph_local_p (node))
975 int caller_count = 0;
976 node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
977 true);
978 gcc_checking_assert (caller_count > 0);
979 if (caller_count == 1)
980 node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
981 NULL, true);
983 else
985 /* When cloning is allowed, we can assume that externally visible
986 functions are not called. We will compensate this by cloning
987 later. */
988 if (ipcp_versionable_function_p (node)
989 && ipcp_cloning_candidate_p (node))
990 variable = true;
991 else
992 disable = true;
995 if (disable || variable)
997 for (i = 0; i < ipa_get_param_count (info) ; i++)
999 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1000 if (disable)
1002 plats->itself.set_to_bottom ();
1003 plats->ctxlat.set_to_bottom ();
1004 set_agg_lats_to_bottom (plats);
1005 plats->alignment.set_to_bottom ();
1007 else
1008 set_all_contains_variable (plats);
1010 if (dump_file && (dump_flags & TDF_DETAILS)
1011 && !node->alias && !node->thunk.thunk_p)
1012 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
1013 node->name (), node->order,
1014 disable ? "BOTTOM" : "VARIABLE");
1017 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1018 if (ie->indirect_info->polymorphic
1019 && ie->indirect_info->param_index >= 0)
1021 gcc_checking_assert (ie->indirect_info->param_index >= 0);
1022 ipa_get_parm_lattices (info,
1023 ie->indirect_info->param_index)->virt_call = 1;
1027 /* Return the result of a (possibly arithmetic) pass through jump function
1028 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
1029 determined or be considered an interprocedural invariant. */
1031 static tree
1032 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
1034 tree restype, res;
1036 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1037 return input;
1038 if (!is_gimple_ip_invariant (input))
1039 return NULL_TREE;
1041 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
1042 == tcc_comparison)
1043 restype = boolean_type_node;
1044 else
1045 restype = TREE_TYPE (input);
1046 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
1047 input, ipa_get_jf_pass_through_operand (jfunc));
1049 if (res && !is_gimple_ip_invariant (res))
1050 return NULL_TREE;
1052 return res;
1055 /* Return the result of an ancestor jump function JFUNC on the constant value
1056 INPUT. Return NULL_TREE if that cannot be determined. */
1058 static tree
1059 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
1061 gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
1062 if (TREE_CODE (input) == ADDR_EXPR)
1064 tree t = TREE_OPERAND (input, 0);
1065 t = build_ref_for_offset (EXPR_LOCATION (t), t,
1066 ipa_get_jf_ancestor_offset (jfunc), false,
1067 ptr_type_node, NULL, false);
1068 return build_fold_addr_expr (t);
1070 else
1071 return NULL_TREE;
1074 /* Determine whether JFUNC evaluates to a single known constant value and if
1075 so, return it. Otherwise return NULL. INFO describes the caller node or
1076 the one it is inlined to, so that pass-through jump functions can be
1077 evaluated. */
1079 tree
1080 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
1082 if (jfunc->type == IPA_JF_CONST)
1083 return ipa_get_jf_constant (jfunc);
1084 else if (jfunc->type == IPA_JF_PASS_THROUGH
1085 || jfunc->type == IPA_JF_ANCESTOR)
1087 tree input;
1088 int idx;
1090 if (jfunc->type == IPA_JF_PASS_THROUGH)
1091 idx = ipa_get_jf_pass_through_formal_id (jfunc);
1092 else
1093 idx = ipa_get_jf_ancestor_formal_id (jfunc);
1095 if (info->ipcp_orig_node)
1096 input = info->known_csts[idx];
1097 else
1099 ipcp_lattice<tree> *lat;
1101 if (!info->lattices
1102 || idx >= ipa_get_param_count (info))
1103 return NULL_TREE;
1104 lat = ipa_get_scalar_lat (info, idx);
1105 if (!lat->is_single_const ())
1106 return NULL_TREE;
1107 input = lat->values->value;
1110 if (!input)
1111 return NULL_TREE;
1113 if (jfunc->type == IPA_JF_PASS_THROUGH)
1114 return ipa_get_jf_pass_through_result (jfunc, input);
1115 else
1116 return ipa_get_jf_ancestor_result (jfunc, input);
1118 else
1119 return NULL_TREE;
1122 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1123 that INFO describes the caller node or the one it is inlined to, CS is the
1124 call graph edge corresponding to JFUNC and CSIDX index of the described
1125 parameter. */
1127 ipa_polymorphic_call_context
1128 ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1129 ipa_jump_func *jfunc)
1131 ipa_edge_args *args = IPA_EDGE_REF (cs);
1132 ipa_polymorphic_call_context ctx;
1133 ipa_polymorphic_call_context *edge_ctx
1134 = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1136 if (edge_ctx && !edge_ctx->useless_p ())
1137 ctx = *edge_ctx;
1139 if (jfunc->type == IPA_JF_PASS_THROUGH
1140 || jfunc->type == IPA_JF_ANCESTOR)
1142 ipa_polymorphic_call_context srcctx;
1143 int srcidx;
1144 bool type_preserved = true;
1145 if (jfunc->type == IPA_JF_PASS_THROUGH)
1147 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1148 return ctx;
1149 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1150 srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1152 else
1154 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1155 srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1157 if (info->ipcp_orig_node)
1159 if (info->known_contexts.exists ())
1160 srcctx = info->known_contexts[srcidx];
1162 else
1164 if (!info->lattices
1165 || srcidx >= ipa_get_param_count (info))
1166 return ctx;
1167 ipcp_lattice<ipa_polymorphic_call_context> *lat;
1168 lat = ipa_get_poly_ctx_lat (info, srcidx);
1169 if (!lat->is_single_const ())
1170 return ctx;
1171 srcctx = lat->values->value;
1173 if (srcctx.useless_p ())
1174 return ctx;
1175 if (jfunc->type == IPA_JF_ANCESTOR)
1176 srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1177 if (!type_preserved)
1178 srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1179 srcctx.combine_with (ctx);
1180 return srcctx;
1183 return ctx;
1186 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1187 bottom, not containing a variable component and without any known value at
1188 the same time. */
1190 DEBUG_FUNCTION void
1191 ipcp_verify_propagated_values (void)
1193 struct cgraph_node *node;
1195 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1197 struct ipa_node_params *info = IPA_NODE_REF (node);
1198 int i, count = ipa_get_param_count (info);
1200 for (i = 0; i < count; i++)
1202 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
1204 if (!lat->bottom
1205 && !lat->contains_variable
1206 && lat->values_count == 0)
1208 if (dump_file)
1210 symtab_node::dump_table (dump_file);
1211 fprintf (dump_file, "\nIPA lattices after constant "
1212 "propagation, before gcc_unreachable:\n");
1213 print_all_lattices (dump_file, true, false);
1216 gcc_unreachable ();
1222 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1224 static bool
1225 values_equal_for_ipcp_p (tree x, tree y)
1227 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
1229 if (x == y)
1230 return true;
1232 if (TREE_CODE (x) == ADDR_EXPR
1233 && TREE_CODE (y) == ADDR_EXPR
1234 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
1235 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
1236 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
1237 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
1238 else
1239 return operand_equal_p (x, y, 0);
1242 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1244 static bool
1245 values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
1246 ipa_polymorphic_call_context y)
1248 return x.equal_to (y);
1252 /* Add a new value source to the value represented by THIS, marking that a
1253 value comes from edge CS and (if the underlying jump function is a
1254 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1255 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1256 scalar value of the parameter itself or the offset within an aggregate. */
1258 template <typename valtype>
1259 void
1260 ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
1261 int src_idx, HOST_WIDE_INT offset)
1263 ipcp_value_source<valtype> *src;
1265 src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
1266 src->offset = offset;
1267 src->cs = cs;
1268 src->val = src_val;
1269 src->index = src_idx;
1271 src->next = sources;
1272 sources = src;
1275 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1276 SOURCE and clear all other fields. */
1278 static ipcp_value<tree> *
1279 allocate_and_init_ipcp_value (tree source)
1281 ipcp_value<tree> *val;
1283 val = ipcp_cst_values_pool.allocate ();
1284 memset (val, 0, sizeof (*val));
1285 val->value = source;
1286 return val;
1289 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1290 value to SOURCE and clear all other fields. */
1292 static ipcp_value<ipa_polymorphic_call_context> *
1293 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source)
1295 ipcp_value<ipa_polymorphic_call_context> *val;
1297 // TODO
1298 val = ipcp_poly_ctx_values_pool.allocate ();
1299 memset (val, 0, sizeof (*val));
1300 val->value = source;
1301 return val;
1304 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1305 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1306 meaning. OFFSET -1 means the source is scalar and not a part of an
1307 aggregate. */
1309 template <typename valtype>
1310 bool
1311 ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
1312 ipcp_value<valtype> *src_val,
1313 int src_idx, HOST_WIDE_INT offset)
1315 ipcp_value<valtype> *val;
1317 if (bottom)
1318 return false;
1320 for (val = values; val; val = val->next)
1321 if (values_equal_for_ipcp_p (val->value, newval))
1323 if (ipa_edge_within_scc (cs))
1325 ipcp_value_source<valtype> *s;
1326 for (s = val->sources; s ; s = s->next)
1327 if (s->cs == cs)
1328 break;
1329 if (s)
1330 return false;
1333 val->add_source (cs, src_val, src_idx, offset);
1334 return false;
1337 if (values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
1339 /* We can only free sources, not the values themselves, because sources
1340 of other values in this SCC might point to them. */
1341 for (val = values; val; val = val->next)
1343 while (val->sources)
1345 ipcp_value_source<valtype> *src = val->sources;
1346 val->sources = src->next;
1347 ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
1351 values = NULL;
1352 return set_to_bottom ();
1355 values_count++;
1356 val = allocate_and_init_ipcp_value (newval);
1357 val->add_source (cs, src_val, src_idx, offset);
1358 val->next = values;
1359 values = val;
1360 return true;
1363 /* Propagate values through a pass-through jump function JFUNC associated with
1364 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1365 is the index of the source parameter. */
1367 static bool
1368 propagate_vals_accross_pass_through (cgraph_edge *cs,
1369 ipa_jump_func *jfunc,
1370 ipcp_lattice<tree> *src_lat,
1371 ipcp_lattice<tree> *dest_lat,
1372 int src_idx)
1374 ipcp_value<tree> *src_val;
1375 bool ret = false;
1377 /* Do not create new values when propagating within an SCC because if there
1378 are arithmetic functions with circular dependencies, there is infinite
1379 number of them and we would just make lattices bottom. */
1380 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1381 && ipa_edge_within_scc (cs))
1382 ret = dest_lat->set_contains_variable ();
1383 else
1384 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1386 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1388 if (cstval)
1389 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx);
1390 else
1391 ret |= dest_lat->set_contains_variable ();
1394 return ret;
1397 /* Propagate values through an ancestor jump function JFUNC associated with
1398 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1399 is the index of the source parameter. */
1401 static bool
1402 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1403 struct ipa_jump_func *jfunc,
1404 ipcp_lattice<tree> *src_lat,
1405 ipcp_lattice<tree> *dest_lat,
1406 int src_idx)
1408 ipcp_value<tree> *src_val;
1409 bool ret = false;
1411 if (ipa_edge_within_scc (cs))
1412 return dest_lat->set_contains_variable ();
1414 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1416 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1418 if (t)
1419 ret |= dest_lat->add_value (t, cs, src_val, src_idx);
1420 else
1421 ret |= dest_lat->set_contains_variable ();
1424 return ret;
1427 /* Propagate scalar values across jump function JFUNC that is associated with
1428 edge CS and put the values into DEST_LAT. */
1430 static bool
1431 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1432 struct ipa_jump_func *jfunc,
1433 ipcp_lattice<tree> *dest_lat)
1435 if (dest_lat->bottom)
1436 return false;
1438 if (jfunc->type == IPA_JF_CONST)
1440 tree val = ipa_get_jf_constant (jfunc);
1441 return dest_lat->add_value (val, cs, NULL, 0);
1443 else if (jfunc->type == IPA_JF_PASS_THROUGH
1444 || jfunc->type == IPA_JF_ANCESTOR)
1446 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1447 ipcp_lattice<tree> *src_lat;
1448 int src_idx;
1449 bool ret;
1451 if (jfunc->type == IPA_JF_PASS_THROUGH)
1452 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1453 else
1454 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1456 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1457 if (src_lat->bottom)
1458 return dest_lat->set_contains_variable ();
1460 /* If we would need to clone the caller and cannot, do not propagate. */
1461 if (!ipcp_versionable_function_p (cs->caller)
1462 && (src_lat->contains_variable
1463 || (src_lat->values_count > 1)))
1464 return dest_lat->set_contains_variable ();
1466 if (jfunc->type == IPA_JF_PASS_THROUGH)
1467 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1468 dest_lat, src_idx);
1469 else
1470 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1471 src_idx);
1473 if (src_lat->contains_variable)
1474 ret |= dest_lat->set_contains_variable ();
1476 return ret;
1479 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1480 use it for indirect inlining), we should propagate them too. */
1481 return dest_lat->set_contains_variable ();
1484 /* Propagate scalar values across jump function JFUNC that is associated with
1485 edge CS and describes argument IDX and put the values into DEST_LAT. */
1487 static bool
1488 propagate_context_accross_jump_function (cgraph_edge *cs,
1489 ipa_jump_func *jfunc, int idx,
1490 ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
1492 ipa_edge_args *args = IPA_EDGE_REF (cs);
1493 if (dest_lat->bottom)
1494 return false;
1495 bool ret = false;
1496 bool added_sth = false;
1497 bool type_preserved = true;
1499 ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
1500 = ipa_get_ith_polymorhic_call_context (args, idx);
1502 if (edge_ctx_ptr)
1503 edge_ctx = *edge_ctx_ptr;
1505 if (jfunc->type == IPA_JF_PASS_THROUGH
1506 || jfunc->type == IPA_JF_ANCESTOR)
1508 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1509 int src_idx;
1510 ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
1512 /* TODO: Once we figure out how to propagate speculations, it will
1513 probably be a good idea to switch to speculation if type_preserved is
1514 not set instead of punting. */
1515 if (jfunc->type == IPA_JF_PASS_THROUGH)
1517 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1518 goto prop_fail;
1519 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1520 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1522 else
1524 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1525 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1528 src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
1529 /* If we would need to clone the caller and cannot, do not propagate. */
1530 if (!ipcp_versionable_function_p (cs->caller)
1531 && (src_lat->contains_variable
1532 || (src_lat->values_count > 1)))
1533 goto prop_fail;
1535 ipcp_value<ipa_polymorphic_call_context> *src_val;
1536 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1538 ipa_polymorphic_call_context cur = src_val->value;
1540 if (!type_preserved)
1541 cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1542 if (jfunc->type == IPA_JF_ANCESTOR)
1543 cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1544 /* TODO: In cases we know how the context is going to be used,
1545 we can improve the result by passing proper OTR_TYPE. */
1546 cur.combine_with (edge_ctx);
1547 if (!cur.useless_p ())
1549 if (src_lat->contains_variable
1550 && !edge_ctx.equal_to (cur))
1551 ret |= dest_lat->set_contains_variable ();
1552 ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
1553 added_sth = true;
1559 prop_fail:
1560 if (!added_sth)
1562 if (!edge_ctx.useless_p ())
1563 ret |= dest_lat->add_value (edge_ctx, cs);
1564 else
1565 ret |= dest_lat->set_contains_variable ();
1568 return ret;
1571 /* Propagate alignments across jump function JFUNC that is associated with
1572 edge CS and update DEST_LAT accordingly. */
1574 static bool
1575 propagate_alignment_accross_jump_function (cgraph_edge *cs,
1576 ipa_jump_func *jfunc,
1577 ipcp_alignment_lattice *dest_lat)
1579 if (dest_lat->bottom_p ())
1580 return false;
1582 if (jfunc->type == IPA_JF_PASS_THROUGH
1583 || jfunc->type == IPA_JF_ANCESTOR)
1585 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1586 HOST_WIDE_INT offset = 0;
1587 int src_idx;
1589 if (jfunc->type == IPA_JF_PASS_THROUGH)
1591 enum tree_code op = ipa_get_jf_pass_through_operation (jfunc);
1592 if (op != NOP_EXPR)
1594 if (op != POINTER_PLUS_EXPR
1595 && op != PLUS_EXPR)
1596 return dest_lat->set_to_bottom ();
1597 tree operand = ipa_get_jf_pass_through_operand (jfunc);
1598 if (!tree_fits_shwi_p (operand))
1599 return dest_lat->set_to_bottom ();
1600 offset = tree_to_shwi (operand);
1602 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1604 else
1606 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1607 offset = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
1610 struct ipcp_param_lattices *src_lats;
1611 src_lats = ipa_get_parm_lattices (caller_info, src_idx);
1612 return dest_lat->meet_with (src_lats->alignment, offset);
1614 else
1616 if (jfunc->alignment.known)
1617 return dest_lat->meet_with (jfunc->alignment.align,
1618 jfunc->alignment.misalign);
1619 else
1620 return dest_lat->set_to_bottom ();
1624 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1625 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1626 other cases, return false). If there are no aggregate items, set
1627 aggs_by_ref to NEW_AGGS_BY_REF. */
1629 static bool
1630 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1631 bool new_aggs_by_ref)
1633 if (dest_plats->aggs)
1635 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1637 set_agg_lats_to_bottom (dest_plats);
1638 return true;
1641 else
1642 dest_plats->aggs_by_ref = new_aggs_by_ref;
1643 return false;
1646 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1647 already existing lattice for the given OFFSET and SIZE, marking all skipped
1648 lattices as containing variable and checking for overlaps. If there is no
1649 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1650 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1651 unless there are too many already. If there are two many, return false. If
1652 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1653 skipped lattices were newly marked as containing variable, set *CHANGE to
1654 true. */
1656 static bool
1657 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1658 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1659 struct ipcp_agg_lattice ***aglat,
1660 bool pre_existing, bool *change)
1662 gcc_checking_assert (offset >= 0);
1664 while (**aglat && (**aglat)->offset < offset)
1666 if ((**aglat)->offset + (**aglat)->size > offset)
1668 set_agg_lats_to_bottom (dest_plats);
1669 return false;
1671 *change |= (**aglat)->set_contains_variable ();
1672 *aglat = &(**aglat)->next;
1675 if (**aglat && (**aglat)->offset == offset)
1677 if ((**aglat)->size != val_size
1678 || ((**aglat)->next
1679 && (**aglat)->next->offset < offset + val_size))
1681 set_agg_lats_to_bottom (dest_plats);
1682 return false;
1684 gcc_checking_assert (!(**aglat)->next
1685 || (**aglat)->next->offset >= offset + val_size);
1686 return true;
1688 else
1690 struct ipcp_agg_lattice *new_al;
1692 if (**aglat && (**aglat)->offset < offset + val_size)
1694 set_agg_lats_to_bottom (dest_plats);
1695 return false;
1697 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1698 return false;
1699 dest_plats->aggs_count++;
1700 new_al = ipcp_agg_lattice_pool.allocate ();
1701 memset (new_al, 0, sizeof (*new_al));
1703 new_al->offset = offset;
1704 new_al->size = val_size;
1705 new_al->contains_variable = pre_existing;
1707 new_al->next = **aglat;
1708 **aglat = new_al;
1709 return true;
1713 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1714 containing an unknown value. */
1716 static bool
1717 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1719 bool ret = false;
1720 while (aglat)
1722 ret |= aglat->set_contains_variable ();
1723 aglat = aglat->next;
1725 return ret;
1728 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1729 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1730 parameter used for lattice value sources. Return true if DEST_PLATS changed
1731 in any way. */
1733 static bool
1734 merge_aggregate_lattices (struct cgraph_edge *cs,
1735 struct ipcp_param_lattices *dest_plats,
1736 struct ipcp_param_lattices *src_plats,
1737 int src_idx, HOST_WIDE_INT offset_delta)
1739 bool pre_existing = dest_plats->aggs != NULL;
1740 struct ipcp_agg_lattice **dst_aglat;
1741 bool ret = false;
1743 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1744 return true;
1745 if (src_plats->aggs_bottom)
1746 return set_agg_lats_contain_variable (dest_plats);
1747 if (src_plats->aggs_contain_variable)
1748 ret |= set_agg_lats_contain_variable (dest_plats);
1749 dst_aglat = &dest_plats->aggs;
1751 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1752 src_aglat;
1753 src_aglat = src_aglat->next)
1755 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1757 if (new_offset < 0)
1758 continue;
1759 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1760 &dst_aglat, pre_existing, &ret))
1762 struct ipcp_agg_lattice *new_al = *dst_aglat;
1764 dst_aglat = &(*dst_aglat)->next;
1765 if (src_aglat->bottom)
1767 ret |= new_al->set_contains_variable ();
1768 continue;
1770 if (src_aglat->contains_variable)
1771 ret |= new_al->set_contains_variable ();
1772 for (ipcp_value<tree> *val = src_aglat->values;
1773 val;
1774 val = val->next)
1775 ret |= new_al->add_value (val->value, cs, val, src_idx,
1776 src_aglat->offset);
1778 else if (dest_plats->aggs_bottom)
1779 return true;
1781 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1782 return ret;
1785 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1786 pass-through JFUNC and if so, whether it has conform and conforms to the
1787 rules about propagating values passed by reference. */
1789 static bool
1790 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1791 struct ipa_jump_func *jfunc)
1793 return src_plats->aggs
1794 && (!src_plats->aggs_by_ref
1795 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1798 /* Propagate scalar values across jump function JFUNC that is associated with
1799 edge CS and put the values into DEST_LAT. */
1801 static bool
1802 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1803 struct ipa_jump_func *jfunc,
1804 struct ipcp_param_lattices *dest_plats)
1806 bool ret = false;
1808 if (dest_plats->aggs_bottom)
1809 return false;
1811 if (jfunc->type == IPA_JF_PASS_THROUGH
1812 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1814 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1815 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1816 struct ipcp_param_lattices *src_plats;
1818 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1819 if (agg_pass_through_permissible_p (src_plats, jfunc))
1821 /* Currently we do not produce clobber aggregate jump
1822 functions, replace with merging when we do. */
1823 gcc_assert (!jfunc->agg.items);
1824 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1825 src_idx, 0);
1827 else
1828 ret |= set_agg_lats_contain_variable (dest_plats);
1830 else if (jfunc->type == IPA_JF_ANCESTOR
1831 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1833 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1834 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1835 struct ipcp_param_lattices *src_plats;
1837 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1838 if (src_plats->aggs && src_plats->aggs_by_ref)
1840 /* Currently we do not produce clobber aggregate jump
1841 functions, replace with merging when we do. */
1842 gcc_assert (!jfunc->agg.items);
1843 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1844 ipa_get_jf_ancestor_offset (jfunc));
1846 else if (!src_plats->aggs_by_ref)
1847 ret |= set_agg_lats_to_bottom (dest_plats);
1848 else
1849 ret |= set_agg_lats_contain_variable (dest_plats);
1851 else if (jfunc->agg.items)
1853 bool pre_existing = dest_plats->aggs != NULL;
1854 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1855 struct ipa_agg_jf_item *item;
1856 int i;
1858 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1859 return true;
1861 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1863 HOST_WIDE_INT val_size;
1865 if (item->offset < 0)
1866 continue;
1867 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1868 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1870 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1871 &aglat, pre_existing, &ret))
1873 ret |= (*aglat)->add_value (item->value, cs, NULL, 0, 0);
1874 aglat = &(*aglat)->next;
1876 else if (dest_plats->aggs_bottom)
1877 return true;
1880 ret |= set_chain_of_aglats_contains_variable (*aglat);
1882 else
1883 ret |= set_agg_lats_contain_variable (dest_plats);
1885 return ret;
1888 /* Return true if on the way cfrom CS->caller to the final (non-alias and
1889 non-thunk) destination, the call passes through a thunk. */
1891 static bool
1892 call_passes_through_thunk_p (cgraph_edge *cs)
1894 cgraph_node *alias_or_thunk = cs->callee;
1895 while (alias_or_thunk->alias)
1896 alias_or_thunk = alias_or_thunk->get_alias_target ();
1897 return alias_or_thunk->thunk.thunk_p;
1900 /* Propagate constants from the caller to the callee of CS. INFO describes the
1901 caller. */
1903 static bool
1904 propagate_constants_accross_call (struct cgraph_edge *cs)
1906 struct ipa_node_params *callee_info;
1907 enum availability availability;
1908 cgraph_node *callee;
1909 struct ipa_edge_args *args;
1910 bool ret = false;
1911 int i, args_count, parms_count;
1913 callee = cs->callee->function_symbol (&availability);
1914 if (!callee->definition)
1915 return false;
1916 gcc_checking_assert (callee->has_gimple_body_p ());
1917 callee_info = IPA_NODE_REF (callee);
1919 args = IPA_EDGE_REF (cs);
1920 args_count = ipa_get_cs_argument_count (args);
1921 parms_count = ipa_get_param_count (callee_info);
1922 if (parms_count == 0)
1923 return false;
1925 /* No propagation through instrumentation thunks is available yet.
1926 It should be possible with proper mapping of call args and
1927 instrumented callee params in the propagation loop below. But
1928 this case mostly occurs when legacy code calls instrumented code
1929 and it is not a primary target for optimizations.
1930 We detect instrumentation thunks in aliases and thunks chain by
1931 checking instrumentation_clone flag for chain source and target.
1932 Going through instrumentation thunks we always have it changed
1933 from 0 to 1 and all other nodes do not change it. */
1934 if (!cs->callee->instrumentation_clone
1935 && callee->instrumentation_clone)
1937 for (i = 0; i < parms_count; i++)
1938 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1939 i));
1940 return ret;
1943 /* If this call goes through a thunk we must not propagate to the first (0th)
1944 parameter. However, we might need to uncover a thunk from below a series
1945 of aliases first. */
1946 if (call_passes_through_thunk_p (cs))
1948 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1949 0));
1950 i = 1;
1952 else
1953 i = 0;
1955 for (; (i < args_count) && (i < parms_count); i++)
1957 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1958 struct ipcp_param_lattices *dest_plats;
1960 dest_plats = ipa_get_parm_lattices (callee_info, i);
1961 if (availability == AVAIL_INTERPOSABLE)
1962 ret |= set_all_contains_variable (dest_plats);
1963 else
1965 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1966 &dest_plats->itself);
1967 ret |= propagate_context_accross_jump_function (cs, jump_func, i,
1968 &dest_plats->ctxlat);
1969 ret |= propagate_alignment_accross_jump_function (cs, jump_func,
1970 &dest_plats->alignment);
1971 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1972 dest_plats);
1975 for (; i < parms_count; i++)
1976 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1978 return ret;
1981 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1982 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
1983 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
1985 static tree
1986 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1987 vec<tree> known_csts,
1988 vec<ipa_polymorphic_call_context> known_contexts,
1989 vec<ipa_agg_jump_function_p> known_aggs,
1990 struct ipa_agg_replacement_value *agg_reps,
1991 bool *speculative)
1993 int param_index = ie->indirect_info->param_index;
1994 HOST_WIDE_INT anc_offset;
1995 tree t;
1996 tree target = NULL;
1998 *speculative = false;
2000 if (param_index == -1
2001 || known_csts.length () <= (unsigned int) param_index)
2002 return NULL_TREE;
2004 if (!ie->indirect_info->polymorphic)
2006 tree t;
2008 if (ie->indirect_info->agg_contents)
2010 t = NULL;
2011 if (agg_reps && ie->indirect_info->guaranteed_unmodified)
2013 while (agg_reps)
2015 if (agg_reps->index == param_index
2016 && agg_reps->offset == ie->indirect_info->offset
2017 && agg_reps->by_ref == ie->indirect_info->by_ref)
2019 t = agg_reps->value;
2020 break;
2022 agg_reps = agg_reps->next;
2025 if (!t)
2027 struct ipa_agg_jump_function *agg;
2028 if (known_aggs.length () > (unsigned int) param_index)
2029 agg = known_aggs[param_index];
2030 else
2031 agg = NULL;
2032 bool from_global_constant;
2033 t = ipa_find_agg_cst_for_param (agg, known_csts[param_index],
2034 ie->indirect_info->offset,
2035 ie->indirect_info->by_ref,
2036 &from_global_constant);
2037 if (t
2038 && !from_global_constant
2039 && !ie->indirect_info->guaranteed_unmodified)
2040 t = NULL_TREE;
2043 else
2044 t = known_csts[param_index];
2046 if (t &&
2047 TREE_CODE (t) == ADDR_EXPR
2048 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
2049 return TREE_OPERAND (t, 0);
2050 else
2051 return NULL_TREE;
2054 if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
2055 return NULL_TREE;
2057 gcc_assert (!ie->indirect_info->agg_contents);
2058 anc_offset = ie->indirect_info->offset;
2060 t = NULL;
2062 /* Try to work out value of virtual table pointer value in replacemnets. */
2063 if (!t && agg_reps && !ie->indirect_info->by_ref)
2065 while (agg_reps)
2067 if (agg_reps->index == param_index
2068 && agg_reps->offset == ie->indirect_info->offset
2069 && agg_reps->by_ref)
2071 t = agg_reps->value;
2072 break;
2074 agg_reps = agg_reps->next;
2078 /* Try to work out value of virtual table pointer value in known
2079 aggregate values. */
2080 if (!t && known_aggs.length () > (unsigned int) param_index
2081 && !ie->indirect_info->by_ref)
2083 struct ipa_agg_jump_function *agg;
2084 agg = known_aggs[param_index];
2085 t = ipa_find_agg_cst_for_param (agg, known_csts[param_index],
2086 ie->indirect_info->offset,
2087 true);
2090 /* If we found the virtual table pointer, lookup the target. */
2091 if (t)
2093 tree vtable;
2094 unsigned HOST_WIDE_INT offset;
2095 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
2097 bool can_refer;
2098 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
2099 vtable, offset, &can_refer);
2100 if (can_refer)
2102 if (!target
2103 || (TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
2104 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
2105 || !possible_polymorphic_call_target_p
2106 (ie, cgraph_node::get (target)))
2108 /* Do not speculate builtin_unreachable, it is stupid! */
2109 if (ie->indirect_info->vptr_changed)
2110 return NULL;
2111 target = ipa_impossible_devirt_target (ie, target);
2113 *speculative = ie->indirect_info->vptr_changed;
2114 if (!*speculative)
2115 return target;
2120 /* Do we know the constant value of pointer? */
2121 if (!t)
2122 t = known_csts[param_index];
2124 gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
2126 ipa_polymorphic_call_context context;
2127 if (known_contexts.length () > (unsigned int) param_index)
2129 context = known_contexts[param_index];
2130 context.offset_by (anc_offset);
2131 if (ie->indirect_info->vptr_changed)
2132 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2133 ie->indirect_info->otr_type);
2134 if (t)
2136 ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
2137 (t, ie->indirect_info->otr_type, anc_offset);
2138 if (!ctx2.useless_p ())
2139 context.combine_with (ctx2, ie->indirect_info->otr_type);
2142 else if (t)
2144 context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
2145 anc_offset);
2146 if (ie->indirect_info->vptr_changed)
2147 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2148 ie->indirect_info->otr_type);
2150 else
2151 return NULL_TREE;
2153 vec <cgraph_node *>targets;
2154 bool final;
2156 targets = possible_polymorphic_call_targets
2157 (ie->indirect_info->otr_type,
2158 ie->indirect_info->otr_token,
2159 context, &final);
2160 if (!final || targets.length () > 1)
2162 struct cgraph_node *node;
2163 if (*speculative)
2164 return target;
2165 if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
2166 || ie->speculative || !ie->maybe_hot_p ())
2167 return NULL;
2168 node = try_speculative_devirtualization (ie->indirect_info->otr_type,
2169 ie->indirect_info->otr_token,
2170 context);
2171 if (node)
2173 *speculative = true;
2174 target = node->decl;
2176 else
2177 return NULL;
2179 else
2181 *speculative = false;
2182 if (targets.length () == 1)
2183 target = targets[0]->decl;
2184 else
2185 target = ipa_impossible_devirt_target (ie, NULL_TREE);
2188 if (target && !possible_polymorphic_call_target_p (ie,
2189 cgraph_node::get (target)))
2191 if (*speculative)
2192 return NULL;
2193 target = ipa_impossible_devirt_target (ie, target);
2196 return target;
2200 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2201 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2202 return the destination. */
2204 tree
2205 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
2206 vec<tree> known_csts,
2207 vec<ipa_polymorphic_call_context> known_contexts,
2208 vec<ipa_agg_jump_function_p> known_aggs,
2209 bool *speculative)
2211 return ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2212 known_aggs, NULL, speculative);
2215 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2216 and KNOWN_CONTEXTS. */
2218 static int
2219 devirtualization_time_bonus (struct cgraph_node *node,
2220 vec<tree> known_csts,
2221 vec<ipa_polymorphic_call_context> known_contexts,
2222 vec<ipa_agg_jump_function_p> known_aggs)
2224 struct cgraph_edge *ie;
2225 int res = 0;
2227 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
2229 struct cgraph_node *callee;
2230 struct inline_summary *isummary;
2231 enum availability avail;
2232 tree target;
2233 bool speculative;
2235 target = ipa_get_indirect_edge_target (ie, known_csts, known_contexts,
2236 known_aggs, &speculative);
2237 if (!target)
2238 continue;
2240 /* Only bare minimum benefit for clearly un-inlineable targets. */
2241 res += 1;
2242 callee = cgraph_node::get (target);
2243 if (!callee || !callee->definition)
2244 continue;
2245 callee = callee->function_symbol (&avail);
2246 if (avail < AVAIL_AVAILABLE)
2247 continue;
2248 isummary = inline_summaries->get (callee);
2249 if (!isummary->inlinable)
2250 continue;
2252 /* FIXME: The values below need re-considering and perhaps also
2253 integrating into the cost metrics, at lest in some very basic way. */
2254 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
2255 res += 31 / ((int)speculative + 1);
2256 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
2257 res += 15 / ((int)speculative + 1);
2258 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
2259 || DECL_DECLARED_INLINE_P (callee->decl))
2260 res += 7 / ((int)speculative + 1);
2263 return res;
2266 /* Return time bonus incurred because of HINTS. */
2268 static int
2269 hint_time_bonus (inline_hints hints)
2271 int result = 0;
2272 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
2273 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
2274 if (hints & INLINE_HINT_array_index)
2275 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
2276 return result;
2279 /* If there is a reason to penalize the function described by INFO in the
2280 cloning goodness evaluation, do so. */
2282 static inline int64_t
2283 incorporate_penalties (ipa_node_params *info, int64_t evaluation)
2285 if (info->node_within_scc)
2286 evaluation = (evaluation
2287 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY))) / 100;
2289 if (info->node_calling_single_call)
2290 evaluation = (evaluation
2291 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY)))
2292 / 100;
2294 return evaluation;
2297 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2298 and SIZE_COST and with the sum of frequencies of incoming edges to the
2299 potential new clone in FREQUENCIES. */
2301 static bool
2302 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
2303 int freq_sum, gcov_type count_sum, int size_cost)
2305 if (time_benefit == 0
2306 || !opt_for_fn (node->decl, flag_ipa_cp_clone)
2307 || node->optimize_for_size_p ())
2308 return false;
2310 gcc_assert (size_cost > 0);
2312 struct ipa_node_params *info = IPA_NODE_REF (node);
2313 if (max_count)
2315 int factor = (count_sum * 1000) / max_count;
2316 int64_t evaluation = (((int64_t) time_benefit * factor)
2317 / size_cost);
2318 evaluation = incorporate_penalties (info, evaluation);
2320 if (dump_file && (dump_flags & TDF_DETAILS))
2321 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2322 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
2323 "%s%s) -> evaluation: " "%" PRId64
2324 ", threshold: %i\n",
2325 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
2326 info->node_within_scc ? ", scc" : "",
2327 info->node_calling_single_call ? ", single_call" : "",
2328 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2330 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2332 else
2334 int64_t evaluation = (((int64_t) time_benefit * freq_sum)
2335 / size_cost);
2336 evaluation = incorporate_penalties (info, evaluation);
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2340 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2341 "%" PRId64 ", threshold: %i\n",
2342 time_benefit, size_cost, freq_sum,
2343 info->node_within_scc ? ", scc" : "",
2344 info->node_calling_single_call ? ", single_call" : "",
2345 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2347 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2351 /* Return all context independent values from aggregate lattices in PLATS in a
2352 vector. Return NULL if there are none. */
2354 static vec<ipa_agg_jf_item, va_gc> *
2355 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
2357 vec<ipa_agg_jf_item, va_gc> *res = NULL;
2359 if (plats->aggs_bottom
2360 || plats->aggs_contain_variable
2361 || plats->aggs_count == 0)
2362 return NULL;
2364 for (struct ipcp_agg_lattice *aglat = plats->aggs;
2365 aglat;
2366 aglat = aglat->next)
2367 if (aglat->is_single_const ())
2369 struct ipa_agg_jf_item item;
2370 item.offset = aglat->offset;
2371 item.value = aglat->values->value;
2372 vec_safe_push (res, item);
2374 return res;
2377 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2378 populate them with values of parameters that are known independent of the
2379 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2380 non-NULL, the movement cost of all removable parameters will be stored in
2381 it. */
2383 static bool
2384 gather_context_independent_values (struct ipa_node_params *info,
2385 vec<tree> *known_csts,
2386 vec<ipa_polymorphic_call_context>
2387 *known_contexts,
2388 vec<ipa_agg_jump_function> *known_aggs,
2389 int *removable_params_cost)
2391 int i, count = ipa_get_param_count (info);
2392 bool ret = false;
2394 known_csts->create (0);
2395 known_contexts->create (0);
2396 known_csts->safe_grow_cleared (count);
2397 known_contexts->safe_grow_cleared (count);
2398 if (known_aggs)
2400 known_aggs->create (0);
2401 known_aggs->safe_grow_cleared (count);
2404 if (removable_params_cost)
2405 *removable_params_cost = 0;
2407 for (i = 0; i < count ; i++)
2409 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2410 ipcp_lattice<tree> *lat = &plats->itself;
2412 if (lat->is_single_const ())
2414 ipcp_value<tree> *val = lat->values;
2415 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2416 (*known_csts)[i] = val->value;
2417 if (removable_params_cost)
2418 *removable_params_cost
2419 += estimate_move_cost (TREE_TYPE (val->value), false);
2420 ret = true;
2422 else if (removable_params_cost
2423 && !ipa_is_param_used (info, i))
2424 *removable_params_cost
2425 += ipa_get_param_move_cost (info, i);
2427 if (!ipa_is_param_used (info, i))
2428 continue;
2430 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2431 /* Do not account known context as reason for cloning. We can see
2432 if it permits devirtualization. */
2433 if (ctxlat->is_single_const ())
2434 (*known_contexts)[i] = ctxlat->values->value;
2436 if (known_aggs)
2438 vec<ipa_agg_jf_item, va_gc> *agg_items;
2439 struct ipa_agg_jump_function *ajf;
2441 agg_items = context_independent_aggregate_values (plats);
2442 ajf = &(*known_aggs)[i];
2443 ajf->items = agg_items;
2444 ajf->by_ref = plats->aggs_by_ref;
2445 ret |= agg_items != NULL;
2449 return ret;
2452 /* The current interface in ipa-inline-analysis requires a pointer vector.
2453 Create it.
2455 FIXME: That interface should be re-worked, this is slightly silly. Still,
2456 I'd like to discuss how to change it first and this demonstrates the
2457 issue. */
2459 static vec<ipa_agg_jump_function_p>
2460 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
2462 vec<ipa_agg_jump_function_p> ret;
2463 struct ipa_agg_jump_function *ajf;
2464 int i;
2466 ret.create (known_aggs.length ());
2467 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
2468 ret.quick_push (ajf);
2469 return ret;
2472 /* Perform time and size measurement of NODE with the context given in
2473 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2474 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2475 all context-independent removable parameters and EST_MOVE_COST of estimated
2476 movement of the considered parameter and store it into VAL. */
2478 static void
2479 perform_estimation_of_a_value (cgraph_node *node, vec<tree> known_csts,
2480 vec<ipa_polymorphic_call_context> known_contexts,
2481 vec<ipa_agg_jump_function_p> known_aggs_ptrs,
2482 int base_time, int removable_params_cost,
2483 int est_move_cost, ipcp_value_base *val)
2485 int time, size, time_benefit;
2486 inline_hints hints;
2488 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2489 known_aggs_ptrs, &size, &time,
2490 &hints);
2491 time_benefit = base_time - time
2492 + devirtualization_time_bonus (node, known_csts, known_contexts,
2493 known_aggs_ptrs)
2494 + hint_time_bonus (hints)
2495 + removable_params_cost + est_move_cost;
2497 gcc_checking_assert (size >=0);
2498 /* The inliner-heuristics based estimates may think that in certain
2499 contexts some functions do not have any size at all but we want
2500 all specializations to have at least a tiny cost, not least not to
2501 divide by zero. */
2502 if (size == 0)
2503 size = 1;
2505 val->local_time_benefit = time_benefit;
2506 val->local_size_cost = size;
2509 /* Iterate over known values of parameters of NODE and estimate the local
2510 effects in terms of time and size they have. */
2512 static void
2513 estimate_local_effects (struct cgraph_node *node)
2515 struct ipa_node_params *info = IPA_NODE_REF (node);
2516 int i, count = ipa_get_param_count (info);
2517 vec<tree> known_csts;
2518 vec<ipa_polymorphic_call_context> known_contexts;
2519 vec<ipa_agg_jump_function> known_aggs;
2520 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
2521 bool always_const;
2522 int base_time = inline_summaries->get (node)->time;
2523 int removable_params_cost;
2525 if (!count || !ipcp_versionable_function_p (node))
2526 return;
2528 if (dump_file && (dump_flags & TDF_DETAILS))
2529 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
2530 node->name (), node->order, base_time);
2532 always_const = gather_context_independent_values (info, &known_csts,
2533 &known_contexts, &known_aggs,
2534 &removable_params_cost);
2535 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
2536 int devirt_bonus = devirtualization_time_bonus (node, known_csts,
2537 known_contexts, known_aggs_ptrs);
2538 if (always_const || devirt_bonus
2539 || (removable_params_cost && node->local.can_change_signature))
2541 struct caller_statistics stats;
2542 inline_hints hints;
2543 int time, size;
2545 init_caller_stats (&stats);
2546 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2547 false);
2548 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2549 known_aggs_ptrs, &size, &time, &hints);
2550 time -= devirt_bonus;
2551 time -= hint_time_bonus (hints);
2552 time -= removable_params_cost;
2553 size -= stats.n_calls * removable_params_cost;
2555 if (dump_file)
2556 fprintf (dump_file, " - context independent values, size: %i, "
2557 "time_benefit: %i\n", size, base_time - time);
2559 if (size <= 0 || node->local.local)
2561 info->do_clone_for_all_contexts = true;
2562 base_time = time;
2564 if (dump_file)
2565 fprintf (dump_file, " Decided to specialize for all "
2566 "known contexts, code not going to grow.\n");
2568 else if (good_cloning_opportunity_p (node, base_time - time,
2569 stats.freq_sum, stats.count_sum,
2570 size))
2572 if (size + overall_size <= max_new_size)
2574 info->do_clone_for_all_contexts = true;
2575 base_time = time;
2576 overall_size += size;
2578 if (dump_file)
2579 fprintf (dump_file, " Decided to specialize for all "
2580 "known contexts, growth deemed beneficial.\n");
2582 else if (dump_file && (dump_flags & TDF_DETAILS))
2583 fprintf (dump_file, " Not cloning for all contexts because "
2584 "max_new_size would be reached with %li.\n",
2585 size + overall_size);
2587 else if (dump_file && (dump_flags & TDF_DETAILS))
2588 fprintf (dump_file, " Not cloning for all contexts because "
2589 "!good_cloning_opportunity_p.\n");
2593 for (i = 0; i < count ; i++)
2595 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2596 ipcp_lattice<tree> *lat = &plats->itself;
2597 ipcp_value<tree> *val;
2599 if (lat->bottom
2600 || !lat->values
2601 || known_csts[i])
2602 continue;
2604 for (val = lat->values; val; val = val->next)
2606 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2607 known_csts[i] = val->value;
2609 int emc = estimate_move_cost (TREE_TYPE (val->value), true);
2610 perform_estimation_of_a_value (node, known_csts, known_contexts,
2611 known_aggs_ptrs, base_time,
2612 removable_params_cost, emc, val);
2614 if (dump_file && (dump_flags & TDF_DETAILS))
2616 fprintf (dump_file, " - estimates for value ");
2617 print_ipcp_constant_value (dump_file, val->value);
2618 fprintf (dump_file, " for ");
2619 ipa_dump_param (dump_file, info, i);
2620 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2621 val->local_time_benefit, val->local_size_cost);
2624 known_csts[i] = NULL_TREE;
2627 for (i = 0; i < count; i++)
2629 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2631 if (!plats->virt_call)
2632 continue;
2634 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2635 ipcp_value<ipa_polymorphic_call_context> *val;
2637 if (ctxlat->bottom
2638 || !ctxlat->values
2639 || !known_contexts[i].useless_p ())
2640 continue;
2642 for (val = ctxlat->values; val; val = val->next)
2644 known_contexts[i] = val->value;
2645 perform_estimation_of_a_value (node, known_csts, known_contexts,
2646 known_aggs_ptrs, base_time,
2647 removable_params_cost, 0, val);
2649 if (dump_file && (dump_flags & TDF_DETAILS))
2651 fprintf (dump_file, " - estimates for polymorphic context ");
2652 print_ipcp_constant_value (dump_file, val->value);
2653 fprintf (dump_file, " for ");
2654 ipa_dump_param (dump_file, info, i);
2655 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2656 val->local_time_benefit, val->local_size_cost);
2659 known_contexts[i] = ipa_polymorphic_call_context ();
2662 for (i = 0; i < count ; i++)
2664 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2665 struct ipa_agg_jump_function *ajf;
2666 struct ipcp_agg_lattice *aglat;
2668 if (plats->aggs_bottom || !plats->aggs)
2669 continue;
2671 ajf = &known_aggs[i];
2672 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2674 ipcp_value<tree> *val;
2675 if (aglat->bottom || !aglat->values
2676 /* If the following is true, the one value is in known_aggs. */
2677 || (!plats->aggs_contain_variable
2678 && aglat->is_single_const ()))
2679 continue;
2681 for (val = aglat->values; val; val = val->next)
2683 struct ipa_agg_jf_item item;
2685 item.offset = aglat->offset;
2686 item.value = val->value;
2687 vec_safe_push (ajf->items, item);
2689 perform_estimation_of_a_value (node, known_csts, known_contexts,
2690 known_aggs_ptrs, base_time,
2691 removable_params_cost, 0, val);
2693 if (dump_file && (dump_flags & TDF_DETAILS))
2695 fprintf (dump_file, " - estimates for value ");
2696 print_ipcp_constant_value (dump_file, val->value);
2697 fprintf (dump_file, " for ");
2698 ipa_dump_param (dump_file, info, i);
2699 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2700 "]: time_benefit: %i, size: %i\n",
2701 plats->aggs_by_ref ? "ref " : "",
2702 aglat->offset,
2703 val->local_time_benefit, val->local_size_cost);
2706 ajf->items->pop ();
2711 for (i = 0; i < count ; i++)
2712 vec_free (known_aggs[i].items);
2714 known_csts.release ();
2715 known_contexts.release ();
2716 known_aggs.release ();
2717 known_aggs_ptrs.release ();
2721 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2722 topological sort of values. */
2724 template <typename valtype>
2725 void
2726 value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
2728 ipcp_value_source<valtype> *src;
2730 if (cur_val->dfs)
2731 return;
2733 dfs_counter++;
2734 cur_val->dfs = dfs_counter;
2735 cur_val->low_link = dfs_counter;
2737 cur_val->topo_next = stack;
2738 stack = cur_val;
2739 cur_val->on_stack = true;
2741 for (src = cur_val->sources; src; src = src->next)
2742 if (src->val)
2744 if (src->val->dfs == 0)
2746 add_val (src->val);
2747 if (src->val->low_link < cur_val->low_link)
2748 cur_val->low_link = src->val->low_link;
2750 else if (src->val->on_stack
2751 && src->val->dfs < cur_val->low_link)
2752 cur_val->low_link = src->val->dfs;
2755 if (cur_val->dfs == cur_val->low_link)
2757 ipcp_value<valtype> *v, *scc_list = NULL;
2761 v = stack;
2762 stack = v->topo_next;
2763 v->on_stack = false;
2765 v->scc_next = scc_list;
2766 scc_list = v;
2768 while (v != cur_val);
2770 cur_val->topo_next = values_topo;
2771 values_topo = cur_val;
2775 /* Add all values in lattices associated with NODE to the topological sort if
2776 they are not there yet. */
2778 static void
2779 add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
2781 struct ipa_node_params *info = IPA_NODE_REF (node);
2782 int i, count = ipa_get_param_count (info);
2784 for (i = 0; i < count ; i++)
2786 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2787 ipcp_lattice<tree> *lat = &plats->itself;
2788 struct ipcp_agg_lattice *aglat;
2790 if (!lat->bottom)
2792 ipcp_value<tree> *val;
2793 for (val = lat->values; val; val = val->next)
2794 topo->constants.add_val (val);
2797 if (!plats->aggs_bottom)
2798 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2799 if (!aglat->bottom)
2801 ipcp_value<tree> *val;
2802 for (val = aglat->values; val; val = val->next)
2803 topo->constants.add_val (val);
2806 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2807 if (!ctxlat->bottom)
2809 ipcp_value<ipa_polymorphic_call_context> *ctxval;
2810 for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
2811 topo->contexts.add_val (ctxval);
2816 /* One pass of constants propagation along the call graph edges, from callers
2817 to callees (requires topological ordering in TOPO), iterate over strongly
2818 connected components. */
2820 static void
2821 propagate_constants_topo (struct ipa_topo_info *topo)
2823 int i;
2825 for (i = topo->nnodes - 1; i >= 0; i--)
2827 unsigned j;
2828 struct cgraph_node *v, *node = topo->order[i];
2829 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
2831 /* First, iteratively propagate within the strongly connected component
2832 until all lattices stabilize. */
2833 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2834 if (v->has_gimple_body_p ())
2835 push_node_to_stack (topo, v);
2837 v = pop_node_from_stack (topo);
2838 while (v)
2840 struct cgraph_edge *cs;
2842 for (cs = v->callees; cs; cs = cs->next_callee)
2843 if (ipa_edge_within_scc (cs))
2845 IPA_NODE_REF (v)->node_within_scc = true;
2846 if (propagate_constants_accross_call (cs))
2847 push_node_to_stack (topo, cs->callee->function_symbol ());
2849 v = pop_node_from_stack (topo);
2852 /* Afterwards, propagate along edges leading out of the SCC, calculates
2853 the local effects of the discovered constants and all valid values to
2854 their topological sort. */
2855 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2856 if (v->has_gimple_body_p ())
2858 struct cgraph_edge *cs;
2860 estimate_local_effects (v);
2861 add_all_node_vals_to_toposort (v, topo);
2862 for (cs = v->callees; cs; cs = cs->next_callee)
2863 if (!ipa_edge_within_scc (cs))
2864 propagate_constants_accross_call (cs);
2866 cycle_nodes.release ();
2871 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2872 the bigger one if otherwise. */
2874 static int
2875 safe_add (int a, int b)
2877 if (a > INT_MAX/2 || b > INT_MAX/2)
2878 return a > b ? a : b;
2879 else
2880 return a + b;
2884 /* Propagate the estimated effects of individual values along the topological
2885 from the dependent values to those they depend on. */
2887 template <typename valtype>
2888 void
2889 value_topo_info<valtype>::propagate_effects ()
2891 ipcp_value<valtype> *base;
2893 for (base = values_topo; base; base = base->topo_next)
2895 ipcp_value_source<valtype> *src;
2896 ipcp_value<valtype> *val;
2897 int time = 0, size = 0;
2899 for (val = base; val; val = val->scc_next)
2901 time = safe_add (time,
2902 val->local_time_benefit + val->prop_time_benefit);
2903 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2906 for (val = base; val; val = val->scc_next)
2907 for (src = val->sources; src; src = src->next)
2908 if (src->val
2909 && src->cs->maybe_hot_p ())
2911 src->val->prop_time_benefit = safe_add (time,
2912 src->val->prop_time_benefit);
2913 src->val->prop_size_cost = safe_add (size,
2914 src->val->prop_size_cost);
2920 /* Propagate constants, polymorphic contexts and their effects from the
2921 summaries interprocedurally. */
2923 static void
2924 ipcp_propagate_stage (struct ipa_topo_info *topo)
2926 struct cgraph_node *node;
2928 if (dump_file)
2929 fprintf (dump_file, "\n Propagating constants:\n\n");
2931 if (in_lto_p)
2932 ipa_update_after_lto_read ();
2935 FOR_EACH_DEFINED_FUNCTION (node)
2937 struct ipa_node_params *info = IPA_NODE_REF (node);
2939 determine_versionability (node, info);
2940 if (node->has_gimple_body_p ())
2942 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2943 ipa_get_param_count (info));
2944 initialize_node_lattices (node);
2946 if (node->definition && !node->alias)
2947 overall_size += inline_summaries->get (node)->self_size;
2948 if (node->count > max_count)
2949 max_count = node->count;
2952 max_new_size = overall_size;
2953 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2954 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2955 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2957 if (dump_file)
2958 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2959 overall_size, max_new_size);
2961 propagate_constants_topo (topo);
2962 if (flag_checking)
2963 ipcp_verify_propagated_values ();
2964 topo->constants.propagate_effects ();
2965 topo->contexts.propagate_effects ();
2967 if (dump_file)
2969 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2970 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2974 /* Discover newly direct outgoing edges from NODE which is a new clone with
2975 known KNOWN_CSTS and make them direct. */
2977 static void
2978 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2979 vec<tree> known_csts,
2980 vec<ipa_polymorphic_call_context>
2981 known_contexts,
2982 struct ipa_agg_replacement_value *aggvals)
2984 struct cgraph_edge *ie, *next_ie;
2985 bool found = false;
2987 for (ie = node->indirect_calls; ie; ie = next_ie)
2989 tree target;
2990 bool speculative;
2992 next_ie = ie->next_callee;
2993 target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2994 vNULL, aggvals, &speculative);
2995 if (target)
2997 bool agg_contents = ie->indirect_info->agg_contents;
2998 bool polymorphic = ie->indirect_info->polymorphic;
2999 int param_index = ie->indirect_info->param_index;
3000 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
3001 speculative);
3002 found = true;
3004 if (cs && !agg_contents && !polymorphic)
3006 struct ipa_node_params *info = IPA_NODE_REF (node);
3007 int c = ipa_get_controlled_uses (info, param_index);
3008 if (c != IPA_UNDESCRIBED_USE)
3010 struct ipa_ref *to_del;
3012 c--;
3013 ipa_set_controlled_uses (info, param_index, c);
3014 if (dump_file && (dump_flags & TDF_DETAILS))
3015 fprintf (dump_file, " controlled uses count of param "
3016 "%i bumped down to %i\n", param_index, c);
3017 if (c == 0
3018 && (to_del = node->find_reference (cs->callee, NULL, 0)))
3020 if (dump_file && (dump_flags & TDF_DETAILS))
3021 fprintf (dump_file, " and even removing its "
3022 "cloning-created reference\n");
3023 to_del->remove_reference ();
3029 /* Turning calls to direct calls will improve overall summary. */
3030 if (found)
3031 inline_update_overall_summary (node);
3034 /* Vector of pointers which for linked lists of clones of an original crgaph
3035 edge. */
3037 static vec<cgraph_edge *> next_edge_clone;
3038 static vec<cgraph_edge *> prev_edge_clone;
3040 static inline void
3041 grow_edge_clone_vectors (void)
3043 if (next_edge_clone.length ()
3044 <= (unsigned) symtab->edges_max_uid)
3045 next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
3046 if (prev_edge_clone.length ()
3047 <= (unsigned) symtab->edges_max_uid)
3048 prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
3051 /* Edge duplication hook to grow the appropriate linked list in
3052 next_edge_clone. */
3054 static void
3055 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3056 void *)
3058 grow_edge_clone_vectors ();
3060 struct cgraph_edge *old_next = next_edge_clone[src->uid];
3061 if (old_next)
3062 prev_edge_clone[old_next->uid] = dst;
3063 prev_edge_clone[dst->uid] = src;
3065 next_edge_clone[dst->uid] = old_next;
3066 next_edge_clone[src->uid] = dst;
3069 /* Hook that is called by cgraph.c when an edge is removed. */
3071 static void
3072 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
3074 grow_edge_clone_vectors ();
3076 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
3077 struct cgraph_edge *next = next_edge_clone[cs->uid];
3078 if (prev)
3079 next_edge_clone[prev->uid] = next;
3080 if (next)
3081 prev_edge_clone[next->uid] = prev;
3084 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
3085 parameter with the given INDEX. */
3087 static tree
3088 get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset,
3089 int index)
3091 struct ipa_agg_replacement_value *aggval;
3093 aggval = ipa_get_agg_replacements_for_node (node);
3094 while (aggval)
3096 if (aggval->offset == offset
3097 && aggval->index == index)
3098 return aggval->value;
3099 aggval = aggval->next;
3101 return NULL_TREE;
3104 /* Return true is NODE is DEST or its clone for all contexts. */
3106 static bool
3107 same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest)
3109 if (node == dest)
3110 return true;
3112 struct ipa_node_params *info = IPA_NODE_REF (node);
3113 return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
3116 /* Return true if edge CS does bring about the value described by SRC to node
3117 DEST or its clone for all contexts. */
3119 static bool
3120 cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
3121 cgraph_node *dest)
3123 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3124 enum availability availability;
3125 cgraph_node *real_dest = cs->callee->function_symbol (&availability);
3127 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3128 || availability <= AVAIL_INTERPOSABLE
3129 || caller_info->node_dead)
3130 return false;
3131 if (!src->val)
3132 return true;
3134 if (caller_info->ipcp_orig_node)
3136 tree t;
3137 if (src->offset == -1)
3138 t = caller_info->known_csts[src->index];
3139 else
3140 t = get_clone_agg_value (cs->caller, src->offset, src->index);
3141 return (t != NULL_TREE
3142 && values_equal_for_ipcp_p (src->val->value, t));
3144 else
3146 struct ipcp_agg_lattice *aglat;
3147 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3148 src->index);
3149 if (src->offset == -1)
3150 return (plats->itself.is_single_const ()
3151 && values_equal_for_ipcp_p (src->val->value,
3152 plats->itself.values->value));
3153 else
3155 if (plats->aggs_bottom || plats->aggs_contain_variable)
3156 return false;
3157 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3158 if (aglat->offset == src->offset)
3159 return (aglat->is_single_const ()
3160 && values_equal_for_ipcp_p (src->val->value,
3161 aglat->values->value));
3163 return false;
3167 /* Return true if edge CS does bring about the value described by SRC to node
3168 DEST or its clone for all contexts. */
3170 static bool
3171 cgraph_edge_brings_value_p (cgraph_edge *cs,
3172 ipcp_value_source<ipa_polymorphic_call_context> *src,
3173 cgraph_node *dest)
3175 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3176 cgraph_node *real_dest = cs->callee->function_symbol ();
3178 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3179 || caller_info->node_dead)
3180 return false;
3181 if (!src->val)
3182 return true;
3184 if (caller_info->ipcp_orig_node)
3185 return (caller_info->known_contexts.length () > (unsigned) src->index)
3186 && values_equal_for_ipcp_p (src->val->value,
3187 caller_info->known_contexts[src->index]);
3189 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3190 src->index);
3191 return plats->ctxlat.is_single_const ()
3192 && values_equal_for_ipcp_p (src->val->value,
3193 plats->ctxlat.values->value);
3196 /* Get the next clone in the linked list of clones of an edge. */
3198 static inline struct cgraph_edge *
3199 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
3201 return next_edge_clone[cs->uid];
3204 /* Given VAL that is intended for DEST, iterate over all its sources and if
3205 they still hold, add their edge frequency and their number into *FREQUENCY
3206 and *CALLER_COUNT respectively. */
3208 template <typename valtype>
3209 static bool
3210 get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
3211 int *freq_sum,
3212 gcov_type *count_sum, int *caller_count)
3214 ipcp_value_source<valtype> *src;
3215 int freq = 0, count = 0;
3216 gcov_type cnt = 0;
3217 bool hot = false;
3219 for (src = val->sources; src; src = src->next)
3221 struct cgraph_edge *cs = src->cs;
3222 while (cs)
3224 if (cgraph_edge_brings_value_p (cs, src, dest))
3226 count++;
3227 freq += cs->frequency;
3228 cnt += cs->count;
3229 hot |= cs->maybe_hot_p ();
3231 cs = get_next_cgraph_edge_clone (cs);
3235 *freq_sum = freq;
3236 *count_sum = cnt;
3237 *caller_count = count;
3238 return hot;
3241 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3242 is assumed their number is known and equal to CALLER_COUNT. */
3244 template <typename valtype>
3245 static vec<cgraph_edge *>
3246 gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
3247 int caller_count)
3249 ipcp_value_source<valtype> *src;
3250 vec<cgraph_edge *> ret;
3252 ret.create (caller_count);
3253 for (src = val->sources; src; src = src->next)
3255 struct cgraph_edge *cs = src->cs;
3256 while (cs)
3258 if (cgraph_edge_brings_value_p (cs, src, dest))
3259 ret.quick_push (cs);
3260 cs = get_next_cgraph_edge_clone (cs);
3264 return ret;
3267 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3268 Return it or NULL if for some reason it cannot be created. */
3270 static struct ipa_replace_map *
3271 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
3273 struct ipa_replace_map *replace_map;
3276 replace_map = ggc_alloc<ipa_replace_map> ();
3277 if (dump_file)
3279 fprintf (dump_file, " replacing ");
3280 ipa_dump_param (dump_file, info, parm_num);
3282 fprintf (dump_file, " with const ");
3283 print_generic_expr (dump_file, value, 0);
3284 fprintf (dump_file, "\n");
3286 replace_map->old_tree = NULL;
3287 replace_map->parm_num = parm_num;
3288 replace_map->new_tree = value;
3289 replace_map->replace_p = true;
3290 replace_map->ref_p = false;
3292 return replace_map;
3295 /* Dump new profiling counts */
3297 static void
3298 dump_profile_updates (struct cgraph_node *orig_node,
3299 struct cgraph_node *new_node)
3301 struct cgraph_edge *cs;
3303 fprintf (dump_file, " setting count of the specialized node to "
3304 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
3305 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3306 fprintf (dump_file, " edge to %s has count "
3307 HOST_WIDE_INT_PRINT_DEC "\n",
3308 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3310 fprintf (dump_file, " setting count of the original node to "
3311 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
3312 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3313 fprintf (dump_file, " edge to %s is left with "
3314 HOST_WIDE_INT_PRINT_DEC "\n",
3315 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3318 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3319 their profile information to reflect this. */
3321 static void
3322 update_profiling_info (struct cgraph_node *orig_node,
3323 struct cgraph_node *new_node)
3325 struct cgraph_edge *cs;
3326 struct caller_statistics stats;
3327 gcov_type new_sum, orig_sum;
3328 gcov_type remainder, orig_node_count = orig_node->count;
3330 if (orig_node_count == 0)
3331 return;
3333 init_caller_stats (&stats);
3334 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3335 false);
3336 orig_sum = stats.count_sum;
3337 init_caller_stats (&stats);
3338 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3339 false);
3340 new_sum = stats.count_sum;
3342 if (orig_node_count < orig_sum + new_sum)
3344 if (dump_file)
3345 fprintf (dump_file, " Problem: node %s/%i has too low count "
3346 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
3347 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
3348 orig_node->name (), orig_node->order,
3349 (HOST_WIDE_INT) orig_node_count,
3350 (HOST_WIDE_INT) (orig_sum + new_sum));
3352 orig_node_count = (orig_sum + new_sum) * 12 / 10;
3353 if (dump_file)
3354 fprintf (dump_file, " proceeding by pretending it was "
3355 HOST_WIDE_INT_PRINT_DEC "\n",
3356 (HOST_WIDE_INT) orig_node_count);
3359 new_node->count = new_sum;
3360 remainder = orig_node_count - new_sum;
3361 orig_node->count = remainder;
3363 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3364 if (cs->frequency)
3365 cs->count = apply_probability (cs->count,
3366 GCOV_COMPUTE_SCALE (new_sum,
3367 orig_node_count));
3368 else
3369 cs->count = 0;
3371 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3372 cs->count = apply_probability (cs->count,
3373 GCOV_COMPUTE_SCALE (remainder,
3374 orig_node_count));
3376 if (dump_file)
3377 dump_profile_updates (orig_node, new_node);
3380 /* Update the respective profile of specialized NEW_NODE and the original
3381 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3382 have been redirected to the specialized version. */
3384 static void
3385 update_specialized_profile (struct cgraph_node *new_node,
3386 struct cgraph_node *orig_node,
3387 gcov_type redirected_sum)
3389 struct cgraph_edge *cs;
3390 gcov_type new_node_count, orig_node_count = orig_node->count;
3392 if (dump_file)
3393 fprintf (dump_file, " the sum of counts of redirected edges is "
3394 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
3395 if (orig_node_count == 0)
3396 return;
3398 gcc_assert (orig_node_count >= redirected_sum);
3400 new_node_count = new_node->count;
3401 new_node->count += redirected_sum;
3402 orig_node->count -= redirected_sum;
3404 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3405 if (cs->frequency)
3406 cs->count += apply_probability (cs->count,
3407 GCOV_COMPUTE_SCALE (redirected_sum,
3408 new_node_count));
3409 else
3410 cs->count = 0;
3412 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3414 gcov_type dec = apply_probability (cs->count,
3415 GCOV_COMPUTE_SCALE (redirected_sum,
3416 orig_node_count));
3417 if (dec < cs->count)
3418 cs->count -= dec;
3419 else
3420 cs->count = 0;
3423 if (dump_file)
3424 dump_profile_updates (orig_node, new_node);
3427 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3428 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3429 redirect all edges in CALLERS to it. */
3431 static struct cgraph_node *
3432 create_specialized_node (struct cgraph_node *node,
3433 vec<tree> known_csts,
3434 vec<ipa_polymorphic_call_context> known_contexts,
3435 struct ipa_agg_replacement_value *aggvals,
3436 vec<cgraph_edge *> callers)
3438 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
3439 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
3440 struct ipa_agg_replacement_value *av;
3441 struct cgraph_node *new_node;
3442 int i, count = ipa_get_param_count (info);
3443 bitmap args_to_skip;
3445 gcc_assert (!info->ipcp_orig_node);
3447 if (node->local.can_change_signature)
3449 args_to_skip = BITMAP_GGC_ALLOC ();
3450 for (i = 0; i < count; i++)
3452 tree t = known_csts[i];
3454 if (t || !ipa_is_param_used (info, i))
3455 bitmap_set_bit (args_to_skip, i);
3458 else
3460 args_to_skip = NULL;
3461 if (dump_file && (dump_flags & TDF_DETAILS))
3462 fprintf (dump_file, " cannot change function signature\n");
3465 for (i = 0; i < count ; i++)
3467 tree t = known_csts[i];
3468 if (t)
3470 struct ipa_replace_map *replace_map;
3472 gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
3473 replace_map = get_replacement_map (info, t, i);
3474 if (replace_map)
3475 vec_safe_push (replace_trees, replace_map);
3479 new_node = node->create_virtual_clone (callers, replace_trees,
3480 args_to_skip, "constprop");
3481 ipa_set_node_agg_value_chain (new_node, aggvals);
3482 for (av = aggvals; av; av = av->next)
3483 new_node->maybe_create_reference (av->value, IPA_REF_ADDR, NULL);
3485 if (dump_file && (dump_flags & TDF_DETAILS))
3487 fprintf (dump_file, " the new node is %s/%i.\n",
3488 new_node->name (), new_node->order);
3489 if (known_contexts.exists ())
3491 for (i = 0; i < count ; i++)
3492 if (!known_contexts[i].useless_p ())
3494 fprintf (dump_file, " known ctx %i is ", i);
3495 known_contexts[i].dump (dump_file);
3498 if (aggvals)
3499 ipa_dump_agg_replacement_values (dump_file, aggvals);
3501 ipa_check_create_node_params ();
3502 update_profiling_info (node, new_node);
3503 new_info = IPA_NODE_REF (new_node);
3504 new_info->ipcp_orig_node = node;
3505 new_info->known_csts = known_csts;
3506 new_info->known_contexts = known_contexts;
3508 ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts, aggvals);
3510 callers.release ();
3511 return new_node;
3514 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3515 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3517 static void
3518 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
3519 vec<tree> known_csts,
3520 vec<cgraph_edge *> callers)
3522 struct ipa_node_params *info = IPA_NODE_REF (node);
3523 int i, count = ipa_get_param_count (info);
3525 for (i = 0; i < count ; i++)
3527 struct cgraph_edge *cs;
3528 tree newval = NULL_TREE;
3529 int j;
3530 bool first = true;
3532 if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
3533 continue;
3535 FOR_EACH_VEC_ELT (callers, j, cs)
3537 struct ipa_jump_func *jump_func;
3538 tree t;
3540 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
3541 || (i == 0
3542 && call_passes_through_thunk_p (cs))
3543 || (!cs->callee->instrumentation_clone
3544 && cs->callee->function_symbol ()->instrumentation_clone))
3546 newval = NULL_TREE;
3547 break;
3549 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
3550 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
3551 if (!t
3552 || (newval
3553 && !values_equal_for_ipcp_p (t, newval))
3554 || (!first && !newval))
3556 newval = NULL_TREE;
3557 break;
3559 else
3560 newval = t;
3561 first = false;
3564 if (newval)
3566 if (dump_file && (dump_flags & TDF_DETAILS))
3568 fprintf (dump_file, " adding an extra known scalar value ");
3569 print_ipcp_constant_value (dump_file, newval);
3570 fprintf (dump_file, " for ");
3571 ipa_dump_param (dump_file, info, i);
3572 fprintf (dump_file, "\n");
3575 known_csts[i] = newval;
3580 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3581 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3582 CALLERS. */
3584 static void
3585 find_more_contexts_for_caller_subset (cgraph_node *node,
3586 vec<ipa_polymorphic_call_context>
3587 *known_contexts,
3588 vec<cgraph_edge *> callers)
3590 ipa_node_params *info = IPA_NODE_REF (node);
3591 int i, count = ipa_get_param_count (info);
3593 for (i = 0; i < count ; i++)
3595 cgraph_edge *cs;
3597 if (ipa_get_poly_ctx_lat (info, i)->bottom
3598 || (known_contexts->exists ()
3599 && !(*known_contexts)[i].useless_p ()))
3600 continue;
3602 ipa_polymorphic_call_context newval;
3603 bool first = true;
3604 int j;
3606 FOR_EACH_VEC_ELT (callers, j, cs)
3608 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3609 return;
3610 ipa_jump_func *jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs),
3612 ipa_polymorphic_call_context ctx;
3613 ctx = ipa_context_from_jfunc (IPA_NODE_REF (cs->caller), cs, i,
3614 jfunc);
3615 if (first)
3617 newval = ctx;
3618 first = false;
3620 else
3621 newval.meet_with (ctx);
3622 if (newval.useless_p ())
3623 break;
3626 if (!newval.useless_p ())
3628 if (dump_file && (dump_flags & TDF_DETAILS))
3630 fprintf (dump_file, " adding an extra known polymorphic "
3631 "context ");
3632 print_ipcp_constant_value (dump_file, newval);
3633 fprintf (dump_file, " for ");
3634 ipa_dump_param (dump_file, info, i);
3635 fprintf (dump_file, "\n");
3638 if (!known_contexts->exists ())
3639 known_contexts->safe_grow_cleared (ipa_get_param_count (info));
3640 (*known_contexts)[i] = newval;
3646 /* Go through PLATS and create a vector of values consisting of values and
3647 offsets (minus OFFSET) of lattices that contain only a single value. */
3649 static vec<ipa_agg_jf_item>
3650 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
3652 vec<ipa_agg_jf_item> res = vNULL;
3654 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3655 return vNULL;
3657 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3658 if (aglat->is_single_const ())
3660 struct ipa_agg_jf_item ti;
3661 ti.offset = aglat->offset - offset;
3662 ti.value = aglat->values->value;
3663 res.safe_push (ti);
3665 return res;
3668 /* Intersect all values in INTER with single value lattices in PLATS (while
3669 subtracting OFFSET). */
3671 static void
3672 intersect_with_plats (struct ipcp_param_lattices *plats,
3673 vec<ipa_agg_jf_item> *inter,
3674 HOST_WIDE_INT offset)
3676 struct ipcp_agg_lattice *aglat;
3677 struct ipa_agg_jf_item *item;
3678 int k;
3680 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3682 inter->release ();
3683 return;
3686 aglat = plats->aggs;
3687 FOR_EACH_VEC_ELT (*inter, k, item)
3689 bool found = false;
3690 if (!item->value)
3691 continue;
3692 while (aglat)
3694 if (aglat->offset - offset > item->offset)
3695 break;
3696 if (aglat->offset - offset == item->offset)
3698 gcc_checking_assert (item->value);
3699 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
3700 found = true;
3701 break;
3703 aglat = aglat->next;
3705 if (!found)
3706 item->value = NULL_TREE;
3710 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
3711 vector result while subtracting OFFSET from the individual value offsets. */
3713 static vec<ipa_agg_jf_item>
3714 agg_replacements_to_vector (struct cgraph_node *node, int index,
3715 HOST_WIDE_INT offset)
3717 struct ipa_agg_replacement_value *av;
3718 vec<ipa_agg_jf_item> res = vNULL;
3720 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
3721 if (av->index == index
3722 && (av->offset - offset) >= 0)
3724 struct ipa_agg_jf_item item;
3725 gcc_checking_assert (av->value);
3726 item.offset = av->offset - offset;
3727 item.value = av->value;
3728 res.safe_push (item);
3731 return res;
3734 /* Intersect all values in INTER with those that we have already scheduled to
3735 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
3736 (while subtracting OFFSET). */
3738 static void
3739 intersect_with_agg_replacements (struct cgraph_node *node, int index,
3740 vec<ipa_agg_jf_item> *inter,
3741 HOST_WIDE_INT offset)
3743 struct ipa_agg_replacement_value *srcvals;
3744 struct ipa_agg_jf_item *item;
3745 int i;
3747 srcvals = ipa_get_agg_replacements_for_node (node);
3748 if (!srcvals)
3750 inter->release ();
3751 return;
3754 FOR_EACH_VEC_ELT (*inter, i, item)
3756 struct ipa_agg_replacement_value *av;
3757 bool found = false;
3758 if (!item->value)
3759 continue;
3760 for (av = srcvals; av; av = av->next)
3762 gcc_checking_assert (av->value);
3763 if (av->index == index
3764 && av->offset - offset == item->offset)
3766 if (values_equal_for_ipcp_p (item->value, av->value))
3767 found = true;
3768 break;
3771 if (!found)
3772 item->value = NULL_TREE;
3776 /* Intersect values in INTER with aggregate values that come along edge CS to
3777 parameter number INDEX and return it. If INTER does not actually exist yet,
3778 copy all incoming values to it. If we determine we ended up with no values
3779 whatsoever, return a released vector. */
3781 static vec<ipa_agg_jf_item>
3782 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3783 vec<ipa_agg_jf_item> inter)
3785 struct ipa_jump_func *jfunc;
3786 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3787 if (jfunc->type == IPA_JF_PASS_THROUGH
3788 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3790 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3791 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3793 if (caller_info->ipcp_orig_node)
3795 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3796 struct ipcp_param_lattices *orig_plats;
3797 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3798 src_idx);
3799 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3801 if (!inter.exists ())
3802 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3803 else
3804 intersect_with_agg_replacements (cs->caller, src_idx,
3805 &inter, 0);
3807 else
3809 inter.release ();
3810 return vNULL;
3813 else
3815 struct ipcp_param_lattices *src_plats;
3816 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3817 if (agg_pass_through_permissible_p (src_plats, jfunc))
3819 /* Currently we do not produce clobber aggregate jump
3820 functions, adjust when we do. */
3821 gcc_checking_assert (!jfunc->agg.items);
3822 if (!inter.exists ())
3823 inter = copy_plats_to_inter (src_plats, 0);
3824 else
3825 intersect_with_plats (src_plats, &inter, 0);
3827 else
3829 inter.release ();
3830 return vNULL;
3834 else if (jfunc->type == IPA_JF_ANCESTOR
3835 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3837 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3838 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3839 struct ipcp_param_lattices *src_plats;
3840 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3842 if (caller_info->ipcp_orig_node)
3844 if (!inter.exists ())
3845 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3846 else
3847 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3848 delta);
3850 else
3852 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3853 /* Currently we do not produce clobber aggregate jump
3854 functions, adjust when we do. */
3855 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3856 if (!inter.exists ())
3857 inter = copy_plats_to_inter (src_plats, delta);
3858 else
3859 intersect_with_plats (src_plats, &inter, delta);
3862 else if (jfunc->agg.items)
3864 struct ipa_agg_jf_item *item;
3865 int k;
3867 if (!inter.exists ())
3868 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3869 inter.safe_push ((*jfunc->agg.items)[i]);
3870 else
3871 FOR_EACH_VEC_ELT (inter, k, item)
3873 int l = 0;
3874 bool found = false;;
3876 if (!item->value)
3877 continue;
3879 while ((unsigned) l < jfunc->agg.items->length ())
3881 struct ipa_agg_jf_item *ti;
3882 ti = &(*jfunc->agg.items)[l];
3883 if (ti->offset > item->offset)
3884 break;
3885 if (ti->offset == item->offset)
3887 gcc_checking_assert (ti->value);
3888 if (values_equal_for_ipcp_p (item->value,
3889 ti->value))
3890 found = true;
3891 break;
3893 l++;
3895 if (!found)
3896 item->value = NULL;
3899 else
3901 inter.release ();
3902 return vec<ipa_agg_jf_item>();
3904 return inter;
3907 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3908 from all of them. */
3910 static struct ipa_agg_replacement_value *
3911 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3912 vec<cgraph_edge *> callers)
3914 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3915 struct ipa_agg_replacement_value *res;
3916 struct ipa_agg_replacement_value **tail = &res;
3917 struct cgraph_edge *cs;
3918 int i, j, count = ipa_get_param_count (dest_info);
3920 FOR_EACH_VEC_ELT (callers, j, cs)
3922 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3923 if (c < count)
3924 count = c;
3927 for (i = 0; i < count ; i++)
3929 struct cgraph_edge *cs;
3930 vec<ipa_agg_jf_item> inter = vNULL;
3931 struct ipa_agg_jf_item *item;
3932 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3933 int j;
3935 /* Among other things, the following check should deal with all by_ref
3936 mismatches. */
3937 if (plats->aggs_bottom)
3938 continue;
3940 FOR_EACH_VEC_ELT (callers, j, cs)
3942 inter = intersect_aggregates_with_edge (cs, i, inter);
3944 if (!inter.exists ())
3945 goto next_param;
3948 FOR_EACH_VEC_ELT (inter, j, item)
3950 struct ipa_agg_replacement_value *v;
3952 if (!item->value)
3953 continue;
3955 v = ggc_alloc<ipa_agg_replacement_value> ();
3956 v->index = i;
3957 v->offset = item->offset;
3958 v->value = item->value;
3959 v->by_ref = plats->aggs_by_ref;
3960 *tail = v;
3961 tail = &v->next;
3964 next_param:
3965 if (inter.exists ())
3966 inter.release ();
3968 *tail = NULL;
3969 return res;
3972 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3974 static struct ipa_agg_replacement_value *
3975 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3977 struct ipa_agg_replacement_value *res;
3978 struct ipa_agg_replacement_value **tail = &res;
3979 struct ipa_agg_jump_function *aggjf;
3980 struct ipa_agg_jf_item *item;
3981 int i, j;
3983 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3984 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3986 struct ipa_agg_replacement_value *v;
3987 v = ggc_alloc<ipa_agg_replacement_value> ();
3988 v->index = i;
3989 v->offset = item->offset;
3990 v->value = item->value;
3991 v->by_ref = aggjf->by_ref;
3992 *tail = v;
3993 tail = &v->next;
3995 *tail = NULL;
3996 return res;
3999 /* Determine whether CS also brings all scalar values that the NODE is
4000 specialized for. */
4002 static bool
4003 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
4004 struct cgraph_node *node)
4006 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
4007 int count = ipa_get_param_count (dest_info);
4008 struct ipa_node_params *caller_info;
4009 struct ipa_edge_args *args;
4010 int i;
4012 caller_info = IPA_NODE_REF (cs->caller);
4013 args = IPA_EDGE_REF (cs);
4014 for (i = 0; i < count; i++)
4016 struct ipa_jump_func *jump_func;
4017 tree val, t;
4019 val = dest_info->known_csts[i];
4020 if (!val)
4021 continue;
4023 if (i >= ipa_get_cs_argument_count (args))
4024 return false;
4025 jump_func = ipa_get_ith_jump_func (args, i);
4026 t = ipa_value_from_jfunc (caller_info, jump_func);
4027 if (!t || !values_equal_for_ipcp_p (val, t))
4028 return false;
4030 return true;
4033 /* Determine whether CS also brings all aggregate values that NODE is
4034 specialized for. */
4035 static bool
4036 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
4037 struct cgraph_node *node)
4039 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
4040 struct ipa_node_params *orig_node_info;
4041 struct ipa_agg_replacement_value *aggval;
4042 int i, ec, count;
4044 aggval = ipa_get_agg_replacements_for_node (node);
4045 if (!aggval)
4046 return true;
4048 count = ipa_get_param_count (IPA_NODE_REF (node));
4049 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
4050 if (ec < count)
4051 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
4052 if (aggval->index >= ec)
4053 return false;
4055 orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node);
4056 if (orig_caller_info->ipcp_orig_node)
4057 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
4059 for (i = 0; i < count; i++)
4061 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
4062 struct ipcp_param_lattices *plats;
4063 bool interesting = false;
4064 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
4065 if (aggval->index == i)
4067 interesting = true;
4068 break;
4070 if (!interesting)
4071 continue;
4073 plats = ipa_get_parm_lattices (orig_node_info, aggval->index);
4074 if (plats->aggs_bottom)
4075 return false;
4077 values = intersect_aggregates_with_edge (cs, i, values);
4078 if (!values.exists ())
4079 return false;
4081 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
4082 if (aggval->index == i)
4084 struct ipa_agg_jf_item *item;
4085 int j;
4086 bool found = false;
4087 FOR_EACH_VEC_ELT (values, j, item)
4088 if (item->value
4089 && item->offset == av->offset
4090 && values_equal_for_ipcp_p (item->value, av->value))
4092 found = true;
4093 break;
4095 if (!found)
4097 values.release ();
4098 return false;
4102 return true;
4105 /* Given an original NODE and a VAL for which we have already created a
4106 specialized clone, look whether there are incoming edges that still lead
4107 into the old node but now also bring the requested value and also conform to
4108 all other criteria such that they can be redirected the special node.
4109 This function can therefore redirect the final edge in a SCC. */
4111 template <typename valtype>
4112 static void
4113 perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
4115 ipcp_value_source<valtype> *src;
4116 gcov_type redirected_sum = 0;
4118 for (src = val->sources; src; src = src->next)
4120 struct cgraph_edge *cs = src->cs;
4121 while (cs)
4123 if (cgraph_edge_brings_value_p (cs, src, node)
4124 && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
4125 && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
4127 if (dump_file)
4128 fprintf (dump_file, " - adding an extra caller %s/%i"
4129 " of %s/%i\n",
4130 xstrdup_for_dump (cs->caller->name ()),
4131 cs->caller->order,
4132 xstrdup_for_dump (val->spec_node->name ()),
4133 val->spec_node->order);
4135 cs->redirect_callee_duplicating_thunks (val->spec_node);
4136 val->spec_node->expand_all_artificial_thunks ();
4137 redirected_sum += cs->count;
4139 cs = get_next_cgraph_edge_clone (cs);
4143 if (redirected_sum)
4144 update_specialized_profile (val->spec_node, node, redirected_sum);
4147 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4149 static bool
4150 known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
4152 ipa_polymorphic_call_context *ctx;
4153 int i;
4155 FOR_EACH_VEC_ELT (known_contexts, i, ctx)
4156 if (!ctx->useless_p ())
4157 return true;
4158 return false;
4161 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4163 static vec<ipa_polymorphic_call_context>
4164 copy_useful_known_contexts (vec<ipa_polymorphic_call_context> known_contexts)
4166 if (known_contexts_useful_p (known_contexts))
4167 return known_contexts.copy ();
4168 else
4169 return vNULL;
4172 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4173 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4175 static void
4176 modify_known_vectors_with_val (vec<tree> *known_csts,
4177 vec<ipa_polymorphic_call_context> *known_contexts,
4178 ipcp_value<tree> *val,
4179 int index)
4181 *known_csts = known_csts->copy ();
4182 *known_contexts = copy_useful_known_contexts (*known_contexts);
4183 (*known_csts)[index] = val->value;
4186 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4187 copy according to VAL and INDEX. */
4189 static void
4190 modify_known_vectors_with_val (vec<tree> *known_csts,
4191 vec<ipa_polymorphic_call_context> *known_contexts,
4192 ipcp_value<ipa_polymorphic_call_context> *val,
4193 int index)
4195 *known_csts = known_csts->copy ();
4196 *known_contexts = known_contexts->copy ();
4197 (*known_contexts)[index] = val->value;
4200 /* Return true if OFFSET indicates this was not an aggregate value or there is
4201 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4202 AGGVALS list. */
4204 DEBUG_FUNCTION bool
4205 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *aggvals,
4206 int index, HOST_WIDE_INT offset, tree value)
4208 if (offset == -1)
4209 return true;
4211 while (aggvals)
4213 if (aggvals->index == index
4214 && aggvals->offset == offset
4215 && values_equal_for_ipcp_p (aggvals->value, value))
4216 return true;
4217 aggvals = aggvals->next;
4219 return false;
4222 /* Return true if offset is minus one because source of a polymorphic contect
4223 cannot be an aggregate value. */
4225 DEBUG_FUNCTION bool
4226 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *,
4227 int , HOST_WIDE_INT offset,
4228 ipa_polymorphic_call_context)
4230 return offset == -1;
4233 /* Decide wheter to create a special version of NODE for value VAL of parameter
4234 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4235 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4236 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4238 template <typename valtype>
4239 static bool
4240 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
4241 ipcp_value<valtype> *val, vec<tree> known_csts,
4242 vec<ipa_polymorphic_call_context> known_contexts)
4244 struct ipa_agg_replacement_value *aggvals;
4245 int freq_sum, caller_count;
4246 gcov_type count_sum;
4247 vec<cgraph_edge *> callers;
4249 if (val->spec_node)
4251 perhaps_add_new_callers (node, val);
4252 return false;
4254 else if (val->local_size_cost + overall_size > max_new_size)
4256 if (dump_file && (dump_flags & TDF_DETAILS))
4257 fprintf (dump_file, " Ignoring candidate value because "
4258 "max_new_size would be reached with %li.\n",
4259 val->local_size_cost + overall_size);
4260 return false;
4262 else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum,
4263 &caller_count))
4264 return false;
4266 if (dump_file && (dump_flags & TDF_DETAILS))
4268 fprintf (dump_file, " - considering value ");
4269 print_ipcp_constant_value (dump_file, val->value);
4270 fprintf (dump_file, " for ");
4271 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
4272 if (offset != -1)
4273 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
4274 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
4277 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
4278 freq_sum, count_sum,
4279 val->local_size_cost)
4280 && !good_cloning_opportunity_p (node,
4281 val->local_time_benefit
4282 + val->prop_time_benefit,
4283 freq_sum, count_sum,
4284 val->local_size_cost
4285 + val->prop_size_cost))
4286 return false;
4288 if (dump_file)
4289 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
4290 node->name (), node->order);
4292 callers = gather_edges_for_value (val, node, caller_count);
4293 if (offset == -1)
4294 modify_known_vectors_with_val (&known_csts, &known_contexts, val, index);
4295 else
4297 known_csts = known_csts.copy ();
4298 known_contexts = copy_useful_known_contexts (known_contexts);
4300 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
4301 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
4302 aggvals = find_aggregate_values_for_callers_subset (node, callers);
4303 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
4304 offset, val->value));
4305 val->spec_node = create_specialized_node (node, known_csts, known_contexts,
4306 aggvals, callers);
4307 overall_size += val->local_size_cost;
4309 /* TODO: If for some lattice there is only one other known value
4310 left, make a special node for it too. */
4312 return true;
4315 /* Decide whether and what specialized clones of NODE should be created. */
4317 static bool
4318 decide_whether_version_node (struct cgraph_node *node)
4320 struct ipa_node_params *info = IPA_NODE_REF (node);
4321 int i, count = ipa_get_param_count (info);
4322 vec<tree> known_csts;
4323 vec<ipa_polymorphic_call_context> known_contexts;
4324 vec<ipa_agg_jump_function> known_aggs = vNULL;
4325 bool ret = false;
4327 if (count == 0)
4328 return false;
4330 if (dump_file && (dump_flags & TDF_DETAILS))
4331 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
4332 node->name (), node->order);
4334 gather_context_independent_values (info, &known_csts, &known_contexts,
4335 info->do_clone_for_all_contexts ? &known_aggs
4336 : NULL, NULL);
4338 for (i = 0; i < count ;i++)
4340 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4341 ipcp_lattice<tree> *lat = &plats->itself;
4342 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
4344 if (!lat->bottom
4345 && !known_csts[i])
4347 ipcp_value<tree> *val;
4348 for (val = lat->values; val; val = val->next)
4349 ret |= decide_about_value (node, i, -1, val, known_csts,
4350 known_contexts);
4353 if (!plats->aggs_bottom)
4355 struct ipcp_agg_lattice *aglat;
4356 ipcp_value<tree> *val;
4357 for (aglat = plats->aggs; aglat; aglat = aglat->next)
4358 if (!aglat->bottom && aglat->values
4359 /* If the following is false, the one value is in
4360 known_aggs. */
4361 && (plats->aggs_contain_variable
4362 || !aglat->is_single_const ()))
4363 for (val = aglat->values; val; val = val->next)
4364 ret |= decide_about_value (node, i, aglat->offset, val,
4365 known_csts, known_contexts);
4368 if (!ctxlat->bottom
4369 && known_contexts[i].useless_p ())
4371 ipcp_value<ipa_polymorphic_call_context> *val;
4372 for (val = ctxlat->values; val; val = val->next)
4373 ret |= decide_about_value (node, i, -1, val, known_csts,
4374 known_contexts);
4377 info = IPA_NODE_REF (node);
4380 if (info->do_clone_for_all_contexts)
4382 struct cgraph_node *clone;
4383 vec<cgraph_edge *> callers;
4385 if (dump_file)
4386 fprintf (dump_file, " - Creating a specialized node of %s/%i "
4387 "for all known contexts.\n", node->name (),
4388 node->order);
4390 callers = node->collect_callers ();
4392 if (!known_contexts_useful_p (known_contexts))
4394 known_contexts.release ();
4395 known_contexts = vNULL;
4397 clone = create_specialized_node (node, known_csts, known_contexts,
4398 known_aggs_to_agg_replacement_list (known_aggs),
4399 callers);
4400 info = IPA_NODE_REF (node);
4401 info->do_clone_for_all_contexts = false;
4402 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
4403 for (i = 0; i < count ; i++)
4404 vec_free (known_aggs[i].items);
4405 known_aggs.release ();
4406 ret = true;
4408 else
4410 known_csts.release ();
4411 known_contexts.release ();
4414 return ret;
4417 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4419 static void
4420 spread_undeadness (struct cgraph_node *node)
4422 struct cgraph_edge *cs;
4424 for (cs = node->callees; cs; cs = cs->next_callee)
4425 if (ipa_edge_within_scc (cs))
4427 struct cgraph_node *callee;
4428 struct ipa_node_params *info;
4430 callee = cs->callee->function_symbol (NULL);
4431 info = IPA_NODE_REF (callee);
4433 if (info->node_dead)
4435 info->node_dead = 0;
4436 spread_undeadness (callee);
4441 /* Return true if NODE has a caller from outside of its SCC that is not
4442 dead. Worker callback for cgraph_for_node_and_aliases. */
4444 static bool
4445 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
4446 void *data ATTRIBUTE_UNUSED)
4448 struct cgraph_edge *cs;
4450 for (cs = node->callers; cs; cs = cs->next_caller)
4451 if (cs->caller->thunk.thunk_p
4452 && cs->caller->call_for_symbol_thunks_and_aliases
4453 (has_undead_caller_from_outside_scc_p, NULL, true))
4454 return true;
4455 else if (!ipa_edge_within_scc (cs)
4456 && !IPA_NODE_REF (cs->caller)->node_dead)
4457 return true;
4458 return false;
4462 /* Identify nodes within the same SCC as NODE which are no longer needed
4463 because of new clones and will be removed as unreachable. */
4465 static void
4466 identify_dead_nodes (struct cgraph_node *node)
4468 struct cgraph_node *v;
4469 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4470 if (v->local.local
4471 && !v->call_for_symbol_thunks_and_aliases
4472 (has_undead_caller_from_outside_scc_p, NULL, true))
4473 IPA_NODE_REF (v)->node_dead = 1;
4475 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4476 if (!IPA_NODE_REF (v)->node_dead)
4477 spread_undeadness (v);
4479 if (dump_file && (dump_flags & TDF_DETAILS))
4481 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4482 if (IPA_NODE_REF (v)->node_dead)
4483 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
4484 v->name (), v->order);
4488 /* The decision stage. Iterate over the topological order of call graph nodes
4489 TOPO and make specialized clones if deemed beneficial. */
4491 static void
4492 ipcp_decision_stage (struct ipa_topo_info *topo)
4494 int i;
4496 if (dump_file)
4497 fprintf (dump_file, "\nIPA decision stage:\n\n");
4499 for (i = topo->nnodes - 1; i >= 0; i--)
4501 struct cgraph_node *node = topo->order[i];
4502 bool change = false, iterate = true;
4504 while (iterate)
4506 struct cgraph_node *v;
4507 iterate = false;
4508 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4509 if (v->has_gimple_body_p ()
4510 && ipcp_versionable_function_p (v))
4511 iterate |= decide_whether_version_node (v);
4513 change |= iterate;
4515 if (change)
4516 identify_dead_nodes (node);
4520 /* Look up all alignment information that we have discovered and copy it over
4521 to the transformation summary. */
4523 static void
4524 ipcp_store_alignment_results (void)
4526 cgraph_node *node;
4528 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4530 ipa_node_params *info = IPA_NODE_REF (node);
4531 bool dumped_sth = false;
4532 bool found_useful_result = false;
4534 if (!opt_for_fn (node->decl, flag_ipa_cp_alignment))
4536 if (dump_file)
4537 fprintf (dump_file, "Not considering %s for alignment discovery "
4538 "and propagate; -fipa-cp-alignment: disabled.\n",
4539 node->name ());
4540 continue;
4543 if (info->ipcp_orig_node)
4544 info = IPA_NODE_REF (info->ipcp_orig_node);
4546 unsigned count = ipa_get_param_count (info);
4547 for (unsigned i = 0; i < count ; i++)
4549 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4550 if (!plats->alignment.bottom_p ()
4551 && !plats->alignment.top_p ())
4553 gcc_checking_assert (plats->alignment.align > 0);
4554 found_useful_result = true;
4555 break;
4558 if (!found_useful_result)
4559 continue;
4561 ipcp_grow_transformations_if_necessary ();
4562 ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node);
4563 vec_safe_reserve_exact (ts->alignments, count);
4565 for (unsigned i = 0; i < count ; i++)
4567 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4568 ipa_alignment al;
4570 if (!plats->alignment.bottom_p ()
4571 && !plats->alignment.top_p ())
4573 al.known = true;
4574 al.align = plats->alignment.align;
4575 al.misalign = plats->alignment.misalign;
4577 else
4578 al.known = false;
4580 ts->alignments->quick_push (al);
4581 if (!dump_file || !al.known)
4582 continue;
4583 if (!dumped_sth)
4585 fprintf (dump_file, "Propagated alignment info for function %s/%i:\n",
4586 node->name (), node->order);
4587 dumped_sth = true;
4589 fprintf (dump_file, " param %i: align: %u, misalign: %u\n",
4590 i, al.align, al.misalign);
4595 /* The IPCP driver. */
4597 static unsigned int
4598 ipcp_driver (void)
4600 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
4601 struct cgraph_edge_hook_list *edge_removal_hook_holder;
4602 struct ipa_topo_info topo;
4604 ipa_check_create_node_params ();
4605 ipa_check_create_edge_args ();
4606 grow_edge_clone_vectors ();
4607 edge_duplication_hook_holder =
4608 symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
4609 edge_removal_hook_holder =
4610 symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
4612 if (dump_file)
4614 fprintf (dump_file, "\nIPA structures before propagation:\n");
4615 if (dump_flags & TDF_DETAILS)
4616 ipa_print_all_params (dump_file);
4617 ipa_print_all_jump_functions (dump_file);
4620 /* Topological sort. */
4621 build_toporder_info (&topo);
4622 /* Do the interprocedural propagation. */
4623 ipcp_propagate_stage (&topo);
4624 /* Decide what constant propagation and cloning should be performed. */
4625 ipcp_decision_stage (&topo);
4626 /* Store results of alignment propagation. */
4627 ipcp_store_alignment_results ();
4629 /* Free all IPCP structures. */
4630 free_toporder_info (&topo);
4631 next_edge_clone.release ();
4632 prev_edge_clone.release ();
4633 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4634 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4635 ipa_free_all_structures_after_ipa_cp ();
4636 if (dump_file)
4637 fprintf (dump_file, "\nIPA constant propagation end\n");
4638 return 0;
4641 /* Initialization and computation of IPCP data structures. This is the initial
4642 intraprocedural analysis of functions, which gathers information to be
4643 propagated later on. */
4645 static void
4646 ipcp_generate_summary (void)
4648 struct cgraph_node *node;
4650 if (dump_file)
4651 fprintf (dump_file, "\nIPA constant propagation start:\n");
4652 ipa_register_cgraph_hooks ();
4654 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4655 ipa_analyze_node (node);
4658 /* Write ipcp summary for nodes in SET. */
4660 static void
4661 ipcp_write_summary (void)
4663 ipa_prop_write_jump_functions ();
4666 /* Read ipcp summary. */
4668 static void
4669 ipcp_read_summary (void)
4671 ipa_prop_read_jump_functions ();
4674 namespace {
4676 const pass_data pass_data_ipa_cp =
4678 IPA_PASS, /* type */
4679 "cp", /* name */
4680 OPTGROUP_NONE, /* optinfo_flags */
4681 TV_IPA_CONSTANT_PROP, /* tv_id */
4682 0, /* properties_required */
4683 0, /* properties_provided */
4684 0, /* properties_destroyed */
4685 0, /* todo_flags_start */
4686 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
4689 class pass_ipa_cp : public ipa_opt_pass_d
4691 public:
4692 pass_ipa_cp (gcc::context *ctxt)
4693 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
4694 ipcp_generate_summary, /* generate_summary */
4695 ipcp_write_summary, /* write_summary */
4696 ipcp_read_summary, /* read_summary */
4697 ipcp_write_transformation_summaries, /*
4698 write_optimization_summary */
4699 ipcp_read_transformation_summaries, /*
4700 read_optimization_summary */
4701 NULL, /* stmt_fixup */
4702 0, /* function_transform_todo_flags_start */
4703 ipcp_transform_function, /* function_transform */
4704 NULL) /* variable_transform */
4707 /* opt_pass methods: */
4708 virtual bool gate (function *)
4710 /* FIXME: We should remove the optimize check after we ensure we never run
4711 IPA passes when not optimizing. */
4712 return (flag_ipa_cp && optimize) || in_lto_p;
4715 virtual unsigned int execute (function *) { return ipcp_driver (); }
4717 }; // class pass_ipa_cp
4719 } // anon namespace
4721 ipa_opt_pass_d *
4722 make_pass_ipa_cp (gcc::context *ctxt)
4724 return new pass_ipa_cp (ctxt);
4727 /* Reset all state within ipa-cp.c so that we can rerun the compiler
4728 within the same process. For use by toplev::finalize. */
4730 void
4731 ipa_cp_c_finalize (void)
4733 max_count = 0;
4734 overall_size = 0;
4735 max_new_size = 0;