2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / ipa-cp.c
blobb87c984137240abe3e545e74f96efd1523a4e7b0
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
5 <mjambor@suse.cz>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Interprocedural constant propagation (IPA-CP).
25 The goal of this transformation is to
27 1) discover functions which are always invoked with some arguments with the
28 same known constant values and modify the functions so that the
29 subsequent optimizations can take advantage of the knowledge, and
31 2) partial specialization - create specialized versions of functions
32 transformed in this way if some parameters are known constants only in
33 certain contexts but the estimated tradeoff between speedup and cost size
34 is deemed good.
36 The algorithm also propagates types and attempts to perform type based
37 devirtualization. Types are propagated much like constants.
39 The algorithm basically consists of three stages. In the first, functions
40 are analyzed one at a time and jump functions are constructed for all known
41 call-sites. In the second phase, the pass propagates information from the
42 jump functions across the call to reveal what values are available at what
43 call sites, performs estimations of effects of known values on functions and
44 their callees, and finally decides what specialized extra versions should be
45 created. In the third, the special versions materialize and appropriate
46 calls are redirected.
48 The algorithm used is to a certain extent based on "Interprocedural Constant
49 Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon,
50 Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D
51 Cooper, Mary W. Hall, and Ken Kennedy.
54 First stage - intraprocedural analysis
55 =======================================
57 This phase computes jump_function and modification flags.
59 A jump function for a call-site represents the values passed as an actual
60 arguments of a given call-site. In principle, there are three types of
61 values:
63 Pass through - the caller's formal parameter is passed as an actual
64 argument, plus an operation on it can be performed.
65 Constant - a constant is passed as an actual argument.
66 Unknown - neither of the above.
68 All jump function types are described in detail in ipa-prop.h, together with
69 the data structures that represent them and methods of accessing them.
71 ipcp_generate_summary() is the main function of the first stage.
73 Second stage - interprocedural analysis
74 ========================================
76 This stage is itself divided into two phases. In the first, we propagate
77 known values over the call graph, in the second, we make cloning decisions.
78 It uses a different algorithm than the original Callahan's paper.
80 First, we traverse the functions topologically from callers to callees and,
81 for each strongly connected component (SCC), we propagate constants
82 according to previously computed jump functions. We also record what known
83 values depend on other known values and estimate local effects. Finally, we
84 propagate cumulative information about these effects from dependent values
85 to those on which they depend.
87 Second, we again traverse the call graph in the same topological order and
88 make clones for functions which we know are called with the same values in
89 all contexts and decide about extra specialized clones of functions just for
90 some contexts - these decisions are based on both local estimates and
91 cumulative estimates propagated from callees.
93 ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
94 third stage.
96 Third phase - materialization of clones, call statement updates.
97 ============================================
99 This stage is currently performed by call graph code (mainly in cgraphunit.c
100 and tree-inline.c) according to instructions inserted to the call graph by
101 the second stage. */
103 #include "config.h"
104 #include "system.h"
105 #include "coretypes.h"
106 #include "alias.h"
107 #include "symtab.h"
108 #include "options.h"
109 #include "tree.h"
110 #include "fold-const.h"
111 #include "gimple-fold.h"
112 #include "gimple-expr.h"
113 #include "target.h"
114 #include "predict.h"
115 #include "basic-block.h"
116 #include "plugin-api.h"
117 #include "tm.h"
118 #include "hard-reg-set.h"
119 #include "function.h"
120 #include "ipa-ref.h"
121 #include "cgraph.h"
122 #include "alloc-pool.h"
123 #include "symbol-summary.h"
124 #include "ipa-prop.h"
125 #include "bitmap.h"
126 #include "tree-pass.h"
127 #include "flags.h"
128 #include "diagnostic.h"
129 #include "tree-pretty-print.h"
130 #include "tree-inline.h"
131 #include "params.h"
132 #include "ipa-inline.h"
133 #include "ipa-utils.h"
135 template <typename valtype> class ipcp_value;
137 /* Describes a particular source for an IPA-CP value. */
139 template <typename valtype>
140 class ipcp_value_source
142 public:
143 /* Aggregate offset of the source, negative if the source is scalar value of
144 the argument itself. */
145 HOST_WIDE_INT offset;
146 /* The incoming edge that brought the value. */
147 cgraph_edge *cs;
148 /* If the jump function that resulted into his value was a pass-through or an
149 ancestor, this is the ipcp_value of the caller from which the described
150 value has been derived. Otherwise it is NULL. */
151 ipcp_value<valtype> *val;
152 /* Next pointer in a linked list of sources of a value. */
153 ipcp_value_source *next;
154 /* If the jump function that resulted into his value was a pass-through or an
155 ancestor, this is the index of the parameter of the caller the jump
156 function references. */
157 int index;
160 /* Common ancestor for all ipcp_value instantiations. */
162 class ipcp_value_base
164 public:
165 /* Time benefit and size cost that specializing the function for this value
166 would bring about in this function alone. */
167 int local_time_benefit, local_size_cost;
168 /* Time benefit and size cost that specializing the function for this value
169 can bring about in it's callees (transitively). */
170 int prop_time_benefit, prop_size_cost;
173 /* Describes one particular value stored in struct ipcp_lattice. */
175 template <typename valtype>
176 class ipcp_value : public ipcp_value_base
178 public:
179 /* The actual value for the given parameter. */
180 valtype value;
181 /* The list of sources from which this value originates. */
182 ipcp_value_source <valtype> *sources;
183 /* Next pointers in a linked list of all values in a lattice. */
184 ipcp_value *next;
185 /* Next pointers in a linked list of values in a strongly connected component
186 of values. */
187 ipcp_value *scc_next;
188 /* Next pointers in a linked list of SCCs of values sorted topologically
189 according their sources. */
190 ipcp_value *topo_next;
191 /* A specialized node created for this value, NULL if none has been (so far)
192 created. */
193 cgraph_node *spec_node;
194 /* Depth first search number and low link for topological sorting of
195 values. */
196 int dfs, low_link;
197 /* True if this valye is currently on the topo-sort stack. */
198 bool on_stack;
200 void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx,
201 HOST_WIDE_INT offset);
204 /* Lattice describing potential values of a formal parameter of a function, or
205 a part of an aggreagate. TOP is represented by a lattice with zero values
206 and with contains_variable and bottom flags cleared. BOTTOM is represented
207 by a lattice with the bottom flag set. In that case, values and
208 contains_variable flag should be disregarded. */
210 template <typename valtype>
211 class ipcp_lattice
213 public:
214 /* The list of known values and types in this lattice. Note that values are
215 not deallocated if a lattice is set to bottom because there may be value
216 sources referencing them. */
217 ipcp_value<valtype> *values;
218 /* Number of known values and types in this lattice. */
219 int values_count;
220 /* The lattice contains a variable component (in addition to values). */
221 bool contains_variable;
222 /* The value of the lattice is bottom (i.e. variable and unusable for any
223 propagation). */
224 bool bottom;
226 inline bool is_single_const ();
227 inline bool set_to_bottom ();
228 inline bool set_contains_variable ();
229 bool add_value (valtype newval, cgraph_edge *cs,
230 ipcp_value<valtype> *src_val = NULL,
231 int src_idx = 0, HOST_WIDE_INT offset = -1);
232 void print (FILE * f, bool dump_sources, bool dump_benefits);
235 /* Lattice of tree values with an offset to describe a part of an
236 aggregate. */
238 class ipcp_agg_lattice : public ipcp_lattice<tree>
240 public:
241 /* Offset that is being described by this lattice. */
242 HOST_WIDE_INT offset;
243 /* Size so that we don't have to re-compute it every time we traverse the
244 list. Must correspond to TYPE_SIZE of all lat values. */
245 HOST_WIDE_INT size;
246 /* Next element of the linked list. */
247 struct ipcp_agg_lattice *next;
250 /* Structure containing lattices for a parameter itself and for pieces of
251 aggregates that are passed in the parameter or by a reference in a parameter
252 plus some other useful flags. */
254 class ipcp_param_lattices
256 public:
257 /* Lattice describing the value of the parameter itself. */
258 ipcp_lattice<tree> itself;
259 /* Lattice describing the the polymorphic contexts of a parameter. */
260 ipcp_lattice<ipa_polymorphic_call_context> ctxlat;
261 /* Lattices describing aggregate parts. */
262 ipcp_agg_lattice *aggs;
263 /* Alignment information. Very basic one value lattice where !known means
264 TOP and zero alignment bottom. */
265 ipa_alignment alignment;
266 /* Number of aggregate lattices */
267 int aggs_count;
268 /* True if aggregate data were passed by reference (as opposed to by
269 value). */
270 bool aggs_by_ref;
271 /* All aggregate lattices contain a variable component (in addition to
272 values). */
273 bool aggs_contain_variable;
274 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
275 for any propagation). */
276 bool aggs_bottom;
278 /* There is a virtual call based on this parameter. */
279 bool virt_call;
282 /* Allocation pools for values and their sources in ipa-cp. */
284 pool_allocator<ipcp_value<tree> > ipcp_cst_values_pool
285 ("IPA-CP constant values", 32);
287 pool_allocator<ipcp_value<ipa_polymorphic_call_context> >
288 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts", 32);
290 pool_allocator<ipcp_value_source<tree> > ipcp_sources_pool
291 ("IPA-CP value sources", 64);
293 pool_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
294 ("IPA_CP aggregate lattices", 32);
296 /* Maximal count found in program. */
298 static gcov_type max_count;
300 /* Original overall size of the program. */
302 static long overall_size, max_new_size;
304 /* Return the param lattices structure corresponding to the Ith formal
305 parameter of the function described by INFO. */
306 static inline struct ipcp_param_lattices *
307 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
309 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
310 gcc_checking_assert (!info->ipcp_orig_node);
311 gcc_checking_assert (info->lattices);
312 return &(info->lattices[i]);
315 /* Return the lattice corresponding to the scalar value of the Ith formal
316 parameter of the function described by INFO. */
317 static inline ipcp_lattice<tree> *
318 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
320 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
321 return &plats->itself;
324 /* Return the lattice corresponding to the scalar value of the Ith formal
325 parameter of the function described by INFO. */
326 static inline ipcp_lattice<ipa_polymorphic_call_context> *
327 ipa_get_poly_ctx_lat (struct ipa_node_params *info, int i)
329 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
330 return &plats->ctxlat;
333 /* Return whether LAT is a lattice with a single constant and without an
334 undefined value. */
336 template <typename valtype>
337 inline bool
338 ipcp_lattice<valtype>::is_single_const ()
340 if (bottom || contains_variable || values_count != 1)
341 return false;
342 else
343 return true;
346 /* Print V which is extracted from a value in a lattice to F. */
348 static void
349 print_ipcp_constant_value (FILE * f, tree v)
351 if (TREE_CODE (v) == ADDR_EXPR
352 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
354 fprintf (f, "& ");
355 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
357 else
358 print_generic_expr (f, v, 0);
361 /* Print V which is extracted from a value in a lattice to F. */
363 static void
364 print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
366 v.dump(f, false);
369 /* Print a lattice LAT to F. */
371 template <typename valtype>
372 void
373 ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
375 ipcp_value<valtype> *val;
376 bool prev = false;
378 if (bottom)
380 fprintf (f, "BOTTOM\n");
381 return;
384 if (!values_count && !contains_variable)
386 fprintf (f, "TOP\n");
387 return;
390 if (contains_variable)
392 fprintf (f, "VARIABLE");
393 prev = true;
394 if (dump_benefits)
395 fprintf (f, "\n");
398 for (val = values; val; val = val->next)
400 if (dump_benefits && prev)
401 fprintf (f, " ");
402 else if (!dump_benefits && prev)
403 fprintf (f, ", ");
404 else
405 prev = true;
407 print_ipcp_constant_value (f, val->value);
409 if (dump_sources)
411 ipcp_value_source<valtype> *s;
413 fprintf (f, " [from:");
414 for (s = val->sources; s; s = s->next)
415 fprintf (f, " %i(%i)", s->cs->caller->order,
416 s->cs->frequency);
417 fprintf (f, "]");
420 if (dump_benefits)
421 fprintf (f, " [loc_time: %i, loc_size: %i, "
422 "prop_time: %i, prop_size: %i]\n",
423 val->local_time_benefit, val->local_size_cost,
424 val->prop_time_benefit, val->prop_size_cost);
426 if (!dump_benefits)
427 fprintf (f, "\n");
430 /* Print all ipcp_lattices of all functions to F. */
432 static void
433 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
435 struct cgraph_node *node;
436 int i, count;
438 fprintf (f, "\nLattices:\n");
439 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
441 struct ipa_node_params *info;
443 info = IPA_NODE_REF (node);
444 fprintf (f, " Node: %s/%i:\n", node->name (),
445 node->order);
446 count = ipa_get_param_count (info);
447 for (i = 0; i < count; i++)
449 struct ipcp_agg_lattice *aglat;
450 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
451 fprintf (f, " param [%d]: ", i);
452 plats->itself.print (f, dump_sources, dump_benefits);
453 fprintf (f, " ctxs: ");
454 plats->ctxlat.print (f, dump_sources, dump_benefits);
455 if (plats->alignment.known && plats->alignment.align > 0)
456 fprintf (f, " Alignment %u, misalignment %u\n",
457 plats->alignment.align, plats->alignment.misalign);
458 else if (plats->alignment.known)
459 fprintf (f, " Alignment unusable\n");
460 else
461 fprintf (f, " Alignment unknown\n");
462 if (plats->virt_call)
463 fprintf (f, " virt_call flag set\n");
465 if (plats->aggs_bottom)
467 fprintf (f, " AGGS BOTTOM\n");
468 continue;
470 if (plats->aggs_contain_variable)
471 fprintf (f, " AGGS VARIABLE\n");
472 for (aglat = plats->aggs; aglat; aglat = aglat->next)
474 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
475 plats->aggs_by_ref ? "ref " : "", aglat->offset);
476 aglat->print (f, dump_sources, dump_benefits);
482 /* Determine whether it is at all technically possible to create clones of NODE
483 and store this information in the ipa_node_params structure associated
484 with NODE. */
486 static void
487 determine_versionability (struct cgraph_node *node)
489 const char *reason = NULL;
491 /* There are a number of generic reasons functions cannot be versioned. We
492 also cannot remove parameters if there are type attributes such as fnspec
493 present. */
494 if (node->alias || node->thunk.thunk_p)
495 reason = "alias or thunk";
496 else if (!node->local.versionable)
497 reason = "not a tree_versionable_function";
498 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
499 reason = "insufficient body availability";
500 else if (!opt_for_fn (node->decl, optimize)
501 || !opt_for_fn (node->decl, flag_ipa_cp))
502 reason = "non-optimized function";
503 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
505 /* Ideally we should clone the SIMD clones themselves and create
506 vector copies of them, so IPA-cp and SIMD clones can happily
507 coexist, but that may not be worth the effort. */
508 reason = "function has SIMD clones";
510 /* Don't clone decls local to a comdat group; it breaks and for C++
511 decloned constructors, inlining is always better anyway. */
512 else if (node->comdat_local_p ())
513 reason = "comdat-local function";
515 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
516 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
517 node->name (), node->order, reason);
519 node->local.versionable = (reason == NULL);
522 /* Return true if it is at all technically possible to create clones of a
523 NODE. */
525 static bool
526 ipcp_versionable_function_p (struct cgraph_node *node)
528 return node->local.versionable;
531 /* Structure holding accumulated information about callers of a node. */
533 struct caller_statistics
535 gcov_type count_sum;
536 int n_calls, n_hot_calls, freq_sum;
539 /* Initialize fields of STAT to zeroes. */
541 static inline void
542 init_caller_stats (struct caller_statistics *stats)
544 stats->count_sum = 0;
545 stats->n_calls = 0;
546 stats->n_hot_calls = 0;
547 stats->freq_sum = 0;
550 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
551 non-thunk incoming edges to NODE. */
553 static bool
554 gather_caller_stats (struct cgraph_node *node, void *data)
556 struct caller_statistics *stats = (struct caller_statistics *) data;
557 struct cgraph_edge *cs;
559 for (cs = node->callers; cs; cs = cs->next_caller)
560 if (!cs->caller->thunk.thunk_p)
562 stats->count_sum += cs->count;
563 stats->freq_sum += cs->frequency;
564 stats->n_calls++;
565 if (cs->maybe_hot_p ())
566 stats->n_hot_calls ++;
568 return false;
572 /* Return true if this NODE is viable candidate for cloning. */
574 static bool
575 ipcp_cloning_candidate_p (struct cgraph_node *node)
577 struct caller_statistics stats;
579 gcc_checking_assert (node->has_gimple_body_p ());
581 if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
583 if (dump_file)
584 fprintf (dump_file, "Not considering %s for cloning; "
585 "-fipa-cp-clone disabled.\n",
586 node->name ());
587 return false;
590 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
592 if (dump_file)
593 fprintf (dump_file, "Not considering %s for cloning; "
594 "optimizing it for size.\n",
595 node->name ());
596 return false;
599 init_caller_stats (&stats);
600 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
602 if (inline_summaries->get (node)->self_size < stats.n_calls)
604 if (dump_file)
605 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
606 node->name ());
607 return true;
610 /* When profile is available and function is hot, propagate into it even if
611 calls seems cold; constant propagation can improve function's speed
612 significantly. */
613 if (max_count)
615 if (stats.count_sum > node->count * 90 / 100)
617 if (dump_file)
618 fprintf (dump_file, "Considering %s for cloning; "
619 "usually called directly.\n",
620 node->name ());
621 return true;
624 if (!stats.n_hot_calls)
626 if (dump_file)
627 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
628 node->name ());
629 return false;
631 if (dump_file)
632 fprintf (dump_file, "Considering %s for cloning.\n",
633 node->name ());
634 return true;
637 template <typename valtype>
638 class value_topo_info
640 public:
641 /* Head of the linked list of topologically sorted values. */
642 ipcp_value<valtype> *values_topo;
643 /* Stack for creating SCCs, represented by a linked list too. */
644 ipcp_value<valtype> *stack;
645 /* Counter driving the algorithm in add_val_to_toposort. */
646 int dfs_counter;
648 value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
650 void add_val (ipcp_value<valtype> *cur_val);
651 void propagate_effects ();
654 /* Arrays representing a topological ordering of call graph nodes and a stack
655 of nodes used during constant propagation and also data required to perform
656 topological sort of values and propagation of benefits in the determined
657 order. */
659 class ipa_topo_info
661 public:
662 /* Array with obtained topological order of cgraph nodes. */
663 struct cgraph_node **order;
664 /* Stack of cgraph nodes used during propagation within SCC until all values
665 in the SCC stabilize. */
666 struct cgraph_node **stack;
667 int nnodes, stack_top;
669 value_topo_info<tree> constants;
670 value_topo_info<ipa_polymorphic_call_context> contexts;
672 ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
673 constants ()
677 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
679 static void
680 build_toporder_info (struct ipa_topo_info *topo)
682 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
683 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
685 gcc_checking_assert (topo->stack_top == 0);
686 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
689 /* Free information about strongly connected components and the arrays in
690 TOPO. */
692 static void
693 free_toporder_info (struct ipa_topo_info *topo)
695 ipa_free_postorder_info ();
696 free (topo->order);
697 free (topo->stack);
700 /* Add NODE to the stack in TOPO, unless it is already there. */
702 static inline void
703 push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node)
705 struct ipa_node_params *info = IPA_NODE_REF (node);
706 if (info->node_enqueued)
707 return;
708 info->node_enqueued = 1;
709 topo->stack[topo->stack_top++] = node;
712 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
713 is empty. */
715 static struct cgraph_node *
716 pop_node_from_stack (struct ipa_topo_info *topo)
718 if (topo->stack_top)
720 struct cgraph_node *node;
721 topo->stack_top--;
722 node = topo->stack[topo->stack_top];
723 IPA_NODE_REF (node)->node_enqueued = 0;
724 return node;
726 else
727 return NULL;
730 /* Set lattice LAT to bottom and return true if it previously was not set as
731 such. */
733 template <typename valtype>
734 inline bool
735 ipcp_lattice<valtype>::set_to_bottom ()
737 bool ret = !bottom;
738 bottom = true;
739 return ret;
742 /* Mark lattice as containing an unknown value and return true if it previously
743 was not marked as such. */
745 template <typename valtype>
746 inline bool
747 ipcp_lattice<valtype>::set_contains_variable ()
749 bool ret = !contains_variable;
750 contains_variable = true;
751 return ret;
754 /* Set all aggegate lattices in PLATS to bottom and return true if they were
755 not previously set as such. */
757 static inline bool
758 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
760 bool ret = !plats->aggs_bottom;
761 plats->aggs_bottom = true;
762 return ret;
765 /* Mark all aggegate lattices in PLATS as containing an unknown value and
766 return true if they were not previously marked as such. */
768 static inline bool
769 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
771 bool ret = !plats->aggs_contain_variable;
772 plats->aggs_contain_variable = true;
773 return ret;
776 /* Return true if alignment information in PLATS is known to be unusable. */
778 static inline bool
779 alignment_bottom_p (ipcp_param_lattices *plats)
781 return plats->alignment.known && (plats->alignment.align == 0);
784 /* Set alignment information in PLATS to unusable. Return true if it
785 previously was usable or unknown. */
787 static inline bool
788 set_alignment_to_bottom (ipcp_param_lattices *plats)
790 if (alignment_bottom_p (plats))
791 return false;
792 plats->alignment.known = true;
793 plats->alignment.align = 0;
794 return true;
797 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
798 return true is any of them has not been marked as such so far. */
800 static inline bool
801 set_all_contains_variable (struct ipcp_param_lattices *plats)
803 bool ret;
804 ret = plats->itself.set_contains_variable ();
805 ret |= plats->ctxlat.set_contains_variable ();
806 ret |= set_agg_lats_contain_variable (plats);
807 ret |= set_alignment_to_bottom (plats);
808 return ret;
811 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
812 points to by the number of callers to NODE. */
814 static bool
815 count_callers (cgraph_node *node, void *data)
817 int *caller_count = (int *) data;
819 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
820 /* Local thunks can be handled transparently, but if the thunk can not
821 be optimized out, count it as a real use. */
822 if (!cs->caller->thunk.thunk_p || !cs->caller->local.local)
823 ++*caller_count;
824 return false;
827 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
828 the one caller of some other node. Set the caller's corresponding flag. */
830 static bool
831 set_single_call_flag (cgraph_node *node, void *)
833 cgraph_edge *cs = node->callers;
834 /* Local thunks can be handled transparently, skip them. */
835 while (cs && cs->caller->thunk.thunk_p && cs->caller->local.local)
836 cs = cs->next_caller;
837 if (cs)
839 IPA_NODE_REF (cs->caller)->node_calling_single_call = true;
840 return true;
842 return false;
845 /* Initialize ipcp_lattices. */
847 static void
848 initialize_node_lattices (struct cgraph_node *node)
850 struct ipa_node_params *info = IPA_NODE_REF (node);
851 struct cgraph_edge *ie;
852 bool disable = false, variable = false;
853 int i;
855 gcc_checking_assert (node->has_gimple_body_p ());
856 if (cgraph_local_p (node))
858 int caller_count = 0;
859 node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
860 true);
861 gcc_checking_assert (caller_count > 0);
862 if (caller_count == 1)
863 node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
864 NULL, true);
866 else
868 /* When cloning is allowed, we can assume that externally visible
869 functions are not called. We will compensate this by cloning
870 later. */
871 if (ipcp_versionable_function_p (node)
872 && ipcp_cloning_candidate_p (node))
873 variable = true;
874 else
875 disable = true;
878 if (disable || variable)
880 for (i = 0; i < ipa_get_param_count (info) ; i++)
882 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
883 if (disable)
885 plats->itself.set_to_bottom ();
886 plats->ctxlat.set_to_bottom ();
887 set_agg_lats_to_bottom (plats);
888 set_alignment_to_bottom (plats);
890 else
891 set_all_contains_variable (plats);
893 if (dump_file && (dump_flags & TDF_DETAILS)
894 && !node->alias && !node->thunk.thunk_p)
895 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
896 node->name (), node->order,
897 disable ? "BOTTOM" : "VARIABLE");
900 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
901 if (ie->indirect_info->polymorphic
902 && ie->indirect_info->param_index >= 0)
904 gcc_checking_assert (ie->indirect_info->param_index >= 0);
905 ipa_get_parm_lattices (info,
906 ie->indirect_info->param_index)->virt_call = 1;
910 /* Return the result of a (possibly arithmetic) pass through jump function
911 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
912 determined or be considered an interprocedural invariant. */
914 static tree
915 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
917 tree restype, res;
919 gcc_checking_assert (is_gimple_ip_invariant (input));
920 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
921 return input;
923 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
924 == tcc_comparison)
925 restype = boolean_type_node;
926 else
927 restype = TREE_TYPE (input);
928 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
929 input, ipa_get_jf_pass_through_operand (jfunc));
931 if (res && !is_gimple_ip_invariant (res))
932 return NULL_TREE;
934 return res;
937 /* Return the result of an ancestor jump function JFUNC on the constant value
938 INPUT. Return NULL_TREE if that cannot be determined. */
940 static tree
941 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
943 gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
944 if (TREE_CODE (input) == ADDR_EXPR)
946 tree t = TREE_OPERAND (input, 0);
947 t = build_ref_for_offset (EXPR_LOCATION (t), t,
948 ipa_get_jf_ancestor_offset (jfunc),
949 ptr_type_node, NULL, false);
950 return build_fold_addr_expr (t);
952 else
953 return NULL_TREE;
956 /* Determine whether JFUNC evaluates to a single known constant value and if
957 so, return it. Otherwise return NULL. INFO describes the caller node or
958 the one it is inlined to, so that pass-through jump functions can be
959 evaluated. */
961 tree
962 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
964 if (jfunc->type == IPA_JF_CONST)
965 return ipa_get_jf_constant (jfunc);
966 else if (jfunc->type == IPA_JF_PASS_THROUGH
967 || jfunc->type == IPA_JF_ANCESTOR)
969 tree input;
970 int idx;
972 if (jfunc->type == IPA_JF_PASS_THROUGH)
973 idx = ipa_get_jf_pass_through_formal_id (jfunc);
974 else
975 idx = ipa_get_jf_ancestor_formal_id (jfunc);
977 if (info->ipcp_orig_node)
978 input = info->known_csts[idx];
979 else
981 ipcp_lattice<tree> *lat;
983 if (!info->lattices
984 || idx >= ipa_get_param_count (info))
985 return NULL_TREE;
986 lat = ipa_get_scalar_lat (info, idx);
987 if (!lat->is_single_const ())
988 return NULL_TREE;
989 input = lat->values->value;
992 if (!input)
993 return NULL_TREE;
995 if (jfunc->type == IPA_JF_PASS_THROUGH)
996 return ipa_get_jf_pass_through_result (jfunc, input);
997 else
998 return ipa_get_jf_ancestor_result (jfunc, input);
1000 else
1001 return NULL_TREE;
1004 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1005 that INFO describes the caller node or the one it is inlined to, CS is the
1006 call graph edge corresponding to JFUNC and CSIDX index of the described
1007 parameter. */
1009 ipa_polymorphic_call_context
1010 ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1011 ipa_jump_func *jfunc)
1013 ipa_edge_args *args = IPA_EDGE_REF (cs);
1014 ipa_polymorphic_call_context ctx;
1015 ipa_polymorphic_call_context *edge_ctx
1016 = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1018 if (edge_ctx && !edge_ctx->useless_p ())
1019 ctx = *edge_ctx;
1021 if (jfunc->type == IPA_JF_PASS_THROUGH
1022 || jfunc->type == IPA_JF_ANCESTOR)
1024 ipa_polymorphic_call_context srcctx;
1025 int srcidx;
1026 bool type_preserved = true;
1027 if (jfunc->type == IPA_JF_PASS_THROUGH)
1029 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1030 return ctx;
1031 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1032 srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1034 else
1036 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1037 srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1039 if (info->ipcp_orig_node)
1041 if (info->known_contexts.exists ())
1042 srcctx = info->known_contexts[srcidx];
1044 else
1046 if (!info->lattices
1047 || srcidx >= ipa_get_param_count (info))
1048 return ctx;
1049 ipcp_lattice<ipa_polymorphic_call_context> *lat;
1050 lat = ipa_get_poly_ctx_lat (info, srcidx);
1051 if (!lat->is_single_const ())
1052 return ctx;
1053 srcctx = lat->values->value;
1055 if (srcctx.useless_p ())
1056 return ctx;
1057 if (jfunc->type == IPA_JF_ANCESTOR)
1058 srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1059 if (!type_preserved)
1060 srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1061 srcctx.combine_with (ctx);
1062 return srcctx;
1065 return ctx;
1068 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1069 bottom, not containing a variable component and without any known value at
1070 the same time. */
1072 DEBUG_FUNCTION void
1073 ipcp_verify_propagated_values (void)
1075 struct cgraph_node *node;
1077 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1079 struct ipa_node_params *info = IPA_NODE_REF (node);
1080 int i, count = ipa_get_param_count (info);
1082 for (i = 0; i < count; i++)
1084 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
1086 if (!lat->bottom
1087 && !lat->contains_variable
1088 && lat->values_count == 0)
1090 if (dump_file)
1092 symtab_node::dump_table (dump_file);
1093 fprintf (dump_file, "\nIPA lattices after constant "
1094 "propagation, before gcc_unreachable:\n");
1095 print_all_lattices (dump_file, true, false);
1098 gcc_unreachable ();
1104 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1106 static bool
1107 values_equal_for_ipcp_p (tree x, tree y)
1109 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
1111 if (x == y)
1112 return true;
1114 if (TREE_CODE (x) == ADDR_EXPR
1115 && TREE_CODE (y) == ADDR_EXPR
1116 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
1117 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
1118 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
1119 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
1120 else
1121 return operand_equal_p (x, y, 0);
1124 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1126 static bool
1127 values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
1128 ipa_polymorphic_call_context y)
1130 return x.equal_to (y);
1134 /* Add a new value source to the value represented by THIS, marking that a
1135 value comes from edge CS and (if the underlying jump function is a
1136 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1137 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1138 scalar value of the parameter itself or the offset within an aggregate. */
1140 template <typename valtype>
1141 void
1142 ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
1143 int src_idx, HOST_WIDE_INT offset)
1145 ipcp_value_source<valtype> *src;
1147 src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
1148 src->offset = offset;
1149 src->cs = cs;
1150 src->val = src_val;
1151 src->index = src_idx;
1153 src->next = sources;
1154 sources = src;
1157 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1158 SOURCE and clear all other fields. */
1160 static ipcp_value<tree> *
1161 allocate_and_init_ipcp_value (tree source)
1163 ipcp_value<tree> *val;
1165 val = ipcp_cst_values_pool.allocate ();
1166 memset (val, 0, sizeof (*val));
1167 val->value = source;
1168 return val;
1171 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1172 value to SOURCE and clear all other fields. */
1174 static ipcp_value<ipa_polymorphic_call_context> *
1175 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source)
1177 ipcp_value<ipa_polymorphic_call_context> *val;
1179 // TODO
1180 val = ipcp_poly_ctx_values_pool.allocate ();
1181 memset (val, 0, sizeof (*val));
1182 val->value = source;
1183 return val;
1186 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1187 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1188 meaning. OFFSET -1 means the source is scalar and not a part of an
1189 aggregate. */
1191 template <typename valtype>
1192 bool
1193 ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
1194 ipcp_value<valtype> *src_val,
1195 int src_idx, HOST_WIDE_INT offset)
1197 ipcp_value<valtype> *val;
1199 if (bottom)
1200 return false;
1202 for (val = values; val; val = val->next)
1203 if (values_equal_for_ipcp_p (val->value, newval))
1205 if (ipa_edge_within_scc (cs))
1207 ipcp_value_source<valtype> *s;
1208 for (s = val->sources; s ; s = s->next)
1209 if (s->cs == cs)
1210 break;
1211 if (s)
1212 return false;
1215 val->add_source (cs, src_val, src_idx, offset);
1216 return false;
1219 if (values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
1221 /* We can only free sources, not the values themselves, because sources
1222 of other values in this this SCC might point to them. */
1223 for (val = values; val; val = val->next)
1225 while (val->sources)
1227 ipcp_value_source<valtype> *src = val->sources;
1228 val->sources = src->next;
1229 ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
1233 values = NULL;
1234 return set_to_bottom ();
1237 values_count++;
1238 val = allocate_and_init_ipcp_value (newval);
1239 val->add_source (cs, src_val, src_idx, offset);
1240 val->next = values;
1241 values = val;
1242 return true;
1245 /* Propagate values through a pass-through jump function JFUNC associated with
1246 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1247 is the index of the source parameter. */
1249 static bool
1250 propagate_vals_accross_pass_through (cgraph_edge *cs,
1251 ipa_jump_func *jfunc,
1252 ipcp_lattice<tree> *src_lat,
1253 ipcp_lattice<tree> *dest_lat,
1254 int src_idx)
1256 ipcp_value<tree> *src_val;
1257 bool ret = false;
1259 /* Do not create new values when propagating within an SCC because if there
1260 are arithmetic functions with circular dependencies, there is infinite
1261 number of them and we would just make lattices bottom. */
1262 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1263 && ipa_edge_within_scc (cs))
1264 ret = dest_lat->set_contains_variable ();
1265 else
1266 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1268 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1270 if (cstval)
1271 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx);
1272 else
1273 ret |= dest_lat->set_contains_variable ();
1276 return ret;
1279 /* Propagate values through an ancestor jump function JFUNC associated with
1280 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1281 is the index of the source parameter. */
1283 static bool
1284 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1285 struct ipa_jump_func *jfunc,
1286 ipcp_lattice<tree> *src_lat,
1287 ipcp_lattice<tree> *dest_lat,
1288 int src_idx)
1290 ipcp_value<tree> *src_val;
1291 bool ret = false;
1293 if (ipa_edge_within_scc (cs))
1294 return dest_lat->set_contains_variable ();
1296 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1298 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1300 if (t)
1301 ret |= dest_lat->add_value (t, cs, src_val, src_idx);
1302 else
1303 ret |= dest_lat->set_contains_variable ();
1306 return ret;
1309 /* Propagate scalar values across jump function JFUNC that is associated with
1310 edge CS and put the values into DEST_LAT. */
1312 static bool
1313 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1314 struct ipa_jump_func *jfunc,
1315 ipcp_lattice<tree> *dest_lat)
1317 if (dest_lat->bottom)
1318 return false;
1320 if (jfunc->type == IPA_JF_CONST)
1322 tree val = ipa_get_jf_constant (jfunc);
1323 return dest_lat->add_value (val, cs, NULL, 0);
1325 else if (jfunc->type == IPA_JF_PASS_THROUGH
1326 || jfunc->type == IPA_JF_ANCESTOR)
1328 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1329 ipcp_lattice<tree> *src_lat;
1330 int src_idx;
1331 bool ret;
1333 if (jfunc->type == IPA_JF_PASS_THROUGH)
1334 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1335 else
1336 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1338 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1339 if (src_lat->bottom)
1340 return dest_lat->set_contains_variable ();
1342 /* If we would need to clone the caller and cannot, do not propagate. */
1343 if (!ipcp_versionable_function_p (cs->caller)
1344 && (src_lat->contains_variable
1345 || (src_lat->values_count > 1)))
1346 return dest_lat->set_contains_variable ();
1348 if (jfunc->type == IPA_JF_PASS_THROUGH)
1349 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1350 dest_lat, src_idx);
1351 else
1352 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1353 src_idx);
1355 if (src_lat->contains_variable)
1356 ret |= dest_lat->set_contains_variable ();
1358 return ret;
1361 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1362 use it for indirect inlining), we should propagate them too. */
1363 return dest_lat->set_contains_variable ();
1366 /* Propagate scalar values across jump function JFUNC that is associated with
1367 edge CS and describes argument IDX and put the values into DEST_LAT. */
1369 static bool
1370 propagate_context_accross_jump_function (cgraph_edge *cs,
1371 ipa_jump_func *jfunc, int idx,
1372 ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
1374 ipa_edge_args *args = IPA_EDGE_REF (cs);
1375 if (dest_lat->bottom)
1376 return false;
1377 bool ret = false;
1378 bool added_sth = false;
1379 bool type_preserved = true;
1381 ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
1382 = ipa_get_ith_polymorhic_call_context (args, idx);
1384 if (edge_ctx_ptr)
1385 edge_ctx = *edge_ctx_ptr;
1387 if (jfunc->type == IPA_JF_PASS_THROUGH
1388 || jfunc->type == IPA_JF_ANCESTOR)
1390 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1391 int src_idx;
1392 ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
1394 /* TODO: Once we figure out how to propagate speculations, it will
1395 probably be a good idea to switch to speculation if type_preserved is
1396 not set instead of punting. */
1397 if (jfunc->type == IPA_JF_PASS_THROUGH)
1399 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1400 goto prop_fail;
1401 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1402 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1404 else
1406 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1407 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1410 src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
1411 /* If we would need to clone the caller and cannot, do not propagate. */
1412 if (!ipcp_versionable_function_p (cs->caller)
1413 && (src_lat->contains_variable
1414 || (src_lat->values_count > 1)))
1415 goto prop_fail;
1417 ipcp_value<ipa_polymorphic_call_context> *src_val;
1418 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1420 ipa_polymorphic_call_context cur = src_val->value;
1422 if (!type_preserved)
1423 cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1424 if (jfunc->type == IPA_JF_ANCESTOR)
1425 cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1426 /* TODO: In cases we know how the context is going to be used,
1427 we can improve the result by passing proper OTR_TYPE. */
1428 cur.combine_with (edge_ctx);
1429 if (!cur.useless_p ())
1431 if (src_lat->contains_variable
1432 && !edge_ctx.equal_to (cur))
1433 ret |= dest_lat->set_contains_variable ();
1434 ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
1435 added_sth = true;
1441 prop_fail:
1442 if (!added_sth)
1444 if (!edge_ctx.useless_p ())
1445 ret |= dest_lat->add_value (edge_ctx, cs);
1446 else
1447 ret |= dest_lat->set_contains_variable ();
1450 return ret;
1453 /* Propagate alignments across jump function JFUNC that is associated with
1454 edge CS and update DEST_LAT accordingly. */
1456 static bool
1457 propagate_alignment_accross_jump_function (struct cgraph_edge *cs,
1458 struct ipa_jump_func *jfunc,
1459 struct ipcp_param_lattices *dest_lat)
1461 if (alignment_bottom_p (dest_lat))
1462 return false;
1464 ipa_alignment cur;
1465 cur.known = false;
1466 if (jfunc->alignment.known)
1467 cur = jfunc->alignment;
1468 else if (jfunc->type == IPA_JF_PASS_THROUGH
1469 || jfunc->type == IPA_JF_ANCESTOR)
1471 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1472 struct ipcp_param_lattices *src_lats;
1473 HOST_WIDE_INT offset = 0;
1474 int src_idx;
1476 if (jfunc->type == IPA_JF_PASS_THROUGH)
1478 enum tree_code op = ipa_get_jf_pass_through_operation (jfunc);
1479 if (op != NOP_EXPR)
1481 if (op != POINTER_PLUS_EXPR
1482 && op != PLUS_EXPR)
1483 goto prop_fail;
1484 tree operand = ipa_get_jf_pass_through_operand (jfunc);
1485 if (!tree_fits_shwi_p (operand))
1486 goto prop_fail;
1487 offset = tree_to_shwi (operand);
1489 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1491 else
1493 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1494 offset = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;;
1497 src_lats = ipa_get_parm_lattices (caller_info, src_idx);
1498 if (!src_lats->alignment.known
1499 || alignment_bottom_p (src_lats))
1500 goto prop_fail;
1502 cur = src_lats->alignment;
1503 cur.misalign = (cur.misalign + offset) % cur.align;
1506 if (cur.known)
1508 if (!dest_lat->alignment.known)
1510 dest_lat->alignment = cur;
1511 return true;
1513 else if (dest_lat->alignment.align == cur.align
1514 && dest_lat->alignment.misalign == cur.misalign)
1515 return false;
1518 prop_fail:
1519 set_alignment_to_bottom (dest_lat);
1520 return true;
1523 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1524 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1525 other cases, return false). If there are no aggregate items, set
1526 aggs_by_ref to NEW_AGGS_BY_REF. */
1528 static bool
1529 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1530 bool new_aggs_by_ref)
1532 if (dest_plats->aggs)
1534 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1536 set_agg_lats_to_bottom (dest_plats);
1537 return true;
1540 else
1541 dest_plats->aggs_by_ref = new_aggs_by_ref;
1542 return false;
1545 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1546 already existing lattice for the given OFFSET and SIZE, marking all skipped
1547 lattices as containing variable and checking for overlaps. If there is no
1548 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1549 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1550 unless there are too many already. If there are two many, return false. If
1551 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1552 skipped lattices were newly marked as containing variable, set *CHANGE to
1553 true. */
1555 static bool
1556 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1557 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1558 struct ipcp_agg_lattice ***aglat,
1559 bool pre_existing, bool *change)
1561 gcc_checking_assert (offset >= 0);
1563 while (**aglat && (**aglat)->offset < offset)
1565 if ((**aglat)->offset + (**aglat)->size > offset)
1567 set_agg_lats_to_bottom (dest_plats);
1568 return false;
1570 *change |= (**aglat)->set_contains_variable ();
1571 *aglat = &(**aglat)->next;
1574 if (**aglat && (**aglat)->offset == offset)
1576 if ((**aglat)->size != val_size
1577 || ((**aglat)->next
1578 && (**aglat)->next->offset < offset + val_size))
1580 set_agg_lats_to_bottom (dest_plats);
1581 return false;
1583 gcc_checking_assert (!(**aglat)->next
1584 || (**aglat)->next->offset >= offset + val_size);
1585 return true;
1587 else
1589 struct ipcp_agg_lattice *new_al;
1591 if (**aglat && (**aglat)->offset < offset + val_size)
1593 set_agg_lats_to_bottom (dest_plats);
1594 return false;
1596 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1597 return false;
1598 dest_plats->aggs_count++;
1599 new_al = ipcp_agg_lattice_pool.allocate ();
1600 memset (new_al, 0, sizeof (*new_al));
1602 new_al->offset = offset;
1603 new_al->size = val_size;
1604 new_al->contains_variable = pre_existing;
1606 new_al->next = **aglat;
1607 **aglat = new_al;
1608 return true;
1612 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1613 containing an unknown value. */
1615 static bool
1616 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1618 bool ret = false;
1619 while (aglat)
1621 ret |= aglat->set_contains_variable ();
1622 aglat = aglat->next;
1624 return ret;
1627 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1628 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1629 parameter used for lattice value sources. Return true if DEST_PLATS changed
1630 in any way. */
1632 static bool
1633 merge_aggregate_lattices (struct cgraph_edge *cs,
1634 struct ipcp_param_lattices *dest_plats,
1635 struct ipcp_param_lattices *src_plats,
1636 int src_idx, HOST_WIDE_INT offset_delta)
1638 bool pre_existing = dest_plats->aggs != NULL;
1639 struct ipcp_agg_lattice **dst_aglat;
1640 bool ret = false;
1642 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1643 return true;
1644 if (src_plats->aggs_bottom)
1645 return set_agg_lats_contain_variable (dest_plats);
1646 if (src_plats->aggs_contain_variable)
1647 ret |= set_agg_lats_contain_variable (dest_plats);
1648 dst_aglat = &dest_plats->aggs;
1650 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1651 src_aglat;
1652 src_aglat = src_aglat->next)
1654 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1656 if (new_offset < 0)
1657 continue;
1658 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1659 &dst_aglat, pre_existing, &ret))
1661 struct ipcp_agg_lattice *new_al = *dst_aglat;
1663 dst_aglat = &(*dst_aglat)->next;
1664 if (src_aglat->bottom)
1666 ret |= new_al->set_contains_variable ();
1667 continue;
1669 if (src_aglat->contains_variable)
1670 ret |= new_al->set_contains_variable ();
1671 for (ipcp_value<tree> *val = src_aglat->values;
1672 val;
1673 val = val->next)
1674 ret |= new_al->add_value (val->value, cs, val, src_idx,
1675 src_aglat->offset);
1677 else if (dest_plats->aggs_bottom)
1678 return true;
1680 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1681 return ret;
1684 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1685 pass-through JFUNC and if so, whether it has conform and conforms to the
1686 rules about propagating values passed by reference. */
1688 static bool
1689 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1690 struct ipa_jump_func *jfunc)
1692 return src_plats->aggs
1693 && (!src_plats->aggs_by_ref
1694 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1697 /* Propagate scalar values across jump function JFUNC that is associated with
1698 edge CS and put the values into DEST_LAT. */
1700 static bool
1701 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1702 struct ipa_jump_func *jfunc,
1703 struct ipcp_param_lattices *dest_plats)
1705 bool ret = false;
1707 if (dest_plats->aggs_bottom)
1708 return false;
1710 if (jfunc->type == IPA_JF_PASS_THROUGH
1711 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1713 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1714 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1715 struct ipcp_param_lattices *src_plats;
1717 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1718 if (agg_pass_through_permissible_p (src_plats, jfunc))
1720 /* Currently we do not produce clobber aggregate jump
1721 functions, replace with merging when we do. */
1722 gcc_assert (!jfunc->agg.items);
1723 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1724 src_idx, 0);
1726 else
1727 ret |= set_agg_lats_contain_variable (dest_plats);
1729 else if (jfunc->type == IPA_JF_ANCESTOR
1730 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1732 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1733 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1734 struct ipcp_param_lattices *src_plats;
1736 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1737 if (src_plats->aggs && src_plats->aggs_by_ref)
1739 /* Currently we do not produce clobber aggregate jump
1740 functions, replace with merging when we do. */
1741 gcc_assert (!jfunc->agg.items);
1742 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1743 ipa_get_jf_ancestor_offset (jfunc));
1745 else if (!src_plats->aggs_by_ref)
1746 ret |= set_agg_lats_to_bottom (dest_plats);
1747 else
1748 ret |= set_agg_lats_contain_variable (dest_plats);
1750 else if (jfunc->agg.items)
1752 bool pre_existing = dest_plats->aggs != NULL;
1753 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1754 struct ipa_agg_jf_item *item;
1755 int i;
1757 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1758 return true;
1760 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1762 HOST_WIDE_INT val_size;
1764 if (item->offset < 0)
1765 continue;
1766 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1767 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1769 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1770 &aglat, pre_existing, &ret))
1772 ret |= (*aglat)->add_value (item->value, cs, NULL, 0, 0);
1773 aglat = &(*aglat)->next;
1775 else if (dest_plats->aggs_bottom)
1776 return true;
1779 ret |= set_chain_of_aglats_contains_variable (*aglat);
1781 else
1782 ret |= set_agg_lats_contain_variable (dest_plats);
1784 return ret;
1787 /* Propagate constants from the caller to the callee of CS. INFO describes the
1788 caller. */
1790 static bool
1791 propagate_constants_accross_call (struct cgraph_edge *cs)
1793 struct ipa_node_params *callee_info;
1794 enum availability availability;
1795 struct cgraph_node *callee, *alias_or_thunk;
1796 struct ipa_edge_args *args;
1797 bool ret = false;
1798 int i, args_count, parms_count;
1800 callee = cs->callee->function_symbol (&availability);
1801 if (!callee->definition)
1802 return false;
1803 gcc_checking_assert (callee->has_gimple_body_p ());
1804 callee_info = IPA_NODE_REF (callee);
1806 args = IPA_EDGE_REF (cs);
1807 args_count = ipa_get_cs_argument_count (args);
1808 parms_count = ipa_get_param_count (callee_info);
1809 if (parms_count == 0)
1810 return false;
1812 /* No propagation through instrumentation thunks is available yet.
1813 It should be possible with proper mapping of call args and
1814 instrumented callee params in the propagation loop below. But
1815 this case mostly occurs when legacy code calls instrumented code
1816 and it is not a primary target for optimizations.
1817 We detect instrumentation thunks in aliases and thunks chain by
1818 checking instrumentation_clone flag for chain source and target.
1819 Going through instrumentation thunks we always have it changed
1820 from 0 to 1 and all other nodes do not change it. */
1821 if (!cs->callee->instrumentation_clone
1822 && callee->instrumentation_clone)
1824 for (i = 0; i < parms_count; i++)
1825 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1826 i));
1827 return ret;
1830 /* If this call goes through a thunk we must not propagate to the first (0th)
1831 parameter. However, we might need to uncover a thunk from below a series
1832 of aliases first. */
1833 alias_or_thunk = cs->callee;
1834 while (alias_or_thunk->alias)
1835 alias_or_thunk = alias_or_thunk->get_alias_target ();
1836 if (alias_or_thunk->thunk.thunk_p)
1838 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1839 0));
1840 i = 1;
1842 else
1843 i = 0;
1845 for (; (i < args_count) && (i < parms_count); i++)
1847 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1848 struct ipcp_param_lattices *dest_plats;
1850 dest_plats = ipa_get_parm_lattices (callee_info, i);
1851 if (availability == AVAIL_INTERPOSABLE)
1852 ret |= set_all_contains_variable (dest_plats);
1853 else
1855 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1856 &dest_plats->itself);
1857 ret |= propagate_context_accross_jump_function (cs, jump_func, i,
1858 &dest_plats->ctxlat);
1859 ret |= propagate_alignment_accross_jump_function (cs, jump_func,
1860 dest_plats);
1861 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1862 dest_plats);
1865 for (; i < parms_count; i++)
1866 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1868 return ret;
1871 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1872 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
1873 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
1875 static tree
1876 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1877 vec<tree> known_csts,
1878 vec<ipa_polymorphic_call_context> known_contexts,
1879 vec<ipa_agg_jump_function_p> known_aggs,
1880 struct ipa_agg_replacement_value *agg_reps,
1881 bool *speculative)
1883 int param_index = ie->indirect_info->param_index;
1884 HOST_WIDE_INT anc_offset;
1885 tree t;
1886 tree target = NULL;
1888 *speculative = false;
1890 if (param_index == -1
1891 || known_csts.length () <= (unsigned int) param_index)
1892 return NULL_TREE;
1894 if (!ie->indirect_info->polymorphic)
1896 tree t;
1898 if (ie->indirect_info->agg_contents)
1900 if (agg_reps)
1902 t = NULL;
1903 while (agg_reps)
1905 if (agg_reps->index == param_index
1906 && agg_reps->offset == ie->indirect_info->offset
1907 && agg_reps->by_ref == ie->indirect_info->by_ref)
1909 t = agg_reps->value;
1910 break;
1912 agg_reps = agg_reps->next;
1915 else if (known_aggs.length () > (unsigned int) param_index)
1917 struct ipa_agg_jump_function *agg;
1918 agg = known_aggs[param_index];
1919 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1920 ie->indirect_info->by_ref);
1922 else
1923 t = NULL;
1925 else
1926 t = known_csts[param_index];
1928 if (t &&
1929 TREE_CODE (t) == ADDR_EXPR
1930 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1931 return TREE_OPERAND (t, 0);
1932 else
1933 return NULL_TREE;
1936 if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
1937 return NULL_TREE;
1939 gcc_assert (!ie->indirect_info->agg_contents);
1940 anc_offset = ie->indirect_info->offset;
1942 t = NULL;
1944 /* Try to work out value of virtual table pointer value in replacemnets. */
1945 if (!t && agg_reps && !ie->indirect_info->by_ref)
1947 while (agg_reps)
1949 if (agg_reps->index == param_index
1950 && agg_reps->offset == ie->indirect_info->offset
1951 && agg_reps->by_ref)
1953 t = agg_reps->value;
1954 break;
1956 agg_reps = agg_reps->next;
1960 /* Try to work out value of virtual table pointer value in known
1961 aggregate values. */
1962 if (!t && known_aggs.length () > (unsigned int) param_index
1963 && !ie->indirect_info->by_ref)
1965 struct ipa_agg_jump_function *agg;
1966 agg = known_aggs[param_index];
1967 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1968 true);
1971 /* If we found the virtual table pointer, lookup the target. */
1972 if (t)
1974 tree vtable;
1975 unsigned HOST_WIDE_INT offset;
1976 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
1978 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
1979 vtable, offset);
1980 if (target)
1982 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
1983 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
1984 || !possible_polymorphic_call_target_p
1985 (ie, cgraph_node::get (target)))
1986 target = ipa_impossible_devirt_target (ie, target);
1987 *speculative = ie->indirect_info->vptr_changed;
1988 if (!*speculative)
1989 return target;
1994 /* Do we know the constant value of pointer? */
1995 if (!t)
1996 t = known_csts[param_index];
1998 gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
2000 ipa_polymorphic_call_context context;
2001 if (known_contexts.length () > (unsigned int) param_index)
2003 context = known_contexts[param_index];
2004 context.offset_by (anc_offset);
2005 if (ie->indirect_info->vptr_changed)
2006 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2007 ie->indirect_info->otr_type);
2008 if (t)
2010 ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
2011 (t, ie->indirect_info->otr_type, anc_offset);
2012 if (!ctx2.useless_p ())
2013 context.combine_with (ctx2, ie->indirect_info->otr_type);
2016 else if (t)
2018 context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
2019 anc_offset);
2020 if (ie->indirect_info->vptr_changed)
2021 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2022 ie->indirect_info->otr_type);
2024 else
2025 return NULL_TREE;
2027 vec <cgraph_node *>targets;
2028 bool final;
2030 targets = possible_polymorphic_call_targets
2031 (ie->indirect_info->otr_type,
2032 ie->indirect_info->otr_token,
2033 context, &final);
2034 if (!final || targets.length () > 1)
2036 struct cgraph_node *node;
2037 if (*speculative)
2038 return target;
2039 if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
2040 || ie->speculative || !ie->maybe_hot_p ())
2041 return NULL;
2042 node = try_speculative_devirtualization (ie->indirect_info->otr_type,
2043 ie->indirect_info->otr_token,
2044 context);
2045 if (node)
2047 *speculative = true;
2048 target = node->decl;
2050 else
2051 return NULL;
2053 else
2055 *speculative = false;
2056 if (targets.length () == 1)
2057 target = targets[0]->decl;
2058 else
2059 target = ipa_impossible_devirt_target (ie, NULL_TREE);
2062 if (target && !possible_polymorphic_call_target_p (ie,
2063 cgraph_node::get (target)))
2064 target = ipa_impossible_devirt_target (ie, target);
2066 return target;
2070 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2071 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2072 return the destination. */
2074 tree
2075 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
2076 vec<tree> known_csts,
2077 vec<ipa_polymorphic_call_context> known_contexts,
2078 vec<ipa_agg_jump_function_p> known_aggs,
2079 bool *speculative)
2081 return ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2082 known_aggs, NULL, speculative);
2085 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2086 and KNOWN_CONTEXTS. */
2088 static int
2089 devirtualization_time_bonus (struct cgraph_node *node,
2090 vec<tree> known_csts,
2091 vec<ipa_polymorphic_call_context> known_contexts,
2092 vec<ipa_agg_jump_function_p> known_aggs)
2094 struct cgraph_edge *ie;
2095 int res = 0;
2097 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
2099 struct cgraph_node *callee;
2100 struct inline_summary *isummary;
2101 enum availability avail;
2102 tree target;
2103 bool speculative;
2105 target = ipa_get_indirect_edge_target (ie, known_csts, known_contexts,
2106 known_aggs, &speculative);
2107 if (!target)
2108 continue;
2110 /* Only bare minimum benefit for clearly un-inlineable targets. */
2111 res += 1;
2112 callee = cgraph_node::get (target);
2113 if (!callee || !callee->definition)
2114 continue;
2115 callee = callee->function_symbol (&avail);
2116 if (avail < AVAIL_AVAILABLE)
2117 continue;
2118 isummary = inline_summaries->get (callee);
2119 if (!isummary->inlinable)
2120 continue;
2122 /* FIXME: The values below need re-considering and perhaps also
2123 integrating into the cost metrics, at lest in some very basic way. */
2124 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
2125 res += 31 / ((int)speculative + 1);
2126 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
2127 res += 15 / ((int)speculative + 1);
2128 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
2129 || DECL_DECLARED_INLINE_P (callee->decl))
2130 res += 7 / ((int)speculative + 1);
2133 return res;
2136 /* Return time bonus incurred because of HINTS. */
2138 static int
2139 hint_time_bonus (inline_hints hints)
2141 int result = 0;
2142 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
2143 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
2144 if (hints & INLINE_HINT_array_index)
2145 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
2146 return result;
2149 /* If there is a reason to penalize the function described by INFO in the
2150 cloning goodness evaluation, do so. */
2152 static inline int64_t
2153 incorporate_penalties (ipa_node_params *info, int64_t evaluation)
2155 if (info->node_within_scc)
2156 evaluation = (evaluation
2157 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY))) / 100;
2159 if (info->node_calling_single_call)
2160 evaluation = (evaluation
2161 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY)))
2162 / 100;
2164 return evaluation;
2167 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2168 and SIZE_COST and with the sum of frequencies of incoming edges to the
2169 potential new clone in FREQUENCIES. */
2171 static bool
2172 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
2173 int freq_sum, gcov_type count_sum, int size_cost)
2175 if (time_benefit == 0
2176 || !opt_for_fn (node->decl, flag_ipa_cp_clone)
2177 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
2178 return false;
2180 gcc_assert (size_cost > 0);
2182 struct ipa_node_params *info = IPA_NODE_REF (node);
2183 if (max_count)
2185 int factor = (count_sum * 1000) / max_count;
2186 int64_t evaluation = (((int64_t) time_benefit * factor)
2187 / size_cost);
2188 evaluation = incorporate_penalties (info, evaluation);
2190 if (dump_file && (dump_flags & TDF_DETAILS))
2191 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2192 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
2193 "%s%s) -> evaluation: " "%" PRId64
2194 ", threshold: %i\n",
2195 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
2196 info->node_within_scc ? ", scc" : "",
2197 info->node_calling_single_call ? ", single_call" : "",
2198 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2200 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2202 else
2204 int64_t evaluation = (((int64_t) time_benefit * freq_sum)
2205 / size_cost);
2206 evaluation = incorporate_penalties (info, evaluation);
2208 if (dump_file && (dump_flags & TDF_DETAILS))
2209 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2210 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2211 "%" PRId64 ", threshold: %i\n",
2212 time_benefit, size_cost, freq_sum,
2213 info->node_within_scc ? ", scc" : "",
2214 info->node_calling_single_call ? ", single_call" : "",
2215 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2217 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2221 /* Return all context independent values from aggregate lattices in PLATS in a
2222 vector. Return NULL if there are none. */
2224 static vec<ipa_agg_jf_item, va_gc> *
2225 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
2227 vec<ipa_agg_jf_item, va_gc> *res = NULL;
2229 if (plats->aggs_bottom
2230 || plats->aggs_contain_variable
2231 || plats->aggs_count == 0)
2232 return NULL;
2234 for (struct ipcp_agg_lattice *aglat = plats->aggs;
2235 aglat;
2236 aglat = aglat->next)
2237 if (aglat->is_single_const ())
2239 struct ipa_agg_jf_item item;
2240 item.offset = aglat->offset;
2241 item.value = aglat->values->value;
2242 vec_safe_push (res, item);
2244 return res;
2247 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2248 populate them with values of parameters that are known independent of the
2249 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2250 non-NULL, the movement cost of all removable parameters will be stored in
2251 it. */
2253 static bool
2254 gather_context_independent_values (struct ipa_node_params *info,
2255 vec<tree> *known_csts,
2256 vec<ipa_polymorphic_call_context>
2257 *known_contexts,
2258 vec<ipa_agg_jump_function> *known_aggs,
2259 int *removable_params_cost)
2261 int i, count = ipa_get_param_count (info);
2262 bool ret = false;
2264 known_csts->create (0);
2265 known_contexts->create (0);
2266 known_csts->safe_grow_cleared (count);
2267 known_contexts->safe_grow_cleared (count);
2268 if (known_aggs)
2270 known_aggs->create (0);
2271 known_aggs->safe_grow_cleared (count);
2274 if (removable_params_cost)
2275 *removable_params_cost = 0;
2277 for (i = 0; i < count ; i++)
2279 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2280 ipcp_lattice<tree> *lat = &plats->itself;
2282 if (lat->is_single_const ())
2284 ipcp_value<tree> *val = lat->values;
2285 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2286 (*known_csts)[i] = val->value;
2287 if (removable_params_cost)
2288 *removable_params_cost
2289 += estimate_move_cost (TREE_TYPE (val->value), false);
2290 ret = true;
2292 else if (removable_params_cost
2293 && !ipa_is_param_used (info, i))
2294 *removable_params_cost
2295 += ipa_get_param_move_cost (info, i);
2297 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2298 if (ctxlat->is_single_const ())
2300 (*known_contexts)[i] = ctxlat->values->value;
2301 ret = true;
2304 if (known_aggs)
2306 vec<ipa_agg_jf_item, va_gc> *agg_items;
2307 struct ipa_agg_jump_function *ajf;
2309 agg_items = context_independent_aggregate_values (plats);
2310 ajf = &(*known_aggs)[i];
2311 ajf->items = agg_items;
2312 ajf->by_ref = plats->aggs_by_ref;
2313 ret |= agg_items != NULL;
2317 return ret;
2320 /* The current interface in ipa-inline-analysis requires a pointer vector.
2321 Create it.
2323 FIXME: That interface should be re-worked, this is slightly silly. Still,
2324 I'd like to discuss how to change it first and this demonstrates the
2325 issue. */
2327 static vec<ipa_agg_jump_function_p>
2328 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
2330 vec<ipa_agg_jump_function_p> ret;
2331 struct ipa_agg_jump_function *ajf;
2332 int i;
2334 ret.create (known_aggs.length ());
2335 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
2336 ret.quick_push (ajf);
2337 return ret;
2340 /* Perform time and size measurement of NODE with the context given in
2341 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2342 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2343 all context-independent removable parameters and EST_MOVE_COST of estimated
2344 movement of the considered parameter and store it into VAL. */
2346 static void
2347 perform_estimation_of_a_value (cgraph_node *node, vec<tree> known_csts,
2348 vec<ipa_polymorphic_call_context> known_contexts,
2349 vec<ipa_agg_jump_function_p> known_aggs_ptrs,
2350 int base_time, int removable_params_cost,
2351 int est_move_cost, ipcp_value_base *val)
2353 int time, size, time_benefit;
2354 inline_hints hints;
2356 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2357 known_aggs_ptrs, &size, &time,
2358 &hints);
2359 time_benefit = base_time - time
2360 + devirtualization_time_bonus (node, known_csts, known_contexts,
2361 known_aggs_ptrs)
2362 + hint_time_bonus (hints)
2363 + removable_params_cost + est_move_cost;
2365 gcc_checking_assert (size >=0);
2366 /* The inliner-heuristics based estimates may think that in certain
2367 contexts some functions do not have any size at all but we want
2368 all specializations to have at least a tiny cost, not least not to
2369 divide by zero. */
2370 if (size == 0)
2371 size = 1;
2373 val->local_time_benefit = time_benefit;
2374 val->local_size_cost = size;
2377 /* Iterate over known values of parameters of NODE and estimate the local
2378 effects in terms of time and size they have. */
2380 static void
2381 estimate_local_effects (struct cgraph_node *node)
2383 struct ipa_node_params *info = IPA_NODE_REF (node);
2384 int i, count = ipa_get_param_count (info);
2385 vec<tree> known_csts;
2386 vec<ipa_polymorphic_call_context> known_contexts;
2387 vec<ipa_agg_jump_function> known_aggs;
2388 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
2389 bool always_const;
2390 int base_time = inline_summaries->get (node)->time;
2391 int removable_params_cost;
2393 if (!count || !ipcp_versionable_function_p (node))
2394 return;
2396 if (dump_file && (dump_flags & TDF_DETAILS))
2397 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
2398 node->name (), node->order, base_time);
2400 always_const = gather_context_independent_values (info, &known_csts,
2401 &known_contexts, &known_aggs,
2402 &removable_params_cost);
2403 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
2404 if (always_const)
2406 struct caller_statistics stats;
2407 inline_hints hints;
2408 int time, size;
2410 init_caller_stats (&stats);
2411 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2412 false);
2413 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2414 known_aggs_ptrs, &size, &time, &hints);
2415 time -= devirtualization_time_bonus (node, known_csts, known_contexts,
2416 known_aggs_ptrs);
2417 time -= hint_time_bonus (hints);
2418 time -= removable_params_cost;
2419 size -= stats.n_calls * removable_params_cost;
2421 if (dump_file)
2422 fprintf (dump_file, " - context independent values, size: %i, "
2423 "time_benefit: %i\n", size, base_time - time);
2425 if (size <= 0
2426 || node->will_be_removed_from_program_if_no_direct_calls_p ())
2428 info->do_clone_for_all_contexts = true;
2429 base_time = time;
2431 if (dump_file)
2432 fprintf (dump_file, " Decided to specialize for all "
2433 "known contexts, code not going to grow.\n");
2435 else if (good_cloning_opportunity_p (node, base_time - time,
2436 stats.freq_sum, stats.count_sum,
2437 size))
2439 if (size + overall_size <= max_new_size)
2441 info->do_clone_for_all_contexts = true;
2442 base_time = time;
2443 overall_size += size;
2445 if (dump_file)
2446 fprintf (dump_file, " Decided to specialize for all "
2447 "known contexts, growth deemed beneficial.\n");
2449 else if (dump_file && (dump_flags & TDF_DETAILS))
2450 fprintf (dump_file, " Not cloning for all contexts because "
2451 "max_new_size would be reached with %li.\n",
2452 size + overall_size);
2456 for (i = 0; i < count ; i++)
2458 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2459 ipcp_lattice<tree> *lat = &plats->itself;
2460 ipcp_value<tree> *val;
2462 if (lat->bottom
2463 || !lat->values
2464 || known_csts[i])
2465 continue;
2467 for (val = lat->values; val; val = val->next)
2469 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2470 known_csts[i] = val->value;
2472 int emc = estimate_move_cost (TREE_TYPE (val->value), true);
2473 perform_estimation_of_a_value (node, known_csts, known_contexts,
2474 known_aggs_ptrs, base_time,
2475 removable_params_cost, emc, val);
2477 if (dump_file && (dump_flags & TDF_DETAILS))
2479 fprintf (dump_file, " - estimates for value ");
2480 print_ipcp_constant_value (dump_file, val->value);
2481 fprintf (dump_file, " for ");
2482 ipa_dump_param (dump_file, info, i);
2483 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2484 val->local_time_benefit, val->local_size_cost);
2487 known_csts[i] = NULL_TREE;
2490 for (i = 0; i < count; i++)
2492 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2494 if (!plats->virt_call)
2495 continue;
2497 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2498 ipcp_value<ipa_polymorphic_call_context> *val;
2500 if (ctxlat->bottom
2501 || !ctxlat->values
2502 || !known_contexts[i].useless_p ())
2503 continue;
2505 for (val = ctxlat->values; val; val = val->next)
2507 known_contexts[i] = val->value;
2508 perform_estimation_of_a_value (node, known_csts, known_contexts,
2509 known_aggs_ptrs, base_time,
2510 removable_params_cost, 0, val);
2512 if (dump_file && (dump_flags & TDF_DETAILS))
2514 fprintf (dump_file, " - estimates for polymorphic context ");
2515 print_ipcp_constant_value (dump_file, val->value);
2516 fprintf (dump_file, " for ");
2517 ipa_dump_param (dump_file, info, i);
2518 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2519 val->local_time_benefit, val->local_size_cost);
2522 known_contexts[i] = ipa_polymorphic_call_context ();
2525 for (i = 0; i < count ; i++)
2527 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2528 struct ipa_agg_jump_function *ajf;
2529 struct ipcp_agg_lattice *aglat;
2531 if (plats->aggs_bottom || !plats->aggs)
2532 continue;
2534 ajf = &known_aggs[i];
2535 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2537 ipcp_value<tree> *val;
2538 if (aglat->bottom || !aglat->values
2539 /* If the following is true, the one value is in known_aggs. */
2540 || (!plats->aggs_contain_variable
2541 && aglat->is_single_const ()))
2542 continue;
2544 for (val = aglat->values; val; val = val->next)
2546 struct ipa_agg_jf_item item;
2548 item.offset = aglat->offset;
2549 item.value = val->value;
2550 vec_safe_push (ajf->items, item);
2552 perform_estimation_of_a_value (node, known_csts, known_contexts,
2553 known_aggs_ptrs, base_time,
2554 removable_params_cost, 0, val);
2556 if (dump_file && (dump_flags & TDF_DETAILS))
2558 fprintf (dump_file, " - estimates for value ");
2559 print_ipcp_constant_value (dump_file, val->value);
2560 fprintf (dump_file, " for ");
2561 ipa_dump_param (dump_file, info, i);
2562 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2563 "]: time_benefit: %i, size: %i\n",
2564 plats->aggs_by_ref ? "ref " : "",
2565 aglat->offset,
2566 val->local_time_benefit, val->local_size_cost);
2569 ajf->items->pop ();
2574 for (i = 0; i < count ; i++)
2575 vec_free (known_aggs[i].items);
2577 known_csts.release ();
2578 known_contexts.release ();
2579 known_aggs.release ();
2580 known_aggs_ptrs.release ();
2584 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2585 topological sort of values. */
2587 template <typename valtype>
2588 void
2589 value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
2591 ipcp_value_source<valtype> *src;
2593 if (cur_val->dfs)
2594 return;
2596 dfs_counter++;
2597 cur_val->dfs = dfs_counter;
2598 cur_val->low_link = dfs_counter;
2600 cur_val->topo_next = stack;
2601 stack = cur_val;
2602 cur_val->on_stack = true;
2604 for (src = cur_val->sources; src; src = src->next)
2605 if (src->val)
2607 if (src->val->dfs == 0)
2609 add_val (src->val);
2610 if (src->val->low_link < cur_val->low_link)
2611 cur_val->low_link = src->val->low_link;
2613 else if (src->val->on_stack
2614 && src->val->dfs < cur_val->low_link)
2615 cur_val->low_link = src->val->dfs;
2618 if (cur_val->dfs == cur_val->low_link)
2620 ipcp_value<valtype> *v, *scc_list = NULL;
2624 v = stack;
2625 stack = v->topo_next;
2626 v->on_stack = false;
2628 v->scc_next = scc_list;
2629 scc_list = v;
2631 while (v != cur_val);
2633 cur_val->topo_next = values_topo;
2634 values_topo = cur_val;
2638 /* Add all values in lattices associated with NODE to the topological sort if
2639 they are not there yet. */
2641 static void
2642 add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
2644 struct ipa_node_params *info = IPA_NODE_REF (node);
2645 int i, count = ipa_get_param_count (info);
2647 for (i = 0; i < count ; i++)
2649 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2650 ipcp_lattice<tree> *lat = &plats->itself;
2651 struct ipcp_agg_lattice *aglat;
2653 if (!lat->bottom)
2655 ipcp_value<tree> *val;
2656 for (val = lat->values; val; val = val->next)
2657 topo->constants.add_val (val);
2660 if (!plats->aggs_bottom)
2661 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2662 if (!aglat->bottom)
2664 ipcp_value<tree> *val;
2665 for (val = aglat->values; val; val = val->next)
2666 topo->constants.add_val (val);
2669 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2670 if (!ctxlat->bottom)
2672 ipcp_value<ipa_polymorphic_call_context> *ctxval;
2673 for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
2674 topo->contexts.add_val (ctxval);
2679 /* One pass of constants propagation along the call graph edges, from callers
2680 to callees (requires topological ordering in TOPO), iterate over strongly
2681 connected components. */
2683 static void
2684 propagate_constants_topo (struct ipa_topo_info *topo)
2686 int i;
2688 for (i = topo->nnodes - 1; i >= 0; i--)
2690 unsigned j;
2691 struct cgraph_node *v, *node = topo->order[i];
2692 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
2694 /* First, iteratively propagate within the strongly connected component
2695 until all lattices stabilize. */
2696 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2697 if (v->has_gimple_body_p ())
2698 push_node_to_stack (topo, v);
2700 v = pop_node_from_stack (topo);
2701 while (v)
2703 struct cgraph_edge *cs;
2705 for (cs = v->callees; cs; cs = cs->next_callee)
2706 if (ipa_edge_within_scc (cs))
2708 IPA_NODE_REF (v)->node_within_scc = true;
2709 if (propagate_constants_accross_call (cs))
2710 push_node_to_stack (topo, cs->callee->function_symbol ());
2712 v = pop_node_from_stack (topo);
2715 /* Afterwards, propagate along edges leading out of the SCC, calculates
2716 the local effects of the discovered constants and all valid values to
2717 their topological sort. */
2718 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2719 if (v->has_gimple_body_p ())
2721 struct cgraph_edge *cs;
2723 estimate_local_effects (v);
2724 add_all_node_vals_to_toposort (v, topo);
2725 for (cs = v->callees; cs; cs = cs->next_callee)
2726 if (!ipa_edge_within_scc (cs))
2727 propagate_constants_accross_call (cs);
2729 cycle_nodes.release ();
2734 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2735 the bigger one if otherwise. */
2737 static int
2738 safe_add (int a, int b)
2740 if (a > INT_MAX/2 || b > INT_MAX/2)
2741 return a > b ? a : b;
2742 else
2743 return a + b;
2747 /* Propagate the estimated effects of individual values along the topological
2748 from the dependent values to those they depend on. */
2750 template <typename valtype>
2751 void
2752 value_topo_info<valtype>::propagate_effects ()
2754 ipcp_value<valtype> *base;
2756 for (base = values_topo; base; base = base->topo_next)
2758 ipcp_value_source<valtype> *src;
2759 ipcp_value<valtype> *val;
2760 int time = 0, size = 0;
2762 for (val = base; val; val = val->scc_next)
2764 time = safe_add (time,
2765 val->local_time_benefit + val->prop_time_benefit);
2766 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2769 for (val = base; val; val = val->scc_next)
2770 for (src = val->sources; src; src = src->next)
2771 if (src->val
2772 && src->cs->maybe_hot_p ())
2774 src->val->prop_time_benefit = safe_add (time,
2775 src->val->prop_time_benefit);
2776 src->val->prop_size_cost = safe_add (size,
2777 src->val->prop_size_cost);
2783 /* Propagate constants, polymorphic contexts and their effects from the
2784 summaries interprocedurally. */
2786 static void
2787 ipcp_propagate_stage (struct ipa_topo_info *topo)
2789 struct cgraph_node *node;
2791 if (dump_file)
2792 fprintf (dump_file, "\n Propagating constants:\n\n");
2794 if (in_lto_p)
2795 ipa_update_after_lto_read ();
2798 FOR_EACH_DEFINED_FUNCTION (node)
2800 struct ipa_node_params *info = IPA_NODE_REF (node);
2802 determine_versionability (node);
2803 if (node->has_gimple_body_p ())
2805 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2806 ipa_get_param_count (info));
2807 initialize_node_lattices (node);
2809 if (node->definition && !node->alias)
2810 overall_size += inline_summaries->get (node)->self_size;
2811 if (node->count > max_count)
2812 max_count = node->count;
2815 max_new_size = overall_size;
2816 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2817 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2818 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2820 if (dump_file)
2821 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2822 overall_size, max_new_size);
2824 propagate_constants_topo (topo);
2825 #ifdef ENABLE_CHECKING
2826 ipcp_verify_propagated_values ();
2827 #endif
2828 topo->constants.propagate_effects ();
2829 topo->contexts.propagate_effects ();
2831 if (dump_file)
2833 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2834 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2838 /* Discover newly direct outgoing edges from NODE which is a new clone with
2839 known KNOWN_CSTS and make them direct. */
2841 static void
2842 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2843 vec<tree> known_csts,
2844 vec<ipa_polymorphic_call_context>
2845 known_contexts,
2846 struct ipa_agg_replacement_value *aggvals)
2848 struct cgraph_edge *ie, *next_ie;
2849 bool found = false;
2851 for (ie = node->indirect_calls; ie; ie = next_ie)
2853 tree target;
2854 bool speculative;
2856 next_ie = ie->next_callee;
2857 target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2858 vNULL, aggvals, &speculative);
2859 if (target)
2861 bool agg_contents = ie->indirect_info->agg_contents;
2862 bool polymorphic = ie->indirect_info->polymorphic;
2863 int param_index = ie->indirect_info->param_index;
2864 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
2865 speculative);
2866 found = true;
2868 if (cs && !agg_contents && !polymorphic)
2870 struct ipa_node_params *info = IPA_NODE_REF (node);
2871 int c = ipa_get_controlled_uses (info, param_index);
2872 if (c != IPA_UNDESCRIBED_USE)
2874 struct ipa_ref *to_del;
2876 c--;
2877 ipa_set_controlled_uses (info, param_index, c);
2878 if (dump_file && (dump_flags & TDF_DETAILS))
2879 fprintf (dump_file, " controlled uses count of param "
2880 "%i bumped down to %i\n", param_index, c);
2881 if (c == 0
2882 && (to_del = node->find_reference (cs->callee, NULL, 0)))
2884 if (dump_file && (dump_flags & TDF_DETAILS))
2885 fprintf (dump_file, " and even removing its "
2886 "cloning-created reference\n");
2887 to_del->remove_reference ();
2893 /* Turning calls to direct calls will improve overall summary. */
2894 if (found)
2895 inline_update_overall_summary (node);
2898 /* Vector of pointers which for linked lists of clones of an original crgaph
2899 edge. */
2901 static vec<cgraph_edge *> next_edge_clone;
2902 static vec<cgraph_edge *> prev_edge_clone;
2904 static inline void
2905 grow_edge_clone_vectors (void)
2907 if (next_edge_clone.length ()
2908 <= (unsigned) symtab->edges_max_uid)
2909 next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2910 if (prev_edge_clone.length ()
2911 <= (unsigned) symtab->edges_max_uid)
2912 prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2915 /* Edge duplication hook to grow the appropriate linked list in
2916 next_edge_clone. */
2918 static void
2919 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2920 void *)
2922 grow_edge_clone_vectors ();
2924 struct cgraph_edge *old_next = next_edge_clone[src->uid];
2925 if (old_next)
2926 prev_edge_clone[old_next->uid] = dst;
2927 prev_edge_clone[dst->uid] = src;
2929 next_edge_clone[dst->uid] = old_next;
2930 next_edge_clone[src->uid] = dst;
2933 /* Hook that is called by cgraph.c when an edge is removed. */
2935 static void
2936 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
2938 grow_edge_clone_vectors ();
2940 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
2941 struct cgraph_edge *next = next_edge_clone[cs->uid];
2942 if (prev)
2943 next_edge_clone[prev->uid] = next;
2944 if (next)
2945 prev_edge_clone[next->uid] = prev;
2948 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2949 parameter with the given INDEX. */
2951 static tree
2952 get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset,
2953 int index)
2955 struct ipa_agg_replacement_value *aggval;
2957 aggval = ipa_get_agg_replacements_for_node (node);
2958 while (aggval)
2960 if (aggval->offset == offset
2961 && aggval->index == index)
2962 return aggval->value;
2963 aggval = aggval->next;
2965 return NULL_TREE;
2968 /* Return true is NODE is DEST or its clone for all contexts. */
2970 static bool
2971 same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest)
2973 if (node == dest)
2974 return true;
2976 struct ipa_node_params *info = IPA_NODE_REF (node);
2977 return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
2980 /* Return true if edge CS does bring about the value described by SRC to node
2981 DEST or its clone for all contexts. */
2983 static bool
2984 cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
2985 cgraph_node *dest)
2987 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2988 enum availability availability;
2989 cgraph_node *real_dest = cs->callee->function_symbol (&availability);
2991 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
2992 || availability <= AVAIL_INTERPOSABLE
2993 || caller_info->node_dead)
2994 return false;
2995 if (!src->val)
2996 return true;
2998 if (caller_info->ipcp_orig_node)
3000 tree t;
3001 if (src->offset == -1)
3002 t = caller_info->known_csts[src->index];
3003 else
3004 t = get_clone_agg_value (cs->caller, src->offset, src->index);
3005 return (t != NULL_TREE
3006 && values_equal_for_ipcp_p (src->val->value, t));
3008 else
3010 struct ipcp_agg_lattice *aglat;
3011 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3012 src->index);
3013 if (src->offset == -1)
3014 return (plats->itself.is_single_const ()
3015 && values_equal_for_ipcp_p (src->val->value,
3016 plats->itself.values->value));
3017 else
3019 if (plats->aggs_bottom || plats->aggs_contain_variable)
3020 return false;
3021 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3022 if (aglat->offset == src->offset)
3023 return (aglat->is_single_const ()
3024 && values_equal_for_ipcp_p (src->val->value,
3025 aglat->values->value));
3027 return false;
3031 /* Return true if edge CS does bring about the value described by SRC to node
3032 DEST or its clone for all contexts. */
3034 static bool
3035 cgraph_edge_brings_value_p (cgraph_edge *cs,
3036 ipcp_value_source<ipa_polymorphic_call_context> *src,
3037 cgraph_node *dest)
3039 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3040 cgraph_node *real_dest = cs->callee->function_symbol ();
3042 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3043 || caller_info->node_dead)
3044 return false;
3045 if (!src->val)
3046 return true;
3048 if (caller_info->ipcp_orig_node)
3049 return (caller_info->known_contexts.length () > (unsigned) src->index)
3050 && values_equal_for_ipcp_p (src->val->value,
3051 caller_info->known_contexts[src->index]);
3053 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3054 src->index);
3055 return plats->ctxlat.is_single_const ()
3056 && values_equal_for_ipcp_p (src->val->value,
3057 plats->ctxlat.values->value);
3060 /* Get the next clone in the linked list of clones of an edge. */
3062 static inline struct cgraph_edge *
3063 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
3065 return next_edge_clone[cs->uid];
3068 /* Given VAL that is intended for DEST, iterate over all its sources and if
3069 they still hold, add their edge frequency and their number into *FREQUENCY
3070 and *CALLER_COUNT respectively. */
3072 template <typename valtype>
3073 static bool
3074 get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
3075 int *freq_sum,
3076 gcov_type *count_sum, int *caller_count)
3078 ipcp_value_source<valtype> *src;
3079 int freq = 0, count = 0;
3080 gcov_type cnt = 0;
3081 bool hot = false;
3083 for (src = val->sources; src; src = src->next)
3085 struct cgraph_edge *cs = src->cs;
3086 while (cs)
3088 if (cgraph_edge_brings_value_p (cs, src, dest))
3090 count++;
3091 freq += cs->frequency;
3092 cnt += cs->count;
3093 hot |= cs->maybe_hot_p ();
3095 cs = get_next_cgraph_edge_clone (cs);
3099 *freq_sum = freq;
3100 *count_sum = cnt;
3101 *caller_count = count;
3102 return hot;
3105 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3106 is assumed their number is known and equal to CALLER_COUNT. */
3108 template <typename valtype>
3109 static vec<cgraph_edge *>
3110 gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
3111 int caller_count)
3113 ipcp_value_source<valtype> *src;
3114 vec<cgraph_edge *> ret;
3116 ret.create (caller_count);
3117 for (src = val->sources; src; src = src->next)
3119 struct cgraph_edge *cs = src->cs;
3120 while (cs)
3122 if (cgraph_edge_brings_value_p (cs, src, dest))
3123 ret.quick_push (cs);
3124 cs = get_next_cgraph_edge_clone (cs);
3128 return ret;
3131 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3132 Return it or NULL if for some reason it cannot be created. */
3134 static struct ipa_replace_map *
3135 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
3137 struct ipa_replace_map *replace_map;
3140 replace_map = ggc_alloc<ipa_replace_map> ();
3141 if (dump_file)
3143 fprintf (dump_file, " replacing ");
3144 ipa_dump_param (dump_file, info, parm_num);
3146 fprintf (dump_file, " with const ");
3147 print_generic_expr (dump_file, value, 0);
3148 fprintf (dump_file, "\n");
3150 replace_map->old_tree = NULL;
3151 replace_map->parm_num = parm_num;
3152 replace_map->new_tree = value;
3153 replace_map->replace_p = true;
3154 replace_map->ref_p = false;
3156 return replace_map;
3159 /* Dump new profiling counts */
3161 static void
3162 dump_profile_updates (struct cgraph_node *orig_node,
3163 struct cgraph_node *new_node)
3165 struct cgraph_edge *cs;
3167 fprintf (dump_file, " setting count of the specialized node to "
3168 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
3169 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3170 fprintf (dump_file, " edge to %s has count "
3171 HOST_WIDE_INT_PRINT_DEC "\n",
3172 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3174 fprintf (dump_file, " setting count of the original node to "
3175 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
3176 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3177 fprintf (dump_file, " edge to %s is left with "
3178 HOST_WIDE_INT_PRINT_DEC "\n",
3179 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3182 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3183 their profile information to reflect this. */
3185 static void
3186 update_profiling_info (struct cgraph_node *orig_node,
3187 struct cgraph_node *new_node)
3189 struct cgraph_edge *cs;
3190 struct caller_statistics stats;
3191 gcov_type new_sum, orig_sum;
3192 gcov_type remainder, orig_node_count = orig_node->count;
3194 if (orig_node_count == 0)
3195 return;
3197 init_caller_stats (&stats);
3198 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3199 false);
3200 orig_sum = stats.count_sum;
3201 init_caller_stats (&stats);
3202 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3203 false);
3204 new_sum = stats.count_sum;
3206 if (orig_node_count < orig_sum + new_sum)
3208 if (dump_file)
3209 fprintf (dump_file, " Problem: node %s/%i has too low count "
3210 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
3211 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
3212 orig_node->name (), orig_node->order,
3213 (HOST_WIDE_INT) orig_node_count,
3214 (HOST_WIDE_INT) (orig_sum + new_sum));
3216 orig_node_count = (orig_sum + new_sum) * 12 / 10;
3217 if (dump_file)
3218 fprintf (dump_file, " proceeding by pretending it was "
3219 HOST_WIDE_INT_PRINT_DEC "\n",
3220 (HOST_WIDE_INT) orig_node_count);
3223 new_node->count = new_sum;
3224 remainder = orig_node_count - new_sum;
3225 orig_node->count = remainder;
3227 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3228 if (cs->frequency)
3229 cs->count = apply_probability (cs->count,
3230 GCOV_COMPUTE_SCALE (new_sum,
3231 orig_node_count));
3232 else
3233 cs->count = 0;
3235 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3236 cs->count = apply_probability (cs->count,
3237 GCOV_COMPUTE_SCALE (remainder,
3238 orig_node_count));
3240 if (dump_file)
3241 dump_profile_updates (orig_node, new_node);
3244 /* Update the respective profile of specialized NEW_NODE and the original
3245 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3246 have been redirected to the specialized version. */
3248 static void
3249 update_specialized_profile (struct cgraph_node *new_node,
3250 struct cgraph_node *orig_node,
3251 gcov_type redirected_sum)
3253 struct cgraph_edge *cs;
3254 gcov_type new_node_count, orig_node_count = orig_node->count;
3256 if (dump_file)
3257 fprintf (dump_file, " the sum of counts of redirected edges is "
3258 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
3259 if (orig_node_count == 0)
3260 return;
3262 gcc_assert (orig_node_count >= redirected_sum);
3264 new_node_count = new_node->count;
3265 new_node->count += redirected_sum;
3266 orig_node->count -= redirected_sum;
3268 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3269 if (cs->frequency)
3270 cs->count += apply_probability (cs->count,
3271 GCOV_COMPUTE_SCALE (redirected_sum,
3272 new_node_count));
3273 else
3274 cs->count = 0;
3276 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3278 gcov_type dec = apply_probability (cs->count,
3279 GCOV_COMPUTE_SCALE (redirected_sum,
3280 orig_node_count));
3281 if (dec < cs->count)
3282 cs->count -= dec;
3283 else
3284 cs->count = 0;
3287 if (dump_file)
3288 dump_profile_updates (orig_node, new_node);
3291 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3292 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3293 redirect all edges in CALLERS to it. */
3295 static struct cgraph_node *
3296 create_specialized_node (struct cgraph_node *node,
3297 vec<tree> known_csts,
3298 vec<ipa_polymorphic_call_context> known_contexts,
3299 struct ipa_agg_replacement_value *aggvals,
3300 vec<cgraph_edge *> callers)
3302 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
3303 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
3304 struct ipa_agg_replacement_value *av;
3305 struct cgraph_node *new_node;
3306 int i, count = ipa_get_param_count (info);
3307 bitmap args_to_skip;
3309 gcc_assert (!info->ipcp_orig_node);
3311 if (node->local.can_change_signature)
3313 args_to_skip = BITMAP_GGC_ALLOC ();
3314 for (i = 0; i < count; i++)
3316 tree t = known_csts[i];
3318 if (t || !ipa_is_param_used (info, i))
3319 bitmap_set_bit (args_to_skip, i);
3322 else
3324 args_to_skip = NULL;
3325 if (dump_file && (dump_flags & TDF_DETAILS))
3326 fprintf (dump_file, " cannot change function signature\n");
3329 for (i = 0; i < count ; i++)
3331 tree t = known_csts[i];
3332 if (t)
3334 struct ipa_replace_map *replace_map;
3336 gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
3337 replace_map = get_replacement_map (info, t, i);
3338 if (replace_map)
3339 vec_safe_push (replace_trees, replace_map);
3343 new_node = node->create_virtual_clone (callers, replace_trees,
3344 args_to_skip, "constprop");
3345 ipa_set_node_agg_value_chain (new_node, aggvals);
3346 for (av = aggvals; av; av = av->next)
3347 new_node->maybe_create_reference (av->value, IPA_REF_ADDR, NULL);
3349 if (dump_file && (dump_flags & TDF_DETAILS))
3351 fprintf (dump_file, " the new node is %s/%i.\n",
3352 new_node->name (), new_node->order);
3353 if (known_contexts.exists ())
3355 for (i = 0; i < count ; i++)
3356 if (!known_contexts[i].useless_p ())
3358 fprintf (dump_file, " known ctx %i is ", i);
3359 known_contexts[i].dump (dump_file);
3362 if (aggvals)
3363 ipa_dump_agg_replacement_values (dump_file, aggvals);
3365 ipa_check_create_node_params ();
3366 update_profiling_info (node, new_node);
3367 new_info = IPA_NODE_REF (new_node);
3368 new_info->ipcp_orig_node = node;
3369 new_info->known_csts = known_csts;
3370 new_info->known_contexts = known_contexts;
3372 ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts, aggvals);
3374 callers.release ();
3375 return new_node;
3378 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3379 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3381 static void
3382 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
3383 vec<tree> known_csts,
3384 vec<cgraph_edge *> callers)
3386 struct ipa_node_params *info = IPA_NODE_REF (node);
3387 int i, count = ipa_get_param_count (info);
3389 for (i = 0; i < count ; i++)
3391 struct cgraph_edge *cs;
3392 tree newval = NULL_TREE;
3393 int j;
3394 bool first = true;
3396 if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
3397 continue;
3399 FOR_EACH_VEC_ELT (callers, j, cs)
3401 struct ipa_jump_func *jump_func;
3402 tree t;
3404 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3406 newval = NULL_TREE;
3407 break;
3409 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
3410 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
3411 if (!t
3412 || (newval
3413 && !values_equal_for_ipcp_p (t, newval))
3414 || (!first && !newval))
3416 newval = NULL_TREE;
3417 break;
3419 else
3420 newval = t;
3421 first = false;
3424 if (newval)
3426 if (dump_file && (dump_flags & TDF_DETAILS))
3428 fprintf (dump_file, " adding an extra known scalar value ");
3429 print_ipcp_constant_value (dump_file, newval);
3430 fprintf (dump_file, " for ");
3431 ipa_dump_param (dump_file, info, i);
3432 fprintf (dump_file, "\n");
3435 known_csts[i] = newval;
3440 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3441 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3442 CALLERS. */
3444 static void
3445 find_more_contexts_for_caller_subset (cgraph_node *node,
3446 vec<ipa_polymorphic_call_context>
3447 *known_contexts,
3448 vec<cgraph_edge *> callers)
3450 ipa_node_params *info = IPA_NODE_REF (node);
3451 int i, count = ipa_get_param_count (info);
3453 for (i = 0; i < count ; i++)
3455 cgraph_edge *cs;
3457 if (ipa_get_poly_ctx_lat (info, i)->bottom
3458 || (known_contexts->exists ()
3459 && !(*known_contexts)[i].useless_p ()))
3460 continue;
3462 ipa_polymorphic_call_context newval;
3463 bool first = true;
3464 int j;
3466 FOR_EACH_VEC_ELT (callers, j, cs)
3468 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3469 return;
3470 ipa_jump_func *jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs),
3472 ipa_polymorphic_call_context ctx;
3473 ctx = ipa_context_from_jfunc (IPA_NODE_REF (cs->caller), cs, i,
3474 jfunc);
3475 if (first)
3477 newval = ctx;
3478 first = false;
3480 else
3481 newval.meet_with (ctx);
3482 if (newval.useless_p ())
3483 break;
3486 if (!newval.useless_p ())
3488 if (dump_file && (dump_flags & TDF_DETAILS))
3490 fprintf (dump_file, " adding an extra known polymorphic "
3491 "context ");
3492 print_ipcp_constant_value (dump_file, newval);
3493 fprintf (dump_file, " for ");
3494 ipa_dump_param (dump_file, info, i);
3495 fprintf (dump_file, "\n");
3498 if (!known_contexts->exists ())
3499 known_contexts->safe_grow_cleared (ipa_get_param_count (info));
3500 (*known_contexts)[i] = newval;
3506 /* Go through PLATS and create a vector of values consisting of values and
3507 offsets (minus OFFSET) of lattices that contain only a single value. */
3509 static vec<ipa_agg_jf_item>
3510 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
3512 vec<ipa_agg_jf_item> res = vNULL;
3514 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3515 return vNULL;
3517 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3518 if (aglat->is_single_const ())
3520 struct ipa_agg_jf_item ti;
3521 ti.offset = aglat->offset - offset;
3522 ti.value = aglat->values->value;
3523 res.safe_push (ti);
3525 return res;
3528 /* Intersect all values in INTER with single value lattices in PLATS (while
3529 subtracting OFFSET). */
3531 static void
3532 intersect_with_plats (struct ipcp_param_lattices *plats,
3533 vec<ipa_agg_jf_item> *inter,
3534 HOST_WIDE_INT offset)
3536 struct ipcp_agg_lattice *aglat;
3537 struct ipa_agg_jf_item *item;
3538 int k;
3540 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3542 inter->release ();
3543 return;
3546 aglat = plats->aggs;
3547 FOR_EACH_VEC_ELT (*inter, k, item)
3549 bool found = false;
3550 if (!item->value)
3551 continue;
3552 while (aglat)
3554 if (aglat->offset - offset > item->offset)
3555 break;
3556 if (aglat->offset - offset == item->offset)
3558 gcc_checking_assert (item->value);
3559 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
3560 found = true;
3561 break;
3563 aglat = aglat->next;
3565 if (!found)
3566 item->value = NULL_TREE;
3570 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
3571 vector result while subtracting OFFSET from the individual value offsets. */
3573 static vec<ipa_agg_jf_item>
3574 agg_replacements_to_vector (struct cgraph_node *node, int index,
3575 HOST_WIDE_INT offset)
3577 struct ipa_agg_replacement_value *av;
3578 vec<ipa_agg_jf_item> res = vNULL;
3580 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
3581 if (av->index == index
3582 && (av->offset - offset) >= 0)
3584 struct ipa_agg_jf_item item;
3585 gcc_checking_assert (av->value);
3586 item.offset = av->offset - offset;
3587 item.value = av->value;
3588 res.safe_push (item);
3591 return res;
3594 /* Intersect all values in INTER with those that we have already scheduled to
3595 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
3596 (while subtracting OFFSET). */
3598 static void
3599 intersect_with_agg_replacements (struct cgraph_node *node, int index,
3600 vec<ipa_agg_jf_item> *inter,
3601 HOST_WIDE_INT offset)
3603 struct ipa_agg_replacement_value *srcvals;
3604 struct ipa_agg_jf_item *item;
3605 int i;
3607 srcvals = ipa_get_agg_replacements_for_node (node);
3608 if (!srcvals)
3610 inter->release ();
3611 return;
3614 FOR_EACH_VEC_ELT (*inter, i, item)
3616 struct ipa_agg_replacement_value *av;
3617 bool found = false;
3618 if (!item->value)
3619 continue;
3620 for (av = srcvals; av; av = av->next)
3622 gcc_checking_assert (av->value);
3623 if (av->index == index
3624 && av->offset - offset == item->offset)
3626 if (values_equal_for_ipcp_p (item->value, av->value))
3627 found = true;
3628 break;
3631 if (!found)
3632 item->value = NULL_TREE;
3636 /* Intersect values in INTER with aggregate values that come along edge CS to
3637 parameter number INDEX and return it. If INTER does not actually exist yet,
3638 copy all incoming values to it. If we determine we ended up with no values
3639 whatsoever, return a released vector. */
3641 static vec<ipa_agg_jf_item>
3642 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3643 vec<ipa_agg_jf_item> inter)
3645 struct ipa_jump_func *jfunc;
3646 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3647 if (jfunc->type == IPA_JF_PASS_THROUGH
3648 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3650 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3651 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3653 if (caller_info->ipcp_orig_node)
3655 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3656 struct ipcp_param_lattices *orig_plats;
3657 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3658 src_idx);
3659 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3661 if (!inter.exists ())
3662 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3663 else
3664 intersect_with_agg_replacements (cs->caller, src_idx,
3665 &inter, 0);
3667 else
3669 inter.release ();
3670 return vNULL;
3673 else
3675 struct ipcp_param_lattices *src_plats;
3676 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3677 if (agg_pass_through_permissible_p (src_plats, jfunc))
3679 /* Currently we do not produce clobber aggregate jump
3680 functions, adjust when we do. */
3681 gcc_checking_assert (!jfunc->agg.items);
3682 if (!inter.exists ())
3683 inter = copy_plats_to_inter (src_plats, 0);
3684 else
3685 intersect_with_plats (src_plats, &inter, 0);
3687 else
3689 inter.release ();
3690 return vNULL;
3694 else if (jfunc->type == IPA_JF_ANCESTOR
3695 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3697 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3698 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3699 struct ipcp_param_lattices *src_plats;
3700 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3702 if (caller_info->ipcp_orig_node)
3704 if (!inter.exists ())
3705 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3706 else
3707 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3708 delta);
3710 else
3712 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3713 /* Currently we do not produce clobber aggregate jump
3714 functions, adjust when we do. */
3715 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3716 if (!inter.exists ())
3717 inter = copy_plats_to_inter (src_plats, delta);
3718 else
3719 intersect_with_plats (src_plats, &inter, delta);
3722 else if (jfunc->agg.items)
3724 struct ipa_agg_jf_item *item;
3725 int k;
3727 if (!inter.exists ())
3728 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3729 inter.safe_push ((*jfunc->agg.items)[i]);
3730 else
3731 FOR_EACH_VEC_ELT (inter, k, item)
3733 int l = 0;
3734 bool found = false;;
3736 if (!item->value)
3737 continue;
3739 while ((unsigned) l < jfunc->agg.items->length ())
3741 struct ipa_agg_jf_item *ti;
3742 ti = &(*jfunc->agg.items)[l];
3743 if (ti->offset > item->offset)
3744 break;
3745 if (ti->offset == item->offset)
3747 gcc_checking_assert (ti->value);
3748 if (values_equal_for_ipcp_p (item->value,
3749 ti->value))
3750 found = true;
3751 break;
3753 l++;
3755 if (!found)
3756 item->value = NULL;
3759 else
3761 inter.release ();
3762 return vec<ipa_agg_jf_item>();
3764 return inter;
3767 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3768 from all of them. */
3770 static struct ipa_agg_replacement_value *
3771 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3772 vec<cgraph_edge *> callers)
3774 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3775 struct ipa_agg_replacement_value *res;
3776 struct ipa_agg_replacement_value **tail = &res;
3777 struct cgraph_edge *cs;
3778 int i, j, count = ipa_get_param_count (dest_info);
3780 FOR_EACH_VEC_ELT (callers, j, cs)
3782 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3783 if (c < count)
3784 count = c;
3787 for (i = 0; i < count ; i++)
3789 struct cgraph_edge *cs;
3790 vec<ipa_agg_jf_item> inter = vNULL;
3791 struct ipa_agg_jf_item *item;
3792 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3793 int j;
3795 /* Among other things, the following check should deal with all by_ref
3796 mismatches. */
3797 if (plats->aggs_bottom)
3798 continue;
3800 FOR_EACH_VEC_ELT (callers, j, cs)
3802 inter = intersect_aggregates_with_edge (cs, i, inter);
3804 if (!inter.exists ())
3805 goto next_param;
3808 FOR_EACH_VEC_ELT (inter, j, item)
3810 struct ipa_agg_replacement_value *v;
3812 if (!item->value)
3813 continue;
3815 v = ggc_alloc<ipa_agg_replacement_value> ();
3816 v->index = i;
3817 v->offset = item->offset;
3818 v->value = item->value;
3819 v->by_ref = plats->aggs_by_ref;
3820 *tail = v;
3821 tail = &v->next;
3824 next_param:
3825 if (inter.exists ())
3826 inter.release ();
3828 *tail = NULL;
3829 return res;
3832 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3834 static struct ipa_agg_replacement_value *
3835 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3837 struct ipa_agg_replacement_value *res;
3838 struct ipa_agg_replacement_value **tail = &res;
3839 struct ipa_agg_jump_function *aggjf;
3840 struct ipa_agg_jf_item *item;
3841 int i, j;
3843 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3844 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3846 struct ipa_agg_replacement_value *v;
3847 v = ggc_alloc<ipa_agg_replacement_value> ();
3848 v->index = i;
3849 v->offset = item->offset;
3850 v->value = item->value;
3851 v->by_ref = aggjf->by_ref;
3852 *tail = v;
3853 tail = &v->next;
3855 *tail = NULL;
3856 return res;
3859 /* Determine whether CS also brings all scalar values that the NODE is
3860 specialized for. */
3862 static bool
3863 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3864 struct cgraph_node *node)
3866 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3867 int count = ipa_get_param_count (dest_info);
3868 struct ipa_node_params *caller_info;
3869 struct ipa_edge_args *args;
3870 int i;
3872 caller_info = IPA_NODE_REF (cs->caller);
3873 args = IPA_EDGE_REF (cs);
3874 for (i = 0; i < count; i++)
3876 struct ipa_jump_func *jump_func;
3877 tree val, t;
3879 val = dest_info->known_csts[i];
3880 if (!val)
3881 continue;
3883 if (i >= ipa_get_cs_argument_count (args))
3884 return false;
3885 jump_func = ipa_get_ith_jump_func (args, i);
3886 t = ipa_value_from_jfunc (caller_info, jump_func);
3887 if (!t || !values_equal_for_ipcp_p (val, t))
3888 return false;
3890 return true;
3893 /* Determine whether CS also brings all aggregate values that NODE is
3894 specialized for. */
3895 static bool
3896 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3897 struct cgraph_node *node)
3899 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3900 struct ipa_node_params *orig_node_info;
3901 struct ipa_agg_replacement_value *aggval;
3902 int i, ec, count;
3904 aggval = ipa_get_agg_replacements_for_node (node);
3905 if (!aggval)
3906 return true;
3908 count = ipa_get_param_count (IPA_NODE_REF (node));
3909 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3910 if (ec < count)
3911 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3912 if (aggval->index >= ec)
3913 return false;
3915 orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node);
3916 if (orig_caller_info->ipcp_orig_node)
3917 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3919 for (i = 0; i < count; i++)
3921 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
3922 struct ipcp_param_lattices *plats;
3923 bool interesting = false;
3924 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3925 if (aggval->index == i)
3927 interesting = true;
3928 break;
3930 if (!interesting)
3931 continue;
3933 plats = ipa_get_parm_lattices (orig_node_info, aggval->index);
3934 if (plats->aggs_bottom)
3935 return false;
3937 values = intersect_aggregates_with_edge (cs, i, values);
3938 if (!values.exists ())
3939 return false;
3941 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3942 if (aggval->index == i)
3944 struct ipa_agg_jf_item *item;
3945 int j;
3946 bool found = false;
3947 FOR_EACH_VEC_ELT (values, j, item)
3948 if (item->value
3949 && item->offset == av->offset
3950 && values_equal_for_ipcp_p (item->value, av->value))
3952 found = true;
3953 break;
3955 if (!found)
3957 values.release ();
3958 return false;
3962 return true;
3965 /* Given an original NODE and a VAL for which we have already created a
3966 specialized clone, look whether there are incoming edges that still lead
3967 into the old node but now also bring the requested value and also conform to
3968 all other criteria such that they can be redirected the the special node.
3969 This function can therefore redirect the final edge in a SCC. */
3971 template <typename valtype>
3972 static void
3973 perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
3975 ipcp_value_source<valtype> *src;
3976 gcov_type redirected_sum = 0;
3978 for (src = val->sources; src; src = src->next)
3980 struct cgraph_edge *cs = src->cs;
3981 while (cs)
3983 if (cgraph_edge_brings_value_p (cs, src, node)
3984 && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3985 && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
3987 if (dump_file)
3988 fprintf (dump_file, " - adding an extra caller %s/%i"
3989 " of %s/%i\n",
3990 xstrdup_for_dump (cs->caller->name ()),
3991 cs->caller->order,
3992 xstrdup_for_dump (val->spec_node->name ()),
3993 val->spec_node->order);
3995 cs->redirect_callee_duplicating_thunks (val->spec_node);
3996 val->spec_node->expand_all_artificial_thunks ();
3997 redirected_sum += cs->count;
3999 cs = get_next_cgraph_edge_clone (cs);
4003 if (redirected_sum)
4004 update_specialized_profile (val->spec_node, node, redirected_sum);
4007 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4009 static bool
4010 known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
4012 ipa_polymorphic_call_context *ctx;
4013 int i;
4015 FOR_EACH_VEC_ELT (known_contexts, i, ctx)
4016 if (!ctx->useless_p ())
4017 return true;
4018 return false;
4021 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4023 static vec<ipa_polymorphic_call_context>
4024 copy_useful_known_contexts (vec<ipa_polymorphic_call_context> known_contexts)
4026 if (known_contexts_useful_p (known_contexts))
4027 return known_contexts.copy ();
4028 else
4029 return vNULL;
4032 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4033 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4035 static void
4036 modify_known_vectors_with_val (vec<tree> *known_csts,
4037 vec<ipa_polymorphic_call_context> *known_contexts,
4038 ipcp_value<tree> *val,
4039 int index)
4041 *known_csts = known_csts->copy ();
4042 *known_contexts = copy_useful_known_contexts (*known_contexts);
4043 (*known_csts)[index] = val->value;
4046 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4047 copy according to VAL and INDEX. */
4049 static void
4050 modify_known_vectors_with_val (vec<tree> *known_csts,
4051 vec<ipa_polymorphic_call_context> *known_contexts,
4052 ipcp_value<ipa_polymorphic_call_context> *val,
4053 int index)
4055 *known_csts = known_csts->copy ();
4056 *known_contexts = known_contexts->copy ();
4057 (*known_contexts)[index] = val->value;
4060 /* Return true if OFFSET indicates this was not an aggregate value or there is
4061 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4062 AGGVALS list. */
4064 DEBUG_FUNCTION bool
4065 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *aggvals,
4066 int index, HOST_WIDE_INT offset, tree value)
4068 if (offset == -1)
4069 return true;
4071 while (aggvals)
4073 if (aggvals->index == index
4074 && aggvals->offset == offset
4075 && values_equal_for_ipcp_p (aggvals->value, value))
4076 return true;
4077 aggvals = aggvals->next;
4079 return false;
4082 /* Return true if offset is minus one because source of a polymorphic contect
4083 cannot be an aggregate value. */
4085 DEBUG_FUNCTION bool
4086 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *,
4087 int , HOST_WIDE_INT offset,
4088 ipa_polymorphic_call_context)
4090 return offset == -1;
4093 /* Decide wheter to create a special version of NODE for value VAL of parameter
4094 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4095 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4096 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4098 template <typename valtype>
4099 static bool
4100 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
4101 ipcp_value<valtype> *val, vec<tree> known_csts,
4102 vec<ipa_polymorphic_call_context> known_contexts)
4104 struct ipa_agg_replacement_value *aggvals;
4105 int freq_sum, caller_count;
4106 gcov_type count_sum;
4107 vec<cgraph_edge *> callers;
4109 if (val->spec_node)
4111 perhaps_add_new_callers (node, val);
4112 return false;
4114 else if (val->local_size_cost + overall_size > max_new_size)
4116 if (dump_file && (dump_flags & TDF_DETAILS))
4117 fprintf (dump_file, " Ignoring candidate value because "
4118 "max_new_size would be reached with %li.\n",
4119 val->local_size_cost + overall_size);
4120 return false;
4122 else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum,
4123 &caller_count))
4124 return false;
4126 if (dump_file && (dump_flags & TDF_DETAILS))
4128 fprintf (dump_file, " - considering value ");
4129 print_ipcp_constant_value (dump_file, val->value);
4130 fprintf (dump_file, " for ");
4131 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
4132 if (offset != -1)
4133 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
4134 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
4137 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
4138 freq_sum, count_sum,
4139 val->local_size_cost)
4140 && !good_cloning_opportunity_p (node,
4141 val->local_time_benefit
4142 + val->prop_time_benefit,
4143 freq_sum, count_sum,
4144 val->local_size_cost
4145 + val->prop_size_cost))
4146 return false;
4148 if (dump_file)
4149 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
4150 node->name (), node->order);
4152 callers = gather_edges_for_value (val, node, caller_count);
4153 if (offset == -1)
4154 modify_known_vectors_with_val (&known_csts, &known_contexts, val, index);
4155 else
4157 known_csts = known_csts.copy ();
4158 known_contexts = copy_useful_known_contexts (known_contexts);
4160 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
4161 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
4162 aggvals = find_aggregate_values_for_callers_subset (node, callers);
4163 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
4164 offset, val->value));
4165 val->spec_node = create_specialized_node (node, known_csts, known_contexts,
4166 aggvals, callers);
4167 overall_size += val->local_size_cost;
4169 /* TODO: If for some lattice there is only one other known value
4170 left, make a special node for it too. */
4172 return true;
4175 /* Decide whether and what specialized clones of NODE should be created. */
4177 static bool
4178 decide_whether_version_node (struct cgraph_node *node)
4180 struct ipa_node_params *info = IPA_NODE_REF (node);
4181 int i, count = ipa_get_param_count (info);
4182 vec<tree> known_csts;
4183 vec<ipa_polymorphic_call_context> known_contexts;
4184 vec<ipa_agg_jump_function> known_aggs = vNULL;
4185 bool ret = false;
4187 if (count == 0)
4188 return false;
4190 if (dump_file && (dump_flags & TDF_DETAILS))
4191 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
4192 node->name (), node->order);
4194 gather_context_independent_values (info, &known_csts, &known_contexts,
4195 info->do_clone_for_all_contexts ? &known_aggs
4196 : NULL, NULL);
4198 for (i = 0; i < count ;i++)
4200 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4201 ipcp_lattice<tree> *lat = &plats->itself;
4202 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
4204 if (!lat->bottom
4205 && !known_csts[i])
4207 ipcp_value<tree> *val;
4208 for (val = lat->values; val; val = val->next)
4209 ret |= decide_about_value (node, i, -1, val, known_csts,
4210 known_contexts);
4213 if (!plats->aggs_bottom)
4215 struct ipcp_agg_lattice *aglat;
4216 ipcp_value<tree> *val;
4217 for (aglat = plats->aggs; aglat; aglat = aglat->next)
4218 if (!aglat->bottom && aglat->values
4219 /* If the following is false, the one value is in
4220 known_aggs. */
4221 && (plats->aggs_contain_variable
4222 || !aglat->is_single_const ()))
4223 for (val = aglat->values; val; val = val->next)
4224 ret |= decide_about_value (node, i, aglat->offset, val,
4225 known_csts, known_contexts);
4228 if (!ctxlat->bottom
4229 && known_contexts[i].useless_p ())
4231 ipcp_value<ipa_polymorphic_call_context> *val;
4232 for (val = ctxlat->values; val; val = val->next)
4233 ret |= decide_about_value (node, i, -1, val, known_csts,
4234 known_contexts);
4237 info = IPA_NODE_REF (node);
4240 if (info->do_clone_for_all_contexts)
4242 struct cgraph_node *clone;
4243 vec<cgraph_edge *> callers;
4245 if (dump_file)
4246 fprintf (dump_file, " - Creating a specialized node of %s/%i "
4247 "for all known contexts.\n", node->name (),
4248 node->order);
4250 callers = node->collect_callers ();
4252 if (!known_contexts_useful_p (known_contexts))
4254 known_contexts.release ();
4255 known_contexts = vNULL;
4257 clone = create_specialized_node (node, known_csts, known_contexts,
4258 known_aggs_to_agg_replacement_list (known_aggs),
4259 callers);
4260 info = IPA_NODE_REF (node);
4261 info->do_clone_for_all_contexts = false;
4262 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
4263 for (i = 0; i < count ; i++)
4264 vec_free (known_aggs[i].items);
4265 known_aggs.release ();
4266 ret = true;
4268 else
4270 known_csts.release ();
4271 known_contexts.release ();
4274 return ret;
4277 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4279 static void
4280 spread_undeadness (struct cgraph_node *node)
4282 struct cgraph_edge *cs;
4284 for (cs = node->callees; cs; cs = cs->next_callee)
4285 if (ipa_edge_within_scc (cs))
4287 struct cgraph_node *callee;
4288 struct ipa_node_params *info;
4290 callee = cs->callee->function_symbol (NULL);
4291 info = IPA_NODE_REF (callee);
4293 if (info->node_dead)
4295 info->node_dead = 0;
4296 spread_undeadness (callee);
4301 /* Return true if NODE has a caller from outside of its SCC that is not
4302 dead. Worker callback for cgraph_for_node_and_aliases. */
4304 static bool
4305 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
4306 void *data ATTRIBUTE_UNUSED)
4308 struct cgraph_edge *cs;
4310 for (cs = node->callers; cs; cs = cs->next_caller)
4311 if (cs->caller->thunk.thunk_p
4312 && cs->caller->call_for_symbol_thunks_and_aliases
4313 (has_undead_caller_from_outside_scc_p, NULL, true))
4314 return true;
4315 else if (!ipa_edge_within_scc (cs)
4316 && !IPA_NODE_REF (cs->caller)->node_dead)
4317 return true;
4318 return false;
4322 /* Identify nodes within the same SCC as NODE which are no longer needed
4323 because of new clones and will be removed as unreachable. */
4325 static void
4326 identify_dead_nodes (struct cgraph_node *node)
4328 struct cgraph_node *v;
4329 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4330 if (v->will_be_removed_from_program_if_no_direct_calls_p ()
4331 && !v->call_for_symbol_thunks_and_aliases
4332 (has_undead_caller_from_outside_scc_p, NULL, true))
4333 IPA_NODE_REF (v)->node_dead = 1;
4335 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4336 if (!IPA_NODE_REF (v)->node_dead)
4337 spread_undeadness (v);
4339 if (dump_file && (dump_flags & TDF_DETAILS))
4341 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4342 if (IPA_NODE_REF (v)->node_dead)
4343 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
4344 v->name (), v->order);
4348 /* The decision stage. Iterate over the topological order of call graph nodes
4349 TOPO and make specialized clones if deemed beneficial. */
4351 static void
4352 ipcp_decision_stage (struct ipa_topo_info *topo)
4354 int i;
4356 if (dump_file)
4357 fprintf (dump_file, "\nIPA decision stage:\n\n");
4359 for (i = topo->nnodes - 1; i >= 0; i--)
4361 struct cgraph_node *node = topo->order[i];
4362 bool change = false, iterate = true;
4364 while (iterate)
4366 struct cgraph_node *v;
4367 iterate = false;
4368 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4369 if (v->has_gimple_body_p ()
4370 && ipcp_versionable_function_p (v))
4371 iterate |= decide_whether_version_node (v);
4373 change |= iterate;
4375 if (change)
4376 identify_dead_nodes (node);
4380 /* Look up all alignment information that we have discovered and copy it over
4381 to the transformation summary. */
4383 static void
4384 ipcp_store_alignment_results (void)
4386 cgraph_node *node;
4388 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4390 ipa_node_params *info = IPA_NODE_REF (node);
4391 bool dumped_sth = false;
4392 bool found_useful_result = false;
4394 if (!opt_for_fn (node->decl, flag_ipa_cp_alignment))
4396 if (dump_file)
4397 fprintf (dump_file, "Not considering %s for alignment discovery "
4398 "and propagate; -fipa-cp-alignment: disabled.\n",
4399 node->name ());
4400 continue;
4403 if (info->ipcp_orig_node)
4404 info = IPA_NODE_REF (info->ipcp_orig_node);
4406 unsigned count = ipa_get_param_count (info);
4407 for (unsigned i = 0; i < count ; i++)
4409 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4410 if (plats->alignment.known
4411 && plats->alignment.align > 0)
4413 found_useful_result = true;
4414 break;
4417 if (!found_useful_result)
4418 continue;
4420 ipcp_grow_transformations_if_necessary ();
4421 ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node);
4422 vec_safe_reserve_exact (ts->alignments, count);
4424 for (unsigned i = 0; i < count ; i++)
4426 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4428 if (plats->alignment.align == 0)
4429 plats->alignment.known = false;
4431 ts->alignments->quick_push (plats->alignment);
4432 if (!dump_file || !plats->alignment.known)
4433 continue;
4434 if (!dumped_sth)
4436 fprintf (dump_file, "Propagated alignment info for function %s/%i:\n",
4437 node->name (), node->order);
4438 dumped_sth = true;
4440 fprintf (dump_file, " param %i: align: %u, misalign: %u\n",
4441 i, plats->alignment.align, plats->alignment.misalign);
4446 /* The IPCP driver. */
4448 static unsigned int
4449 ipcp_driver (void)
4451 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
4452 struct cgraph_edge_hook_list *edge_removal_hook_holder;
4453 struct ipa_topo_info topo;
4455 ipa_check_create_node_params ();
4456 ipa_check_create_edge_args ();
4457 grow_edge_clone_vectors ();
4458 edge_duplication_hook_holder =
4459 symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
4460 edge_removal_hook_holder =
4461 symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
4463 if (dump_file)
4465 fprintf (dump_file, "\nIPA structures before propagation:\n");
4466 if (dump_flags & TDF_DETAILS)
4467 ipa_print_all_params (dump_file);
4468 ipa_print_all_jump_functions (dump_file);
4471 /* Topological sort. */
4472 build_toporder_info (&topo);
4473 /* Do the interprocedural propagation. */
4474 ipcp_propagate_stage (&topo);
4475 /* Decide what constant propagation and cloning should be performed. */
4476 ipcp_decision_stage (&topo);
4477 /* Store results of alignment propagation. */
4478 ipcp_store_alignment_results ();
4480 /* Free all IPCP structures. */
4481 free_toporder_info (&topo);
4482 next_edge_clone.release ();
4483 prev_edge_clone.release ();
4484 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4485 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4486 ipa_free_all_structures_after_ipa_cp ();
4487 if (dump_file)
4488 fprintf (dump_file, "\nIPA constant propagation end\n");
4489 return 0;
4492 /* Initialization and computation of IPCP data structures. This is the initial
4493 intraprocedural analysis of functions, which gathers information to be
4494 propagated later on. */
4496 static void
4497 ipcp_generate_summary (void)
4499 struct cgraph_node *node;
4501 if (dump_file)
4502 fprintf (dump_file, "\nIPA constant propagation start:\n");
4503 ipa_register_cgraph_hooks ();
4505 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4507 node->local.versionable
4508 = tree_versionable_function_p (node->decl);
4509 ipa_analyze_node (node);
4513 /* Write ipcp summary for nodes in SET. */
4515 static void
4516 ipcp_write_summary (void)
4518 ipa_prop_write_jump_functions ();
4521 /* Read ipcp summary. */
4523 static void
4524 ipcp_read_summary (void)
4526 ipa_prop_read_jump_functions ();
4529 namespace {
4531 const pass_data pass_data_ipa_cp =
4533 IPA_PASS, /* type */
4534 "cp", /* name */
4535 OPTGROUP_NONE, /* optinfo_flags */
4536 TV_IPA_CONSTANT_PROP, /* tv_id */
4537 0, /* properties_required */
4538 0, /* properties_provided */
4539 0, /* properties_destroyed */
4540 0, /* todo_flags_start */
4541 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
4544 class pass_ipa_cp : public ipa_opt_pass_d
4546 public:
4547 pass_ipa_cp (gcc::context *ctxt)
4548 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
4549 ipcp_generate_summary, /* generate_summary */
4550 ipcp_write_summary, /* write_summary */
4551 ipcp_read_summary, /* read_summary */
4552 ipcp_write_transformation_summaries, /*
4553 write_optimization_summary */
4554 ipcp_read_transformation_summaries, /*
4555 read_optimization_summary */
4556 NULL, /* stmt_fixup */
4557 0, /* function_transform_todo_flags_start */
4558 ipcp_transform_function, /* function_transform */
4559 NULL) /* variable_transform */
4562 /* opt_pass methods: */
4563 virtual bool gate (function *)
4565 /* FIXME: We should remove the optimize check after we ensure we never run
4566 IPA passes when not optimizing. */
4567 return (flag_ipa_cp && optimize) || in_lto_p;
4570 virtual unsigned int execute (function *) { return ipcp_driver (); }
4572 }; // class pass_ipa_cp
4574 } // anon namespace
4576 ipa_opt_pass_d *
4577 make_pass_ipa_cp (gcc::context *ctxt)
4579 return new pass_ipa_cp (ctxt);
4582 /* Reset all state within ipa-cp.c so that we can rerun the compiler
4583 within the same process. For use by toplev::finalize. */
4585 void
4586 ipa_cp_c_finalize (void)
4588 max_count = 0;
4589 overall_size = 0;
4590 max_new_size = 0;