[ARM] Fix CLZ_DEFINED_VALUE_AT_ZERO for vector modes
[official-gcc.git] / gcc / ipa-cp.c
blob3331dca7f8b0d77e9600519c5ea024221e5b70e2
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2014 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 "tree.h"
107 #include "gimple-fold.h"
108 #include "gimple-expr.h"
109 #include "target.h"
110 #include "predict.h"
111 #include "basic-block.h"
112 #include "vec.h"
113 #include "hash-map.h"
114 #include "is-a.h"
115 #include "plugin-api.h"
116 #include "hashtab.h"
117 #include "hash-set.h"
118 #include "machmode.h"
119 #include "tm.h"
120 #include "hard-reg-set.h"
121 #include "input.h"
122 #include "function.h"
123 #include "ipa-ref.h"
124 #include "cgraph.h"
125 #include "alloc-pool.h"
126 #include "ipa-prop.h"
127 #include "bitmap.h"
128 #include "tree-pass.h"
129 #include "flags.h"
130 #include "diagnostic.h"
131 #include "tree-pretty-print.h"
132 #include "tree-inline.h"
133 #include "params.h"
134 #include "ipa-inline.h"
135 #include "ipa-utils.h"
137 struct ipcp_value;
139 /* Describes a particular source for an IPA-CP value. */
141 struct ipcp_value_source
143 /* Aggregate offset of the source, negative if the source is scalar value of
144 the argument itself. */
145 HOST_WIDE_INT offset;
146 /* The incoming edge that brought the value. */
147 struct cgraph_edge *cs;
148 /* If the jump function that resulted into his value was a pass-through or an
149 ancestor, this is the ipcp_value of the caller from which the described
150 value has been derived. Otherwise it is NULL. */
151 struct ipcp_value *val;
152 /* Next pointer in a linked list of sources of a value. */
153 struct ipcp_value_source *next;
154 /* If the jump function that resulted into his value was a pass-through or an
155 ancestor, this is the index of the parameter of the caller the jump
156 function references. */
157 int index;
160 /* Describes one particular value stored in struct ipcp_lattice. */
162 struct ipcp_value
164 /* The actual value for the given parameter. This is either an IPA invariant
165 or a TREE_BINFO describing a type that can be used for
166 devirtualization. */
167 tree value;
168 /* The list of sources from which this value originates. */
169 struct ipcp_value_source *sources;
170 /* Next pointers in a linked list of all values in a lattice. */
171 struct ipcp_value *next;
172 /* Next pointers in a linked list of values in a strongly connected component
173 of values. */
174 struct ipcp_value *scc_next;
175 /* Next pointers in a linked list of SCCs of values sorted topologically
176 according their sources. */
177 struct ipcp_value *topo_next;
178 /* A specialized node created for this value, NULL if none has been (so far)
179 created. */
180 struct cgraph_node *spec_node;
181 /* Depth first search number and low link for topological sorting of
182 values. */
183 int dfs, low_link;
184 /* Time benefit and size cost that specializing the function for this value
185 would bring about in this function alone. */
186 int local_time_benefit, local_size_cost;
187 /* Time benefit and size cost that specializing the function for this value
188 can bring about in it's callees (transitively). */
189 int prop_time_benefit, prop_size_cost;
190 /* True if this valye is currently on the topo-sort stack. */
191 bool on_stack;
194 /* Lattice describing potential values of a formal parameter of a function, or
195 a part of an aggreagate. TOP is represented by a lattice with zero values
196 and with contains_variable and bottom flags cleared. BOTTOM is represented
197 by a lattice with the bottom flag set. In that case, values and
198 contains_variable flag should be disregarded. */
200 struct ipcp_lattice
202 /* The list of known values and types in this lattice. Note that values are
203 not deallocated if a lattice is set to bottom because there may be value
204 sources referencing them. */
205 struct ipcp_value *values;
206 /* Number of known values and types in this lattice. */
207 int values_count;
208 /* The lattice contains a variable component (in addition to values). */
209 bool contains_variable;
210 /* The value of the lattice is bottom (i.e. variable and unusable for any
211 propagation). */
212 bool bottom;
215 /* Lattice with an offset to describe a part of an aggregate. */
217 struct ipcp_agg_lattice : public ipcp_lattice
219 /* Offset that is being described by this lattice. */
220 HOST_WIDE_INT offset;
221 /* Size so that we don't have to re-compute it every time we traverse the
222 list. Must correspond to TYPE_SIZE of all lat values. */
223 HOST_WIDE_INT size;
224 /* Next element of the linked list. */
225 struct ipcp_agg_lattice *next;
228 /* Structure containing lattices for a parameter itself and for pieces of
229 aggregates that are passed in the parameter or by a reference in a parameter
230 plus some other useful flags. */
232 struct ipcp_param_lattices
234 /* Lattice describing the value of the parameter itself. */
235 struct ipcp_lattice itself;
236 /* Lattices describing aggregate parts. */
237 struct ipcp_agg_lattice *aggs;
238 /* Number of aggregate lattices */
239 int aggs_count;
240 /* True if aggregate data were passed by reference (as opposed to by
241 value). */
242 bool aggs_by_ref;
243 /* All aggregate lattices contain a variable component (in addition to
244 values). */
245 bool aggs_contain_variable;
246 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
247 for any propagation). */
248 bool aggs_bottom;
250 /* There is a virtual call based on this parameter. */
251 bool virt_call;
254 /* Allocation pools for values and their sources in ipa-cp. */
256 alloc_pool ipcp_values_pool;
257 alloc_pool ipcp_sources_pool;
258 alloc_pool ipcp_agg_lattice_pool;
260 /* Maximal count found in program. */
262 static gcov_type max_count;
264 /* Original overall size of the program. */
266 static long overall_size, max_new_size;
268 /* Head of the linked list of topologically sorted values. */
270 static struct ipcp_value *values_topo;
272 /* Return the param lattices structure corresponding to the Ith formal
273 parameter of the function described by INFO. */
274 static inline struct ipcp_param_lattices *
275 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
277 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
278 gcc_checking_assert (!info->ipcp_orig_node);
279 gcc_checking_assert (info->lattices);
280 return &(info->lattices[i]);
283 /* Return the lattice corresponding to the scalar value of the Ith formal
284 parameter of the function described by INFO. */
285 static inline struct ipcp_lattice *
286 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
288 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
289 return &plats->itself;
292 /* Return whether LAT is a lattice with a single constant and without an
293 undefined value. */
295 static inline bool
296 ipa_lat_is_single_const (struct ipcp_lattice *lat)
298 if (lat->bottom
299 || lat->contains_variable
300 || lat->values_count != 1)
301 return false;
302 else
303 return true;
306 /* Print V which is extracted from a value in a lattice to F. */
308 static void
309 print_ipcp_constant_value (FILE * f, tree v)
311 if (TREE_CODE (v) == TREE_BINFO)
313 fprintf (f, "BINFO ");
314 print_generic_expr (f, BINFO_TYPE (v), 0);
316 else if (TREE_CODE (v) == ADDR_EXPR
317 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
319 fprintf (f, "& ");
320 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
322 else
323 print_generic_expr (f, v, 0);
326 /* Print a lattice LAT to F. */
328 static void
329 print_lattice (FILE * f, struct ipcp_lattice *lat,
330 bool dump_sources, bool dump_benefits)
332 struct ipcp_value *val;
333 bool prev = false;
335 if (lat->bottom)
337 fprintf (f, "BOTTOM\n");
338 return;
341 if (!lat->values_count && !lat->contains_variable)
343 fprintf (f, "TOP\n");
344 return;
347 if (lat->contains_variable)
349 fprintf (f, "VARIABLE");
350 prev = true;
351 if (dump_benefits)
352 fprintf (f, "\n");
355 for (val = lat->values; val; val = val->next)
357 if (dump_benefits && prev)
358 fprintf (f, " ");
359 else if (!dump_benefits && prev)
360 fprintf (f, ", ");
361 else
362 prev = true;
364 print_ipcp_constant_value (f, val->value);
366 if (dump_sources)
368 struct ipcp_value_source *s;
370 fprintf (f, " [from:");
371 for (s = val->sources; s; s = s->next)
372 fprintf (f, " %i(%i)", s->cs->caller->order,
373 s->cs->frequency);
374 fprintf (f, "]");
377 if (dump_benefits)
378 fprintf (f, " [loc_time: %i, loc_size: %i, "
379 "prop_time: %i, prop_size: %i]\n",
380 val->local_time_benefit, val->local_size_cost,
381 val->prop_time_benefit, val->prop_size_cost);
383 if (!dump_benefits)
384 fprintf (f, "\n");
387 /* Print all ipcp_lattices of all functions to F. */
389 static void
390 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
392 struct cgraph_node *node;
393 int i, count;
395 fprintf (f, "\nLattices:\n");
396 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
398 struct ipa_node_params *info;
400 info = IPA_NODE_REF (node);
401 fprintf (f, " Node: %s/%i:\n", node->name (),
402 node->order);
403 count = ipa_get_param_count (info);
404 for (i = 0; i < count; i++)
406 struct ipcp_agg_lattice *aglat;
407 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
408 fprintf (f, " param [%d]: ", i);
409 print_lattice (f, &plats->itself, dump_sources, dump_benefits);
411 if (plats->virt_call)
412 fprintf (f, " virt_call flag set\n");
414 if (plats->aggs_bottom)
416 fprintf (f, " AGGS BOTTOM\n");
417 continue;
419 if (plats->aggs_contain_variable)
420 fprintf (f, " AGGS VARIABLE\n");
421 for (aglat = plats->aggs; aglat; aglat = aglat->next)
423 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
424 plats->aggs_by_ref ? "ref " : "", aglat->offset);
425 print_lattice (f, aglat, dump_sources, dump_benefits);
431 /* Determine whether it is at all technically possible to create clones of NODE
432 and store this information in the ipa_node_params structure associated
433 with NODE. */
435 static void
436 determine_versionability (struct cgraph_node *node)
438 const char *reason = NULL;
440 /* There are a number of generic reasons functions cannot be versioned. We
441 also cannot remove parameters if there are type attributes such as fnspec
442 present. */
443 if (node->alias || node->thunk.thunk_p)
444 reason = "alias or thunk";
445 else if (!node->local.versionable)
446 reason = "not a tree_versionable_function";
447 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
448 reason = "insufficient body availability";
449 else if (!opt_for_fn (node->decl, optimize)
450 || !opt_for_fn (node->decl, flag_ipa_cp))
451 reason = "non-optimized function";
452 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
454 /* Ideally we should clone the SIMD clones themselves and create
455 vector copies of them, so IPA-cp and SIMD clones can happily
456 coexist, but that may not be worth the effort. */
457 reason = "function has SIMD clones";
459 /* Don't clone decls local to a comdat group; it breaks and for C++
460 decloned constructors, inlining is always better anyway. */
461 else if (node->comdat_local_p ())
462 reason = "comdat-local function";
464 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
465 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
466 node->name (), node->order, reason);
468 node->local.versionable = (reason == NULL);
471 /* Return true if it is at all technically possible to create clones of a
472 NODE. */
474 static bool
475 ipcp_versionable_function_p (struct cgraph_node *node)
477 return node->local.versionable;
480 /* Structure holding accumulated information about callers of a node. */
482 struct caller_statistics
484 gcov_type count_sum;
485 int n_calls, n_hot_calls, freq_sum;
488 /* Initialize fields of STAT to zeroes. */
490 static inline void
491 init_caller_stats (struct caller_statistics *stats)
493 stats->count_sum = 0;
494 stats->n_calls = 0;
495 stats->n_hot_calls = 0;
496 stats->freq_sum = 0;
499 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
500 non-thunk incoming edges to NODE. */
502 static bool
503 gather_caller_stats (struct cgraph_node *node, void *data)
505 struct caller_statistics *stats = (struct caller_statistics *) data;
506 struct cgraph_edge *cs;
508 for (cs = node->callers; cs; cs = cs->next_caller)
509 if (cs->caller->thunk.thunk_p)
510 cs->caller->call_for_symbol_thunks_and_aliases (gather_caller_stats,
511 stats, false);
512 else
514 stats->count_sum += cs->count;
515 stats->freq_sum += cs->frequency;
516 stats->n_calls++;
517 if (cs->maybe_hot_p ())
518 stats->n_hot_calls ++;
520 return false;
524 /* Return true if this NODE is viable candidate for cloning. */
526 static bool
527 ipcp_cloning_candidate_p (struct cgraph_node *node)
529 struct caller_statistics stats;
531 gcc_checking_assert (node->has_gimple_body_p ());
533 if (!flag_ipa_cp_clone)
535 if (dump_file)
536 fprintf (dump_file, "Not considering %s for cloning; "
537 "-fipa-cp-clone disabled.\n",
538 node->name ());
539 return false;
542 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
544 if (dump_file)
545 fprintf (dump_file, "Not considering %s for cloning; "
546 "optimizing it for size.\n",
547 node->name ());
548 return false;
551 init_caller_stats (&stats);
552 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
554 if (inline_summary (node)->self_size < stats.n_calls)
556 if (dump_file)
557 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
558 node->name ());
559 return true;
562 /* When profile is available and function is hot, propagate into it even if
563 calls seems cold; constant propagation can improve function's speed
564 significantly. */
565 if (max_count)
567 if (stats.count_sum > node->count * 90 / 100)
569 if (dump_file)
570 fprintf (dump_file, "Considering %s for cloning; "
571 "usually called directly.\n",
572 node->name ());
573 return true;
576 if (!stats.n_hot_calls)
578 if (dump_file)
579 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
580 node->name ());
581 return false;
583 if (dump_file)
584 fprintf (dump_file, "Considering %s for cloning.\n",
585 node->name ());
586 return true;
589 /* Arrays representing a topological ordering of call graph nodes and a stack
590 of noes used during constant propagation. */
592 struct ipa_topo_info
594 struct cgraph_node **order;
595 struct cgraph_node **stack;
596 int nnodes, stack_top;
599 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
601 static void
602 build_toporder_info (struct ipa_topo_info *topo)
604 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
605 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
607 topo->stack_top = 0;
608 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
611 /* Free information about strongly connected components and the arrays in
612 TOPO. */
614 static void
615 free_toporder_info (struct ipa_topo_info *topo)
617 ipa_free_postorder_info ();
618 free (topo->order);
619 free (topo->stack);
622 /* Add NODE to the stack in TOPO, unless it is already there. */
624 static inline void
625 push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node)
627 struct ipa_node_params *info = IPA_NODE_REF (node);
628 if (info->node_enqueued)
629 return;
630 info->node_enqueued = 1;
631 topo->stack[topo->stack_top++] = node;
634 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
635 is empty. */
637 static struct cgraph_node *
638 pop_node_from_stack (struct ipa_topo_info *topo)
640 if (topo->stack_top)
642 struct cgraph_node *node;
643 topo->stack_top--;
644 node = topo->stack[topo->stack_top];
645 IPA_NODE_REF (node)->node_enqueued = 0;
646 return node;
648 else
649 return NULL;
652 /* Set lattice LAT to bottom and return true if it previously was not set as
653 such. */
655 static inline bool
656 set_lattice_to_bottom (struct ipcp_lattice *lat)
658 bool ret = !lat->bottom;
659 lat->bottom = true;
660 return ret;
663 /* Mark lattice as containing an unknown value and return true if it previously
664 was not marked as such. */
666 static inline bool
667 set_lattice_contains_variable (struct ipcp_lattice *lat)
669 bool ret = !lat->contains_variable;
670 lat->contains_variable = true;
671 return ret;
674 /* Set all aggegate lattices in PLATS to bottom and return true if they were
675 not previously set as such. */
677 static inline bool
678 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
680 bool ret = !plats->aggs_bottom;
681 plats->aggs_bottom = true;
682 return ret;
685 /* Mark all aggegate lattices in PLATS as containing an unknown value and
686 return true if they were not previously marked as such. */
688 static inline bool
689 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
691 bool ret = !plats->aggs_contain_variable;
692 plats->aggs_contain_variable = true;
693 return ret;
696 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
697 return true is any of them has not been marked as such so far. */
699 static inline bool
700 set_all_contains_variable (struct ipcp_param_lattices *plats)
702 bool ret = !plats->itself.contains_variable || !plats->aggs_contain_variable;
703 plats->itself.contains_variable = true;
704 plats->aggs_contain_variable = true;
705 return ret;
708 /* Initialize ipcp_lattices. */
710 static void
711 initialize_node_lattices (struct cgraph_node *node)
713 struct ipa_node_params *info = IPA_NODE_REF (node);
714 struct cgraph_edge *ie;
715 bool disable = false, variable = false;
716 int i;
718 gcc_checking_assert (node->has_gimple_body_p ());
719 if (!node->local.local)
721 /* When cloning is allowed, we can assume that externally visible
722 functions are not called. We will compensate this by cloning
723 later. */
724 if (ipcp_versionable_function_p (node)
725 && ipcp_cloning_candidate_p (node))
726 variable = true;
727 else
728 disable = true;
731 if (disable || variable)
733 for (i = 0; i < ipa_get_param_count (info) ; i++)
735 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
736 if (disable)
738 set_lattice_to_bottom (&plats->itself);
739 set_agg_lats_to_bottom (plats);
741 else
742 set_all_contains_variable (plats);
744 if (dump_file && (dump_flags & TDF_DETAILS)
745 && !node->alias && !node->thunk.thunk_p)
746 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
747 node->name (), node->order,
748 disable ? "BOTTOM" : "VARIABLE");
751 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
752 if (ie->indirect_info->polymorphic
753 && ie->indirect_info->param_index >= 0)
755 gcc_checking_assert (ie->indirect_info->param_index >= 0);
756 ipa_get_parm_lattices (info,
757 ie->indirect_info->param_index)->virt_call = 1;
761 /* Return the result of a (possibly arithmetic) pass through jump function
762 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
763 determined or be considered an interprocedural invariant. */
765 static tree
766 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
768 tree restype, res;
770 if (TREE_CODE (input) == TREE_BINFO)
772 if (ipa_get_jf_pass_through_type_preserved (jfunc))
774 gcc_checking_assert (ipa_get_jf_pass_through_operation (jfunc)
775 == NOP_EXPR);
776 return input;
778 return NULL_TREE;
781 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
782 return input;
784 gcc_checking_assert (is_gimple_ip_invariant (input));
785 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
786 == tcc_comparison)
787 restype = boolean_type_node;
788 else
789 restype = TREE_TYPE (input);
790 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
791 input, ipa_get_jf_pass_through_operand (jfunc));
793 if (res && !is_gimple_ip_invariant (res))
794 return NULL_TREE;
796 return res;
799 /* Return the result of an ancestor jump function JFUNC on the constant value
800 INPUT. Return NULL_TREE if that cannot be determined. */
802 static tree
803 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
805 if (TREE_CODE (input) == TREE_BINFO)
807 if (!ipa_get_jf_ancestor_type_preserved (jfunc))
808 return NULL;
809 /* FIXME: At LTO we can't propagate to non-polymorphic type, because
810 we have no ODR equivalency on those. This should be fixed by
811 propagating on types rather than binfos that would make type
812 matching here unnecesary. */
813 if (in_lto_p
814 && (TREE_CODE (ipa_get_jf_ancestor_type (jfunc)) != RECORD_TYPE
815 || !TYPE_BINFO (ipa_get_jf_ancestor_type (jfunc))
816 || !BINFO_VTABLE (TYPE_BINFO (ipa_get_jf_ancestor_type (jfunc)))))
818 if (!ipa_get_jf_ancestor_offset (jfunc))
819 return input;
820 return NULL;
822 return get_binfo_at_offset (input,
823 ipa_get_jf_ancestor_offset (jfunc),
824 ipa_get_jf_ancestor_type (jfunc));
826 else if (TREE_CODE (input) == ADDR_EXPR)
828 tree t = TREE_OPERAND (input, 0);
829 t = build_ref_for_offset (EXPR_LOCATION (t), t,
830 ipa_get_jf_ancestor_offset (jfunc),
831 ipa_get_jf_ancestor_type (jfunc)
832 ? ipa_get_jf_ancestor_type (jfunc)
833 : ptr_type_node, NULL, false);
834 return build_fold_addr_expr (t);
836 else
837 return NULL_TREE;
840 /* Determine whether JFUNC evaluates to a known value (that is either a
841 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
842 describes the caller node so that pass-through jump functions can be
843 evaluated. */
845 tree
846 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
848 if (jfunc->type == IPA_JF_CONST)
849 return ipa_get_jf_constant (jfunc);
850 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
851 return ipa_binfo_from_known_type_jfunc (jfunc);
852 else if (jfunc->type == IPA_JF_PASS_THROUGH
853 || jfunc->type == IPA_JF_ANCESTOR)
855 tree input;
856 int idx;
858 if (jfunc->type == IPA_JF_PASS_THROUGH)
859 idx = ipa_get_jf_pass_through_formal_id (jfunc);
860 else
861 idx = ipa_get_jf_ancestor_formal_id (jfunc);
863 if (info->ipcp_orig_node)
864 input = info->known_vals[idx];
865 else
867 struct ipcp_lattice *lat;
869 if (!info->lattices)
871 gcc_checking_assert (!flag_ipa_cp);
872 return NULL_TREE;
874 lat = ipa_get_scalar_lat (info, idx);
875 if (!ipa_lat_is_single_const (lat))
876 return NULL_TREE;
877 input = lat->values->value;
880 if (!input)
881 return NULL_TREE;
883 if (jfunc->type == IPA_JF_PASS_THROUGH)
884 return ipa_get_jf_pass_through_result (jfunc, input);
885 else
886 return ipa_get_jf_ancestor_result (jfunc, input);
888 else
889 return NULL_TREE;
893 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
894 bottom, not containing a variable component and without any known value at
895 the same time. */
897 DEBUG_FUNCTION void
898 ipcp_verify_propagated_values (void)
900 struct cgraph_node *node;
902 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
904 struct ipa_node_params *info = IPA_NODE_REF (node);
905 int i, count = ipa_get_param_count (info);
907 for (i = 0; i < count; i++)
909 struct ipcp_lattice *lat = ipa_get_scalar_lat (info, i);
911 if (!lat->bottom
912 && !lat->contains_variable
913 && lat->values_count == 0)
915 if (dump_file)
917 symtab_node::dump_table (dump_file);
918 fprintf (dump_file, "\nIPA lattices after constant "
919 "propagation, before gcc_unreachable:\n");
920 print_all_lattices (dump_file, true, false);
923 gcc_unreachable ();
929 /* Return true iff X and Y should be considered equal values by IPA-CP. */
931 static bool
932 values_equal_for_ipcp_p (tree x, tree y)
934 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
936 if (x == y)
937 return true;
939 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
940 return false;
942 if (TREE_CODE (x) == ADDR_EXPR
943 && TREE_CODE (y) == ADDR_EXPR
944 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
945 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
946 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
947 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
948 else
949 return operand_equal_p (x, y, 0);
952 /* Add a new value source to VAL, marking that a value comes from edge CS and
953 (if the underlying jump function is a pass-through or an ancestor one) from
954 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. OFFSET
955 is negative if the source was the scalar value of the parameter itself or
956 the offset within an aggregate. */
958 static void
959 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
960 struct ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset)
962 struct ipcp_value_source *src;
964 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
965 src->offset = offset;
966 src->cs = cs;
967 src->val = src_val;
968 src->index = src_idx;
970 src->next = val->sources;
971 val->sources = src;
974 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
975 it. CS, SRC_VAL SRC_INDEX and OFFSET are meant for add_value_source and
976 have the same meaning. */
978 static bool
979 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
980 struct cgraph_edge *cs, struct ipcp_value *src_val,
981 int src_idx, HOST_WIDE_INT offset)
983 struct ipcp_value *val;
985 if (lat->bottom)
986 return false;
988 for (val = lat->values; val; val = val->next)
989 if (values_equal_for_ipcp_p (val->value, newval))
991 if (ipa_edge_within_scc (cs))
993 struct ipcp_value_source *s;
994 for (s = val->sources; s ; s = s->next)
995 if (s->cs == cs)
996 break;
997 if (s)
998 return false;
1001 add_value_source (val, cs, src_val, src_idx, offset);
1002 return false;
1005 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
1007 /* We can only free sources, not the values themselves, because sources
1008 of other values in this this SCC might point to them. */
1009 for (val = lat->values; val; val = val->next)
1011 while (val->sources)
1013 struct ipcp_value_source *src = val->sources;
1014 val->sources = src->next;
1015 pool_free (ipcp_sources_pool, src);
1019 lat->values = NULL;
1020 return set_lattice_to_bottom (lat);
1023 lat->values_count++;
1024 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
1025 memset (val, 0, sizeof (*val));
1027 add_value_source (val, cs, src_val, src_idx, offset);
1028 val->value = newval;
1029 val->next = lat->values;
1030 lat->values = val;
1031 return true;
1034 /* Like above but passes a special value of offset to distinguish that the
1035 origin is the scalar value of the parameter rather than a part of an
1036 aggregate. */
1038 static inline bool
1039 add_scalar_value_to_lattice (struct ipcp_lattice *lat, tree newval,
1040 struct cgraph_edge *cs,
1041 struct ipcp_value *src_val, int src_idx)
1043 return add_value_to_lattice (lat, newval, cs, src_val, src_idx, -1);
1046 /* Propagate values through a pass-through jump function JFUNC associated with
1047 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1048 is the index of the source parameter. */
1050 static bool
1051 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
1052 struct ipa_jump_func *jfunc,
1053 struct ipcp_lattice *src_lat,
1054 struct ipcp_lattice *dest_lat,
1055 int src_idx)
1057 struct ipcp_value *src_val;
1058 bool ret = false;
1060 /* Do not create new values when propagating within an SCC because if there
1061 are arithmetic functions with circular dependencies, there is infinite
1062 number of them and we would just make lattices bottom. */
1063 if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1064 && ipa_edge_within_scc (cs))
1065 ret = set_lattice_contains_variable (dest_lat);
1066 else
1067 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1069 tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value);
1071 if (cstval)
1072 ret |= add_scalar_value_to_lattice (dest_lat, cstval, cs, src_val,
1073 src_idx);
1074 else
1075 ret |= set_lattice_contains_variable (dest_lat);
1078 return ret;
1081 /* Propagate values through an ancestor jump function JFUNC associated with
1082 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1083 is the index of the source parameter. */
1085 static bool
1086 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1087 struct ipa_jump_func *jfunc,
1088 struct ipcp_lattice *src_lat,
1089 struct ipcp_lattice *dest_lat,
1090 int src_idx)
1092 struct ipcp_value *src_val;
1093 bool ret = false;
1095 if (ipa_edge_within_scc (cs))
1096 return set_lattice_contains_variable (dest_lat);
1098 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1100 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1102 if (t)
1103 ret |= add_scalar_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
1104 else
1105 ret |= set_lattice_contains_variable (dest_lat);
1108 return ret;
1111 /* Propagate scalar values across jump function JFUNC that is associated with
1112 edge CS and put the values into DEST_LAT. */
1114 static bool
1115 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1116 struct ipa_jump_func *jfunc,
1117 struct ipcp_lattice *dest_lat)
1119 if (dest_lat->bottom)
1120 return false;
1122 if (jfunc->type == IPA_JF_CONST
1123 || jfunc->type == IPA_JF_KNOWN_TYPE)
1125 tree val;
1127 if (jfunc->type == IPA_JF_KNOWN_TYPE)
1129 val = ipa_binfo_from_known_type_jfunc (jfunc);
1130 if (!val)
1131 return set_lattice_contains_variable (dest_lat);
1133 else
1134 val = ipa_get_jf_constant (jfunc);
1135 return add_scalar_value_to_lattice (dest_lat, val, cs, NULL, 0);
1137 else if (jfunc->type == IPA_JF_PASS_THROUGH
1138 || jfunc->type == IPA_JF_ANCESTOR)
1140 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1141 struct ipcp_lattice *src_lat;
1142 int src_idx;
1143 bool ret;
1145 if (jfunc->type == IPA_JF_PASS_THROUGH)
1146 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1147 else
1148 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1150 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1151 if (src_lat->bottom)
1152 return set_lattice_contains_variable (dest_lat);
1154 /* If we would need to clone the caller and cannot, do not propagate. */
1155 if (!ipcp_versionable_function_p (cs->caller)
1156 && (src_lat->contains_variable
1157 || (src_lat->values_count > 1)))
1158 return set_lattice_contains_variable (dest_lat);
1160 if (jfunc->type == IPA_JF_PASS_THROUGH)
1161 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1162 dest_lat, src_idx);
1163 else
1164 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1165 src_idx);
1167 if (src_lat->contains_variable)
1168 ret |= set_lattice_contains_variable (dest_lat);
1170 return ret;
1173 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1174 use it for indirect inlining), we should propagate them too. */
1175 return set_lattice_contains_variable (dest_lat);
1178 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1179 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1180 other cases, return false). If there are no aggregate items, set
1181 aggs_by_ref to NEW_AGGS_BY_REF. */
1183 static bool
1184 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1185 bool new_aggs_by_ref)
1187 if (dest_plats->aggs)
1189 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1191 set_agg_lats_to_bottom (dest_plats);
1192 return true;
1195 else
1196 dest_plats->aggs_by_ref = new_aggs_by_ref;
1197 return false;
1200 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1201 already existing lattice for the given OFFSET and SIZE, marking all skipped
1202 lattices as containing variable and checking for overlaps. If there is no
1203 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1204 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1205 unless there are too many already. If there are two many, return false. If
1206 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1207 skipped lattices were newly marked as containing variable, set *CHANGE to
1208 true. */
1210 static bool
1211 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1212 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1213 struct ipcp_agg_lattice ***aglat,
1214 bool pre_existing, bool *change)
1216 gcc_checking_assert (offset >= 0);
1218 while (**aglat && (**aglat)->offset < offset)
1220 if ((**aglat)->offset + (**aglat)->size > offset)
1222 set_agg_lats_to_bottom (dest_plats);
1223 return false;
1225 *change |= set_lattice_contains_variable (**aglat);
1226 *aglat = &(**aglat)->next;
1229 if (**aglat && (**aglat)->offset == offset)
1231 if ((**aglat)->size != val_size
1232 || ((**aglat)->next
1233 && (**aglat)->next->offset < offset + val_size))
1235 set_agg_lats_to_bottom (dest_plats);
1236 return false;
1238 gcc_checking_assert (!(**aglat)->next
1239 || (**aglat)->next->offset >= offset + val_size);
1240 return true;
1242 else
1244 struct ipcp_agg_lattice *new_al;
1246 if (**aglat && (**aglat)->offset < offset + val_size)
1248 set_agg_lats_to_bottom (dest_plats);
1249 return false;
1251 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1252 return false;
1253 dest_plats->aggs_count++;
1254 new_al = (struct ipcp_agg_lattice *) pool_alloc (ipcp_agg_lattice_pool);
1255 memset (new_al, 0, sizeof (*new_al));
1257 new_al->offset = offset;
1258 new_al->size = val_size;
1259 new_al->contains_variable = pre_existing;
1261 new_al->next = **aglat;
1262 **aglat = new_al;
1263 return true;
1267 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1268 containing an unknown value. */
1270 static bool
1271 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1273 bool ret = false;
1274 while (aglat)
1276 ret |= set_lattice_contains_variable (aglat);
1277 aglat = aglat->next;
1279 return ret;
1282 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1283 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1284 parameter used for lattice value sources. Return true if DEST_PLATS changed
1285 in any way. */
1287 static bool
1288 merge_aggregate_lattices (struct cgraph_edge *cs,
1289 struct ipcp_param_lattices *dest_plats,
1290 struct ipcp_param_lattices *src_plats,
1291 int src_idx, HOST_WIDE_INT offset_delta)
1293 bool pre_existing = dest_plats->aggs != NULL;
1294 struct ipcp_agg_lattice **dst_aglat;
1295 bool ret = false;
1297 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1298 return true;
1299 if (src_plats->aggs_bottom)
1300 return set_agg_lats_contain_variable (dest_plats);
1301 if (src_plats->aggs_contain_variable)
1302 ret |= set_agg_lats_contain_variable (dest_plats);
1303 dst_aglat = &dest_plats->aggs;
1305 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1306 src_aglat;
1307 src_aglat = src_aglat->next)
1309 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1311 if (new_offset < 0)
1312 continue;
1313 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1314 &dst_aglat, pre_existing, &ret))
1316 struct ipcp_agg_lattice *new_al = *dst_aglat;
1318 dst_aglat = &(*dst_aglat)->next;
1319 if (src_aglat->bottom)
1321 ret |= set_lattice_contains_variable (new_al);
1322 continue;
1324 if (src_aglat->contains_variable)
1325 ret |= set_lattice_contains_variable (new_al);
1326 for (struct ipcp_value *val = src_aglat->values;
1327 val;
1328 val = val->next)
1329 ret |= add_value_to_lattice (new_al, val->value, cs, val, src_idx,
1330 src_aglat->offset);
1332 else if (dest_plats->aggs_bottom)
1333 return true;
1335 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1336 return ret;
1339 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1340 pass-through JFUNC and if so, whether it has conform and conforms to the
1341 rules about propagating values passed by reference. */
1343 static bool
1344 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1345 struct ipa_jump_func *jfunc)
1347 return src_plats->aggs
1348 && (!src_plats->aggs_by_ref
1349 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1352 /* Propagate scalar values across jump function JFUNC that is associated with
1353 edge CS and put the values into DEST_LAT. */
1355 static bool
1356 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1357 struct ipa_jump_func *jfunc,
1358 struct ipcp_param_lattices *dest_plats)
1360 bool ret = false;
1362 if (dest_plats->aggs_bottom)
1363 return false;
1365 if (jfunc->type == IPA_JF_PASS_THROUGH
1366 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1368 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1369 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1370 struct ipcp_param_lattices *src_plats;
1372 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1373 if (agg_pass_through_permissible_p (src_plats, jfunc))
1375 /* Currently we do not produce clobber aggregate jump
1376 functions, replace with merging when we do. */
1377 gcc_assert (!jfunc->agg.items);
1378 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1379 src_idx, 0);
1381 else
1382 ret |= set_agg_lats_contain_variable (dest_plats);
1384 else if (jfunc->type == IPA_JF_ANCESTOR
1385 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1387 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1388 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1389 struct ipcp_param_lattices *src_plats;
1391 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1392 if (src_plats->aggs && src_plats->aggs_by_ref)
1394 /* Currently we do not produce clobber aggregate jump
1395 functions, replace with merging when we do. */
1396 gcc_assert (!jfunc->agg.items);
1397 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1398 ipa_get_jf_ancestor_offset (jfunc));
1400 else if (!src_plats->aggs_by_ref)
1401 ret |= set_agg_lats_to_bottom (dest_plats);
1402 else
1403 ret |= set_agg_lats_contain_variable (dest_plats);
1405 else if (jfunc->agg.items)
1407 bool pre_existing = dest_plats->aggs != NULL;
1408 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1409 struct ipa_agg_jf_item *item;
1410 int i;
1412 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1413 return true;
1415 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1417 HOST_WIDE_INT val_size;
1419 if (item->offset < 0)
1420 continue;
1421 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1422 val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value)));
1424 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1425 &aglat, pre_existing, &ret))
1427 ret |= add_value_to_lattice (*aglat, item->value, cs, NULL, 0, 0);
1428 aglat = &(*aglat)->next;
1430 else if (dest_plats->aggs_bottom)
1431 return true;
1434 ret |= set_chain_of_aglats_contains_variable (*aglat);
1436 else
1437 ret |= set_agg_lats_contain_variable (dest_plats);
1439 return ret;
1442 /* Propagate constants from the caller to the callee of CS. INFO describes the
1443 caller. */
1445 static bool
1446 propagate_constants_accross_call (struct cgraph_edge *cs)
1448 struct ipa_node_params *callee_info;
1449 enum availability availability;
1450 struct cgraph_node *callee, *alias_or_thunk;
1451 struct ipa_edge_args *args;
1452 bool ret = false;
1453 int i, args_count, parms_count;
1455 callee = cs->callee->function_symbol (&availability);
1456 if (!callee->definition)
1457 return false;
1458 gcc_checking_assert (callee->has_gimple_body_p ());
1459 callee_info = IPA_NODE_REF (callee);
1461 args = IPA_EDGE_REF (cs);
1462 args_count = ipa_get_cs_argument_count (args);
1463 parms_count = ipa_get_param_count (callee_info);
1464 if (parms_count == 0)
1465 return false;
1467 /* If this call goes through a thunk we must not propagate to the first (0th)
1468 parameter. However, we might need to uncover a thunk from below a series
1469 of aliases first. */
1470 alias_or_thunk = cs->callee;
1471 while (alias_or_thunk->alias)
1472 alias_or_thunk = alias_or_thunk->get_alias_target ();
1473 if (alias_or_thunk->thunk.thunk_p)
1475 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1476 0));
1477 i = 1;
1479 else
1480 i = 0;
1482 for (; (i < args_count) && (i < parms_count); i++)
1484 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1485 struct ipcp_param_lattices *dest_plats;
1487 dest_plats = ipa_get_parm_lattices (callee_info, i);
1488 if (availability == AVAIL_INTERPOSABLE)
1489 ret |= set_all_contains_variable (dest_plats);
1490 else
1492 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1493 &dest_plats->itself);
1494 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1495 dest_plats);
1498 for (; i < parms_count; i++)
1499 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1501 return ret;
1504 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1505 (which can contain both constants and binfos), KNOWN_BINFOS, KNOWN_AGGS or
1506 AGG_REPS return the destination. The latter three can be NULL. If AGG_REPS
1507 is not NULL, KNOWN_AGGS is ignored. */
1509 static tree
1510 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1511 vec<tree> known_vals,
1512 vec<tree> known_binfos,
1513 vec<ipa_agg_jump_function_p> known_aggs,
1514 struct ipa_agg_replacement_value *agg_reps)
1516 int param_index = ie->indirect_info->param_index;
1517 HOST_WIDE_INT token, anc_offset;
1518 tree otr_type;
1519 tree t;
1520 tree target = NULL;
1522 if (param_index == -1
1523 || known_vals.length () <= (unsigned int) param_index)
1524 return NULL_TREE;
1526 if (!ie->indirect_info->polymorphic)
1528 tree t;
1530 if (ie->indirect_info->agg_contents)
1532 if (agg_reps)
1534 t = NULL;
1535 while (agg_reps)
1537 if (agg_reps->index == param_index
1538 && agg_reps->offset == ie->indirect_info->offset
1539 && agg_reps->by_ref == ie->indirect_info->by_ref)
1541 t = agg_reps->value;
1542 break;
1544 agg_reps = agg_reps->next;
1547 else if (known_aggs.length () > (unsigned int) param_index)
1549 struct ipa_agg_jump_function *agg;
1550 agg = known_aggs[param_index];
1551 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1552 ie->indirect_info->by_ref);
1554 else
1555 t = NULL;
1557 else
1558 t = known_vals[param_index];
1560 if (t &&
1561 TREE_CODE (t) == ADDR_EXPR
1562 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1563 return TREE_OPERAND (t, 0);
1564 else
1565 return NULL_TREE;
1568 if (!flag_devirtualize)
1569 return NULL_TREE;
1571 gcc_assert (!ie->indirect_info->agg_contents);
1572 token = ie->indirect_info->otr_token;
1573 anc_offset = ie->indirect_info->offset;
1574 otr_type = ie->indirect_info->otr_type;
1576 t = NULL;
1578 /* Try to work out value of virtual table pointer value in replacemnets. */
1579 if (!t && agg_reps && !ie->indirect_info->by_ref
1580 && !ie->indirect_info->vptr_changed)
1582 while (agg_reps)
1584 if (agg_reps->index == param_index
1585 && agg_reps->offset == ie->indirect_info->offset
1586 && agg_reps->by_ref)
1588 t = agg_reps->value;
1589 break;
1591 agg_reps = agg_reps->next;
1595 /* Try to work out value of virtual table pointer value in known
1596 aggregate values. */
1597 if (!t && known_aggs.length () > (unsigned int) param_index
1598 && !ie->indirect_info->by_ref
1599 && !ie->indirect_info->vptr_changed)
1601 struct ipa_agg_jump_function *agg;
1602 agg = known_aggs[param_index];
1603 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1604 true);
1607 /* If we found the virtual table pointer, lookup the target. */
1608 if (t)
1610 tree vtable;
1611 unsigned HOST_WIDE_INT offset;
1612 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
1614 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
1615 vtable, offset);
1616 if (target)
1618 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
1619 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
1620 || !possible_polymorphic_call_target_p
1621 (ie, cgraph_node::get (target)))
1622 target = ipa_impossible_devirt_target (ie, target);
1623 return target;
1628 /* Did we work out BINFO via type propagation? */
1629 if (!t && known_binfos.length () > (unsigned int) param_index)
1630 t = known_binfos[param_index];
1631 /* Or do we know the constant value of pointer? */
1632 if (!t)
1633 t = known_vals[param_index];
1634 if (!t)
1635 return NULL_TREE;
1637 if (TREE_CODE (t) != TREE_BINFO)
1639 ipa_polymorphic_call_context context (t, ie->indirect_info->otr_type,
1640 anc_offset);
1641 vec <cgraph_node *>targets;
1642 bool final;
1644 targets = possible_polymorphic_call_targets
1645 (ie->indirect_info->otr_type,
1646 ie->indirect_info->otr_token,
1647 context, &final);
1648 if (!final || targets.length () > 1)
1649 return NULL_TREE;
1650 if (targets.length () == 1)
1651 target = targets[0]->decl;
1652 else
1653 target = ipa_impossible_devirt_target (ie, NULL_TREE);
1655 else
1657 tree binfo;
1659 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1660 if (!binfo)
1661 return NULL_TREE;
1662 target = gimple_get_virt_method_for_binfo (token, binfo);
1665 if (target && !possible_polymorphic_call_target_p (ie,
1666 cgraph_node::get (target)))
1667 target = ipa_impossible_devirt_target (ie, target);
1669 return target;
1673 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1674 (which can contain both constants and binfos), KNOWN_BINFOS (which can be
1675 NULL) or KNOWN_AGGS (which also can be NULL) return the destination. */
1677 tree
1678 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1679 vec<tree> known_vals,
1680 vec<tree> known_binfos,
1681 vec<ipa_agg_jump_function_p> known_aggs)
1683 return ipa_get_indirect_edge_target_1 (ie, known_vals, known_binfos,
1684 known_aggs, NULL);
1687 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1688 and KNOWN_BINFOS. */
1690 static int
1691 devirtualization_time_bonus (struct cgraph_node *node,
1692 vec<tree> known_csts,
1693 vec<tree> known_binfos,
1694 vec<ipa_agg_jump_function_p> known_aggs)
1696 struct cgraph_edge *ie;
1697 int res = 0;
1699 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1701 struct cgraph_node *callee;
1702 struct inline_summary *isummary;
1703 enum availability avail;
1704 tree target;
1706 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos,
1707 known_aggs);
1708 if (!target)
1709 continue;
1711 /* Only bare minimum benefit for clearly un-inlineable targets. */
1712 res += 1;
1713 callee = cgraph_node::get (target);
1714 if (!callee || !callee->definition)
1715 continue;
1716 callee = callee->function_symbol (&avail);
1717 if (avail < AVAIL_AVAILABLE)
1718 continue;
1719 isummary = inline_summary (callee);
1720 if (!isummary->inlinable)
1721 continue;
1723 /* FIXME: The values below need re-considering and perhaps also
1724 integrating into the cost metrics, at lest in some very basic way. */
1725 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1726 res += 31;
1727 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1728 res += 15;
1729 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1730 || DECL_DECLARED_INLINE_P (callee->decl))
1731 res += 7;
1734 return res;
1737 /* Return time bonus incurred because of HINTS. */
1739 static int
1740 hint_time_bonus (inline_hints hints)
1742 int result = 0;
1743 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
1744 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
1745 if (hints & INLINE_HINT_array_index)
1746 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
1747 return result;
1750 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1751 and SIZE_COST and with the sum of frequencies of incoming edges to the
1752 potential new clone in FREQUENCIES. */
1754 static bool
1755 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1756 int freq_sum, gcov_type count_sum, int size_cost)
1758 if (time_benefit == 0
1759 || !flag_ipa_cp_clone
1760 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
1761 return false;
1763 gcc_assert (size_cost > 0);
1765 if (max_count)
1767 int factor = (count_sum * 1000) / max_count;
1768 int64_t evaluation = (((int64_t) time_benefit * factor)
1769 / size_cost);
1771 if (dump_file && (dump_flags & TDF_DETAILS))
1772 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1773 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1774 ") -> evaluation: " "%"PRId64
1775 ", threshold: %i\n",
1776 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1777 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1779 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1781 else
1783 int64_t evaluation = (((int64_t) time_benefit * freq_sum)
1784 / size_cost);
1786 if (dump_file && (dump_flags & TDF_DETAILS))
1787 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1788 "size: %i, freq_sum: %i) -> evaluation: "
1789 "%"PRId64 ", threshold: %i\n",
1790 time_benefit, size_cost, freq_sum, evaluation,
1791 PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1793 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1797 /* Return all context independent values from aggregate lattices in PLATS in a
1798 vector. Return NULL if there are none. */
1800 static vec<ipa_agg_jf_item, va_gc> *
1801 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
1803 vec<ipa_agg_jf_item, va_gc> *res = NULL;
1805 if (plats->aggs_bottom
1806 || plats->aggs_contain_variable
1807 || plats->aggs_count == 0)
1808 return NULL;
1810 for (struct ipcp_agg_lattice *aglat = plats->aggs;
1811 aglat;
1812 aglat = aglat->next)
1813 if (ipa_lat_is_single_const (aglat))
1815 struct ipa_agg_jf_item item;
1816 item.offset = aglat->offset;
1817 item.value = aglat->values->value;
1818 vec_safe_push (res, item);
1820 return res;
1823 /* Allocate KNOWN_CSTS, KNOWN_BINFOS and, if non-NULL, KNOWN_AGGS and populate
1824 them with values of parameters that are known independent of the context.
1825 INFO describes the function. If REMOVABLE_PARAMS_COST is non-NULL, the
1826 movement cost of all removable parameters will be stored in it. */
1828 static bool
1829 gather_context_independent_values (struct ipa_node_params *info,
1830 vec<tree> *known_csts,
1831 vec<tree> *known_binfos,
1832 vec<ipa_agg_jump_function> *known_aggs,
1833 int *removable_params_cost)
1835 int i, count = ipa_get_param_count (info);
1836 bool ret = false;
1838 known_csts->create (0);
1839 known_binfos->create (0);
1840 known_csts->safe_grow_cleared (count);
1841 known_binfos->safe_grow_cleared (count);
1842 if (known_aggs)
1844 known_aggs->create (0);
1845 known_aggs->safe_grow_cleared (count);
1848 if (removable_params_cost)
1849 *removable_params_cost = 0;
1851 for (i = 0; i < count ; i++)
1853 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1854 struct ipcp_lattice *lat = &plats->itself;
1856 if (ipa_lat_is_single_const (lat))
1858 struct ipcp_value *val = lat->values;
1859 if (TREE_CODE (val->value) != TREE_BINFO)
1861 (*known_csts)[i] = val->value;
1862 if (removable_params_cost)
1863 *removable_params_cost
1864 += estimate_move_cost (TREE_TYPE (val->value), false);
1865 ret = true;
1867 else if (plats->virt_call)
1869 (*known_binfos)[i] = val->value;
1870 ret = true;
1872 else if (removable_params_cost
1873 && !ipa_is_param_used (info, i))
1874 *removable_params_cost += ipa_get_param_move_cost (info, i);
1876 else if (removable_params_cost
1877 && !ipa_is_param_used (info, i))
1878 *removable_params_cost
1879 += ipa_get_param_move_cost (info, i);
1881 if (known_aggs)
1883 vec<ipa_agg_jf_item, va_gc> *agg_items;
1884 struct ipa_agg_jump_function *ajf;
1886 agg_items = context_independent_aggregate_values (plats);
1887 ajf = &(*known_aggs)[i];
1888 ajf->items = agg_items;
1889 ajf->by_ref = plats->aggs_by_ref;
1890 ret |= agg_items != NULL;
1894 return ret;
1897 /* The current interface in ipa-inline-analysis requires a pointer vector.
1898 Create it.
1900 FIXME: That interface should be re-worked, this is slightly silly. Still,
1901 I'd like to discuss how to change it first and this demonstrates the
1902 issue. */
1904 static vec<ipa_agg_jump_function_p>
1905 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs)
1907 vec<ipa_agg_jump_function_p> ret;
1908 struct ipa_agg_jump_function *ajf;
1909 int i;
1911 ret.create (known_aggs.length ());
1912 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
1913 ret.quick_push (ajf);
1914 return ret;
1917 /* Iterate over known values of parameters of NODE and estimate the local
1918 effects in terms of time and size they have. */
1920 static void
1921 estimate_local_effects (struct cgraph_node *node)
1923 struct ipa_node_params *info = IPA_NODE_REF (node);
1924 int i, count = ipa_get_param_count (info);
1925 vec<tree> known_csts, known_binfos;
1926 vec<ipa_agg_jump_function> known_aggs;
1927 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
1928 bool always_const;
1929 int base_time = inline_summary (node)->time;
1930 int removable_params_cost;
1932 if (!count || !ipcp_versionable_function_p (node))
1933 return;
1935 if (dump_file && (dump_flags & TDF_DETAILS))
1936 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1937 node->name (), node->order, base_time);
1939 always_const = gather_context_independent_values (info, &known_csts,
1940 &known_binfos, &known_aggs,
1941 &removable_params_cost);
1942 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
1943 if (always_const)
1945 struct caller_statistics stats;
1946 inline_hints hints;
1947 int time, size;
1949 init_caller_stats (&stats);
1950 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
1951 false);
1952 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1953 known_aggs_ptrs, &size, &time, &hints);
1954 time -= devirtualization_time_bonus (node, known_csts, known_binfos,
1955 known_aggs_ptrs);
1956 time -= hint_time_bonus (hints);
1957 time -= removable_params_cost;
1958 size -= stats.n_calls * removable_params_cost;
1960 if (dump_file)
1961 fprintf (dump_file, " - context independent values, size: %i, "
1962 "time_benefit: %i\n", size, base_time - time);
1964 if (size <= 0
1965 || node->will_be_removed_from_program_if_no_direct_calls_p ())
1967 info->do_clone_for_all_contexts = true;
1968 base_time = time;
1970 if (dump_file)
1971 fprintf (dump_file, " Decided to specialize for all "
1972 "known contexts, code not going to grow.\n");
1974 else if (good_cloning_opportunity_p (node, base_time - time,
1975 stats.freq_sum, stats.count_sum,
1976 size))
1978 if (size + overall_size <= max_new_size)
1980 info->do_clone_for_all_contexts = true;
1981 base_time = time;
1982 overall_size += size;
1984 if (dump_file)
1985 fprintf (dump_file, " Decided to specialize for all "
1986 "known contexts, growth deemed beneficial.\n");
1988 else if (dump_file && (dump_flags & TDF_DETAILS))
1989 fprintf (dump_file, " Not cloning for all contexts because "
1990 "max_new_size would be reached with %li.\n",
1991 size + overall_size);
1995 for (i = 0; i < count ; i++)
1997 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1998 struct ipcp_lattice *lat = &plats->itself;
1999 struct ipcp_value *val;
2000 int emc;
2002 if (lat->bottom
2003 || !lat->values
2004 || known_csts[i]
2005 || known_binfos[i])
2006 continue;
2008 for (val = lat->values; val; val = val->next)
2010 int time, size, time_benefit;
2011 inline_hints hints;
2013 if (TREE_CODE (val->value) != TREE_BINFO)
2015 known_csts[i] = val->value;
2016 known_binfos[i] = NULL_TREE;
2017 emc = estimate_move_cost (TREE_TYPE (val->value), true);
2019 else if (plats->virt_call)
2021 known_csts[i] = NULL_TREE;
2022 known_binfos[i] = val->value;
2023 emc = 0;
2025 else
2026 continue;
2028 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
2029 known_aggs_ptrs, &size, &time,
2030 &hints);
2031 time_benefit = base_time - time
2032 + devirtualization_time_bonus (node, known_csts, known_binfos,
2033 known_aggs_ptrs)
2034 + hint_time_bonus (hints)
2035 + removable_params_cost + emc;
2037 gcc_checking_assert (size >=0);
2038 /* The inliner-heuristics based estimates may think that in certain
2039 contexts some functions do not have any size at all but we want
2040 all specializations to have at least a tiny cost, not least not to
2041 divide by zero. */
2042 if (size == 0)
2043 size = 1;
2045 if (dump_file && (dump_flags & TDF_DETAILS))
2047 fprintf (dump_file, " - estimates for value ");
2048 print_ipcp_constant_value (dump_file, val->value);
2049 fprintf (dump_file, " for ");
2050 ipa_dump_param (dump_file, info, i);
2051 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
2052 time_benefit, size);
2055 val->local_time_benefit = time_benefit;
2056 val->local_size_cost = size;
2058 known_binfos[i] = NULL_TREE;
2059 known_csts[i] = NULL_TREE;
2062 for (i = 0; i < count ; i++)
2064 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2065 struct ipa_agg_jump_function *ajf;
2066 struct ipcp_agg_lattice *aglat;
2068 if (plats->aggs_bottom || !plats->aggs)
2069 continue;
2071 ajf = &known_aggs[i];
2072 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2074 struct ipcp_value *val;
2075 if (aglat->bottom || !aglat->values
2076 /* If the following is true, the one value is in known_aggs. */
2077 || (!plats->aggs_contain_variable
2078 && ipa_lat_is_single_const (aglat)))
2079 continue;
2081 for (val = aglat->values; val; val = val->next)
2083 int time, size, time_benefit;
2084 struct ipa_agg_jf_item item;
2085 inline_hints hints;
2087 item.offset = aglat->offset;
2088 item.value = val->value;
2089 vec_safe_push (ajf->items, item);
2091 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
2092 known_aggs_ptrs, &size, &time,
2093 &hints);
2094 time_benefit = base_time - time
2095 + devirtualization_time_bonus (node, known_csts, known_binfos,
2096 known_aggs_ptrs)
2097 + hint_time_bonus (hints);
2098 gcc_checking_assert (size >=0);
2099 if (size == 0)
2100 size = 1;
2102 if (dump_file && (dump_flags & TDF_DETAILS))
2104 fprintf (dump_file, " - estimates for value ");
2105 print_ipcp_constant_value (dump_file, val->value);
2106 fprintf (dump_file, " for ");
2107 ipa_dump_param (dump_file, info, i);
2108 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2109 "]: time_benefit: %i, size: %i\n",
2110 plats->aggs_by_ref ? "ref " : "",
2111 aglat->offset, time_benefit, size);
2114 val->local_time_benefit = time_benefit;
2115 val->local_size_cost = size;
2116 ajf->items->pop ();
2121 for (i = 0; i < count ; i++)
2122 vec_free (known_aggs[i].items);
2124 known_csts.release ();
2125 known_binfos.release ();
2126 known_aggs.release ();
2127 known_aggs_ptrs.release ();
2131 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2132 topological sort of values. */
2134 static void
2135 add_val_to_toposort (struct ipcp_value *cur_val)
2137 static int dfs_counter = 0;
2138 static struct ipcp_value *stack;
2139 struct ipcp_value_source *src;
2141 if (cur_val->dfs)
2142 return;
2144 dfs_counter++;
2145 cur_val->dfs = dfs_counter;
2146 cur_val->low_link = dfs_counter;
2148 cur_val->topo_next = stack;
2149 stack = cur_val;
2150 cur_val->on_stack = true;
2152 for (src = cur_val->sources; src; src = src->next)
2153 if (src->val)
2155 if (src->val->dfs == 0)
2157 add_val_to_toposort (src->val);
2158 if (src->val->low_link < cur_val->low_link)
2159 cur_val->low_link = src->val->low_link;
2161 else if (src->val->on_stack
2162 && src->val->dfs < cur_val->low_link)
2163 cur_val->low_link = src->val->dfs;
2166 if (cur_val->dfs == cur_val->low_link)
2168 struct ipcp_value *v, *scc_list = NULL;
2172 v = stack;
2173 stack = v->topo_next;
2174 v->on_stack = false;
2176 v->scc_next = scc_list;
2177 scc_list = v;
2179 while (v != cur_val);
2181 cur_val->topo_next = values_topo;
2182 values_topo = cur_val;
2186 /* Add all values in lattices associated with NODE to the topological sort if
2187 they are not there yet. */
2189 static void
2190 add_all_node_vals_to_toposort (struct cgraph_node *node)
2192 struct ipa_node_params *info = IPA_NODE_REF (node);
2193 int i, count = ipa_get_param_count (info);
2195 for (i = 0; i < count ; i++)
2197 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2198 struct ipcp_lattice *lat = &plats->itself;
2199 struct ipcp_agg_lattice *aglat;
2200 struct ipcp_value *val;
2202 if (!lat->bottom)
2203 for (val = lat->values; val; val = val->next)
2204 add_val_to_toposort (val);
2206 if (!plats->aggs_bottom)
2207 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2208 if (!aglat->bottom)
2209 for (val = aglat->values; val; val = val->next)
2210 add_val_to_toposort (val);
2214 /* One pass of constants propagation along the call graph edges, from callers
2215 to callees (requires topological ordering in TOPO), iterate over strongly
2216 connected components. */
2218 static void
2219 propagate_constants_topo (struct ipa_topo_info *topo)
2221 int i;
2223 for (i = topo->nnodes - 1; i >= 0; i--)
2225 unsigned j;
2226 struct cgraph_node *v, *node = topo->order[i];
2227 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
2229 /* First, iteratively propagate within the strongly connected component
2230 until all lattices stabilize. */
2231 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2232 if (v->has_gimple_body_p ())
2233 push_node_to_stack (topo, v);
2235 v = pop_node_from_stack (topo);
2236 while (v)
2238 struct cgraph_edge *cs;
2240 for (cs = v->callees; cs; cs = cs->next_callee)
2241 if (ipa_edge_within_scc (cs)
2242 && propagate_constants_accross_call (cs))
2243 push_node_to_stack (topo, cs->callee);
2244 v = pop_node_from_stack (topo);
2247 /* Afterwards, propagate along edges leading out of the SCC, calculates
2248 the local effects of the discovered constants and all valid values to
2249 their topological sort. */
2250 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
2251 if (v->has_gimple_body_p ())
2253 struct cgraph_edge *cs;
2255 estimate_local_effects (v);
2256 add_all_node_vals_to_toposort (v);
2257 for (cs = v->callees; cs; cs = cs->next_callee)
2258 if (!ipa_edge_within_scc (cs))
2259 propagate_constants_accross_call (cs);
2261 cycle_nodes.release ();
2266 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2267 the bigger one if otherwise. */
2269 static int
2270 safe_add (int a, int b)
2272 if (a > INT_MAX/2 || b > INT_MAX/2)
2273 return a > b ? a : b;
2274 else
2275 return a + b;
2279 /* Propagate the estimated effects of individual values along the topological
2280 from the dependent values to those they depend on. */
2282 static void
2283 propagate_effects (void)
2285 struct ipcp_value *base;
2287 for (base = values_topo; base; base = base->topo_next)
2289 struct ipcp_value_source *src;
2290 struct ipcp_value *val;
2291 int time = 0, size = 0;
2293 for (val = base; val; val = val->scc_next)
2295 time = safe_add (time,
2296 val->local_time_benefit + val->prop_time_benefit);
2297 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2300 for (val = base; val; val = val->scc_next)
2301 for (src = val->sources; src; src = src->next)
2302 if (src->val
2303 && src->cs->maybe_hot_p ())
2305 src->val->prop_time_benefit = safe_add (time,
2306 src->val->prop_time_benefit);
2307 src->val->prop_size_cost = safe_add (size,
2308 src->val->prop_size_cost);
2314 /* Propagate constants, binfos and their effects from the summaries
2315 interprocedurally. */
2317 static void
2318 ipcp_propagate_stage (struct ipa_topo_info *topo)
2320 struct cgraph_node *node;
2322 if (dump_file)
2323 fprintf (dump_file, "\n Propagating constants:\n\n");
2325 if (in_lto_p)
2326 ipa_update_after_lto_read ();
2329 FOR_EACH_DEFINED_FUNCTION (node)
2331 struct ipa_node_params *info = IPA_NODE_REF (node);
2333 determine_versionability (node);
2334 if (node->has_gimple_body_p ())
2336 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2337 ipa_get_param_count (info));
2338 initialize_node_lattices (node);
2340 if (node->definition && !node->alias)
2341 overall_size += inline_summary (node)->self_size;
2342 if (node->count > max_count)
2343 max_count = node->count;
2346 max_new_size = overall_size;
2347 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2348 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2349 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2351 if (dump_file)
2352 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2353 overall_size, max_new_size);
2355 propagate_constants_topo (topo);
2356 #ifdef ENABLE_CHECKING
2357 ipcp_verify_propagated_values ();
2358 #endif
2359 propagate_effects ();
2361 if (dump_file)
2363 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2364 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2368 /* Discover newly direct outgoing edges from NODE which is a new clone with
2369 known KNOWN_VALS and make them direct. */
2371 static void
2372 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2373 vec<tree> known_vals,
2374 struct ipa_agg_replacement_value *aggvals)
2376 struct cgraph_edge *ie, *next_ie;
2377 bool found = false;
2379 for (ie = node->indirect_calls; ie; ie = next_ie)
2381 tree target;
2383 next_ie = ie->next_callee;
2384 target = ipa_get_indirect_edge_target_1 (ie, known_vals, vNULL, vNULL,
2385 aggvals);
2386 if (target)
2388 bool agg_contents = ie->indirect_info->agg_contents;
2389 bool polymorphic = ie->indirect_info->polymorphic;
2390 int param_index = ie->indirect_info->param_index;
2391 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target);
2392 found = true;
2394 if (cs && !agg_contents && !polymorphic)
2396 struct ipa_node_params *info = IPA_NODE_REF (node);
2397 int c = ipa_get_controlled_uses (info, param_index);
2398 if (c != IPA_UNDESCRIBED_USE)
2400 struct ipa_ref *to_del;
2402 c--;
2403 ipa_set_controlled_uses (info, param_index, c);
2404 if (dump_file && (dump_flags & TDF_DETAILS))
2405 fprintf (dump_file, " controlled uses count of param "
2406 "%i bumped down to %i\n", param_index, c);
2407 if (c == 0
2408 && (to_del = node->find_reference (cs->callee, NULL, 0)))
2410 if (dump_file && (dump_flags & TDF_DETAILS))
2411 fprintf (dump_file, " and even removing its "
2412 "cloning-created reference\n");
2413 to_del->remove_reference ();
2419 /* Turning calls to direct calls will improve overall summary. */
2420 if (found)
2421 inline_update_overall_summary (node);
2424 /* Vector of pointers which for linked lists of clones of an original crgaph
2425 edge. */
2427 static vec<cgraph_edge *> next_edge_clone;
2428 static vec<cgraph_edge *> prev_edge_clone;
2430 static inline void
2431 grow_edge_clone_vectors (void)
2433 if (next_edge_clone.length ()
2434 <= (unsigned) symtab->edges_max_uid)
2435 next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2436 if (prev_edge_clone.length ()
2437 <= (unsigned) symtab->edges_max_uid)
2438 prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1);
2441 /* Edge duplication hook to grow the appropriate linked list in
2442 next_edge_clone. */
2444 static void
2445 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2446 void *)
2448 grow_edge_clone_vectors ();
2450 struct cgraph_edge *old_next = next_edge_clone[src->uid];
2451 if (old_next)
2452 prev_edge_clone[old_next->uid] = dst;
2453 prev_edge_clone[dst->uid] = src;
2455 next_edge_clone[dst->uid] = old_next;
2456 next_edge_clone[src->uid] = dst;
2459 /* Hook that is called by cgraph.c when an edge is removed. */
2461 static void
2462 ipcp_edge_removal_hook (struct cgraph_edge *cs, void *)
2464 grow_edge_clone_vectors ();
2466 struct cgraph_edge *prev = prev_edge_clone[cs->uid];
2467 struct cgraph_edge *next = next_edge_clone[cs->uid];
2468 if (prev)
2469 next_edge_clone[prev->uid] = next;
2470 if (next)
2471 prev_edge_clone[next->uid] = prev;
2474 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2475 parameter with the given INDEX. */
2477 static tree
2478 get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset,
2479 int index)
2481 struct ipa_agg_replacement_value *aggval;
2483 aggval = ipa_get_agg_replacements_for_node (node);
2484 while (aggval)
2486 if (aggval->offset == offset
2487 && aggval->index == index)
2488 return aggval->value;
2489 aggval = aggval->next;
2491 return NULL_TREE;
2494 /* Return true if edge CS does bring about the value described by SRC. */
2496 static bool
2497 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
2498 struct ipcp_value_source *src)
2500 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2501 cgraph_node *real_dest = cs->callee->function_symbol ();
2502 struct ipa_node_params *dst_info = IPA_NODE_REF (real_dest);
2504 if ((dst_info->ipcp_orig_node && !dst_info->is_all_contexts_clone)
2505 || caller_info->node_dead)
2506 return false;
2507 if (!src->val)
2508 return true;
2510 if (caller_info->ipcp_orig_node)
2512 tree t;
2513 if (src->offset == -1)
2514 t = caller_info->known_vals[src->index];
2515 else
2516 t = get_clone_agg_value (cs->caller, src->offset, src->index);
2517 return (t != NULL_TREE
2518 && values_equal_for_ipcp_p (src->val->value, t));
2520 else
2522 struct ipcp_agg_lattice *aglat;
2523 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
2524 src->index);
2525 if (src->offset == -1)
2526 return (ipa_lat_is_single_const (&plats->itself)
2527 && values_equal_for_ipcp_p (src->val->value,
2528 plats->itself.values->value));
2529 else
2531 if (plats->aggs_bottom || plats->aggs_contain_variable)
2532 return false;
2533 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2534 if (aglat->offset == src->offset)
2535 return (ipa_lat_is_single_const (aglat)
2536 && values_equal_for_ipcp_p (src->val->value,
2537 aglat->values->value));
2539 return false;
2543 /* Get the next clone in the linked list of clones of an edge. */
2545 static inline struct cgraph_edge *
2546 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
2548 return next_edge_clone[cs->uid];
2551 /* Given VAL, iterate over all its sources and if they still hold, add their
2552 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
2553 respectively. */
2555 static bool
2556 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
2557 gcov_type *count_sum, int *caller_count)
2559 struct ipcp_value_source *src;
2560 int freq = 0, count = 0;
2561 gcov_type cnt = 0;
2562 bool hot = false;
2564 for (src = val->sources; src; src = src->next)
2566 struct cgraph_edge *cs = src->cs;
2567 while (cs)
2569 if (cgraph_edge_brings_value_p (cs, src))
2571 count++;
2572 freq += cs->frequency;
2573 cnt += cs->count;
2574 hot |= cs->maybe_hot_p ();
2576 cs = get_next_cgraph_edge_clone (cs);
2580 *freq_sum = freq;
2581 *count_sum = cnt;
2582 *caller_count = count;
2583 return hot;
2586 /* Return a vector of incoming edges that do bring value VAL. It is assumed
2587 their number is known and equal to CALLER_COUNT. */
2589 static vec<cgraph_edge *>
2590 gather_edges_for_value (struct ipcp_value *val, int caller_count)
2592 struct ipcp_value_source *src;
2593 vec<cgraph_edge *> ret;
2595 ret.create (caller_count);
2596 for (src = val->sources; src; src = src->next)
2598 struct cgraph_edge *cs = src->cs;
2599 while (cs)
2601 if (cgraph_edge_brings_value_p (cs, src))
2602 ret.quick_push (cs);
2603 cs = get_next_cgraph_edge_clone (cs);
2607 return ret;
2610 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
2611 Return it or NULL if for some reason it cannot be created. */
2613 static struct ipa_replace_map *
2614 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
2616 struct ipa_replace_map *replace_map;
2619 replace_map = ggc_alloc<ipa_replace_map> ();
2620 if (dump_file)
2622 fprintf (dump_file, " replacing ");
2623 ipa_dump_param (dump_file, info, parm_num);
2625 fprintf (dump_file, " with const ");
2626 print_generic_expr (dump_file, value, 0);
2627 fprintf (dump_file, "\n");
2629 replace_map->old_tree = NULL;
2630 replace_map->parm_num = parm_num;
2631 replace_map->new_tree = value;
2632 replace_map->replace_p = true;
2633 replace_map->ref_p = false;
2635 return replace_map;
2638 /* Dump new profiling counts */
2640 static void
2641 dump_profile_updates (struct cgraph_node *orig_node,
2642 struct cgraph_node *new_node)
2644 struct cgraph_edge *cs;
2646 fprintf (dump_file, " setting count of the specialized node to "
2647 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
2648 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2649 fprintf (dump_file, " edge to %s has count "
2650 HOST_WIDE_INT_PRINT_DEC "\n",
2651 cs->callee->name (), (HOST_WIDE_INT) cs->count);
2653 fprintf (dump_file, " setting count of the original node to "
2654 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
2655 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2656 fprintf (dump_file, " edge to %s is left with "
2657 HOST_WIDE_INT_PRINT_DEC "\n",
2658 cs->callee->name (), (HOST_WIDE_INT) cs->count);
2661 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
2662 their profile information to reflect this. */
2664 static void
2665 update_profiling_info (struct cgraph_node *orig_node,
2666 struct cgraph_node *new_node)
2668 struct cgraph_edge *cs;
2669 struct caller_statistics stats;
2670 gcov_type new_sum, orig_sum;
2671 gcov_type remainder, orig_node_count = orig_node->count;
2673 if (orig_node_count == 0)
2674 return;
2676 init_caller_stats (&stats);
2677 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2678 false);
2679 orig_sum = stats.count_sum;
2680 init_caller_stats (&stats);
2681 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
2682 false);
2683 new_sum = stats.count_sum;
2685 if (orig_node_count < orig_sum + new_sum)
2687 if (dump_file)
2688 fprintf (dump_file, " Problem: node %s/%i has too low count "
2689 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
2690 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
2691 orig_node->name (), orig_node->order,
2692 (HOST_WIDE_INT) orig_node_count,
2693 (HOST_WIDE_INT) (orig_sum + new_sum));
2695 orig_node_count = (orig_sum + new_sum) * 12 / 10;
2696 if (dump_file)
2697 fprintf (dump_file, " proceeding by pretending it was "
2698 HOST_WIDE_INT_PRINT_DEC "\n",
2699 (HOST_WIDE_INT) orig_node_count);
2702 new_node->count = new_sum;
2703 remainder = orig_node_count - new_sum;
2704 orig_node->count = remainder;
2706 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2707 if (cs->frequency)
2708 cs->count = apply_probability (cs->count,
2709 GCOV_COMPUTE_SCALE (new_sum,
2710 orig_node_count));
2711 else
2712 cs->count = 0;
2714 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2715 cs->count = apply_probability (cs->count,
2716 GCOV_COMPUTE_SCALE (remainder,
2717 orig_node_count));
2719 if (dump_file)
2720 dump_profile_updates (orig_node, new_node);
2723 /* Update the respective profile of specialized NEW_NODE and the original
2724 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
2725 have been redirected to the specialized version. */
2727 static void
2728 update_specialized_profile (struct cgraph_node *new_node,
2729 struct cgraph_node *orig_node,
2730 gcov_type redirected_sum)
2732 struct cgraph_edge *cs;
2733 gcov_type new_node_count, orig_node_count = orig_node->count;
2735 if (dump_file)
2736 fprintf (dump_file, " the sum of counts of redirected edges is "
2737 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
2738 if (orig_node_count == 0)
2739 return;
2741 gcc_assert (orig_node_count >= redirected_sum);
2743 new_node_count = new_node->count;
2744 new_node->count += redirected_sum;
2745 orig_node->count -= redirected_sum;
2747 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2748 if (cs->frequency)
2749 cs->count += apply_probability (cs->count,
2750 GCOV_COMPUTE_SCALE (redirected_sum,
2751 new_node_count));
2752 else
2753 cs->count = 0;
2755 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2757 gcov_type dec = apply_probability (cs->count,
2758 GCOV_COMPUTE_SCALE (redirected_sum,
2759 orig_node_count));
2760 if (dec < cs->count)
2761 cs->count -= dec;
2762 else
2763 cs->count = 0;
2766 if (dump_file)
2767 dump_profile_updates (orig_node, new_node);
2770 /* Create a specialized version of NODE with known constants and types of
2771 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
2773 static struct cgraph_node *
2774 create_specialized_node (struct cgraph_node *node,
2775 vec<tree> known_vals,
2776 struct ipa_agg_replacement_value *aggvals,
2777 vec<cgraph_edge *> callers)
2779 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
2780 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
2781 struct ipa_agg_replacement_value *av;
2782 struct cgraph_node *new_node;
2783 int i, count = ipa_get_param_count (info);
2784 bitmap args_to_skip;
2786 gcc_assert (!info->ipcp_orig_node);
2788 if (node->local.can_change_signature)
2790 args_to_skip = BITMAP_GGC_ALLOC ();
2791 for (i = 0; i < count; i++)
2793 tree t = known_vals[i];
2795 if ((t && TREE_CODE (t) != TREE_BINFO)
2796 || !ipa_is_param_used (info, i))
2797 bitmap_set_bit (args_to_skip, i);
2800 else
2802 args_to_skip = NULL;
2803 if (dump_file && (dump_flags & TDF_DETAILS))
2804 fprintf (dump_file, " cannot change function signature\n");
2807 for (i = 0; i < count ; i++)
2809 tree t = known_vals[i];
2810 if (t && TREE_CODE (t) != TREE_BINFO)
2812 struct ipa_replace_map *replace_map;
2814 replace_map = get_replacement_map (info, t, i);
2815 if (replace_map)
2816 vec_safe_push (replace_trees, replace_map);
2820 new_node = node->create_virtual_clone (callers, replace_trees,
2821 args_to_skip, "constprop");
2822 ipa_set_node_agg_value_chain (new_node, aggvals);
2823 for (av = aggvals; av; av = av->next)
2824 new_node->maybe_create_reference (av->value, IPA_REF_ADDR, NULL);
2826 if (dump_file && (dump_flags & TDF_DETAILS))
2828 fprintf (dump_file, " the new node is %s/%i.\n",
2829 new_node->name (), new_node->order);
2830 if (aggvals)
2831 ipa_dump_agg_replacement_values (dump_file, aggvals);
2833 ipa_check_create_node_params ();
2834 update_profiling_info (node, new_node);
2835 new_info = IPA_NODE_REF (new_node);
2836 new_info->ipcp_orig_node = node;
2837 new_info->known_vals = known_vals;
2839 ipcp_discover_new_direct_edges (new_node, known_vals, aggvals);
2841 callers.release ();
2842 return new_node;
2845 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2846 KNOWN_VALS with constants and types that are also known for all of the
2847 CALLERS. */
2849 static void
2850 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
2851 vec<tree> known_vals,
2852 vec<cgraph_edge *> callers)
2854 struct ipa_node_params *info = IPA_NODE_REF (node);
2855 int i, count = ipa_get_param_count (info);
2857 for (i = 0; i < count ; i++)
2859 struct cgraph_edge *cs;
2860 tree newval = NULL_TREE;
2861 int j;
2863 if (ipa_get_scalar_lat (info, i)->bottom || known_vals[i])
2864 continue;
2866 FOR_EACH_VEC_ELT (callers, j, cs)
2868 struct ipa_jump_func *jump_func;
2869 tree t;
2871 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2873 newval = NULL_TREE;
2874 break;
2876 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2877 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2878 if (!t
2879 || (newval
2880 && !values_equal_for_ipcp_p (t, newval)))
2882 newval = NULL_TREE;
2883 break;
2885 else
2886 newval = t;
2889 if (newval)
2891 if (dump_file && (dump_flags & TDF_DETAILS))
2893 fprintf (dump_file, " adding an extra known scalar value ");
2894 print_ipcp_constant_value (dump_file, newval);
2895 fprintf (dump_file, " for ");
2896 ipa_dump_param (dump_file, info, i);
2897 fprintf (dump_file, "\n");
2900 known_vals[i] = newval;
2905 /* Go through PLATS and create a vector of values consisting of values and
2906 offsets (minus OFFSET) of lattices that contain only a single value. */
2908 static vec<ipa_agg_jf_item>
2909 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
2911 vec<ipa_agg_jf_item> res = vNULL;
2913 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2914 return vNULL;
2916 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
2917 if (ipa_lat_is_single_const (aglat))
2919 struct ipa_agg_jf_item ti;
2920 ti.offset = aglat->offset - offset;
2921 ti.value = aglat->values->value;
2922 res.safe_push (ti);
2924 return res;
2927 /* Intersect all values in INTER with single value lattices in PLATS (while
2928 subtracting OFFSET). */
2930 static void
2931 intersect_with_plats (struct ipcp_param_lattices *plats,
2932 vec<ipa_agg_jf_item> *inter,
2933 HOST_WIDE_INT offset)
2935 struct ipcp_agg_lattice *aglat;
2936 struct ipa_agg_jf_item *item;
2937 int k;
2939 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2941 inter->release ();
2942 return;
2945 aglat = plats->aggs;
2946 FOR_EACH_VEC_ELT (*inter, k, item)
2948 bool found = false;
2949 if (!item->value)
2950 continue;
2951 while (aglat)
2953 if (aglat->offset - offset > item->offset)
2954 break;
2955 if (aglat->offset - offset == item->offset)
2957 gcc_checking_assert (item->value);
2958 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
2959 found = true;
2960 break;
2962 aglat = aglat->next;
2964 if (!found)
2965 item->value = NULL_TREE;
2969 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
2970 vector result while subtracting OFFSET from the individual value offsets. */
2972 static vec<ipa_agg_jf_item>
2973 agg_replacements_to_vector (struct cgraph_node *node, int index,
2974 HOST_WIDE_INT offset)
2976 struct ipa_agg_replacement_value *av;
2977 vec<ipa_agg_jf_item> res = vNULL;
2979 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
2980 if (av->index == index
2981 && (av->offset - offset) >= 0)
2983 struct ipa_agg_jf_item item;
2984 gcc_checking_assert (av->value);
2985 item.offset = av->offset - offset;
2986 item.value = av->value;
2987 res.safe_push (item);
2990 return res;
2993 /* Intersect all values in INTER with those that we have already scheduled to
2994 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
2995 (while subtracting OFFSET). */
2997 static void
2998 intersect_with_agg_replacements (struct cgraph_node *node, int index,
2999 vec<ipa_agg_jf_item> *inter,
3000 HOST_WIDE_INT offset)
3002 struct ipa_agg_replacement_value *srcvals;
3003 struct ipa_agg_jf_item *item;
3004 int i;
3006 srcvals = ipa_get_agg_replacements_for_node (node);
3007 if (!srcvals)
3009 inter->release ();
3010 return;
3013 FOR_EACH_VEC_ELT (*inter, i, item)
3015 struct ipa_agg_replacement_value *av;
3016 bool found = false;
3017 if (!item->value)
3018 continue;
3019 for (av = srcvals; av; av = av->next)
3021 gcc_checking_assert (av->value);
3022 if (av->index == index
3023 && av->offset - offset == item->offset)
3025 if (values_equal_for_ipcp_p (item->value, av->value))
3026 found = true;
3027 break;
3030 if (!found)
3031 item->value = NULL_TREE;
3035 /* Intersect values in INTER with aggregate values that come along edge CS to
3036 parameter number INDEX and return it. If INTER does not actually exist yet,
3037 copy all incoming values to it. If we determine we ended up with no values
3038 whatsoever, return a released vector. */
3040 static vec<ipa_agg_jf_item>
3041 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
3042 vec<ipa_agg_jf_item> inter)
3044 struct ipa_jump_func *jfunc;
3045 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
3046 if (jfunc->type == IPA_JF_PASS_THROUGH
3047 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3049 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3050 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3052 if (caller_info->ipcp_orig_node)
3054 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
3055 struct ipcp_param_lattices *orig_plats;
3056 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
3057 src_idx);
3058 if (agg_pass_through_permissible_p (orig_plats, jfunc))
3060 if (!inter.exists ())
3061 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
3062 else
3063 intersect_with_agg_replacements (cs->caller, src_idx,
3064 &inter, 0);
3066 else
3068 inter.release ();
3069 return vNULL;
3072 else
3074 struct ipcp_param_lattices *src_plats;
3075 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3076 if (agg_pass_through_permissible_p (src_plats, jfunc))
3078 /* Currently we do not produce clobber aggregate jump
3079 functions, adjust when we do. */
3080 gcc_checking_assert (!jfunc->agg.items);
3081 if (!inter.exists ())
3082 inter = copy_plats_to_inter (src_plats, 0);
3083 else
3084 intersect_with_plats (src_plats, &inter, 0);
3086 else
3088 inter.release ();
3089 return vNULL;
3093 else if (jfunc->type == IPA_JF_ANCESTOR
3094 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3096 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
3097 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3098 struct ipcp_param_lattices *src_plats;
3099 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
3101 if (caller_info->ipcp_orig_node)
3103 if (!inter.exists ())
3104 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
3105 else
3106 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
3107 delta);
3109 else
3111 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
3112 /* Currently we do not produce clobber aggregate jump
3113 functions, adjust when we do. */
3114 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
3115 if (!inter.exists ())
3116 inter = copy_plats_to_inter (src_plats, delta);
3117 else
3118 intersect_with_plats (src_plats, &inter, delta);
3121 else if (jfunc->agg.items)
3123 struct ipa_agg_jf_item *item;
3124 int k;
3126 if (!inter.exists ())
3127 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
3128 inter.safe_push ((*jfunc->agg.items)[i]);
3129 else
3130 FOR_EACH_VEC_ELT (inter, k, item)
3132 int l = 0;
3133 bool found = false;;
3135 if (!item->value)
3136 continue;
3138 while ((unsigned) l < jfunc->agg.items->length ())
3140 struct ipa_agg_jf_item *ti;
3141 ti = &(*jfunc->agg.items)[l];
3142 if (ti->offset > item->offset)
3143 break;
3144 if (ti->offset == item->offset)
3146 gcc_checking_assert (ti->value);
3147 if (values_equal_for_ipcp_p (item->value,
3148 ti->value))
3149 found = true;
3150 break;
3152 l++;
3154 if (!found)
3155 item->value = NULL;
3158 else
3160 inter.release ();
3161 return vec<ipa_agg_jf_item>();
3163 return inter;
3166 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3167 from all of them. */
3169 static struct ipa_agg_replacement_value *
3170 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3171 vec<cgraph_edge *> callers)
3173 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3174 struct ipa_agg_replacement_value *res;
3175 struct ipa_agg_replacement_value **tail = &res;
3176 struct cgraph_edge *cs;
3177 int i, j, count = ipa_get_param_count (dest_info);
3179 FOR_EACH_VEC_ELT (callers, j, cs)
3181 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3182 if (c < count)
3183 count = c;
3186 for (i = 0; i < count ; i++)
3188 struct cgraph_edge *cs;
3189 vec<ipa_agg_jf_item> inter = vNULL;
3190 struct ipa_agg_jf_item *item;
3191 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3192 int j;
3194 /* Among other things, the following check should deal with all by_ref
3195 mismatches. */
3196 if (plats->aggs_bottom)
3197 continue;
3199 FOR_EACH_VEC_ELT (callers, j, cs)
3201 inter = intersect_aggregates_with_edge (cs, i, inter);
3203 if (!inter.exists ())
3204 goto next_param;
3207 FOR_EACH_VEC_ELT (inter, j, item)
3209 struct ipa_agg_replacement_value *v;
3211 if (!item->value)
3212 continue;
3214 v = ggc_alloc<ipa_agg_replacement_value> ();
3215 v->index = i;
3216 v->offset = item->offset;
3217 v->value = item->value;
3218 v->by_ref = plats->aggs_by_ref;
3219 *tail = v;
3220 tail = &v->next;
3223 next_param:
3224 if (inter.exists ())
3225 inter.release ();
3227 *tail = NULL;
3228 return res;
3231 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3233 static struct ipa_agg_replacement_value *
3234 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function> known_aggs)
3236 struct ipa_agg_replacement_value *res;
3237 struct ipa_agg_replacement_value **tail = &res;
3238 struct ipa_agg_jump_function *aggjf;
3239 struct ipa_agg_jf_item *item;
3240 int i, j;
3242 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3243 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3245 struct ipa_agg_replacement_value *v;
3246 v = ggc_alloc<ipa_agg_replacement_value> ();
3247 v->index = i;
3248 v->offset = item->offset;
3249 v->value = item->value;
3250 v->by_ref = aggjf->by_ref;
3251 *tail = v;
3252 tail = &v->next;
3254 *tail = NULL;
3255 return res;
3258 /* Determine whether CS also brings all scalar values that the NODE is
3259 specialized for. */
3261 static bool
3262 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3263 struct cgraph_node *node)
3265 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3266 int count = ipa_get_param_count (dest_info);
3267 struct ipa_node_params *caller_info;
3268 struct ipa_edge_args *args;
3269 int i;
3271 caller_info = IPA_NODE_REF (cs->caller);
3272 args = IPA_EDGE_REF (cs);
3273 for (i = 0; i < count; i++)
3275 struct ipa_jump_func *jump_func;
3276 tree val, t;
3278 val = dest_info->known_vals[i];
3279 if (!val)
3280 continue;
3282 if (i >= ipa_get_cs_argument_count (args))
3283 return false;
3284 jump_func = ipa_get_ith_jump_func (args, i);
3285 t = ipa_value_from_jfunc (caller_info, jump_func);
3286 if (!t || !values_equal_for_ipcp_p (val, t))
3287 return false;
3289 return true;
3292 /* Determine whether CS also brings all aggregate values that NODE is
3293 specialized for. */
3294 static bool
3295 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3296 struct cgraph_node *node)
3298 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3299 struct ipa_node_params *orig_node_info;
3300 struct ipa_agg_replacement_value *aggval;
3301 int i, ec, count;
3303 aggval = ipa_get_agg_replacements_for_node (node);
3304 if (!aggval)
3305 return true;
3307 count = ipa_get_param_count (IPA_NODE_REF (node));
3308 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3309 if (ec < count)
3310 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3311 if (aggval->index >= ec)
3312 return false;
3314 orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node);
3315 if (orig_caller_info->ipcp_orig_node)
3316 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3318 for (i = 0; i < count; i++)
3320 static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>();
3321 struct ipcp_param_lattices *plats;
3322 bool interesting = false;
3323 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3324 if (aggval->index == i)
3326 interesting = true;
3327 break;
3329 if (!interesting)
3330 continue;
3332 plats = ipa_get_parm_lattices (orig_node_info, aggval->index);
3333 if (plats->aggs_bottom)
3334 return false;
3336 values = intersect_aggregates_with_edge (cs, i, values);
3337 if (!values.exists ())
3338 return false;
3340 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3341 if (aggval->index == i)
3343 struct ipa_agg_jf_item *item;
3344 int j;
3345 bool found = false;
3346 FOR_EACH_VEC_ELT (values, j, item)
3347 if (item->value
3348 && item->offset == av->offset
3349 && values_equal_for_ipcp_p (item->value, av->value))
3351 found = true;
3352 break;
3354 if (!found)
3356 values.release ();
3357 return false;
3361 return true;
3364 /* Given an original NODE and a VAL for which we have already created a
3365 specialized clone, look whether there are incoming edges that still lead
3366 into the old node but now also bring the requested value and also conform to
3367 all other criteria such that they can be redirected the the special node.
3368 This function can therefore redirect the final edge in a SCC. */
3370 static void
3371 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
3373 struct ipcp_value_source *src;
3374 gcov_type redirected_sum = 0;
3376 for (src = val->sources; src; src = src->next)
3378 struct cgraph_edge *cs = src->cs;
3379 while (cs)
3381 enum availability availability;
3382 struct cgraph_node *dst = cs->callee->function_symbol (&availability);
3383 if ((dst == node || IPA_NODE_REF (dst)->is_all_contexts_clone)
3384 && availability > AVAIL_INTERPOSABLE
3385 && cgraph_edge_brings_value_p (cs, src))
3387 if (cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3388 && cgraph_edge_brings_all_agg_vals_for_node (cs,
3389 val->spec_node))
3391 if (dump_file)
3392 fprintf (dump_file, " - adding an extra caller %s/%i"
3393 " of %s/%i\n",
3394 xstrdup (cs->caller->name ()),
3395 cs->caller->order,
3396 xstrdup (val->spec_node->name ()),
3397 val->spec_node->order);
3399 cs->redirect_callee (val->spec_node);
3400 redirected_sum += cs->count;
3403 cs = get_next_cgraph_edge_clone (cs);
3407 if (redirected_sum)
3408 update_specialized_profile (val->spec_node, node, redirected_sum);
3412 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
3414 static void
3415 move_binfos_to_values (vec<tree> known_vals,
3416 vec<tree> known_binfos)
3418 tree t;
3419 int i;
3421 for (i = 0; known_binfos.iterate (i, &t); i++)
3422 if (t)
3423 known_vals[i] = t;
3426 /* Return true if there is a replacement equivalent to VALUE, INDEX and OFFSET
3427 among those in the AGGVALS list. */
3429 DEBUG_FUNCTION bool
3430 ipcp_val_in_agg_replacements_p (struct ipa_agg_replacement_value *aggvals,
3431 int index, HOST_WIDE_INT offset, tree value)
3433 while (aggvals)
3435 if (aggvals->index == index
3436 && aggvals->offset == offset
3437 && values_equal_for_ipcp_p (aggvals->value, value))
3438 return true;
3439 aggvals = aggvals->next;
3441 return false;
3444 /* Decide wheter to create a special version of NODE for value VAL of parameter
3445 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
3446 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
3447 KNOWN_BINFOS and KNOWN_AGGS describe the other already known values. */
3449 static bool
3450 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
3451 struct ipcp_value *val, vec<tree> known_csts,
3452 vec<tree> known_binfos)
3454 struct ipa_agg_replacement_value *aggvals;
3455 int freq_sum, caller_count;
3456 gcov_type count_sum;
3457 vec<cgraph_edge *> callers;
3458 vec<tree> kv;
3460 if (val->spec_node)
3462 perhaps_add_new_callers (node, val);
3463 return false;
3465 else if (val->local_size_cost + overall_size > max_new_size)
3467 if (dump_file && (dump_flags & TDF_DETAILS))
3468 fprintf (dump_file, " Ignoring candidate value because "
3469 "max_new_size would be reached with %li.\n",
3470 val->local_size_cost + overall_size);
3471 return false;
3473 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
3474 &caller_count))
3475 return false;
3477 if (dump_file && (dump_flags & TDF_DETAILS))
3479 fprintf (dump_file, " - considering value ");
3480 print_ipcp_constant_value (dump_file, val->value);
3481 fprintf (dump_file, " for ");
3482 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
3483 if (offset != -1)
3484 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
3485 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
3488 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
3489 freq_sum, count_sum,
3490 val->local_size_cost)
3491 && !good_cloning_opportunity_p (node,
3492 val->local_time_benefit
3493 + val->prop_time_benefit,
3494 freq_sum, count_sum,
3495 val->local_size_cost
3496 + val->prop_size_cost))
3497 return false;
3499 if (dump_file)
3500 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
3501 node->name (), node->order);
3503 callers = gather_edges_for_value (val, caller_count);
3504 kv = known_csts.copy ();
3505 move_binfos_to_values (kv, known_binfos);
3506 if (offset == -1)
3507 kv[index] = val->value;
3508 find_more_scalar_values_for_callers_subset (node, kv, callers);
3509 aggvals = find_aggregate_values_for_callers_subset (node, callers);
3510 gcc_checking_assert (offset == -1
3511 || ipcp_val_in_agg_replacements_p (aggvals, index,
3512 offset, val->value));
3513 val->spec_node = create_specialized_node (node, kv, aggvals, callers);
3514 overall_size += val->local_size_cost;
3516 /* TODO: If for some lattice there is only one other known value
3517 left, make a special node for it too. */
3519 return true;
3522 /* Decide whether and what specialized clones of NODE should be created. */
3524 static bool
3525 decide_whether_version_node (struct cgraph_node *node)
3527 struct ipa_node_params *info = IPA_NODE_REF (node);
3528 int i, count = ipa_get_param_count (info);
3529 vec<tree> known_csts, known_binfos;
3530 vec<ipa_agg_jump_function> known_aggs = vNULL;
3531 bool ret = false;
3533 if (count == 0)
3534 return false;
3536 if (dump_file && (dump_flags & TDF_DETAILS))
3537 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
3538 node->name (), node->order);
3540 gather_context_independent_values (info, &known_csts, &known_binfos,
3541 info->do_clone_for_all_contexts ? &known_aggs
3542 : NULL, NULL);
3544 for (i = 0; i < count ;i++)
3546 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3547 struct ipcp_lattice *lat = &plats->itself;
3548 struct ipcp_value *val;
3550 if (!lat->bottom
3551 && !known_csts[i]
3552 && !known_binfos[i])
3553 for (val = lat->values; val; val = val->next)
3554 ret |= decide_about_value (node, i, -1, val, known_csts,
3555 known_binfos);
3557 if (!plats->aggs_bottom)
3559 struct ipcp_agg_lattice *aglat;
3560 struct ipcp_value *val;
3561 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3562 if (!aglat->bottom && aglat->values
3563 /* If the following is false, the one value is in
3564 known_aggs. */
3565 && (plats->aggs_contain_variable
3566 || !ipa_lat_is_single_const (aglat)))
3567 for (val = aglat->values; val; val = val->next)
3568 ret |= decide_about_value (node, i, aglat->offset, val,
3569 known_csts, known_binfos);
3571 info = IPA_NODE_REF (node);
3574 if (info->do_clone_for_all_contexts)
3576 struct cgraph_node *clone;
3577 vec<cgraph_edge *> callers;
3579 if (dump_file)
3580 fprintf (dump_file, " - Creating a specialized node of %s/%i "
3581 "for all known contexts.\n", node->name (),
3582 node->order);
3584 callers = node->collect_callers ();
3585 move_binfos_to_values (known_csts, known_binfos);
3586 clone = create_specialized_node (node, known_csts,
3587 known_aggs_to_agg_replacement_list (known_aggs),
3588 callers);
3589 info = IPA_NODE_REF (node);
3590 info->do_clone_for_all_contexts = false;
3591 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
3592 for (i = 0; i < count ; i++)
3593 vec_free (known_aggs[i].items);
3594 known_aggs.release ();
3595 ret = true;
3597 else
3598 known_csts.release ();
3600 known_binfos.release ();
3601 return ret;
3604 /* Transitively mark all callees of NODE within the same SCC as not dead. */
3606 static void
3607 spread_undeadness (struct cgraph_node *node)
3609 struct cgraph_edge *cs;
3611 for (cs = node->callees; cs; cs = cs->next_callee)
3612 if (ipa_edge_within_scc (cs))
3614 struct cgraph_node *callee;
3615 struct ipa_node_params *info;
3617 callee = cs->callee->function_symbol (NULL);
3618 info = IPA_NODE_REF (callee);
3620 if (info->node_dead)
3622 info->node_dead = 0;
3623 spread_undeadness (callee);
3628 /* Return true if NODE has a caller from outside of its SCC that is not
3629 dead. Worker callback for cgraph_for_node_and_aliases. */
3631 static bool
3632 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
3633 void *data ATTRIBUTE_UNUSED)
3635 struct cgraph_edge *cs;
3637 for (cs = node->callers; cs; cs = cs->next_caller)
3638 if (cs->caller->thunk.thunk_p
3639 && cs->caller->call_for_symbol_thunks_and_aliases
3640 (has_undead_caller_from_outside_scc_p, NULL, true))
3641 return true;
3642 else if (!ipa_edge_within_scc (cs)
3643 && !IPA_NODE_REF (cs->caller)->node_dead)
3644 return true;
3645 return false;
3649 /* Identify nodes within the same SCC as NODE which are no longer needed
3650 because of new clones and will be removed as unreachable. */
3652 static void
3653 identify_dead_nodes (struct cgraph_node *node)
3655 struct cgraph_node *v;
3656 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3657 if (v->will_be_removed_from_program_if_no_direct_calls_p ()
3658 && !v->call_for_symbol_thunks_and_aliases
3659 (has_undead_caller_from_outside_scc_p, NULL, true))
3660 IPA_NODE_REF (v)->node_dead = 1;
3662 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3663 if (!IPA_NODE_REF (v)->node_dead)
3664 spread_undeadness (v);
3666 if (dump_file && (dump_flags & TDF_DETAILS))
3668 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3669 if (IPA_NODE_REF (v)->node_dead)
3670 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
3671 v->name (), v->order);
3675 /* The decision stage. Iterate over the topological order of call graph nodes
3676 TOPO and make specialized clones if deemed beneficial. */
3678 static void
3679 ipcp_decision_stage (struct ipa_topo_info *topo)
3681 int i;
3683 if (dump_file)
3684 fprintf (dump_file, "\nIPA decision stage:\n\n");
3686 for (i = topo->nnodes - 1; i >= 0; i--)
3688 struct cgraph_node *node = topo->order[i];
3689 bool change = false, iterate = true;
3691 while (iterate)
3693 struct cgraph_node *v;
3694 iterate = false;
3695 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
3696 if (v->has_gimple_body_p ()
3697 && ipcp_versionable_function_p (v))
3698 iterate |= decide_whether_version_node (v);
3700 change |= iterate;
3702 if (change)
3703 identify_dead_nodes (node);
3707 /* The IPCP driver. */
3709 static unsigned int
3710 ipcp_driver (void)
3712 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
3713 struct cgraph_edge_hook_list *edge_removal_hook_holder;
3714 struct ipa_topo_info topo;
3716 ipa_check_create_node_params ();
3717 ipa_check_create_edge_args ();
3718 grow_edge_clone_vectors ();
3719 edge_duplication_hook_holder =
3720 symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
3721 edge_removal_hook_holder =
3722 symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL);
3724 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
3725 sizeof (struct ipcp_value), 32);
3726 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
3727 sizeof (struct ipcp_value_source), 64);
3728 ipcp_agg_lattice_pool = create_alloc_pool ("IPA_CP aggregate lattices",
3729 sizeof (struct ipcp_agg_lattice),
3730 32);
3731 if (dump_file)
3733 fprintf (dump_file, "\nIPA structures before propagation:\n");
3734 if (dump_flags & TDF_DETAILS)
3735 ipa_print_all_params (dump_file);
3736 ipa_print_all_jump_functions (dump_file);
3739 /* Topological sort. */
3740 build_toporder_info (&topo);
3741 /* Do the interprocedural propagation. */
3742 ipcp_propagate_stage (&topo);
3743 /* Decide what constant propagation and cloning should be performed. */
3744 ipcp_decision_stage (&topo);
3746 /* Free all IPCP structures. */
3747 free_toporder_info (&topo);
3748 next_edge_clone.release ();
3749 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
3750 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
3751 ipa_free_all_structures_after_ipa_cp ();
3752 if (dump_file)
3753 fprintf (dump_file, "\nIPA constant propagation end\n");
3754 return 0;
3757 /* Initialization and computation of IPCP data structures. This is the initial
3758 intraprocedural analysis of functions, which gathers information to be
3759 propagated later on. */
3761 static void
3762 ipcp_generate_summary (void)
3764 struct cgraph_node *node;
3766 if (dump_file)
3767 fprintf (dump_file, "\nIPA constant propagation start:\n");
3768 ipa_register_cgraph_hooks ();
3770 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
3772 node->local.versionable
3773 = tree_versionable_function_p (node->decl);
3774 ipa_analyze_node (node);
3778 /* Write ipcp summary for nodes in SET. */
3780 static void
3781 ipcp_write_summary (void)
3783 ipa_prop_write_jump_functions ();
3786 /* Read ipcp summary. */
3788 static void
3789 ipcp_read_summary (void)
3791 ipa_prop_read_jump_functions ();
3794 namespace {
3796 const pass_data pass_data_ipa_cp =
3798 IPA_PASS, /* type */
3799 "cp", /* name */
3800 OPTGROUP_NONE, /* optinfo_flags */
3801 TV_IPA_CONSTANT_PROP, /* tv_id */
3802 0, /* properties_required */
3803 0, /* properties_provided */
3804 0, /* properties_destroyed */
3805 0, /* todo_flags_start */
3806 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
3809 class pass_ipa_cp : public ipa_opt_pass_d
3811 public:
3812 pass_ipa_cp (gcc::context *ctxt)
3813 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
3814 ipcp_generate_summary, /* generate_summary */
3815 ipcp_write_summary, /* write_summary */
3816 ipcp_read_summary, /* read_summary */
3817 ipa_prop_write_all_agg_replacement, /*
3818 write_optimization_summary */
3819 ipa_prop_read_all_agg_replacement, /*
3820 read_optimization_summary */
3821 NULL, /* stmt_fixup */
3822 0, /* function_transform_todo_flags_start */
3823 ipcp_transform_function, /* function_transform */
3824 NULL) /* variable_transform */
3827 /* opt_pass methods: */
3828 virtual bool gate (function *)
3830 /* FIXME: We should remove the optimize check after we ensure we never run
3831 IPA passes when not optimizing. */
3832 return flag_ipa_cp && optimize;
3835 virtual unsigned int execute (function *) { return ipcp_driver (); }
3837 }; // class pass_ipa_cp
3839 } // anon namespace
3841 ipa_opt_pass_d *
3842 make_pass_ipa_cp (gcc::context *ctxt)
3844 return new pass_ipa_cp (ctxt);
3847 /* Reset all state within ipa-cp.c so that we can rerun the compiler
3848 within the same process. For use by toplev::finalize. */
3850 void
3851 ipa_cp_c_finalize (void)
3853 max_count = 0;
3854 overall_size = 0;
3855 max_new_size = 0;
3856 values_topo = NULL;