OSDN Git Service

2011-04-29 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-cp.c
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>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* Interprocedural constant propagation.  The aim of interprocedural constant
23    propagation (IPCP) is to find which function's argument has the same
24    constant value in each invocation throughout the whole program. For example,
25    consider the following program:
26
27    int g (int y)
28    {
29      printf ("value is %d",y);
30    }
31
32    int f (int x)
33    {
34      g (x);
35    }
36
37    int h (int y)
38    {
39      g (y);
40    }
41
42    void main (void)
43    {
44      f (3);
45      h (3);
46    }
47
48
49    The IPCP algorithm will find that g's formal argument y is always called
50    with the value 3.
51
52    The algorithm used is based on "Interprocedural Constant Propagation", by
53    David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon, Comp86, pg
54    152-161
55
56    The optimization is divided into three stages:
57
58    First stage - intraprocedural analysis
59    =======================================
60    This phase computes jump_function and modification flags.
61
62    A jump function for a callsite represents the values passed as an actual
63    arguments of a given callsite. There are three types of values:
64    Pass through - the caller's formal parameter is passed as an actual argument.
65    Constant - a constant is passed as an actual argument.
66    Unknown - neither of the above.
67
68    The jump function info, ipa_jump_func, is stored in ipa_edge_args
69    structure (defined in ipa_prop.h and pointed to by cgraph_node->aux)
70    modified_flags are defined in ipa_node_params structure
71    (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
72
73    -ipcp_generate_summary() is the first stage driver.
74
75    Second stage - interprocedural analysis
76    ========================================
77    This phase does the interprocedural constant propagation.
78    It computes lattices for all formal parameters in the program
79    and their value that may be:
80    TOP - unknown.
81    BOTTOM - non constant.
82    CONSTANT - constant value.
83
84    Lattice describing a formal parameter p will have a constant value if all
85    callsites invoking this function have the same constant value passed to p.
86
87    The lattices are stored in ipcp_lattice which is itself in ipa_node_params
88    structure (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
89
90    -ipcp_iterate_stage() is the second stage driver.
91
92    Third phase - transformation of function code
93    ============================================
94    Propagates the constant-valued formals into the function.
95    For each function whose parameters are constants, we create its clone.
96
97    Then we process the clone in two ways:
98    1. We insert an assignment statement 'parameter = const' at the beginning
99       of the cloned function.
100    2. For read-only parameters that do not live in memory, we replace all their
101       uses with the constant.
102
103    We also need to modify some callsites to call the cloned functions instead
104    of the original ones.  For a callsite passing an argument found to be a
105    constant by IPCP, there are two different cases to handle:
106    1. A constant is passed as an argument.  In this case the callsite in the
107       should be redirected to call the cloned callee.
108    2. A parameter (of the caller) passed as an argument (pass through
109       argument).  In such cases both the caller and the callee have clones and
110       only the callsite in the cloned caller is redirected to call to the
111       cloned callee.
112
113    This update is done in two steps: First all cloned functions are created
114    during a traversal of the call graph, during which all callsites are
115    redirected to call the cloned function.  Then the callsites are traversed
116    and many calls redirected back to fit the description above.
117
118    -ipcp_insert_stage() is the third phase driver.
119
120
121    This pass also performs devirtualization - turns virtual calls into direct
122    ones if it can prove that all invocations of the function call the same
123    callee.  This is achieved by building a list of all base types (actually,
124    their BINFOs) that individual parameters can have in an iterative matter
125    just like propagating scalar constants and then examining whether virtual
126    calls which take a parameter as their object fold to the same target for all
127    these types.  If we cannot enumerate all types or there is a type which does
128    not have any BINFO associated with it, cannot_devirtualize of the associated
129    parameter descriptor is set which is an equivalent of BOTTOM lattice value
130    in standard IPA constant propagation.
131 */
132
133 #include "config.h"
134 #include "system.h"
135 #include "coretypes.h"
136 #include "tree.h"
137 #include "target.h"
138 #include "gimple.h"
139 #include "cgraph.h"
140 #include "ipa-prop.h"
141 #include "tree-flow.h"
142 #include "tree-pass.h"
143 #include "flags.h"
144 #include "timevar.h"
145 #include "diagnostic.h"
146 #include "tree-pretty-print.h"
147 #include "tree-dump.h"
148 #include "tree-inline.h"
149 #include "fibheap.h"
150 #include "params.h"
151 #include "ipa-inline.h"
152
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;
157
158 /* Maximal count found in program.  */
159 static gcov_type max_count;
160
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;
164
165 static void ipcp_print_profile_data (FILE *);
166 static void ipcp_function_scale_print (FILE *);
167
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)
171 {
172   return IPA_NODE_REF (node)->ipcp_orig_node;
173 }
174
175 /* Return true if NODE describes a cloned/versioned function.  */
176 static inline bool
177 ipcp_node_is_clone (struct cgraph_node *node)
178 {
179   return (ipcp_get_orig_node (node) != NULL);
180 }
181
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)
187 {
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;
194 }
195
196 /* Return scale for NODE.  */
197 static inline gcov_type
198 ipcp_get_node_scale (struct cgraph_node *node)
199 {
200   return IPA_NODE_REF (node)->count_scale;
201 }
202
203 /* Set COUNT as scale for NODE.  */
204 static inline void
205 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
206 {
207   IPA_NODE_REF (node)->count_scale = count;
208 }
209
210 /* Return whether LAT is a constant lattice.  */
211 static inline bool
212 ipcp_lat_is_const (struct ipcp_lattice *lat)
213 {
214   if (lat->type == IPA_CONST_VALUE)
215     return true;
216   else
217     return false;
218 }
219
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)
224 {
225   return lat->type == IPA_CONST_VALUE;
226 }
227
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)
231 {
232   gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
233   if (lat1->type != lat2->type)
234     return false;
235
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);
244 }
245
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)
254 {
255   if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
256     {
257       res->type = IPA_BOTTOM;
258       return;
259     }
260   if (lat1->type == IPA_TOP)
261     {
262       res->type = lat2->type;
263       res->constant = lat2->constant;
264       return;
265     }
266   if (lat2->type == IPA_TOP)
267     {
268       res->type = lat1->type;
269       res->constant = lat1->constant;
270       return;
271     }
272   if (!ipcp_lats_are_equal (lat1, lat2))
273     {
274       res->type = IPA_BOTTOM;
275       return;
276     }
277   res->type = lat1->type;
278   res->constant = lat1->constant;
279 }
280
281 /* True when OLD_LAT and NEW_LAT values are not the same.  */
282
283 static bool
284 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
285                       struct ipcp_lattice *new_lat)
286 {
287   if (old_lat->type == new_lat->type)
288     {
289       if (!ipcp_lat_is_const (old_lat))
290         return false;
291       if (ipcp_lats_are_equal (old_lat, new_lat))
292         return false;
293     }
294   return true;
295 }
296
297 /* Print all ipcp_lattices of all functions to F.  */
298 static void
299 ipcp_print_all_lattices (FILE * f)
300 {
301   struct cgraph_node *node;
302   int i, count;
303
304   fprintf (f, "\nLattice:\n");
305   for (node = cgraph_nodes; node; node = node->next)
306     {
307       struct ipa_node_params *info;
308
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++)
315         {
316           struct ipcp_lattice *lat = ipa_get_lattice (info, i);
317
318           fprintf (f, "    param [%d]: ", i);
319           if (lat->type == IPA_CONST_VALUE)
320             {
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)
326                 {
327                   fprintf (f, " -> ");
328                   print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (cst, 0)),
329                                                        0);
330                 }
331             }
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");
342         }
343     }
344 }
345
346 /* Return true if ipcp algorithms would allow cloning NODE.  */
347
348 static bool
349 ipcp_versionable_function_p (struct cgraph_node *node)
350 {
351   struct cgraph_edge *edge;
352
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;
359
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)
363     {
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;
369     }
370
371   return true;
372 }
373
374 /* Return true if this NODE is viable candidate for cloning.  */
375 static bool
376 ipcp_cloning_candidate_p (struct cgraph_node *node)
377 {
378   int n_calls = 0;
379   int n_hot_calls = 0;
380   gcov_type direct_call_sum = 0;
381   struct cgraph_edge *e;
382
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.
386      */
387   if (cgraph_only_called_directly_p (node) || !node->analyzed)
388     return false;
389
390   /* When function address is taken, we are pretty sure it will be called in hidden way.  */
391   if (node->address_taken)
392     {
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;
397     }
398
399   if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
400     {
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;
405     }
406   if (!ipcp_versionable_function_p (node))
407     {
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;
412     }
413   for (e = node->callers; e; e = e->next_caller)
414     {
415       direct_call_sum += e->count;
416       n_calls ++;
417       if (cgraph_maybe_hot_edge_p (e))
418         n_hot_calls ++;
419     }
420
421   if (!n_calls)
422     {
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;
427     }
428   if (inline_summary (node)->self_size < n_calls)
429     {
430       if (dump_file)
431         fprintf (dump_file, "Considering %s for cloning; code would shrink.\n",
432                  cgraph_node_name (node));
433       return true;
434     }
435
436   if (!flag_ipa_cp_clone)
437     {
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;
442     }
443
444   if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
445     {
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;
450     }
451
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)
456     {
457       if (direct_call_sum > node->count * 90 / 100)
458         {
459           if (dump_file)
460             fprintf (dump_file, "Considering %s for cloning; usually called directly.\n",
461                      cgraph_node_name (node));
462           return true;
463         }
464     }
465   if (!n_hot_calls)
466     {
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;
471     }
472   if (dump_file)
473     fprintf (dump_file, "Considering %s for cloning.\n",
474              cgraph_node_name (node));
475   return true;
476 }
477
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.  */
480
481 static bool
482 ipa_set_param_cannot_devirtualize (struct ipa_node_params *info, int i)
483 {
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;
489 }
490
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)
496 {
497   int i;
498   struct ipa_node_params *info = IPA_NODE_REF (node);
499   enum ipa_lattice_type type;
500
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;
511
512   for (i = 0; i < ipa_get_param_count (info) ; i++)
513     {
514       ipa_get_lattice (info, i)->type = type;
515       if (type == IPA_BOTTOM)
516         ipa_set_param_cannot_devirtualize (info, i);
517     }
518 }
519
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)
525 {
526   tree val;
527
528   gcc_assert (ipcp_lat_is_const (lat));
529   val = lat->constant;
530
531   if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
532     {
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;
539     }
540   return val;
541 }
542
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).
546
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)
554 {
555   gcov_type sum;
556   struct cgraph_edge *cs;
557
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);
572 }
573
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)
579 {
580   int i, count;
581   struct cgraph_node *node;
582   bool prop_again;
583
584   prop_again = false;
585   for (node = cgraph_nodes; node; node = node->next)
586     {
587       struct ipa_node_params *info = IPA_NODE_REF (node);
588       count = ipa_get_param_count (info);
589       for (i = 0; i < count; i++)
590         {
591           struct ipcp_lattice *lat = ipa_get_lattice (info, i);
592           if (lat->type == IPA_TOP)
593             {
594               prop_again = true;
595               if (dump_file)
596                 {
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));
601                 }
602               lat->type = IPA_BOTTOM;
603             }
604           if (!ipa_param_cannot_devirtualize_p (info, i)
605               && ipa_param_types_vec_empty (info, i))
606             {
607               prop_again = true;
608               ipa_set_param_cannot_devirtualize (info, i);
609               if (dump_file)
610                 {
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));
616                 }
617             }
618         }
619     }
620   return prop_again;
621 }
622
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.  */
626
627 static bool
628 ipcp_add_param_type (struct ipa_node_params *callee_info, int i, tree binfo)
629 {
630   int j, count;
631
632   if (ipa_param_cannot_devirtualize_p (callee_info, i))
633     return false;
634
635   if (callee_info->params[i].types)
636     {
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;
641     }
642
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);
646
647   VEC_safe_push (tree, heap, callee_info->params[i].types, binfo);
648   return true;
649 }
650
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.  */
655
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)
660 {
661   int caller_idx, j, count;
662   bool res;
663
664   if (ipa_param_cannot_devirtualize_p (callee_info, callee_idx))
665     return false;
666
667   if (jf->type == IPA_JF_PASS_THROUGH)
668     {
669       if (jf->value.pass_through.operation != NOP_EXPR)
670         {
671           ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
672           return true;
673         }
674       caller_idx = jf->value.pass_through.formal_id;
675     }
676   else
677     caller_idx = jf->value.ancestor.formal_id;
678
679   if (ipa_param_cannot_devirtualize_p (caller_info, caller_idx))
680     {
681       ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
682       return true;
683     }
684
685   if (!caller_info->params[caller_idx].types)
686     return false;
687
688   res = false;
689   count = VEC_length (tree, caller_info->params[caller_idx].types);
690   for (j = 0; j < count; j++)
691     {
692       tree binfo = VEC_index (tree, caller_info->params[caller_idx].types, j);
693       if (jf->type == IPA_JF_ANCESTOR)
694         {
695           binfo = get_binfo_at_offset (binfo, jf->value.ancestor.offset,
696                                        jf->value.ancestor.type);
697           if (!binfo)
698             {
699               ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
700               return true;
701             }
702         }
703       res |= ipcp_add_param_type (callee_info, callee_idx, binfo);
704     }
705   return res;
706 }
707
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.  */
711
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)
716 {
717   switch (jf->type)
718     {
719     case IPA_JF_UNKNOWN:
720     case IPA_JF_CONST_MEMBER_PTR:
721     case IPA_JF_CONST:
722       break;
723
724     case IPA_JF_KNOWN_TYPE:
725       return ipcp_add_param_type (callee_info, i, jf->value.base_binfo);
726
727     case IPA_JF_PASS_THROUGH:
728     case IPA_JF_ANCESTOR:
729       return ipcp_copy_types (caller_info, callee_info, i, jf);
730     }
731
732   /* If we reach this we cannot use this parameter for devirtualization.  */
733   return !ipa_set_param_cannot_devirtualize (callee_info, i);
734 }
735
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)
740 {
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;
749
750   ipa_check_create_node_params ();
751   ipa_check_create_edge_args ();
752
753   /* Initialize worklist to contain all functions.  */
754   wl = ipa_init_func_list ();
755   while (wl)
756     {
757       struct cgraph_node *node = ipa_pop_func_from_list (&wl);
758       struct ipa_node_params *info = IPA_NODE_REF (node);
759
760       for (cs = node->callees; cs; cs = cs->next_callee)
761         {
762           struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
763           struct ipa_edge_args *args = IPA_EDGE_REF (cs);
764
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;
769
770           count = ipa_get_cs_argument_count (args);
771           for (i = 0; i < count; i++)
772             {
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))
778                 {
779                   dest_lat->type = new_lat.type;
780                   dest_lat->constant = new_lat.constant;
781                   ipa_push_func_to_list (&wl, cs->callee);
782                 }
783
784               if (ipcp_propagate_types (info, callee_info, jump_func, i))
785                 ipa_push_func_to_list (&wl, cs->callee);
786             }
787         }
788     }
789 }
790
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)
795 {
796   struct cgraph_node *node;
797   n_cloning_candidates = 0;
798
799   if (dump_file)
800     fprintf (dump_file, "\nIPA iterate stage:\n\n");
801
802   if (in_lto_p)
803     ipa_update_after_lto_read ();
804
805   for (node = cgraph_nodes; node; node = node->next)
806     {
807       ipcp_initialize_node_lattices (node);
808       ipcp_compute_node_scale (node);
809     }
810   if (dump_file && (dump_flags & TDF_DETAILS))
811     {
812       ipcp_print_all_lattices (dump_file);
813       ipcp_function_scale_print (dump_file);
814     }
815
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.  */
820     {
821       gcc_assert (n_cloning_candidates);
822       ipcp_propagate_stage ();
823     }
824   if (dump_file)
825     {
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);
830     }
831 }
832
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)
837 {
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);
841 }
842
843 /* Print count scale data structures.  */
844 static void
845 ipcp_function_scale_print (FILE * f)
846 {
847   struct cgraph_node *node;
848
849   for (node = cgraph_nodes; node; node = node->next)
850     {
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));
856     }
857 }
858
859 /* Print counts of all cgraph nodes.  */
860 static void
861 ipcp_print_func_profile_counts (FILE * f)
862 {
863   struct cgraph_node *node;
864
865   for (node = cgraph_nodes; node; node = node->next)
866     {
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);
870     }
871 }
872
873 /* Print counts of all cgraph edges.  */
874 static void
875 ipcp_print_call_profile_counts (FILE * f)
876 {
877   struct cgraph_node *node;
878   struct cgraph_edge *cs;
879
880   for (node = cgraph_nodes; node; node = node->next)
881     {
882       for (cs = node->callees; cs; cs = cs->next_callee)
883         {
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);
888         }
889     }
890 }
891
892 /* Print profile info for all functions.  */
893 static void
894 ipcp_print_profile_data (FILE * f)
895 {
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);
900 }
901
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)
908 {
909   struct ipa_replace_map *replace_map;
910   tree const_val;
911
912   const_val = build_const_val (lat, TREE_TYPE (parm_tree));
913   if (const_val == NULL_TREE)
914     {
915       if (dump_file)
916         {
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");
922         }
923       return NULL;
924     }
925   replace_map = ggc_alloc_ipa_replace_map ();
926   if (dump_file)
927     {
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");
933     }
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;
938
939   return replace_map;
940 }
941
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)
946 {
947   struct ipa_node_params *orig_callee_info;
948   int i, count;
949   struct cgraph_node *node = cs->callee, *orig;
950
951   if (!n_cloning_candidates)
952     return false;
953
954   if ((orig = ipcp_get_orig_node (node)) != NULL)
955     node = orig;
956   if (ipcp_get_orig_node (cs->caller))
957     return false;
958
959   orig_callee_info = IPA_NODE_REF (node);
960   count = ipa_get_param_count (orig_callee_info);
961   for (i = 0; i < count; i++)
962     {
963       struct ipcp_lattice *lat = ipa_get_lattice (orig_callee_info, i);
964       struct ipa_jump_func *jump_func;
965
966       jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
967       if ((ipcp_lat_is_const (lat)
968            && jump_func->type != IPA_JF_CONST)
969           || (!ipa_param_cannot_devirtualize_p (orig_callee_info, i)
970               && !ipa_param_types_vec_empty (orig_callee_info, i)
971               && jump_func->type != IPA_JF_CONST
972               && jump_func->type != IPA_JF_KNOWN_TYPE))
973         return true;
974     }
975
976   return false;
977 }
978
979 /* Fix the callsites and the call graph after function cloning was done.  */
980 static void
981 ipcp_update_callgraph (void)
982 {
983   struct cgraph_node *node;
984
985   for (node = cgraph_nodes; node; node = node->next)
986     if (node->analyzed && ipcp_node_is_clone (node))
987       {
988         bitmap args_to_skip = NULL;
989         struct cgraph_node *orig_node = ipcp_get_orig_node (node);
990         struct ipa_node_params *info = IPA_NODE_REF (orig_node);
991         int i, count = ipa_get_param_count (info);
992         struct cgraph_edge *cs, *next;
993
994         if (node->local.can_change_signature)
995           {
996             args_to_skip = BITMAP_ALLOC (NULL);
997             for (i = 0; i < count; i++)
998               {
999                 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1000
1001                 /* We can proactively remove obviously unused arguments.  */
1002                 if (!ipa_is_param_used (info, i))
1003                   {
1004                     bitmap_set_bit (args_to_skip, i);
1005                     continue;
1006                   }
1007
1008                 if (lat->type == IPA_CONST_VALUE)
1009                   bitmap_set_bit (args_to_skip, i);
1010               }
1011           }
1012         for (cs = node->callers; cs; cs = next)
1013           {
1014             next = cs->next_caller;
1015             if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
1016               {
1017                 if (dump_file)
1018                   fprintf (dump_file, "Redirecting edge %s/%i -> %s/%i "
1019                            "back to %s/%i.",
1020                            cgraph_node_name (cs->caller), cs->caller->uid,
1021                            cgraph_node_name (cs->callee), cs->callee->uid,
1022                            cgraph_node_name (orig_node), orig_node->uid);
1023                 cgraph_redirect_edge_callee (cs, orig_node);
1024               }
1025           }
1026       }
1027 }
1028
1029 /* Update profiling info for versioned functions and the functions they were
1030    versioned from.  */
1031 static void
1032 ipcp_update_profiling (void)
1033 {
1034   struct cgraph_node *node, *orig_node;
1035   gcov_type scale, scale_complement;
1036   struct cgraph_edge *cs;
1037
1038   for (node = cgraph_nodes; node; node = node->next)
1039     {
1040       if (ipcp_node_is_clone (node))
1041         {
1042           orig_node = ipcp_get_orig_node (node);
1043           scale = ipcp_get_node_scale (orig_node);
1044           node->count = orig_node->count * scale / REG_BR_PROB_BASE;
1045           scale_complement = REG_BR_PROB_BASE - scale;
1046
1047           gcc_assert (scale_complement >= 0);
1048           orig_node->count =
1049             orig_node->count * scale_complement / REG_BR_PROB_BASE;
1050           for (cs = node->callees; cs; cs = cs->next_callee)
1051             cs->count = cs->count * scale / REG_BR_PROB_BASE;
1052           for (cs = orig_node->callees; cs; cs = cs->next_callee)
1053             cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
1054         }
1055     }
1056 }
1057
1058 /* If NODE was cloned, how much would program grow? */
1059 static long
1060 ipcp_estimate_growth (struct cgraph_node *node)
1061 {
1062   struct cgraph_edge *cs;
1063   int redirectable_node_callers = 0;
1064   int removable_args = 0;
1065   bool need_original
1066      = !cgraph_will_be_removed_from_program_if_no_direct_calls (node);
1067   struct ipa_node_params *info;
1068   int i, count;
1069   int growth;
1070
1071   for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1072     if (cs->caller == node || !ipcp_need_redirect_p (cs))
1073       redirectable_node_callers++;
1074     else
1075       need_original = true;
1076
1077   /* If we will be able to fully replace original node, we never increase
1078      program size.  */
1079   if (!need_original)
1080     return 0;
1081
1082   info = IPA_NODE_REF (node);
1083   count = ipa_get_param_count (info);
1084   if (node->local.can_change_signature)
1085     for (i = 0; i < count; i++)
1086       {
1087         struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1088
1089         /* We can proactively remove obviously unused arguments.  */
1090         if (!ipa_is_param_used (info, i))
1091           removable_args++;
1092
1093         if (lat->type == IPA_CONST_VALUE)
1094           removable_args++;
1095       }
1096
1097   /* We make just very simple estimate of savings for removal of operand from
1098      call site.  Precise cost is difficult to get, as our size metric counts
1099      constants and moves as free.  Generally we are looking for cases that
1100      small function is called very many times.  */
1101   growth = inline_summary (node)->self_size
1102            - removable_args * redirectable_node_callers;
1103   if (growth < 0)
1104     return 0;
1105   return growth;
1106 }
1107
1108
1109 /* Estimate cost of cloning NODE.  */
1110 static long
1111 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1112 {
1113   int freq_sum = 1;
1114   gcov_type count_sum = 1;
1115   struct cgraph_edge *e;
1116   int cost;
1117
1118   cost = ipcp_estimate_growth (node) * 1000;
1119   if (!cost)
1120     {
1121       if (dump_file)
1122         fprintf (dump_file, "Versioning of %s will save code size\n",
1123                  cgraph_node_name (node));
1124       return 0;
1125     }
1126
1127   for (e = node->callers; e; e = e->next_caller)
1128     if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1129         && !ipcp_need_redirect_p (e))
1130       {
1131         count_sum += e->count;
1132         freq_sum += e->frequency + 1;
1133       }
1134
1135   if (max_count)
1136     cost /= count_sum * 1000 / max_count + 1;
1137   else
1138     cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1139   if (dump_file)
1140     fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1141              cgraph_node_name (node), cost, inline_summary (node)->self_size,
1142              freq_sum);
1143   return cost + 1;
1144 }
1145
1146 /* Walk indirect calls of NODE and if any polymorphic can be turned into a
1147    direct one now, do so.  */
1148
1149 static void
1150 ipcp_process_devirtualization_opportunities (struct cgraph_node *node)
1151 {
1152   struct ipa_node_params *info = IPA_NODE_REF (node);
1153   struct cgraph_edge *ie, *next_ie;
1154
1155   for (ie = node->indirect_calls; ie; ie = next_ie)
1156     {
1157       int param_index;
1158       HOST_WIDE_INT token, anc_offset;
1159       tree target, delta, otr_type;
1160       struct ipcp_lattice *lat;
1161
1162       next_ie = ie->next_callee;
1163       if (!ie->indirect_info->polymorphic)
1164         continue;
1165       param_index = ie->indirect_info->param_index;
1166       if (param_index == -1)
1167         continue;
1168
1169       lat = ipa_get_lattice (info, param_index);
1170       token = ie->indirect_info->otr_token;
1171       anc_offset = ie->indirect_info->anc_offset;
1172       otr_type = ie->indirect_info->otr_type;
1173       target = NULL_TREE;
1174       if (lat->type == IPA_CONST_VALUE)
1175         {
1176           tree binfo = gimple_extract_devirt_binfo_from_cst (lat->constant);
1177           if (!binfo)
1178             continue;
1179           binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1180           if (!binfo)
1181             continue;
1182           target = gimple_get_virt_method_for_binfo (token, binfo, &delta,
1183                                                      false);
1184         }
1185       else
1186         {
1187           int  types_count, j;
1188
1189           if (ipa_param_cannot_devirtualize_p (info, param_index)
1190               || ipa_param_types_vec_empty (info, param_index))
1191             continue;
1192
1193           types_count = VEC_length (tree, info->params[param_index].types);
1194           for (j = 0; j < types_count; j++)
1195             {
1196               tree binfo = VEC_index (tree, info->params[param_index].types, j);
1197               tree d, t;
1198
1199               binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1200               if (!binfo)
1201                 {
1202                   target = NULL_TREE;
1203                   break;
1204                 }
1205
1206               t = gimple_get_virt_method_for_binfo (token, binfo, &d, true);
1207               if (!t)
1208                 {
1209                   target = NULL_TREE;
1210                   break;
1211                 }
1212               else if (!target)
1213                 {
1214                   target = t;
1215                   delta = d;
1216                 }
1217               else if (target != t || !tree_int_cst_equal (delta, d))
1218                 {
1219                   target = NULL_TREE;
1220                   break;
1221                 }
1222             }
1223         }
1224
1225       if (target)
1226         ipa_make_edge_direct_to_target (ie, target, delta);
1227     }
1228 }
1229
1230 /* Return number of live constant parameters.  */
1231 static int
1232 ipcp_const_param_count (struct cgraph_node *node)
1233 {
1234   int const_param = 0;
1235   struct ipa_node_params *info = IPA_NODE_REF (node);
1236   int count = ipa_get_param_count (info);
1237   int i;
1238
1239   for (i = 0; i < count; i++)
1240     {
1241       struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1242       if ((ipcp_lat_is_insertable (lat)
1243           /* Do not count obviously unused arguments.  */
1244            && ipa_is_param_used (info, i))
1245           || (!ipa_param_cannot_devirtualize_p (info, i)
1246               && !ipa_param_types_vec_empty (info, i)))
1247         const_param++;
1248     }
1249   return const_param;
1250 }
1251
1252 /* Given that a formal parameter of NODE given by INDEX is known to be constant
1253    CST, try to find any indirect edges that can be made direct and make them
1254    so.  Note that INDEX is the number the parameter at the time of analyzing
1255    parameter uses and parameter removals should not be considered for it.  (In
1256    fact, the parameter itself has just been removed.)  */
1257
1258 static void
1259 ipcp_discover_new_direct_edges (struct cgraph_node *node, int index, tree cst)
1260 {
1261   struct cgraph_edge *ie, *next_ie;
1262
1263   for (ie = node->indirect_calls; ie; ie = next_ie)
1264     {
1265       struct cgraph_indirect_call_info *ici = ie->indirect_info;
1266
1267       next_ie = ie->next_callee;
1268       if (ici->param_index != index
1269           || ici->polymorphic)
1270         continue;
1271
1272       ipa_make_edge_direct_to_target (ie, cst, NULL_TREE);
1273     }
1274 }
1275
1276
1277 /* Propagate the constant parameters found by ipcp_iterate_stage()
1278    to the function's code.  */
1279 static void
1280 ipcp_insert_stage (void)
1281 {
1282   struct cgraph_node *node, *node1 = NULL;
1283   int i;
1284   VEC (cgraph_edge_p, heap) * redirect_callers;
1285   VEC (ipa_replace_map_p,gc)* replace_trees;
1286   int node_callers, count;
1287   tree parm_tree;
1288   struct ipa_replace_map *replace_param;
1289   fibheap_t heap;
1290   long overall_size = 0, new_size = 0;
1291   long max_new_size;
1292
1293   ipa_check_create_node_params ();
1294   ipa_check_create_edge_args ();
1295   if (dump_file)
1296     fprintf (dump_file, "\nIPA insert stage:\n\n");
1297
1298   dead_nodes = BITMAP_ALLOC (NULL);
1299
1300   for (node = cgraph_nodes; node; node = node->next)
1301     if (node->analyzed)
1302       {
1303         if (node->count > max_count)
1304           max_count = node->count;
1305         overall_size += inline_summary (node)->self_size;
1306       }
1307
1308   max_new_size = overall_size;
1309   if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1310     max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1311   max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1312
1313   /* First collect all functions we proved to have constant arguments to
1314      heap.  */
1315   heap = fibheap_new ();
1316   for (node = cgraph_nodes; node; node = node->next)
1317     {
1318       struct ipa_node_params *info;
1319       /* Propagation of the constant is forbidden in certain conditions.  */
1320       if (!node->analyzed || !ipcp_node_modifiable_p (node))
1321           continue;
1322       info = IPA_NODE_REF (node);
1323       if (ipa_is_called_with_var_arguments (info))
1324         continue;
1325       if (ipcp_const_param_count (node))
1326         node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node),
1327                                     node);
1328      }
1329
1330   /* Now clone in priority order until code size growth limits are met or
1331      heap is emptied.  */
1332   while (!fibheap_empty (heap))
1333     {
1334       struct ipa_node_params *info;
1335       int growth = 0;
1336       bitmap args_to_skip;
1337       struct cgraph_edge *cs;
1338
1339       node = (struct cgraph_node *)fibheap_extract_min (heap);
1340       node->aux = NULL;
1341       if (dump_file)
1342         fprintf (dump_file, "considering function %s\n",
1343                  cgraph_node_name (node));
1344
1345       growth = ipcp_estimate_growth (node);
1346
1347       if (new_size + growth > max_new_size)
1348         break;
1349       if (growth
1350           && cgraph_optimize_for_size_p (node))
1351         {
1352           if (dump_file)
1353             fprintf (dump_file, "Not versioning, cold code would grow");
1354           continue;
1355         }
1356
1357       info = IPA_NODE_REF (node);
1358       count = ipa_get_param_count (info);
1359
1360       replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1361
1362       if (node->local.can_change_signature)
1363         args_to_skip = BITMAP_GGC_ALLOC ();
1364       else
1365         args_to_skip = NULL;
1366       for (i = 0; i < count; i++)
1367         {
1368           struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1369           parm_tree = ipa_get_param (info, i);
1370
1371           /* We can proactively remove obviously unused arguments.  */
1372           if (!ipa_is_param_used (info, i))
1373             {
1374               if (args_to_skip)
1375                 bitmap_set_bit (args_to_skip, i);
1376               continue;
1377             }
1378
1379           if (lat->type == IPA_CONST_VALUE)
1380             {
1381               replace_param =
1382                 ipcp_create_replace_map (parm_tree, lat);
1383               if (replace_param == NULL)
1384                 break;
1385               VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1386               if (args_to_skip)
1387                 bitmap_set_bit (args_to_skip, i);
1388             }
1389         }
1390       if (i < count)
1391         {
1392           if (dump_file)
1393             fprintf (dump_file, "Not versioning, some parameters couldn't be replaced");
1394           continue;
1395         }
1396
1397       new_size += growth;
1398
1399       /* Look if original function becomes dead after cloning.  */
1400       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1401         if (cs->caller == node || ipcp_need_redirect_p (cs))
1402           break;
1403       if (!cs && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1404         bitmap_set_bit (dead_nodes, node->uid);
1405
1406       /* Compute how many callers node has.  */
1407       node_callers = 0;
1408       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1409         node_callers++;
1410       redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1411       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1412         if (!cs->indirect_inlining_edge)
1413           VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1414
1415       /* Redirecting all the callers of the node to the
1416          new versioned node.  */
1417       node1 =
1418         cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1419                                      args_to_skip, "constprop");
1420       args_to_skip = NULL;
1421       VEC_free (cgraph_edge_p, heap, redirect_callers);
1422       replace_trees = NULL;
1423
1424       if (node1 == NULL)
1425         continue;
1426       ipcp_process_devirtualization_opportunities (node1);
1427
1428       if (dump_file)
1429         fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1430                  cgraph_node_name (node), (int)growth, (int)new_size);
1431       ipcp_init_cloned_node (node, node1);
1432
1433       info = IPA_NODE_REF (node);
1434       for (i = 0; i < count; i++)
1435         {
1436           struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1437           if (lat->type == IPA_CONST_VALUE)
1438             ipcp_discover_new_direct_edges (node1, i, lat->constant);
1439         }
1440
1441       if (dump_file)
1442         dump_function_to_file (node1->decl, dump_file, dump_flags);
1443
1444       for (cs = node->callees; cs; cs = cs->next_callee)
1445         if (cs->callee->aux)
1446           {
1447             fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1448             cs->callee->aux = fibheap_insert (heap,
1449                                               ipcp_estimate_cloning_cost (cs->callee),
1450                                               cs->callee);
1451           }
1452     }
1453
1454   while (!fibheap_empty (heap))
1455     {
1456       if (dump_file)
1457         fprintf (dump_file, "skipping function %s\n",
1458                  cgraph_node_name (node));
1459       node = (struct cgraph_node *) fibheap_extract_min (heap);
1460       node->aux = NULL;
1461     }
1462   fibheap_delete (heap);
1463   BITMAP_FREE (dead_nodes);
1464   ipcp_update_callgraph ();
1465   ipcp_update_profiling ();
1466 }
1467
1468 /* The IPCP driver.  */
1469 static unsigned int
1470 ipcp_driver (void)
1471 {
1472   cgraph_remove_unreachable_nodes (true,dump_file);
1473   if (dump_file)
1474     {
1475       fprintf (dump_file, "\nIPA structures before propagation:\n");
1476       if (dump_flags & TDF_DETAILS)
1477         ipa_print_all_params (dump_file);
1478       ipa_print_all_jump_functions (dump_file);
1479     }
1480   ipa_check_create_node_params ();
1481   ipa_check_create_edge_args ();
1482   /* 2. Do the interprocedural propagation.  */
1483   ipcp_iterate_stage ();
1484   /* 3. Insert the constants found to the functions.  */
1485   ipcp_insert_stage ();
1486   if (dump_file && (dump_flags & TDF_DETAILS))
1487     {
1488       fprintf (dump_file, "\nProfiling info after insert stage:\n");
1489       ipcp_print_profile_data (dump_file);
1490     }
1491   /* Free all IPCP structures.  */
1492   ipa_free_all_structures_after_ipa_cp ();
1493   if (dump_file)
1494     fprintf (dump_file, "\nIPA constant propagation end\n");
1495   return 0;
1496 }
1497
1498 /* Initialization and computation of IPCP data structures.  This is the initial
1499    intraprocedural analysis of functions, which gathers information to be
1500    propagated later on.  */
1501
1502 static void
1503 ipcp_generate_summary (void)
1504 {
1505   struct cgraph_node *node;
1506
1507   if (dump_file)
1508     fprintf (dump_file, "\nIPA constant propagation start:\n");
1509   ipa_register_cgraph_hooks ();
1510
1511   for (node = cgraph_nodes; node; node = node->next)
1512     if (node->analyzed)
1513       {
1514         /* Unreachable nodes should have been eliminated before ipcp.  */
1515         gcc_assert (node->needed || node->reachable);
1516
1517         inline_summary (node)->versionable = tree_versionable_function_p (node->decl);
1518         ipa_analyze_node (node);
1519       }
1520 }
1521
1522 /* Write ipcp summary for nodes in SET.  */
1523 static void
1524 ipcp_write_summary (cgraph_node_set set,
1525                     varpool_node_set vset ATTRIBUTE_UNUSED)
1526 {
1527   ipa_prop_write_jump_functions (set);
1528 }
1529
1530 /* Read ipcp summary.  */
1531 static void
1532 ipcp_read_summary (void)
1533 {
1534   ipa_prop_read_jump_functions ();
1535 }
1536
1537 /* Gate for IPCP optimization.  */
1538 static bool
1539 cgraph_gate_cp (void)
1540 {
1541   /* FIXME: We should remove the optimize check after we ensure we never run
1542      IPA passes when not optimizing.  */
1543   return flag_ipa_cp && optimize;
1544 }
1545
1546 struct ipa_opt_pass_d pass_ipa_cp =
1547 {
1548  {
1549   IPA_PASS,
1550   "cp",                         /* name */
1551   cgraph_gate_cp,               /* gate */
1552   ipcp_driver,                  /* execute */
1553   NULL,                         /* sub */
1554   NULL,                         /* next */
1555   0,                            /* static_pass_number */
1556   TV_IPA_CONSTANT_PROP,         /* tv_id */
1557   0,                            /* properties_required */
1558   0,                            /* properties_provided */
1559   0,                            /* properties_destroyed */
1560   0,                            /* todo_flags_start */
1561   TODO_dump_cgraph | TODO_dump_func |
1562   TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
1563  },
1564  ipcp_generate_summary,                 /* generate_summary */
1565  ipcp_write_summary,                    /* write_summary */
1566  ipcp_read_summary,                     /* read_summary */
1567  NULL,                                  /* write_optimization_summary */
1568  NULL,                                  /* read_optimization_summary */
1569  NULL,                                  /* stmt_fixup */
1570  0,                                     /* TODOs */
1571  NULL,                                  /* function_transform */
1572  NULL,                                  /* variable_transform */
1573 };