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