OSDN Git Service

* ipa-cp.c (ipcp_process_devirtualization_opportunities):
[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   /* We can't redirect anything in thunks, yet.  */
955   if (cs->caller->thunk.thunk_p)
956     return true;
957
958   if ((orig = ipcp_get_orig_node (node)) != NULL)
959     node = orig;
960   if (ipcp_get_orig_node (cs->caller))
961     return false;
962
963   orig_callee_info = IPA_NODE_REF (node);
964   count = ipa_get_param_count (orig_callee_info);
965   for (i = 0; i < count; i++)
966     {
967       struct ipcp_lattice *lat = ipa_get_lattice (orig_callee_info, i);
968       struct ipa_jump_func *jump_func;
969
970       jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
971       if ((ipcp_lat_is_const (lat)
972            && jump_func->type != IPA_JF_CONST)
973           || (!ipa_param_cannot_devirtualize_p (orig_callee_info, i)
974               && !ipa_param_types_vec_empty (orig_callee_info, i)
975               && jump_func->type != IPA_JF_CONST
976               && jump_func->type != IPA_JF_KNOWN_TYPE))
977         return true;
978     }
979
980   return false;
981 }
982
983 /* Fix the callsites and the call graph after function cloning was done.  */
984 static void
985 ipcp_update_callgraph (void)
986 {
987   struct cgraph_node *node;
988
989   for (node = cgraph_nodes; node; node = node->next)
990     if (node->analyzed && ipcp_node_is_clone (node))
991       {
992         bitmap args_to_skip = NULL;
993         struct cgraph_node *orig_node = ipcp_get_orig_node (node);
994         struct ipa_node_params *info = IPA_NODE_REF (orig_node);
995         int i, count = ipa_get_param_count (info);
996         struct cgraph_edge *cs, *next;
997
998         if (node->local.can_change_signature)
999           {
1000             args_to_skip = BITMAP_ALLOC (NULL);
1001             for (i = 0; i < count; i++)
1002               {
1003                 struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1004
1005                 /* We can proactively remove obviously unused arguments.  */
1006                 if (!ipa_is_param_used (info, i))
1007                   {
1008                     bitmap_set_bit (args_to_skip, i);
1009                     continue;
1010                   }
1011
1012                 if (lat->type == IPA_CONST_VALUE)
1013                   bitmap_set_bit (args_to_skip, i);
1014               }
1015           }
1016         for (cs = node->callers; cs; cs = next)
1017           {
1018             next = cs->next_caller;
1019             if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
1020               {
1021                 if (dump_file)
1022                   fprintf (dump_file, "Redirecting edge %s/%i -> %s/%i "
1023                            "back to %s/%i.",
1024                            cgraph_node_name (cs->caller), cs->caller->uid,
1025                            cgraph_node_name (cs->callee), cs->callee->uid,
1026                            cgraph_node_name (orig_node), orig_node->uid);
1027                 cgraph_redirect_edge_callee (cs, orig_node);
1028               }
1029           }
1030       }
1031 }
1032
1033 /* Update profiling info for versioned functions and the functions they were
1034    versioned from.  */
1035 static void
1036 ipcp_update_profiling (void)
1037 {
1038   struct cgraph_node *node, *orig_node;
1039   gcov_type scale, scale_complement;
1040   struct cgraph_edge *cs;
1041
1042   for (node = cgraph_nodes; node; node = node->next)
1043     {
1044       if (ipcp_node_is_clone (node))
1045         {
1046           orig_node = ipcp_get_orig_node (node);
1047           scale = ipcp_get_node_scale (orig_node);
1048           node->count = orig_node->count * scale / REG_BR_PROB_BASE;
1049           scale_complement = REG_BR_PROB_BASE - scale;
1050
1051           gcc_assert (scale_complement >= 0);
1052           orig_node->count =
1053             orig_node->count * scale_complement / REG_BR_PROB_BASE;
1054           for (cs = node->callees; cs; cs = cs->next_callee)
1055             cs->count = cs->count * scale / REG_BR_PROB_BASE;
1056           for (cs = orig_node->callees; cs; cs = cs->next_callee)
1057             cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
1058         }
1059     }
1060 }
1061
1062 /* If NODE was cloned, how much would program grow? */
1063 static long
1064 ipcp_estimate_growth (struct cgraph_node *node)
1065 {
1066   struct cgraph_edge *cs;
1067   int redirectable_node_callers = 0;
1068   int removable_args = 0;
1069   bool need_original
1070      = !cgraph_will_be_removed_from_program_if_no_direct_calls (node);
1071   VEC (tree, heap) *known_vals = NULL;
1072   struct ipa_node_params *info;
1073   int i, count;
1074   int growth;
1075
1076   for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1077     if (cs->caller == node || !ipcp_need_redirect_p (cs))
1078       redirectable_node_callers++;
1079     else
1080       need_original = true;
1081
1082   /* If we will be able to fully replace original node, we never increase
1083      program size.  */
1084   if (!need_original)
1085     return 0;
1086
1087   info = IPA_NODE_REF (node);
1088   count = ipa_get_param_count (info);
1089   VEC_safe_grow_cleared (tree, heap, known_vals, count);
1090   if (node->local.can_change_signature)
1091     for (i = 0; i < count; i++)
1092       {
1093         struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1094
1095         /* We can proactively remove obviously unused arguments.  */
1096         if (!ipa_is_param_used (info, i))
1097           removable_args++;
1098
1099         if (lat->type == IPA_CONST_VALUE)
1100           {
1101             removable_args++;
1102             VEC_replace (tree, known_vals, i, lat->constant);
1103           }
1104       }
1105
1106   /* We make just very simple estimate of savings for removal of operand from
1107      call site.  Precise cost is difficult to get, as our size metric counts
1108      constants and moves as free.  Generally we are looking for cases that
1109      small function is called very many times.  */
1110   estimate_ipcp_clone_size_and_time (node, known_vals, &growth, NULL);
1111   VEC_free (tree, heap, known_vals);
1112   growth = growth
1113            - removable_args * redirectable_node_callers;
1114   if (growth < 0)
1115     return 0;
1116   return growth;
1117 }
1118
1119
1120 /* Estimate cost of cloning NODE.  */
1121 static long
1122 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1123 {
1124   int freq_sum = 1;
1125   gcov_type count_sum = 1;
1126   struct cgraph_edge *e;
1127   int cost;
1128
1129   cost = ipcp_estimate_growth (node) * 1000;
1130   if (!cost)
1131     {
1132       if (dump_file)
1133         fprintf (dump_file, "Versioning of %s will save code size\n",
1134                  cgraph_node_name (node));
1135       return 0;
1136     }
1137
1138   for (e = node->callers; e; e = e->next_caller)
1139     if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1140         && !ipcp_need_redirect_p (e))
1141       {
1142         count_sum += e->count;
1143         freq_sum += e->frequency + 1;
1144       }
1145
1146   if (max_count)
1147     cost /= count_sum * 1000 / max_count + 1;
1148   else
1149     cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1150   if (dump_file)
1151     fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1152              cgraph_node_name (node), cost, inline_summary (node)->self_size,
1153              freq_sum);
1154   return cost + 1;
1155 }
1156
1157 /* Walk indirect calls of NODE and if any polymorphic can be turned into a
1158    direct one now, do so.  */
1159
1160 static void
1161 ipcp_process_devirtualization_opportunities (struct cgraph_node *node)
1162 {
1163   struct ipa_node_params *info = IPA_NODE_REF (node);
1164   struct cgraph_edge *ie, *next_ie;
1165
1166   for (ie = node->indirect_calls; ie; ie = next_ie)
1167     {
1168       int param_index;
1169       HOST_WIDE_INT token, anc_offset;
1170       tree target, delta, otr_type;
1171       struct ipcp_lattice *lat;
1172
1173       next_ie = ie->next_callee;
1174       if (!ie->indirect_info->polymorphic)
1175         continue;
1176       param_index = ie->indirect_info->param_index;
1177       if (param_index == -1)
1178         continue;
1179
1180       lat = ipa_get_lattice (info, param_index);
1181       token = ie->indirect_info->otr_token;
1182       anc_offset = ie->indirect_info->anc_offset;
1183       otr_type = ie->indirect_info->otr_type;
1184       target = NULL_TREE;
1185       if (lat->type == IPA_CONST_VALUE)
1186         {
1187           tree binfo = gimple_extract_devirt_binfo_from_cst (lat->constant);
1188           if (!binfo)
1189             continue;
1190           binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1191           if (!binfo)
1192             continue;
1193           target = gimple_get_virt_method_for_binfo (token, binfo, &delta);
1194         }
1195       else
1196         {
1197           int  types_count, j;
1198
1199           if (ipa_param_cannot_devirtualize_p (info, param_index)
1200               || ipa_param_types_vec_empty (info, param_index))
1201             continue;
1202
1203           types_count = VEC_length (tree, info->params[param_index].types);
1204           for (j = 0; j < types_count; j++)
1205             {
1206               tree binfo = VEC_index (tree, info->params[param_index].types, j);
1207               tree d, t;
1208
1209               binfo = get_binfo_at_offset (binfo, anc_offset, otr_type);
1210               if (!binfo)
1211                 {
1212                   target = NULL_TREE;
1213                   break;
1214                 }
1215
1216               t = gimple_get_virt_method_for_binfo (token, binfo, &d);
1217               if (!t)
1218                 {
1219                   target = NULL_TREE;
1220                   break;
1221                 }
1222               else if (!target)
1223                 {
1224                   target = t;
1225                   delta = d;
1226                 }
1227               else if (target != t || !tree_int_cst_equal (delta, d))
1228                 {
1229                   target = NULL_TREE;
1230                   break;
1231                 }
1232             }
1233         }
1234
1235       if (target)
1236         ipa_make_edge_direct_to_target (ie, target, delta);
1237     }
1238 }
1239
1240 /* Return number of live constant parameters.  */
1241 static int
1242 ipcp_const_param_count (struct cgraph_node *node)
1243 {
1244   int const_param = 0;
1245   struct ipa_node_params *info = IPA_NODE_REF (node);
1246   int count = ipa_get_param_count (info);
1247   int i;
1248
1249   for (i = 0; i < count; i++)
1250     {
1251       struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1252       if ((ipcp_lat_is_insertable (lat)
1253           /* Do not count obviously unused arguments.  */
1254            && ipa_is_param_used (info, i))
1255           || (!ipa_param_cannot_devirtualize_p (info, i)
1256               && !ipa_param_types_vec_empty (info, i)))
1257         const_param++;
1258     }
1259   return const_param;
1260 }
1261
1262 /* Given that a formal parameter of NODE given by INDEX is known to be constant
1263    CST, try to find any indirect edges that can be made direct and make them
1264    so.  Note that INDEX is the number the parameter at the time of analyzing
1265    parameter uses and parameter removals should not be considered for it.  (In
1266    fact, the parameter itself has just been removed.)  */
1267
1268 static void
1269 ipcp_discover_new_direct_edges (struct cgraph_node *node, int index, tree cst)
1270 {
1271   struct cgraph_edge *ie, *next_ie;
1272
1273   for (ie = node->indirect_calls; ie; ie = next_ie)
1274     {
1275       struct cgraph_indirect_call_info *ici = ie->indirect_info;
1276
1277       next_ie = ie->next_callee;
1278       if (ici->param_index != index
1279           || ici->polymorphic)
1280         continue;
1281
1282       ipa_make_edge_direct_to_target (ie, cst, NULL_TREE);
1283     }
1284 }
1285
1286
1287 /* Propagate the constant parameters found by ipcp_iterate_stage()
1288    to the function's code.  */
1289 static void
1290 ipcp_insert_stage (void)
1291 {
1292   struct cgraph_node *node, *node1 = NULL;
1293   int i;
1294   VEC (cgraph_edge_p, heap) * redirect_callers;
1295   VEC (ipa_replace_map_p,gc)* replace_trees;
1296   int node_callers, count;
1297   tree parm_tree;
1298   struct ipa_replace_map *replace_param;
1299   fibheap_t heap;
1300   long overall_size = 0, new_size = 0;
1301   long max_new_size;
1302
1303   ipa_check_create_node_params ();
1304   ipa_check_create_edge_args ();
1305   if (dump_file)
1306     fprintf (dump_file, "\nIPA insert stage:\n\n");
1307
1308   dead_nodes = BITMAP_ALLOC (NULL);
1309
1310   for (node = cgraph_nodes; node; node = node->next)
1311     if (node->analyzed)
1312       {
1313         if (node->count > max_count)
1314           max_count = node->count;
1315         overall_size += inline_summary (node)->self_size;
1316       }
1317
1318   max_new_size = overall_size;
1319   if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1320     max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1321   max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1322
1323   /* First collect all functions we proved to have constant arguments to
1324      heap.  */
1325   heap = fibheap_new ();
1326   for (node = cgraph_nodes; node; node = node->next)
1327     {
1328       struct ipa_node_params *info;
1329       /* Propagation of the constant is forbidden in certain conditions.  */
1330       if (!node->analyzed || !ipcp_node_modifiable_p (node))
1331           continue;
1332       info = IPA_NODE_REF (node);
1333       if (ipa_is_called_with_var_arguments (info))
1334         continue;
1335       if (ipcp_const_param_count (node))
1336         node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node),
1337                                     node);
1338      }
1339
1340   /* Now clone in priority order until code size growth limits are met or
1341      heap is emptied.  */
1342   while (!fibheap_empty (heap))
1343     {
1344       struct ipa_node_params *info;
1345       int growth = 0;
1346       bitmap args_to_skip;
1347       struct cgraph_edge *cs;
1348
1349       node = (struct cgraph_node *)fibheap_extract_min (heap);
1350       node->aux = NULL;
1351       if (dump_file)
1352         fprintf (dump_file, "considering function %s\n",
1353                  cgraph_node_name (node));
1354
1355       growth = ipcp_estimate_growth (node);
1356
1357       if (new_size + growth > max_new_size)
1358         break;
1359       if (growth
1360           && cgraph_optimize_for_size_p (node))
1361         {
1362           if (dump_file)
1363             fprintf (dump_file, "Not versioning, cold code would grow");
1364           continue;
1365         }
1366
1367       info = IPA_NODE_REF (node);
1368       count = ipa_get_param_count (info);
1369
1370       replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1371
1372       if (node->local.can_change_signature)
1373         args_to_skip = BITMAP_GGC_ALLOC ();
1374       else
1375         args_to_skip = NULL;
1376       for (i = 0; i < count; i++)
1377         {
1378           struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1379           parm_tree = ipa_get_param (info, i);
1380
1381           /* We can proactively remove obviously unused arguments.  */
1382           if (!ipa_is_param_used (info, i))
1383             {
1384               if (args_to_skip)
1385                 bitmap_set_bit (args_to_skip, i);
1386               continue;
1387             }
1388
1389           if (lat->type == IPA_CONST_VALUE)
1390             {
1391               replace_param =
1392                 ipcp_create_replace_map (parm_tree, lat);
1393               if (replace_param == NULL)
1394                 break;
1395               VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1396               if (args_to_skip)
1397                 bitmap_set_bit (args_to_skip, i);
1398             }
1399         }
1400       if (i < count)
1401         {
1402           if (dump_file)
1403             fprintf (dump_file, "Not versioning, some parameters couldn't be replaced");
1404           continue;
1405         }
1406
1407       new_size += growth;
1408
1409       /* Look if original function becomes dead after cloning.  */
1410       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1411         if (cs->caller == node || ipcp_need_redirect_p (cs))
1412           break;
1413       if (!cs && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1414         bitmap_set_bit (dead_nodes, node->uid);
1415
1416       /* Compute how many callers node has.  */
1417       node_callers = 0;
1418       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1419         node_callers++;
1420       redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1421       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1422         if (!cs->indirect_inlining_edge)
1423           VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1424
1425       /* Redirecting all the callers of the node to the
1426          new versioned node.  */
1427       node1 =
1428         cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1429                                      args_to_skip, "constprop");
1430       args_to_skip = NULL;
1431       VEC_free (cgraph_edge_p, heap, redirect_callers);
1432       replace_trees = NULL;
1433
1434       if (node1 == NULL)
1435         continue;
1436       ipcp_process_devirtualization_opportunities (node1);
1437
1438       if (dump_file)
1439         fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1440                  cgraph_node_name (node), (int)growth, (int)new_size);
1441       ipcp_init_cloned_node (node, node1);
1442
1443       info = IPA_NODE_REF (node);
1444       for (i = 0; i < count; i++)
1445         {
1446           struct ipcp_lattice *lat = ipa_get_lattice (info, i);
1447           if (lat->type == IPA_CONST_VALUE)
1448             ipcp_discover_new_direct_edges (node1, i, lat->constant);
1449         }
1450
1451       if (dump_file)
1452         dump_function_to_file (node1->decl, dump_file, dump_flags);
1453
1454       for (cs = node->callees; cs; cs = cs->next_callee)
1455         if (cs->callee->aux)
1456           {
1457             fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1458             cs->callee->aux = fibheap_insert (heap,
1459                                               ipcp_estimate_cloning_cost (cs->callee),
1460                                               cs->callee);
1461           }
1462     }
1463
1464   while (!fibheap_empty (heap))
1465     {
1466       if (dump_file)
1467         fprintf (dump_file, "skipping function %s\n",
1468                  cgraph_node_name (node));
1469       node = (struct cgraph_node *) fibheap_extract_min (heap);
1470       node->aux = NULL;
1471     }
1472   fibheap_delete (heap);
1473   BITMAP_FREE (dead_nodes);
1474   ipcp_update_callgraph ();
1475   ipcp_update_profiling ();
1476 }
1477
1478 /* The IPCP driver.  */
1479 static unsigned int
1480 ipcp_driver (void)
1481 {
1482   cgraph_remove_unreachable_nodes (true,dump_file);
1483   if (dump_file)
1484     {
1485       fprintf (dump_file, "\nIPA structures before propagation:\n");
1486       if (dump_flags & TDF_DETAILS)
1487         ipa_print_all_params (dump_file);
1488       ipa_print_all_jump_functions (dump_file);
1489     }
1490   ipa_check_create_node_params ();
1491   ipa_check_create_edge_args ();
1492   /* 2. Do the interprocedural propagation.  */
1493   ipcp_iterate_stage ();
1494   /* 3. Insert the constants found to the functions.  */
1495   ipcp_insert_stage ();
1496   if (dump_file && (dump_flags & TDF_DETAILS))
1497     {
1498       fprintf (dump_file, "\nProfiling info after insert stage:\n");
1499       ipcp_print_profile_data (dump_file);
1500     }
1501   /* Free all IPCP structures.  */
1502   ipa_free_all_structures_after_ipa_cp ();
1503   if (dump_file)
1504     fprintf (dump_file, "\nIPA constant propagation end\n");
1505   return 0;
1506 }
1507
1508 /* Initialization and computation of IPCP data structures.  This is the initial
1509    intraprocedural analysis of functions, which gathers information to be
1510    propagated later on.  */
1511
1512 static void
1513 ipcp_generate_summary (void)
1514 {
1515   struct cgraph_node *node;
1516
1517   if (dump_file)
1518     fprintf (dump_file, "\nIPA constant propagation start:\n");
1519   ipa_register_cgraph_hooks ();
1520
1521   /* FIXME: We could propagate through thunks happily and we could be
1522      even able to clone them, if needed.  Do that later.  */
1523   FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1524       {
1525         /* Unreachable nodes should have been eliminated before ipcp.  */
1526         gcc_assert (node->needed || node->reachable);
1527
1528         inline_summary (node)->versionable = tree_versionable_function_p (node->decl);
1529         ipa_analyze_node (node);
1530       }
1531 }
1532
1533 /* Write ipcp summary for nodes in SET.  */
1534 static void
1535 ipcp_write_summary (cgraph_node_set set,
1536                     varpool_node_set vset ATTRIBUTE_UNUSED)
1537 {
1538   ipa_prop_write_jump_functions (set);
1539 }
1540
1541 /* Read ipcp summary.  */
1542 static void
1543 ipcp_read_summary (void)
1544 {
1545   ipa_prop_read_jump_functions ();
1546 }
1547
1548 /* Gate for IPCP optimization.  */
1549 static bool
1550 cgraph_gate_cp (void)
1551 {
1552   /* FIXME: We should remove the optimize check after we ensure we never run
1553      IPA passes when not optimizing.  */
1554   return flag_ipa_cp && optimize;
1555 }
1556
1557 struct ipa_opt_pass_d pass_ipa_cp =
1558 {
1559  {
1560   IPA_PASS,
1561   "cp",                         /* name */
1562   cgraph_gate_cp,               /* gate */
1563   ipcp_driver,                  /* execute */
1564   NULL,                         /* sub */
1565   NULL,                         /* next */
1566   0,                            /* static_pass_number */
1567   TV_IPA_CONSTANT_PROP,         /* tv_id */
1568   0,                            /* properties_required */
1569   0,                            /* properties_provided */
1570   0,                            /* properties_destroyed */
1571   0,                            /* todo_flags_start */
1572   TODO_dump_cgraph | TODO_dump_func |
1573   TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
1574  },
1575  ipcp_generate_summary,                 /* generate_summary */
1576  ipcp_write_summary,                    /* write_summary */
1577  ipcp_read_summary,                     /* read_summary */
1578  NULL,                                  /* write_optimization_summary */
1579  NULL,                                  /* read_optimization_summary */
1580  NULL,                                  /* stmt_fixup */
1581  0,                                     /* TODOs */
1582  NULL,                                  /* function_transform */
1583  NULL,                                  /* variable_transform */
1584 };