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