OSDN Git Service

2010-12-09 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-cp.c
1 /* Interprocedural constant propagation
2    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
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    Challahan David, 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
152 /* Number of functions identified as candidates for cloning. When not cloning
153    we can simplify iterate stage not forcing it to go through the decision
154    on what is profitable and what not.  */
155 static int n_cloning_candidates;
156
157 /* Maximal count found in program.  */
158 static gcov_type max_count;
159
160 /* Cgraph nodes that has been completely replaced by cloning during iterate
161  * stage and will be removed after ipcp is finished.  */
162 static bitmap dead_nodes;
163
164 static void ipcp_print_profile_data (FILE *);
165 static void ipcp_function_scale_print (FILE *);
166
167 /* Get the original node field of ipa_node_params associated with node NODE.  */
168 static inline struct cgraph_node *
169 ipcp_get_orig_node (struct cgraph_node *node)
170 {
171   return IPA_NODE_REF (node)->ipcp_orig_node;
172 }
173
174 /* Return true if NODE describes a cloned/versioned function.  */
175 static inline bool
176 ipcp_node_is_clone (struct cgraph_node *node)
177 {
178   return (ipcp_get_orig_node (node) != NULL);
179 }
180
181 /* Create ipa_node_params and its data structures for NEW_NODE.  Set ORIG_NODE
182    as the ipcp_orig_node field in ipa_node_params.  */
183 static void
184 ipcp_init_cloned_node (struct cgraph_node *orig_node,
185                        struct cgraph_node *new_node)
186 {
187   gcc_checking_assert (ipa_node_params_vector
188                        && (VEC_length (ipa_node_params_t,
189                                        ipa_node_params_vector)
190                            > (unsigned) cgraph_max_uid));
191   gcc_checking_assert (IPA_NODE_REF (new_node)->params);
192   IPA_NODE_REF (new_node)->ipcp_orig_node = orig_node;
193 }
194
195 /* Return scale for NODE.  */
196 static inline gcov_type
197 ipcp_get_node_scale (struct cgraph_node *node)
198 {
199   return IPA_NODE_REF (node)->count_scale;
200 }
201
202 /* Set COUNT as scale for NODE.  */
203 static inline void
204 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
205 {
206   IPA_NODE_REF (node)->count_scale = count;
207 }
208
209 /* Return whether LAT is a constant lattice.  */
210 static inline bool
211 ipcp_lat_is_const (struct ipcp_lattice *lat)
212 {
213   if (lat->type == IPA_CONST_VALUE)
214     return true;
215   else
216     return false;
217 }
218
219 /* Return whether LAT is a constant lattice that ipa-cp can actually insert
220    into the code (i.e. constants excluding member pointers and pointers).  */
221 static inline bool
222 ipcp_lat_is_insertable (struct ipcp_lattice *lat)
223 {
224   return lat->type == IPA_CONST_VALUE;
225 }
226
227 /* Return true if LAT1 and LAT2 are equal.  */
228 static inline bool
229 ipcp_lats_are_equal (struct ipcp_lattice *lat1, struct ipcp_lattice *lat2)
230 {
231   gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
232   if (lat1->type != lat2->type)
233     return false;
234
235   if (TREE_CODE (lat1->constant) ==  ADDR_EXPR
236       && TREE_CODE (lat2->constant) ==  ADDR_EXPR
237       && TREE_CODE (TREE_OPERAND (lat1->constant, 0)) == CONST_DECL
238       && TREE_CODE (TREE_OPERAND (lat2->constant, 0)) == CONST_DECL)
239     return operand_equal_p (DECL_INITIAL (TREE_OPERAND (lat1->constant, 0)),
240                             DECL_INITIAL (TREE_OPERAND (lat2->constant, 0)), 0);
241   else
242     return operand_equal_p (lat1->constant, lat2->constant, 0);
243 }
244
245 /* Compute Meet arithmetics:
246    Meet (IPA_BOTTOM, x) = IPA_BOTTOM
247    Meet (IPA_TOP,x) = x
248    Meet (const_a,const_b) = IPA_BOTTOM,  if const_a != const_b.
249    MEET (const_a,const_b) = const_a, if const_a == const_b.*/
250 static void
251 ipa_lattice_meet (struct ipcp_lattice *res, struct ipcp_lattice *lat1,
252                   struct ipcp_lattice *lat2)
253 {
254   if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
255     {
256       res->type = IPA_BOTTOM;
257       return;
258     }
259   if (lat1->type == IPA_TOP)
260     {
261       res->type = lat2->type;
262       res->constant = lat2->constant;
263       return;
264     }
265   if (lat2->type == IPA_TOP)
266     {
267       res->type = lat1->type;
268       res->constant = lat1->constant;
269       return;
270     }
271   if (!ipcp_lats_are_equal (lat1, lat2))
272     {
273       res->type = IPA_BOTTOM;
274       return;
275     }
276   res->type = lat1->type;
277   res->constant = lat1->constant;
278 }
279
280 /* Return the lattice corresponding to the Ith formal parameter of the function
281    described by INFO.  */
282 static inline struct ipcp_lattice *
283 ipcp_get_lattice (struct ipa_node_params *info, int i)
284 {
285   return &(info->params[i].ipcp_lattice);
286 }
287
288 /* Given the jump function JFUNC, compute the lattice LAT that describes the
289    value coming down the callsite. INFO describes the caller node so that
290    pass-through jump functions can be evaluated.  */
291 static void
292 ipcp_lattice_from_jfunc (struct ipa_node_params *info, struct ipcp_lattice *lat,
293                          struct ipa_jump_func *jfunc)
294 {
295   if (jfunc->type == IPA_JF_CONST)
296     {
297       lat->type = IPA_CONST_VALUE;
298       lat->constant = jfunc->value.constant;
299     }
300   else if (jfunc->type == IPA_JF_PASS_THROUGH)
301     {
302       struct ipcp_lattice *caller_lat;
303       tree cst;
304
305       caller_lat = ipcp_get_lattice (info, jfunc->value.pass_through.formal_id);
306       lat->type = caller_lat->type;
307       if (caller_lat->type != IPA_CONST_VALUE)
308         return;
309       cst = caller_lat->constant;
310
311       if (jfunc->value.pass_through.operation != NOP_EXPR)
312         {
313           tree restype;
314           if (TREE_CODE_CLASS (jfunc->value.pass_through.operation)
315               == tcc_comparison)
316             restype = boolean_type_node;
317           else
318             restype = TREE_TYPE (cst);
319           cst = fold_binary (jfunc->value.pass_through.operation,
320                              restype, cst, jfunc->value.pass_through.operand);
321         }
322       if (!cst || !is_gimple_ip_invariant (cst))
323         lat->type = IPA_BOTTOM;
324       lat->constant = cst;
325     }
326   else if (jfunc->type == IPA_JF_ANCESTOR)
327     {
328       struct ipcp_lattice *caller_lat;
329       tree t;
330
331       caller_lat = ipcp_get_lattice (info, jfunc->value.ancestor.formal_id);
332       lat->type = caller_lat->type;
333       if (caller_lat->type != IPA_CONST_VALUE)
334         return;
335       if (TREE_CODE (caller_lat->constant) != ADDR_EXPR)
336         {
337           /* This can happen when the constant is a NULL pointer.  */
338           lat->type = IPA_BOTTOM;
339           return;
340         }
341       t = TREE_OPERAND (caller_lat->constant, 0);
342       t = build_ref_for_offset (EXPR_LOCATION (t), t,
343                                 jfunc->value.ancestor.offset,
344                                 jfunc->value.ancestor.type, NULL, false);
345       lat->constant = build_fold_addr_expr (t);
346     }
347   else
348     lat->type = IPA_BOTTOM;
349 }
350
351 /* True when OLD_LAT and NEW_LAT values are not the same.  */
352
353 static bool
354 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
355                       struct ipcp_lattice *new_lat)
356 {
357   if (old_lat->type == new_lat->type)
358     {
359       if (!ipcp_lat_is_const (old_lat))
360         return false;
361       if (ipcp_lats_are_equal (old_lat, new_lat))
362         return false;
363     }
364   return true;
365 }
366
367 /* Print all ipcp_lattices of all functions to F.  */
368 static void
369 ipcp_print_all_lattices (FILE * f)
370 {
371   struct cgraph_node *node;
372   int i, count;
373
374   fprintf (f, "\nLattice:\n");
375   for (node = cgraph_nodes; node; node = node->next)
376     {
377       struct ipa_node_params *info;
378
379       if (!node->analyzed)
380         continue;
381       info = IPA_NODE_REF (node);
382       fprintf (f, "  Node: %s:\n", cgraph_node_name (node));
383       count = ipa_get_param_count (info);
384       for (i = 0; i < count; i++)
385         {
386           struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
387
388           fprintf (f, "    param [%d]: ", i);
389           if (lat->type == IPA_CONST_VALUE)
390             {
391               tree cst = lat->constant;
392               fprintf (f, "type is CONST ");
393               print_generic_expr (f, cst, 0);
394               if (TREE_CODE (cst) == ADDR_EXPR
395                   && TREE_CODE (TREE_OPERAND (cst, 0)) == CONST_DECL)
396                 {
397                   fprintf (f, " -> ");
398                   print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (cst, 0)),
399                                                        0);
400                 }
401             }
402           else if (lat->type == IPA_TOP)
403             fprintf (f, "type is TOP");
404           else
405             fprintf (f, "type is BOTTOM");
406           if (ipa_param_cannot_devirtualize_p (info, i))
407             fprintf (f, " - cannot_devirtualize set\n");
408           else if (ipa_param_types_vec_empty (info, i))
409             fprintf (f, " - type list empty\n");
410           else
411             fprintf (f, "\n");
412         }
413     }
414 }
415
416 /* Return true if ipcp algorithms would allow cloning NODE.  */
417
418 static bool
419 ipcp_versionable_function_p (struct cgraph_node *node)
420 {
421   struct cgraph_edge *edge;
422
423   /* There are a number of generic reasons functions cannot be versioned.  We
424      also cannot remove parameters if there are type attributes such as fnspec
425      present.  */
426   if (!node->local.versionable
427       || TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
428     return false;
429
430   /* Removing arguments doesn't work if the function takes varargs
431      or use __builtin_apply_args. */
432   for (edge = node->callees; edge; edge = edge->next_callee)
433     {
434       tree t = edge->callee->decl;
435       if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
436           && (DECL_FUNCTION_CODE (t) == BUILT_IN_APPLY_ARGS
437              || DECL_FUNCTION_CODE (t) == BUILT_IN_VA_START))
438         return false;
439     }
440
441   return true;
442 }
443
444 /* Return true if this NODE is viable candidate for cloning.  */
445 static bool
446 ipcp_cloning_candidate_p (struct cgraph_node *node)
447 {
448   int n_calls = 0;
449   int n_hot_calls = 0;
450   gcov_type direct_call_sum = 0;
451   struct cgraph_edge *e;
452
453   /* We never clone functions that are not visible from outside.
454      FIXME: in future we should clone such functions when they are called with
455      different constants, but current ipcp implementation is not good on this.
456      */
457   if (cgraph_only_called_directly_p (node) || !node->analyzed)
458     return false;
459
460   /* When function address is taken, we are pretty sure it will be called in hidden way.  */
461   if (node->address_taken)
462     {
463       if (dump_file)
464         fprintf (dump_file, "Not considering %s for cloning; address is taken.\n",
465                  cgraph_node_name (node));
466       return false;
467     }
468
469   if (cgraph_function_body_availability (node) <= AVAIL_OVERWRITABLE)
470     {
471       if (dump_file)
472         fprintf (dump_file, "Not considering %s for cloning; body is overwrittable.\n",
473                  cgraph_node_name (node));
474       return false;
475     }
476   if (!ipcp_versionable_function_p (node))
477     {
478       if (dump_file)
479         fprintf (dump_file, "Not considering %s for cloning; body is not versionable.\n",
480                  cgraph_node_name (node));
481       return false;
482     }
483   for (e = node->callers; e; e = e->next_caller)
484     {
485       direct_call_sum += e->count;
486       n_calls ++;
487       if (cgraph_maybe_hot_edge_p (e))
488         n_hot_calls ++;
489     }
490
491   if (!n_calls)
492     {
493       if (dump_file)
494         fprintf (dump_file, "Not considering %s for cloning; no direct calls.\n",
495                  cgraph_node_name (node));
496       return false;
497     }
498   if (node->local.inline_summary.self_size < n_calls)
499     {
500       if (dump_file)
501         fprintf (dump_file, "Considering %s for cloning; code would shrink.\n",
502                  cgraph_node_name (node));
503       return true;
504     }
505
506   if (!flag_ipa_cp_clone)
507     {
508       if (dump_file)
509         fprintf (dump_file, "Not considering %s for cloning; -fipa-cp-clone disabled.\n",
510                  cgraph_node_name (node));
511       return false;
512     }
513
514   if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node->decl)))
515     {
516       if (dump_file)
517         fprintf (dump_file, "Not considering %s for cloning; optimizing it for size.\n",
518                  cgraph_node_name (node));
519       return false;
520     }
521
522   /* When profile is available and function is hot, propagate into it even if
523      calls seems cold; constant propagation can improve function's speed
524      significandly.  */
525   if (max_count)
526     {
527       if (direct_call_sum > node->count * 90 / 100)
528         {
529           if (dump_file)
530             fprintf (dump_file, "Considering %s for cloning; usually called directly.\n",
531                      cgraph_node_name (node));
532           return true;
533         }
534     }
535   if (!n_hot_calls)
536     {
537       if (dump_file)
538         fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
539                  cgraph_node_name (node));
540       return false;
541     }
542   if (dump_file)
543     fprintf (dump_file, "Considering %s for cloning.\n",
544              cgraph_node_name (node));
545   return true;
546 }
547
548 /* Mark parameter with index I of function described by INFO as unsuitable for
549    devirtualization.  Return true if it has already been marked so.  */
550
551 static bool
552 ipa_set_param_cannot_devirtualize (struct ipa_node_params *info, int i)
553 {
554   bool ret = info->params[i].cannot_devirtualize;
555   info->params[i].cannot_devirtualize = true;
556   if (info->params[i].types)
557     VEC_free (tree, heap, info->params[i].types);
558   return ret;
559 }
560
561 /* Initialize ipcp_lattices array.  The lattices corresponding to supported
562    types (integers, real types and Fortran constants defined as const_decls)
563    are initialized to IPA_TOP, the rest of them to IPA_BOTTOM.  */
564 static void
565 ipcp_initialize_node_lattices (struct cgraph_node *node)
566 {
567   int i;
568   struct ipa_node_params *info = IPA_NODE_REF (node);
569   enum ipa_lattice_type type;
570
571   if (ipa_is_called_with_var_arguments (info))
572     type = IPA_BOTTOM;
573   else if (node->local.local)
574     type = IPA_TOP;
575   /* When cloning is allowed, we can assume that externally visible functions
576      are not called.  We will compensate this by cloning later.  */
577   else if (ipcp_cloning_candidate_p (node))
578     type = IPA_TOP, n_cloning_candidates ++;
579   else
580     type = IPA_BOTTOM;
581
582   for (i = 0; i < ipa_get_param_count (info) ; i++)
583     {
584       ipcp_get_lattice (info, i)->type = type;
585       if (type == IPA_BOTTOM)
586         ipa_set_param_cannot_devirtualize (info, i);
587     }
588 }
589
590 /* build INTEGER_CST tree with type TREE_TYPE and value according to LAT.
591    Return the tree.  */
592 static tree
593 build_const_val (struct ipcp_lattice *lat, tree tree_type)
594 {
595   tree val;
596
597   gcc_assert (ipcp_lat_is_const (lat));
598   val = lat->constant;
599
600   if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
601     {
602       if (fold_convertible_p (tree_type, val))
603         return fold_build1 (NOP_EXPR, tree_type, val);
604       else
605         return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
606     }
607   return val;
608 }
609
610 /* Compute the proper scale for NODE.  It is the ratio between the number of
611    direct calls (represented on the incoming cgraph_edges) and sum of all
612    invocations of NODE (represented as count in cgraph_node).
613
614    FIXME: This code is wrong.  Since the callers can be also clones and
615    the clones are not scaled yet, the sums gets unrealistically high.
616    To properly compute the counts, we would need to do propagation across
617    callgraph (as external call to A might imply call to non-clonned B
618    if A's clone calls clonned B).  */
619 static void
620 ipcp_compute_node_scale (struct cgraph_node *node)
621 {
622   gcov_type sum;
623   struct cgraph_edge *cs;
624
625   sum = 0;
626   /* Compute sum of all counts of callers. */
627   for (cs = node->callers; cs != NULL; cs = cs->next_caller)
628     sum += cs->count;
629   /* Work around the unrealistically high sum problem.  We just don't want
630      the non-cloned body to have negative or very low frequency.  Since
631      majority of execution time will be spent in clones anyway, this should
632      give good enough profile.  */
633   if (sum > node->count * 9 / 10)
634     sum = node->count * 9 / 10;
635   if (node->count == 0)
636     ipcp_set_node_scale (node, 0);
637   else
638     ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
639 }
640
641 /* Return true if there are some formal parameters whose value is IPA_TOP (in
642    the whole compilation unit).  Change their values to IPA_BOTTOM, since they
643    most probably get their values from outside of this compilation unit.  */
644 static bool
645 ipcp_change_tops_to_bottom (void)
646 {
647   int i, count;
648   struct cgraph_node *node;
649   bool prop_again;
650
651   prop_again = false;
652   for (node = cgraph_nodes; node; node = node->next)
653     {
654       struct ipa_node_params *info = IPA_NODE_REF (node);
655       count = ipa_get_param_count (info);
656       for (i = 0; i < count; i++)
657         {
658           struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
659           if (lat->type == IPA_TOP)
660             {
661               prop_again = true;
662               if (dump_file)
663                 {
664                   fprintf (dump_file, "Forcing param ");
665                   print_generic_expr (dump_file, ipa_get_param (info, i), 0);
666                   fprintf (dump_file, " of node %s to bottom.\n",
667                            cgraph_node_name (node));
668                 }
669               lat->type = IPA_BOTTOM;
670             }
671           if (!ipa_param_cannot_devirtualize_p (info, i)
672               && ipa_param_types_vec_empty (info, i))
673             {
674               prop_again = true;
675               ipa_set_param_cannot_devirtualize (info, i);
676               if (dump_file)
677                 {
678                   fprintf (dump_file, "Marking param ");
679                   print_generic_expr (dump_file, ipa_get_param (info, i), 0);
680                   fprintf (dump_file, " of node %s as unusable for "
681                            "devirtualization.\n",
682                            cgraph_node_name (node));
683                 }
684             }
685         }
686     }
687   return prop_again;
688 }
689
690 /* Insert BINFO to the list of known types of parameter number I of the
691    function described by CALLEE_INFO.  Return true iff the type information
692    associated with the callee parameter changed in any way.  */
693
694 static bool
695 ipcp_add_param_type (struct ipa_node_params *callee_info, int i, tree binfo)
696 {
697   int j, count;
698
699   if (ipa_param_cannot_devirtualize_p (callee_info, i))
700     return false;
701
702   if (callee_info->params[i].types)
703     {
704       count = VEC_length (tree, callee_info->params[i].types);
705       for (j = 0; j < count; j++)
706         if (VEC_index (tree, callee_info->params[i].types, j) == binfo)
707           return false;
708     }
709
710   if (VEC_length (tree, callee_info->params[i].types)
711       == (unsigned) PARAM_VALUE (PARAM_DEVIRT_TYPE_LIST_SIZE))
712     return !ipa_set_param_cannot_devirtualize (callee_info, i);
713
714   VEC_safe_push (tree, heap, callee_info->params[i].types, binfo);
715   return true;
716 }
717
718 /* Copy known types information for parameter number CALLEE_IDX of CALLEE_INFO
719    from a parameter of CALLER_INFO as described by JF.  Return true iff the
720    type information changed in any way.  JF must be a pass-through or an
721    ancestor jump function.  */
722
723 static bool
724 ipcp_copy_types (struct ipa_node_params *caller_info,
725                  struct ipa_node_params *callee_info,
726                  int callee_idx, struct ipa_jump_func *jf)
727 {
728   int caller_idx, j, count;
729   bool res;
730
731   if (ipa_param_cannot_devirtualize_p (callee_info, callee_idx))
732     return false;
733
734   if (jf->type == IPA_JF_PASS_THROUGH)
735     {
736       if (jf->value.pass_through.operation != NOP_EXPR)
737         {
738           ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
739           return true;
740         }
741       caller_idx = jf->value.pass_through.formal_id;
742     }
743   else
744     caller_idx = jf->value.ancestor.formal_id;
745
746   if (ipa_param_cannot_devirtualize_p (caller_info, caller_idx))
747     {
748       ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
749       return true;
750     }
751
752   if (!caller_info->params[caller_idx].types)
753     return false;
754
755   res = false;
756   count = VEC_length (tree, caller_info->params[caller_idx].types);
757   for (j = 0; j < count; j++)
758     {
759       tree binfo = VEC_index (tree, caller_info->params[caller_idx].types, j);
760       if (jf->type == IPA_JF_ANCESTOR)
761         {
762           binfo = get_binfo_at_offset (binfo, jf->value.ancestor.offset,
763                                        jf->value.ancestor.type);
764           if (!binfo)
765             {
766               ipa_set_param_cannot_devirtualize (callee_info, callee_idx);
767               return true;
768             }
769         }
770       res |= ipcp_add_param_type (callee_info, callee_idx, binfo);
771     }
772   return res;
773 }
774
775 /* Propagate type information for parameter of CALLEE_INFO number I as
776    described by JF.  CALLER_INFO describes the caller.  Return true iff the
777    type information changed in any way.  */
778
779 static bool
780 ipcp_propagate_types (struct ipa_node_params *caller_info,
781                       struct ipa_node_params *callee_info,
782                       struct ipa_jump_func *jf, int i)
783 {
784   tree cst, binfo;
785
786   switch (jf->type)
787     {
788     case IPA_JF_UNKNOWN:
789     case IPA_JF_CONST_MEMBER_PTR:
790       break;
791
792     case IPA_JF_KNOWN_TYPE:
793       return ipcp_add_param_type (callee_info, i, jf->value.base_binfo);
794
795     case IPA_JF_CONST:
796       cst = jf->value.constant;
797       if (TREE_CODE (cst) != ADDR_EXPR)
798         break;
799       binfo = gimple_get_relevant_ref_binfo (TREE_OPERAND (cst, 0), NULL_TREE);
800       if (!binfo)
801         break;
802       return ipcp_add_param_type (callee_info, i, binfo);
803
804     case IPA_JF_PASS_THROUGH:
805     case IPA_JF_ANCESTOR:
806       return ipcp_copy_types (caller_info, callee_info, i, jf);
807     }
808
809   /* If we reach this we cannot use this parameter for devirtualization.  */
810   return !ipa_set_param_cannot_devirtualize (callee_info, i);
811 }
812
813 /* Interprocedural analysis. The algorithm propagates constants from the
814    caller's parameters to the callee's arguments.  */
815 static void
816 ipcp_propagate_stage (void)
817 {
818   int i;
819   struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
820   struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
821   struct ipcp_lattice *dest_lat;
822   struct cgraph_edge *cs;
823   struct ipa_jump_func *jump_func;
824   struct ipa_func_list *wl;
825   int count;
826
827   ipa_check_create_node_params ();
828   ipa_check_create_edge_args ();
829
830   /* Initialize worklist to contain all functions.  */
831   wl = ipa_init_func_list ();
832   while (wl)
833     {
834       struct cgraph_node *node = ipa_pop_func_from_list (&wl);
835       struct ipa_node_params *info = IPA_NODE_REF (node);
836
837       for (cs = node->callees; cs; cs = cs->next_callee)
838         {
839           struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
840           struct ipa_edge_args *args = IPA_EDGE_REF (cs);
841
842           if (ipa_is_called_with_var_arguments (callee_info)
843               || !cs->callee->analyzed
844               || ipa_is_called_with_var_arguments (callee_info))
845             continue;
846
847           count = ipa_get_cs_argument_count (args);
848           for (i = 0; i < count; i++)
849             {
850               jump_func = ipa_get_ith_jump_func (args, i);
851               ipcp_lattice_from_jfunc (info, &inc_lat, jump_func);
852               dest_lat = ipcp_get_lattice (callee_info, i);
853               ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
854               if (ipcp_lattice_changed (&new_lat, dest_lat))
855                 {
856                   dest_lat->type = new_lat.type;
857                   dest_lat->constant = new_lat.constant;
858                   ipa_push_func_to_list (&wl, cs->callee);
859                 }
860
861               if (ipcp_propagate_types (info, callee_info, jump_func, i))
862                 ipa_push_func_to_list (&wl, cs->callee);
863             }
864         }
865     }
866 }
867
868 /* Call the constant propagation algorithm and re-call it if necessary
869    (if there are undetermined values left).  */
870 static void
871 ipcp_iterate_stage (void)
872 {
873   struct cgraph_node *node;
874   n_cloning_candidates = 0;
875
876   if (dump_file)
877     fprintf (dump_file, "\nIPA iterate stage:\n\n");
878
879   if (in_lto_p)
880     ipa_update_after_lto_read ();
881
882   for (node = cgraph_nodes; node; node = node->next)
883     {
884       ipcp_initialize_node_lattices (node);
885       ipcp_compute_node_scale (node);
886     }
887   if (dump_file && (dump_flags & TDF_DETAILS))
888     {
889       ipcp_print_all_lattices (dump_file);
890       ipcp_function_scale_print (dump_file);
891     }
892
893   ipcp_propagate_stage ();
894   if (ipcp_change_tops_to_bottom ())
895     /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
896        This change should be propagated.  */
897     {
898       gcc_assert (n_cloning_candidates);
899       ipcp_propagate_stage ();
900     }
901   if (dump_file)
902     {
903       fprintf (dump_file, "\nIPA lattices after propagation:\n");
904       ipcp_print_all_lattices (dump_file);
905       if (dump_flags & TDF_DETAILS)
906         ipcp_print_profile_data (dump_file);
907     }
908 }
909
910 /* Check conditions to forbid constant insertion to function described by
911    NODE.  */
912 static inline bool
913 ipcp_node_modifiable_p (struct cgraph_node *node)
914 {
915   /* Once we will be able to do in-place replacement, we can be more
916      lax here.  */
917   return ipcp_versionable_function_p (node);
918 }
919
920 /* Print count scale data structures.  */
921 static void
922 ipcp_function_scale_print (FILE * f)
923 {
924   struct cgraph_node *node;
925
926   for (node = cgraph_nodes; node; node = node->next)
927     {
928       if (!node->analyzed)
929         continue;
930       fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
931       fprintf (f, "value is  " HOST_WIDE_INT_PRINT_DEC
932                "  \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
933     }
934 }
935
936 /* Print counts of all cgraph nodes.  */
937 static void
938 ipcp_print_func_profile_counts (FILE * f)
939 {
940   struct cgraph_node *node;
941
942   for (node = cgraph_nodes; node; node = node->next)
943     {
944       fprintf (f, "function %s: ", cgraph_node_name (node));
945       fprintf (f, "count is  " HOST_WIDE_INT_PRINT_DEC
946                "  \n", (HOST_WIDE_INT) node->count);
947     }
948 }
949
950 /* Print counts of all cgraph edges.  */
951 static void
952 ipcp_print_call_profile_counts (FILE * f)
953 {
954   struct cgraph_node *node;
955   struct cgraph_edge *cs;
956
957   for (node = cgraph_nodes; node; node = node->next)
958     {
959       for (cs = node->callees; cs; cs = cs->next_callee)
960         {
961           fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
962                    cgraph_node_name (cs->callee));
963           fprintf (f, "count is  " HOST_WIDE_INT_PRINT_DEC "  \n",
964                    (HOST_WIDE_INT) cs->count);
965         }
966     }
967 }
968
969 /* Print profile info for all functions.  */
970 static void
971 ipcp_print_profile_data (FILE * f)
972 {
973   fprintf (f, "\nNODE COUNTS :\n");
974   ipcp_print_func_profile_counts (f);
975   fprintf (f, "\nCS COUNTS stage:\n");
976   ipcp_print_call_profile_counts (f);
977 }
978
979 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
980    processed by versioning, which operates according to the flags set.
981    PARM_TREE is the formal parameter found to be constant.  LAT represents the
982    constant.  */
983 static struct ipa_replace_map *
984 ipcp_create_replace_map (tree parm_tree, struct ipcp_lattice *lat)
985 {
986   struct ipa_replace_map *replace_map;
987   tree const_val;
988
989   replace_map = ggc_alloc_ipa_replace_map ();
990   const_val = build_const_val (lat, TREE_TYPE (parm_tree));
991   if (dump_file)
992     {
993       fprintf (dump_file, "  replacing param ");
994       print_generic_expr (dump_file, parm_tree, 0);
995       fprintf (dump_file, " with const ");
996       print_generic_expr (dump_file, const_val, 0);
997       fprintf (dump_file, "\n");
998     }
999   replace_map->old_tree = parm_tree;
1000   replace_map->new_tree = const_val;
1001   replace_map->replace_p = true;
1002   replace_map->ref_p = false;
1003
1004   return replace_map;
1005 }
1006
1007 /* Return true if this callsite should be redirected to the original callee
1008    (instead of the cloned one).  */
1009 static bool
1010 ipcp_need_redirect_p (struct cgraph_edge *cs)
1011 {
1012   struct ipa_node_params *orig_callee_info;
1013   int i, count;
1014   struct cgraph_node *node = cs->callee, *orig;
1015
1016   if (!n_cloning_candidates)
1017     return false;
1018
1019   if ((orig = ipcp_get_orig_node (node)) != NULL)
1020     node = orig;
1021   if (ipcp_get_orig_node (cs->caller))
1022     return false;
1023
1024   orig_callee_info = IPA_NODE_REF (node);
1025   count = ipa_get_param_count (orig_callee_info);
1026   for (i = 0; i < count; i++)
1027     {
1028       struct ipcp_lattice *lat = ipcp_get_lattice (orig_callee_info, i);
1029       struct ipa_jump_func *jump_func;
1030
1031       jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
1032       if ((ipcp_lat_is_const (lat)
1033            && jump_func->type != IPA_JF_CONST)
1034           || (!ipa_param_cannot_devirtualize_p (orig_callee_info, i)
1035               && !ipa_param_types_vec_empty (orig_callee_info, i)
1036               && jump_func->type != IPA_JF_CONST
1037               && jump_func->type != IPA_JF_KNOWN_TYPE))
1038         return true;
1039     }
1040
1041   return false;
1042 }
1043
1044 /* Fix the callsites and the call graph after function cloning was done.  */
1045 static void
1046 ipcp_update_callgraph (void)
1047 {
1048   struct cgraph_node *node;
1049
1050   for (node = cgraph_nodes; node; node = node->next)
1051     if (node->analyzed && ipcp_node_is_clone (node))
1052       {
1053         bitmap args_to_skip = BITMAP_ALLOC (NULL);
1054         struct cgraph_node *orig_node = ipcp_get_orig_node (node);
1055         struct ipa_node_params *info = IPA_NODE_REF (orig_node);
1056         int i, count = ipa_get_param_count (info);
1057         struct cgraph_edge *cs, *next;
1058
1059         for (i = 0; i < count; i++)
1060           {
1061             struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1062
1063             /* We can proactively remove obviously unused arguments.  */
1064             if (!ipa_is_param_used (info, i))
1065               {
1066                 bitmap_set_bit (args_to_skip, i);
1067                 continue;
1068               }
1069
1070             if (lat->type == IPA_CONST_VALUE)
1071               bitmap_set_bit (args_to_skip, i);
1072           }
1073         for (cs = node->callers; cs; cs = next)
1074           {
1075             next = cs->next_caller;
1076             if (!ipcp_node_is_clone (cs->caller) && ipcp_need_redirect_p (cs))
1077               {
1078                 if (dump_file)
1079                   fprintf (dump_file, "Redirecting edge %s/%i -> %s/%i "
1080                            "back to %s/%i.",
1081                            cgraph_node_name (cs->caller), cs->caller->uid,
1082                            cgraph_node_name (cs->callee), cs->callee->uid,
1083                            cgraph_node_name (orig_node), orig_node->uid);
1084                 cgraph_redirect_edge_callee (cs, orig_node);
1085               }
1086           }
1087       }
1088 }
1089
1090 /* Update profiling info for versioned functions and the functions they were
1091    versioned from.  */
1092 static void
1093 ipcp_update_profiling (void)
1094 {
1095   struct cgraph_node *node, *orig_node;
1096   gcov_type scale, scale_complement;
1097   struct cgraph_edge *cs;
1098
1099   for (node = cgraph_nodes; node; node = node->next)
1100     {
1101       if (ipcp_node_is_clone (node))
1102         {
1103           orig_node = ipcp_get_orig_node (node);
1104           scale = ipcp_get_node_scale (orig_node);
1105           node->count = orig_node->count * scale / REG_BR_PROB_BASE;
1106           scale_complement = REG_BR_PROB_BASE - scale;
1107           orig_node->count =
1108             orig_node->count * scale_complement / REG_BR_PROB_BASE;
1109           for (cs = node->callees; cs; cs = cs->next_callee)
1110             cs->count = cs->count * scale / REG_BR_PROB_BASE;
1111           for (cs = orig_node->callees; cs; cs = cs->next_callee)
1112             cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
1113         }
1114     }
1115 }
1116
1117 /* If NODE was cloned, how much would program grow? */
1118 static long
1119 ipcp_estimate_growth (struct cgraph_node *node)
1120 {
1121   struct cgraph_edge *cs;
1122   int redirectable_node_callers = 0;
1123   int removable_args = 0;
1124   bool need_original
1125      = !cgraph_will_be_removed_from_program_if_no_direct_calls (node);
1126   struct ipa_node_params *info;
1127   int i, count;
1128   int growth;
1129
1130   for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1131     if (cs->caller == node || !ipcp_need_redirect_p (cs))
1132       redirectable_node_callers++;
1133     else
1134       need_original = true;
1135
1136   /* If we will be able to fully replace orignal node, we never increase
1137      program size.  */
1138   if (!need_original)
1139     return 0;
1140
1141   info = IPA_NODE_REF (node);
1142   count = ipa_get_param_count (info);
1143   for (i = 0; i < count; i++)
1144     {
1145       struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1146
1147       /* We can proactively remove obviously unused arguments.  */
1148       if (!ipa_is_param_used (info, i))
1149         removable_args++;
1150
1151       if (lat->type == IPA_CONST_VALUE)
1152         removable_args++;
1153     }
1154
1155   /* We make just very simple estimate of savings for removal of operand from
1156      call site.  Precise cost is dificult to get, as our size metric counts
1157      constants and moves as free.  Generally we are looking for cases that
1158      small function is called very many times.  */
1159   growth = node->local.inline_summary.self_size
1160            - removable_args * redirectable_node_callers;
1161   if (growth < 0)
1162     return 0;
1163   return growth;
1164 }
1165
1166
1167 /* Estimate cost of cloning NODE.  */
1168 static long
1169 ipcp_estimate_cloning_cost (struct cgraph_node *node)
1170 {
1171   int freq_sum = 1;
1172   gcov_type count_sum = 1;
1173   struct cgraph_edge *e;
1174   int cost;
1175
1176   cost = ipcp_estimate_growth (node) * 1000;
1177   if (!cost)
1178     {
1179       if (dump_file)
1180         fprintf (dump_file, "Versioning of %s will save code size\n",
1181                  cgraph_node_name (node));
1182       return 0;
1183     }
1184
1185   for (e = node->callers; e; e = e->next_caller)
1186     if (!bitmap_bit_p (dead_nodes, e->caller->uid)
1187         && !ipcp_need_redirect_p (e))
1188       {
1189         count_sum += e->count;
1190         freq_sum += e->frequency + 1;
1191       }
1192
1193   if (max_count)
1194     cost /= count_sum * 1000 / max_count + 1;
1195   else
1196     cost /= freq_sum * 1000 / REG_BR_PROB_BASE + 1;
1197   if (dump_file)
1198     fprintf (dump_file, "Cost of versioning %s is %i, (size: %i, freq: %i)\n",
1199              cgraph_node_name (node), cost, node->local.inline_summary.self_size,
1200              freq_sum);
1201   return cost + 1;
1202 }
1203
1204 /* Walk indirect calls of NODE and if any polymorphic can be turned into a
1205    direct one now, do so.  */
1206
1207 static void
1208 ipcp_process_devirtualization_opportunities (struct cgraph_node *node)
1209 {
1210   struct ipa_node_params *info = IPA_NODE_REF (node);
1211   struct cgraph_edge *ie, *next_ie;
1212
1213   for (ie = node->indirect_calls; ie; ie = next_ie)
1214     {
1215       int param_index, types_count, j;
1216       HOST_WIDE_INT token;
1217       tree target;
1218
1219       next_ie = ie->next_callee;
1220       if (!ie->indirect_info->polymorphic)
1221         continue;
1222       param_index = ie->indirect_info->param_index;
1223       if (param_index == -1
1224           || ipa_param_cannot_devirtualize_p (info, param_index)
1225           || ipa_param_types_vec_empty (info, param_index))
1226         continue;
1227
1228       token = ie->indirect_info->otr_token;
1229       target = NULL_TREE;
1230       types_count = VEC_length (tree, info->params[param_index].types);
1231       for (j = 0; j < types_count; j++)
1232         {
1233           tree binfo = VEC_index (tree, info->params[param_index].types, j);
1234           tree t = gimple_fold_obj_type_ref_known_binfo (token, binfo);
1235
1236           if (!t)
1237             {
1238               target = NULL_TREE;
1239               break;
1240             }
1241           else if (!target)
1242             target = t;
1243           else if (target != t)
1244             {
1245               target = NULL_TREE;
1246               break;
1247             }
1248         }
1249
1250       if (target)
1251         ipa_make_edge_direct_to_target (ie, target);
1252     }
1253 }
1254
1255 /* Return number of live constant parameters.  */
1256 static int
1257 ipcp_const_param_count (struct cgraph_node *node)
1258 {
1259   int const_param = 0;
1260   struct ipa_node_params *info = IPA_NODE_REF (node);
1261   int count = ipa_get_param_count (info);
1262   int i;
1263
1264   for (i = 0; i < count; i++)
1265     {
1266       struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1267       if ((ipcp_lat_is_insertable (lat)
1268           /* Do not count obviously unused arguments.  */
1269            && ipa_is_param_used (info, i))
1270           || (!ipa_param_cannot_devirtualize_p (info, i)
1271               && !ipa_param_types_vec_empty (info, i)))
1272         const_param++;
1273     }
1274   return const_param;
1275 }
1276
1277 /* Given that a formal parameter of NODE given by INDEX is known to be constant
1278    CST, try to find any indirect edges that can be made direct and make them
1279    so.  Note that INDEX is the number the parameter at the time of analyzing
1280    parameter uses and parameter removals should not be considered for it.  (In
1281    fact, the parameter itself has just been removed.)  */
1282
1283 static void
1284 ipcp_discover_new_direct_edges (struct cgraph_node *node, int index, tree cst)
1285 {
1286   struct cgraph_edge *ie, *next_ie;
1287
1288   for (ie = node->indirect_calls; ie; ie = next_ie)
1289     {
1290       struct cgraph_indirect_call_info *ici = ie->indirect_info;
1291
1292       next_ie = ie->next_callee;
1293       if (ici->param_index != index)
1294         continue;
1295
1296       if (ici->polymorphic)
1297         {
1298           tree binfo;
1299           HOST_WIDE_INT token;
1300
1301           if (TREE_CODE (cst) != ADDR_EXPR)
1302             continue;
1303
1304           binfo = gimple_get_relevant_ref_binfo (TREE_OPERAND (cst, 0),
1305                                                  NULL_TREE);
1306           if (!binfo)
1307             continue;
1308           gcc_assert (ie->indirect_info->anc_offset == 0);
1309           token = ie->indirect_info->otr_token;
1310           cst = gimple_fold_obj_type_ref_known_binfo (token, binfo);
1311           if (!cst)
1312             continue;
1313         }
1314
1315       ipa_make_edge_direct_to_target (ie, cst);
1316     }
1317 }
1318
1319
1320 /* Propagate the constant parameters found by ipcp_iterate_stage()
1321    to the function's code.  */
1322 static void
1323 ipcp_insert_stage (void)
1324 {
1325   struct cgraph_node *node, *node1 = NULL;
1326   int i;
1327   VEC (cgraph_edge_p, heap) * redirect_callers;
1328   VEC (ipa_replace_map_p,gc)* replace_trees;
1329   int node_callers, count;
1330   tree parm_tree;
1331   struct ipa_replace_map *replace_param;
1332   fibheap_t heap;
1333   long overall_size = 0, new_size = 0;
1334   long max_new_size;
1335
1336   ipa_check_create_node_params ();
1337   ipa_check_create_edge_args ();
1338   if (dump_file)
1339     fprintf (dump_file, "\nIPA insert stage:\n\n");
1340
1341   dead_nodes = BITMAP_ALLOC (NULL);
1342
1343   for (node = cgraph_nodes; node; node = node->next)
1344     if (node->analyzed)
1345       {
1346         if (node->count > max_count)
1347           max_count = node->count;
1348         overall_size += node->local.inline_summary.self_size;
1349       }
1350
1351   max_new_size = overall_size;
1352   if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS))
1353     max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS);
1354   max_new_size = max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1;
1355
1356   /* First collect all functions we proved to have constant arguments to
1357      heap.  */
1358   heap = fibheap_new ();
1359   for (node = cgraph_nodes; node; node = node->next)
1360     {
1361       struct ipa_node_params *info;
1362       /* Propagation of the constant is forbidden in certain conditions.  */
1363       if (!node->analyzed || !ipcp_node_modifiable_p (node))
1364           continue;
1365       info = IPA_NODE_REF (node);
1366       if (ipa_is_called_with_var_arguments (info))
1367         continue;
1368       if (ipcp_const_param_count (node))
1369         node->aux = fibheap_insert (heap, ipcp_estimate_cloning_cost (node),
1370                                     node);
1371      }
1372
1373   /* Now clone in priority order until code size growth limits are met or
1374      heap is emptied.  */
1375   while (!fibheap_empty (heap))
1376     {
1377       struct ipa_node_params *info;
1378       int growth = 0;
1379       bitmap args_to_skip;
1380       struct cgraph_edge *cs;
1381
1382       node = (struct cgraph_node *)fibheap_extract_min (heap);
1383       node->aux = NULL;
1384       if (dump_file)
1385         fprintf (dump_file, "considering function %s\n",
1386                  cgraph_node_name (node));
1387
1388       growth = ipcp_estimate_growth (node);
1389
1390       if (new_size + growth > max_new_size)
1391         break;
1392       if (growth
1393           && optimize_function_for_size_p (DECL_STRUCT_FUNCTION (node->decl)))
1394         {
1395           if (dump_file)
1396             fprintf (dump_file, "Not versioning, cold code would grow");
1397           continue;
1398         }
1399
1400       new_size += growth;
1401
1402       /* Look if original function becomes dead after clonning.  */
1403       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1404         if (cs->caller == node || ipcp_need_redirect_p (cs))
1405           break;
1406       if (!cs && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
1407         bitmap_set_bit (dead_nodes, node->uid);
1408
1409       info = IPA_NODE_REF (node);
1410       count = ipa_get_param_count (info);
1411
1412       replace_trees = VEC_alloc (ipa_replace_map_p, gc, 1);
1413       args_to_skip = BITMAP_GGC_ALLOC ();
1414       for (i = 0; i < count; i++)
1415         {
1416           struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1417           parm_tree = ipa_get_param (info, i);
1418
1419           /* We can proactively remove obviously unused arguments.  */
1420           if (!ipa_is_param_used (info, i))
1421             {
1422               bitmap_set_bit (args_to_skip, i);
1423               continue;
1424             }
1425
1426           if (lat->type == IPA_CONST_VALUE)
1427             {
1428               replace_param =
1429                 ipcp_create_replace_map (parm_tree, lat);
1430               VEC_safe_push (ipa_replace_map_p, gc, replace_trees, replace_param);
1431               bitmap_set_bit (args_to_skip, i);
1432             }
1433         }
1434
1435       /* Compute how many callers node has.  */
1436       node_callers = 0;
1437       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1438         node_callers++;
1439       redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
1440       for (cs = node->callers; cs != NULL; cs = cs->next_caller)
1441         if (!cs->indirect_inlining_edge)
1442           VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
1443
1444       /* Redirecting all the callers of the node to the
1445          new versioned node.  */
1446       node1 =
1447         cgraph_create_virtual_clone (node, redirect_callers, replace_trees,
1448                                      args_to_skip, "constprop");
1449       args_to_skip = NULL;
1450       VEC_free (cgraph_edge_p, heap, redirect_callers);
1451       replace_trees = NULL;
1452
1453       if (node1 == NULL)
1454         continue;
1455       ipcp_process_devirtualization_opportunities (node1);
1456
1457       if (dump_file)
1458         fprintf (dump_file, "versioned function %s with growth %i, overall %i\n",
1459                  cgraph_node_name (node), (int)growth, (int)new_size);
1460       ipcp_init_cloned_node (node, node1);
1461
1462       info = IPA_NODE_REF (node);
1463       for (i = 0; i < count; i++)
1464         {
1465           struct ipcp_lattice *lat = ipcp_get_lattice (info, i);
1466           if (lat->type == IPA_CONST_VALUE)
1467             ipcp_discover_new_direct_edges (node1, i, lat->constant);
1468         }
1469
1470       if (dump_file)
1471         dump_function_to_file (node1->decl, dump_file, dump_flags);
1472
1473       for (cs = node->callees; cs; cs = cs->next_callee)
1474         if (cs->callee->aux)
1475           {
1476             fibheap_delete_node (heap, (fibnode_t) cs->callee->aux);
1477             cs->callee->aux = fibheap_insert (heap,
1478                                               ipcp_estimate_cloning_cost (cs->callee),
1479                                               cs->callee);
1480           }
1481     }
1482
1483   while (!fibheap_empty (heap))
1484     {
1485       if (dump_file)
1486         fprintf (dump_file, "skipping function %s\n",
1487                  cgraph_node_name (node));
1488       node = (struct cgraph_node *) fibheap_extract_min (heap);
1489       node->aux = NULL;
1490     }
1491   fibheap_delete (heap);
1492   BITMAP_FREE (dead_nodes);
1493   ipcp_update_callgraph ();
1494   ipcp_update_profiling ();
1495 }
1496
1497 /* The IPCP driver.  */
1498 static unsigned int
1499 ipcp_driver (void)
1500 {
1501   cgraph_remove_unreachable_nodes (true,dump_file);
1502   if (dump_file)
1503     {
1504       fprintf (dump_file, "\nIPA structures before propagation:\n");
1505       if (dump_flags & TDF_DETAILS)
1506         ipa_print_all_params (dump_file);
1507       ipa_print_all_jump_functions (dump_file);
1508     }
1509   /* 2. Do the interprocedural propagation.  */
1510   ipcp_iterate_stage ();
1511   /* 3. Insert the constants found to the functions.  */
1512   ipcp_insert_stage ();
1513   if (dump_file && (dump_flags & TDF_DETAILS))
1514     {
1515       fprintf (dump_file, "\nProfiling info after insert stage:\n");
1516       ipcp_print_profile_data (dump_file);
1517     }
1518   /* Free all IPCP structures.  */
1519   ipa_free_all_structures_after_ipa_cp ();
1520   if (dump_file)
1521     fprintf (dump_file, "\nIPA constant propagation end\n");
1522   return 0;
1523 }
1524
1525 /* Initialization and computation of IPCP data structures.  This is the initial
1526    intraprocedural analysis of functions, which gathers information to be
1527    propagated later on.  */
1528
1529 static void
1530 ipcp_generate_summary (void)
1531 {
1532   struct cgraph_node *node;
1533
1534   if (dump_file)
1535     fprintf (dump_file, "\nIPA constant propagation start:\n");
1536   ipa_check_create_node_params ();
1537   ipa_check_create_edge_args ();
1538   ipa_register_cgraph_hooks ();
1539
1540   for (node = cgraph_nodes; node; node = node->next)
1541     if (node->analyzed)
1542       {
1543         /* Unreachable nodes should have been eliminated before ipcp.  */
1544         gcc_assert (node->needed || node->reachable);
1545
1546         node->local.versionable = tree_versionable_function_p (node->decl);
1547         ipa_analyze_node (node);
1548       }
1549 }
1550
1551 /* Write ipcp summary for nodes in SET.  */
1552 static void
1553 ipcp_write_summary (cgraph_node_set set,
1554                     varpool_node_set vset ATTRIBUTE_UNUSED)
1555 {
1556   ipa_prop_write_jump_functions (set);
1557 }
1558
1559 /* Read ipcp summary.  */
1560 static void
1561 ipcp_read_summary (void)
1562 {
1563   ipa_prop_read_jump_functions ();
1564 }
1565
1566 /* Gate for IPCP optimization.  */
1567 static bool
1568 cgraph_gate_cp (void)
1569 {
1570   /* FIXME: We should remove the optimize check after we ensure we never run
1571      IPA passes when not optimizng.  */
1572   return flag_ipa_cp && optimize;
1573 }
1574
1575 struct ipa_opt_pass_d pass_ipa_cp =
1576 {
1577  {
1578   IPA_PASS,
1579   "cp",                         /* name */
1580   cgraph_gate_cp,               /* gate */
1581   ipcp_driver,                  /* execute */
1582   NULL,                         /* sub */
1583   NULL,                         /* next */
1584   0,                            /* static_pass_number */
1585   TV_IPA_CONSTANT_PROP,         /* tv_id */
1586   0,                            /* properties_required */
1587   0,                            /* properties_provided */
1588   0,                            /* properties_destroyed */
1589   0,                            /* todo_flags_start */
1590   TODO_dump_cgraph | TODO_dump_func |
1591   TODO_remove_functions | TODO_ggc_collect /* todo_flags_finish */
1592  },
1593  ipcp_generate_summary,                 /* generate_summary */
1594  ipcp_write_summary,                    /* write_summary */
1595  ipcp_read_summary,                     /* read_summary */
1596  NULL,                                  /* write_optimization_summary */
1597  NULL,                                  /* read_optimization_summary */
1598  NULL,                                  /* stmt_fixup */
1599  0,                                     /* TODOs */
1600  NULL,                                  /* function_transform */
1601  NULL,                                  /* variable_transform */
1602 };