re PR middle-end/42371 (dead code not eliminated during folding with whole-program)
[gcc.git] / gcc / ipa-cp.c
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
5 <mjambor@suse.cz>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 /* Interprocedural constant propagation (IPA-CP).
24
25 The goal of this transformation is to
26
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
30
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.
35
36 The algorithm also propagates types and attempts to perform type based
37 devirtualization. Types are propagated much like constants.
38
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.
47
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.
52
53
54 First stage - intraprocedural analysis
55 =======================================
56
57 This phase computes jump_function and modification flags.
58
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:
62
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.
67
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.
70
71 ipcp_generate_summary() is the main function of the first stage.
72
73 Second stage - interprocedural analysis
74 ========================================
75
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.
79
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.
86
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.
92
93 ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
94 third stage.
95
96 Third phase - materialization of clones, call statement updates.
97 ============================================
98
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. */
102
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"
120
121 struct ipcp_value;
122
123 /* Describes a particular source for an IPA-CP value. */
124
125 struct ipcp_value_source
126 {
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;
142 };
143
144 /* Describes one particular value stored in struct ipcp_lattice. */
145
146 struct ipcp_value
147 {
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;
176 };
177
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. */
183
184 struct ipcp_lattice
185 {
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;
197 };
198
199 /* Lattice with an offset to describe a part of an aggregate. */
200
201 struct ipcp_agg_lattice : public ipcp_lattice
202 {
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;
210 };
211
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. */
215
216 struct ipcp_param_lattices
217 {
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;
233
234 /* There is a virtual call based on this parameter. */
235 bool virt_call;
236 };
237
238 /* Allocation pools for values and their sources in ipa-cp. */
239
240 alloc_pool ipcp_values_pool;
241 alloc_pool ipcp_sources_pool;
242 alloc_pool ipcp_agg_lattice_pool;
243
244 /* Maximal count found in program. */
245
246 static gcov_type max_count;
247
248 /* Original overall size of the program. */
249
250 static long overall_size, max_new_size;
251
252 /* Head of the linked list of topologically sorted values. */
253
254 static struct ipcp_value *values_topo;
255
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)
260 {
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]);
265 }
266
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)
271 {
272 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
273 return &plats->itself;
274 }
275
276 /* Return whether LAT is a lattice with a single constant and without an
277 undefined value. */
278
279 static inline bool
280 ipa_lat_is_single_const (struct ipcp_lattice *lat)
281 {
282 if (lat->bottom
283 || lat->contains_variable
284 || lat->values_count != 1)
285 return false;
286 else
287 return true;
288 }
289
290 /* Return true iff the CS is an edge within a strongly connected component as
291 computed by ipa_reduced_postorder. */
292
293 static inline bool
294 edge_within_scc (struct cgraph_edge *cs)
295 {
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);
299
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);
304 }
305
306 /* Print V which is extracted from a value in a lattice to F. */
307
308 static void
309 print_ipcp_constant_value (FILE * f, tree v)
310 {
311 if (TREE_CODE (v) == TREE_BINFO)
312 {
313 fprintf (f, "BINFO ");
314 print_generic_expr (f, BINFO_TYPE (v), 0);
315 }
316 else if (TREE_CODE (v) == ADDR_EXPR
317 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
318 {
319 fprintf (f, "& ");
320 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)), 0);
321 }
322 else
323 print_generic_expr (f, v, 0);
324 }
325
326 /* Print a lattice LAT to F. */
327
328 static void
329 print_lattice (FILE * f, struct ipcp_lattice *lat,
330 bool dump_sources, bool dump_benefits)
331 {
332 struct ipcp_value *val;
333 bool prev = false;
334
335 if (lat->bottom)
336 {
337 fprintf (f, "BOTTOM\n");
338 return;
339 }
340
341 if (!lat->values_count && !lat->contains_variable)
342 {
343 fprintf (f, "TOP\n");
344 return;
345 }
346
347 if (lat->contains_variable)
348 {
349 fprintf (f, "VARIABLE");
350 prev = true;
351 if (dump_benefits)
352 fprintf (f, "\n");
353 }
354
355 for (val = lat->values; val; val = val->next)
356 {
357 if (dump_benefits && prev)
358 fprintf (f, " ");
359 else if (!dump_benefits && prev)
360 fprintf (f, ", ");
361 else
362 prev = true;
363
364 print_ipcp_constant_value (f, val->value);
365
366 if (dump_sources)
367 {
368 struct ipcp_value_source *s;
369
370 fprintf (f, " [from:");
371 for (s = val->sources; s; s = s->next)
372 fprintf (f, " %i(%i)", s->cs->caller->uid,s->cs->frequency);
373 fprintf (f, "]");
374 }
375
376 if (dump_benefits)
377 fprintf (f, " [loc_time: %i, loc_size: %i, "
378 "prop_time: %i, prop_size: %i]\n",
379 val->local_time_benefit, val->local_size_cost,
380 val->prop_time_benefit, val->prop_size_cost);
381 }
382 if (!dump_benefits)
383 fprintf (f, "\n");
384 }
385
386 /* Print all ipcp_lattices of all functions to F. */
387
388 static void
389 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
390 {
391 struct cgraph_node *node;
392 int i, count;
393
394 fprintf (f, "\nLattices:\n");
395 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
396 {
397 struct ipa_node_params *info;
398
399 info = IPA_NODE_REF (node);
400 fprintf (f, " Node: %s/%i:\n", cgraph_node_name (node), node->uid);
401 count = ipa_get_param_count (info);
402 for (i = 0; i < count; i++)
403 {
404 struct ipcp_agg_lattice *aglat;
405 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
406 fprintf (f, " param [%d]: ", i);
407 print_lattice (f, &plats->itself, dump_sources, dump_benefits);
408
409 if (plats->virt_call)
410 fprintf (f, " virt_call flag set\n");
411
412 if (plats->aggs_bottom)
413 {
414 fprintf (f, " AGGS BOTTOM\n");
415 continue;
416 }
417 if (plats->aggs_contain_variable)
418 fprintf (f, " AGGS VARIABLE\n");
419 for (aglat = plats->aggs; aglat; aglat = aglat->next)
420 {
421 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
422 plats->aggs_by_ref ? "ref " : "", aglat->offset);
423 print_lattice (f, aglat, dump_sources, dump_benefits);
424 }
425 }
426 }
427 }
428
429 /* Determine whether it is at all technically possible to create clones of NODE
430 and store this information in the ipa_node_params structure associated
431 with NODE. */
432
433 static void
434 determine_versionability (struct cgraph_node *node)
435 {
436 const char *reason = NULL;
437
438 /* There are a number of generic reasons functions cannot be versioned. We
439 also cannot remove parameters if there are type attributes such as fnspec
440 present. */
441 if (node->alias || node->thunk.thunk_p)
442 reason = "alias or thunk";
443 else if (!node->local.versionable)
444 reason = "not a tree_versionable_function";
445 else if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
446 reason = "insufficient body availability";
447
448 if (reason && dump_file && !node->alias && !node->thunk.thunk_p)
449 fprintf (dump_file, "Function %s/%i is not versionable, reason: %s.\n",
450 cgraph_node_name (node), node->uid, reason);
451
452 node->local.versionable = (reason == NULL);
453 }
454
455 /* Return true if it is at all technically possible to create clones of a
456 NODE. */
457
458 static bool
459 ipcp_versionable_function_p (struct cgraph_node *node)
460 {
461 return node->local.versionable;
462 }
463
464 /* Structure holding accumulated information about callers of a node. */
465
466 struct caller_statistics
467 {
468 gcov_type count_sum;
469 int n_calls, n_hot_calls, freq_sum;
470 };
471
472 /* Initialize fields of STAT to zeroes. */
473
474 static inline void
475 init_caller_stats (struct caller_statistics *stats)
476 {
477 stats->count_sum = 0;
478 stats->n_calls = 0;
479 stats->n_hot_calls = 0;
480 stats->freq_sum = 0;
481 }
482
483 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
484 non-thunk incoming edges to NODE. */
485
486 static bool
487 gather_caller_stats (struct cgraph_node *node, void *data)
488 {
489 struct caller_statistics *stats = (struct caller_statistics *) data;
490 struct cgraph_edge *cs;
491
492 for (cs = node->callers; cs; cs = cs->next_caller)
493 if (cs->caller->thunk.thunk_p)
494 cgraph_for_node_and_aliases (cs->caller, gather_caller_stats,
495 stats, false);
496 else
497 {
498 stats->count_sum += cs->count;
499 stats->freq_sum += cs->frequency;
500 stats->n_calls++;
501 if (cgraph_maybe_hot_edge_p (cs))
502 stats->n_hot_calls ++;
503 }
504 return false;
505
506 }
507
508 /* Return true if this NODE is viable candidate for cloning. */
509
510 static bool
511 ipcp_cloning_candidate_p (struct cgraph_node *node)
512 {
513 struct caller_statistics stats;
514
515 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
516
517 if (!flag_ipa_cp_clone)
518 {
519 if (dump_file)
520 fprintf (dump_file, "Not considering %s for cloning; "
521 "-fipa-cp-clone disabled.\n",
522 cgraph_node_name (node));
523 return false;
524 }
525
526 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->symbol.decl)))
527 {
528 if (dump_file)
529 fprintf (dump_file, "Not considering %s for cloning; "
530 "optimizing it for size.\n",
531 cgraph_node_name (node));
532 return false;
533 }
534
535 init_caller_stats (&stats);
536 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
537
538 if (inline_summary (node)->self_size < stats.n_calls)
539 {
540 if (dump_file)
541 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
542 cgraph_node_name (node));
543 return true;
544 }
545
546 /* When profile is available and function is hot, propagate into it even if
547 calls seems cold; constant propagation can improve function's speed
548 significantly. */
549 if (max_count)
550 {
551 if (stats.count_sum > node->count * 90 / 100)
552 {
553 if (dump_file)
554 fprintf (dump_file, "Considering %s for cloning; "
555 "usually called directly.\n",
556 cgraph_node_name (node));
557 return true;
558 }
559 }
560 if (!stats.n_hot_calls)
561 {
562 if (dump_file)
563 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
564 cgraph_node_name (node));
565 return false;
566 }
567 if (dump_file)
568 fprintf (dump_file, "Considering %s for cloning.\n",
569 cgraph_node_name (node));
570 return true;
571 }
572
573 /* Arrays representing a topological ordering of call graph nodes and a stack
574 of noes used during constant propagation. */
575
576 struct topo_info
577 {
578 struct cgraph_node **order;
579 struct cgraph_node **stack;
580 int nnodes, stack_top;
581 };
582
583 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
584
585 static void
586 build_toporder_info (struct topo_info *topo)
587 {
588 topo->order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
589 topo->stack = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
590 topo->stack_top = 0;
591 topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL);
592 }
593
594 /* Free information about strongly connected components and the arrays in
595 TOPO. */
596
597 static void
598 free_toporder_info (struct topo_info *topo)
599 {
600 ipa_free_postorder_info ();
601 free (topo->order);
602 free (topo->stack);
603 }
604
605 /* Add NODE to the stack in TOPO, unless it is already there. */
606
607 static inline void
608 push_node_to_stack (struct topo_info *topo, struct cgraph_node *node)
609 {
610 struct ipa_node_params *info = IPA_NODE_REF (node);
611 if (info->node_enqueued)
612 return;
613 info->node_enqueued = 1;
614 topo->stack[topo->stack_top++] = node;
615 }
616
617 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
618 is empty. */
619
620 static struct cgraph_node *
621 pop_node_from_stack (struct topo_info *topo)
622 {
623 if (topo->stack_top)
624 {
625 struct cgraph_node *node;
626 topo->stack_top--;
627 node = topo->stack[topo->stack_top];
628 IPA_NODE_REF (node)->node_enqueued = 0;
629 return node;
630 }
631 else
632 return NULL;
633 }
634
635 /* Set lattice LAT to bottom and return true if it previously was not set as
636 such. */
637
638 static inline bool
639 set_lattice_to_bottom (struct ipcp_lattice *lat)
640 {
641 bool ret = !lat->bottom;
642 lat->bottom = true;
643 return ret;
644 }
645
646 /* Mark lattice as containing an unknown value and return true if it previously
647 was not marked as such. */
648
649 static inline bool
650 set_lattice_contains_variable (struct ipcp_lattice *lat)
651 {
652 bool ret = !lat->contains_variable;
653 lat->contains_variable = true;
654 return ret;
655 }
656
657 /* Set all aggegate lattices in PLATS to bottom and return true if they were
658 not previously set as such. */
659
660 static inline bool
661 set_agg_lats_to_bottom (struct ipcp_param_lattices *plats)
662 {
663 bool ret = !plats->aggs_bottom;
664 plats->aggs_bottom = true;
665 return ret;
666 }
667
668 /* Mark all aggegate lattices in PLATS as containing an unknown value and
669 return true if they were not previously marked as such. */
670
671 static inline bool
672 set_agg_lats_contain_variable (struct ipcp_param_lattices *plats)
673 {
674 bool ret = !plats->aggs_contain_variable;
675 plats->aggs_contain_variable = true;
676 return ret;
677 }
678
679 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
680 return true is any of them has not been marked as such so far. */
681
682 static inline bool
683 set_all_contains_variable (struct ipcp_param_lattices *plats)
684 {
685 bool ret = !plats->itself.contains_variable || !plats->aggs_contain_variable;
686 plats->itself.contains_variable = true;
687 plats->aggs_contain_variable = true;
688 return ret;
689 }
690
691 /* Initialize ipcp_lattices. */
692
693 static void
694 initialize_node_lattices (struct cgraph_node *node)
695 {
696 struct ipa_node_params *info = IPA_NODE_REF (node);
697 struct cgraph_edge *ie;
698 bool disable = false, variable = false;
699 int i;
700
701 gcc_checking_assert (cgraph_function_with_gimple_body_p (node));
702 if (!node->local.local)
703 {
704 /* When cloning is allowed, we can assume that externally visible
705 functions are not called. We will compensate this by cloning
706 later. */
707 if (ipcp_versionable_function_p (node)
708 && ipcp_cloning_candidate_p (node))
709 variable = true;
710 else
711 disable = true;
712 }
713
714 if (disable || variable)
715 {
716 for (i = 0; i < ipa_get_param_count (info) ; i++)
717 {
718 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
719 if (disable)
720 {
721 set_lattice_to_bottom (&plats->itself);
722 set_agg_lats_to_bottom (plats);
723 }
724 else
725 set_all_contains_variable (plats);
726 }
727 if (dump_file && (dump_flags & TDF_DETAILS)
728 && !node->alias && !node->thunk.thunk_p)
729 fprintf (dump_file, "Marking all lattices of %s/%i as %s\n",
730 cgraph_node_name (node), node->uid,
731 disable ? "BOTTOM" : "VARIABLE");
732 }
733
734 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
735 if (ie->indirect_info->polymorphic)
736 {
737 gcc_checking_assert (ie->indirect_info->param_index >= 0);
738 ipa_get_parm_lattices (info,
739 ie->indirect_info->param_index)->virt_call = 1;
740 }
741 }
742
743 /* Return the result of a (possibly arithmetic) pass through jump function
744 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
745 determined or itself is considered an interprocedural invariant. */
746
747 static tree
748 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input)
749 {
750 tree restype, res;
751
752 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
753 return input;
754 else if (TREE_CODE (input) == TREE_BINFO)
755 return NULL_TREE;
756
757 gcc_checking_assert (is_gimple_ip_invariant (input));
758 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc))
759 == tcc_comparison)
760 restype = boolean_type_node;
761 else
762 restype = TREE_TYPE (input);
763 res = fold_binary (ipa_get_jf_pass_through_operation (jfunc), restype,
764 input, ipa_get_jf_pass_through_operand (jfunc));
765
766 if (res && !is_gimple_ip_invariant (res))
767 return NULL_TREE;
768
769 return res;
770 }
771
772 /* Return the result of an ancestor jump function JFUNC on the constant value
773 INPUT. Return NULL_TREE if that cannot be determined. */
774
775 static tree
776 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
777 {
778 if (TREE_CODE (input) == TREE_BINFO)
779 return get_binfo_at_offset (input,
780 ipa_get_jf_ancestor_offset (jfunc),
781 ipa_get_jf_ancestor_type (jfunc));
782 else if (TREE_CODE (input) == ADDR_EXPR)
783 {
784 tree t = TREE_OPERAND (input, 0);
785 t = build_ref_for_offset (EXPR_LOCATION (t), t,
786 ipa_get_jf_ancestor_offset (jfunc),
787 ipa_get_jf_ancestor_type (jfunc), NULL, false);
788 return build_fold_addr_expr (t);
789 }
790 else
791 return NULL_TREE;
792 }
793
794 /* Determine whether JFUNC evaluates to a known value (that is either a
795 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
796 describes the caller node so that pass-through jump functions can be
797 evaluated. */
798
799 tree
800 ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc)
801 {
802 if (jfunc->type == IPA_JF_CONST)
803 return ipa_get_jf_constant (jfunc);
804 else if (jfunc->type == IPA_JF_KNOWN_TYPE)
805 return ipa_binfo_from_known_type_jfunc (jfunc);
806 else if (jfunc->type == IPA_JF_PASS_THROUGH
807 || jfunc->type == IPA_JF_ANCESTOR)
808 {
809 tree input;
810 int idx;
811
812 if (jfunc->type == IPA_JF_PASS_THROUGH)
813 idx = ipa_get_jf_pass_through_formal_id (jfunc);
814 else
815 idx = ipa_get_jf_ancestor_formal_id (jfunc);
816
817 if (info->ipcp_orig_node)
818 input = info->known_vals[idx];
819 else
820 {
821 struct ipcp_lattice *lat;
822
823 if (!info->lattices)
824 {
825 gcc_checking_assert (!flag_ipa_cp);
826 return NULL_TREE;
827 }
828 lat = ipa_get_scalar_lat (info, idx);
829 if (!ipa_lat_is_single_const (lat))
830 return NULL_TREE;
831 input = lat->values->value;
832 }
833
834 if (!input)
835 return NULL_TREE;
836
837 if (jfunc->type == IPA_JF_PASS_THROUGH)
838 return ipa_get_jf_pass_through_result (jfunc, input);
839 else
840 return ipa_get_jf_ancestor_result (jfunc, input);
841 }
842 else
843 return NULL_TREE;
844 }
845
846
847 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
848 bottom, not containing a variable component and without any known value at
849 the same time. */
850
851 DEBUG_FUNCTION void
852 ipcp_verify_propagated_values (void)
853 {
854 struct cgraph_node *node;
855
856 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
857 {
858 struct ipa_node_params *info = IPA_NODE_REF (node);
859 int i, count = ipa_get_param_count (info);
860
861 for (i = 0; i < count; i++)
862 {
863 struct ipcp_lattice *lat = ipa_get_scalar_lat (info, i);
864
865 if (!lat->bottom
866 && !lat->contains_variable
867 && lat->values_count == 0)
868 {
869 if (dump_file)
870 {
871 fprintf (dump_file, "\nIPA lattices after constant "
872 "propagation:\n");
873 print_all_lattices (dump_file, true, false);
874 }
875
876 gcc_unreachable ();
877 }
878 }
879 }
880 }
881
882 /* Return true iff X and Y should be considered equal values by IPA-CP. */
883
884 static bool
885 values_equal_for_ipcp_p (tree x, tree y)
886 {
887 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
888
889 if (x == y)
890 return true;
891
892 if (TREE_CODE (x) == TREE_BINFO || TREE_CODE (y) == TREE_BINFO)
893 return false;
894
895 if (TREE_CODE (x) == ADDR_EXPR
896 && TREE_CODE (y) == ADDR_EXPR
897 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
898 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
899 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
900 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
901 else
902 return operand_equal_p (x, y, 0);
903 }
904
905 /* Add a new value source to VAL, marking that a value comes from edge CS and
906 (if the underlying jump function is a pass-through or an ancestor one) from
907 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. OFFSET
908 is negative if the source was the scalar value of the parameter itself or
909 the offset within an aggregate. */
910
911 static void
912 add_value_source (struct ipcp_value *val, struct cgraph_edge *cs,
913 struct ipcp_value *src_val, int src_idx, HOST_WIDE_INT offset)
914 {
915 struct ipcp_value_source *src;
916
917 src = (struct ipcp_value_source *) pool_alloc (ipcp_sources_pool);
918 src->offset = offset;
919 src->cs = cs;
920 src->val = src_val;
921 src->index = src_idx;
922
923 src->next = val->sources;
924 val->sources = src;
925 }
926
927 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
928 it. CS, SRC_VAL SRC_INDEX and OFFSET are meant for add_value_source and
929 have the same meaning. */
930
931 static bool
932 add_value_to_lattice (struct ipcp_lattice *lat, tree newval,
933 struct cgraph_edge *cs, struct ipcp_value *src_val,
934 int src_idx, HOST_WIDE_INT offset)
935 {
936 struct ipcp_value *val;
937
938 if (lat->bottom)
939 return false;
940
941 for (val = lat->values; val; val = val->next)
942 if (values_equal_for_ipcp_p (val->value, newval))
943 {
944 if (edge_within_scc (cs))
945 {
946 struct ipcp_value_source *s;
947 for (s = val->sources; s ; s = s->next)
948 if (s->cs == cs)
949 break;
950 if (s)
951 return false;
952 }
953
954 add_value_source (val, cs, src_val, src_idx, offset);
955 return false;
956 }
957
958 if (lat->values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE))
959 {
960 /* We can only free sources, not the values themselves, because sources
961 of other values in this this SCC might point to them. */
962 for (val = lat->values; val; val = val->next)
963 {
964 while (val->sources)
965 {
966 struct ipcp_value_source *src = val->sources;
967 val->sources = src->next;
968 pool_free (ipcp_sources_pool, src);
969 }
970 }
971
972 lat->values = NULL;
973 return set_lattice_to_bottom (lat);
974 }
975
976 lat->values_count++;
977 val = (struct ipcp_value *) pool_alloc (ipcp_values_pool);
978 memset (val, 0, sizeof (*val));
979
980 add_value_source (val, cs, src_val, src_idx, offset);
981 val->value = newval;
982 val->next = lat->values;
983 lat->values = val;
984 return true;
985 }
986
987 /* Like above but passes a special value of offset to distinguish that the
988 origin is the scalar value of the parameter rather than a part of an
989 aggregate. */
990
991 static inline bool
992 add_scalar_value_to_lattice (struct ipcp_lattice *lat, tree newval,
993 struct cgraph_edge *cs,
994 struct ipcp_value *src_val, int src_idx)
995 {
996 return add_value_to_lattice (lat, newval, cs, src_val, src_idx, -1);
997 }
998
999 /* Propagate values through a pass-through jump function JFUNC associated with
1000 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1001 is the index of the source parameter. */
1002
1003 static bool
1004 propagate_vals_accross_pass_through (struct cgraph_edge *cs,
1005 struct ipa_jump_func *jfunc,
1006 struct ipcp_lattice *src_lat,
1007 struct ipcp_lattice *dest_lat,
1008 int src_idx)
1009 {
1010 struct ipcp_value *src_val;
1011 bool ret = false;
1012
1013 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1014 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1015 ret |= add_scalar_value_to_lattice (dest_lat, src_val->value, cs,
1016 src_val, src_idx);
1017 /* Do not create new values when propagating within an SCC because if there
1018 are arithmetic functions with circular dependencies, there is infinite
1019 number of them and we would just make lattices bottom. */
1020 else if (edge_within_scc (cs))
1021 ret = set_lattice_contains_variable (dest_lat);
1022 else
1023 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1024 {
1025 tree cstval = src_val->value;
1026
1027 if (TREE_CODE (cstval) == TREE_BINFO)
1028 {
1029 ret |= set_lattice_contains_variable (dest_lat);
1030 continue;
1031 }
1032 cstval = ipa_get_jf_pass_through_result (jfunc, cstval);
1033
1034 if (cstval)
1035 ret |= add_scalar_value_to_lattice (dest_lat, cstval, cs, src_val,
1036 src_idx);
1037 else
1038 ret |= set_lattice_contains_variable (dest_lat);
1039 }
1040
1041 return ret;
1042 }
1043
1044 /* Propagate values through an ancestor jump function JFUNC associated with
1045 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1046 is the index of the source parameter. */
1047
1048 static bool
1049 propagate_vals_accross_ancestor (struct cgraph_edge *cs,
1050 struct ipa_jump_func *jfunc,
1051 struct ipcp_lattice *src_lat,
1052 struct ipcp_lattice *dest_lat,
1053 int src_idx)
1054 {
1055 struct ipcp_value *src_val;
1056 bool ret = false;
1057
1058 if (edge_within_scc (cs))
1059 return set_lattice_contains_variable (dest_lat);
1060
1061 for (src_val = src_lat->values; src_val; src_val = src_val->next)
1062 {
1063 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
1064
1065 if (t)
1066 ret |= add_scalar_value_to_lattice (dest_lat, t, cs, src_val, src_idx);
1067 else
1068 ret |= set_lattice_contains_variable (dest_lat);
1069 }
1070
1071 return ret;
1072 }
1073
1074 /* Propagate scalar values across jump function JFUNC that is associated with
1075 edge CS and put the values into DEST_LAT. */
1076
1077 static bool
1078 propagate_scalar_accross_jump_function (struct cgraph_edge *cs,
1079 struct ipa_jump_func *jfunc,
1080 struct ipcp_lattice *dest_lat)
1081 {
1082 if (dest_lat->bottom)
1083 return false;
1084
1085 if (jfunc->type == IPA_JF_CONST
1086 || jfunc->type == IPA_JF_KNOWN_TYPE)
1087 {
1088 tree val;
1089
1090 if (jfunc->type == IPA_JF_KNOWN_TYPE)
1091 {
1092 val = ipa_binfo_from_known_type_jfunc (jfunc);
1093 if (!val)
1094 return set_lattice_contains_variable (dest_lat);
1095 }
1096 else
1097 val = ipa_get_jf_constant (jfunc);
1098 return add_scalar_value_to_lattice (dest_lat, val, cs, NULL, 0);
1099 }
1100 else if (jfunc->type == IPA_JF_PASS_THROUGH
1101 || jfunc->type == IPA_JF_ANCESTOR)
1102 {
1103 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1104 struct ipcp_lattice *src_lat;
1105 int src_idx;
1106 bool ret;
1107
1108 if (jfunc->type == IPA_JF_PASS_THROUGH)
1109 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1110 else
1111 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1112
1113 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
1114 if (src_lat->bottom)
1115 return set_lattice_contains_variable (dest_lat);
1116
1117 /* If we would need to clone the caller and cannot, do not propagate. */
1118 if (!ipcp_versionable_function_p (cs->caller)
1119 && (src_lat->contains_variable
1120 || (src_lat->values_count > 1)))
1121 return set_lattice_contains_variable (dest_lat);
1122
1123 if (jfunc->type == IPA_JF_PASS_THROUGH)
1124 ret = propagate_vals_accross_pass_through (cs, jfunc, src_lat,
1125 dest_lat, src_idx);
1126 else
1127 ret = propagate_vals_accross_ancestor (cs, jfunc, src_lat, dest_lat,
1128 src_idx);
1129
1130 if (src_lat->contains_variable)
1131 ret |= set_lattice_contains_variable (dest_lat);
1132
1133 return ret;
1134 }
1135
1136 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1137 use it for indirect inlining), we should propagate them too. */
1138 return set_lattice_contains_variable (dest_lat);
1139 }
1140
1141 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1142 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1143 other cases, return false). If there are no aggregate items, set
1144 aggs_by_ref to NEW_AGGS_BY_REF. */
1145
1146 static bool
1147 set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats,
1148 bool new_aggs_by_ref)
1149 {
1150 if (dest_plats->aggs)
1151 {
1152 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
1153 {
1154 set_agg_lats_to_bottom (dest_plats);
1155 return true;
1156 }
1157 }
1158 else
1159 dest_plats->aggs_by_ref = new_aggs_by_ref;
1160 return false;
1161 }
1162
1163 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1164 already existing lattice for the given OFFSET and SIZE, marking all skipped
1165 lattices as containing variable and checking for overlaps. If there is no
1166 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1167 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1168 unless there are too many already. If there are two many, return false. If
1169 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1170 skipped lattices were newly marked as containing variable, set *CHANGE to
1171 true. */
1172
1173 static bool
1174 merge_agg_lats_step (struct ipcp_param_lattices *dest_plats,
1175 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
1176 struct ipcp_agg_lattice ***aglat,
1177 bool pre_existing, bool *change)
1178 {
1179 gcc_checking_assert (offset >= 0);
1180
1181 while (**aglat && (**aglat)->offset < offset)
1182 {
1183 if ((**aglat)->offset + (**aglat)->size > offset)
1184 {
1185 set_agg_lats_to_bottom (dest_plats);
1186 return false;
1187 }
1188 *change |= set_lattice_contains_variable (**aglat);
1189 *aglat = &(**aglat)->next;
1190 }
1191
1192 if (**aglat && (**aglat)->offset == offset)
1193 {
1194 if ((**aglat)->size != val_size
1195 || ((**aglat)->next
1196 && (**aglat)->next->offset < offset + val_size))
1197 {
1198 set_agg_lats_to_bottom (dest_plats);
1199 return false;
1200 }
1201 gcc_checking_assert (!(**aglat)->next
1202 || (**aglat)->next->offset >= offset + val_size);
1203 return true;
1204 }
1205 else
1206 {
1207 struct ipcp_agg_lattice *new_al;
1208
1209 if (**aglat && (**aglat)->offset < offset + val_size)
1210 {
1211 set_agg_lats_to_bottom (dest_plats);
1212 return false;
1213 }
1214 if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1215 return false;
1216 dest_plats->aggs_count++;
1217 new_al = (struct ipcp_agg_lattice *) pool_alloc (ipcp_agg_lattice_pool);
1218 memset (new_al, 0, sizeof (*new_al));
1219
1220 new_al->offset = offset;
1221 new_al->size = val_size;
1222 new_al->contains_variable = pre_existing;
1223
1224 new_al->next = **aglat;
1225 **aglat = new_al;
1226 return true;
1227 }
1228 }
1229
1230 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1231 containing an unknown value. */
1232
1233 static bool
1234 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
1235 {
1236 bool ret = false;
1237 while (aglat)
1238 {
1239 ret |= set_lattice_contains_variable (aglat);
1240 aglat = aglat->next;
1241 }
1242 return ret;
1243 }
1244
1245 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1246 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1247 parameter used for lattice value sources. Return true if DEST_PLATS changed
1248 in any way. */
1249
1250 static bool
1251 merge_aggregate_lattices (struct cgraph_edge *cs,
1252 struct ipcp_param_lattices *dest_plats,
1253 struct ipcp_param_lattices *src_plats,
1254 int src_idx, HOST_WIDE_INT offset_delta)
1255 {
1256 bool pre_existing = dest_plats->aggs != NULL;
1257 struct ipcp_agg_lattice **dst_aglat;
1258 bool ret = false;
1259
1260 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
1261 return true;
1262 if (src_plats->aggs_bottom)
1263 return set_agg_lats_contain_variable (dest_plats);
1264 if (src_plats->aggs_contain_variable)
1265 ret |= set_agg_lats_contain_variable (dest_plats);
1266 dst_aglat = &dest_plats->aggs;
1267
1268 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
1269 src_aglat;
1270 src_aglat = src_aglat->next)
1271 {
1272 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
1273
1274 if (new_offset < 0)
1275 continue;
1276 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
1277 &dst_aglat, pre_existing, &ret))
1278 {
1279 struct ipcp_agg_lattice *new_al = *dst_aglat;
1280
1281 dst_aglat = &(*dst_aglat)->next;
1282 if (src_aglat->bottom)
1283 {
1284 ret |= set_lattice_contains_variable (new_al);
1285 continue;
1286 }
1287 if (src_aglat->contains_variable)
1288 ret |= set_lattice_contains_variable (new_al);
1289 for (struct ipcp_value *val = src_aglat->values;
1290 val;
1291 val = val->next)
1292 ret |= add_value_to_lattice (new_al, val->value, cs, val, src_idx,
1293 src_aglat->offset);
1294 }
1295 else if (dest_plats->aggs_bottom)
1296 return true;
1297 }
1298 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
1299 return ret;
1300 }
1301
1302 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1303 pass-through JFUNC and if so, whether it has conform and conforms to the
1304 rules about propagating values passed by reference. */
1305
1306 static bool
1307 agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats,
1308 struct ipa_jump_func *jfunc)
1309 {
1310 return src_plats->aggs
1311 && (!src_plats->aggs_by_ref
1312 || ipa_get_jf_pass_through_agg_preserved (jfunc));
1313 }
1314
1315 /* Propagate scalar values across jump function JFUNC that is associated with
1316 edge CS and put the values into DEST_LAT. */
1317
1318 static bool
1319 propagate_aggs_accross_jump_function (struct cgraph_edge *cs,
1320 struct ipa_jump_func *jfunc,
1321 struct ipcp_param_lattices *dest_plats)
1322 {
1323 bool ret = false;
1324
1325 if (dest_plats->aggs_bottom)
1326 return false;
1327
1328 if (jfunc->type == IPA_JF_PASS_THROUGH
1329 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
1330 {
1331 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1332 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
1333 struct ipcp_param_lattices *src_plats;
1334
1335 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1336 if (agg_pass_through_permissible_p (src_plats, jfunc))
1337 {
1338 /* Currently we do not produce clobber aggregate jump
1339 functions, replace with merging when we do. */
1340 gcc_assert (!jfunc->agg.items);
1341 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
1342 src_idx, 0);
1343 }
1344 else
1345 ret |= set_agg_lats_contain_variable (dest_plats);
1346 }
1347 else if (jfunc->type == IPA_JF_ANCESTOR
1348 && ipa_get_jf_ancestor_agg_preserved (jfunc))
1349 {
1350 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
1351 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
1352 struct ipcp_param_lattices *src_plats;
1353
1354 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
1355 if (src_plats->aggs && src_plats->aggs_by_ref)
1356 {
1357 /* Currently we do not produce clobber aggregate jump
1358 functions, replace with merging when we do. */
1359 gcc_assert (!jfunc->agg.items);
1360 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
1361 ipa_get_jf_ancestor_offset (jfunc));
1362 }
1363 else if (!src_plats->aggs_by_ref)
1364 ret |= set_agg_lats_to_bottom (dest_plats);
1365 else
1366 ret |= set_agg_lats_contain_variable (dest_plats);
1367 }
1368 else if (jfunc->agg.items)
1369 {
1370 bool pre_existing = dest_plats->aggs != NULL;
1371 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
1372 struct ipa_agg_jf_item *item;
1373 int i;
1374
1375 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
1376 return true;
1377
1378 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
1379 {
1380 HOST_WIDE_INT val_size;
1381
1382 if (item->offset < 0)
1383 continue;
1384 gcc_checking_assert (is_gimple_ip_invariant (item->value));
1385 val_size = tree_low_cst (TYPE_SIZE (TREE_TYPE (item->value)), 1);
1386
1387 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
1388 &aglat, pre_existing, &ret))
1389 {
1390 ret |= add_value_to_lattice (*aglat, item->value, cs, NULL, 0, 0);
1391 aglat = &(*aglat)->next;
1392 }
1393 else if (dest_plats->aggs_bottom)
1394 return true;
1395 }
1396
1397 ret |= set_chain_of_aglats_contains_variable (*aglat);
1398 }
1399 else
1400 ret |= set_agg_lats_contain_variable (dest_plats);
1401
1402 return ret;
1403 }
1404
1405 /* Propagate constants from the caller to the callee of CS. INFO describes the
1406 caller. */
1407
1408 static bool
1409 propagate_constants_accross_call (struct cgraph_edge *cs)
1410 {
1411 struct ipa_node_params *callee_info;
1412 enum availability availability;
1413 struct cgraph_node *callee, *alias_or_thunk;
1414 struct ipa_edge_args *args;
1415 bool ret = false;
1416 int i, args_count, parms_count;
1417
1418 callee = cgraph_function_node (cs->callee, &availability);
1419 if (!callee->analyzed)
1420 return false;
1421 gcc_checking_assert (cgraph_function_with_gimple_body_p (callee));
1422 callee_info = IPA_NODE_REF (callee);
1423
1424 args = IPA_EDGE_REF (cs);
1425 args_count = ipa_get_cs_argument_count (args);
1426 parms_count = ipa_get_param_count (callee_info);
1427
1428 /* If this call goes through a thunk we must not propagate to the first (0th)
1429 parameter. However, we might need to uncover a thunk from below a series
1430 of aliases first. */
1431 alias_or_thunk = cs->callee;
1432 while (alias_or_thunk->alias)
1433 alias_or_thunk = cgraph_alias_aliased_node (alias_or_thunk);
1434 if (alias_or_thunk->thunk.thunk_p)
1435 {
1436 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
1437 0));
1438 i = 1;
1439 }
1440 else
1441 i = 0;
1442
1443 for (; (i < args_count) && (i < parms_count); i++)
1444 {
1445 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
1446 struct ipcp_param_lattices *dest_plats;
1447
1448 dest_plats = ipa_get_parm_lattices (callee_info, i);
1449 if (availability == AVAIL_OVERWRITABLE)
1450 ret |= set_all_contains_variable (dest_plats);
1451 else
1452 {
1453 ret |= propagate_scalar_accross_jump_function (cs, jump_func,
1454 &dest_plats->itself);
1455 ret |= propagate_aggs_accross_jump_function (cs, jump_func,
1456 dest_plats);
1457 }
1458 }
1459 for (; i < parms_count; i++)
1460 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
1461
1462 return ret;
1463 }
1464
1465 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1466 (which can contain both constants and binfos), KNOWN_BINFOS, KNOWN_AGGS or
1467 AGG_REPS return the destination. The latter three can be NULL. If AGG_REPS
1468 is not NULL, KNOWN_AGGS is ignored. */
1469
1470 static tree
1471 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
1472 vec<tree> known_vals,
1473 vec<tree> known_binfos,
1474 vec<ipa_agg_jump_function_p> known_aggs,
1475 struct ipa_agg_replacement_value *agg_reps)
1476 {
1477 int param_index = ie->indirect_info->param_index;
1478 HOST_WIDE_INT token, anc_offset;
1479 tree otr_type;
1480 tree t;
1481
1482 if (param_index == -1)
1483 return NULL_TREE;
1484
1485 if (!ie->indirect_info->polymorphic)
1486 {
1487 tree t;
1488
1489 if (ie->indirect_info->agg_contents)
1490 {
1491 if (agg_reps)
1492 {
1493 t = NULL;
1494 while (agg_reps)
1495 {
1496 if (agg_reps->index == param_index
1497 && agg_reps->offset == ie->indirect_info->offset
1498 && agg_reps->by_ref == ie->indirect_info->by_ref)
1499 {
1500 t = agg_reps->value;
1501 break;
1502 }
1503 agg_reps = agg_reps->next;
1504 }
1505 }
1506 else if (known_aggs.length () > (unsigned int) param_index)
1507 {
1508 struct ipa_agg_jump_function *agg;
1509 agg = known_aggs[param_index];
1510 t = ipa_find_agg_cst_for_param (agg, ie->indirect_info->offset,
1511 ie->indirect_info->by_ref);
1512 }
1513 else
1514 t = NULL;
1515 }
1516 else
1517 t = (known_vals.length () > (unsigned int) param_index
1518 ? known_vals[param_index] : NULL);
1519
1520 if (t &&
1521 TREE_CODE (t) == ADDR_EXPR
1522 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
1523 return TREE_OPERAND (t, 0);
1524 else
1525 return NULL_TREE;
1526 }
1527
1528 gcc_assert (!ie->indirect_info->agg_contents);
1529 token = ie->indirect_info->otr_token;
1530 anc_offset = ie->indirect_info->offset;
1531 otr_type = ie->indirect_info->otr_type;
1532
1533 t = known_vals[param_index];
1534 if (!t && known_binfos.length () > (unsigned int) param_index)
1535 t = known_binfos[param_index];
1536 if (!t)
1537 return NULL_TREE;
1538
1539 if (TREE_CODE (t) != TREE_BINFO)
1540 {
1541 tree binfo;
1542 binfo = gimple_extract_devirt_binfo_from_cst (t);
1543 if (!binfo)
1544 return NULL_TREE;
1545 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1546 if (!binfo)
1547 return NULL_TREE;
1548 return gimple_get_virt_method_for_binfo (token, binfo);
1549 }
1550 else
1551 {
1552 tree binfo;
1553
1554 binfo = get_binfo_at_offset (t, anc_offset, otr_type);
1555 if (!binfo)
1556 return NULL_TREE;
1557 return gimple_get_virt_method_for_binfo (token, binfo);
1558 }
1559 }
1560
1561
1562 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1563 (which can contain both constants and binfos), KNOWN_BINFOS (which can be
1564 NULL) or KNOWN_AGGS (which also can be NULL) return the destination. */
1565
1566 tree
1567 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1568 vec<tree> known_vals,
1569 vec<tree> known_binfos,
1570 vec<ipa_agg_jump_function_p> known_aggs)
1571 {
1572 return ipa_get_indirect_edge_target_1 (ie, known_vals, known_binfos,
1573 known_aggs, NULL);
1574 }
1575
1576 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1577 and KNOWN_BINFOS. */
1578
1579 static int
1580 devirtualization_time_bonus (struct cgraph_node *node,
1581 vec<tree> known_csts,
1582 vec<tree> known_binfos,
1583 vec<ipa_agg_jump_function_p> known_aggs)
1584 {
1585 struct cgraph_edge *ie;
1586 int res = 0;
1587
1588 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1589 {
1590 struct cgraph_node *callee;
1591 struct inline_summary *isummary;
1592 tree target;
1593
1594 target = ipa_get_indirect_edge_target (ie, known_csts, known_binfos,
1595 known_aggs);
1596 if (!target)
1597 continue;
1598
1599 /* Only bare minimum benefit for clearly un-inlineable targets. */
1600 res += 1;
1601 callee = cgraph_get_node (target);
1602 if (!callee || !callee->analyzed)
1603 continue;
1604 isummary = inline_summary (callee);
1605 if (!isummary->inlinable)
1606 continue;
1607
1608 /* FIXME: The values below need re-considering and perhaps also
1609 integrating into the cost metrics, at lest in some very basic way. */
1610 if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4)
1611 res += 31;
1612 else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2)
1613 res += 15;
1614 else if (isummary->size <= MAX_INLINE_INSNS_AUTO
1615 || DECL_DECLARED_INLINE_P (callee->symbol.decl))
1616 res += 7;
1617 }
1618
1619 return res;
1620 }
1621
1622 /* Return time bonus incurred because of HINTS. */
1623
1624 static int
1625 hint_time_bonus (inline_hints hints)
1626 {
1627 int result = 0;
1628 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
1629 result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS);
1630 if (hints & INLINE_HINT_array_index)
1631 result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS);
1632 return result;
1633 }
1634
1635 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1636 and SIZE_COST and with the sum of frequencies of incoming edges to the
1637 potential new clone in FREQUENCIES. */
1638
1639 static bool
1640 good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit,
1641 int freq_sum, gcov_type count_sum, int size_cost)
1642 {
1643 if (time_benefit == 0
1644 || !flag_ipa_cp_clone
1645 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->symbol.decl)))
1646 return false;
1647
1648 gcc_assert (size_cost > 0);
1649
1650 if (max_count)
1651 {
1652 int factor = (count_sum * 1000) / max_count;
1653 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * factor)
1654 / size_cost);
1655
1656 if (dump_file && (dump_flags & TDF_DETAILS))
1657 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1658 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1659 ") -> evaluation: " HOST_WIDEST_INT_PRINT_DEC
1660 ", threshold: %i\n",
1661 time_benefit, size_cost, (HOST_WIDE_INT) count_sum,
1662 evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1663
1664 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1665 }
1666 else
1667 {
1668 HOST_WIDEST_INT evaluation = (((HOST_WIDEST_INT) time_benefit * freq_sum)
1669 / size_cost);
1670
1671 if (dump_file && (dump_flags & TDF_DETAILS))
1672 fprintf (dump_file, " good_cloning_opportunity_p (time: %i, "
1673 "size: %i, freq_sum: %i) -> evaluation: "
1674 HOST_WIDEST_INT_PRINT_DEC ", threshold: %i\n",
1675 time_benefit, size_cost, freq_sum, evaluation,
1676 PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD));
1677
1678 return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD);
1679 }
1680 }
1681
1682 /* Return all context independent values from aggregate lattices in PLATS in a
1683 vector. Return NULL if there are none. */
1684
1685 static vec<ipa_agg_jf_item_t, va_gc> *
1686 context_independent_aggregate_values (struct ipcp_param_lattices *plats)
1687 {
1688 vec<ipa_agg_jf_item_t, va_gc> *res = NULL;
1689
1690 if (plats->aggs_bottom
1691 || plats->aggs_contain_variable
1692 || plats->aggs_count == 0)
1693 return NULL;
1694
1695 for (struct ipcp_agg_lattice *aglat = plats->aggs;
1696 aglat;
1697 aglat = aglat->next)
1698 if (ipa_lat_is_single_const (aglat))
1699 {
1700 struct ipa_agg_jf_item item;
1701 item.offset = aglat->offset;
1702 item.value = aglat->values->value;
1703 vec_safe_push (res, item);
1704 }
1705 return res;
1706 }
1707
1708 /* Allocate KNOWN_CSTS, KNOWN_BINFOS and, if non-NULL, KNOWN_AGGS and populate
1709 them with values of parameters that are known independent of the context.
1710 INFO describes the function. If REMOVABLE_PARAMS_COST is non-NULL, the
1711 movement cost of all removable parameters will be stored in it. */
1712
1713 static bool
1714 gather_context_independent_values (struct ipa_node_params *info,
1715 vec<tree> *known_csts,
1716 vec<tree> *known_binfos,
1717 vec<ipa_agg_jump_function_t> *known_aggs,
1718 int *removable_params_cost)
1719 {
1720 int i, count = ipa_get_param_count (info);
1721 bool ret = false;
1722
1723 known_csts->create (0);
1724 known_binfos->create (0);
1725 known_csts->safe_grow_cleared (count);
1726 known_binfos->safe_grow_cleared (count);
1727 if (known_aggs)
1728 {
1729 known_aggs->create (0);
1730 known_aggs->safe_grow_cleared (count);
1731 }
1732
1733 if (removable_params_cost)
1734 *removable_params_cost = 0;
1735
1736 for (i = 0; i < count ; i++)
1737 {
1738 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1739 struct ipcp_lattice *lat = &plats->itself;
1740
1741 if (ipa_lat_is_single_const (lat))
1742 {
1743 struct ipcp_value *val = lat->values;
1744 if (TREE_CODE (val->value) != TREE_BINFO)
1745 {
1746 (*known_csts)[i] = val->value;
1747 if (removable_params_cost)
1748 *removable_params_cost
1749 += estimate_move_cost (TREE_TYPE (val->value));
1750 ret = true;
1751 }
1752 else if (plats->virt_call)
1753 {
1754 (*known_binfos)[i] = val->value;
1755 ret = true;
1756 }
1757 else if (removable_params_cost
1758 && !ipa_is_param_used (info, i))
1759 *removable_params_cost
1760 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1761 }
1762 else if (removable_params_cost
1763 && !ipa_is_param_used (info, i))
1764 *removable_params_cost
1765 += estimate_move_cost (TREE_TYPE (ipa_get_param (info, i)));
1766
1767 if (known_aggs)
1768 {
1769 vec<ipa_agg_jf_item_t, va_gc> *agg_items;
1770 struct ipa_agg_jump_function *ajf;
1771
1772 agg_items = context_independent_aggregate_values (plats);
1773 ajf = &(*known_aggs)[i];
1774 ajf->items = agg_items;
1775 ajf->by_ref = plats->aggs_by_ref;
1776 ret |= agg_items != NULL;
1777 }
1778 }
1779
1780 return ret;
1781 }
1782
1783 /* The current interface in ipa-inline-analysis requires a pointer vector.
1784 Create it.
1785
1786 FIXME: That interface should be re-worked, this is slightly silly. Still,
1787 I'd like to discuss how to change it first and this demonstrates the
1788 issue. */
1789
1790 static vec<ipa_agg_jump_function_p>
1791 agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function_t> known_aggs)
1792 {
1793 vec<ipa_agg_jump_function_p> ret;
1794 struct ipa_agg_jump_function *ajf;
1795 int i;
1796
1797 ret.create (known_aggs.length ());
1798 FOR_EACH_VEC_ELT (known_aggs, i, ajf)
1799 ret.quick_push (ajf);
1800 return ret;
1801 }
1802
1803 /* Iterate over known values of parameters of NODE and estimate the local
1804 effects in terms of time and size they have. */
1805
1806 static void
1807 estimate_local_effects (struct cgraph_node *node)
1808 {
1809 struct ipa_node_params *info = IPA_NODE_REF (node);
1810 int i, count = ipa_get_param_count (info);
1811 vec<tree> known_csts, known_binfos;
1812 vec<ipa_agg_jump_function_t> known_aggs;
1813 vec<ipa_agg_jump_function_p> known_aggs_ptrs;
1814 bool always_const;
1815 int base_time = inline_summary (node)->time;
1816 int removable_params_cost;
1817
1818 if (!count || !ipcp_versionable_function_p (node))
1819 return;
1820
1821 if (dump_file && (dump_flags & TDF_DETAILS))
1822 fprintf (dump_file, "\nEstimating effects for %s/%i, base_time: %i.\n",
1823 cgraph_node_name (node), node->uid, base_time);
1824
1825 always_const = gather_context_independent_values (info, &known_csts,
1826 &known_binfos, &known_aggs,
1827 &removable_params_cost);
1828 known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs);
1829 if (always_const)
1830 {
1831 struct caller_statistics stats;
1832 inline_hints hints;
1833 int time, size;
1834
1835 init_caller_stats (&stats);
1836 cgraph_for_node_and_aliases (node, gather_caller_stats, &stats, false);
1837 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1838 known_aggs_ptrs, &size, &time, &hints);
1839 time -= devirtualization_time_bonus (node, known_csts, known_binfos,
1840 known_aggs_ptrs);
1841 time -= hint_time_bonus (hints);
1842 time -= removable_params_cost;
1843 size -= stats.n_calls * removable_params_cost;
1844
1845 if (dump_file)
1846 fprintf (dump_file, " - context independent values, size: %i, "
1847 "time_benefit: %i\n", size, base_time - time);
1848
1849 if (size <= 0
1850 || cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1851 {
1852 info->do_clone_for_all_contexts = true;
1853 base_time = time;
1854
1855 if (dump_file)
1856 fprintf (dump_file, " Decided to specialize for all "
1857 "known contexts, code not going to grow.\n");
1858 }
1859 else if (good_cloning_opportunity_p (node, base_time - time,
1860 stats.freq_sum, stats.count_sum,
1861 size))
1862 {
1863 if (size + overall_size <= max_new_size)
1864 {
1865 info->do_clone_for_all_contexts = true;
1866 base_time = time;
1867 overall_size += size;
1868
1869 if (dump_file)
1870 fprintf (dump_file, " Decided to specialize for all "
1871 "known contexts, growth deemed beneficial.\n");
1872 }
1873 else if (dump_file && (dump_flags & TDF_DETAILS))
1874 fprintf (dump_file, " Not cloning for all contexts because "
1875 "max_new_size would be reached with %li.\n",
1876 size + overall_size);
1877 }
1878 }
1879
1880 for (i = 0; i < count ; i++)
1881 {
1882 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1883 struct ipcp_lattice *lat = &plats->itself;
1884 struct ipcp_value *val;
1885 int emc;
1886
1887 if (lat->bottom
1888 || !lat->values
1889 || known_csts[i]
1890 || known_binfos[i])
1891 continue;
1892
1893 for (val = lat->values; val; val = val->next)
1894 {
1895 int time, size, time_benefit;
1896 inline_hints hints;
1897
1898 if (TREE_CODE (val->value) != TREE_BINFO)
1899 {
1900 known_csts[i] = val->value;
1901 known_binfos[i] = NULL_TREE;
1902 emc = estimate_move_cost (TREE_TYPE (val->value));
1903 }
1904 else if (plats->virt_call)
1905 {
1906 known_csts[i] = NULL_TREE;
1907 known_binfos[i] = val->value;
1908 emc = 0;
1909 }
1910 else
1911 continue;
1912
1913 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1914 known_aggs_ptrs, &size, &time,
1915 &hints);
1916 time_benefit = base_time - time
1917 + devirtualization_time_bonus (node, known_csts, known_binfos,
1918 known_aggs_ptrs)
1919 + hint_time_bonus (hints)
1920 + removable_params_cost + emc;
1921
1922 gcc_checking_assert (size >=0);
1923 /* The inliner-heuristics based estimates may think that in certain
1924 contexts some functions do not have any size at all but we want
1925 all specializations to have at least a tiny cost, not least not to
1926 divide by zero. */
1927 if (size == 0)
1928 size = 1;
1929
1930 if (dump_file && (dump_flags & TDF_DETAILS))
1931 {
1932 fprintf (dump_file, " - estimates for value ");
1933 print_ipcp_constant_value (dump_file, val->value);
1934 fprintf (dump_file, " for parameter ");
1935 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
1936 fprintf (dump_file, ": time_benefit: %i, size: %i\n",
1937 time_benefit, size);
1938 }
1939
1940 val->local_time_benefit = time_benefit;
1941 val->local_size_cost = size;
1942 }
1943 known_binfos[i] = NULL_TREE;
1944 known_csts[i] = NULL_TREE;
1945 }
1946
1947 for (i = 0; i < count ; i++)
1948 {
1949 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1950 struct ipa_agg_jump_function *ajf;
1951 struct ipcp_agg_lattice *aglat;
1952
1953 if (plats->aggs_bottom || !plats->aggs)
1954 continue;
1955
1956 ajf = &known_aggs[i];
1957 for (aglat = plats->aggs; aglat; aglat = aglat->next)
1958 {
1959 struct ipcp_value *val;
1960 if (aglat->bottom || !aglat->values
1961 /* If the following is true, the one value is in known_aggs. */
1962 || (!plats->aggs_contain_variable
1963 && ipa_lat_is_single_const (aglat)))
1964 continue;
1965
1966 for (val = aglat->values; val; val = val->next)
1967 {
1968 int time, size, time_benefit;
1969 struct ipa_agg_jf_item item;
1970 inline_hints hints;
1971
1972 item.offset = aglat->offset;
1973 item.value = val->value;
1974 vec_safe_push (ajf->items, item);
1975
1976 estimate_ipcp_clone_size_and_time (node, known_csts, known_binfos,
1977 known_aggs_ptrs, &size, &time,
1978 &hints);
1979 time_benefit = base_time - time
1980 + devirtualization_time_bonus (node, known_csts, known_binfos,
1981 known_aggs_ptrs)
1982 + hint_time_bonus (hints);
1983 gcc_checking_assert (size >=0);
1984 if (size == 0)
1985 size = 1;
1986
1987 if (dump_file && (dump_flags & TDF_DETAILS))
1988 {
1989 fprintf (dump_file, " - estimates for value ");
1990 print_ipcp_constant_value (dump_file, val->value);
1991 fprintf (dump_file, " for parameter ");
1992 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
1993 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
1994 "]: time_benefit: %i, size: %i\n",
1995 plats->aggs_by_ref ? "ref " : "",
1996 aglat->offset, time_benefit, size);
1997 }
1998
1999 val->local_time_benefit = time_benefit;
2000 val->local_size_cost = size;
2001 ajf->items->pop ();
2002 }
2003 }
2004 }
2005
2006 for (i = 0; i < count ; i++)
2007 vec_free (known_aggs[i].items);
2008
2009 known_csts.release ();
2010 known_binfos.release ();
2011 known_aggs.release ();
2012 known_aggs_ptrs.release ();
2013 }
2014
2015
2016 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2017 topological sort of values. */
2018
2019 static void
2020 add_val_to_toposort (struct ipcp_value *cur_val)
2021 {
2022 static int dfs_counter = 0;
2023 static struct ipcp_value *stack;
2024 struct ipcp_value_source *src;
2025
2026 if (cur_val->dfs)
2027 return;
2028
2029 dfs_counter++;
2030 cur_val->dfs = dfs_counter;
2031 cur_val->low_link = dfs_counter;
2032
2033 cur_val->topo_next = stack;
2034 stack = cur_val;
2035 cur_val->on_stack = true;
2036
2037 for (src = cur_val->sources; src; src = src->next)
2038 if (src->val)
2039 {
2040 if (src->val->dfs == 0)
2041 {
2042 add_val_to_toposort (src->val);
2043 if (src->val->low_link < cur_val->low_link)
2044 cur_val->low_link = src->val->low_link;
2045 }
2046 else if (src->val->on_stack
2047 && src->val->dfs < cur_val->low_link)
2048 cur_val->low_link = src->val->dfs;
2049 }
2050
2051 if (cur_val->dfs == cur_val->low_link)
2052 {
2053 struct ipcp_value *v, *scc_list = NULL;
2054
2055 do
2056 {
2057 v = stack;
2058 stack = v->topo_next;
2059 v->on_stack = false;
2060
2061 v->scc_next = scc_list;
2062 scc_list = v;
2063 }
2064 while (v != cur_val);
2065
2066 cur_val->topo_next = values_topo;
2067 values_topo = cur_val;
2068 }
2069 }
2070
2071 /* Add all values in lattices associated with NODE to the topological sort if
2072 they are not there yet. */
2073
2074 static void
2075 add_all_node_vals_to_toposort (struct cgraph_node *node)
2076 {
2077 struct ipa_node_params *info = IPA_NODE_REF (node);
2078 int i, count = ipa_get_param_count (info);
2079
2080 for (i = 0; i < count ; i++)
2081 {
2082 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
2083 struct ipcp_lattice *lat = &plats->itself;
2084 struct ipcp_agg_lattice *aglat;
2085 struct ipcp_value *val;
2086
2087 if (!lat->bottom)
2088 for (val = lat->values; val; val = val->next)
2089 add_val_to_toposort (val);
2090
2091 if (!plats->aggs_bottom)
2092 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2093 if (!aglat->bottom)
2094 for (val = aglat->values; val; val = val->next)
2095 add_val_to_toposort (val);
2096 }
2097 }
2098
2099 /* One pass of constants propagation along the call graph edges, from callers
2100 to callees (requires topological ordering in TOPO), iterate over strongly
2101 connected components. */
2102
2103 static void
2104 propagate_constants_topo (struct topo_info *topo)
2105 {
2106 int i;
2107
2108 for (i = topo->nnodes - 1; i >= 0; i--)
2109 {
2110 struct cgraph_node *v, *node = topo->order[i];
2111 struct ipa_dfs_info *node_dfs_info;
2112
2113 if (!cgraph_function_with_gimple_body_p (node))
2114 continue;
2115
2116 node_dfs_info = (struct ipa_dfs_info *) node->symbol.aux;
2117 /* First, iteratively propagate within the strongly connected component
2118 until all lattices stabilize. */
2119 v = node_dfs_info->next_cycle;
2120 while (v)
2121 {
2122 push_node_to_stack (topo, v);
2123 v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle;
2124 }
2125
2126 v = node;
2127 while (v)
2128 {
2129 struct cgraph_edge *cs;
2130
2131 for (cs = v->callees; cs; cs = cs->next_callee)
2132 if (edge_within_scc (cs)
2133 && propagate_constants_accross_call (cs))
2134 push_node_to_stack (topo, cs->callee);
2135 v = pop_node_from_stack (topo);
2136 }
2137
2138 /* Afterwards, propagate along edges leading out of the SCC, calculates
2139 the local effects of the discovered constants and all valid values to
2140 their topological sort. */
2141 v = node;
2142 while (v)
2143 {
2144 struct cgraph_edge *cs;
2145
2146 estimate_local_effects (v);
2147 add_all_node_vals_to_toposort (v);
2148 for (cs = v->callees; cs; cs = cs->next_callee)
2149 if (!edge_within_scc (cs))
2150 propagate_constants_accross_call (cs);
2151
2152 v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle;
2153 }
2154 }
2155 }
2156
2157
2158 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2159 the bigger one if otherwise. */
2160
2161 static int
2162 safe_add (int a, int b)
2163 {
2164 if (a > INT_MAX/2 || b > INT_MAX/2)
2165 return a > b ? a : b;
2166 else
2167 return a + b;
2168 }
2169
2170
2171 /* Propagate the estimated effects of individual values along the topological
2172 from the dependent values to those they depend on. */
2173
2174 static void
2175 propagate_effects (void)
2176 {
2177 struct ipcp_value *base;
2178
2179 for (base = values_topo; base; base = base->topo_next)
2180 {
2181 struct ipcp_value_source *src;
2182 struct ipcp_value *val;
2183 int time = 0, size = 0;
2184
2185 for (val = base; val; val = val->scc_next)
2186 {
2187 time = safe_add (time,
2188 val->local_time_benefit + val->prop_time_benefit);
2189 size = safe_add (size, val->local_size_cost + val->prop_size_cost);
2190 }
2191
2192 for (val = base; val; val = val->scc_next)
2193 for (src = val->sources; src; src = src->next)
2194 if (src->val
2195 && cgraph_maybe_hot_edge_p (src->cs))
2196 {
2197 src->val->prop_time_benefit = safe_add (time,
2198 src->val->prop_time_benefit);
2199 src->val->prop_size_cost = safe_add (size,
2200 src->val->prop_size_cost);
2201 }
2202 }
2203 }
2204
2205
2206 /* Propagate constants, binfos and their effects from the summaries
2207 interprocedurally. */
2208
2209 static void
2210 ipcp_propagate_stage (struct topo_info *topo)
2211 {
2212 struct cgraph_node *node;
2213
2214 if (dump_file)
2215 fprintf (dump_file, "\n Propagating constants:\n\n");
2216
2217 if (in_lto_p)
2218 ipa_update_after_lto_read ();
2219
2220
2221 FOR_EACH_DEFINED_FUNCTION (node)
2222 {
2223 struct ipa_node_params *info = IPA_NODE_REF (node);
2224
2225 determine_versionability (node);
2226 if (cgraph_function_with_gimple_body_p (node))
2227 {
2228 info->lattices = XCNEWVEC (struct ipcp_param_lattices,
2229 ipa_get_param_count (info));
2230 initialize_node_lattices (node);
2231 }
2232 if (node->count > max_count)
2233 max_count = node->count;
2234 overall_size += inline_summary (node)->self_size;
2235 }
2236
2237 max_new_size = overall_size;
2238 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
2239 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
2240 max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
2241
2242 if (dump_file)
2243 fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n",
2244 overall_size, max_new_size);
2245
2246 propagate_constants_topo (topo);
2247 #ifdef ENABLE_CHECKING
2248 ipcp_verify_propagated_values ();
2249 #endif
2250 propagate_effects ();
2251
2252 if (dump_file)
2253 {
2254 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
2255 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
2256 }
2257 }
2258
2259 /* Discover newly direct outgoing edges from NODE which is a new clone with
2260 known KNOWN_VALS and make them direct. */
2261
2262 static void
2263 ipcp_discover_new_direct_edges (struct cgraph_node *node,
2264 vec<tree> known_vals,
2265 struct ipa_agg_replacement_value *aggvals)
2266 {
2267 struct cgraph_edge *ie, *next_ie;
2268 bool found = false;
2269
2270 for (ie = node->indirect_calls; ie; ie = next_ie)
2271 {
2272 tree target;
2273
2274 next_ie = ie->next_callee;
2275 target = ipa_get_indirect_edge_target_1 (ie, known_vals, vNULL, vNULL,
2276 aggvals);
2277 if (target)
2278 {
2279 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target);
2280 found = true;
2281
2282 if (cs && !ie->indirect_info->agg_contents
2283 && !ie->indirect_info->polymorphic)
2284 {
2285 struct ipa_node_params *info = IPA_NODE_REF (node);
2286 int param_index = ie->indirect_info->param_index;
2287 int c = ipa_get_controlled_uses (info, param_index);
2288 if (c != IPA_UNDESCRIBED_USE)
2289 {
2290 struct ipa_ref *to_del;
2291
2292 c--;
2293 ipa_set_controlled_uses (info, param_index, c);
2294 if (dump_file && (dump_flags & TDF_DETAILS))
2295 fprintf (dump_file, " controlled uses count of param "
2296 "%i bumped down to %i\n", param_index, c);
2297 if (c == 0
2298 && (to_del = ipa_find_reference ((symtab_node) node,
2299 (symtab_node) cs->callee,
2300 NULL)))
2301 {
2302 if (dump_file && (dump_flags & TDF_DETAILS))
2303 fprintf (dump_file, " and even removing its "
2304 "cloning-created reference\n");
2305 ipa_remove_reference (to_del);
2306 }
2307 }
2308 }
2309 }
2310 }
2311 /* Turning calls to direct calls will improve overall summary. */
2312 if (found)
2313 inline_update_overall_summary (node);
2314 }
2315
2316 /* Vector of pointers which for linked lists of clones of an original crgaph
2317 edge. */
2318
2319 static vec<cgraph_edge_p> next_edge_clone;
2320
2321 static inline void
2322 grow_next_edge_clone_vector (void)
2323 {
2324 if (next_edge_clone.length ()
2325 <= (unsigned) cgraph_edge_max_uid)
2326 next_edge_clone.safe_grow_cleared (cgraph_edge_max_uid + 1);
2327 }
2328
2329 /* Edge duplication hook to grow the appropriate linked list in
2330 next_edge_clone. */
2331
2332 static void
2333 ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2334 __attribute__((unused)) void *data)
2335 {
2336 grow_next_edge_clone_vector ();
2337 next_edge_clone[dst->uid] = next_edge_clone[src->uid];
2338 next_edge_clone[src->uid] = dst;
2339 }
2340
2341 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2342 parameter with the given INDEX. */
2343
2344 static tree
2345 get_clone_agg_value (struct cgraph_node *node, HOST_WIDEST_INT offset,
2346 int index)
2347 {
2348 struct ipa_agg_replacement_value *aggval;
2349
2350 aggval = ipa_get_agg_replacements_for_node (node);
2351 while (aggval)
2352 {
2353 if (aggval->offset == offset
2354 && aggval->index == index)
2355 return aggval->value;
2356 aggval = aggval->next;
2357 }
2358 return NULL_TREE;
2359 }
2360
2361 /* Return true if edge CS does bring about the value described by SRC. */
2362
2363 static bool
2364 cgraph_edge_brings_value_p (struct cgraph_edge *cs,
2365 struct ipcp_value_source *src)
2366 {
2367 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2368 struct ipa_node_params *dst_info = IPA_NODE_REF (cs->callee);
2369
2370 if ((dst_info->ipcp_orig_node && !dst_info->is_all_contexts_clone)
2371 || caller_info->node_dead)
2372 return false;
2373 if (!src->val)
2374 return true;
2375
2376 if (caller_info->ipcp_orig_node)
2377 {
2378 tree t;
2379 if (src->offset == -1)
2380 t = caller_info->known_vals[src->index];
2381 else
2382 t = get_clone_agg_value (cs->caller, src->offset, src->index);
2383 return (t != NULL_TREE
2384 && values_equal_for_ipcp_p (src->val->value, t));
2385 }
2386 else
2387 {
2388 struct ipcp_agg_lattice *aglat;
2389 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
2390 src->index);
2391 if (src->offset == -1)
2392 return (ipa_lat_is_single_const (&plats->itself)
2393 && values_equal_for_ipcp_p (src->val->value,
2394 plats->itself.values->value));
2395 else
2396 {
2397 if (plats->aggs_bottom || plats->aggs_contain_variable)
2398 return false;
2399 for (aglat = plats->aggs; aglat; aglat = aglat->next)
2400 if (aglat->offset == src->offset)
2401 return (ipa_lat_is_single_const (aglat)
2402 && values_equal_for_ipcp_p (src->val->value,
2403 aglat->values->value));
2404 }
2405 return false;
2406 }
2407 }
2408
2409 /* Get the next clone in the linked list of clones of an edge. */
2410
2411 static inline struct cgraph_edge *
2412 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
2413 {
2414 return next_edge_clone[cs->uid];
2415 }
2416
2417 /* Given VAL, iterate over all its sources and if they still hold, add their
2418 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
2419 respectively. */
2420
2421 static bool
2422 get_info_about_necessary_edges (struct ipcp_value *val, int *freq_sum,
2423 gcov_type *count_sum, int *caller_count)
2424 {
2425 struct ipcp_value_source *src;
2426 int freq = 0, count = 0;
2427 gcov_type cnt = 0;
2428 bool hot = false;
2429
2430 for (src = val->sources; src; src = src->next)
2431 {
2432 struct cgraph_edge *cs = src->cs;
2433 while (cs)
2434 {
2435 if (cgraph_edge_brings_value_p (cs, src))
2436 {
2437 count++;
2438 freq += cs->frequency;
2439 cnt += cs->count;
2440 hot |= cgraph_maybe_hot_edge_p (cs);
2441 }
2442 cs = get_next_cgraph_edge_clone (cs);
2443 }
2444 }
2445
2446 *freq_sum = freq;
2447 *count_sum = cnt;
2448 *caller_count = count;
2449 return hot;
2450 }
2451
2452 /* Return a vector of incoming edges that do bring value VAL. It is assumed
2453 their number is known and equal to CALLER_COUNT. */
2454
2455 static vec<cgraph_edge_p>
2456 gather_edges_for_value (struct ipcp_value *val, int caller_count)
2457 {
2458 struct ipcp_value_source *src;
2459 vec<cgraph_edge_p> ret;
2460
2461 ret.create (caller_count);
2462 for (src = val->sources; src; src = src->next)
2463 {
2464 struct cgraph_edge *cs = src->cs;
2465 while (cs)
2466 {
2467 if (cgraph_edge_brings_value_p (cs, src))
2468 ret.quick_push (cs);
2469 cs = get_next_cgraph_edge_clone (cs);
2470 }
2471 }
2472
2473 return ret;
2474 }
2475
2476 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
2477 Return it or NULL if for some reason it cannot be created. */
2478
2479 static struct ipa_replace_map *
2480 get_replacement_map (tree value, tree parm)
2481 {
2482 tree req_type = TREE_TYPE (parm);
2483 struct ipa_replace_map *replace_map;
2484
2485 if (!useless_type_conversion_p (req_type, TREE_TYPE (value)))
2486 {
2487 if (fold_convertible_p (req_type, value))
2488 value = fold_build1 (NOP_EXPR, req_type, value);
2489 else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (value)))
2490 value = fold_build1 (VIEW_CONVERT_EXPR, req_type, value);
2491 else
2492 {
2493 if (dump_file)
2494 {
2495 fprintf (dump_file, " const ");
2496 print_generic_expr (dump_file, value, 0);
2497 fprintf (dump_file, " can't be converted to param ");
2498 print_generic_expr (dump_file, parm, 0);
2499 fprintf (dump_file, "\n");
2500 }
2501 return NULL;
2502 }
2503 }
2504
2505 replace_map = ggc_alloc_ipa_replace_map ();
2506 if (dump_file)
2507 {
2508 fprintf (dump_file, " replacing param ");
2509 print_generic_expr (dump_file, parm, 0);
2510 fprintf (dump_file, " with const ");
2511 print_generic_expr (dump_file, value, 0);
2512 fprintf (dump_file, "\n");
2513 }
2514 replace_map->old_tree = parm;
2515 replace_map->new_tree = value;
2516 replace_map->replace_p = true;
2517 replace_map->ref_p = false;
2518
2519 return replace_map;
2520 }
2521
2522 /* Dump new profiling counts */
2523
2524 static void
2525 dump_profile_updates (struct cgraph_node *orig_node,
2526 struct cgraph_node *new_node)
2527 {
2528 struct cgraph_edge *cs;
2529
2530 fprintf (dump_file, " setting count of the specialized node to "
2531 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) new_node->count);
2532 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2533 fprintf (dump_file, " edge to %s has count "
2534 HOST_WIDE_INT_PRINT_DEC "\n",
2535 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
2536
2537 fprintf (dump_file, " setting count of the original node to "
2538 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) orig_node->count);
2539 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2540 fprintf (dump_file, " edge to %s is left with "
2541 HOST_WIDE_INT_PRINT_DEC "\n",
2542 cgraph_node_name (cs->callee), (HOST_WIDE_INT) cs->count);
2543 }
2544
2545 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
2546 their profile information to reflect this. */
2547
2548 static void
2549 update_profiling_info (struct cgraph_node *orig_node,
2550 struct cgraph_node *new_node)
2551 {
2552 struct cgraph_edge *cs;
2553 struct caller_statistics stats;
2554 gcov_type new_sum, orig_sum;
2555 gcov_type remainder, orig_node_count = orig_node->count;
2556
2557 if (orig_node_count == 0)
2558 return;
2559
2560 init_caller_stats (&stats);
2561 cgraph_for_node_and_aliases (orig_node, gather_caller_stats, &stats, false);
2562 orig_sum = stats.count_sum;
2563 init_caller_stats (&stats);
2564 cgraph_for_node_and_aliases (new_node, gather_caller_stats, &stats, false);
2565 new_sum = stats.count_sum;
2566
2567 if (orig_node_count < orig_sum + new_sum)
2568 {
2569 if (dump_file)
2570 fprintf (dump_file, " Problem: node %s/%i has too low count "
2571 HOST_WIDE_INT_PRINT_DEC " while the sum of incoming "
2572 "counts is " HOST_WIDE_INT_PRINT_DEC "\n",
2573 cgraph_node_name (orig_node), orig_node->uid,
2574 (HOST_WIDE_INT) orig_node_count,
2575 (HOST_WIDE_INT) (orig_sum + new_sum));
2576
2577 orig_node_count = (orig_sum + new_sum) * 12 / 10;
2578 if (dump_file)
2579 fprintf (dump_file, " proceeding by pretending it was "
2580 HOST_WIDE_INT_PRINT_DEC "\n",
2581 (HOST_WIDE_INT) orig_node_count);
2582 }
2583
2584 new_node->count = new_sum;
2585 remainder = orig_node_count - new_sum;
2586 orig_node->count = remainder;
2587
2588 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2589 if (cs->frequency)
2590 cs->count = apply_probability (cs->count,
2591 GCOV_COMPUTE_SCALE (new_sum,
2592 orig_node_count));
2593 else
2594 cs->count = 0;
2595
2596 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2597 cs->count = apply_probability (cs->count,
2598 GCOV_COMPUTE_SCALE (remainder,
2599 orig_node_count));
2600
2601 if (dump_file)
2602 dump_profile_updates (orig_node, new_node);
2603 }
2604
2605 /* Update the respective profile of specialized NEW_NODE and the original
2606 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
2607 have been redirected to the specialized version. */
2608
2609 static void
2610 update_specialized_profile (struct cgraph_node *new_node,
2611 struct cgraph_node *orig_node,
2612 gcov_type redirected_sum)
2613 {
2614 struct cgraph_edge *cs;
2615 gcov_type new_node_count, orig_node_count = orig_node->count;
2616
2617 if (dump_file)
2618 fprintf (dump_file, " the sum of counts of redirected edges is "
2619 HOST_WIDE_INT_PRINT_DEC "\n", (HOST_WIDE_INT) redirected_sum);
2620 if (orig_node_count == 0)
2621 return;
2622
2623 gcc_assert (orig_node_count >= redirected_sum);
2624
2625 new_node_count = new_node->count;
2626 new_node->count += redirected_sum;
2627 orig_node->count -= redirected_sum;
2628
2629 for (cs = new_node->callees; cs ; cs = cs->next_callee)
2630 if (cs->frequency)
2631 cs->count += apply_probability (cs->count,
2632 GCOV_COMPUTE_SCALE (redirected_sum,
2633 new_node_count));
2634 else
2635 cs->count = 0;
2636
2637 for (cs = orig_node->callees; cs ; cs = cs->next_callee)
2638 {
2639 gcov_type dec = apply_probability (cs->count,
2640 GCOV_COMPUTE_SCALE (redirected_sum,
2641 orig_node_count));
2642 if (dec < cs->count)
2643 cs->count -= dec;
2644 else
2645 cs->count = 0;
2646 }
2647
2648 if (dump_file)
2649 dump_profile_updates (orig_node, new_node);
2650 }
2651
2652 /* Create a specialized version of NODE with known constants and types of
2653 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
2654
2655 static struct cgraph_node *
2656 create_specialized_node (struct cgraph_node *node,
2657 vec<tree> known_vals,
2658 struct ipa_agg_replacement_value *aggvals,
2659 vec<cgraph_edge_p> callers)
2660 {
2661 struct ipa_node_params *new_info, *info = IPA_NODE_REF (node);
2662 vec<ipa_replace_map_p, va_gc> *replace_trees = NULL;
2663 struct cgraph_node *new_node;
2664 int i, count = ipa_get_param_count (info);
2665 bitmap args_to_skip;
2666
2667 gcc_assert (!info->ipcp_orig_node);
2668
2669 if (node->local.can_change_signature)
2670 {
2671 args_to_skip = BITMAP_GGC_ALLOC ();
2672 for (i = 0; i < count; i++)
2673 {
2674 tree t = known_vals[i];
2675
2676 if ((t && TREE_CODE (t) != TREE_BINFO)
2677 || !ipa_is_param_used (info, i))
2678 bitmap_set_bit (args_to_skip, i);
2679 }
2680 }
2681 else
2682 {
2683 args_to_skip = NULL;
2684 if (dump_file && (dump_flags & TDF_DETAILS))
2685 fprintf (dump_file, " cannot change function signature\n");
2686 }
2687
2688 for (i = 0; i < count ; i++)
2689 {
2690 tree t = known_vals[i];
2691 if (t && TREE_CODE (t) != TREE_BINFO)
2692 {
2693 struct ipa_replace_map *replace_map;
2694
2695 replace_map = get_replacement_map (t, ipa_get_param (info, i));
2696 if (replace_map)
2697 vec_safe_push (replace_trees, replace_map);
2698 }
2699 }
2700
2701 new_node = cgraph_create_virtual_clone (node, callers, replace_trees,
2702 args_to_skip, "constprop");
2703 ipa_set_node_agg_value_chain (new_node, aggvals);
2704 if (dump_file && (dump_flags & TDF_DETAILS))
2705 {
2706 fprintf (dump_file, " the new node is %s/%i.\n",
2707 cgraph_node_name (new_node), new_node->uid);
2708 if (aggvals)
2709 ipa_dump_agg_replacement_values (dump_file, aggvals);
2710 }
2711 gcc_checking_assert (ipa_node_params_vector.exists ()
2712 && (ipa_node_params_vector.length ()
2713 > (unsigned) cgraph_max_uid));
2714 update_profiling_info (node, new_node);
2715 new_info = IPA_NODE_REF (new_node);
2716 new_info->ipcp_orig_node = node;
2717 new_info->known_vals = known_vals;
2718
2719 ipcp_discover_new_direct_edges (new_node, known_vals, aggvals);
2720
2721 callers.release ();
2722 return new_node;
2723 }
2724
2725 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2726 KNOWN_VALS with constants and types that are also known for all of the
2727 CALLERS. */
2728
2729 static void
2730 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
2731 vec<tree> known_vals,
2732 vec<cgraph_edge_p> callers)
2733 {
2734 struct ipa_node_params *info = IPA_NODE_REF (node);
2735 int i, count = ipa_get_param_count (info);
2736
2737 for (i = 0; i < count ; i++)
2738 {
2739 struct cgraph_edge *cs;
2740 tree newval = NULL_TREE;
2741 int j;
2742
2743 if (ipa_get_scalar_lat (info, i)->bottom || known_vals[i])
2744 continue;
2745
2746 FOR_EACH_VEC_ELT (callers, j, cs)
2747 {
2748 struct ipa_jump_func *jump_func;
2749 tree t;
2750
2751 if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)))
2752 {
2753 newval = NULL_TREE;
2754 break;
2755 }
2756 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
2757 t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func);
2758 if (!t
2759 || (newval
2760 && !values_equal_for_ipcp_p (t, newval)))
2761 {
2762 newval = NULL_TREE;
2763 break;
2764 }
2765 else
2766 newval = t;
2767 }
2768
2769 if (newval)
2770 {
2771 if (dump_file && (dump_flags & TDF_DETAILS))
2772 {
2773 fprintf (dump_file, " adding an extra known scalar value ");
2774 print_ipcp_constant_value (dump_file, newval);
2775 fprintf (dump_file, " for parameter ");
2776 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
2777 fprintf (dump_file, "\n");
2778 }
2779
2780 known_vals[i] = newval;
2781 }
2782 }
2783 }
2784
2785 /* Go through PLATS and create a vector of values consisting of values and
2786 offsets (minus OFFSET) of lattices that contain only a single value. */
2787
2788 static vec<ipa_agg_jf_item_t>
2789 copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset)
2790 {
2791 vec<ipa_agg_jf_item_t> res = vNULL;
2792
2793 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2794 return vNULL;
2795
2796 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
2797 if (ipa_lat_is_single_const (aglat))
2798 {
2799 struct ipa_agg_jf_item ti;
2800 ti.offset = aglat->offset - offset;
2801 ti.value = aglat->values->value;
2802 res.safe_push (ti);
2803 }
2804 return res;
2805 }
2806
2807 /* Intersect all values in INTER with single value lattices in PLATS (while
2808 subtracting OFFSET). */
2809
2810 static void
2811 intersect_with_plats (struct ipcp_param_lattices *plats,
2812 vec<ipa_agg_jf_item_t> *inter,
2813 HOST_WIDE_INT offset)
2814 {
2815 struct ipcp_agg_lattice *aglat;
2816 struct ipa_agg_jf_item *item;
2817 int k;
2818
2819 if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom)
2820 {
2821 inter->release ();
2822 return;
2823 }
2824
2825 aglat = plats->aggs;
2826 FOR_EACH_VEC_ELT (*inter, k, item)
2827 {
2828 bool found = false;
2829 if (!item->value)
2830 continue;
2831 while (aglat)
2832 {
2833 if (aglat->offset - offset > item->offset)
2834 break;
2835 if (aglat->offset - offset == item->offset)
2836 {
2837 gcc_checking_assert (item->value);
2838 if (values_equal_for_ipcp_p (item->value, aglat->values->value))
2839 found = true;
2840 break;
2841 }
2842 aglat = aglat->next;
2843 }
2844 if (!found)
2845 item->value = NULL_TREE;
2846 }
2847 }
2848
2849 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
2850 vector result while subtracting OFFSET from the individual value offsets. */
2851
2852 static vec<ipa_agg_jf_item_t>
2853 agg_replacements_to_vector (struct cgraph_node *node, int index,
2854 HOST_WIDE_INT offset)
2855 {
2856 struct ipa_agg_replacement_value *av;
2857 vec<ipa_agg_jf_item_t> res = vNULL;
2858
2859 for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next)
2860 if (av->index == index
2861 && (av->offset - offset) >= 0)
2862 {
2863 struct ipa_agg_jf_item item;
2864 gcc_checking_assert (av->value);
2865 item.offset = av->offset - offset;
2866 item.value = av->value;
2867 res.safe_push (item);
2868 }
2869
2870 return res;
2871 }
2872
2873 /* Intersect all values in INTER with those that we have already scheduled to
2874 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
2875 (while subtracting OFFSET). */
2876
2877 static void
2878 intersect_with_agg_replacements (struct cgraph_node *node, int index,
2879 vec<ipa_agg_jf_item_t> *inter,
2880 HOST_WIDE_INT offset)
2881 {
2882 struct ipa_agg_replacement_value *srcvals;
2883 struct ipa_agg_jf_item *item;
2884 int i;
2885
2886 srcvals = ipa_get_agg_replacements_for_node (node);
2887 if (!srcvals)
2888 {
2889 inter->release ();
2890 return;
2891 }
2892
2893 FOR_EACH_VEC_ELT (*inter, i, item)
2894 {
2895 struct ipa_agg_replacement_value *av;
2896 bool found = false;
2897 if (!item->value)
2898 continue;
2899 for (av = srcvals; av; av = av->next)
2900 {
2901 gcc_checking_assert (av->value);
2902 if (av->index == index
2903 && av->offset - offset == item->offset)
2904 {
2905 if (values_equal_for_ipcp_p (item->value, av->value))
2906 found = true;
2907 break;
2908 }
2909 }
2910 if (!found)
2911 item->value = NULL_TREE;
2912 }
2913 }
2914
2915 /* Intersect values in INTER with aggregate values that come along edge CS to
2916 parameter number INDEX and return it. If INTER does not actually exist yet,
2917 copy all incoming values to it. If we determine we ended up with no values
2918 whatsoever, return a released vector. */
2919
2920 static vec<ipa_agg_jf_item_t>
2921 intersect_aggregates_with_edge (struct cgraph_edge *cs, int index,
2922 vec<ipa_agg_jf_item_t> inter)
2923 {
2924 struct ipa_jump_func *jfunc;
2925 jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index);
2926 if (jfunc->type == IPA_JF_PASS_THROUGH
2927 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2928 {
2929 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2930 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2931
2932 if (caller_info->ipcp_orig_node)
2933 {
2934 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
2935 struct ipcp_param_lattices *orig_plats;
2936 orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node),
2937 src_idx);
2938 if (agg_pass_through_permissible_p (orig_plats, jfunc))
2939 {
2940 if (!inter.exists ())
2941 inter = agg_replacements_to_vector (cs->caller, src_idx, 0);
2942 else
2943 intersect_with_agg_replacements (cs->caller, src_idx,
2944 &inter, 0);
2945 }
2946 }
2947 else
2948 {
2949 struct ipcp_param_lattices *src_plats;
2950 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
2951 if (agg_pass_through_permissible_p (src_plats, jfunc))
2952 {
2953 /* Currently we do not produce clobber aggregate jump
2954 functions, adjust when we do. */
2955 gcc_checking_assert (!jfunc->agg.items);
2956 if (!inter.exists ())
2957 inter = copy_plats_to_inter (src_plats, 0);
2958 else
2959 intersect_with_plats (src_plats, &inter, 0);
2960 }
2961 }
2962 }
2963 else if (jfunc->type == IPA_JF_ANCESTOR
2964 && ipa_get_jf_ancestor_agg_preserved (jfunc))
2965 {
2966 struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller);
2967 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2968 struct ipcp_param_lattices *src_plats;
2969 HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc);
2970
2971 if (caller_info->ipcp_orig_node)
2972 {
2973 if (!inter.exists ())
2974 inter = agg_replacements_to_vector (cs->caller, src_idx, delta);
2975 else
2976 intersect_with_agg_replacements (cs->caller, src_idx, &inter,
2977 delta);
2978 }
2979 else
2980 {
2981 src_plats = ipa_get_parm_lattices (caller_info, src_idx);;
2982 /* Currently we do not produce clobber aggregate jump
2983 functions, adjust when we do. */
2984 gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items);
2985 if (!inter.exists ())
2986 inter = copy_plats_to_inter (src_plats, delta);
2987 else
2988 intersect_with_plats (src_plats, &inter, delta);
2989 }
2990 }
2991 else if (jfunc->agg.items)
2992 {
2993 struct ipa_agg_jf_item *item;
2994 int k;
2995
2996 if (!inter.exists ())
2997 for (unsigned i = 0; i < jfunc->agg.items->length (); i++)
2998 inter.safe_push ((*jfunc->agg.items)[i]);
2999 else
3000 FOR_EACH_VEC_ELT (inter, k, item)
3001 {
3002 int l = 0;
3003 bool found = false;;
3004
3005 if (!item->value)
3006 continue;
3007
3008 while ((unsigned) l < jfunc->agg.items->length ())
3009 {
3010 struct ipa_agg_jf_item *ti;
3011 ti = &(*jfunc->agg.items)[l];
3012 if (ti->offset > item->offset)
3013 break;
3014 if (ti->offset == item->offset)
3015 {
3016 gcc_checking_assert (ti->value);
3017 if (values_equal_for_ipcp_p (item->value,
3018 ti->value))
3019 found = true;
3020 break;
3021 }
3022 l++;
3023 }
3024 if (!found)
3025 item->value = NULL;
3026 }
3027 }
3028 else
3029 {
3030 inter.release();
3031 return vec<ipa_agg_jf_item_t>();
3032 }
3033 return inter;
3034 }
3035
3036 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3037 from all of them. */
3038
3039 static struct ipa_agg_replacement_value *
3040 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
3041 vec<cgraph_edge_p> callers)
3042 {
3043 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3044 struct ipa_agg_replacement_value *res = NULL;
3045 struct cgraph_edge *cs;
3046 int i, j, count = ipa_get_param_count (dest_info);
3047
3048 FOR_EACH_VEC_ELT (callers, j, cs)
3049 {
3050 int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3051 if (c < count)
3052 count = c;
3053 }
3054
3055 for (i = 0; i < count ; i++)
3056 {
3057 struct cgraph_edge *cs;
3058 vec<ipa_agg_jf_item_t> inter = vNULL;
3059 struct ipa_agg_jf_item *item;
3060 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i);
3061 int j;
3062
3063 /* Among other things, the following check should deal with all by_ref
3064 mismatches. */
3065 if (plats->aggs_bottom)
3066 continue;
3067
3068 FOR_EACH_VEC_ELT (callers, j, cs)
3069 {
3070 inter = intersect_aggregates_with_edge (cs, i, inter);
3071
3072 if (!inter.exists ())
3073 goto next_param;
3074 }
3075
3076 FOR_EACH_VEC_ELT (inter, j, item)
3077 {
3078 struct ipa_agg_replacement_value *v;
3079
3080 if (!item->value)
3081 continue;
3082
3083 v = ggc_alloc_ipa_agg_replacement_value ();
3084 v->index = i;
3085 v->offset = item->offset;
3086 v->value = item->value;
3087 v->by_ref = plats->aggs_by_ref;
3088 v->next = res;
3089 res = v;
3090 }
3091
3092 next_param:
3093 if (inter.exists ())
3094 inter.release ();
3095 }
3096 return res;
3097 }
3098
3099 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3100
3101 static struct ipa_agg_replacement_value *
3102 known_aggs_to_agg_replacement_list (vec<ipa_agg_jump_function_t> known_aggs)
3103 {
3104 struct ipa_agg_replacement_value *res = NULL;
3105 struct ipa_agg_jump_function *aggjf;
3106 struct ipa_agg_jf_item *item;
3107 int i, j;
3108
3109 FOR_EACH_VEC_ELT (known_aggs, i, aggjf)
3110 FOR_EACH_VEC_SAFE_ELT (aggjf->items, j, item)
3111 {
3112 struct ipa_agg_replacement_value *v;
3113 v = ggc_alloc_ipa_agg_replacement_value ();
3114 v->index = i;
3115 v->offset = item->offset;
3116 v->value = item->value;
3117 v->by_ref = aggjf->by_ref;
3118 v->next = res;
3119 res = v;
3120 }
3121 return res;
3122 }
3123
3124 /* Determine whether CS also brings all scalar values that the NODE is
3125 specialized for. */
3126
3127 static bool
3128 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
3129 struct cgraph_node *node)
3130 {
3131 struct ipa_node_params *dest_info = IPA_NODE_REF (node);
3132 int count = ipa_get_param_count (dest_info);
3133 struct ipa_node_params *caller_info;
3134 struct ipa_edge_args *args;
3135 int i;
3136
3137 caller_info = IPA_NODE_REF (cs->caller);
3138 args = IPA_EDGE_REF (cs);
3139 for (i = 0; i < count; i++)
3140 {
3141 struct ipa_jump_func *jump_func;
3142 tree val, t;
3143
3144 val = dest_info->known_vals[i];
3145 if (!val)
3146 continue;
3147
3148 if (i >= ipa_get_cs_argument_count (args))
3149 return false;
3150 jump_func = ipa_get_ith_jump_func (args, i);
3151 t = ipa_value_from_jfunc (caller_info, jump_func);
3152 if (!t || !values_equal_for_ipcp_p (val, t))
3153 return false;
3154 }
3155 return true;
3156 }
3157
3158 /* Determine whether CS also brings all aggregate values that NODE is
3159 specialized for. */
3160 static bool
3161 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
3162 struct cgraph_node *node)
3163 {
3164 struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller);
3165 struct ipa_agg_replacement_value *aggval;
3166 int i, ec, count;
3167
3168 aggval = ipa_get_agg_replacements_for_node (node);
3169 if (!aggval)
3170 return true;
3171
3172 count = ipa_get_param_count (IPA_NODE_REF (node));
3173 ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
3174 if (ec < count)
3175 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3176 if (aggval->index >= ec)
3177 return false;
3178
3179 if (orig_caller_info->ipcp_orig_node)
3180 orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node);
3181
3182 for (i = 0; i < count; i++)
3183 {
3184 static vec<ipa_agg_jf_item_t> values = vec<ipa_agg_jf_item_t>();
3185 struct ipcp_param_lattices *plats;
3186 bool interesting = false;
3187 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3188 if (aggval->index == i)
3189 {
3190 interesting = true;
3191 break;
3192 }
3193 if (!interesting)
3194 continue;
3195
3196 plats = ipa_get_parm_lattices (orig_caller_info, aggval->index);
3197 if (plats->aggs_bottom)
3198 return false;
3199
3200 values = intersect_aggregates_with_edge (cs, i, values);
3201 if (!values.exists())
3202 return false;
3203
3204 for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next)
3205 if (aggval->index == i)
3206 {
3207 struct ipa_agg_jf_item *item;
3208 int j;
3209 bool found = false;
3210 FOR_EACH_VEC_ELT (values, j, item)
3211 if (item->value
3212 && item->offset == av->offset
3213 && values_equal_for_ipcp_p (item->value, av->value))
3214 found = true;
3215 if (!found)
3216 {
3217 values.release();
3218 return false;
3219 }
3220 }
3221 }
3222 return true;
3223 }
3224
3225 /* Given an original NODE and a VAL for which we have already created a
3226 specialized clone, look whether there are incoming edges that still lead
3227 into the old node but now also bring the requested value and also conform to
3228 all other criteria such that they can be redirected the the special node.
3229 This function can therefore redirect the final edge in a SCC. */
3230
3231 static void
3232 perhaps_add_new_callers (struct cgraph_node *node, struct ipcp_value *val)
3233 {
3234 struct ipcp_value_source *src;
3235 gcov_type redirected_sum = 0;
3236
3237 for (src = val->sources; src; src = src->next)
3238 {
3239 struct cgraph_edge *cs = src->cs;
3240 while (cs)
3241 {
3242 enum availability availability;
3243 struct cgraph_node *dst = cgraph_function_node (cs->callee,
3244 &availability);
3245 if ((dst == node || IPA_NODE_REF (dst)->is_all_contexts_clone)
3246 && availability > AVAIL_OVERWRITABLE
3247 && cgraph_edge_brings_value_p (cs, src))
3248 {
3249 if (cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
3250 && cgraph_edge_brings_all_agg_vals_for_node (cs,
3251 val->spec_node))
3252 {
3253 if (dump_file)
3254 fprintf (dump_file, " - adding an extra caller %s/%i"
3255 " of %s/%i\n",
3256 xstrdup (cgraph_node_name (cs->caller)),
3257 cs->caller->uid,
3258 xstrdup (cgraph_node_name (val->spec_node)),
3259 val->spec_node->uid);
3260
3261 cgraph_redirect_edge_callee (cs, val->spec_node);
3262 redirected_sum += cs->count;
3263 }
3264 }
3265 cs = get_next_cgraph_edge_clone (cs);
3266 }
3267 }
3268
3269 if (redirected_sum)
3270 update_specialized_profile (val->spec_node, node, redirected_sum);
3271 }
3272
3273
3274 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
3275
3276 static void
3277 move_binfos_to_values (vec<tree> known_vals,
3278 vec<tree> known_binfos)
3279 {
3280 tree t;
3281 int i;
3282
3283 for (i = 0; known_binfos.iterate (i, &t); i++)
3284 if (t)
3285 known_vals[i] = t;
3286 }
3287
3288 /* Return true if there is a replacement equivalent to VALUE, INDEX and OFFSET
3289 among those in the AGGVALS list. */
3290
3291 DEBUG_FUNCTION bool
3292 ipcp_val_in_agg_replacements_p (struct ipa_agg_replacement_value *aggvals,
3293 int index, HOST_WIDE_INT offset, tree value)
3294 {
3295 while (aggvals)
3296 {
3297 if (aggvals->index == index
3298 && aggvals->offset == offset
3299 && values_equal_for_ipcp_p (aggvals->value, value))
3300 return true;
3301 aggvals = aggvals->next;
3302 }
3303 return false;
3304 }
3305
3306 /* Decide wheter to create a special version of NODE for value VAL of parameter
3307 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
3308 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
3309 KNOWN_BINFOS and KNOWN_AGGS describe the other already known values. */
3310
3311 static bool
3312 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
3313 struct ipcp_value *val, vec<tree> known_csts,
3314 vec<tree> known_binfos)
3315 {
3316 struct ipa_agg_replacement_value *aggvals;
3317 int freq_sum, caller_count;
3318 gcov_type count_sum;
3319 vec<cgraph_edge_p> callers;
3320 vec<tree> kv;
3321
3322 if (val->spec_node)
3323 {
3324 perhaps_add_new_callers (node, val);
3325 return false;
3326 }
3327 else if (val->local_size_cost + overall_size > max_new_size)
3328 {
3329 if (dump_file && (dump_flags & TDF_DETAILS))
3330 fprintf (dump_file, " Ignoring candidate value because "
3331 "max_new_size would be reached with %li.\n",
3332 val->local_size_cost + overall_size);
3333 return false;
3334 }
3335 else if (!get_info_about_necessary_edges (val, &freq_sum, &count_sum,
3336 &caller_count))
3337 return false;
3338
3339 if (dump_file && (dump_flags & TDF_DETAILS))
3340 {
3341 fprintf (dump_file, " - considering value ");
3342 print_ipcp_constant_value (dump_file, val->value);
3343 fprintf (dump_file, " for parameter ");
3344 print_generic_expr (dump_file, ipa_get_param (IPA_NODE_REF (node),
3345 index), 0);
3346 if (offset != -1)
3347 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
3348 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
3349 }
3350
3351 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
3352 freq_sum, count_sum,
3353 val->local_size_cost)
3354 && !good_cloning_opportunity_p (node,
3355 val->local_time_benefit
3356 + val->prop_time_benefit,
3357 freq_sum, count_sum,
3358 val->local_size_cost
3359 + val->prop_size_cost))
3360 return false;
3361
3362 if (dump_file)
3363 fprintf (dump_file, " Creating a specialized node of %s/%i.\n",
3364 cgraph_node_name (node), node->uid);
3365
3366 callers = gather_edges_for_value (val, caller_count);
3367 kv = known_csts.copy ();
3368 move_binfos_to_values (kv, known_binfos);
3369 if (offset == -1)
3370 kv[index] = val->value;
3371 find_more_scalar_values_for_callers_subset (node, kv, callers);
3372 aggvals = find_aggregate_values_for_callers_subset (node, callers);
3373 gcc_checking_assert (offset == -1
3374 || ipcp_val_in_agg_replacements_p (aggvals, index,
3375 offset, val->value));
3376 val->spec_node = create_specialized_node (node, kv, aggvals, callers);
3377 overall_size += val->local_size_cost;
3378
3379 /* TODO: If for some lattice there is only one other known value
3380 left, make a special node for it too. */
3381
3382 return true;
3383 }
3384
3385 /* Decide whether and what specialized clones of NODE should be created. */
3386
3387 static bool
3388 decide_whether_version_node (struct cgraph_node *node)
3389 {
3390 struct ipa_node_params *info = IPA_NODE_REF (node);
3391 int i, count = ipa_get_param_count (info);
3392 vec<tree> known_csts, known_binfos;
3393 vec<ipa_agg_jump_function_t> known_aggs = vNULL;
3394 bool ret = false;
3395
3396 if (count == 0)
3397 return false;
3398
3399 if (dump_file && (dump_flags & TDF_DETAILS))
3400 fprintf (dump_file, "\nEvaluating opportunities for %s/%i.\n",
3401 cgraph_node_name (node), node->uid);
3402
3403 gather_context_independent_values (info, &known_csts, &known_binfos,
3404 info->do_clone_for_all_contexts ? &known_aggs
3405 : NULL, NULL);
3406
3407 for (i = 0; i < count ;i++)
3408 {
3409 struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3410 struct ipcp_lattice *lat = &plats->itself;
3411 struct ipcp_value *val;
3412
3413 if (!lat->bottom
3414 && !known_csts[i]
3415 && !known_binfos[i])
3416 for (val = lat->values; val; val = val->next)
3417 ret |= decide_about_value (node, i, -1, val, known_csts,
3418 known_binfos);
3419
3420 if (!plats->aggs_bottom)
3421 {
3422 struct ipcp_agg_lattice *aglat;
3423 struct ipcp_value *val;
3424 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3425 if (!aglat->bottom && aglat->values
3426 /* If the following is false, the one value is in
3427 known_aggs. */
3428 && (plats->aggs_contain_variable
3429 || !ipa_lat_is_single_const (aglat)))
3430 for (val = aglat->values; val; val = val->next)
3431 ret |= decide_about_value (node, i, aglat->offset, val,
3432 known_csts, known_binfos);
3433 }
3434 info = IPA_NODE_REF (node);
3435 }
3436
3437 if (info->do_clone_for_all_contexts)
3438 {
3439 struct cgraph_node *clone;
3440 vec<cgraph_edge_p> callers;
3441
3442 if (dump_file)
3443 fprintf (dump_file, " - Creating a specialized node of %s/%i "
3444 "for all known contexts.\n", cgraph_node_name (node),
3445 node->uid);
3446
3447 callers = collect_callers_of_node (node);
3448 move_binfos_to_values (known_csts, known_binfos);
3449 clone = create_specialized_node (node, known_csts,
3450 known_aggs_to_agg_replacement_list (known_aggs),
3451 callers);
3452 info = IPA_NODE_REF (node);
3453 info->do_clone_for_all_contexts = false;
3454 IPA_NODE_REF (clone)->is_all_contexts_clone = true;
3455 for (i = 0; i < count ; i++)
3456 vec_free (known_aggs[i].items);
3457 known_aggs.release ();
3458 ret = true;
3459 }
3460 else
3461 known_csts.release ();
3462
3463 known_binfos.release ();
3464 return ret;
3465 }
3466
3467 /* Transitively mark all callees of NODE within the same SCC as not dead. */
3468
3469 static void
3470 spread_undeadness (struct cgraph_node *node)
3471 {
3472 struct cgraph_edge *cs;
3473
3474 for (cs = node->callees; cs; cs = cs->next_callee)
3475 if (edge_within_scc (cs))
3476 {
3477 struct cgraph_node *callee;
3478 struct ipa_node_params *info;
3479
3480 callee = cgraph_function_node (cs->callee, NULL);
3481 info = IPA_NODE_REF (callee);
3482
3483 if (info->node_dead)
3484 {
3485 info->node_dead = 0;
3486 spread_undeadness (callee);
3487 }
3488 }
3489 }
3490
3491 /* Return true if NODE has a caller from outside of its SCC that is not
3492 dead. Worker callback for cgraph_for_node_and_aliases. */
3493
3494 static bool
3495 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
3496 void *data ATTRIBUTE_UNUSED)
3497 {
3498 struct cgraph_edge *cs;
3499
3500 for (cs = node->callers; cs; cs = cs->next_caller)
3501 if (cs->caller->thunk.thunk_p
3502 && cgraph_for_node_and_aliases (cs->caller,
3503 has_undead_caller_from_outside_scc_p,
3504 NULL, true))
3505 return true;
3506 else if (!edge_within_scc (cs)
3507 && !IPA_NODE_REF (cs->caller)->node_dead)
3508 return true;
3509 return false;
3510 }
3511
3512
3513 /* Identify nodes within the same SCC as NODE which are no longer needed
3514 because of new clones and will be removed as unreachable. */
3515
3516 static void
3517 identify_dead_nodes (struct cgraph_node *node)
3518 {
3519 struct cgraph_node *v;
3520 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3521 if (cgraph_will_be_removed_from_program_if_no_direct_calls (v)
3522 && !cgraph_for_node_and_aliases (v,
3523 has_undead_caller_from_outside_scc_p,
3524 NULL, true))
3525 IPA_NODE_REF (v)->node_dead = 1;
3526
3527 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3528 if (!IPA_NODE_REF (v)->node_dead)
3529 spread_undeadness (v);
3530
3531 if (dump_file && (dump_flags & TDF_DETAILS))
3532 {
3533 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3534 if (IPA_NODE_REF (v)->node_dead)
3535 fprintf (dump_file, " Marking node as dead: %s/%i.\n",
3536 cgraph_node_name (v), v->uid);
3537 }
3538 }
3539
3540 /* The decision stage. Iterate over the topological order of call graph nodes
3541 TOPO and make specialized clones if deemed beneficial. */
3542
3543 static void
3544 ipcp_decision_stage (struct topo_info *topo)
3545 {
3546 int i;
3547
3548 if (dump_file)
3549 fprintf (dump_file, "\nIPA decision stage:\n\n");
3550
3551 for (i = topo->nnodes - 1; i >= 0; i--)
3552 {
3553 struct cgraph_node *node = topo->order[i];
3554 bool change = false, iterate = true;
3555
3556 while (iterate)
3557 {
3558 struct cgraph_node *v;
3559 iterate = false;
3560 for (v = node; v ; v = ((struct ipa_dfs_info *) v->symbol.aux)->next_cycle)
3561 if (cgraph_function_with_gimple_body_p (v)
3562 && ipcp_versionable_function_p (v))
3563 iterate |= decide_whether_version_node (v);
3564
3565 change |= iterate;
3566 }
3567 if (change)
3568 identify_dead_nodes (node);
3569 }
3570 }
3571
3572 /* The IPCP driver. */
3573
3574 static unsigned int
3575 ipcp_driver (void)
3576 {
3577 struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
3578 struct topo_info topo;
3579
3580 ipa_check_create_node_params ();
3581 ipa_check_create_edge_args ();
3582 grow_next_edge_clone_vector ();
3583 edge_duplication_hook_holder =
3584 cgraph_add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL);
3585 ipcp_values_pool = create_alloc_pool ("IPA-CP values",
3586 sizeof (struct ipcp_value), 32);
3587 ipcp_sources_pool = create_alloc_pool ("IPA-CP value sources",
3588 sizeof (struct ipcp_value_source), 64);
3589 ipcp_agg_lattice_pool = create_alloc_pool ("IPA_CP aggregate lattices",
3590 sizeof (struct ipcp_agg_lattice),
3591 32);
3592 if (dump_file)
3593 {
3594 fprintf (dump_file, "\nIPA structures before propagation:\n");
3595 if (dump_flags & TDF_DETAILS)
3596 ipa_print_all_params (dump_file);
3597 ipa_print_all_jump_functions (dump_file);
3598 }
3599
3600 /* Topological sort. */
3601 build_toporder_info (&topo);
3602 /* Do the interprocedural propagation. */
3603 ipcp_propagate_stage (&topo);
3604 /* Decide what constant propagation and cloning should be performed. */
3605 ipcp_decision_stage (&topo);
3606
3607 /* Free all IPCP structures. */
3608 free_toporder_info (&topo);
3609 next_edge_clone.release ();
3610 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3611 ipa_free_all_structures_after_ipa_cp ();
3612 if (dump_file)
3613 fprintf (dump_file, "\nIPA constant propagation end\n");
3614 return 0;
3615 }
3616
3617 /* Initialization and computation of IPCP data structures. This is the initial
3618 intraprocedural analysis of functions, which gathers information to be
3619 propagated later on. */
3620
3621 static void
3622 ipcp_generate_summary (void)
3623 {
3624 struct cgraph_node *node;
3625
3626 if (dump_file)
3627 fprintf (dump_file, "\nIPA constant propagation start:\n");
3628 ipa_register_cgraph_hooks ();
3629
3630 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
3631 {
3632 node->local.versionable
3633 = tree_versionable_function_p (node->symbol.decl);
3634 ipa_analyze_node (node);
3635 }
3636 }
3637
3638 /* Write ipcp summary for nodes in SET. */
3639
3640 static void
3641 ipcp_write_summary (void)
3642 {
3643 ipa_prop_write_jump_functions ();
3644 }
3645
3646 /* Read ipcp summary. */
3647
3648 static void
3649 ipcp_read_summary (void)
3650 {
3651 ipa_prop_read_jump_functions ();
3652 }
3653
3654 /* Gate for IPCP optimization. */
3655
3656 static bool
3657 cgraph_gate_cp (void)
3658 {
3659 /* FIXME: We should remove the optimize check after we ensure we never run
3660 IPA passes when not optimizing. */
3661 return flag_ipa_cp && optimize;
3662 }
3663
3664 struct ipa_opt_pass_d pass_ipa_cp =
3665 {
3666 {
3667 IPA_PASS,
3668 "cp", /* name */
3669 OPTGROUP_NONE, /* optinfo_flags */
3670 cgraph_gate_cp, /* gate */
3671 ipcp_driver, /* execute */
3672 NULL, /* sub */
3673 NULL, /* next */
3674 0, /* static_pass_number */
3675 TV_IPA_CONSTANT_PROP, /* tv_id */
3676 0, /* properties_required */
3677 0, /* properties_provided */
3678 0, /* properties_destroyed */
3679 0, /* todo_flags_start */
3680 TODO_dump_symtab |
3681 TODO_remove_functions /* todo_flags_finish */
3682 },
3683 ipcp_generate_summary, /* generate_summary */
3684 ipcp_write_summary, /* write_summary */
3685 ipcp_read_summary, /* read_summary */
3686 ipa_prop_write_all_agg_replacement, /* write_optimization_summary */
3687 ipa_prop_read_all_agg_replacement, /* read_optimization_summary */
3688 NULL, /* stmt_fixup */
3689 0, /* TODOs */
3690 ipcp_transform_function, /* function_transform */
3691 NULL, /* variable_transform */
3692 };