2011-04-29 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob8a4c883b1a399cb35d53db37118ddea9358e8a57
1 /* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
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.
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.
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/>. */
22 /* Analysis used by the inliner and other passes limiting code size growth.
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
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).
39 We provide accestor to the inline_summary datastructure and
40 basic logic updating the parameters when inlining is performed.
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.
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.
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.
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. */
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"
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
95 /* Number of bits in integer, but we really want to be stable across different
96 hosts. */
97 #define NUM_CONDITIONS 32
99 enum predicate_conditions
101 predicate_false_condition = 0,
102 predicate_not_inlined_condition = 1,
103 predicate_first_dynamic_condition = 2
106 /* Special condition code we use to represent test that operand is compile time
107 constant. */
108 #define IS_NOT_CONSTANT ERROR_MARK
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 *);
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;
129 /* Cached node/edge growths. */
130 VEC(int,heap) *node_growth_cache;
131 VEC(edge_growth_cache_entry,heap) *edge_growth_cache;
133 /* Edge predicates goes here. */
134 static alloc_pool edge_predicate_pool;
136 /* Return true predicate (tautology).
137 We represent it by empty list of clauses. */
139 static inline struct predicate
140 true_predicate (void)
142 struct predicate p;
143 p.clause[0]=0;
144 return p;
148 /* Return predicate testing single condition number COND. */
150 static inline struct predicate
151 single_cond_predicate (int cond)
153 struct predicate p;
154 p.clause[0]=1 << cond;
155 p.clause[1]=0;
156 return p;
160 /* Return false predicate. First clause require false condition. */
162 static inline struct predicate
163 false_predicate (void)
165 return single_cond_predicate (predicate_false_condition);
169 /* Return true if P is (false). */
171 static inline bool
172 true_predicate_p (struct predicate *p)
174 return !p->clause[0];
178 /* Return true if P is (false). */
180 static inline bool
181 false_predicate_p (struct predicate *p)
183 if (p->clause[0] == (1 << predicate_false_condition))
185 gcc_checking_assert (!p->clause[1]
186 && p->clause[0] == 1 << predicate_false_condition);
187 return true;
189 return false;
193 /* Return predicate that is set true when function is not inlined. */
194 static inline struct predicate
195 not_inlined_predicate (void)
197 return single_cond_predicate (predicate_not_inlined_condition);
201 /* Add condition to condition list CONDS. */
203 static struct predicate
204 add_condition (struct inline_summary *summary, int operand_num,
205 enum tree_code code, tree val)
207 int i;
208 struct condition *c;
209 struct condition new_cond;
211 for (i = 0; VEC_iterate (condition, summary->conds, i, c); i++)
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);
218 /* Too many conditions. Give up and return constant true. */
219 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
220 return true_predicate ();
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);
230 /* Add clause CLAUSE into the predicate. */
232 static inline void
233 add_clause (struct predicate *p, clause_t clause)
235 int i;
236 int insert_here = -1;
238 /* True clause. */
239 if (!clause)
240 return;
242 /* Flase clause makes the whole predicate false. Kill the other variants. */
243 if (clause == (1 << predicate_false_condition))
245 p->clause[0] = (1 << predicate_false_condition);
246 p->clause[1] = 0;
247 return;
249 if (false_predicate_p (p))
250 return;
251 gcc_assert (!(clause & (1 << predicate_false_condition)));
252 for (i = 0; i < MAX_CLAUSES - 1; i++)
254 if (p->clause[i] == clause)
255 return;
256 if (!p->clause[i])
257 break;
258 if (p->clause[i] < clause && !insert_here)
259 insert_here = i;
261 /* We run out of variants. Be conservative in positive direciton. */
262 if (i == MAX_CLAUSES)
263 return;
264 /* Keep clauses ordered by index, so equivalence testing is easy. */
265 p->clause[i + 1] = 0;
266 if (insert_here >= 0)
267 for (;i > insert_here; i--)
268 p->clause[i] = p->clause[i - 1];
269 else
270 insert_here = i;
271 p->clause[insert_here] = clause;
275 /* Return P & P2. */
277 static struct predicate
278 and_predicates (struct predicate *p, struct predicate *p2)
280 struct predicate out = *p;
281 int i;
283 for (i = 0; p2->clause[i]; i++)
285 gcc_checking_assert (i < MAX_CLAUSES);
286 add_clause (&out, p2->clause[i]);
288 return out;
292 /* Return P | P2. */
294 static struct predicate
295 or_predicates (struct predicate *p, struct predicate *p2)
297 struct predicate out = true_predicate ();
298 int i,j;
300 /* If one of conditions is false, return the other. */
301 if (false_predicate_p (p2))
302 return *p;
303 if (false_predicate_p (p))
304 return *p2;
305 for (i = 0; p->clause[i]; i++)
306 for (j = 0; p2->clause[j]; j++)
308 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
309 add_clause (&out, p->clause[i] | p2->clause[j]);
311 return out;
315 /* Return true if predicates are obviously equal. */
317 static inline bool
318 predicates_equal_p (struct predicate *p, struct predicate *p2)
320 int i;
321 for (i = 0; p->clause[i]; i++)
323 gcc_checking_assert (i < MAX_CLAUSES);
324 if (p->clause[i] != p2->clause[i])
325 return false;
327 return !p2->clause[i];
331 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false if predicate P
332 to be false. */
334 static bool
335 evaluate_predicate (struct predicate *p, clause_t possible_truths)
337 int i;
339 /* True remains true. */
340 if (true_predicate_p (p))
341 return true;
343 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
345 /* See if we can find clause we can disprove. */
346 for (i = 0; p->clause[i]; i++)
348 gcc_checking_assert (i < MAX_CLAUSES);
349 if (!(p->clause[i] & possible_truths))
350 return false;
352 return true;
356 /* Dump conditional COND. */
358 static void
359 dump_condition (FILE *f, conditions conditions, int cond)
361 condition *c;
362 if (cond == predicate_false_condition)
363 fprintf (f, "false");
364 else if (cond == predicate_not_inlined_condition)
365 fprintf (f, "not inlined");
366 else
368 c = VEC_index (condition, conditions, cond - predicate_first_dynamic_condition);
369 fprintf (f, "op%i", c->operand_num);
370 if (c->code == IS_NOT_CONSTANT)
372 fprintf (f, " not constant");
373 return;
375 fprintf (f, " %s ", op_symbol_code (c->code));
376 print_generic_expr (f, c->val, 1);
381 /* Dump clause CLAUSE. */
383 static void
384 dump_clause (FILE *f, conditions conds, clause_t clause)
386 int i;
387 bool found = false;
388 fprintf (f, "(");
389 if (!clause)
390 fprintf (f, "true");
391 for (i = 0; i < NUM_CONDITIONS; i++)
392 if (clause & (1 << i))
394 if (found)
395 fprintf (f, " || ");
396 found = true;
397 dump_condition (f, conds, i);
399 fprintf (f, ")");
403 /* Dump predicate PREDICATE. */
405 static void
406 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
408 int i;
409 if (true_predicate_p (pred))
410 dump_clause (f, conds, 0);
411 else
412 for (i = 0; pred->clause[i]; i++)
414 if (i)
415 fprintf (f, " && ");
416 dump_clause (f, conds, pred->clause[i]);
418 fprintf (f, "\n");
422 /* Record SIZE and TIME under condition PRED into the inline summary. */
424 static void
425 account_size_time (struct inline_summary *summary, int size, int time, struct predicate *pred)
427 size_time_entry *e;
428 bool found = false;
429 int i;
431 if (false_predicate_p (pred))
432 return;
434 /* We need to create initial empty unconitional clause, but otherwie
435 we don't need to account empty times and sizes. */
436 if (!size && !time && summary->conds)
437 return;
439 /* Watch overflow that might result from insane profiles. */
440 if (time > MAX_TIME * INLINE_TIME_SCALE)
441 time = MAX_TIME * INLINE_TIME_SCALE;
442 gcc_assert (time >= 0);
444 for (i = 0; VEC_iterate (size_time_entry, summary->entry, i, e); i++)
445 if (predicates_equal_p (&e->predicate, pred))
447 found = true;
448 break;
450 if (i == 32)
452 i = 0;
453 found = true;
454 e = VEC_index (size_time_entry, summary->entry, 0);
455 gcc_assert (!e->predicate.clause[0]);
457 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
459 fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
460 ((double)size) / INLINE_SIZE_SCALE, ((double)time) / INLINE_TIME_SCALE,
461 found ? "" : "new ");
462 dump_predicate (dump_file, summary->conds, pred);
464 if (!found)
466 struct size_time_entry new_entry;
467 new_entry.size = size;
468 new_entry.time = time;
469 new_entry.predicate = *pred;
470 VEC_safe_push (size_time_entry, gc, summary->entry, &new_entry);
472 else
474 e->size += size;
475 e->time += time;
476 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
477 e->time = MAX_TIME * INLINE_TIME_SCALE;
481 /* Set predicate for edge E. */
483 static void
484 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
486 struct inline_edge_summary *es = inline_edge_summary (e);
487 if (predicate && !true_predicate_p (predicate))
489 if (!es->predicate)
490 es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
491 *es->predicate = *predicate;
493 else
495 if (es->predicate)
496 pool_free (edge_predicate_pool, es->predicate);
497 es->predicate = NULL;
502 /* Work out what conditions might be true at invocation of E. */
504 static clause_t
505 evaluate_conditions_for_edge (struct cgraph_edge *e, bool inline_p)
507 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
508 struct inline_summary *info = inline_summary (e->callee);
509 int i;
511 if (ipa_node_params_vector && info->conds
512 /* FIXME: it seems that we forget to get argument count in some cases,
513 probaby for previously indirect edges or so. */
514 && ipa_get_cs_argument_count (IPA_EDGE_REF (e)))
516 struct ipa_node_params *parms_info;
517 struct ipa_edge_args *args = IPA_EDGE_REF (e);
518 int i, count = ipa_get_cs_argument_count (args);
519 struct ipcp_lattice lat;
520 struct condition *c;
521 VEC (tree, heap) *known_vals = NULL;
523 if (e->caller->global.inlined_to)
524 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
525 else
526 parms_info = IPA_NODE_REF (e->caller);
528 VEC_safe_grow_cleared (tree, heap, known_vals, count);
529 for (i = 0; i < count; i++)
531 ipa_lattice_from_jfunc (parms_info, &lat, ipa_get_ith_jump_func (args, i));
532 if (lat.type == IPA_CONST_VALUE)
533 VEC_replace (tree, known_vals, i, lat.constant);
535 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
537 tree val = VEC_index (tree, known_vals, c->operand_num);
538 tree res;
540 if (!val)
542 clause |= 1 << (i + predicate_first_dynamic_condition);
543 continue;
545 if (c->code == IS_NOT_CONSTANT)
546 continue;
547 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
548 if (res
549 && integer_zerop (res))
550 continue;
551 clause |= 1 << (i + predicate_first_dynamic_condition);
553 VEC_free (tree, heap, known_vals);
555 else
556 for (i = 0; i < (int)VEC_length (condition, info->conds); i++)
557 clause |= 1 << (i + predicate_first_dynamic_condition);
559 return clause;
563 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
565 static void
566 inline_summary_alloc (void)
568 if (!node_removal_hook_holder)
569 node_removal_hook_holder =
570 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
571 if (!edge_removal_hook_holder)
572 edge_removal_hook_holder =
573 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
574 if (!node_duplication_hook_holder)
575 node_duplication_hook_holder =
576 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
577 if (!edge_duplication_hook_holder)
578 edge_duplication_hook_holder =
579 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
581 if (VEC_length (inline_summary_t, inline_summary_vec)
582 <= (unsigned) cgraph_max_uid)
583 VEC_safe_grow_cleared (inline_summary_t, gc,
584 inline_summary_vec, cgraph_max_uid + 1);
585 if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
586 <= (unsigned) cgraph_edge_max_uid)
587 VEC_safe_grow_cleared (inline_edge_summary_t, heap,
588 inline_edge_summary_vec, cgraph_edge_max_uid + 1);
589 if (!edge_predicate_pool)
590 edge_predicate_pool = create_alloc_pool ("edge predicates", sizeof (struct predicate),
591 10);
594 /* Hook that is called by cgraph.c when a node is removed. */
596 static void
597 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
599 struct inline_summary *info;
600 if (VEC_length (inline_summary_t, inline_summary_vec)
601 <= (unsigned)node->uid)
602 return;
603 info = inline_summary (node);
604 reset_node_growth_cache (node);
605 VEC_free (condition, gc, info->conds);
606 VEC_free (size_time_entry, gc, info->entry);
607 info->conds = NULL;
608 info->entry = NULL;
609 memset (info, 0, sizeof (inline_summary_t));
613 /* Hook that is called by cgraph.c when a node is duplicated. */
615 static void
616 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
617 ATTRIBUTE_UNUSED void *data)
619 struct inline_summary *info;
620 inline_summary_alloc ();
621 info = inline_summary (dst);
622 memcpy (info, inline_summary (src),
623 sizeof (struct inline_summary));
624 info->conds = VEC_copy (condition, gc, info->conds);
625 info->entry = VEC_copy (size_time_entry, gc, info->entry);
629 /* Hook that is called by cgraph.c when a node is duplicated. */
631 static void
632 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
633 ATTRIBUTE_UNUSED void *data)
635 struct inline_edge_summary *info;
636 struct inline_edge_summary *srcinfo;
637 inline_summary_alloc ();
638 info = inline_edge_summary (dst);
639 srcinfo = inline_edge_summary (src);
640 memcpy (info, srcinfo,
641 sizeof (struct inline_edge_summary));
642 info->predicate = NULL;
643 edge_set_predicate (dst, srcinfo->predicate);
647 /* Keep edge cache consistent across edge removal. */
649 static void
650 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
652 if (edge_growth_cache)
653 reset_edge_growth_cache (edge);
654 if (edge->uid < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
656 edge_set_predicate (edge, NULL);
657 memset (inline_edge_summary (edge), 0, sizeof (struct inline_edge_summary));
662 /* Initialize growth caches. */
664 void
665 initialize_growth_caches (void)
667 if (cgraph_edge_max_uid)
668 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
669 cgraph_edge_max_uid);
670 if (cgraph_max_uid)
671 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
675 /* Free growth caches. */
677 void
678 free_growth_caches (void)
680 VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
681 edge_growth_cache = 0;
682 VEC_free (int, heap, node_growth_cache);
683 node_growth_cache = 0;
687 /* Dump edge summaries associated to NODE and recursively to all clones.
688 Indent by INDENT. */
690 static void
691 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
692 struct inline_summary *info)
694 struct cgraph_edge *edge;
695 for (edge = node->callees; edge; edge = edge->next_callee)
697 struct inline_edge_summary *es = inline_edge_summary (edge);
698 fprintf (f, "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i time: %2i",
699 indent, "", cgraph_node_name (edge->callee),
700 edge->callee->uid,
701 !edge->inline_failed ? "inlined"
702 : cgraph_inline_failed_string (edge->inline_failed),
703 indent, "",
704 es->loop_depth,
705 edge->frequency,
706 es->call_stmt_size,
707 es->call_stmt_time);
708 if (es->predicate)
710 fprintf (f, " predicate: ");
711 dump_predicate (f, info->conds, es->predicate);
713 else
714 fprintf (f, "\n");
715 if (!edge->inline_failed)
716 dump_inline_edge_summary (f, indent+2, edge->callee, info);
718 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
720 struct inline_edge_summary *es = inline_edge_summary (edge);
721 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i time: %2i\n",
722 indent, "",
723 es->loop_depth,
724 edge->frequency,
725 es->call_stmt_size,
726 es->call_stmt_time);
727 if (es->predicate)
729 fprintf (f, "predicate: ");
730 dump_predicate (f, info->conds, es->predicate);
732 else
733 fprintf (f, "\n");
738 static void
739 dump_inline_summary (FILE * f, struct cgraph_node *node)
741 if (node->analyzed)
743 struct inline_summary *s = inline_summary (node);
744 size_time_entry *e;
745 int i;
746 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
747 node->uid);
748 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
749 fprintf (f, " always_inline");
750 if (s->inlinable)
751 fprintf (f, " inlinable");
752 if (s->versionable)
753 fprintf (f, " versionable");
754 fprintf (f, "\n self time: %i\n",
755 s->self_time);
756 fprintf (f, " global time: %i\n", s->time);
757 fprintf (f, " self size: %i\n",
758 s->self_size);
759 fprintf (f, " global size: %i\n", s->size);
760 fprintf (f, " self stack: %i\n",
761 (int) s->estimated_self_stack_size);
762 fprintf (f, " global stack: %i\n",
763 (int) s->estimated_stack_size);
764 for (i = 0;
765 VEC_iterate (size_time_entry, s->entry, i, e);
766 i++)
768 fprintf (f, " size:%f, time:%f, predicate:",
769 (double) e->size / INLINE_SIZE_SCALE,
770 (double) e->time / INLINE_TIME_SCALE);
771 dump_predicate (f, s->conds, &e->predicate);
773 fprintf (f, " calls:\n");
774 dump_inline_edge_summary (f, 4, node, s);
775 fprintf (f, "\n");
779 void
780 debug_inline_summary (struct cgraph_node *node)
782 dump_inline_summary (stderr, node);
785 void
786 dump_inline_summaries (FILE *f)
788 struct cgraph_node *node;
790 for (node = cgraph_nodes; node; node = node->next)
791 if (node->analyzed && !node->global.inlined_to)
792 dump_inline_summary (f, node);
795 /* Give initial reasons why inlining would fail on EDGE. This gets either
796 nullified or usually overwritten by more precise reasons later. */
798 void
799 initialize_inline_failed (struct cgraph_edge *e)
801 struct cgraph_node *callee = e->callee;
803 if (e->indirect_unknown_callee)
804 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
805 else if (!callee->analyzed)
806 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
807 else if (callee->local.redefined_extern_inline)
808 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
809 else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
810 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
811 else
812 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
815 /* See if statement might disappear after inlining.
816 0 - means not eliminated
817 1 - half of statements goes away
818 2 - for sure it is eliminated.
819 We are not terribly sophisticated, basically looking for simple abstraction
820 penalty wrappers. */
822 static int
823 eliminated_by_inlining_prob (gimple stmt)
825 enum gimple_code code = gimple_code (stmt);
826 switch (code)
828 case GIMPLE_RETURN:
829 return 2;
830 case GIMPLE_ASSIGN:
831 if (gimple_num_ops (stmt) != 2)
832 return 0;
834 /* Casts of parameters, loads from parameters passed by reference
835 and stores to return value or parameters are often free after
836 inlining dua to SRA and further combining.
837 Assume that half of statements goes away. */
838 if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
839 || gimple_assign_rhs_code (stmt) == NOP_EXPR
840 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
841 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
843 tree rhs = gimple_assign_rhs1 (stmt);
844 tree lhs = gimple_assign_lhs (stmt);
845 tree inner_rhs = rhs;
846 tree inner_lhs = lhs;
847 bool rhs_free = false;
848 bool lhs_free = false;
850 while (handled_component_p (inner_lhs)
851 || TREE_CODE (inner_lhs) == MEM_REF)
852 inner_lhs = TREE_OPERAND (inner_lhs, 0);
853 while (handled_component_p (inner_rhs)
854 || TREE_CODE (inner_rhs) == ADDR_EXPR
855 || TREE_CODE (inner_rhs) == MEM_REF)
856 inner_rhs = TREE_OPERAND (inner_rhs, 0);
859 if (TREE_CODE (inner_rhs) == PARM_DECL
860 || (TREE_CODE (inner_rhs) == SSA_NAME
861 && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
862 && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
863 rhs_free = true;
864 if (rhs_free && is_gimple_reg (lhs))
865 lhs_free = true;
866 if (((TREE_CODE (inner_lhs) == PARM_DECL
867 || (TREE_CODE (inner_lhs) == SSA_NAME
868 && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
869 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
870 && inner_lhs != lhs)
871 || TREE_CODE (inner_lhs) == RESULT_DECL
872 || (TREE_CODE (inner_lhs) == SSA_NAME
873 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
874 lhs_free = true;
875 if (lhs_free
876 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
877 rhs_free = true;
878 if (lhs_free && rhs_free)
879 return 1;
881 return 0;
882 default:
883 return 0;
888 /* Return predicate that must be true when is E executable. */
890 static struct predicate
891 edge_execution_predicate (struct ipa_node_params *info,
892 struct inline_summary *summary,
893 edge e)
895 struct predicate p = true_predicate ();
896 gimple last;
897 tree op;
898 int index;
899 enum tree_code code;
901 if (e->src == ENTRY_BLOCK_PTR)
902 return p;
904 last = last_stmt (e->src);
905 /* TODO: handle switch. */
906 if (!last
907 || gimple_code (last) != GIMPLE_COND)
908 return p;
909 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
910 return p;
911 op = gimple_cond_lhs (last);
912 /* TODO: handle conditionals like
913 var = op0 < 4;
914 if (var != 0)
915 and __bulitin_constant_p. */
916 if (TREE_CODE (op) != SSA_NAME
917 || !SSA_NAME_IS_DEFAULT_DEF (op))
918 return p;
919 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
920 if (index == -1)
921 return p;
922 code = gimple_cond_code (last);
924 if (EDGE_TRUE_VALUE)
925 code = invert_tree_comparison (code,
926 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
928 return add_condition (summary,
929 index,
930 gimple_cond_code (last),
931 gimple_cond_rhs (last));
935 /* We keep info about constantness of SSA names. */
937 typedef struct predicate predicate_t;
938 DEF_VEC_O (predicate_t);
939 DEF_VEC_ALLOC_O (predicate_t, heap);
942 /* Return predicate specifying when the STMT might have result that is not a compile
943 time constant. */
945 static struct predicate
946 will_be_nonconstant_predicate (struct ipa_node_params *info,
947 struct inline_summary *summary,
948 gimple stmt,
949 VEC (predicate_t, heap) *nonconstant_names)
952 struct predicate p = true_predicate ();
953 ssa_op_iter iter;
954 tree use;
955 struct predicate op_non_const;
957 /* What statments might be optimized away
958 when their arguments are constant
959 TODO: also trivial builtins, especially builtin_constant_p. */
960 if (gimple_code (stmt) != GIMPLE_ASSIGN
961 && gimple_code (stmt) != GIMPLE_COND
962 && gimple_code (stmt) != GIMPLE_SWITCH)
963 return p;
965 /* Stores and loads will stay anyway.
966 TODO: Constant memory accesses could be handled here, too. */
967 if (gimple_vuse (stmt))
968 return p;
970 /* See if we understand all operands before we start
971 adding conditionals. */
972 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
974 if (TREE_CODE (use) != SSA_NAME)
975 return p;
976 /* For arguments we can build a condition. */
977 if (SSA_NAME_IS_DEFAULT_DEF (use)
978 && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
979 continue;
980 /* If we know when operand is constant,
981 we still can say something useful. */
982 if (!true_predicate_p (VEC_index (predicate_t, nonconstant_names,
983 SSA_NAME_VERSION (use))))
984 continue;
985 return p;
987 op_non_const = false_predicate ();
988 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
990 if (SSA_NAME_IS_DEFAULT_DEF (use)
991 && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
992 p = add_condition (summary,
993 ipa_get_param_decl_index (info, SSA_NAME_VAR (use)),
994 IS_NOT_CONSTANT, NULL);
995 else
996 p = *VEC_index (predicate_t, nonconstant_names,
997 SSA_NAME_VERSION (use));
998 op_non_const = or_predicates (&p, &op_non_const);
1000 if (gimple_code (stmt) == GIMPLE_ASSIGN
1001 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
1002 VEC_replace (predicate_t, nonconstant_names,
1003 SSA_NAME_VERSION (gimple_assign_lhs (stmt)), &op_non_const);
1004 return op_non_const;
1008 /* Compute function body size parameters for NODE.
1009 When EARLY is true, we compute only simple summaries without
1010 non-trivial predicates to drive the early inliner. */
1012 static void
1013 estimate_function_body_sizes (struct cgraph_node *node, bool early)
1015 gcov_type time = 0;
1016 /* Estimate static overhead for function prologue/epilogue and alignment. */
1017 int size = 2;
1018 /* Benefits are scaled by probability of elimination that is in range
1019 <0,2>. */
1020 basic_block bb;
1021 gimple_stmt_iterator bsi;
1022 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1023 int freq;
1024 struct inline_summary *info = inline_summary (node);
1025 struct predicate bb_predicate;
1026 struct ipa_node_params *parms_info = NULL;
1027 VEC (predicate_t, heap) *nonconstant_names = NULL;
1029 if (ipa_node_params_vector && !early && optimize)
1031 parms_info = IPA_NODE_REF (node);
1032 VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
1033 VEC_length (tree, SSANAMES (my_function)));
1036 info->conds = 0;
1037 info->entry = 0;
1040 if (dump_file)
1041 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
1042 cgraph_node_name (node));
1044 /* When we run into maximal number of entries, we assign everything to the
1045 constant truth case. Be sure to have it in list. */
1046 bb_predicate = true_predicate ();
1047 account_size_time (info, 0, 0, &bb_predicate);
1049 bb_predicate = not_inlined_predicate ();
1050 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
1052 gcc_assert (my_function && my_function->cfg);
1053 FOR_EACH_BB_FN (bb, my_function)
1055 edge e;
1056 edge_iterator ei;
1058 freq = compute_call_stmt_bb_frequency (node->decl, bb);
1060 /* TODO: Obviously predicates can be propagated down across CFG. */
1061 if (parms_info)
1063 bb_predicate = false_predicate ();
1064 FOR_EACH_EDGE (e, ei, bb->preds)
1066 struct predicate ep;
1067 ep = edge_execution_predicate (parms_info, info, e);
1068 bb_predicate = or_predicates (&ep, &bb_predicate);
1071 else
1072 bb_predicate = true_predicate ();
1074 if (dump_file && (dump_flags & TDF_DETAILS))
1076 fprintf (dump_file, "\n BB %i predicate:", bb->index);
1077 dump_predicate (dump_file, info->conds, &bb_predicate);
1080 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1082 gimple stmt = gsi_stmt (bsi);
1083 int this_size = estimate_num_insns (stmt, &eni_size_weights);
1084 int this_time = estimate_num_insns (stmt, &eni_time_weights);
1085 int prob;
1087 if (dump_file && (dump_flags & TDF_DETAILS))
1089 fprintf (dump_file, " ");
1090 print_gimple_stmt (dump_file, stmt, 0, 0);
1091 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
1092 ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
1095 if (is_gimple_call (stmt))
1097 struct cgraph_edge *edge = cgraph_edge (node, stmt);
1098 struct inline_edge_summary *es = inline_edge_summary (edge);
1100 /* Special case: results of BUILT_IN_CONSTANT_P will be always
1101 resolved as constant. We however don't want to optimize
1102 out the cgraph edges. */
1103 if (nonconstant_names
1104 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
1105 && gimple_call_lhs (stmt)
1106 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
1108 struct predicate false_p = false_predicate ();
1109 VEC_replace (predicate_t, nonconstant_names,
1110 SSA_NAME_VERSION (gimple_call_lhs (stmt)), &false_p);
1113 es->call_stmt_size = this_size;
1114 es->call_stmt_time = this_time;
1115 es->loop_depth = bb->loop_depth;
1116 edge_set_predicate (edge, &bb_predicate);
1118 /* Do not inline calls where we cannot triviall work around
1119 mismatches in argument or return types. */
1120 if (edge->callee
1121 && !gimple_check_call_matching_types (stmt, edge->callee->decl))
1123 edge->call_stmt_cannot_inline_p = true;
1124 gimple_call_set_cannot_inline (stmt, true);
1126 else
1127 gcc_assert (!gimple_call_cannot_inline_p (stmt));
1130 if (this_time || this_size)
1132 struct predicate will_be_nonconstant;
1133 struct predicate p;
1135 this_time *= freq;
1136 time += this_time;
1137 size += this_size;
1139 prob = eliminated_by_inlining_prob (stmt);
1140 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1141 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
1142 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1143 fprintf (dump_file, "\t\twill eliminated by inlining\n");
1145 if (parms_info)
1147 will_be_nonconstant
1148 = will_be_nonconstant_predicate (parms_info, info,
1149 stmt, nonconstant_names);
1150 p = and_predicates (&bb_predicate, &will_be_nonconstant);
1152 else
1153 p = true_predicate ();
1155 /* We account everything but the calls. Calls have their own
1156 size/time info attached to cgraph edges. This is neccesary
1157 in order to make the cost disappear after inlining. */
1158 if (!is_gimple_call (stmt))
1160 if (prob)
1162 struct predicate ip = not_inlined_predicate ();
1163 ip = and_predicates (&ip, &p);
1164 account_size_time (info, this_size * prob,
1165 this_time * prob, &ip);
1167 if (prob != 2)
1168 account_size_time (info, this_size * (2 - prob),
1169 this_time * (2 - prob), &p);
1172 gcc_assert (time >= 0);
1173 gcc_assert (size >= 0);
1177 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1178 if (time > MAX_TIME)
1179 time = MAX_TIME;
1180 inline_summary (node)->self_time = time;
1181 inline_summary (node)->self_size = size;
1182 VEC_free (predicate_t, heap, nonconstant_names);
1183 if (dump_file)
1185 fprintf (dump_file, "\n");
1186 dump_inline_summary (dump_file, node);
1191 /* Compute parameters of functions used by inliner.
1192 EARLY is true when we compute parameters for the early inliner */
1194 void
1195 compute_inline_parameters (struct cgraph_node *node, bool early)
1197 HOST_WIDE_INT self_stack_size;
1198 struct cgraph_edge *e;
1199 struct inline_summary *info;
1201 gcc_assert (!node->global.inlined_to);
1203 inline_summary_alloc ();
1205 info = inline_summary (node);
1207 /* Estimate the stack size for the function if we're optimizing. */
1208 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
1209 info->estimated_self_stack_size = self_stack_size;
1210 info->estimated_stack_size = self_stack_size;
1211 info->stack_frame_offset = 0;
1213 /* Can this function be inlined at all? */
1214 info->inlinable = tree_inlinable_function_p (node->decl);
1216 /* Inlinable functions always can change signature. */
1217 if (info->inlinable)
1218 node->local.can_change_signature = true;
1219 else
1221 /* Functions calling builtin_apply can not change signature. */
1222 for (e = node->callees; e; e = e->next_callee)
1223 if (DECL_BUILT_IN (e->callee->decl)
1224 && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
1225 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_APPLY_ARGS)
1226 break;
1227 node->local.can_change_signature = !e;
1229 estimate_function_body_sizes (node, early);
1231 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
1232 info->time = info->self_time;
1233 info->size = info->self_size;
1234 info->stack_frame_offset = 0;
1235 info->estimated_stack_size = info->estimated_self_stack_size;
1239 /* Compute parameters of functions used by inliner using
1240 current_function_decl. */
1242 static unsigned int
1243 compute_inline_parameters_for_current (void)
1245 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
1246 return 0;
1249 struct gimple_opt_pass pass_inline_parameters =
1252 GIMPLE_PASS,
1253 "inline_param", /* name */
1254 NULL, /* gate */
1255 compute_inline_parameters_for_current,/* execute */
1256 NULL, /* sub */
1257 NULL, /* next */
1258 0, /* static_pass_number */
1259 TV_INLINE_HEURISTICS, /* tv_id */
1260 0, /* properties_required */
1261 0, /* properties_provided */
1262 0, /* properties_destroyed */
1263 0, /* todo_flags_start */
1264 0 /* todo_flags_finish */
1269 /* Increase SIZE and TIME for size and time needed to handle edge E. */
1271 static void
1272 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time)
1274 struct inline_edge_summary *es = inline_edge_summary (e);
1275 *size += es->call_stmt_size * INLINE_SIZE_SCALE;
1276 *time += (es->call_stmt_time
1277 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
1278 if (*time > MAX_TIME * INLINE_TIME_SCALE)
1279 *time = MAX_TIME * INLINE_TIME_SCALE;
1283 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE. */
1285 static void
1286 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
1287 clause_t possible_truths)
1289 struct cgraph_edge *e;
1290 for (e = node->callees; e; e = e->next_callee)
1292 struct inline_edge_summary *es = inline_edge_summary (e);
1293 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1295 if (e->inline_failed)
1296 estimate_edge_size_and_time (e, size, time);
1297 else
1298 estimate_calls_size_and_time (e->callee, size, time,
1299 possible_truths);
1302 /* TODO: look for devirtualizing oppurtunities. */
1303 for (e = node->indirect_calls; e; e = e->next_callee)
1305 struct inline_edge_summary *es = inline_edge_summary (e);
1306 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1307 estimate_edge_size_and_time (e, size, time);
1312 /* Estimate size and time needed to execute callee of EDGE assuming
1313 that parameters known to be constant at caller of EDGE are
1314 propagated. If INLINE_P is true, it is assumed that call will
1315 be inlined. */
1317 static void
1318 estimate_callee_size_and_time (struct cgraph_edge *edge, bool inline_p,
1319 int *ret_size, int *ret_time)
1321 struct inline_summary *info = inline_summary (edge->callee);
1322 clause_t clause = evaluate_conditions_for_edge (edge, inline_p);
1323 size_time_entry *e;
1324 int size = 0, time = 0;
1325 int i;
1327 if (dump_file
1328 && (dump_flags & TDF_DETAILS))
1330 bool found = false;
1331 fprintf (dump_file, " Estimating callee body: %s/%i\n"
1332 " Known to be false: ",
1333 cgraph_node_name (edge->callee),
1334 edge->callee->uid);
1336 for (i = predicate_not_inlined_condition;
1337 i < (predicate_first_dynamic_condition
1338 + (int)VEC_length (condition, info->conds)); i++)
1339 if (!(clause & (1 << i)))
1341 if (found)
1342 fprintf (dump_file, ", ");
1343 found = true;
1344 dump_condition (dump_file, info->conds, i);
1348 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
1349 if (evaluate_predicate (&e->predicate, clause))
1350 time += e->time, size += e->size;
1352 if (time > MAX_TIME * INLINE_TIME_SCALE)
1353 time = MAX_TIME * INLINE_TIME_SCALE;
1355 estimate_calls_size_and_time (edge->callee, &size, &time, clause);
1356 time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
1357 size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
1360 if (dump_file
1361 && (dump_flags & TDF_DETAILS))
1362 fprintf (dump_file, "\n size:%i time:%i\n", size, time);
1363 if (ret_time)
1364 *ret_time = time;
1365 if (ret_size)
1366 *ret_size = size;
1367 return;
1371 /* Translate all conditions from callee representation into caller representation and
1372 symbolically evaluate predicate P into new predicate.
1374 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO is summary
1375 of function predicate P is from. OPERAND_MAP is array giving callee formal IDs the
1376 caller formal IDs. POSSSIBLE_TRUTHS is clausule of all callee conditions that
1377 may be true in caller context. TOPLEV_PREDICATE is predicate under which callee
1378 is executed. */
1380 static struct predicate
1381 remap_predicate (struct inline_summary *info, struct inline_summary *callee_info,
1382 struct predicate *p,
1383 VEC (int, heap) *operand_map,
1384 clause_t possible_truths,
1385 struct predicate *toplev_predicate)
1387 int i;
1388 struct predicate out = true_predicate ();
1390 /* True predicate is easy. */
1391 if (true_predicate_p (p))
1392 return *toplev_predicate;
1393 for (i = 0; p->clause[i]; i++)
1395 clause_t clause = p->clause[i];
1396 int cond;
1397 struct predicate clause_predicate = false_predicate ();
1399 gcc_assert (i < MAX_CLAUSES);
1401 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
1402 /* Do we have condition we can't disprove? */
1403 if (clause & possible_truths & (1 << cond))
1405 struct predicate cond_predicate;
1406 /* Work out if the condition can translate to predicate in the
1407 inlined function. */
1408 if (cond >= predicate_first_dynamic_condition)
1410 struct condition *c;
1412 c = VEC_index (condition, callee_info->conds,
1413 cond - predicate_first_dynamic_condition);
1414 /* See if we can remap condition operand to caller's operand.
1415 Otherwise give up. */
1416 if (!operand_map
1417 || VEC_index (int, operand_map, c->operand_num) == -1)
1418 cond_predicate = true_predicate ();
1419 else
1420 cond_predicate = add_condition (info,
1421 VEC_index (int, operand_map,
1422 c->operand_num),
1423 c->code, c->val);
1425 /* Fixed conditions remains same, construct single
1426 condition predicate. */
1427 else
1429 cond_predicate.clause[0] = 1 << cond;
1430 cond_predicate.clause[1] = 0;
1432 clause_predicate = or_predicates (&clause_predicate, &cond_predicate);
1434 out = and_predicates (&out, &clause_predicate);
1436 return and_predicates (&out, toplev_predicate);
1440 /* Update summary information of inline clones after inlining.
1441 Compute peak stack usage. */
1443 static void
1444 inline_update_callee_summaries (struct cgraph_node *node,
1445 int depth)
1447 struct cgraph_edge *e;
1448 struct inline_summary *callee_info = inline_summary (node);
1449 struct inline_summary *caller_info = inline_summary (node->callers->caller);
1450 HOST_WIDE_INT peak;
1452 callee_info->stack_frame_offset
1453 = caller_info->stack_frame_offset
1454 + caller_info->estimated_self_stack_size;
1455 peak = callee_info->stack_frame_offset
1456 + callee_info->estimated_self_stack_size;
1457 if (inline_summary (node->global.inlined_to)->estimated_stack_size
1458 < peak)
1459 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
1460 cgraph_propagate_frequency (node);
1461 for (e = node->callees; e; e = e->next_callee)
1463 if (!e->inline_failed)
1464 inline_update_callee_summaries (e->callee, depth);
1465 inline_edge_summary (e)->loop_depth += depth;
1467 for (e = node->indirect_calls; e; e = e->next_callee)
1468 inline_edge_summary (e)->loop_depth += depth;
1472 /* Remap predicates of callees of NODE. Rest of arguments match
1473 remap_predicate. */
1475 static void
1476 remap_edge_predicates (struct cgraph_node *node,
1477 struct inline_summary *info,
1478 struct inline_summary *callee_info,
1479 VEC (int, heap) *operand_map,
1480 clause_t possible_truths,
1481 struct predicate *toplev_predicate)
1483 struct cgraph_edge *e;
1484 for (e = node->callees; e; e = e->next_callee)
1486 struct inline_edge_summary *es = inline_edge_summary (e);
1487 struct predicate p;
1488 if (es->predicate)
1490 p = remap_predicate (info, callee_info,
1491 es->predicate, operand_map, possible_truths,
1492 toplev_predicate);
1493 edge_set_predicate (e, &p);
1494 /* TODO: We should remove the edge for code that will be optimized out,
1495 but we need to keep verifiers and tree-inline happy.
1496 Make it cold for now. */
1497 if (false_predicate_p (&p))
1499 e->count = 0;
1500 e->frequency = 0;
1503 if (!e->inline_failed)
1504 remap_edge_predicates (e->callee, info, callee_info, operand_map,
1505 possible_truths, toplev_predicate);
1507 for (e = node->indirect_calls; e; e = e->next_callee)
1509 struct inline_edge_summary *es = inline_edge_summary (e);
1510 struct predicate p;
1511 if (es->predicate)
1513 p = remap_predicate (info, callee_info,
1514 es->predicate, operand_map, possible_truths,
1515 toplev_predicate);
1516 edge_set_predicate (e, &p);
1517 /* TODO: We should remove the edge for code that will be optimized out,
1518 but we need to keep verifiers and tree-inline happy.
1519 Make it cold for now. */
1520 if (false_predicate_p (&p))
1522 e->count = 0;
1523 e->frequency = 0;
1530 /* We inlined EDGE. Update summary of the function we inlined into. */
1532 void
1533 inline_merge_summary (struct cgraph_edge *edge)
1535 struct inline_summary *callee_info = inline_summary (edge->callee);
1536 struct cgraph_node *to = (edge->caller->global.inlined_to
1537 ? edge->caller->global.inlined_to : edge->caller);
1538 struct inline_summary *info = inline_summary (to);
1539 clause_t clause = 0; /* not_inline is known to be false. */
1540 size_time_entry *e;
1541 VEC (int, heap) *operand_map = NULL;
1542 int i;
1543 struct predicate toplev_predicate;
1544 struct inline_edge_summary *es = inline_edge_summary (edge);
1546 if (es->predicate)
1547 toplev_predicate = *es->predicate;
1548 else
1549 toplev_predicate = true_predicate ();
1551 if (ipa_node_params_vector && callee_info->conds
1552 /* FIXME: it seems that we forget to get argument count in some cases,
1553 probaby for previously indirect edges or so.
1554 Removing the test leads to ICE on tramp3d. */
1555 && ipa_get_cs_argument_count (IPA_EDGE_REF (edge)))
1557 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
1558 int count = ipa_get_cs_argument_count (args);
1559 int i;
1561 clause = evaluate_conditions_for_edge (edge, true);
1562 VEC_safe_grow_cleared (int, heap, operand_map, count);
1563 for (i = 0; i < count; i++)
1565 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
1566 int map = -1;
1567 /* TODO: handle non-NOPs when merging. */
1568 if (jfunc->type == IPA_JF_PASS_THROUGH
1569 && jfunc->value.pass_through.operation == NOP_EXPR)
1570 map = jfunc->value.pass_through.formal_id;
1571 VEC_replace (int, operand_map, i, map);
1572 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
1575 for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
1577 struct predicate p = remap_predicate (info, callee_info,
1578 &e->predicate, operand_map, clause,
1579 &toplev_predicate);
1580 gcov_type add_time = ((gcov_type)e->time * edge->frequency
1581 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1582 if (add_time > MAX_TIME)
1583 add_time = MAX_TIME;
1584 account_size_time (info, e->size, add_time, &p);
1586 remap_edge_predicates (edge->callee, info, callee_info, operand_map,
1587 clause, &toplev_predicate);
1588 info->size = 0;
1589 info->time = 0;
1590 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
1591 info->size += e->size, info->time += e->time;
1592 estimate_calls_size_and_time (to, &info->size, &info->time,
1593 ~(clause_t)(1 << predicate_false_condition));
1595 inline_update_callee_summaries (edge->callee,
1596 inline_edge_summary (edge)->loop_depth);
1598 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
1599 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
1603 /* Estimate the time cost for the caller when inlining EDGE.
1604 Only to be called via estimate_edge_time, that handles the
1605 caching mechanism.
1607 When caching, also update the cache entry. Compute both time and
1608 size, since we always need both metrics eventually. */
1611 do_estimate_edge_time (struct cgraph_edge *edge)
1613 int time;
1614 int size;
1615 gcov_type ret;
1616 struct inline_edge_summary *es = inline_edge_summary (edge);
1618 gcc_checking_assert (edge->inline_failed);
1619 estimate_callee_size_and_time (edge, true, &size, &time);
1621 ret = (((gcov_type)time - es->call_stmt_time) * edge->frequency
1622 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1623 if (ret > MAX_TIME)
1624 ret = MAX_TIME;
1626 /* When caching, update the cache entry. */
1627 if (edge_growth_cache)
1629 int ret_size;
1630 if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
1631 <= edge->uid)
1632 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
1633 cgraph_edge_max_uid);
1634 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->time
1635 = ret + (ret >= 0);
1637 ret_size = size - es->call_stmt_size;
1638 gcc_checking_assert (es->call_stmt_size);
1639 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->size
1640 = ret_size + (ret_size >= 0);
1642 return ret;
1646 /* Estimate the growth of the caller when inlining EDGE.
1647 Only to be called via estimate_edge_size. */
1650 do_estimate_edge_growth (struct cgraph_edge *edge)
1652 int size;
1654 /* When we do caching, use do_estimate_edge_time to populate the entry. */
1656 if (edge_growth_cache)
1658 do_estimate_edge_time (edge);
1659 size = VEC_index (edge_growth_cache_entry,
1660 edge_growth_cache,
1661 edge->uid)->size;
1662 gcc_checking_assert (size);
1663 return size - (size > 0);
1666 /* Early inliner runs without caching, go ahead and do the dirty work. */
1667 gcc_checking_assert (edge->inline_failed);
1668 estimate_callee_size_and_time (edge, true, &size, NULL);
1669 gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
1670 return size - inline_edge_summary (edge)->call_stmt_size;
1674 /* Estimate self time of the function NODE after inlining EDGE. */
1677 estimate_time_after_inlining (struct cgraph_node *node,
1678 struct cgraph_edge *edge)
1680 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
1681 if (time < 0)
1682 time = 0;
1683 if (time > MAX_TIME)
1684 time = MAX_TIME;
1685 return time;
1689 /* Estimate the size of NODE after inlining EDGE which should be an
1690 edge to either NODE or a call inlined into NODE. */
1693 estimate_size_after_inlining (struct cgraph_node *node,
1694 struct cgraph_edge *edge)
1696 int size = inline_summary (node)->size + estimate_edge_growth (edge);
1697 gcc_assert (size >= 0);
1698 return size;
1702 /* Estimate the growth caused by inlining NODE into all callees. */
1705 do_estimate_growth (struct cgraph_node *node)
1707 int growth = 0;
1708 struct cgraph_edge *e;
1709 bool self_recursive = false;
1710 struct inline_summary *info = inline_summary (node);
1712 for (e = node->callers; e; e = e->next_caller)
1714 gcc_checking_assert (e->inline_failed);
1716 if (e->caller == node
1717 || (e->caller->global.inlined_to
1718 && e->caller->global.inlined_to == node))
1719 self_recursive = true;
1720 growth += estimate_edge_growth (e);
1724 /* For self recursive functions the growth estimation really should be
1725 infinity. We don't want to return very large values because the growth
1726 plays various roles in badness computation fractions. Be sure to not
1727 return zero or negative growths. */
1728 if (self_recursive)
1729 growth = growth < info->size ? info->size : growth;
1730 else
1732 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node)
1733 && !DECL_EXTERNAL (node->decl))
1734 growth -= info->size;
1735 /* COMDAT functions are very often not shared across multiple units since they
1736 come from various template instantiations. Take this into account. */
1737 else if (DECL_COMDAT (node->decl)
1738 && cgraph_can_remove_if_no_direct_calls_p (node))
1739 growth -= (info->size
1740 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY)) + 50) / 100;
1743 if (node_growth_cache)
1745 if ((int)VEC_length (int, node_growth_cache) <= node->uid)
1746 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
1747 VEC_replace (int, node_growth_cache, node->uid, growth + (growth >= 0));
1749 return growth;
1753 /* This function performs intraprocedural analysis in NODE that is required to
1754 inline indirect calls. */
1756 static void
1757 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
1759 ipa_analyze_node (node);
1760 if (dump_file && (dump_flags & TDF_DETAILS))
1762 ipa_print_node_params (dump_file, node);
1763 ipa_print_node_jump_functions (dump_file, node);
1768 /* Note function body size. */
1770 static void
1771 inline_analyze_function (struct cgraph_node *node)
1773 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1774 current_function_decl = node->decl;
1776 if (dump_file)
1777 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
1778 cgraph_node_name (node), node->uid);
1779 /* FIXME: We should remove the optimize check after we ensure we never run
1780 IPA passes when not optimizing. */
1781 if (flag_indirect_inlining && optimize)
1782 inline_indirect_intraprocedural_analysis (node);
1783 compute_inline_parameters (node, false);
1785 current_function_decl = NULL;
1786 pop_cfun ();
1790 /* Called when new function is inserted to callgraph late. */
1792 static void
1793 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1795 inline_analyze_function (node);
1799 /* Note function body size. */
1801 void
1802 inline_generate_summary (void)
1804 struct cgraph_node *node;
1806 function_insertion_hook_holder =
1807 cgraph_add_function_insertion_hook (&add_new_function, NULL);
1809 if (flag_indirect_inlining)
1810 ipa_register_cgraph_hooks ();
1812 for (node = cgraph_nodes; node; node = node->next)
1813 if (node->analyzed)
1814 inline_analyze_function (node);
1818 /* Read predicate from IB. */
1820 static struct predicate
1821 read_predicate (struct lto_input_block *ib)
1823 struct predicate out;
1824 clause_t clause;
1825 int k = 0;
1829 clause = out.clause[k++] = lto_input_uleb128 (ib);
1830 gcc_assert (k < MAX_CLAUSES);
1832 while (clause);
1833 return out;
1837 /* Write inline summary for edge E to OB. */
1839 static void
1840 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
1842 struct inline_edge_summary *es = inline_edge_summary (e);
1843 struct predicate p;
1845 es->call_stmt_size = lto_input_uleb128 (ib);
1846 es->call_stmt_time = lto_input_uleb128 (ib);
1847 es->loop_depth = lto_input_uleb128 (ib);
1848 p = read_predicate (ib);
1849 edge_set_predicate (e, &p);
1853 /* Stream in inline summaries from the section. */
1855 static void
1856 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
1857 size_t len)
1859 const struct lto_function_header *header =
1860 (const struct lto_function_header *) data;
1861 const int32_t cfg_offset = sizeof (struct lto_function_header);
1862 const int32_t main_offset = cfg_offset + header->cfg_size;
1863 const int32_t string_offset = main_offset + header->main_size;
1864 struct data_in *data_in;
1865 struct lto_input_block ib;
1866 unsigned int i, count2, j;
1867 unsigned int f_count;
1869 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
1870 header->main_size);
1872 data_in =
1873 lto_data_in_create (file_data, (const char *) data + string_offset,
1874 header->string_size, NULL);
1875 f_count = lto_input_uleb128 (&ib);
1876 for (i = 0; i < f_count; i++)
1878 unsigned int index;
1879 struct cgraph_node *node;
1880 struct inline_summary *info;
1881 lto_cgraph_encoder_t encoder;
1882 struct bitpack_d bp;
1883 struct cgraph_edge *e;
1885 index = lto_input_uleb128 (&ib);
1886 encoder = file_data->cgraph_node_encoder;
1887 node = lto_cgraph_encoder_deref (encoder, index);
1888 info = inline_summary (node);
1890 info->estimated_stack_size
1891 = info->estimated_self_stack_size = lto_input_uleb128 (&ib);
1892 info->size = info->self_size = lto_input_uleb128 (&ib);
1893 info->time = info->self_time = lto_input_uleb128 (&ib);
1895 bp = lto_input_bitpack (&ib);
1896 info->inlinable = bp_unpack_value (&bp, 1);
1897 info->versionable = bp_unpack_value (&bp, 1);
1899 count2 = lto_input_uleb128 (&ib);
1900 gcc_assert (!info->conds);
1901 for (j = 0; j < count2; j++)
1903 struct condition c;
1904 c.operand_num = lto_input_uleb128 (&ib);
1905 c.code = (enum tree_code) lto_input_uleb128 (&ib);
1906 c.val = lto_input_tree (&ib, data_in);
1907 VEC_safe_push (condition, gc, info->conds, &c);
1909 count2 = lto_input_uleb128 (&ib);
1910 gcc_assert (!info->entry);
1911 for (j = 0; j < count2; j++)
1913 struct size_time_entry e;
1915 e.size = lto_input_uleb128 (&ib);
1916 e.time = lto_input_uleb128 (&ib);
1917 e.predicate = read_predicate (&ib);
1919 VEC_safe_push (size_time_entry, gc, info->entry, &e);
1921 for (e = node->callees; e; e = e->next_callee)
1922 read_inline_edge_summary (&ib, e);
1923 for (e = node->indirect_calls; e; e = e->next_callee)
1924 read_inline_edge_summary (&ib, e);
1927 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
1928 len);
1929 lto_data_in_delete (data_in);
1933 /* Read inline summary. Jump functions are shared among ipa-cp
1934 and inliner, so when ipa-cp is active, we don't need to write them
1935 twice. */
1937 void
1938 inline_read_summary (void)
1940 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
1941 struct lto_file_decl_data *file_data;
1942 unsigned int j = 0;
1944 inline_summary_alloc ();
1946 while ((file_data = file_data_vec[j++]))
1948 size_t len;
1949 const char *data = lto_get_section_data (file_data, LTO_section_inline_summary, NULL, &len);
1950 if (data)
1951 inline_read_section (file_data, data, len);
1952 else
1953 /* Fatal error here. We do not want to support compiling ltrans units with
1954 different version of compiler or different flags than the WPA unit, so
1955 this should never happen. */
1956 fatal_error ("ipa inline summary is missing in input file");
1958 if (flag_indirect_inlining)
1960 ipa_register_cgraph_hooks ();
1961 if (!flag_ipa_cp)
1962 ipa_prop_read_jump_functions ();
1964 function_insertion_hook_holder =
1965 cgraph_add_function_insertion_hook (&add_new_function, NULL);
1969 /* Write predicate P to OB. */
1971 static void
1972 write_predicate (struct output_block *ob, struct predicate *p)
1974 int j;
1975 if (p)
1976 for (j = 0; p->clause[j]; j++)
1978 gcc_assert (j < MAX_CLAUSES);
1979 lto_output_uleb128_stream (ob->main_stream,
1980 p->clause[j]);
1982 lto_output_uleb128_stream (ob->main_stream, 0);
1986 /* Write inline summary for edge E to OB. */
1988 static void
1989 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
1991 struct inline_edge_summary *es = inline_edge_summary (e);
1992 lto_output_uleb128_stream (ob->main_stream, es->call_stmt_size);
1993 lto_output_uleb128_stream (ob->main_stream, es->call_stmt_time);
1994 lto_output_uleb128_stream (ob->main_stream, es->loop_depth);
1995 write_predicate (ob, es->predicate);
1999 /* Write inline summary for node in SET.
2000 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2001 active, we don't need to write them twice. */
2003 void
2004 inline_write_summary (cgraph_node_set set,
2005 varpool_node_set vset ATTRIBUTE_UNUSED)
2007 struct cgraph_node *node;
2008 struct output_block *ob = create_output_block (LTO_section_inline_summary);
2009 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2010 unsigned int count = 0;
2011 int i;
2013 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2014 if (lto_cgraph_encoder_deref (encoder, i)->analyzed)
2015 count++;
2016 lto_output_uleb128_stream (ob->main_stream, count);
2018 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2020 node = lto_cgraph_encoder_deref (encoder, i);
2021 if (node->analyzed)
2023 struct inline_summary *info = inline_summary (node);
2024 struct bitpack_d bp;
2025 struct cgraph_edge *edge;
2026 int i;
2027 size_time_entry *e;
2028 struct condition *c;
2031 lto_output_uleb128_stream (ob->main_stream,
2032 lto_cgraph_encoder_encode (encoder, node));
2033 lto_output_sleb128_stream (ob->main_stream,
2034 info->estimated_self_stack_size);
2035 lto_output_sleb128_stream (ob->main_stream,
2036 info->self_size);
2037 lto_output_sleb128_stream (ob->main_stream,
2038 info->self_time);
2039 bp = bitpack_create (ob->main_stream);
2040 bp_pack_value (&bp, info->inlinable, 1);
2041 bp_pack_value (&bp, info->versionable, 1);
2042 lto_output_bitpack (&bp);
2043 lto_output_uleb128_stream (ob->main_stream,
2044 VEC_length (condition, info->conds));
2045 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
2047 lto_output_uleb128_stream (ob->main_stream,
2048 c->operand_num);
2049 lto_output_uleb128_stream (ob->main_stream,
2050 c->code);
2051 lto_output_tree (ob, c->val, true);
2053 lto_output_uleb128_stream (ob->main_stream,
2054 VEC_length (size_time_entry, info->entry));
2055 for (i = 0;
2056 VEC_iterate (size_time_entry, info->entry, i, e);
2057 i++)
2059 lto_output_uleb128_stream (ob->main_stream,
2060 e->size);
2061 lto_output_uleb128_stream (ob->main_stream,
2062 e->time);
2063 write_predicate (ob, &e->predicate);
2065 for (edge = node->callees; edge; edge = edge->next_callee)
2066 write_inline_edge_summary (ob, edge);
2067 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2068 write_inline_edge_summary (ob, edge);
2071 lto_output_1_stream (ob->main_stream, 0);
2072 produce_asm (ob, NULL);
2073 destroy_output_block (ob);
2075 if (flag_indirect_inlining && !flag_ipa_cp)
2076 ipa_prop_write_jump_functions (set);
2080 /* Release inline summary. */
2082 void
2083 inline_free_summary (void)
2085 if (function_insertion_hook_holder)
2086 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2087 function_insertion_hook_holder = NULL;
2088 if (node_removal_hook_holder)
2089 cgraph_remove_node_removal_hook (node_removal_hook_holder);
2090 if (edge_removal_hook_holder)
2091 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2092 node_removal_hook_holder = NULL;
2093 if (node_duplication_hook_holder)
2094 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2095 if (edge_duplication_hook_holder)
2096 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2097 node_duplication_hook_holder = NULL;
2098 VEC_free (inline_summary_t, gc, inline_summary_vec);
2099 inline_summary_vec = NULL;
2100 VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
2101 inline_edge_summary_vec = NULL;
2102 if (edge_predicate_pool)
2103 free_alloc_pool (edge_predicate_pool);
2104 edge_predicate_pool = 0;