merge with trunk @ 139506
[official-gcc.git] / gcc / ipa-cp.c
blobd534d198203f7f40e2d6f7b2e43df85f90355991
1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Razya Ladelsky <RAZYA@il.ibm.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Interprocedural constant propagation. The aim of interprocedural constant
22 propagation (IPCP) is to find which function's argument has the same
23 constant value in each invocation throughout the whole program. For example,
24 consider the following program:
26 int g (int y)
28 printf ("value is %d",y);
31 int f (int x)
33 g (x);
36 int h (int y)
38 g (y);
41 void main (void)
43 f (3);
44 h (3);
48 The IPCP algorithm will find that g's formal argument y is always called
49 with the value 3.
51 The algorithm used is based on "Interprocedural Constant Propagation", by
52 Challahan David, Keith D Cooper, Ken Kennedy, Linda Torczon, Comp86, pg
53 152-161
55 The optimization is divided into three stages:
57 First stage - intraprocedural analysis
58 =======================================
59 This phase computes jump_function and modification flags.
61 A jump function for a callsite represents the values passed as an actual
62 arguments of a given callsite. There are three types of values:
63 Pass through - the caller's formal parameter is passed as an actual argument.
64 Constant - a constant is passed as an actual argument.
65 Unknown - neither of the above.
67 The jump function info, ipa_jump_func, is stored in ipa_edge_args
68 structure (defined in ipa_prop.h and pointed to by cgraph_node->aux)
69 modified_flags are defined in ipa_node_params structure
70 (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
72 -ipcp_init_stage() is the first stage driver.
74 Second stage - interprocedural analysis
75 ========================================
76 This phase does the interprocedural constant propagation.
77 It computes lattices for all formal parameters in the program
78 and their value that may be:
79 TOP - unknown.
80 BOTTOM - non constant.
81 CONSTANT - constant value.
83 Lattice describing a formal parameter p will have a constant value if all
84 callsites invoking this function have the same constant value passed to p.
86 The lattices are stored in ipcp_lattice which is itself in ipa_node_params
87 structure (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
89 -ipcp_iterate_stage() is the second stage driver.
91 Third phase - transformation of function code
92 ============================================
93 Propagates the constant-valued formals into the function.
94 For each function whose parameters are constants, we create its clone.
96 Then we process the clone in two ways:
97 1. We insert an assignment statement 'parameter = const' at the beginning
98 of the cloned function.
99 2. For read-only parameters that do not live in memory, we replace all their
100 uses with the constant.
102 We also need to modify some callsites to call the cloned functions instead
103 of the original ones. For a callsite passing an argument found to be a
104 constant by IPCP, there are two different cases to handle:
105 1. A constant is passed as an argument. In this case the callsite in the
106 should be redirected to call the cloned callee.
107 2. A parameter (of the caller) passed as an argument (pass through
108 argument). In such cases both the caller and the callee have clones and
109 only the callsite in the cloned caller is redirected to call to the
110 cloned callee.
112 This update is done in two steps: First all cloned functions are created
113 during a traversal of the call graph, during which all callsites are
114 redirected to call the cloned function. Then the callsites are traversed
115 and many calls redirected back to fit the description above.
117 -ipcp_insert_stage() is the third phase driver.
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "tree.h"
125 #include "target.h"
126 #include "cgraph.h"
127 #include "ipa-prop.h"
128 #include "tree-flow.h"
129 #include "tree-pass.h"
130 #include "flags.h"
131 #include "timevar.h"
132 #include "diagnostic.h"
133 #include "tree-dump.h"
134 #include "tree-inline.h"
136 /* Get the original node field of ipa_node_params associated with node NODE. */
137 static inline struct cgraph_node *
138 ipcp_get_orig_node (struct cgraph_node *node)
140 return IPA_NODE_REF (node)->ipcp_orig_node;
143 /* Return true if NODE describes a cloned/versioned function. */
144 static inline bool
145 ipcp_node_is_clone (struct cgraph_node *node)
147 return (ipcp_get_orig_node (node) != NULL);
150 /* Create ipa_node_params and its data structures for NEW_NODE. Set ORIG_NODE
151 as the ipcp_orig_node field in ipa_node_params. */
152 static void
153 ipcp_init_cloned_node (struct cgraph_node *orig_node,
154 struct cgraph_node *new_node)
156 ipa_check_create_node_params ();
157 IPA_NODE_REF (new_node)->ipcp_orig_node = orig_node;
158 ipa_count_formal_params (new_node);
159 ipa_create_param_decls_array (new_node);
162 /* Recompute all local information since node might've got new
163 direct calls after clonning. */
164 static void
165 ipcp_update_cloned_node (struct cgraph_node *new_node)
167 /* We might've introduced new direct calls. */
168 push_cfun (DECL_STRUCT_FUNCTION (new_node->decl));
169 current_function_decl = new_node->decl;
170 rebuild_cgraph_edges ();
172 if (flag_indirect_inlining)
174 struct cgraph_edge *cs;
176 ipa_check_create_node_params ();
177 ipa_count_formal_params (new_node);
178 ipa_create_param_decls_array (new_node);
179 ipa_detect_param_modifications (new_node);
180 ipa_analyze_params_uses (new_node);
182 for (cs = new_node->callees; cs; cs = cs->next_callee)
184 ipa_count_arguments (cs);
185 ipa_compute_jump_functions (cs);
188 pop_cfun ();
189 current_function_decl = NULL;
192 /* Return scale for NODE. */
193 static inline gcov_type
194 ipcp_get_node_scale (struct cgraph_node *node)
196 return IPA_NODE_REF (node)->count_scale;
199 /* Set COUNT as scale for NODE. */
200 static inline void
201 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
203 IPA_NODE_REF (node)->count_scale = count;
206 /* Return whether LAT is a constant lattice. */
207 static inline bool
208 ipcp_lat_is_const (struct ipcp_lattice *lat)
210 if (lat->type == IPA_CONST_VALUE || lat->type == IPA_CONST_VALUE_REF)
211 return true;
212 else
213 return false;
216 /* Return whether LAT is a constant lattice that ipa-cp can actually insert
217 into the code (i.e. constants excluding member pointers and pointers). */
218 static inline bool
219 ipcp_lat_is_insertable (struct ipcp_lattice *lat)
221 if ((lat->type == IPA_CONST_VALUE || lat->type == IPA_CONST_VALUE_REF)
222 && !POINTER_TYPE_P (TREE_TYPE (lat->constant)))
223 return true;
224 else
225 return false;
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 (operand_equal_p (lat1->constant, lat2->constant, 0))
237 return true;
239 return false;
242 /* Compute Meet arithmetics:
243 Meet (IPA_BOTTOM, x) = IPA_BOTTOM
244 Meet (IPA_TOP,x) = x
245 Meet (const_a,const_b) = IPA_BOTTOM, if const_a != const_b.
246 MEET (const_a,const_b) = const_a, if const_a == const_b.*/
247 static void
248 ipa_lattice_meet (struct ipcp_lattice *res, struct ipcp_lattice *lat1,
249 struct ipcp_lattice *lat2)
251 if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
253 res->type = IPA_BOTTOM;
254 return;
256 if (lat1->type == IPA_TOP)
258 res->type = lat2->type;
259 res->constant = lat2->constant;
260 return;
262 if (lat2->type == IPA_TOP)
264 res->type = lat1->type;
265 res->constant = lat1->constant;
266 return;
268 if (!ipcp_lats_are_equal (lat1, lat2))
270 res->type = IPA_BOTTOM;
271 return;
273 res->type = lat1->type;
274 res->constant = lat1->constant;
277 /* Return the lattice corresponding to the Ith formal parameter of the function
278 described by INFO. */
279 static inline struct ipcp_lattice *
280 ipcp_get_ith_lattice (struct ipa_node_params *info, int i)
282 return &(info->ipcp_lattices[i]);
285 /* Given the jump function JFUNC, compute the lattice LAT that describes the
286 value coming down the callsite. INFO describes the caller node so that
287 pass-through jump functions can be evaluated. */
288 static void
289 ipcp_lattice_from_jfunc (struct ipa_node_params *info, struct ipcp_lattice *lat,
290 struct ipa_jump_func *jfunc)
292 if (jfunc->type == IPA_CONST)
294 lat->type = IPA_CONST_VALUE;
295 lat->constant = jfunc->value.constant;
297 else if (jfunc->type == IPA_CONST_REF)
299 lat->type = IPA_CONST_VALUE_REF;
300 lat->constant = jfunc->value.constant;
302 else if (jfunc->type == IPA_PASS_THROUGH)
304 struct ipcp_lattice *caller_lat;
306 caller_lat = ipcp_get_ith_lattice (info, jfunc->value.formal_id);
307 lat->type = caller_lat->type;
308 lat->constant = caller_lat->constant;
310 else
311 lat->type = IPA_BOTTOM;
314 /* True when OLD_LAT and NEW_LAT values are not the same. */
316 static bool
317 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
318 struct ipcp_lattice *new_lat)
320 if (old_lat->type == new_lat->type)
322 if (!ipcp_lat_is_const (old_lat))
323 return false;
324 if (ipcp_lats_are_equal (old_lat, new_lat))
325 return false;
327 return true;
330 /* Print all ipcp_lattices of all functions to F. */
331 static void
332 ipcp_print_all_lattices (FILE * f)
334 struct cgraph_node *node;
335 int i, count;
337 fprintf (f, "\nLATTICE PRINT\n");
338 for (node = cgraph_nodes; node; node = node->next)
340 struct ipa_node_params *info;
342 if (!node->analyzed)
343 continue;
344 info = IPA_NODE_REF (node);
345 fprintf (f, "Printing lattices %s:\n", cgraph_node_name (node));
346 count = ipa_get_param_count (info);
347 for (i = 0; i < count; i++)
349 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
351 fprintf (f, " param [%d]: ", i);
352 if (lat->type == IPA_CONST_VALUE || lat->type == IPA_CONST_VALUE_REF)
354 fprintf (f, "type is CONST ");
355 print_generic_expr (f, lat->constant, 0);
356 fprintf (f, "\n");
358 else if (lat->type == IPA_TOP)
359 fprintf (f, "type is TOP\n");
360 else
361 fprintf (f, "type is BOTTOM\n");
366 /* Initialize ipcp_lattices array. The lattices corresponding to supported
367 types (integers, real types and Fortran constants defined as const_decls)
368 are initialized to IPA_TOP, the rest of them to IPA_BOTTOM. */
369 static void
370 ipcp_initialize_node_lattices (struct cgraph_node *node)
372 int i;
373 struct ipa_node_params *info = IPA_NODE_REF (node);
375 info->ipcp_lattices = XCNEWVEC (struct ipcp_lattice,
376 ipa_get_param_count (info));
377 for (i = 0; i < ipa_get_param_count (info) ; i++)
379 tree parm_tree = ipa_get_ith_param (info, i);
380 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
382 if (INTEGRAL_TYPE_P (TREE_TYPE (parm_tree))
383 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (parm_tree))
384 || POINTER_TYPE_P (TREE_TYPE (parm_tree)))
385 lat->type = IPA_TOP;
386 else
387 lat->type = IPA_BOTTOM;
391 /* Create a new assignment statement and make it the first statement in the
392 function. PARM1 is the lhs of the assignment and VAL is the rhs. */
393 static void
394 constant_val_insert (tree parm1 ATTRIBUTE_UNUSED, tree val ATTRIBUTE_UNUSED)
396 gimple init_stmt = NULL;
397 edge e_step;
399 init_stmt = gimple_build_assign (parm1, val);
400 gcc_assert (init_stmt);
401 e_step = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FUNCTION (cfun));
402 gsi_insert_on_edge_immediate (e_step, init_stmt);
405 /* build INTEGER_CST tree with type TREE_TYPE and value according to LAT.
406 Return the tree. */
407 static tree
408 build_const_val (struct ipcp_lattice *lat, tree tree_type)
410 tree val;
412 gcc_assert (ipcp_lat_is_const (lat));
413 val = lat->constant;
415 /* compute_jump_functions inserts FUNCTION_DECL as value of parameter
416 when address of function is taken. It would make more sense to pass
417 whole ADDR_EXPR, but for now compensate here. */
418 if ((lat->type == IPA_CONST_VALUE
419 && TREE_CODE (val) == FUNCTION_DECL)
420 || lat->type == IPA_CONST_VALUE_REF)
421 return build_fold_addr_expr_with_type (val, tree_type);
423 if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
425 if (fold_convertible_p (tree_type, val))
426 return fold_build1 (NOP_EXPR, tree_type, val);
427 else
428 return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
430 return val;
433 /* Build the tree representing the constant and call constant_val_insert(). */
434 static void
435 ipcp_propagate_one_const (struct cgraph_node *node, int param,
436 struct ipcp_lattice *lat)
438 tree const_val;
439 tree parm_tree;
441 if (dump_file)
442 fprintf (dump_file, "propagating const to %s\n", cgraph_node_name (node));
443 parm_tree = ipa_get_ith_param (IPA_NODE_REF (node), param);
444 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
445 constant_val_insert (parm_tree, const_val);
448 /* Compute the proper scale for NODE. It is the ratio between the number of
449 direct calls (represented on the incoming cgraph_edges) and sum of all
450 invocations of NODE (represented as count in cgraph_node). */
451 static void
452 ipcp_compute_node_scale (struct cgraph_node *node)
454 gcov_type sum;
455 struct cgraph_edge *cs;
457 sum = 0;
458 /* Compute sum of all counts of callers. */
459 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
460 sum += cs->count;
461 if (node->count == 0)
462 ipcp_set_node_scale (node, 0);
463 else
464 ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
467 /* Initialization and computation of IPCP data structures. This is the initial
468 intraprocedural analysis of functions, which gathers information to be
469 propagated later on. */
470 static void
471 ipcp_init_stage (void)
473 struct cgraph_node *node;
474 struct cgraph_edge *cs;
476 for (node = cgraph_nodes; node; node = node->next)
478 if (!node->analyzed)
479 continue;
480 /* Unreachable nodes should have been eliminated before ipcp. */
481 gcc_assert (node->needed || node->reachable);
483 ipa_count_formal_params (node);
484 ipa_create_param_decls_array (node);
485 ipcp_initialize_node_lattices (node);
486 ipa_detect_param_modifications (node);
487 ipcp_compute_node_scale (node);
489 for (node = cgraph_nodes; node; node = node->next)
491 if (!node->analyzed)
492 continue;
493 /* building jump functions */
494 for (cs = node->callees; cs; cs = cs->next_callee)
496 if (!cs->callee->analyzed)
497 continue;
498 ipa_count_arguments (cs);
499 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
500 != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
502 /* Handle cases of functions with
503 a variable number of parameters. */
504 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
505 if (flag_indirect_inlining)
506 ipa_compute_jump_functions (cs);
508 else
509 ipa_compute_jump_functions (cs);
514 /* Return true if there are some formal parameters whose value is IPA_TOP (in
515 the whole compilation unit). Change their values to IPA_BOTTOM, since they
516 most probably get their values from outside of this compilation unit. */
517 static bool
518 ipcp_change_tops_to_bottom (void)
520 int i, count;
521 struct cgraph_node *node;
522 bool prop_again;
524 prop_again = false;
525 for (node = cgraph_nodes; node; node = node->next)
527 struct ipa_node_params *info = IPA_NODE_REF (node);
528 count = ipa_get_param_count (info);
529 for (i = 0; i < count; i++)
531 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
532 if (lat->type == IPA_TOP)
534 prop_again = true;
535 lat->type = IPA_BOTTOM;
539 return prop_again;
542 /* Interprocedural analysis. The algorithm propagates constants from the
543 caller's parameters to the callee's arguments. */
544 static void
545 ipcp_propagate_stage (void)
547 int i;
548 struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
549 struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
550 struct ipcp_lattice *dest_lat;
551 struct cgraph_edge *cs;
552 struct ipa_jump_func *jump_func;
553 struct ipa_func_list *wl;
554 int count;
556 ipa_check_create_node_params ();
557 ipa_check_create_edge_args ();
558 /* Initialize worklist to contain all functions. */
559 wl = ipa_init_func_list ();
560 while (wl)
562 struct cgraph_node *node = ipa_pop_func_from_list (&wl);
563 struct ipa_node_params *info = IPA_NODE_REF (node);
565 for (cs = node->callees; cs; cs = cs->next_callee)
567 struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
568 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
570 if (ipa_is_called_with_var_arguments (callee_info))
571 continue;
573 count = ipa_get_cs_argument_count (args);
574 for (i = 0; i < count; i++)
576 jump_func = ipa_get_ith_jump_func (args, i);
577 ipcp_lattice_from_jfunc (info, &inc_lat, jump_func);
578 dest_lat = ipcp_get_ith_lattice (callee_info, i);
579 ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
580 if (ipcp_lattice_changed (&new_lat, dest_lat))
582 dest_lat->type = new_lat.type;
583 dest_lat->constant = new_lat.constant;
584 ipa_push_func_to_list (&wl, cs->callee);
591 /* Call the constant propagation algorithm and re-call it if necessary
592 (if there are undetermined values left). */
593 static void
594 ipcp_iterate_stage (void)
596 ipcp_propagate_stage ();
597 if (ipcp_change_tops_to_bottom ())
598 /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
599 This change should be propagated. */
600 ipcp_propagate_stage ();
603 /* Check conditions to forbid constant insertion to function described by
604 NODE. */
605 static inline bool
606 ipcp_node_not_modifiable_p (struct cgraph_node *node)
608 /* ??? Handle pending sizes case. */
609 if (DECL_UNINLINABLE (node->decl))
610 return true;
611 return false;
614 /* Print count scale data structures. */
615 static void
616 ipcp_function_scale_print (FILE * f)
618 struct cgraph_node *node;
620 for (node = cgraph_nodes; node; node = node->next)
622 if (!node->analyzed)
623 continue;
624 fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
625 fprintf (f, "value is " HOST_WIDE_INT_PRINT_DEC
626 " \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
630 /* Print counts of all cgraph nodes. */
631 static void
632 ipcp_print_func_profile_counts (FILE * f)
634 struct cgraph_node *node;
636 for (node = cgraph_nodes; node; node = node->next)
638 fprintf (f, "function %s: ", cgraph_node_name (node));
639 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC
640 " \n", (HOST_WIDE_INT) node->count);
644 /* Print counts of all cgraph edges. */
645 static void
646 ipcp_print_call_profile_counts (FILE * f)
648 struct cgraph_node *node;
649 struct cgraph_edge *cs;
651 for (node = cgraph_nodes; node; node = node->next)
653 for (cs = node->callees; cs; cs = cs->next_callee)
655 fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
656 cgraph_node_name (cs->callee));
657 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC " \n",
658 (HOST_WIDE_INT) cs->count);
663 /* Print all counts and probabilities of cfg edges of all functions. */
664 static void
665 ipcp_print_edge_profiles (FILE * f)
667 struct cgraph_node *node;
668 basic_block bb;
669 edge_iterator ei;
670 edge e;
672 for (node = cgraph_nodes; node; node = node->next)
674 fprintf (f, "function %s: \n", cgraph_node_name (node));
675 if (node->analyzed)
677 bb =
678 ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (node->decl));
679 fprintf (f, "ENTRY: ");
680 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
681 " %d\n", (HOST_WIDE_INT) bb->count, bb->frequency);
683 if (bb->succs)
684 FOR_EACH_EDGE (e, ei, bb->succs)
686 if (e->dest ==
687 EXIT_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION
688 (node->decl)))
689 fprintf (f, "edge ENTRY -> EXIT, Count");
690 else
691 fprintf (f, "edge ENTRY -> %d, Count", e->dest->index);
692 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
693 " Prob %d\n", (HOST_WIDE_INT) e->count,
694 e->probability);
696 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
698 fprintf (f, "bb[%d]: ", bb->index);
699 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
700 " %d\n", (HOST_WIDE_INT) bb->count, bb->frequency);
701 FOR_EACH_EDGE (e, ei, bb->succs)
703 if (e->dest ==
704 EXIT_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION
705 (node->decl)))
706 fprintf (f, "edge %d -> EXIT, Count", e->src->index);
707 else
708 fprintf (f, "edge %d -> %d, Count", e->src->index,
709 e->dest->index);
710 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC " Prob %d\n",
711 (HOST_WIDE_INT) e->count, e->probability);
718 /* Print counts and frequencies for all basic blocks of all functions. */
719 static void
720 ipcp_print_bb_profiles (FILE * f)
722 basic_block bb;
723 struct cgraph_node *node;
725 for (node = cgraph_nodes; node; node = node->next)
727 fprintf (f, "function %s: \n", cgraph_node_name (node));
728 if (node->analyzed)
730 bb =
731 ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (node->decl));
732 fprintf (f, "ENTRY: Count");
733 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
734 " Frequency %d\n", (HOST_WIDE_INT) bb->count,
735 bb->frequency);
737 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
739 fprintf (f, "bb[%d]: Count", bb->index);
740 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
741 " Frequency %d\n", (HOST_WIDE_INT) bb->count,
742 bb->frequency);
744 bb =
745 EXIT_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (node->decl));
746 fprintf (f, "EXIT: Count");
747 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
748 " Frequency %d\n", (HOST_WIDE_INT) bb->count,
749 bb->frequency);
755 /* Print all IPCP data structures to F. */
756 static void
757 ipcp_print_all_structures (FILE * f)
759 ipcp_print_all_lattices (f);
760 ipcp_function_scale_print (f);
761 ipa_print_all_tree_maps (f);
762 ipa_print_all_param_flags (f);
763 ipa_print_all_jump_functions (f);
766 /* Print profile info for all functions. */
767 static void
768 ipcp_print_profile_data (FILE * f)
770 fprintf (f, "\nNODE COUNTS :\n");
771 ipcp_print_func_profile_counts (f);
772 fprintf (f, "\nCS COUNTS stage:\n");
773 ipcp_print_call_profile_counts (f);
774 fprintf (f, "\nBB COUNTS and FREQUENCIES :\n");
775 ipcp_print_bb_profiles (f);
776 fprintf (f, "\nCFG EDGES COUNTS and PROBABILITIES :\n");
777 ipcp_print_edge_profiles (f);
780 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
781 processed by versioning, which operates according to the flags set.
782 PARM_TREE is the formal parameter found to be constant. LAT represents the
783 constant. */
784 static struct ipa_replace_map *
785 ipcp_create_replace_map (struct function *func, tree parm_tree,
786 struct ipcp_lattice *lat)
788 struct ipa_replace_map *replace_map;
789 tree const_val;
791 replace_map = XCNEW (struct ipa_replace_map);
792 if (is_gimple_reg (parm_tree) && gimple_default_def (func, parm_tree)
793 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_default_def (func,
794 parm_tree)))
796 if (dump_file)
797 fprintf (dump_file, "replacing param with const\n");
798 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
799 replace_map->old_tree =gimple_default_def (func, parm_tree);
800 replace_map->new_tree = const_val;
801 replace_map->replace_p = true;
802 replace_map->ref_p = false;
804 else
806 replace_map->old_tree = NULL;
807 replace_map->new_tree = NULL;
808 replace_map->replace_p = false;
809 replace_map->ref_p = false;
812 return replace_map;
815 /* Return true if this callsite should be redirected to the original callee
816 (instead of the cloned one). */
817 static bool
818 ipcp_need_redirect_p (struct cgraph_edge *cs)
820 struct ipa_node_params *orig_callee_info;
821 int i, count;
822 struct ipa_jump_func *jump_func;
824 orig_callee_info = IPA_NODE_REF (ipcp_get_orig_node (cs->callee));
825 count = ipa_get_param_count (orig_callee_info);
826 for (i = 0; i < count; i++)
828 struct ipcp_lattice *lat = ipcp_get_ith_lattice (orig_callee_info, i);
829 if (ipcp_lat_is_const (lat))
831 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
832 if (jump_func->type != IPA_CONST && jump_func->type != IPA_CONST_REF
833 && jump_func->type != IPA_CONST_MEMBER_PTR)
834 return true;
838 return false;
841 /* Fix the callsites and the call graph after function cloning was done. */
842 static void
843 ipcp_update_callgraph (void)
845 struct cgraph_node *node, *orig_callee;
846 struct cgraph_edge *cs;
848 for (node = cgraph_nodes; node; node = node->next)
850 /* want to fix only original nodes */
851 if (!node->analyzed || ipcp_node_is_clone (node))
852 continue;
853 for (cs = node->callees; cs; cs = cs->next_callee)
854 if (ipcp_node_is_clone (cs->callee))
856 /* Callee is a cloned node */
857 orig_callee = ipcp_get_orig_node (cs->callee);
858 if (ipcp_need_redirect_p (cs))
860 cgraph_redirect_edge_callee (cs, orig_callee);
861 gimple_call_set_fndecl (cs->call_stmt, orig_callee->decl);
867 /* Update all cfg basic blocks in NODE according to SCALE. */
868 static void
869 ipcp_update_bb_counts (struct cgraph_node *node, gcov_type scale)
871 basic_block bb;
873 FOR_ALL_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
874 bb->count = bb->count * scale / REG_BR_PROB_BASE;
877 /* Update all cfg edges in NODE according to SCALE. */
878 static void
879 ipcp_update_edges_counts (struct cgraph_node *node, gcov_type scale)
881 basic_block bb;
882 edge_iterator ei;
883 edge e;
885 FOR_ALL_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
886 FOR_EACH_EDGE (e, ei, bb->succs)
887 e->count = e->count * scale / REG_BR_PROB_BASE;
890 /* Update profiling info for versioned functions and the functions they were
891 versioned from. */
892 static void
893 ipcp_update_profiling (void)
895 struct cgraph_node *node, *orig_node;
896 gcov_type scale, scale_complement;
897 struct cgraph_edge *cs;
899 for (node = cgraph_nodes; node; node = node->next)
901 if (ipcp_node_is_clone (node))
903 orig_node = ipcp_get_orig_node (node);
904 scale = ipcp_get_node_scale (orig_node);
905 node->count = orig_node->count * scale / REG_BR_PROB_BASE;
906 scale_complement = REG_BR_PROB_BASE - scale;
907 orig_node->count =
908 orig_node->count * scale_complement / REG_BR_PROB_BASE;
909 for (cs = node->callees; cs; cs = cs->next_callee)
910 cs->count = cs->count * scale / REG_BR_PROB_BASE;
911 for (cs = orig_node->callees; cs; cs = cs->next_callee)
912 cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
913 ipcp_update_bb_counts (node, scale);
914 ipcp_update_bb_counts (orig_node, scale_complement);
915 ipcp_update_edges_counts (node, scale);
916 ipcp_update_edges_counts (orig_node, scale_complement);
921 /* Propagate the constant parameters found by ipcp_iterate_stage()
922 to the function's code. */
923 static void
924 ipcp_insert_stage (void)
926 struct cgraph_node *node, *node1 = NULL;
927 int i, const_param;
928 VEC (cgraph_edge_p, heap) * redirect_callers;
929 varray_type replace_trees;
930 struct cgraph_edge *cs;
931 int node_callers, count;
932 tree parm_tree;
933 struct ipa_replace_map *replace_param;
935 ipa_check_create_node_params ();
936 ipa_check_create_edge_args ();
938 for (node = cgraph_nodes; node; node = node->next)
940 struct ipa_node_params *info;
941 /* Propagation of the constant is forbidden in certain conditions. */
942 if (!node->analyzed || ipcp_node_not_modifiable_p (node))
943 continue;
944 info = IPA_NODE_REF (node);
945 if (ipa_is_called_with_var_arguments (info))
946 continue;
947 const_param = 0;
948 count = ipa_get_param_count (info);
949 for (i = 0; i < count; i++)
951 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
952 if (ipcp_lat_is_insertable (lat))
953 const_param++;
955 if (const_param == 0)
956 continue;
957 VARRAY_GENERIC_PTR_INIT (replace_trees, const_param, "replace_trees");
958 for (i = 0; i < count; i++)
960 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
961 if (lat->type == IPA_CONST_VALUE
962 && !POINTER_TYPE_P (TREE_TYPE (lat->constant)))
964 parm_tree = ipa_get_ith_param (info, i);
965 replace_param =
966 ipcp_create_replace_map (DECL_STRUCT_FUNCTION (node->decl),
967 parm_tree, lat);
968 VARRAY_PUSH_GENERIC_PTR (replace_trees, replace_param);
971 /* Compute how many callers node has. */
972 node_callers = 0;
973 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
974 node_callers++;
975 redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
976 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
977 VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
978 /* Redirecting all the callers of the node to the
979 new versioned node. */
980 node1 =
981 cgraph_function_versioning (node, redirect_callers, replace_trees);
982 VEC_free (cgraph_edge_p, heap, redirect_callers);
983 VARRAY_CLEAR (replace_trees);
984 if (node1 == NULL)
985 continue;
986 if (dump_file)
987 fprintf (dump_file, "versioned function %s\n",
988 cgraph_node_name (node));
989 ipcp_init_cloned_node (node, node1);
990 if (const_param > 0)
992 push_cfun (DECL_STRUCT_FUNCTION (node1->decl));
993 gimple_register_cfg_hooks ();
994 current_function_decl = node1->decl;
996 for (i = 0; i < count; i++)
998 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
999 if (ipcp_lat_is_insertable (lat))
1001 parm_tree = ipa_get_ith_param (info, i);
1002 if (lat->type != IPA_CONST_VALUE_REF
1003 && !is_gimple_reg (parm_tree))
1004 ipcp_propagate_one_const (node1, i, lat);
1007 if (gimple_in_ssa_p (cfun))
1009 update_ssa (TODO_update_ssa);
1010 #ifdef ENABLE_CHECKING
1011 verify_ssa (true);
1012 #endif
1014 free_dominance_info (CDI_DOMINATORS);
1015 free_dominance_info (CDI_POST_DOMINATORS);
1016 pop_cfun ();
1017 current_function_decl = NULL;
1018 /* We've possibly introduced direct calls. */
1019 ipcp_update_cloned_node (node1);
1022 if (dump_file)
1023 dump_function_to_file (node1->decl, dump_file, dump_flags);
1025 ipcp_update_callgraph ();
1026 ipcp_update_profiling ();
1029 /* The IPCP driver. */
1030 static unsigned int
1031 ipcp_driver (void)
1033 /* 2. Do the interprocedural propagation. */
1034 ipcp_iterate_stage ();
1035 if (dump_file)
1037 fprintf (dump_file, "\nIPA structures after propagation:\n");
1038 ipcp_print_all_structures (dump_file);
1039 fprintf (dump_file, "\nProfiling info before insert stage:\n");
1040 ipcp_print_profile_data (dump_file);
1042 /* 3. Insert the constants found to the functions. */
1043 ipcp_insert_stage ();
1044 if (dump_file)
1046 fprintf (dump_file, "\nProfiling info after insert stage:\n");
1047 ipcp_print_profile_data (dump_file);
1049 /* Free all IPCP structures. */
1050 free_all_ipa_structures_after_ipa_cp ();
1051 if (dump_file)
1052 fprintf (dump_file, "\nIPA constant propagation end\n");
1053 cgraph_remove_unreachable_nodes (true, NULL);
1054 return 0;
1057 /* Note function body size. */
1058 static void
1059 ipcp_generate_summary (void)
1061 if (dump_file)
1062 fprintf (dump_file, "\nIPA constant propagation start:\n");
1063 ipa_check_create_node_params ();
1064 ipa_check_create_edge_args ();
1065 ipa_register_cgraph_hooks ();
1066 /* 1. Call the init stage to initialize
1067 the ipa_node_params and ipa_edge_args structures. */
1068 ipcp_init_stage ();
1069 if (dump_file)
1071 fprintf (dump_file, "\nIPA structures before propagation:\n");
1072 ipcp_print_all_structures (dump_file);
1076 /* Gate for IPCP optimization. */
1077 static bool
1078 cgraph_gate_cp (void)
1080 return flag_ipa_cp;
1083 struct ipa_opt_pass pass_ipa_cp =
1086 IPA_PASS,
1087 "cp", /* name */
1088 cgraph_gate_cp, /* gate */
1089 ipcp_driver, /* execute */
1090 NULL, /* sub */
1091 NULL, /* next */
1092 0, /* static_pass_number */
1093 TV_IPA_CONSTANT_PROP, /* tv_id */
1094 0, /* properties_required */
1095 PROP_trees, /* properties_provided */
1096 0, /* properties_destroyed */
1097 0, /* todo_flags_start */
1098 TODO_dump_cgraph | TODO_dump_func /* todo_flags_finish */
1100 ipcp_generate_summary, /* generate_summary */
1101 NULL, /* write_summary */
1102 NULL, /* read_summary */
1103 NULL, /* function_read_summary */
1104 0, /* TODOs */
1105 NULL, /* function_transform */
1106 NULL, /* variable_transform */