cgraph.c (cgraph_clone_node): Add call_duplication_hook parameter.
[gcc.git] / gcc / ipa-cp.c
1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Interprocedural constant propagation. The aim of interprocedural constant
23 propagation (IPCP) is to find which function's argument has the same
24 constant value in each invocation throughout the whole program. For example,
25 consider the following program:
26
27 int g (int y)
28 {
29 printf ("value is %d",y);
30 }
31
32 int f (int x)
33 {
34 g (x);
35 }
36
37 int h (int y)
38 {
39 g (y);
40 }
41
42 void main (void)
43 {
44 f (3);
45 h (3);
46 }
47
48
49 The IPCP algorithm will find that g's formal argument y is always called
50 with the value 3.
51
52 The algorithm used is based on "Interprocedural Constant Propagation", by
53 David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon, Comp86, pg
54 152-161
55
56 The optimization is divided into three stages:
57
58 First stage - intraprocedural analysis
59 =======================================
60 This phase computes jump_function and modification flags.
61
62 A jump function for a callsite represents the values passed as an actual
63 arguments of a given callsite. There are three types of values:
64 Pass through - the caller's formal parameter is passed as an actual argument.
65 Constant - a constant is passed as an actual argument.
66 Unknown - neither of the above.
67
68 The jump function info, ipa_jump_func, is stored in ipa_edge_args
69 structure (defined in ipa_prop.h and pointed to by cgraph_node->aux)
70 modified_flags are defined in ipa_node_params structure
71 (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
72
73 -ipcp_generate_summary() is the first stage driver.
74
75 Second stage - interprocedural analysis
76 ========================================
77 This phase does the interprocedural constant propagation.
78 It computes lattices for all formal parameters in the program
79 and their value that may be:
80 TOP - unknown.
81 BOTTOM - non constant.
82 CONSTANT - constant value.
83
84 Lattice describing a formal parameter p will have a constant value if all
85 callsites invoking this function have the same constant value passed to p.
86
87 The lattices are stored in ipcp_lattice which is itself in ipa_node_params
88 structure (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
89
90 -ipcp_iterate_stage() is the second stage driver.
91
92 Third phase - transformation of function code
93 ============================================
94 Propagates the constant-valued formals into the function.
95 For each function whose parameters are constants, we create its clone.
96
97 Then we process the clone in two ways:
98 1. We insert an assignment statement 'parameter = const' at the beginning
99 of the cloned function.
100 2. For read-only parameters that do not live in memory, we replace all their
101 uses with the constant.
102
103 We also need to modify some callsites to call the cloned functions instead
104 of the original ones. For a callsite passing an argument found to be a
105 constant by IPCP, there are two different cases to handle:
106 1. A constant is passed as an argument. In this case the callsite in the
107 should be redirected to call the cloned callee.
108 2. A parameter (of the caller) passed as an argument (pass through
109 argument). In such cases both the caller and the callee have clones and
110 only the callsite in the cloned caller is redirected to call to the
111 cloned callee.
112
113 This update is done in two steps: First all cloned functions are created
114 during a traversal of the call graph, during which all callsites are
115 redirected to call the cloned function. Then the callsites are traversed
116 and many calls redirected back to fit the description above.
117
118 -ipcp_insert_stage() is the third phase driver.
119
120
121 This pass also performs devirtualization - turns virtual calls into direct
122 ones if it can prove that all invocations of the function call the same
123 callee. This is achieved by building a list of all base types (actually,
124 their BINFOs) that individual parameters can have in an iterative matter
125 just like propagating scalar constants and then examining whether virtual
126 calls which take a parameter as their object fold to the same target for all
127 these types. If we cannot enumerate all types or there is a type which does
128 not have any BINFO associated with it, cannot_devirtualize of the associated
129 parameter descriptor is set which is an equivalent of BOTTOM lattice value
130 in standard IPA constant propagation.
131 */
132
133 #include "config.h"
134 #include "system.h"
135 #include "coretypes.h"
136 #include "tree.h"
137 #include "target.h"
138 #include "gimple.h"
139 #include "cgraph.h"
140 #include "ipa-prop.h"
141 #include "tree-flow.h"
142 #include "tree-pass.h"
143 #include "flags.h"
144 #include "timevar.h"
145 #include "diagnostic.h"
146 #include "tree-pretty-print.h"
147 #include "tree-dump.h"
148 #include "tree-inline.h"
149 #include "fibheap.h"
150 #include "params.h"
151 #include "ipa-inline.h"
152
153 /* Number of functions identified as candidates for cloning. When not cloning
154 we can simplify iterate stage not forcing it to go through the decision
155 on what is profitable and what not. */
156 static int n_cloning_candidates;
157
158 /* Maximal count found in program. */
159 static gcov_type max_count;
160
161 /* Cgraph nodes that has been completely replaced by cloning during iterate
162 * stage and will be removed after ipcp is finished. */
163 static bitmap dead_nodes;
164
165 static void ipcp_print_profile_data (FILE *);
166 static void ipcp_function_scale_print (FILE *);
167
168 /* Get the original node field of ipa_node_params associated with node NODE. */
169 static inline struct cgraph_node *
170 ipcp_get_orig_node (struct cgraph_node *node)
171 {
172 return IPA_NODE_REF (node)->ipcp_orig_node;
173 }
174
175 /* Return true if NODE describes a cloned/versioned function. */
176 static inline bool
177 ipcp_node_is_clone (struct cgraph_node *node)
178 {
179 return (ipcp_get_orig_node (node) != NULL);
180 }
181
182 /* Create ipa_node_params and its data structures for NEW_NODE. Set ORIG_NODE
183 as the ipcp_orig_node field in ipa_node_params. */
184 static void
185 ipcp_init_cloned_node (struct cgraph_node *orig_node,
186 struct cgraph_node *new_node)
187 {
188 gcc_checking_assert (ipa_node_params_vector
189 && (VEC_length (ipa_node_params_t,
190 ipa_node_params_vector)
191 > (unsigned) cgraph_max_uid));
192 gcc_checking_assert (IPA_NODE_REF (new_node)->params);
193 IPA_NODE_REF (new_node)->ipcp_orig_node = orig_node;
194 }
195
196 /* Return scale for NODE. */
197 static inline gcov_type
198 ipcp_get_node_scale (struct cgraph_node *node)
199 {
200 return IPA_NODE_REF (node)->count_scale;
201 }
202
203 /* Set COUNT as scale for NODE. */
204 static inline void
205 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
206 {
207 IPA_NODE_REF (node)->count_scale = count;
208 }
209
210 /* Return whether LAT is a constant lattice. */
211 static inline bool
212 ipcp_lat_is_const (struct ipcp_lattice *lat)
213 {
214 if (lat->type == IPA_CONST_VALUE)
215 return true;
216 else
217 return false;
218 }
219
220 /* Return whether LAT is a constant lattice that ipa-cp can actually insert
221 into the code (i.e. constants excluding member pointers and pointers). */
222 static inline bool
223 ipcp_lat_is_insertable (struct ipcp_lattice *lat)
224 {
225 return lat->type == IPA_CONST_VALUE;
226 }
227
228 /* Return true if LAT1 and LAT2 are equal. */
229 static inline bool
230 ipcp_lats_are_equal (struct ipcp_lattice *lat1, struct ipcp_lattice *lat2)
231 {
232 gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
233 if (lat1->type != lat2->type)
234 return false;
235
236 if (TREE_CODE (lat1->constant) == ADDR_EXPR
237 && TREE_CODE (lat2->constant) == ADDR_EXPR
238 && TREE_CODE (TREE_OPERAND (lat1->constant, 0)) == CONST_DECL
239 && TREE_CODE (TREE_OPERAND (lat2->constant, 0)) == CONST_DECL)
240 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (lat1->constant, 0)),
241 DECL_INITIAL (TREE_OPERAND (lat2->constant, 0)), 0);
242 else
243 return operand_equal_p (lat1->constant, lat2->constant, 0);
244 }
245
246 /* Compute Meet arithmetics:
247 Meet (IPA_BOTTOM, x) = IPA_BOTTOM
248 Meet (IPA_TOP,x) = x
249 Meet (const_a,const_b) = IPA_BOTTOM, if const_a != const_b.
250 MEET (const_a,const_b) = const_a, if const_a == const_b.*/
251 static void
252 ipa_lattice_meet (struct ipcp_lattice *res, struct ipcp_lattice *lat1,
253 struct ipcp_lattice *lat2)
254 {
255 if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
256 {
257 res->type = IPA_BOTTOM;
258 return;
259 }
260 if (lat1->type == IPA_TOP)
261 {
262 res->type = lat2->type;
263 res->constant = lat2->constant;
264 return;
265 }
266 if (lat2->type == IPA_TOP)
267 {
268 res->type = lat1->type;
269 res->constant = lat1->constant;
270 return;
271 }
272 if (!ipcp_lats_are_equal (lat1, lat2))
273 {
274 res->type = IPA_BOTTOM;
275 return;
276 }
277 res->type = lat1->type;
278 res->constant = lat1->constant;
279 }
280
281 /* True when OLD_LAT and NEW_LAT values are not the same. */
282
283 static bool
284 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
285 struct ipcp_lattice *new_lat)
286 {
287 if (old_lat->type == new_lat->type)
288 {
289 if (!ipcp_lat_is_const (old_lat))
290 return false;
291 if (ipcp_lats_are_equal (old_lat, new_lat))
292 return false;
293 }
294 return true;
295 }
296
297 /* Print all ipcp_lattices of all functions to F. */
298 static void
299 ipcp_print_all_lattices (FILE * f)
300 {
301 struct cgraph_node *node;
302 int i, count;
303
304 fprintf (f, "\nLattice:\n");
305 for (node = cgraph_nodes; node; node = node->next)
306 {
307 struct ipa_node_params *info;
308
309 if (!node->analyzed)
310 continue;
311 info = IPA_NODE_REF (node);
312 fprintf (f, " Node: %s:\n", cgraph_node_name (node));
313 count = ipa_get_param_count (info);
314 for (i = 0; i < count; i++)
315 {
316 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
317
318 fprintf (f, " param [%d]: ", i);
319 if (lat->type == IPA_CONST_VALUE)
320 {
321 tree cst = lat->constant;
322 fprintf (f, "type is CONST ");
323 print_generic_expr (f, cst, 0);
324 if (TREE_CODE (cst) == ADDR_EXPR
325 && TREE_CODE (TREE_OPERAND (cst, 0)) == CONST_DECL)
326 {
327 fprintf (f, " -> ");
328 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (cst, 0)),
329 0);
330 }
331 }
332 else if (lat->type == IPA_TOP)
333 fprintf (f, "type is TOP");
334 else
335 fprintf (f, "type is BOTTOM");
336 if (ipa_param_cannot_devirtualize_p (info, i))
337 fprintf (f, " - cannot_devirtualize set\n");
338 else if (ipa_param_types_vec_empty (info, i))
339 fprintf (f, " - type list empty\n");
340 else
341 fprintf (f, "\n");
342 }
343 }
344 }
345
346 /* Return true if ipcp algorithms would allow cloning NODE. */
347
348 static bool
349 ipcp_versionable_function_p (struct cgraph_node *node)
350 {
351 struct cgraph_edge *edge;
352
353 /* There are a number of generic reasons functions cannot be versioned. We
354 also cannot remove parameters if there are type attributes such as fnspec
355 present. */
356 if (!inline_summary (node)->versionable
357 || TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
358 return false;
359
360 /* Removing arguments doesn't work if the function takes varargs
361 or use __builtin_apply_args. */
362 for (edge = node->callees; edge; edge = edge->next_callee)
363 {
364 tree t = edge->callee->decl;
365 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
366 && (DECL_FUNCTION_CODE (t) == BUILT_IN_APPLY_ARGS
367 || DECL_FUNCTION_CODE (t) == BUILT_IN_VA_START))
368 return false;
369 }
370
371 return true;
372 }
373
374 /* Return true if this NODE is viable candidate for cloning. */
375 static bool
376 ipcp_cloning_candidate_p (struct cgraph_node *node)
377 {
378 int n_calls = 0;
379 int n_hot_calls = 0;
380 gcov_type direct_call_sum = 0;
381 struct cgraph_edge *e;
382
383 /* We never clone functions that are not visible from outside.
384 FIXME: in future we should clone such functions when they are called with
385 different constants, but current ipcp implementation is not good on this.
386 */
387 if (cgraph_only_called_directly_p (node) || !node->analyzed)
388 return false;
389
390 /* When function address is taken, we are pretty sure it will be called in hidden way. */
391 if (node->address_taken)
392 {
393 if (dump_file)
394 fprintf (dump_file, "Not considering %s for cloning; address is taken.\n",
395 cgraph_node_name (node));
396 return false;
397 }
398
399 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
400 {
401 if (dump_file)
402 fprintf (dump_file, "Not considering %s for cloning; body is overwritable.\n",
403 cgraph_node_name (node));
404 return false;
405 }
406 if (!ipcp_versionable_function_p (node))
407 {
408 if (dump_file)
409 fprintf (dump_file, "Not considering %s for cloning; body is not versionable.\n",
410 cgraph_node_name (node));
411 return false;
412 }
413 for (e = node->callers; e; e = e->next_caller)
414 {
415 direct_call_sum += e->count;
416 n_calls ++;
417 if (cgraph_maybe_hot_edge_p (e))
418 n_hot_calls ++;
419 }
420
421 if (!n_calls)
422 {
423 if (dump_file)
424 fprintf (dump_file, "Not considering %s for cloning; no direct calls.\n",
425 cgraph_node_name (node));
426 return false;
427 }
428 if (inline_summary (node)->self_size < n_calls)
429 {
430 if (dump_file)
431 fprintf (dump_file, "Considering %s for cloning; code would shrink.\n",
432 cgraph_node_name (node));
433 return true;
434 }
435
436 if (!flag_ipa_cp_clone)
437 {
438 if (dump_file)
439 fprintf (dump_file, "Not considering %s for cloning; -fipa-cp-clone disabled.\n",
440 cgraph_node_name (node));
441 return false;
442 }
443
444 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
445 {
446 if (dump_file)
447 fprintf (dump_file, "Not considering %s for cloning; optimizing it for size.\n",
448 cgraph_node_name (node));
449 return false;
450 }
451
452 /* When profile is available and function is hot, propagate into it even if
453 calls seems cold; constant propagation can improve function's speed
454 significantly. */
455 if (max_count)
456 {
457 if (direct_call_sum > node->count * 90 / 100)
458 {
459 if (dump_file)
460 fprintf (dump_file, "Considering %s for cloning; usually called directly.\n",
461 cgraph_node_name (node));
462 return true;
463 }
464 }
465 if (!n_hot_calls)
466 {
467 if (dump_file)
468 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
469 cgraph_node_name (node));
470 return false;
471 }
472 if (dump_file)
473 fprintf (dump_file, "Considering %s for cloning.\n",
474 cgraph_node_name (node));
475 return true;
476 }
477
478 /* Mark parameter with index I of function described by INFO as unsuitable for
479 devirtualization. Return true if it has already been marked so. */
480
481 static bool
482 ipa_set_param_cannot_devirtualize (struct ipa_node_params *info, int i)
483 {
484 bool ret = info->params[i].cannot_devirtualize;
485 info->params[i].cannot_devirtualize = true;
486 if (info->params[i].types)
487 VEC_free (tree, heap, info->params[i].types);
488 return ret;
489 }
490
491 /* Initialize ipcp_lattices array. The lattices corresponding to supported
492 types (integers, real types and Fortran constants defined as const_decls)
493 are initialized to IPA_TOP, the rest of them to IPA_BOTTOM. */
494 static void
495 ipcp_initialize_node_lattices (struct cgraph_node *node)
496 {
497 int i;
498 struct ipa_node_params *info = IPA_NODE_REF (node);
499 enum ipa_lattice_type type;
500
501 if (ipa_is_called_with_var_arguments (info))
502 type = IPA_BOTTOM;
503 else if (node->local.local)
504 type = IPA_TOP;
505 /* When cloning is allowed, we can assume that externally visible functions
506 are not called. We will compensate this by cloning later. */
507 else if (ipcp_cloning_candidate_p (node))
508 type = IPA_TOP, n_cloning_candidates ++;
509 else
510 type = IPA_BOTTOM;
511
512 for (i = 0; i < ipa_get_param_count (info) ; i++)
513 {
514 ipa_get_lattice (info, i)->type = type;
515 if (type == IPA_BOTTOM)
516 ipa_set_param_cannot_devirtualize (info, i);
517 }
518 }
519
520 /* Build a constant tree with type TREE_TYPE and value according to LAT.
521 Return the tree, or, if it is not possible to convert such value
522 to TREE_TYPE, NULL. */
523 static tree
524 build_const_val (struct ipcp_lattice *lat, tree tree_type)
525 {
526 tree val;
527
528 gcc_assert (ipcp_lat_is_const (lat));
529 val = lat->constant;
530
531 if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
532 {
533 if (fold_convertible_p (tree_type, val))
534 return fold_build1 (NOP_EXPR, tree_type, val);
535 else if (TYPE_SIZE (tree_type) == TYPE_SIZE (TREE_TYPE (val)))
536 return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
537 else
538 return NULL;
539 }
540 return val;
541 }
542
543 /* Compute the proper scale for NODE. It is the ratio between the number of
544 direct calls (represented on the incoming cgraph_edges) and sum of all
545 invocations of NODE (represented as count in cgraph_node).
546
547 FIXME: This code is wrong. Since the callers can be also clones and
548 the clones are not scaled yet, the sums gets unrealistically high.
549 To properly compute the counts, we would need to do propagation across
550 callgraph (as external call to A might imply call to non-cloned B
551 if A's clone calls cloned B). */
552 static void
553 ipcp_compute_node_scale (struct cgraph_node *node)
554 {
555 gcov_type sum;
556 struct cgraph_edge *cs;
557
558 sum = 0;
559 /* Compute sum of all counts of callers. */
560 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
561 sum += cs->count;
562 /* Work around the unrealistically high sum problem. We just don't want
563 the non-cloned body to have negative or very low frequency. Since
564 majority of execution time will be spent in clones anyway, this should
565 give good enough profile. */
566 if (sum > node->count * 9 / 10)
567 sum = node->count * 9 / 10;
568 if (node->count == 0)
569 ipcp_set_node_scale (node, 0);
570 else
571 ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
572 }
573
574 /* Return true if there are some formal parameters whose value is IPA_TOP (in
575 the whole compilation unit). Change their values to IPA_BOTTOM, since they
576 most probably get their values from outside of this compilation unit. */
577 static bool
578 ipcp_change_tops_to_bottom (void)
579 {
580 int i, count;
581 struct cgraph_node *node;
582 bool prop_again;
583
584 prop_again = false;
585 for (node = cgraph_nodes; node; node = node->next)
586 {
587 struct ipa_node_params *info = IPA_NODE_REF (node);
588 count = ipa_get_param_count (info);
589 for (i = 0; i < count; i++)
590 {
591 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
592 if (lat->type == IPA_TOP)
593 {
594 prop_again = true;
595 if (dump_file)
596 {
597 fprintf (dump_file, "Forcing param ");
598 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
599 fprintf (dump_file, " of node %s to bottom.\n",
600 cgraph_node_name (node));
601 }
602 lat->type = IPA_BOTTOM;
603 }
604 if (!ipa_param_cannot_devirtualize_p (info, i)
605 && ipa_param_types_vec_empty (info, i))
606 {
607 prop_again = true;
608 ipa_set_param_cannot_devirtualize (info, i);
609 if (dump_file)
610 {
611 fprintf (dump_file, "Marking param ");
612 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
613 fprintf (dump_file, " of node %s as unusable for "
614 "devirtualization.\n",
615 cgraph_node_name (node));
616 }
617 }
618 }
619 }
620 return prop_again;
621 }
622
623 /* Insert BINFO to the list of known types of parameter number I of the
624 function described by CALLEE_INFO. Return true iff the type information
625 associated with the callee parameter changed in any way. */
626
627 static bool
628 ipcp_add_param_type (struct ipa_node_params *callee_info, int i, tree binfo)
629 {
630 int j, count;
631
632 if (ipa_param_cannot_devirtualize_p (callee_info, i))
633 return false;
634
635 if (callee_info->params[i].types)
636 {
637 count = VEC_length (tree, callee_info->params[i].types);
638 for (j = 0; j < count; j++)
639 if (VEC_index (tree, callee_info->params[i].types, j) == binfo)
640 return false;
641 }
642
643 if (VEC_length (tree, callee_info->params[i].types)
644 == (unsigned) PARAM_VALUE (PARAM_DEVIRT_TYPE_LIST_SIZE))
645 return !ipa_set_param_cannot_devirtualize (callee_info, i);
646
647 VEC_safe_push (tree, heap, callee_info->params[i].types, binfo);
648 return true;
649 }
650
651 /* Copy known types information for parameter number CALLEE_IDX of CALLEE_INFO
652 from a parameter of CALLER_INFO as described by JF. Return true iff the
653 type information changed in any way. JF must be a pass-through or an
654 ancestor jump function. */
655
656 static bool
657 ipcp_copy_types (struct ipa_node_params *caller_info,
658 struct ipa_node_params *callee_info,
659 int callee_idx, struct ipa_jump_func *jf)
660 {
661 int caller_idx, j, count;
662 bool res;
663
664 if (ipa_param_cannot_devirtualize_p (callee_info, callee_idx))
665 return false;
666
667 if (jf->type == IPA_JF_PASS_THROUGH)
668 {
669 if (jf->value.pass_through.operation != NOP_EXPR)
670 {
671 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
672 return true;
673 }
674 caller_idx = jf->value.pass_through.formal_id;
675 }
676 else
677 caller_idx = jf->value.ancestor.formal_id;
678
679 if (ipa_param_cannot_devirtualize_p (caller_info, caller_idx))
680 {
681 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
682 return true;
683 }
684
685 if (!caller_info->params[caller_idx].types)
686 return false;
687
688 res = false;
689 count = VEC_length (tree, caller_info->params[caller_idx].types);
690 for (j = 0; j < count; j++)
691 {
692 tree binfo = VEC_index (tree, caller_info->params[caller_idx].types, j);
693 if (jf->type == IPA_JF_ANCESTOR)
694 {
695 binfo = get_binfo_at_offset (binfo, jf->value.ancestor.offset,
696 jf->value.ancestor.type);
697 if (!binfo)
698 {
699 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
700 return true;
701 }
702 }
703 res |= ipcp_add_param_type (callee_info, callee_idx, binfo);
704 }
705 return res;
706 }
707
708 /* Propagate type information for parameter of CALLEE_INFO number I as
709 described by JF. CALLER_INFO describes the caller. Return true iff the
710 type information changed in any way. */
711
712 static bool
713 ipcp_propagate_types (struct ipa_node_params *caller_info,
714 struct ipa_node_params *callee_info,
715 struct ipa_jump_func *jf, int i)
716 {
717 switch (jf->type)
718 {
719 case IPA_JF_UNKNOWN:
720 case IPA_JF_CONST_MEMBER_PTR:
721 case IPA_JF_CONST:
722 break;
723
724 case IPA_JF_KNOWN_TYPE:
725 return ipcp_add_param_type (callee_info, i, jf->value.base_binfo);
726
727 case IPA_JF_PASS_THROUGH:
728 case IPA_JF_ANCESTOR:
729 return ipcp_copy_types (caller_info, callee_info, i, jf);
730 }
731
732 /* If we reach this we cannot use this parameter for devirtualization. */
733 return !ipa_set_param_cannot_devirtualize (callee_info, i);
734 }
735
736 /* Interprocedural analysis. The algorithm propagates constants from the
737 caller's parameters to the callee's arguments. */
738 static void
739 ipcp_propagate_stage (void)
740 {
741 int i;
742 struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
743 struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
744 struct ipcp_lattice *dest_lat;
745 struct cgraph_edge *cs;
746 struct ipa_jump_func *jump_func;
747 struct ipa_func_list *wl;
748 int count;
749
750 ipa_check_create_node_params ();
751 ipa_check_create_edge_args ();
752
753 /* Initialize worklist to contain all functions. */
754 wl = ipa_init_func_list ();
755 while (wl)
756 {
757 struct cgraph_node *node = ipa_pop_func_from_list (&wl);
758 struct ipa_node_params *info = IPA_NODE_REF (node);
759
760 for (cs = node->callees; cs; cs = cs->next_callee)
761 {
762 struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
763 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
764
765 if (ipa_is_called_with_var_arguments (callee_info)
766 || !cs->callee->analyzed
767 || ipa_is_called_with_var_arguments (callee_info))
768 continue;
769
770 count = ipa_get_cs_argument_count (args);
771 for (i = 0; i < count; i++)
772 {
773 jump_func = ipa_get_ith_jump_func (args, i);
774 ipa_lattice_from_jfunc (info, &inc_lat, jump_func);
775 dest_lat = ipa_get_lattice (callee_info, i);
776 ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
777 if (ipcp_lattice_changed (&new_lat, dest_lat))
778 {
779 dest_lat->type = new_lat.type;
780 dest_lat->constant = new_lat.constant;
781 ipa_push_func_to_list (&wl, cs->callee);
782 }
783
784 if (ipcp_propagate_types (info, callee_info, jump_func, i))
785 ipa_push_func_to_list (&wl, cs->callee);
786 }
787 }
788 }
789 }
790
791 /* Call the constant propagation algorithm and re-call it if necessary
792 (if there are undetermined values left). */
793 static void
794 ipcp_iterate_stage (void)
795 {
796 struct cgraph_node *node;
797 n_cloning_candidates = 0;
798
799 if (dump_file)
800 fprintf (dump_file, "\nIPA iterate stage:\n\n");
801
802 if (in_lto_p)
803 ipa_update_after_lto_read ();
804
805 for (node = cgraph_nodes; node; node = node->next)
806 {
807 ipcp_initialize_node_lattices (node);
808 ipcp_compute_node_scale (node);
809 }
810 if (dump_file && (dump_flags & TDF_DETAILS))
811 {
812 ipcp_print_all_lattices (dump_file);
813 ipcp_function_scale_print (dump_file);
814 }
815
816 ipcp_propagate_stage ();
817 if (ipcp_change_tops_to_bottom ())
818 /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
819 This change should be propagated. */
820 {
821 gcc_assert (n_cloning_candidates);
822 ipcp_propagate_stage ();
823 }
824 if (dump_file)
825 {
826 fprintf (dump_file, "\nIPA lattices after propagation:\n");
827 ipcp_print_all_lattices (dump_file);
828 if (dump_flags & TDF_DETAILS)
829 ipcp_print_profile_data (dump_file);
830 }
831 }
832
833 /* Check conditions to forbid constant insertion to function described by
834 NODE. */
835 static inline bool
836 ipcp_node_modifiable_p (struct cgraph_node *node)
837 {
838 /* Once we will be able to do in-place replacement, we can be more
839 lax here. */
840 return ipcp_versionable_function_p (node);
841 }
842
843 /* Print count scale data structures. */
844 static void
845 ipcp_function_scale_print (FILE * f)
846 {
847 struct cgraph_node *node;
848
849 for (node = cgraph_nodes; node; node = node->next)
850 {
851 if (!node->analyzed)
852 continue;
853 fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
854 fprintf (f, "value is " HOST_WIDE_INT_PRINT_DEC
855 " \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
856 }
857 }
858
859 /* Print counts of all cgraph nodes. */
860 static void
861 ipcp_print_func_profile_counts (FILE * f)
862 {
863 struct cgraph_node *node;
864
865 for (node = cgraph_nodes; node; node = node->next)
866 {
867 fprintf (f, "function %s: ", cgraph_node_name (node));
868 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC
869 " \n", (HOST_WIDE_INT) node->count);
870 }
871 }
872
873 /* Print counts of all cgraph edges. */
874 static void
875 ipcp_print_call_profile_counts (FILE * f)
876 {
877 struct cgraph_node *node;
878 struct cgraph_edge *cs;
879
880 for (node = cgraph_nodes; node; node = node->next)
881 {
882 for (cs = node->callees; cs; cs = cs->next_callee)
883 {
884 fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
885 cgraph_node_name (cs->callee));
886 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC " \n",
887 (HOST_WIDE_INT) cs->count);
888 }
889 }
890 }
891
892 /* Print profile info for all functions. */
893 static void
894 ipcp_print_profile_data (FILE * f)
895 {
896 fprintf (f, "\nNODE COUNTS :\n");
897 ipcp_print_func_profile_counts (f);
898 fprintf (f, "\nCS COUNTS stage:\n");
899 ipcp_print_call_profile_counts (f);
900 }
901
902 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
903 processed by versioning, which operates according to the flags set.
904 PARM_TREE is the formal parameter found to be constant. LAT represents the
905 constant. */
906 static struct ipa_replace_map *
907 ipcp_create_replace_map (tree parm_tree, struct ipcp_lattice *lat)
908 {
909 struct ipa_replace_map *replace_map;
910 tree const_val;
911
912 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
913 if (const_val == NULL_TREE)
914 {
915 if (dump_file)
916 {
917 fprintf (dump_file, " const ");
918 print_generic_expr (dump_file, lat->constant, 0);
919 fprintf (dump_file, " can't be converted to param ");
920 print_generic_expr (dump_file, parm_tree, 0);
921 fprintf (dump_file, "\n");
922 }
923 return NULL;
924 }
925 replace_map = ggc_alloc_ipa_replace_map ();
926 if (dump_file)
927 {
928 fprintf (dump_file, " replacing param ");
929 print_generic_expr (dump_file, parm_tree, 0);
930 fprintf (dump_file, " with const ");
931 print_generic_expr (dump_file, const_val, 0);
932 fprintf (dump_file, "\n");
933 }
934 replace_map->old_tree = parm_tree;
935 replace_map->new_tree = const_val;
936 replace_map->replace_p = true;
937 replace_map->ref_p = false;
938
939 return replace_map;
940 }
941
942 /* Return true if this callsite should be redirected to the original callee
943 (instead of the cloned one). */
944 static bool
945 ipcp_need_redirect_p (struct cgraph_edge *cs)
946 {
947 struct ipa_node_params *orig_callee_info;
948 int i, count;
949 struct cgraph_node *node = cs->callee, *orig;
950
951 if (!n_cloning_candidates)
952 return false;
953
954 /* We can't redirect anything in thunks, yet. */
955 if (cs->caller->thunk.thunk_p)
956 return true;
957
958 if ((orig = ipcp_get_orig_node (node)) != NULL)
959 node = orig;
960 if (ipcp_get_orig_node (cs->caller))
961 return false;
962
963 orig_callee_info = IPA_NODE_REF (node);
964 count = ipa_get_param_count (orig_callee_info);
965 for (i = 0; i < count; i++)
966 {
967 struct ipcp_lattice *lat = ipa_get_lattice (orig_callee_info, i);
968 struct ipa_jump_func *jump_func;
969
970 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
971 if ((ipcp_lat_is_const (lat)
972 && jump_func->type != IPA_JF_CONST)
973 || (!ipa_param_cannot_devirtualize_p (orig_callee_info, i)
974 && !ipa_param_types_vec_empty (orig_callee_info, i)
975 && jump_func->type != IPA_JF_CONST
976 && jump_func->type != IPA_JF_KNOWN_TYPE))
977 return true;
978 }
979
980 return false;
981 }
982
983 /* Fix the callsites and the call graph after function cloning was done. */
984 static void
985 ipcp_update_callgraph (void)
986 {
987 struct cgraph_node *node;
988
989 for (node = cgraph_nodes; node; node = node->next)
990 if (node->analyzed && ipcp_node_is_clone (node))
991 {
992 bitmap args_to_skip = NULL;
993 struct cgraph_node *orig_node = ipcp_get_orig_node (node);
994 struct ipa_node_params *info = IPA_NODE_REF (orig_node);
995 int i, count = ipa_get_param_count (info);
996 struct cgraph_edge *cs, *next;
997
998 if (node->local.can_change_signature)
999 {
1000 args_to_skip = BITMAP_ALLOC (NULL);
1001 for (i = 0; i < count; i++)
1002 {
1003 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1004
1005 /* We can proactively remove obviously unused arguments. */
1006 if (!ipa_is_param_used (info, i))
1007 {
1008 bitmap_set_bit (args_to_skip, i);
1009 continue;
1010 }
1011
1012 if (lat->type == IPA_CONST_VALUE)
1013 bitmap_set_bit (args_to_skip, i);
1014 }
1015 }
1016 for (cs = node->callers; cs; cs = next)
1017 {
1018 next = cs->next_caller;
1019 if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
1020 {
1021 if (dump_file)
1022 fprintf (dump_file, "Redirecting edge %s/%i -> %s/%i "
1023 "back to %s/%i.",
1024 cgraph_node_name (cs->caller), cs->caller->uid,
1025 cgraph_node_name (cs->callee), cs->callee->uid,
1026 cgraph_node_name (orig_node), orig_node->uid);
1027 cgraph_redirect_edge_callee (cs, orig_node);
1028 }
1029 }
1030 }
1031 }
1032
1033 /* Update profiling info for versioned functions and the functions they were
1034 versioned from. */
1035 static void
1036 ipcp_update_profiling (void)
1037 {
1038 struct cgraph_node *node, *orig_node;
1039 gcov_type scale, scale_complement;
1040 struct cgraph_edge *cs;
1041
1042 for (node = cgraph_nodes; node; node = node->next)
1043 {
1044 if (ipcp_node_is_clone (node))
1045 {
1046 orig_node = ipcp_get_orig_node (node);
1047 scale = ipcp_get_node_scale (orig_node);
1048 node->count = orig_node->count * scale / REG_BR_PROB_BASE;
1049 scale_complement = REG_BR_PROB_BASE - scale;
1050
1051 gcc_assert (scale_complement >= 0);
1052 orig_node->count =
1053 orig_node->count * scale_complement / REG_BR_PROB_BASE;
1054 for (cs = node->callees; cs; cs = cs->next_callee)
1055 cs->count = cs->count * scale / REG_BR_PROB_BASE;
1056 for (cs = orig_node->callees; cs; cs = cs->next_callee)
1057 cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
1058 }
1059 }
1060 }
1061
1062 /* If NODE was cloned, how much would program grow? */
1063 static long
1064 ipcp_estimate_growth (struct cgraph_node *node)
1065 {
1066 struct cgraph_edge *cs;
1067 int redirectable_node_callers = 0;
1068 int removable_args = 0;
1069 bool need_original
1070 = !cgraph_will_be_removed_from_program_if_no_direct_calls (node);
1071 struct ipa_node_params *info;
1072 int i, count;
1073 int growth;
1074
1075 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1076 if (cs->caller == node || !ipcp_need_redirect_p (cs))
1077 redirectable_node_callers++;
1078 else
1079 need_original = true;
1080
1081 /* If we will be able to fully replace original node, we never increase
1082 program size. */
1083 if (!need_original)
1084 return 0;
1085
1086 info = IPA_NODE_REF (node);
1087 count = ipa_get_param_count (info);
1088 if (node->local.can_change_signature)
1089 for (i = 0; i < count; i++)
1090 {
1091 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1092
1093 /* We can proactively remove obviously unused arguments. */
1094 if (!ipa_is_param_used (info, i))
1095 removable_args++;
1096
1097 if (lat->type == IPA_CONST_VALUE)
1098 removable_args++;
1099 }
1100
1101 /* We make just very simple estimate of savings for removal of operand from
1102 call site. Precise cost is difficult to get, as our size metric counts
1103 constants and moves as free. Generally we are looking for cases that
1104 small function is called very many times. */
1105 estimate_ipcp_clone_size_and_time (node, &growth, NULL);
1106 growth = growth
1107 - removable_args * redirectable_node_callers;
1108 if (growth < 0)
1109 return 0;
1110 return growth;
1111 }
1112
1113
1114 /* Estimate cost of cloning NODE. */
1115 static long
1116 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1117 {
1118 int freq_sum = 1;
1119 gcov_type count_sum = 1;
1120 struct cgraph_edge *e;
1121 int cost;
1122
1123 cost = ipcp_estimate_growth (node) * 1000;
1124 if (!cost)
1125 {
1126 if (dump_file)
1127 fprintf (dump_file, "Versioning of %s will save code size\n",
1128 cgraph_node_name (node));
1129 return 0;
1130 }
1131
1132 for (e = node->callers; e; e = e->next_caller)
1133 if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1134 && !ipcp_need_redirect_p (e))
1135 {
1136 count_sum += e->count;
1137 freq_sum += e->frequency + 1;
1138 }
1139
1140 if (max_count)
1141 cost /= count_sum * 1000 / max_count + 1;
1142 else
1143 cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1144 if (dump_file)
1145 fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1146 cgraph_node_name (node), cost, inline_summary (node)->self_size,
1147 freq_sum);
1148 return cost + 1;
1149 }
1150
1151 /* Walk indirect calls of NODE and if any polymorphic can be turned into a
1152 direct one now, do so. */
1153
1154 static void
1155 ipcp_process_devirtualization_opportunities (struct cgraph_node *node)
1156 {
1157 struct ipa_node_params *info = IPA_NODE_REF (node);
1158 struct cgraph_edge *ie, *next_ie;
1159
1160 for (ie = node->indirect_calls; ie; ie = next_ie)
1161 {
1162 int param_index;
1163 HOST_WIDE_INT token, anc_offset;
1164 tree target, delta, otr_type;
1165 struct ipcp_lattice *lat;
1166
1167 next_ie = ie->next_callee;
1168 if (!ie->indirect_info->polymorphic)
1169 continue;
1170 param_index = ie->indirect_info->param_index;
1171 if (param_index == -1)
1172 continue;
1173
1174 lat = ipa_get_lattice (info, param_index);
1175 token = ie->indirect_info->otr_token;
1176 anc_offset = ie->indirect_info->anc_offset;
1177 otr_type = ie->indirect_info->otr_type;
1178 target = NULL_TREE;
1179 if (lat->type == IPA_CONST_VALUE)
1180 {
1181 tree binfo = gimple_extract_devirt_binfo_from_cst (lat->constant);
1182 if (!binfo)
1183 continue;
1184 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1185 if (!binfo)
1186 continue;
1187 target = gimple_get_virt_method_for_binfo (token, binfo, &delta,
1188 false);
1189 }
1190 else
1191 {
1192 int types_count, j;
1193
1194 if (ipa_param_cannot_devirtualize_p (info, param_index)
1195 || ipa_param_types_vec_empty (info, param_index))
1196 continue;
1197
1198 types_count = VEC_length (tree, info->params[param_index].types);
1199 for (j = 0; j < types_count; j++)
1200 {
1201 tree binfo = VEC_index (tree, info->params[param_index].types, j);
1202 tree d, t;
1203
1204 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1205 if (!binfo)
1206 {
1207 target = NULL_TREE;
1208 break;
1209 }
1210
1211 t = gimple_get_virt_method_for_binfo (token, binfo, &d, true);
1212 if (!t)
1213 {
1214 target = NULL_TREE;
1215 break;
1216 }
1217 else if (!target)
1218 {
1219 target = t;
1220 delta = d;
1221 }
1222 else if (target != t || !tree_int_cst_equal (delta, d))
1223 {
1224 target = NULL_TREE;
1225 break;
1226 }
1227 }
1228 }
1229
1230 if (target)
1231 ipa_make_edge_direct_to_target (ie, target, delta);
1232 }
1233 }
1234
1235 /* Return number of live constant parameters. */
1236 static int
1237 ipcp_const_param_count (struct cgraph_node *node)
1238 {
1239 int const_param = 0;
1240 struct ipa_node_params *info = IPA_NODE_REF (node);
1241 int count = ipa_get_param_count (info);
1242 int i;
1243
1244 for (i = 0; i < count; i++)
1245 {
1246 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1247 if ((ipcp_lat_is_insertable (lat)
1248 /* Do not count obviously unused arguments. */
1249 && ipa_is_param_used (info, i))
1250 || (!ipa_param_cannot_devirtualize_p (info, i)
1251 && !ipa_param_types_vec_empty (info, i)))
1252 const_param++;
1253 }
1254 return const_param;
1255 }
1256
1257 /* Given that a formal parameter of NODE given by INDEX is known to be constant
1258 CST, try to find any indirect edges that can be made direct and make them
1259 so. Note that INDEX is the number the parameter at the time of analyzing
1260 parameter uses and parameter removals should not be considered for it. (In
1261 fact, the parameter itself has just been removed.) */
1262
1263 static void
1264 ipcp_discover_new_direct_edges (struct cgraph_node *node, int index, tree cst)
1265 {
1266 struct cgraph_edge *ie, *next_ie;
1267
1268 for (ie = node->indirect_calls; ie; ie = next_ie)
1269 {
1270 struct cgraph_indirect_call_info *ici = ie->indirect_info;
1271
1272 next_ie = ie->next_callee;
1273 if (ici->param_index != index
1274 || ici->polymorphic)
1275 continue;
1276
1277 ipa_make_edge_direct_to_target (ie, cst, NULL_TREE);
1278 }
1279 }
1280
1281
1282 /* Propagate the constant parameters found by ipcp_iterate_stage()
1283 to the function's code. */
1284 static void
1285 ipcp_insert_stage (void)
1286 {
1287 struct cgraph_node *node, *node1 = NULL;
1288 int i;
1289 VEC (cgraph_edge_p, heap) * redirect_callers;
1290 VEC (ipa_replace_map_p,gc)* replace_trees;
1291 int node_callers, count;
1292 tree parm_tree;
1293 struct ipa_replace_map *replace_param;
1294 fibheap_t heap;
1295 long overall_size = 0, new_size = 0;
1296 long max_new_size;
1297
1298 ipa_check_create_node_params ();
1299 ipa_check_create_edge_args ();
1300 if (dump_file)
1301 fprintf (dump_file, "\nIPA insert stage:\n\n");
1302
1303 dead_nodes = BITMAP_ALLOC (NULL);
1304
1305 for (node = cgraph_nodes; node; node = node->next)
1306 if (node->analyzed)
1307 {
1308 if (node->count > max_count)
1309 max_count = node->count;
1310 overall_size += inline_summary (node)->self_size;
1311 }
1312
1313 max_new_size = overall_size;
1314 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1315 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1316 max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1317
1318 /* First collect all functions we proved to have constant arguments to
1319 heap. */
1320 heap = fibheap_new ();
1321 for (node = cgraph_nodes; node; node = node->next)
1322 {
1323 struct ipa_node_params *info;
1324 /* Propagation of the constant is forbidden in certain conditions. */
1325 if (!node->analyzed || !ipcp_node_modifiable_p (node))
1326 continue;
1327 info = IPA_NODE_REF (node);
1328 if (ipa_is_called_with_var_arguments (info))
1329 continue;
1330 if (ipcp_const_param_count (node))
1331 node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node),
1332 node);
1333 }
1334
1335 /* Now clone in priority order until code size growth limits are met or
1336 heap is emptied. */
1337 while (!fibheap_empty (heap))
1338 {
1339 struct ipa_node_params *info;
1340 int growth = 0;
1341 bitmap args_to_skip;
1342 struct cgraph_edge *cs;
1343
1344 node = (struct cgraph_node *)fibheap_extract_min (heap);
1345 node->aux = NULL;
1346 if (dump_file)
1347 fprintf (dump_file, "considering function %s\n",
1348 cgraph_node_name (node));
1349
1350 growth = ipcp_estimate_growth (node);
1351
1352 if (new_size + growth > max_new_size)
1353 break;
1354 if (growth
1355 && cgraph_optimize_for_size_p (node))
1356 {
1357 if (dump_file)
1358 fprintf (dump_file, "Not versioning, cold code would grow");
1359 continue;
1360 }
1361
1362 info = IPA_NODE_REF (node);
1363 count = ipa_get_param_count (info);
1364
1365 replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1366
1367 if (node->local.can_change_signature)
1368 args_to_skip = BITMAP_GGC_ALLOC ();
1369 else
1370 args_to_skip = NULL;
1371 for (i = 0; i < count; i++)
1372 {
1373 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1374 parm_tree = ipa_get_param (info, i);
1375
1376 /* We can proactively remove obviously unused arguments. */
1377 if (!ipa_is_param_used (info, i))
1378 {
1379 if (args_to_skip)
1380 bitmap_set_bit (args_to_skip, i);
1381 continue;
1382 }
1383
1384 if (lat->type == IPA_CONST_VALUE)
1385 {
1386 replace_param =
1387 ipcp_create_replace_map (parm_tree, lat);
1388 if (replace_param == NULL)
1389 break;
1390 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1391 if (args_to_skip)
1392 bitmap_set_bit (args_to_skip, i);
1393 }
1394 }
1395 if (i < count)
1396 {
1397 if (dump_file)
1398 fprintf (dump_file, "Not versioning, some parameters couldn't be replaced");
1399 continue;
1400 }
1401
1402 new_size += growth;
1403
1404 /* Look if original function becomes dead after cloning. */
1405 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1406 if (cs->caller == node || ipcp_need_redirect_p (cs))
1407 break;
1408 if (!cs && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1409 bitmap_set_bit (dead_nodes, node->uid);
1410
1411 /* Compute how many callers node has. */
1412 node_callers = 0;
1413 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1414 node_callers++;
1415 redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1416 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1417 if (!cs->indirect_inlining_edge)
1418 VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1419
1420 /* Redirecting all the callers of the node to the
1421 new versioned node. */
1422 node1 =
1423 cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1424 args_to_skip, "constprop");
1425 args_to_skip = NULL;
1426 VEC_free (cgraph_edge_p, heap, redirect_callers);
1427 replace_trees = NULL;
1428
1429 if (node1 == NULL)
1430 continue;
1431 ipcp_process_devirtualization_opportunities (node1);
1432
1433 if (dump_file)
1434 fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1435 cgraph_node_name (node), (int)growth, (int)new_size);
1436 ipcp_init_cloned_node (node, node1);
1437
1438 info = IPA_NODE_REF (node);
1439 for (i = 0; i < count; i++)
1440 {
1441 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1442 if (lat->type == IPA_CONST_VALUE)
1443 ipcp_discover_new_direct_edges (node1, i, lat->constant);
1444 }
1445
1446 if (dump_file)
1447 dump_function_to_file (node1->decl, dump_file, dump_flags);
1448
1449 for (cs = node->callees; cs; cs = cs->next_callee)
1450 if (cs->callee->aux)
1451 {
1452 fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1453 cs->callee->aux = fibheap_insert (heap,
1454 ipcp_estimate_cloning_cost (cs->callee),
1455 cs->callee);
1456 }
1457 }
1458
1459 while (!fibheap_empty (heap))
1460 {
1461 if (dump_file)
1462 fprintf (dump_file, "skipping function %s\n",
1463 cgraph_node_name (node));
1464 node = (struct cgraph_node *) fibheap_extract_min (heap);
1465 node->aux = NULL;
1466 }
1467 fibheap_delete (heap);
1468 BITMAP_FREE (dead_nodes);
1469 ipcp_update_callgraph ();
1470 ipcp_update_profiling ();
1471 }
1472
1473 /* The IPCP driver. */
1474 static unsigned int
1475 ipcp_driver (void)
1476 {
1477 cgraph_remove_unreachable_nodes (true,dump_file);
1478 if (dump_file)
1479 {
1480 fprintf (dump_file, "\nIPA structures before propagation:\n");
1481 if (dump_flags & TDF_DETAILS)
1482 ipa_print_all_params (dump_file);
1483 ipa_print_all_jump_functions (dump_file);
1484 }
1485 ipa_check_create_node_params ();
1486 ipa_check_create_edge_args ();
1487 /* 2. Do the interprocedural propagation. */
1488 ipcp_iterate_stage ();
1489 /* 3. Insert the constants found to the functions. */
1490 ipcp_insert_stage ();
1491 if (dump_file && (dump_flags & TDF_DETAILS))
1492 {
1493 fprintf (dump_file, "\nProfiling info after insert stage:\n");
1494 ipcp_print_profile_data (dump_file);
1495 }
1496 /* Free all IPCP structures. */
1497 ipa_free_all_structures_after_ipa_cp ();
1498 if (dump_file)
1499 fprintf (dump_file, "\nIPA constant propagation end\n");
1500 return 0;
1501 }
1502
1503 /* Initialization and computation of IPCP data structures. This is the initial
1504 intraprocedural analysis of functions, which gathers information to be
1505 propagated later on. */
1506
1507 static void
1508 ipcp_generate_summary (void)
1509 {
1510 struct cgraph_node *node;
1511
1512 if (dump_file)
1513 fprintf (dump_file, "\nIPA constant propagation start:\n");
1514 ipa_register_cgraph_hooks ();
1515
1516 /* FIXME: We could propagate through thunks happily and we could be
1517 even able to clone them, if needed. Do that later. */
1518 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1519 {
1520 /* Unreachable nodes should have been eliminated before ipcp. */
1521 gcc_assert (node->needed || node->reachable);
1522
1523 inline_summary (node)->versionable = tree_versionable_function_p (node->decl);
1524 ipa_analyze_node (node);
1525 }
1526 }
1527
1528 /* Write ipcp summary for nodes in SET. */
1529 static void
1530 ipcp_write_summary (cgraph_node_set set,
1531 varpool_node_set vset ATTRIBUTE_UNUSED)
1532 {
1533 ipa_prop_write_jump_functions (set);
1534 }
1535
1536 /* Read ipcp summary. */
1537 static void
1538 ipcp_read_summary (void)
1539 {
1540 ipa_prop_read_jump_functions ();
1541 }
1542
1543 /* Gate for IPCP optimization. */
1544 static bool
1545 cgraph_gate_cp (void)
1546 {
1547 /* FIXME: We should remove the optimize check after we ensure we never run
1548 IPA passes when not optimizing. */
1549 return flag_ipa_cp && optimize;
1550 }
1551
1552 struct ipa_opt_pass_d pass_ipa_cp =
1553 {
1554 {
1555 IPA_PASS,
1556 "cp", /* name */
1557 cgraph_gate_cp, /* gate */
1558 ipcp_driver, /* execute */
1559 NULL, /* sub */
1560 NULL, /* next */
1561 0, /* static_pass_number */
1562 TV_IPA_CONSTANT_PROP, /* tv_id */
1563 0, /* properties_required */
1564 0, /* properties_provided */
1565 0, /* properties_destroyed */
1566 0, /* todo_flags_start */
1567 TODO_dump_cgraph | TODO_dump_func |
1568 TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
1569 },
1570 ipcp_generate_summary, /* generate_summary */
1571 ipcp_write_summary, /* write_summary */
1572 ipcp_read_summary, /* read_summary */
1573 NULL, /* write_optimization_summary */
1574 NULL, /* read_optimization_summary */
1575 NULL, /* stmt_fixup */
1576 0, /* TODOs */
1577 NULL, /* function_transform */
1578 NULL, /* variable_transform */
1579 };