Remove unused variable and field
[official-gcc.git] / gcc / ipa-cp.c
bloba52bf7f5524cea70767dad659af4231836489c4f
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2013 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 "target.h"
108 #include "gimple.h"
109 #include "cgraph.h"
110 #include "ipa-prop.h"
111 #include "tree-flow.h"
112 #include "tree-pass.h"
113 #include "flags.h"
114 #include "diagnostic.h"
115 #include "tree-pretty-print.h"
116 #include "tree-inline.h"
117 #include "params.h"
118 #include "ipa-inline.h"
119 #include "ipa-utils.h"
121 struct ipcp_value;
123 /* Describes a particular source for an IPA-CP value. */
125 struct ipcp_value_source
127 /* Aggregate offset of the source, negative if the source is scalar value of
128 the argument itself. */
129 HOST_WIDE_INT offset;
130 /* The incoming edge that brought the value. */
131 struct cgraph_edge *cs;
132 /* If the jump function that resulted into his value was a pass-through or an
133 ancestor, this is the ipcp_value of the caller from which the described
134 value has been derived. Otherwise it is NULL. */
135 struct ipcp_value *val;
136 /* Next pointer in a linked list of sources of a value. */
137 struct ipcp_value_source *next;
138 /* If the jump function that resulted into his value was a pass-through or an
139 ancestor, this is the index of the parameter of the caller the jump
140 function references. */
141 int index;
144 /* Describes one particular value stored in struct ipcp_lattice. */
146 struct ipcp_value
148 /* The actual value for the given parameter. This is either an IPA invariant
149 or a TREE_BINFO describing a type that can be used for
150 devirtualization. */
151 tree value;
152 /* The list of sources from which this value originates. */
153 struct ipcp_value_source *sources;
154 /* Next pointers in a linked list of all values in a lattice. */
155 struct ipcp_value *next;
156 /* Next pointers in a linked list of values in a strongly connected component
157 of values. */
158 struct ipcp_value *scc_next;
159 /* Next pointers in a linked list of SCCs of values sorted topologically
160 according their sources. */
161 struct ipcp_value *topo_next;
162 /* A specialized node created for this value, NULL if none has been (so far)
163 created. */
164 struct cgraph_node *spec_node;
165 /* Depth first search number and low link for topological sorting of
166 values. */
167 int dfs, low_link;
168 /* Time benefit and size cost that specializing the function for this value
169 would bring about in this function alone. */
170 int local_time_benefit, local_size_cost;
171 /* Time benefit and size cost that specializing the function for this value
172 can bring about in it's callees (transitively). */
173 int prop_time_benefit, prop_size_cost;
174 /* True if this valye is currently on the topo-sort stack. */
175 bool on_stack;
178 /* Lattice describing potential values of a formal parameter of a function, or
179 a part of an aggreagate. TOP is represented by a lattice with zero values
180 and with contains_variable and bottom flags cleared. BOTTOM is represented
181 by a lattice with the bottom flag set. In that case, values and
182 contains_variable flag should be disregarded. */
184 struct ipcp_lattice
186 /* The list of known values and types in this lattice. Note that values are
187 not deallocated if a lattice is set to bottom because there may be value
188 sources referencing them. */
189 struct ipcp_value *values;
190 /* Number of known values and types in this lattice. */
191 int values_count;
192 /* The lattice contains a variable component (in addition to values). */
193 bool contains_variable;
194 /* The value of the lattice is bottom (i.e. variable and unusable for any
195 propagation). */
196 bool bottom;
199 /* Lattice with an offset to describe a part of an aggregate. */
201 struct ipcp_agg_lattice : public ipcp_lattice
203 /* Offset that is being described by this lattice. */
204 HOST_WIDE_INT offset;
205 /* Size so that we don't have to re-compute it every time we traverse the
206 list. Must correspond to TYPE_SIZE of all lat values. */
207 HOST_WIDE_INT size;
208 /* Next element of the linked list. */
209 struct ipcp_agg_lattice *next;
212 /* Structure containing lattices for a parameter itself and for pieces of
213 aggregates that are passed in the parameter or by a reference in a parameter
214 plus some other useful flags. */
216 struct ipcp_param_lattices
218 /* Lattice describing the value of the parameter itself. */
219 struct ipcp_lattice itself;
220 /* Lattices describing aggregate parts. */
221 struct ipcp_agg_lattice *aggs;
222 /* Number of aggregate lattices */
223 int aggs_count;
224 /* True if aggregate data were passed by reference (as opposed to by
225 value). */
226 bool aggs_by_ref;
227 /* All aggregate lattices contain a variable component (in addition to
228 values). */
229 bool aggs_contain_variable;
230 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
231 for any propagation). */
232 bool aggs_bottom;
234 /* There is a virtual call based on this parameter. */
235 bool virt_call;
238 /* Allocation pools for values and their sources in ipa-cp. */
240 alloc_pool ipcp_values_pool;
241 alloc_pool ipcp_sources_pool;
242 alloc_pool ipcp_agg_lattice_pool;
244 /* Maximal count found in program. */
246 static gcov_type max_count;
248 /* Original overall size of the program. */
250 static long overall_size, max_new_size;
252 /* Head of the linked list of topologically sorted values. */
254 static struct ipcp_value *values_topo;
256 /* Return the param lattices structure corresponding to the Ith formal
257 parameter of the function described by INFO. */
258 static inline struct ipcp_param_lattices *
259 ipa_get_parm_lattices (struct ipa_node_params *info, int i)
261 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
262 gcc_checking_assert (!info->ipcp_orig_node);
263 gcc_checking_assert (info->lattices);
264 return &(info->lattices[i]);
267 /* Return the lattice corresponding to the scalar value of the Ith formal
268 parameter of the function described by INFO. */
269 static inline struct ipcp_lattice *
270 ipa_get_scalar_lat (struct ipa_node_params *info, int i)
272 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
273 return &plats->itself;
276 /* Return whether LAT is a lattice with a single constant and without an
277 undefined value. */
279 static inline bool
280 ipa_lat_is_single_const (struct ipcp_lattice *lat)
282 if (lat->bottom
283 || lat->contains_variable
284 || lat->values_count != 1)
285 return false;
286 else
287 return true;
290 /* Return true iff the CS is an edge within a strongly connected component as
291 computed by ipa_reduced_postorder. */
293 static inline bool
294 edge_within_scc (struct cgraph_edge *cs)
296 struct ipa_dfs_info *caller_dfs = (struct ipa_dfs_info *) cs->caller->symbol.aux;
297 struct ipa_dfs_info *callee_dfs;
298 struct cgraph_node *callee = cgraph_function_node (cs->callee, NULL);
300 callee_dfs = (struct ipa_dfs_info *) callee->symbol.aux;
301 return (caller_dfs
302 && callee_dfs
303 && caller_dfs->scc_no == callee_dfs->scc_no);
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->symbol.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", cgraph_node_name (node),
402 node->symbol.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->symbol.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 (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
448 reason = "insufficient body availability";
450 if (reason && dump_file && !node->symbol.alias && !node->thunk.thunk_p)
451 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
452 cgraph_node_name (node), node->symbol.order, reason);
454 node->local.versionable = (reason == NULL);
457 /* Return true if it is at all technically possible to create clones of a
458 NODE. */
460 static bool
461 ipcp_versionable_function_p (struct cgraph_node *node)
463 return node->local.versionable;
466 /* Structure holding accumulated information about callers of a node. */
468 struct caller_statistics
470 gcov_type count_sum;
471 int n_calls, n_hot_calls, freq_sum;
474 /* Initialize fields of STAT to zeroes. */
476 static inline void
477 init_caller_stats (struct caller_statistics *stats)
479 stats->count_sum = 0;
480 stats->n_calls = 0;
481 stats->n_hot_calls = 0;
482 stats->freq_sum = 0;
485 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
486 non-thunk incoming edges to NODE. */
488 static bool
489 gather_caller_stats (struct cgraph_node *node, void *data)
491 struct caller_statistics *stats = (struct caller_statistics *) data;
492 struct cgraph_edge *cs;
494 for (cs = node->callers; cs; cs = cs->next_caller)
495 if (cs->caller->thunk.thunk_p)
496 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
497 stats, false);
498 else
500 stats->count_sum += cs->count;
501 stats->freq_sum += cs->frequency;
502 stats->n_calls++;
503 if (cgraph_maybe_hot_edge_p (cs))
504 stats->n_hot_calls ++;
506 return false;
510 /* Return true if this NODE is viable candidate for cloning. */
512 static bool
513 ipcp_cloning_candidate_p (struct cgraph_node *node)
515 struct caller_statistics stats;
517 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
519 if (!flag_ipa_cp_clone)
521 if (dump_file)
522 fprintf (dump_file, "Not considering %s for cloning; "
523 "-fipa-cp-clone disabled.\n",
524 cgraph_node_name (node));
525 return false;
528 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->symbol.decl)))
530 if (dump_file)
531 fprintf (dump_file, "Not considering %s for cloning; "
532 "optimizing it for size.\n",
533 cgraph_node_name (node));
534 return false;
537 init_caller_stats (&stats);
538 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
540 if (inline_summary (node)->self_size < stats.n_calls)
542 if (dump_file)
543 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
544 cgraph_node_name (node));
545 return true;
548 /* When profile is available and function is hot, propagate into it even if
549 calls seems cold; constant propagation can improve function's speed
550 significantly. */
551 if (max_count)
553 if (stats.count_sum > node->count * 90 / 100)
555 if (dump_file)
556 fprintf (dump_file, "Considering %s for cloning; "
557 "usually called directly.\n",
558 cgraph_node_name (node));
559 return true;
562 if (!stats.n_hot_calls)
564 if (dump_file)
565 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
566 cgraph_node_name (node));
567 return false;
569 if (dump_file)
570 fprintf (dump_file, "Considering %s for cloning.\n",
571 cgraph_node_name (node));
572 return true;
575 /* Arrays representing a topological ordering of call graph nodes and a stack
576 of noes used during constant propagation. */
578 struct topo_info
580 struct cgraph_node **order;
581 struct cgraph_node **stack;
582 int nnodes, stack_top;
585 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
587 static void
588 build_toporder_info (struct topo_info *topo)
590 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
591 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
592 topo->stack_top = 0;
593 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
596 /* Free information about strongly connected components and the arrays in
597 TOPO. */
599 static void
600 free_toporder_info (struct topo_info *topo)
602 ipa_free_postorder_info ();
603 free (topo->order);
604 free (topo->stack);
607 /* Add NODE to the stack in TOPO, unless it is already there. */
609 static inline void
610 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
612 struct ipa_node_params *info = IPA_NODE_REF (node);
613 if (info->node_enqueued)
614 return;
615 info->node_enqueued = 1;
616 topo->stack[topo->stack_top++] = node;
619 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
620 is empty. */
622 static struct cgraph_node *
623 pop_node_from_stack (struct topo_info *topo)
625 if (topo->stack_top)
627 struct cgraph_node *node;
628 topo->stack_top--;
629 node = topo->stack[topo->stack_top];
630 IPA_NODE_REF (node)->node_enqueued = 0;
631 return node;
633 else
634 return NULL;
637 /* Set lattice LAT to bottom and return true if it previously was not set as
638 such. */
640 static inline bool
641 set_lattice_to_bottom (struct ipcp_lattice *lat)
643 bool ret = !lat->bottom;
644 lat->bottom = true;
645 return ret;
648 /* Mark lattice as containing an unknown value and return true if it previously
649 was not marked as such. */
651 static inline bool
652 set_lattice_contains_variable (struct ipcp_lattice *lat)
654 bool ret = !lat->contains_variable;
655 lat->contains_variable = true;
656 return ret;
659 /* Set all aggegate lattices in PLATS to bottom and return true if they were
660 not previously set as such. */
662 static inline bool
663 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
665 bool ret = !plats->aggs_bottom;
666 plats->aggs_bottom = true;
667 return ret;
670 /* Mark all aggegate lattices in PLATS as containing an unknown value and
671 return true if they were not previously marked as such. */
673 static inline bool
674 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
676 bool ret = !plats->aggs_contain_variable;
677 plats->aggs_contain_variable = true;
678 return ret;
681 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
682 return true is any of them has not been marked as such so far. */
684 static inline bool
685 set_all_contains_variable (struct ipcp_param_lattices *plats)
687 bool ret = !plats->itself.contains_variable || !plats->aggs_contain_variable;
688 plats->itself.contains_variable = true;
689 plats->aggs_contain_variable = true;
690 return ret;
693 /* Initialize ipcp_lattices. */
695 static void
696 initialize_node_lattices (struct cgraph_node *node)
698 struct ipa_node_params *info = IPA_NODE_REF (node);
699 struct cgraph_edge *ie;
700 bool disable = false, variable = false;
701 int i;
703 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
704 if (!node->local.local)
706 /* When cloning is allowed, we can assume that externally visible
707 functions are not called. We will compensate this by cloning
708 later. */
709 if (ipcp_versionable_function_p (node)
710 && ipcp_cloning_candidate_p (node))
711 variable = true;
712 else
713 disable = true;
716 if (disable || variable)
718 for (i = 0; i < ipa_get_param_count (info) ; i++)
720 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
721 if (disable)
723 set_lattice_to_bottom (&plats->itself);
724 set_agg_lats_to_bottom (plats);
726 else
727 set_all_contains_variable (plats);
729 if (dump_file && (dump_flags & TDF_DETAILS)
730 && !node->symbol.alias && !node->thunk.thunk_p)
731 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
732 cgraph_node_name (node), node->symbol.order,
733 disable ? "BOTTOM" : "VARIABLE");
736 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
737 if (ie->indirect_info->polymorphic)
739 gcc_checking_assert (ie->indirect_info->param_index >= 0);
740 ipa_get_parm_lattices (info,
741 ie->indirect_info->param_index)->virt_call = 1;
745 /* Return the result of a (possibly arithmetic) pass through jump function
746 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
747 determined or itself is considered an interprocedural invariant. */
749 static tree
750 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
752 tree restype, res;
754 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
755 return input;
756 else if (TREE_CODE (input) == TREE_BINFO)
757 return NULL_TREE;
759 gcc_checking_assert (is_gimple_ip_invariant (input));
760 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
761 == tcc_comparison)
762 restype = boolean_type_node;
763 else
764 restype = TREE_TYPE (input);
765 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
766 input, ipa_get_jf_pass_through_operand (jfunc));
768 if (res && !is_gimple_ip_invariant (res))
769 return NULL_TREE;
771 return res;
774 /* Return the result of an ancestor jump function JFUNC on the constant value
775 INPUT. Return NULL_TREE if that cannot be determined. */
777 static tree
778 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
780 if (TREE_CODE (input) == TREE_BINFO)
781 return get_binfo_at_offset (input,
782 ipa_get_jf_ancestor_offset (jfunc),
783 ipa_get_jf_ancestor_type (jfunc));
784 else if (TREE_CODE (input) == ADDR_EXPR)
786 tree t = TREE_OPERAND (input, 0);
787 t = build_ref_for_offset (EXPR_LOCATION (t), t,
788 ipa_get_jf_ancestor_offset (jfunc),
789 ipa_get_jf_ancestor_type (jfunc), NULL, false);
790 return build_fold_addr_expr (t);
792 else
793 return NULL_TREE;
796 /* Determine whether JFUNC evaluates to a known value (that is either a
797 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
798 describes the caller node so that pass-through jump functions can be
799 evaluated. */
801 tree
802 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
804 if (jfunc->type == IPA_JF_CONST)
805 return ipa_get_jf_constant (jfunc);
806 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
807 return ipa_binfo_from_known_type_jfunc (jfunc);
808 else if (jfunc->type == IPA_JF_PASS_THROUGH
809 || jfunc->type == IPA_JF_ANCESTOR)
811 tree input;
812 int idx;
814 if (jfunc->type == IPA_JF_PASS_THROUGH)
815 idx = ipa_get_jf_pass_through_formal_id (jfunc);
816 else
817 idx = ipa_get_jf_ancestor_formal_id (jfunc);
819 if (info->ipcp_orig_node)
820 input = info->known_vals[idx];
821 else
823 struct ipcp_lattice *lat;
825 if (!info->lattices)
827 gcc_checking_assert (!flag_ipa_cp);
828 return NULL_TREE;
830 lat = ipa_get_scalar_lat (info, idx);
831 if (!ipa_lat_is_single_const (lat))
832 return NULL_TREE;
833 input = lat->values->value;
836 if (!input)
837 return NULL_TREE;
839 if (jfunc->type == IPA_JF_PASS_THROUGH)
840 return ipa_get_jf_pass_through_result (jfunc, input);
841 else
842 return ipa_get_jf_ancestor_result (jfunc, input);
844 else
845 return NULL_TREE;
849 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
850 bottom, not containing a variable component and without any known value at
851 the same time. */
853 DEBUG_FUNCTION void
854 ipcp_verify_propagated_values (void)
856 struct cgraph_node *node;
858 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
860 struct ipa_node_params *info = IPA_NODE_REF (node);
861 int i, count = ipa_get_param_count (info);
863 for (i = 0; i < count; i++)
865 struct ipcp_lattice *lat = ipa_get_scalar_lat (info, i);
867 if (!lat->bottom
868 && !lat->contains_variable
869 && lat->values_count == 0)
871 if (dump_file)
873 fprintf (dump_file, "\nIPA lattices after constant "
874 "propagation:\n");
875 print_all_lattices (dump_file, true, false);
878 gcc_unreachable ();
884 /* Return true iff X and Y should be considered equal values by IPA-CP. */
886 static bool
887 values_equal_for_ipcp_p (tree x, tree y)
889 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
891 if (x == y)
892 return true;
894 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
895 return false;
897 if (TREE_CODE (x) == ADDR_EXPR
898 && TREE_CODE (y) == ADDR_EXPR
899 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
900 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
901 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
902 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
903 else
904 return operand_equal_p (x, y, 0);
907 /* Add a new value source to VAL, marking that a value comes from edge CS and
908 (if the underlying jump function is a pass-through or an ancestor one) from
909 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. OFFSET
910 is negative if the source was the scalar value of the parameter itself or
911 the offset within an aggregate. */
913 static void
914 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
915 struct ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset)
917 struct ipcp_value_source *src;
919 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
920 src->offset = offset;
921 src->cs = cs;
922 src->val = src_val;
923 src->index = src_idx;
925 src->next = val->sources;
926 val->sources = src;
929 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
930 it. CS, SRC_VAL SRC_INDEX and OFFSET are meant for add_value_source and
931 have the same meaning. */
933 static bool
934 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
935 struct cgraph_edge *cs, struct ipcp_value *src_val,
936 int src_idx, HOST_WIDE_INT offset)
938 struct ipcp_value *val;
940 if (lat->bottom)
941 return false;
943 for (val = lat->values; val; val = val->next)
944 if (values_equal_for_ipcp_p (val->value, newval))
946 if (edge_within_scc (cs))
948 struct ipcp_value_source *s;
949 for (s = val->sources; s ; s = s->next)
950 if (s->cs == cs)
951 break;
952 if (s)
953 return false;
956 add_value_source (val, cs, src_val, src_idx, offset);
957 return false;
960 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
962 /* We can only free sources, not the values themselves, because sources
963 of other values in this this SCC might point to them. */
964 for (val = lat->values; val; val = val->next)
966 while (val->sources)
968 struct ipcp_value_source *src = val->sources;
969 val->sources = src->next;
970 pool_free (ipcp_sources_pool, src);
974 lat->values = NULL;
975 return set_lattice_to_bottom (lat);
978 lat->values_count++;
979 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
980 memset (val, 0, sizeof (*val));
982 add_value_source (val, cs, src_val, src_idx, offset);
983 val->value = newval;
984 val->next = lat->values;
985 lat->values = val;
986 return true;
989 /* Like above but passes a special value of offset to distinguish that the
990 origin is the scalar value of the parameter rather than a part of an
991 aggregate. */
993 static inline bool
994 add_scalar_value_to_lattice (struct ipcp_lattice *lat, tree newval,
995 struct cgraph_edge *cs,
996 struct ipcp_value *src_val, int src_idx)
998 return add_value_to_lattice (lat, newval, cs, src_val, src_idx, -1);
1001 /* Propagate values through a pass-through jump function JFUNC associated with
1002 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1003 is the index of the source parameter. */
1005 static bool
1006 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
1007 struct ipa_jump_func *jfunc,
1008 struct ipcp_lattice *src_lat,
1009 struct ipcp_lattice *dest_lat,
1010 int src_idx)
1012 struct ipcp_value *src_val;
1013 bool ret = false;
1015 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1016 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1017 ret |= add_scalar_value_to_lattice (dest_lat, src_val->value, cs,
1018 src_val, src_idx);
1019 /* Do not create new values when propagating within an SCC because if there
1020 are arithmetic functions with circular dependencies, there is infinite
1021 number of them and we would just make lattices bottom. */
1022 else if (edge_within_scc (cs))
1023 ret = set_lattice_contains_variable (dest_lat);
1024 else
1025 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1027 tree cstval = src_val->value;
1029 if (TREE_CODE (cstval) == TREE_BINFO)
1031 ret |= set_lattice_contains_variable (dest_lat);
1032 continue;
1034 cstval = ipa_get_jf_pass_through_result (jfunc, cstval);
1036 if (cstval)
1037 ret |= add_scalar_value_to_lattice (dest_lat, cstval, cs, src_val,
1038 src_idx);
1039 else
1040 ret |= set_lattice_contains_variable (dest_lat);
1043 return ret;
1046 /* Propagate values through an ancestor 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_ancestor (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 if (edge_within_scc (cs))
1061 return set_lattice_contains_variable (dest_lat);
1063 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1065 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1067 if (t)
1068 ret |= add_scalar_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
1069 else
1070 ret |= set_lattice_contains_variable (dest_lat);
1073 return ret;
1076 /* Propagate scalar values across jump function JFUNC that is associated with
1077 edge CS and put the values into DEST_LAT. */
1079 static bool
1080 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1081 struct ipa_jump_func *jfunc,
1082 struct ipcp_lattice *dest_lat)
1084 if (dest_lat->bottom)
1085 return false;
1087 if (jfunc->type == IPA_JF_CONST
1088 || jfunc->type == IPA_JF_KNOWN_TYPE)
1090 tree val;
1092 if (jfunc->type == IPA_JF_KNOWN_TYPE)
1094 val = ipa_binfo_from_known_type_jfunc (jfunc);
1095 if (!val)
1096 return set_lattice_contains_variable (dest_lat);
1098 else
1099 val = ipa_get_jf_constant (jfunc);
1100 return add_scalar_value_to_lattice (dest_lat, val, cs, NULL, 0);
1102 else if (jfunc->type == IPA_JF_PASS_THROUGH
1103 || jfunc->type == IPA_JF_ANCESTOR)
1105 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1106 struct ipcp_lattice *src_lat;
1107 int src_idx;
1108 bool ret;
1110 if (jfunc->type == IPA_JF_PASS_THROUGH)
1111 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1112 else
1113 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1115 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1116 if (src_lat->bottom)
1117 return set_lattice_contains_variable (dest_lat);
1119 /* If we would need to clone the caller and cannot, do not propagate. */
1120 if (!ipcp_versionable_function_p (cs->caller)
1121 && (src_lat->contains_variable
1122 || (src_lat->values_count > 1)))
1123 return set_lattice_contains_variable (dest_lat);
1125 if (jfunc->type == IPA_JF_PASS_THROUGH)
1126 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1127 dest_lat, src_idx);
1128 else
1129 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1130 src_idx);
1132 if (src_lat->contains_variable)
1133 ret |= set_lattice_contains_variable (dest_lat);
1135 return ret;
1138 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1139 use it for indirect inlining), we should propagate them too. */
1140 return set_lattice_contains_variable (dest_lat);
1143 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1144 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1145 other cases, return false). If there are no aggregate items, set
1146 aggs_by_ref to NEW_AGGS_BY_REF. */
1148 static bool
1149 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1150 bool new_aggs_by_ref)
1152 if (dest_plats->aggs)
1154 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1156 set_agg_lats_to_bottom (dest_plats);
1157 return true;
1160 else
1161 dest_plats->aggs_by_ref = new_aggs_by_ref;
1162 return false;
1165 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1166 already existing lattice for the given OFFSET and SIZE, marking all skipped
1167 lattices as containing variable and checking for overlaps. If there is no
1168 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1169 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1170 unless there are too many already. If there are two many, return false. If
1171 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1172 skipped lattices were newly marked as containing variable, set *CHANGE to
1173 true. */
1175 static bool
1176 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1177 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1178 struct ipcp_agg_lattice ***aglat,
1179 bool pre_existing, bool *change)
1181 gcc_checking_assert (offset >= 0);
1183 while (**aglat && (**aglat)->offset < offset)
1185 if ((**aglat)->offset + (**aglat)->size > offset)
1187 set_agg_lats_to_bottom (dest_plats);
1188 return false;
1190 *change |= set_lattice_contains_variable (**aglat);
1191 *aglat = &(**aglat)->next;
1194 if (**aglat && (**aglat)->offset == offset)
1196 if ((**aglat)->size != val_size
1197 || ((**aglat)->next
1198 && (**aglat)->next->offset < offset + val_size))
1200 set_agg_lats_to_bottom (dest_plats);
1201 return false;
1203 gcc_checking_assert (!(**aglat)->next
1204 || (**aglat)->next->offset >= offset + val_size);
1205 return true;
1207 else
1209 struct ipcp_agg_lattice *new_al;
1211 if (**aglat && (**aglat)->offset < offset + val_size)
1213 set_agg_lats_to_bottom (dest_plats);
1214 return false;
1216 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1217 return false;
1218 dest_plats->aggs_count++;
1219 new_al = (struct ipcp_agg_lattice *) pool_alloc (ipcp_agg_lattice_pool);
1220 memset (new_al, 0, sizeof (*new_al));
1222 new_al->offset = offset;
1223 new_al->size = val_size;
1224 new_al->contains_variable = pre_existing;
1226 new_al->next = **aglat;
1227 **aglat = new_al;
1228 return true;
1232 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1233 containing an unknown value. */
1235 static bool
1236 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1238 bool ret = false;
1239 while (aglat)
1241 ret |= set_lattice_contains_variable (aglat);
1242 aglat = aglat->next;
1244 return ret;
1247 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1248 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1249 parameter used for lattice value sources. Return true if DEST_PLATS changed
1250 in any way. */
1252 static bool
1253 merge_aggregate_lattices (struct cgraph_edge *cs,
1254 struct ipcp_param_lattices *dest_plats,
1255 struct ipcp_param_lattices *src_plats,
1256 int src_idx, HOST_WIDE_INT offset_delta)
1258 bool pre_existing = dest_plats->aggs != NULL;
1259 struct ipcp_agg_lattice **dst_aglat;
1260 bool ret = false;
1262 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1263 return true;
1264 if (src_plats->aggs_bottom)
1265 return set_agg_lats_contain_variable (dest_plats);
1266 if (src_plats->aggs_contain_variable)
1267 ret |= set_agg_lats_contain_variable (dest_plats);
1268 dst_aglat = &dest_plats->aggs;
1270 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1271 src_aglat;
1272 src_aglat = src_aglat->next)
1274 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1276 if (new_offset < 0)
1277 continue;
1278 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1279 &dst_aglat, pre_existing, &ret))
1281 struct ipcp_agg_lattice *new_al = *dst_aglat;
1283 dst_aglat = &(*dst_aglat)->next;
1284 if (src_aglat->bottom)
1286 ret |= set_lattice_contains_variable (new_al);
1287 continue;
1289 if (src_aglat->contains_variable)
1290 ret |= set_lattice_contains_variable (new_al);
1291 for (struct ipcp_value *val = src_aglat->values;
1292 val;
1293 val = val->next)
1294 ret |= add_value_to_lattice (new_al, val->value, cs, val, src_idx,
1295 src_aglat->offset);
1297 else if (dest_plats->aggs_bottom)
1298 return true;
1300 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1301 return ret;
1304 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1305 pass-through JFUNC and if so, whether it has conform and conforms to the
1306 rules about propagating values passed by reference. */
1308 static bool
1309 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1310 struct ipa_jump_func *jfunc)
1312 return src_plats->aggs
1313 && (!src_plats->aggs_by_ref
1314 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1317 /* Propagate scalar values across jump function JFUNC that is associated with
1318 edge CS and put the values into DEST_LAT. */
1320 static bool
1321 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1322 struct ipa_jump_func *jfunc,
1323 struct ipcp_param_lattices *dest_plats)
1325 bool ret = false;
1327 if (dest_plats->aggs_bottom)
1328 return false;
1330 if (jfunc->type == IPA_JF_PASS_THROUGH
1331 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1333 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1334 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1335 struct ipcp_param_lattices *src_plats;
1337 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1338 if (agg_pass_through_permissible_p (src_plats, jfunc))
1340 /* Currently we do not produce clobber aggregate jump
1341 functions, replace with merging when we do. */
1342 gcc_assert (!jfunc->agg.items);
1343 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1344 src_idx, 0);
1346 else
1347 ret |= set_agg_lats_contain_variable (dest_plats);
1349 else if (jfunc->type == IPA_JF_ANCESTOR
1350 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1352 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1353 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1354 struct ipcp_param_lattices *src_plats;
1356 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1357 if (src_plats->aggs && src_plats->aggs_by_ref)
1359 /* Currently we do not produce clobber aggregate jump
1360 functions, replace with merging when we do. */
1361 gcc_assert (!jfunc->agg.items);
1362 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1363 ipa_get_jf_ancestor_offset (jfunc));
1365 else if (!src_plats->aggs_by_ref)
1366 ret |= set_agg_lats_to_bottom (dest_plats);
1367 else
1368 ret |= set_agg_lats_contain_variable (dest_plats);
1370 else if (jfunc->agg.items)
1372 bool pre_existing = dest_plats->aggs != NULL;
1373 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1374 struct ipa_agg_jf_item *item;
1375 int i;
1377 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1378 return true;
1380 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1382 HOST_WIDE_INT val_size;
1384 if (item->offset < 0)
1385 continue;
1386 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1387 val_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (item->value)), 1);
1389 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1390 &aglat, pre_existing, &ret))
1392 ret |= add_value_to_lattice (*aglat, item->value, cs, NULL, 0, 0);
1393 aglat = &(*aglat)->next;
1395 else if (dest_plats->aggs_bottom)
1396 return true;
1399 ret |= set_chain_of_aglats_contains_variable (*aglat);
1401 else
1402 ret |= set_agg_lats_contain_variable (dest_plats);
1404 return ret;
1407 /* Propagate constants from the caller to the callee of CS. INFO describes the
1408 caller. */
1410 static bool
1411 propagate_constants_accross_call (struct cgraph_edge *cs)
1413 struct ipa_node_params *callee_info;
1414 enum availability availability;
1415 struct cgraph_node *callee, *alias_or_thunk;
1416 struct ipa_edge_args *args;
1417 bool ret = false;
1418 int i, args_count, parms_count;
1420 callee = cgraph_function_node (cs->callee, &availability);
1421 if (!callee->symbol.definition)
1422 return false;
1423 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1424 callee_info = IPA_NODE_REF (callee);
1426 args = IPA_EDGE_REF (cs);
1427 args_count = ipa_get_cs_argument_count (args);
1428 parms_count = ipa_get_param_count (callee_info);
1430 /* If this call goes through a thunk we must not propagate to the first (0th)
1431 parameter. However, we might need to uncover a thunk from below a series
1432 of aliases first. */
1433 alias_or_thunk = cs->callee;
1434 while (alias_or_thunk->symbol.alias)
1435 alias_or_thunk = cgraph_alias_target (alias_or_thunk);
1436 if (alias_or_thunk->thunk.thunk_p)
1438 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1439 0));
1440 i = 1;
1442 else
1443 i = 0;
1445 for (; (i < args_count) && (i < parms_count); i++)
1447 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1448 struct ipcp_param_lattices *dest_plats;
1450 dest_plats = ipa_get_parm_lattices (callee_info, i);
1451 if (availability == AVAIL_OVERWRITABLE)
1452 ret |= set_all_contains_variable (dest_plats);
1453 else
1455 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1456 &dest_plats->itself);
1457 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1458 dest_plats);
1461 for (; i < parms_count; i++)
1462 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1464 return ret;
1467 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1468 (which can contain both constants and binfos), KNOWN_BINFOS, KNOWN_AGGS or
1469 AGG_REPS return the destination. The latter three can be NULL. If AGG_REPS
1470 is not NULL, KNOWN_AGGS is ignored. */
1472 static tree
1473 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1474 vec<tree> known_vals,
1475 vec<tree> known_binfos,
1476 vec<ipa_agg_jump_function_p> known_aggs,
1477 struct ipa_agg_replacement_value *agg_reps)
1479 int param_index = ie->indirect_info->param_index;
1480 HOST_WIDE_INT token, anc_offset;
1481 tree otr_type;
1482 tree t;
1484 if (param_index == -1
1485 || known_vals.length () <= (unsigned int) param_index)
1486 return NULL_TREE;
1488 if (!ie->indirect_info->polymorphic)
1490 tree t;
1492 if (ie->indirect_info->agg_contents)
1494 if (agg_reps)
1496 t = NULL;
1497 while (agg_reps)
1499 if (agg_reps->index == param_index
1500 && agg_reps->offset == ie->indirect_info->offset
1501 && agg_reps->by_ref == ie->indirect_info->by_ref)
1503 t = agg_reps->value;
1504 break;
1506 agg_reps = agg_reps->next;
1509 else if (known_aggs.length () > (unsigned int) param_index)
1511 struct ipa_agg_jump_function *agg;
1512 agg = known_aggs[param_index];
1513 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1514 ie->indirect_info->by_ref);
1516 else
1517 t = NULL;
1519 else
1520 t = known_vals[param_index];
1522 if (t &&
1523 TREE_CODE (t) == ADDR_EXPR
1524 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1525 return TREE_OPERAND (t, 0);
1526 else
1527 return NULL_TREE;
1530 gcc_assert (!ie->indirect_info->agg_contents);
1531 token = ie->indirect_info->otr_token;
1532 anc_offset = ie->indirect_info->offset;
1533 otr_type = ie->indirect_info->otr_type;
1535 t = known_vals[param_index];
1536 if (!t && known_binfos.length () > (unsigned int) param_index)
1537 t = known_binfos[param_index];
1538 if (!t)
1539 return NULL_TREE;
1541 if (TREE_CODE (t) != TREE_BINFO)
1543 tree binfo;
1544 binfo = gimple_extract_devirt_binfo_from_cst (t);
1545 if (!binfo)
1546 return NULL_TREE;
1547 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1548 if (!binfo)
1549 return NULL_TREE;
1550 return gimple_get_virt_method_for_binfo (token, binfo);
1552 else
1554 tree binfo;
1556 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1557 if (!binfo)
1558 return NULL_TREE;
1559 return gimple_get_virt_method_for_binfo (token, binfo);
1564 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1565 (which can contain both constants and binfos), KNOWN_BINFOS (which can be
1566 NULL) or KNOWN_AGGS (which also can be NULL) return the destination. */
1568 tree
1569 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1570 vec<tree> known_vals,
1571 vec<tree> known_binfos,
1572 vec<ipa_agg_jump_function_p> known_aggs)
1574 return ipa_get_indirect_edge_target_1 (ie, known_vals, known_binfos,
1575 known_aggs, NULL);
1578 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1579 and KNOWN_BINFOS. */
1581 static int
1582 devirtualization_time_bonus (struct cgraph_node *node,
1583 vec<tree> known_csts,
1584 vec<tree> known_binfos,
1585 vec<ipa_agg_jump_function_p> known_aggs)
1587 struct cgraph_edge *ie;
1588 int res = 0;
1590 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1592 struct cgraph_node *callee;
1593 struct inline_summary *isummary;
1594 tree target;
1596 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos,
1597 known_aggs);
1598 if (!target)
1599 continue;
1601 /* Only bare minimum benefit for clearly un-inlineable targets. */
1602 res += 1;
1603 callee = cgraph_get_node (target);
1604 if (!callee || !callee->symbol.definition)
1605 continue;
1606 isummary = inline_summary (callee);
1607 if (!isummary->inlinable)
1608 continue;
1610 /* FIXME: The values below need re-considering and perhaps also
1611 integrating into the cost metrics, at lest in some very basic way. */
1612 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1613 res += 31;
1614 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1615 res += 15;
1616 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1617 || DECL_DECLARED_INLINE_P (callee->symbol.decl))
1618 res += 7;
1621 return res;
1624 /* Return time bonus incurred because of HINTS. */
1626 static int
1627 hint_time_bonus (inline_hints hints)
1629 int result = 0;
1630 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
1631 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
1632 if (hints & INLINE_HINT_array_index)
1633 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
1634 return result;
1637 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1638 and SIZE_COST and with the sum of frequencies of incoming edges to the
1639 potential new clone in FREQUENCIES. */
1641 static bool
1642 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1643 int freq_sum, gcov_type count_sum, int size_cost)
1645 if (time_benefit == 0
1646 || !flag_ipa_cp_clone
1647 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->symbol.decl)))
1648 return false;
1650 gcc_assert (size_cost > 0);
1652 if (max_count)
1654 int factor = (count_sum * 1000) / max_count;
1655 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * factor)
1656 / size_cost);
1658 if (dump_file && (dump_flags & TDF_DETAILS))
1659 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1660 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1661 ") -> evaluation: " HOST_WIDEST_INT_PRINT_DEC
1662 ", threshold: %i\n",
1663 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1664 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1666 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1668 else
1670 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * freq_sum)
1671 / size_cost);
1673 if (dump_file && (dump_flags & TDF_DETAILS))
1674 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1675 "size: %i, freq_sum: %i) -> evaluation: "
1676 HOST_WIDEST_INT_PRINT_DEC ", threshold: %i\n",
1677 time_benefit, size_cost, freq_sum, evaluation,
1678 PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1680 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1684 /* Return all context independent values from aggregate lattices in PLATS in a
1685 vector. Return NULL if there are none. */
1687 static vec<ipa_agg_jf_item_t, va_gc> *
1688 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
1690 vec<ipa_agg_jf_item_t, va_gc> *res = NULL;
1692 if (plats->aggs_bottom
1693 || plats->aggs_contain_variable
1694 || plats->aggs_count == 0)
1695 return NULL;
1697 for (struct ipcp_agg_lattice *aglat = plats->aggs;
1698 aglat;
1699 aglat = aglat->next)
1700 if (ipa_lat_is_single_const (aglat))
1702 struct ipa_agg_jf_item item;
1703 item.offset = aglat->offset;
1704 item.value = aglat->values->value;
1705 vec_safe_push (res, item);
1707 return res;
1710 /* Allocate KNOWN_CSTS, KNOWN_BINFOS and, if non-NULL, KNOWN_AGGS and populate
1711 them with values of parameters that are known independent of the context.
1712 INFO describes the function. If REMOVABLE_PARAMS_COST is non-NULL, the
1713 movement cost of all removable parameters will be stored in it. */
1715 static bool
1716 gather_context_independent_values (struct ipa_node_params *info,
1717 vec<tree> *known_csts,
1718 vec<tree> *known_binfos,
1719 vec<ipa_agg_jump_function_t> *known_aggs,
1720 int *removable_params_cost)
1722 int i, count = ipa_get_param_count (info);
1723 bool ret = false;
1725 known_csts->create (0);
1726 known_binfos->create (0);
1727 known_csts->safe_grow_cleared (count);
1728 known_binfos->safe_grow_cleared (count);
1729 if (known_aggs)
1731 known_aggs->create (0);
1732 known_aggs->safe_grow_cleared (count);
1735 if (removable_params_cost)
1736 *removable_params_cost = 0;
1738 for (i = 0; i < count ; i++)
1740 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1741 struct ipcp_lattice *lat = &plats->itself;
1743 if (ipa_lat_is_single_const (lat))
1745 struct ipcp_value *val = lat->values;
1746 if (TREE_CODE (val->value) != TREE_BINFO)
1748 (*known_csts)[i] = val->value;
1749 if (removable_params_cost)
1750 *removable_params_cost
1751 += estimate_move_cost (TREE_TYPE (val->value));
1752 ret = true;
1754 else if (plats->virt_call)
1756 (*known_binfos)[i] = val->value;
1757 ret = true;
1759 else if (removable_params_cost
1760 && !ipa_is_param_used (info, i))
1761 *removable_params_cost += ipa_get_param_move_cost (info, i);
1763 else if (removable_params_cost
1764 && !ipa_is_param_used (info, i))
1765 *removable_params_cost
1766 += ipa_get_param_move_cost (info, i);
1768 if (known_aggs)
1770 vec<ipa_agg_jf_item_t, va_gc> *agg_items;
1771 struct ipa_agg_jump_function *ajf;
1773 agg_items = context_independent_aggregate_values (plats);
1774 ajf = &(*known_aggs)[i];
1775 ajf->items = agg_items;
1776 ajf->by_ref = plats->aggs_by_ref;
1777 ret |= agg_items != NULL;
1781 return ret;
1784 /* The current interface in ipa-inline-analysis requires a pointer vector.
1785 Create it.
1787 FIXME: That interface should be re-worked, this is slightly silly. Still,
1788 I'd like to discuss how to change it first and this demonstrates the
1789 issue. */
1791 static vec<ipa_agg_jump_function_p>
1792 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function_t> known_aggs)
1794 vec<ipa_agg_jump_function_p> ret;
1795 struct ipa_agg_jump_function *ajf;
1796 int i;
1798 ret.create (known_aggs.length ());
1799 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
1800 ret.quick_push (ajf);
1801 return ret;
1804 /* Iterate over known values of parameters of NODE and estimate the local
1805 effects in terms of time and size they have. */
1807 static void
1808 estimate_local_effects (struct cgraph_node *node)
1810 struct ipa_node_params *info = IPA_NODE_REF (node);
1811 int i, count = ipa_get_param_count (info);
1812 vec<tree> known_csts, known_binfos;
1813 vec<ipa_agg_jump_function_t> known_aggs;
1814 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
1815 bool always_const;
1816 int base_time = inline_summary (node)->time;
1817 int removable_params_cost;
1819 if (!count || !ipcp_versionable_function_p (node))
1820 return;
1822 if (dump_file && (dump_flags & TDF_DETAILS))
1823 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1824 cgraph_node_name (node), node->symbol.order, base_time);
1826 always_const = gather_context_independent_values (info, &known_csts,
1827 &known_binfos, &known_aggs,
1828 &removable_params_cost);
1829 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
1830 if (always_const)
1832 struct caller_statistics stats;
1833 inline_hints hints;
1834 int time, size;
1836 init_caller_stats (&stats);
1837 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1838 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1839 known_aggs_ptrs, &size, &time, &hints);
1840 time -= devirtualization_time_bonus (node, known_csts, known_binfos,
1841 known_aggs_ptrs);
1842 time -= hint_time_bonus (hints);
1843 time -= removable_params_cost;
1844 size -= stats.n_calls * removable_params_cost;
1846 if (dump_file)
1847 fprintf (dump_file, " - context independent values, size: %i, "
1848 "time_benefit: %i\n", size, base_time - time);
1850 if (size <= 0
1851 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1853 info->do_clone_for_all_contexts = true;
1854 base_time = time;
1856 if (dump_file)
1857 fprintf (dump_file, " Decided to specialize for all "
1858 "known contexts, code not going to grow.\n");
1860 else if (good_cloning_opportunity_p (node, base_time - time,
1861 stats.freq_sum, stats.count_sum,
1862 size))
1864 if (size + overall_size <= max_new_size)
1866 info->do_clone_for_all_contexts = true;
1867 base_time = time;
1868 overall_size += size;
1870 if (dump_file)
1871 fprintf (dump_file, " Decided to specialize for all "
1872 "known contexts, growth deemed beneficial.\n");
1874 else if (dump_file && (dump_flags & TDF_DETAILS))
1875 fprintf (dump_file, " Not cloning for all contexts because "
1876 "max_new_size would be reached with %li.\n",
1877 size + overall_size);
1881 for (i = 0; i < count ; i++)
1883 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1884 struct ipcp_lattice *lat = &plats->itself;
1885 struct ipcp_value *val;
1886 int emc;
1888 if (lat->bottom
1889 || !lat->values
1890 || known_csts[i]
1891 || known_binfos[i])
1892 continue;
1894 for (val = lat->values; val; val = val->next)
1896 int time, size, time_benefit;
1897 inline_hints hints;
1899 if (TREE_CODE (val->value) != TREE_BINFO)
1901 known_csts[i] = val->value;
1902 known_binfos[i] = NULL_TREE;
1903 emc = estimate_move_cost (TREE_TYPE (val->value));
1905 else if (plats->virt_call)
1907 known_csts[i] = NULL_TREE;
1908 known_binfos[i] = val->value;
1909 emc = 0;
1911 else
1912 continue;
1914 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1915 known_aggs_ptrs, &size, &time,
1916 &hints);
1917 time_benefit = base_time - time
1918 + devirtualization_time_bonus (node, known_csts, known_binfos,
1919 known_aggs_ptrs)
1920 + hint_time_bonus (hints)
1921 + removable_params_cost + emc;
1923 gcc_checking_assert (size >=0);
1924 /* The inliner-heuristics based estimates may think that in certain
1925 contexts some functions do not have any size at all but we want
1926 all specializations to have at least a tiny cost, not least not to
1927 divide by zero. */
1928 if (size == 0)
1929 size = 1;
1931 if (dump_file && (dump_flags & TDF_DETAILS))
1933 fprintf (dump_file, " - estimates for value ");
1934 print_ipcp_constant_value (dump_file, val->value);
1935 fprintf (dump_file, " for ");
1936 ipa_dump_param (dump_file, info, i);
1937 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
1938 time_benefit, size);
1941 val->local_time_benefit = time_benefit;
1942 val->local_size_cost = size;
1944 known_binfos[i] = NULL_TREE;
1945 known_csts[i] = NULL_TREE;
1948 for (i = 0; i < count ; i++)
1950 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1951 struct ipa_agg_jump_function *ajf;
1952 struct ipcp_agg_lattice *aglat;
1954 if (plats->aggs_bottom || !plats->aggs)
1955 continue;
1957 ajf = &known_aggs[i];
1958 for (aglat = plats->aggs; aglat; aglat = aglat->next)
1960 struct ipcp_value *val;
1961 if (aglat->bottom || !aglat->values
1962 /* If the following is true, the one value is in known_aggs. */
1963 || (!plats->aggs_contain_variable
1964 && ipa_lat_is_single_const (aglat)))
1965 continue;
1967 for (val = aglat->values; val; val = val->next)
1969 int time, size, time_benefit;
1970 struct ipa_agg_jf_item item;
1971 inline_hints hints;
1973 item.offset = aglat->offset;
1974 item.value = val->value;
1975 vec_safe_push (ajf->items, item);
1977 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1978 known_aggs_ptrs, &size, &time,
1979 &hints);
1980 time_benefit = base_time - time
1981 + devirtualization_time_bonus (node, known_csts, known_binfos,
1982 known_aggs_ptrs)
1983 + hint_time_bonus (hints);
1984 gcc_checking_assert (size >=0);
1985 if (size == 0)
1986 size = 1;
1988 if (dump_file && (dump_flags & TDF_DETAILS))
1990 fprintf (dump_file, " - estimates for value ");
1991 print_ipcp_constant_value (dump_file, val->value);
1992 fprintf (dump_file, " for ");
1993 ipa_dump_param (dump_file, info, i);
1994 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
1995 "]: time_benefit: %i, size: %i\n",
1996 plats->aggs_by_ref ? "ref " : "",
1997 aglat->offset, time_benefit, size);
2000 val->local_time_benefit = time_benefit;
2001 val->local_size_cost = size;
2002 ajf->items->pop ();
2007 for (i = 0; i < count ; i++)
2008 vec_free (known_aggs[i].items);
2010 known_csts.release ();
2011 known_binfos.release ();
2012 known_aggs.release ();
2013 known_aggs_ptrs.release ();
2017 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2018 topological sort of values. */
2020 static void
2021 add_val_to_toposort (struct ipcp_value *cur_val)
2023 static int dfs_counter = 0;
2024 static struct ipcp_value *stack;
2025 struct ipcp_value_source *src;
2027 if (cur_val->dfs)
2028 return;
2030 dfs_counter++;
2031 cur_val->dfs = dfs_counter;
2032 cur_val->low_link = dfs_counter;
2034 cur_val->topo_next = stack;
2035 stack = cur_val;
2036 cur_val->on_stack = true;
2038 for (src = cur_val->sources; src; src = src->next)
2039 if (src->val)
2041 if (src->val->dfs == 0)
2043 add_val_to_toposort (src->val);
2044 if (src->val->low_link < cur_val->low_link)
2045 cur_val->low_link = src->val->low_link;
2047 else if (src->val->on_stack
2048 && src->val->dfs < cur_val->low_link)
2049 cur_val->low_link = src->val->dfs;
2052 if (cur_val->dfs == cur_val->low_link)
2054 struct ipcp_value *v, *scc_list = NULL;
2058 v = stack;
2059 stack = v->topo_next;
2060 v->on_stack = false;
2062 v->scc_next = scc_list;
2063 scc_list = v;
2065 while (v != cur_val);
2067 cur_val->topo_next = values_topo;
2068 values_topo = cur_val;
2072 /* Add all values in lattices associated with NODE to the topological sort if
2073 they are not there yet. */
2075 static void
2076 add_all_node_vals_to_toposort (struct cgraph_node *node)
2078 struct ipa_node_params *info = IPA_NODE_REF (node);
2079 int i, count = ipa_get_param_count (info);
2081 for (i = 0; i < count ; i++)
2083 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2084 struct ipcp_lattice *lat = &plats->itself;
2085 struct ipcp_agg_lattice *aglat;
2086 struct ipcp_value *val;
2088 if (!lat->bottom)
2089 for (val = lat->values; val; val = val->next)
2090 add_val_to_toposort (val);
2092 if (!plats->aggs_bottom)
2093 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2094 if (!aglat->bottom)
2095 for (val = aglat->values; val; val = val->next)
2096 add_val_to_toposort (val);
2100 /* One pass of constants propagation along the call graph edges, from callers
2101 to callees (requires topological ordering in TOPO), iterate over strongly
2102 connected components. */
2104 static void
2105 propagate_constants_topo (struct topo_info *topo)
2107 int i;
2109 for (i = topo->nnodes - 1; i >= 0; i--)
2111 struct cgraph_node *v, *node = topo->order[i];
2112 struct ipa_dfs_info *node_dfs_info;
2114 if (!cgraph_function_with_gimple_body_p (node))
2115 continue;
2117 node_dfs_info = (struct ipa_dfs_info *) node->symbol.aux;
2118 /* First, iteratively propagate within the strongly connected component
2119 until all lattices stabilize. */
2120 v = node_dfs_info->next_cycle;
2121 while (v)
2123 push_node_to_stack (topo, v);
2124 v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle;
2127 v = node;
2128 while (v)
2130 struct cgraph_edge *cs;
2132 for (cs = v->callees; cs; cs = cs->next_callee)
2133 if (edge_within_scc (cs)
2134 && propagate_constants_accross_call (cs))
2135 push_node_to_stack (topo, cs->callee);
2136 v = pop_node_from_stack (topo);
2139 /* Afterwards, propagate along edges leading out of the SCC, calculates
2140 the local effects of the discovered constants and all valid values to
2141 their topological sort. */
2142 v = node;
2143 while (v)
2145 struct cgraph_edge *cs;
2147 estimate_local_effects (v);
2148 add_all_node_vals_to_toposort (v);
2149 for (cs = v->callees; cs; cs = cs->next_callee)
2150 if (!edge_within_scc (cs))
2151 propagate_constants_accross_call (cs);
2153 v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle;
2159 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2160 the bigger one if otherwise. */
2162 static int
2163 safe_add (int a, int b)
2165 if (a > INT_MAX/2 || b > INT_MAX/2)
2166 return a > b ? a : b;
2167 else
2168 return a + b;
2172 /* Propagate the estimated effects of individual values along the topological
2173 from the dependent values to those they depend on. */
2175 static void
2176 propagate_effects (void)
2178 struct ipcp_value *base;
2180 for (base = values_topo; base; base = base->topo_next)
2182 struct ipcp_value_source *src;
2183 struct ipcp_value *val;
2184 int time = 0, size = 0;
2186 for (val = base; val; val = val->scc_next)
2188 time = safe_add (time,
2189 val->local_time_benefit + val->prop_time_benefit);
2190 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2193 for (val = base; val; val = val->scc_next)
2194 for (src = val->sources; src; src = src->next)
2195 if (src->val
2196 && cgraph_maybe_hot_edge_p (src->cs))
2198 src->val->prop_time_benefit = safe_add (time,
2199 src->val->prop_time_benefit);
2200 src->val->prop_size_cost = safe_add (size,
2201 src->val->prop_size_cost);
2207 /* Propagate constants, binfos and their effects from the summaries
2208 interprocedurally. */
2210 static void
2211 ipcp_propagate_stage (struct topo_info *topo)
2213 struct cgraph_node *node;
2215 if (dump_file)
2216 fprintf (dump_file, "\n Propagating constants:\n\n");
2218 if (in_lto_p)
2219 ipa_update_after_lto_read ();
2222 FOR_EACH_DEFINED_FUNCTION (node)
2224 struct ipa_node_params *info = IPA_NODE_REF (node);
2226 determine_versionability (node);
2227 if (cgraph_function_with_gimple_body_p (node))
2229 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2230 ipa_get_param_count (info));
2231 initialize_node_lattices (node);
2233 if (node->symbol.definition && !node->symbol.alias)
2234 overall_size += inline_summary (node)->self_size;
2235 if (node->count > max_count)
2236 max_count = node->count;
2239 max_new_size = overall_size;
2240 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2241 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2242 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2244 if (dump_file)
2245 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2246 overall_size, max_new_size);
2248 propagate_constants_topo (topo);
2249 #ifdef ENABLE_CHECKING
2250 ipcp_verify_propagated_values ();
2251 #endif
2252 propagate_effects ();
2254 if (dump_file)
2256 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2257 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2261 /* Discover newly direct outgoing edges from NODE which is a new clone with
2262 known KNOWN_VALS and make them direct. */
2264 static void
2265 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2266 vec<tree> known_vals,
2267 struct ipa_agg_replacement_value *aggvals)
2269 struct cgraph_edge *ie, *next_ie;
2270 bool found = false;
2272 for (ie = node->indirect_calls; ie; ie = next_ie)
2274 tree target;
2276 next_ie = ie->next_callee;
2277 target = ipa_get_indirect_edge_target_1 (ie, known_vals, vNULL, vNULL,
2278 aggvals);
2279 if (target)
2281 bool agg_contents = ie->indirect_info->agg_contents;
2282 bool polymorphic = ie->indirect_info->polymorphic;
2283 bool param_index = ie->indirect_info->param_index;
2284 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target);
2285 found = true;
2287 if (cs && !agg_contents && !polymorphic)
2289 struct ipa_node_params *info = IPA_NODE_REF (node);
2290 int c = ipa_get_controlled_uses (info, param_index);
2291 if (c != IPA_UNDESCRIBED_USE)
2293 struct ipa_ref *to_del;
2295 c--;
2296 ipa_set_controlled_uses (info, param_index, c);
2297 if (dump_file && (dump_flags & TDF_DETAILS))
2298 fprintf (dump_file, " controlled uses count of param "
2299 "%i bumped down to %i\n", param_index, c);
2300 if (c == 0
2301 && (to_del = ipa_find_reference ((symtab_node) node,
2302 (symtab_node) cs->callee,
2303 NULL, 0)))
2305 if (dump_file && (dump_flags & TDF_DETAILS))
2306 fprintf (dump_file, " and even removing its "
2307 "cloning-created reference\n");
2308 ipa_remove_reference (to_del);
2314 /* Turning calls to direct calls will improve overall summary. */
2315 if (found)
2316 inline_update_overall_summary (node);
2319 /* Vector of pointers which for linked lists of clones of an original crgaph
2320 edge. */
2322 static vec<cgraph_edge_p> next_edge_clone;
2324 static inline void
2325 grow_next_edge_clone_vector (void)
2327 if (next_edge_clone.length ()
2328 <= (unsigned) cgraph_edge_max_uid)
2329 next_edge_clone.safe_grow_cleared (cgraph_edge_max_uid + 1);
2332 /* Edge duplication hook to grow the appropriate linked list in
2333 next_edge_clone. */
2335 static void
2336 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2337 __attribute__((unused)) void *data)
2339 grow_next_edge_clone_vector ();
2340 next_edge_clone[dst->uid] = next_edge_clone[src->uid];
2341 next_edge_clone[src->uid] = dst;
2344 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2345 parameter with the given INDEX. */
2347 static tree
2348 get_clone_agg_value (struct cgraph_node *node, HOST_WIDEST_INT offset,
2349 int index)
2351 struct ipa_agg_replacement_value *aggval;
2353 aggval = ipa_get_agg_replacements_for_node (node);
2354 while (aggval)
2356 if (aggval->offset == offset
2357 && aggval->index == index)
2358 return aggval->value;
2359 aggval = aggval->next;
2361 return NULL_TREE;
2364 /* Return true if edge CS does bring about the value described by SRC. */
2366 static bool
2367 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
2368 struct ipcp_value_source *src)
2370 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2371 struct ipa_node_params *dst_info = IPA_NODE_REF (cs->callee);
2373 if ((dst_info->ipcp_orig_node && !dst_info->is_all_contexts_clone)
2374 || caller_info->node_dead)
2375 return false;
2376 if (!src->val)
2377 return true;
2379 if (caller_info->ipcp_orig_node)
2381 tree t;
2382 if (src->offset == -1)
2383 t = caller_info->known_vals[src->index];
2384 else
2385 t = get_clone_agg_value (cs->caller, src->offset, src->index);
2386 return (t != NULL_TREE
2387 && values_equal_for_ipcp_p (src->val->value, t));
2389 else
2391 struct ipcp_agg_lattice *aglat;
2392 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
2393 src->index);
2394 if (src->offset == -1)
2395 return (ipa_lat_is_single_const (&plats->itself)
2396 && values_equal_for_ipcp_p (src->val->value,
2397 plats->itself.values->value));
2398 else
2400 if (plats->aggs_bottom || plats->aggs_contain_variable)
2401 return false;
2402 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2403 if (aglat->offset == src->offset)
2404 return (ipa_lat_is_single_const (aglat)
2405 && values_equal_for_ipcp_p (src->val->value,
2406 aglat->values->value));
2408 return false;
2412 /* Get the next clone in the linked list of clones of an edge. */
2414 static inline struct cgraph_edge *
2415 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
2417 return next_edge_clone[cs->uid];
2420 /* Given VAL, iterate over all its sources and if they still hold, add their
2421 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
2422 respectively. */
2424 static bool
2425 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
2426 gcov_type *count_sum, int *caller_count)
2428 struct ipcp_value_source *src;
2429 int freq = 0, count = 0;
2430 gcov_type cnt = 0;
2431 bool hot = false;
2433 for (src = val->sources; src; src = src->next)
2435 struct cgraph_edge *cs = src->cs;
2436 while (cs)
2438 if (cgraph_edge_brings_value_p (cs, src))
2440 count++;
2441 freq += cs->frequency;
2442 cnt += cs->count;
2443 hot |= cgraph_maybe_hot_edge_p (cs);
2445 cs = get_next_cgraph_edge_clone (cs);
2449 *freq_sum = freq;
2450 *count_sum = cnt;
2451 *caller_count = count;
2452 return hot;
2455 /* Return a vector of incoming edges that do bring value VAL. It is assumed
2456 their number is known and equal to CALLER_COUNT. */
2458 static vec<cgraph_edge_p>
2459 gather_edges_for_value (struct ipcp_value *val, int caller_count)
2461 struct ipcp_value_source *src;
2462 vec<cgraph_edge_p> ret;
2464 ret.create (caller_count);
2465 for (src = val->sources; src; src = src->next)
2467 struct cgraph_edge *cs = src->cs;
2468 while (cs)
2470 if (cgraph_edge_brings_value_p (cs, src))
2471 ret.quick_push (cs);
2472 cs = get_next_cgraph_edge_clone (cs);
2476 return ret;
2479 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
2480 Return it or NULL if for some reason it cannot be created. */
2482 static struct ipa_replace_map *
2483 get_replacement_map (struct ipa_node_params *info, tree value, int parm_num)
2485 struct ipa_replace_map *replace_map;
2488 replace_map = ggc_alloc_ipa_replace_map ();
2489 if (dump_file)
2491 fprintf (dump_file, " replacing ");
2492 ipa_dump_param (dump_file, info, parm_num);
2494 fprintf (dump_file, " with const ");
2495 print_generic_expr (dump_file, value, 0);
2496 fprintf (dump_file, "\n");
2498 replace_map->old_tree = NULL;
2499 replace_map->parm_num = parm_num;
2500 replace_map->new_tree = value;
2501 replace_map->replace_p = true;
2502 replace_map->ref_p = false;
2504 return replace_map;
2507 /* Dump new profiling counts */
2509 static void
2510 dump_profile_updates (struct cgraph_node *orig_node,
2511 struct cgraph_node *new_node)
2513 struct cgraph_edge *cs;
2515 fprintf (dump_file, " setting count of the specialized node to "
2516 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
2517 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2518 fprintf (dump_file, " edge to %s has count "
2519 HOST_WIDE_INT_PRINT_DEC "\n",
2520 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
2522 fprintf (dump_file, " setting count of the original node to "
2523 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
2524 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2525 fprintf (dump_file, " edge to %s is left with "
2526 HOST_WIDE_INT_PRINT_DEC "\n",
2527 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
2530 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
2531 their profile information to reflect this. */
2533 static void
2534 update_profiling_info (struct cgraph_node *orig_node,
2535 struct cgraph_node *new_node)
2537 struct cgraph_edge *cs;
2538 struct caller_statistics stats;
2539 gcov_type new_sum, orig_sum;
2540 gcov_type remainder, orig_node_count = orig_node->count;
2542 if (orig_node_count == 0)
2543 return;
2545 init_caller_stats (&stats);
2546 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
2547 orig_sum = stats.count_sum;
2548 init_caller_stats (&stats);
2549 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
2550 new_sum = stats.count_sum;
2552 if (orig_node_count < orig_sum + new_sum)
2554 if (dump_file)
2555 fprintf (dump_file, " Problem: node %s/%i has too low count "
2556 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
2557 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
2558 cgraph_node_name (orig_node), orig_node->symbol.order,
2559 (HOST_WIDE_INT) orig_node_count,
2560 (HOST_WIDE_INT) (orig_sum + new_sum));
2562 orig_node_count = (orig_sum + new_sum) * 12 / 10;
2563 if (dump_file)
2564 fprintf (dump_file, " proceeding by pretending it was "
2565 HOST_WIDE_INT_PRINT_DEC "\n",
2566 (HOST_WIDE_INT) orig_node_count);
2569 new_node->count = new_sum;
2570 remainder = orig_node_count - new_sum;
2571 orig_node->count = remainder;
2573 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2574 if (cs->frequency)
2575 cs->count = apply_probability (cs->count,
2576 GCOV_COMPUTE_SCALE (new_sum,
2577 orig_node_count));
2578 else
2579 cs->count = 0;
2581 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2582 cs->count = apply_probability (cs->count,
2583 GCOV_COMPUTE_SCALE (remainder,
2584 orig_node_count));
2586 if (dump_file)
2587 dump_profile_updates (orig_node, new_node);
2590 /* Update the respective profile of specialized NEW_NODE and the original
2591 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
2592 have been redirected to the specialized version. */
2594 static void
2595 update_specialized_profile (struct cgraph_node *new_node,
2596 struct cgraph_node *orig_node,
2597 gcov_type redirected_sum)
2599 struct cgraph_edge *cs;
2600 gcov_type new_node_count, orig_node_count = orig_node->count;
2602 if (dump_file)
2603 fprintf (dump_file, " the sum of counts of redirected edges is "
2604 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
2605 if (orig_node_count == 0)
2606 return;
2608 gcc_assert (orig_node_count >= redirected_sum);
2610 new_node_count = new_node->count;
2611 new_node->count += redirected_sum;
2612 orig_node->count -= redirected_sum;
2614 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2615 if (cs->frequency)
2616 cs->count += apply_probability (cs->count,
2617 GCOV_COMPUTE_SCALE (redirected_sum,
2618 new_node_count));
2619 else
2620 cs->count = 0;
2622 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2624 gcov_type dec = apply_probability (cs->count,
2625 GCOV_COMPUTE_SCALE (redirected_sum,
2626 orig_node_count));
2627 if (dec < cs->count)
2628 cs->count -= dec;
2629 else
2630 cs->count = 0;
2633 if (dump_file)
2634 dump_profile_updates (orig_node, new_node);
2637 /* Create a specialized version of NODE with known constants and types of
2638 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
2640 static struct cgraph_node *
2641 create_specialized_node (struct cgraph_node *node,
2642 vec<tree> known_vals,
2643 struct ipa_agg_replacement_value *aggvals,
2644 vec<cgraph_edge_p> callers)
2646 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
2647 vec<ipa_replace_map_p, va_gc> *replace_trees = NULL;
2648 struct ipa_agg_replacement_value *av;
2649 struct cgraph_node *new_node;
2650 int i, count = ipa_get_param_count (info);
2651 bitmap args_to_skip;
2653 gcc_assert (!info->ipcp_orig_node);
2655 if (node->local.can_change_signature)
2657 args_to_skip = BITMAP_GGC_ALLOC ();
2658 for (i = 0; i < count; i++)
2660 tree t = known_vals[i];
2662 if ((t && TREE_CODE (t) != TREE_BINFO)
2663 || !ipa_is_param_used (info, i))
2664 bitmap_set_bit (args_to_skip, i);
2667 else
2669 args_to_skip = NULL;
2670 if (dump_file && (dump_flags & TDF_DETAILS))
2671 fprintf (dump_file, " cannot change function signature\n");
2674 for (i = 0; i < count ; i++)
2676 tree t = known_vals[i];
2677 if (t && TREE_CODE (t) != TREE_BINFO)
2679 struct ipa_replace_map *replace_map;
2681 replace_map = get_replacement_map (info, t, i);
2682 if (replace_map)
2683 vec_safe_push (replace_trees, replace_map);
2687 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2688 args_to_skip, "constprop");
2689 ipa_set_node_agg_value_chain (new_node, aggvals);
2690 for (av = aggvals; av; av = av->next)
2691 ipa_maybe_record_reference ((symtab_node) new_node, av->value,
2692 IPA_REF_ADDR, NULL);
2694 if (dump_file && (dump_flags & TDF_DETAILS))
2696 fprintf (dump_file, " the new node is %s/%i.\n",
2697 cgraph_node_name (new_node), new_node->symbol.order);
2698 if (aggvals)
2699 ipa_dump_agg_replacement_values (dump_file, aggvals);
2701 gcc_checking_assert (ipa_node_params_vector.exists ()
2702 && (ipa_node_params_vector.length ()
2703 > (unsigned) cgraph_max_uid));
2704 update_profiling_info (node, new_node);
2705 new_info = IPA_NODE_REF (new_node);
2706 new_info->ipcp_orig_node = node;
2707 new_info->known_vals = known_vals;
2709 ipcp_discover_new_direct_edges (new_node, known_vals, aggvals);
2711 callers.release ();
2712 return new_node;
2715 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2716 KNOWN_VALS with constants and types that are also known for all of the
2717 CALLERS. */
2719 static void
2720 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
2721 vec<tree> known_vals,
2722 vec<cgraph_edge_p> callers)
2724 struct ipa_node_params *info = IPA_NODE_REF (node);
2725 int i, count = ipa_get_param_count (info);
2727 for (i = 0; i < count ; i++)
2729 struct cgraph_edge *cs;
2730 tree newval = NULL_TREE;
2731 int j;
2733 if (ipa_get_scalar_lat (info, i)->bottom || known_vals[i])
2734 continue;
2736 FOR_EACH_VEC_ELT (callers, j, cs)
2738 struct ipa_jump_func *jump_func;
2739 tree t;
2741 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2743 newval = NULL_TREE;
2744 break;
2746 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2747 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2748 if (!t
2749 || (newval
2750 && !values_equal_for_ipcp_p (t, newval)))
2752 newval = NULL_TREE;
2753 break;
2755 else
2756 newval = t;
2759 if (newval)
2761 if (dump_file && (dump_flags & TDF_DETAILS))
2763 fprintf (dump_file, " adding an extra known scalar value ");
2764 print_ipcp_constant_value (dump_file, newval);
2765 fprintf (dump_file, " for ");
2766 ipa_dump_param (dump_file, info, i);
2767 fprintf (dump_file, "\n");
2770 known_vals[i] = newval;
2775 /* Go through PLATS and create a vector of values consisting of values and
2776 offsets (minus OFFSET) of lattices that contain only a single value. */
2778 static vec<ipa_agg_jf_item_t>
2779 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
2781 vec<ipa_agg_jf_item_t> res = vNULL;
2783 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2784 return vNULL;
2786 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
2787 if (ipa_lat_is_single_const (aglat))
2789 struct ipa_agg_jf_item ti;
2790 ti.offset = aglat->offset - offset;
2791 ti.value = aglat->values->value;
2792 res.safe_push (ti);
2794 return res;
2797 /* Intersect all values in INTER with single value lattices in PLATS (while
2798 subtracting OFFSET). */
2800 static void
2801 intersect_with_plats (struct ipcp_param_lattices *plats,
2802 vec<ipa_agg_jf_item_t> *inter,
2803 HOST_WIDE_INT offset)
2805 struct ipcp_agg_lattice *aglat;
2806 struct ipa_agg_jf_item *item;
2807 int k;
2809 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2811 inter->release ();
2812 return;
2815 aglat = plats->aggs;
2816 FOR_EACH_VEC_ELT (*inter, k, item)
2818 bool found = false;
2819 if (!item->value)
2820 continue;
2821 while (aglat)
2823 if (aglat->offset - offset > item->offset)
2824 break;
2825 if (aglat->offset - offset == item->offset)
2827 gcc_checking_assert (item->value);
2828 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
2829 found = true;
2830 break;
2832 aglat = aglat->next;
2834 if (!found)
2835 item->value = NULL_TREE;
2839 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
2840 vector result while subtracting OFFSET from the individual value offsets. */
2842 static vec<ipa_agg_jf_item_t>
2843 agg_replacements_to_vector (struct cgraph_node *node, int index,
2844 HOST_WIDE_INT offset)
2846 struct ipa_agg_replacement_value *av;
2847 vec<ipa_agg_jf_item_t> res = vNULL;
2849 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
2850 if (av->index == index
2851 && (av->offset - offset) >= 0)
2853 struct ipa_agg_jf_item item;
2854 gcc_checking_assert (av->value);
2855 item.offset = av->offset - offset;
2856 item.value = av->value;
2857 res.safe_push (item);
2860 return res;
2863 /* Intersect all values in INTER with those that we have already scheduled to
2864 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
2865 (while subtracting OFFSET). */
2867 static void
2868 intersect_with_agg_replacements (struct cgraph_node *node, int index,
2869 vec<ipa_agg_jf_item_t> *inter,
2870 HOST_WIDE_INT offset)
2872 struct ipa_agg_replacement_value *srcvals;
2873 struct ipa_agg_jf_item *item;
2874 int i;
2876 srcvals = ipa_get_agg_replacements_for_node (node);
2877 if (!srcvals)
2879 inter->release ();
2880 return;
2883 FOR_EACH_VEC_ELT (*inter, i, item)
2885 struct ipa_agg_replacement_value *av;
2886 bool found = false;
2887 if (!item->value)
2888 continue;
2889 for (av = srcvals; av; av = av->next)
2891 gcc_checking_assert (av->value);
2892 if (av->index == index
2893 && av->offset - offset == item->offset)
2895 if (values_equal_for_ipcp_p (item->value, av->value))
2896 found = true;
2897 break;
2900 if (!found)
2901 item->value = NULL_TREE;
2905 /* Intersect values in INTER with aggregate values that come along edge CS to
2906 parameter number INDEX and return it. If INTER does not actually exist yet,
2907 copy all incoming values to it. If we determine we ended up with no values
2908 whatsoever, return a released vector. */
2910 static vec<ipa_agg_jf_item_t>
2911 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
2912 vec<ipa_agg_jf_item_t> inter)
2914 struct ipa_jump_func *jfunc;
2915 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
2916 if (jfunc->type == IPA_JF_PASS_THROUGH
2917 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2919 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2920 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2922 if (caller_info->ipcp_orig_node)
2924 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
2925 struct ipcp_param_lattices *orig_plats;
2926 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
2927 src_idx);
2928 if (agg_pass_through_permissible_p (orig_plats, jfunc))
2930 if (!inter.exists ())
2931 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
2932 else
2933 intersect_with_agg_replacements (cs->caller, src_idx,
2934 &inter, 0);
2937 else
2939 struct ipcp_param_lattices *src_plats;
2940 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
2941 if (agg_pass_through_permissible_p (src_plats, jfunc))
2943 /* Currently we do not produce clobber aggregate jump
2944 functions, adjust when we do. */
2945 gcc_checking_assert (!jfunc->agg.items);
2946 if (!inter.exists ())
2947 inter = copy_plats_to_inter (src_plats, 0);
2948 else
2949 intersect_with_plats (src_plats, &inter, 0);
2953 else if (jfunc->type == IPA_JF_ANCESTOR
2954 && ipa_get_jf_ancestor_agg_preserved (jfunc))
2956 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2957 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2958 struct ipcp_param_lattices *src_plats;
2959 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
2961 if (caller_info->ipcp_orig_node)
2963 if (!inter.exists ())
2964 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
2965 else
2966 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
2967 delta);
2969 else
2971 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
2972 /* Currently we do not produce clobber aggregate jump
2973 functions, adjust when we do. */
2974 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
2975 if (!inter.exists ())
2976 inter = copy_plats_to_inter (src_plats, delta);
2977 else
2978 intersect_with_plats (src_plats, &inter, delta);
2981 else if (jfunc->agg.items)
2983 struct ipa_agg_jf_item *item;
2984 int k;
2986 if (!inter.exists ())
2987 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
2988 inter.safe_push ((*jfunc->agg.items)[i]);
2989 else
2990 FOR_EACH_VEC_ELT (inter, k, item)
2992 int l = 0;
2993 bool found = false;;
2995 if (!item->value)
2996 continue;
2998 while ((unsigned) l < jfunc->agg.items->length ())
3000 struct ipa_agg_jf_item *ti;
3001 ti = &(*jfunc->agg.items)[l];
3002 if (ti->offset > item->offset)
3003 break;
3004 if (ti->offset == item->offset)
3006 gcc_checking_assert (ti->value);
3007 if (values_equal_for_ipcp_p (item->value,
3008 ti->value))
3009 found = true;
3010 break;
3012 l++;
3014 if (!found)
3015 item->value = NULL;
3018 else
3020 inter.release();
3021 return vec<ipa_agg_jf_item_t>();
3023 return inter;
3026 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3027 from all of them. */
3029 static struct ipa_agg_replacement_value *
3030 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3031 vec<cgraph_edge_p> callers)
3033 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3034 struct ipa_agg_replacement_value *res = NULL;
3035 struct cgraph_edge *cs;
3036 int i, j, count = ipa_get_param_count (dest_info);
3038 FOR_EACH_VEC_ELT (callers, j, cs)
3040 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3041 if (c < count)
3042 count = c;
3045 for (i = 0; i < count ; i++)
3047 struct cgraph_edge *cs;
3048 vec<ipa_agg_jf_item_t> inter = vNULL;
3049 struct ipa_agg_jf_item *item;
3050 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3051 int j;
3053 /* Among other things, the following check should deal with all by_ref
3054 mismatches. */
3055 if (plats->aggs_bottom)
3056 continue;
3058 FOR_EACH_VEC_ELT (callers, j, cs)
3060 inter = intersect_aggregates_with_edge (cs, i, inter);
3062 if (!inter.exists ())
3063 goto next_param;
3066 FOR_EACH_VEC_ELT (inter, j, item)
3068 struct ipa_agg_replacement_value *v;
3070 if (!item->value)
3071 continue;
3073 v = ggc_alloc_ipa_agg_replacement_value ();
3074 v->index = i;
3075 v->offset = item->offset;
3076 v->value = item->value;
3077 v->by_ref = plats->aggs_by_ref;
3078 v->next = res;
3079 res = v;
3082 next_param:
3083 if (inter.exists ())
3084 inter.release ();
3086 return res;
3089 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3091 static struct ipa_agg_replacement_value *
3092 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function_t> known_aggs)
3094 struct ipa_agg_replacement_value *res = NULL;
3095 struct ipa_agg_jump_function *aggjf;
3096 struct ipa_agg_jf_item *item;
3097 int i, j;
3099 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3100 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3102 struct ipa_agg_replacement_value *v;
3103 v = ggc_alloc_ipa_agg_replacement_value ();
3104 v->index = i;
3105 v->offset = item->offset;
3106 v->value = item->value;
3107 v->by_ref = aggjf->by_ref;
3108 v->next = res;
3109 res = v;
3111 return res;
3114 /* Determine whether CS also brings all scalar values that the NODE is
3115 specialized for. */
3117 static bool
3118 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3119 struct cgraph_node *node)
3121 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3122 int count = ipa_get_param_count (dest_info);
3123 struct ipa_node_params *caller_info;
3124 struct ipa_edge_args *args;
3125 int i;
3127 caller_info = IPA_NODE_REF (cs->caller);
3128 args = IPA_EDGE_REF (cs);
3129 for (i = 0; i < count; i++)
3131 struct ipa_jump_func *jump_func;
3132 tree val, t;
3134 val = dest_info->known_vals[i];
3135 if (!val)
3136 continue;
3138 if (i >= ipa_get_cs_argument_count (args))
3139 return false;
3140 jump_func = ipa_get_ith_jump_func (args, i);
3141 t = ipa_value_from_jfunc (caller_info, jump_func);
3142 if (!t || !values_equal_for_ipcp_p (val, t))
3143 return false;
3145 return true;
3148 /* Determine whether CS also brings all aggregate values that NODE is
3149 specialized for. */
3150 static bool
3151 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3152 struct cgraph_node *node)
3154 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3155 struct ipa_agg_replacement_value *aggval;
3156 int i, ec, count;
3158 aggval = ipa_get_agg_replacements_for_node (node);
3159 if (!aggval)
3160 return true;
3162 count = ipa_get_param_count (IPA_NODE_REF (node));
3163 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3164 if (ec < count)
3165 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3166 if (aggval->index >= ec)
3167 return false;
3169 if (orig_caller_info->ipcp_orig_node)
3170 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3172 for (i = 0; i < count; i++)
3174 static vec<ipa_agg_jf_item_t> values = vec<ipa_agg_jf_item_t>();
3175 struct ipcp_param_lattices *plats;
3176 bool interesting = false;
3177 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3178 if (aggval->index == i)
3180 interesting = true;
3181 break;
3183 if (!interesting)
3184 continue;
3186 plats = ipa_get_parm_lattices (orig_caller_info, aggval->index);
3187 if (plats->aggs_bottom)
3188 return false;
3190 values = intersect_aggregates_with_edge (cs, i, values);
3191 if (!values.exists())
3192 return false;
3194 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3195 if (aggval->index == i)
3197 struct ipa_agg_jf_item *item;
3198 int j;
3199 bool found = false;
3200 FOR_EACH_VEC_ELT (values, j, item)
3201 if (item->value
3202 && item->offset == av->offset
3203 && values_equal_for_ipcp_p (item->value, av->value))
3205 found = true;
3206 break;
3208 if (!found)
3210 values.release();
3211 return false;
3215 return true;
3218 /* Given an original NODE and a VAL for which we have already created a
3219 specialized clone, look whether there are incoming edges that still lead
3220 into the old node but now also bring the requested value and also conform to
3221 all other criteria such that they can be redirected the the special node.
3222 This function can therefore redirect the final edge in a SCC. */
3224 static void
3225 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
3227 struct ipcp_value_source *src;
3228 gcov_type redirected_sum = 0;
3230 for (src = val->sources; src; src = src->next)
3232 struct cgraph_edge *cs = src->cs;
3233 while (cs)
3235 enum availability availability;
3236 struct cgraph_node *dst = cgraph_function_node (cs->callee,
3237 &availability);
3238 if ((dst == node || IPA_NODE_REF (dst)->is_all_contexts_clone)
3239 && availability > AVAIL_OVERWRITABLE
3240 && cgraph_edge_brings_value_p (cs, src))
3242 if (cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3243 && cgraph_edge_brings_all_agg_vals_for_node (cs,
3244 val->spec_node))
3246 if (dump_file)
3247 fprintf (dump_file, " - adding an extra caller %s/%i"
3248 " of %s/%i\n",
3249 xstrdup (cgraph_node_name (cs->caller)),
3250 cs->caller->symbol.order,
3251 xstrdup (cgraph_node_name (val->spec_node)),
3252 val->spec_node->symbol.order);
3254 cgraph_redirect_edge_callee (cs, val->spec_node);
3255 redirected_sum += cs->count;
3258 cs = get_next_cgraph_edge_clone (cs);
3262 if (redirected_sum)
3263 update_specialized_profile (val->spec_node, node, redirected_sum);
3267 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
3269 static void
3270 move_binfos_to_values (vec<tree> known_vals,
3271 vec<tree> known_binfos)
3273 tree t;
3274 int i;
3276 for (i = 0; known_binfos.iterate (i, &t); i++)
3277 if (t)
3278 known_vals[i] = t;
3281 /* Return true if there is a replacement equivalent to VALUE, INDEX and OFFSET
3282 among those in the AGGVALS list. */
3284 DEBUG_FUNCTION bool
3285 ipcp_val_in_agg_replacements_p (struct ipa_agg_replacement_value *aggvals,
3286 int index, HOST_WIDE_INT offset, tree value)
3288 while (aggvals)
3290 if (aggvals->index == index
3291 && aggvals->offset == offset
3292 && values_equal_for_ipcp_p (aggvals->value, value))
3293 return true;
3294 aggvals = aggvals->next;
3296 return false;
3299 /* Decide wheter to create a special version of NODE for value VAL of parameter
3300 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
3301 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
3302 KNOWN_BINFOS and KNOWN_AGGS describe the other already known values. */
3304 static bool
3305 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
3306 struct ipcp_value *val, vec<tree> known_csts,
3307 vec<tree> known_binfos)
3309 struct ipa_agg_replacement_value *aggvals;
3310 int freq_sum, caller_count;
3311 gcov_type count_sum;
3312 vec<cgraph_edge_p> callers;
3313 vec<tree> kv;
3315 if (val->spec_node)
3317 perhaps_add_new_callers (node, val);
3318 return false;
3320 else if (val->local_size_cost + overall_size > max_new_size)
3322 if (dump_file && (dump_flags & TDF_DETAILS))
3323 fprintf (dump_file, " Ignoring candidate value because "
3324 "max_new_size would be reached with %li.\n",
3325 val->local_size_cost + overall_size);
3326 return false;
3328 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
3329 &caller_count))
3330 return false;
3332 if (dump_file && (dump_flags & TDF_DETAILS))
3334 fprintf (dump_file, " - considering value ");
3335 print_ipcp_constant_value (dump_file, val->value);
3336 fprintf (dump_file, " for ");
3337 ipa_dump_param (dump_file, IPA_NODE_REF (node), index);
3338 if (offset != -1)
3339 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
3340 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
3343 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
3344 freq_sum, count_sum,
3345 val->local_size_cost)
3346 && !good_cloning_opportunity_p (node,
3347 val->local_time_benefit
3348 + val->prop_time_benefit,
3349 freq_sum, count_sum,
3350 val->local_size_cost
3351 + val->prop_size_cost))
3352 return false;
3354 if (dump_file)
3355 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
3356 cgraph_node_name (node), node->symbol.order);
3358 callers = gather_edges_for_value (val, caller_count);
3359 kv = known_csts.copy ();
3360 move_binfos_to_values (kv, known_binfos);
3361 if (offset == -1)
3362 kv[index] = val->value;
3363 find_more_scalar_values_for_callers_subset (node, kv, callers);
3364 aggvals = find_aggregate_values_for_callers_subset (node, callers);
3365 gcc_checking_assert (offset == -1
3366 || ipcp_val_in_agg_replacements_p (aggvals, index,
3367 offset, val->value));
3368 val->spec_node = create_specialized_node (node, kv, aggvals, callers);
3369 overall_size += val->local_size_cost;
3371 /* TODO: If for some lattice there is only one other known value
3372 left, make a special node for it too. */
3374 return true;
3377 /* Decide whether and what specialized clones of NODE should be created. */
3379 static bool
3380 decide_whether_version_node (struct cgraph_node *node)
3382 struct ipa_node_params *info = IPA_NODE_REF (node);
3383 int i, count = ipa_get_param_count (info);
3384 vec<tree> known_csts, known_binfos;
3385 vec<ipa_agg_jump_function_t> known_aggs = vNULL;
3386 bool ret = false;
3388 if (count == 0)
3389 return false;
3391 if (dump_file && (dump_flags & TDF_DETAILS))
3392 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
3393 cgraph_node_name (node), node->symbol.order);
3395 gather_context_independent_values (info, &known_csts, &known_binfos,
3396 info->do_clone_for_all_contexts ? &known_aggs
3397 : NULL, NULL);
3399 for (i = 0; i < count ;i++)
3401 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3402 struct ipcp_lattice *lat = &plats->itself;
3403 struct ipcp_value *val;
3405 if (!lat->bottom
3406 && !known_csts[i]
3407 && !known_binfos[i])
3408 for (val = lat->values; val; val = val->next)
3409 ret |= decide_about_value (node, i, -1, val, known_csts,
3410 known_binfos);
3412 if (!plats->aggs_bottom)
3414 struct ipcp_agg_lattice *aglat;
3415 struct ipcp_value *val;
3416 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3417 if (!aglat->bottom && aglat->values
3418 /* If the following is false, the one value is in
3419 known_aggs. */
3420 && (plats->aggs_contain_variable
3421 || !ipa_lat_is_single_const (aglat)))
3422 for (val = aglat->values; val; val = val->next)
3423 ret |= decide_about_value (node, i, aglat->offset, val,
3424 known_csts, known_binfos);
3426 info = IPA_NODE_REF (node);
3429 if (info->do_clone_for_all_contexts)
3431 struct cgraph_node *clone;
3432 vec<cgraph_edge_p> callers;
3434 if (dump_file)
3435 fprintf (dump_file, " - Creating a specialized node of %s/%i "
3436 "for all known contexts.\n", cgraph_node_name (node),
3437 node->symbol.order);
3439 callers = collect_callers_of_node (node);
3440 move_binfos_to_values (known_csts, known_binfos);
3441 clone = create_specialized_node (node, known_csts,
3442 known_aggs_to_agg_replacement_list (known_aggs),
3443 callers);
3444 info = IPA_NODE_REF (node);
3445 info->do_clone_for_all_contexts = false;
3446 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
3447 for (i = 0; i < count ; i++)
3448 vec_free (known_aggs[i].items);
3449 known_aggs.release ();
3450 ret = true;
3452 else
3453 known_csts.release ();
3455 known_binfos.release ();
3456 return ret;
3459 /* Transitively mark all callees of NODE within the same SCC as not dead. */
3461 static void
3462 spread_undeadness (struct cgraph_node *node)
3464 struct cgraph_edge *cs;
3466 for (cs = node->callees; cs; cs = cs->next_callee)
3467 if (edge_within_scc (cs))
3469 struct cgraph_node *callee;
3470 struct ipa_node_params *info;
3472 callee = cgraph_function_node (cs->callee, NULL);
3473 info = IPA_NODE_REF (callee);
3475 if (info->node_dead)
3477 info->node_dead = 0;
3478 spread_undeadness (callee);
3483 /* Return true if NODE has a caller from outside of its SCC that is not
3484 dead. Worker callback for cgraph_for_node_and_aliases. */
3486 static bool
3487 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
3488 void *data ATTRIBUTE_UNUSED)
3490 struct cgraph_edge *cs;
3492 for (cs = node->callers; cs; cs = cs->next_caller)
3493 if (cs->caller->thunk.thunk_p
3494 && cgraph_for_node_and_aliases (cs->caller,
3495 has_undead_caller_from_outside_scc_p,
3496 NULL, true))
3497 return true;
3498 else if (!edge_within_scc (cs)
3499 && !IPA_NODE_REF (cs->caller)->node_dead)
3500 return true;
3501 return false;
3505 /* Identify nodes within the same SCC as NODE which are no longer needed
3506 because of new clones and will be removed as unreachable. */
3508 static void
3509 identify_dead_nodes (struct cgraph_node *node)
3511 struct cgraph_node *v;
3512 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3513 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
3514 && !cgraph_for_node_and_aliases (v,
3515 has_undead_caller_from_outside_scc_p,
3516 NULL, true))
3517 IPA_NODE_REF (v)->node_dead = 1;
3519 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3520 if (!IPA_NODE_REF (v)->node_dead)
3521 spread_undeadness (v);
3523 if (dump_file && (dump_flags & TDF_DETAILS))
3525 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3526 if (IPA_NODE_REF (v)->node_dead)
3527 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
3528 cgraph_node_name (v), v->symbol.order);
3532 /* The decision stage. Iterate over the topological order of call graph nodes
3533 TOPO and make specialized clones if deemed beneficial. */
3535 static void
3536 ipcp_decision_stage (struct topo_info *topo)
3538 int i;
3540 if (dump_file)
3541 fprintf (dump_file, "\nIPA decision stage:\n\n");
3543 for (i = topo->nnodes - 1; i >= 0; i--)
3545 struct cgraph_node *node = topo->order[i];
3546 bool change = false, iterate = true;
3548 while (iterate)
3550 struct cgraph_node *v;
3551 iterate = false;
3552 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3553 if (cgraph_function_with_gimple_body_p (v)
3554 && ipcp_versionable_function_p (v))
3555 iterate |= decide_whether_version_node (v);
3557 change |= iterate;
3559 if (change)
3560 identify_dead_nodes (node);
3564 /* The IPCP driver. */
3566 static unsigned int
3567 ipcp_driver (void)
3569 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
3570 struct topo_info topo;
3572 ipa_check_create_node_params ();
3573 ipa_check_create_edge_args ();
3574 grow_next_edge_clone_vector ();
3575 edge_duplication_hook_holder =
3576 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
3577 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
3578 sizeof (struct ipcp_value), 32);
3579 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
3580 sizeof (struct ipcp_value_source), 64);
3581 ipcp_agg_lattice_pool = create_alloc_pool ("IPA_CP aggregate lattices",
3582 sizeof (struct ipcp_agg_lattice),
3583 32);
3584 if (dump_file)
3586 fprintf (dump_file, "\nIPA structures before propagation:\n");
3587 if (dump_flags & TDF_DETAILS)
3588 ipa_print_all_params (dump_file);
3589 ipa_print_all_jump_functions (dump_file);
3592 /* Topological sort. */
3593 build_toporder_info (&topo);
3594 /* Do the interprocedural propagation. */
3595 ipcp_propagate_stage (&topo);
3596 /* Decide what constant propagation and cloning should be performed. */
3597 ipcp_decision_stage (&topo);
3599 /* Free all IPCP structures. */
3600 free_toporder_info (&topo);
3601 next_edge_clone.release ();
3602 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3603 ipa_free_all_structures_after_ipa_cp ();
3604 if (dump_file)
3605 fprintf (dump_file, "\nIPA constant propagation end\n");
3606 return 0;
3609 /* Initialization and computation of IPCP data structures. This is the initial
3610 intraprocedural analysis of functions, which gathers information to be
3611 propagated later on. */
3613 static void
3614 ipcp_generate_summary (void)
3616 struct cgraph_node *node;
3618 if (dump_file)
3619 fprintf (dump_file, "\nIPA constant propagation start:\n");
3620 ipa_register_cgraph_hooks ();
3622 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
3624 node->local.versionable
3625 = tree_versionable_function_p (node->symbol.decl);
3626 ipa_analyze_node (node);
3630 /* Write ipcp summary for nodes in SET. */
3632 static void
3633 ipcp_write_summary (void)
3635 ipa_prop_write_jump_functions ();
3638 /* Read ipcp summary. */
3640 static void
3641 ipcp_read_summary (void)
3643 ipa_prop_read_jump_functions ();
3646 /* Gate for IPCP optimization. */
3648 static bool
3649 cgraph_gate_cp (void)
3651 /* FIXME: We should remove the optimize check after we ensure we never run
3652 IPA passes when not optimizing. */
3653 return flag_ipa_cp && optimize;
3656 namespace {
3658 const pass_data pass_data_ipa_cp =
3660 IPA_PASS, /* type */
3661 "cp", /* name */
3662 OPTGROUP_NONE, /* optinfo_flags */
3663 true, /* has_gate */
3664 true, /* has_execute */
3665 TV_IPA_CONSTANT_PROP, /* tv_id */
3666 0, /* properties_required */
3667 0, /* properties_provided */
3668 0, /* properties_destroyed */
3669 0, /* todo_flags_start */
3670 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
3673 class pass_ipa_cp : public ipa_opt_pass_d
3675 public:
3676 pass_ipa_cp(gcc::context *ctxt)
3677 : ipa_opt_pass_d(pass_data_ipa_cp, ctxt,
3678 ipcp_generate_summary, /* generate_summary */
3679 ipcp_write_summary, /* write_summary */
3680 ipcp_read_summary, /* read_summary */
3681 ipa_prop_write_all_agg_replacement, /*
3682 write_optimization_summary */
3683 ipa_prop_read_all_agg_replacement, /*
3684 read_optimization_summary */
3685 NULL, /* stmt_fixup */
3686 0, /* function_transform_todo_flags_start */
3687 ipcp_transform_function, /* function_transform */
3688 NULL) /* variable_transform */
3691 /* opt_pass methods: */
3692 bool gate () { return cgraph_gate_cp (); }
3693 unsigned int execute () { return ipcp_driver (); }
3695 }; // class pass_ipa_cp
3697 } // anon namespace
3699 ipa_opt_pass_d *
3700 make_pass_ipa_cp (gcc::context *ctxt)
3702 return new pass_ipa_cp (ctxt);