2011-05-31 Gabriel Charette <gchare@google.com>
[official-gcc.git] / gcc / ipa-cp.c
blob18bfe90235deb59988c19d286bb1735e34363326
1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com>
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 David Callahan, 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"
151 #include "ipa-inline.h"
153 /* Number of functions identified as candidates for cloning. When not cloning
154 we can simplify iterate stage not forcing it to go through the decision
155 on what is profitable and what not. */
156 static int n_cloning_candidates;
158 /* Maximal count found in program. */
159 static gcov_type max_count;
161 /* Cgraph nodes that has been completely replaced by cloning during iterate
162 * stage and will be removed after ipcp is finished. */
163 static bitmap dead_nodes;
165 static void ipcp_print_profile_data (FILE *);
166 static void ipcp_function_scale_print (FILE *);
168 /* Get the original node field of ipa_node_params associated with node NODE. */
169 static inline struct cgraph_node *
170 ipcp_get_orig_node (struct cgraph_node *node)
172 return IPA_NODE_REF (node)->ipcp_orig_node;
175 /* Return true if NODE describes a cloned/versioned function. */
176 static inline bool
177 ipcp_node_is_clone (struct cgraph_node *node)
179 return (ipcp_get_orig_node (node) != NULL);
182 /* Create ipa_node_params and its data structures for NEW_NODE. Set ORIG_NODE
183 as the ipcp_orig_node field in ipa_node_params. */
184 static void
185 ipcp_init_cloned_node (struct cgraph_node *orig_node,
186 struct cgraph_node *new_node)
188 gcc_checking_assert (ipa_node_params_vector
189 && (VEC_length (ipa_node_params_t,
190 ipa_node_params_vector)
191 > (unsigned) cgraph_max_uid));
192 gcc_checking_assert (IPA_NODE_REF (new_node)->params);
193 IPA_NODE_REF (new_node)->ipcp_orig_node = orig_node;
196 /* Return scale for NODE. */
197 static inline gcov_type
198 ipcp_get_node_scale (struct cgraph_node *node)
200 return IPA_NODE_REF (node)->count_scale;
203 /* Set COUNT as scale for NODE. */
204 static inline void
205 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
207 IPA_NODE_REF (node)->count_scale = count;
210 /* Return whether LAT is a constant lattice. */
211 static inline bool
212 ipcp_lat_is_const (struct ipcp_lattice *lat)
214 if (lat->type == IPA_CONST_VALUE)
215 return true;
216 else
217 return false;
220 /* Return whether LAT is a constant lattice that ipa-cp can actually insert
221 into the code (i.e. constants excluding member pointers and pointers). */
222 static inline bool
223 ipcp_lat_is_insertable (struct ipcp_lattice *lat)
225 return lat->type == IPA_CONST_VALUE;
228 /* Return true if LAT1 and LAT2 are equal. */
229 static inline bool
230 ipcp_lats_are_equal (struct ipcp_lattice *lat1, struct ipcp_lattice *lat2)
232 gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
233 if (lat1->type != lat2->type)
234 return false;
236 if (TREE_CODE (lat1->constant) == ADDR_EXPR
237 && TREE_CODE (lat2->constant) == ADDR_EXPR
238 && TREE_CODE (TREE_OPERAND (lat1->constant, 0)) == CONST_DECL
239 && TREE_CODE (TREE_OPERAND (lat2->constant, 0)) == CONST_DECL)
240 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (lat1->constant, 0)),
241 DECL_INITIAL (TREE_OPERAND (lat2->constant, 0)), 0);
242 else
243 return operand_equal_p (lat1->constant, lat2->constant, 0);
246 /* Compute Meet arithmetics:
247 Meet (IPA_BOTTOM, x) = IPA_BOTTOM
248 Meet (IPA_TOP,x) = x
249 Meet (const_a,const_b) = IPA_BOTTOM, if const_a != const_b.
250 MEET (const_a,const_b) = const_a, if const_a == const_b.*/
251 static void
252 ipa_lattice_meet (struct ipcp_lattice *res, struct ipcp_lattice *lat1,
253 struct ipcp_lattice *lat2)
255 if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
257 res->type = IPA_BOTTOM;
258 return;
260 if (lat1->type == IPA_TOP)
262 res->type = lat2->type;
263 res->constant = lat2->constant;
264 return;
266 if (lat2->type == IPA_TOP)
268 res->type = lat1->type;
269 res->constant = lat1->constant;
270 return;
272 if (!ipcp_lats_are_equal (lat1, lat2))
274 res->type = IPA_BOTTOM;
275 return;
277 res->type = lat1->type;
278 res->constant = lat1->constant;
281 /* True when OLD_LAT and NEW_LAT values are not the same. */
283 static bool
284 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
285 struct ipcp_lattice *new_lat)
287 if (old_lat->type == new_lat->type)
289 if (!ipcp_lat_is_const (old_lat))
290 return false;
291 if (ipcp_lats_are_equal (old_lat, new_lat))
292 return false;
294 return true;
297 /* Print all ipcp_lattices of all functions to F. */
298 static void
299 ipcp_print_all_lattices (FILE * f)
301 struct cgraph_node *node;
302 int i, count;
304 fprintf (f, "\nLattice:\n");
305 for (node = cgraph_nodes; node; node = node->next)
307 struct ipa_node_params *info;
309 if (!node->analyzed)
310 continue;
311 info = IPA_NODE_REF (node);
312 fprintf (f, " Node: %s:\n", cgraph_node_name (node));
313 count = ipa_get_param_count (info);
314 for (i = 0; i < count; i++)
316 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
318 fprintf (f, " param [%d]: ", i);
319 if (lat->type == IPA_CONST_VALUE)
321 tree cst = lat->constant;
322 fprintf (f, "type is CONST ");
323 print_generic_expr (f, cst, 0);
324 if (TREE_CODE (cst) == ADDR_EXPR
325 && TREE_CODE (TREE_OPERAND (cst, 0)) == CONST_DECL)
327 fprintf (f, " -> ");
328 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (cst, 0)),
332 else if (lat->type == IPA_TOP)
333 fprintf (f, "type is TOP");
334 else
335 fprintf (f, "type is BOTTOM");
336 if (ipa_param_cannot_devirtualize_p (info, i))
337 fprintf (f, " - cannot_devirtualize set\n");
338 else if (ipa_param_types_vec_empty (info, i))
339 fprintf (f, " - type list empty\n");
340 else
341 fprintf (f, "\n");
346 /* Return true if ipcp algorithms would allow cloning NODE. */
348 static bool
349 ipcp_versionable_function_p (struct cgraph_node *node)
351 struct cgraph_edge *edge;
353 /* There are a number of generic reasons functions cannot be versioned. We
354 also cannot remove parameters if there are type attributes such as fnspec
355 present. */
356 if (!inline_summary (node)->versionable
357 || TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
358 return false;
360 /* Removing arguments doesn't work if the function takes varargs
361 or use __builtin_apply_args. */
362 for (edge = node->callees; edge; edge = edge->next_callee)
364 tree t = edge->callee->decl;
365 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
366 && (DECL_FUNCTION_CODE (t) == BUILT_IN_APPLY_ARGS
367 || DECL_FUNCTION_CODE (t) == BUILT_IN_VA_START))
368 return false;
371 return true;
374 /* Return true if this NODE is viable candidate for cloning. */
375 static bool
376 ipcp_cloning_candidate_p (struct cgraph_node *node)
378 int n_calls = 0;
379 int n_hot_calls = 0;
380 gcov_type direct_call_sum = 0;
381 struct cgraph_edge *e;
383 /* We never clone functions that are not visible from outside.
384 FIXME: in future we should clone such functions when they are called with
385 different constants, but current ipcp implementation is not good on this.
387 if (cgraph_only_called_directly_p (node) || !node->analyzed)
388 return false;
390 /* When function address is taken, we are pretty sure it will be called in hidden way. */
391 if (node->address_taken)
393 if (dump_file)
394 fprintf (dump_file, "Not considering %s for cloning; address is taken.\n",
395 cgraph_node_name (node));
396 return false;
399 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
401 if (dump_file)
402 fprintf (dump_file, "Not considering %s for cloning; body is overwritable.\n",
403 cgraph_node_name (node));
404 return false;
406 if (!ipcp_versionable_function_p (node))
408 if (dump_file)
409 fprintf (dump_file, "Not considering %s for cloning; body is not versionable.\n",
410 cgraph_node_name (node));
411 return false;
413 for (e = node->callers; e; e = e->next_caller)
415 direct_call_sum += e->count;
416 n_calls ++;
417 if (cgraph_maybe_hot_edge_p (e))
418 n_hot_calls ++;
421 if (!n_calls)
423 if (dump_file)
424 fprintf (dump_file, "Not considering %s for cloning; no direct calls.\n",
425 cgraph_node_name (node));
426 return false;
428 if (inline_summary (node)->self_size < n_calls)
430 if (dump_file)
431 fprintf (dump_file, "Considering %s for cloning; code would shrink.\n",
432 cgraph_node_name (node));
433 return true;
436 if (!flag_ipa_cp_clone)
438 if (dump_file)
439 fprintf (dump_file, "Not considering %s for cloning; -fipa-cp-clone disabled.\n",
440 cgraph_node_name (node));
441 return false;
444 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
446 if (dump_file)
447 fprintf (dump_file, "Not considering %s for cloning; optimizing it for size.\n",
448 cgraph_node_name (node));
449 return false;
452 /* When profile is available and function is hot, propagate into it even if
453 calls seems cold; constant propagation can improve function's speed
454 significantly. */
455 if (max_count)
457 if (direct_call_sum > node->count * 90 / 100)
459 if (dump_file)
460 fprintf (dump_file, "Considering %s for cloning; usually called directly.\n",
461 cgraph_node_name (node));
462 return true;
465 if (!n_hot_calls)
467 if (dump_file)
468 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
469 cgraph_node_name (node));
470 return false;
472 if (dump_file)
473 fprintf (dump_file, "Considering %s for cloning.\n",
474 cgraph_node_name (node));
475 return true;
478 /* Mark parameter with index I of function described by INFO as unsuitable for
479 devirtualization. Return true if it has already been marked so. */
481 static bool
482 ipa_set_param_cannot_devirtualize (struct ipa_node_params *info, int i)
484 bool ret = info->params[i].cannot_devirtualize;
485 info->params[i].cannot_devirtualize = true;
486 if (info->params[i].types)
487 VEC_free (tree, heap, info->params[i].types);
488 return ret;
491 /* Initialize ipcp_lattices array. The lattices corresponding to supported
492 types (integers, real types and Fortran constants defined as const_decls)
493 are initialized to IPA_TOP, the rest of them to IPA_BOTTOM. */
494 static void
495 ipcp_initialize_node_lattices (struct cgraph_node *node)
497 int i;
498 struct ipa_node_params *info = IPA_NODE_REF (node);
499 enum ipa_lattice_type type;
501 if (ipa_is_called_with_var_arguments (info))
502 type = IPA_BOTTOM;
503 else if (node->local.local)
504 type = IPA_TOP;
505 /* When cloning is allowed, we can assume that externally visible functions
506 are not called. We will compensate this by cloning later. */
507 else if (ipcp_cloning_candidate_p (node))
508 type = IPA_TOP, n_cloning_candidates ++;
509 else
510 type = IPA_BOTTOM;
512 for (i = 0; i < ipa_get_param_count (info) ; i++)
514 ipa_get_lattice (info, i)->type = type;
515 if (type == IPA_BOTTOM)
516 ipa_set_param_cannot_devirtualize (info, i);
520 /* Build a constant tree with type TREE_TYPE and value according to LAT.
521 Return the tree, or, if it is not possible to convert such value
522 to TREE_TYPE, NULL. */
523 static tree
524 build_const_val (struct ipcp_lattice *lat, tree tree_type)
526 tree val;
528 gcc_assert (ipcp_lat_is_const (lat));
529 val = lat->constant;
531 if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
533 if (fold_convertible_p (tree_type, val))
534 return fold_build1 (NOP_EXPR, tree_type, val);
535 else if (TYPE_SIZE (tree_type) == TYPE_SIZE (TREE_TYPE (val)))
536 return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
537 else
538 return NULL;
540 return val;
543 /* Compute the proper scale for NODE. It is the ratio between the number of
544 direct calls (represented on the incoming cgraph_edges) and sum of all
545 invocations of NODE (represented as count in cgraph_node).
547 FIXME: This code is wrong. Since the callers can be also clones and
548 the clones are not scaled yet, the sums gets unrealistically high.
549 To properly compute the counts, we would need to do propagation across
550 callgraph (as external call to A might imply call to non-cloned B
551 if A's clone calls cloned B). */
552 static void
553 ipcp_compute_node_scale (struct cgraph_node *node)
555 gcov_type sum;
556 struct cgraph_edge *cs;
558 sum = 0;
559 /* Compute sum of all counts of callers. */
560 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
561 sum += cs->count;
562 /* Work around the unrealistically high sum problem. We just don't want
563 the non-cloned body to have negative or very low frequency. Since
564 majority of execution time will be spent in clones anyway, this should
565 give good enough profile. */
566 if (sum > node->count * 9 / 10)
567 sum = node->count * 9 / 10;
568 if (node->count == 0)
569 ipcp_set_node_scale (node, 0);
570 else
571 ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
574 /* Return true if there are some formal parameters whose value is IPA_TOP (in
575 the whole compilation unit). Change their values to IPA_BOTTOM, since they
576 most probably get their values from outside of this compilation unit. */
577 static bool
578 ipcp_change_tops_to_bottom (void)
580 int i, count;
581 struct cgraph_node *node;
582 bool prop_again;
584 prop_again = false;
585 for (node = cgraph_nodes; node; node = node->next)
587 struct ipa_node_params *info = IPA_NODE_REF (node);
588 count = ipa_get_param_count (info);
589 for (i = 0; i < count; i++)
591 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
592 if (lat->type == IPA_TOP)
594 prop_again = true;
595 if (dump_file)
597 fprintf (dump_file, "Forcing param ");
598 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
599 fprintf (dump_file, " of node %s to bottom.\n",
600 cgraph_node_name (node));
602 lat->type = IPA_BOTTOM;
604 if (!ipa_param_cannot_devirtualize_p (info, i)
605 && ipa_param_types_vec_empty (info, i))
607 prop_again = true;
608 ipa_set_param_cannot_devirtualize (info, i);
609 if (dump_file)
611 fprintf (dump_file, "Marking param ");
612 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
613 fprintf (dump_file, " of node %s as unusable for "
614 "devirtualization.\n",
615 cgraph_node_name (node));
620 return prop_again;
623 /* Insert BINFO to the list of known types of parameter number I of the
624 function described by CALLEE_INFO. Return true iff the type information
625 associated with the callee parameter changed in any way. */
627 static bool
628 ipcp_add_param_type (struct ipa_node_params *callee_info, int i, tree binfo)
630 int j, count;
632 if (ipa_param_cannot_devirtualize_p (callee_info, i))
633 return false;
635 if (callee_info->params[i].types)
637 count = VEC_length (tree, callee_info->params[i].types);
638 for (j = 0; j < count; j++)
639 if (VEC_index (tree, callee_info->params[i].types, j) == binfo)
640 return false;
643 if (VEC_length (tree, callee_info->params[i].types)
644 == (unsigned) PARAM_VALUE (PARAM_DEVIRT_TYPE_LIST_SIZE))
645 return !ipa_set_param_cannot_devirtualize (callee_info, i);
647 VEC_safe_push (tree, heap, callee_info->params[i].types, binfo);
648 return true;
651 /* Copy known types information for parameter number CALLEE_IDX of CALLEE_INFO
652 from a parameter of CALLER_INFO as described by JF. Return true iff the
653 type information changed in any way. JF must be a pass-through or an
654 ancestor jump function. */
656 static bool
657 ipcp_copy_types (struct ipa_node_params *caller_info,
658 struct ipa_node_params *callee_info,
659 int callee_idx, struct ipa_jump_func *jf)
661 int caller_idx, j, count;
662 bool res;
664 if (ipa_param_cannot_devirtualize_p (callee_info, callee_idx))
665 return false;
667 if (jf->type == IPA_JF_PASS_THROUGH)
669 if (jf->value.pass_through.operation != NOP_EXPR)
671 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
672 return true;
674 caller_idx = jf->value.pass_through.formal_id;
676 else
677 caller_idx = jf->value.ancestor.formal_id;
679 if (ipa_param_cannot_devirtualize_p (caller_info, caller_idx))
681 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
682 return true;
685 if (!caller_info->params[caller_idx].types)
686 return false;
688 res = false;
689 count = VEC_length (tree, caller_info->params[caller_idx].types);
690 for (j = 0; j < count; j++)
692 tree binfo = VEC_index (tree, caller_info->params[caller_idx].types, j);
693 if (jf->type == IPA_JF_ANCESTOR)
695 binfo = get_binfo_at_offset (binfo, jf->value.ancestor.offset,
696 jf->value.ancestor.type);
697 if (!binfo)
699 ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
700 return true;
703 res |= ipcp_add_param_type (callee_info, callee_idx, binfo);
705 return res;
708 /* Propagate type information for parameter of CALLEE_INFO number I as
709 described by JF. CALLER_INFO describes the caller. Return true iff the
710 type information changed in any way. */
712 static bool
713 ipcp_propagate_types (struct ipa_node_params *caller_info,
714 struct ipa_node_params *callee_info,
715 struct ipa_jump_func *jf, int i)
717 switch (jf->type)
719 case IPA_JF_UNKNOWN:
720 case IPA_JF_CONST_MEMBER_PTR:
721 case IPA_JF_CONST:
722 break;
724 case IPA_JF_KNOWN_TYPE:
725 return ipcp_add_param_type (callee_info, i, jf->value.base_binfo);
727 case IPA_JF_PASS_THROUGH:
728 case IPA_JF_ANCESTOR:
729 return ipcp_copy_types (caller_info, callee_info, i, jf);
732 /* If we reach this we cannot use this parameter for devirtualization. */
733 return !ipa_set_param_cannot_devirtualize (callee_info, i);
736 /* Interprocedural analysis. The algorithm propagates constants from the
737 caller's parameters to the callee's arguments. */
738 static void
739 ipcp_propagate_stage (void)
741 int i;
742 struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
743 struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
744 struct ipcp_lattice *dest_lat;
745 struct cgraph_edge *cs;
746 struct ipa_jump_func *jump_func;
747 struct ipa_func_list *wl;
748 int count;
750 ipa_check_create_node_params ();
751 ipa_check_create_edge_args ();
753 /* Initialize worklist to contain all functions. */
754 wl = ipa_init_func_list ();
755 while (wl)
757 struct cgraph_node *node = ipa_pop_func_from_list (&wl);
758 struct ipa_node_params *info = IPA_NODE_REF (node);
760 for (cs = node->callees; cs; cs = cs->next_callee)
762 struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
763 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
765 if (ipa_is_called_with_var_arguments (callee_info)
766 || !cs->callee->analyzed
767 || ipa_is_called_with_var_arguments (callee_info))
768 continue;
770 count = ipa_get_cs_argument_count (args);
771 for (i = 0; i < count; i++)
773 jump_func = ipa_get_ith_jump_func (args, i);
774 ipa_lattice_from_jfunc (info, &inc_lat, jump_func);
775 dest_lat = ipa_get_lattice (callee_info, i);
776 ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
777 if (ipcp_lattice_changed (&new_lat, dest_lat))
779 dest_lat->type = new_lat.type;
780 dest_lat->constant = new_lat.constant;
781 ipa_push_func_to_list (&wl, cs->callee);
784 if (ipcp_propagate_types (info, callee_info, jump_func, i))
785 ipa_push_func_to_list (&wl, cs->callee);
791 /* Call the constant propagation algorithm and re-call it if necessary
792 (if there are undetermined values left). */
793 static void
794 ipcp_iterate_stage (void)
796 struct cgraph_node *node;
797 n_cloning_candidates = 0;
799 if (dump_file)
800 fprintf (dump_file, "\nIPA iterate stage:\n\n");
802 if (in_lto_p)
803 ipa_update_after_lto_read ();
805 for (node = cgraph_nodes; node; node = node->next)
807 ipcp_initialize_node_lattices (node);
808 ipcp_compute_node_scale (node);
810 if (dump_file && (dump_flags & TDF_DETAILS))
812 ipcp_print_all_lattices (dump_file);
813 ipcp_function_scale_print (dump_file);
816 ipcp_propagate_stage ();
817 if (ipcp_change_tops_to_bottom ())
818 /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
819 This change should be propagated. */
821 gcc_assert (n_cloning_candidates);
822 ipcp_propagate_stage ();
824 if (dump_file)
826 fprintf (dump_file, "\nIPA lattices after propagation:\n");
827 ipcp_print_all_lattices (dump_file);
828 if (dump_flags & TDF_DETAILS)
829 ipcp_print_profile_data (dump_file);
833 /* Check conditions to forbid constant insertion to function described by
834 NODE. */
835 static inline bool
836 ipcp_node_modifiable_p (struct cgraph_node *node)
838 /* Once we will be able to do in-place replacement, we can be more
839 lax here. */
840 return ipcp_versionable_function_p (node);
843 /* Print count scale data structures. */
844 static void
845 ipcp_function_scale_print (FILE * f)
847 struct cgraph_node *node;
849 for (node = cgraph_nodes; node; node = node->next)
851 if (!node->analyzed)
852 continue;
853 fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
854 fprintf (f, "value is " HOST_WIDE_INT_PRINT_DEC
855 " \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
859 /* Print counts of all cgraph nodes. */
860 static void
861 ipcp_print_func_profile_counts (FILE * f)
863 struct cgraph_node *node;
865 for (node = cgraph_nodes; node; node = node->next)
867 fprintf (f, "function %s: ", cgraph_node_name (node));
868 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC
869 " \n", (HOST_WIDE_INT) node->count);
873 /* Print counts of all cgraph edges. */
874 static void
875 ipcp_print_call_profile_counts (FILE * f)
877 struct cgraph_node *node;
878 struct cgraph_edge *cs;
880 for (node = cgraph_nodes; node; node = node->next)
882 for (cs = node->callees; cs; cs = cs->next_callee)
884 fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
885 cgraph_node_name (cs->callee));
886 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC " \n",
887 (HOST_WIDE_INT) cs->count);
892 /* Print profile info for all functions. */
893 static void
894 ipcp_print_profile_data (FILE * f)
896 fprintf (f, "\nNODE COUNTS :\n");
897 ipcp_print_func_profile_counts (f);
898 fprintf (f, "\nCS COUNTS stage:\n");
899 ipcp_print_call_profile_counts (f);
902 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
903 processed by versioning, which operates according to the flags set.
904 PARM_TREE is the formal parameter found to be constant. LAT represents the
905 constant. */
906 static struct ipa_replace_map *
907 ipcp_create_replace_map (tree parm_tree, struct ipcp_lattice *lat)
909 struct ipa_replace_map *replace_map;
910 tree const_val;
912 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
913 if (const_val == NULL_TREE)
915 if (dump_file)
917 fprintf (dump_file, " const ");
918 print_generic_expr (dump_file, lat->constant, 0);
919 fprintf (dump_file, " can't be converted to param ");
920 print_generic_expr (dump_file, parm_tree, 0);
921 fprintf (dump_file, "\n");
923 return NULL;
925 replace_map = ggc_alloc_ipa_replace_map ();
926 if (dump_file)
928 fprintf (dump_file, " replacing param ");
929 print_generic_expr (dump_file, parm_tree, 0);
930 fprintf (dump_file, " with const ");
931 print_generic_expr (dump_file, const_val, 0);
932 fprintf (dump_file, "\n");
934 replace_map->old_tree = parm_tree;
935 replace_map->new_tree = const_val;
936 replace_map->replace_p = true;
937 replace_map->ref_p = false;
939 return replace_map;
942 /* Return true if this callsite should be redirected to the original callee
943 (instead of the cloned one). */
944 static bool
945 ipcp_need_redirect_p (struct cgraph_edge *cs)
947 struct ipa_node_params *orig_callee_info;
948 int i, count;
949 struct cgraph_node *node = cs->callee, *orig;
951 if (!n_cloning_candidates)
952 return false;
954 /* We can't redirect anything in thunks, yet. */
955 if (cs->caller->thunk.thunk_p)
956 return true;
958 if ((orig = ipcp_get_orig_node (node)) != NULL)
959 node = orig;
960 if (ipcp_get_orig_node (cs->caller))
961 return false;
963 orig_callee_info = IPA_NODE_REF (node);
964 count = ipa_get_param_count (orig_callee_info);
965 for (i = 0; i < count; i++)
967 struct ipcp_lattice *lat = ipa_get_lattice (orig_callee_info, i);
968 struct ipa_jump_func *jump_func;
970 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
971 if ((ipcp_lat_is_const (lat)
972 && jump_func->type != IPA_JF_CONST)
973 || (!ipa_param_cannot_devirtualize_p (orig_callee_info, i)
974 && !ipa_param_types_vec_empty (orig_callee_info, i)
975 && jump_func->type != IPA_JF_CONST
976 && jump_func->type != IPA_JF_KNOWN_TYPE))
977 return true;
980 return false;
983 /* Fix the callsites and the call graph after function cloning was done. */
984 static void
985 ipcp_update_callgraph (void)
987 struct cgraph_node *node;
989 for (node = cgraph_nodes; node; node = node->next)
990 if (node->analyzed && ipcp_node_is_clone (node))
992 bitmap args_to_skip = NULL;
993 struct cgraph_node *orig_node = ipcp_get_orig_node (node);
994 struct ipa_node_params *info = IPA_NODE_REF (orig_node);
995 int i, count = ipa_get_param_count (info);
996 struct cgraph_edge *cs, *next;
998 if (node->local.can_change_signature)
1000 args_to_skip = BITMAP_ALLOC (NULL);
1001 for (i = 0; i < count; i++)
1003 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1005 /* We can proactively remove obviously unused arguments. */
1006 if (!ipa_is_param_used (info, i))
1008 bitmap_set_bit (args_to_skip, i);
1009 continue;
1012 if (lat->type == IPA_CONST_VALUE)
1013 bitmap_set_bit (args_to_skip, i);
1016 for (cs = node->callers; cs; cs = next)
1018 next = cs->next_caller;
1019 if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
1021 if (dump_file)
1022 fprintf (dump_file, "Redirecting edge %s/%i -> %s/%i "
1023 "back to %s/%i.",
1024 cgraph_node_name (cs->caller), cs->caller->uid,
1025 cgraph_node_name (cs->callee), cs->callee->uid,
1026 cgraph_node_name (orig_node), orig_node->uid);
1027 cgraph_redirect_edge_callee (cs, orig_node);
1033 /* Update profiling info for versioned functions and the functions they were
1034 versioned from. */
1035 static void
1036 ipcp_update_profiling (void)
1038 struct cgraph_node *node, *orig_node;
1039 gcov_type scale, scale_complement;
1040 struct cgraph_edge *cs;
1042 for (node = cgraph_nodes; node; node = node->next)
1044 if (ipcp_node_is_clone (node))
1046 orig_node = ipcp_get_orig_node (node);
1047 scale = ipcp_get_node_scale (orig_node);
1048 node->count = orig_node->count * scale / REG_BR_PROB_BASE;
1049 scale_complement = REG_BR_PROB_BASE - scale;
1051 gcc_assert (scale_complement >= 0);
1052 orig_node->count =
1053 orig_node->count * scale_complement / REG_BR_PROB_BASE;
1054 for (cs = node->callees; cs; cs = cs->next_callee)
1055 cs->count = cs->count * scale / REG_BR_PROB_BASE;
1056 for (cs = orig_node->callees; cs; cs = cs->next_callee)
1057 cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
1062 /* If NODE was cloned, how much would program grow? */
1063 static long
1064 ipcp_estimate_growth (struct cgraph_node *node)
1066 struct cgraph_edge *cs;
1067 int redirectable_node_callers = 0;
1068 int removable_args = 0;
1069 bool need_original
1070 = !cgraph_will_be_removed_from_program_if_no_direct_calls (node);
1071 VEC (tree, heap) *known_vals = NULL;
1072 struct ipa_node_params *info;
1073 int i, count;
1074 int growth;
1076 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1077 if (cs->caller == node || !ipcp_need_redirect_p (cs))
1078 redirectable_node_callers++;
1079 else
1080 need_original = true;
1082 /* If we will be able to fully replace original node, we never increase
1083 program size. */
1084 if (!need_original)
1085 return 0;
1087 info = IPA_NODE_REF (node);
1088 count = ipa_get_param_count (info);
1089 VEC_safe_grow_cleared (tree, heap, known_vals, count);
1090 if (node->local.can_change_signature)
1091 for (i = 0; i < count; i++)
1093 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1095 /* We can proactively remove obviously unused arguments. */
1096 if (!ipa_is_param_used (info, i))
1097 removable_args++;
1099 if (lat->type == IPA_CONST_VALUE)
1101 removable_args++;
1102 VEC_replace (tree, known_vals, i, lat->constant);
1106 /* We make just very simple estimate of savings for removal of operand from
1107 call site. Precise cost is difficult to get, as our size metric counts
1108 constants and moves as free. Generally we are looking for cases that
1109 small function is called very many times. */
1110 estimate_ipcp_clone_size_and_time (node, known_vals, &growth, NULL);
1111 VEC_free (tree, heap, known_vals);
1112 growth = growth
1113 - removable_args * redirectable_node_callers;
1114 if (growth < 0)
1115 return 0;
1116 return growth;
1120 /* Estimate cost of cloning NODE. */
1121 static long
1122 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1124 int freq_sum = 1;
1125 gcov_type count_sum = 1;
1126 struct cgraph_edge *e;
1127 int cost;
1129 cost = ipcp_estimate_growth (node) * 1000;
1130 if (!cost)
1132 if (dump_file)
1133 fprintf (dump_file, "Versioning of %s will save code size\n",
1134 cgraph_node_name (node));
1135 return 0;
1138 for (e = node->callers; e; e = e->next_caller)
1139 if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1140 && !ipcp_need_redirect_p (e))
1142 count_sum += e->count;
1143 freq_sum += e->frequency + 1;
1146 if (max_count)
1147 cost /= count_sum * 1000 / max_count + 1;
1148 else
1149 cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1150 if (dump_file)
1151 fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1152 cgraph_node_name (node), cost, inline_summary (node)->self_size,
1153 freq_sum);
1154 return cost + 1;
1157 /* Walk indirect calls of NODE and if any polymorphic can be turned into a
1158 direct one now, do so. */
1160 static void
1161 ipcp_process_devirtualization_opportunities (struct cgraph_node *node)
1163 struct ipa_node_params *info = IPA_NODE_REF (node);
1164 struct cgraph_edge *ie, *next_ie;
1166 for (ie = node->indirect_calls; ie; ie = next_ie)
1168 int param_index;
1169 HOST_WIDE_INT token, anc_offset;
1170 tree target, delta, otr_type;
1171 struct ipcp_lattice *lat;
1173 next_ie = ie->next_callee;
1174 if (!ie->indirect_info->polymorphic)
1175 continue;
1176 param_index = ie->indirect_info->param_index;
1177 if (param_index == -1)
1178 continue;
1180 lat = ipa_get_lattice (info, param_index);
1181 token = ie->indirect_info->otr_token;
1182 anc_offset = ie->indirect_info->anc_offset;
1183 otr_type = ie->indirect_info->otr_type;
1184 target = NULL_TREE;
1185 if (lat->type == IPA_CONST_VALUE)
1187 tree binfo = gimple_extract_devirt_binfo_from_cst (lat->constant);
1188 if (!binfo)
1189 continue;
1190 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1191 if (!binfo)
1192 continue;
1193 target = gimple_get_virt_method_for_binfo (token, binfo, &delta,
1194 false);
1196 else
1198 int types_count, j;
1200 if (ipa_param_cannot_devirtualize_p (info, param_index)
1201 || ipa_param_types_vec_empty (info, param_index))
1202 continue;
1204 types_count = VEC_length (tree, info->params[param_index].types);
1205 for (j = 0; j < types_count; j++)
1207 tree binfo = VEC_index (tree, info->params[param_index].types, j);
1208 tree d, t;
1210 binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1211 if (!binfo)
1213 target = NULL_TREE;
1214 break;
1217 t = gimple_get_virt_method_for_binfo (token, binfo, &d, true);
1218 if (!t)
1220 target = NULL_TREE;
1221 break;
1223 else if (!target)
1225 target = t;
1226 delta = d;
1228 else if (target != t || !tree_int_cst_equal (delta, d))
1230 target = NULL_TREE;
1231 break;
1236 if (target)
1237 ipa_make_edge_direct_to_target (ie, target, delta);
1241 /* Return number of live constant parameters. */
1242 static int
1243 ipcp_const_param_count (struct cgraph_node *node)
1245 int const_param = 0;
1246 struct ipa_node_params *info = IPA_NODE_REF (node);
1247 int count = ipa_get_param_count (info);
1248 int i;
1250 for (i = 0; i < count; i++)
1252 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1253 if ((ipcp_lat_is_insertable (lat)
1254 /* Do not count obviously unused arguments. */
1255 && ipa_is_param_used (info, i))
1256 || (!ipa_param_cannot_devirtualize_p (info, i)
1257 && !ipa_param_types_vec_empty (info, i)))
1258 const_param++;
1260 return const_param;
1263 /* Given that a formal parameter of NODE given by INDEX is known to be constant
1264 CST, try to find any indirect edges that can be made direct and make them
1265 so. Note that INDEX is the number the parameter at the time of analyzing
1266 parameter uses and parameter removals should not be considered for it. (In
1267 fact, the parameter itself has just been removed.) */
1269 static void
1270 ipcp_discover_new_direct_edges (struct cgraph_node *node, int index, tree cst)
1272 struct cgraph_edge *ie, *next_ie;
1274 for (ie = node->indirect_calls; ie; ie = next_ie)
1276 struct cgraph_indirect_call_info *ici = ie->indirect_info;
1278 next_ie = ie->next_callee;
1279 if (ici->param_index != index
1280 || ici->polymorphic)
1281 continue;
1283 ipa_make_edge_direct_to_target (ie, cst, NULL_TREE);
1288 /* Propagate the constant parameters found by ipcp_iterate_stage()
1289 to the function's code. */
1290 static void
1291 ipcp_insert_stage (void)
1293 struct cgraph_node *node, *node1 = NULL;
1294 int i;
1295 VEC (cgraph_edge_p, heap) * redirect_callers;
1296 VEC (ipa_replace_map_p,gc)* replace_trees;
1297 int node_callers, count;
1298 tree parm_tree;
1299 struct ipa_replace_map *replace_param;
1300 fibheap_t heap;
1301 long overall_size = 0, new_size = 0;
1302 long max_new_size;
1304 ipa_check_create_node_params ();
1305 ipa_check_create_edge_args ();
1306 if (dump_file)
1307 fprintf (dump_file, "\nIPA insert stage:\n\n");
1309 dead_nodes = BITMAP_ALLOC (NULL);
1311 for (node = cgraph_nodes; node; node = node->next)
1312 if (node->analyzed)
1314 if (node->count > max_count)
1315 max_count = node->count;
1316 overall_size += inline_summary (node)->self_size;
1319 max_new_size = overall_size;
1320 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1321 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1322 max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1324 /* First collect all functions we proved to have constant arguments to
1325 heap. */
1326 heap = fibheap_new ();
1327 for (node = cgraph_nodes; node; node = node->next)
1329 struct ipa_node_params *info;
1330 /* Propagation of the constant is forbidden in certain conditions. */
1331 if (!node->analyzed || !ipcp_node_modifiable_p (node))
1332 continue;
1333 info = IPA_NODE_REF (node);
1334 if (ipa_is_called_with_var_arguments (info))
1335 continue;
1336 if (ipcp_const_param_count (node))
1337 node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node),
1338 node);
1341 /* Now clone in priority order until code size growth limits are met or
1342 heap is emptied. */
1343 while (!fibheap_empty (heap))
1345 struct ipa_node_params *info;
1346 int growth = 0;
1347 bitmap args_to_skip;
1348 struct cgraph_edge *cs;
1350 node = (struct cgraph_node *)fibheap_extract_min (heap);
1351 node->aux = NULL;
1352 if (dump_file)
1353 fprintf (dump_file, "considering function %s\n",
1354 cgraph_node_name (node));
1356 growth = ipcp_estimate_growth (node);
1358 if (new_size + growth > max_new_size)
1359 break;
1360 if (growth
1361 && cgraph_optimize_for_size_p (node))
1363 if (dump_file)
1364 fprintf (dump_file, "Not versioning, cold code would grow");
1365 continue;
1368 info = IPA_NODE_REF (node);
1369 count = ipa_get_param_count (info);
1371 replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1373 if (node->local.can_change_signature)
1374 args_to_skip = BITMAP_GGC_ALLOC ();
1375 else
1376 args_to_skip = NULL;
1377 for (i = 0; i < count; i++)
1379 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1380 parm_tree = ipa_get_param (info, i);
1382 /* We can proactively remove obviously unused arguments. */
1383 if (!ipa_is_param_used (info, i))
1385 if (args_to_skip)
1386 bitmap_set_bit (args_to_skip, i);
1387 continue;
1390 if (lat->type == IPA_CONST_VALUE)
1392 replace_param =
1393 ipcp_create_replace_map (parm_tree, lat);
1394 if (replace_param == NULL)
1395 break;
1396 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1397 if (args_to_skip)
1398 bitmap_set_bit (args_to_skip, i);
1401 if (i < count)
1403 if (dump_file)
1404 fprintf (dump_file, "Not versioning, some parameters couldn't be replaced");
1405 continue;
1408 new_size += growth;
1410 /* Look if original function becomes dead after cloning. */
1411 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1412 if (cs->caller == node || ipcp_need_redirect_p (cs))
1413 break;
1414 if (!cs && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1415 bitmap_set_bit (dead_nodes, node->uid);
1417 /* Compute how many callers node has. */
1418 node_callers = 0;
1419 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1420 node_callers++;
1421 redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1422 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1423 if (!cs->indirect_inlining_edge)
1424 VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1426 /* Redirecting all the callers of the node to the
1427 new versioned node. */
1428 node1 =
1429 cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1430 args_to_skip, "constprop");
1431 args_to_skip = NULL;
1432 VEC_free (cgraph_edge_p, heap, redirect_callers);
1433 replace_trees = NULL;
1435 if (node1 == NULL)
1436 continue;
1437 ipcp_process_devirtualization_opportunities (node1);
1439 if (dump_file)
1440 fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1441 cgraph_node_name (node), (int)growth, (int)new_size);
1442 ipcp_init_cloned_node (node, node1);
1444 info = IPA_NODE_REF (node);
1445 for (i = 0; i < count; i++)
1447 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1448 if (lat->type == IPA_CONST_VALUE)
1449 ipcp_discover_new_direct_edges (node1, i, lat->constant);
1452 if (dump_file)
1453 dump_function_to_file (node1->decl, dump_file, dump_flags);
1455 for (cs = node->callees; cs; cs = cs->next_callee)
1456 if (cs->callee->aux)
1458 fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1459 cs->callee->aux = fibheap_insert (heap,
1460 ipcp_estimate_cloning_cost (cs->callee),
1461 cs->callee);
1465 while (!fibheap_empty (heap))
1467 if (dump_file)
1468 fprintf (dump_file, "skipping function %s\n",
1469 cgraph_node_name (node));
1470 node = (struct cgraph_node *) fibheap_extract_min (heap);
1471 node->aux = NULL;
1473 fibheap_delete (heap);
1474 BITMAP_FREE (dead_nodes);
1475 ipcp_update_callgraph ();
1476 ipcp_update_profiling ();
1479 /* The IPCP driver. */
1480 static unsigned int
1481 ipcp_driver (void)
1483 cgraph_remove_unreachable_nodes (true,dump_file);
1484 if (dump_file)
1486 fprintf (dump_file, "\nIPA structures before propagation:\n");
1487 if (dump_flags & TDF_DETAILS)
1488 ipa_print_all_params (dump_file);
1489 ipa_print_all_jump_functions (dump_file);
1491 ipa_check_create_node_params ();
1492 ipa_check_create_edge_args ();
1493 /* 2. Do the interprocedural propagation. */
1494 ipcp_iterate_stage ();
1495 /* 3. Insert the constants found to the functions. */
1496 ipcp_insert_stage ();
1497 if (dump_file && (dump_flags & TDF_DETAILS))
1499 fprintf (dump_file, "\nProfiling info after insert stage:\n");
1500 ipcp_print_profile_data (dump_file);
1502 /* Free all IPCP structures. */
1503 ipa_free_all_structures_after_ipa_cp ();
1504 if (dump_file)
1505 fprintf (dump_file, "\nIPA constant propagation end\n");
1506 return 0;
1509 /* Initialization and computation of IPCP data structures. This is the initial
1510 intraprocedural analysis of functions, which gathers information to be
1511 propagated later on. */
1513 static void
1514 ipcp_generate_summary (void)
1516 struct cgraph_node *node;
1518 if (dump_file)
1519 fprintf (dump_file, "\nIPA constant propagation start:\n");
1520 ipa_register_cgraph_hooks ();
1522 /* FIXME: We could propagate through thunks happily and we could be
1523 even able to clone them, if needed. Do that later. */
1524 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1526 /* Unreachable nodes should have been eliminated before ipcp. */
1527 gcc_assert (node->needed || node->reachable);
1529 inline_summary (node)->versionable = tree_versionable_function_p (node->decl);
1530 ipa_analyze_node (node);
1534 /* Write ipcp summary for nodes in SET. */
1535 static void
1536 ipcp_write_summary (cgraph_node_set set,
1537 varpool_node_set vset ATTRIBUTE_UNUSED)
1539 ipa_prop_write_jump_functions (set);
1542 /* Read ipcp summary. */
1543 static void
1544 ipcp_read_summary (void)
1546 ipa_prop_read_jump_functions ();
1549 /* Gate for IPCP optimization. */
1550 static bool
1551 cgraph_gate_cp (void)
1553 /* FIXME: We should remove the optimize check after we ensure we never run
1554 IPA passes when not optimizing. */
1555 return flag_ipa_cp && optimize;
1558 struct ipa_opt_pass_d pass_ipa_cp =
1561 IPA_PASS,
1562 "cp", /* name */
1563 cgraph_gate_cp, /* gate */
1564 ipcp_driver, /* execute */
1565 NULL, /* sub */
1566 NULL, /* next */
1567 0, /* static_pass_number */
1568 TV_IPA_CONSTANT_PROP, /* tv_id */
1569 0, /* properties_required */
1570 0, /* properties_provided */
1571 0, /* properties_destroyed */
1572 0, /* todo_flags_start */
1573 TODO_dump_cgraph | TODO_dump_func |
1574 TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
1576 ipcp_generate_summary, /* generate_summary */
1577 ipcp_write_summary, /* write_summary */
1578 ipcp_read_summary, /* read_summary */
1579 NULL, /* write_optimization_summary */
1580 NULL, /* read_optimization_summary */
1581 NULL, /* stmt_fixup */
1582 0, /* TODOs */
1583 NULL, /* function_transform */
1584 NULL, /* variable_transform */