1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 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
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
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:
28 printf ("value is %d",y);
48 The IPCP algorithm will find that g's formal argument y is always called
51 The algorithm used is based on "Interprocedural Constant Propagation", by
52 Challahan David, Keith D Cooper, Ken Kennedy, Linda Torczon, Comp86, pg
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:
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
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.
123 #include "coretypes.h"
127 #include "ipa-prop.h"
128 #include "tree-flow.h"
129 #include "tree-pass.h"
132 #include "diagnostic.h"
133 #include "tree-dump.h"
134 #include "tree-inline.h"
138 /* Number of functions identified as candidates for cloning. When not cloning
139 we can simplify iterate stage not forcing it to go through the decision
140 on what is profitable and what not. */
141 static int n_cloning_candidates;
143 /* Maximal count found in program. */
144 static gcov_type max_count;
146 /* Cgraph nodes that has been completely replaced by cloning during iterate
147 * stage and will be removed after ipcp is finished. */
148 static bitmap dead_nodes;
150 static void ipcp_print_profile_data (FILE *);
151 static void ipcp_function_scale_print (FILE *);
153 /* Get the original node field of ipa_node_params associated with node NODE. */
154 static inline struct cgraph_node *
155 ipcp_get_orig_node (struct cgraph_node *node)
157 return IPA_NODE_REF (node)->ipcp_orig_node;
160 /* Return true if NODE describes a cloned/versioned function. */
162 ipcp_node_is_clone (struct cgraph_node *node)
164 return (ipcp_get_orig_node (node) != NULL);
167 /* Create ipa_node_params and its data structures for NEW_NODE. Set ORIG_NODE
168 as the ipcp_orig_node field in ipa_node_params. */
170 ipcp_init_cloned_node (struct cgraph_node *orig_node,
171 struct cgraph_node *new_node)
173 ipa_check_create_node_params ();
174 ipa_initialize_node_params (new_node);
175 IPA_NODE_REF (new_node)->ipcp_orig_node = orig_node;
178 /* Perform intraprocedrual analysis needed for ipcp. */
180 ipcp_analyze_node (struct cgraph_node *node)
182 /* Unreachable nodes should have been eliminated before ipcp. */
183 gcc_assert (node->needed || node->reachable);
185 ipa_initialize_node_params (node);
186 ipa_detect_param_modifications (node);
189 /* Return scale for NODE. */
190 static inline gcov_type
191 ipcp_get_node_scale (struct cgraph_node *node)
193 return IPA_NODE_REF (node)->count_scale;
196 /* Set COUNT as scale for NODE. */
198 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
200 IPA_NODE_REF (node)->count_scale = count;
203 /* Return whether LAT is a constant lattice. */
205 ipcp_lat_is_const (struct ipcp_lattice *lat)
207 if (lat->type == IPA_CONST_VALUE)
213 /* Return whether LAT is a constant lattice that ipa-cp can actually insert
214 into the code (i.e. constants excluding member pointers and pointers). */
216 ipcp_lat_is_insertable (struct ipcp_lattice *lat)
218 return lat->type == IPA_CONST_VALUE;
221 /* Return true if LAT1 and LAT2 are equal. */
223 ipcp_lats_are_equal (struct ipcp_lattice *lat1, struct ipcp_lattice *lat2)
225 gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
226 if (lat1->type != lat2->type)
229 if (operand_equal_p (lat1->constant, lat2->constant, 0))
235 /* Compute Meet arithmetics:
236 Meet (IPA_BOTTOM, x) = IPA_BOTTOM
238 Meet (const_a,const_b) = IPA_BOTTOM, if const_a != const_b.
239 MEET (const_a,const_b) = const_a, if const_a == const_b.*/
241 ipa_lattice_meet (struct ipcp_lattice *res, struct ipcp_lattice *lat1,
242 struct ipcp_lattice *lat2)
244 if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
246 res->type = IPA_BOTTOM;
249 if (lat1->type == IPA_TOP)
251 res->type = lat2->type;
252 res->constant = lat2->constant;
255 if (lat2->type == IPA_TOP)
257 res->type = lat1->type;
258 res->constant = lat1->constant;
261 if (!ipcp_lats_are_equal (lat1, lat2))
263 res->type = IPA_BOTTOM;
266 res->type = lat1->type;
267 res->constant = lat1->constant;
270 /* Return the lattice corresponding to the Ith formal parameter of the function
271 described by INFO. */
272 static inline struct ipcp_lattice *
273 ipcp_get_lattice (struct ipa_node_params *info, int i)
275 return &(info->params[i].ipcp_lattice);
278 /* Given the jump function JFUNC, compute the lattice LAT that describes the
279 value coming down the callsite. INFO describes the caller node so that
280 pass-through jump functions can be evaluated. */
282 ipcp_lattice_from_jfunc (struct ipa_node_params *info, struct ipcp_lattice *lat,
283 struct ipa_jump_func *jfunc)
285 if (jfunc->type == IPA_JF_CONST)
287 lat->type = IPA_CONST_VALUE;
288 lat->constant = jfunc->value.constant;
290 else if (jfunc->type == IPA_JF_PASS_THROUGH)
292 struct ipcp_lattice *caller_lat;
295 caller_lat = ipcp_get_lattice (info, jfunc->value.pass_through.formal_id);
296 lat->type = caller_lat->type;
297 if (caller_lat->type != IPA_CONST_VALUE)
299 cst = caller_lat->constant;
301 if (jfunc->value.pass_through.operation != NOP_EXPR)
304 if (TREE_CODE_CLASS (jfunc->value.pass_through.operation)
306 restype = boolean_type_node;
308 restype = TREE_TYPE (cst);
309 cst = fold_binary (jfunc->value.pass_through.operation,
310 restype, cst, jfunc->value.pass_through.operand);
312 if (!cst || !is_gimple_ip_invariant (cst))
313 lat->type = IPA_BOTTOM;
316 else if (jfunc->type == IPA_JF_ANCESTOR)
318 struct ipcp_lattice *caller_lat;
322 caller_lat = ipcp_get_lattice (info, jfunc->value.ancestor.formal_id);
323 lat->type = caller_lat->type;
324 if (caller_lat->type != IPA_CONST_VALUE)
326 if (TREE_CODE (caller_lat->constant) != ADDR_EXPR)
328 /* This can happen when the constant is a NULL pointer. */
329 lat->type = IPA_BOTTOM;
332 t = TREE_OPERAND (caller_lat->constant, 0);
333 ok = build_ref_for_offset (&t, TREE_TYPE (t),
334 jfunc->value.ancestor.offset,
335 jfunc->value.ancestor.type, false);
338 lat->type = IPA_BOTTOM;
339 lat->constant = NULL_TREE;
342 lat->constant = build_fold_addr_expr (t);
345 lat->type = IPA_BOTTOM;
348 /* True when OLD_LAT and NEW_LAT values are not the same. */
351 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
352 struct ipcp_lattice *new_lat)
354 if (old_lat->type == new_lat->type)
356 if (!ipcp_lat_is_const (old_lat))
358 if (ipcp_lats_are_equal (old_lat, new_lat))
364 /* Print all ipcp_lattices of all functions to F. */
366 ipcp_print_all_lattices (FILE * f)
368 struct cgraph_node *node;
371 fprintf (f, "\nLattice:\n");
372 for (node = cgraph_nodes; node; node = node->next)
374 struct ipa_node_params *info;
378 info = IPA_NODE_REF (node);
379 fprintf (f, " Node: %s:\n", cgraph_node_name (node));
380 count = ipa_get_param_count (info);
381 for (i = 0; i < count; i++)
383 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
385 fprintf (f, " param [%d]: ", i);
386 if (lat->type == IPA_CONST_VALUE)
388 fprintf (f, "type is CONST ");
389 print_generic_expr (f, lat->constant, 0);
392 else if (lat->type == IPA_TOP)
393 fprintf (f, "type is TOP\n");
395 fprintf (f, "type is BOTTOM\n");
400 /* Return true if ipcp algorithms would allow cloning NODE. */
403 ipcp_versionable_function_p (struct cgraph_node *node)
405 tree decl = node->decl;
408 /* There are a number of generic reasons functions cannot be versioned. */
409 if (!tree_versionable_function_p (decl))
412 /* Removing arguments doesn't work if the function takes varargs. */
413 if (DECL_STRUCT_FUNCTION (decl)->stdarg)
416 /* Removing arguments doesn't work if we use __builtin_apply_args. */
417 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (decl))
419 gimple_stmt_iterator gsi;
420 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
422 const_gimple stmt = gsi_stmt (gsi);
425 if (!is_gimple_call (stmt))
427 t = gimple_call_fndecl (stmt);
430 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
431 && DECL_FUNCTION_CODE (t) == BUILT_IN_APPLY_ARGS)
439 /* Return true if this NODE is viable candidate for cloning. */
441 ipcp_cloning_candidate_p (struct cgraph_node *node)
445 gcov_type direct_call_sum = 0;
446 struct cgraph_edge *e;
448 /* We never clone functions that are not visible from outside.
449 FIXME: in future we should clone such functions when they are called with
450 different constants, but current ipcp implementation is not good on this.
452 if (cgraph_only_called_directly_p (node) || !node->analyzed)
455 if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
458 fprintf (dump_file, "Not considering %s for cloning; body is overwrittable.\n",
459 cgraph_node_name (node));
462 if (!ipcp_versionable_function_p (node))
465 fprintf (dump_file, "Not considering %s for cloning; body is not versionable.\n",
466 cgraph_node_name (node));
469 for (e = node->callers; e; e = e->next_caller)
471 direct_call_sum += e->count;
473 if (cgraph_maybe_hot_edge_p (e))
480 fprintf (dump_file, "Not considering %s for cloning; no direct calls.\n",
481 cgraph_node_name (node));
484 if (node->local.inline_summary.self_size < n_calls)
487 fprintf (dump_file, "Considering %s for cloning; code would shrink.\n",
488 cgraph_node_name (node));
492 if (!flag_ipa_cp_clone)
495 fprintf (dump_file, "Not considering %s for cloning; -fipa-cp-clone disabled.\n",
496 cgraph_node_name (node));
500 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
503 fprintf (dump_file, "Not considering %s for cloning; optimizing it for size.\n",
504 cgraph_node_name (node));
508 /* When profile is available and function is hot, propagate into it even if
509 calls seems cold; constant propagation can improve function's speed
513 if (direct_call_sum > node->count * 90 / 100)
516 fprintf (dump_file, "Considering %s for cloning; usually called directly.\n",
517 cgraph_node_name (node));
524 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
525 cgraph_node_name (node));
529 fprintf (dump_file, "Considering %s for cloning.\n",
530 cgraph_node_name (node));
534 /* Initialize ipcp_lattices array. The lattices corresponding to supported
535 types (integers, real types and Fortran constants defined as const_decls)
536 are initialized to IPA_TOP, the rest of them to IPA_BOTTOM. */
538 ipcp_initialize_node_lattices (struct cgraph_node *node)
541 struct ipa_node_params *info = IPA_NODE_REF (node);
542 enum ipa_lattice_type type;
544 if (ipa_is_called_with_var_arguments (info))
546 else if (cgraph_only_called_directly_p (node))
548 /* When cloning is allowed, we can assume that externally visible functions
549 are not called. We will compensate this by cloning later. */
550 else if (ipcp_cloning_candidate_p (node))
551 type = IPA_TOP, n_cloning_candidates ++;
555 for (i = 0; i < ipa_get_param_count (info) ; i++)
556 ipcp_get_lattice (info, i)->type = type;
559 /* build INTEGER_CST tree with type TREE_TYPE and value according to LAT.
562 build_const_val (struct ipcp_lattice *lat, tree tree_type)
566 gcc_assert (ipcp_lat_is_const (lat));
569 if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
571 if (fold_convertible_p (tree_type, val))
572 return fold_build1 (NOP_EXPR, tree_type, val);
574 return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
579 /* Compute the proper scale for NODE. It is the ratio between the number of
580 direct calls (represented on the incoming cgraph_edges) and sum of all
581 invocations of NODE (represented as count in cgraph_node). */
583 ipcp_compute_node_scale (struct cgraph_node *node)
586 struct cgraph_edge *cs;
589 /* Compute sum of all counts of callers. */
590 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
592 if (node->count == 0)
593 ipcp_set_node_scale (node, 0);
595 ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
598 /* Initialization and computation of IPCP data structures. This is the initial
599 intraprocedural analysis of functions, which gathers information to be
600 propagated later on. */
602 ipcp_init_stage (void)
604 struct cgraph_node *node;
605 struct cgraph_edge *cs;
607 for (node = cgraph_nodes; node; node = node->next)
609 ipcp_analyze_node (node);
610 for (node = cgraph_nodes; node; node = node->next)
614 /* building jump functions */
615 for (cs = node->callees; cs; cs = cs->next_callee)
617 if (!cs->callee->analyzed)
619 ipa_count_arguments (cs);
620 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
621 != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
623 /* Handle cases of functions with
624 a variable number of parameters. */
625 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
626 if (flag_indirect_inlining)
627 ipa_compute_jump_functions (cs);
630 ipa_compute_jump_functions (cs);
635 /* Return true if there are some formal parameters whose value is IPA_TOP (in
636 the whole compilation unit). Change their values to IPA_BOTTOM, since they
637 most probably get their values from outside of this compilation unit. */
639 ipcp_change_tops_to_bottom (void)
642 struct cgraph_node *node;
646 for (node = cgraph_nodes; node; node = node->next)
648 struct ipa_node_params *info = IPA_NODE_REF (node);
649 count = ipa_get_param_count (info);
650 for (i = 0; i < count; i++)
652 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
653 if (lat->type == IPA_TOP)
658 fprintf (dump_file, "Forcing param ");
659 print_generic_expr (dump_file, ipa_get_param (info, i), 0);
660 fprintf (dump_file, " of node %s to bottom.\n",
661 cgraph_node_name (node));
663 lat->type = IPA_BOTTOM;
670 /* Interprocedural analysis. The algorithm propagates constants from the
671 caller's parameters to the callee's arguments. */
673 ipcp_propagate_stage (void)
676 struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
677 struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
678 struct ipcp_lattice *dest_lat;
679 struct cgraph_edge *cs;
680 struct ipa_jump_func *jump_func;
681 struct ipa_func_list *wl;
684 ipa_check_create_node_params ();
685 ipa_check_create_edge_args ();
687 /* Initialize worklist to contain all functions. */
688 wl = ipa_init_func_list ();
691 struct cgraph_node *node = ipa_pop_func_from_list (&wl);
692 struct ipa_node_params *info = IPA_NODE_REF (node);
694 for (cs = node->callees; cs; cs = cs->next_callee)
696 struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
697 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
699 if (ipa_is_called_with_var_arguments (callee_info))
702 count = ipa_get_cs_argument_count (args);
703 for (i = 0; i < count; i++)
705 jump_func = ipa_get_ith_jump_func (args, i);
706 ipcp_lattice_from_jfunc (info, &inc_lat, jump_func);
707 dest_lat = ipcp_get_lattice (callee_info, i);
708 ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
709 if (ipcp_lattice_changed (&new_lat, dest_lat))
711 dest_lat->type = new_lat.type;
712 dest_lat->constant = new_lat.constant;
713 ipa_push_func_to_list (&wl, cs->callee);
720 /* Call the constant propagation algorithm and re-call it if necessary
721 (if there are undetermined values left). */
723 ipcp_iterate_stage (void)
725 struct cgraph_node *node;
726 n_cloning_candidates = 0;
729 fprintf (dump_file, "\nIPA iterate stage:\n\n");
730 for (node = cgraph_nodes; node; node = node->next)
732 ipcp_initialize_node_lattices (node);
733 ipcp_compute_node_scale (node);
735 if (dump_file && (dump_flags & TDF_DETAILS))
737 ipcp_print_all_lattices (dump_file);
738 ipcp_function_scale_print (dump_file);
741 ipcp_propagate_stage ();
742 if (ipcp_change_tops_to_bottom ())
743 /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
744 This change should be propagated. */
746 gcc_assert (n_cloning_candidates);
747 ipcp_propagate_stage ();
751 fprintf (dump_file, "\nIPA lattices after propagation:\n");
752 ipcp_print_all_lattices (dump_file);
753 if (dump_flags & TDF_DETAILS)
754 ipcp_print_profile_data (dump_file);
758 /* Check conditions to forbid constant insertion to function described by
761 ipcp_node_modifiable_p (struct cgraph_node *node)
763 /* Once we will be able to do in-place replacement, we can be more
765 return ipcp_versionable_function_p (node);
768 /* Print count scale data structures. */
770 ipcp_function_scale_print (FILE * f)
772 struct cgraph_node *node;
774 for (node = cgraph_nodes; node; node = node->next)
778 fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
779 fprintf (f, "value is " HOST_WIDE_INT_PRINT_DEC
780 " \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
784 /* Print counts of all cgraph nodes. */
786 ipcp_print_func_profile_counts (FILE * f)
788 struct cgraph_node *node;
790 for (node = cgraph_nodes; node; node = node->next)
792 fprintf (f, "function %s: ", cgraph_node_name (node));
793 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC
794 " \n", (HOST_WIDE_INT) node->count);
798 /* Print counts of all cgraph edges. */
800 ipcp_print_call_profile_counts (FILE * f)
802 struct cgraph_node *node;
803 struct cgraph_edge *cs;
805 for (node = cgraph_nodes; node; node = node->next)
807 for (cs = node->callees; cs; cs = cs->next_callee)
809 fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
810 cgraph_node_name (cs->callee));
811 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC " \n",
812 (HOST_WIDE_INT) cs->count);
817 /* Print profile info for all functions. */
819 ipcp_print_profile_data (FILE * f)
821 fprintf (f, "\nNODE COUNTS :\n");
822 ipcp_print_func_profile_counts (f);
823 fprintf (f, "\nCS COUNTS stage:\n");
824 ipcp_print_call_profile_counts (f);
827 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
828 processed by versioning, which operates according to the flags set.
829 PARM_TREE is the formal parameter found to be constant. LAT represents the
831 static struct ipa_replace_map *
832 ipcp_create_replace_map (tree parm_tree, struct ipcp_lattice *lat)
834 struct ipa_replace_map *replace_map;
837 replace_map = GGC_NEW (struct ipa_replace_map);
838 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
841 fprintf (dump_file, " replacing param ");
842 print_generic_expr (dump_file, parm_tree, 0);
843 fprintf (dump_file, " with const ");
844 print_generic_expr (dump_file, const_val, 0);
845 fprintf (dump_file, "\n");
847 replace_map->old_tree = parm_tree;
848 replace_map->new_tree = const_val;
849 replace_map->replace_p = true;
850 replace_map->ref_p = false;
855 /* Return true if this callsite should be redirected to the original callee
856 (instead of the cloned one). */
858 ipcp_need_redirect_p (struct cgraph_edge *cs)
860 struct ipa_node_params *orig_callee_info;
862 struct ipa_jump_func *jump_func;
863 struct cgraph_node *node = cs->callee, *orig;
865 if (!n_cloning_candidates)
868 if ((orig = ipcp_get_orig_node (node)) != NULL)
870 if (ipcp_get_orig_node (cs->caller))
873 orig_callee_info = IPA_NODE_REF (node);
874 count = ipa_get_param_count (orig_callee_info);
875 for (i = 0; i < count; i++)
877 struct ipcp_lattice *lat = ipcp_get_lattice (orig_callee_info, i);
878 if (ipcp_lat_is_const (lat))
880 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
881 if (jump_func->type != IPA_JF_CONST)
889 /* Fix the callsites and the call graph after function cloning was done. */
891 ipcp_update_callgraph (void)
893 struct cgraph_node *node;
895 for (node = cgraph_nodes; node; node = node->next)
896 if (node->analyzed && ipcp_node_is_clone (node))
898 bitmap args_to_skip = BITMAP_ALLOC (NULL);
899 struct cgraph_node *orig_node = ipcp_get_orig_node (node);
900 struct ipa_node_params *info = IPA_NODE_REF (orig_node);
901 int i, count = ipa_get_param_count (info);
902 struct cgraph_edge *cs, *next;
904 for (i = 0; i < count; i++)
906 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
907 tree parm_tree = ipa_get_param (info, i);
909 /* We can proactively remove obviously unused arguments. */
910 if (is_gimple_reg (parm_tree)
911 && !gimple_default_def (DECL_STRUCT_FUNCTION (orig_node->decl),
914 bitmap_set_bit (args_to_skip, i);
918 if (lat->type == IPA_CONST_VALUE)
919 bitmap_set_bit (args_to_skip, i);
921 for (cs = node->callers; cs; cs = next)
923 next = cs->next_caller;
924 if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
925 cgraph_redirect_edge_callee (cs, orig_node);
930 /* Update profiling info for versioned functions and the functions they were
933 ipcp_update_profiling (void)
935 struct cgraph_node *node, *orig_node;
936 gcov_type scale, scale_complement;
937 struct cgraph_edge *cs;
939 for (node = cgraph_nodes; node; node = node->next)
941 if (ipcp_node_is_clone (node))
943 orig_node = ipcp_get_orig_node (node);
944 scale = ipcp_get_node_scale (orig_node);
945 node->count = orig_node->count * scale / REG_BR_PROB_BASE;
946 scale_complement = REG_BR_PROB_BASE - scale;
948 orig_node->count * scale_complement / REG_BR_PROB_BASE;
949 for (cs = node->callees; cs; cs = cs->next_callee)
950 cs->count = cs->count * scale / REG_BR_PROB_BASE;
951 for (cs = orig_node->callees; cs; cs = cs->next_callee)
952 cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
957 /* If NODE was cloned, how much would program grow? */
959 ipcp_estimate_growth (struct cgraph_node *node)
961 struct cgraph_edge *cs;
962 int redirectable_node_callers = 0;
963 int removable_args = 0;
964 bool need_original = !cgraph_only_called_directly_p (node);
965 struct ipa_node_params *info;
969 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
970 if (cs->caller == node || !ipcp_need_redirect_p (cs))
971 redirectable_node_callers++;
973 need_original = true;
975 /* If we will be able to fully replace orignal node, we never increase
980 info = IPA_NODE_REF (node);
981 count = ipa_get_param_count (info);
982 for (i = 0; i < count; i++)
984 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
985 tree parm_tree = ipa_get_param (info, i);
987 /* We can proactively remove obviously unused arguments. */
988 if (is_gimple_reg (parm_tree)
989 && !gimple_default_def (DECL_STRUCT_FUNCTION (node->decl),
993 if (lat->type == IPA_CONST_VALUE)
997 /* We make just very simple estimate of savings for removal of operand from
998 call site. Precise cost is dificult to get, as our size metric counts
999 constants and moves as free. Generally we are looking for cases that
1000 small function is called very many times. */
1001 growth = node->local.inline_summary.self_size
1002 - removable_args * redirectable_node_callers;
1009 /* Estimate cost of cloning NODE. */
1011 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1014 gcov_type count_sum = 1;
1015 struct cgraph_edge *e;
1018 cost = ipcp_estimate_growth (node) * 1000;
1022 fprintf (dump_file, "Versioning of %s will save code size\n",
1023 cgraph_node_name (node));
1027 for (e = node->callers; e; e = e->next_caller)
1028 if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1029 && !ipcp_need_redirect_p (e))
1031 count_sum += e->count;
1032 freq_sum += e->frequency + 1;
1036 cost /= count_sum * 1000 / max_count + 1;
1038 cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1040 fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1041 cgraph_node_name (node), cost, node->local.inline_summary.self_size,
1046 /* Return number of live constant parameters. */
1048 ipcp_const_param_count (struct cgraph_node *node)
1050 int const_param = 0;
1051 struct ipa_node_params *info = IPA_NODE_REF (node);
1052 int count = ipa_get_param_count (info);
1055 for (i = 0; i < count; i++)
1057 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1058 tree parm_tree = ipa_get_param (info, i);
1059 if (ipcp_lat_is_insertable (lat)
1060 /* Do not count obviously unused arguments. */
1061 && (!is_gimple_reg (parm_tree)
1062 || gimple_default_def (DECL_STRUCT_FUNCTION (node->decl),
1069 /* Propagate the constant parameters found by ipcp_iterate_stage()
1070 to the function's code. */
1072 ipcp_insert_stage (void)
1074 struct cgraph_node *node, *node1 = NULL;
1076 VEC (cgraph_edge_p, heap) * redirect_callers;
1077 VEC (ipa_replace_map_p,gc)* replace_trees;
1078 int node_callers, count;
1080 struct ipa_replace_map *replace_param;
1082 long overall_size = 0, new_size = 0;
1085 ipa_check_create_node_params ();
1086 ipa_check_create_edge_args ();
1088 fprintf (dump_file, "\nIPA insert stage:\n\n");
1090 dead_nodes = BITMAP_ALLOC (NULL);
1092 for (node = cgraph_nodes; node; node = node->next)
1095 if (node->count > max_count)
1096 max_count = node->count;
1097 overall_size += node->local.inline_summary.self_size;
1100 max_new_size = overall_size;
1101 if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1102 max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1103 max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1105 /* First collect all functions we proved to have constant arguments to heap. */
1106 heap = fibheap_new ();
1107 for (node = cgraph_nodes; node; node = node->next)
1109 struct ipa_node_params *info;
1110 /* Propagation of the constant is forbidden in certain conditions. */
1111 if (!node->analyzed || !ipcp_node_modifiable_p (node))
1113 info = IPA_NODE_REF (node);
1114 if (ipa_is_called_with_var_arguments (info))
1116 if (ipcp_const_param_count (node))
1117 node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node), node);
1120 /* Now clone in priority order until code size growth limits are met or
1122 while (!fibheap_empty (heap))
1124 struct ipa_node_params *info;
1126 bitmap args_to_skip;
1127 struct cgraph_edge *cs;
1129 node = (struct cgraph_node *)fibheap_extract_min (heap);
1132 fprintf (dump_file, "considering function %s\n",
1133 cgraph_node_name (node));
1135 growth = ipcp_estimate_growth (node);
1137 if (new_size + growth > max_new_size)
1140 && optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl)))
1143 fprintf (dump_file, "Not versioning, cold code would grow");
1149 /* Look if original function becomes dead after clonning. */
1150 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1151 if (cs->caller == node || ipcp_need_redirect_p (cs))
1153 if (!cs && cgraph_only_called_directly_p (node))
1154 bitmap_set_bit (dead_nodes, node->uid);
1156 info = IPA_NODE_REF (node);
1157 count = ipa_get_param_count (info);
1159 replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1160 args_to_skip = BITMAP_GGC_ALLOC ();
1161 for (i = 0; i < count; i++)
1163 struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1164 parm_tree = ipa_get_param (info, i);
1166 /* We can proactively remove obviously unused arguments. */
1167 if (is_gimple_reg (parm_tree)
1168 && !gimple_default_def (DECL_STRUCT_FUNCTION (node->decl),
1171 bitmap_set_bit (args_to_skip, i);
1175 if (lat->type == IPA_CONST_VALUE)
1178 ipcp_create_replace_map (parm_tree, lat);
1179 VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1180 bitmap_set_bit (args_to_skip, i);
1184 /* Compute how many callers node has. */
1186 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1188 redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1189 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1190 VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1192 /* Redirecting all the callers of the node to the
1193 new versioned node. */
1195 cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1197 args_to_skip = NULL;
1198 VEC_free (cgraph_edge_p, heap, redirect_callers);
1199 replace_trees = NULL;
1204 fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1205 cgraph_node_name (node), (int)growth, (int)new_size);
1206 ipcp_init_cloned_node (node, node1);
1208 /* TODO: We can use indirect inlning info to produce new calls. */
1211 dump_function_to_file (node1->decl, dump_file, dump_flags);
1213 for (cs = node->callees; cs; cs = cs->next_callee)
1214 if (cs->callee->aux)
1216 fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1217 cs->callee->aux = fibheap_insert (heap,
1218 ipcp_estimate_cloning_cost (cs->callee),
1223 while (!fibheap_empty (heap))
1226 fprintf (dump_file, "skipping function %s\n",
1227 cgraph_node_name (node));
1228 node = (struct cgraph_node *) fibheap_extract_min (heap);
1231 fibheap_delete (heap);
1232 BITMAP_FREE (dead_nodes);
1233 ipcp_update_callgraph ();
1234 ipcp_update_profiling ();
1237 /* The IPCP driver. */
1241 cgraph_remove_unreachable_nodes (true,dump_file);
1244 fprintf (dump_file, "\nIPA structures before propagation:\n");
1245 if (dump_flags & TDF_DETAILS)
1246 ipa_print_all_params (dump_file);
1247 ipa_print_all_jump_functions (dump_file);
1249 /* 2. Do the interprocedural propagation. */
1250 ipcp_iterate_stage ();
1251 /* 3. Insert the constants found to the functions. */
1252 ipcp_insert_stage ();
1253 if (dump_file && (dump_flags & TDF_DETAILS))
1255 fprintf (dump_file, "\nProfiling info after insert stage:\n");
1256 ipcp_print_profile_data (dump_file);
1258 /* Free all IPCP structures. */
1259 free_all_ipa_structures_after_ipa_cp ();
1261 fprintf (dump_file, "\nIPA constant propagation end\n");
1265 /* Note function body size. */
1267 ipcp_generate_summary (void)
1270 fprintf (dump_file, "\nIPA constant propagation start:\n");
1271 ipa_check_create_node_params ();
1272 ipa_check_create_edge_args ();
1273 ipa_register_cgraph_hooks ();
1274 /* 1. Call the init stage to initialize
1275 the ipa_node_params and ipa_edge_args structures. */
1279 /* Gate for IPCP optimization. */
1281 cgraph_gate_cp (void)
1283 /* FIXME lto. IPA-CP does not tolerate running when the inlining decisions
1284 have not been applied. This happens when WPA modifies the callgraph.
1285 Since those decisions are not applied until after all the IPA passes
1286 have been run in LTRANS, this means that IPA passes may see partially
1287 modified callgraphs. The solution to this is to apply WPA decisions
1288 early during LTRANS. */
1289 return flag_ipa_cp && !flag_ltrans;
1292 struct ipa_opt_pass_d pass_ipa_cp =
1297 cgraph_gate_cp, /* gate */
1298 ipcp_driver, /* execute */
1301 0, /* static_pass_number */
1302 TV_IPA_CONSTANT_PROP, /* tv_id */
1303 0, /* properties_required */
1304 0, /* properties_provided */
1305 0, /* properties_destroyed */
1306 0, /* todo_flags_start */
1307 TODO_dump_cgraph | TODO_dump_func |
1308 TODO_remove_functions /* todo_flags_finish */
1310 ipcp_generate_summary, /* generate_summary */
1311 NULL, /* write_summary */
1312 NULL, /* read_summary */
1313 NULL, /* function_read_summary */
1315 NULL, /* function_transform */
1316 NULL, /* variable_transform */