OSDN Git Service

PR c++/45399
[pf3gnuchains/gcc-fork.git] / gcc / ipa-inline-analysis.c
1 /* Inlining decision heuristics.
2    Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3    Free Software Foundation, Inc.
4    Contributed by Jan Hubicka
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 /* Analysis used by the inliner and other passes limiting code size growth.
23
24    We estimate for each function
25      - function body size
26      - average function execution time
27      - inlining size benefit (that is how much of function body size
28        and its call sequence is expected to disappear by inlining)
29      - inlining time benefit
30      - function frame size
31    For each call
32      - call statement size and time
33
34    inlinie_summary datastructures store above information locally (i.e.
35    parameters of the function itself) and globally (i.e. parameters of
36    the function created by applying all the inline decisions already
37    present in the callgraph).
38
39    We provide accestor to the inline_summary datastructure and
40    basic logic updating the parameters when inlining is performed. 
41
42    The summaries are context sensitive.  Context means
43      1) partial assignment of known constant values of operands
44      2) whether function is inlined into the call or not.
45    It is easy to add more variants.  To represent function size and time
46    that depends on context (i.e. it is known to be optimized away when
47    context is known either by inlining or from IP-CP and clonning),
48    we use predicates. Predicates are logical formulas in
49    conjunctive-disjunctive form consisting of clauses. Clauses are bitmaps
50    specifying what conditions must be true. Conditions are simple test
51    of the form described above.
52
53    In order to make predicate (possibly) true, all of its clauses must
54    be (possibly) true. To make clause (possibly) true, one of conditions
55    it mentions must be (possibly) true.  There are fixed bounds on
56    number of clauses and conditions and all the manipulation functions
57    are conservative in positive direction. I.e. we may lose precision
58    by thinking that predicate may be true even when it is not.
59
60    estimate_edge_size and estimate_edge_growth can be used to query
61    function size/time in the given context.  inline_merge_summary merges
62    properties of caller and callee after inlining.
63
64    Finally pass_inline_parameters is exported.  This is used to drive
65    computation of function parameters used by the early inliner. IPA
66    inlined performs analysis via its analyze_function method. */
67
68 #include "config.h"
69 #include "system.h"
70 #include "coretypes.h"
71 #include "tm.h"
72 #include "tree.h"
73 #include "tree-inline.h"
74 #include "langhooks.h"
75 #include "flags.h"
76 #include "cgraph.h"
77 #include "diagnostic.h"
78 #include "gimple-pretty-print.h"
79 #include "timevar.h"
80 #include "params.h"
81 #include "tree-pass.h"
82 #include "coverage.h"
83 #include "ggc.h"
84 #include "tree-flow.h"
85 #include "ipa-prop.h"
86 #include "lto-streamer.h"
87 #include "ipa-inline.h"
88 #include "alloc-pool.h"
89
90 /* Estimate runtime of function can easilly run into huge numbers with many
91    nested loops.  Be sure we can compute time * INLINE_SIZE_SCALE in integer.
92    For anything larger we use gcov_type.  */
93 #define MAX_TIME 1000000
94
95 /* Number of bits in integer, but we really want to be stable across different
96    hosts.  */
97 #define NUM_CONDITIONS 32
98
99 enum predicate_conditions
100 {
101   predicate_false_condition = 0,
102   predicate_not_inlined_condition = 1,
103   predicate_first_dynamic_condition = 2
104 };
105
106 /* Special condition code we use to represent test that operand is compile time
107    constant.  */
108 #define IS_NOT_CONSTANT ERROR_MARK
109
110 /* Holders of ipa cgraph hooks: */
111 static struct cgraph_node_hook_list *function_insertion_hook_holder;
112 static struct cgraph_node_hook_list *node_removal_hook_holder;
113 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
114 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
115 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
116 static void inline_node_removal_hook (struct cgraph_node *, void *);
117 static void inline_node_duplication_hook (struct cgraph_node *,
118                                           struct cgraph_node *, void *);
119 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
120 static void inline_edge_duplication_hook (struct cgraph_edge *,
121                                           struct cgraph_edge *,
122                                           void *);
123
124 /* VECtor holding inline summaries.  
125    In GGC memory because conditions might point to constant trees.  */
126 VEC(inline_summary_t,gc) *inline_summary_vec;
127 VEC(inline_edge_summary_t,heap) *inline_edge_summary_vec;
128
129 /* Cached node/edge growths.  */
130 VEC(int,heap) *node_growth_cache;
131 VEC(edge_growth_cache_entry,heap) *edge_growth_cache;
132
133 /* Edge predicates goes here.  */
134 static alloc_pool edge_predicate_pool;
135
136 /* Return true predicate (tautology).
137    We represent it by empty list of clauses.  */
138
139 static inline struct predicate
140 true_predicate (void)
141 {
142   struct predicate p;
143   p.clause[0]=0;
144   return p;
145 }
146
147
148 /* Return predicate testing single condition number COND.  */
149
150 static inline struct predicate
151 single_cond_predicate (int cond)
152 {
153   struct predicate p;
154   p.clause[0]=1 << cond;
155   p.clause[1]=0;
156   return p;
157 }
158
159
160 /* Return false predicate.  First clause require false condition.  */
161
162 static inline struct predicate
163 false_predicate (void)
164 {
165   return single_cond_predicate (predicate_false_condition);
166 }
167
168
169 /* Return true if P is (false).  */
170
171 static inline bool
172 true_predicate_p (struct predicate *p)
173 {
174   return !p->clause[0];
175 }
176
177
178 /* Return true if P is (false).  */
179
180 static inline bool
181 false_predicate_p (struct predicate *p)
182 {
183   if (p->clause[0] == (1 << predicate_false_condition))
184     {
185       gcc_checking_assert (!p->clause[1]
186                            && p->clause[0] == 1 << predicate_false_condition);
187       return true;
188     }
189   return false;
190 }
191
192
193 /* Return predicate that is set true when function is not inlined.  */
194 static inline struct predicate
195 not_inlined_predicate (void)
196 {
197   return single_cond_predicate (predicate_not_inlined_condition);
198 }
199
200
201 /* Add condition to condition list CONDS.  */
202
203 static struct predicate
204 add_condition (struct inline_summary *summary, int operand_num,
205                enum tree_code code, tree val)
206 {
207   int i;
208   struct condition *c;
209   struct condition new_cond;
210
211   for (i = 0; VEC_iterate (condition, summary->conds, i, c); i++)
212     {
213       if (c->operand_num == operand_num
214           && c->code == code
215           && c->val == val)
216         return single_cond_predicate (i + predicate_first_dynamic_condition);
217     }
218   /* Too many conditions.  Give up and return constant true.  */
219   if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
220     return true_predicate ();
221
222   new_cond.operand_num = operand_num;
223   new_cond.code = code;
224   new_cond.val = val;
225   VEC_safe_push (condition, gc, summary->conds, &new_cond);
226   return single_cond_predicate (i + predicate_first_dynamic_condition);
227 }
228
229
230 /* Add clause CLAUSE into the predicate P.  */
231
232 static inline void
233 add_clause (struct predicate *p, clause_t clause)
234 {
235   int i;
236   int i2;
237   int insert_here = -1;
238
239   /* True clause.  */
240   if (!clause)
241     return;
242
243   /* False clause makes the whole predicate false.  Kill the other variants.  */
244   if (clause == (1 << predicate_false_condition))
245     {
246       p->clause[0] = (1 << predicate_false_condition);
247       p->clause[1] = 0;
248       return;
249     }
250   if (false_predicate_p (p))
251     return;
252
253   /* No one should be sily enough to add false into nontrivial clauses.  */
254   gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
255
256   /* Look where to insert the clause.  At the same time prune out
257      clauses of P that are implied by the new clause and thus
258      redundant.  */
259   for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
260     {
261       p->clause[i2] = p->clause[i];
262
263       if (!p->clause[i])
264         break;
265
266       /* If p->clause[i] implies clause, there is nothing to add.  */
267       if ((p->clause[i] & clause) == p->clause[i])
268         {
269           /* We had nothing to add, none of clauses should've become redundant.  */
270           gcc_checking_assert (i == i2);
271           return;
272         }
273
274       if (p->clause[i] < clause && insert_here < 0)
275         insert_here = i2;
276
277       /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
278          Otherwise the p->clause[i] has to stay.  */
279       if ((p->clause[i] & clause) != clause)
280         i2++;
281     }
282   /* We run out of variants.  Be conservative in positive direction.  */
283   if (i2 == MAX_CLAUSES)
284     return;
285   /* Keep clauses in decreasing order. This makes equivalence testing easy.  */
286   p->clause[i2 + 1] = 0;
287   if (insert_here >= 0)
288     for (;i2 > insert_here; i2--)
289       p->clause[i2] = p->clause[i2 - 1];
290   else
291     insert_here = i2;
292   p->clause[insert_here] = clause;
293 }
294
295
296 /* Return P & P2.  */
297
298 static struct predicate
299 and_predicates (struct predicate *p, struct predicate *p2)
300 {
301   struct predicate out = *p;
302   int i;
303
304   /* Avoid busy work.  */
305   if (false_predicate_p (p2) || true_predicate_p (p))
306     return *p2;
307   if (false_predicate_p (p) || true_predicate_p (p2))
308     return *p;
309
310   /* See how far predicates match.  */
311   for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
312     {
313       gcc_checking_assert (i < MAX_CLAUSES);
314     }
315     
316   /* Combine the predicates rest.  */
317   for (; p2->clause[i]; i++)
318     {
319       gcc_checking_assert (i < MAX_CLAUSES);
320       add_clause (&out, p2->clause[i]);
321     }
322   return out;
323 }
324
325
326 /* Return true if predicates are obviously equal.  */
327
328 static inline bool
329 predicates_equal_p (struct predicate *p, struct predicate *p2)
330 {
331   int i;
332   for (i = 0; p->clause[i]; i++)
333     {
334       gcc_checking_assert (i < MAX_CLAUSES);
335       gcc_checking_assert (p->clause [i] > p->clause[i + 1]);
336       gcc_checking_assert (!p2->clause[i] || p2->clause [i] > p2->clause[i + 1]);
337       if (p->clause[i] != p2->clause[i])
338         return false;
339     }
340   return !p2->clause[i];
341 }
342
343
344 /* Return P | P2.  */
345
346 static struct predicate
347 or_predicates (struct predicate *p, struct predicate *p2)
348 {
349   struct predicate out = true_predicate ();
350   int i,j;
351
352   /* Avoid busy work.  */
353   if (false_predicate_p (p2) || true_predicate_p (p))
354     return *p;
355   if (false_predicate_p (p) || true_predicate_p (p2))
356     return *p2;
357   if (predicates_equal_p (p, p2))
358     return *p;
359
360   /* OK, combine the predicates.  */
361   for (i = 0; p->clause[i]; i++)
362     for (j = 0; p2->clause[j]; j++)
363       {
364         gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
365         add_clause (&out, p->clause[i] | p2->clause[j]);
366       }
367   return out;
368 }
369
370
371 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false if predicate P
372    to be false.  */
373
374 static bool
375 evaluate_predicate (struct predicate *p, clause_t possible_truths)
376 {
377   int i;
378
379   /* True remains true.  */
380   if (true_predicate_p (p))
381     return true;
382
383   gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
384
385   /* See if we can find clause we can disprove.  */
386   for (i = 0; p->clause[i]; i++)
387     {
388       gcc_checking_assert (i < MAX_CLAUSES);
389       if (!(p->clause[i] & possible_truths))
390         return false;
391     }
392   return true;
393 }
394
395
396 /* Dump conditional COND.  */
397
398 static void
399 dump_condition (FILE *f, conditions conditions, int cond)
400 {
401   condition *c;
402   if (cond == predicate_false_condition)
403     fprintf (f, "false");
404   else if (cond == predicate_not_inlined_condition)
405     fprintf (f, "not inlined");
406   else
407     {
408       c = VEC_index (condition, conditions, cond - predicate_first_dynamic_condition);
409       fprintf (f, "op%i", c->operand_num);
410       if (c->code == IS_NOT_CONSTANT)
411         {
412           fprintf (f, " not constant");
413           return;
414         }
415       fprintf (f, " %s ", op_symbol_code (c->code));
416       print_generic_expr (f, c->val, 1);
417     }
418 }
419
420
421 /* Dump clause CLAUSE.  */
422
423 static void
424 dump_clause (FILE *f, conditions conds, clause_t clause)
425 {
426   int i;
427   bool found = false;
428   fprintf (f, "(");
429   if (!clause)
430     fprintf (f, "true");
431   for (i = 0; i < NUM_CONDITIONS; i++)
432     if (clause & (1 << i))
433       {
434         if (found)
435           fprintf (f, " || ");
436         found = true;
437         dump_condition (f, conds, i);
438       }
439   fprintf (f, ")");
440 }
441
442
443 /* Dump predicate PREDICATE.  */
444
445 static void
446 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
447 {
448   int i;
449   if (true_predicate_p (pred))
450     dump_clause (f, conds, 0);
451   else
452     for (i = 0; pred->clause[i]; i++)
453       {
454         if (i)
455           fprintf (f, " && ");
456         dump_clause (f, conds, pred->clause[i]);
457       }
458   fprintf (f, "\n");
459 }
460
461
462 /* Record SIZE and TIME under condition PRED into the inline summary.  */
463
464 static void
465 account_size_time (struct inline_summary *summary, int size, int time, struct predicate *pred)
466 {
467   size_time_entry *e;
468   bool found = false;
469   int i;
470
471   if (false_predicate_p (pred))
472     return;
473
474   /* We need to create initial empty unconitional clause, but otherwie
475      we don't need to account empty times and sizes.  */
476   if (!size && !time && summary->entry)
477     return;
478
479   /* Watch overflow that might result from insane profiles.  */
480   if (time > MAX_TIME * INLINE_TIME_SCALE)
481     time = MAX_TIME * INLINE_TIME_SCALE;
482   gcc_assert (time >= 0);
483
484   for (i = 0; VEC_iterate (size_time_entry, summary->entry, i, e); i++)
485     if (predicates_equal_p (&e->predicate, pred))
486       {
487         found = true;
488         break;
489       }
490   if (i == 32)
491     {
492       i = 0;
493       found = true;
494       e = VEC_index (size_time_entry, summary->entry, 0);
495       gcc_assert (!e->predicate.clause[0]);
496     }
497   if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
498     {
499       fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
500                ((double)size) / INLINE_SIZE_SCALE, ((double)time) / INLINE_TIME_SCALE,
501                found ? "" : "new ");
502       dump_predicate (dump_file, summary->conds, pred);
503     }
504   if (!found)
505     {
506       struct size_time_entry new_entry;
507       new_entry.size = size;
508       new_entry.time = time;
509       new_entry.predicate = *pred;
510       VEC_safe_push (size_time_entry, gc, summary->entry, &new_entry);
511     }
512   else
513     {
514       e->size += size;
515       e->time += time;
516       if (e->time > MAX_TIME * INLINE_TIME_SCALE)
517         e->time = MAX_TIME * INLINE_TIME_SCALE;
518     }
519 }
520
521 /* Set predicate for edge E.  */
522
523 static void
524 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
525 {
526   struct inline_edge_summary *es = inline_edge_summary (e);
527   if (predicate && !true_predicate_p (predicate))
528     {
529       if (!es->predicate)
530         es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
531       *es->predicate = *predicate;
532     }
533   else
534     {
535       if (es->predicate)
536         pool_free (edge_predicate_pool, es->predicate);
537       es->predicate = NULL;
538     }
539 }
540
541
542 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
543    Return clause of possible truths. When INLINE_P is true, assume that
544    we are inlining.  */
545
546 static clause_t
547 evaluate_conditions_for_known_args (struct cgraph_node *node,
548                                     bool inline_p,
549                                     VEC (tree, heap) *known_vals)
550 {
551   clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
552   struct inline_summary *info = inline_summary (node);
553   int i;
554   struct condition *c;
555
556   for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
557     {
558       tree val;
559       tree res;
560
561       /* We allow call stmt to have fewer arguments than the callee
562          function (especially for K&R style programs).  So bound
563          check here.  */
564       if (c->operand_num < (int)VEC_length (tree, known_vals))
565         val = VEC_index (tree, known_vals, c->operand_num);
566       else
567         val = NULL;
568
569       if (!val)
570         {
571           clause |= 1 << (i + predicate_first_dynamic_condition);
572           continue;
573         }
574       if (c->code == IS_NOT_CONSTANT)
575         continue;
576       res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
577       if (res
578           && integer_zerop (res))
579         continue;
580       clause |= 1 << (i + predicate_first_dynamic_condition);
581     }
582   return clause;
583 }
584
585
586 /* Work out what conditions might be true at invocation of E.  */
587
588 static clause_t
589 evaluate_conditions_for_edge (struct cgraph_edge *e, bool inline_p)
590 {
591   clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
592   struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
593   struct inline_summary *info = inline_summary (callee);
594   int i;
595
596   if (ipa_node_params_vector && info->conds
597       /* FIXME: it seems that we forget to get argument count in some cases,
598          probaby for previously indirect edges or so.  */
599       && ipa_get_cs_argument_count (IPA_EDGE_REF (e)))
600     {
601       struct ipa_node_params *parms_info;
602       struct ipa_edge_args *args = IPA_EDGE_REF (e);
603       int i, count = ipa_get_cs_argument_count (args);
604       VEC (tree, heap) *known_vals = NULL;
605
606       if (e->caller->global.inlined_to)
607         parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
608       else
609         parms_info = IPA_NODE_REF (e->caller);
610
611       VEC_safe_grow_cleared (tree, heap, known_vals, count);
612       for (i = 0; i < count; i++)
613         {
614           tree cst = ipa_cst_from_jfunc (parms_info,
615                                          ipa_get_ith_jump_func (args, i));
616           if (cst)
617             VEC_replace (tree, known_vals, i, cst);
618         }
619       clause = evaluate_conditions_for_known_args (callee,
620                                                    inline_p, known_vals);
621       VEC_free (tree, heap, known_vals);
622     }
623   else
624     for (i = 0; i < (int)VEC_length (condition, info->conds); i++)
625       clause |= 1 << (i + predicate_first_dynamic_condition);
626
627   return clause;
628 }
629
630
631 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
632
633 static void
634 inline_summary_alloc (void)
635 {
636   if (!node_removal_hook_holder)
637     node_removal_hook_holder =
638       cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
639   if (!edge_removal_hook_holder)
640     edge_removal_hook_holder =
641       cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
642   if (!node_duplication_hook_holder)
643     node_duplication_hook_holder =
644       cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
645   if (!edge_duplication_hook_holder)
646     edge_duplication_hook_holder =
647       cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
648
649   if (VEC_length (inline_summary_t, inline_summary_vec)
650       <= (unsigned) cgraph_max_uid)
651     VEC_safe_grow_cleared (inline_summary_t, gc,
652                            inline_summary_vec, cgraph_max_uid + 1);
653   if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
654       <= (unsigned) cgraph_edge_max_uid)
655     VEC_safe_grow_cleared (inline_edge_summary_t, heap,
656                            inline_edge_summary_vec, cgraph_edge_max_uid + 1);
657   if (!edge_predicate_pool)
658     edge_predicate_pool = create_alloc_pool ("edge predicates", sizeof (struct predicate),
659                                              10);
660 }
661
662 /* Hook that is called by cgraph.c when a node is removed.  */
663
664 static void
665 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
666 {
667   struct inline_summary *info;
668   if (VEC_length (inline_summary_t, inline_summary_vec)
669       <= (unsigned)node->uid)
670     return;
671   info = inline_summary (node);
672   reset_node_growth_cache (node);
673   VEC_free (condition, gc, info->conds);
674   VEC_free (size_time_entry, gc, info->entry);
675   info->conds = NULL;
676   info->entry = NULL;
677   memset (info, 0, sizeof (inline_summary_t));
678 }
679
680
681 /* Hook that is called by cgraph.c when a node is duplicated.  */
682
683 static void
684 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
685                               ATTRIBUTE_UNUSED void *data)
686 {
687   struct inline_summary *info;
688   inline_summary_alloc ();
689   info = inline_summary (dst);
690   memcpy (info, inline_summary (src),
691           sizeof (struct inline_summary));
692   /* TODO: as an optimization, we may avoid copying conditions
693      that are known to be false or true.  */
694   info->conds = VEC_copy (condition, gc, info->conds);
695
696   /* When there are any replacements in the function body, see if we can figure
697      out that something was optimized out.  */
698   if (ipa_node_params_vector && dst->clone.tree_map)
699     {
700       VEC(size_time_entry,gc) *entry = info->entry;
701       /* Use SRC parm info since it may not be copied yet.  */
702       struct ipa_node_params *parms_info = IPA_NODE_REF (src);
703       VEC (tree, heap) *known_vals = NULL;
704       int count = ipa_get_param_count (parms_info);
705       int i,j;
706       clause_t possible_truths;
707       struct predicate true_pred = true_predicate ();
708       size_time_entry *e;
709       int optimized_out_size = 0;
710       gcov_type optimized_out_time = 0;
711       bool inlined_to_p = false;
712       struct cgraph_edge *edge;
713
714       info->entry = 0;
715       VEC_safe_grow_cleared (tree, heap, known_vals, count);
716       for (i = 0; i < count; i++)
717         {
718           tree t = ipa_get_param (parms_info, i);
719           struct ipa_replace_map *r;
720
721           for (j = 0;
722                VEC_iterate (ipa_replace_map_p, dst->clone.tree_map, j, r);
723                j++)
724             {
725               if (r->old_tree == t
726                   && r->replace_p
727                   && !r->ref_p)
728                 {
729                   VEC_replace (tree, known_vals, i, r->new_tree);
730                   break;
731                 }
732             }
733         }
734       possible_truths = evaluate_conditions_for_known_args (dst,
735                                                             false, known_vals);
736       VEC_free (tree, heap, known_vals);
737
738       account_size_time (info, 0, 0, &true_pred);
739
740       /* Remap size_time vectors.
741          Simplify the predicate by prunning out alternatives that are known
742          to be false.
743          TODO: as on optimization, we can also eliminate conditions known to be true.  */
744       for (i = 0; VEC_iterate (size_time_entry, entry, i, e); i++)
745         {
746           struct predicate new_predicate = true_predicate ();
747           for (j = 0; e->predicate.clause[j]; j++)
748             if (!(possible_truths & e->predicate.clause[j]))
749               {
750                 new_predicate = false_predicate ();
751                 break;
752               }
753             else
754               add_clause (&new_predicate,
755                           possible_truths & e->predicate.clause[j]);
756           if (false_predicate_p (&new_predicate))
757             {
758               optimized_out_size += e->size;
759               optimized_out_time += e->time;
760             }
761           else
762             account_size_time (info, e->size, e->time, &new_predicate);
763         }
764
765       /* Remap edge predicates with the same simplificaiton as above.  */
766       for (edge = dst->callees; edge; edge = edge->next_callee)
767         {
768           struct predicate new_predicate = true_predicate ();
769           struct inline_edge_summary *es = inline_edge_summary (edge);
770
771           if (!edge->inline_failed)
772             inlined_to_p = true;
773           if (!es->predicate)
774             continue;
775           for (j = 0; es->predicate->clause[j]; j++)
776             if (!(possible_truths & es->predicate->clause[j]))
777               {
778                 new_predicate = false_predicate ();
779                 break;
780               }
781             else
782               add_clause (&new_predicate,
783                           possible_truths & es->predicate->clause[j]);
784           if (false_predicate_p (&new_predicate)
785               && !false_predicate_p (es->predicate))
786             {
787               optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
788               optimized_out_time += (es->call_stmt_time
789                                      * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
790                                      * edge->frequency);
791               edge->frequency = 0;
792             }
793           *es->predicate = new_predicate;
794         }
795
796       /* Remap indirect edge predicates with the same simplificaiton as above.  */
797       for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
798         {
799           struct predicate new_predicate = true_predicate ();
800           struct inline_edge_summary *es = inline_edge_summary (edge);
801
802           if (!edge->inline_failed)
803             inlined_to_p = true;
804           if (!es->predicate)
805             continue;
806           for (j = 0; es->predicate->clause[j]; j++)
807             if (!(possible_truths & es->predicate->clause[j]))
808               {
809                 new_predicate = false_predicate ();
810                 break;
811               }
812             else
813               add_clause (&new_predicate,
814                           possible_truths & es->predicate->clause[j]);
815           if (false_predicate_p (&new_predicate)
816               && !false_predicate_p (es->predicate))
817             {
818               optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
819               optimized_out_time += (es->call_stmt_time
820                                      * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
821                                      * edge->frequency);
822               edge->frequency = 0;
823             }
824           *es->predicate = new_predicate;
825         }
826
827       /* If inliner or someone after inliner will ever start producing
828          non-trivial clones, we will get trouble with lack of information
829          about updating self sizes, because size vectors already contains
830          sizes of the calees.  */
831       gcc_assert (!inlined_to_p 
832                   || (!optimized_out_size && !optimized_out_time));
833
834       info->size -= optimized_out_size / INLINE_SIZE_SCALE;
835       info->self_size -= optimized_out_size / INLINE_SIZE_SCALE;
836       gcc_assert (info->size > 0);
837       gcc_assert (info->self_size > 0);
838
839       optimized_out_time /= INLINE_TIME_SCALE;
840       if (optimized_out_time > MAX_TIME)
841         optimized_out_time = MAX_TIME;
842       info->time -= optimized_out_time;
843       info->self_time -= optimized_out_time;
844       if (info->time < 0)
845         info->time = 0;
846       if (info->self_time < 0)
847         info->self_time = 0;
848     }
849   else
850     info->entry = VEC_copy (size_time_entry, gc, info->entry);
851 }
852
853
854 /* Hook that is called by cgraph.c when a node is duplicated.  */
855
856 static void
857 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
858                               ATTRIBUTE_UNUSED void *data)
859 {
860   struct inline_edge_summary *info;
861   struct inline_edge_summary *srcinfo;
862   inline_summary_alloc ();
863   info = inline_edge_summary (dst);
864   srcinfo = inline_edge_summary (src);
865   memcpy (info, srcinfo,
866           sizeof (struct inline_edge_summary));
867   info->predicate = NULL;
868   edge_set_predicate (dst, srcinfo->predicate);
869 }
870
871
872 /* Keep edge cache consistent across edge removal.  */
873
874 static void
875 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
876 {
877   if (edge_growth_cache)
878     reset_edge_growth_cache (edge);
879   if (edge->uid < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
880     {
881       edge_set_predicate (edge, NULL);
882       memset (inline_edge_summary (edge), 0, sizeof (struct inline_edge_summary));
883     }
884 }
885
886
887 /* Initialize growth caches.  */
888
889 void
890 initialize_growth_caches (void)
891 {
892   if (cgraph_edge_max_uid)
893     VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
894                            cgraph_edge_max_uid);
895   if (cgraph_max_uid)
896     VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
897 }
898
899
900 /* Free growth caches.  */
901
902 void
903 free_growth_caches (void)
904 {
905   VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
906   edge_growth_cache = 0;
907   VEC_free (int, heap, node_growth_cache);
908   node_growth_cache = 0;
909 }
910
911
912 /* Dump edge summaries associated to NODE and recursively to all clones.
913    Indent by INDENT.  */
914
915 static void
916 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
917                           struct inline_summary *info)
918 {
919   struct cgraph_edge *edge;
920   for (edge = node->callees; edge; edge = edge->next_callee)
921     {
922       struct inline_edge_summary *es = inline_edge_summary (edge);
923       struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee, NULL);
924       fprintf (f, "%*s%s/%i %s\n%*s  loop depth:%2i freq:%4i size:%2i time: %2i callee size:%2i stack:%2i",
925                indent, "", cgraph_node_name (callee),
926                callee->uid, 
927                !edge->inline_failed ? "inlined"
928                : cgraph_inline_failed_string (edge->inline_failed),
929                indent, "",
930                es->loop_depth,  
931                edge->frequency,
932                es->call_stmt_size,
933                es->call_stmt_time,
934                (int)inline_summary (callee)->size,
935                (int)inline_summary (callee)->estimated_stack_size);
936       if (es->predicate)
937         {
938           fprintf (f, " predicate: ");
939           dump_predicate (f, info->conds, es->predicate);
940         }
941       else
942           fprintf (f, "\n");
943       if (!edge->inline_failed)
944         {
945           fprintf (f, "%*sStack frame offset %i, callee self size %i, callee size %i\n",
946                    indent+2, "",
947                    (int)inline_summary (callee)->stack_frame_offset,
948                    (int)inline_summary (callee)->estimated_self_stack_size,
949                    (int)inline_summary (callee)->estimated_stack_size);
950           dump_inline_edge_summary (f, indent+2, callee, info);
951         }
952     }
953   for (edge = node->indirect_calls; edge; edge = edge->next_callee)
954     {
955       struct inline_edge_summary *es = inline_edge_summary (edge);
956       fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i time: %2i\n",
957                indent, "",
958                es->loop_depth,  
959                edge->frequency,
960                es->call_stmt_size,
961                es->call_stmt_time);
962       if (es->predicate)
963         {
964           fprintf (f, "predicate: ");
965           dump_predicate (f, info->conds, es->predicate);
966         }
967       else
968           fprintf (f, "\n");
969     }
970 }
971
972
973 void
974 dump_inline_summary (FILE * f, struct cgraph_node *node)
975 {
976   if (node->analyzed)
977     {
978       struct inline_summary *s = inline_summary (node);
979       size_time_entry *e;
980       int i;
981       fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
982                node->uid);
983       if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
984         fprintf (f, " always_inline");
985       if (s->inlinable)
986         fprintf (f, " inlinable");
987       if (s->versionable)
988         fprintf (f, " versionable");
989       fprintf (f, "\n  self time:       %i\n",
990                s->self_time);
991       fprintf (f, "  global time:     %i\n", s->time);
992       fprintf (f, "  self size:       %i\n",
993                s->self_size);
994       fprintf (f, "  global size:     %i\n", s->size);
995       fprintf (f, "  self stack:      %i\n",
996                (int) s->estimated_self_stack_size);
997       fprintf (f, "  global stack:    %i\n",
998                (int) s->estimated_stack_size);
999       for (i = 0;
1000            VEC_iterate (size_time_entry, s->entry, i, e);
1001            i++)
1002         {
1003           fprintf (f, "    size:%f, time:%f, predicate:",
1004                    (double) e->size / INLINE_SIZE_SCALE,
1005                    (double) e->time / INLINE_TIME_SCALE);
1006           dump_predicate (f, s->conds, &e->predicate);
1007         }
1008       fprintf (f, "  calls:\n");
1009       dump_inline_edge_summary (f, 4, node, s);
1010       fprintf (f, "\n");
1011     }
1012 }
1013
1014 DEBUG_FUNCTION void
1015 debug_inline_summary (struct cgraph_node *node)
1016 {
1017   dump_inline_summary (stderr, node);
1018 }
1019
1020 void
1021 dump_inline_summaries (FILE *f)
1022 {
1023   struct cgraph_node *node;
1024
1025   for (node = cgraph_nodes; node; node = node->next)
1026     if (node->analyzed && !node->global.inlined_to)
1027       dump_inline_summary (f, node);
1028 }
1029
1030 /* Give initial reasons why inlining would fail on EDGE.  This gets either
1031    nullified or usually overwritten by more precise reasons later.  */
1032
1033 void
1034 initialize_inline_failed (struct cgraph_edge *e)
1035 {
1036   struct cgraph_node *callee = e->callee;
1037
1038   if (e->indirect_unknown_callee)
1039     e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1040   else if (!callee->analyzed)
1041     e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1042   else if (callee->local.redefined_extern_inline)
1043     e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1044   else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
1045     e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1046   else
1047     e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1048 }
1049
1050 /* See if statement might disappear after inlining.
1051    0 - means not eliminated
1052    1 - half of statements goes away
1053    2 - for sure it is eliminated.
1054    We are not terribly sophisticated, basically looking for simple abstraction
1055    penalty wrappers.  */
1056
1057 static int
1058 eliminated_by_inlining_prob (gimple stmt)
1059 {
1060   enum gimple_code code = gimple_code (stmt);
1061   switch (code)
1062     {
1063       case GIMPLE_RETURN:
1064         return 2;
1065       case GIMPLE_ASSIGN:
1066         if (gimple_num_ops (stmt) != 2)
1067           return 0;
1068
1069         /* Casts of parameters, loads from parameters passed by reference
1070            and stores to return value or parameters are often free after
1071            inlining dua to SRA and further combining.
1072            Assume that half of statements goes away.  */
1073         if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1074             || gimple_assign_rhs_code (stmt) == NOP_EXPR
1075             || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1076             || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1077           {
1078             tree rhs = gimple_assign_rhs1 (stmt);
1079             tree lhs = gimple_assign_lhs (stmt);
1080             tree inner_rhs = rhs;
1081             tree inner_lhs = lhs;
1082             bool rhs_free = false;
1083             bool lhs_free = false;
1084
1085             while (handled_component_p (inner_lhs)
1086                    || TREE_CODE (inner_lhs) == MEM_REF)
1087               inner_lhs = TREE_OPERAND (inner_lhs, 0);
1088             while (handled_component_p (inner_rhs)
1089                    || TREE_CODE (inner_rhs) == ADDR_EXPR
1090                    || TREE_CODE (inner_rhs) == MEM_REF)
1091               inner_rhs = TREE_OPERAND (inner_rhs, 0);
1092
1093
1094             if (TREE_CODE (inner_rhs) == PARM_DECL
1095                 || (TREE_CODE (inner_rhs) == SSA_NAME
1096                     && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
1097                     && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
1098               rhs_free = true;
1099             if (rhs_free && is_gimple_reg (lhs))
1100               lhs_free = true;
1101             if (((TREE_CODE (inner_lhs) == PARM_DECL
1102                   || (TREE_CODE (inner_lhs) == SSA_NAME
1103                       && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
1104                       && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
1105                  && inner_lhs != lhs)
1106                 || TREE_CODE (inner_lhs) == RESULT_DECL
1107                 || (TREE_CODE (inner_lhs) == SSA_NAME
1108                     && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
1109               lhs_free = true;
1110             if (lhs_free
1111                 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1112               rhs_free = true;
1113             if (lhs_free && rhs_free)
1114               return 1;
1115           }
1116         return 0;
1117       default:
1118         return 0;
1119     }
1120 }
1121
1122
1123 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1124    predicates to the CFG edges.   */
1125
1126 static void
1127 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1128                                    struct inline_summary *summary,
1129                                    basic_block bb)
1130 {
1131   gimple last;
1132   tree op;
1133   int index;
1134   enum tree_code code, inverted_code;
1135   edge e;
1136   edge_iterator ei;
1137   gimple set_stmt;
1138   tree op2;
1139
1140   last = last_stmt (bb);
1141   if (!last
1142       || gimple_code (last) != GIMPLE_COND)
1143     return;
1144   if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1145     return;
1146   op = gimple_cond_lhs (last);
1147   /* TODO: handle conditionals like
1148      var = op0 < 4;
1149      if (var != 0).  */
1150   if (TREE_CODE (op) != SSA_NAME)
1151     return;
1152   if (SSA_NAME_IS_DEFAULT_DEF (op))
1153     {
1154       index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
1155       if (index == -1)
1156         return;
1157       code = gimple_cond_code (last);
1158       inverted_code = invert_tree_comparison (code,
1159                                               HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1160
1161       FOR_EACH_EDGE (e, ei, bb->succs)
1162         {
1163           struct predicate p = add_condition (summary,
1164                                               index,
1165                                               e->flags & EDGE_TRUE_VALUE
1166                                               ? code : inverted_code,
1167                                               gimple_cond_rhs (last));
1168           e->aux = pool_alloc (edge_predicate_pool);
1169           *(struct predicate *)e->aux = p;
1170         }
1171     }
1172
1173   /* Special case
1174      if (builtin_constant_p (op))
1175        constant_code
1176      else
1177        nonconstant_code.
1178      Here we can predicate nonconstant_code.  We can't
1179      really handle constant_code since we have no predicate
1180      for this and also the constant code is not known to be
1181      optimized away when inliner doen't see operand is constant.
1182      Other optimizers might think otherwise.  */
1183   set_stmt = SSA_NAME_DEF_STMT (op);
1184   if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1185       || gimple_call_num_args (set_stmt) != 1)
1186     return;
1187   op2 = gimple_call_arg (set_stmt, 0);
1188   if (!SSA_NAME_IS_DEFAULT_DEF (op2))
1189     return;
1190   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op2));
1191   if (index == -1)
1192     return;
1193   if (gimple_cond_code (last) != NE_EXPR
1194       || !integer_zerop (gimple_cond_rhs (last)))
1195     return;
1196   FOR_EACH_EDGE (e, ei, bb->succs)
1197     if (e->flags & EDGE_FALSE_VALUE)
1198       {
1199         struct predicate p = add_condition (summary,
1200                                             index,
1201                                             IS_NOT_CONSTANT,
1202                                             NULL);
1203         e->aux = pool_alloc (edge_predicate_pool);
1204         *(struct predicate *)e->aux = p;
1205       }
1206 }
1207
1208
1209 /* If BB ends by a switch we can turn into predicates, attach corresponding
1210    predicates to the CFG edges.   */
1211
1212 static void
1213 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1214                                    struct inline_summary *summary,
1215                                    basic_block bb)
1216 {
1217   gimple last;
1218   tree op;
1219   int index;
1220   edge e;
1221   edge_iterator ei;
1222   size_t n;
1223   size_t case_idx;
1224
1225   last = last_stmt (bb);
1226   if (!last
1227       || gimple_code (last) != GIMPLE_SWITCH)
1228     return;
1229   op = gimple_switch_index (last);
1230   if (TREE_CODE (op) != SSA_NAME
1231       || !SSA_NAME_IS_DEFAULT_DEF (op))
1232     return;
1233   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
1234   if (index == -1)
1235     return;
1236
1237   FOR_EACH_EDGE (e, ei, bb->succs)
1238     {
1239       e->aux = pool_alloc (edge_predicate_pool);
1240       *(struct predicate *)e->aux = false_predicate ();
1241     }
1242   n = gimple_switch_num_labels(last);
1243   for (case_idx = 0; case_idx < n; ++case_idx)
1244     {
1245       tree cl = gimple_switch_label (last, case_idx);
1246       tree min, max;
1247       struct predicate p;
1248
1249       e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1250       min = CASE_LOW (cl);
1251       max = CASE_HIGH (cl);
1252
1253       /* For default we might want to construct predicate that none
1254          of cases is met, but it is bit hard to do not having negations
1255          of conditionals handy.  */
1256       if (!min && !max)
1257         p = true_predicate ();
1258       else if (!max)
1259         p = add_condition (summary, index,
1260                            EQ_EXPR,
1261                            min);
1262       else
1263         {
1264           struct predicate p1, p2;
1265           p1 = add_condition (summary, index,
1266                               GE_EXPR,
1267                               min);
1268           p2 = add_condition (summary, index,
1269                               LE_EXPR,
1270                               max);
1271           p = and_predicates (&p1, &p2);
1272         }
1273       *(struct predicate *)e->aux
1274         = or_predicates (&p, (struct predicate *)e->aux);
1275     }
1276 }
1277
1278
1279 /* For each BB in NODE attach to its AUX pointer predicate under
1280    which it is executable.  */
1281
1282 static void
1283 compute_bb_predicates (struct cgraph_node *node,
1284                        struct ipa_node_params *parms_info,
1285                        struct inline_summary *summary)
1286 {
1287   struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1288   bool done = false;
1289   basic_block bb;
1290
1291   FOR_EACH_BB_FN (bb, my_function)
1292     {
1293       set_cond_stmt_execution_predicate (parms_info, summary, bb);
1294       set_switch_stmt_execution_predicate (parms_info, summary, bb);
1295     }
1296
1297   /* Entry block is always executable.  */
1298   ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux = pool_alloc (edge_predicate_pool);
1299   *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1300     = true_predicate ();
1301
1302   /* A simple dataflow propagation of predicates forward in the CFG.
1303      TODO: work in reverse postorder.  */
1304   while (!done)
1305     {
1306       done = true;
1307       FOR_EACH_BB_FN (bb, my_function)
1308         {
1309           struct predicate p = false_predicate ();
1310           edge e;
1311           edge_iterator ei;
1312           FOR_EACH_EDGE (e, ei, bb->preds)
1313             {
1314               if (e->src->aux)
1315                 {
1316                   struct predicate this_bb_predicate = *(struct predicate *)e->src->aux;
1317                   if (e->aux)
1318                     this_bb_predicate = and_predicates (&this_bb_predicate,
1319                                                         (struct predicate *)e->aux);
1320                   p = or_predicates (&p, &this_bb_predicate);
1321                   if (true_predicate_p (&p))
1322                     break;
1323                 }
1324             }
1325           if (false_predicate_p (&p))
1326             gcc_assert (!bb->aux);
1327           else
1328             {
1329               if (!bb->aux)
1330                 {
1331                   done = false;
1332                   bb->aux = pool_alloc (edge_predicate_pool);
1333                   *((struct predicate *)bb->aux) = p;
1334                 }
1335               else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1336                 {
1337                   done = false;
1338                   *((struct predicate *)bb->aux) = p;
1339                 }
1340             }
1341         }
1342     }
1343 }
1344
1345
1346 /* We keep info about constantness of SSA names.  */
1347
1348 typedef struct predicate predicate_t;
1349 DEF_VEC_O (predicate_t);
1350 DEF_VEC_ALLOC_O (predicate_t, heap);
1351
1352
1353 /* Return predicate specifying when the STMT might have result that is not a compile
1354    time constant.  */
1355
1356 static struct predicate
1357 will_be_nonconstant_predicate (struct ipa_node_params *info,
1358                                struct inline_summary *summary,
1359                                gimple stmt,
1360                                VEC (predicate_t, heap) *nonconstant_names)
1361                               
1362 {
1363   struct predicate p = true_predicate ();
1364   ssa_op_iter iter;
1365   tree use;
1366   struct predicate op_non_const;
1367
1368   /* What statments might be optimized away
1369      when their arguments are constant
1370      TODO: also trivial builtins.
1371      builtin_constant_p is already handled later.  */
1372   if (gimple_code (stmt) != GIMPLE_ASSIGN
1373       && gimple_code (stmt) != GIMPLE_COND
1374       && gimple_code (stmt) != GIMPLE_SWITCH)
1375     return p;
1376
1377   /* Stores and loads will stay anyway.
1378      TODO: Constant memory accesses could be handled here, too.  */
1379   if (gimple_vuse (stmt))
1380     return p;
1381
1382   /* See if we understand all operands before we start
1383      adding conditionals.  */
1384   FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1385     {
1386       if (TREE_CODE (use) != SSA_NAME)
1387         return p;
1388       /* For arguments we can build a condition.  */
1389       if (SSA_NAME_IS_DEFAULT_DEF (use)
1390           && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
1391         continue;
1392       /* If we know when operand is constant,
1393          we still can say something useful.  */
1394       if (!true_predicate_p (VEC_index (predicate_t, nonconstant_names,
1395                                         SSA_NAME_VERSION (use))))
1396         continue;
1397       return p;
1398     }
1399   op_non_const = false_predicate ();
1400   FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1401     {
1402       if (SSA_NAME_IS_DEFAULT_DEF (use)
1403           && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
1404         p = add_condition (summary,
1405                            ipa_get_param_decl_index (info, SSA_NAME_VAR (use)),
1406                            IS_NOT_CONSTANT, NULL);
1407       else
1408         p = *VEC_index (predicate_t, nonconstant_names,
1409                         SSA_NAME_VERSION (use));
1410       op_non_const = or_predicates (&p, &op_non_const);
1411     }
1412   if (gimple_code (stmt) == GIMPLE_ASSIGN
1413       && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
1414     VEC_replace (predicate_t, nonconstant_names,
1415                  SSA_NAME_VERSION (gimple_assign_lhs (stmt)), &op_non_const);
1416   return op_non_const;
1417 }
1418
1419
1420 /* Compute function body size parameters for NODE.
1421    When EARLY is true, we compute only simple summaries without
1422    non-trivial predicates to drive the early inliner.  */
1423
1424 static void
1425 estimate_function_body_sizes (struct cgraph_node *node, bool early)
1426 {
1427   gcov_type time = 0;
1428   /* Estimate static overhead for function prologue/epilogue and alignment. */
1429   int size = 2;
1430   /* Benefits are scaled by probability of elimination that is in range
1431      <0,2>.  */
1432   basic_block bb;
1433   gimple_stmt_iterator bsi;
1434   struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1435   int freq;
1436   struct inline_summary *info = inline_summary (node);
1437   struct predicate bb_predicate;
1438   struct ipa_node_params *parms_info = NULL;
1439   VEC (predicate_t, heap) *nonconstant_names = NULL;
1440
1441   if (ipa_node_params_vector && !early && optimize)
1442     {
1443       parms_info = IPA_NODE_REF (node);
1444       VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
1445                              VEC_length (tree, SSANAMES (my_function)));
1446     }
1447
1448   info->conds = 0;
1449   info->entry = 0;
1450
1451
1452   if (dump_file)
1453     fprintf (dump_file, "\nAnalyzing function body size: %s\n",
1454              cgraph_node_name (node));
1455
1456   /* When we run into maximal number of entries, we assign everything to the
1457      constant truth case.  Be sure to have it in list. */
1458   bb_predicate = true_predicate ();
1459   account_size_time (info, 0, 0, &bb_predicate);
1460
1461   bb_predicate = not_inlined_predicate ();
1462   account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
1463
1464   gcc_assert (my_function && my_function->cfg);
1465   if (parms_info)
1466     compute_bb_predicates (node, parms_info, info);
1467   FOR_EACH_BB_FN (bb, my_function)
1468     {
1469       freq = compute_call_stmt_bb_frequency (node->decl, bb);
1470
1471       /* TODO: Obviously predicates can be propagated down across CFG.  */
1472       if (parms_info)
1473         {
1474           if (bb->aux)
1475             bb_predicate = *(struct predicate *)bb->aux;
1476           else
1477             bb_predicate = false_predicate ();
1478         }
1479       else
1480         bb_predicate = true_predicate ();
1481
1482       if (dump_file && (dump_flags & TDF_DETAILS))
1483         {
1484           fprintf (dump_file, "\n BB %i predicate:", bb->index);
1485           dump_predicate (dump_file, info->conds, &bb_predicate);
1486         }
1487       
1488       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1489         {
1490           gimple stmt = gsi_stmt (bsi);
1491           int this_size = estimate_num_insns (stmt, &eni_size_weights);
1492           int this_time = estimate_num_insns (stmt, &eni_time_weights);
1493           int prob;
1494           struct predicate will_be_nonconstant;
1495
1496           if (dump_file && (dump_flags & TDF_DETAILS))
1497             {
1498               fprintf (dump_file, "  ");
1499               print_gimple_stmt (dump_file, stmt, 0, 0);
1500               fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
1501                        ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
1502             }
1503
1504           if (is_gimple_call (stmt))
1505             {
1506               struct cgraph_edge *edge = cgraph_edge (node, stmt);
1507               struct inline_edge_summary *es = inline_edge_summary (edge);
1508
1509               /* Special case: results of BUILT_IN_CONSTANT_P will be always
1510                  resolved as constant.  We however don't want to optimize
1511                  out the cgraph edges.  */
1512               if (nonconstant_names
1513                   && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
1514                   && gimple_call_lhs (stmt)
1515                   && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
1516                 {
1517                   struct predicate false_p = false_predicate ();
1518                   VEC_replace (predicate_t, nonconstant_names,
1519                                SSA_NAME_VERSION (gimple_call_lhs (stmt)), &false_p);
1520                 }
1521
1522               es->call_stmt_size = this_size;
1523               es->call_stmt_time = this_time;
1524               es->loop_depth = bb->loop_depth;
1525               edge_set_predicate (edge, &bb_predicate);
1526
1527               /* Do not inline calls where we cannot triviall work around
1528                  mismatches in argument or return types.  */
1529               if (edge->callee
1530                   && cgraph_function_or_thunk_node (edge->callee, NULL)
1531                   && !gimple_check_call_matching_types (stmt,
1532                                                         cgraph_function_or_thunk_node (edge->callee,
1533                                                                                        NULL)->decl))
1534                 {
1535                   edge->call_stmt_cannot_inline_p = true;
1536                   gimple_call_set_cannot_inline (stmt, true);
1537                 }
1538               else
1539                 gcc_assert (!gimple_call_cannot_inline_p (stmt));
1540             }
1541
1542           /* TODO: When conditional jump or swithc is known to be constant, but
1543              we did not translate it into the predicates, we really can account
1544              just maximum of the possible paths.  */
1545           if (parms_info)
1546             will_be_nonconstant
1547                = will_be_nonconstant_predicate (parms_info, info,
1548                                                 stmt, nonconstant_names);
1549           if (this_time || this_size)
1550             {
1551               struct predicate p;
1552
1553               this_time *= freq;
1554               time += this_time;
1555               size += this_size;
1556
1557               prob = eliminated_by_inlining_prob (stmt);
1558               if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1559                 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
1560               if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1561                 fprintf (dump_file, "\t\twill eliminated by inlining\n");
1562
1563               if (parms_info)
1564                 p = and_predicates (&bb_predicate, &will_be_nonconstant);
1565               else
1566                 p = true_predicate ();
1567
1568               /* We account everything but the calls.  Calls have their own
1569                  size/time info attached to cgraph edges.  This is neccesary
1570                  in order to make the cost disappear after inlining.  */
1571               if (!is_gimple_call (stmt))
1572                 {
1573                   if (prob)
1574                     {
1575                       struct predicate ip = not_inlined_predicate ();
1576                       ip = and_predicates (&ip, &p);
1577                       account_size_time (info, this_size * prob,
1578                                          this_time * prob, &ip);
1579                     }
1580                   if (prob != 2)
1581                     account_size_time (info, this_size * (2 - prob),
1582                                        this_time * (2 - prob), &p);
1583                 }
1584
1585               gcc_assert (time >= 0);
1586               gcc_assert (size >= 0);
1587             }
1588         }
1589     }
1590   FOR_ALL_BB_FN (bb, my_function)
1591     {
1592       edge e;
1593       edge_iterator ei;
1594
1595       if (bb->aux)
1596         pool_free (edge_predicate_pool, bb->aux);
1597       bb->aux = NULL;
1598       FOR_EACH_EDGE (e, ei, bb->succs)
1599         {
1600           if (e->aux)
1601             pool_free (edge_predicate_pool, e->aux);
1602           e->aux = NULL;
1603         }
1604     }
1605   time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1606   if (time > MAX_TIME)
1607     time = MAX_TIME;
1608   inline_summary (node)->self_time = time;
1609   inline_summary (node)->self_size = size;
1610   VEC_free (predicate_t, heap, nonconstant_names);
1611   if (dump_file)
1612     {
1613       fprintf (dump_file, "\n");
1614       dump_inline_summary (dump_file, node);
1615     }
1616 }
1617
1618
1619 /* Compute parameters of functions used by inliner.
1620    EARLY is true when we compute parameters for the early inliner  */
1621
1622 void
1623 compute_inline_parameters (struct cgraph_node *node, bool early)
1624 {
1625   HOST_WIDE_INT self_stack_size;
1626   struct cgraph_edge *e;
1627   struct inline_summary *info;
1628
1629   gcc_assert (!node->global.inlined_to);
1630
1631   inline_summary_alloc ();
1632
1633   info = inline_summary (node);
1634
1635   /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
1636      Once this happen, we will need to more curefully predict call
1637      statement size.  */
1638   if (node->thunk.thunk_p)
1639     {
1640       struct inline_edge_summary *es = inline_edge_summary (node->callees);
1641       struct predicate t = true_predicate ();
1642
1643       info->inlinable = info->versionable = 0;
1644       node->callees->call_stmt_cannot_inline_p = true;
1645       node->local.can_change_signature = false;
1646       es->call_stmt_time = 1;
1647       es->call_stmt_size = 1;
1648       account_size_time (info, 0, 0, &t);
1649       return;
1650     }
1651
1652   /* Estimate the stack size for the function if we're optimizing.  */
1653   self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
1654   info->estimated_self_stack_size = self_stack_size;
1655   info->estimated_stack_size = self_stack_size;
1656   info->stack_frame_offset = 0;
1657
1658   /* Can this function be inlined at all?  */
1659   info->inlinable = tree_inlinable_function_p (node->decl);
1660
1661   /* Inlinable functions always can change signature.  */
1662   if (info->inlinable)
1663     node->local.can_change_signature = true;
1664   else
1665     {
1666       /* Functions calling builtin_apply can not change signature.  */
1667       for (e = node->callees; e; e = e->next_callee)
1668         if (DECL_BUILT_IN (e->callee->decl)
1669             && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
1670             && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_APPLY_ARGS)
1671           break;
1672       node->local.can_change_signature = !e;
1673     }
1674   estimate_function_body_sizes (node, early);
1675
1676   /* Inlining characteristics are maintained by the cgraph_mark_inline.  */
1677   info->time = info->self_time;
1678   info->size = info->self_size;
1679   info->stack_frame_offset = 0;
1680   info->estimated_stack_size = info->estimated_self_stack_size;
1681 }
1682
1683
1684 /* Compute parameters of functions used by inliner using
1685    current_function_decl.  */
1686
1687 static unsigned int
1688 compute_inline_parameters_for_current (void)
1689 {
1690   compute_inline_parameters (cgraph_get_node (current_function_decl), true);
1691   return 0;
1692 }
1693
1694 struct gimple_opt_pass pass_inline_parameters =
1695 {
1696  {
1697   GIMPLE_PASS,
1698   "inline_param",                       /* name */
1699   NULL,                                 /* gate */
1700   compute_inline_parameters_for_current,/* execute */
1701   NULL,                                 /* sub */
1702   NULL,                                 /* next */
1703   0,                                    /* static_pass_number */
1704   TV_INLINE_HEURISTICS,                 /* tv_id */
1705   0,                                    /* properties_required */
1706   0,                                    /* properties_provided */
1707   0,                                    /* properties_destroyed */
1708   0,                                    /* todo_flags_start */
1709   0                                     /* todo_flags_finish */
1710  }
1711 };
1712
1713
1714 /* Increase SIZE and TIME for size and time needed to handle edge E.  */
1715
1716 static void
1717 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time)
1718 {
1719   struct inline_edge_summary *es = inline_edge_summary (e);
1720   *size += es->call_stmt_size * INLINE_SIZE_SCALE;
1721   *time += (es->call_stmt_time
1722             * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
1723   if (*time > MAX_TIME * INLINE_TIME_SCALE)
1724     *time = MAX_TIME * INLINE_TIME_SCALE;
1725 }
1726
1727
1728 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.  */
1729
1730 static void
1731 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
1732                               clause_t possible_truths)
1733 {
1734   struct cgraph_edge *e;
1735   for (e = node->callees; e; e = e->next_callee)
1736     {
1737       struct inline_edge_summary *es = inline_edge_summary (e);
1738       if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1739         {
1740           if (e->inline_failed)
1741             estimate_edge_size_and_time (e, size, time);
1742           else
1743             estimate_calls_size_and_time (e->callee, size, time,
1744                                           possible_truths);
1745         }
1746     }
1747   /* TODO: look for devirtualizing oppurtunities.  */
1748   for (e = node->indirect_calls; e; e = e->next_callee)
1749     {
1750       struct inline_edge_summary *es = inline_edge_summary (e);
1751       if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1752         estimate_edge_size_and_time (e, size, time);
1753     }
1754 }
1755
1756
1757 /* Estimate size and time needed to execute NODE assuming
1758    POSSIBLE_TRUTHS clause. */
1759
1760 static void
1761 estimate_node_size_and_time (struct cgraph_node *node,
1762                              clause_t possible_truths,
1763                              int *ret_size, int *ret_time)
1764 {
1765   struct inline_summary *info = inline_summary (node);
1766   size_time_entry *e;
1767   int size = 0, time = 0;
1768   int i;
1769
1770   if (dump_file
1771       && (dump_flags & TDF_DETAILS))
1772     {
1773       bool found = false;
1774       fprintf (dump_file, "   Estimating body: %s/%i\n"
1775                           "   Known to be false: ",
1776                cgraph_node_name (node),
1777                node->uid);
1778
1779       for (i = predicate_not_inlined_condition;
1780            i < (predicate_first_dynamic_condition
1781                 + (int)VEC_length (condition, info->conds)); i++)
1782         if (!(possible_truths & (1 << i)))
1783           {
1784             if (found)
1785               fprintf (dump_file, ", ");
1786             found = true;
1787             dump_condition (dump_file, info->conds, i);
1788           }
1789     }
1790
1791   for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
1792     if (evaluate_predicate (&e->predicate, possible_truths))
1793       time += e->time, size += e->size;
1794
1795   if (time > MAX_TIME * INLINE_TIME_SCALE)
1796     time = MAX_TIME * INLINE_TIME_SCALE;
1797
1798   estimate_calls_size_and_time (node, &size, &time, possible_truths);
1799   time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
1800   size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
1801
1802
1803   if (dump_file
1804       && (dump_flags & TDF_DETAILS))
1805     fprintf (dump_file, "\n   size:%i time:%i\n", size, time);
1806   if (ret_time)
1807     *ret_time = time;
1808   if (ret_size)
1809     *ret_size = size;
1810   return;
1811 }
1812
1813
1814 /* Estimate size and time needed to execute callee of EDGE assuming that
1815    parameters known to be constant at caller of EDGE are propagated.
1816    KNOWN_VALs is a vector of assumed known constant values for parameters.  */
1817
1818 void
1819 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
1820                                    VEC (tree, heap) *known_vals,
1821                                    int *ret_size, int *ret_time)
1822 {
1823   clause_t clause;
1824
1825   clause = evaluate_conditions_for_known_args (node, false, known_vals);
1826   estimate_node_size_and_time (node, clause, ret_size, ret_time);
1827 }
1828
1829
1830 /* Translate all conditions from callee representation into caller representation and
1831    symbolically evaluate predicate P into new predicate.
1832
1833    INFO is inline_summary of function we are adding predicate into, CALLEE_INFO is summary
1834    of function predicate P is from. OPERAND_MAP is array giving callee formal IDs the
1835    caller formal IDs. POSSSIBLE_TRUTHS is clausule of all callee conditions that
1836    may be true in caller context. TOPLEV_PREDICATE is predicate under which callee
1837    is executed.  */
1838
1839 static struct predicate
1840 remap_predicate (struct inline_summary *info, struct inline_summary *callee_info,
1841                  struct predicate *p,
1842                  VEC (int, heap) *operand_map,
1843                  clause_t possible_truths,
1844                  struct predicate *toplev_predicate)
1845 {
1846   int i;
1847   struct predicate out = true_predicate ();
1848
1849   /* True predicate is easy.  */
1850   if (true_predicate_p (p))
1851     return *toplev_predicate;
1852   for (i = 0; p->clause[i]; i++)
1853     {
1854       clause_t clause = p->clause[i];
1855       int cond;
1856       struct predicate clause_predicate = false_predicate ();
1857
1858       gcc_assert (i < MAX_CLAUSES);
1859
1860       for (cond = 0; cond < NUM_CONDITIONS; cond ++)
1861         /* Do we have condition we can't disprove?   */
1862         if (clause & possible_truths & (1 << cond))
1863           {
1864             struct predicate cond_predicate;
1865             /* Work out if the condition can translate to predicate in the
1866                inlined function.  */
1867             if (cond >= predicate_first_dynamic_condition)
1868               {
1869                  struct condition *c;
1870
1871                  c = VEC_index (condition, callee_info->conds,
1872                                 cond - predicate_first_dynamic_condition);
1873                  /* See if we can remap condition operand to caller's operand.
1874                     Otherwise give up.  */
1875                  if (!operand_map
1876                      || VEC_index (int, operand_map, c->operand_num) == -1)
1877                    cond_predicate = true_predicate ();
1878                  else
1879                    cond_predicate = add_condition (info,
1880                                                    VEC_index (int, operand_map,
1881                                                               c->operand_num),
1882                                                    c->code, c->val);
1883               }
1884             /* Fixed conditions remains same, construct single
1885                condition predicate.  */
1886             else
1887               {
1888                 cond_predicate.clause[0] = 1 << cond;
1889                 cond_predicate.clause[1] = 0;
1890               }
1891             clause_predicate = or_predicates (&clause_predicate, &cond_predicate);
1892           }
1893       out = and_predicates (&out, &clause_predicate);
1894     }
1895   return and_predicates (&out, toplev_predicate);
1896 }
1897
1898
1899 /* Update summary information of inline clones after inlining.
1900    Compute peak stack usage.  */
1901
1902 static void
1903 inline_update_callee_summaries (struct cgraph_node *node,
1904                                 int depth)
1905 {
1906   struct cgraph_edge *e;
1907   struct inline_summary *callee_info = inline_summary (node);
1908   struct inline_summary *caller_info = inline_summary (node->callers->caller);
1909   HOST_WIDE_INT peak;
1910
1911   callee_info->stack_frame_offset
1912     = caller_info->stack_frame_offset
1913       + caller_info->estimated_self_stack_size;
1914   peak = callee_info->stack_frame_offset
1915       + callee_info->estimated_self_stack_size;
1916   if (inline_summary (node->global.inlined_to)->estimated_stack_size
1917       < peak)
1918     inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
1919   cgraph_propagate_frequency (node);
1920   for (e = node->callees; e; e = e->next_callee)
1921     {
1922       if (!e->inline_failed)
1923         inline_update_callee_summaries (e->callee, depth);
1924       inline_edge_summary (e)->loop_depth += depth;
1925     }
1926   for (e = node->indirect_calls; e; e = e->next_callee)
1927     inline_edge_summary (e)->loop_depth += depth;
1928 }
1929
1930
1931 /* Remap predicates of callees of NODE.  Rest of arguments match
1932    remap_predicate.  */
1933
1934 static void
1935 remap_edge_predicates (struct cgraph_node *node,
1936                        struct inline_summary *info,
1937                        struct inline_summary *callee_info,
1938                        VEC (int, heap) *operand_map,
1939                        clause_t possible_truths,
1940                        struct predicate *toplev_predicate)
1941 {
1942   struct cgraph_edge *e;
1943   for (e = node->callees; e; e = e->next_callee)
1944     {
1945       struct inline_edge_summary *es = inline_edge_summary (e);
1946       struct predicate p;
1947       if (es->predicate)
1948         {
1949           p = remap_predicate (info, callee_info,
1950                                es->predicate, operand_map, possible_truths,
1951                                toplev_predicate);
1952           edge_set_predicate (e, &p);
1953           /* TODO: We should remove the edge for code that will be optimized out,
1954              but we need to keep verifiers and tree-inline happy.
1955              Make it cold for now.  */
1956           if (false_predicate_p (&p))
1957             {
1958               e->count = 0;
1959               e->frequency = 0;
1960             }
1961         }
1962       if (!e->inline_failed)
1963         remap_edge_predicates (e->callee, info, callee_info, operand_map,
1964                                possible_truths, toplev_predicate);
1965       else
1966         edge_set_predicate (e, toplev_predicate);
1967     }
1968   for (e = node->indirect_calls; e; e = e->next_callee)
1969     {
1970       struct inline_edge_summary *es = inline_edge_summary (e);
1971       struct predicate p;
1972       if (es->predicate)
1973         {
1974           p = remap_predicate (info, callee_info,
1975                                es->predicate, operand_map, possible_truths,
1976                                toplev_predicate);
1977           edge_set_predicate (e, &p);
1978           /* TODO: We should remove the edge for code that will be optimized out,
1979              but we need to keep verifiers and tree-inline happy.
1980              Make it cold for now.  */
1981           if (false_predicate_p (&p))
1982             {
1983               e->count = 0;
1984               e->frequency = 0;
1985             }
1986         }
1987       else
1988         edge_set_predicate (e, toplev_predicate);
1989     }
1990 }
1991
1992
1993 /* We inlined EDGE.  Update summary of the function we inlined into.  */
1994
1995 void
1996 inline_merge_summary (struct cgraph_edge *edge)
1997 {
1998   struct inline_summary *callee_info = inline_summary (edge->callee);
1999   struct cgraph_node *to = (edge->caller->global.inlined_to
2000                             ? edge->caller->global.inlined_to : edge->caller);
2001   struct inline_summary *info = inline_summary (to);
2002   clause_t clause = 0;          /* not_inline is known to be false.  */
2003   size_time_entry *e;
2004   VEC (int, heap) *operand_map = NULL;
2005   int i;
2006   struct predicate toplev_predicate;
2007   struct inline_edge_summary *es = inline_edge_summary (edge);
2008
2009   if (es->predicate)
2010     toplev_predicate = *es->predicate;
2011   else
2012     toplev_predicate = true_predicate ();
2013
2014   if (ipa_node_params_vector && callee_info->conds
2015       /* FIXME: it seems that we forget to get argument count in some cases,
2016          probaby for previously indirect edges or so.
2017          Removing the test leads to ICE on tramp3d.  */
2018       && ipa_get_cs_argument_count (IPA_EDGE_REF (edge)))
2019     {
2020       struct ipa_edge_args *args = IPA_EDGE_REF (edge);
2021       int count = ipa_get_cs_argument_count (args);
2022       int i;
2023
2024       clause = evaluate_conditions_for_edge (edge, true);
2025       VEC_safe_grow_cleared (int, heap, operand_map, count);
2026       for (i = 0; i < count; i++)
2027         {
2028           struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
2029           int map = -1;
2030           /* TODO: handle non-NOPs when merging.  */
2031           if (jfunc->type == IPA_JF_PASS_THROUGH
2032               && jfunc->value.pass_through.operation == NOP_EXPR)
2033             map = jfunc->value.pass_through.formal_id;
2034           VEC_replace (int, operand_map, i, map);
2035           gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
2036         }
2037     }
2038   for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
2039     {
2040       struct predicate p = remap_predicate (info, callee_info,
2041                                             &e->predicate, operand_map, clause,
2042                                             &toplev_predicate);
2043       gcov_type add_time = ((gcov_type)e->time * edge->frequency
2044                             + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2045       if (add_time > MAX_TIME)
2046         add_time = MAX_TIME;
2047       account_size_time (info, e->size, add_time, &p);
2048     }
2049   remap_edge_predicates (edge->callee, info, callee_info, operand_map,
2050                          clause, &toplev_predicate);
2051   info->size = 0;
2052   info->time = 0;
2053   for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2054     info->size += e->size, info->time += e->time;
2055   estimate_calls_size_and_time (to, &info->size, &info->time,
2056                                 ~(clause_t)(1 << predicate_false_condition));
2057
2058   inline_update_callee_summaries (edge->callee,
2059                                   inline_edge_summary (edge)->loop_depth);
2060
2061   info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
2062   info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
2063 }
2064
2065
2066 /* Estimate the time cost for the caller when inlining EDGE.
2067    Only to be called via estimate_edge_time, that handles the
2068    caching mechanism.
2069
2070    When caching, also update the cache entry.  Compute both time and
2071    size, since we always need both metrics eventually.  */
2072
2073 int
2074 do_estimate_edge_time (struct cgraph_edge *edge)
2075 {
2076   int time;
2077   int size;
2078   gcov_type ret;
2079   struct inline_edge_summary *es = inline_edge_summary (edge);
2080
2081   gcc_checking_assert (edge->inline_failed);
2082   estimate_node_size_and_time (cgraph_function_or_thunk_node (edge->callee, NULL),
2083                                evaluate_conditions_for_edge (edge, true),
2084                                &size, &time);
2085
2086   ret = (((gcov_type)time - es->call_stmt_time) * edge->frequency
2087          + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2088   if (ret > MAX_TIME)
2089     ret = MAX_TIME;
2090
2091   /* When caching, update the cache entry.  */
2092   if (edge_growth_cache)
2093     {
2094       int ret_size;
2095       if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
2096           <= edge->uid)
2097         VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
2098                                cgraph_edge_max_uid);
2099       VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->time
2100         = ret + (ret >= 0);
2101
2102       ret_size = size - es->call_stmt_size;
2103       gcc_checking_assert (es->call_stmt_size);
2104       VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->size
2105         = ret_size + (ret_size >= 0);
2106     }
2107   return ret;
2108 }
2109
2110
2111 /* Estimate the growth of the caller when inlining EDGE.
2112    Only to be called via estimate_edge_size.  */
2113
2114 int
2115 do_estimate_edge_growth (struct cgraph_edge *edge)
2116 {
2117   int size;
2118   struct cgraph_node *callee;
2119
2120   /* When we do caching, use do_estimate_edge_time to populate the entry.  */
2121
2122   if (edge_growth_cache)
2123     {
2124       do_estimate_edge_time (edge);
2125       size = VEC_index (edge_growth_cache_entry,
2126                         edge_growth_cache,
2127                         edge->uid)->size;
2128       gcc_checking_assert (size);
2129       return size - (size > 0);
2130     }
2131   callee = cgraph_function_or_thunk_node (edge->callee, NULL);
2132
2133   /* Early inliner runs without caching, go ahead and do the dirty work.  */
2134   gcc_checking_assert (edge->inline_failed);
2135   estimate_node_size_and_time (callee,
2136                                evaluate_conditions_for_edge (edge, true),
2137                                &size, NULL);
2138   gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
2139   return size - inline_edge_summary (edge)->call_stmt_size;
2140 }
2141
2142
2143 /* Estimate self time of the function NODE after inlining EDGE.  */
2144
2145 int
2146 estimate_time_after_inlining (struct cgraph_node *node,
2147                               struct cgraph_edge *edge)
2148 {
2149   struct inline_edge_summary *es = inline_edge_summary (edge);
2150   if (!es->predicate || !false_predicate_p (es->predicate))
2151     {
2152       gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
2153       if (time < 0)
2154         time = 0;
2155       if (time > MAX_TIME)
2156         time = MAX_TIME;
2157       return time;
2158     }
2159   return inline_summary (node)->time;
2160 }
2161
2162
2163 /* Estimate the size of NODE after inlining EDGE which should be an
2164    edge to either NODE or a call inlined into NODE.  */
2165
2166 int
2167 estimate_size_after_inlining (struct cgraph_node *node,
2168                               struct cgraph_edge *edge)
2169 {
2170   struct inline_edge_summary *es = inline_edge_summary (edge);
2171   if (!es->predicate || !false_predicate_p (es->predicate))
2172     {
2173       int size = inline_summary (node)->size + estimate_edge_growth (edge);
2174       gcc_assert (size >= 0);
2175       return size;
2176     }
2177   return inline_summary (node)->size;
2178 }
2179
2180
2181 struct growth_data
2182 {
2183   bool self_recursive;
2184   int growth;
2185 };
2186
2187
2188 /* Worker for do_estimate_growth.  Collect growth for all callers.  */
2189
2190 static bool
2191 do_estimate_growth_1 (struct cgraph_node *node, void *data)
2192 {
2193   struct cgraph_edge *e;
2194   struct growth_data *d = (struct growth_data *) data;
2195
2196   for (e = node->callers; e; e = e->next_caller)
2197     {
2198       gcc_checking_assert (e->inline_failed);
2199
2200       if (e->caller == node
2201           || (e->caller->global.inlined_to
2202               && e->caller->global.inlined_to == node))
2203         d->self_recursive = true;
2204       d->growth += estimate_edge_growth (e);
2205     }
2206   return false;
2207 }
2208
2209
2210 /* Estimate the growth caused by inlining NODE into all callees.  */
2211
2212 int
2213 do_estimate_growth (struct cgraph_node *node)
2214 {
2215   struct growth_data d = {0, false};
2216   struct inline_summary *info = inline_summary (node);
2217
2218   cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
2219
2220   /* For self recursive functions the growth estimation really should be
2221      infinity.  We don't want to return very large values because the growth
2222      plays various roles in badness computation fractions.  Be sure to not
2223      return zero or negative growths. */
2224   if (d.self_recursive)
2225     d.growth = d.growth < info->size ? info->size : d.growth;
2226   else
2227     {
2228       if (!DECL_EXTERNAL (node->decl)
2229           && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
2230         d.growth -= info->size;
2231       /* COMDAT functions are very often not shared across multiple units since they
2232          come from various template instantiations.  Take this into account.  */
2233       else  if (DECL_COMDAT (node->decl)
2234                 && cgraph_can_remove_if_no_direct_calls_p (node))
2235         d.growth -= (info->size
2236                      * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY)) + 50) / 100;
2237     }
2238
2239   if (node_growth_cache)
2240     {
2241       if ((int)VEC_length (int, node_growth_cache) <= node->uid)
2242         VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
2243       VEC_replace (int, node_growth_cache, node->uid, d.growth + (d.growth >= 0));
2244     }
2245   return d.growth;
2246 }
2247
2248
2249 /* This function performs intraprocedural analysis in NODE that is required to
2250    inline indirect calls.  */
2251
2252 static void
2253 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
2254 {
2255   ipa_analyze_node (node);
2256   if (dump_file && (dump_flags & TDF_DETAILS))
2257     {
2258       ipa_print_node_params (dump_file, node);
2259       ipa_print_node_jump_functions (dump_file, node);
2260     }
2261 }
2262
2263
2264 /* Note function body size.  */
2265
2266 static void
2267 inline_analyze_function (struct cgraph_node *node)
2268 {
2269   push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2270   current_function_decl = node->decl;
2271
2272   if (dump_file)
2273     fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
2274              cgraph_node_name (node), node->uid);
2275   /* FIXME: We should remove the optimize check after we ensure we never run
2276      IPA passes when not optimizing.  */
2277   if (flag_indirect_inlining && optimize && !node->thunk.thunk_p)
2278     inline_indirect_intraprocedural_analysis (node);
2279   compute_inline_parameters (node, false);
2280
2281   current_function_decl = NULL;
2282   pop_cfun ();
2283 }
2284
2285
2286 /* Called when new function is inserted to callgraph late.  */
2287
2288 static void
2289 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2290 {
2291   inline_analyze_function (node);
2292 }
2293
2294
2295 /* Note function body size.  */
2296
2297 void
2298 inline_generate_summary (void)
2299 {
2300   struct cgraph_node *node;
2301
2302   function_insertion_hook_holder =
2303       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2304
2305   if (flag_indirect_inlining)
2306     ipa_register_cgraph_hooks ();
2307
2308   FOR_EACH_DEFINED_FUNCTION (node)
2309     if (!node->alias)
2310       inline_analyze_function (node);
2311 }
2312
2313
2314 /* Read predicate from IB.  */
2315
2316 static struct predicate
2317 read_predicate (struct lto_input_block *ib)
2318 {
2319   struct predicate out;
2320   clause_t clause;
2321   int k = 0;
2322
2323   do 
2324     {
2325       gcc_assert (k <= MAX_CLAUSES);
2326       clause = out.clause[k++] = lto_input_uleb128 (ib);
2327     }
2328   while (clause);
2329
2330   /* Zero-initialize the remaining clauses in OUT.  */
2331   while (k <= MAX_CLAUSES)
2332     out.clause[k++] = 0;
2333
2334   return out;
2335 }
2336
2337
2338 /* Write inline summary for edge E to OB.  */
2339
2340 static void
2341 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
2342 {
2343   struct inline_edge_summary *es = inline_edge_summary (e);
2344   struct predicate p;
2345
2346   es->call_stmt_size = lto_input_uleb128 (ib);
2347   es->call_stmt_time = lto_input_uleb128 (ib);
2348   es->loop_depth = lto_input_uleb128 (ib);
2349   p = read_predicate (ib);
2350   edge_set_predicate (e, &p);
2351 }
2352
2353
2354 /* Stream in inline summaries from the section.  */
2355
2356 static void
2357 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
2358                      size_t len)
2359 {
2360   const struct lto_function_header *header =
2361     (const struct lto_function_header *) data;
2362   const int32_t cfg_offset = sizeof (struct lto_function_header);
2363   const int32_t main_offset = cfg_offset + header->cfg_size;
2364   const int32_t string_offset = main_offset + header->main_size;
2365   struct data_in *data_in;
2366   struct lto_input_block ib;
2367   unsigned int i, count2, j;
2368   unsigned int f_count;
2369
2370   LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
2371                         header->main_size);
2372
2373   data_in =
2374     lto_data_in_create (file_data, (const char *) data + string_offset,
2375                         header->string_size, NULL);
2376   f_count = lto_input_uleb128 (&ib);
2377   for (i = 0; i < f_count; i++)
2378     {
2379       unsigned int index;
2380       struct cgraph_node *node;
2381       struct inline_summary *info;
2382       lto_cgraph_encoder_t encoder;
2383       struct bitpack_d bp;
2384       struct cgraph_edge *e;
2385
2386       index = lto_input_uleb128 (&ib);
2387       encoder = file_data->cgraph_node_encoder;
2388       node = lto_cgraph_encoder_deref (encoder, index);
2389       info = inline_summary (node);
2390
2391       info->estimated_stack_size
2392         = info->estimated_self_stack_size = lto_input_uleb128 (&ib);
2393       info->size = info->self_size = lto_input_uleb128 (&ib);
2394       info->time = info->self_time = lto_input_uleb128 (&ib);
2395
2396       bp = lto_input_bitpack (&ib);
2397       info->inlinable = bp_unpack_value (&bp, 1);
2398       info->versionable = bp_unpack_value (&bp, 1);
2399
2400       count2 = lto_input_uleb128 (&ib);
2401       gcc_assert (!info->conds);
2402       for (j = 0; j < count2; j++)
2403         {
2404           struct condition c;
2405           c.operand_num = lto_input_uleb128 (&ib);
2406           c.code = (enum tree_code) lto_input_uleb128 (&ib);
2407           c.val = lto_input_tree (&ib, data_in);
2408           VEC_safe_push (condition, gc, info->conds, &c);
2409         }
2410       count2 = lto_input_uleb128 (&ib);
2411       gcc_assert (!info->entry);
2412       for (j = 0; j < count2; j++)
2413         {
2414           struct size_time_entry e;
2415
2416           e.size = lto_input_uleb128 (&ib);
2417           e.time = lto_input_uleb128 (&ib);
2418           e.predicate = read_predicate (&ib);
2419
2420           VEC_safe_push (size_time_entry, gc, info->entry, &e);
2421         }
2422       for (e = node->callees; e; e = e->next_callee)
2423         read_inline_edge_summary (&ib, e);
2424       for (e = node->indirect_calls; e; e = e->next_callee)
2425         read_inline_edge_summary (&ib, e);
2426     }
2427
2428   lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
2429                          len);
2430   lto_data_in_delete (data_in);
2431 }
2432
2433
2434 /* Read inline summary.  Jump functions are shared among ipa-cp
2435    and inliner, so when ipa-cp is active, we don't need to write them
2436    twice.  */
2437
2438 void
2439 inline_read_summary (void)
2440 {
2441   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2442   struct lto_file_decl_data *file_data;
2443   unsigned int j = 0;
2444
2445   inline_summary_alloc ();
2446
2447   while ((file_data = file_data_vec[j++]))
2448     {
2449       size_t len;
2450       const char *data = lto_get_section_data (file_data, LTO_section_inline_summary, NULL, &len);
2451       if (data)
2452         inline_read_section (file_data, data, len);
2453       else
2454         /* Fatal error here.  We do not want to support compiling ltrans units with
2455            different version of compiler or different flags than the WPA unit, so
2456            this should never happen.  */
2457         fatal_error ("ipa inline summary is missing in input file");
2458     }
2459   if (flag_indirect_inlining)
2460     {
2461       ipa_register_cgraph_hooks ();
2462       if (!flag_ipa_cp)
2463         ipa_prop_read_jump_functions ();
2464     }
2465   function_insertion_hook_holder =
2466       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2467 }
2468
2469
2470 /* Write predicate P to OB.  */
2471
2472 static void
2473 write_predicate (struct output_block *ob, struct predicate *p)
2474 {
2475   int j;
2476   if (p)
2477     for (j = 0; p->clause[j]; j++)
2478       {
2479          gcc_assert (j < MAX_CLAUSES);
2480          lto_output_uleb128_stream (ob->main_stream,
2481                                     p->clause[j]);
2482       }
2483   lto_output_uleb128_stream (ob->main_stream, 0);
2484 }
2485
2486
2487 /* Write inline summary for edge E to OB.  */
2488
2489 static void
2490 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
2491 {
2492   struct inline_edge_summary *es = inline_edge_summary (e);
2493   lto_output_uleb128_stream (ob->main_stream, es->call_stmt_size);
2494   lto_output_uleb128_stream (ob->main_stream, es->call_stmt_time);
2495   lto_output_uleb128_stream (ob->main_stream, es->loop_depth);
2496   write_predicate (ob, es->predicate);
2497 }
2498
2499
2500 /* Write inline summary for node in SET.
2501    Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2502    active, we don't need to write them twice.  */
2503
2504 void
2505 inline_write_summary (cgraph_node_set set,
2506                       varpool_node_set vset ATTRIBUTE_UNUSED)
2507 {
2508   struct cgraph_node *node;
2509   struct output_block *ob = create_output_block (LTO_section_inline_summary);
2510   lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2511   unsigned int count = 0;
2512   int i;
2513
2514   for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2515     if (lto_cgraph_encoder_deref (encoder, i)->analyzed)
2516       count++;
2517   lto_output_uleb128_stream (ob->main_stream, count);
2518
2519   for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2520     {
2521       node = lto_cgraph_encoder_deref (encoder, i);
2522       if (node->analyzed)
2523         {
2524           struct inline_summary *info = inline_summary (node);
2525           struct bitpack_d bp;
2526           struct cgraph_edge *edge;
2527           int i;
2528           size_time_entry *e;
2529           struct condition *c;
2530           
2531
2532           lto_output_uleb128_stream (ob->main_stream,
2533                                      lto_cgraph_encoder_encode (encoder, node));
2534           lto_output_sleb128_stream (ob->main_stream,
2535                                      info->estimated_self_stack_size);
2536           lto_output_sleb128_stream (ob->main_stream,
2537                                      info->self_size);
2538           lto_output_sleb128_stream (ob->main_stream,
2539                                      info->self_time);
2540           bp = bitpack_create (ob->main_stream);
2541           bp_pack_value (&bp, info->inlinable, 1);
2542           bp_pack_value (&bp, info->versionable, 1);
2543           lto_output_bitpack (&bp);
2544           lto_output_uleb128_stream (ob->main_stream,
2545                                      VEC_length (condition, info->conds));
2546           for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
2547             {
2548               lto_output_uleb128_stream (ob->main_stream,
2549                                          c->operand_num);
2550               lto_output_uleb128_stream (ob->main_stream,
2551                                          c->code);
2552               lto_output_tree (ob, c->val, true);
2553             }
2554           lto_output_uleb128_stream (ob->main_stream,
2555                                      VEC_length (size_time_entry, info->entry));
2556           for (i = 0;
2557                VEC_iterate (size_time_entry, info->entry, i, e);
2558                i++)
2559             {
2560               lto_output_uleb128_stream (ob->main_stream,
2561                                          e->size);
2562               lto_output_uleb128_stream (ob->main_stream,
2563                                          e->time);
2564               write_predicate (ob, &e->predicate);
2565             }
2566           for (edge = node->callees; edge; edge = edge->next_callee)
2567             write_inline_edge_summary (ob, edge);
2568           for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2569             write_inline_edge_summary (ob, edge);
2570         }
2571     }
2572   lto_output_1_stream (ob->main_stream, 0);
2573   produce_asm (ob, NULL);
2574   destroy_output_block (ob);
2575
2576   if (flag_indirect_inlining && !flag_ipa_cp)
2577     ipa_prop_write_jump_functions (set);
2578 }
2579
2580
2581 /* Release inline summary.  */
2582
2583 void
2584 inline_free_summary (void)
2585 {
2586   if (function_insertion_hook_holder)
2587     cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2588   function_insertion_hook_holder = NULL;
2589   if (node_removal_hook_holder)
2590     cgraph_remove_node_removal_hook (node_removal_hook_holder);
2591   if (edge_removal_hook_holder)
2592     cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2593   node_removal_hook_holder = NULL;
2594   if (node_duplication_hook_holder)
2595     cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2596   if (edge_duplication_hook_holder)
2597     cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2598   node_duplication_hook_holder = NULL;
2599   VEC_free (inline_summary_t, gc, inline_summary_vec);
2600   inline_summary_vec = NULL;
2601   VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
2602   inline_edge_summary_vec = NULL;
2603   if (edge_predicate_pool)
2604     free_alloc_pool (edge_predicate_pool);
2605   edge_predicate_pool = 0;
2606 }