OSDN Git Service

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