re PR middle-end/50301 (416.gamess in SPEC CPU 2006 failed to build with LTO)
[gcc.git] / gcc / ipa-cp.c
1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4
5 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
6 <mjambor@suse.cz>
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 /* Interprocedural constant propagation (IPA-CP).
25
26 The goal of this transformation is to
27
28 1) discover functions which are always invoked with some arguments with the
29 same known constant values and modify the functions so that the
30 subsequent optimizations can take advantage of the knowledge, and
31
32 2) partial specialization - create specialized versions of functions
33 transformed in this way if some parameters are known constants only in
34 certain contexts but the estimated tradeoff between speedup and cost size
35 is deemed good.
36
37 The algorithm also propagates types and attempts to perform type based
38 devirtualization. Types are propagated much like constants.
39
40 The algorithm basically consists of three stages. In the first, functions
41 are analyzed one at a time and jump functions are constructed for all known
42 call-sites. In the second phase, the pass propagates information from the
43 jump functions across the call to reveal what values are available at what
44 call sites, performs estimations of effects of known values on functions and
45 their callees, and finally decides what specialized extra versions should be
46 created. In the third, the special versions materialize and appropriate
47 calls are redirected.
48
49 The algorithm used is to a certain extent based on "Interprocedural Constant
50 Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon,
51 Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D
52 Cooper, Mary W. Hall, and Ken Kennedy.
53
54
55 First stage - intraprocedural analysis
56 =======================================
57
58 This phase computes jump_function and modification flags.
59
60 A jump function for a call-site represents the values passed as an actual
61 arguments of a given call-site. In principle, there are three types of
62 values:
63
64 Pass through - the caller's formal parameter is passed as an actual
65 argument, plus an operation on it can be performed.
66 Constant - a constant is passed as an actual argument.
67 Unknown - neither of the above.
68
69 All jump function types are described in detail in ipa-prop.h, together with
70 the data structures that represent them and methods of accessing them.
71
72 ipcp_generate_summary() is the main function of the first stage.
73
74 Second stage - interprocedural analysis
75 ========================================
76
77 This stage is itself divided into two phases. In the first, we propagate
78 known values over the call graph, in the second, we make cloning decisions.
79 It uses a different algorithm than the original Callahan's paper.
80
81 First, we traverse the functions topologically from callers to callees and,
82 for each strongly connected component (SCC), we propagate constants
83 according to previously computed jump functions. We also record what known
84 values depend on other known values and estimate local effects. Finally, we
85 propagate cumulative information about these effects from dependant values
86 to those on which they depend.
87
88 Second, we again traverse the call graph in the same topological order and
89 make clones for functions which we know are called with the same values in
90 all contexts and decide about extra specialized clones of functions just for
91 some contexts - these decisions are based on both local estimates and
92 cumulative estimates propagated from callees.
93
94 ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
95 third stage.
96
97 Third phase - materialization of clones, call statement updates.
98 ============================================
99
100 This stage is currently performed by call graph code (mainly in cgraphunit.c
101 and tree-inline.c) according to instructions inserted to the call graph by
102 the second stage. */
103
104 #include "config.h"
105 #include "system.h"
106 #include "coretypes.h"
107 #include "tree.h"
108 #include "target.h"
109 #include "gimple.h"
110 #include "cgraph.h"
111 #include "ipa-prop.h"
112 #include "tree-flow.h"
113 #include "tree-pass.h"
114 #include "flags.h"
115 #include "timevar.h"
116 #include "diagnostic.h"
117 #include "tree-pretty-print.h"
118 #include "tree-dump.h"
119 #include "tree-inline.h"
120 #include "fibheap.h"
121 #include "params.h"
122 #include "ipa-inline.h"
123 #include "ipa-utils.h"
124
125 struct ipcp_value;
126
127 /* Describes a particular source for an IPA-CP value. */
128
129 struct ipcp_value_source
130 {
131 /* The incoming edge that brought the value. */
132 struct cgraph_edge *cs;
133 /* If the jump function that resulted into his value was a pass-through or an
134 ancestor, this is the ipcp_value of the caller from which the described
135 value has been derived. Otherwise it is NULL. */
136 struct ipcp_value *val;
137 /* Next pointer in a linked list of sources of a value. */
138 struct ipcp_value_source *next;
139 /* If the jump function that resulted into his value was a pass-through or an
140 ancestor, this is the index of the parameter of the caller the jump
141 function references. */
142 int index;
143 };
144
145 /* Describes one particular value stored in struct ipcp_lattice. */
146
147 struct ipcp_value
148 {
149 /* The actual value for the given parameter. This is either an IPA invariant
150 or a TREE_BINFO describing a type that can be used for
151 devirtualization. */
152 tree value;
153 /* The list of sources from which this value originates. */
154 struct ipcp_value_source *sources;
155 /* Next pointers in a linked list of all values in a lattice. */
156 struct ipcp_value *next;
157 /* Next pointers in a linked list of values in a strongly connected component
158 of values. */
159 struct ipcp_value *scc_next;
160 /* Next pointers in a linked list of SCCs of values sorted topologically
161 according their sources. */
162 struct ipcp_value *topo_next;
163 /* A specialized node created for this value, NULL if none has been (so far)
164 created. */
165 struct cgraph_node *spec_node;
166 /* Depth first search number and low link for topological sorting of
167 values. */
168 int dfs, low_link;
169 /* Time benefit and size cost that specializing the function for this value
170 would bring about in this function alone. */
171 int local_time_benefit, local_size_cost;
172 /* Time benefit and size cost that specializing the function for this value
173 can bring about in it's callees (transitively). */
174 int prop_time_benefit, prop_size_cost;
175 /* True if this valye is currently on the topo-sort stack. */
176 bool on_stack;
177 };
178
179 /* Allocation pools for values and their sources in ipa-cp. */
180
181 alloc_pool ipcp_values_pool;
182 alloc_pool ipcp_sources_pool;
183
184 /* Lattice describing potential values of a formal parameter of a function and
185 some of their other properties. TOP is represented by a lattice with zero
186 values and with contains_variable and bottom flags cleared. BOTTOM is
187 represented by a lattice with the bottom flag set. In that case, values and
188 contains_variable flag should be disregarded. */
189
190 struct ipcp_lattice
191 {
192 /* The list of known values and types in this lattice. Note that values are
193 not deallocated if a lattice is set to bottom because there may be value
194 sources referencing them. */
195 struct ipcp_value *values;
196 /* Number of known values and types in this lattice. */
197 int values_count;
198 /* The lattice contains a variable component (in addition to values). */
199 bool contains_variable;
200 /* The value of the lattice is bottom (i.e. variable and unusable for any
201 propagation). */
202 bool bottom;
203 /* There is a virtual call based on this parameter. */
204 bool virt_call;
205 };
206
207 /* Maximal count found in program. */
208
209 static gcov_type max_count;
210
211 /* Original overall size of the program. */
212
213 static long overall_size, max_new_size;
214
215 /* Head of the linked list of topologically sorted values. */
216
217 static struct ipcp_value *values_topo;
218
219 /* Return the lattice corresponding to the Ith formal parameter of the function
220 described by INFO. */
221 static inline struct ipcp_lattice *
222 ipa_get_lattice (struct ipa_node_params *info, int i)
223 {
224 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
225 gcc_checking_assert (!info->ipcp_orig_node);
226 gcc_checking_assert (info->lattices);
227 return &(info->lattices[i]);
228 }
229
230 /* Return whether LAT is a lattice with a single constant and without an
231 undefined value. */
232
233 static inline bool
234 ipa_lat_is_single_const (struct ipcp_lattice *lat)
235 {
236 if (lat->bottom
237 || lat->contains_variable
238 || lat->values_count != 1)
239 return false;
240 else
241 return true;
242 }
243
244 /* Return true iff the CS is an edge within a strongly connected component as
245 computed by ipa_reduced_postorder. */
246
247 static inline bool
248 edge_within_scc (struct cgraph_edge *cs)
249 {
250 struct ipa_dfs_info *caller_dfs = (struct ipa_dfs_info *) cs->caller->aux;
251 struct ipa_dfs_info *callee_dfs;
252 struct cgraph_node *callee = cgraph_function_node (cs->callee, NULL);
253
254 callee_dfs = (struct ipa_dfs_info *) callee->aux;
255 return (caller_dfs
256 && callee_dfs
257 && caller_dfs->scc_no == callee_dfs->scc_no);
258 }
259
260 /* Print V which is extracted from a value in a lattice to F. */
261
262 static void
263 print_ipcp_constant_value (FILE * f, tree v)
264 {
265 if (TREE_CODE (v) == TREE_BINFO)
266 {
267 fprintf (f, "BINFO ");
268 print_generic_expr (f, BINFO_TYPE (v), 0);
269 }
270 else if (TREE_CODE (v) == ADDR_EXPR
271 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
272 {
273 fprintf (f, "& ");
274 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
275 }
276 else
277 print_generic_expr (f, v, 0);
278 }
279
280 /* Print all ipcp_lattices of all functions to F. */
281
282 static void
283 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
284 {
285 struct cgraph_node *node;
286 int i, count;
287
288 fprintf (f, "\nLattices:\n");
289 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
290 {
291 struct ipa_node_params *info;
292
293 info = IPA_NODE_REF (node);
294 fprintf (f, " Node: %s/%i:\n", cgraph_node_name (node), node->uid);
295 count = ipa_get_param_count (info);
296 for (i = 0; i < count; i++)
297 {
298 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
299 struct ipcp_value *val;
300 bool prev = false;
301
302 fprintf (f, " param [%d]: ", i);
303 if (lat->bottom)
304 {
305 fprintf (f, "BOTTOM\n");
306 continue;
307 }
308
309 if (!lat->values_count && !lat->contains_variable)
310 {
311 fprintf (f, "TOP\n");
312 continue;
313 }
314
315 if (lat->contains_variable)
316 {
317 fprintf (f, "VARIABLE");
318 prev = true;
319 if (dump_benefits)
320 fprintf (f, "\n");
321 }
322
323 for (val = lat->values; val; val = val->next)
324 {
325 if (dump_benefits && prev)
326 fprintf (f, " ");
327 else if (!dump_benefits && prev)
328 fprintf (f, ", ");
329 else
330 prev = true;
331
332 print_ipcp_constant_value (f, val->value);
333
334 if (dump_sources)
335 {
336 struct ipcp_value_source *s;
337
338 fprintf (f, " [from:");
339 for (s = val->sources; s; s = s->next)
340 fprintf (f, " %i(%i)", s->cs->caller->uid,s->cs->frequency);
341 fprintf (f, "]");
342 }
343
344 if (dump_benefits)
345 fprintf (f, " [loc_time: %i, loc_size: %i, "
346 "prop_time: %i, prop_size: %i]\n",
347 val->local_time_benefit, val->local_size_cost,
348 val->prop_time_benefit, val->prop_size_cost);
349 }
350 if (!dump_benefits)
351 fprintf (f, "\n");
352 }
353 }
354 }
355
356 /* Determine whether it is at all technically possible to create clones of NODE
357 and store this information in the ipa_node_params structure associated
358 with NODE. */
359
360 static void
361 determine_versionability (struct cgraph_node *node)
362 {
363 const char *reason = NULL;
364
365 /* There are a number of generic reasons functions cannot be versioned. We
366 also cannot remove parameters if there are type attributes such as fnspec
367 present. */
368 if (node->alias || node->thunk.thunk_p)
369 reason = "alias or thunk";
370 else if (!node->local.versionable)
371 reason = "not a tree_versionable_function";
372 else if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
373 reason = "insufficient body availability";
374
375 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
376 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
377 cgraph_node_name (node), node->uid, reason);
378
379 node->local.versionable = (reason == NULL);
380 }
381
382 /* Return true if it is at all technically possible to create clones of a
383 NODE. */
384
385 static bool
386 ipcp_versionable_function_p (struct cgraph_node *node)
387 {
388 return node->local.versionable;
389 }
390
391 /* Structure holding accumulated information about callers of a node. */
392
393 struct caller_statistics
394 {
395 gcov_type count_sum;
396 int n_calls, n_hot_calls, freq_sum;
397 };
398
399 /* Initialize fields of STAT to zeroes. */
400
401 static inline void
402 init_caller_stats (struct caller_statistics *stats)
403 {
404 stats->count_sum = 0;
405 stats->n_calls = 0;
406 stats->n_hot_calls = 0;
407 stats->freq_sum = 0;
408 }
409
410 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
411 non-thunk incoming edges to NODE. */
412
413 static bool
414 gather_caller_stats (struct cgraph_node *node, void *data)
415 {
416 struct caller_statistics *stats = (struct caller_statistics *) data;
417 struct cgraph_edge *cs;
418
419 for (cs = node->callers; cs; cs = cs->next_caller)
420 if (cs->caller->thunk.thunk_p)
421 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
422 stats, false);
423 else
424 {
425 stats->count_sum += cs->count;
426 stats->freq_sum += cs->frequency;
427 stats->n_calls++;
428 if (cgraph_maybe_hot_edge_p (cs))
429 stats->n_hot_calls ++;
430 }
431 return false;
432
433 }
434
435 /* Return true if this NODE is viable candidate for cloning. */
436
437 static bool
438 ipcp_cloning_candidate_p (struct cgraph_node *node)
439 {
440 struct caller_statistics stats;
441
442 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
443
444 if (!flag_ipa_cp_clone)
445 {
446 if (dump_file)
447 fprintf (dump_file, "Not considering %s for cloning; "
448 "-fipa-cp-clone disabled.\n",
449 cgraph_node_name (node));
450 return false;
451 }
452
453 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
454 {
455 if (dump_file)
456 fprintf (dump_file, "Not considering %s for cloning; "
457 "optimizing it for size.\n",
458 cgraph_node_name (node));
459 return false;
460 }
461
462 init_caller_stats (&stats);
463 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
464
465 if (inline_summary (node)->self_size < stats.n_calls)
466 {
467 if (dump_file)
468 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
469 cgraph_node_name (node));
470 return true;
471 }
472
473 /* When profile is available and function is hot, propagate into it even if
474 calls seems cold; constant propagation can improve function's speed
475 significantly. */
476 if (max_count)
477 {
478 if (stats.count_sum > node->count * 90 / 100)
479 {
480 if (dump_file)
481 fprintf (dump_file, "Considering %s for cloning; "
482 "usually called directly.\n",
483 cgraph_node_name (node));
484 return true;
485 }
486 }
487 if (!stats.n_hot_calls)
488 {
489 if (dump_file)
490 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
491 cgraph_node_name (node));
492 return false;
493 }
494 if (dump_file)
495 fprintf (dump_file, "Considering %s for cloning.\n",
496 cgraph_node_name (node));
497 return true;
498 }
499
500 /* Arrays representing a topological ordering of call graph nodes and a stack
501 of noes used during constant propagation. */
502
503 struct topo_info
504 {
505 struct cgraph_node **order;
506 struct cgraph_node **stack;
507 int nnodes, stack_top;
508 };
509
510 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
511
512 static void
513 build_toporder_info (struct topo_info *topo)
514 {
515 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
516 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
517 topo->stack_top = 0;
518 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
519 }
520
521 /* Free information about strongly connected components and the arrays in
522 TOPO. */
523
524 static void
525 free_toporder_info (struct topo_info *topo)
526 {
527 ipa_free_postorder_info ();
528 free (topo->order);
529 free (topo->stack);
530 }
531
532 /* Add NODE to the stack in TOPO, unless it is already there. */
533
534 static inline void
535 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
536 {
537 struct ipa_node_params *info = IPA_NODE_REF (node);
538 if (info->node_enqueued)
539 return;
540 info->node_enqueued = 1;
541 topo->stack[topo->stack_top++] = node;
542 }
543
544 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
545 is empty. */
546
547 static struct cgraph_node *
548 pop_node_from_stack (struct topo_info *topo)
549 {
550 if (topo->stack_top)
551 {
552 struct cgraph_node *node;
553 topo->stack_top--;
554 node = topo->stack[topo->stack_top];
555 IPA_NODE_REF (node)->node_enqueued = 0;
556 return node;
557 }
558 else
559 return NULL;
560 }
561
562 /* Set lattice LAT to bottom and return true if it previously was not set as
563 such. */
564
565 static inline bool
566 set_lattice_to_bottom (struct ipcp_lattice *lat)
567 {
568 bool ret = !lat->bottom;
569 lat->bottom = true;
570 return ret;
571 }
572
573 /* Mark lattice as containing an unknown value and return true if it previously
574 was not marked as such. */
575
576 static inline bool
577 set_lattice_contains_variable (struct ipcp_lattice *lat)
578 {
579 bool ret = !lat->contains_variable;
580 lat->contains_variable = true;
581 return ret;
582 }
583
584 /* Initialize ipcp_lattices. */
585
586 static void
587 initialize_node_lattices (struct cgraph_node *node)
588 {
589 struct ipa_node_params *info = IPA_NODE_REF (node);
590 struct cgraph_edge *ie;
591 bool disable = false, variable = false;
592 int i;
593
594 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
595 if (!node->local.local)
596 {
597 /* When cloning is allowed, we can assume that externally visible
598 functions are not called. We will compensate this by cloning
599 later. */
600 if (ipcp_versionable_function_p (node)
601 && ipcp_cloning_candidate_p (node))
602 variable = true;
603 else
604 disable = true;
605 }
606
607 if (disable || variable)
608 {
609 for (i = 0; i < ipa_get_param_count (info) ; i++)
610 {
611 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
612 if (disable)
613 set_lattice_to_bottom (lat);
614 else
615 set_lattice_contains_variable (lat);
616 }
617 if (dump_file && (dump_flags & TDF_DETAILS)
618 && node->alias && node->thunk.thunk_p)
619 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
620 cgraph_node_name (node), node->uid,
621 disable ? "BOTTOM" : "VARIABLE");
622 }
623
624 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
625 if (ie->indirect_info->polymorphic)
626 {
627 gcc_checking_assert (ie->indirect_info->param_index >= 0);
628 ipa_get_lattice (info, ie->indirect_info->param_index)->virt_call = 1;
629 }
630 }
631
632 /* Return the result of a (possibly arithmetic) pass through jump function
633 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
634 determined or itself is considered an interprocedural invariant. */
635
636 static tree
637 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
638 {
639 tree restype, res;
640
641 gcc_checking_assert (is_gimple_ip_invariant (input));
642 if (jfunc->value.pass_through.operation == NOP_EXPR)
643 return input;
644
645 if (TREE_CODE_CLASS (jfunc->value.pass_through.operation)
646 == tcc_comparison)
647 restype = boolean_type_node;
648 else
649 restype = TREE_TYPE (input);
650 res = fold_binary (jfunc->value.pass_through.operation, restype,
651 input, jfunc->value.pass_through.operand);
652
653 if (res && !is_gimple_ip_invariant (res))
654 return NULL_TREE;
655
656 return res;
657 }
658
659 /* Return the result of an ancestor jump function JFUNC on the constant value
660 INPUT. Return NULL_TREE if that cannot be determined. */
661
662 static tree
663 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
664 {
665 if (TREE_CODE (input) == ADDR_EXPR)
666 {
667 tree t = TREE_OPERAND (input, 0);
668 t = build_ref_for_offset (EXPR_LOCATION (t), t,
669 jfunc->value.ancestor.offset,
670 jfunc->value.ancestor.type, NULL, false);
671 return build_fold_addr_expr (t);
672 }
673 else
674 return NULL_TREE;
675 }
676
677 /* Determine whether JFUNC evaluates to a known value (that is either a
678 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
679 describes the caller node so that pass-through jump functions can be
680 evaluated. */
681
682 static tree
683 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
684 {
685 if (jfunc->type == IPA_JF_CONST)
686 return jfunc->value.constant;
687 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
688 return jfunc->value.base_binfo;
689 else if (jfunc->type == IPA_JF_PASS_THROUGH
690 || jfunc->type == IPA_JF_ANCESTOR)
691 {
692 tree input;
693 int idx;
694
695 if (jfunc->type == IPA_JF_PASS_THROUGH)
696 idx = jfunc->value.pass_through.formal_id;
697 else
698 idx = jfunc->value.ancestor.formal_id;
699
700 if (info->ipcp_orig_node)
701 input = VEC_index (tree, info->known_vals, idx);
702 else
703 {
704 struct ipcp_lattice *lat;
705
706 if (!info->lattices)
707 {
708 gcc_checking_assert (!flag_ipa_cp);
709 return NULL_TREE;
710 }
711 lat = ipa_get_lattice (info, idx);
712 if (!ipa_lat_is_single_const (lat))
713 return NULL_TREE;
714 input = lat->values->value;
715 }
716
717 if (!input)
718 return NULL_TREE;
719
720 if (jfunc->type == IPA_JF_PASS_THROUGH)
721 {
722 if (jfunc->value.pass_through.operation == NOP_EXPR)
723 return input;
724 else if (TREE_CODE (input) == TREE_BINFO)
725 return NULL_TREE;
726 else
727 return ipa_get_jf_pass_through_result (jfunc, input);
728 }
729 else
730 {
731 if (TREE_CODE (input) == TREE_BINFO)
732 return get_binfo_at_offset (input, jfunc->value.ancestor.offset,
733 jfunc->value.ancestor.type);
734 else
735 return ipa_get_jf_ancestor_result (jfunc, input);
736 }
737 }
738 else
739 return NULL_TREE;
740 }
741
742 /* Determine whether JFUNC evaluates to a constant and if so, return it.
743 Otherwise return NULL. INFO describes the caller node so that pass-through
744 jump functions can be evaluated. */
745
746 tree
747 ipa_cst_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
748 {
749 tree res = ipa_value_from_jfunc (info, jfunc);
750
751 if (res && TREE_CODE (res) == TREE_BINFO)
752 return NULL_TREE;
753 else
754 return res;
755 }
756
757
758 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
759 bottom, not containing a variable component and without any known value at
760 the same time. */
761
762 DEBUG_FUNCTION void
763 ipcp_verify_propagated_values (void)
764 {
765 struct cgraph_node *node;
766
767 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
768 {
769 struct ipa_node_params *info = IPA_NODE_REF (node);
770 int i, count = ipa_get_param_count (info);
771
772 for (i = 0; i < count; i++)
773 {
774 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
775
776 if (!lat->bottom
777 && !lat->contains_variable
778 && lat->values_count == 0)
779 {
780 if (dump_file)
781 {
782 fprintf (dump_file, "\nIPA lattices after constant "
783 "propagation:\n");
784 print_all_lattices (dump_file, true, false);
785 }
786
787 gcc_unreachable ();
788 }
789 }
790 }
791 }
792
793 /* Return true iff X and Y should be considered equal values by IPA-CP. */
794
795 static bool
796 values_equal_for_ipcp_p (tree x, tree y)
797 {
798 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
799
800 if (x == y)
801 return true;
802
803 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
804 return false;
805
806 if (TREE_CODE (x) == ADDR_EXPR
807 && TREE_CODE (y) == ADDR_EXPR
808 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
809 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
810 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
811 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
812 else
813 return operand_equal_p (x, y, 0);
814 }
815
816 /* Add a new value source to VAL, marking that a value comes from edge CS and
817 (if the underlying jump function is a pass-through or an ancestor one) from
818 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. */
819
820 static void
821 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
822 struct ipcp_value *src_val, int src_idx)
823 {
824 struct ipcp_value_source *src;
825
826 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
827 src->cs = cs;
828 src->val = src_val;
829 src->index = src_idx;
830
831 src->next = val->sources;
832 val->sources = src;
833 }
834
835
836 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
837 it. CS, SRC_VAL and SRC_INDEX are meant for add_value_source and have the
838 same meaning. */
839
840 static bool
841 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
842 struct cgraph_edge *cs, struct ipcp_value *src_val,
843 int src_idx)
844 {
845 struct ipcp_value *val;
846
847 if (lat->bottom)
848 return false;
849
850
851 for (val = lat->values; val; val = val->next)
852 if (values_equal_for_ipcp_p (val->value, newval))
853 {
854 if (edge_within_scc (cs))
855 {
856 struct ipcp_value_source *s;
857 for (s = val->sources; s ; s = s->next)
858 if (s->cs == cs)
859 break;
860 if (s)
861 return false;
862 }
863
864 add_value_source (val, cs, src_val, src_idx);
865 return false;
866 }
867
868 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
869 {
870 /* We can only free sources, not the values themselves, because sources
871 of other values in this this SCC might point to them. */
872 for (val = lat->values; val; val = val->next)
873 {
874 while (val->sources)
875 {
876 struct ipcp_value_source *src = val->sources;
877 val->sources = src->next;
878 pool_free (ipcp_sources_pool, src);
879 }
880 }
881
882 lat->values = NULL;
883 return set_lattice_to_bottom (lat);
884 }
885
886 lat->values_count++;
887 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
888 memset (val, 0, sizeof (*val));
889
890 add_value_source (val, cs, src_val, src_idx);
891 val->value = newval;
892 val->next = lat->values;
893 lat->values = val;
894 return true;
895 }
896
897 /* Propagate values through a pass-through jump function JFUNC associated with
898 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
899 is the index of the source parameter. */
900
901 static bool
902 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
903 struct ipa_jump_func *jfunc,
904 struct ipcp_lattice *src_lat,
905 struct ipcp_lattice *dest_lat,
906 int src_idx)
907 {
908 struct ipcp_value *src_val;
909 bool ret = false;
910
911 if (jfunc->value.pass_through.operation == NOP_EXPR)
912 for (src_val = src_lat->values; src_val; src_val = src_val->next)
913 ret |= add_value_to_lattice (dest_lat, src_val->value, cs,
914 src_val, src_idx);
915 /* Do not create new values when propagating within an SCC because if there
916 arithmetic functions with circular dependencies, there is infinite number
917 of them and we would just make lattices bottom. */
918 else if (edge_within_scc (cs))
919 ret = set_lattice_contains_variable (dest_lat);
920 else
921 for (src_val = src_lat->values; src_val; src_val = src_val->next)
922 {
923 tree cstval = src_val->value;
924
925 if (TREE_CODE (cstval) == TREE_BINFO)
926 {
927 ret |= set_lattice_contains_variable (dest_lat);
928 continue;
929 }
930 cstval = ipa_get_jf_pass_through_result (jfunc, cstval);
931
932 if (cstval)
933 ret |= add_value_to_lattice (dest_lat, cstval, cs, src_val, src_idx);
934 else
935 ret |= set_lattice_contains_variable (dest_lat);
936 }
937
938 return ret;
939 }
940
941 /* Propagate values through an ancestor jump function JFUNC associated with
942 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
943 is the index of the source parameter. */
944
945 static bool
946 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
947 struct ipa_jump_func *jfunc,
948 struct ipcp_lattice *src_lat,
949 struct ipcp_lattice *dest_lat,
950 int src_idx)
951 {
952 struct ipcp_value *src_val;
953 bool ret = false;
954
955 if (edge_within_scc (cs))
956 return set_lattice_contains_variable (dest_lat);
957
958 for (src_val = src_lat->values; src_val; src_val = src_val->next)
959 {
960 tree t = src_val->value;
961
962 if (TREE_CODE (t) == TREE_BINFO)
963 t = get_binfo_at_offset (t, jfunc->value.ancestor.offset,
964 jfunc->value.ancestor.type);
965 else
966 t = ipa_get_jf_ancestor_result (jfunc, t);
967
968 if (t)
969 ret |= add_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
970 else
971 ret |= set_lattice_contains_variable (dest_lat);
972 }
973
974 return ret;
975 }
976
977 /* Propagate values across jump function JFUNC that is associated with edge CS
978 and put the values into DEST_LAT. */
979
980 static bool
981 propagate_accross_jump_function (struct cgraph_edge *cs,
982 struct ipa_jump_func *jfunc,
983 struct ipcp_lattice *dest_lat)
984 {
985 if (dest_lat->bottom)
986 return false;
987
988 if (jfunc->type == IPA_JF_CONST
989 || jfunc->type == IPA_JF_KNOWN_TYPE)
990 {
991 tree val;
992
993 if (jfunc->type == IPA_JF_KNOWN_TYPE)
994 val = jfunc->value.base_binfo;
995 else
996 val = jfunc->value.constant;
997 return add_value_to_lattice (dest_lat, val, cs, NULL, 0);
998 }
999 else if (jfunc->type == IPA_JF_PASS_THROUGH
1000 || jfunc->type == IPA_JF_ANCESTOR)
1001 {
1002 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1003 struct ipcp_lattice *src_lat;
1004 int src_idx;
1005 bool ret;
1006
1007 if (jfunc->type == IPA_JF_PASS_THROUGH)
1008 src_idx = jfunc->value.pass_through.formal_id;
1009 else
1010 src_idx = jfunc->value.ancestor.formal_id;
1011
1012 src_lat = ipa_get_lattice (caller_info, src_idx);
1013 if (src_lat->bottom)
1014 return set_lattice_contains_variable (dest_lat);
1015
1016 /* If we would need to clone the caller and cannot, do not propagate. */
1017 if (!ipcp_versionable_function_p (cs->caller)
1018 && (src_lat->contains_variable
1019 || (src_lat->values_count > 1)))
1020 return set_lattice_contains_variable (dest_lat);
1021
1022 if (jfunc->type == IPA_JF_PASS_THROUGH)
1023 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1024 dest_lat, src_idx);
1025 else
1026 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1027 src_idx);
1028
1029 if (src_lat->contains_variable)
1030 ret |= set_lattice_contains_variable (dest_lat);
1031
1032 return ret;
1033 }
1034
1035 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1036 use it for indirect inlining), we should propagate them too. */
1037 return set_lattice_contains_variable (dest_lat);
1038 }
1039
1040 /* Propagate constants from the caller to the callee of CS. INFO describes the
1041 caller. */
1042
1043 static bool
1044 propagate_constants_accross_call (struct cgraph_edge *cs)
1045 {
1046 struct ipa_node_params *callee_info;
1047 enum availability availability;
1048 struct cgraph_node *callee, *alias_or_thunk;
1049 struct ipa_edge_args *args;
1050 bool ret = false;
1051 int i, args_count, parms_count;
1052
1053 callee = cgraph_function_node (cs->callee, &availability);
1054 if (!callee->analyzed)
1055 return false;
1056 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1057 callee_info = IPA_NODE_REF (callee);
1058
1059 args = IPA_EDGE_REF (cs);
1060 args_count = ipa_get_cs_argument_count (args);
1061 parms_count = ipa_get_param_count (callee_info);
1062
1063 /* If this call goes through a thunk we must not propagate to the first (0th)
1064 parameter. However, we might need to uncover a thunk from below a series
1065 of aliases first. */
1066 alias_or_thunk = cs->callee;
1067 while (alias_or_thunk->alias)
1068 alias_or_thunk = cgraph_alias_aliased_node (alias_or_thunk);
1069 if (alias_or_thunk->thunk.thunk_p)
1070 {
1071 ret |= set_lattice_contains_variable (ipa_get_lattice (callee_info, 0));
1072 i = 1;
1073 }
1074 else
1075 i = 0;
1076
1077 for (; (i < args_count) && (i < parms_count); i++)
1078 {
1079 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1080 struct ipcp_lattice *dest_lat = ipa_get_lattice (callee_info, i);
1081
1082 if (availability == AVAIL_OVERWRITABLE)
1083 ret |= set_lattice_contains_variable (dest_lat);
1084 else
1085 ret |= propagate_accross_jump_function (cs, jump_func, dest_lat);
1086 }
1087 for (; i < parms_count; i++)
1088 ret |= set_lattice_contains_variable (ipa_get_lattice (callee_info, i));
1089
1090 return ret;
1091 }
1092
1093 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1094 (which can contain both constants and binfos) or KNOWN_BINFOS (which can be
1095 NULL) return the destination. */
1096
1097 static tree
1098 get_indirect_edge_target (struct cgraph_edge *ie,
1099 VEC (tree, heap) *known_vals,
1100 VEC (tree, heap) *known_binfos)
1101 {
1102 int param_index = ie->indirect_info->param_index;
1103 HOST_WIDE_INT token, anc_offset;
1104 tree otr_type;
1105 tree t;
1106
1107 if (param_index == -1)
1108 return NULL_TREE;
1109
1110 if (!ie->indirect_info->polymorphic)
1111 {
1112 tree t = VEC_index (tree, known_vals, param_index);
1113 if (t &&
1114 TREE_CODE (t) == ADDR_EXPR
1115 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1116 return TREE_OPERAND (t, 0);
1117 else
1118 return NULL_TREE;
1119 }
1120
1121 token = ie->indirect_info->otr_token;
1122 anc_offset = ie->indirect_info->anc_offset;
1123 otr_type = ie->indirect_info->otr_type;
1124
1125 t = VEC_index (tree, known_vals, param_index);
1126 if (!t && known_binfos)
1127 t = VEC_index (tree, known_binfos, param_index);
1128 if (!t)
1129 return NULL_TREE;
1130
1131 if (TREE_CODE (t) != TREE_BINFO)
1132 {
1133 tree binfo;
1134 binfo = gimple_extract_devirt_binfo_from_cst (t);
1135 if (!binfo)
1136 return NULL_TREE;
1137 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1138 if (!binfo)
1139 return NULL_TREE;
1140 return gimple_get_virt_method_for_binfo (token, binfo);
1141 }
1142 else
1143 {
1144 tree binfo;
1145
1146 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1147 if (!binfo)
1148 return NULL_TREE;
1149 return gimple_get_virt_method_for_binfo (token, binfo);
1150 }
1151 }
1152
1153 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1154 and KNOWN_BINFOS. */
1155
1156 static int
1157 devirtualization_time_bonus (struct cgraph_node *node,
1158 VEC (tree, heap) *known_csts,
1159 VEC (tree, heap) *known_binfos)
1160 {
1161 struct cgraph_edge *ie;
1162 int res = 0;
1163
1164 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1165 {
1166 struct cgraph_node *callee;
1167 struct inline_summary *isummary;
1168 tree target;
1169
1170 target = get_indirect_edge_target (ie, known_csts, known_binfos);
1171 if (!target)
1172 continue;
1173
1174 /* Only bare minimum benefit for clearly un-inlineable targets. */
1175 res += 1;
1176 callee = cgraph_get_node (target);
1177 if (!callee || !callee->analyzed)
1178 continue;
1179 isummary = inline_summary (callee);
1180 if (!isummary->inlinable)
1181 continue;
1182
1183 /* FIXME: The values below need re-considering and perhaps also
1184 integrating into the cost metrics, at lest in some very basic way. */
1185 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1186 res += 31;
1187 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1188 res += 15;
1189 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1190 || DECL_DECLARED_INLINE_P (callee->decl))
1191 res += 7;
1192 }
1193
1194 return res;
1195 }
1196
1197 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1198 and SIZE_COST and with the sum of frequencies of incoming edges to the
1199 potential new clone in FREQUENCIES. */
1200
1201 static bool
1202 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1203 int freq_sum, gcov_type count_sum, int size_cost)
1204 {
1205 if (time_benefit == 0
1206 || !flag_ipa_cp_clone
1207 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
1208 return false;
1209
1210 gcc_checking_assert (size_cost >= 0);
1211
1212 /* FIXME: These decisions need tuning. */
1213 if (max_count)
1214 {
1215 int evaluation, factor = (count_sum * 1000) / max_count;
1216
1217 evaluation = (time_benefit * factor) / size_cost;
1218
1219 if (dump_file && (dump_flags & TDF_DETAILS))
1220 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1221 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1222 ") -> evaluation: %i, threshold: %i\n",
1223 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1224 evaluation, 500);
1225
1226 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1227 }
1228 else
1229 {
1230 int evaluation = (time_benefit * freq_sum) / size_cost;
1231
1232 if (dump_file && (dump_flags & TDF_DETAILS))
1233 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1234 "size: %i, freq_sum: %i) -> evaluation: %i, threshold: %i\n",
1235 time_benefit, size_cost, freq_sum, evaluation,
1236 CGRAPH_FREQ_BASE /2);
1237
1238 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1239 }
1240 }
1241
1242
1243 /* Allocate KNOWN_CSTS and KNOWN_BINFOS and populate them with values of
1244 parameters that are known independent of the context. INFO describes the
1245 function. If REMOVABLE_PARAMS_COST is non-NULL, the movement cost of all
1246 removable parameters will be stored in it. */
1247
1248 static bool
1249 gather_context_independent_values (struct ipa_node_params *info,
1250 VEC (tree, heap) **known_csts,
1251 VEC (tree, heap) **known_binfos,
1252 int *removable_params_cost)
1253 {
1254 int i, count = ipa_get_param_count (info);
1255 bool ret = false;
1256
1257 *known_csts = NULL;
1258 *known_binfos = NULL;
1259 VEC_safe_grow_cleared (tree, heap, *known_csts, count);
1260 VEC_safe_grow_cleared (tree, heap, *known_binfos, count);
1261
1262 if (removable_params_cost)
1263 *removable_params_cost = 0;
1264
1265 for (i = 0; i < count ; i++)
1266 {
1267 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1268
1269 if (ipa_lat_is_single_const (lat))
1270 {
1271 struct ipcp_value *val = lat->values;
1272 if (TREE_CODE (val->value) != TREE_BINFO)
1273 {
1274 VEC_replace (tree, *known_csts, i, val->value);
1275 if (removable_params_cost)
1276 *removable_params_cost
1277 += estimate_move_cost (TREE_TYPE (val->value));
1278 ret = true;
1279 }
1280 else if (lat->virt_call)
1281 {
1282 VEC_replace (tree, *known_binfos, i, val->value);
1283 ret = true;
1284 }
1285 else if (removable_params_cost
1286 && !ipa_is_param_used (info, i))
1287 *removable_params_cost
1288 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1289 }
1290 else if (removable_params_cost
1291 && !ipa_is_param_used (info, i))
1292 *removable_params_cost
1293 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1294 }
1295
1296 return ret;
1297 }
1298
1299 /* Iterate over known values of parameters of NODE and estimate the local
1300 effects in terms of time and size they have. */
1301
1302 static void
1303 estimate_local_effects (struct cgraph_node *node)
1304 {
1305 struct ipa_node_params *info = IPA_NODE_REF (node);
1306 int i, count = ipa_get_param_count (info);
1307 VEC (tree, heap) *known_csts, *known_binfos;
1308 bool always_const;
1309 int base_time = inline_summary (node)->time;
1310 int removable_params_cost;
1311
1312 if (!count || !ipcp_versionable_function_p (node))
1313 return;
1314
1315 if (dump_file && (dump_flags & TDF_DETAILS))
1316 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1317 cgraph_node_name (node), node->uid, base_time);
1318
1319 always_const = gather_context_independent_values (info, &known_csts,
1320 &known_binfos,
1321 &removable_params_cost);
1322 if (always_const)
1323 {
1324 struct caller_statistics stats;
1325 int time, size;
1326
1327 init_caller_stats (&stats);
1328 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1329 estimate_ipcp_clone_size_and_time (node, known_csts, &size, &time);
1330 time -= devirtualization_time_bonus (node, known_csts, known_binfos);
1331 time -= removable_params_cost;
1332 size -= stats.n_calls * removable_params_cost;
1333
1334 if (dump_file)
1335 fprintf (dump_file, " - context independent values, size: %i, "
1336 "time_benefit: %i\n", size, base_time - time);
1337
1338 if (size <= 0
1339 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1340 {
1341 info->clone_for_all_contexts = true;
1342 base_time = time;
1343
1344 if (dump_file)
1345 fprintf (dump_file, " Decided to specialize for all "
1346 "known contexts, code not going to grow.\n");
1347 }
1348 else if (good_cloning_opportunity_p (node, base_time - time,
1349 stats.freq_sum, stats.count_sum,
1350 size))
1351 {
1352 if (size + overall_size <= max_new_size)
1353 {
1354 info->clone_for_all_contexts = true;
1355 base_time = time;
1356 overall_size += size;
1357
1358 if (dump_file)
1359 fprintf (dump_file, " Decided to specialize for all "
1360 "known contexts, growth deemed beneficial.\n");
1361 }
1362 else if (dump_file && (dump_flags & TDF_DETAILS))
1363 fprintf (dump_file, " Not cloning for all contexts because "
1364 "max_new_size would be reached with %li.\n",
1365 size + overall_size);
1366 }
1367 }
1368
1369 for (i = 0; i < count ; i++)
1370 {
1371 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1372 struct ipcp_value *val;
1373 int emc;
1374
1375 if (lat->bottom
1376 || !lat->values
1377 || VEC_index (tree, known_csts, i)
1378 || VEC_index (tree, known_binfos, i))
1379 continue;
1380
1381 for (val = lat->values; val; val = val->next)
1382 {
1383 int time, size, time_benefit;
1384
1385 if (TREE_CODE (val->value) != TREE_BINFO)
1386 {
1387 VEC_replace (tree, known_csts, i, val->value);
1388 VEC_replace (tree, known_binfos, i, NULL_TREE);
1389 emc = estimate_move_cost (TREE_TYPE (val->value));
1390 }
1391 else if (lat->virt_call)
1392 {
1393 VEC_replace (tree, known_csts, i, NULL_TREE);
1394 VEC_replace (tree, known_binfos, i, val->value);
1395 emc = 0;
1396 }
1397 else
1398 continue;
1399
1400 estimate_ipcp_clone_size_and_time (node, known_csts, &size, &time);
1401 time_benefit = base_time - time
1402 + devirtualization_time_bonus (node, known_csts, known_binfos)
1403 + removable_params_cost + emc;
1404
1405 if (dump_file && (dump_flags & TDF_DETAILS))
1406 {
1407 fprintf (dump_file, " - estimates for value ");
1408 print_ipcp_constant_value (dump_file, val->value);
1409 fprintf (dump_file, " for parameter ");
1410 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
1411 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
1412 time_benefit, size);
1413 }
1414
1415 val->local_time_benefit = time_benefit;
1416 val->local_size_cost = size;
1417 }
1418 }
1419
1420 VEC_free (tree, heap, known_csts);
1421 VEC_free (tree, heap, known_binfos);
1422 }
1423
1424
1425 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
1426 topological sort of values. */
1427
1428 static void
1429 add_val_to_toposort (struct ipcp_value *cur_val)
1430 {
1431 static int dfs_counter = 0;
1432 static struct ipcp_value *stack;
1433 struct ipcp_value_source *src;
1434
1435 if (cur_val->dfs)
1436 return;
1437
1438 dfs_counter++;
1439 cur_val->dfs = dfs_counter;
1440 cur_val->low_link = dfs_counter;
1441
1442 cur_val->topo_next = stack;
1443 stack = cur_val;
1444 cur_val->on_stack = true;
1445
1446 for (src = cur_val->sources; src; src = src->next)
1447 if (src->val)
1448 {
1449 if (src->val->dfs == 0)
1450 {
1451 add_val_to_toposort (src->val);
1452 if (src->val->low_link < cur_val->low_link)
1453 cur_val->low_link = src->val->low_link;
1454 }
1455 else if (src->val->on_stack
1456 && src->val->dfs < cur_val->low_link)
1457 cur_val->low_link = src->val->dfs;
1458 }
1459
1460 if (cur_val->dfs == cur_val->low_link)
1461 {
1462 struct ipcp_value *v, *scc_list = NULL;
1463
1464 do
1465 {
1466 v = stack;
1467 stack = v->topo_next;
1468 v->on_stack = false;
1469
1470 v->scc_next = scc_list;
1471 scc_list = v;
1472 }
1473 while (v != cur_val);
1474
1475 cur_val->topo_next = values_topo;
1476 values_topo = cur_val;
1477 }
1478 }
1479
1480 /* Add all values in lattices associated with NODE to the topological sort if
1481 they are not there yet. */
1482
1483 static void
1484 add_all_node_vals_to_toposort (struct cgraph_node *node)
1485 {
1486 struct ipa_node_params *info = IPA_NODE_REF (node);
1487 int i, count = ipa_get_param_count (info);
1488
1489 for (i = 0; i < count ; i++)
1490 {
1491 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1492 struct ipcp_value *val;
1493
1494 if (lat->bottom || !lat->values)
1495 continue;
1496 for (val = lat->values; val; val = val->next)
1497 add_val_to_toposort (val);
1498 }
1499 }
1500
1501 /* One pass of constants propagation along the call graph edges, from callers
1502 to callees (requires topological ordering in TOPO), iterate over strongly
1503 connected components. */
1504
1505 static void
1506 propagate_constants_topo (struct topo_info *topo)
1507 {
1508 int i;
1509
1510 for (i = topo->nnodes - 1; i >= 0; i--)
1511 {
1512 struct cgraph_node *v, *node = topo->order[i];
1513 struct ipa_dfs_info *node_dfs_info;
1514
1515 if (!cgraph_function_with_gimple_body_p (node))
1516 continue;
1517
1518 node_dfs_info = (struct ipa_dfs_info *) node->aux;
1519 /* First, iteratively propagate within the strongly connected component
1520 until all lattices stabilize. */
1521 v = node_dfs_info->next_cycle;
1522 while (v)
1523 {
1524 push_node_to_stack (topo, v);
1525 v = ((struct ipa_dfs_info *) v->aux)->next_cycle;
1526 }
1527
1528 v = node;
1529 while (v)
1530 {
1531 struct cgraph_edge *cs;
1532
1533 for (cs = v->callees; cs; cs = cs->next_callee)
1534 if (edge_within_scc (cs)
1535 && propagate_constants_accross_call (cs))
1536 push_node_to_stack (topo, cs->callee);
1537 v = pop_node_from_stack (topo);
1538 }
1539
1540 /* Afterwards, propagate along edges leading out of the SCC, calculates
1541 the local effects of the discovered constants and all valid values to
1542 their topological sort. */
1543 v = node;
1544 while (v)
1545 {
1546 struct cgraph_edge *cs;
1547
1548 estimate_local_effects (v);
1549 add_all_node_vals_to_toposort (v);
1550 for (cs = v->callees; cs; cs = cs->next_callee)
1551 if (!edge_within_scc (cs))
1552 propagate_constants_accross_call (cs);
1553
1554 v = ((struct ipa_dfs_info *) v->aux)->next_cycle;
1555 }
1556 }
1557 }
1558
1559 /* Propagate the estimated effects of individual values along the topological
1560 from the dependant values to those they depend on. */
1561
1562 static void
1563 propagate_effects (void)
1564 {
1565 struct ipcp_value *base;
1566
1567 for (base = values_topo; base; base = base->topo_next)
1568 {
1569 struct ipcp_value_source *src;
1570 struct ipcp_value *val;
1571 int time = 0, size = 0;
1572
1573 for (val = base; val; val = val->scc_next)
1574 {
1575 time += val->local_time_benefit + val->prop_time_benefit;
1576 size += val->local_size_cost + val->prop_size_cost;
1577 }
1578
1579 for (val = base; val; val = val->scc_next)
1580 for (src = val->sources; src; src = src->next)
1581 if (src->val
1582 && cgraph_maybe_hot_edge_p (src->cs))
1583 {
1584 src->val->prop_time_benefit += time;
1585 src->val->prop_size_cost += size;
1586 }
1587 }
1588 }
1589
1590
1591 /* Propagate constants, binfos and their effects from the summaries
1592 interprocedurally. */
1593
1594 static void
1595 ipcp_propagate_stage (struct topo_info *topo)
1596 {
1597 struct cgraph_node *node;
1598
1599 if (dump_file)
1600 fprintf (dump_file, "\n Propagating constants:\n\n");
1601
1602 if (in_lto_p)
1603 ipa_update_after_lto_read ();
1604
1605
1606 FOR_EACH_DEFINED_FUNCTION (node)
1607 {
1608 struct ipa_node_params *info = IPA_NODE_REF (node);
1609
1610 determine_versionability (node);
1611 if (cgraph_function_with_gimple_body_p (node))
1612 {
1613 info->lattices = XCNEWVEC (struct ipcp_lattice,
1614 ipa_get_param_count (info));
1615 initialize_node_lattices (node);
1616 }
1617 if (node->count > max_count)
1618 max_count = node->count;
1619 overall_size += inline_summary (node)->self_size;
1620 }
1621
1622 max_new_size = overall_size;
1623 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1624 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1625 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1626
1627 if (dump_file)
1628 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
1629 overall_size, max_new_size);
1630
1631 propagate_constants_topo (topo);
1632 #ifdef ENABLE_CHECKING
1633 ipcp_verify_propagated_values ();
1634 #endif
1635 propagate_effects ();
1636
1637 if (dump_file)
1638 {
1639 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
1640 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
1641 }
1642 }
1643
1644 /* Discover newly direct outgoing edges from NODE which is a new clone with
1645 known KNOWN_VALS and make them direct. */
1646
1647 static void
1648 ipcp_discover_new_direct_edges (struct cgraph_node *node,
1649 VEC (tree, heap) *known_vals)
1650 {
1651 struct cgraph_edge *ie, *next_ie;
1652
1653 for (ie = node->indirect_calls; ie; ie = next_ie)
1654 {
1655 tree target;
1656
1657 next_ie = ie->next_callee;
1658 target = get_indirect_edge_target (ie, known_vals, NULL);
1659 if (target)
1660 ipa_make_edge_direct_to_target (ie, target);
1661 }
1662 }
1663
1664 /* Vector of pointers which for linked lists of clones of an original crgaph
1665 edge. */
1666
1667 static VEC (cgraph_edge_p, heap) *next_edge_clone;
1668
1669 static inline void
1670 grow_next_edge_clone_vector (void)
1671 {
1672 if (VEC_length (cgraph_edge_p, next_edge_clone)
1673 <= (unsigned) cgraph_edge_max_uid)
1674 VEC_safe_grow_cleared (cgraph_edge_p, heap, next_edge_clone,
1675 cgraph_edge_max_uid + 1);
1676 }
1677
1678 /* Edge duplication hook to grow the appropriate linked list in
1679 next_edge_clone. */
1680
1681 static void
1682 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1683 __attribute__((unused)) void *data)
1684 {
1685 grow_next_edge_clone_vector ();
1686 VEC_replace (cgraph_edge_p, next_edge_clone, dst->uid,
1687 VEC_index (cgraph_edge_p, next_edge_clone, src->uid));
1688 VEC_replace (cgraph_edge_p, next_edge_clone, src->uid, dst);
1689 }
1690
1691 /* Get the next clone in the linked list of clones of an edge. */
1692
1693 static inline struct cgraph_edge *
1694 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
1695 {
1696 return VEC_index (cgraph_edge_p, next_edge_clone, cs->uid);
1697 }
1698
1699 /* Return true if edge CS does bring about the value described by SRC. */
1700
1701 static bool
1702 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
1703 struct ipcp_value_source *src)
1704 {
1705 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1706
1707 if (IPA_NODE_REF (cs->callee)->ipcp_orig_node
1708 || caller_info->node_dead)
1709 return false;
1710 if (!src->val)
1711 return true;
1712
1713 if (caller_info->ipcp_orig_node)
1714 {
1715 tree t = VEC_index (tree, caller_info->known_vals, src->index);
1716 return (t != NULL_TREE
1717 && values_equal_for_ipcp_p (src->val->value, t));
1718 }
1719 else
1720 {
1721 struct ipcp_lattice *lat = ipa_get_lattice (caller_info, src->index);
1722 if (ipa_lat_is_single_const (lat)
1723 && values_equal_for_ipcp_p (src->val->value, lat->values->value))
1724 return true;
1725 else
1726 return false;
1727 }
1728 }
1729
1730 /* Given VAL, iterate over all its sources and if they still hold, add their
1731 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
1732 respectively. */
1733
1734 static bool
1735 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
1736 gcov_type *count_sum, int *caller_count)
1737 {
1738 struct ipcp_value_source *src;
1739 int freq = 0, count = 0;
1740 gcov_type cnt = 0;
1741 bool hot = false;
1742
1743 for (src = val->sources; src; src = src->next)
1744 {
1745 struct cgraph_edge *cs = src->cs;
1746 while (cs)
1747 {
1748 if (cgraph_edge_brings_value_p (cs, src))
1749 {
1750 count++;
1751 freq += cs->frequency;
1752 cnt += cs->count;
1753 hot |= cgraph_maybe_hot_edge_p (cs);
1754 }
1755 cs = get_next_cgraph_edge_clone (cs);
1756 }
1757 }
1758
1759 *freq_sum = freq;
1760 *count_sum = cnt;
1761 *caller_count = count;
1762 return hot;
1763 }
1764
1765 /* Return a vector of incoming edges that do bring value VAL. It is assumed
1766 their number is known and equal to CALLER_COUNT. */
1767
1768 static VEC (cgraph_edge_p,heap) *
1769 gather_edges_for_value (struct ipcp_value *val, int caller_count)
1770 {
1771 struct ipcp_value_source *src;
1772 VEC (cgraph_edge_p,heap) *ret;
1773
1774 ret = VEC_alloc (cgraph_edge_p, heap, caller_count);
1775 for (src = val->sources; src; src = src->next)
1776 {
1777 struct cgraph_edge *cs = src->cs;
1778 while (cs)
1779 {
1780 if (cgraph_edge_brings_value_p (cs, src))
1781 VEC_quick_push (cgraph_edge_p, ret, cs);
1782 cs = get_next_cgraph_edge_clone (cs);
1783 }
1784 }
1785
1786 return ret;
1787 }
1788
1789 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
1790 Return it or NULL if for some reason it cannot be created. */
1791
1792 static struct ipa_replace_map *
1793 get_replacement_map (tree value, tree parm)
1794 {
1795 tree req_type = TREE_TYPE (parm);
1796 struct ipa_replace_map *replace_map;
1797
1798 if (!useless_type_conversion_p (req_type, TREE_TYPE (value)))
1799 {
1800 if (fold_convertible_p (req_type, value))
1801 value = fold_build1 (NOP_EXPR, req_type, value);
1802 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (value)))
1803 value = fold_build1 (VIEW_CONVERT_EXPR, req_type, value);
1804 else
1805 {
1806 if (dump_file)
1807 {
1808 fprintf (dump_file, " const ");
1809 print_generic_expr (dump_file, value, 0);
1810 fprintf (dump_file, " can't be converted to param ");
1811 print_generic_expr (dump_file, parm, 0);
1812 fprintf (dump_file, "\n");
1813 }
1814 return NULL;
1815 }
1816 }
1817
1818 replace_map = ggc_alloc_ipa_replace_map ();
1819 if (dump_file)
1820 {
1821 fprintf (dump_file, " replacing param ");
1822 print_generic_expr (dump_file, parm, 0);
1823 fprintf (dump_file, " with const ");
1824 print_generic_expr (dump_file, value, 0);
1825 fprintf (dump_file, "\n");
1826 }
1827 replace_map->old_tree = parm;
1828 replace_map->new_tree = value;
1829 replace_map->replace_p = true;
1830 replace_map->ref_p = false;
1831
1832 return replace_map;
1833 }
1834
1835 /* Dump new profiling counts */
1836
1837 static void
1838 dump_profile_updates (struct cgraph_node *orig_node,
1839 struct cgraph_node *new_node)
1840 {
1841 struct cgraph_edge *cs;
1842
1843 fprintf (dump_file, " setting count of the specialized node to "
1844 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
1845 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1846 fprintf (dump_file, " edge to %s has count "
1847 HOST_WIDE_INT_PRINT_DEC "\n",
1848 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
1849
1850 fprintf (dump_file, " setting count of the original node to "
1851 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
1852 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1853 fprintf (dump_file, " edge to %s is left with "
1854 HOST_WIDE_INT_PRINT_DEC "\n",
1855 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
1856 }
1857
1858 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
1859 their profile information to reflect this. */
1860
1861 static void
1862 update_profiling_info (struct cgraph_node *orig_node,
1863 struct cgraph_node *new_node)
1864 {
1865 struct cgraph_edge *cs;
1866 struct caller_statistics stats;
1867 gcov_type new_sum, orig_sum;
1868 gcov_type remainder, orig_node_count = orig_node->count;
1869
1870 if (orig_node_count == 0)
1871 return;
1872
1873 init_caller_stats (&stats);
1874 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
1875 orig_sum = stats.count_sum;
1876 init_caller_stats (&stats);
1877 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
1878 new_sum = stats.count_sum;
1879
1880 if (orig_node_count < orig_sum + new_sum)
1881 {
1882 if (dump_file)
1883 fprintf (dump_file, " Problem: node %s/%i has too low count "
1884 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
1885 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
1886 cgraph_node_name (orig_node), orig_node->uid,
1887 (HOST_WIDE_INT) orig_node_count,
1888 (HOST_WIDE_INT) (orig_sum + new_sum));
1889
1890 orig_node_count = (orig_sum + new_sum) * 12 / 10;
1891 if (dump_file)
1892 fprintf (dump_file, " proceeding by pretending it was "
1893 HOST_WIDE_INT_PRINT_DEC "\n",
1894 (HOST_WIDE_INT) orig_node_count);
1895 }
1896
1897 new_node->count = new_sum;
1898 remainder = orig_node_count - new_sum;
1899 orig_node->count = remainder;
1900
1901 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1902 if (cs->frequency)
1903 cs->count = cs->count * (new_sum * REG_BR_PROB_BASE
1904 / orig_node_count) / REG_BR_PROB_BASE;
1905 else
1906 cs->count = 0;
1907
1908 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1909 cs->count = cs->count * (remainder * REG_BR_PROB_BASE
1910 / orig_node_count) / REG_BR_PROB_BASE;
1911
1912 if (dump_file)
1913 dump_profile_updates (orig_node, new_node);
1914 }
1915
1916 /* Update the respective profile of specialized NEW_NODE and the original
1917 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
1918 have been redirected to the specialized version. */
1919
1920 static void
1921 update_specialized_profile (struct cgraph_node *new_node,
1922 struct cgraph_node *orig_node,
1923 gcov_type redirected_sum)
1924 {
1925 struct cgraph_edge *cs;
1926 gcov_type new_node_count, orig_node_count = orig_node->count;
1927
1928 if (dump_file)
1929 fprintf (dump_file, " the sum of counts of redirected edges is "
1930 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
1931 if (orig_node_count == 0)
1932 return;
1933
1934 gcc_assert (orig_node_count >= redirected_sum);
1935
1936 new_node_count = new_node->count;
1937 new_node->count += redirected_sum;
1938 orig_node->count -= redirected_sum;
1939
1940 for (cs = new_node->callees; cs ; cs = cs->next_callee)
1941 if (cs->frequency)
1942 cs->count += cs->count * redirected_sum / new_node_count;
1943 else
1944 cs->count = 0;
1945
1946 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
1947 {
1948 gcov_type dec = cs->count * (redirected_sum * REG_BR_PROB_BASE
1949 / orig_node_count) / REG_BR_PROB_BASE;
1950 if (dec < cs->count)
1951 cs->count -= dec;
1952 else
1953 cs->count = 0;
1954 }
1955
1956 if (dump_file)
1957 dump_profile_updates (orig_node, new_node);
1958 }
1959
1960 /* Create a specialized version of NODE with known constants and types of
1961 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
1962
1963 static struct cgraph_node *
1964 create_specialized_node (struct cgraph_node *node,
1965 VEC (tree, heap) *known_vals,
1966 VEC (cgraph_edge_p,heap) *callers)
1967 {
1968 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
1969 VEC (ipa_replace_map_p,gc)* replace_trees = NULL;
1970 struct cgraph_node *new_node;
1971 int i, count = ipa_get_param_count (info);
1972 bitmap args_to_skip;
1973
1974 gcc_assert (!info->ipcp_orig_node);
1975
1976 if (node->local.can_change_signature)
1977 {
1978 args_to_skip = BITMAP_GGC_ALLOC ();
1979 for (i = 0; i < count; i++)
1980 {
1981 tree t = VEC_index (tree, known_vals, i);
1982
1983 if ((t && TREE_CODE (t) != TREE_BINFO)
1984 || !ipa_is_param_used (info, i))
1985 bitmap_set_bit (args_to_skip, i);
1986 }
1987 }
1988 else
1989 {
1990 args_to_skip = NULL;
1991 if (dump_file && (dump_flags & TDF_DETAILS))
1992 fprintf (dump_file, " cannot change function signature\n");
1993 }
1994
1995 for (i = 0; i < count ; i++)
1996 {
1997 tree t = VEC_index (tree, known_vals, i);
1998 if (t && TREE_CODE (t) != TREE_BINFO)
1999 {
2000 struct ipa_replace_map *replace_map;
2001
2002 replace_map = get_replacement_map (t, ipa_get_param (info, i));
2003 if (replace_map)
2004 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_map);
2005 }
2006 }
2007
2008 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2009 args_to_skip, "constprop");
2010 if (dump_file && (dump_flags & TDF_DETAILS))
2011 fprintf (dump_file, " the new node is %s/%i.\n",
2012 cgraph_node_name (new_node), new_node->uid);
2013 gcc_checking_assert (ipa_node_params_vector
2014 && (VEC_length (ipa_node_params_t,
2015 ipa_node_params_vector)
2016 > (unsigned) cgraph_max_uid));
2017 update_profiling_info (node, new_node);
2018 new_info = IPA_NODE_REF (new_node);
2019 new_info->ipcp_orig_node = node;
2020 new_info->known_vals = known_vals;
2021
2022 ipcp_discover_new_direct_edges (new_node, known_vals);
2023
2024 VEC_free (cgraph_edge_p, heap, callers);
2025 return new_node;
2026 }
2027
2028 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2029 KNOWN_VALS with constants and types that are also known for all of the
2030 CALLERS. */
2031
2032 static void
2033 find_more_values_for_callers_subset (struct cgraph_node *node,
2034 VEC (tree, heap) *known_vals,
2035 VEC (cgraph_edge_p,heap) *callers)
2036 {
2037 struct ipa_node_params *info = IPA_NODE_REF (node);
2038 int i, count = ipa_get_param_count (info);
2039
2040 for (i = 0; i < count ; i++)
2041 {
2042 struct cgraph_edge *cs;
2043 tree newval = NULL_TREE;
2044 int j;
2045
2046 if (ipa_get_lattice (info, i)->bottom
2047 || VEC_index (tree, known_vals, i))
2048 continue;
2049
2050 FOR_EACH_VEC_ELT (cgraph_edge_p, callers, j, cs)
2051 {
2052 struct ipa_jump_func *jump_func;
2053 tree t;
2054
2055 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2056 {
2057 newval = NULL_TREE;
2058 break;
2059 }
2060 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2061 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2062 if (!t
2063 || (newval
2064 && !values_equal_for_ipcp_p (t, newval)))
2065 {
2066 newval = NULL_TREE;
2067 break;
2068 }
2069 else
2070 newval = t;
2071 }
2072
2073 if (newval)
2074 {
2075 if (dump_file && (dump_flags & TDF_DETAILS))
2076 {
2077 fprintf (dump_file, " adding an extra known value ");
2078 print_ipcp_constant_value (dump_file, newval);
2079 fprintf (dump_file, " for parameter ");
2080 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2081 fprintf (dump_file, "\n");
2082 }
2083
2084 VEC_replace (tree, known_vals, i, newval);
2085 }
2086 }
2087 }
2088
2089 /* Given an original NODE and a VAL for which we have already created a
2090 specialized clone, look whether there are incoming edges that still lead
2091 into the old node but now also bring the requested value and also conform to
2092 all other criteria such that they can be redirected the the special node.
2093 This function can therefore redirect the final edge in a SCC. */
2094
2095 static void
2096 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
2097 {
2098 struct ipa_node_params *dest_info = IPA_NODE_REF (val->spec_node);
2099 struct ipcp_value_source *src;
2100 int count = ipa_get_param_count (dest_info);
2101 gcov_type redirected_sum = 0;
2102
2103 for (src = val->sources; src; src = src->next)
2104 {
2105 struct cgraph_edge *cs = src->cs;
2106 while (cs)
2107 {
2108 enum availability availability;
2109 bool insufficient = false;
2110
2111 if (cgraph_function_node (cs->callee, &availability) == node
2112 && availability > AVAIL_OVERWRITABLE
2113 && cgraph_edge_brings_value_p (cs, src))
2114 {
2115 struct ipa_node_params *caller_info;
2116 struct ipa_edge_args *args;
2117 int i;
2118
2119 caller_info = IPA_NODE_REF (cs->caller);
2120 args = IPA_EDGE_REF (cs);
2121 for (i = 0; i < count; i++)
2122 {
2123 struct ipa_jump_func *jump_func;
2124 tree val, t;
2125
2126 val = VEC_index (tree, dest_info->known_vals, i);
2127 if (!val)
2128 continue;
2129
2130 if (i >= ipa_get_cs_argument_count (args))
2131 {
2132 insufficient = true;
2133 break;
2134 }
2135 jump_func = ipa_get_ith_jump_func (args, i);
2136 t = ipa_value_from_jfunc (caller_info, jump_func);
2137 if (!t || !values_equal_for_ipcp_p (val, t))
2138 {
2139 insufficient = true;
2140 break;
2141 }
2142 }
2143
2144 if (!insufficient)
2145 {
2146 if (dump_file)
2147 fprintf (dump_file, " - adding an extra caller %s/%i"
2148 " of %s/%i\n",
2149 cgraph_node_name (cs->caller), cs->caller->uid,
2150 cgraph_node_name (val->spec_node),
2151 val->spec_node->uid);
2152
2153 cgraph_redirect_edge_callee (cs, val->spec_node);
2154 redirected_sum += cs->count;
2155 }
2156 }
2157 cs = get_next_cgraph_edge_clone (cs);
2158 }
2159 }
2160
2161 if (redirected_sum)
2162 update_specialized_profile (val->spec_node, node, redirected_sum);
2163 }
2164
2165
2166 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
2167
2168 static void
2169 move_binfos_to_values (VEC (tree, heap) *known_vals,
2170 VEC (tree, heap) *known_binfos)
2171 {
2172 tree t;
2173 int i;
2174
2175 for (i = 0; VEC_iterate (tree, known_binfos, i, t); i++)
2176 if (t)
2177 VEC_replace (tree, known_vals, i, t);
2178 }
2179
2180
2181 /* Decide whether and what specialized clones of NODE should be created. */
2182
2183 static bool
2184 decide_whether_version_node (struct cgraph_node *node)
2185 {
2186 struct ipa_node_params *info = IPA_NODE_REF (node);
2187 int i, count = ipa_get_param_count (info);
2188 VEC (tree, heap) *known_csts, *known_binfos;
2189 bool ret = false;
2190
2191 if (count == 0)
2192 return false;
2193
2194 if (dump_file && (dump_flags & TDF_DETAILS))
2195 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
2196 cgraph_node_name (node), node->uid);
2197
2198 gather_context_independent_values (info, &known_csts, &known_binfos,
2199 NULL);
2200
2201 for (i = 0; i < count ; i++)
2202 {
2203 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
2204 struct ipcp_value *val;
2205
2206 if (lat->bottom
2207 || VEC_index (tree, known_csts, i)
2208 || VEC_index (tree, known_binfos, i))
2209 continue;
2210
2211 for (val = lat->values; val; val = val->next)
2212 {
2213 int freq_sum, caller_count;
2214 gcov_type count_sum;
2215 VEC (cgraph_edge_p, heap) *callers;
2216 VEC (tree, heap) *kv;
2217
2218 if (val->spec_node)
2219 {
2220 perhaps_add_new_callers (node, val);
2221 continue;
2222 }
2223 else if (val->local_size_cost + overall_size > max_new_size)
2224 {
2225 if (dump_file && (dump_flags & TDF_DETAILS))
2226 fprintf (dump_file, " Ignoring candidate value because "
2227 "max_new_size would be reached with %li.\n",
2228 val->local_size_cost + overall_size);
2229 continue;
2230 }
2231 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
2232 &caller_count))
2233 continue;
2234
2235 if (dump_file && (dump_flags & TDF_DETAILS))
2236 {
2237 fprintf (dump_file, " - considering value ");
2238 print_ipcp_constant_value (dump_file, val->value);
2239 fprintf (dump_file, " for parameter ");
2240 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2241 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
2242 }
2243
2244
2245 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
2246 freq_sum, count_sum,
2247 val->local_size_cost)
2248 && !good_cloning_opportunity_p (node,
2249 val->local_time_benefit
2250 + val->prop_time_benefit,
2251 freq_sum, count_sum,
2252 val->local_size_cost
2253 + val->prop_size_cost))
2254 continue;
2255
2256 if (dump_file)
2257 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
2258 cgraph_node_name (node), node->uid);
2259
2260 callers = gather_edges_for_value (val, caller_count);
2261 kv = VEC_copy (tree, heap, known_csts);
2262 move_binfos_to_values (kv, known_binfos);
2263 VEC_replace (tree, kv, i, val->value);
2264 find_more_values_for_callers_subset (node, kv, callers);
2265 val->spec_node = create_specialized_node (node, kv, callers);
2266 overall_size += val->local_size_cost;
2267 info = IPA_NODE_REF (node);
2268
2269 /* TODO: If for some lattice there is only one other known value
2270 left, make a special node for it too. */
2271 ret = true;
2272
2273 VEC_replace (tree, kv, i, val->value);
2274 }
2275 }
2276
2277 if (info->clone_for_all_contexts)
2278 {
2279 VEC (cgraph_edge_p, heap) *callers;
2280
2281 if (dump_file)
2282 fprintf (dump_file, " - Creating a specialized node of %s/%i "
2283 "for all known contexts.\n", cgraph_node_name (node),
2284 node->uid);
2285
2286 callers = collect_callers_of_node (node);
2287 move_binfos_to_values (known_csts, known_binfos);
2288 create_specialized_node (node, known_csts, callers);
2289 info = IPA_NODE_REF (node);
2290 info->clone_for_all_contexts = false;
2291 ret = true;
2292 }
2293 else
2294 VEC_free (tree, heap, known_csts);
2295
2296 VEC_free (tree, heap, known_binfos);
2297 return ret;
2298 }
2299
2300 /* Transitively mark all callees of NODE within the same SCC as not dead. */
2301
2302 static void
2303 spread_undeadness (struct cgraph_node *node)
2304 {
2305 struct cgraph_edge *cs;
2306
2307 for (cs = node->callees; cs; cs = cs->next_callee)
2308 if (edge_within_scc (cs))
2309 {
2310 struct cgraph_node *callee;
2311 struct ipa_node_params *info;
2312
2313 callee = cgraph_function_node (cs->callee, NULL);
2314 info = IPA_NODE_REF (callee);
2315
2316 if (info->node_dead)
2317 {
2318 info->node_dead = 0;
2319 spread_undeadness (callee);
2320 }
2321 }
2322 }
2323
2324 /* Return true if NODE has a caller from outside of its SCC that is not
2325 dead. Worker callback for cgraph_for_node_and_aliases. */
2326
2327 static bool
2328 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
2329 void *data ATTRIBUTE_UNUSED)
2330 {
2331 struct cgraph_edge *cs;
2332
2333 for (cs = node->callers; cs; cs = cs->next_caller)
2334 if (cs->caller->thunk.thunk_p
2335 && cgraph_for_node_and_aliases (cs->caller,
2336 has_undead_caller_from_outside_scc_p,
2337 NULL, true))
2338 return true;
2339 else if (!edge_within_scc (cs)
2340 && !IPA_NODE_REF (cs->caller)->node_dead)
2341 return true;
2342 return false;
2343 }
2344
2345
2346 /* Identify nodes within the same SCC as NODE which are no longer needed
2347 because of new clones and will be removed as unreachable. */
2348
2349 static void
2350 identify_dead_nodes (struct cgraph_node *node)
2351 {
2352 struct cgraph_node *v;
2353 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2354 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
2355 && !cgraph_for_node_and_aliases (v,
2356 has_undead_caller_from_outside_scc_p,
2357 NULL, true))
2358 IPA_NODE_REF (v)->node_dead = 1;
2359
2360 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2361 if (!IPA_NODE_REF (v)->node_dead)
2362 spread_undeadness (v);
2363
2364 if (dump_file && (dump_flags & TDF_DETAILS))
2365 {
2366 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2367 if (IPA_NODE_REF (v)->node_dead)
2368 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
2369 cgraph_node_name (v), v->uid);
2370 }
2371 }
2372
2373 /* The decision stage. Iterate over the topological order of call graph nodes
2374 TOPO and make specialized clones if deemed beneficial. */
2375
2376 static void
2377 ipcp_decision_stage (struct topo_info *topo)
2378 {
2379 int i;
2380
2381 if (dump_file)
2382 fprintf (dump_file, "\nIPA decision stage:\n\n");
2383
2384 for (i = topo->nnodes - 1; i >= 0; i--)
2385 {
2386 struct cgraph_node *node = topo->order[i];
2387 bool change = false, iterate = true;
2388
2389 while (iterate)
2390 {
2391 struct cgraph_node *v;
2392 iterate = false;
2393 for (v = node; v ; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
2394 if (cgraph_function_with_gimple_body_p (v)
2395 && ipcp_versionable_function_p (v))
2396 iterate |= decide_whether_version_node (v);
2397
2398 change |= iterate;
2399 }
2400 if (change)
2401 identify_dead_nodes (node);
2402 }
2403 }
2404
2405 /* The IPCP driver. */
2406
2407 static unsigned int
2408 ipcp_driver (void)
2409 {
2410 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
2411 struct topo_info topo;
2412
2413 cgraph_remove_unreachable_nodes (true,dump_file);
2414 ipa_check_create_node_params ();
2415 ipa_check_create_edge_args ();
2416 grow_next_edge_clone_vector ();
2417 edge_duplication_hook_holder =
2418 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
2419 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
2420 sizeof (struct ipcp_value), 32);
2421 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
2422 sizeof (struct ipcp_value_source), 64);
2423 if (dump_file)
2424 {
2425 fprintf (dump_file, "\nIPA structures before propagation:\n");
2426 if (dump_flags & TDF_DETAILS)
2427 ipa_print_all_params (dump_file);
2428 ipa_print_all_jump_functions (dump_file);
2429 }
2430
2431 /* Topological sort. */
2432 build_toporder_info (&topo);
2433 /* Do the interprocedural propagation. */
2434 ipcp_propagate_stage (&topo);
2435 /* Decide what constant propagation and cloning should be performed. */
2436 ipcp_decision_stage (&topo);
2437
2438 /* Free all IPCP structures. */
2439 free_toporder_info (&topo);
2440 VEC_free (cgraph_edge_p, heap, next_edge_clone);
2441 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2442 ipa_free_all_structures_after_ipa_cp ();
2443 if (dump_file)
2444 fprintf (dump_file, "\nIPA constant propagation end\n");
2445 return 0;
2446 }
2447
2448 /* Initialization and computation of IPCP data structures. This is the initial
2449 intraprocedural analysis of functions, which gathers information to be
2450 propagated later on. */
2451
2452 static void
2453 ipcp_generate_summary (void)
2454 {
2455 struct cgraph_node *node;
2456
2457 if (dump_file)
2458 fprintf (dump_file, "\nIPA constant propagation start:\n");
2459 ipa_register_cgraph_hooks ();
2460
2461 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2462 {
2463 /* Unreachable nodes should have been eliminated before ipcp. */
2464 gcc_assert (node->needed || node->reachable);
2465 node->local.versionable = tree_versionable_function_p (node->decl);
2466 ipa_analyze_node (node);
2467 }
2468 }
2469
2470 /* Write ipcp summary for nodes in SET. */
2471
2472 static void
2473 ipcp_write_summary (cgraph_node_set set,
2474 varpool_node_set vset ATTRIBUTE_UNUSED)
2475 {
2476 ipa_prop_write_jump_functions (set);
2477 }
2478
2479 /* Read ipcp summary. */
2480
2481 static void
2482 ipcp_read_summary (void)
2483 {
2484 ipa_prop_read_jump_functions ();
2485 }
2486
2487 /* Gate for IPCP optimization. */
2488
2489 static bool
2490 cgraph_gate_cp (void)
2491 {
2492 /* FIXME: We should remove the optimize check after we ensure we never run
2493 IPA passes when not optimizing. */
2494 return flag_ipa_cp && optimize;
2495 }
2496
2497 struct ipa_opt_pass_d pass_ipa_cp =
2498 {
2499 {
2500 IPA_PASS,
2501 "cp", /* name */
2502 cgraph_gate_cp, /* gate */
2503 ipcp_driver, /* execute */
2504 NULL, /* sub */
2505 NULL, /* next */
2506 0, /* static_pass_number */
2507 TV_IPA_CONSTANT_PROP, /* tv_id */
2508 0, /* properties_required */
2509 0, /* properties_provided */
2510 0, /* properties_destroyed */
2511 0, /* todo_flags_start */
2512 TODO_dump_cgraph |
2513 TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
2514 },
2515 ipcp_generate_summary, /* generate_summary */
2516 ipcp_write_summary, /* write_summary */
2517 ipcp_read_summary, /* read_summary */
2518 NULL, /* write_optimization_summary */
2519 NULL, /* read_optimization_summary */
2520 NULL, /* stmt_fixup */
2521 0, /* TODOs */
2522 NULL, /* function_transform */
2523 NULL, /* variable_transform */
2524 };