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