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