OSDN Git Service

2011-05-26 Richard Guenther <rguenther@suse.de>
[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 = VEC_index (tree, known_vals, c->operand_num);
559       tree res;
560
561       if (!val)
562         {
563           clause |= 1 << (i + predicate_first_dynamic_condition);
564           continue;
565         }
566       if (c->code == IS_NOT_CONSTANT)
567         continue;
568       res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
569       if (res
570           && integer_zerop (res))
571         continue;
572       clause |= 1 << (i + predicate_first_dynamic_condition);
573     }
574   return clause;
575 }
576
577
578 /* Work out what conditions might be true at invocation of E.  */
579
580 static clause_t
581 evaluate_conditions_for_edge (struct cgraph_edge *e, bool inline_p)
582 {
583   clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
584   struct inline_summary *info = inline_summary (e->callee);
585   int i;
586
587   if (ipa_node_params_vector && info->conds
588       /* FIXME: it seems that we forget to get argument count in some cases,
589          probaby for previously indirect edges or so.  */
590       && ipa_get_cs_argument_count (IPA_EDGE_REF (e)))
591     {
592       struct ipa_node_params *parms_info;
593       struct ipa_edge_args *args = IPA_EDGE_REF (e);
594       int i, count = ipa_get_cs_argument_count (args);
595       VEC (tree, heap) *known_vals = NULL;
596
597       if (e->caller->global.inlined_to)
598         parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
599       else
600         parms_info = IPA_NODE_REF (e->caller);
601
602       VEC_safe_grow_cleared (tree, heap, known_vals, count);
603       for (i = 0; i < count; i++)
604         {
605           tree cst = ipa_cst_from_jfunc (parms_info,
606                                          ipa_get_ith_jump_func (args, i));
607           if (cst)
608             VEC_replace (tree, known_vals, i, cst);
609         }
610       clause = evaluate_conditions_for_known_args (e->callee,
611                                                    inline_p, known_vals);
612       VEC_free (tree, heap, known_vals);
613     }
614   else
615     for (i = 0; i < (int)VEC_length (condition, info->conds); i++)
616       clause |= 1 << (i + predicate_first_dynamic_condition);
617
618   return clause;
619 }
620
621
622 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
623
624 static void
625 inline_summary_alloc (void)
626 {
627   if (!node_removal_hook_holder)
628     node_removal_hook_holder =
629       cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
630   if (!edge_removal_hook_holder)
631     edge_removal_hook_holder =
632       cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
633   if (!node_duplication_hook_holder)
634     node_duplication_hook_holder =
635       cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
636   if (!edge_duplication_hook_holder)
637     edge_duplication_hook_holder =
638       cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
639
640   if (VEC_length (inline_summary_t, inline_summary_vec)
641       <= (unsigned) cgraph_max_uid)
642     VEC_safe_grow_cleared (inline_summary_t, gc,
643                            inline_summary_vec, cgraph_max_uid + 1);
644   if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
645       <= (unsigned) cgraph_edge_max_uid)
646     VEC_safe_grow_cleared (inline_edge_summary_t, heap,
647                            inline_edge_summary_vec, cgraph_edge_max_uid + 1);
648   if (!edge_predicate_pool)
649     edge_predicate_pool = create_alloc_pool ("edge predicates", sizeof (struct predicate),
650                                              10);
651 }
652
653 /* Hook that is called by cgraph.c when a node is removed.  */
654
655 static void
656 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
657 {
658   struct inline_summary *info;
659   if (VEC_length (inline_summary_t, inline_summary_vec)
660       <= (unsigned)node->uid)
661     return;
662   info = inline_summary (node);
663   reset_node_growth_cache (node);
664   VEC_free (condition, gc, info->conds);
665   VEC_free (size_time_entry, gc, info->entry);
666   info->conds = NULL;
667   info->entry = NULL;
668   memset (info, 0, sizeof (inline_summary_t));
669 }
670
671
672 /* Hook that is called by cgraph.c when a node is duplicated.  */
673
674 static void
675 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
676                               ATTRIBUTE_UNUSED void *data)
677 {
678   struct inline_summary *info;
679   inline_summary_alloc ();
680   info = inline_summary (dst);
681   memcpy (info, inline_summary (src),
682           sizeof (struct inline_summary));
683   /* TODO: as an optimization, we may avoid copying conditions
684      that are known to be false or true.  */
685   info->conds = VEC_copy (condition, gc, info->conds);
686
687   /* When there are any replacements in the function body, see if we can figure
688      out that something was optimized out.  */
689   if (ipa_node_params_vector && dst->clone.tree_map)
690     {
691       VEC(size_time_entry,gc) *entry = info->entry;
692       /* Use SRC parm info since it may not be copied yet.  */
693       struct ipa_node_params *parms_info = IPA_NODE_REF (src);
694       VEC (tree, heap) *known_vals = NULL;
695       int count = ipa_get_param_count (parms_info);
696       int i,j;
697       clause_t possible_truths;
698       struct predicate true_pred = true_predicate ();
699       size_time_entry *e;
700       int optimized_out_size = 0;
701       gcov_type optimized_out_time = 0;
702       bool inlined_to_p = false;
703       struct cgraph_edge *edge;
704
705       info->entry = 0;
706       VEC_safe_grow_cleared (tree, heap, known_vals, count);
707       for (i = 0; i < count; i++)
708         {
709           tree t = ipa_get_param (parms_info, i);
710           struct ipa_replace_map *r;
711
712           for (j = 0;
713                VEC_iterate (ipa_replace_map_p, dst->clone.tree_map, j, r);
714                j++)
715             {
716               if (r->old_tree == t
717                   && r->replace_p
718                   && !r->ref_p)
719                 {
720                   VEC_replace (tree, known_vals, i, r->new_tree);
721                   break;
722                 }
723             }
724         }
725       possible_truths = evaluate_conditions_for_known_args (dst,
726                                                             false, known_vals);
727       VEC_free (tree, heap, known_vals);
728
729       account_size_time (info, 0, 0, &true_pred);
730
731       /* Remap size_time vectors.
732          Simplify the predicate by prunning out alternatives that are known
733          to be false.
734          TODO: as on optimization, we can also eliminate conditions known to be true.  */
735       for (i = 0; VEC_iterate (size_time_entry, entry, i, e); i++)
736         {
737           struct predicate new_predicate = true_predicate ();
738           for (j = 0; e->predicate.clause[j]; j++)
739             if (!(possible_truths & e->predicate.clause[j]))
740               {
741                 new_predicate = false_predicate ();
742                 break;
743               }
744             else
745               add_clause (&new_predicate,
746                           possible_truths & e->predicate.clause[j]);
747           if (false_predicate_p (&new_predicate))
748             {
749               optimized_out_size += e->size;
750               optimized_out_time += e->time;
751             }
752           else
753             account_size_time (info, e->size, e->time, &new_predicate);
754         }
755
756       /* Remap edge predicates with the same simplificaiton as above.  */
757       for (edge = dst->callees; edge; edge = edge->next_callee)
758         {
759           struct predicate new_predicate = true_predicate ();
760           struct inline_edge_summary *es = inline_edge_summary (edge);
761
762           if (!edge->inline_failed)
763             inlined_to_p = true;
764           if (!es->predicate)
765             continue;
766           for (j = 0; es->predicate->clause[j]; j++)
767             if (!(possible_truths & es->predicate->clause[j]))
768               {
769                 new_predicate = false_predicate ();
770                 break;
771               }
772             else
773               add_clause (&new_predicate,
774                           possible_truths & es->predicate->clause[j]);
775           if (false_predicate_p (&new_predicate)
776               && !false_predicate_p (es->predicate))
777             {
778               optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
779               optimized_out_time += (es->call_stmt_time
780                                      * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
781                                      * edge->frequency);
782               edge->frequency = 0;
783             }
784           *es->predicate = new_predicate;
785         }
786
787       /* Remap indirect edge predicates with the same simplificaiton as above.  */
788       for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
789         {
790           struct predicate new_predicate = true_predicate ();
791           struct inline_edge_summary *es = inline_edge_summary (edge);
792
793           if (!edge->inline_failed)
794             inlined_to_p = true;
795           if (!es->predicate)
796             continue;
797           for (j = 0; es->predicate->clause[j]; j++)
798             if (!(possible_truths & es->predicate->clause[j]))
799               {
800                 new_predicate = false_predicate ();
801                 break;
802               }
803             else
804               add_clause (&new_predicate,
805                           possible_truths & es->predicate->clause[j]);
806           if (false_predicate_p (&new_predicate)
807               && !false_predicate_p (es->predicate))
808             {
809               optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
810               optimized_out_time += (es->call_stmt_time
811                                      * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
812                                      * edge->frequency);
813               edge->frequency = 0;
814             }
815           *es->predicate = new_predicate;
816         }
817
818       /* If inliner or someone after inliner will ever start producing
819          non-trivial clones, we will get trouble with lack of information
820          about updating self sizes, because size vectors already contains
821          sizes of the calees.  */
822       gcc_assert (!inlined_to_p 
823                   || (!optimized_out_size && !optimized_out_time));
824
825       info->size -= optimized_out_size / INLINE_SIZE_SCALE;
826       info->self_size -= optimized_out_size / INLINE_SIZE_SCALE;
827       gcc_assert (info->size > 0);
828       gcc_assert (info->self_size > 0);
829
830       optimized_out_time /= INLINE_TIME_SCALE;
831       if (optimized_out_time > MAX_TIME)
832         optimized_out_time = MAX_TIME;
833       info->time -= optimized_out_time;
834       info->self_time -= optimized_out_time;
835       if (info->time < 0)
836         info->time = 0;
837       if (info->self_time < 0)
838         info->self_time = 0;
839     }
840   else
841     info->entry = VEC_copy (size_time_entry, gc, info->entry);
842 }
843
844
845 /* Hook that is called by cgraph.c when a node is duplicated.  */
846
847 static void
848 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
849                               ATTRIBUTE_UNUSED void *data)
850 {
851   struct inline_edge_summary *info;
852   struct inline_edge_summary *srcinfo;
853   inline_summary_alloc ();
854   info = inline_edge_summary (dst);
855   srcinfo = inline_edge_summary (src);
856   memcpy (info, srcinfo,
857           sizeof (struct inline_edge_summary));
858   info->predicate = NULL;
859   edge_set_predicate (dst, srcinfo->predicate);
860 }
861
862
863 /* Keep edge cache consistent across edge removal.  */
864
865 static void
866 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
867 {
868   if (edge_growth_cache)
869     reset_edge_growth_cache (edge);
870   if (edge->uid < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
871     {
872       edge_set_predicate (edge, NULL);
873       memset (inline_edge_summary (edge), 0, sizeof (struct inline_edge_summary));
874     }
875 }
876
877
878 /* Initialize growth caches.  */
879
880 void
881 initialize_growth_caches (void)
882 {
883   if (cgraph_edge_max_uid)
884     VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
885                            cgraph_edge_max_uid);
886   if (cgraph_max_uid)
887     VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
888 }
889
890
891 /* Free growth caches.  */
892
893 void
894 free_growth_caches (void)
895 {
896   VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
897   edge_growth_cache = 0;
898   VEC_free (int, heap, node_growth_cache);
899   node_growth_cache = 0;
900 }
901
902
903 /* Dump edge summaries associated to NODE and recursively to all clones.
904    Indent by INDENT.  */
905
906 static void
907 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
908                           struct inline_summary *info)
909 {
910   struct cgraph_edge *edge;
911   for (edge = node->callees; edge; edge = edge->next_callee)
912     {
913       struct inline_edge_summary *es = inline_edge_summary (edge);
914       fprintf (f, "%*s%s/%i %s\n%*s  loop depth:%2i freq:%4i size:%2i time: %2i callee size:%2i stack:%2i",
915                indent, "", cgraph_node_name (edge->callee),
916                edge->callee->uid, 
917                !edge->inline_failed ? "inlined"
918                : cgraph_inline_failed_string (edge->inline_failed),
919                indent, "",
920                es->loop_depth,  
921                edge->frequency,
922                es->call_stmt_size,
923                es->call_stmt_time,
924                (int)inline_summary (edge->callee)->size,
925                (int)inline_summary (edge->callee)->estimated_stack_size);
926       if (es->predicate)
927         {
928           fprintf (f, " predicate: ");
929           dump_predicate (f, info->conds, es->predicate);
930         }
931       else
932           fprintf (f, "\n");
933       if (!edge->inline_failed)
934         {
935           fprintf (f, "%*sStack frame offset %i, callee self size %i, callee size %i\n",
936                    indent+2, "",
937                    (int)inline_summary (edge->callee)->stack_frame_offset,
938                    (int)inline_summary (edge->callee)->estimated_self_stack_size,
939                    (int)inline_summary (edge->callee)->estimated_stack_size);
940           dump_inline_edge_summary (f, indent+2, edge->callee, info);
941         }
942     }
943   for (edge = node->indirect_calls; edge; edge = edge->next_callee)
944     {
945       struct inline_edge_summary *es = inline_edge_summary (edge);
946       fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i time: %2i\n",
947                indent, "",
948                es->loop_depth,  
949                edge->frequency,
950                es->call_stmt_size,
951                es->call_stmt_time);
952       if (es->predicate)
953         {
954           fprintf (f, "predicate: ");
955           dump_predicate (f, info->conds, es->predicate);
956         }
957       else
958           fprintf (f, "\n");
959     }
960 }
961
962
963 void
964 dump_inline_summary (FILE * f, struct cgraph_node *node)
965 {
966   if (node->analyzed)
967     {
968       struct inline_summary *s = inline_summary (node);
969       size_time_entry *e;
970       int i;
971       fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
972                node->uid);
973       if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
974         fprintf (f, " always_inline");
975       if (s->inlinable)
976         fprintf (f, " inlinable");
977       if (s->versionable)
978         fprintf (f, " versionable");
979       fprintf (f, "\n  self time:       %i\n",
980                s->self_time);
981       fprintf (f, "  global time:     %i\n", s->time);
982       fprintf (f, "  self size:       %i\n",
983                s->self_size);
984       fprintf (f, "  global size:     %i\n", s->size);
985       fprintf (f, "  self stack:      %i\n",
986                (int) s->estimated_self_stack_size);
987       fprintf (f, "  global stack:    %i\n",
988                (int) s->estimated_stack_size);
989       for (i = 0;
990            VEC_iterate (size_time_entry, s->entry, i, e);
991            i++)
992         {
993           fprintf (f, "    size:%f, time:%f, predicate:",
994                    (double) e->size / INLINE_SIZE_SCALE,
995                    (double) e->time / INLINE_TIME_SCALE);
996           dump_predicate (f, s->conds, &e->predicate);
997         }
998       fprintf (f, "  calls:\n");
999       dump_inline_edge_summary (f, 4, node, s);
1000       fprintf (f, "\n");
1001     }
1002 }
1003
1004 DEBUG_FUNCTION void
1005 debug_inline_summary (struct cgraph_node *node)
1006 {
1007   dump_inline_summary (stderr, node);
1008 }
1009
1010 void
1011 dump_inline_summaries (FILE *f)
1012 {
1013   struct cgraph_node *node;
1014
1015   for (node = cgraph_nodes; node; node = node->next)
1016     if (node->analyzed && !node->global.inlined_to)
1017       dump_inline_summary (f, node);
1018 }
1019
1020 /* Give initial reasons why inlining would fail on EDGE.  This gets either
1021    nullified or usually overwritten by more precise reasons later.  */
1022
1023 void
1024 initialize_inline_failed (struct cgraph_edge *e)
1025 {
1026   struct cgraph_node *callee = e->callee;
1027
1028   if (e->indirect_unknown_callee)
1029     e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1030   else if (!callee->analyzed)
1031     e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1032   else if (callee->local.redefined_extern_inline)
1033     e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1034   else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
1035     e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1036   else
1037     e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1038 }
1039
1040 /* See if statement might disappear after inlining.
1041    0 - means not eliminated
1042    1 - half of statements goes away
1043    2 - for sure it is eliminated.
1044    We are not terribly sophisticated, basically looking for simple abstraction
1045    penalty wrappers.  */
1046
1047 static int
1048 eliminated_by_inlining_prob (gimple stmt)
1049 {
1050   enum gimple_code code = gimple_code (stmt);
1051   switch (code)
1052     {
1053       case GIMPLE_RETURN:
1054         return 2;
1055       case GIMPLE_ASSIGN:
1056         if (gimple_num_ops (stmt) != 2)
1057           return 0;
1058
1059         /* Casts of parameters, loads from parameters passed by reference
1060            and stores to return value or parameters are often free after
1061            inlining dua to SRA and further combining.
1062            Assume that half of statements goes away.  */
1063         if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1064             || gimple_assign_rhs_code (stmt) == NOP_EXPR
1065             || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1066             || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1067           {
1068             tree rhs = gimple_assign_rhs1 (stmt);
1069             tree lhs = gimple_assign_lhs (stmt);
1070             tree inner_rhs = rhs;
1071             tree inner_lhs = lhs;
1072             bool rhs_free = false;
1073             bool lhs_free = false;
1074
1075             while (handled_component_p (inner_lhs)
1076                    || TREE_CODE (inner_lhs) == MEM_REF)
1077               inner_lhs = TREE_OPERAND (inner_lhs, 0);
1078             while (handled_component_p (inner_rhs)
1079                    || TREE_CODE (inner_rhs) == ADDR_EXPR
1080                    || TREE_CODE (inner_rhs) == MEM_REF)
1081               inner_rhs = TREE_OPERAND (inner_rhs, 0);
1082
1083
1084             if (TREE_CODE (inner_rhs) == PARM_DECL
1085                 || (TREE_CODE (inner_rhs) == SSA_NAME
1086                     && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
1087                     && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
1088               rhs_free = true;
1089             if (rhs_free && is_gimple_reg (lhs))
1090               lhs_free = true;
1091             if (((TREE_CODE (inner_lhs) == PARM_DECL
1092                   || (TREE_CODE (inner_lhs) == SSA_NAME
1093                       && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
1094                       && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
1095                  && inner_lhs != lhs)
1096                 || TREE_CODE (inner_lhs) == RESULT_DECL
1097                 || (TREE_CODE (inner_lhs) == SSA_NAME
1098                     && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
1099               lhs_free = true;
1100             if (lhs_free
1101                 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1102               rhs_free = true;
1103             if (lhs_free && rhs_free)
1104               return 1;
1105           }
1106         return 0;
1107       default:
1108         return 0;
1109     }
1110 }
1111
1112
1113 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1114    predicates to the CFG edges.   */
1115
1116 static void
1117 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1118                                    struct inline_summary *summary,
1119                                    basic_block bb)
1120 {
1121   gimple last;
1122   tree op;
1123   int index;
1124   enum tree_code code, inverted_code;
1125   edge e;
1126   edge_iterator ei;
1127   gimple set_stmt;
1128   tree op2;
1129
1130   last = last_stmt (bb);
1131   if (!last
1132       || gimple_code (last) != GIMPLE_COND)
1133     return;
1134   if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1135     return;
1136   op = gimple_cond_lhs (last);
1137   /* TODO: handle conditionals like
1138      var = op0 < 4;
1139      if (var != 0).  */
1140   if (TREE_CODE (op) != SSA_NAME)
1141     return;
1142   if (SSA_NAME_IS_DEFAULT_DEF (op))
1143     {
1144       index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
1145       if (index == -1)
1146         return;
1147       code = gimple_cond_code (last);
1148       inverted_code = invert_tree_comparison (code,
1149                                               HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1150
1151       FOR_EACH_EDGE (e, ei, bb->succs)
1152         {
1153           struct predicate p = add_condition (summary,
1154                                               index,
1155                                               e->flags & EDGE_TRUE_VALUE
1156                                               ? code : inverted_code,
1157                                               gimple_cond_rhs (last));
1158           e->aux = pool_alloc (edge_predicate_pool);
1159           *(struct predicate *)e->aux = p;
1160         }
1161     }
1162
1163   /* Special case
1164      if (builtin_constant_p (op))
1165        constant_code
1166      else
1167        nonconstant_code.
1168      Here we can predicate nonconstant_code.  We can't
1169      really handle constant_code since we have no predicate
1170      for this and also the constant code is not known to be
1171      optimized away when inliner doen't see operand is constant.
1172      Other optimizers might think otherwise.  */
1173   set_stmt = SSA_NAME_DEF_STMT (op);
1174   if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1175       || gimple_call_num_args (set_stmt) != 1)
1176     return;
1177   op2 = gimple_call_arg (set_stmt, 0);
1178   if (!SSA_NAME_IS_DEFAULT_DEF (op2))
1179     return;
1180   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op2));
1181   if (index == -1)
1182     return;
1183   if (gimple_cond_code (last) != NE_EXPR
1184       || !integer_zerop (gimple_cond_rhs (last)))
1185     return;
1186   FOR_EACH_EDGE (e, ei, bb->succs)
1187     if (e->flags & EDGE_FALSE_VALUE)
1188       {
1189         struct predicate p = add_condition (summary,
1190                                             index,
1191                                             IS_NOT_CONSTANT,
1192                                             NULL);
1193         e->aux = pool_alloc (edge_predicate_pool);
1194         *(struct predicate *)e->aux = p;
1195       }
1196 }
1197
1198
1199 /* If BB ends by a switch we can turn into predicates, attach corresponding
1200    predicates to the CFG edges.   */
1201
1202 static void
1203 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1204                                    struct inline_summary *summary,
1205                                    basic_block bb)
1206 {
1207   gimple last;
1208   tree op;
1209   int index;
1210   edge e;
1211   edge_iterator ei;
1212   size_t n;
1213   size_t case_idx;
1214
1215   last = last_stmt (bb);
1216   if (!last
1217       || gimple_code (last) != GIMPLE_SWITCH)
1218     return;
1219   op = gimple_switch_index (last);
1220   if (TREE_CODE (op) != SSA_NAME
1221       || !SSA_NAME_IS_DEFAULT_DEF (op))
1222     return;
1223   index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
1224   if (index == -1)
1225     return;
1226
1227   FOR_EACH_EDGE (e, ei, bb->succs)
1228     {
1229       e->aux = pool_alloc (edge_predicate_pool);
1230       *(struct predicate *)e->aux = false_predicate ();
1231     }
1232   n = gimple_switch_num_labels(last);
1233   for (case_idx = 0; case_idx < n; ++case_idx)
1234     {
1235       tree cl = gimple_switch_label (last, case_idx);
1236       tree min, max;
1237       struct predicate p;
1238
1239       e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1240       min = CASE_LOW (cl);
1241       max = CASE_HIGH (cl);
1242
1243       /* For default we might want to construct predicate that none
1244          of cases is met, but it is bit hard to do not having negations
1245          of conditionals handy.  */
1246       if (!min && !max)
1247         p = true_predicate ();
1248       else if (!max)
1249         p = add_condition (summary, index,
1250                            EQ_EXPR,
1251                            min);
1252       else
1253         {
1254           struct predicate p1, p2;
1255           p1 = add_condition (summary, index,
1256                               GE_EXPR,
1257                               min);
1258           p2 = add_condition (summary, index,
1259                               LE_EXPR,
1260                               max);
1261           p = and_predicates (&p1, &p2);
1262         }
1263       *(struct predicate *)e->aux
1264         = or_predicates (&p, (struct predicate *)e->aux);
1265     }
1266 }
1267
1268
1269 /* For each BB in NODE attach to its AUX pointer predicate under
1270    which it is executable.  */
1271
1272 static void
1273 compute_bb_predicates (struct cgraph_node *node,
1274                        struct ipa_node_params *parms_info,
1275                        struct inline_summary *summary)
1276 {
1277   struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1278   bool done = false;
1279   basic_block bb;
1280
1281   FOR_EACH_BB_FN (bb, my_function)
1282     {
1283       set_cond_stmt_execution_predicate (parms_info, summary, bb);
1284       set_switch_stmt_execution_predicate (parms_info, summary, bb);
1285     }
1286
1287   /* Entry block is always executable.  */
1288   ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux = pool_alloc (edge_predicate_pool);
1289   *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1290     = true_predicate ();
1291
1292   /* A simple dataflow propagation of predicates forward in the CFG.
1293      TODO: work in reverse postorder.  */
1294   while (!done)
1295     {
1296       done = true;
1297       FOR_EACH_BB_FN (bb, my_function)
1298         {
1299           struct predicate p = false_predicate ();
1300           edge e;
1301           edge_iterator ei;
1302           FOR_EACH_EDGE (e, ei, bb->preds)
1303             {
1304               if (e->src->aux)
1305                 {
1306                   struct predicate this_bb_predicate = *(struct predicate *)e->src->aux;
1307                   if (e->aux)
1308                     this_bb_predicate = and_predicates (&this_bb_predicate,
1309                                                         (struct predicate *)e->aux);
1310                   p = or_predicates (&p, &this_bb_predicate);
1311                   if (true_predicate_p (&p))
1312                     break;
1313                 }
1314             }
1315           if (false_predicate_p (&p))
1316             gcc_assert (!bb->aux);
1317           else
1318             {
1319               if (!bb->aux)
1320                 {
1321                   done = false;
1322                   bb->aux = pool_alloc (edge_predicate_pool);
1323                   *((struct predicate *)bb->aux) = p;
1324                 }
1325               else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1326                 {
1327                   done = false;
1328                   *((struct predicate *)bb->aux) = p;
1329                 }
1330             }
1331         }
1332     }
1333 }
1334
1335
1336 /* We keep info about constantness of SSA names.  */
1337
1338 typedef struct predicate predicate_t;
1339 DEF_VEC_O (predicate_t);
1340 DEF_VEC_ALLOC_O (predicate_t, heap);
1341
1342
1343 /* Return predicate specifying when the STMT might have result that is not a compile
1344    time constant.  */
1345
1346 static struct predicate
1347 will_be_nonconstant_predicate (struct ipa_node_params *info,
1348                                struct inline_summary *summary,
1349                                gimple stmt,
1350                                VEC (predicate_t, heap) *nonconstant_names)
1351                               
1352 {
1353   struct predicate p = true_predicate ();
1354   ssa_op_iter iter;
1355   tree use;
1356   struct predicate op_non_const;
1357
1358   /* What statments might be optimized away
1359      when their arguments are constant
1360      TODO: also trivial builtins.
1361      builtin_constant_p is already handled later.  */
1362   if (gimple_code (stmt) != GIMPLE_ASSIGN
1363       && gimple_code (stmt) != GIMPLE_COND
1364       && gimple_code (stmt) != GIMPLE_SWITCH)
1365     return p;
1366
1367   /* Stores and loads will stay anyway.
1368      TODO: Constant memory accesses could be handled here, too.  */
1369   if (gimple_vuse (stmt))
1370     return p;
1371
1372   /* See if we understand all operands before we start
1373      adding conditionals.  */
1374   FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1375     {
1376       if (TREE_CODE (use) != SSA_NAME)
1377         return p;
1378       /* For arguments we can build a condition.  */
1379       if (SSA_NAME_IS_DEFAULT_DEF (use)
1380           && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
1381         continue;
1382       /* If we know when operand is constant,
1383          we still can say something useful.  */
1384       if (!true_predicate_p (VEC_index (predicate_t, nonconstant_names,
1385                                         SSA_NAME_VERSION (use))))
1386         continue;
1387       return p;
1388     }
1389   op_non_const = false_predicate ();
1390   FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1391     {
1392       if (SSA_NAME_IS_DEFAULT_DEF (use)
1393           && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
1394         p = add_condition (summary,
1395                            ipa_get_param_decl_index (info, SSA_NAME_VAR (use)),
1396                            IS_NOT_CONSTANT, NULL);
1397       else
1398         p = *VEC_index (predicate_t, nonconstant_names,
1399                         SSA_NAME_VERSION (use));
1400       op_non_const = or_predicates (&p, &op_non_const);
1401     }
1402   if (gimple_code (stmt) == GIMPLE_ASSIGN
1403       && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
1404     VEC_replace (predicate_t, nonconstant_names,
1405                  SSA_NAME_VERSION (gimple_assign_lhs (stmt)), &op_non_const);
1406   return op_non_const;
1407 }
1408
1409
1410 /* Compute function body size parameters for NODE.
1411    When EARLY is true, we compute only simple summaries without
1412    non-trivial predicates to drive the early inliner.  */
1413
1414 static void
1415 estimate_function_body_sizes (struct cgraph_node *node, bool early)
1416 {
1417   gcov_type time = 0;
1418   /* Estimate static overhead for function prologue/epilogue and alignment. */
1419   int size = 2;
1420   /* Benefits are scaled by probability of elimination that is in range
1421      <0,2>.  */
1422   basic_block bb;
1423   gimple_stmt_iterator bsi;
1424   struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1425   int freq;
1426   struct inline_summary *info = inline_summary (node);
1427   struct predicate bb_predicate;
1428   struct ipa_node_params *parms_info = NULL;
1429   VEC (predicate_t, heap) *nonconstant_names = NULL;
1430
1431   if (ipa_node_params_vector && !early && optimize)
1432     {
1433       parms_info = IPA_NODE_REF (node);
1434       VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
1435                              VEC_length (tree, SSANAMES (my_function)));
1436     }
1437
1438   info->conds = 0;
1439   info->entry = 0;
1440
1441
1442   if (dump_file)
1443     fprintf (dump_file, "\nAnalyzing function body size: %s\n",
1444              cgraph_node_name (node));
1445
1446   /* When we run into maximal number of entries, we assign everything to the
1447      constant truth case.  Be sure to have it in list. */
1448   bb_predicate = true_predicate ();
1449   account_size_time (info, 0, 0, &bb_predicate);
1450
1451   bb_predicate = not_inlined_predicate ();
1452   account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
1453
1454   gcc_assert (my_function && my_function->cfg);
1455   if (parms_info)
1456     compute_bb_predicates (node, parms_info, info);
1457   FOR_EACH_BB_FN (bb, my_function)
1458     {
1459       freq = compute_call_stmt_bb_frequency (node->decl, bb);
1460
1461       /* TODO: Obviously predicates can be propagated down across CFG.  */
1462       if (parms_info)
1463         {
1464           if (bb->aux)
1465             bb_predicate = *(struct predicate *)bb->aux;
1466           else
1467             bb_predicate = false_predicate ();
1468         }
1469       else
1470         bb_predicate = true_predicate ();
1471
1472       if (dump_file && (dump_flags & TDF_DETAILS))
1473         {
1474           fprintf (dump_file, "\n BB %i predicate:", bb->index);
1475           dump_predicate (dump_file, info->conds, &bb_predicate);
1476         }
1477       
1478       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1479         {
1480           gimple stmt = gsi_stmt (bsi);
1481           int this_size = estimate_num_insns (stmt, &eni_size_weights);
1482           int this_time = estimate_num_insns (stmt, &eni_time_weights);
1483           int prob;
1484           struct predicate will_be_nonconstant;
1485
1486           if (dump_file && (dump_flags & TDF_DETAILS))
1487             {
1488               fprintf (dump_file, "  ");
1489               print_gimple_stmt (dump_file, stmt, 0, 0);
1490               fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
1491                        ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
1492             }
1493
1494           if (is_gimple_call (stmt))
1495             {
1496               struct cgraph_edge *edge = cgraph_edge (node, stmt);
1497               struct inline_edge_summary *es = inline_edge_summary (edge);
1498
1499               /* Special case: results of BUILT_IN_CONSTANT_P will be always
1500                  resolved as constant.  We however don't want to optimize
1501                  out the cgraph edges.  */
1502               if (nonconstant_names
1503                   && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
1504                   && gimple_call_lhs (stmt)
1505                   && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
1506                 {
1507                   struct predicate false_p = false_predicate ();
1508                   VEC_replace (predicate_t, nonconstant_names,
1509                                SSA_NAME_VERSION (gimple_call_lhs (stmt)), &false_p);
1510                 }
1511
1512               es->call_stmt_size = this_size;
1513               es->call_stmt_time = this_time;
1514               es->loop_depth = bb->loop_depth;
1515               edge_set_predicate (edge, &bb_predicate);
1516
1517               /* Do not inline calls where we cannot triviall work around
1518                  mismatches in argument or return types.  */
1519               if (edge->callee
1520                   && !gimple_check_call_matching_types (stmt, edge->callee->decl))
1521                 {
1522                   edge->call_stmt_cannot_inline_p = true;
1523                   gimple_call_set_cannot_inline (stmt, true);
1524                 }
1525               else
1526                 gcc_assert (!gimple_call_cannot_inline_p (stmt));
1527             }
1528
1529           /* TODO: When conditional jump or swithc is known to be constant, but
1530              we did not translate it into the predicates, we really can account
1531              just maximum of the possible paths.  */
1532           if (parms_info)
1533             will_be_nonconstant
1534                = will_be_nonconstant_predicate (parms_info, info,
1535                                                 stmt, nonconstant_names);
1536           if (this_time || this_size)
1537             {
1538               struct predicate p;
1539
1540               this_time *= freq;
1541               time += this_time;
1542               size += this_size;
1543
1544               prob = eliminated_by_inlining_prob (stmt);
1545               if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1546                 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
1547               if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1548                 fprintf (dump_file, "\t\twill eliminated by inlining\n");
1549
1550               if (parms_info)
1551                 p = and_predicates (&bb_predicate, &will_be_nonconstant);
1552               else
1553                 p = true_predicate ();
1554
1555               /* We account everything but the calls.  Calls have their own
1556                  size/time info attached to cgraph edges.  This is neccesary
1557                  in order to make the cost disappear after inlining.  */
1558               if (!is_gimple_call (stmt))
1559                 {
1560                   if (prob)
1561                     {
1562                       struct predicate ip = not_inlined_predicate ();
1563                       ip = and_predicates (&ip, &p);
1564                       account_size_time (info, this_size * prob,
1565                                          this_time * prob, &ip);
1566                     }
1567                   if (prob != 2)
1568                     account_size_time (info, this_size * (2 - prob),
1569                                        this_time * (2 - prob), &p);
1570                 }
1571
1572               gcc_assert (time >= 0);
1573               gcc_assert (size >= 0);
1574             }
1575         }
1576     }
1577   FOR_ALL_BB_FN (bb, my_function)
1578     {
1579       edge e;
1580       edge_iterator ei;
1581
1582       if (bb->aux)
1583         pool_free (edge_predicate_pool, bb->aux);
1584       bb->aux = NULL;
1585       FOR_EACH_EDGE (e, ei, bb->succs)
1586         {
1587           if (e->aux)
1588             pool_free (edge_predicate_pool, e->aux);
1589           e->aux = NULL;
1590         }
1591     }
1592   time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1593   if (time > MAX_TIME)
1594     time = MAX_TIME;
1595   inline_summary (node)->self_time = time;
1596   inline_summary (node)->self_size = size;
1597   VEC_free (predicate_t, heap, nonconstant_names);
1598   if (dump_file)
1599     {
1600       fprintf (dump_file, "\n");
1601       dump_inline_summary (dump_file, node);
1602     }
1603 }
1604
1605
1606 /* Compute parameters of functions used by inliner.
1607    EARLY is true when we compute parameters for the early inliner  */
1608
1609 void
1610 compute_inline_parameters (struct cgraph_node *node, bool early)
1611 {
1612   HOST_WIDE_INT self_stack_size;
1613   struct cgraph_edge *e;
1614   struct inline_summary *info;
1615
1616   gcc_assert (!node->global.inlined_to);
1617
1618   inline_summary_alloc ();
1619
1620   info = inline_summary (node);
1621
1622   /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
1623      Once this happen, we will need to more curefully predict call
1624      statement size.  */
1625   if (node->thunk.thunk_p)
1626     {
1627       struct inline_edge_summary *es = inline_edge_summary (node->callees);
1628       struct predicate t = true_predicate ();
1629
1630       info->inlinable = info->versionable = 0;
1631       node->callees->call_stmt_cannot_inline_p = true;
1632       node->local.can_change_signature = false;
1633       es->call_stmt_time = 1;
1634       es->call_stmt_size = 1;
1635       account_size_time (info, 0, 0, &t);
1636       return;
1637     }
1638
1639   /* Estimate the stack size for the function if we're optimizing.  */
1640   self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
1641   info->estimated_self_stack_size = self_stack_size;
1642   info->estimated_stack_size = self_stack_size;
1643   info->stack_frame_offset = 0;
1644
1645   /* Can this function be inlined at all?  */
1646   info->inlinable = tree_inlinable_function_p (node->decl);
1647
1648   /* Inlinable functions always can change signature.  */
1649   if (info->inlinable)
1650     node->local.can_change_signature = true;
1651   else
1652     {
1653       /* Functions calling builtin_apply can not change signature.  */
1654       for (e = node->callees; e; e = e->next_callee)
1655         if (DECL_BUILT_IN (e->callee->decl)
1656             && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
1657             && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_APPLY_ARGS)
1658           break;
1659       node->local.can_change_signature = !e;
1660     }
1661   estimate_function_body_sizes (node, early);
1662
1663   /* Inlining characteristics are maintained by the cgraph_mark_inline.  */
1664   info->time = info->self_time;
1665   info->size = info->self_size;
1666   info->stack_frame_offset = 0;
1667   info->estimated_stack_size = info->estimated_self_stack_size;
1668 }
1669
1670
1671 /* Compute parameters of functions used by inliner using
1672    current_function_decl.  */
1673
1674 static unsigned int
1675 compute_inline_parameters_for_current (void)
1676 {
1677   compute_inline_parameters (cgraph_get_node (current_function_decl), true);
1678   return 0;
1679 }
1680
1681 struct gimple_opt_pass pass_inline_parameters =
1682 {
1683  {
1684   GIMPLE_PASS,
1685   "inline_param",                       /* name */
1686   NULL,                                 /* gate */
1687   compute_inline_parameters_for_current,/* execute */
1688   NULL,                                 /* sub */
1689   NULL,                                 /* next */
1690   0,                                    /* static_pass_number */
1691   TV_INLINE_HEURISTICS,                 /* tv_id */
1692   0,                                    /* properties_required */
1693   0,                                    /* properties_provided */
1694   0,                                    /* properties_destroyed */
1695   0,                                    /* todo_flags_start */
1696   0                                     /* todo_flags_finish */
1697  }
1698 };
1699
1700
1701 /* Increase SIZE and TIME for size and time needed to handle edge E.  */
1702
1703 static void
1704 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time)
1705 {
1706   struct inline_edge_summary *es = inline_edge_summary (e);
1707   *size += es->call_stmt_size * INLINE_SIZE_SCALE;
1708   *time += (es->call_stmt_time
1709             * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
1710   if (*time > MAX_TIME * INLINE_TIME_SCALE)
1711     *time = MAX_TIME * INLINE_TIME_SCALE;
1712 }
1713
1714
1715 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.  */
1716
1717 static void
1718 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
1719                               clause_t possible_truths)
1720 {
1721   struct cgraph_edge *e;
1722   for (e = node->callees; e; e = e->next_callee)
1723     {
1724       struct inline_edge_summary *es = inline_edge_summary (e);
1725       if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1726         {
1727           if (e->inline_failed)
1728             estimate_edge_size_and_time (e, size, time);
1729           else
1730             estimate_calls_size_and_time (e->callee, size, time,
1731                                           possible_truths);
1732         }
1733     }
1734   /* TODO: look for devirtualizing oppurtunities.  */
1735   for (e = node->indirect_calls; 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         estimate_edge_size_and_time (e, size, time);
1740     }
1741 }
1742
1743
1744 /* Estimate size and time needed to execute NODE assuming
1745    POSSIBLE_TRUTHS clause. */
1746
1747 static void
1748 estimate_node_size_and_time (struct cgraph_node *node,
1749                              clause_t possible_truths,
1750                              int *ret_size, int *ret_time)
1751 {
1752   struct inline_summary *info = inline_summary (node);
1753   size_time_entry *e;
1754   int size = 0, time = 0;
1755   int i;
1756
1757   if (dump_file
1758       && (dump_flags & TDF_DETAILS))
1759     {
1760       bool found = false;
1761       fprintf (dump_file, "   Estimating body: %s/%i\n"
1762                           "   Known to be false: ",
1763                cgraph_node_name (node),
1764                node->uid);
1765
1766       for (i = predicate_not_inlined_condition;
1767            i < (predicate_first_dynamic_condition
1768                 + (int)VEC_length (condition, info->conds)); i++)
1769         if (!(possible_truths & (1 << i)))
1770           {
1771             if (found)
1772               fprintf (dump_file, ", ");
1773             found = true;
1774             dump_condition (dump_file, info->conds, i);
1775           }
1776     }
1777
1778   for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
1779     if (evaluate_predicate (&e->predicate, possible_truths))
1780       time += e->time, size += e->size;
1781
1782   if (time > MAX_TIME * INLINE_TIME_SCALE)
1783     time = MAX_TIME * INLINE_TIME_SCALE;
1784
1785   estimate_calls_size_and_time (node, &size, &time, possible_truths);
1786   time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
1787   size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
1788
1789
1790   if (dump_file
1791       && (dump_flags & TDF_DETAILS))
1792     fprintf (dump_file, "\n   size:%i time:%i\n", size, time);
1793   if (ret_time)
1794     *ret_time = time;
1795   if (ret_size)
1796     *ret_size = size;
1797   return;
1798 }
1799
1800
1801 /* Estimate size and time needed to execute callee of EDGE assuming that
1802    parameters known to be constant at caller of EDGE are propagated.
1803    KNOWN_VALs is a vector of assumed known constant values for parameters.  */
1804
1805 void
1806 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
1807                                    VEC (tree, heap) *known_vals,
1808                                    int *ret_size, int *ret_time)
1809 {
1810   clause_t clause;
1811
1812   clause = evaluate_conditions_for_known_args (node, false, known_vals);
1813   estimate_node_size_and_time (node, clause, ret_size, ret_time);
1814 }
1815
1816
1817 /* Translate all conditions from callee representation into caller representation and
1818    symbolically evaluate predicate P into new predicate.
1819
1820    INFO is inline_summary of function we are adding predicate into, CALLEE_INFO is summary
1821    of function predicate P is from. OPERAND_MAP is array giving callee formal IDs the
1822    caller formal IDs. POSSSIBLE_TRUTHS is clausule of all callee conditions that
1823    may be true in caller context. TOPLEV_PREDICATE is predicate under which callee
1824    is executed.  */
1825
1826 static struct predicate
1827 remap_predicate (struct inline_summary *info, struct inline_summary *callee_info,
1828                  struct predicate *p,
1829                  VEC (int, heap) *operand_map,
1830                  clause_t possible_truths,
1831                  struct predicate *toplev_predicate)
1832 {
1833   int i;
1834   struct predicate out = true_predicate ();
1835
1836   /* True predicate is easy.  */
1837   if (true_predicate_p (p))
1838     return *toplev_predicate;
1839   for (i = 0; p->clause[i]; i++)
1840     {
1841       clause_t clause = p->clause[i];
1842       int cond;
1843       struct predicate clause_predicate = false_predicate ();
1844
1845       gcc_assert (i < MAX_CLAUSES);
1846
1847       for (cond = 0; cond < NUM_CONDITIONS; cond ++)
1848         /* Do we have condition we can't disprove?   */
1849         if (clause & possible_truths & (1 << cond))
1850           {
1851             struct predicate cond_predicate;
1852             /* Work out if the condition can translate to predicate in the
1853                inlined function.  */
1854             if (cond >= predicate_first_dynamic_condition)
1855               {
1856                  struct condition *c;
1857
1858                  c = VEC_index (condition, callee_info->conds,
1859                                 cond - predicate_first_dynamic_condition);
1860                  /* See if we can remap condition operand to caller's operand.
1861                     Otherwise give up.  */
1862                  if (!operand_map
1863                      || VEC_index (int, operand_map, c->operand_num) == -1)
1864                    cond_predicate = true_predicate ();
1865                  else
1866                    cond_predicate = add_condition (info,
1867                                                    VEC_index (int, operand_map,
1868                                                               c->operand_num),
1869                                                    c->code, c->val);
1870               }
1871             /* Fixed conditions remains same, construct single
1872                condition predicate.  */
1873             else
1874               {
1875                 cond_predicate.clause[0] = 1 << cond;
1876                 cond_predicate.clause[1] = 0;
1877               }
1878             clause_predicate = or_predicates (&clause_predicate, &cond_predicate);
1879           }
1880       out = and_predicates (&out, &clause_predicate);
1881     }
1882   return and_predicates (&out, toplev_predicate);
1883 }
1884
1885
1886 /* Update summary information of inline clones after inlining.
1887    Compute peak stack usage.  */
1888
1889 static void
1890 inline_update_callee_summaries (struct cgraph_node *node,
1891                                 int depth)
1892 {
1893   struct cgraph_edge *e;
1894   struct inline_summary *callee_info = inline_summary (node);
1895   struct inline_summary *caller_info = inline_summary (node->callers->caller);
1896   HOST_WIDE_INT peak;
1897
1898   callee_info->stack_frame_offset
1899     = caller_info->stack_frame_offset
1900       + caller_info->estimated_self_stack_size;
1901   peak = callee_info->stack_frame_offset
1902       + callee_info->estimated_self_stack_size;
1903   if (inline_summary (node->global.inlined_to)->estimated_stack_size
1904       < peak)
1905     inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
1906   cgraph_propagate_frequency (node);
1907   for (e = node->callees; e; e = e->next_callee)
1908     {
1909       if (!e->inline_failed)
1910         inline_update_callee_summaries (e->callee, depth);
1911       inline_edge_summary (e)->loop_depth += depth;
1912     }
1913   for (e = node->indirect_calls; e; e = e->next_callee)
1914     inline_edge_summary (e)->loop_depth += depth;
1915 }
1916
1917
1918 /* Remap predicates of callees of NODE.  Rest of arguments match
1919    remap_predicate.  */
1920
1921 static void
1922 remap_edge_predicates (struct cgraph_node *node,
1923                        struct inline_summary *info,
1924                        struct inline_summary *callee_info,
1925                        VEC (int, heap) *operand_map,
1926                        clause_t possible_truths,
1927                        struct predicate *toplev_predicate)
1928 {
1929   struct cgraph_edge *e;
1930   for (e = node->callees; e; e = e->next_callee)
1931     {
1932       struct inline_edge_summary *es = inline_edge_summary (e);
1933       struct predicate p;
1934       if (es->predicate)
1935         {
1936           p = remap_predicate (info, callee_info,
1937                                es->predicate, operand_map, possible_truths,
1938                                toplev_predicate);
1939           edge_set_predicate (e, &p);
1940           /* TODO: We should remove the edge for code that will be optimized out,
1941              but we need to keep verifiers and tree-inline happy.
1942              Make it cold for now.  */
1943           if (false_predicate_p (&p))
1944             {
1945               e->count = 0;
1946               e->frequency = 0;
1947             }
1948         }
1949       if (!e->inline_failed)
1950         remap_edge_predicates (e->callee, info, callee_info, operand_map,
1951                                possible_truths, toplev_predicate);
1952     }
1953   for (e = node->indirect_calls; e; e = e->next_callee)
1954     {
1955       struct inline_edge_summary *es = inline_edge_summary (e);
1956       struct predicate p;
1957       if (es->predicate)
1958         {
1959           p = remap_predicate (info, callee_info,
1960                                es->predicate, operand_map, possible_truths,
1961                                toplev_predicate);
1962           edge_set_predicate (e, &p);
1963           /* TODO: We should remove the edge for code that will be optimized out,
1964              but we need to keep verifiers and tree-inline happy.
1965              Make it cold for now.  */
1966           if (false_predicate_p (&p))
1967             {
1968               e->count = 0;
1969               e->frequency = 0;
1970             }
1971         }
1972     }
1973 }
1974
1975
1976 /* We inlined EDGE.  Update summary of the function we inlined into.  */
1977
1978 void
1979 inline_merge_summary (struct cgraph_edge *edge)
1980 {
1981   struct inline_summary *callee_info = inline_summary (edge->callee);
1982   struct cgraph_node *to = (edge->caller->global.inlined_to
1983                             ? edge->caller->global.inlined_to : edge->caller);
1984   struct inline_summary *info = inline_summary (to);
1985   clause_t clause = 0;          /* not_inline is known to be false.  */
1986   size_time_entry *e;
1987   VEC (int, heap) *operand_map = NULL;
1988   int i;
1989   struct predicate toplev_predicate;
1990   struct inline_edge_summary *es = inline_edge_summary (edge);
1991
1992   if (es->predicate)
1993     toplev_predicate = *es->predicate;
1994   else
1995     toplev_predicate = true_predicate ();
1996
1997   if (ipa_node_params_vector && callee_info->conds
1998       /* FIXME: it seems that we forget to get argument count in some cases,
1999          probaby for previously indirect edges or so.
2000          Removing the test leads to ICE on tramp3d.  */
2001       && ipa_get_cs_argument_count (IPA_EDGE_REF (edge)))
2002     {
2003       struct ipa_edge_args *args = IPA_EDGE_REF (edge);
2004       int count = ipa_get_cs_argument_count (args);
2005       int i;
2006
2007       clause = evaluate_conditions_for_edge (edge, true);
2008       VEC_safe_grow_cleared (int, heap, operand_map, count);
2009       for (i = 0; i < count; i++)
2010         {
2011           struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
2012           int map = -1;
2013           /* TODO: handle non-NOPs when merging.  */
2014           if (jfunc->type == IPA_JF_PASS_THROUGH
2015               && jfunc->value.pass_through.operation == NOP_EXPR)
2016             map = jfunc->value.pass_through.formal_id;
2017           VEC_replace (int, operand_map, i, map);
2018           gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
2019         }
2020     }
2021   for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
2022     {
2023       struct predicate p = remap_predicate (info, callee_info,
2024                                             &e->predicate, operand_map, clause,
2025                                             &toplev_predicate);
2026       gcov_type add_time = ((gcov_type)e->time * edge->frequency
2027                             + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2028       if (add_time > MAX_TIME)
2029         add_time = MAX_TIME;
2030       account_size_time (info, e->size, add_time, &p);
2031     }
2032   remap_edge_predicates (edge->callee, info, callee_info, operand_map,
2033                          clause, &toplev_predicate);
2034   info->size = 0;
2035   info->time = 0;
2036   for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2037     info->size += e->size, info->time += e->time;
2038   estimate_calls_size_and_time (to, &info->size, &info->time,
2039                                 ~(clause_t)(1 << predicate_false_condition));
2040
2041   inline_update_callee_summaries (edge->callee,
2042                                   inline_edge_summary (edge)->loop_depth);
2043
2044   info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
2045   info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
2046 }
2047
2048
2049 /* Estimate the time cost for the caller when inlining EDGE.
2050    Only to be called via estimate_edge_time, that handles the
2051    caching mechanism.
2052
2053    When caching, also update the cache entry.  Compute both time and
2054    size, since we always need both metrics eventually.  */
2055
2056 int
2057 do_estimate_edge_time (struct cgraph_edge *edge)
2058 {
2059   int time;
2060   int size;
2061   gcov_type ret;
2062   struct inline_edge_summary *es = inline_edge_summary (edge);
2063
2064   gcc_checking_assert (edge->inline_failed);
2065   estimate_node_size_and_time (edge->callee,
2066                                evaluate_conditions_for_edge (edge, true),
2067                                &size, &time);
2068
2069   ret = (((gcov_type)time - es->call_stmt_time) * edge->frequency
2070          + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2071   if (ret > MAX_TIME)
2072     ret = MAX_TIME;
2073
2074   /* When caching, update the cache entry.  */
2075   if (edge_growth_cache)
2076     {
2077       int ret_size;
2078       if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
2079           <= edge->uid)
2080         VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
2081                                cgraph_edge_max_uid);
2082       VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->time
2083         = ret + (ret >= 0);
2084
2085       ret_size = size - es->call_stmt_size;
2086       gcc_checking_assert (es->call_stmt_size);
2087       VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->size
2088         = ret_size + (ret_size >= 0);
2089     }
2090   return ret;
2091 }
2092
2093
2094 /* Estimate the growth of the caller when inlining EDGE.
2095    Only to be called via estimate_edge_size.  */
2096
2097 int
2098 do_estimate_edge_growth (struct cgraph_edge *edge)
2099 {
2100   int size;
2101
2102   /* When we do caching, use do_estimate_edge_time to populate the entry.  */
2103
2104   if (edge_growth_cache)
2105     {
2106       do_estimate_edge_time (edge);
2107       size = VEC_index (edge_growth_cache_entry,
2108                         edge_growth_cache,
2109                         edge->uid)->size;
2110       gcc_checking_assert (size);
2111       return size - (size > 0);
2112     }
2113
2114   /* Early inliner runs without caching, go ahead and do the dirty work.  */
2115   gcc_checking_assert (edge->inline_failed);
2116   estimate_node_size_and_time (edge->callee,
2117                                evaluate_conditions_for_edge (edge, true),
2118                                &size, NULL);
2119   gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
2120   return size - inline_edge_summary (edge)->call_stmt_size;
2121 }
2122
2123
2124 /* Estimate self time of the function NODE after inlining EDGE.  */
2125
2126 int
2127 estimate_time_after_inlining (struct cgraph_node *node,
2128                               struct cgraph_edge *edge)
2129 {
2130   struct inline_edge_summary *es = inline_edge_summary (edge);
2131   if (!es->predicate || !false_predicate_p (es->predicate))
2132     {
2133       gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
2134       if (time < 0)
2135         time = 0;
2136       if (time > MAX_TIME)
2137         time = MAX_TIME;
2138       return time;
2139     }
2140   return inline_summary (node)->time;
2141 }
2142
2143
2144 /* Estimate the size of NODE after inlining EDGE which should be an
2145    edge to either NODE or a call inlined into NODE.  */
2146
2147 int
2148 estimate_size_after_inlining (struct cgraph_node *node,
2149                               struct cgraph_edge *edge)
2150 {
2151   struct inline_edge_summary *es = inline_edge_summary (edge);
2152   if (!es->predicate || !false_predicate_p (es->predicate))
2153     {
2154       int size = inline_summary (node)->size + estimate_edge_growth (edge);
2155       gcc_assert (size >= 0);
2156       return size;
2157     }
2158   return inline_summary (node)->size;
2159 }
2160
2161
2162 /* Estimate the growth caused by inlining NODE into all callees.  */
2163
2164 int
2165 do_estimate_growth (struct cgraph_node *node)
2166 {
2167   int growth = 0;
2168   struct cgraph_edge *e;
2169   bool self_recursive = false;
2170   struct inline_summary *info = inline_summary (node);
2171
2172   for (e = node->callers; e; e = e->next_caller)
2173     {
2174       gcc_checking_assert (e->inline_failed);
2175
2176       if (e->caller == node
2177           || (e->caller->global.inlined_to
2178               && e->caller->global.inlined_to == node))
2179         self_recursive = true;
2180       growth += estimate_edge_growth (e);
2181     }
2182      
2183
2184   /* For self recursive functions the growth estimation really should be
2185      infinity.  We don't want to return very large values because the growth
2186      plays various roles in badness computation fractions.  Be sure to not
2187      return zero or negative growths. */
2188   if (self_recursive)
2189     growth = growth < info->size ? info->size : growth;
2190   else
2191     {
2192       if (cgraph_will_be_removed_from_program_if_no_direct_calls (node)
2193           && !DECL_EXTERNAL (node->decl))
2194         growth -= info->size;
2195       /* COMDAT functions are very often not shared across multiple units since they
2196          come from various template instantiations.  Take this into account.  */
2197       else  if (DECL_COMDAT (node->decl)
2198                 && cgraph_can_remove_if_no_direct_calls_p (node))
2199         growth -= (info->size
2200                    * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY)) + 50) / 100;
2201     }
2202
2203   if (node_growth_cache)
2204     {
2205       if ((int)VEC_length (int, node_growth_cache) <= node->uid)
2206         VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
2207       VEC_replace (int, node_growth_cache, node->uid, growth + (growth >= 0));
2208     }
2209   return growth;
2210 }
2211
2212
2213 /* This function performs intraprocedural analysis in NODE that is required to
2214    inline indirect calls.  */
2215
2216 static void
2217 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
2218 {
2219   ipa_analyze_node (node);
2220   if (dump_file && (dump_flags & TDF_DETAILS))
2221     {
2222       ipa_print_node_params (dump_file, node);
2223       ipa_print_node_jump_functions (dump_file, node);
2224     }
2225 }
2226
2227
2228 /* Note function body size.  */
2229
2230 static void
2231 inline_analyze_function (struct cgraph_node *node)
2232 {
2233   push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2234   current_function_decl = node->decl;
2235
2236   if (dump_file)
2237     fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
2238              cgraph_node_name (node), node->uid);
2239   /* FIXME: We should remove the optimize check after we ensure we never run
2240      IPA passes when not optimizing.  */
2241   if (flag_indirect_inlining && optimize && !node->thunk.thunk_p)
2242     inline_indirect_intraprocedural_analysis (node);
2243   compute_inline_parameters (node, false);
2244
2245   current_function_decl = NULL;
2246   pop_cfun ();
2247 }
2248
2249
2250 /* Called when new function is inserted to callgraph late.  */
2251
2252 static void
2253 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2254 {
2255   inline_analyze_function (node);
2256 }
2257
2258
2259 /* Note function body size.  */
2260
2261 void
2262 inline_generate_summary (void)
2263 {
2264   struct cgraph_node *node;
2265
2266   function_insertion_hook_holder =
2267       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2268
2269   if (flag_indirect_inlining)
2270     ipa_register_cgraph_hooks ();
2271
2272   FOR_EACH_DEFINED_FUNCTION (node)
2273       inline_analyze_function (node);
2274 }
2275
2276
2277 /* Read predicate from IB.  */
2278
2279 static struct predicate
2280 read_predicate (struct lto_input_block *ib)
2281 {
2282   struct predicate out;
2283   clause_t clause;
2284   int k = 0;
2285
2286   do 
2287     {
2288       gcc_assert (k <= MAX_CLAUSES);
2289       clause = out.clause[k++] = lto_input_uleb128 (ib);
2290     }
2291   while (clause);
2292   return out;
2293 }
2294
2295
2296 /* Write inline summary for edge E to OB.  */
2297
2298 static void
2299 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
2300 {
2301   struct inline_edge_summary *es = inline_edge_summary (e);
2302   struct predicate p;
2303
2304   es->call_stmt_size = lto_input_uleb128 (ib);
2305   es->call_stmt_time = lto_input_uleb128 (ib);
2306   es->loop_depth = lto_input_uleb128 (ib);
2307   p = read_predicate (ib);
2308   edge_set_predicate (e, &p);
2309 }
2310
2311
2312 /* Stream in inline summaries from the section.  */
2313
2314 static void
2315 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
2316                      size_t len)
2317 {
2318   const struct lto_function_header *header =
2319     (const struct lto_function_header *) data;
2320   const int32_t cfg_offset = sizeof (struct lto_function_header);
2321   const int32_t main_offset = cfg_offset + header->cfg_size;
2322   const int32_t string_offset = main_offset + header->main_size;
2323   struct data_in *data_in;
2324   struct lto_input_block ib;
2325   unsigned int i, count2, j;
2326   unsigned int f_count;
2327
2328   LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
2329                         header->main_size);
2330
2331   data_in =
2332     lto_data_in_create (file_data, (const char *) data + string_offset,
2333                         header->string_size, NULL);
2334   f_count = lto_input_uleb128 (&ib);
2335   for (i = 0; i < f_count; i++)
2336     {
2337       unsigned int index;
2338       struct cgraph_node *node;
2339       struct inline_summary *info;
2340       lto_cgraph_encoder_t encoder;
2341       struct bitpack_d bp;
2342       struct cgraph_edge *e;
2343
2344       index = lto_input_uleb128 (&ib);
2345       encoder = file_data->cgraph_node_encoder;
2346       node = lto_cgraph_encoder_deref (encoder, index);
2347       info = inline_summary (node);
2348
2349       info->estimated_stack_size
2350         = info->estimated_self_stack_size = lto_input_uleb128 (&ib);
2351       info->size = info->self_size = lto_input_uleb128 (&ib);
2352       info->time = info->self_time = lto_input_uleb128 (&ib);
2353
2354       bp = lto_input_bitpack (&ib);
2355       info->inlinable = bp_unpack_value (&bp, 1);
2356       info->versionable = bp_unpack_value (&bp, 1);
2357
2358       count2 = lto_input_uleb128 (&ib);
2359       gcc_assert (!info->conds);
2360       for (j = 0; j < count2; j++)
2361         {
2362           struct condition c;
2363           c.operand_num = lto_input_uleb128 (&ib);
2364           c.code = (enum tree_code) lto_input_uleb128 (&ib);
2365           c.val = lto_input_tree (&ib, data_in);
2366           VEC_safe_push (condition, gc, info->conds, &c);
2367         }
2368       count2 = lto_input_uleb128 (&ib);
2369       gcc_assert (!info->entry);
2370       for (j = 0; j < count2; j++)
2371         {
2372           struct size_time_entry e;
2373
2374           e.size = lto_input_uleb128 (&ib);
2375           e.time = lto_input_uleb128 (&ib);
2376           e.predicate = read_predicate (&ib);
2377
2378           VEC_safe_push (size_time_entry, gc, info->entry, &e);
2379         }
2380       for (e = node->callees; e; e = e->next_callee)
2381         read_inline_edge_summary (&ib, e);
2382       for (e = node->indirect_calls; e; e = e->next_callee)
2383         read_inline_edge_summary (&ib, e);
2384     }
2385
2386   lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
2387                          len);
2388   lto_data_in_delete (data_in);
2389 }
2390
2391
2392 /* Read inline summary.  Jump functions are shared among ipa-cp
2393    and inliner, so when ipa-cp is active, we don't need to write them
2394    twice.  */
2395
2396 void
2397 inline_read_summary (void)
2398 {
2399   struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2400   struct lto_file_decl_data *file_data;
2401   unsigned int j = 0;
2402
2403   inline_summary_alloc ();
2404
2405   while ((file_data = file_data_vec[j++]))
2406     {
2407       size_t len;
2408       const char *data = lto_get_section_data (file_data, LTO_section_inline_summary, NULL, &len);
2409       if (data)
2410         inline_read_section (file_data, data, len);
2411       else
2412         /* Fatal error here.  We do not want to support compiling ltrans units with
2413            different version of compiler or different flags than the WPA unit, so
2414            this should never happen.  */
2415         fatal_error ("ipa inline summary is missing in input file");
2416     }
2417   if (flag_indirect_inlining)
2418     {
2419       ipa_register_cgraph_hooks ();
2420       if (!flag_ipa_cp)
2421         ipa_prop_read_jump_functions ();
2422     }
2423   function_insertion_hook_holder =
2424       cgraph_add_function_insertion_hook (&add_new_function, NULL);
2425 }
2426
2427
2428 /* Write predicate P to OB.  */
2429
2430 static void
2431 write_predicate (struct output_block *ob, struct predicate *p)
2432 {
2433   int j;
2434   if (p)
2435     for (j = 0; p->clause[j]; j++)
2436       {
2437          gcc_assert (j < MAX_CLAUSES);
2438          lto_output_uleb128_stream (ob->main_stream,
2439                                     p->clause[j]);
2440       }
2441   lto_output_uleb128_stream (ob->main_stream, 0);
2442 }
2443
2444
2445 /* Write inline summary for edge E to OB.  */
2446
2447 static void
2448 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
2449 {
2450   struct inline_edge_summary *es = inline_edge_summary (e);
2451   lto_output_uleb128_stream (ob->main_stream, es->call_stmt_size);
2452   lto_output_uleb128_stream (ob->main_stream, es->call_stmt_time);
2453   lto_output_uleb128_stream (ob->main_stream, es->loop_depth);
2454   write_predicate (ob, es->predicate);
2455 }
2456
2457
2458 /* Write inline summary for node in SET.
2459    Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2460    active, we don't need to write them twice.  */
2461
2462 void
2463 inline_write_summary (cgraph_node_set set,
2464                       varpool_node_set vset ATTRIBUTE_UNUSED)
2465 {
2466   struct cgraph_node *node;
2467   struct output_block *ob = create_output_block (LTO_section_inline_summary);
2468   lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2469   unsigned int count = 0;
2470   int i;
2471
2472   for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2473     if (lto_cgraph_encoder_deref (encoder, i)->analyzed)
2474       count++;
2475   lto_output_uleb128_stream (ob->main_stream, count);
2476
2477   for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2478     {
2479       node = lto_cgraph_encoder_deref (encoder, i);
2480       if (node->analyzed)
2481         {
2482           struct inline_summary *info = inline_summary (node);
2483           struct bitpack_d bp;
2484           struct cgraph_edge *edge;
2485           int i;
2486           size_time_entry *e;
2487           struct condition *c;
2488           
2489
2490           lto_output_uleb128_stream (ob->main_stream,
2491                                      lto_cgraph_encoder_encode (encoder, node));
2492           lto_output_sleb128_stream (ob->main_stream,
2493                                      info->estimated_self_stack_size);
2494           lto_output_sleb128_stream (ob->main_stream,
2495                                      info->self_size);
2496           lto_output_sleb128_stream (ob->main_stream,
2497                                      info->self_time);
2498           bp = bitpack_create (ob->main_stream);
2499           bp_pack_value (&bp, info->inlinable, 1);
2500           bp_pack_value (&bp, info->versionable, 1);
2501           lto_output_bitpack (&bp);
2502           lto_output_uleb128_stream (ob->main_stream,
2503                                      VEC_length (condition, info->conds));
2504           for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
2505             {
2506               lto_output_uleb128_stream (ob->main_stream,
2507                                          c->operand_num);
2508               lto_output_uleb128_stream (ob->main_stream,
2509                                          c->code);
2510               lto_output_tree (ob, c->val, true);
2511             }
2512           lto_output_uleb128_stream (ob->main_stream,
2513                                      VEC_length (size_time_entry, info->entry));
2514           for (i = 0;
2515                VEC_iterate (size_time_entry, info->entry, i, e);
2516                i++)
2517             {
2518               lto_output_uleb128_stream (ob->main_stream,
2519                                          e->size);
2520               lto_output_uleb128_stream (ob->main_stream,
2521                                          e->time);
2522               write_predicate (ob, &e->predicate);
2523             }
2524           for (edge = node->callees; edge; edge = edge->next_callee)
2525             write_inline_edge_summary (ob, edge);
2526           for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2527             write_inline_edge_summary (ob, edge);
2528         }
2529     }
2530   lto_output_1_stream (ob->main_stream, 0);
2531   produce_asm (ob, NULL);
2532   destroy_output_block (ob);
2533
2534   if (flag_indirect_inlining && !flag_ipa_cp)
2535     ipa_prop_write_jump_functions (set);
2536 }
2537
2538
2539 /* Release inline summary.  */
2540
2541 void
2542 inline_free_summary (void)
2543 {
2544   if (function_insertion_hook_holder)
2545     cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2546   function_insertion_hook_holder = NULL;
2547   if (node_removal_hook_holder)
2548     cgraph_remove_node_removal_hook (node_removal_hook_holder);
2549   if (edge_removal_hook_holder)
2550     cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2551   node_removal_hook_holder = NULL;
2552   if (node_duplication_hook_holder)
2553     cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2554   if (edge_duplication_hook_holder)
2555     cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2556   node_duplication_hook_holder = NULL;
2557   VEC_free (inline_summary_t, gc, inline_summary_vec);
2558   inline_summary_vec = NULL;
2559   VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
2560   inline_edge_summary_vec = NULL;
2561   if (edge_predicate_pool)
2562     free_alloc_pool (edge_predicate_pool);
2563   edge_predicate_pool = 0;
2564 }