[PATCH] Report LTO phase in lto1 process name v2
[official-gcc.git] / gcc / ipa-cp.c
blob140dfc017537827bac3376cd62bd95d119476d20
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>
6 This file is part of GCC.
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.
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.
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/>. */
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:
27 int g (int y)
29 printf ("value is %d",y);
32 int f (int x)
34 g (x);
37 int h (int y)
39 g (y);
42 void main (void)
44 f (3);
45 h (3);
49 The IPCP algorithm will find that g's formal argument y is always called
50 with the value 3.
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
56 The optimization is divided into three stages:
58 First stage - intraprocedural analysis
59 =======================================
60 This phase computes jump_function and modification flags.
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.
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).
73 -ipcp_generate_summary() is the first stage driver.
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.
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.
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).
90 -ipcp_iterate_stage() is the second stage driver.
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.
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.
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.
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.
118 -ipcp_insert_stage() is the third phase driver.
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.
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"
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;
157 /* Maximal count found in program. */
158 static gcov_type max_count;
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;
164 static void ipcp_print_profile_data (FILE *);
165 static void ipcp_function_scale_print (FILE *);
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)
171 return IPA_NODE_REF (node)->ipcp_orig_node;
174 /* Return true if NODE describes a cloned/versioned function. */
175 static inline bool
176 ipcp_node_is_clone (struct cgraph_node *node)
178 return (ipcp_get_orig_node (node) != NULL);
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)
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;
195 /* Return scale for NODE. */
196 static inline gcov_type
197 ipcp_get_node_scale (struct cgraph_node *node)
199 return IPA_NODE_REF (node)->count_scale;
202 /* Set COUNT as scale for NODE. */
203 static inline void
204 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
206 IPA_NODE_REF (node)->count_scale = count;
209 /* Return whether LAT is a constant lattice. */
210 static inline bool
211 ipcp_lat_is_const (struct ipcp_lattice *lat)
213 if (lat->type == IPA_CONST_VALUE)
214 return true;
215 else
216 return false;
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)
224 return lat->type == IPA_CONST_VALUE;
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)
231 gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
232 if (lat1->type != lat2->type)
233 return false;
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);
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)
254 if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
256 res->type = IPA_BOTTOM;
257 return;
259 if (lat1->type == IPA_TOP)
261 res->type = lat2->type;
262 res->constant = lat2->constant;
263 return;
265 if (lat2->type == IPA_TOP)
267 res->type = lat1->type;
268 res->constant = lat1->constant;
269 return;
271 if (!ipcp_lats_are_equal (lat1, lat2))
273 res->type = IPA_BOTTOM;
274 return;
276 res->type = lat1->type;
277 res->constant = lat1->constant;
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)
285 return &(info->params[i].ipcp_lattice);
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)
295 if (jfunc->type == IPA_JF_CONST)
297 lat->type = IPA_CONST_VALUE;
298 lat->constant = jfunc->value.constant;
300 else if (jfunc->type == IPA_JF_PASS_THROUGH)
302 struct ipcp_lattice *caller_lat;
303 tree cst;
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;
311 if (jfunc->value.pass_through.operation != NOP_EXPR)
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);
322 if (!cst || !is_gimple_ip_invariant (cst))
323 lat->type = IPA_BOTTOM;
324 lat->constant = cst;
326 else if (jfunc->type == IPA_JF_ANCESTOR)
328 struct ipcp_lattice *caller_lat;
329 tree t;
331 caller_lat = ipcp_get_lattice (info, jfunc->value.ancestor.formal_id);
332 lat->type = caller_lat->type;
333 if (caller_lat->type != IPA_CONST_VALUE)
334 return;
335 if (TREE_CODE (caller_lat->constant) != ADDR_EXPR)
337 /* This can happen when the constant is a NULL pointer. */
338 lat->type = IPA_BOTTOM;
339 return;
341 t = TREE_OPERAND (caller_lat->constant, 0);
342 t = build_ref_for_offset (EXPR_LOCATION (t), t,
343 jfunc->value.ancestor.offset,
344 jfunc->value.ancestor.type, NULL, false);
345 lat->constant = build_fold_addr_expr (t);
347 else
348 lat->type = IPA_BOTTOM;
351 /* True when OLD_LAT and NEW_LAT values are not the same. */
353 static bool
354 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
355 struct ipcp_lattice *new_lat)
357 if (old_lat->type == new_lat->type)
359 if (!ipcp_lat_is_const (old_lat))
360 return false;
361 if (ipcp_lats_are_equal (old_lat, new_lat))
362 return false;
364 return true;
367 /* Print all ipcp_lattices of all functions to F. */
368 static void
369 ipcp_print_all_lattices (FILE * f)
371 struct cgraph_node *node;
372 int i, count;
374 fprintf (f, "\nLattice:\n");
375 for (node = cgraph_nodes; node; node = node->next)
377 struct ipa_node_params *info;
379 if (!node->analyzed)
380 continue;
381 info = IPA_NODE_REF (node);
382 fprintf (f, " Node: %s:\n", cgraph_node_name (node));
383 count = ipa_get_param_count (info);
384 for (i = 0; i < count; i++)
386 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
388 fprintf (f, " param [%d]: ", i);
389 if (lat->type == IPA_CONST_VALUE)
391 tree cst = lat->constant;
392 fprintf (f, "type is CONST ");
393 print_generic_expr (f, cst, 0);
394 if (TREE_CODE (cst) == ADDR_EXPR
395 && TREE_CODE (TREE_OPERAND (cst, 0)) == CONST_DECL)
397 fprintf (f, " -> ");
398 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (cst, 0)),
402 else if (lat->type == IPA_TOP)
403 fprintf (f, "type is TOP");
404 else
405 fprintf (f, "type is BOTTOM");
406 if (ipa_param_cannot_devirtualize_p (info, i))
407 fprintf (f, " - cannot_devirtualize set\n");
408 else if (ipa_param_types_vec_empty (info, i))
409 fprintf (f, " - type list empty\n");
410 else
411 fprintf (f, "\n");
416 /* Return true if ipcp algorithms would allow cloning NODE. */
418 static bool
419 ipcp_versionable_function_p (struct cgraph_node *node)
421 struct cgraph_edge *edge;
423 /* There are a number of generic reasons functions cannot be versioned. We
424 also cannot remove parameters if there are type attributes such as fnspec
425 present. */
426 if (!node->local.versionable
427 || TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
428 return false;
430 /* Removing arguments doesn't work if the function takes varargs
431 or use __builtin_apply_args. */
432 for (edge = node->callees; edge; edge = edge->next_callee)
434 tree t = edge->callee->decl;
435 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
436 && (DECL_FUNCTION_CODE (t) == BUILT_IN_APPLY_ARGS
437 || DECL_FUNCTION_CODE (t) == BUILT_IN_VA_START))
438 return false;
441 return true;
444 /* Return true if this NODE is viable candidate for cloning. */
445 static bool
446 ipcp_cloning_candidate_p (struct cgraph_node *node)
448 int n_calls = 0;
449 int n_hot_calls = 0;
450 gcov_type direct_call_sum = 0;
451 struct cgraph_edge *e;
453 /* We never clone functions that are not visible from outside.
454 FIXME: in future we should clone such functions when they are called with
455 different constants, but current ipcp implementation is not good on this.
457 if (cgraph_only_called_directly_p (node) || !node->analyzed)
458 return false;
460 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
462 if (dump_file)
463 fprintf (dump_file, "Not considering %s for cloning; body is overwrittable.\n",
464 cgraph_node_name (node));
465 return false;
467 if (!ipcp_versionable_function_p (node))
469 if (dump_file)
470 fprintf (dump_file, "Not considering %s for cloning; body is not versionable.\n",
471 cgraph_node_name (node));
472 return false;
474 for (e = node->callers; e; e = e->next_caller)
476 direct_call_sum += e->count;
477 n_calls ++;
478 if (cgraph_maybe_hot_edge_p (e))
479 n_hot_calls ++;
482 if (!n_calls)
484 if (dump_file)
485 fprintf (dump_file, "Not considering %s for cloning; no direct calls.\n",
486 cgraph_node_name (node));
487 return false;
489 if (node->local.inline_summary.self_size < n_calls)
491 if (dump_file)
492 fprintf (dump_file, "Considering %s for cloning; code would shrink.\n",
493 cgraph_node_name (node));
494 return true;
497 if (!flag_ipa_cp_clone)
499 if (dump_file)
500 fprintf (dump_file, "Not considering %s for cloning; -fipa-cp-clone disabled.\n",
501 cgraph_node_name (node));
502 return false;
505 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
507 if (dump_file)
508 fprintf (dump_file, "Not considering %s for cloning; optimizing it for size.\n",
509 cgraph_node_name (node));
510 return false;
513 /* When profile is available and function is hot, propagate into it even if
514 calls seems cold; constant propagation can improve function's speed
515 significandly. */
516 if (max_count)
518 if (direct_call_sum > node->count * 90 / 100)
520 if (dump_file)
521 fprintf (dump_file, "Considering %s for cloning; usually called directly.\n",
522 cgraph_node_name (node));
523 return true;
526 if (!n_hot_calls)
528 if (dump_file)
529 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
530 cgraph_node_name (node));
531 return false;
533 if (dump_file)
534 fprintf (dump_file, "Considering %s for cloning.\n",
535 cgraph_node_name (node));
536 return true;
539 /* Mark parameter with index I of function described by INFO as unsuitable for
540 devirtualization. Return true if it has already been marked so. */
542 static bool
543 ipa_set_param_cannot_devirtualize (struct ipa_node_params *info, int i)
545 bool ret = info->params[i].cannot_devirtualize;
546 info->params[i].cannot_devirtualize = true;
547 if (info->params[i].types)
548 VEC_free (tree, heap, info->params[i].types);
549 return ret;
552 /* Initialize ipcp_lattices array. The lattices corresponding to supported
553 types (integers, real types and Fortran constants defined as const_decls)
554 are initialized to IPA_TOP, the rest of them to IPA_BOTTOM. */
555 static void
556 ipcp_initialize_node_lattices (struct cgraph_node *node)
558 int i;
559 struct ipa_node_params *info = IPA_NODE_REF (node);
560 enum ipa_lattice_type type;
562 if (ipa_is_called_with_var_arguments (info))
563 type = IPA_BOTTOM;
564 else if (cgraph_only_called_directly_p (node))
565 type = IPA_TOP;
566 /* When cloning is allowed, we can assume that externally visible functions
567 are not called. We will compensate this by cloning later. */
568 else if (ipcp_cloning_candidate_p (node))
569 type = IPA_TOP, n_cloning_candidates ++;
570 else
571 type = IPA_BOTTOM;
573 for (i = 0; i < ipa_get_param_count (info) ; i++)
575 ipcp_get_lattice (info, i)->type = type;
576 if (type == IPA_BOTTOM)
577 ipa_set_param_cannot_devirtualize (info, i);
581 /* build INTEGER_CST tree with type TREE_TYPE and value according to LAT.
582 Return the tree. */
583 static tree
584 build_const_val (struct ipcp_lattice *lat, tree tree_type)
586 tree val;
588 gcc_assert (ipcp_lat_is_const (lat));
589 val = lat->constant;
591 if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
593 if (fold_convertible_p (tree_type, val))
594 return fold_build1 (NOP_EXPR, tree_type, val);
595 else
596 return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
598 return val;
601 /* Compute the proper scale for NODE. It is the ratio between the number of
602 direct calls (represented on the incoming cgraph_edges) and sum of all
603 invocations of NODE (represented as count in cgraph_node).
605 FIXME: This code is wrong. Since the callers can be also clones and
606 the clones are not scaled yet, the sums gets unrealistically high.
607 To properly compute the counts, we would need to do propagation across
608 callgraph (as external call to A might imply call to non-clonned B
609 if A's clone calls clonned B). */
610 static void
611 ipcp_compute_node_scale (struct cgraph_node *node)
613 gcov_type sum;
614 struct cgraph_edge *cs;
616 sum = 0;
617 /* Compute sum of all counts of callers. */
618 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
619 sum += cs->count;
620 /* Work around the unrealistically high sum problem. We just don't want
621 the non-cloned body to have negative or very low frequency. Since
622 majority of execution time will be spent in clones anyway, this should
623 give good enough profile. */
624 if (sum > node->count * 9 / 10)
625 sum = node->count * 9 / 10;
626 if (node->count == 0)
627 ipcp_set_node_scale (node, 0);
628 else
629 ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
632 /* Return true if there are some formal parameters whose value is IPA_TOP (in
633 the whole compilation unit). Change their values to IPA_BOTTOM, since they
634 most probably get their values from outside of this compilation unit. */
635 static bool
636 ipcp_change_tops_to_bottom (void)
638 int i, count;
639 struct cgraph_node *node;
640 bool prop_again;
642 prop_again = false;
643 for (node = cgraph_nodes; node; node = node->next)
645 struct ipa_node_params *info = IPA_NODE_REF (node);
646 count = ipa_get_param_count (info);
647 for (i = 0; i < count; i++)
649 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
650 if (lat->type == IPA_TOP)
652 prop_again = true;
653 if (dump_file)
655 fprintf (dump_file, "Forcing param ");
656 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
657 fprintf (dump_file, " of node %s to bottom.\n",
658 cgraph_node_name (node));
660 lat->type = IPA_BOTTOM;
662 if (!ipa_param_cannot_devirtualize_p (info, i)
663 && ipa_param_types_vec_empty (info, i))
665 prop_again = true;
666 ipa_set_param_cannot_devirtualize (info, i);
667 if (dump_file)
669 fprintf (dump_file, "Marking param ");
670 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
671 fprintf (dump_file, " of node %s as unusable for "
672 "devirtualization.\n",
673 cgraph_node_name (node));
678 return prop_again;
681 /* Insert BINFO to the list of known types of parameter number I of the
682 function described by CALLEE_INFO. Return true iff the type information
683 associated with the callee parameter changed in any way. */
685 static bool
686 ipcp_add_param_type (struct ipa_node_params *callee_info, int i, tree binfo)
688 int j, count;
690 if (ipa_param_cannot_devirtualize_p (callee_info, i))
691 return false;
693 if (callee_info->params[i].types)
695 count = VEC_length (tree, callee_info->params[i].types);
696 for (j = 0; j < count; j++)
697 if (VEC_index (tree, callee_info->params[i].types, j) == binfo)
698 return false;
701 if (VEC_length (tree, callee_info->params[i].types)
702 == (unsigned) PARAM_VALUE (PARAM_DEVIRT_TYPE_LIST_SIZE))
703 return !ipa_set_param_cannot_devirtualize (callee_info, i);
705 VEC_safe_push (tree, heap, callee_info->params[i].types, binfo);
706 return true;
709 /* Copy known types information for parameter number CALLEE_IDX of CALLEE_INFO
710 from a parameter of CALLER_INFO as described by JF. Return true iff the
711 type information changed in any way. JF must be a pass-through or an
712 ancestor jump function. */
714 static bool
715 ipcp_copy_types (struct ipa_node_params *caller_info,
716 struct ipa_node_params *callee_info,
717 int callee_idx, struct ipa_jump_func *jf)
719 int caller_idx, j, count;
720 bool res;
722 if (ipa_param_cannot_devirtualize_p (callee_info, callee_idx))
723 return false;
725 if (jf->type == IPA_JF_PASS_THROUGH)
727 if (jf->value.pass_through.operation != NOP_EXPR)
729 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
730 return true;
732 caller_idx = jf->value.pass_through.formal_id;
734 else
735 caller_idx = jf->value.ancestor.formal_id;
737 if (ipa_param_cannot_devirtualize_p (caller_info, caller_idx))
739 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
740 return true;
743 if (!caller_info->params[caller_idx].types)
744 return false;
746 res = false;
747 count = VEC_length (tree, caller_info->params[caller_idx].types);
748 for (j = 0; j < count; j++)
750 tree binfo = VEC_index (tree, caller_info->params[caller_idx].types, j);
751 if (jf->type == IPA_JF_ANCESTOR)
753 binfo = get_binfo_at_offset (binfo, jf->value.ancestor.offset,
754 jf->value.ancestor.type);
755 if (!binfo)
757 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
758 return true;
761 res |= ipcp_add_param_type (callee_info, callee_idx, binfo);
763 return res;
766 /* Propagate type information for parameter of CALLEE_INFO number I as
767 described by JF. CALLER_INFO describes the caller. Return true iff the
768 type information changed in any way. */
770 static bool
771 ipcp_propagate_types (struct ipa_node_params *caller_info,
772 struct ipa_node_params *callee_info,
773 struct ipa_jump_func *jf, int i)
775 tree cst, binfo;
777 switch (jf->type)
779 case IPA_JF_UNKNOWN:
780 case IPA_JF_CONST_MEMBER_PTR:
781 break;
783 case IPA_JF_KNOWN_TYPE:
784 return ipcp_add_param_type (callee_info, i, jf->value.base_binfo);
786 case IPA_JF_CONST:
787 cst = jf->value.constant;
788 if (TREE_CODE (cst) != ADDR_EXPR)
789 break;
790 binfo = gimple_get_relevant_ref_binfo (TREE_OPERAND (cst, 0), NULL_TREE);
791 if (!binfo)
792 break;
793 return ipcp_add_param_type (callee_info, i, binfo);
795 case IPA_JF_PASS_THROUGH:
796 case IPA_JF_ANCESTOR:
797 return ipcp_copy_types (caller_info, callee_info, i, jf);
800 /* If we reach this we cannot use this parameter for devirtualization. */
801 return !ipa_set_param_cannot_devirtualize (callee_info, i);
804 /* Interprocedural analysis. The algorithm propagates constants from the
805 caller's parameters to the callee's arguments. */
806 static void
807 ipcp_propagate_stage (void)
809 int i;
810 struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
811 struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
812 struct ipcp_lattice *dest_lat;
813 struct cgraph_edge *cs;
814 struct ipa_jump_func *jump_func;
815 struct ipa_func_list *wl;
816 int count;
818 ipa_check_create_node_params ();
819 ipa_check_create_edge_args ();
821 /* Initialize worklist to contain all functions. */
822 wl = ipa_init_func_list ();
823 while (wl)
825 struct cgraph_node *node = ipa_pop_func_from_list (&wl);
826 struct ipa_node_params *info = IPA_NODE_REF (node);
828 for (cs = node->callees; cs; cs = cs->next_callee)
830 struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
831 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
833 if (ipa_is_called_with_var_arguments (callee_info)
834 || !cs->callee->analyzed
835 || ipa_is_called_with_var_arguments (callee_info))
836 continue;
838 count = ipa_get_cs_argument_count (args);
839 for (i = 0; i < count; i++)
841 jump_func = ipa_get_ith_jump_func (args, i);
842 ipcp_lattice_from_jfunc (info, &inc_lat, jump_func);
843 dest_lat = ipcp_get_lattice (callee_info, i);
844 ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
845 if (ipcp_lattice_changed (&new_lat, dest_lat))
847 dest_lat->type = new_lat.type;
848 dest_lat->constant = new_lat.constant;
849 ipa_push_func_to_list (&wl, cs->callee);
852 if (ipcp_propagate_types (info, callee_info, jump_func, i))
853 ipa_push_func_to_list (&wl, cs->callee);
859 /* Call the constant propagation algorithm and re-call it if necessary
860 (if there are undetermined values left). */
861 static void
862 ipcp_iterate_stage (void)
864 struct cgraph_node *node;
865 n_cloning_candidates = 0;
867 if (dump_file)
868 fprintf (dump_file, "\nIPA iterate stage:\n\n");
870 if (in_lto_p)
871 ipa_update_after_lto_read ();
873 for (node = cgraph_nodes; node; node = node->next)
875 ipcp_initialize_node_lattices (node);
876 ipcp_compute_node_scale (node);
878 if (dump_file && (dump_flags & TDF_DETAILS))
880 ipcp_print_all_lattices (dump_file);
881 ipcp_function_scale_print (dump_file);
884 ipcp_propagate_stage ();
885 if (ipcp_change_tops_to_bottom ())
886 /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
887 This change should be propagated. */
889 gcc_assert (n_cloning_candidates);
890 ipcp_propagate_stage ();
892 if (dump_file)
894 fprintf (dump_file, "\nIPA lattices after propagation:\n");
895 ipcp_print_all_lattices (dump_file);
896 if (dump_flags & TDF_DETAILS)
897 ipcp_print_profile_data (dump_file);
901 /* Check conditions to forbid constant insertion to function described by
902 NODE. */
903 static inline bool
904 ipcp_node_modifiable_p (struct cgraph_node *node)
906 /* Once we will be able to do in-place replacement, we can be more
907 lax here. */
908 return ipcp_versionable_function_p (node);
911 /* Print count scale data structures. */
912 static void
913 ipcp_function_scale_print (FILE * f)
915 struct cgraph_node *node;
917 for (node = cgraph_nodes; node; node = node->next)
919 if (!node->analyzed)
920 continue;
921 fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
922 fprintf (f, "value is " HOST_WIDE_INT_PRINT_DEC
923 " \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
927 /* Print counts of all cgraph nodes. */
928 static void
929 ipcp_print_func_profile_counts (FILE * f)
931 struct cgraph_node *node;
933 for (node = cgraph_nodes; node; node = node->next)
935 fprintf (f, "function %s: ", cgraph_node_name (node));
936 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC
937 " \n", (HOST_WIDE_INT) node->count);
941 /* Print counts of all cgraph edges. */
942 static void
943 ipcp_print_call_profile_counts (FILE * f)
945 struct cgraph_node *node;
946 struct cgraph_edge *cs;
948 for (node = cgraph_nodes; node; node = node->next)
950 for (cs = node->callees; cs; cs = cs->next_callee)
952 fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
953 cgraph_node_name (cs->callee));
954 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC " \n",
955 (HOST_WIDE_INT) cs->count);
960 /* Print profile info for all functions. */
961 static void
962 ipcp_print_profile_data (FILE * f)
964 fprintf (f, "\nNODE COUNTS :\n");
965 ipcp_print_func_profile_counts (f);
966 fprintf (f, "\nCS COUNTS stage:\n");
967 ipcp_print_call_profile_counts (f);
970 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
971 processed by versioning, which operates according to the flags set.
972 PARM_TREE is the formal parameter found to be constant. LAT represents the
973 constant. */
974 static struct ipa_replace_map *
975 ipcp_create_replace_map (tree parm_tree, struct ipcp_lattice *lat)
977 struct ipa_replace_map *replace_map;
978 tree const_val;
980 replace_map = ggc_alloc_ipa_replace_map ();
981 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
982 if (dump_file)
984 fprintf (dump_file, " replacing param ");
985 print_generic_expr (dump_file, parm_tree, 0);
986 fprintf (dump_file, " with const ");
987 print_generic_expr (dump_file, const_val, 0);
988 fprintf (dump_file, "\n");
990 replace_map->old_tree = parm_tree;
991 replace_map->new_tree = const_val;
992 replace_map->replace_p = true;
993 replace_map->ref_p = false;
995 return replace_map;
998 /* Return true if this callsite should be redirected to the original callee
999 (instead of the cloned one). */
1000 static bool
1001 ipcp_need_redirect_p (struct cgraph_edge *cs)
1003 struct ipa_node_params *orig_callee_info;
1004 int i, count;
1005 struct cgraph_node *node = cs->callee, *orig;
1007 if (!n_cloning_candidates)
1008 return false;
1010 if ((orig = ipcp_get_orig_node (node)) != NULL)
1011 node = orig;
1012 if (ipcp_get_orig_node (cs->caller))
1013 return false;
1015 orig_callee_info = IPA_NODE_REF (node);
1016 count = ipa_get_param_count (orig_callee_info);
1017 for (i = 0; i < count; i++)
1019 struct ipcp_lattice *lat = ipcp_get_lattice (orig_callee_info, i);
1020 struct ipa_jump_func *jump_func;
1022 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
1023 if ((ipcp_lat_is_const (lat)
1024 && jump_func->type != IPA_JF_CONST)
1025 || (!ipa_param_cannot_devirtualize_p (orig_callee_info, i)
1026 && !ipa_param_types_vec_empty (orig_callee_info, i)
1027 && jump_func->type != IPA_JF_CONST
1028 && jump_func->type != IPA_JF_KNOWN_TYPE))
1029 return true;
1032 return false;
1035 /* Fix the callsites and the call graph after function cloning was done. */
1036 static void
1037 ipcp_update_callgraph (void)
1039 struct cgraph_node *node;
1041 for (node = cgraph_nodes; node; node = node->next)
1042 if (node->analyzed && ipcp_node_is_clone (node))
1044 bitmap args_to_skip = BITMAP_ALLOC (NULL);
1045 struct cgraph_node *orig_node = ipcp_get_orig_node (node);
1046 struct ipa_node_params *info = IPA_NODE_REF (orig_node);
1047 int i, count = ipa_get_param_count (info);
1048 struct cgraph_edge *cs, *next;
1050 for (i = 0; i < count; i++)
1052 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1054 /* We can proactively remove obviously unused arguments. */
1055 if (!ipa_is_param_used (info, i))
1057 bitmap_set_bit (args_to_skip, i);
1058 continue;
1061 if (lat->type == IPA_CONST_VALUE)
1062 bitmap_set_bit (args_to_skip, i);
1064 for (cs = node->callers; cs; cs = next)
1066 next = cs->next_caller;
1067 if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
1069 if (dump_file)
1070 fprintf (dump_file, "Redirecting edge %s/%i -> %s/%i "
1071 "back to %s/%i.",
1072 cgraph_node_name (cs->caller), cs->caller->uid,
1073 cgraph_node_name (cs->callee), cs->callee->uid,
1074 cgraph_node_name (orig_node), orig_node->uid);
1075 cgraph_redirect_edge_callee (cs, orig_node);
1081 /* Update profiling info for versioned functions and the functions they were
1082 versioned from. */
1083 static void
1084 ipcp_update_profiling (void)
1086 struct cgraph_node *node, *orig_node;
1087 gcov_type scale, scale_complement;
1088 struct cgraph_edge *cs;
1090 for (node = cgraph_nodes; node; node = node->next)
1092 if (ipcp_node_is_clone (node))
1094 orig_node = ipcp_get_orig_node (node);
1095 scale = ipcp_get_node_scale (orig_node);
1096 node->count = orig_node->count * scale / REG_BR_PROB_BASE;
1097 scale_complement = REG_BR_PROB_BASE - scale;
1098 orig_node->count =
1099 orig_node->count * scale_complement / REG_BR_PROB_BASE;
1100 for (cs = node->callees; cs; cs = cs->next_callee)
1101 cs->count = cs->count * scale / REG_BR_PROB_BASE;
1102 for (cs = orig_node->callees; cs; cs = cs->next_callee)
1103 cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
1108 /* If NODE was cloned, how much would program grow? */
1109 static long
1110 ipcp_estimate_growth (struct cgraph_node *node)
1112 struct cgraph_edge *cs;
1113 int redirectable_node_callers = 0;
1114 int removable_args = 0;
1115 bool need_original
1116 = !cgraph_will_be_removed_from_program_if_no_direct_calls (node);
1117 struct ipa_node_params *info;
1118 int i, count;
1119 int growth;
1121 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1122 if (cs->caller == node || !ipcp_need_redirect_p (cs))
1123 redirectable_node_callers++;
1124 else
1125 need_original = true;
1127 /* If we will be able to fully replace orignal node, we never increase
1128 program size. */
1129 if (!need_original)
1130 return 0;
1132 info = IPA_NODE_REF (node);
1133 count = ipa_get_param_count (info);
1134 for (i = 0; i < count; i++)
1136 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1138 /* We can proactively remove obviously unused arguments. */
1139 if (!ipa_is_param_used (info, i))
1140 removable_args++;
1142 if (lat->type == IPA_CONST_VALUE)
1143 removable_args++;
1146 /* We make just very simple estimate of savings for removal of operand from
1147 call site. Precise cost is dificult to get, as our size metric counts
1148 constants and moves as free. Generally we are looking for cases that
1149 small function is called very many times. */
1150 growth = node->local.inline_summary.self_size
1151 - removable_args * redirectable_node_callers;
1152 if (growth < 0)
1153 return 0;
1154 return growth;
1158 /* Estimate cost of cloning NODE. */
1159 static long
1160 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1162 int freq_sum = 1;
1163 gcov_type count_sum = 1;
1164 struct cgraph_edge *e;
1165 int cost;
1167 cost = ipcp_estimate_growth (node) * 1000;
1168 if (!cost)
1170 if (dump_file)
1171 fprintf (dump_file, "Versioning of %s will save code size\n",
1172 cgraph_node_name (node));
1173 return 0;
1176 for (e = node->callers; e; e = e->next_caller)
1177 if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1178 && !ipcp_need_redirect_p (e))
1180 count_sum += e->count;
1181 freq_sum += e->frequency + 1;
1184 if (max_count)
1185 cost /= count_sum * 1000 / max_count + 1;
1186 else
1187 cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1188 if (dump_file)
1189 fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1190 cgraph_node_name (node), cost, node->local.inline_summary.self_size,
1191 freq_sum);
1192 return cost + 1;
1195 /* Walk indirect calls of NODE and if any polymorphic can be turned into a
1196 direct one now, do so. */
1198 static void
1199 ipcp_process_devirtualization_opportunities (struct cgraph_node *node)
1201 struct ipa_node_params *info = IPA_NODE_REF (node);
1202 struct cgraph_edge *ie, *next_ie;
1204 for (ie = node->indirect_calls; ie; ie = next_ie)
1206 int param_index, types_count, j;
1207 HOST_WIDE_INT token;
1208 tree target;
1210 next_ie = ie->next_callee;
1211 if (!ie->indirect_info->polymorphic)
1212 continue;
1213 param_index = ie->indirect_info->param_index;
1214 if (param_index == -1
1215 || ipa_param_cannot_devirtualize_p (info, param_index)
1216 || ipa_param_types_vec_empty (info, param_index))
1217 continue;
1219 token = ie->indirect_info->otr_token;
1220 target = NULL_TREE;
1221 types_count = VEC_length (tree, info->params[param_index].types);
1222 for (j = 0; j < types_count; j++)
1224 tree binfo = VEC_index (tree, info->params[param_index].types, j);
1225 tree t = gimple_fold_obj_type_ref_known_binfo (token, binfo);
1227 if (!t)
1229 target = NULL_TREE;
1230 break;
1232 else if (!target)
1233 target = t;
1234 else if (target != t)
1236 target = NULL_TREE;
1237 break;
1241 if (target)
1242 ipa_make_edge_direct_to_target (ie, target);
1246 /* Return number of live constant parameters. */
1247 static int
1248 ipcp_const_param_count (struct cgraph_node *node)
1250 int const_param = 0;
1251 struct ipa_node_params *info = IPA_NODE_REF (node);
1252 int count = ipa_get_param_count (info);
1253 int i;
1255 for (i = 0; i < count; i++)
1257 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1258 if ((ipcp_lat_is_insertable (lat)
1259 /* Do not count obviously unused arguments. */
1260 && ipa_is_param_used (info, i))
1261 || (!ipa_param_cannot_devirtualize_p (info, i)
1262 && !ipa_param_types_vec_empty (info, i)))
1263 const_param++;
1265 return const_param;
1268 /* Given that a formal parameter of NODE given by INDEX is known to be constant
1269 CST, try to find any indirect edges that can be made direct and make them
1270 so. Note that INDEX is the number the parameter at the time of analyzing
1271 parameter uses and parameter removals should not be considered for it. (In
1272 fact, the parameter itself has just been removed.) */
1274 static void
1275 ipcp_discover_new_direct_edges (struct cgraph_node *node, int index, tree cst)
1277 struct cgraph_edge *ie, *next_ie;
1279 for (ie = node->indirect_calls; ie; ie = next_ie)
1281 struct cgraph_indirect_call_info *ici = ie->indirect_info;
1283 next_ie = ie->next_callee;
1284 if (ici->param_index != index)
1285 continue;
1287 if (ici->polymorphic)
1289 tree binfo;
1290 HOST_WIDE_INT token;
1292 if (TREE_CODE (cst) != ADDR_EXPR)
1293 continue;
1295 binfo = gimple_get_relevant_ref_binfo (TREE_OPERAND (cst, 0),
1296 NULL_TREE);
1297 if (!binfo)
1298 continue;
1299 gcc_assert (ie->indirect_info->anc_offset == 0);
1300 token = ie->indirect_info->otr_token;
1301 cst = gimple_fold_obj_type_ref_known_binfo (token, binfo);
1302 if (!cst)
1303 continue;
1306 ipa_make_edge_direct_to_target (ie, cst);
1311 /* Propagate the constant parameters found by ipcp_iterate_stage()
1312 to the function's code. */
1313 static void
1314 ipcp_insert_stage (void)
1316 struct cgraph_node *node, *node1 = NULL;
1317 int i;
1318 VEC (cgraph_edge_p, heap) * redirect_callers;
1319 VEC (ipa_replace_map_p,gc)* replace_trees;
1320 int node_callers, count;
1321 tree parm_tree;
1322 struct ipa_replace_map *replace_param;
1323 fibheap_t heap;
1324 long overall_size = 0, new_size = 0;
1325 long max_new_size;
1327 ipa_check_create_node_params ();
1328 ipa_check_create_edge_args ();
1329 if (dump_file)
1330 fprintf (dump_file, "\nIPA insert stage:\n\n");
1332 dead_nodes = BITMAP_ALLOC (NULL);
1334 for (node = cgraph_nodes; node; node = node->next)
1335 if (node->analyzed)
1337 if (node->count > max_count)
1338 max_count = node->count;
1339 overall_size += node->local.inline_summary.self_size;
1342 max_new_size = overall_size;
1343 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1344 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1345 max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1347 /* First collect all functions we proved to have constant arguments to
1348 heap. */
1349 heap = fibheap_new ();
1350 for (node = cgraph_nodes; node; node = node->next)
1352 struct ipa_node_params *info;
1353 /* Propagation of the constant is forbidden in certain conditions. */
1354 if (!node->analyzed || !ipcp_node_modifiable_p (node))
1355 continue;
1356 info = IPA_NODE_REF (node);
1357 if (ipa_is_called_with_var_arguments (info))
1358 continue;
1359 if (ipcp_const_param_count (node))
1360 node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node),
1361 node);
1364 /* Now clone in priority order until code size growth limits are met or
1365 heap is emptied. */
1366 while (!fibheap_empty (heap))
1368 struct ipa_node_params *info;
1369 int growth = 0;
1370 bitmap args_to_skip;
1371 struct cgraph_edge *cs;
1373 node = (struct cgraph_node *)fibheap_extract_min (heap);
1374 node->aux = NULL;
1375 if (dump_file)
1376 fprintf (dump_file, "considering function %s\n",
1377 cgraph_node_name (node));
1379 growth = ipcp_estimate_growth (node);
1381 if (new_size + growth > max_new_size)
1382 break;
1383 if (growth
1384 && optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl)))
1386 if (dump_file)
1387 fprintf (dump_file, "Not versioning, cold code would grow");
1388 continue;
1391 new_size += growth;
1393 /* Look if original function becomes dead after clonning. */
1394 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1395 if (cs->caller == node || ipcp_need_redirect_p (cs))
1396 break;
1397 if (!cs && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1398 bitmap_set_bit (dead_nodes, node->uid);
1400 info = IPA_NODE_REF (node);
1401 count = ipa_get_param_count (info);
1403 replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1404 args_to_skip = BITMAP_GGC_ALLOC ();
1405 for (i = 0; i < count; i++)
1407 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1408 parm_tree = ipa_get_param (info, i);
1410 /* We can proactively remove obviously unused arguments. */
1411 if (!ipa_is_param_used (info, i))
1413 bitmap_set_bit (args_to_skip, i);
1414 continue;
1417 if (lat->type == IPA_CONST_VALUE)
1419 replace_param =
1420 ipcp_create_replace_map (parm_tree, lat);
1421 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1422 bitmap_set_bit (args_to_skip, i);
1426 /* Compute how many callers node has. */
1427 node_callers = 0;
1428 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1429 node_callers++;
1430 redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1431 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1432 if (!cs->indirect_inlining_edge)
1433 VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1435 /* Redirecting all the callers of the node to the
1436 new versioned node. */
1437 node1 =
1438 cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1439 args_to_skip, "constprop");
1440 args_to_skip = NULL;
1441 VEC_free (cgraph_edge_p, heap, redirect_callers);
1442 replace_trees = NULL;
1444 if (node1 == NULL)
1445 continue;
1446 ipcp_process_devirtualization_opportunities (node1);
1448 if (dump_file)
1449 fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1450 cgraph_node_name (node), (int)growth, (int)new_size);
1451 ipcp_init_cloned_node (node, node1);
1453 info = IPA_NODE_REF (node);
1454 for (i = 0; i < count; i++)
1456 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1457 if (lat->type == IPA_CONST_VALUE)
1458 ipcp_discover_new_direct_edges (node1, i, lat->constant);
1461 if (dump_file)
1462 dump_function_to_file (node1->decl, dump_file, dump_flags);
1464 for (cs = node->callees; cs; cs = cs->next_callee)
1465 if (cs->callee->aux)
1467 fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1468 cs->callee->aux = fibheap_insert (heap,
1469 ipcp_estimate_cloning_cost (cs->callee),
1470 cs->callee);
1474 while (!fibheap_empty (heap))
1476 if (dump_file)
1477 fprintf (dump_file, "skipping function %s\n",
1478 cgraph_node_name (node));
1479 node = (struct cgraph_node *) fibheap_extract_min (heap);
1480 node->aux = NULL;
1482 fibheap_delete (heap);
1483 BITMAP_FREE (dead_nodes);
1484 ipcp_update_callgraph ();
1485 ipcp_update_profiling ();
1488 /* The IPCP driver. */
1489 static unsigned int
1490 ipcp_driver (void)
1492 cgraph_remove_unreachable_nodes (true,dump_file);
1493 if (dump_file)
1495 fprintf (dump_file, "\nIPA structures before propagation:\n");
1496 if (dump_flags & TDF_DETAILS)
1497 ipa_print_all_params (dump_file);
1498 ipa_print_all_jump_functions (dump_file);
1500 /* 2. Do the interprocedural propagation. */
1501 ipcp_iterate_stage ();
1502 /* 3. Insert the constants found to the functions. */
1503 ipcp_insert_stage ();
1504 if (dump_file && (dump_flags & TDF_DETAILS))
1506 fprintf (dump_file, "\nProfiling info after insert stage:\n");
1507 ipcp_print_profile_data (dump_file);
1509 /* Free all IPCP structures. */
1510 ipa_free_all_structures_after_ipa_cp ();
1511 if (dump_file)
1512 fprintf (dump_file, "\nIPA constant propagation end\n");
1513 return 0;
1516 /* Initialization and computation of IPCP data structures. This is the initial
1517 intraprocedural analysis of functions, which gathers information to be
1518 propagated later on. */
1520 static void
1521 ipcp_generate_summary (void)
1523 struct cgraph_node *node;
1525 if (dump_file)
1526 fprintf (dump_file, "\nIPA constant propagation start:\n");
1527 ipa_check_create_node_params ();
1528 ipa_check_create_edge_args ();
1529 ipa_register_cgraph_hooks ();
1531 for (node = cgraph_nodes; node; node = node->next)
1532 if (node->analyzed)
1534 /* Unreachable nodes should have been eliminated before ipcp. */
1535 gcc_assert (node->needed || node->reachable);
1537 node->local.versionable = tree_versionable_function_p (node->decl);
1538 ipa_analyze_node (node);
1542 /* Write ipcp summary for nodes in SET. */
1543 static void
1544 ipcp_write_summary (cgraph_node_set set,
1545 varpool_node_set vset ATTRIBUTE_UNUSED)
1547 ipa_prop_write_jump_functions (set);
1550 /* Read ipcp summary. */
1551 static void
1552 ipcp_read_summary (void)
1554 ipa_prop_read_jump_functions ();
1557 /* Gate for IPCP optimization. */
1558 static bool
1559 cgraph_gate_cp (void)
1561 /* FIXME: We should remove the optimize check after we ensure we never run
1562 IPA passes when not optimizng. */
1563 return flag_ipa_cp && optimize;
1566 struct ipa_opt_pass_d pass_ipa_cp =
1569 IPA_PASS,
1570 "cp", /* name */
1571 cgraph_gate_cp, /* gate */
1572 ipcp_driver, /* execute */
1573 NULL, /* sub */
1574 NULL, /* next */
1575 0, /* static_pass_number */
1576 TV_IPA_CONSTANT_PROP, /* tv_id */
1577 0, /* properties_required */
1578 0, /* properties_provided */
1579 0, /* properties_destroyed */
1580 0, /* todo_flags_start */
1581 TODO_dump_cgraph | TODO_dump_func |
1582 TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
1584 ipcp_generate_summary, /* generate_summary */
1585 ipcp_write_summary, /* write_summary */
1586 ipcp_read_summary, /* read_summary */
1587 NULL, /* write_optimization_summary */
1588 NULL, /* read_optimization_summary */
1589 NULL, /* stmt_fixup */
1590 0, /* TODOs */
1591 NULL, /* function_transform */
1592 NULL, /* variable_transform */