* doc/install.texi (*-ibm-aix*): Additional information for AIX 7.1.
[official-gcc.git] / gcc / ipa-cp.c
blobef93b209dfb445a29a0d92789b56709627cd3baf
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 "tree.h"
108 #include "options.h"
109 #include "fold-const.h"
110 #include "gimple-fold.h"
111 #include "gimple-expr.h"
112 #include "target.h"
113 #include "backend.h"
114 #include "predict.h"
115 #include "hard-reg-set.h"
116 #include "cgraph.h"
117 #include "alloc-pool.h"
118 #include "symbol-summary.h"
119 #include "ipa-prop.h"
120 #include "tree-pass.h"
121 #include "flags.h"
122 #include "diagnostic.h"
123 #include "tree-pretty-print.h"
124 #include "tree-inline.h"
125 #include "params.h"
126 #include "ipa-inline.h"
127 #include "ipa-utils.h"
129 template <typename valtype> class ipcp_value;
131 /* Describes a particular source for an IPA-CP value. */
133 template <typename valtype>
134 class ipcp_value_source
136 public:
137 /* Aggregate offset of the source, negative if the source is scalar value of
138 the argument itself. */
139 HOST_WIDE_INT offset;
140 /* The incoming edge that brought the value. */
141 cgraph_edge *cs;
142 /* If the jump function that resulted into his value was a pass-through or an
143 ancestor, this is the ipcp_value of the caller from which the described
144 value has been derived. Otherwise it is NULL. */
145 ipcp_value<valtype> *val;
146 /* Next pointer in a linked list of sources of a value. */
147 ipcp_value_source *next;
148 /* If the jump function that resulted into his value was a pass-through or an
149 ancestor, this is the index of the parameter of the caller the jump
150 function references. */
151 int index;
154 /* Common ancestor for all ipcp_value instantiations. */
156 class ipcp_value_base
158 public:
159 /* Time benefit and size cost that specializing the function for this value
160 would bring about in this function alone. */
161 int local_time_benefit, local_size_cost;
162 /* Time benefit and size cost that specializing the function for this value
163 can bring about in it's callees (transitively). */
164 int prop_time_benefit, prop_size_cost;
167 /* Describes one particular value stored in struct ipcp_lattice. */
169 template <typename valtype>
170 class ipcp_value : public ipcp_value_base
172 public:
173 /* The actual value for the given parameter. */
174 valtype value;
175 /* The list of sources from which this value originates. */
176 ipcp_value_source <valtype> *sources;
177 /* Next pointers in a linked list of all values in a lattice. */
178 ipcp_value *next;
179 /* Next pointers in a linked list of values in a strongly connected component
180 of values. */
181 ipcp_value *scc_next;
182 /* Next pointers in a linked list of SCCs of values sorted topologically
183 according their sources. */
184 ipcp_value *topo_next;
185 /* A specialized node created for this value, NULL if none has been (so far)
186 created. */
187 cgraph_node *spec_node;
188 /* Depth first search number and low link for topological sorting of
189 values. */
190 int dfs, low_link;
191 /* True if this valye is currently on the topo-sort stack. */
192 bool on_stack;
194 void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx,
195 HOST_WIDE_INT offset);
198 /* Lattice describing potential values of a formal parameter of a function, or
199 a part of an aggreagate. TOP is represented by a lattice with zero values
200 and with contains_variable and bottom flags cleared. BOTTOM is represented
201 by a lattice with the bottom flag set. In that case, values and
202 contains_variable flag should be disregarded. */
204 template <typename valtype>
205 class ipcp_lattice
207 public:
208 /* The list of known values and types in this lattice. Note that values are
209 not deallocated if a lattice is set to bottom because there may be value
210 sources referencing them. */
211 ipcp_value<valtype> *values;
212 /* Number of known values and types in this lattice. */
213 int values_count;
214 /* The lattice contains a variable component (in addition to values). */
215 bool contains_variable;
216 /* The value of the lattice is bottom (i.e. variable and unusable for any
217 propagation). */
218 bool bottom;
220 inline bool is_single_const ();
221 inline bool set_to_bottom ();
222 inline bool set_contains_variable ();
223 bool add_value (valtype newval, cgraph_edge *cs,
224 ipcp_value<valtype> *src_val = NULL,
225 int src_idx = 0, HOST_WIDE_INT offset = -1);
226 void print (FILE * f, bool dump_sources, bool dump_benefits);
229 /* Lattice of tree values with an offset to describe a part of an
230 aggregate. */
232 class ipcp_agg_lattice : public ipcp_lattice<tree>
234 public:
235 /* Offset that is being described by this lattice. */
236 HOST_WIDE_INT offset;
237 /* Size so that we don't have to re-compute it every time we traverse the
238 list. Must correspond to TYPE_SIZE of all lat values. */
239 HOST_WIDE_INT size;
240 /* Next element of the linked list. */
241 struct ipcp_agg_lattice *next;
244 /* Lattice of pointer alignment. Unlike the previous types of lattices, this
245 one is only capable of holding one value. */
247 class ipcp_alignment_lattice
249 public:
250 /* If bottom and top are both false, these two fields hold values as given by
251 ptr_info_def and get_pointer_alignment_1. */
252 unsigned align;
253 unsigned misalign;
255 inline bool bottom_p () const;
256 inline bool top_p () const;
257 inline bool set_to_bottom ();
258 bool meet_with (unsigned new_align, unsigned new_misalign);
259 bool meet_with (const ipcp_alignment_lattice &other, HOST_WIDE_INT offset);
260 void print (FILE * f);
261 private:
262 /* If set, this lattice is bottom and all other fields should be
263 disregarded. */
264 bool bottom;
265 /* If bottom and not_top are false, the lattice is TOP. If not_top is true,
266 the known alignment is stored in the fields align and misalign. The field
267 is negated so that memset to zero initializes the lattice to TOP
268 state. */
269 bool not_top;
271 bool meet_with_1 (unsigned new_align, unsigned new_misalign);
274 /* Structure containing lattices for a parameter itself and for pieces of
275 aggregates that are passed in the parameter or by a reference in a parameter
276 plus some other useful flags. */
278 class ipcp_param_lattices
280 public:
281 /* Lattice describing the value of the parameter itself. */
282 ipcp_lattice<tree> itself;
283 /* Lattice describing the polymorphic contexts of a parameter. */
284 ipcp_lattice<ipa_polymorphic_call_context> ctxlat;
285 /* Lattices describing aggregate parts. */
286 ipcp_agg_lattice *aggs;
287 /* Lattice describing known alignment. */
288 ipcp_alignment_lattice alignment;
289 /* Number of aggregate lattices */
290 int aggs_count;
291 /* True if aggregate data were passed by reference (as opposed to by
292 value). */
293 bool aggs_by_ref;
294 /* All aggregate lattices contain a variable component (in addition to
295 values). */
296 bool aggs_contain_variable;
297 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
298 for any propagation). */
299 bool aggs_bottom;
301 /* There is a virtual call based on this parameter. */
302 bool virt_call;
305 /* Allocation pools for values and their sources in ipa-cp. */
307 object_allocator<ipcp_value<tree> > ipcp_cst_values_pool
308 ("IPA-CP constant values");
310 object_allocator<ipcp_value<ipa_polymorphic_call_context> >
311 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
313 object_allocator<ipcp_value_source<tree> > ipcp_sources_pool
314 ("IPA-CP value sources");
316 object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
317 ("IPA_CP aggregate lattices");
319 /* Maximal count found in program. */
321 static gcov_type max_count;
323 /* Original overall size of the program. */
325 static long overall_size, max_new_size;
327 /* Return the param lattices structure corresponding to the Ith formal
328 parameter of the function described by INFO. */
329 static inline struct ipcp_param_lattices *
330 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
332 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
333 gcc_checking_assert (!info->ipcp_orig_node);
334 gcc_checking_assert (info->lattices);
335 return &(info->lattices[i]);
338 /* Return the lattice corresponding to the scalar value of the Ith formal
339 parameter of the function described by INFO. */
340 static inline ipcp_lattice<tree> *
341 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
343 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
344 return &plats->itself;
347 /* Return the lattice corresponding to the scalar value of the Ith formal
348 parameter of the function described by INFO. */
349 static inline ipcp_lattice<ipa_polymorphic_call_context> *
350 ipa_get_poly_ctx_lat (struct ipa_node_params *info, int i)
352 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
353 return &plats->ctxlat;
356 /* Return whether LAT is a lattice with a single constant and without an
357 undefined value. */
359 template <typename valtype>
360 inline bool
361 ipcp_lattice<valtype>::is_single_const ()
363 if (bottom || contains_variable || values_count != 1)
364 return false;
365 else
366 return true;
369 /* Print V which is extracted from a value in a lattice to F. */
371 static void
372 print_ipcp_constant_value (FILE * f, tree v)
374 if (TREE_CODE (v) == ADDR_EXPR
375 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
377 fprintf (f, "& ");
378 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
380 else
381 print_generic_expr (f, v, 0);
384 /* Print V which is extracted from a value in a lattice to F. */
386 static void
387 print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
389 v.dump(f, false);
392 /* Print a lattice LAT to F. */
394 template <typename valtype>
395 void
396 ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
398 ipcp_value<valtype> *val;
399 bool prev = false;
401 if (bottom)
403 fprintf (f, "BOTTOM\n");
404 return;
407 if (!values_count && !contains_variable)
409 fprintf (f, "TOP\n");
410 return;
413 if (contains_variable)
415 fprintf (f, "VARIABLE");
416 prev = true;
417 if (dump_benefits)
418 fprintf (f, "\n");
421 for (val = values; val; val = val->next)
423 if (dump_benefits && prev)
424 fprintf (f, " ");
425 else if (!dump_benefits && prev)
426 fprintf (f, ", ");
427 else
428 prev = true;
430 print_ipcp_constant_value (f, val->value);
432 if (dump_sources)
434 ipcp_value_source<valtype> *s;
436 fprintf (f, " [from:");
437 for (s = val->sources; s; s = s->next)
438 fprintf (f, " %i(%i)", s->cs->caller->order,
439 s->cs->frequency);
440 fprintf (f, "]");
443 if (dump_benefits)
444 fprintf (f, " [loc_time: %i, loc_size: %i, "
445 "prop_time: %i, prop_size: %i]\n",
446 val->local_time_benefit, val->local_size_cost,
447 val->prop_time_benefit, val->prop_size_cost);
449 if (!dump_benefits)
450 fprintf (f, "\n");
453 /* Print alignment lattice to F. */
455 void
456 ipcp_alignment_lattice::print (FILE * f)
458 if (top_p ())
459 fprintf (f, " Alignment unknown (TOP)\n");
460 else if (bottom_p ())
461 fprintf (f, " Alignment unusable (BOTTOM)\n");
462 else
463 fprintf (f, " Alignment %u, misalignment %u\n", align, misalign);
466 /* Print all ipcp_lattices of all functions to F. */
468 static void
469 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
471 struct cgraph_node *node;
472 int i, count;
474 fprintf (f, "\nLattices:\n");
475 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
477 struct ipa_node_params *info;
479 info = IPA_NODE_REF (node);
480 fprintf (f, " Node: %s/%i:\n", node->name (),
481 node->order);
482 count = ipa_get_param_count (info);
483 for (i = 0; i < count; i++)
485 struct ipcp_agg_lattice *aglat;
486 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
487 fprintf (f, " param [%d]: ", i);
488 plats->itself.print (f, dump_sources, dump_benefits);
489 fprintf (f, " ctxs: ");
490 plats->ctxlat.print (f, dump_sources, dump_benefits);
491 plats->alignment.print (f);
492 if (plats->virt_call)
493 fprintf (f, " virt_call flag set\n");
495 if (plats->aggs_bottom)
497 fprintf (f, " AGGS BOTTOM\n");
498 continue;
500 if (plats->aggs_contain_variable)
501 fprintf (f, " AGGS VARIABLE\n");
502 for (aglat = plats->aggs; aglat; aglat = aglat->next)
504 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
505 plats->aggs_by_ref ? "ref " : "", aglat->offset);
506 aglat->print (f, dump_sources, dump_benefits);
512 /* Determine whether it is at all technically possible to create clones of NODE
513 and store this information in the ipa_node_params structure associated
514 with NODE. */
516 static void
517 determine_versionability (struct cgraph_node *node,
518 struct ipa_node_params *info)
520 const char *reason = NULL;
522 /* There are a number of generic reasons functions cannot be versioned. We
523 also cannot remove parameters if there are type attributes such as fnspec
524 present. */
525 if (node->alias || node->thunk.thunk_p)
526 reason = "alias or thunk";
527 else if (!node->local.versionable)
528 reason = "not a tree_versionable_function";
529 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
530 reason = "insufficient body availability";
531 else if (!opt_for_fn (node->decl, optimize)
532 || !opt_for_fn (node->decl, flag_ipa_cp))
533 reason = "non-optimized function";
534 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
536 /* Ideally we should clone the SIMD clones themselves and create
537 vector copies of them, so IPA-cp and SIMD clones can happily
538 coexist, but that may not be worth the effort. */
539 reason = "function has SIMD clones";
541 /* Don't clone decls local to a comdat group; it breaks and for C++
542 decloned constructors, inlining is always better anyway. */
543 else if (node->comdat_local_p ())
544 reason = "comdat-local function";
546 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
547 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
548 node->name (), node->order, reason);
550 info->versionable = (reason == NULL);
553 /* Return true if it is at all technically possible to create clones of a
554 NODE. */
556 static bool
557 ipcp_versionable_function_p (struct cgraph_node *node)
559 return IPA_NODE_REF (node)->versionable;
562 /* Structure holding accumulated information about callers of a node. */
564 struct caller_statistics
566 gcov_type count_sum;
567 int n_calls, n_hot_calls, freq_sum;
570 /* Initialize fields of STAT to zeroes. */
572 static inline void
573 init_caller_stats (struct caller_statistics *stats)
575 stats->count_sum = 0;
576 stats->n_calls = 0;
577 stats->n_hot_calls = 0;
578 stats->freq_sum = 0;
581 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
582 non-thunk incoming edges to NODE. */
584 static bool
585 gather_caller_stats (struct cgraph_node *node, void *data)
587 struct caller_statistics *stats = (struct caller_statistics *) data;
588 struct cgraph_edge *cs;
590 for (cs = node->callers; cs; cs = cs->next_caller)
591 if (!cs->caller->thunk.thunk_p)
593 stats->count_sum += cs->count;
594 stats->freq_sum += cs->frequency;
595 stats->n_calls++;
596 if (cs->maybe_hot_p ())
597 stats->n_hot_calls ++;
599 return false;
603 /* Return true if this NODE is viable candidate for cloning. */
605 static bool
606 ipcp_cloning_candidate_p (struct cgraph_node *node)
608 struct caller_statistics stats;
610 gcc_checking_assert (node->has_gimple_body_p ());
612 if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
614 if (dump_file)
615 fprintf (dump_file, "Not considering %s for cloning; "
616 "-fipa-cp-clone disabled.\n",
617 node->name ());
618 return false;
621 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
623 if (dump_file)
624 fprintf (dump_file, "Not considering %s for cloning; "
625 "optimizing it for size.\n",
626 node->name ());
627 return false;
630 init_caller_stats (&stats);
631 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
633 if (inline_summaries->get (node)->self_size < stats.n_calls)
635 if (dump_file)
636 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
637 node->name ());
638 return true;
641 /* When profile is available and function is hot, propagate into it even if
642 calls seems cold; constant propagation can improve function's speed
643 significantly. */
644 if (max_count)
646 if (stats.count_sum > node->count * 90 / 100)
648 if (dump_file)
649 fprintf (dump_file, "Considering %s for cloning; "
650 "usually called directly.\n",
651 node->name ());
652 return true;
655 if (!stats.n_hot_calls)
657 if (dump_file)
658 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
659 node->name ());
660 return false;
662 if (dump_file)
663 fprintf (dump_file, "Considering %s for cloning.\n",
664 node->name ());
665 return true;
668 template <typename valtype>
669 class value_topo_info
671 public:
672 /* Head of the linked list of topologically sorted values. */
673 ipcp_value<valtype> *values_topo;
674 /* Stack for creating SCCs, represented by a linked list too. */
675 ipcp_value<valtype> *stack;
676 /* Counter driving the algorithm in add_val_to_toposort. */
677 int dfs_counter;
679 value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
681 void add_val (ipcp_value<valtype> *cur_val);
682 void propagate_effects ();
685 /* Arrays representing a topological ordering of call graph nodes and a stack
686 of nodes used during constant propagation and also data required to perform
687 topological sort of values and propagation of benefits in the determined
688 order. */
690 class ipa_topo_info
692 public:
693 /* Array with obtained topological order of cgraph nodes. */
694 struct cgraph_node **order;
695 /* Stack of cgraph nodes used during propagation within SCC until all values
696 in the SCC stabilize. */
697 struct cgraph_node **stack;
698 int nnodes, stack_top;
700 value_topo_info<tree> constants;
701 value_topo_info<ipa_polymorphic_call_context> contexts;
703 ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
704 constants ()
708 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
710 static void
711 build_toporder_info (struct ipa_topo_info *topo)
713 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
714 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
716 gcc_checking_assert (topo->stack_top == 0);
717 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
720 /* Free information about strongly connected components and the arrays in
721 TOPO. */
723 static void
724 free_toporder_info (struct ipa_topo_info *topo)
726 ipa_free_postorder_info ();
727 free (topo->order);
728 free (topo->stack);
731 /* Add NODE to the stack in TOPO, unless it is already there. */
733 static inline void
734 push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node)
736 struct ipa_node_params *info = IPA_NODE_REF (node);
737 if (info->node_enqueued)
738 return;
739 info->node_enqueued = 1;
740 topo->stack[topo->stack_top++] = node;
743 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
744 is empty. */
746 static struct cgraph_node *
747 pop_node_from_stack (struct ipa_topo_info *topo)
749 if (topo->stack_top)
751 struct cgraph_node *node;
752 topo->stack_top--;
753 node = topo->stack[topo->stack_top];
754 IPA_NODE_REF (node)->node_enqueued = 0;
755 return node;
757 else
758 return NULL;
761 /* Set lattice LAT to bottom and return true if it previously was not set as
762 such. */
764 template <typename valtype>
765 inline bool
766 ipcp_lattice<valtype>::set_to_bottom ()
768 bool ret = !bottom;
769 bottom = true;
770 return ret;
773 /* Mark lattice as containing an unknown value and return true if it previously
774 was not marked as such. */
776 template <typename valtype>
777 inline bool
778 ipcp_lattice<valtype>::set_contains_variable ()
780 bool ret = !contains_variable;
781 contains_variable = true;
782 return ret;
785 /* Set all aggegate lattices in PLATS to bottom and return true if they were
786 not previously set as such. */
788 static inline bool
789 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
791 bool ret = !plats->aggs_bottom;
792 plats->aggs_bottom = true;
793 return ret;
796 /* Mark all aggegate lattices in PLATS as containing an unknown value and
797 return true if they were not previously marked as such. */
799 static inline bool
800 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
802 bool ret = !plats->aggs_contain_variable;
803 plats->aggs_contain_variable = true;
804 return ret;
807 /* Return true if alignment information in the lattice is yet unknown. */
809 bool
810 ipcp_alignment_lattice::top_p () const
812 return !bottom && !not_top;
815 /* Return true if alignment information in the lattice is known to be
816 unusable. */
818 bool
819 ipcp_alignment_lattice::bottom_p () const
821 return bottom;
824 /* Set alignment information in the lattice to bottom. Return true if it
825 previously was in a different state. */
827 bool
828 ipcp_alignment_lattice::set_to_bottom ()
830 if (bottom_p ())
831 return false;
832 bottom = true;
833 return true;
836 /* Meet the current value of the lattice with alignment described by NEW_ALIGN
837 and NEW_MISALIGN, assuming that we know the current value is neither TOP nor
838 BOTTOM. Return true if the value of lattice has changed. */
840 bool
841 ipcp_alignment_lattice::meet_with_1 (unsigned new_align, unsigned new_misalign)
843 gcc_checking_assert (new_align != 0);
844 if (align == new_align && misalign == new_misalign)
845 return false;
847 bool changed = false;
848 if (align > new_align)
850 align = new_align;
851 misalign = misalign % new_align;
852 changed = true;
854 if (misalign != (new_misalign % align))
856 int diff = abs ((int) misalign - (int) (new_misalign % align));
857 align = (unsigned) diff & -diff;
858 if (align)
859 misalign = misalign % align;
860 else
861 set_to_bottom ();
862 changed = true;
864 gcc_checking_assert (bottom_p () || align != 0);
865 return changed;
868 /* Meet the current value of the lattice with alignment described by NEW_ALIGN
869 and NEW_MISALIGN. Return true if the value of lattice has changed. */
871 bool
872 ipcp_alignment_lattice::meet_with (unsigned new_align, unsigned new_misalign)
874 gcc_assert (new_align != 0);
875 if (bottom_p ())
876 return false;
877 if (top_p ())
879 not_top = true;
880 align = new_align;
881 misalign = new_misalign;
882 return true;
884 return meet_with_1 (new_align, new_misalign);
887 /* Meet the current value of the lattice with OTHER, taking into account that
888 OFFSET has been added to the pointer value. Return true if the value of
889 lattice has changed. */
891 bool
892 ipcp_alignment_lattice::meet_with (const ipcp_alignment_lattice &other,
893 HOST_WIDE_INT offset)
895 if (other.bottom_p ())
896 return set_to_bottom ();
897 if (bottom_p () || other.top_p ())
898 return false;
900 unsigned adjusted_misalign = (other.misalign + offset) % other.align;
901 if (top_p ())
903 not_top = true;
904 align = other.align;
905 misalign = adjusted_misalign;
906 return true;
909 return meet_with_1 (other.align, adjusted_misalign);
912 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
913 return true is any of them has not been marked as such so far. */
915 static inline bool
916 set_all_contains_variable (struct ipcp_param_lattices *plats)
918 bool ret;
919 ret = plats->itself.set_contains_variable ();
920 ret |= plats->ctxlat.set_contains_variable ();
921 ret |= set_agg_lats_contain_variable (plats);
922 ret |= plats->alignment.set_to_bottom ();
923 return ret;
926 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
927 points to by the number of callers to NODE. */
929 static bool
930 count_callers (cgraph_node *node, void *data)
932 int *caller_count = (int *) data;
934 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
935 /* Local thunks can be handled transparently, but if the thunk can not
936 be optimized out, count it as a real use. */
937 if (!cs->caller->thunk.thunk_p || !cs->caller->local.local)
938 ++*caller_count;
939 return false;
942 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
943 the one caller of some other node. Set the caller's corresponding flag. */
945 static bool
946 set_single_call_flag (cgraph_node *node, void *)
948 cgraph_edge *cs = node->callers;
949 /* Local thunks can be handled transparently, skip them. */
950 while (cs && cs->caller->thunk.thunk_p && cs->caller->local.local)
951 cs = cs->next_caller;
952 if (cs)
954 IPA_NODE_REF (cs->caller)->node_calling_single_call = true;
955 return true;
957 return false;
960 /* Initialize ipcp_lattices. */
962 static void
963 initialize_node_lattices (struct cgraph_node *node)
965 struct ipa_node_params *info = IPA_NODE_REF (node);
966 struct cgraph_edge *ie;
967 bool disable = false, variable = false;
968 int i;
970 gcc_checking_assert (node->has_gimple_body_p ());
971 if (cgraph_local_p (node))
973 int caller_count = 0;
974 node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
975 true);
976 gcc_checking_assert (caller_count > 0);
977 if (caller_count == 1)
978 node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
979 NULL, true);
981 else
983 /* When cloning is allowed, we can assume that externally visible
984 functions are not called. We will compensate this by cloning
985 later. */
986 if (ipcp_versionable_function_p (node)
987 && ipcp_cloning_candidate_p (node))
988 variable = true;
989 else
990 disable = true;
993 if (disable || variable)
995 for (i = 0; i < ipa_get_param_count (info) ; i++)
997 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
998 if (disable)
1000 plats->itself.set_to_bottom ();
1001 plats->ctxlat.set_to_bottom ();
1002 set_agg_lats_to_bottom (plats);
1003 plats->alignment.set_to_bottom ();
1005 else
1006 set_all_contains_variable (plats);
1008 if (dump_file && (dump_flags & TDF_DETAILS)
1009 && !node->alias && !node->thunk.thunk_p)
1010 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
1011 node->name (), node->order,
1012 disable ? "BOTTOM" : "VARIABLE");
1015 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1016 if (ie->indirect_info->polymorphic
1017 && ie->indirect_info->param_index >= 0)
1019 gcc_checking_assert (ie->indirect_info->param_index >= 0);
1020 ipa_get_parm_lattices (info,
1021 ie->indirect_info->param_index)->virt_call = 1;
1025 /* Return the result of a (possibly arithmetic) pass through jump function
1026 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
1027 determined or be considered an interprocedural invariant. */
1029 static tree
1030 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
1032 tree restype, res;
1034 gcc_checking_assert (is_gimple_ip_invariant (input));
1035 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1036 return input;
1038 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
1039 == tcc_comparison)
1040 restype = boolean_type_node;
1041 else
1042 restype = TREE_TYPE (input);
1043 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
1044 input, ipa_get_jf_pass_through_operand (jfunc));
1046 if (res && !is_gimple_ip_invariant (res))
1047 return NULL_TREE;
1049 return res;
1052 /* Return the result of an ancestor jump function JFUNC on the constant value
1053 INPUT. Return NULL_TREE if that cannot be determined. */
1055 static tree
1056 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
1058 gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
1059 if (TREE_CODE (input) == ADDR_EXPR)
1061 tree t = TREE_OPERAND (input, 0);
1062 t = build_ref_for_offset (EXPR_LOCATION (t), t,
1063 ipa_get_jf_ancestor_offset (jfunc),
1064 ptr_type_node, NULL, false);
1065 return build_fold_addr_expr (t);
1067 else
1068 return NULL_TREE;
1071 /* Determine whether JFUNC evaluates to a single known constant value and if
1072 so, return it. Otherwise return NULL. INFO describes the caller node or
1073 the one it is inlined to, so that pass-through jump functions can be
1074 evaluated. */
1076 tree
1077 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
1079 if (jfunc->type == IPA_JF_CONST)
1080 return ipa_get_jf_constant (jfunc);
1081 else if (jfunc->type == IPA_JF_PASS_THROUGH
1082 || jfunc->type == IPA_JF_ANCESTOR)
1084 tree input;
1085 int idx;
1087 if (jfunc->type == IPA_JF_PASS_THROUGH)
1088 idx = ipa_get_jf_pass_through_formal_id (jfunc);
1089 else
1090 idx = ipa_get_jf_ancestor_formal_id (jfunc);
1092 if (info->ipcp_orig_node)
1093 input = info->known_csts[idx];
1094 else
1096 ipcp_lattice<tree> *lat;
1098 if (!info->lattices
1099 || idx >= ipa_get_param_count (info))
1100 return NULL_TREE;
1101 lat = ipa_get_scalar_lat (info, idx);
1102 if (!lat->is_single_const ())
1103 return NULL_TREE;
1104 input = lat->values->value;
1107 if (!input)
1108 return NULL_TREE;
1110 if (jfunc->type == IPA_JF_PASS_THROUGH)
1111 return ipa_get_jf_pass_through_result (jfunc, input);
1112 else
1113 return ipa_get_jf_ancestor_result (jfunc, input);
1115 else
1116 return NULL_TREE;
1119 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1120 that INFO describes the caller node or the one it is inlined to, CS is the
1121 call graph edge corresponding to JFUNC and CSIDX index of the described
1122 parameter. */
1124 ipa_polymorphic_call_context
1125 ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1126 ipa_jump_func *jfunc)
1128 ipa_edge_args *args = IPA_EDGE_REF (cs);
1129 ipa_polymorphic_call_context ctx;
1130 ipa_polymorphic_call_context *edge_ctx
1131 = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1133 if (edge_ctx && !edge_ctx->useless_p ())
1134 ctx = *edge_ctx;
1136 if (jfunc->type == IPA_JF_PASS_THROUGH
1137 || jfunc->type == IPA_JF_ANCESTOR)
1139 ipa_polymorphic_call_context srcctx;
1140 int srcidx;
1141 bool type_preserved = true;
1142 if (jfunc->type == IPA_JF_PASS_THROUGH)
1144 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1145 return ctx;
1146 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1147 srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1149 else
1151 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1152 srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1154 if (info->ipcp_orig_node)
1156 if (info->known_contexts.exists ())
1157 srcctx = info->known_contexts[srcidx];
1159 else
1161 if (!info->lattices
1162 || srcidx >= ipa_get_param_count (info))
1163 return ctx;
1164 ipcp_lattice<ipa_polymorphic_call_context> *lat;
1165 lat = ipa_get_poly_ctx_lat (info, srcidx);
1166 if (!lat->is_single_const ())
1167 return ctx;
1168 srcctx = lat->values->value;
1170 if (srcctx.useless_p ())
1171 return ctx;
1172 if (jfunc->type == IPA_JF_ANCESTOR)
1173 srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1174 if (!type_preserved)
1175 srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1176 srcctx.combine_with (ctx);
1177 return srcctx;
1180 return ctx;
1183 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1184 bottom, not containing a variable component and without any known value at
1185 the same time. */
1187 DEBUG_FUNCTION void
1188 ipcp_verify_propagated_values (void)
1190 struct cgraph_node *node;
1192 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1194 struct ipa_node_params *info = IPA_NODE_REF (node);
1195 int i, count = ipa_get_param_count (info);
1197 for (i = 0; i < count; i++)
1199 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
1201 if (!lat->bottom
1202 && !lat->contains_variable
1203 && lat->values_count == 0)
1205 if (dump_file)
1207 symtab_node::dump_table (dump_file);
1208 fprintf (dump_file, "\nIPA lattices after constant "
1209 "propagation, before gcc_unreachable:\n");
1210 print_all_lattices (dump_file, true, false);
1213 gcc_unreachable ();
1219 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1221 static bool
1222 values_equal_for_ipcp_p (tree x, tree y)
1224 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
1226 if (x == y)
1227 return true;
1229 if (TREE_CODE (x) == ADDR_EXPR
1230 && TREE_CODE (y) == ADDR_EXPR
1231 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
1232 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
1233 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
1234 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
1235 else
1236 return operand_equal_p (x, y, 0);
1239 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1241 static bool
1242 values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
1243 ipa_polymorphic_call_context y)
1245 return x.equal_to (y);
1249 /* Add a new value source to the value represented by THIS, marking that a
1250 value comes from edge CS and (if the underlying jump function is a
1251 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1252 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1253 scalar value of the parameter itself or the offset within an aggregate. */
1255 template <typename valtype>
1256 void
1257 ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
1258 int src_idx, HOST_WIDE_INT offset)
1260 ipcp_value_source<valtype> *src;
1262 src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
1263 src->offset = offset;
1264 src->cs = cs;
1265 src->val = src_val;
1266 src->index = src_idx;
1268 src->next = sources;
1269 sources = src;
1272 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1273 SOURCE and clear all other fields. */
1275 static ipcp_value<tree> *
1276 allocate_and_init_ipcp_value (tree source)
1278 ipcp_value<tree> *val;
1280 val = ipcp_cst_values_pool.allocate ();
1281 memset (val, 0, sizeof (*val));
1282 val->value = source;
1283 return val;
1286 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1287 value to SOURCE and clear all other fields. */
1289 static ipcp_value<ipa_polymorphic_call_context> *
1290 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source)
1292 ipcp_value<ipa_polymorphic_call_context> *val;
1294 // TODO
1295 val = ipcp_poly_ctx_values_pool.allocate ();
1296 memset (val, 0, sizeof (*val));
1297 val->value = source;
1298 return val;
1301 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1302 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1303 meaning. OFFSET -1 means the source is scalar and not a part of an
1304 aggregate. */
1306 template <typename valtype>
1307 bool
1308 ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
1309 ipcp_value<valtype> *src_val,
1310 int src_idx, HOST_WIDE_INT offset)
1312 ipcp_value<valtype> *val;
1314 if (bottom)
1315 return false;
1317 for (val = values; val; val = val->next)
1318 if (values_equal_for_ipcp_p (val->value, newval))
1320 if (ipa_edge_within_scc (cs))
1322 ipcp_value_source<valtype> *s;
1323 for (s = val->sources; s ; s = s->next)
1324 if (s->cs == cs)
1325 break;
1326 if (s)
1327 return false;
1330 val->add_source (cs, src_val, src_idx, offset);
1331 return false;
1334 if (values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
1336 /* We can only free sources, not the values themselves, because sources
1337 of other values in this SCC might point to them. */
1338 for (val = values; val; val = val->next)
1340 while (val->sources)
1342 ipcp_value_source<valtype> *src = val->sources;
1343 val->sources = src->next;
1344 ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
1348 values = NULL;
1349 return set_to_bottom ();
1352 values_count++;
1353 val = allocate_and_init_ipcp_value (newval);
1354 val->add_source (cs, src_val, src_idx, offset);
1355 val->next = values;
1356 values = val;
1357 return true;
1360 /* Propagate values through a pass-through jump function JFUNC associated with
1361 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1362 is the index of the source parameter. */
1364 static bool
1365 propagate_vals_accross_pass_through (cgraph_edge *cs,
1366 ipa_jump_func *jfunc,
1367 ipcp_lattice<tree> *src_lat,
1368 ipcp_lattice<tree> *dest_lat,
1369 int src_idx)
1371 ipcp_value<tree> *src_val;
1372 bool ret = false;
1374 /* Do not create new values when propagating within an SCC because if there
1375 are arithmetic functions with circular dependencies, there is infinite
1376 number of them and we would just make lattices bottom. */
1377 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1378 && ipa_edge_within_scc (cs))
1379 ret = dest_lat->set_contains_variable ();
1380 else
1381 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1383 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1385 if (cstval)
1386 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx);
1387 else
1388 ret |= dest_lat->set_contains_variable ();
1391 return ret;
1394 /* Propagate values through an ancestor jump function JFUNC associated with
1395 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1396 is the index of the source parameter. */
1398 static bool
1399 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1400 struct ipa_jump_func *jfunc,
1401 ipcp_lattice<tree> *src_lat,
1402 ipcp_lattice<tree> *dest_lat,
1403 int src_idx)
1405 ipcp_value<tree> *src_val;
1406 bool ret = false;
1408 if (ipa_edge_within_scc (cs))
1409 return dest_lat->set_contains_variable ();
1411 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1413 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1415 if (t)
1416 ret |= dest_lat->add_value (t, cs, src_val, src_idx);
1417 else
1418 ret |= dest_lat->set_contains_variable ();
1421 return ret;
1424 /* Propagate scalar values across jump function JFUNC that is associated with
1425 edge CS and put the values into DEST_LAT. */
1427 static bool
1428 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1429 struct ipa_jump_func *jfunc,
1430 ipcp_lattice<tree> *dest_lat)
1432 if (dest_lat->bottom)
1433 return false;
1435 if (jfunc->type == IPA_JF_CONST)
1437 tree val = ipa_get_jf_constant (jfunc);
1438 return dest_lat->add_value (val, cs, NULL, 0);
1440 else if (jfunc->type == IPA_JF_PASS_THROUGH
1441 || jfunc->type == IPA_JF_ANCESTOR)
1443 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1444 ipcp_lattice<tree> *src_lat;
1445 int src_idx;
1446 bool ret;
1448 if (jfunc->type == IPA_JF_PASS_THROUGH)
1449 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1450 else
1451 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1453 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1454 if (src_lat->bottom)
1455 return dest_lat->set_contains_variable ();
1457 /* If we would need to clone the caller and cannot, do not propagate. */
1458 if (!ipcp_versionable_function_p (cs->caller)
1459 && (src_lat->contains_variable
1460 || (src_lat->values_count > 1)))
1461 return dest_lat->set_contains_variable ();
1463 if (jfunc->type == IPA_JF_PASS_THROUGH)
1464 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1465 dest_lat, src_idx);
1466 else
1467 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1468 src_idx);
1470 if (src_lat->contains_variable)
1471 ret |= dest_lat->set_contains_variable ();
1473 return ret;
1476 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1477 use it for indirect inlining), we should propagate them too. */
1478 return dest_lat->set_contains_variable ();
1481 /* Propagate scalar values across jump function JFUNC that is associated with
1482 edge CS and describes argument IDX and put the values into DEST_LAT. */
1484 static bool
1485 propagate_context_accross_jump_function (cgraph_edge *cs,
1486 ipa_jump_func *jfunc, int idx,
1487 ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
1489 ipa_edge_args *args = IPA_EDGE_REF (cs);
1490 if (dest_lat->bottom)
1491 return false;
1492 bool ret = false;
1493 bool added_sth = false;
1494 bool type_preserved = true;
1496 ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
1497 = ipa_get_ith_polymorhic_call_context (args, idx);
1499 if (edge_ctx_ptr)
1500 edge_ctx = *edge_ctx_ptr;
1502 if (jfunc->type == IPA_JF_PASS_THROUGH
1503 || jfunc->type == IPA_JF_ANCESTOR)
1505 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1506 int src_idx;
1507 ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
1509 /* TODO: Once we figure out how to propagate speculations, it will
1510 probably be a good idea to switch to speculation if type_preserved is
1511 not set instead of punting. */
1512 if (jfunc->type == IPA_JF_PASS_THROUGH)
1514 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1515 goto prop_fail;
1516 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1517 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1519 else
1521 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1522 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1525 src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
1526 /* If we would need to clone the caller and cannot, do not propagate. */
1527 if (!ipcp_versionable_function_p (cs->caller)
1528 && (src_lat->contains_variable
1529 || (src_lat->values_count > 1)))
1530 goto prop_fail;
1532 ipcp_value<ipa_polymorphic_call_context> *src_val;
1533 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1535 ipa_polymorphic_call_context cur = src_val->value;
1537 if (!type_preserved)
1538 cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1539 if (jfunc->type == IPA_JF_ANCESTOR)
1540 cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1541 /* TODO: In cases we know how the context is going to be used,
1542 we can improve the result by passing proper OTR_TYPE. */
1543 cur.combine_with (edge_ctx);
1544 if (!cur.useless_p ())
1546 if (src_lat->contains_variable
1547 && !edge_ctx.equal_to (cur))
1548 ret |= dest_lat->set_contains_variable ();
1549 ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
1550 added_sth = true;
1556 prop_fail:
1557 if (!added_sth)
1559 if (!edge_ctx.useless_p ())
1560 ret |= dest_lat->add_value (edge_ctx, cs);
1561 else
1562 ret |= dest_lat->set_contains_variable ();
1565 return ret;
1568 /* Propagate alignments across jump function JFUNC that is associated with
1569 edge CS and update DEST_LAT accordingly. */
1571 static bool
1572 propagate_alignment_accross_jump_function (cgraph_edge *cs,
1573 ipa_jump_func *jfunc,
1574 ipcp_alignment_lattice *dest_lat)
1576 if (dest_lat->bottom_p ())
1577 return false;
1579 if (jfunc->type == IPA_JF_PASS_THROUGH
1580 || jfunc->type == IPA_JF_ANCESTOR)
1582 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1583 HOST_WIDE_INT offset = 0;
1584 int src_idx;
1586 if (jfunc->type == IPA_JF_PASS_THROUGH)
1588 enum tree_code op = ipa_get_jf_pass_through_operation (jfunc);
1589 if (op != NOP_EXPR)
1591 if (op != POINTER_PLUS_EXPR
1592 && op != PLUS_EXPR)
1593 return dest_lat->set_to_bottom ();
1594 tree operand = ipa_get_jf_pass_through_operand (jfunc);
1595 if (!tree_fits_shwi_p (operand))
1596 return dest_lat->set_to_bottom ();
1597 offset = tree_to_shwi (operand);
1599 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1601 else
1603 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1604 offset = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
1607 struct ipcp_param_lattices *src_lats;
1608 src_lats = ipa_get_parm_lattices (caller_info, src_idx);
1609 return dest_lat->meet_with (src_lats->alignment, offset);
1611 else
1613 if (jfunc->alignment.known)
1614 return dest_lat->meet_with (jfunc->alignment.align,
1615 jfunc->alignment.misalign);
1616 else
1617 return dest_lat->set_to_bottom ();
1621 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1622 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1623 other cases, return false). If there are no aggregate items, set
1624 aggs_by_ref to NEW_AGGS_BY_REF. */
1626 static bool
1627 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1628 bool new_aggs_by_ref)
1630 if (dest_plats->aggs)
1632 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1634 set_agg_lats_to_bottom (dest_plats);
1635 return true;
1638 else
1639 dest_plats->aggs_by_ref = new_aggs_by_ref;
1640 return false;
1643 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1644 already existing lattice for the given OFFSET and SIZE, marking all skipped
1645 lattices as containing variable and checking for overlaps. If there is no
1646 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1647 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1648 unless there are too many already. If there are two many, return false. If
1649 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1650 skipped lattices were newly marked as containing variable, set *CHANGE to
1651 true. */
1653 static bool
1654 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1655 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1656 struct ipcp_agg_lattice ***aglat,
1657 bool pre_existing, bool *change)
1659 gcc_checking_assert (offset >= 0);
1661 while (**aglat && (**aglat)->offset < offset)
1663 if ((**aglat)->offset + (**aglat)->size > offset)
1665 set_agg_lats_to_bottom (dest_plats);
1666 return false;
1668 *change |= (**aglat)->set_contains_variable ();
1669 *aglat = &(**aglat)->next;
1672 if (**aglat && (**aglat)->offset == offset)
1674 if ((**aglat)->size != val_size
1675 || ((**aglat)->next
1676 && (**aglat)->next->offset < offset + val_size))
1678 set_agg_lats_to_bottom (dest_plats);
1679 return false;
1681 gcc_checking_assert (!(**aglat)->next
1682 || (**aglat)->next->offset >= offset + val_size);
1683 return true;
1685 else
1687 struct ipcp_agg_lattice *new_al;
1689 if (**aglat && (**aglat)->offset < offset + val_size)
1691 set_agg_lats_to_bottom (dest_plats);
1692 return false;
1694 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1695 return false;
1696 dest_plats->aggs_count++;
1697 new_al = ipcp_agg_lattice_pool.allocate ();
1698 memset (new_al, 0, sizeof (*new_al));
1700 new_al->offset = offset;
1701 new_al->size = val_size;
1702 new_al->contains_variable = pre_existing;
1704 new_al->next = **aglat;
1705 **aglat = new_al;
1706 return true;
1710 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1711 containing an unknown value. */
1713 static bool
1714 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1716 bool ret = false;
1717 while (aglat)
1719 ret |= aglat->set_contains_variable ();
1720 aglat = aglat->next;
1722 return ret;
1725 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1726 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1727 parameter used for lattice value sources. Return true if DEST_PLATS changed
1728 in any way. */
1730 static bool
1731 merge_aggregate_lattices (struct cgraph_edge *cs,
1732 struct ipcp_param_lattices *dest_plats,
1733 struct ipcp_param_lattices *src_plats,
1734 int src_idx, HOST_WIDE_INT offset_delta)
1736 bool pre_existing = dest_plats->aggs != NULL;
1737 struct ipcp_agg_lattice **dst_aglat;
1738 bool ret = false;
1740 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1741 return true;
1742 if (src_plats->aggs_bottom)
1743 return set_agg_lats_contain_variable (dest_plats);
1744 if (src_plats->aggs_contain_variable)
1745 ret |= set_agg_lats_contain_variable (dest_plats);
1746 dst_aglat = &dest_plats->aggs;
1748 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1749 src_aglat;
1750 src_aglat = src_aglat->next)
1752 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1754 if (new_offset < 0)
1755 continue;
1756 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1757 &dst_aglat, pre_existing, &ret))
1759 struct ipcp_agg_lattice *new_al = *dst_aglat;
1761 dst_aglat = &(*dst_aglat)->next;
1762 if (src_aglat->bottom)
1764 ret |= new_al->set_contains_variable ();
1765 continue;
1767 if (src_aglat->contains_variable)
1768 ret |= new_al->set_contains_variable ();
1769 for (ipcp_value<tree> *val = src_aglat->values;
1770 val;
1771 val = val->next)
1772 ret |= new_al->add_value (val->value, cs, val, src_idx,
1773 src_aglat->offset);
1775 else if (dest_plats->aggs_bottom)
1776 return true;
1778 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1779 return ret;
1782 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1783 pass-through JFUNC and if so, whether it has conform and conforms to the
1784 rules about propagating values passed by reference. */
1786 static bool
1787 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1788 struct ipa_jump_func *jfunc)
1790 return src_plats->aggs
1791 && (!src_plats->aggs_by_ref
1792 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1795 /* Propagate scalar values across jump function JFUNC that is associated with
1796 edge CS and put the values into DEST_LAT. */
1798 static bool
1799 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1800 struct ipa_jump_func *jfunc,
1801 struct ipcp_param_lattices *dest_plats)
1803 bool ret = false;
1805 if (dest_plats->aggs_bottom)
1806 return false;
1808 if (jfunc->type == IPA_JF_PASS_THROUGH
1809 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1811 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1812 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1813 struct ipcp_param_lattices *src_plats;
1815 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1816 if (agg_pass_through_permissible_p (src_plats, jfunc))
1818 /* Currently we do not produce clobber aggregate jump
1819 functions, replace with merging when we do. */
1820 gcc_assert (!jfunc->agg.items);
1821 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1822 src_idx, 0);
1824 else
1825 ret |= set_agg_lats_contain_variable (dest_plats);
1827 else if (jfunc->type == IPA_JF_ANCESTOR
1828 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1830 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1831 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1832 struct ipcp_param_lattices *src_plats;
1834 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1835 if (src_plats->aggs && src_plats->aggs_by_ref)
1837 /* Currently we do not produce clobber aggregate jump
1838 functions, replace with merging when we do. */
1839 gcc_assert (!jfunc->agg.items);
1840 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1841 ipa_get_jf_ancestor_offset (jfunc));
1843 else if (!src_plats->aggs_by_ref)
1844 ret |= set_agg_lats_to_bottom (dest_plats);
1845 else
1846 ret |= set_agg_lats_contain_variable (dest_plats);
1848 else if (jfunc->agg.items)
1850 bool pre_existing = dest_plats->aggs != NULL;
1851 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1852 struct ipa_agg_jf_item *item;
1853 int i;
1855 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1856 return true;
1858 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1860 HOST_WIDE_INT val_size;
1862 if (item->offset < 0)
1863 continue;
1864 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1865 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1867 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1868 &aglat, pre_existing, &ret))
1870 ret |= (*aglat)->add_value (item->value, cs, NULL, 0, 0);
1871 aglat = &(*aglat)->next;
1873 else if (dest_plats->aggs_bottom)
1874 return true;
1877 ret |= set_chain_of_aglats_contains_variable (*aglat);
1879 else
1880 ret |= set_agg_lats_contain_variable (dest_plats);
1882 return ret;
1885 /* Propagate constants from the caller to the callee of CS. INFO describes the
1886 caller. */
1888 static bool
1889 propagate_constants_accross_call (struct cgraph_edge *cs)
1891 struct ipa_node_params *callee_info;
1892 enum availability availability;
1893 struct cgraph_node *callee, *alias_or_thunk;
1894 struct ipa_edge_args *args;
1895 bool ret = false;
1896 int i, args_count, parms_count;
1898 callee = cs->callee->function_symbol (&availability);
1899 if (!callee->definition)
1900 return false;
1901 gcc_checking_assert (callee->has_gimple_body_p ());
1902 callee_info = IPA_NODE_REF (callee);
1904 args = IPA_EDGE_REF (cs);
1905 args_count = ipa_get_cs_argument_count (args);
1906 parms_count = ipa_get_param_count (callee_info);
1907 if (parms_count == 0)
1908 return false;
1910 /* No propagation through instrumentation thunks is available yet.
1911 It should be possible with proper mapping of call args and
1912 instrumented callee params in the propagation loop below. But
1913 this case mostly occurs when legacy code calls instrumented code
1914 and it is not a primary target for optimizations.
1915 We detect instrumentation thunks in aliases and thunks chain by
1916 checking instrumentation_clone flag for chain source and target.
1917 Going through instrumentation thunks we always have it changed
1918 from 0 to 1 and all other nodes do not change it. */
1919 if (!cs->callee->instrumentation_clone
1920 && callee->instrumentation_clone)
1922 for (i = 0; i < parms_count; i++)
1923 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1924 i));
1925 return ret;
1928 /* If this call goes through a thunk we must not propagate to the first (0th)
1929 parameter. However, we might need to uncover a thunk from below a series
1930 of aliases first. */
1931 alias_or_thunk = cs->callee;
1932 while (alias_or_thunk->alias)
1933 alias_or_thunk = alias_or_thunk->get_alias_target ();
1934 if (alias_or_thunk->thunk.thunk_p)
1936 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1937 0));
1938 i = 1;
1940 else
1941 i = 0;
1943 for (; (i < args_count) && (i < parms_count); i++)
1945 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1946 struct ipcp_param_lattices *dest_plats;
1948 dest_plats = ipa_get_parm_lattices (callee_info, i);
1949 if (availability == AVAIL_INTERPOSABLE)
1950 ret |= set_all_contains_variable (dest_plats);
1951 else
1953 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1954 &dest_plats->itself);
1955 ret |= propagate_context_accross_jump_function (cs, jump_func, i,
1956 &dest_plats->ctxlat);
1957 ret |= propagate_alignment_accross_jump_function (cs, jump_func,
1958 &dest_plats->alignment);
1959 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1960 dest_plats);
1963 for (; i < parms_count; i++)
1964 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1966 return ret;
1969 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1970 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
1971 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
1973 static tree
1974 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1975 vec<tree> known_csts,
1976 vec<ipa_polymorphic_call_context> known_contexts,
1977 vec<ipa_agg_jump_function_p> known_aggs,
1978 struct ipa_agg_replacement_value *agg_reps,
1979 bool *speculative)
1981 int param_index = ie->indirect_info->param_index;
1982 HOST_WIDE_INT anc_offset;
1983 tree t;
1984 tree target = NULL;
1986 *speculative = false;
1988 if (param_index == -1
1989 || known_csts.length () <= (unsigned int) param_index)
1990 return NULL_TREE;
1992 if (!ie->indirect_info->polymorphic)
1994 tree t;
1996 if (ie->indirect_info->agg_contents)
1998 if (agg_reps)
2000 t = NULL;
2001 while (agg_reps)
2003 if (agg_reps->index == param_index
2004 && agg_reps->offset == ie->indirect_info->offset
2005 && agg_reps->by_ref == ie->indirect_info->by_ref)
2007 t = agg_reps->value;
2008 break;
2010 agg_reps = agg_reps->next;
2013 else if (known_aggs.length () > (unsigned int) param_index)
2015 struct ipa_agg_jump_function *agg;
2016 agg = known_aggs[param_index];
2017 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
2018 ie->indirect_info->by_ref);
2020 else
2021 t = NULL;
2023 else
2024 t = known_csts[param_index];
2026 if (t &&
2027 TREE_CODE (t) == ADDR_EXPR
2028 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
2029 return TREE_OPERAND (t, 0);
2030 else
2031 return NULL_TREE;
2034 if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
2035 return NULL_TREE;
2037 gcc_assert (!ie->indirect_info->agg_contents);
2038 anc_offset = ie->indirect_info->offset;
2040 t = NULL;
2042 /* Try to work out value of virtual table pointer value in replacemnets. */
2043 if (!t && agg_reps && !ie->indirect_info->by_ref)
2045 while (agg_reps)
2047 if (agg_reps->index == param_index
2048 && agg_reps->offset == ie->indirect_info->offset
2049 && agg_reps->by_ref)
2051 t = agg_reps->value;
2052 break;
2054 agg_reps = agg_reps->next;
2058 /* Try to work out value of virtual table pointer value in known
2059 aggregate values. */
2060 if (!t && known_aggs.length () > (unsigned int) param_index
2061 && !ie->indirect_info->by_ref)
2063 struct ipa_agg_jump_function *agg;
2064 agg = known_aggs[param_index];
2065 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
2066 true);
2069 /* If we found the virtual table pointer, lookup the target. */
2070 if (t)
2072 tree vtable;
2073 unsigned HOST_WIDE_INT offset;
2074 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
2076 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
2077 vtable, offset);
2078 if (target)
2080 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
2081 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
2082 || !possible_polymorphic_call_target_p
2083 (ie, cgraph_node::get (target)))
2084 target = ipa_impossible_devirt_target (ie, target);
2085 *speculative = ie->indirect_info->vptr_changed;
2086 if (!*speculative)
2087 return target;
2092 /* Do we know the constant value of pointer? */
2093 if (!t)
2094 t = known_csts[param_index];
2096 gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
2098 ipa_polymorphic_call_context context;
2099 if (known_contexts.length () > (unsigned int) param_index)
2101 context = known_contexts[param_index];
2102 context.offset_by (anc_offset);
2103 if (ie->indirect_info->vptr_changed)
2104 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2105 ie->indirect_info->otr_type);
2106 if (t)
2108 ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
2109 (t, ie->indirect_info->otr_type, anc_offset);
2110 if (!ctx2.useless_p ())
2111 context.combine_with (ctx2, ie->indirect_info->otr_type);
2114 else if (t)
2116 context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
2117 anc_offset);
2118 if (ie->indirect_info->vptr_changed)
2119 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
2120 ie->indirect_info->otr_type);
2122 else
2123 return NULL_TREE;
2125 vec <cgraph_node *>targets;
2126 bool final;
2128 targets = possible_polymorphic_call_targets
2129 (ie->indirect_info->otr_type,
2130 ie->indirect_info->otr_token,
2131 context, &final);
2132 if (!final || targets.length () > 1)
2134 struct cgraph_node *node;
2135 if (*speculative)
2136 return target;
2137 if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
2138 || ie->speculative || !ie->maybe_hot_p ())
2139 return NULL;
2140 node = try_speculative_devirtualization (ie->indirect_info->otr_type,
2141 ie->indirect_info->otr_token,
2142 context);
2143 if (node)
2145 *speculative = true;
2146 target = node->decl;
2148 else
2149 return NULL;
2151 else
2153 *speculative = false;
2154 if (targets.length () == 1)
2155 target = targets[0]->decl;
2156 else
2157 target = ipa_impossible_devirt_target (ie, NULL_TREE);
2160 if (target && !possible_polymorphic_call_target_p (ie,
2161 cgraph_node::get (target)))
2162 target = ipa_impossible_devirt_target (ie, target);
2164 return target;
2168 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2169 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2170 return the destination. */
2172 tree
2173 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
2174 vec<tree> known_csts,
2175 vec<ipa_polymorphic_call_context> known_contexts,
2176 vec<ipa_agg_jump_function_p> known_aggs,
2177 bool *speculative)
2179 return ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2180 known_aggs, NULL, speculative);
2183 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2184 and KNOWN_CONTEXTS. */
2186 static int
2187 devirtualization_time_bonus (struct cgraph_node *node,
2188 vec<tree> known_csts,
2189 vec<ipa_polymorphic_call_context> known_contexts,
2190 vec<ipa_agg_jump_function_p> known_aggs)
2192 struct cgraph_edge *ie;
2193 int res = 0;
2195 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
2197 struct cgraph_node *callee;
2198 struct inline_summary *isummary;
2199 enum availability avail;
2200 tree target;
2201 bool speculative;
2203 target = ipa_get_indirect_edge_target (ie, known_csts, known_contexts,
2204 known_aggs, &speculative);
2205 if (!target)
2206 continue;
2208 /* Only bare minimum benefit for clearly un-inlineable targets. */
2209 res += 1;
2210 callee = cgraph_node::get (target);
2211 if (!callee || !callee->definition)
2212 continue;
2213 callee = callee->function_symbol (&avail);
2214 if (avail < AVAIL_AVAILABLE)
2215 continue;
2216 isummary = inline_summaries->get (callee);
2217 if (!isummary->inlinable)
2218 continue;
2220 /* FIXME: The values below need re-considering and perhaps also
2221 integrating into the cost metrics, at lest in some very basic way. */
2222 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
2223 res += 31 / ((int)speculative + 1);
2224 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
2225 res += 15 / ((int)speculative + 1);
2226 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
2227 || DECL_DECLARED_INLINE_P (callee->decl))
2228 res += 7 / ((int)speculative + 1);
2231 return res;
2234 /* Return time bonus incurred because of HINTS. */
2236 static int
2237 hint_time_bonus (inline_hints hints)
2239 int result = 0;
2240 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
2241 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
2242 if (hints & INLINE_HINT_array_index)
2243 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
2244 return result;
2247 /* If there is a reason to penalize the function described by INFO in the
2248 cloning goodness evaluation, do so. */
2250 static inline int64_t
2251 incorporate_penalties (ipa_node_params *info, int64_t evaluation)
2253 if (info->node_within_scc)
2254 evaluation = (evaluation
2255 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY))) / 100;
2257 if (info->node_calling_single_call)
2258 evaluation = (evaluation
2259 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY)))
2260 / 100;
2262 return evaluation;
2265 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2266 and SIZE_COST and with the sum of frequencies of incoming edges to the
2267 potential new clone in FREQUENCIES. */
2269 static bool
2270 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
2271 int freq_sum, gcov_type count_sum, int size_cost)
2273 if (time_benefit == 0
2274 || !opt_for_fn (node->decl, flag_ipa_cp_clone)
2275 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
2276 return false;
2278 gcc_assert (size_cost > 0);
2280 struct ipa_node_params *info = IPA_NODE_REF (node);
2281 if (max_count)
2283 int factor = (count_sum * 1000) / max_count;
2284 int64_t evaluation = (((int64_t) time_benefit * factor)
2285 / size_cost);
2286 evaluation = incorporate_penalties (info, evaluation);
2288 if (dump_file && (dump_flags & TDF_DETAILS))
2289 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2290 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
2291 "%s%s) -> evaluation: " "%" PRId64
2292 ", threshold: %i\n",
2293 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
2294 info->node_within_scc ? ", scc" : "",
2295 info->node_calling_single_call ? ", single_call" : "",
2296 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2298 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2300 else
2302 int64_t evaluation = (((int64_t) time_benefit * freq_sum)
2303 / size_cost);
2304 evaluation = incorporate_penalties (info, evaluation);
2306 if (dump_file && (dump_flags & TDF_DETAILS))
2307 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
2308 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2309 "%" PRId64 ", threshold: %i\n",
2310 time_benefit, size_cost, freq_sum,
2311 info->node_within_scc ? ", scc" : "",
2312 info->node_calling_single_call ? ", single_call" : "",
2313 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
2315 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
2319 /* Return all context independent values from aggregate lattices in PLATS in a
2320 vector. Return NULL if there are none. */
2322 static vec<ipa_agg_jf_item, va_gc> *
2323 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
2325 vec<ipa_agg_jf_item, va_gc> *res = NULL;
2327 if (plats->aggs_bottom
2328 || plats->aggs_contain_variable
2329 || plats->aggs_count == 0)
2330 return NULL;
2332 for (struct ipcp_agg_lattice *aglat = plats->aggs;
2333 aglat;
2334 aglat = aglat->next)
2335 if (aglat->is_single_const ())
2337 struct ipa_agg_jf_item item;
2338 item.offset = aglat->offset;
2339 item.value = aglat->values->value;
2340 vec_safe_push (res, item);
2342 return res;
2345 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2346 populate them with values of parameters that are known independent of the
2347 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2348 non-NULL, the movement cost of all removable parameters will be stored in
2349 it. */
2351 static bool
2352 gather_context_independent_values (struct ipa_node_params *info,
2353 vec<tree> *known_csts,
2354 vec<ipa_polymorphic_call_context>
2355 *known_contexts,
2356 vec<ipa_agg_jump_function> *known_aggs,
2357 int *removable_params_cost)
2359 int i, count = ipa_get_param_count (info);
2360 bool ret = false;
2362 known_csts->create (0);
2363 known_contexts->create (0);
2364 known_csts->safe_grow_cleared (count);
2365 known_contexts->safe_grow_cleared (count);
2366 if (known_aggs)
2368 known_aggs->create (0);
2369 known_aggs->safe_grow_cleared (count);
2372 if (removable_params_cost)
2373 *removable_params_cost = 0;
2375 for (i = 0; i < count ; i++)
2377 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2378 ipcp_lattice<tree> *lat = &plats->itself;
2380 if (lat->is_single_const ())
2382 ipcp_value<tree> *val = lat->values;
2383 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2384 (*known_csts)[i] = val->value;
2385 if (removable_params_cost)
2386 *removable_params_cost
2387 += estimate_move_cost (TREE_TYPE (val->value), false);
2388 ret = true;
2390 else if (removable_params_cost
2391 && !ipa_is_param_used (info, i))
2392 *removable_params_cost
2393 += ipa_get_param_move_cost (info, i);
2395 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2396 if (ctxlat->is_single_const ())
2398 (*known_contexts)[i] = ctxlat->values->value;
2399 ret = true;
2402 if (known_aggs)
2404 vec<ipa_agg_jf_item, va_gc> *agg_items;
2405 struct ipa_agg_jump_function *ajf;
2407 agg_items = context_independent_aggregate_values (plats);
2408 ajf = &(*known_aggs)[i];
2409 ajf->items = agg_items;
2410 ajf->by_ref = plats->aggs_by_ref;
2411 ret |= agg_items != NULL;
2415 return ret;
2418 /* The current interface in ipa-inline-analysis requires a pointer vector.
2419 Create it.
2421 FIXME: That interface should be re-worked, this is slightly silly. Still,
2422 I'd like to discuss how to change it first and this demonstrates the
2423 issue. */
2425 static vec<ipa_agg_jump_function_p>
2426 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
2428 vec<ipa_agg_jump_function_p> ret;
2429 struct ipa_agg_jump_function *ajf;
2430 int i;
2432 ret.create (known_aggs.length ());
2433 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
2434 ret.quick_push (ajf);
2435 return ret;
2438 /* Perform time and size measurement of NODE with the context given in
2439 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2440 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2441 all context-independent removable parameters and EST_MOVE_COST of estimated
2442 movement of the considered parameter and store it into VAL. */
2444 static void
2445 perform_estimation_of_a_value (cgraph_node *node, vec<tree> known_csts,
2446 vec<ipa_polymorphic_call_context> known_contexts,
2447 vec<ipa_agg_jump_function_p> known_aggs_ptrs,
2448 int base_time, int removable_params_cost,
2449 int est_move_cost, ipcp_value_base *val)
2451 int time, size, time_benefit;
2452 inline_hints hints;
2454 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2455 known_aggs_ptrs, &size, &time,
2456 &hints);
2457 time_benefit = base_time - time
2458 + devirtualization_time_bonus (node, known_csts, known_contexts,
2459 known_aggs_ptrs)
2460 + hint_time_bonus (hints)
2461 + removable_params_cost + est_move_cost;
2463 gcc_checking_assert (size >=0);
2464 /* The inliner-heuristics based estimates may think that in certain
2465 contexts some functions do not have any size at all but we want
2466 all specializations to have at least a tiny cost, not least not to
2467 divide by zero. */
2468 if (size == 0)
2469 size = 1;
2471 val->local_time_benefit = time_benefit;
2472 val->local_size_cost = size;
2475 /* Iterate over known values of parameters of NODE and estimate the local
2476 effects in terms of time and size they have. */
2478 static void
2479 estimate_local_effects (struct cgraph_node *node)
2481 struct ipa_node_params *info = IPA_NODE_REF (node);
2482 int i, count = ipa_get_param_count (info);
2483 vec<tree> known_csts;
2484 vec<ipa_polymorphic_call_context> known_contexts;
2485 vec<ipa_agg_jump_function> known_aggs;
2486 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
2487 bool always_const;
2488 int base_time = inline_summaries->get (node)->time;
2489 int removable_params_cost;
2491 if (!count || !ipcp_versionable_function_p (node))
2492 return;
2494 if (dump_file && (dump_flags & TDF_DETAILS))
2495 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
2496 node->name (), node->order, base_time);
2498 always_const = gather_context_independent_values (info, &known_csts,
2499 &known_contexts, &known_aggs,
2500 &removable_params_cost);
2501 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
2502 if (always_const)
2504 struct caller_statistics stats;
2505 inline_hints hints;
2506 int time, size;
2508 init_caller_stats (&stats);
2509 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2510 false);
2511 estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts,
2512 known_aggs_ptrs, &size, &time, &hints);
2513 time -= devirtualization_time_bonus (node, known_csts, known_contexts,
2514 known_aggs_ptrs);
2515 time -= hint_time_bonus (hints);
2516 time -= removable_params_cost;
2517 size -= stats.n_calls * removable_params_cost;
2519 if (dump_file)
2520 fprintf (dump_file, " - context independent values, size: %i, "
2521 "time_benefit: %i\n", size, base_time - time);
2523 if (size <= 0
2524 || node->will_be_removed_from_program_if_no_direct_calls_p ())
2526 info->do_clone_for_all_contexts = true;
2527 base_time = time;
2529 if (dump_file)
2530 fprintf (dump_file, " Decided to specialize for all "
2531 "known contexts, code not going to grow.\n");
2533 else if (good_cloning_opportunity_p (node, base_time - time,
2534 stats.freq_sum, stats.count_sum,
2535 size))
2537 if (size + overall_size <= max_new_size)
2539 info->do_clone_for_all_contexts = true;
2540 base_time = time;
2541 overall_size += size;
2543 if (dump_file)
2544 fprintf (dump_file, " Decided to specialize for all "
2545 "known contexts, growth deemed beneficial.\n");
2547 else if (dump_file && (dump_flags & TDF_DETAILS))
2548 fprintf (dump_file, " Not cloning for all contexts because "
2549 "max_new_size would be reached with %li.\n",
2550 size + overall_size);
2554 for (i = 0; i < count ; i++)
2556 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2557 ipcp_lattice<tree> *lat = &plats->itself;
2558 ipcp_value<tree> *val;
2560 if (lat->bottom
2561 || !lat->values
2562 || known_csts[i])
2563 continue;
2565 for (val = lat->values; val; val = val->next)
2567 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
2568 known_csts[i] = val->value;
2570 int emc = estimate_move_cost (TREE_TYPE (val->value), true);
2571 perform_estimation_of_a_value (node, known_csts, known_contexts,
2572 known_aggs_ptrs, base_time,
2573 removable_params_cost, emc, val);
2575 if (dump_file && (dump_flags & TDF_DETAILS))
2577 fprintf (dump_file, " - estimates for value ");
2578 print_ipcp_constant_value (dump_file, val->value);
2579 fprintf (dump_file, " for ");
2580 ipa_dump_param (dump_file, info, i);
2581 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2582 val->local_time_benefit, val->local_size_cost);
2585 known_csts[i] = NULL_TREE;
2588 for (i = 0; i < count; i++)
2590 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2592 if (!plats->virt_call)
2593 continue;
2595 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2596 ipcp_value<ipa_polymorphic_call_context> *val;
2598 if (ctxlat->bottom
2599 || !ctxlat->values
2600 || !known_contexts[i].useless_p ())
2601 continue;
2603 for (val = ctxlat->values; val; val = val->next)
2605 known_contexts[i] = val->value;
2606 perform_estimation_of_a_value (node, known_csts, known_contexts,
2607 known_aggs_ptrs, base_time,
2608 removable_params_cost, 0, val);
2610 if (dump_file && (dump_flags & TDF_DETAILS))
2612 fprintf (dump_file, " - estimates for polymorphic context ");
2613 print_ipcp_constant_value (dump_file, val->value);
2614 fprintf (dump_file, " for ");
2615 ipa_dump_param (dump_file, info, i);
2616 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2617 val->local_time_benefit, val->local_size_cost);
2620 known_contexts[i] = ipa_polymorphic_call_context ();
2623 for (i = 0; i < count ; i++)
2625 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2626 struct ipa_agg_jump_function *ajf;
2627 struct ipcp_agg_lattice *aglat;
2629 if (plats->aggs_bottom || !plats->aggs)
2630 continue;
2632 ajf = &known_aggs[i];
2633 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2635 ipcp_value<tree> *val;
2636 if (aglat->bottom || !aglat->values
2637 /* If the following is true, the one value is in known_aggs. */
2638 || (!plats->aggs_contain_variable
2639 && aglat->is_single_const ()))
2640 continue;
2642 for (val = aglat->values; val; val = val->next)
2644 struct ipa_agg_jf_item item;
2646 item.offset = aglat->offset;
2647 item.value = val->value;
2648 vec_safe_push (ajf->items, item);
2650 perform_estimation_of_a_value (node, known_csts, known_contexts,
2651 known_aggs_ptrs, base_time,
2652 removable_params_cost, 0, val);
2654 if (dump_file && (dump_flags & TDF_DETAILS))
2656 fprintf (dump_file, " - estimates for value ");
2657 print_ipcp_constant_value (dump_file, val->value);
2658 fprintf (dump_file, " for ");
2659 ipa_dump_param (dump_file, info, i);
2660 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2661 "]: time_benefit: %i, size: %i\n",
2662 plats->aggs_by_ref ? "ref " : "",
2663 aglat->offset,
2664 val->local_time_benefit, val->local_size_cost);
2667 ajf->items->pop ();
2672 for (i = 0; i < count ; i++)
2673 vec_free (known_aggs[i].items);
2675 known_csts.release ();
2676 known_contexts.release ();
2677 known_aggs.release ();
2678 known_aggs_ptrs.release ();
2682 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2683 topological sort of values. */
2685 template <typename valtype>
2686 void
2687 value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
2689 ipcp_value_source<valtype> *src;
2691 if (cur_val->dfs)
2692 return;
2694 dfs_counter++;
2695 cur_val->dfs = dfs_counter;
2696 cur_val->low_link = dfs_counter;
2698 cur_val->topo_next = stack;
2699 stack = cur_val;
2700 cur_val->on_stack = true;
2702 for (src = cur_val->sources; src; src = src->next)
2703 if (src->val)
2705 if (src->val->dfs == 0)
2707 add_val (src->val);
2708 if (src->val->low_link < cur_val->low_link)
2709 cur_val->low_link = src->val->low_link;
2711 else if (src->val->on_stack
2712 && src->val->dfs < cur_val->low_link)
2713 cur_val->low_link = src->val->dfs;
2716 if (cur_val->dfs == cur_val->low_link)
2718 ipcp_value<valtype> *v, *scc_list = NULL;
2722 v = stack;
2723 stack = v->topo_next;
2724 v->on_stack = false;
2726 v->scc_next = scc_list;
2727 scc_list = v;
2729 while (v != cur_val);
2731 cur_val->topo_next = values_topo;
2732 values_topo = cur_val;
2736 /* Add all values in lattices associated with NODE to the topological sort if
2737 they are not there yet. */
2739 static void
2740 add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
2742 struct ipa_node_params *info = IPA_NODE_REF (node);
2743 int i, count = ipa_get_param_count (info);
2745 for (i = 0; i < count ; i++)
2747 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2748 ipcp_lattice<tree> *lat = &plats->itself;
2749 struct ipcp_agg_lattice *aglat;
2751 if (!lat->bottom)
2753 ipcp_value<tree> *val;
2754 for (val = lat->values; val; val = val->next)
2755 topo->constants.add_val (val);
2758 if (!plats->aggs_bottom)
2759 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2760 if (!aglat->bottom)
2762 ipcp_value<tree> *val;
2763 for (val = aglat->values; val; val = val->next)
2764 topo->constants.add_val (val);
2767 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
2768 if (!ctxlat->bottom)
2770 ipcp_value<ipa_polymorphic_call_context> *ctxval;
2771 for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
2772 topo->contexts.add_val (ctxval);
2777 /* One pass of constants propagation along the call graph edges, from callers
2778 to callees (requires topological ordering in TOPO), iterate over strongly
2779 connected components. */
2781 static void
2782 propagate_constants_topo (struct ipa_topo_info *topo)
2784 int i;
2786 for (i = topo->nnodes - 1; i >= 0; i--)
2788 unsigned j;
2789 struct cgraph_node *v, *node = topo->order[i];
2790 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
2792 /* First, iteratively propagate within the strongly connected component
2793 until all lattices stabilize. */
2794 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2795 if (v->has_gimple_body_p ())
2796 push_node_to_stack (topo, v);
2798 v = pop_node_from_stack (topo);
2799 while (v)
2801 struct cgraph_edge *cs;
2803 for (cs = v->callees; cs; cs = cs->next_callee)
2804 if (ipa_edge_within_scc (cs))
2806 IPA_NODE_REF (v)->node_within_scc = true;
2807 if (propagate_constants_accross_call (cs))
2808 push_node_to_stack (topo, cs->callee->function_symbol ());
2810 v = pop_node_from_stack (topo);
2813 /* Afterwards, propagate along edges leading out of the SCC, calculates
2814 the local effects of the discovered constants and all valid values to
2815 their topological sort. */
2816 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2817 if (v->has_gimple_body_p ())
2819 struct cgraph_edge *cs;
2821 estimate_local_effects (v);
2822 add_all_node_vals_to_toposort (v, topo);
2823 for (cs = v->callees; cs; cs = cs->next_callee)
2824 if (!ipa_edge_within_scc (cs))
2825 propagate_constants_accross_call (cs);
2827 cycle_nodes.release ();
2832 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2833 the bigger one if otherwise. */
2835 static int
2836 safe_add (int a, int b)
2838 if (a > INT_MAX/2 || b > INT_MAX/2)
2839 return a > b ? a : b;
2840 else
2841 return a + b;
2845 /* Propagate the estimated effects of individual values along the topological
2846 from the dependent values to those they depend on. */
2848 template <typename valtype>
2849 void
2850 value_topo_info<valtype>::propagate_effects ()
2852 ipcp_value<valtype> *base;
2854 for (base = values_topo; base; base = base->topo_next)
2856 ipcp_value_source<valtype> *src;
2857 ipcp_value<valtype> *val;
2858 int time = 0, size = 0;
2860 for (val = base; val; val = val->scc_next)
2862 time = safe_add (time,
2863 val->local_time_benefit + val->prop_time_benefit);
2864 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2867 for (val = base; val; val = val->scc_next)
2868 for (src = val->sources; src; src = src->next)
2869 if (src->val
2870 && src->cs->maybe_hot_p ())
2872 src->val->prop_time_benefit = safe_add (time,
2873 src->val->prop_time_benefit);
2874 src->val->prop_size_cost = safe_add (size,
2875 src->val->prop_size_cost);
2881 /* Propagate constants, polymorphic contexts and their effects from the
2882 summaries interprocedurally. */
2884 static void
2885 ipcp_propagate_stage (struct ipa_topo_info *topo)
2887 struct cgraph_node *node;
2889 if (dump_file)
2890 fprintf (dump_file, "\n Propagating constants:\n\n");
2892 if (in_lto_p)
2893 ipa_update_after_lto_read ();
2896 FOR_EACH_DEFINED_FUNCTION (node)
2898 struct ipa_node_params *info = IPA_NODE_REF (node);
2900 determine_versionability (node, info);
2901 if (node->has_gimple_body_p ())
2903 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2904 ipa_get_param_count (info));
2905 initialize_node_lattices (node);
2907 if (node->definition && !node->alias)
2908 overall_size += inline_summaries->get (node)->self_size;
2909 if (node->count > max_count)
2910 max_count = node->count;
2913 max_new_size = overall_size;
2914 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2915 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2916 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2918 if (dump_file)
2919 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2920 overall_size, max_new_size);
2922 propagate_constants_topo (topo);
2923 #ifdef ENABLE_CHECKING
2924 ipcp_verify_propagated_values ();
2925 #endif
2926 topo->constants.propagate_effects ();
2927 topo->contexts.propagate_effects ();
2929 if (dump_file)
2931 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2932 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2936 /* Discover newly direct outgoing edges from NODE which is a new clone with
2937 known KNOWN_CSTS and make them direct. */
2939 static void
2940 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2941 vec<tree> known_csts,
2942 vec<ipa_polymorphic_call_context>
2943 known_contexts,
2944 struct ipa_agg_replacement_value *aggvals)
2946 struct cgraph_edge *ie, *next_ie;
2947 bool found = false;
2949 for (ie = node->indirect_calls; ie; ie = next_ie)
2951 tree target;
2952 bool speculative;
2954 next_ie = ie->next_callee;
2955 target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
2956 vNULL, aggvals, &speculative);
2957 if (target)
2959 bool agg_contents = ie->indirect_info->agg_contents;
2960 bool polymorphic = ie->indirect_info->polymorphic;
2961 int param_index = ie->indirect_info->param_index;
2962 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
2963 speculative);
2964 found = true;
2966 if (cs && !agg_contents && !polymorphic)
2968 struct ipa_node_params *info = IPA_NODE_REF (node);
2969 int c = ipa_get_controlled_uses (info, param_index);
2970 if (c != IPA_UNDESCRIBED_USE)
2972 struct ipa_ref *to_del;
2974 c--;
2975 ipa_set_controlled_uses (info, param_index, c);
2976 if (dump_file && (dump_flags & TDF_DETAILS))
2977 fprintf (dump_file, " controlled uses count of param "
2978 "%i bumped down to %i\n", param_index, c);
2979 if (c == 0
2980 && (to_del = node->find_reference (cs->callee, NULL, 0)))
2982 if (dump_file && (dump_flags & TDF_DETAILS))
2983 fprintf (dump_file, " and even removing its "
2984 "cloning-created reference\n");
2985 to_del->remove_reference ();
2991 /* Turning calls to direct calls will improve overall summary. */
2992 if (found)
2993 inline_update_overall_summary (node);
2996 /* Vector of pointers which for linked lists of clones of an original crgaph
2997 edge. */
2999 static vec<cgraph_edge *> next_edge_clone;
3000 static vec<cgraph_edge *> prev_edge_clone;
3002 static inline void
3003 grow_edge_clone_vectors (void)
3005 if (next_edge_clone.length ()
3006 <= (unsigned) symtab->edges_max_uid)
3007 next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
3008 if (prev_edge_clone.length ()
3009 <= (unsigned) symtab->edges_max_uid)
3010 prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
3013 /* Edge duplication hook to grow the appropriate linked list in
3014 next_edge_clone. */
3016 static void
3017 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3018 void *)
3020 grow_edge_clone_vectors ();
3022 struct cgraph_edge *old_next = next_edge_clone[src->uid];
3023 if (old_next)
3024 prev_edge_clone[old_next->uid] = dst;
3025 prev_edge_clone[dst->uid] = src;
3027 next_edge_clone[dst->uid] = old_next;
3028 next_edge_clone[src->uid] = dst;
3031 /* Hook that is called by cgraph.c when an edge is removed. */
3033 static void
3034 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
3036 grow_edge_clone_vectors ();
3038 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
3039 struct cgraph_edge *next = next_edge_clone[cs->uid];
3040 if (prev)
3041 next_edge_clone[prev->uid] = next;
3042 if (next)
3043 prev_edge_clone[next->uid] = prev;
3046 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
3047 parameter with the given INDEX. */
3049 static tree
3050 get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset,
3051 int index)
3053 struct ipa_agg_replacement_value *aggval;
3055 aggval = ipa_get_agg_replacements_for_node (node);
3056 while (aggval)
3058 if (aggval->offset == offset
3059 && aggval->index == index)
3060 return aggval->value;
3061 aggval = aggval->next;
3063 return NULL_TREE;
3066 /* Return true is NODE is DEST or its clone for all contexts. */
3068 static bool
3069 same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest)
3071 if (node == dest)
3072 return true;
3074 struct ipa_node_params *info = IPA_NODE_REF (node);
3075 return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
3078 /* Return true if edge CS does bring about the value described by SRC to node
3079 DEST or its clone for all contexts. */
3081 static bool
3082 cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
3083 cgraph_node *dest)
3085 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3086 enum availability availability;
3087 cgraph_node *real_dest = cs->callee->function_symbol (&availability);
3089 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3090 || availability <= AVAIL_INTERPOSABLE
3091 || caller_info->node_dead)
3092 return false;
3093 if (!src->val)
3094 return true;
3096 if (caller_info->ipcp_orig_node)
3098 tree t;
3099 if (src->offset == -1)
3100 t = caller_info->known_csts[src->index];
3101 else
3102 t = get_clone_agg_value (cs->caller, src->offset, src->index);
3103 return (t != NULL_TREE
3104 && values_equal_for_ipcp_p (src->val->value, t));
3106 else
3108 struct ipcp_agg_lattice *aglat;
3109 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3110 src->index);
3111 if (src->offset == -1)
3112 return (plats->itself.is_single_const ()
3113 && values_equal_for_ipcp_p (src->val->value,
3114 plats->itself.values->value));
3115 else
3117 if (plats->aggs_bottom || plats->aggs_contain_variable)
3118 return false;
3119 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3120 if (aglat->offset == src->offset)
3121 return (aglat->is_single_const ()
3122 && values_equal_for_ipcp_p (src->val->value,
3123 aglat->values->value));
3125 return false;
3129 /* Return true if edge CS does bring about the value described by SRC to node
3130 DEST or its clone for all contexts. */
3132 static bool
3133 cgraph_edge_brings_value_p (cgraph_edge *cs,
3134 ipcp_value_source<ipa_polymorphic_call_context> *src,
3135 cgraph_node *dest)
3137 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3138 cgraph_node *real_dest = cs->callee->function_symbol ();
3140 if (!same_node_or_its_all_contexts_clone_p (real_dest, dest)
3141 || caller_info->node_dead)
3142 return false;
3143 if (!src->val)
3144 return true;
3146 if (caller_info->ipcp_orig_node)
3147 return (caller_info->known_contexts.length () > (unsigned) src->index)
3148 && values_equal_for_ipcp_p (src->val->value,
3149 caller_info->known_contexts[src->index]);
3151 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
3152 src->index);
3153 return plats->ctxlat.is_single_const ()
3154 && values_equal_for_ipcp_p (src->val->value,
3155 plats->ctxlat.values->value);
3158 /* Get the next clone in the linked list of clones of an edge. */
3160 static inline struct cgraph_edge *
3161 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
3163 return next_edge_clone[cs->uid];
3166 /* Given VAL that is intended for DEST, iterate over all its sources and if
3167 they still hold, add their edge frequency and their number into *FREQUENCY
3168 and *CALLER_COUNT respectively. */
3170 template <typename valtype>
3171 static bool
3172 get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
3173 int *freq_sum,
3174 gcov_type *count_sum, int *caller_count)
3176 ipcp_value_source<valtype> *src;
3177 int freq = 0, count = 0;
3178 gcov_type cnt = 0;
3179 bool hot = false;
3181 for (src = val->sources; src; src = src->next)
3183 struct cgraph_edge *cs = src->cs;
3184 while (cs)
3186 if (cgraph_edge_brings_value_p (cs, src, dest))
3188 count++;
3189 freq += cs->frequency;
3190 cnt += cs->count;
3191 hot |= cs->maybe_hot_p ();
3193 cs = get_next_cgraph_edge_clone (cs);
3197 *freq_sum = freq;
3198 *count_sum = cnt;
3199 *caller_count = count;
3200 return hot;
3203 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3204 is assumed their number is known and equal to CALLER_COUNT. */
3206 template <typename valtype>
3207 static vec<cgraph_edge *>
3208 gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
3209 int caller_count)
3211 ipcp_value_source<valtype> *src;
3212 vec<cgraph_edge *> ret;
3214 ret.create (caller_count);
3215 for (src = val->sources; src; src = src->next)
3217 struct cgraph_edge *cs = src->cs;
3218 while (cs)
3220 if (cgraph_edge_brings_value_p (cs, src, dest))
3221 ret.quick_push (cs);
3222 cs = get_next_cgraph_edge_clone (cs);
3226 return ret;
3229 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3230 Return it or NULL if for some reason it cannot be created. */
3232 static struct ipa_replace_map *
3233 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
3235 struct ipa_replace_map *replace_map;
3238 replace_map = ggc_alloc<ipa_replace_map> ();
3239 if (dump_file)
3241 fprintf (dump_file, " replacing ");
3242 ipa_dump_param (dump_file, info, parm_num);
3244 fprintf (dump_file, " with const ");
3245 print_generic_expr (dump_file, value, 0);
3246 fprintf (dump_file, "\n");
3248 replace_map->old_tree = NULL;
3249 replace_map->parm_num = parm_num;
3250 replace_map->new_tree = value;
3251 replace_map->replace_p = true;
3252 replace_map->ref_p = false;
3254 return replace_map;
3257 /* Dump new profiling counts */
3259 static void
3260 dump_profile_updates (struct cgraph_node *orig_node,
3261 struct cgraph_node *new_node)
3263 struct cgraph_edge *cs;
3265 fprintf (dump_file, " setting count of the specialized node to "
3266 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
3267 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3268 fprintf (dump_file, " edge to %s has count "
3269 HOST_WIDE_INT_PRINT_DEC "\n",
3270 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3272 fprintf (dump_file, " setting count of the original node to "
3273 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
3274 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3275 fprintf (dump_file, " edge to %s is left with "
3276 HOST_WIDE_INT_PRINT_DEC "\n",
3277 cs->callee->name (), (HOST_WIDE_INT) cs->count);
3280 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3281 their profile information to reflect this. */
3283 static void
3284 update_profiling_info (struct cgraph_node *orig_node,
3285 struct cgraph_node *new_node)
3287 struct cgraph_edge *cs;
3288 struct caller_statistics stats;
3289 gcov_type new_sum, orig_sum;
3290 gcov_type remainder, orig_node_count = orig_node->count;
3292 if (orig_node_count == 0)
3293 return;
3295 init_caller_stats (&stats);
3296 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3297 false);
3298 orig_sum = stats.count_sum;
3299 init_caller_stats (&stats);
3300 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3301 false);
3302 new_sum = stats.count_sum;
3304 if (orig_node_count < orig_sum + new_sum)
3306 if (dump_file)
3307 fprintf (dump_file, " Problem: node %s/%i has too low count "
3308 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
3309 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
3310 orig_node->name (), orig_node->order,
3311 (HOST_WIDE_INT) orig_node_count,
3312 (HOST_WIDE_INT) (orig_sum + new_sum));
3314 orig_node_count = (orig_sum + new_sum) * 12 / 10;
3315 if (dump_file)
3316 fprintf (dump_file, " proceeding by pretending it was "
3317 HOST_WIDE_INT_PRINT_DEC "\n",
3318 (HOST_WIDE_INT) orig_node_count);
3321 new_node->count = new_sum;
3322 remainder = orig_node_count - new_sum;
3323 orig_node->count = remainder;
3325 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3326 if (cs->frequency)
3327 cs->count = apply_probability (cs->count,
3328 GCOV_COMPUTE_SCALE (new_sum,
3329 orig_node_count));
3330 else
3331 cs->count = 0;
3333 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3334 cs->count = apply_probability (cs->count,
3335 GCOV_COMPUTE_SCALE (remainder,
3336 orig_node_count));
3338 if (dump_file)
3339 dump_profile_updates (orig_node, new_node);
3342 /* Update the respective profile of specialized NEW_NODE and the original
3343 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3344 have been redirected to the specialized version. */
3346 static void
3347 update_specialized_profile (struct cgraph_node *new_node,
3348 struct cgraph_node *orig_node,
3349 gcov_type redirected_sum)
3351 struct cgraph_edge *cs;
3352 gcov_type new_node_count, orig_node_count = orig_node->count;
3354 if (dump_file)
3355 fprintf (dump_file, " the sum of counts of redirected edges is "
3356 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
3357 if (orig_node_count == 0)
3358 return;
3360 gcc_assert (orig_node_count >= redirected_sum);
3362 new_node_count = new_node->count;
3363 new_node->count += redirected_sum;
3364 orig_node->count -= redirected_sum;
3366 for (cs = new_node->callees; cs ; cs = cs->next_callee)
3367 if (cs->frequency)
3368 cs->count += apply_probability (cs->count,
3369 GCOV_COMPUTE_SCALE (redirected_sum,
3370 new_node_count));
3371 else
3372 cs->count = 0;
3374 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
3376 gcov_type dec = apply_probability (cs->count,
3377 GCOV_COMPUTE_SCALE (redirected_sum,
3378 orig_node_count));
3379 if (dec < cs->count)
3380 cs->count -= dec;
3381 else
3382 cs->count = 0;
3385 if (dump_file)
3386 dump_profile_updates (orig_node, new_node);
3389 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3390 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3391 redirect all edges in CALLERS to it. */
3393 static struct cgraph_node *
3394 create_specialized_node (struct cgraph_node *node,
3395 vec<tree> known_csts,
3396 vec<ipa_polymorphic_call_context> known_contexts,
3397 struct ipa_agg_replacement_value *aggvals,
3398 vec<cgraph_edge *> callers)
3400 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
3401 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
3402 struct ipa_agg_replacement_value *av;
3403 struct cgraph_node *new_node;
3404 int i, count = ipa_get_param_count (info);
3405 bitmap args_to_skip;
3407 gcc_assert (!info->ipcp_orig_node);
3409 if (node->local.can_change_signature)
3411 args_to_skip = BITMAP_GGC_ALLOC ();
3412 for (i = 0; i < count; i++)
3414 tree t = known_csts[i];
3416 if (t || !ipa_is_param_used (info, i))
3417 bitmap_set_bit (args_to_skip, i);
3420 else
3422 args_to_skip = NULL;
3423 if (dump_file && (dump_flags & TDF_DETAILS))
3424 fprintf (dump_file, " cannot change function signature\n");
3427 for (i = 0; i < count ; i++)
3429 tree t = known_csts[i];
3430 if (t)
3432 struct ipa_replace_map *replace_map;
3434 gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
3435 replace_map = get_replacement_map (info, t, i);
3436 if (replace_map)
3437 vec_safe_push (replace_trees, replace_map);
3441 new_node = node->create_virtual_clone (callers, replace_trees,
3442 args_to_skip, "constprop");
3443 ipa_set_node_agg_value_chain (new_node, aggvals);
3444 for (av = aggvals; av; av = av->next)
3445 new_node->maybe_create_reference (av->value, IPA_REF_ADDR, NULL);
3447 if (dump_file && (dump_flags & TDF_DETAILS))
3449 fprintf (dump_file, " the new node is %s/%i.\n",
3450 new_node->name (), new_node->order);
3451 if (known_contexts.exists ())
3453 for (i = 0; i < count ; i++)
3454 if (!known_contexts[i].useless_p ())
3456 fprintf (dump_file, " known ctx %i is ", i);
3457 known_contexts[i].dump (dump_file);
3460 if (aggvals)
3461 ipa_dump_agg_replacement_values (dump_file, aggvals);
3463 ipa_check_create_node_params ();
3464 update_profiling_info (node, new_node);
3465 new_info = IPA_NODE_REF (new_node);
3466 new_info->ipcp_orig_node = node;
3467 new_info->known_csts = known_csts;
3468 new_info->known_contexts = known_contexts;
3470 ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts, aggvals);
3472 callers.release ();
3473 return new_node;
3476 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3477 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3479 static void
3480 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
3481 vec<tree> known_csts,
3482 vec<cgraph_edge *> callers)
3484 struct ipa_node_params *info = IPA_NODE_REF (node);
3485 int i, count = ipa_get_param_count (info);
3487 for (i = 0; i < count ; i++)
3489 struct cgraph_edge *cs;
3490 tree newval = NULL_TREE;
3491 int j;
3492 bool first = true;
3494 if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
3495 continue;
3497 FOR_EACH_VEC_ELT (callers, j, cs)
3499 struct ipa_jump_func *jump_func;
3500 tree t;
3502 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3504 newval = NULL_TREE;
3505 break;
3507 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
3508 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
3509 if (!t
3510 || (newval
3511 && !values_equal_for_ipcp_p (t, newval))
3512 || (!first && !newval))
3514 newval = NULL_TREE;
3515 break;
3517 else
3518 newval = t;
3519 first = false;
3522 if (newval)
3524 if (dump_file && (dump_flags & TDF_DETAILS))
3526 fprintf (dump_file, " adding an extra known scalar value ");
3527 print_ipcp_constant_value (dump_file, newval);
3528 fprintf (dump_file, " for ");
3529 ipa_dump_param (dump_file, info, i);
3530 fprintf (dump_file, "\n");
3533 known_csts[i] = newval;
3538 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3539 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3540 CALLERS. */
3542 static void
3543 find_more_contexts_for_caller_subset (cgraph_node *node,
3544 vec<ipa_polymorphic_call_context>
3545 *known_contexts,
3546 vec<cgraph_edge *> callers)
3548 ipa_node_params *info = IPA_NODE_REF (node);
3549 int i, count = ipa_get_param_count (info);
3551 for (i = 0; i < count ; i++)
3553 cgraph_edge *cs;
3555 if (ipa_get_poly_ctx_lat (info, i)->bottom
3556 || (known_contexts->exists ()
3557 && !(*known_contexts)[i].useless_p ()))
3558 continue;
3560 ipa_polymorphic_call_context newval;
3561 bool first = true;
3562 int j;
3564 FOR_EACH_VEC_ELT (callers, j, cs)
3566 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
3567 return;
3568 ipa_jump_func *jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs),
3570 ipa_polymorphic_call_context ctx;
3571 ctx = ipa_context_from_jfunc (IPA_NODE_REF (cs->caller), cs, i,
3572 jfunc);
3573 if (first)
3575 newval = ctx;
3576 first = false;
3578 else
3579 newval.meet_with (ctx);
3580 if (newval.useless_p ())
3581 break;
3584 if (!newval.useless_p ())
3586 if (dump_file && (dump_flags & TDF_DETAILS))
3588 fprintf (dump_file, " adding an extra known polymorphic "
3589 "context ");
3590 print_ipcp_constant_value (dump_file, newval);
3591 fprintf (dump_file, " for ");
3592 ipa_dump_param (dump_file, info, i);
3593 fprintf (dump_file, "\n");
3596 if (!known_contexts->exists ())
3597 known_contexts->safe_grow_cleared (ipa_get_param_count (info));
3598 (*known_contexts)[i] = newval;
3604 /* Go through PLATS and create a vector of values consisting of values and
3605 offsets (minus OFFSET) of lattices that contain only a single value. */
3607 static vec<ipa_agg_jf_item>
3608 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
3610 vec<ipa_agg_jf_item> res = vNULL;
3612 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3613 return vNULL;
3615 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3616 if (aglat->is_single_const ())
3618 struct ipa_agg_jf_item ti;
3619 ti.offset = aglat->offset - offset;
3620 ti.value = aglat->values->value;
3621 res.safe_push (ti);
3623 return res;
3626 /* Intersect all values in INTER with single value lattices in PLATS (while
3627 subtracting OFFSET). */
3629 static void
3630 intersect_with_plats (struct ipcp_param_lattices *plats,
3631 vec<ipa_agg_jf_item> *inter,
3632 HOST_WIDE_INT offset)
3634 struct ipcp_agg_lattice *aglat;
3635 struct ipa_agg_jf_item *item;
3636 int k;
3638 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
3640 inter->release ();
3641 return;
3644 aglat = plats->aggs;
3645 FOR_EACH_VEC_ELT (*inter, k, item)
3647 bool found = false;
3648 if (!item->value)
3649 continue;
3650 while (aglat)
3652 if (aglat->offset - offset > item->offset)
3653 break;
3654 if (aglat->offset - offset == item->offset)
3656 gcc_checking_assert (item->value);
3657 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
3658 found = true;
3659 break;
3661 aglat = aglat->next;
3663 if (!found)
3664 item->value = NULL_TREE;
3668 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
3669 vector result while subtracting OFFSET from the individual value offsets. */
3671 static vec<ipa_agg_jf_item>
3672 agg_replacements_to_vector (struct cgraph_node *node, int index,
3673 HOST_WIDE_INT offset)
3675 struct ipa_agg_replacement_value *av;
3676 vec<ipa_agg_jf_item> res = vNULL;
3678 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
3679 if (av->index == index
3680 && (av->offset - offset) >= 0)
3682 struct ipa_agg_jf_item item;
3683 gcc_checking_assert (av->value);
3684 item.offset = av->offset - offset;
3685 item.value = av->value;
3686 res.safe_push (item);
3689 return res;
3692 /* Intersect all values in INTER with those that we have already scheduled to
3693 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
3694 (while subtracting OFFSET). */
3696 static void
3697 intersect_with_agg_replacements (struct cgraph_node *node, int index,
3698 vec<ipa_agg_jf_item> *inter,
3699 HOST_WIDE_INT offset)
3701 struct ipa_agg_replacement_value *srcvals;
3702 struct ipa_agg_jf_item *item;
3703 int i;
3705 srcvals = ipa_get_agg_replacements_for_node (node);
3706 if (!srcvals)
3708 inter->release ();
3709 return;
3712 FOR_EACH_VEC_ELT (*inter, i, item)
3714 struct ipa_agg_replacement_value *av;
3715 bool found = false;
3716 if (!item->value)
3717 continue;
3718 for (av = srcvals; av; av = av->next)
3720 gcc_checking_assert (av->value);
3721 if (av->index == index
3722 && av->offset - offset == item->offset)
3724 if (values_equal_for_ipcp_p (item->value, av->value))
3725 found = true;
3726 break;
3729 if (!found)
3730 item->value = NULL_TREE;
3734 /* Intersect values in INTER with aggregate values that come along edge CS to
3735 parameter number INDEX and return it. If INTER does not actually exist yet,
3736 copy all incoming values to it. If we determine we ended up with no values
3737 whatsoever, return a released vector. */
3739 static vec<ipa_agg_jf_item>
3740 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3741 vec<ipa_agg_jf_item> inter)
3743 struct ipa_jump_func *jfunc;
3744 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3745 if (jfunc->type == IPA_JF_PASS_THROUGH
3746 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3748 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3749 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3751 if (caller_info->ipcp_orig_node)
3753 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3754 struct ipcp_param_lattices *orig_plats;
3755 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3756 src_idx);
3757 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3759 if (!inter.exists ())
3760 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3761 else
3762 intersect_with_agg_replacements (cs->caller, src_idx,
3763 &inter, 0);
3765 else
3767 inter.release ();
3768 return vNULL;
3771 else
3773 struct ipcp_param_lattices *src_plats;
3774 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3775 if (agg_pass_through_permissible_p (src_plats, jfunc))
3777 /* Currently we do not produce clobber aggregate jump
3778 functions, adjust when we do. */
3779 gcc_checking_assert (!jfunc->agg.items);
3780 if (!inter.exists ())
3781 inter = copy_plats_to_inter (src_plats, 0);
3782 else
3783 intersect_with_plats (src_plats, &inter, 0);
3785 else
3787 inter.release ();
3788 return vNULL;
3792 else if (jfunc->type == IPA_JF_ANCESTOR
3793 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3795 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3796 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3797 struct ipcp_param_lattices *src_plats;
3798 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3800 if (caller_info->ipcp_orig_node)
3802 if (!inter.exists ())
3803 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3804 else
3805 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3806 delta);
3808 else
3810 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3811 /* Currently we do not produce clobber aggregate jump
3812 functions, adjust when we do. */
3813 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3814 if (!inter.exists ())
3815 inter = copy_plats_to_inter (src_plats, delta);
3816 else
3817 intersect_with_plats (src_plats, &inter, delta);
3820 else if (jfunc->agg.items)
3822 struct ipa_agg_jf_item *item;
3823 int k;
3825 if (!inter.exists ())
3826 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3827 inter.safe_push ((*jfunc->agg.items)[i]);
3828 else
3829 FOR_EACH_VEC_ELT (inter, k, item)
3831 int l = 0;
3832 bool found = false;;
3834 if (!item->value)
3835 continue;
3837 while ((unsigned) l < jfunc->agg.items->length ())
3839 struct ipa_agg_jf_item *ti;
3840 ti = &(*jfunc->agg.items)[l];
3841 if (ti->offset > item->offset)
3842 break;
3843 if (ti->offset == item->offset)
3845 gcc_checking_assert (ti->value);
3846 if (values_equal_for_ipcp_p (item->value,
3847 ti->value))
3848 found = true;
3849 break;
3851 l++;
3853 if (!found)
3854 item->value = NULL;
3857 else
3859 inter.release ();
3860 return vec<ipa_agg_jf_item>();
3862 return inter;
3865 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3866 from all of them. */
3868 static struct ipa_agg_replacement_value *
3869 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3870 vec<cgraph_edge *> callers)
3872 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3873 struct ipa_agg_replacement_value *res;
3874 struct ipa_agg_replacement_value **tail = &res;
3875 struct cgraph_edge *cs;
3876 int i, j, count = ipa_get_param_count (dest_info);
3878 FOR_EACH_VEC_ELT (callers, j, cs)
3880 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3881 if (c < count)
3882 count = c;
3885 for (i = 0; i < count ; i++)
3887 struct cgraph_edge *cs;
3888 vec<ipa_agg_jf_item> inter = vNULL;
3889 struct ipa_agg_jf_item *item;
3890 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3891 int j;
3893 /* Among other things, the following check should deal with all by_ref
3894 mismatches. */
3895 if (plats->aggs_bottom)
3896 continue;
3898 FOR_EACH_VEC_ELT (callers, j, cs)
3900 inter = intersect_aggregates_with_edge (cs, i, inter);
3902 if (!inter.exists ())
3903 goto next_param;
3906 FOR_EACH_VEC_ELT (inter, j, item)
3908 struct ipa_agg_replacement_value *v;
3910 if (!item->value)
3911 continue;
3913 v = ggc_alloc<ipa_agg_replacement_value> ();
3914 v->index = i;
3915 v->offset = item->offset;
3916 v->value = item->value;
3917 v->by_ref = plats->aggs_by_ref;
3918 *tail = v;
3919 tail = &v->next;
3922 next_param:
3923 if (inter.exists ())
3924 inter.release ();
3926 *tail = NULL;
3927 return res;
3930 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3932 static struct ipa_agg_replacement_value *
3933 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3935 struct ipa_agg_replacement_value *res;
3936 struct ipa_agg_replacement_value **tail = &res;
3937 struct ipa_agg_jump_function *aggjf;
3938 struct ipa_agg_jf_item *item;
3939 int i, j;
3941 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3942 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3944 struct ipa_agg_replacement_value *v;
3945 v = ggc_alloc<ipa_agg_replacement_value> ();
3946 v->index = i;
3947 v->offset = item->offset;
3948 v->value = item->value;
3949 v->by_ref = aggjf->by_ref;
3950 *tail = v;
3951 tail = &v->next;
3953 *tail = NULL;
3954 return res;
3957 /* Determine whether CS also brings all scalar values that the NODE is
3958 specialized for. */
3960 static bool
3961 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3962 struct cgraph_node *node)
3964 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3965 int count = ipa_get_param_count (dest_info);
3966 struct ipa_node_params *caller_info;
3967 struct ipa_edge_args *args;
3968 int i;
3970 caller_info = IPA_NODE_REF (cs->caller);
3971 args = IPA_EDGE_REF (cs);
3972 for (i = 0; i < count; i++)
3974 struct ipa_jump_func *jump_func;
3975 tree val, t;
3977 val = dest_info->known_csts[i];
3978 if (!val)
3979 continue;
3981 if (i >= ipa_get_cs_argument_count (args))
3982 return false;
3983 jump_func = ipa_get_ith_jump_func (args, i);
3984 t = ipa_value_from_jfunc (caller_info, jump_func);
3985 if (!t || !values_equal_for_ipcp_p (val, t))
3986 return false;
3988 return true;
3991 /* Determine whether CS also brings all aggregate values that NODE is
3992 specialized for. */
3993 static bool
3994 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3995 struct cgraph_node *node)
3997 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3998 struct ipa_node_params *orig_node_info;
3999 struct ipa_agg_replacement_value *aggval;
4000 int i, ec, count;
4002 aggval = ipa_get_agg_replacements_for_node (node);
4003 if (!aggval)
4004 return true;
4006 count = ipa_get_param_count (IPA_NODE_REF (node));
4007 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
4008 if (ec < count)
4009 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
4010 if (aggval->index >= ec)
4011 return false;
4013 orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node);
4014 if (orig_caller_info->ipcp_orig_node)
4015 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
4017 for (i = 0; i < count; i++)
4019 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
4020 struct ipcp_param_lattices *plats;
4021 bool interesting = false;
4022 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
4023 if (aggval->index == i)
4025 interesting = true;
4026 break;
4028 if (!interesting)
4029 continue;
4031 plats = ipa_get_parm_lattices (orig_node_info, aggval->index);
4032 if (plats->aggs_bottom)
4033 return false;
4035 values = intersect_aggregates_with_edge (cs, i, values);
4036 if (!values.exists ())
4037 return false;
4039 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
4040 if (aggval->index == i)
4042 struct ipa_agg_jf_item *item;
4043 int j;
4044 bool found = false;
4045 FOR_EACH_VEC_ELT (values, j, item)
4046 if (item->value
4047 && item->offset == av->offset
4048 && values_equal_for_ipcp_p (item->value, av->value))
4050 found = true;
4051 break;
4053 if (!found)
4055 values.release ();
4056 return false;
4060 return true;
4063 /* Given an original NODE and a VAL for which we have already created a
4064 specialized clone, look whether there are incoming edges that still lead
4065 into the old node but now also bring the requested value and also conform to
4066 all other criteria such that they can be redirected the special node.
4067 This function can therefore redirect the final edge in a SCC. */
4069 template <typename valtype>
4070 static void
4071 perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
4073 ipcp_value_source<valtype> *src;
4074 gcov_type redirected_sum = 0;
4076 for (src = val->sources; src; src = src->next)
4078 struct cgraph_edge *cs = src->cs;
4079 while (cs)
4081 if (cgraph_edge_brings_value_p (cs, src, node)
4082 && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
4083 && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
4085 if (dump_file)
4086 fprintf (dump_file, " - adding an extra caller %s/%i"
4087 " of %s/%i\n",
4088 xstrdup_for_dump (cs->caller->name ()),
4089 cs->caller->order,
4090 xstrdup_for_dump (val->spec_node->name ()),
4091 val->spec_node->order);
4093 cs->redirect_callee_duplicating_thunks (val->spec_node);
4094 val->spec_node->expand_all_artificial_thunks ();
4095 redirected_sum += cs->count;
4097 cs = get_next_cgraph_edge_clone (cs);
4101 if (redirected_sum)
4102 update_specialized_profile (val->spec_node, node, redirected_sum);
4105 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4107 static bool
4108 known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
4110 ipa_polymorphic_call_context *ctx;
4111 int i;
4113 FOR_EACH_VEC_ELT (known_contexts, i, ctx)
4114 if (!ctx->useless_p ())
4115 return true;
4116 return false;
4119 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4121 static vec<ipa_polymorphic_call_context>
4122 copy_useful_known_contexts (vec<ipa_polymorphic_call_context> known_contexts)
4124 if (known_contexts_useful_p (known_contexts))
4125 return known_contexts.copy ();
4126 else
4127 return vNULL;
4130 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4131 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4133 static void
4134 modify_known_vectors_with_val (vec<tree> *known_csts,
4135 vec<ipa_polymorphic_call_context> *known_contexts,
4136 ipcp_value<tree> *val,
4137 int index)
4139 *known_csts = known_csts->copy ();
4140 *known_contexts = copy_useful_known_contexts (*known_contexts);
4141 (*known_csts)[index] = val->value;
4144 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4145 copy according to VAL and INDEX. */
4147 static void
4148 modify_known_vectors_with_val (vec<tree> *known_csts,
4149 vec<ipa_polymorphic_call_context> *known_contexts,
4150 ipcp_value<ipa_polymorphic_call_context> *val,
4151 int index)
4153 *known_csts = known_csts->copy ();
4154 *known_contexts = known_contexts->copy ();
4155 (*known_contexts)[index] = val->value;
4158 /* Return true if OFFSET indicates this was not an aggregate value or there is
4159 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4160 AGGVALS list. */
4162 DEBUG_FUNCTION bool
4163 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *aggvals,
4164 int index, HOST_WIDE_INT offset, tree value)
4166 if (offset == -1)
4167 return true;
4169 while (aggvals)
4171 if (aggvals->index == index
4172 && aggvals->offset == offset
4173 && values_equal_for_ipcp_p (aggvals->value, value))
4174 return true;
4175 aggvals = aggvals->next;
4177 return false;
4180 /* Return true if offset is minus one because source of a polymorphic contect
4181 cannot be an aggregate value. */
4183 DEBUG_FUNCTION bool
4184 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *,
4185 int , HOST_WIDE_INT offset,
4186 ipa_polymorphic_call_context)
4188 return offset == -1;
4191 /* Decide wheter to create a special version of NODE for value VAL of parameter
4192 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4193 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4194 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4196 template <typename valtype>
4197 static bool
4198 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
4199 ipcp_value<valtype> *val, vec<tree> known_csts,
4200 vec<ipa_polymorphic_call_context> known_contexts)
4202 struct ipa_agg_replacement_value *aggvals;
4203 int freq_sum, caller_count;
4204 gcov_type count_sum;
4205 vec<cgraph_edge *> callers;
4207 if (val->spec_node)
4209 perhaps_add_new_callers (node, val);
4210 return false;
4212 else if (val->local_size_cost + overall_size > max_new_size)
4214 if (dump_file && (dump_flags & TDF_DETAILS))
4215 fprintf (dump_file, " Ignoring candidate value because "
4216 "max_new_size would be reached with %li.\n",
4217 val->local_size_cost + overall_size);
4218 return false;
4220 else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum,
4221 &caller_count))
4222 return false;
4224 if (dump_file && (dump_flags & TDF_DETAILS))
4226 fprintf (dump_file, " - considering value ");
4227 print_ipcp_constant_value (dump_file, val->value);
4228 fprintf (dump_file, " for ");
4229 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
4230 if (offset != -1)
4231 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
4232 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
4235 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
4236 freq_sum, count_sum,
4237 val->local_size_cost)
4238 && !good_cloning_opportunity_p (node,
4239 val->local_time_benefit
4240 + val->prop_time_benefit,
4241 freq_sum, count_sum,
4242 val->local_size_cost
4243 + val->prop_size_cost))
4244 return false;
4246 if (dump_file)
4247 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
4248 node->name (), node->order);
4250 callers = gather_edges_for_value (val, node, caller_count);
4251 if (offset == -1)
4252 modify_known_vectors_with_val (&known_csts, &known_contexts, val, index);
4253 else
4255 known_csts = known_csts.copy ();
4256 known_contexts = copy_useful_known_contexts (known_contexts);
4258 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
4259 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
4260 aggvals = find_aggregate_values_for_callers_subset (node, callers);
4261 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
4262 offset, val->value));
4263 val->spec_node = create_specialized_node (node, known_csts, known_contexts,
4264 aggvals, callers);
4265 overall_size += val->local_size_cost;
4267 /* TODO: If for some lattice there is only one other known value
4268 left, make a special node for it too. */
4270 return true;
4273 /* Decide whether and what specialized clones of NODE should be created. */
4275 static bool
4276 decide_whether_version_node (struct cgraph_node *node)
4278 struct ipa_node_params *info = IPA_NODE_REF (node);
4279 int i, count = ipa_get_param_count (info);
4280 vec<tree> known_csts;
4281 vec<ipa_polymorphic_call_context> known_contexts;
4282 vec<ipa_agg_jump_function> known_aggs = vNULL;
4283 bool ret = false;
4285 if (count == 0)
4286 return false;
4288 if (dump_file && (dump_flags & TDF_DETAILS))
4289 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
4290 node->name (), node->order);
4292 gather_context_independent_values (info, &known_csts, &known_contexts,
4293 info->do_clone_for_all_contexts ? &known_aggs
4294 : NULL, NULL);
4296 for (i = 0; i < count ;i++)
4298 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4299 ipcp_lattice<tree> *lat = &plats->itself;
4300 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
4302 if (!lat->bottom
4303 && !known_csts[i])
4305 ipcp_value<tree> *val;
4306 for (val = lat->values; val; val = val->next)
4307 ret |= decide_about_value (node, i, -1, val, known_csts,
4308 known_contexts);
4311 if (!plats->aggs_bottom)
4313 struct ipcp_agg_lattice *aglat;
4314 ipcp_value<tree> *val;
4315 for (aglat = plats->aggs; aglat; aglat = aglat->next)
4316 if (!aglat->bottom && aglat->values
4317 /* If the following is false, the one value is in
4318 known_aggs. */
4319 && (plats->aggs_contain_variable
4320 || !aglat->is_single_const ()))
4321 for (val = aglat->values; val; val = val->next)
4322 ret |= decide_about_value (node, i, aglat->offset, val,
4323 known_csts, known_contexts);
4326 if (!ctxlat->bottom
4327 && known_contexts[i].useless_p ())
4329 ipcp_value<ipa_polymorphic_call_context> *val;
4330 for (val = ctxlat->values; val; val = val->next)
4331 ret |= decide_about_value (node, i, -1, val, known_csts,
4332 known_contexts);
4335 info = IPA_NODE_REF (node);
4338 if (info->do_clone_for_all_contexts)
4340 struct cgraph_node *clone;
4341 vec<cgraph_edge *> callers;
4343 if (dump_file)
4344 fprintf (dump_file, " - Creating a specialized node of %s/%i "
4345 "for all known contexts.\n", node->name (),
4346 node->order);
4348 callers = node->collect_callers ();
4350 if (!known_contexts_useful_p (known_contexts))
4352 known_contexts.release ();
4353 known_contexts = vNULL;
4355 clone = create_specialized_node (node, known_csts, known_contexts,
4356 known_aggs_to_agg_replacement_list (known_aggs),
4357 callers);
4358 info = IPA_NODE_REF (node);
4359 info->do_clone_for_all_contexts = false;
4360 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
4361 for (i = 0; i < count ; i++)
4362 vec_free (known_aggs[i].items);
4363 known_aggs.release ();
4364 ret = true;
4366 else
4368 known_csts.release ();
4369 known_contexts.release ();
4372 return ret;
4375 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4377 static void
4378 spread_undeadness (struct cgraph_node *node)
4380 struct cgraph_edge *cs;
4382 for (cs = node->callees; cs; cs = cs->next_callee)
4383 if (ipa_edge_within_scc (cs))
4385 struct cgraph_node *callee;
4386 struct ipa_node_params *info;
4388 callee = cs->callee->function_symbol (NULL);
4389 info = IPA_NODE_REF (callee);
4391 if (info->node_dead)
4393 info->node_dead = 0;
4394 spread_undeadness (callee);
4399 /* Return true if NODE has a caller from outside of its SCC that is not
4400 dead. Worker callback for cgraph_for_node_and_aliases. */
4402 static bool
4403 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
4404 void *data ATTRIBUTE_UNUSED)
4406 struct cgraph_edge *cs;
4408 for (cs = node->callers; cs; cs = cs->next_caller)
4409 if (cs->caller->thunk.thunk_p
4410 && cs->caller->call_for_symbol_thunks_and_aliases
4411 (has_undead_caller_from_outside_scc_p, NULL, true))
4412 return true;
4413 else if (!ipa_edge_within_scc (cs)
4414 && !IPA_NODE_REF (cs->caller)->node_dead)
4415 return true;
4416 return false;
4420 /* Identify nodes within the same SCC as NODE which are no longer needed
4421 because of new clones and will be removed as unreachable. */
4423 static void
4424 identify_dead_nodes (struct cgraph_node *node)
4426 struct cgraph_node *v;
4427 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4428 if (v->will_be_removed_from_program_if_no_direct_calls_p ()
4429 && !v->call_for_symbol_thunks_and_aliases
4430 (has_undead_caller_from_outside_scc_p, NULL, true))
4431 IPA_NODE_REF (v)->node_dead = 1;
4433 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4434 if (!IPA_NODE_REF (v)->node_dead)
4435 spread_undeadness (v);
4437 if (dump_file && (dump_flags & TDF_DETAILS))
4439 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4440 if (IPA_NODE_REF (v)->node_dead)
4441 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
4442 v->name (), v->order);
4446 /* The decision stage. Iterate over the topological order of call graph nodes
4447 TOPO and make specialized clones if deemed beneficial. */
4449 static void
4450 ipcp_decision_stage (struct ipa_topo_info *topo)
4452 int i;
4454 if (dump_file)
4455 fprintf (dump_file, "\nIPA decision stage:\n\n");
4457 for (i = topo->nnodes - 1; i >= 0; i--)
4459 struct cgraph_node *node = topo->order[i];
4460 bool change = false, iterate = true;
4462 while (iterate)
4464 struct cgraph_node *v;
4465 iterate = false;
4466 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
4467 if (v->has_gimple_body_p ()
4468 && ipcp_versionable_function_p (v))
4469 iterate |= decide_whether_version_node (v);
4471 change |= iterate;
4473 if (change)
4474 identify_dead_nodes (node);
4478 /* Look up all alignment information that we have discovered and copy it over
4479 to the transformation summary. */
4481 static void
4482 ipcp_store_alignment_results (void)
4484 cgraph_node *node;
4486 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4488 ipa_node_params *info = IPA_NODE_REF (node);
4489 bool dumped_sth = false;
4490 bool found_useful_result = false;
4492 if (!opt_for_fn (node->decl, flag_ipa_cp_alignment))
4494 if (dump_file)
4495 fprintf (dump_file, "Not considering %s for alignment discovery "
4496 "and propagate; -fipa-cp-alignment: disabled.\n",
4497 node->name ());
4498 continue;
4501 if (info->ipcp_orig_node)
4502 info = IPA_NODE_REF (info->ipcp_orig_node);
4504 unsigned count = ipa_get_param_count (info);
4505 for (unsigned i = 0; i < count ; i++)
4507 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4508 if (!plats->alignment.bottom_p ()
4509 && !plats->alignment.top_p ())
4511 gcc_checking_assert (plats->alignment.align > 0);
4512 found_useful_result = true;
4513 break;
4516 if (!found_useful_result)
4517 continue;
4519 ipcp_grow_transformations_if_necessary ();
4520 ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node);
4521 vec_safe_reserve_exact (ts->alignments, count);
4523 for (unsigned i = 0; i < count ; i++)
4525 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
4526 ipa_alignment al;
4528 if (!plats->alignment.bottom_p ()
4529 && !plats->alignment.top_p ())
4531 al.known = true;
4532 al.align = plats->alignment.align;
4533 al.misalign = plats->alignment.misalign;
4535 else
4536 al.known = false;
4538 ts->alignments->quick_push (al);
4539 if (!dump_file || !al.known)
4540 continue;
4541 if (!dumped_sth)
4543 fprintf (dump_file, "Propagated alignment info for function %s/%i:\n",
4544 node->name (), node->order);
4545 dumped_sth = true;
4547 fprintf (dump_file, " param %i: align: %u, misalign: %u\n",
4548 i, al.align, al.misalign);
4553 /* The IPCP driver. */
4555 static unsigned int
4556 ipcp_driver (void)
4558 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
4559 struct cgraph_edge_hook_list *edge_removal_hook_holder;
4560 struct ipa_topo_info topo;
4562 ipa_check_create_node_params ();
4563 ipa_check_create_edge_args ();
4564 grow_edge_clone_vectors ();
4565 edge_duplication_hook_holder =
4566 symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
4567 edge_removal_hook_holder =
4568 symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
4570 if (dump_file)
4572 fprintf (dump_file, "\nIPA structures before propagation:\n");
4573 if (dump_flags & TDF_DETAILS)
4574 ipa_print_all_params (dump_file);
4575 ipa_print_all_jump_functions (dump_file);
4578 /* Topological sort. */
4579 build_toporder_info (&topo);
4580 /* Do the interprocedural propagation. */
4581 ipcp_propagate_stage (&topo);
4582 /* Decide what constant propagation and cloning should be performed. */
4583 ipcp_decision_stage (&topo);
4584 /* Store results of alignment propagation. */
4585 ipcp_store_alignment_results ();
4587 /* Free all IPCP structures. */
4588 free_toporder_info (&topo);
4589 next_edge_clone.release ();
4590 prev_edge_clone.release ();
4591 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4592 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4593 ipa_free_all_structures_after_ipa_cp ();
4594 if (dump_file)
4595 fprintf (dump_file, "\nIPA constant propagation end\n");
4596 return 0;
4599 /* Initialization and computation of IPCP data structures. This is the initial
4600 intraprocedural analysis of functions, which gathers information to be
4601 propagated later on. */
4603 static void
4604 ipcp_generate_summary (void)
4606 struct cgraph_node *node;
4608 if (dump_file)
4609 fprintf (dump_file, "\nIPA constant propagation start:\n");
4610 ipa_register_cgraph_hooks ();
4612 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
4613 ipa_analyze_node (node);
4616 /* Write ipcp summary for nodes in SET. */
4618 static void
4619 ipcp_write_summary (void)
4621 ipa_prop_write_jump_functions ();
4624 /* Read ipcp summary. */
4626 static void
4627 ipcp_read_summary (void)
4629 ipa_prop_read_jump_functions ();
4632 namespace {
4634 const pass_data pass_data_ipa_cp =
4636 IPA_PASS, /* type */
4637 "cp", /* name */
4638 OPTGROUP_NONE, /* optinfo_flags */
4639 TV_IPA_CONSTANT_PROP, /* tv_id */
4640 0, /* properties_required */
4641 0, /* properties_provided */
4642 0, /* properties_destroyed */
4643 0, /* todo_flags_start */
4644 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
4647 class pass_ipa_cp : public ipa_opt_pass_d
4649 public:
4650 pass_ipa_cp (gcc::context *ctxt)
4651 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
4652 ipcp_generate_summary, /* generate_summary */
4653 ipcp_write_summary, /* write_summary */
4654 ipcp_read_summary, /* read_summary */
4655 ipcp_write_transformation_summaries, /*
4656 write_optimization_summary */
4657 ipcp_read_transformation_summaries, /*
4658 read_optimization_summary */
4659 NULL, /* stmt_fixup */
4660 0, /* function_transform_todo_flags_start */
4661 ipcp_transform_function, /* function_transform */
4662 NULL) /* variable_transform */
4665 /* opt_pass methods: */
4666 virtual bool gate (function *)
4668 /* FIXME: We should remove the optimize check after we ensure we never run
4669 IPA passes when not optimizing. */
4670 return (flag_ipa_cp && optimize) || in_lto_p;
4673 virtual unsigned int execute (function *) { return ipcp_driver (); }
4675 }; // class pass_ipa_cp
4677 } // anon namespace
4679 ipa_opt_pass_d *
4680 make_pass_ipa_cp (gcc::context *ctxt)
4682 return new pass_ipa_cp (ctxt);
4685 /* Reset all state within ipa-cp.c so that we can rerun the compiler
4686 within the same process. For use by toplev::finalize. */
4688 void
4689 ipa_cp_c_finalize (void)
4691 max_count = 0;
4692 overall_size = 0;
4693 max_new_size = 0;