2012-11-19 Mans Rullgard <mans@mansr.com>
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob61fb48dccb00bc01a24d21a56654fc9a7442a84e
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 "params.h"
80 #include "tree-pass.h"
81 #include "coverage.h"
82 #include "ggc.h"
83 #include "tree-flow.h"
84 #include "ipa-prop.h"
85 #include "lto-streamer.h"
86 #include "data-streamer.h"
87 #include "tree-streamer.h"
88 #include "ipa-inline.h"
89 #include "alloc-pool.h"
90 #include "cfgloop.h"
91 #include "cfgloop.h"
92 #include "tree-scalar-evolution.h"
94 /* Estimate runtime of function can easilly run into huge numbers with many
95 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
96 integer. For anything larger we use gcov_type. */
97 #define MAX_TIME 500000
99 /* Number of bits in integer, but we really want to be stable across different
100 hosts. */
101 #define NUM_CONDITIONS 32
103 enum predicate_conditions
105 predicate_false_condition = 0,
106 predicate_not_inlined_condition = 1,
107 predicate_first_dynamic_condition = 2
110 /* Special condition code we use to represent test that operand is compile time
111 constant. */
112 #define IS_NOT_CONSTANT ERROR_MARK
113 /* Special condition code we use to represent test that operand is not changed
114 across invocation of the function. When operand IS_NOT_CONSTANT it is always
115 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
116 of executions even when they are not compile time constants. */
117 #define CHANGED IDENTIFIER_NODE
119 /* Holders of ipa cgraph hooks: */
120 static struct cgraph_node_hook_list *function_insertion_hook_holder;
121 static struct cgraph_node_hook_list *node_removal_hook_holder;
122 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
123 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
124 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
125 static void inline_node_removal_hook (struct cgraph_node *, void *);
126 static void inline_node_duplication_hook (struct cgraph_node *,
127 struct cgraph_node *, void *);
128 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
129 static void inline_edge_duplication_hook (struct cgraph_edge *,
130 struct cgraph_edge *,
131 void *);
133 /* VECtor holding inline summaries.
134 In GGC memory because conditions might point to constant trees. */
135 vec<inline_summary_t, va_gc> *inline_summary_vec;
136 vec<inline_edge_summary_t> inline_edge_summary_vec;
138 /* Cached node/edge growths. */
139 vec<int> node_growth_cache;
140 vec<edge_growth_cache_entry> edge_growth_cache;
142 /* Edge predicates goes here. */
143 static alloc_pool edge_predicate_pool;
145 /* Return true predicate (tautology).
146 We represent it by empty list of clauses. */
148 static inline struct predicate
149 true_predicate (void)
151 struct predicate p;
152 p.clause[0] = 0;
153 return p;
157 /* Return predicate testing single condition number COND. */
159 static inline struct predicate
160 single_cond_predicate (int cond)
162 struct predicate p;
163 p.clause[0] = 1 << cond;
164 p.clause[1] = 0;
165 return p;
169 /* Return false predicate. First clause require false condition. */
171 static inline struct predicate
172 false_predicate (void)
174 return single_cond_predicate (predicate_false_condition);
178 /* Return true if P is (false). */
180 static inline bool
181 true_predicate_p (struct predicate *p)
183 return !p->clause[0];
187 /* Return true if P is (false). */
189 static inline bool
190 false_predicate_p (struct predicate *p)
192 if (p->clause[0] == (1 << predicate_false_condition))
194 gcc_checking_assert (!p->clause[1]
195 && p->clause[0] == 1 << predicate_false_condition);
196 return true;
198 return false;
202 /* Return predicate that is set true when function is not inlined. */
203 static inline struct predicate
204 not_inlined_predicate (void)
206 return single_cond_predicate (predicate_not_inlined_condition);
209 /* Simple description of whether a memory load or a condition refers to a load
210 from an aggregate and if so, how and where from in the aggregate.
211 Individual fields have the same meaning like fields with the same name in
212 struct condition. */
214 struct agg_position_info
216 HOST_WIDE_INT offset;
217 bool agg_contents;
218 bool by_ref;
221 /* Add condition to condition list CONDS. AGGPOS describes whether the used
222 oprand is loaded from an aggregate and where in the aggregate it is. It can
223 be NULL, which means this not a load from an aggregate. */
225 static struct predicate
226 add_condition (struct inline_summary *summary, int operand_num,
227 struct agg_position_info *aggpos,
228 enum tree_code code, tree val)
230 int i;
231 struct condition *c;
232 struct condition new_cond;
233 HOST_WIDE_INT offset;
234 bool agg_contents, by_ref;
236 if (aggpos)
238 offset = aggpos->offset;
239 agg_contents = aggpos->agg_contents;
240 by_ref = aggpos->by_ref;
242 else
244 offset = 0;
245 agg_contents = false;
246 by_ref = false;
249 gcc_checking_assert (operand_num >= 0);
250 for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
252 if (c->operand_num == operand_num
253 && c->code == code
254 && c->val == val
255 && c->agg_contents == agg_contents
256 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
257 return single_cond_predicate (i + predicate_first_dynamic_condition);
259 /* Too many conditions. Give up and return constant true. */
260 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
261 return true_predicate ();
263 new_cond.operand_num = operand_num;
264 new_cond.code = code;
265 new_cond.val = val;
266 new_cond.agg_contents = agg_contents;
267 new_cond.by_ref = by_ref;
268 new_cond.offset = offset;
269 vec_safe_push (summary->conds, new_cond);
270 return single_cond_predicate (i + predicate_first_dynamic_condition);
274 /* Add clause CLAUSE into the predicate P. */
276 static inline void
277 add_clause (conditions conditions, struct predicate *p, clause_t clause)
279 int i;
280 int i2;
281 int insert_here = -1;
282 int c1, c2;
284 /* True clause. */
285 if (!clause)
286 return;
288 /* False clause makes the whole predicate false. Kill the other variants. */
289 if (clause == (1 << predicate_false_condition))
291 p->clause[0] = (1 << predicate_false_condition);
292 p->clause[1] = 0;
293 return;
295 if (false_predicate_p (p))
296 return;
298 /* No one should be sily enough to add false into nontrivial clauses. */
299 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
301 /* Look where to insert the clause. At the same time prune out
302 clauses of P that are implied by the new clause and thus
303 redundant. */
304 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
306 p->clause[i2] = p->clause[i];
308 if (!p->clause[i])
309 break;
311 /* If p->clause[i] implies clause, there is nothing to add. */
312 if ((p->clause[i] & clause) == p->clause[i])
314 /* We had nothing to add, none of clauses should've become
315 redundant. */
316 gcc_checking_assert (i == i2);
317 return;
320 if (p->clause[i] < clause && insert_here < 0)
321 insert_here = i2;
323 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
324 Otherwise the p->clause[i] has to stay. */
325 if ((p->clause[i] & clause) != clause)
326 i2++;
329 /* Look for clauses that are obviously true. I.e.
330 op0 == 5 || op0 != 5. */
331 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
333 condition *cc1;
334 if (!(clause & (1 << c1)))
335 continue;
336 cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
337 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
338 and thus there is no point for looking for them. */
339 if (cc1->code == CHANGED
340 || cc1->code == IS_NOT_CONSTANT)
341 continue;
342 for (c2 = c1 + 1; c2 <= NUM_CONDITIONS; c2++)
343 if (clause & (1 << c2))
345 condition *cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
346 condition *cc2 = &(*conditions)[c2 - predicate_first_dynamic_condition];
347 if (cc1->operand_num == cc2->operand_num
348 && cc1->val == cc2->val
349 && cc2->code != IS_NOT_CONSTANT
350 && cc2->code != CHANGED
351 && cc1->code == invert_tree_comparison
352 (cc2->code,
353 HONOR_NANS (TYPE_MODE (TREE_TYPE (cc1->val)))))
354 return;
359 /* We run out of variants. Be conservative in positive direction. */
360 if (i2 == MAX_CLAUSES)
361 return;
362 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
363 p->clause[i2 + 1] = 0;
364 if (insert_here >= 0)
365 for (;i2 > insert_here; i2--)
366 p->clause[i2] = p->clause[i2 - 1];
367 else
368 insert_here = i2;
369 p->clause[insert_here] = clause;
373 /* Return P & P2. */
375 static struct predicate
376 and_predicates (conditions conditions,
377 struct predicate *p, struct predicate *p2)
379 struct predicate out = *p;
380 int i;
382 /* Avoid busy work. */
383 if (false_predicate_p (p2) || true_predicate_p (p))
384 return *p2;
385 if (false_predicate_p (p) || true_predicate_p (p2))
386 return *p;
388 /* See how far predicates match. */
389 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
391 gcc_checking_assert (i < MAX_CLAUSES);
394 /* Combine the predicates rest. */
395 for (; p2->clause[i]; i++)
397 gcc_checking_assert (i < MAX_CLAUSES);
398 add_clause (conditions, &out, p2->clause[i]);
400 return out;
404 /* Return true if predicates are obviously equal. */
406 static inline bool
407 predicates_equal_p (struct predicate *p, struct predicate *p2)
409 int i;
410 for (i = 0; p->clause[i]; i++)
412 gcc_checking_assert (i < MAX_CLAUSES);
413 gcc_checking_assert (p->clause [i] > p->clause[i + 1]);
414 gcc_checking_assert (!p2->clause[i]
415 || p2->clause [i] > p2->clause[i + 1]);
416 if (p->clause[i] != p2->clause[i])
417 return false;
419 return !p2->clause[i];
423 /* Return P | P2. */
425 static struct predicate
426 or_predicates (conditions conditions, struct predicate *p, struct predicate *p2)
428 struct predicate out = true_predicate ();
429 int i,j;
431 /* Avoid busy work. */
432 if (false_predicate_p (p2) || true_predicate_p (p))
433 return *p;
434 if (false_predicate_p (p) || true_predicate_p (p2))
435 return *p2;
436 if (predicates_equal_p (p, p2))
437 return *p;
439 /* OK, combine the predicates. */
440 for (i = 0; p->clause[i]; i++)
441 for (j = 0; p2->clause[j]; j++)
443 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
444 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
446 return out;
450 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
451 if predicate P is known to be false. */
453 static bool
454 evaluate_predicate (struct predicate *p, clause_t possible_truths)
456 int i;
458 /* True remains true. */
459 if (true_predicate_p (p))
460 return true;
462 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
464 /* See if we can find clause we can disprove. */
465 for (i = 0; p->clause[i]; i++)
467 gcc_checking_assert (i < MAX_CLAUSES);
468 if (!(p->clause[i] & possible_truths))
469 return false;
471 return true;
474 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
475 instruction will be recomputed per invocation of the inlined call. */
477 static int
478 predicate_probability (conditions conds,
479 struct predicate *p, clause_t possible_truths,
480 vec<inline_param_summary_t> inline_param_summary)
482 int i;
483 int combined_prob = REG_BR_PROB_BASE;
485 /* True remains true. */
486 if (true_predicate_p (p))
487 return REG_BR_PROB_BASE;
489 if (false_predicate_p (p))
490 return 0;
492 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
494 /* See if we can find clause we can disprove. */
495 for (i = 0; p->clause[i]; i++)
497 gcc_checking_assert (i < MAX_CLAUSES);
498 if (!(p->clause[i] & possible_truths))
499 return 0;
500 else
502 int this_prob = 0;
503 int i2;
504 if (!inline_param_summary.exists ())
505 return REG_BR_PROB_BASE;
506 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
507 if ((p->clause[i] & possible_truths) & (1 << i2))
509 if (i2 >= predicate_first_dynamic_condition)
511 condition *c = &(*conds)[i2 - predicate_first_dynamic_condition];
512 if (c->code == CHANGED
513 && (c->operand_num
514 < (int) inline_param_summary.length ()))
516 int iprob = inline_param_summary[c->operand_num].change_prob;
517 this_prob = MAX (this_prob, iprob);
519 else
520 this_prob = REG_BR_PROB_BASE;
522 else
523 this_prob = REG_BR_PROB_BASE;
525 combined_prob = MIN (this_prob, combined_prob);
526 if (!combined_prob)
527 return 0;
530 return combined_prob;
534 /* Dump conditional COND. */
536 static void
537 dump_condition (FILE *f, conditions conditions, int cond)
539 condition *c;
540 if (cond == predicate_false_condition)
541 fprintf (f, "false");
542 else if (cond == predicate_not_inlined_condition)
543 fprintf (f, "not inlined");
544 else
546 c = &(*conditions)[cond - predicate_first_dynamic_condition];
547 fprintf (f, "op%i", c->operand_num);
548 if (c->agg_contents)
549 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
550 c->by_ref ? "ref " : "", c->offset);
551 if (c->code == IS_NOT_CONSTANT)
553 fprintf (f, " not constant");
554 return;
556 if (c->code == CHANGED)
558 fprintf (f, " changed");
559 return;
561 fprintf (f, " %s ", op_symbol_code (c->code));
562 print_generic_expr (f, c->val, 1);
567 /* Dump clause CLAUSE. */
569 static void
570 dump_clause (FILE *f, conditions conds, clause_t clause)
572 int i;
573 bool found = false;
574 fprintf (f, "(");
575 if (!clause)
576 fprintf (f, "true");
577 for (i = 0; i < NUM_CONDITIONS; i++)
578 if (clause & (1 << i))
580 if (found)
581 fprintf (f, " || ");
582 found = true;
583 dump_condition (f, conds, i);
585 fprintf (f, ")");
589 /* Dump predicate PREDICATE. */
591 static void
592 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
594 int i;
595 if (true_predicate_p (pred))
596 dump_clause (f, conds, 0);
597 else
598 for (i = 0; pred->clause[i]; i++)
600 if (i)
601 fprintf (f, " && ");
602 dump_clause (f, conds, pred->clause[i]);
604 fprintf (f, "\n");
608 /* Dump inline hints. */
609 void
610 dump_inline_hints (FILE *f, inline_hints hints)
612 if (!hints)
613 return;
614 fprintf (f, "inline hints:");
615 if (hints & INLINE_HINT_indirect_call)
617 hints &= ~INLINE_HINT_indirect_call;
618 fprintf (f, " indirect_call");
620 if (hints & INLINE_HINT_loop_iterations)
622 hints &= ~INLINE_HINT_loop_iterations;
623 fprintf (f, " loop_iterations");
625 if (hints & INLINE_HINT_loop_stride)
627 hints &= ~INLINE_HINT_loop_stride;
628 fprintf (f, " loop_stride");
630 if (hints & INLINE_HINT_same_scc)
632 hints &= ~INLINE_HINT_same_scc;
633 fprintf (f, " same_scc");
635 if (hints & INLINE_HINT_in_scc)
637 hints &= ~INLINE_HINT_in_scc;
638 fprintf (f, " in_scc");
640 if (hints & INLINE_HINT_cross_module)
642 hints &= ~INLINE_HINT_cross_module;
643 fprintf (f, " cross_module");
645 if (hints & INLINE_HINT_declared_inline)
647 hints &= ~INLINE_HINT_declared_inline;
648 fprintf (f, " declared_inline");
650 if (hints & INLINE_HINT_array_index)
652 hints &= ~INLINE_HINT_array_index;
653 fprintf (f, " array_index");
655 gcc_assert (!hints);
659 /* Record SIZE and TIME under condition PRED into the inline summary. */
661 static void
662 account_size_time (struct inline_summary *summary, int size, int time,
663 struct predicate *pred)
665 size_time_entry *e;
666 bool found = false;
667 int i;
669 if (false_predicate_p (pred))
670 return;
672 /* We need to create initial empty unconitional clause, but otherwie
673 we don't need to account empty times and sizes. */
674 if (!size && !time && summary->entry)
675 return;
677 /* Watch overflow that might result from insane profiles. */
678 if (time > MAX_TIME * INLINE_TIME_SCALE)
679 time = MAX_TIME * INLINE_TIME_SCALE;
680 gcc_assert (time >= 0);
682 for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
683 if (predicates_equal_p (&e->predicate, pred))
685 found = true;
686 break;
688 if (i == 256)
690 i = 0;
691 found = true;
692 e = &(*summary->entry)[0];
693 gcc_assert (!e->predicate.clause[0]);
694 if (dump_file && (dump_flags & TDF_DETAILS))
695 fprintf (dump_file, "\t\tReached limit on number of entries, ignoring the predicate.");
697 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
699 fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
700 ((double)size) / INLINE_SIZE_SCALE,
701 ((double)time) / INLINE_TIME_SCALE,
702 found ? "" : "new ");
703 dump_predicate (dump_file, summary->conds, pred);
705 if (!found)
707 struct size_time_entry new_entry;
708 new_entry.size = size;
709 new_entry.time = time;
710 new_entry.predicate = *pred;
711 vec_safe_push (summary->entry, new_entry);
713 else
715 e->size += size;
716 e->time += time;
717 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
718 e->time = MAX_TIME * INLINE_TIME_SCALE;
722 /* Set predicate for edge E. */
724 static void
725 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
727 struct inline_edge_summary *es = inline_edge_summary (e);
728 if (predicate && !true_predicate_p (predicate))
730 if (!es->predicate)
731 es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
732 *es->predicate = *predicate;
734 else
736 if (es->predicate)
737 pool_free (edge_predicate_pool, es->predicate);
738 es->predicate = NULL;
742 /* Set predicate for hint *P. */
744 static void
745 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
747 if (false_predicate_p (&new_predicate)
748 || true_predicate_p (&new_predicate))
750 if (*p)
751 pool_free (edge_predicate_pool, *p);
752 *p = NULL;
754 else
756 if (!*p)
757 *p = (struct predicate *)pool_alloc (edge_predicate_pool);
758 **p = new_predicate;
763 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
764 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
765 Return clause of possible truths. When INLINE_P is true, assume that we are
766 inlining.
768 ERROR_MARK means compile time invariant. */
770 static clause_t
771 evaluate_conditions_for_known_args (struct cgraph_node *node,
772 bool inline_p,
773 vec<tree> known_vals,
774 vec<ipa_agg_jump_function_p> known_aggs)
776 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
777 struct inline_summary *info = inline_summary (node);
778 int i;
779 struct condition *c;
781 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
783 tree val;
784 tree res;
786 /* We allow call stmt to have fewer arguments than the callee function
787 (especially for K&R style programs). So bound check here (we assume
788 known_aggs vector, if non-NULL, has the same length as
789 known_vals). */
790 gcc_checking_assert (!known_aggs.exists ()
791 || (known_vals.length () == known_aggs.length ()));
792 if (c->operand_num >= (int) known_vals.length ())
794 clause |= 1 << (i + predicate_first_dynamic_condition);
795 continue;
798 if (c->agg_contents)
800 struct ipa_agg_jump_function *agg;
802 if (c->code == CHANGED
803 && !c->by_ref
804 && (known_vals[c->operand_num]
805 == error_mark_node))
806 continue;
808 if (known_aggs.exists ())
810 agg = known_aggs[c->operand_num];
811 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
813 else
814 val = NULL_TREE;
816 else
818 val = known_vals[c->operand_num];
819 if (val == error_mark_node && c->code != CHANGED)
820 val = NULL_TREE;
823 if (!val)
825 clause |= 1 << (i + predicate_first_dynamic_condition);
826 continue;
828 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
829 continue;
830 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
831 if (res
832 && integer_zerop (res))
833 continue;
834 clause |= 1 << (i + predicate_first_dynamic_condition);
836 return clause;
840 /* Work out what conditions might be true at invocation of E. */
842 static void
843 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
844 clause_t *clause_ptr,
845 vec<tree> *known_vals_ptr,
846 vec<tree> *known_binfos_ptr,
847 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
849 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
850 struct inline_summary *info = inline_summary (callee);
851 vec<tree> known_vals = vec<tree>();
852 vec<ipa_agg_jump_function_p> known_aggs
853 = vec<ipa_agg_jump_function_p>();
855 if (clause_ptr)
856 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
857 if (known_vals_ptr)
858 known_vals_ptr->create (0);
859 if (known_binfos_ptr)
860 known_binfos_ptr->create (0);
862 if (ipa_node_params_vector.exists ()
863 && !e->call_stmt_cannot_inline_p
864 && ((clause_ptr && info->conds)
865 || known_vals_ptr || known_binfos_ptr))
867 struct ipa_node_params *parms_info;
868 struct ipa_edge_args *args = IPA_EDGE_REF (e);
869 struct inline_edge_summary *es = inline_edge_summary (e);
870 int i, count = ipa_get_cs_argument_count (args);
872 if (e->caller->global.inlined_to)
873 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
874 else
875 parms_info = IPA_NODE_REF (e->caller);
877 if (count && (info->conds || known_vals_ptr))
878 known_vals.safe_grow_cleared (count);
879 if (count && (info->conds || known_aggs_ptr))
880 known_aggs.safe_grow_cleared (count);
881 if (count && known_binfos_ptr)
882 known_binfos_ptr->safe_grow_cleared (count);
884 for (i = 0; i < count; i++)
886 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
887 tree cst = ipa_value_from_jfunc (parms_info, jf);
888 if (cst)
890 if (known_vals.exists () && TREE_CODE (cst) != TREE_BINFO)
891 known_vals[i] = cst;
892 else if (known_binfos_ptr != NULL && TREE_CODE (cst) == TREE_BINFO)
893 (*known_binfos_ptr)[i] = cst;
895 else if (inline_p && !es->param[i].change_prob)
896 known_vals[i] = error_mark_node;
897 /* TODO: When IPA-CP starts propagating and merging aggregate jump
898 functions, use its knowledge of the caller too, just like the
899 scalar case above. */
900 known_aggs[i] = &jf->agg;
904 if (clause_ptr)
905 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
906 known_vals, known_aggs);
908 if (known_vals_ptr)
909 *known_vals_ptr = known_vals;
910 else
911 known_vals.release ();
913 if (known_aggs_ptr)
914 *known_aggs_ptr = known_aggs;
915 else
916 known_aggs.release ();
920 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
922 static void
923 inline_summary_alloc (void)
925 if (!node_removal_hook_holder)
926 node_removal_hook_holder =
927 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
928 if (!edge_removal_hook_holder)
929 edge_removal_hook_holder =
930 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
931 if (!node_duplication_hook_holder)
932 node_duplication_hook_holder =
933 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
934 if (!edge_duplication_hook_holder)
935 edge_duplication_hook_holder =
936 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
938 if (vec_safe_length (inline_summary_vec) <= (unsigned) cgraph_max_uid)
939 vec_safe_grow_cleared (inline_summary_vec, cgraph_max_uid + 1);
940 if (inline_edge_summary_vec.length () <= (unsigned) cgraph_edge_max_uid)
941 inline_edge_summary_vec.safe_grow_cleared (cgraph_edge_max_uid + 1);
942 if (!edge_predicate_pool)
943 edge_predicate_pool = create_alloc_pool ("edge predicates",
944 sizeof (struct predicate),
945 10);
948 /* We are called multiple time for given function; clear
949 data from previous run so they are not cumulated. */
951 static void
952 reset_inline_edge_summary (struct cgraph_edge *e)
954 if (e->uid < (int)inline_edge_summary_vec.length ())
956 struct inline_edge_summary *es = inline_edge_summary (e);
958 es->call_stmt_size = es->call_stmt_time = 0;
959 if (es->predicate)
960 pool_free (edge_predicate_pool, es->predicate);
961 es->predicate = NULL;
962 es->param.release ();
966 /* We are called multiple time for given function; clear
967 data from previous run so they are not cumulated. */
969 static void
970 reset_inline_summary (struct cgraph_node *node)
972 struct inline_summary *info = inline_summary (node);
973 struct cgraph_edge *e;
975 info->self_size = info->self_time = 0;
976 info->estimated_stack_size = 0;
977 info->estimated_self_stack_size = 0;
978 info->stack_frame_offset = 0;
979 info->size = 0;
980 info->time = 0;
981 info->growth = 0;
982 info->scc_no = 0;
983 if (info->loop_iterations)
985 pool_free (edge_predicate_pool, info->loop_iterations);
986 info->loop_iterations = NULL;
988 if (info->loop_stride)
990 pool_free (edge_predicate_pool, info->loop_stride);
991 info->loop_stride = NULL;
993 if (info->array_index)
995 pool_free (edge_predicate_pool, info->array_index);
996 info->array_index = NULL;
998 vec_free (info->conds);
999 vec_free (info->entry);
1000 for (e = node->callees; e; e = e->next_callee)
1001 reset_inline_edge_summary (e);
1002 for (e = node->indirect_calls; e; e = e->next_callee)
1003 reset_inline_edge_summary (e);
1006 /* Hook that is called by cgraph.c when a node is removed. */
1008 static void
1009 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1011 struct inline_summary *info;
1012 if (vec_safe_length (inline_summary_vec) <= (unsigned)node->uid)
1013 return;
1014 info = inline_summary (node);
1015 reset_inline_summary (node);
1016 memset (info, 0, sizeof (inline_summary_t));
1019 /* Remap predicate P of former function to be predicate of duplicated functoin.
1020 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1021 INFO is inline summary of the duplicated node. */
1023 static struct predicate
1024 remap_predicate_after_duplication (struct predicate *p,
1025 clause_t possible_truths,
1026 struct inline_summary *info)
1028 struct predicate new_predicate = true_predicate ();
1029 int j;
1030 for (j = 0; p->clause[j]; j++)
1031 if (!(possible_truths & p->clause[j]))
1033 new_predicate = false_predicate ();
1034 break;
1036 else
1037 add_clause (info->conds, &new_predicate,
1038 possible_truths & p->clause[j]);
1039 return new_predicate;
1042 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1043 Additionally care about allocating new memory slot for updated predicate
1044 and set it to NULL when it becomes true or false (and thus uninteresting).
1047 static void
1048 remap_hint_predicate_after_duplication (struct predicate **p,
1049 clause_t possible_truths,
1050 struct inline_summary *info)
1052 struct predicate new_predicate;
1054 if (!*p)
1055 return;
1057 new_predicate = remap_predicate_after_duplication (*p,
1058 possible_truths,
1059 info);
1060 /* We do not want to free previous predicate; it is used by node origin. */
1061 *p = NULL;
1062 set_hint_predicate (p, new_predicate);
1066 /* Hook that is called by cgraph.c when a node is duplicated. */
1068 static void
1069 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1070 ATTRIBUTE_UNUSED void *data)
1072 struct inline_summary *info;
1073 inline_summary_alloc ();
1074 info = inline_summary (dst);
1075 memcpy (info, inline_summary (src),
1076 sizeof (struct inline_summary));
1077 /* TODO: as an optimization, we may avoid copying conditions
1078 that are known to be false or true. */
1079 info->conds = vec_safe_copy (info->conds);
1081 /* When there are any replacements in the function body, see if we can figure
1082 out that something was optimized out. */
1083 if (ipa_node_params_vector.exists ()
1084 && dst->clone.tree_map)
1086 vec<size_time_entry, va_gc> *entry = info->entry;
1087 /* Use SRC parm info since it may not be copied yet. */
1088 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1089 vec<tree> known_vals = vec<tree>();
1090 int count = ipa_get_param_count (parms_info);
1091 int i,j;
1092 clause_t possible_truths;
1093 struct predicate true_pred = true_predicate ();
1094 size_time_entry *e;
1095 int optimized_out_size = 0;
1096 bool inlined_to_p = false;
1097 struct cgraph_edge *edge;
1099 info->entry = 0;
1100 known_vals.safe_grow_cleared (count);
1101 for (i = 0; i < count; i++)
1103 tree t = ipa_get_param (parms_info, i);
1104 struct ipa_replace_map *r;
1106 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1108 if (r->old_tree == t
1109 && r->replace_p
1110 && !r->ref_p)
1112 known_vals[i] = r->new_tree;
1113 break;
1117 possible_truths = evaluate_conditions_for_known_args (dst, false,
1118 known_vals,
1119 vec<ipa_agg_jump_function_p>());
1120 known_vals.release ();
1122 account_size_time (info, 0, 0, &true_pred);
1124 /* Remap size_time vectors.
1125 Simplify the predicate by prunning out alternatives that are known
1126 to be false.
1127 TODO: as on optimization, we can also eliminate conditions known
1128 to be true. */
1129 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1131 struct predicate new_predicate;
1132 new_predicate = remap_predicate_after_duplication (&e->predicate,
1133 possible_truths,
1134 info);
1135 if (false_predicate_p (&new_predicate))
1136 optimized_out_size += e->size;
1137 else
1138 account_size_time (info, e->size, e->time, &new_predicate);
1141 /* Remap edge predicates with the same simplification as above.
1142 Also copy constantness arrays. */
1143 for (edge = dst->callees; edge; edge = edge->next_callee)
1145 struct predicate new_predicate;
1146 struct inline_edge_summary *es = inline_edge_summary (edge);
1148 if (!edge->inline_failed)
1149 inlined_to_p = true;
1150 if (!es->predicate)
1151 continue;
1152 new_predicate = remap_predicate_after_duplication (es->predicate,
1153 possible_truths,
1154 info);
1155 if (false_predicate_p (&new_predicate)
1156 && !false_predicate_p (es->predicate))
1158 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1159 edge->frequency = 0;
1161 edge_set_predicate (edge, &new_predicate);
1164 /* Remap indirect edge predicates with the same simplificaiton as above.
1165 Also copy constantness arrays. */
1166 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1168 struct predicate new_predicate;
1169 struct inline_edge_summary *es = inline_edge_summary (edge);
1171 gcc_checking_assert (edge->inline_failed);
1172 if (!es->predicate)
1173 continue;
1174 new_predicate = remap_predicate_after_duplication (es->predicate,
1175 possible_truths,
1176 info);
1177 if (false_predicate_p (&new_predicate)
1178 && !false_predicate_p (es->predicate))
1180 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1181 edge->frequency = 0;
1183 edge_set_predicate (edge, &new_predicate);
1185 remap_hint_predicate_after_duplication (&info->loop_iterations,
1186 possible_truths,
1187 info);
1188 remap_hint_predicate_after_duplication (&info->loop_stride,
1189 possible_truths,
1190 info);
1191 remap_hint_predicate_after_duplication (&info->array_index,
1192 possible_truths,
1193 info);
1195 /* If inliner or someone after inliner will ever start producing
1196 non-trivial clones, we will get trouble with lack of information
1197 about updating self sizes, because size vectors already contains
1198 sizes of the calees. */
1199 gcc_assert (!inlined_to_p
1200 || !optimized_out_size);
1202 else
1204 info->entry = vec_safe_copy (info->entry);
1205 if (info->loop_iterations)
1207 predicate p = *info->loop_iterations;
1208 info->loop_iterations = NULL;
1209 set_hint_predicate (&info->loop_iterations, p);
1211 if (info->loop_stride)
1213 predicate p = *info->loop_stride;
1214 info->loop_stride = NULL;
1215 set_hint_predicate (&info->loop_stride, p);
1217 if (info->array_index)
1219 predicate p = *info->array_index;
1220 info->array_index = NULL;
1221 set_hint_predicate (&info->array_index, p);
1224 inline_update_overall_summary (dst);
1228 /* Hook that is called by cgraph.c when a node is duplicated. */
1230 static void
1231 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1232 ATTRIBUTE_UNUSED void *data)
1234 struct inline_edge_summary *info;
1235 struct inline_edge_summary *srcinfo;
1236 inline_summary_alloc ();
1237 info = inline_edge_summary (dst);
1238 srcinfo = inline_edge_summary (src);
1239 memcpy (info, srcinfo,
1240 sizeof (struct inline_edge_summary));
1241 info->predicate = NULL;
1242 edge_set_predicate (dst, srcinfo->predicate);
1243 info->param = srcinfo->param.copy ();
1247 /* Keep edge cache consistent across edge removal. */
1249 static void
1250 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
1252 if (edge_growth_cache.exists ())
1253 reset_edge_growth_cache (edge);
1254 reset_inline_edge_summary (edge);
1258 /* Initialize growth caches. */
1260 void
1261 initialize_growth_caches (void)
1263 if (cgraph_edge_max_uid)
1264 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
1265 if (cgraph_max_uid)
1266 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
1270 /* Free growth caches. */
1272 void
1273 free_growth_caches (void)
1275 edge_growth_cache.release ();
1276 node_growth_cache.release ();
1280 /* Dump edge summaries associated to NODE and recursively to all clones.
1281 Indent by INDENT. */
1283 static void
1284 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
1285 struct inline_summary *info)
1287 struct cgraph_edge *edge;
1288 for (edge = node->callees; edge; edge = edge->next_callee)
1290 struct inline_edge_summary *es = inline_edge_summary (edge);
1291 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1292 int i;
1294 fprintf (f, "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i time: %2i callee size:%2i stack:%2i",
1295 indent, "", cgraph_node_name (callee),
1296 callee->uid,
1297 !edge->inline_failed ? "inlined"
1298 : cgraph_inline_failed_string (edge->inline_failed),
1299 indent, "",
1300 es->loop_depth,
1301 edge->frequency,
1302 es->call_stmt_size,
1303 es->call_stmt_time,
1304 (int)inline_summary (callee)->size / INLINE_SIZE_SCALE,
1305 (int)inline_summary (callee)->estimated_stack_size);
1307 if (es->predicate)
1309 fprintf (f, " predicate: ");
1310 dump_predicate (f, info->conds, es->predicate);
1312 else
1313 fprintf (f, "\n");
1314 if (es->param.exists ())
1315 for (i = 0; i < (int)es->param.length (); i++)
1317 int prob = es->param[i].change_prob;
1319 if (!prob)
1320 fprintf (f, "%*s op%i is compile time invariant\n",
1321 indent + 2, "", i);
1322 else if (prob != REG_BR_PROB_BASE)
1323 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1324 prob * 100.0 / REG_BR_PROB_BASE);
1326 if (!edge->inline_failed)
1328 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1329 " callee size %i\n",
1330 indent+2, "",
1331 (int)inline_summary (callee)->stack_frame_offset,
1332 (int)inline_summary (callee)->estimated_self_stack_size,
1333 (int)inline_summary (callee)->estimated_stack_size);
1334 dump_inline_edge_summary (f, indent+2, callee, info);
1337 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1339 struct inline_edge_summary *es = inline_edge_summary (edge);
1340 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1341 " time: %2i",
1342 indent, "",
1343 es->loop_depth,
1344 edge->frequency,
1345 es->call_stmt_size,
1346 es->call_stmt_time);
1347 if (es->predicate)
1349 fprintf (f, "predicate: ");
1350 dump_predicate (f, info->conds, es->predicate);
1352 else
1353 fprintf (f, "\n");
1358 void
1359 dump_inline_summary (FILE * f, struct cgraph_node *node)
1361 if (node->analyzed)
1363 struct inline_summary *s = inline_summary (node);
1364 size_time_entry *e;
1365 int i;
1366 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
1367 node->uid);
1368 if (DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl))
1369 fprintf (f, " always_inline");
1370 if (s->inlinable)
1371 fprintf (f, " inlinable");
1372 fprintf (f, "\n self time: %i\n",
1373 s->self_time);
1374 fprintf (f, " global time: %i\n", s->time);
1375 fprintf (f, " self size: %i\n",
1376 s->self_size);
1377 fprintf (f, " global size: %i\n", s->size);
1378 fprintf (f, " self stack: %i\n",
1379 (int) s->estimated_self_stack_size);
1380 fprintf (f, " global stack: %i\n",
1381 (int) s->estimated_stack_size);
1382 if (s->growth)
1383 fprintf (f, " estimated growth:%i\n",
1384 (int) s->growth);
1385 if (s->scc_no)
1386 fprintf (f, " In SCC: %i\n",
1387 (int) s->scc_no);
1388 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1390 fprintf (f, " size:%f, time:%f, predicate:",
1391 (double) e->size / INLINE_SIZE_SCALE,
1392 (double) e->time / INLINE_TIME_SCALE);
1393 dump_predicate (f, s->conds, &e->predicate);
1395 if (s->loop_iterations)
1397 fprintf (f, " loop iterations:");
1398 dump_predicate (f, s->conds, s->loop_iterations);
1400 if (s->loop_stride)
1402 fprintf (f, " loop stride:");
1403 dump_predicate (f, s->conds, s->loop_stride);
1405 if (s->array_index)
1407 fprintf (f, " array index:");
1408 dump_predicate (f, s->conds, s->array_index);
1410 fprintf (f, " calls:\n");
1411 dump_inline_edge_summary (f, 4, node, s);
1412 fprintf (f, "\n");
1416 DEBUG_FUNCTION void
1417 debug_inline_summary (struct cgraph_node *node)
1419 dump_inline_summary (stderr, node);
1422 void
1423 dump_inline_summaries (FILE *f)
1425 struct cgraph_node *node;
1427 FOR_EACH_DEFINED_FUNCTION (node)
1428 if (!node->global.inlined_to)
1429 dump_inline_summary (f, node);
1432 /* Give initial reasons why inlining would fail on EDGE. This gets either
1433 nullified or usually overwritten by more precise reasons later. */
1435 void
1436 initialize_inline_failed (struct cgraph_edge *e)
1438 struct cgraph_node *callee = e->callee;
1440 if (e->indirect_unknown_callee)
1441 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1442 else if (!callee->analyzed)
1443 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1444 else if (callee->local.redefined_extern_inline)
1445 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1446 else if (e->call_stmt_cannot_inline_p)
1447 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1448 else
1449 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1452 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1453 boolean variable pointed to by DATA. */
1455 static bool
1456 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1457 void *data)
1459 bool *b = (bool *) data;
1460 *b = true;
1461 return true;
1464 /* If OP refers to value of function parameter, return the corresponding
1465 parameter. */
1467 static tree
1468 unmodified_parm_1 (gimple stmt, tree op)
1470 /* SSA_NAME referring to parm default def? */
1471 if (TREE_CODE (op) == SSA_NAME
1472 && SSA_NAME_IS_DEFAULT_DEF (op)
1473 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1474 return SSA_NAME_VAR (op);
1475 /* Non-SSA parm reference? */
1476 if (TREE_CODE (op) == PARM_DECL)
1478 bool modified = false;
1480 ao_ref refd;
1481 ao_ref_init (&refd, op);
1482 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1483 NULL);
1484 if (!modified)
1485 return op;
1487 return NULL_TREE;
1490 /* If OP refers to value of function parameter, return the corresponding
1491 parameter. Also traverse chains of SSA register assignments. */
1493 static tree
1494 unmodified_parm (gimple stmt, tree op)
1496 tree res = unmodified_parm_1 (stmt, op);
1497 if (res)
1498 return res;
1500 if (TREE_CODE (op) == SSA_NAME
1501 && !SSA_NAME_IS_DEFAULT_DEF (op)
1502 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1503 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1504 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1505 return NULL_TREE;
1508 /* If OP refers to a value of a function parameter or value loaded from an
1509 aggregate passed to a parameter (either by value or reference), return TRUE
1510 and store the number of the parameter to *INDEX_P and information whether
1511 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1512 the function parameters, STMT is the statement in which OP is used or
1513 loaded. */
1515 static bool
1516 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1517 gimple stmt, tree op, int *index_p,
1518 struct agg_position_info *aggpos)
1520 tree res = unmodified_parm_1 (stmt, op);
1522 gcc_checking_assert (aggpos);
1523 if (res)
1525 *index_p = ipa_get_param_decl_index (info, res);
1526 if (*index_p < 0)
1527 return false;
1528 aggpos->agg_contents = false;
1529 aggpos->by_ref = false;
1530 return true;
1533 if (TREE_CODE (op) == SSA_NAME)
1535 if (SSA_NAME_IS_DEFAULT_DEF (op)
1536 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1537 return false;
1538 stmt = SSA_NAME_DEF_STMT (op);
1539 op = gimple_assign_rhs1 (stmt);
1540 if (!REFERENCE_CLASS_P (op))
1541 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1542 aggpos);
1545 aggpos->agg_contents = true;
1546 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1547 &aggpos->by_ref);
1550 /* See if statement might disappear after inlining.
1551 0 - means not eliminated
1552 1 - half of statements goes away
1553 2 - for sure it is eliminated.
1554 We are not terribly sophisticated, basically looking for simple abstraction
1555 penalty wrappers. */
1557 static int
1558 eliminated_by_inlining_prob (gimple stmt)
1560 enum gimple_code code = gimple_code (stmt);
1561 enum tree_code rhs_code;
1563 if (!optimize)
1564 return 0;
1566 switch (code)
1568 case GIMPLE_RETURN:
1569 return 2;
1570 case GIMPLE_ASSIGN:
1571 if (gimple_num_ops (stmt) != 2)
1572 return 0;
1574 rhs_code = gimple_assign_rhs_code (stmt);
1576 /* Casts of parameters, loads from parameters passed by reference
1577 and stores to return value or parameters are often free after
1578 inlining dua to SRA and further combining.
1579 Assume that half of statements goes away. */
1580 if (rhs_code == CONVERT_EXPR
1581 || rhs_code == NOP_EXPR
1582 || rhs_code == VIEW_CONVERT_EXPR
1583 || rhs_code == ADDR_EXPR
1584 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1586 tree rhs = gimple_assign_rhs1 (stmt);
1587 tree lhs = gimple_assign_lhs (stmt);
1588 tree inner_rhs = get_base_address (rhs);
1589 tree inner_lhs = get_base_address (lhs);
1590 bool rhs_free = false;
1591 bool lhs_free = false;
1593 if (!inner_rhs)
1594 inner_rhs = rhs;
1595 if (!inner_lhs)
1596 inner_lhs = lhs;
1598 /* Reads of parameter are expected to be free. */
1599 if (unmodified_parm (stmt, inner_rhs))
1600 rhs_free = true;
1601 /* Match expressions of form &this->field. Those will most likely
1602 combine with something upstream after inlining. */
1603 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1605 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1606 if (TREE_CODE (op) == PARM_DECL)
1607 rhs_free = true;
1608 else if (TREE_CODE (op) == MEM_REF
1609 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1610 rhs_free = true;
1613 /* When parameter is not SSA register because its address is taken
1614 and it is just copied into one, the statement will be completely
1615 free after inlining (we will copy propagate backward). */
1616 if (rhs_free && is_gimple_reg (lhs))
1617 return 2;
1619 /* Reads of parameters passed by reference
1620 expected to be free (i.e. optimized out after inlining). */
1621 if (TREE_CODE(inner_rhs) == MEM_REF
1622 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1623 rhs_free = true;
1625 /* Copying parameter passed by reference into gimple register is
1626 probably also going to copy propagate, but we can't be quite
1627 sure. */
1628 if (rhs_free && is_gimple_reg (lhs))
1629 lhs_free = true;
1631 /* Writes to parameters, parameters passed by value and return value
1632 (either dirrectly or passed via invisible reference) are free.
1634 TODO: We ought to handle testcase like
1635 struct a {int a,b;};
1636 struct a
1637 retrurnsturct (void)
1639 struct a a ={1,2};
1640 return a;
1643 This translate into:
1645 retrurnsturct ()
1647 int a$b;
1648 int a$a;
1649 struct a a;
1650 struct a D.2739;
1652 <bb 2>:
1653 D.2739.a = 1;
1654 D.2739.b = 2;
1655 return D.2739;
1658 For that we either need to copy ipa-split logic detecting writes
1659 to return value. */
1660 if (TREE_CODE (inner_lhs) == PARM_DECL
1661 || TREE_CODE (inner_lhs) == RESULT_DECL
1662 || (TREE_CODE(inner_lhs) == MEM_REF
1663 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1664 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1665 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1666 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1667 (inner_lhs, 0))) == RESULT_DECL))))
1668 lhs_free = true;
1669 if (lhs_free
1670 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1671 rhs_free = true;
1672 if (lhs_free && rhs_free)
1673 return 1;
1675 return 0;
1676 default:
1677 return 0;
1682 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1683 predicates to the CFG edges. */
1685 static void
1686 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1687 struct inline_summary *summary,
1688 basic_block bb)
1690 gimple last;
1691 tree op;
1692 int index;
1693 struct agg_position_info aggpos;
1694 enum tree_code code, inverted_code;
1695 edge e;
1696 edge_iterator ei;
1697 gimple set_stmt;
1698 tree op2;
1700 last = last_stmt (bb);
1701 if (!last
1702 || gimple_code (last) != GIMPLE_COND)
1703 return;
1704 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1705 return;
1706 op = gimple_cond_lhs (last);
1707 /* TODO: handle conditionals like
1708 var = op0 < 4;
1709 if (var != 0). */
1710 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1712 code = gimple_cond_code (last);
1713 inverted_code
1714 = invert_tree_comparison (code,
1715 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1717 FOR_EACH_EDGE (e, ei, bb->succs)
1719 struct predicate p = add_condition (summary, index, &aggpos,
1720 e->flags & EDGE_TRUE_VALUE
1721 ? code : inverted_code,
1722 gimple_cond_rhs (last));
1723 e->aux = pool_alloc (edge_predicate_pool);
1724 *(struct predicate *)e->aux = p;
1728 if (TREE_CODE (op) != SSA_NAME)
1729 return;
1730 /* Special case
1731 if (builtin_constant_p (op))
1732 constant_code
1733 else
1734 nonconstant_code.
1735 Here we can predicate nonconstant_code. We can't
1736 really handle constant_code since we have no predicate
1737 for this and also the constant code is not known to be
1738 optimized away when inliner doen't see operand is constant.
1739 Other optimizers might think otherwise. */
1740 if (gimple_cond_code (last) != NE_EXPR
1741 || !integer_zerop (gimple_cond_rhs (last)))
1742 return;
1743 set_stmt = SSA_NAME_DEF_STMT (op);
1744 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1745 || gimple_call_num_args (set_stmt) != 1)
1746 return;
1747 op2 = gimple_call_arg (set_stmt, 0);
1748 if (!unmodified_parm_or_parm_agg_item (info, set_stmt, op2, &index, &aggpos))
1749 return;
1750 FOR_EACH_EDGE (e, ei, bb->succs)
1751 if (e->flags & EDGE_FALSE_VALUE)
1753 struct predicate p = add_condition (summary, index, &aggpos,
1754 IS_NOT_CONSTANT, NULL_TREE);
1755 e->aux = pool_alloc (edge_predicate_pool);
1756 *(struct predicate *)e->aux = p;
1761 /* If BB ends by a switch we can turn into predicates, attach corresponding
1762 predicates to the CFG edges. */
1764 static void
1765 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1766 struct inline_summary *summary,
1767 basic_block bb)
1769 gimple last;
1770 tree op;
1771 int index;
1772 struct agg_position_info aggpos;
1773 edge e;
1774 edge_iterator ei;
1775 size_t n;
1776 size_t case_idx;
1778 last = last_stmt (bb);
1779 if (!last
1780 || gimple_code (last) != GIMPLE_SWITCH)
1781 return;
1782 op = gimple_switch_index (last);
1783 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1784 return;
1786 FOR_EACH_EDGE (e, ei, bb->succs)
1788 e->aux = pool_alloc (edge_predicate_pool);
1789 *(struct predicate *)e->aux = false_predicate ();
1791 n = gimple_switch_num_labels(last);
1792 for (case_idx = 0; case_idx < n; ++case_idx)
1794 tree cl = gimple_switch_label (last, case_idx);
1795 tree min, max;
1796 struct predicate p;
1798 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1799 min = CASE_LOW (cl);
1800 max = CASE_HIGH (cl);
1802 /* For default we might want to construct predicate that none
1803 of cases is met, but it is bit hard to do not having negations
1804 of conditionals handy. */
1805 if (!min && !max)
1806 p = true_predicate ();
1807 else if (!max)
1808 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1809 else
1811 struct predicate p1, p2;
1812 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1813 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1814 p = and_predicates (summary->conds, &p1, &p2);
1816 *(struct predicate *)e->aux
1817 = or_predicates (summary->conds, &p, (struct predicate *)e->aux);
1822 /* For each BB in NODE attach to its AUX pointer predicate under
1823 which it is executable. */
1825 static void
1826 compute_bb_predicates (struct cgraph_node *node,
1827 struct ipa_node_params *parms_info,
1828 struct inline_summary *summary)
1830 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
1831 bool done = false;
1832 basic_block bb;
1834 FOR_EACH_BB_FN (bb, my_function)
1836 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1837 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1840 /* Entry block is always executable. */
1841 ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1842 = pool_alloc (edge_predicate_pool);
1843 *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1844 = true_predicate ();
1846 /* A simple dataflow propagation of predicates forward in the CFG.
1847 TODO: work in reverse postorder. */
1848 while (!done)
1850 done = true;
1851 FOR_EACH_BB_FN (bb, my_function)
1853 struct predicate p = false_predicate ();
1854 edge e;
1855 edge_iterator ei;
1856 FOR_EACH_EDGE (e, ei, bb->preds)
1858 if (e->src->aux)
1860 struct predicate this_bb_predicate
1861 = *(struct predicate *)e->src->aux;
1862 if (e->aux)
1863 this_bb_predicate
1864 = and_predicates (summary->conds, &this_bb_predicate,
1865 (struct predicate *)e->aux);
1866 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1867 if (true_predicate_p (&p))
1868 break;
1871 if (false_predicate_p (&p))
1872 gcc_assert (!bb->aux);
1873 else
1875 if (!bb->aux)
1877 done = false;
1878 bb->aux = pool_alloc (edge_predicate_pool);
1879 *((struct predicate *)bb->aux) = p;
1881 else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1883 done = false;
1884 *((struct predicate *)bb->aux) = p;
1892 /* We keep info about constantness of SSA names. */
1894 typedef struct predicate predicate_t;
1895 /* Return predicate specifying when the STMT might have result that is not
1896 a compile time constant. */
1898 static struct predicate
1899 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1900 struct inline_summary *summary,
1901 tree expr,
1902 vec<predicate_t> nonconstant_names)
1904 tree parm;
1905 int index;
1907 while (UNARY_CLASS_P (expr))
1908 expr = TREE_OPERAND (expr, 0);
1910 parm = unmodified_parm (NULL, expr);
1911 if (parm
1912 && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1913 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1914 if (is_gimple_min_invariant (expr))
1915 return false_predicate ();
1916 if (TREE_CODE (expr) == SSA_NAME)
1917 return nonconstant_names[SSA_NAME_VERSION (expr)];
1918 if (BINARY_CLASS_P (expr)
1919 || COMPARISON_CLASS_P (expr))
1921 struct predicate p1 = will_be_nonconstant_expr_predicate
1922 (info, summary, TREE_OPERAND (expr, 0),
1923 nonconstant_names);
1924 struct predicate p2;
1925 if (true_predicate_p (&p1))
1926 return p1;
1927 p2 = will_be_nonconstant_expr_predicate (info, summary,
1928 TREE_OPERAND (expr, 1),
1929 nonconstant_names);
1930 return or_predicates (summary->conds, &p1, &p2);
1932 else if (TREE_CODE (expr) == COND_EXPR)
1934 struct predicate p1 = will_be_nonconstant_expr_predicate
1935 (info, summary, TREE_OPERAND (expr, 0),
1936 nonconstant_names);
1937 struct predicate p2;
1938 if (true_predicate_p (&p1))
1939 return p1;
1940 p2 = will_be_nonconstant_expr_predicate (info, summary,
1941 TREE_OPERAND (expr, 1),
1942 nonconstant_names);
1943 if (true_predicate_p (&p2))
1944 return p2;
1945 p1 = or_predicates (summary->conds, &p1, &p2);
1946 p2 = will_be_nonconstant_expr_predicate (info, summary,
1947 TREE_OPERAND (expr, 2),
1948 nonconstant_names);
1949 return or_predicates (summary->conds, &p1, &p2);
1951 else
1953 debug_tree (expr);
1954 gcc_unreachable ();
1956 return false_predicate ();
1960 /* Return predicate specifying when the STMT might have result that is not
1961 a compile time constant. */
1963 static struct predicate
1964 will_be_nonconstant_predicate (struct ipa_node_params *info,
1965 struct inline_summary *summary,
1966 gimple stmt,
1967 vec<predicate_t> nonconstant_names)
1969 struct predicate p = true_predicate ();
1970 ssa_op_iter iter;
1971 tree use;
1972 struct predicate op_non_const;
1973 bool is_load;
1974 int base_index;
1975 struct agg_position_info aggpos;
1977 /* What statments might be optimized away
1978 when their arguments are constant
1979 TODO: also trivial builtins.
1980 builtin_constant_p is already handled later. */
1981 if (gimple_code (stmt) != GIMPLE_ASSIGN
1982 && gimple_code (stmt) != GIMPLE_COND
1983 && gimple_code (stmt) != GIMPLE_SWITCH)
1984 return p;
1986 /* Stores will stay anyway. */
1987 if (gimple_store_p (stmt))
1988 return p;
1990 is_load = gimple_assign_load_p (stmt);
1992 /* Loads can be optimized when the value is known. */
1993 if (is_load)
1995 tree op;
1996 gcc_assert (gimple_assign_single_p (stmt));
1997 op = gimple_assign_rhs1 (stmt);
1998 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
1999 &aggpos))
2000 return p;
2002 else
2003 base_index = -1;
2005 /* See if we understand all operands before we start
2006 adding conditionals. */
2007 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2009 tree parm = unmodified_parm (stmt, use);
2010 /* For arguments we can build a condition. */
2011 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2012 continue;
2013 if (TREE_CODE (use) != SSA_NAME)
2014 return p;
2015 /* If we know when operand is constant,
2016 we still can say something useful. */
2017 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2018 continue;
2019 return p;
2022 if (is_load)
2023 op_non_const = add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2024 else
2025 op_non_const = false_predicate ();
2026 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2028 tree parm = unmodified_parm (stmt, use);
2029 int index;
2031 if (parm
2032 && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2034 if (index != base_index)
2035 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2036 else
2037 continue;
2039 else
2040 p = nonconstant_names[SSA_NAME_VERSION (use)];
2041 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2043 if (gimple_code (stmt) == GIMPLE_ASSIGN
2044 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2045 nonconstant_names[SSA_NAME_VERSION (gimple_assign_lhs (stmt))]
2046 = op_non_const;
2047 return op_non_const;
2050 struct record_modified_bb_info
2052 bitmap bb_set;
2053 gimple stmt;
2056 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2057 set except for info->stmt. */
2059 static bool
2060 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef,
2061 void *data)
2063 struct record_modified_bb_info *info = (struct record_modified_bb_info *) data;
2064 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2065 return false;
2066 bitmap_set_bit (info->bb_set,
2067 SSA_NAME_IS_DEFAULT_DEF (vdef)
2068 ? ENTRY_BLOCK_PTR->index : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2069 return false;
2072 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2073 will change since last invocation of STMT.
2075 Value 0 is reserved for compile time invariants.
2076 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2077 ought to be REG_BR_PROB_BASE / estimated_iters. */
2079 static int
2080 param_change_prob (gimple stmt, int i)
2082 tree op = gimple_call_arg (stmt, i);
2083 basic_block bb = gimple_bb (stmt);
2084 tree base;
2086 if (is_gimple_min_invariant (op))
2087 return 0;
2088 /* We would have to do non-trivial analysis to really work out what
2089 is the probability of value to change (i.e. when init statement
2090 is in a sibling loop of the call).
2092 We do an conservative estimate: when call is executed N times more often
2093 than the statement defining value, we take the frequency 1/N. */
2094 if (TREE_CODE (op) == SSA_NAME)
2096 int init_freq;
2098 if (!bb->frequency)
2099 return REG_BR_PROB_BASE;
2101 if (SSA_NAME_IS_DEFAULT_DEF (op))
2102 init_freq = ENTRY_BLOCK_PTR->frequency;
2103 else
2104 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2106 if (!init_freq)
2107 init_freq = 1;
2108 if (init_freq < bb->frequency)
2109 return MAX ((init_freq * REG_BR_PROB_BASE +
2110 bb->frequency / 2) / bb->frequency, 1);
2111 else
2112 return REG_BR_PROB_BASE;
2115 base = get_base_address (op);
2116 if (base)
2118 ao_ref refd;
2119 int max;
2120 struct record_modified_bb_info info;
2121 bitmap_iterator bi;
2122 unsigned index;
2124 if (const_value_known_p (base))
2125 return 0;
2126 if (!bb->frequency)
2127 return REG_BR_PROB_BASE;
2128 ao_ref_init (&refd, op);
2129 info.stmt = stmt;
2130 info.bb_set = BITMAP_ALLOC (NULL);
2131 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2132 NULL);
2133 if (bitmap_bit_p (info.bb_set, bb->index))
2135 BITMAP_FREE (info.bb_set);
2136 return REG_BR_PROB_BASE;
2139 /* Assume that every memory is initialized at entry.
2140 TODO: Can we easilly determine if value is always defined
2141 and thus we may skip entry block? */
2142 if (ENTRY_BLOCK_PTR->frequency)
2143 max = ENTRY_BLOCK_PTR->frequency;
2144 else
2145 max = 1;
2147 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2148 max = MIN (max, BASIC_BLOCK (index)->frequency);
2150 BITMAP_FREE (info.bb_set);
2151 if (max < bb->frequency)
2152 return MAX ((max * REG_BR_PROB_BASE +
2153 bb->frequency / 2) / bb->frequency, 1);
2154 else
2155 return REG_BR_PROB_BASE;
2157 return REG_BR_PROB_BASE;
2160 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2161 sub-graph and if the predicate the condition depends on is known. If so,
2162 return true and store the pointer the predicate in *P. */
2164 static bool
2165 phi_result_unknown_predicate (struct ipa_node_params *info,
2166 struct inline_summary *summary, basic_block bb,
2167 struct predicate *p,
2168 vec<predicate_t> nonconstant_names)
2170 edge e;
2171 edge_iterator ei;
2172 basic_block first_bb = NULL;
2173 gimple stmt;
2175 if (single_pred_p (bb))
2177 *p = false_predicate ();
2178 return true;
2181 FOR_EACH_EDGE (e, ei, bb->preds)
2183 if (single_succ_p (e->src))
2185 if (!single_pred_p (e->src))
2186 return false;
2187 if (!first_bb)
2188 first_bb = single_pred (e->src);
2189 else if (single_pred (e->src) != first_bb)
2190 return false;
2192 else
2194 if (!first_bb)
2195 first_bb = e->src;
2196 else if (e->src != first_bb)
2197 return false;
2201 if (!first_bb)
2202 return false;
2204 stmt = last_stmt (first_bb);
2205 if (!stmt
2206 || gimple_code (stmt) != GIMPLE_COND
2207 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2208 return false;
2210 *p = will_be_nonconstant_expr_predicate (info, summary,
2211 gimple_cond_lhs (stmt),
2212 nonconstant_names);
2213 if (true_predicate_p (p))
2214 return false;
2215 else
2216 return true;
2219 /* Given a PHI statement in a function described by inline properties SUMMARY
2220 and *P being the predicate describing whether the selected PHI argument is
2221 known, store a predicate for the result of the PHI statement into
2222 NONCONSTANT_NAMES, if possible. */
2224 static void
2225 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2226 struct predicate *p,
2227 vec<predicate_t> nonconstant_names)
2229 unsigned i;
2231 for (i = 0; i < gimple_phi_num_args (phi); i++)
2233 tree arg = gimple_phi_arg (phi, i)->def;
2234 if (!is_gimple_min_invariant (arg))
2236 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2237 *p = or_predicates (summary->conds, p,
2238 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2239 if (true_predicate_p (p))
2240 return;
2244 if (dump_file && (dump_flags & TDF_DETAILS))
2246 fprintf (dump_file, "\t\tphi predicate: ");
2247 dump_predicate (dump_file, summary->conds, p);
2249 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2252 /* Return predicate specifying when array index in access OP becomes non-constant. */
2254 static struct predicate
2255 array_index_predicate (struct inline_summary *info,
2256 vec<predicate_t> nonconstant_names, tree op)
2258 struct predicate p = false_predicate ();
2259 while (handled_component_p (op))
2261 if (TREE_CODE (op) == ARRAY_REF
2262 || TREE_CODE (op) == ARRAY_RANGE_REF)
2264 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2265 p = or_predicates (info->conds, &p,
2266 &nonconstant_names[
2267 SSA_NAME_VERSION (TREE_OPERAND (op, 1))]);
2269 op = TREE_OPERAND (op, 0);
2271 return p;
2274 /* Compute function body size parameters for NODE.
2275 When EARLY is true, we compute only simple summaries without
2276 non-trivial predicates to drive the early inliner. */
2278 static void
2279 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2281 gcov_type time = 0;
2282 /* Estimate static overhead for function prologue/epilogue and alignment. */
2283 int size = 2;
2284 /* Benefits are scaled by probability of elimination that is in range
2285 <0,2>. */
2286 basic_block bb;
2287 gimple_stmt_iterator bsi;
2288 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
2289 int freq;
2290 struct inline_summary *info = inline_summary (node);
2291 struct predicate bb_predicate;
2292 struct ipa_node_params *parms_info = NULL;
2293 vec<predicate_t> nonconstant_names = vec<predicate_t>();
2294 int nblocks, n;
2295 int *order;
2296 predicate array_index = true_predicate ();
2298 info->conds = NULL;
2299 info->entry = NULL;
2301 if (optimize && !early)
2303 calculate_dominance_info (CDI_DOMINATORS);
2304 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2306 if (ipa_node_params_vector.exists ())
2308 parms_info = IPA_NODE_REF (node);
2309 nonconstant_names.safe_grow_cleared(SSANAMES (my_function)->length());
2313 if (dump_file)
2314 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2315 cgraph_node_name (node));
2317 /* When we run into maximal number of entries, we assign everything to the
2318 constant truth case. Be sure to have it in list. */
2319 bb_predicate = true_predicate ();
2320 account_size_time (info, 0, 0, &bb_predicate);
2322 bb_predicate = not_inlined_predicate ();
2323 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2325 gcc_assert (my_function && my_function->cfg);
2326 if (parms_info)
2327 compute_bb_predicates (node, parms_info, info);
2328 gcc_assert (cfun == my_function);
2329 order = XNEWVEC (int, n_basic_blocks);
2330 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2331 for (n = 0; n < nblocks; n++)
2333 bb = BASIC_BLOCK (order[n]);
2334 freq = compute_call_stmt_bb_frequency (node->symbol.decl, bb);
2336 /* TODO: Obviously predicates can be propagated down across CFG. */
2337 if (parms_info)
2339 if (bb->aux)
2340 bb_predicate = *(struct predicate *)bb->aux;
2341 else
2342 bb_predicate = false_predicate ();
2344 else
2345 bb_predicate = true_predicate ();
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2349 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2350 dump_predicate (dump_file, info->conds, &bb_predicate);
2353 if (parms_info && nonconstant_names.exists ())
2355 struct predicate phi_predicate;
2356 bool first_phi = true;
2358 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2360 if (first_phi
2361 && !phi_result_unknown_predicate (parms_info, info, bb,
2362 &phi_predicate,
2363 nonconstant_names))
2364 break;
2365 first_phi = false;
2366 if (dump_file && (dump_flags & TDF_DETAILS))
2368 fprintf (dump_file, " ");
2369 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2371 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2372 nonconstant_names);
2376 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2378 gimple stmt = gsi_stmt (bsi);
2379 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2380 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2381 int prob;
2382 struct predicate will_be_nonconstant;
2384 if (dump_file && (dump_flags & TDF_DETAILS))
2386 fprintf (dump_file, " ");
2387 print_gimple_stmt (dump_file, stmt, 0, 0);
2388 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2389 ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
2392 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2394 struct predicate this_array_index;
2395 this_array_index = array_index_predicate (info, nonconstant_names,
2396 gimple_assign_rhs1 (stmt));
2397 if (!false_predicate_p (&this_array_index))
2398 array_index = and_predicates (info->conds, &array_index, &this_array_index);
2400 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2402 struct predicate this_array_index;
2403 this_array_index = array_index_predicate (info, nonconstant_names,
2404 gimple_get_lhs (stmt));
2405 if (!false_predicate_p (&this_array_index))
2406 array_index = and_predicates (info->conds, &array_index, &this_array_index);
2410 if (is_gimple_call (stmt))
2412 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2413 struct inline_edge_summary *es = inline_edge_summary (edge);
2415 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2416 resolved as constant. We however don't want to optimize
2417 out the cgraph edges. */
2418 if (nonconstant_names.exists ()
2419 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2420 && gimple_call_lhs (stmt)
2421 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2423 struct predicate false_p = false_predicate ();
2424 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2425 = false_p;
2427 if (ipa_node_params_vector.exists ())
2429 int count = gimple_call_num_args (stmt);
2430 int i;
2432 if (count)
2433 es->param.safe_grow_cleared (count);
2434 for (i = 0; i < count; i++)
2436 int prob = param_change_prob (stmt, i);
2437 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2438 es->param[i].change_prob = prob;
2442 es->call_stmt_size = this_size;
2443 es->call_stmt_time = this_time;
2444 es->loop_depth = bb_loop_depth (bb);
2445 edge_set_predicate (edge, &bb_predicate);
2448 /* TODO: When conditional jump or swithc is known to be constant, but
2449 we did not translate it into the predicates, we really can account
2450 just maximum of the possible paths. */
2451 if (parms_info)
2452 will_be_nonconstant
2453 = will_be_nonconstant_predicate (parms_info, info,
2454 stmt, nonconstant_names);
2455 if (this_time || this_size)
2457 struct predicate p;
2459 this_time *= freq;
2461 prob = eliminated_by_inlining_prob (stmt);
2462 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2463 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
2464 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2465 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2467 if (parms_info)
2468 p = and_predicates (info->conds, &bb_predicate,
2469 &will_be_nonconstant);
2470 else
2471 p = true_predicate ();
2473 if (!false_predicate_p (&p))
2475 time += this_time;
2476 size += this_size;
2477 if (time > MAX_TIME * INLINE_TIME_SCALE)
2478 time = MAX_TIME * INLINE_TIME_SCALE;
2481 /* We account everything but the calls. Calls have their own
2482 size/time info attached to cgraph edges. This is necessary
2483 in order to make the cost disappear after inlining. */
2484 if (!is_gimple_call (stmt))
2486 if (prob)
2488 struct predicate ip = not_inlined_predicate ();
2489 ip = and_predicates (info->conds, &ip, &p);
2490 account_size_time (info, this_size * prob,
2491 this_time * prob, &ip);
2493 if (prob != 2)
2494 account_size_time (info, this_size * (2 - prob),
2495 this_time * (2 - prob), &p);
2498 gcc_assert (time >= 0);
2499 gcc_assert (size >= 0);
2503 set_hint_predicate (&inline_summary (node)->array_index, array_index);
2504 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2505 if (time > MAX_TIME)
2506 time = MAX_TIME;
2507 free (order);
2509 if (!early && nonconstant_names.exists ())
2511 struct loop *loop;
2512 loop_iterator li;
2513 predicate loop_iterations = true_predicate ();
2514 predicate loop_stride = true_predicate ();
2516 if (dump_file && (dump_flags & TDF_DETAILS))
2517 flow_loops_dump (dump_file, NULL, 0);
2518 scev_initialize ();
2519 FOR_EACH_LOOP (li, loop, 0)
2521 vec<edge> exits;
2522 edge ex;
2523 unsigned int j, i;
2524 struct tree_niter_desc niter_desc;
2525 basic_block *body = get_loop_body (loop);
2526 bb_predicate = *(struct predicate *)loop->header->aux;
2528 exits = get_loop_exit_edges (loop);
2529 FOR_EACH_VEC_ELT (exits, j, ex)
2530 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2531 && !is_gimple_min_invariant (niter_desc.niter))
2533 predicate will_be_nonconstant
2534 = will_be_nonconstant_expr_predicate (parms_info, info,
2535 niter_desc.niter, nonconstant_names);
2536 if (!true_predicate_p (&will_be_nonconstant))
2537 will_be_nonconstant = and_predicates (info->conds,
2538 &bb_predicate,
2539 &will_be_nonconstant);
2540 if (!true_predicate_p (&will_be_nonconstant)
2541 && !false_predicate_p (&will_be_nonconstant))
2542 /* This is slightly inprecise. We may want to represent each loop with
2543 independent predicate. */
2544 loop_iterations = and_predicates (info->conds, &loop_iterations, &will_be_nonconstant);
2546 exits.release ();
2548 for (i = 0; i < loop->num_nodes; i++)
2550 gimple_stmt_iterator gsi;
2551 bb_predicate = *(struct predicate *)body[i]->aux;
2552 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
2554 gimple stmt = gsi_stmt (gsi);
2555 affine_iv iv;
2556 ssa_op_iter iter;
2557 tree use;
2559 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2561 predicate will_be_nonconstant;
2563 if (!simple_iv (loop, loop_containing_stmt (stmt), use, &iv, true)
2564 || is_gimple_min_invariant (iv.step))
2565 continue;
2566 will_be_nonconstant
2567 = will_be_nonconstant_expr_predicate (parms_info, info,
2568 iv.step, nonconstant_names);
2569 if (!true_predicate_p (&will_be_nonconstant))
2570 will_be_nonconstant = and_predicates (info->conds,
2571 &bb_predicate,
2572 &will_be_nonconstant);
2573 if (!true_predicate_p (&will_be_nonconstant)
2574 && !false_predicate_p (&will_be_nonconstant))
2575 /* This is slightly inprecise. We may want to represent each loop with
2576 independent predicate. */
2577 loop_stride = and_predicates (info->conds, &loop_stride, &will_be_nonconstant);
2581 free (body);
2583 set_hint_predicate (&inline_summary (node)->loop_iterations, loop_iterations);
2584 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2585 scev_finalize ();
2587 FOR_ALL_BB_FN (bb, my_function)
2589 edge e;
2590 edge_iterator ei;
2592 if (bb->aux)
2593 pool_free (edge_predicate_pool, bb->aux);
2594 bb->aux = NULL;
2595 FOR_EACH_EDGE (e, ei, bb->succs)
2597 if (e->aux)
2598 pool_free (edge_predicate_pool, e->aux);
2599 e->aux = NULL;
2602 inline_summary (node)->self_time = time;
2603 inline_summary (node)->self_size = size;
2604 nonconstant_names.release ();
2605 if (optimize && !early)
2607 loop_optimizer_finalize ();
2608 free_dominance_info (CDI_DOMINATORS);
2610 if (dump_file)
2612 fprintf (dump_file, "\n");
2613 dump_inline_summary (dump_file, node);
2618 /* Compute parameters of functions used by inliner.
2619 EARLY is true when we compute parameters for the early inliner */
2621 void
2622 compute_inline_parameters (struct cgraph_node *node, bool early)
2624 HOST_WIDE_INT self_stack_size;
2625 struct cgraph_edge *e;
2626 struct inline_summary *info;
2628 gcc_assert (!node->global.inlined_to);
2630 inline_summary_alloc ();
2632 info = inline_summary (node);
2633 reset_inline_summary (node);
2635 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2636 Once this happen, we will need to more curefully predict call
2637 statement size. */
2638 if (node->thunk.thunk_p)
2640 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2641 struct predicate t = true_predicate ();
2643 info->inlinable = 0;
2644 node->callees->call_stmt_cannot_inline_p = true;
2645 node->local.can_change_signature = false;
2646 es->call_stmt_time = 1;
2647 es->call_stmt_size = 1;
2648 account_size_time (info, 0, 0, &t);
2649 return;
2652 /* Even is_gimple_min_invariant rely on current_function_decl. */
2653 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2655 /* Estimate the stack size for the function if we're optimizing. */
2656 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2657 info->estimated_self_stack_size = self_stack_size;
2658 info->estimated_stack_size = self_stack_size;
2659 info->stack_frame_offset = 0;
2661 /* Can this function be inlined at all? */
2662 info->inlinable = tree_inlinable_function_p (node->symbol.decl);
2664 /* Type attributes can use parameter indices to describe them. */
2665 if (TYPE_ATTRIBUTES (TREE_TYPE (node->symbol.decl)))
2666 node->local.can_change_signature = false;
2667 else
2669 /* Otherwise, inlinable functions always can change signature. */
2670 if (info->inlinable)
2671 node->local.can_change_signature = true;
2672 else
2674 /* Functions calling builtin_apply can not change signature. */
2675 for (e = node->callees; e; e = e->next_callee)
2677 tree cdecl = e->callee->symbol.decl;
2678 if (DECL_BUILT_IN (cdecl)
2679 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2680 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2681 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2682 break;
2684 node->local.can_change_signature = !e;
2687 estimate_function_body_sizes (node, early);
2689 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2690 info->time = info->self_time;
2691 info->size = info->self_size;
2692 info->stack_frame_offset = 0;
2693 info->estimated_stack_size = info->estimated_self_stack_size;
2694 #ifdef ENABLE_CHECKING
2695 inline_update_overall_summary (node);
2696 gcc_assert (info->time == info->self_time
2697 && info->size == info->self_size);
2698 #endif
2700 pop_cfun ();
2704 /* Compute parameters of functions used by inliner using
2705 current_function_decl. */
2707 static unsigned int
2708 compute_inline_parameters_for_current (void)
2710 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2711 return 0;
2714 struct gimple_opt_pass pass_inline_parameters =
2717 GIMPLE_PASS,
2718 "inline_param", /* name */
2719 OPTGROUP_INLINE, /* optinfo_flags */
2720 NULL, /* gate */
2721 compute_inline_parameters_for_current,/* execute */
2722 NULL, /* sub */
2723 NULL, /* next */
2724 0, /* static_pass_number */
2725 TV_INLINE_PARAMETERS, /* tv_id */
2726 0, /* properties_required */
2727 0, /* properties_provided */
2728 0, /* properties_destroyed */
2729 0, /* todo_flags_start */
2730 0 /* todo_flags_finish */
2735 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2736 KNOWN_BINFOS. */
2738 static bool
2739 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2740 int *size, int *time,
2741 vec<tree> known_vals,
2742 vec<tree> known_binfos,
2743 vec<ipa_agg_jump_function_p> known_aggs)
2745 tree target;
2746 struct cgraph_node *callee;
2747 struct inline_summary *isummary;
2749 if (!known_vals.exists () && !known_binfos.exists ())
2750 return false;
2751 if (!flag_indirect_inlining)
2752 return false;
2754 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2755 known_aggs);
2756 if (!target)
2757 return false;
2759 /* Account for difference in cost between indirect and direct calls. */
2760 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
2761 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
2762 gcc_checking_assert (*time >= 0);
2763 gcc_checking_assert (*size >= 0);
2765 callee = cgraph_get_node (target);
2766 if (!callee || !callee->analyzed)
2767 return false;
2768 isummary = inline_summary (callee);
2769 return isummary->inlinable;
2772 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2774 static inline void
2775 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2776 int prob,
2777 vec<tree> known_vals,
2778 vec<tree> known_binfos,
2779 vec<ipa_agg_jump_function_p> known_aggs,
2780 inline_hints *hints)
2783 struct inline_edge_summary *es = inline_edge_summary (e);
2784 int call_size = es->call_stmt_size;
2785 int call_time = es->call_stmt_time;
2786 if (!e->callee
2787 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
2788 known_vals, known_binfos, known_aggs)
2789 && hints
2790 && cgraph_maybe_hot_edge_p (e))
2791 *hints |= INLINE_HINT_indirect_call;
2792 *size += call_size * INLINE_SIZE_SCALE;
2793 *time += call_time * prob / REG_BR_PROB_BASE
2794 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
2795 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2796 *time = MAX_TIME * INLINE_TIME_SCALE;
2801 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.
2802 POSSIBLE_TRUTHS, KNOWN_VALS and KNOWN_BINFOS describe context of the call
2803 site. */
2805 static void
2806 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
2807 inline_hints *hints,
2808 clause_t possible_truths,
2809 vec<tree> known_vals,
2810 vec<tree> known_binfos,
2811 vec<ipa_agg_jump_function_p> known_aggs)
2813 struct cgraph_edge *e;
2814 for (e = node->callees; e; e = e->next_callee)
2816 struct inline_edge_summary *es = inline_edge_summary (e);
2817 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2819 if (e->inline_failed)
2821 /* Predicates of calls shall not use NOT_CHANGED codes,
2822 sowe do not need to compute probabilities. */
2823 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
2824 known_vals, known_binfos, known_aggs,
2825 hints);
2827 else
2828 estimate_calls_size_and_time (e->callee, size, time, hints,
2829 possible_truths,
2830 known_vals, known_binfos, known_aggs);
2833 for (e = node->indirect_calls; e; e = e->next_callee)
2835 struct inline_edge_summary *es = inline_edge_summary (e);
2836 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2837 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
2838 known_vals, known_binfos, known_aggs,
2839 hints);
2844 /* Estimate size and time needed to execute NODE assuming
2845 POSSIBLE_TRUTHS clause, and KNOWN_VALS and KNOWN_BINFOS information
2846 about NODE's arguments. */
2848 static void
2849 estimate_node_size_and_time (struct cgraph_node *node,
2850 clause_t possible_truths,
2851 vec<tree> known_vals,
2852 vec<tree> known_binfos,
2853 vec<ipa_agg_jump_function_p> known_aggs,
2854 int *ret_size, int *ret_time,
2855 inline_hints *ret_hints,
2856 vec<inline_param_summary_t>
2857 inline_param_summary)
2859 struct inline_summary *info = inline_summary (node);
2860 size_time_entry *e;
2861 int size = 0;
2862 int time = 0;
2863 inline_hints hints = 0;
2864 int i;
2866 if (dump_file
2867 && (dump_flags & TDF_DETAILS))
2869 bool found = false;
2870 fprintf (dump_file, " Estimating body: %s/%i\n"
2871 " Known to be false: ",
2872 cgraph_node_name (node),
2873 node->uid);
2875 for (i = predicate_not_inlined_condition;
2876 i < (predicate_first_dynamic_condition
2877 + (int)vec_safe_length (info->conds)); i++)
2878 if (!(possible_truths & (1 << i)))
2880 if (found)
2881 fprintf (dump_file, ", ");
2882 found = true;
2883 dump_condition (dump_file, info->conds, i);
2887 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
2888 if (evaluate_predicate (&e->predicate, possible_truths))
2890 size += e->size;
2891 gcc_checking_assert (e->time >= 0);
2892 gcc_checking_assert (time >= 0);
2893 if (!inline_param_summary.exists ())
2894 time += e->time;
2895 else
2897 int prob = predicate_probability (info->conds,
2898 &e->predicate,
2899 possible_truths,
2900 inline_param_summary);
2901 gcc_checking_assert (prob >= 0);
2902 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
2903 time += ((gcov_type)e->time * prob) / REG_BR_PROB_BASE;
2905 if (time > MAX_TIME * INLINE_TIME_SCALE)
2906 time = MAX_TIME * INLINE_TIME_SCALE;
2907 gcc_checking_assert (time >= 0);
2910 gcc_checking_assert (size >= 0);
2911 gcc_checking_assert (time >= 0);
2913 if (info->loop_iterations
2914 && !evaluate_predicate (info->loop_iterations, possible_truths))
2915 hints |=INLINE_HINT_loop_iterations;
2916 if (info->loop_stride
2917 && !evaluate_predicate (info->loop_stride, possible_truths))
2918 hints |=INLINE_HINT_loop_stride;
2919 if (info->array_index
2920 && !evaluate_predicate (info->array_index, possible_truths))
2921 hints |=INLINE_HINT_array_index;
2922 if (info->scc_no)
2923 hints |= INLINE_HINT_in_scc;
2924 if (DECL_DECLARED_INLINE_P (node->symbol.decl))
2925 hints |= INLINE_HINT_declared_inline;
2927 estimate_calls_size_and_time (node, &size, &time, &hints, possible_truths,
2928 known_vals, known_binfos, known_aggs);
2929 gcc_checking_assert (size >= 0);
2930 gcc_checking_assert (time >= 0);
2931 time = RDIV (time, INLINE_TIME_SCALE);
2932 size = RDIV (size, INLINE_SIZE_SCALE);
2934 if (dump_file
2935 && (dump_flags & TDF_DETAILS))
2936 fprintf (dump_file, "\n size:%i time:%i\n", (int)size, (int)time);
2937 if (ret_time)
2938 *ret_time = time;
2939 if (ret_size)
2940 *ret_size = size;
2941 if (ret_hints)
2942 *ret_hints = hints;
2943 return;
2947 /* Estimate size and time needed to execute callee of EDGE assuming that
2948 parameters known to be constant at caller of EDGE are propagated.
2949 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
2950 and types for parameters. */
2952 void
2953 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
2954 vec<tree> known_vals,
2955 vec<tree> known_binfos,
2956 vec<ipa_agg_jump_function_p> known_aggs,
2957 int *ret_size, int *ret_time,
2958 inline_hints *hints)
2960 clause_t clause;
2962 clause = evaluate_conditions_for_known_args (node, false, known_vals,
2963 known_aggs);
2964 estimate_node_size_and_time (node, clause, known_vals, known_binfos,
2965 known_aggs, ret_size, ret_time, hints,
2966 vec<inline_param_summary_t>());
2969 /* Translate all conditions from callee representation into caller
2970 representation and symbolically evaluate predicate P into new predicate.
2972 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
2973 is summary of function predicate P is from. OPERAND_MAP is array giving
2974 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
2975 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
2976 predicate under which callee is executed. OFFSET_MAP is an array of of
2977 offsets that need to be added to conditions, negative offset means that
2978 conditions relying on values passed by reference have to be discarded
2979 because they might not be preserved (and should be considered offset zero
2980 for other purposes). */
2982 static struct predicate
2983 remap_predicate (struct inline_summary *info,
2984 struct inline_summary *callee_info,
2985 struct predicate *p,
2986 vec<int> operand_map,
2987 vec<int> offset_map,
2988 clause_t possible_truths,
2989 struct predicate *toplev_predicate)
2991 int i;
2992 struct predicate out = true_predicate ();
2994 /* True predicate is easy. */
2995 if (true_predicate_p (p))
2996 return *toplev_predicate;
2997 for (i = 0; p->clause[i]; i++)
2999 clause_t clause = p->clause[i];
3000 int cond;
3001 struct predicate clause_predicate = false_predicate ();
3003 gcc_assert (i < MAX_CLAUSES);
3005 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
3006 /* Do we have condition we can't disprove? */
3007 if (clause & possible_truths & (1 << cond))
3009 struct predicate cond_predicate;
3010 /* Work out if the condition can translate to predicate in the
3011 inlined function. */
3012 if (cond >= predicate_first_dynamic_condition)
3014 struct condition *c;
3016 c = &(*callee_info->conds)[cond
3017 - predicate_first_dynamic_condition];
3018 /* See if we can remap condition operand to caller's operand.
3019 Otherwise give up. */
3020 if (!operand_map.exists ()
3021 || (int)operand_map.length () <= c->operand_num
3022 || operand_map[c->operand_num] == -1
3023 /* TODO: For non-aggregate conditions, adding an offset is
3024 basically an arithmetic jump function processing which
3025 we should support in future. */
3026 || ((!c->agg_contents || !c->by_ref)
3027 && offset_map[c->operand_num] > 0)
3028 || (c->agg_contents && c->by_ref
3029 && offset_map[c->operand_num] < 0))
3030 cond_predicate = true_predicate ();
3031 else
3033 struct agg_position_info ap;
3034 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3035 if (offset_delta < 0)
3037 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3038 offset_delta = 0;
3040 gcc_assert (!c->agg_contents
3041 || c->by_ref
3042 || offset_delta == 0);
3043 ap.offset = c->offset + offset_delta;
3044 ap.agg_contents = c->agg_contents;
3045 ap.by_ref = c->by_ref;
3046 cond_predicate = add_condition (info,
3047 operand_map[c->operand_num],
3048 &ap, c->code, c->val);
3051 /* Fixed conditions remains same, construct single
3052 condition predicate. */
3053 else
3055 cond_predicate.clause[0] = 1 << cond;
3056 cond_predicate.clause[1] = 0;
3058 clause_predicate = or_predicates (info->conds, &clause_predicate,
3059 &cond_predicate);
3061 out = and_predicates (info->conds, &out, &clause_predicate);
3063 return and_predicates (info->conds, &out, toplev_predicate);
3067 /* Update summary information of inline clones after inlining.
3068 Compute peak stack usage. */
3070 static void
3071 inline_update_callee_summaries (struct cgraph_node *node,
3072 int depth)
3074 struct cgraph_edge *e;
3075 struct inline_summary *callee_info = inline_summary (node);
3076 struct inline_summary *caller_info = inline_summary (node->callers->caller);
3077 HOST_WIDE_INT peak;
3079 callee_info->stack_frame_offset
3080 = caller_info->stack_frame_offset
3081 + caller_info->estimated_self_stack_size;
3082 peak = callee_info->stack_frame_offset
3083 + callee_info->estimated_self_stack_size;
3084 if (inline_summary (node->global.inlined_to)->estimated_stack_size
3085 < peak)
3086 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
3087 cgraph_propagate_frequency (node);
3088 for (e = node->callees; e; e = e->next_callee)
3090 if (!e->inline_failed)
3091 inline_update_callee_summaries (e->callee, depth);
3092 inline_edge_summary (e)->loop_depth += depth;
3094 for (e = node->indirect_calls; e; e = e->next_callee)
3095 inline_edge_summary (e)->loop_depth += depth;
3098 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3099 When functoin A is inlined in B and A calls C with parameter that
3100 changes with probability PROB1 and C is known to be passthroug
3101 of argument if B that change with probability PROB2, the probability
3102 of change is now PROB1*PROB2. */
3104 static void
3105 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3106 struct cgraph_edge *edge)
3108 if (ipa_node_params_vector.exists ())
3110 int i;
3111 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3112 struct inline_edge_summary *es = inline_edge_summary (edge);
3113 struct inline_edge_summary *inlined_es
3114 = inline_edge_summary (inlined_edge);
3116 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3118 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3119 if (jfunc->type == IPA_JF_PASS_THROUGH
3120 && (ipa_get_jf_pass_through_formal_id (jfunc)
3121 < (int) inlined_es->param.length ()))
3123 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3124 int prob1 = es->param[i].change_prob;
3125 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3126 int prob = ((prob1 * prob2 + REG_BR_PROB_BASE / 2)
3127 / REG_BR_PROB_BASE);
3129 if (prob1 && prob2 && !prob)
3130 prob = 1;
3132 es->param[i].change_prob = prob;
3138 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3140 Remap predicates of callees of NODE. Rest of arguments match
3141 remap_predicate.
3143 Also update change probabilities. */
3145 static void
3146 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3147 struct cgraph_node *node,
3148 struct inline_summary *info,
3149 struct inline_summary *callee_info,
3150 vec<int> operand_map,
3151 vec<int> offset_map,
3152 clause_t possible_truths,
3153 struct predicate *toplev_predicate)
3155 struct cgraph_edge *e;
3156 for (e = node->callees; e; e = e->next_callee)
3158 struct inline_edge_summary *es = inline_edge_summary (e);
3159 struct predicate p;
3161 if (e->inline_failed)
3163 remap_edge_change_prob (inlined_edge, e);
3165 if (es->predicate)
3167 p = remap_predicate (info, callee_info,
3168 es->predicate, operand_map, offset_map,
3169 possible_truths,
3170 toplev_predicate);
3171 edge_set_predicate (e, &p);
3172 /* TODO: We should remove the edge for code that will be
3173 optimized out, but we need to keep verifiers and tree-inline
3174 happy. Make it cold for now. */
3175 if (false_predicate_p (&p))
3177 e->count = 0;
3178 e->frequency = 0;
3181 else
3182 edge_set_predicate (e, toplev_predicate);
3184 else
3185 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3186 operand_map, offset_map, possible_truths,
3187 toplev_predicate);
3189 for (e = node->indirect_calls; e; e = e->next_callee)
3191 struct inline_edge_summary *es = inline_edge_summary (e);
3192 struct predicate p;
3194 remap_edge_change_prob (inlined_edge, e);
3195 if (es->predicate)
3197 p = remap_predicate (info, callee_info,
3198 es->predicate, operand_map, offset_map,
3199 possible_truths, toplev_predicate);
3200 edge_set_predicate (e, &p);
3201 /* TODO: We should remove the edge for code that will be optimized
3202 out, but we need to keep verifiers and tree-inline happy.
3203 Make it cold for now. */
3204 if (false_predicate_p (&p))
3206 e->count = 0;
3207 e->frequency = 0;
3210 else
3211 edge_set_predicate (e, toplev_predicate);
3215 /* Same as remap_predicate, but set result into hint *HINT. */
3217 static void
3218 remap_hint_predicate (struct inline_summary *info,
3219 struct inline_summary *callee_info,
3220 struct predicate **hint,
3221 vec<int> operand_map,
3222 vec<int> offset_map,
3223 clause_t possible_truths,
3224 struct predicate *toplev_predicate)
3226 predicate p;
3228 if (!*hint)
3229 return;
3230 p = remap_predicate (info, callee_info,
3231 *hint,
3232 operand_map, offset_map,
3233 possible_truths,
3234 toplev_predicate);
3235 if (!false_predicate_p (&p)
3236 && !true_predicate_p (&p))
3238 if (!*hint)
3239 set_hint_predicate (hint, p);
3240 else
3241 **hint = and_predicates (info->conds,
3242 *hint,
3243 &p);
3247 /* We inlined EDGE. Update summary of the function we inlined into. */
3249 void
3250 inline_merge_summary (struct cgraph_edge *edge)
3252 struct inline_summary *callee_info = inline_summary (edge->callee);
3253 struct cgraph_node *to = (edge->caller->global.inlined_to
3254 ? edge->caller->global.inlined_to : edge->caller);
3255 struct inline_summary *info = inline_summary (to);
3256 clause_t clause = 0; /* not_inline is known to be false. */
3257 size_time_entry *e;
3258 vec<int> operand_map = vec<int>();
3259 vec<int> offset_map = vec<int>();
3260 int i;
3261 struct predicate toplev_predicate;
3262 struct predicate true_p = true_predicate ();
3263 struct inline_edge_summary *es = inline_edge_summary (edge);
3265 if (es->predicate)
3266 toplev_predicate = *es->predicate;
3267 else
3268 toplev_predicate = true_predicate ();
3270 if (ipa_node_params_vector.exists () && callee_info->conds)
3272 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3273 int count = ipa_get_cs_argument_count (args);
3274 int i;
3276 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3277 if (count)
3279 operand_map.safe_grow_cleared (count);
3280 offset_map.safe_grow_cleared (count);
3282 for (i = 0; i < count; i++)
3284 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3285 int map = -1;
3287 /* TODO: handle non-NOPs when merging. */
3288 if (jfunc->type == IPA_JF_PASS_THROUGH)
3290 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3291 map = ipa_get_jf_pass_through_formal_id (jfunc);
3292 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3293 offset_map[i] = -1;
3295 else if (jfunc->type == IPA_JF_ANCESTOR)
3297 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3298 if (offset >= 0 && offset < INT_MAX)
3300 map = ipa_get_jf_ancestor_formal_id (jfunc);
3301 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3302 offset = -1;
3303 offset_map[i] = offset;
3306 operand_map[i] = map;
3307 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3310 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3312 struct predicate p = remap_predicate (info, callee_info,
3313 &e->predicate, operand_map,
3314 offset_map, clause,
3315 &toplev_predicate);
3316 if (!false_predicate_p (&p))
3318 gcov_type add_time = ((gcov_type)e->time * edge->frequency
3319 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3320 int prob = predicate_probability (callee_info->conds,
3321 &e->predicate,
3322 clause, es->param);
3323 add_time = ((gcov_type)add_time * prob) / REG_BR_PROB_BASE;
3324 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3325 add_time = MAX_TIME * INLINE_TIME_SCALE;
3326 if (prob != REG_BR_PROB_BASE
3327 && dump_file && (dump_flags & TDF_DETAILS))
3329 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3330 (double)prob / REG_BR_PROB_BASE);
3332 account_size_time (info, e->size, add_time, &p);
3335 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3336 offset_map, clause, &toplev_predicate);
3337 remap_hint_predicate (info, callee_info,
3338 &callee_info->loop_iterations,
3339 operand_map, offset_map,
3340 clause, &toplev_predicate);
3341 remap_hint_predicate (info, callee_info,
3342 &callee_info->loop_stride,
3343 operand_map, offset_map,
3344 clause, &toplev_predicate);
3345 remap_hint_predicate (info, callee_info,
3346 &callee_info->array_index,
3347 operand_map, offset_map,
3348 clause, &toplev_predicate);
3350 inline_update_callee_summaries (edge->callee,
3351 inline_edge_summary (edge)->loop_depth);
3353 /* We do not maintain predicates of inlined edges, free it. */
3354 edge_set_predicate (edge, &true_p);
3355 /* Similarly remove param summaries. */
3356 es->param.release ();
3357 operand_map.release ();
3358 offset_map.release ();
3361 /* For performance reasons inline_merge_summary is not updating overall size
3362 and time. Recompute it. */
3364 void
3365 inline_update_overall_summary (struct cgraph_node *node)
3367 struct inline_summary *info = inline_summary (node);
3368 size_time_entry *e;
3369 int i;
3371 info->size = 0;
3372 info->time = 0;
3373 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3375 info->size += e->size, info->time += e->time;
3376 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3377 info->time = MAX_TIME * INLINE_TIME_SCALE;
3379 estimate_calls_size_and_time (node, &info->size, &info->time, NULL,
3380 ~(clause_t)(1 << predicate_false_condition),
3381 vec<tree>(),
3382 vec<tree>(),
3383 vec<ipa_agg_jump_function_p>());
3384 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3385 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3388 /* Return hints derrived from EDGE. */
3390 simple_edge_hints (struct cgraph_edge *edge)
3392 int hints = 0;
3393 struct cgraph_node *to = (edge->caller->global.inlined_to
3394 ? edge->caller->global.inlined_to
3395 : edge->caller);
3396 if (inline_summary (to)->scc_no
3397 && inline_summary (to)->scc_no == inline_summary (edge->callee)->scc_no
3398 && !cgraph_edge_recursive_p (edge))
3399 hints |= INLINE_HINT_same_scc;
3401 if (to->symbol.lto_file_data && edge->callee->symbol.lto_file_data
3402 && to->symbol.lto_file_data != edge->callee->symbol.lto_file_data)
3403 hints |= INLINE_HINT_cross_module;
3405 return hints;
3408 /* Estimate the time cost for the caller when inlining EDGE.
3409 Only to be called via estimate_edge_time, that handles the
3410 caching mechanism.
3412 When caching, also update the cache entry. Compute both time and
3413 size, since we always need both metrics eventually. */
3416 do_estimate_edge_time (struct cgraph_edge *edge)
3418 int time;
3419 int size;
3420 inline_hints hints;
3421 struct cgraph_node *callee;
3422 clause_t clause;
3423 vec<tree> known_vals;
3424 vec<tree> known_binfos;
3425 vec<ipa_agg_jump_function_p> known_aggs;
3426 struct inline_edge_summary *es = inline_edge_summary (edge);
3428 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3430 gcc_checking_assert (edge->inline_failed);
3431 evaluate_properties_for_edge (edge, true,
3432 &clause, &known_vals, &known_binfos,
3433 &known_aggs);
3434 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3435 known_aggs, &size, &time, &hints, es->param);
3436 known_vals.release ();
3437 known_binfos.release ();
3438 known_aggs.release ();
3439 gcc_checking_assert (size >= 0);
3440 gcc_checking_assert (time >= 0);
3442 /* When caching, update the cache entry. */
3443 if (edge_growth_cache.exists ())
3445 if ((int)edge_growth_cache.length () <= edge->uid)
3446 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
3447 edge_growth_cache[edge->uid].time = time + (time >= 0);
3449 edge_growth_cache[edge->uid].size = size + (size >= 0);
3450 hints |= simple_edge_hints (edge);
3451 edge_growth_cache[edge->uid].hints = hints + 1;
3453 return time;
3457 /* Return estimated callee growth after inlining EDGE.
3458 Only to be called via estimate_edge_size. */
3461 do_estimate_edge_size (struct cgraph_edge *edge)
3463 int size;
3464 struct cgraph_node *callee;
3465 clause_t clause;
3466 vec<tree> known_vals;
3467 vec<tree> known_binfos;
3468 vec<ipa_agg_jump_function_p> known_aggs;
3470 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3472 if (edge_growth_cache.exists ())
3474 do_estimate_edge_time (edge);
3475 size = edge_growth_cache[edge->uid].size;
3476 gcc_checking_assert (size);
3477 return size - (size > 0);
3480 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3482 /* Early inliner runs without caching, go ahead and do the dirty work. */
3483 gcc_checking_assert (edge->inline_failed);
3484 evaluate_properties_for_edge (edge, true,
3485 &clause, &known_vals, &known_binfos,
3486 &known_aggs);
3487 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3488 known_aggs, &size, NULL, NULL,
3489 vec<inline_param_summary_t>());
3490 known_vals.release ();
3491 known_binfos.release ();
3492 known_aggs.release ();
3493 return size;
3497 /* Estimate the growth of the caller when inlining EDGE.
3498 Only to be called via estimate_edge_size. */
3500 inline_hints
3501 do_estimate_edge_hints (struct cgraph_edge *edge)
3503 inline_hints hints;
3504 struct cgraph_node *callee;
3505 clause_t clause;
3506 vec<tree> known_vals;
3507 vec<tree> known_binfos;
3508 vec<ipa_agg_jump_function_p> known_aggs;
3510 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3512 if (edge_growth_cache.exists ())
3514 do_estimate_edge_time (edge);
3515 hints = edge_growth_cache[edge->uid].hints;
3516 gcc_checking_assert (hints);
3517 return hints - 1;
3520 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3522 /* Early inliner runs without caching, go ahead and do the dirty work. */
3523 gcc_checking_assert (edge->inline_failed);
3524 evaluate_properties_for_edge (edge, true,
3525 &clause, &known_vals, &known_binfos,
3526 &known_aggs);
3527 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3528 known_aggs, NULL, NULL, &hints,
3529 vec<inline_param_summary_t>());
3530 known_vals.release ();
3531 known_binfos.release ();
3532 known_aggs.release ();
3533 hints |= simple_edge_hints (edge);
3534 return hints;
3538 /* Estimate self time of the function NODE after inlining EDGE. */
3541 estimate_time_after_inlining (struct cgraph_node *node,
3542 struct cgraph_edge *edge)
3544 struct inline_edge_summary *es = inline_edge_summary (edge);
3545 if (!es->predicate || !false_predicate_p (es->predicate))
3547 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
3548 if (time < 0)
3549 time = 0;
3550 if (time > MAX_TIME)
3551 time = MAX_TIME;
3552 return time;
3554 return inline_summary (node)->time;
3558 /* Estimate the size of NODE after inlining EDGE which should be an
3559 edge to either NODE or a call inlined into NODE. */
3562 estimate_size_after_inlining (struct cgraph_node *node,
3563 struct cgraph_edge *edge)
3565 struct inline_edge_summary *es = inline_edge_summary (edge);
3566 if (!es->predicate || !false_predicate_p (es->predicate))
3568 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3569 gcc_assert (size >= 0);
3570 return size;
3572 return inline_summary (node)->size;
3576 struct growth_data
3578 bool self_recursive;
3579 int growth;
3583 /* Worker for do_estimate_growth. Collect growth for all callers. */
3585 static bool
3586 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3588 struct cgraph_edge *e;
3589 struct growth_data *d = (struct growth_data *) data;
3591 for (e = node->callers; e; e = e->next_caller)
3593 gcc_checking_assert (e->inline_failed);
3595 if (e->caller == node
3596 || (e->caller->global.inlined_to
3597 && e->caller->global.inlined_to == node))
3598 d->self_recursive = true;
3599 d->growth += estimate_edge_growth (e);
3601 return false;
3605 /* Estimate the growth caused by inlining NODE into all callees. */
3608 do_estimate_growth (struct cgraph_node *node)
3610 struct growth_data d = {0, false};
3611 struct inline_summary *info = inline_summary (node);
3613 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3615 /* For self recursive functions the growth estimation really should be
3616 infinity. We don't want to return very large values because the growth
3617 plays various roles in badness computation fractions. Be sure to not
3618 return zero or negative growths. */
3619 if (d.self_recursive)
3620 d.growth = d.growth < info->size ? info->size : d.growth;
3621 else if (DECL_EXTERNAL (node->symbol.decl))
3623 else
3625 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3626 d.growth -= info->size;
3627 /* COMDAT functions are very often not shared across multiple units
3628 since they come from various template instantiations.
3629 Take this into account. */
3630 else if (DECL_COMDAT (node->symbol.decl)
3631 && cgraph_can_remove_if_no_direct_calls_p (node))
3632 d.growth -= (info->size
3633 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3634 + 50) / 100;
3637 if (node_growth_cache.exists ())
3639 if ((int)node_growth_cache.length () <= node->uid)
3640 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
3641 node_growth_cache[node->uid] = d.growth + (d.growth >= 0);
3643 return d.growth;
3647 /* This function performs intraprocedural analysis in NODE that is required to
3648 inline indirect calls. */
3650 static void
3651 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3653 ipa_analyze_node (node);
3654 if (dump_file && (dump_flags & TDF_DETAILS))
3656 ipa_print_node_params (dump_file, node);
3657 ipa_print_node_jump_functions (dump_file, node);
3662 /* Note function body size. */
3664 static void
3665 inline_analyze_function (struct cgraph_node *node)
3667 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
3669 if (dump_file)
3670 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3671 cgraph_node_name (node), node->uid);
3672 if (optimize && !node->thunk.thunk_p)
3673 inline_indirect_intraprocedural_analysis (node);
3674 compute_inline_parameters (node, false);
3676 pop_cfun ();
3680 /* Called when new function is inserted to callgraph late. */
3682 static void
3683 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3685 inline_analyze_function (node);
3689 /* Note function body size. */
3691 void
3692 inline_generate_summary (void)
3694 struct cgraph_node *node;
3696 function_insertion_hook_holder =
3697 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3699 ipa_register_cgraph_hooks ();
3700 inline_free_summary ();
3702 FOR_EACH_DEFINED_FUNCTION (node)
3703 if (!node->alias)
3704 inline_analyze_function (node);
3708 /* Read predicate from IB. */
3710 static struct predicate
3711 read_predicate (struct lto_input_block *ib)
3713 struct predicate out;
3714 clause_t clause;
3715 int k = 0;
3719 gcc_assert (k <= MAX_CLAUSES);
3720 clause = out.clause[k++] = streamer_read_uhwi (ib);
3722 while (clause);
3724 /* Zero-initialize the remaining clauses in OUT. */
3725 while (k <= MAX_CLAUSES)
3726 out.clause[k++] = 0;
3728 return out;
3732 /* Write inline summary for edge E to OB. */
3734 static void
3735 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
3737 struct inline_edge_summary *es = inline_edge_summary (e);
3738 struct predicate p;
3739 int length, i;
3741 es->call_stmt_size = streamer_read_uhwi (ib);
3742 es->call_stmt_time = streamer_read_uhwi (ib);
3743 es->loop_depth = streamer_read_uhwi (ib);
3744 p = read_predicate (ib);
3745 edge_set_predicate (e, &p);
3746 length = streamer_read_uhwi (ib);
3747 if (length)
3749 es->param.safe_grow_cleared (length);
3750 for (i = 0; i < length; i++)
3751 es->param[i].change_prob
3752 = streamer_read_uhwi (ib);
3757 /* Stream in inline summaries from the section. */
3759 static void
3760 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3761 size_t len)
3763 const struct lto_function_header *header =
3764 (const struct lto_function_header *) data;
3765 const int cfg_offset = sizeof (struct lto_function_header);
3766 const int main_offset = cfg_offset + header->cfg_size;
3767 const int string_offset = main_offset + header->main_size;
3768 struct data_in *data_in;
3769 struct lto_input_block ib;
3770 unsigned int i, count2, j;
3771 unsigned int f_count;
3773 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
3774 header->main_size);
3776 data_in =
3777 lto_data_in_create (file_data, (const char *) data + string_offset,
3778 header->string_size,
3779 vec<ld_plugin_symbol_resolution_t>());
3780 f_count = streamer_read_uhwi (&ib);
3781 for (i = 0; i < f_count; i++)
3783 unsigned int index;
3784 struct cgraph_node *node;
3785 struct inline_summary *info;
3786 lto_symtab_encoder_t encoder;
3787 struct bitpack_d bp;
3788 struct cgraph_edge *e;
3789 predicate p;
3791 index = streamer_read_uhwi (&ib);
3792 encoder = file_data->symtab_node_encoder;
3793 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3794 info = inline_summary (node);
3796 info->estimated_stack_size
3797 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
3798 info->size = info->self_size = streamer_read_uhwi (&ib);
3799 info->time = info->self_time = streamer_read_uhwi (&ib);
3801 bp = streamer_read_bitpack (&ib);
3802 info->inlinable = bp_unpack_value (&bp, 1);
3804 count2 = streamer_read_uhwi (&ib);
3805 gcc_assert (!info->conds);
3806 for (j = 0; j < count2; j++)
3808 struct condition c;
3809 c.operand_num = streamer_read_uhwi (&ib);
3810 c.code = (enum tree_code) streamer_read_uhwi (&ib);
3811 c.val = stream_read_tree (&ib, data_in);
3812 bp = streamer_read_bitpack (&ib);
3813 c.agg_contents = bp_unpack_value (&bp, 1);
3814 c.by_ref = bp_unpack_value (&bp, 1);
3815 if (c.agg_contents)
3816 c.offset = streamer_read_uhwi (&ib);
3817 vec_safe_push (info->conds, c);
3819 count2 = streamer_read_uhwi (&ib);
3820 gcc_assert (!info->entry);
3821 for (j = 0; j < count2; j++)
3823 struct size_time_entry e;
3825 e.size = streamer_read_uhwi (&ib);
3826 e.time = streamer_read_uhwi (&ib);
3827 e.predicate = read_predicate (&ib);
3829 vec_safe_push (info->entry, e);
3832 p = read_predicate (&ib);
3833 set_hint_predicate (&info->loop_iterations, p);
3834 p = read_predicate (&ib);
3835 set_hint_predicate (&info->loop_stride, p);
3836 p = read_predicate (&ib);
3837 set_hint_predicate (&info->array_index, p);
3838 for (e = node->callees; e; e = e->next_callee)
3839 read_inline_edge_summary (&ib, e);
3840 for (e = node->indirect_calls; e; e = e->next_callee)
3841 read_inline_edge_summary (&ib, e);
3844 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
3845 len);
3846 lto_data_in_delete (data_in);
3850 /* Read inline summary. Jump functions are shared among ipa-cp
3851 and inliner, so when ipa-cp is active, we don't need to write them
3852 twice. */
3854 void
3855 inline_read_summary (void)
3857 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3858 struct lto_file_decl_data *file_data;
3859 unsigned int j = 0;
3861 inline_summary_alloc ();
3863 while ((file_data = file_data_vec[j++]))
3865 size_t len;
3866 const char *data = lto_get_section_data (file_data,
3867 LTO_section_inline_summary,
3868 NULL, &len);
3869 if (data)
3870 inline_read_section (file_data, data, len);
3871 else
3872 /* Fatal error here. We do not want to support compiling ltrans units
3873 with different version of compiler or different flags than the WPA
3874 unit, so this should never happen. */
3875 fatal_error ("ipa inline summary is missing in input file");
3877 if (optimize)
3879 ipa_register_cgraph_hooks ();
3880 if (!flag_ipa_cp)
3881 ipa_prop_read_jump_functions ();
3883 function_insertion_hook_holder =
3884 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3888 /* Write predicate P to OB. */
3890 static void
3891 write_predicate (struct output_block *ob, struct predicate *p)
3893 int j;
3894 if (p)
3895 for (j = 0; p->clause[j]; j++)
3897 gcc_assert (j < MAX_CLAUSES);
3898 streamer_write_uhwi (ob, p->clause[j]);
3900 streamer_write_uhwi (ob, 0);
3904 /* Write inline summary for edge E to OB. */
3906 static void
3907 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
3909 struct inline_edge_summary *es = inline_edge_summary (e);
3910 int i;
3912 streamer_write_uhwi (ob, es->call_stmt_size);
3913 streamer_write_uhwi (ob, es->call_stmt_time);
3914 streamer_write_uhwi (ob, es->loop_depth);
3915 write_predicate (ob, es->predicate);
3916 streamer_write_uhwi (ob, es->param.length ());
3917 for (i = 0; i < (int)es->param.length (); i++)
3918 streamer_write_uhwi (ob, es->param[i].change_prob);
3922 /* Write inline summary for node in SET.
3923 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
3924 active, we don't need to write them twice. */
3926 void
3927 inline_write_summary (void)
3929 struct cgraph_node *node;
3930 struct output_block *ob = create_output_block (LTO_section_inline_summary);
3931 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3932 unsigned int count = 0;
3933 int i;
3935 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3937 symtab_node snode = lto_symtab_encoder_deref (encoder, i);
3938 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
3939 if (cnode && cnode->analyzed)
3940 count++;
3942 streamer_write_uhwi (ob, count);
3944 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3946 symtab_node snode = lto_symtab_encoder_deref (encoder, i);
3947 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
3948 if (cnode && (node = cnode)->analyzed)
3950 struct inline_summary *info = inline_summary (node);
3951 struct bitpack_d bp;
3952 struct cgraph_edge *edge;
3953 int i;
3954 size_time_entry *e;
3955 struct condition *c;
3957 streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, (symtab_node)node));
3958 streamer_write_hwi (ob, info->estimated_self_stack_size);
3959 streamer_write_hwi (ob, info->self_size);
3960 streamer_write_hwi (ob, info->self_time);
3961 bp = bitpack_create (ob->main_stream);
3962 bp_pack_value (&bp, info->inlinable, 1);
3963 streamer_write_bitpack (&bp);
3964 streamer_write_uhwi (ob, vec_safe_length (info->conds));
3965 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
3967 streamer_write_uhwi (ob, c->operand_num);
3968 streamer_write_uhwi (ob, c->code);
3969 stream_write_tree (ob, c->val, true);
3970 bp = bitpack_create (ob->main_stream);
3971 bp_pack_value (&bp, c->agg_contents, 1);
3972 bp_pack_value (&bp, c->by_ref, 1);
3973 streamer_write_bitpack (&bp);
3974 if (c->agg_contents)
3975 streamer_write_uhwi (ob, c->offset);
3977 streamer_write_uhwi (ob, vec_safe_length (info->entry));
3978 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3980 streamer_write_uhwi (ob, e->size);
3981 streamer_write_uhwi (ob, e->time);
3982 write_predicate (ob, &e->predicate);
3984 write_predicate (ob, info->loop_iterations);
3985 write_predicate (ob, info->loop_stride);
3986 write_predicate (ob, info->array_index);
3987 for (edge = node->callees; edge; edge = edge->next_callee)
3988 write_inline_edge_summary (ob, edge);
3989 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
3990 write_inline_edge_summary (ob, edge);
3993 streamer_write_char_stream (ob->main_stream, 0);
3994 produce_asm (ob, NULL);
3995 destroy_output_block (ob);
3997 if (optimize && !flag_ipa_cp)
3998 ipa_prop_write_jump_functions ();
4002 /* Release inline summary. */
4004 void
4005 inline_free_summary (void)
4007 struct cgraph_node *node;
4008 if (!inline_edge_summary_vec.exists ())
4009 return;
4010 FOR_EACH_DEFINED_FUNCTION (node)
4011 reset_inline_summary (node);
4012 if (function_insertion_hook_holder)
4013 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
4014 function_insertion_hook_holder = NULL;
4015 if (node_removal_hook_holder)
4016 cgraph_remove_node_removal_hook (node_removal_hook_holder);
4017 node_removal_hook_holder = NULL;
4018 if (edge_removal_hook_holder)
4019 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
4020 edge_removal_hook_holder = NULL;
4021 if (node_duplication_hook_holder)
4022 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
4023 node_duplication_hook_holder = NULL;
4024 if (edge_duplication_hook_holder)
4025 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
4026 edge_duplication_hook_holder = NULL;
4027 vec_free (inline_summary_vec);
4028 inline_edge_summary_vec.release ();
4029 if (edge_predicate_pool)
4030 free_alloc_pool (edge_predicate_pool);
4031 edge_predicate_pool = 0;