2014-03-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob98f42ef1e55e7ea94c817491d9bdaed55fac75ce
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Analysis used by the inliner and other passes limiting code size growth.
23 We estimate for each function
24 - function body size
25 - average function execution time
26 - inlining size benefit (that is how much of function body size
27 and its call sequence is expected to disappear by inlining)
28 - inlining time benefit
29 - function frame size
30 For each call
31 - call statement size and time
33 inlinie_summary datastructures store above information locally (i.e.
34 parameters of the function itself) and globally (i.e. parameters of
35 the function created by applying all the inline decisions already
36 present in the callgraph).
38 We provide accestor to the inline_summary datastructure and
39 basic logic updating the parameters when inlining is performed.
41 The summaries are context sensitive. Context means
42 1) partial assignment of known constant values of operands
43 2) whether function is inlined into the call or not.
44 It is easy to add more variants. To represent function size and time
45 that depends on context (i.e. it is known to be optimized away when
46 context is known either by inlining or from IP-CP and clonning),
47 we use predicates. Predicates are logical formulas in
48 conjunctive-disjunctive form consisting of clauses. Clauses are bitmaps
49 specifying what conditions must be true. Conditions are simple test
50 of the form described above.
52 In order to make predicate (possibly) true, all of its clauses must
53 be (possibly) true. To make clause (possibly) true, one of conditions
54 it mentions must be (possibly) true. There are fixed bounds on
55 number of clauses and conditions and all the manipulation functions
56 are conservative in positive direction. I.e. we may lose precision
57 by thinking that predicate may be true even when it is not.
59 estimate_edge_size and estimate_edge_growth can be used to query
60 function size/time in the given context. inline_merge_summary merges
61 properties of caller and callee after inlining.
63 Finally pass_inline_parameters is exported. This is used to drive
64 computation of function parameters used by the early inliner. IPA
65 inlined performs analysis via its analyze_function method. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "tm.h"
71 #include "tree.h"
72 #include "stor-layout.h"
73 #include "stringpool.h"
74 #include "print-tree.h"
75 #include "tree-inline.h"
76 #include "langhooks.h"
77 #include "flags.h"
78 #include "diagnostic.h"
79 #include "gimple-pretty-print.h"
80 #include "params.h"
81 #include "tree-pass.h"
82 #include "coverage.h"
83 #include "basic-block.h"
84 #include "tree-ssa-alias.h"
85 #include "internal-fn.h"
86 #include "gimple-expr.h"
87 #include "is-a.h"
88 #include "gimple.h"
89 #include "gimple-iterator.h"
90 #include "gimple-ssa.h"
91 #include "tree-cfg.h"
92 #include "tree-phinodes.h"
93 #include "ssa-iterators.h"
94 #include "tree-ssanames.h"
95 #include "tree-ssa-loop-niter.h"
96 #include "tree-ssa-loop.h"
97 #include "ipa-prop.h"
98 #include "lto-streamer.h"
99 #include "data-streamer.h"
100 #include "tree-streamer.h"
101 #include "ipa-inline.h"
102 #include "alloc-pool.h"
103 #include "cfgloop.h"
104 #include "tree-scalar-evolution.h"
105 #include "ipa-utils.h"
106 #include "cilk.h"
107 #include "cfgexpand.h"
109 /* Estimate runtime of function can easilly run into huge numbers with many
110 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
111 integer. For anything larger we use gcov_type. */
112 #define MAX_TIME 500000
114 /* Number of bits in integer, but we really want to be stable across different
115 hosts. */
116 #define NUM_CONDITIONS 32
118 enum predicate_conditions
120 predicate_false_condition = 0,
121 predicate_not_inlined_condition = 1,
122 predicate_first_dynamic_condition = 2
125 /* Special condition code we use to represent test that operand is compile time
126 constant. */
127 #define IS_NOT_CONSTANT ERROR_MARK
128 /* Special condition code we use to represent test that operand is not changed
129 across invocation of the function. When operand IS_NOT_CONSTANT it is always
130 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
131 of executions even when they are not compile time constants. */
132 #define CHANGED IDENTIFIER_NODE
134 /* Holders of ipa cgraph hooks: */
135 static struct cgraph_node_hook_list *function_insertion_hook_holder;
136 static struct cgraph_node_hook_list *node_removal_hook_holder;
137 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
138 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
139 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
140 static void inline_node_removal_hook (struct cgraph_node *, void *);
141 static void inline_node_duplication_hook (struct cgraph_node *,
142 struct cgraph_node *, void *);
143 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
144 static void inline_edge_duplication_hook (struct cgraph_edge *,
145 struct cgraph_edge *, void *);
147 /* VECtor holding inline summaries.
148 In GGC memory because conditions might point to constant trees. */
149 vec<inline_summary_t, va_gc> *inline_summary_vec;
150 vec<inline_edge_summary_t> inline_edge_summary_vec;
152 /* Cached node/edge growths. */
153 vec<int> node_growth_cache;
154 vec<edge_growth_cache_entry> edge_growth_cache;
156 /* Edge predicates goes here. */
157 static alloc_pool edge_predicate_pool;
159 /* Return true predicate (tautology).
160 We represent it by empty list of clauses. */
162 static inline struct predicate
163 true_predicate (void)
165 struct predicate p;
166 p.clause[0] = 0;
167 return p;
171 /* Return predicate testing single condition number COND. */
173 static inline struct predicate
174 single_cond_predicate (int cond)
176 struct predicate p;
177 p.clause[0] = 1 << cond;
178 p.clause[1] = 0;
179 return p;
183 /* Return false predicate. First clause require false condition. */
185 static inline struct predicate
186 false_predicate (void)
188 return single_cond_predicate (predicate_false_condition);
192 /* Return true if P is (true). */
194 static inline bool
195 true_predicate_p (struct predicate *p)
197 return !p->clause[0];
201 /* Return true if P is (false). */
203 static inline bool
204 false_predicate_p (struct predicate *p)
206 if (p->clause[0] == (1 << predicate_false_condition))
208 gcc_checking_assert (!p->clause[1]
209 && p->clause[0] == 1 << predicate_false_condition);
210 return true;
212 return false;
216 /* Return predicate that is set true when function is not inlined. */
218 static inline struct predicate
219 not_inlined_predicate (void)
221 return single_cond_predicate (predicate_not_inlined_condition);
224 /* Simple description of whether a memory load or a condition refers to a load
225 from an aggregate and if so, how and where from in the aggregate.
226 Individual fields have the same meaning like fields with the same name in
227 struct condition. */
229 struct agg_position_info
231 HOST_WIDE_INT offset;
232 bool agg_contents;
233 bool by_ref;
236 /* Add condition to condition list CONDS. AGGPOS describes whether the used
237 oprand is loaded from an aggregate and where in the aggregate it is. It can
238 be NULL, which means this not a load from an aggregate. */
240 static struct predicate
241 add_condition (struct inline_summary *summary, int operand_num,
242 struct agg_position_info *aggpos,
243 enum tree_code code, tree val)
245 int i;
246 struct condition *c;
247 struct condition new_cond;
248 HOST_WIDE_INT offset;
249 bool agg_contents, by_ref;
251 if (aggpos)
253 offset = aggpos->offset;
254 agg_contents = aggpos->agg_contents;
255 by_ref = aggpos->by_ref;
257 else
259 offset = 0;
260 agg_contents = false;
261 by_ref = false;
264 gcc_checking_assert (operand_num >= 0);
265 for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
267 if (c->operand_num == operand_num
268 && c->code == code
269 && c->val == val
270 && c->agg_contents == agg_contents
271 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
272 return single_cond_predicate (i + predicate_first_dynamic_condition);
274 /* Too many conditions. Give up and return constant true. */
275 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
276 return true_predicate ();
278 new_cond.operand_num = operand_num;
279 new_cond.code = code;
280 new_cond.val = val;
281 new_cond.agg_contents = agg_contents;
282 new_cond.by_ref = by_ref;
283 new_cond.offset = offset;
284 vec_safe_push (summary->conds, new_cond);
285 return single_cond_predicate (i + predicate_first_dynamic_condition);
289 /* Add clause CLAUSE into the predicate P. */
291 static inline void
292 add_clause (conditions conditions, struct predicate *p, clause_t clause)
294 int i;
295 int i2;
296 int insert_here = -1;
297 int c1, c2;
299 /* True clause. */
300 if (!clause)
301 return;
303 /* False clause makes the whole predicate false. Kill the other variants. */
304 if (clause == (1 << predicate_false_condition))
306 p->clause[0] = (1 << predicate_false_condition);
307 p->clause[1] = 0;
308 return;
310 if (false_predicate_p (p))
311 return;
313 /* No one should be silly enough to add false into nontrivial clauses. */
314 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
316 /* Look where to insert the clause. At the same time prune out
317 clauses of P that are implied by the new clause and thus
318 redundant. */
319 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
321 p->clause[i2] = p->clause[i];
323 if (!p->clause[i])
324 break;
326 /* If p->clause[i] implies clause, there is nothing to add. */
327 if ((p->clause[i] & clause) == p->clause[i])
329 /* We had nothing to add, none of clauses should've become
330 redundant. */
331 gcc_checking_assert (i == i2);
332 return;
335 if (p->clause[i] < clause && insert_here < 0)
336 insert_here = i2;
338 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
339 Otherwise the p->clause[i] has to stay. */
340 if ((p->clause[i] & clause) != clause)
341 i2++;
344 /* Look for clauses that are obviously true. I.e.
345 op0 == 5 || op0 != 5. */
346 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
348 condition *cc1;
349 if (!(clause & (1 << c1)))
350 continue;
351 cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
352 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
353 and thus there is no point for looking for them. */
354 if (cc1->code == CHANGED || cc1->code == IS_NOT_CONSTANT)
355 continue;
356 for (c2 = c1 + 1; c2 < NUM_CONDITIONS; c2++)
357 if (clause & (1 << c2))
359 condition *cc1 =
360 &(*conditions)[c1 - predicate_first_dynamic_condition];
361 condition *cc2 =
362 &(*conditions)[c2 - predicate_first_dynamic_condition];
363 if (cc1->operand_num == cc2->operand_num
364 && cc1->val == cc2->val
365 && cc2->code != IS_NOT_CONSTANT
366 && cc2->code != CHANGED
367 && cc1->code == invert_tree_comparison
368 (cc2->code,
369 HONOR_NANS (TYPE_MODE (TREE_TYPE (cc1->val)))))
370 return;
375 /* We run out of variants. Be conservative in positive direction. */
376 if (i2 == MAX_CLAUSES)
377 return;
378 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
379 p->clause[i2 + 1] = 0;
380 if (insert_here >= 0)
381 for (; i2 > insert_here; i2--)
382 p->clause[i2] = p->clause[i2 - 1];
383 else
384 insert_here = i2;
385 p->clause[insert_here] = clause;
389 /* Return P & P2. */
391 static struct predicate
392 and_predicates (conditions conditions,
393 struct predicate *p, struct predicate *p2)
395 struct predicate out = *p;
396 int i;
398 /* Avoid busy work. */
399 if (false_predicate_p (p2) || true_predicate_p (p))
400 return *p2;
401 if (false_predicate_p (p) || true_predicate_p (p2))
402 return *p;
404 /* See how far predicates match. */
405 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
407 gcc_checking_assert (i < MAX_CLAUSES);
410 /* Combine the predicates rest. */
411 for (; p2->clause[i]; i++)
413 gcc_checking_assert (i < MAX_CLAUSES);
414 add_clause (conditions, &out, p2->clause[i]);
416 return out;
420 /* Return true if predicates are obviously equal. */
422 static inline bool
423 predicates_equal_p (struct predicate *p, struct predicate *p2)
425 int i;
426 for (i = 0; p->clause[i]; i++)
428 gcc_checking_assert (i < MAX_CLAUSES);
429 gcc_checking_assert (p->clause[i] > p->clause[i + 1]);
430 gcc_checking_assert (!p2->clause[i]
431 || p2->clause[i] > p2->clause[i + 1]);
432 if (p->clause[i] != p2->clause[i])
433 return false;
435 return !p2->clause[i];
439 /* Return P | P2. */
441 static struct predicate
442 or_predicates (conditions conditions,
443 struct predicate *p, struct predicate *p2)
445 struct predicate out = true_predicate ();
446 int i, j;
448 /* Avoid busy work. */
449 if (false_predicate_p (p2) || true_predicate_p (p))
450 return *p;
451 if (false_predicate_p (p) || true_predicate_p (p2))
452 return *p2;
453 if (predicates_equal_p (p, p2))
454 return *p;
456 /* OK, combine the predicates. */
457 for (i = 0; p->clause[i]; i++)
458 for (j = 0; p2->clause[j]; j++)
460 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
461 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
463 return out;
467 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
468 if predicate P is known to be false. */
470 static bool
471 evaluate_predicate (struct predicate *p, clause_t possible_truths)
473 int i;
475 /* True remains true. */
476 if (true_predicate_p (p))
477 return true;
479 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
481 /* See if we can find clause we can disprove. */
482 for (i = 0; p->clause[i]; i++)
484 gcc_checking_assert (i < MAX_CLAUSES);
485 if (!(p->clause[i] & possible_truths))
486 return false;
488 return true;
491 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
492 instruction will be recomputed per invocation of the inlined call. */
494 static int
495 predicate_probability (conditions conds,
496 struct predicate *p, clause_t possible_truths,
497 vec<inline_param_summary> inline_param_summary)
499 int i;
500 int combined_prob = REG_BR_PROB_BASE;
502 /* True remains true. */
503 if (true_predicate_p (p))
504 return REG_BR_PROB_BASE;
506 if (false_predicate_p (p))
507 return 0;
509 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
511 /* See if we can find clause we can disprove. */
512 for (i = 0; p->clause[i]; i++)
514 gcc_checking_assert (i < MAX_CLAUSES);
515 if (!(p->clause[i] & possible_truths))
516 return 0;
517 else
519 int this_prob = 0;
520 int i2;
521 if (!inline_param_summary.exists ())
522 return REG_BR_PROB_BASE;
523 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
524 if ((p->clause[i] & possible_truths) & (1 << i2))
526 if (i2 >= predicate_first_dynamic_condition)
528 condition *c =
529 &(*conds)[i2 - predicate_first_dynamic_condition];
530 if (c->code == CHANGED
531 && (c->operand_num <
532 (int) inline_param_summary.length ()))
534 int iprob =
535 inline_param_summary[c->operand_num].change_prob;
536 this_prob = MAX (this_prob, iprob);
538 else
539 this_prob = REG_BR_PROB_BASE;
541 else
542 this_prob = REG_BR_PROB_BASE;
544 combined_prob = MIN (this_prob, combined_prob);
545 if (!combined_prob)
546 return 0;
549 return combined_prob;
553 /* Dump conditional COND. */
555 static void
556 dump_condition (FILE *f, conditions conditions, int cond)
558 condition *c;
559 if (cond == predicate_false_condition)
560 fprintf (f, "false");
561 else if (cond == predicate_not_inlined_condition)
562 fprintf (f, "not inlined");
563 else
565 c = &(*conditions)[cond - predicate_first_dynamic_condition];
566 fprintf (f, "op%i", c->operand_num);
567 if (c->agg_contents)
568 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
569 c->by_ref ? "ref " : "", c->offset);
570 if (c->code == IS_NOT_CONSTANT)
572 fprintf (f, " not constant");
573 return;
575 if (c->code == CHANGED)
577 fprintf (f, " changed");
578 return;
580 fprintf (f, " %s ", op_symbol_code (c->code));
581 print_generic_expr (f, c->val, 1);
586 /* Dump clause CLAUSE. */
588 static void
589 dump_clause (FILE *f, conditions conds, clause_t clause)
591 int i;
592 bool found = false;
593 fprintf (f, "(");
594 if (!clause)
595 fprintf (f, "true");
596 for (i = 0; i < NUM_CONDITIONS; i++)
597 if (clause & (1 << i))
599 if (found)
600 fprintf (f, " || ");
601 found = true;
602 dump_condition (f, conds, i);
604 fprintf (f, ")");
608 /* Dump predicate PREDICATE. */
610 static void
611 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
613 int i;
614 if (true_predicate_p (pred))
615 dump_clause (f, conds, 0);
616 else
617 for (i = 0; pred->clause[i]; i++)
619 if (i)
620 fprintf (f, " && ");
621 dump_clause (f, conds, pred->clause[i]);
623 fprintf (f, "\n");
627 /* Dump inline hints. */
628 void
629 dump_inline_hints (FILE *f, inline_hints hints)
631 if (!hints)
632 return;
633 fprintf (f, "inline hints:");
634 if (hints & INLINE_HINT_indirect_call)
636 hints &= ~INLINE_HINT_indirect_call;
637 fprintf (f, " indirect_call");
639 if (hints & INLINE_HINT_loop_iterations)
641 hints &= ~INLINE_HINT_loop_iterations;
642 fprintf (f, " loop_iterations");
644 if (hints & INLINE_HINT_loop_stride)
646 hints &= ~INLINE_HINT_loop_stride;
647 fprintf (f, " loop_stride");
649 if (hints & INLINE_HINT_same_scc)
651 hints &= ~INLINE_HINT_same_scc;
652 fprintf (f, " same_scc");
654 if (hints & INLINE_HINT_in_scc)
656 hints &= ~INLINE_HINT_in_scc;
657 fprintf (f, " in_scc");
659 if (hints & INLINE_HINT_cross_module)
661 hints &= ~INLINE_HINT_cross_module;
662 fprintf (f, " cross_module");
664 if (hints & INLINE_HINT_declared_inline)
666 hints &= ~INLINE_HINT_declared_inline;
667 fprintf (f, " declared_inline");
669 if (hints & INLINE_HINT_array_index)
671 hints &= ~INLINE_HINT_array_index;
672 fprintf (f, " array_index");
674 gcc_assert (!hints);
678 /* Record SIZE and TIME under condition PRED into the inline summary. */
680 static void
681 account_size_time (struct inline_summary *summary, int size, int time,
682 struct predicate *pred)
684 size_time_entry *e;
685 bool found = false;
686 int i;
688 if (false_predicate_p (pred))
689 return;
691 /* We need to create initial empty unconitional clause, but otherwie
692 we don't need to account empty times and sizes. */
693 if (!size && !time && summary->entry)
694 return;
696 /* Watch overflow that might result from insane profiles. */
697 if (time > MAX_TIME * INLINE_TIME_SCALE)
698 time = MAX_TIME * INLINE_TIME_SCALE;
699 gcc_assert (time >= 0);
701 for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
702 if (predicates_equal_p (&e->predicate, pred))
704 found = true;
705 break;
707 if (i == 256)
709 i = 0;
710 found = true;
711 e = &(*summary->entry)[0];
712 gcc_assert (!e->predicate.clause[0]);
713 if (dump_file && (dump_flags & TDF_DETAILS))
714 fprintf (dump_file,
715 "\t\tReached limit on number of entries, "
716 "ignoring the predicate.");
718 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
720 fprintf (dump_file,
721 "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
722 ((double) size) / INLINE_SIZE_SCALE,
723 ((double) time) / INLINE_TIME_SCALE, found ? "" : "new ");
724 dump_predicate (dump_file, summary->conds, pred);
726 if (!found)
728 struct size_time_entry new_entry;
729 new_entry.size = size;
730 new_entry.time = time;
731 new_entry.predicate = *pred;
732 vec_safe_push (summary->entry, new_entry);
734 else
736 e->size += size;
737 e->time += time;
738 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
739 e->time = MAX_TIME * INLINE_TIME_SCALE;
743 /* Set predicate for edge E. */
745 static void
746 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
748 struct inline_edge_summary *es = inline_edge_summary (e);
749 if (predicate && !true_predicate_p (predicate))
751 if (!es->predicate)
752 es->predicate = (struct predicate *) pool_alloc (edge_predicate_pool);
753 *es->predicate = *predicate;
755 else
757 if (es->predicate)
758 pool_free (edge_predicate_pool, es->predicate);
759 es->predicate = NULL;
763 /* Set predicate for hint *P. */
765 static void
766 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
768 if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
770 if (*p)
771 pool_free (edge_predicate_pool, *p);
772 *p = NULL;
774 else
776 if (!*p)
777 *p = (struct predicate *) pool_alloc (edge_predicate_pool);
778 **p = new_predicate;
783 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
784 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
785 Return clause of possible truths. When INLINE_P is true, assume that we are
786 inlining.
788 ERROR_MARK means compile time invariant. */
790 static clause_t
791 evaluate_conditions_for_known_args (struct cgraph_node *node,
792 bool inline_p,
793 vec<tree> known_vals,
794 vec<ipa_agg_jump_function_p>
795 known_aggs)
797 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
798 struct inline_summary *info = inline_summary (node);
799 int i;
800 struct condition *c;
802 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
804 tree val;
805 tree res;
807 /* We allow call stmt to have fewer arguments than the callee function
808 (especially for K&R style programs). So bound check here (we assume
809 known_aggs vector, if non-NULL, has the same length as
810 known_vals). */
811 gcc_checking_assert (!known_aggs.exists ()
812 || (known_vals.length () == known_aggs.length ()));
813 if (c->operand_num >= (int) known_vals.length ())
815 clause |= 1 << (i + predicate_first_dynamic_condition);
816 continue;
819 if (c->agg_contents)
821 struct ipa_agg_jump_function *agg;
823 if (c->code == CHANGED
824 && !c->by_ref
825 && (known_vals[c->operand_num] == error_mark_node))
826 continue;
828 if (known_aggs.exists ())
830 agg = known_aggs[c->operand_num];
831 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
833 else
834 val = NULL_TREE;
836 else
838 val = known_vals[c->operand_num];
839 if (val == error_mark_node && c->code != CHANGED)
840 val = NULL_TREE;
843 if (!val)
845 clause |= 1 << (i + predicate_first_dynamic_condition);
846 continue;
848 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
849 continue;
850 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
851 if (res && integer_zerop (res))
852 continue;
853 clause |= 1 << (i + predicate_first_dynamic_condition);
855 return clause;
859 /* Work out what conditions might be true at invocation of E. */
861 static void
862 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
863 clause_t *clause_ptr,
864 vec<tree> *known_vals_ptr,
865 vec<tree> *known_binfos_ptr,
866 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
868 struct cgraph_node *callee =
869 cgraph_function_or_thunk_node (e->callee, NULL);
870 struct inline_summary *info = inline_summary (callee);
871 vec<tree> known_vals = vNULL;
872 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
874 if (clause_ptr)
875 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
876 if (known_vals_ptr)
877 known_vals_ptr->create (0);
878 if (known_binfos_ptr)
879 known_binfos_ptr->create (0);
881 if (ipa_node_params_vector.exists ()
882 && !e->call_stmt_cannot_inline_p
883 && ((clause_ptr && info->conds) || known_vals_ptr || known_binfos_ptr))
885 struct ipa_node_params *parms_info;
886 struct ipa_edge_args *args = IPA_EDGE_REF (e);
887 struct inline_edge_summary *es = inline_edge_summary (e);
888 int i, count = ipa_get_cs_argument_count (args);
890 if (e->caller->global.inlined_to)
891 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
892 else
893 parms_info = IPA_NODE_REF (e->caller);
895 if (count && (info->conds || known_vals_ptr))
896 known_vals.safe_grow_cleared (count);
897 if (count && (info->conds || known_aggs_ptr))
898 known_aggs.safe_grow_cleared (count);
899 if (count && known_binfos_ptr)
900 known_binfos_ptr->safe_grow_cleared (count);
902 for (i = 0; i < count; i++)
904 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
905 tree cst = ipa_value_from_jfunc (parms_info, jf);
906 if (cst)
908 if (known_vals.exists () && TREE_CODE (cst) != TREE_BINFO)
909 known_vals[i] = cst;
910 else if (known_binfos_ptr != NULL
911 && TREE_CODE (cst) == TREE_BINFO)
912 (*known_binfos_ptr)[i] = cst;
914 else if (inline_p && !es->param[i].change_prob)
915 known_vals[i] = error_mark_node;
916 /* TODO: When IPA-CP starts propagating and merging aggregate jump
917 functions, use its knowledge of the caller too, just like the
918 scalar case above. */
919 known_aggs[i] = &jf->agg;
923 if (clause_ptr)
924 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
925 known_vals, known_aggs);
927 if (known_vals_ptr)
928 *known_vals_ptr = known_vals;
929 else
930 known_vals.release ();
932 if (known_aggs_ptr)
933 *known_aggs_ptr = known_aggs;
934 else
935 known_aggs.release ();
939 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
941 static void
942 inline_summary_alloc (void)
944 if (!node_removal_hook_holder)
945 node_removal_hook_holder =
946 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
947 if (!edge_removal_hook_holder)
948 edge_removal_hook_holder =
949 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
950 if (!node_duplication_hook_holder)
951 node_duplication_hook_holder =
952 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
953 if (!edge_duplication_hook_holder)
954 edge_duplication_hook_holder =
955 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
957 if (vec_safe_length (inline_summary_vec) <= (unsigned) cgraph_max_uid)
958 vec_safe_grow_cleared (inline_summary_vec, cgraph_max_uid + 1);
959 if (inline_edge_summary_vec.length () <= (unsigned) cgraph_edge_max_uid)
960 inline_edge_summary_vec.safe_grow_cleared (cgraph_edge_max_uid + 1);
961 if (!edge_predicate_pool)
962 edge_predicate_pool = create_alloc_pool ("edge predicates",
963 sizeof (struct predicate), 10);
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_edge_summary (struct cgraph_edge *e)
972 if (e->uid < (int) inline_edge_summary_vec.length ())
974 struct inline_edge_summary *es = inline_edge_summary (e);
976 es->call_stmt_size = es->call_stmt_time = 0;
977 if (es->predicate)
978 pool_free (edge_predicate_pool, es->predicate);
979 es->predicate = NULL;
980 es->param.release ();
984 /* We are called multiple time for given function; clear
985 data from previous run so they are not cumulated. */
987 static void
988 reset_inline_summary (struct cgraph_node *node)
990 struct inline_summary *info = inline_summary (node);
991 struct cgraph_edge *e;
993 info->self_size = info->self_time = 0;
994 info->estimated_stack_size = 0;
995 info->estimated_self_stack_size = 0;
996 info->stack_frame_offset = 0;
997 info->size = 0;
998 info->time = 0;
999 info->growth = 0;
1000 info->scc_no = 0;
1001 if (info->loop_iterations)
1003 pool_free (edge_predicate_pool, info->loop_iterations);
1004 info->loop_iterations = NULL;
1006 if (info->loop_stride)
1008 pool_free (edge_predicate_pool, info->loop_stride);
1009 info->loop_stride = NULL;
1011 if (info->array_index)
1013 pool_free (edge_predicate_pool, info->array_index);
1014 info->array_index = NULL;
1016 vec_free (info->conds);
1017 vec_free (info->entry);
1018 for (e = node->callees; e; e = e->next_callee)
1019 reset_inline_edge_summary (e);
1020 for (e = node->indirect_calls; e; e = e->next_callee)
1021 reset_inline_edge_summary (e);
1024 /* Hook that is called by cgraph.c when a node is removed. */
1026 static void
1027 inline_node_removal_hook (struct cgraph_node *node,
1028 void *data ATTRIBUTE_UNUSED)
1030 struct inline_summary *info;
1031 if (vec_safe_length (inline_summary_vec) <= (unsigned) node->uid)
1032 return;
1033 info = inline_summary (node);
1034 reset_inline_summary (node);
1035 memset (info, 0, sizeof (inline_summary_t));
1038 /* Remap predicate P of former function to be predicate of duplicated function.
1039 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1040 INFO is inline summary of the duplicated node. */
1042 static struct predicate
1043 remap_predicate_after_duplication (struct predicate *p,
1044 clause_t possible_truths,
1045 struct inline_summary *info)
1047 struct predicate new_predicate = true_predicate ();
1048 int j;
1049 for (j = 0; p->clause[j]; j++)
1050 if (!(possible_truths & p->clause[j]))
1052 new_predicate = false_predicate ();
1053 break;
1055 else
1056 add_clause (info->conds, &new_predicate,
1057 possible_truths & p->clause[j]);
1058 return new_predicate;
1061 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1062 Additionally care about allocating new memory slot for updated predicate
1063 and set it to NULL when it becomes true or false (and thus uninteresting).
1066 static void
1067 remap_hint_predicate_after_duplication (struct predicate **p,
1068 clause_t possible_truths,
1069 struct inline_summary *info)
1071 struct predicate new_predicate;
1073 if (!*p)
1074 return;
1076 new_predicate = remap_predicate_after_duplication (*p,
1077 possible_truths, info);
1078 /* We do not want to free previous predicate; it is used by node origin. */
1079 *p = NULL;
1080 set_hint_predicate (p, new_predicate);
1084 /* Hook that is called by cgraph.c when a node is duplicated. */
1086 static void
1087 inline_node_duplication_hook (struct cgraph_node *src,
1088 struct cgraph_node *dst,
1089 ATTRIBUTE_UNUSED void *data)
1091 struct inline_summary *info;
1092 inline_summary_alloc ();
1093 info = inline_summary (dst);
1094 memcpy (info, inline_summary (src), sizeof (struct inline_summary));
1095 /* TODO: as an optimization, we may avoid copying conditions
1096 that are known to be false or true. */
1097 info->conds = vec_safe_copy (info->conds);
1099 /* When there are any replacements in the function body, see if we can figure
1100 out that something was optimized out. */
1101 if (ipa_node_params_vector.exists () && dst->clone.tree_map)
1103 vec<size_time_entry, va_gc> *entry = info->entry;
1104 /* Use SRC parm info since it may not be copied yet. */
1105 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1106 vec<tree> known_vals = vNULL;
1107 int count = ipa_get_param_count (parms_info);
1108 int i, j;
1109 clause_t possible_truths;
1110 struct predicate true_pred = true_predicate ();
1111 size_time_entry *e;
1112 int optimized_out_size = 0;
1113 bool inlined_to_p = false;
1114 struct cgraph_edge *edge;
1116 info->entry = 0;
1117 known_vals.safe_grow_cleared (count);
1118 for (i = 0; i < count; i++)
1120 struct ipa_replace_map *r;
1122 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1124 if (((!r->old_tree && r->parm_num == i)
1125 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1126 && r->replace_p && !r->ref_p)
1128 known_vals[i] = r->new_tree;
1129 break;
1133 possible_truths = evaluate_conditions_for_known_args (dst, false,
1134 known_vals,
1135 vNULL);
1136 known_vals.release ();
1138 account_size_time (info, 0, 0, &true_pred);
1140 /* Remap size_time vectors.
1141 Simplify the predicate by prunning out alternatives that are known
1142 to be false.
1143 TODO: as on optimization, we can also eliminate conditions known
1144 to be true. */
1145 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1147 struct predicate new_predicate;
1148 new_predicate = remap_predicate_after_duplication (&e->predicate,
1149 possible_truths,
1150 info);
1151 if (false_predicate_p (&new_predicate))
1152 optimized_out_size += e->size;
1153 else
1154 account_size_time (info, e->size, e->time, &new_predicate);
1157 /* Remap edge predicates with the same simplification as above.
1158 Also copy constantness arrays. */
1159 for (edge = dst->callees; edge; edge = edge->next_callee)
1161 struct predicate new_predicate;
1162 struct inline_edge_summary *es = inline_edge_summary (edge);
1164 if (!edge->inline_failed)
1165 inlined_to_p = true;
1166 if (!es->predicate)
1167 continue;
1168 new_predicate = remap_predicate_after_duplication (es->predicate,
1169 possible_truths,
1170 info);
1171 if (false_predicate_p (&new_predicate)
1172 && !false_predicate_p (es->predicate))
1174 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1175 edge->frequency = 0;
1177 edge_set_predicate (edge, &new_predicate);
1180 /* Remap indirect edge predicates with the same simplificaiton as above.
1181 Also copy constantness arrays. */
1182 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1184 struct predicate new_predicate;
1185 struct inline_edge_summary *es = inline_edge_summary (edge);
1187 gcc_checking_assert (edge->inline_failed);
1188 if (!es->predicate)
1189 continue;
1190 new_predicate = remap_predicate_after_duplication (es->predicate,
1191 possible_truths,
1192 info);
1193 if (false_predicate_p (&new_predicate)
1194 && !false_predicate_p (es->predicate))
1196 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1197 edge->frequency = 0;
1199 edge_set_predicate (edge, &new_predicate);
1201 remap_hint_predicate_after_duplication (&info->loop_iterations,
1202 possible_truths, info);
1203 remap_hint_predicate_after_duplication (&info->loop_stride,
1204 possible_truths, info);
1205 remap_hint_predicate_after_duplication (&info->array_index,
1206 possible_truths, info);
1208 /* If inliner or someone after inliner will ever start producing
1209 non-trivial clones, we will get trouble with lack of information
1210 about updating self sizes, because size vectors already contains
1211 sizes of the calees. */
1212 gcc_assert (!inlined_to_p || !optimized_out_size);
1214 else
1216 info->entry = vec_safe_copy (info->entry);
1217 if (info->loop_iterations)
1219 predicate p = *info->loop_iterations;
1220 info->loop_iterations = NULL;
1221 set_hint_predicate (&info->loop_iterations, p);
1223 if (info->loop_stride)
1225 predicate p = *info->loop_stride;
1226 info->loop_stride = NULL;
1227 set_hint_predicate (&info->loop_stride, p);
1229 if (info->array_index)
1231 predicate p = *info->array_index;
1232 info->array_index = NULL;
1233 set_hint_predicate (&info->array_index, p);
1236 inline_update_overall_summary (dst);
1240 /* Hook that is called by cgraph.c when a node is duplicated. */
1242 static void
1243 inline_edge_duplication_hook (struct cgraph_edge *src,
1244 struct cgraph_edge *dst,
1245 ATTRIBUTE_UNUSED void *data)
1247 struct inline_edge_summary *info;
1248 struct inline_edge_summary *srcinfo;
1249 inline_summary_alloc ();
1250 info = inline_edge_summary (dst);
1251 srcinfo = inline_edge_summary (src);
1252 memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
1253 info->predicate = NULL;
1254 edge_set_predicate (dst, srcinfo->predicate);
1255 info->param = srcinfo->param.copy ();
1259 /* Keep edge cache consistent across edge removal. */
1261 static void
1262 inline_edge_removal_hook (struct cgraph_edge *edge,
1263 void *data ATTRIBUTE_UNUSED)
1265 if (edge_growth_cache.exists ())
1266 reset_edge_growth_cache (edge);
1267 reset_inline_edge_summary (edge);
1271 /* Initialize growth caches. */
1273 void
1274 initialize_growth_caches (void)
1276 if (cgraph_edge_max_uid)
1277 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
1278 if (cgraph_max_uid)
1279 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
1283 /* Free growth caches. */
1285 void
1286 free_growth_caches (void)
1288 edge_growth_cache.release ();
1289 node_growth_cache.release ();
1293 /* Dump edge summaries associated to NODE and recursively to all clones.
1294 Indent by INDENT. */
1296 static void
1297 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1298 struct inline_summary *info)
1300 struct cgraph_edge *edge;
1301 for (edge = node->callees; edge; edge = edge->next_callee)
1303 struct inline_edge_summary *es = inline_edge_summary (edge);
1304 struct cgraph_node *callee =
1305 cgraph_function_or_thunk_node (edge->callee, NULL);
1306 int i;
1308 fprintf (f,
1309 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1310 " time: %2i callee size:%2i stack:%2i",
1311 indent, "", callee->name (), callee->order,
1312 !edge->inline_failed
1313 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1314 indent, "", es->loop_depth, edge->frequency,
1315 es->call_stmt_size, es->call_stmt_time,
1316 (int) inline_summary (callee)->size / INLINE_SIZE_SCALE,
1317 (int) inline_summary (callee)->estimated_stack_size);
1319 if (es->predicate)
1321 fprintf (f, " predicate: ");
1322 dump_predicate (f, info->conds, es->predicate);
1324 else
1325 fprintf (f, "\n");
1326 if (es->param.exists ())
1327 for (i = 0; i < (int) es->param.length (); i++)
1329 int prob = es->param[i].change_prob;
1331 if (!prob)
1332 fprintf (f, "%*s op%i is compile time invariant\n",
1333 indent + 2, "", i);
1334 else if (prob != REG_BR_PROB_BASE)
1335 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1336 prob * 100.0 / REG_BR_PROB_BASE);
1338 if (!edge->inline_failed)
1340 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1341 " callee size %i\n",
1342 indent + 2, "",
1343 (int) inline_summary (callee)->stack_frame_offset,
1344 (int) inline_summary (callee)->estimated_self_stack_size,
1345 (int) inline_summary (callee)->estimated_stack_size);
1346 dump_inline_edge_summary (f, indent + 2, callee, info);
1349 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1351 struct inline_edge_summary *es = inline_edge_summary (edge);
1352 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1353 " time: %2i",
1354 indent, "",
1355 es->loop_depth,
1356 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1357 if (es->predicate)
1359 fprintf (f, "predicate: ");
1360 dump_predicate (f, info->conds, es->predicate);
1362 else
1363 fprintf (f, "\n");
1368 void
1369 dump_inline_summary (FILE *f, struct cgraph_node *node)
1371 if (node->definition)
1373 struct inline_summary *s = inline_summary (node);
1374 size_time_entry *e;
1375 int i;
1376 fprintf (f, "Inline summary for %s/%i", node->name (),
1377 node->order);
1378 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1379 fprintf (f, " always_inline");
1380 if (s->inlinable)
1381 fprintf (f, " inlinable");
1382 fprintf (f, "\n self time: %i\n", s->self_time);
1383 fprintf (f, " global time: %i\n", s->time);
1384 fprintf (f, " self size: %i\n", s->self_size);
1385 fprintf (f, " global size: %i\n", s->size);
1386 fprintf (f, " self stack: %i\n",
1387 (int) s->estimated_self_stack_size);
1388 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1389 if (s->growth)
1390 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1391 if (s->scc_no)
1392 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1393 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1395 fprintf (f, " size:%f, time:%f, predicate:",
1396 (double) e->size / INLINE_SIZE_SCALE,
1397 (double) e->time / INLINE_TIME_SCALE);
1398 dump_predicate (f, s->conds, &e->predicate);
1400 if (s->loop_iterations)
1402 fprintf (f, " loop iterations:");
1403 dump_predicate (f, s->conds, s->loop_iterations);
1405 if (s->loop_stride)
1407 fprintf (f, " loop stride:");
1408 dump_predicate (f, s->conds, s->loop_stride);
1410 if (s->array_index)
1412 fprintf (f, " array index:");
1413 dump_predicate (f, s->conds, s->array_index);
1415 fprintf (f, " calls:\n");
1416 dump_inline_edge_summary (f, 4, node, s);
1417 fprintf (f, "\n");
1421 DEBUG_FUNCTION void
1422 debug_inline_summary (struct cgraph_node *node)
1424 dump_inline_summary (stderr, node);
1427 void
1428 dump_inline_summaries (FILE *f)
1430 struct cgraph_node *node;
1432 FOR_EACH_DEFINED_FUNCTION (node)
1433 if (!node->global.inlined_to)
1434 dump_inline_summary (f, node);
1437 /* Give initial reasons why inlining would fail on EDGE. This gets either
1438 nullified or usually overwritten by more precise reasons later. */
1440 void
1441 initialize_inline_failed (struct cgraph_edge *e)
1443 struct cgraph_node *callee = e->callee;
1445 if (e->indirect_unknown_callee)
1446 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1447 else if (!callee->definition)
1448 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1449 else if (callee->local.redefined_extern_inline)
1450 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1451 else if (e->call_stmt_cannot_inline_p)
1452 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1453 else if (cfun && fn_contains_cilk_spawn_p (cfun))
1454 /* We can't inline if the function is spawing a function. */
1455 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
1456 else
1457 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1460 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1461 boolean variable pointed to by DATA. */
1463 static bool
1464 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1465 void *data)
1467 bool *b = (bool *) data;
1468 *b = true;
1469 return true;
1472 /* If OP refers to value of function parameter, return the corresponding
1473 parameter. */
1475 static tree
1476 unmodified_parm_1 (gimple stmt, tree op)
1478 /* SSA_NAME referring to parm default def? */
1479 if (TREE_CODE (op) == SSA_NAME
1480 && SSA_NAME_IS_DEFAULT_DEF (op)
1481 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1482 return SSA_NAME_VAR (op);
1483 /* Non-SSA parm reference? */
1484 if (TREE_CODE (op) == PARM_DECL)
1486 bool modified = false;
1488 ao_ref refd;
1489 ao_ref_init (&refd, op);
1490 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1491 NULL);
1492 if (!modified)
1493 return op;
1495 return NULL_TREE;
1498 /* If OP refers to value of function parameter, return the corresponding
1499 parameter. Also traverse chains of SSA register assignments. */
1501 static tree
1502 unmodified_parm (gimple stmt, tree op)
1504 tree res = unmodified_parm_1 (stmt, op);
1505 if (res)
1506 return res;
1508 if (TREE_CODE (op) == SSA_NAME
1509 && !SSA_NAME_IS_DEFAULT_DEF (op)
1510 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1511 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1512 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1513 return NULL_TREE;
1516 /* If OP refers to a value of a function parameter or value loaded from an
1517 aggregate passed to a parameter (either by value or reference), return TRUE
1518 and store the number of the parameter to *INDEX_P and information whether
1519 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1520 the function parameters, STMT is the statement in which OP is used or
1521 loaded. */
1523 static bool
1524 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1525 gimple stmt, tree op, int *index_p,
1526 struct agg_position_info *aggpos)
1528 tree res = unmodified_parm_1 (stmt, op);
1530 gcc_checking_assert (aggpos);
1531 if (res)
1533 *index_p = ipa_get_param_decl_index (info, res);
1534 if (*index_p < 0)
1535 return false;
1536 aggpos->agg_contents = false;
1537 aggpos->by_ref = false;
1538 return true;
1541 if (TREE_CODE (op) == SSA_NAME)
1543 if (SSA_NAME_IS_DEFAULT_DEF (op)
1544 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1545 return false;
1546 stmt = SSA_NAME_DEF_STMT (op);
1547 op = gimple_assign_rhs1 (stmt);
1548 if (!REFERENCE_CLASS_P (op))
1549 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1550 aggpos);
1553 aggpos->agg_contents = true;
1554 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1555 &aggpos->by_ref);
1558 /* See if statement might disappear after inlining.
1559 0 - means not eliminated
1560 1 - half of statements goes away
1561 2 - for sure it is eliminated.
1562 We are not terribly sophisticated, basically looking for simple abstraction
1563 penalty wrappers. */
1565 static int
1566 eliminated_by_inlining_prob (gimple stmt)
1568 enum gimple_code code = gimple_code (stmt);
1569 enum tree_code rhs_code;
1571 if (!optimize)
1572 return 0;
1574 switch (code)
1576 case GIMPLE_RETURN:
1577 return 2;
1578 case GIMPLE_ASSIGN:
1579 if (gimple_num_ops (stmt) != 2)
1580 return 0;
1582 rhs_code = gimple_assign_rhs_code (stmt);
1584 /* Casts of parameters, loads from parameters passed by reference
1585 and stores to return value or parameters are often free after
1586 inlining dua to SRA and further combining.
1587 Assume that half of statements goes away. */
1588 if (rhs_code == CONVERT_EXPR
1589 || rhs_code == NOP_EXPR
1590 || rhs_code == VIEW_CONVERT_EXPR
1591 || rhs_code == ADDR_EXPR
1592 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1594 tree rhs = gimple_assign_rhs1 (stmt);
1595 tree lhs = gimple_assign_lhs (stmt);
1596 tree inner_rhs = get_base_address (rhs);
1597 tree inner_lhs = get_base_address (lhs);
1598 bool rhs_free = false;
1599 bool lhs_free = false;
1601 if (!inner_rhs)
1602 inner_rhs = rhs;
1603 if (!inner_lhs)
1604 inner_lhs = lhs;
1606 /* Reads of parameter are expected to be free. */
1607 if (unmodified_parm (stmt, inner_rhs))
1608 rhs_free = true;
1609 /* Match expressions of form &this->field. Those will most likely
1610 combine with something upstream after inlining. */
1611 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1613 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1614 if (TREE_CODE (op) == PARM_DECL)
1615 rhs_free = true;
1616 else if (TREE_CODE (op) == MEM_REF
1617 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1618 rhs_free = true;
1621 /* When parameter is not SSA register because its address is taken
1622 and it is just copied into one, the statement will be completely
1623 free after inlining (we will copy propagate backward). */
1624 if (rhs_free && is_gimple_reg (lhs))
1625 return 2;
1627 /* Reads of parameters passed by reference
1628 expected to be free (i.e. optimized out after inlining). */
1629 if (TREE_CODE (inner_rhs) == MEM_REF
1630 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1631 rhs_free = true;
1633 /* Copying parameter passed by reference into gimple register is
1634 probably also going to copy propagate, but we can't be quite
1635 sure. */
1636 if (rhs_free && is_gimple_reg (lhs))
1637 lhs_free = true;
1639 /* Writes to parameters, parameters passed by value and return value
1640 (either dirrectly or passed via invisible reference) are free.
1642 TODO: We ought to handle testcase like
1643 struct a {int a,b;};
1644 struct a
1645 retrurnsturct (void)
1647 struct a a ={1,2};
1648 return a;
1651 This translate into:
1653 retrurnsturct ()
1655 int a$b;
1656 int a$a;
1657 struct a a;
1658 struct a D.2739;
1660 <bb 2>:
1661 D.2739.a = 1;
1662 D.2739.b = 2;
1663 return D.2739;
1666 For that we either need to copy ipa-split logic detecting writes
1667 to return value. */
1668 if (TREE_CODE (inner_lhs) == PARM_DECL
1669 || TREE_CODE (inner_lhs) == RESULT_DECL
1670 || (TREE_CODE (inner_lhs) == MEM_REF
1671 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1672 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1673 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1674 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1675 (inner_lhs,
1676 0))) == RESULT_DECL))))
1677 lhs_free = true;
1678 if (lhs_free
1679 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1680 rhs_free = true;
1681 if (lhs_free && rhs_free)
1682 return 1;
1684 return 0;
1685 default:
1686 return 0;
1691 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1692 predicates to the CFG edges. */
1694 static void
1695 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1696 struct inline_summary *summary,
1697 basic_block bb)
1699 gimple last;
1700 tree op;
1701 int index;
1702 struct agg_position_info aggpos;
1703 enum tree_code code, inverted_code;
1704 edge e;
1705 edge_iterator ei;
1706 gimple set_stmt;
1707 tree op2;
1709 last = last_stmt (bb);
1710 if (!last || gimple_code (last) != GIMPLE_COND)
1711 return;
1712 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1713 return;
1714 op = gimple_cond_lhs (last);
1715 /* TODO: handle conditionals like
1716 var = op0 < 4;
1717 if (var != 0). */
1718 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1720 code = gimple_cond_code (last);
1721 inverted_code
1722 = invert_tree_comparison (code,
1723 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1725 FOR_EACH_EDGE (e, ei, bb->succs)
1727 struct predicate p = add_condition (summary, index, &aggpos,
1728 e->flags & EDGE_TRUE_VALUE
1729 ? code : inverted_code,
1730 gimple_cond_rhs (last));
1731 e->aux = pool_alloc (edge_predicate_pool);
1732 *(struct predicate *) e->aux = p;
1736 if (TREE_CODE (op) != SSA_NAME)
1737 return;
1738 /* Special case
1739 if (builtin_constant_p (op))
1740 constant_code
1741 else
1742 nonconstant_code.
1743 Here we can predicate nonconstant_code. We can't
1744 really handle constant_code since we have no predicate
1745 for this and also the constant code is not known to be
1746 optimized away when inliner doen't see operand is constant.
1747 Other optimizers might think otherwise. */
1748 if (gimple_cond_code (last) != NE_EXPR
1749 || !integer_zerop (gimple_cond_rhs (last)))
1750 return;
1751 set_stmt = SSA_NAME_DEF_STMT (op);
1752 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1753 || gimple_call_num_args (set_stmt) != 1)
1754 return;
1755 op2 = gimple_call_arg (set_stmt, 0);
1756 if (!unmodified_parm_or_parm_agg_item
1757 (info, set_stmt, op2, &index, &aggpos))
1758 return;
1759 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1761 struct predicate p = add_condition (summary, index, &aggpos,
1762 IS_NOT_CONSTANT, NULL_TREE);
1763 e->aux = pool_alloc (edge_predicate_pool);
1764 *(struct predicate *) e->aux = p;
1769 /* If BB ends by a switch we can turn into predicates, attach corresponding
1770 predicates to the CFG edges. */
1772 static void
1773 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1774 struct inline_summary *summary,
1775 basic_block bb)
1777 gimple last;
1778 tree op;
1779 int index;
1780 struct agg_position_info aggpos;
1781 edge e;
1782 edge_iterator ei;
1783 size_t n;
1784 size_t case_idx;
1786 last = last_stmt (bb);
1787 if (!last || gimple_code (last) != GIMPLE_SWITCH)
1788 return;
1789 op = gimple_switch_index (last);
1790 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1791 return;
1793 FOR_EACH_EDGE (e, ei, bb->succs)
1795 e->aux = pool_alloc (edge_predicate_pool);
1796 *(struct predicate *) e->aux = false_predicate ();
1798 n = gimple_switch_num_labels (last);
1799 for (case_idx = 0; case_idx < n; ++case_idx)
1801 tree cl = gimple_switch_label (last, case_idx);
1802 tree min, max;
1803 struct predicate p;
1805 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1806 min = CASE_LOW (cl);
1807 max = CASE_HIGH (cl);
1809 /* For default we might want to construct predicate that none
1810 of cases is met, but it is bit hard to do not having negations
1811 of conditionals handy. */
1812 if (!min && !max)
1813 p = true_predicate ();
1814 else if (!max)
1815 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1816 else
1818 struct predicate p1, p2;
1819 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1820 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1821 p = and_predicates (summary->conds, &p1, &p2);
1823 *(struct predicate *) e->aux
1824 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1829 /* For each BB in NODE attach to its AUX pointer predicate under
1830 which it is executable. */
1832 static void
1833 compute_bb_predicates (struct cgraph_node *node,
1834 struct ipa_node_params *parms_info,
1835 struct inline_summary *summary)
1837 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1838 bool done = false;
1839 basic_block bb;
1841 FOR_EACH_BB_FN (bb, my_function)
1843 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1844 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1847 /* Entry block is always executable. */
1848 ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1849 = pool_alloc (edge_predicate_pool);
1850 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1851 = true_predicate ();
1853 /* A simple dataflow propagation of predicates forward in the CFG.
1854 TODO: work in reverse postorder. */
1855 while (!done)
1857 done = true;
1858 FOR_EACH_BB_FN (bb, my_function)
1860 struct predicate p = false_predicate ();
1861 edge e;
1862 edge_iterator ei;
1863 FOR_EACH_EDGE (e, ei, bb->preds)
1865 if (e->src->aux)
1867 struct predicate this_bb_predicate
1868 = *(struct predicate *) e->src->aux;
1869 if (e->aux)
1870 this_bb_predicate
1871 = and_predicates (summary->conds, &this_bb_predicate,
1872 (struct predicate *) e->aux);
1873 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1874 if (true_predicate_p (&p))
1875 break;
1878 if (false_predicate_p (&p))
1879 gcc_assert (!bb->aux);
1880 else
1882 if (!bb->aux)
1884 done = false;
1885 bb->aux = pool_alloc (edge_predicate_pool);
1886 *((struct predicate *) bb->aux) = p;
1888 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1890 /* This OR operation is needed to ensure monotonous data flow
1891 in the case we hit the limit on number of clauses and the
1892 and/or operations above give approximate answers. */
1893 p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
1894 if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1896 done = false;
1897 *((struct predicate *) bb->aux) = p;
1906 /* We keep info about constantness of SSA names. */
1908 typedef struct predicate predicate_t;
1909 /* Return predicate specifying when the STMT might have result that is not
1910 a compile time constant. */
1912 static struct predicate
1913 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1914 struct inline_summary *summary,
1915 tree expr,
1916 vec<predicate_t> nonconstant_names)
1918 tree parm;
1919 int index;
1921 while (UNARY_CLASS_P (expr))
1922 expr = TREE_OPERAND (expr, 0);
1924 parm = unmodified_parm (NULL, expr);
1925 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1926 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1927 if (is_gimple_min_invariant (expr))
1928 return false_predicate ();
1929 if (TREE_CODE (expr) == SSA_NAME)
1930 return nonconstant_names[SSA_NAME_VERSION (expr)];
1931 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
1933 struct predicate p1 = will_be_nonconstant_expr_predicate
1934 (info, summary, TREE_OPERAND (expr, 0),
1935 nonconstant_names);
1936 struct predicate p2;
1937 if (true_predicate_p (&p1))
1938 return p1;
1939 p2 = will_be_nonconstant_expr_predicate (info, summary,
1940 TREE_OPERAND (expr, 1),
1941 nonconstant_names);
1942 return or_predicates (summary->conds, &p1, &p2);
1944 else if (TREE_CODE (expr) == COND_EXPR)
1946 struct predicate p1 = will_be_nonconstant_expr_predicate
1947 (info, summary, TREE_OPERAND (expr, 0),
1948 nonconstant_names);
1949 struct predicate p2;
1950 if (true_predicate_p (&p1))
1951 return p1;
1952 p2 = will_be_nonconstant_expr_predicate (info, summary,
1953 TREE_OPERAND (expr, 1),
1954 nonconstant_names);
1955 if (true_predicate_p (&p2))
1956 return p2;
1957 p1 = or_predicates (summary->conds, &p1, &p2);
1958 p2 = will_be_nonconstant_expr_predicate (info, summary,
1959 TREE_OPERAND (expr, 2),
1960 nonconstant_names);
1961 return or_predicates (summary->conds, &p1, &p2);
1963 else
1965 debug_tree (expr);
1966 gcc_unreachable ();
1968 return false_predicate ();
1972 /* Return predicate specifying when the STMT might have result that is not
1973 a compile time constant. */
1975 static struct predicate
1976 will_be_nonconstant_predicate (struct ipa_node_params *info,
1977 struct inline_summary *summary,
1978 gimple stmt,
1979 vec<predicate_t> nonconstant_names)
1981 struct predicate p = true_predicate ();
1982 ssa_op_iter iter;
1983 tree use;
1984 struct predicate op_non_const;
1985 bool is_load;
1986 int base_index;
1987 struct agg_position_info aggpos;
1989 /* What statments might be optimized away
1990 when their arguments are constant
1991 TODO: also trivial builtins.
1992 builtin_constant_p is already handled later. */
1993 if (gimple_code (stmt) != GIMPLE_ASSIGN
1994 && gimple_code (stmt) != GIMPLE_COND
1995 && gimple_code (stmt) != GIMPLE_SWITCH)
1996 return p;
1998 /* Stores will stay anyway. */
1999 if (gimple_store_p (stmt))
2000 return p;
2002 is_load = gimple_assign_load_p (stmt);
2004 /* Loads can be optimized when the value is known. */
2005 if (is_load)
2007 tree op;
2008 gcc_assert (gimple_assign_single_p (stmt));
2009 op = gimple_assign_rhs1 (stmt);
2010 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
2011 &aggpos))
2012 return p;
2014 else
2015 base_index = -1;
2017 /* See if we understand all operands before we start
2018 adding conditionals. */
2019 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2021 tree parm = unmodified_parm (stmt, use);
2022 /* For arguments we can build a condition. */
2023 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2024 continue;
2025 if (TREE_CODE (use) != SSA_NAME)
2026 return p;
2027 /* If we know when operand is constant,
2028 we still can say something useful. */
2029 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2030 continue;
2031 return p;
2034 if (is_load)
2035 op_non_const =
2036 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2037 else
2038 op_non_const = false_predicate ();
2039 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2041 tree parm = unmodified_parm (stmt, use);
2042 int index;
2044 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2046 if (index != base_index)
2047 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2048 else
2049 continue;
2051 else
2052 p = nonconstant_names[SSA_NAME_VERSION (use)];
2053 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2055 if (gimple_code (stmt) == GIMPLE_ASSIGN
2056 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2057 nonconstant_names[SSA_NAME_VERSION (gimple_assign_lhs (stmt))]
2058 = op_non_const;
2059 return op_non_const;
2062 struct record_modified_bb_info
2064 bitmap bb_set;
2065 gimple stmt;
2068 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2069 set except for info->stmt. */
2071 static bool
2072 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2074 struct record_modified_bb_info *info =
2075 (struct record_modified_bb_info *) data;
2076 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2077 return false;
2078 bitmap_set_bit (info->bb_set,
2079 SSA_NAME_IS_DEFAULT_DEF (vdef)
2080 ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2081 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2082 return false;
2085 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2086 will change since last invocation of STMT.
2088 Value 0 is reserved for compile time invariants.
2089 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2090 ought to be REG_BR_PROB_BASE / estimated_iters. */
2092 static int
2093 param_change_prob (gimple stmt, int i)
2095 tree op = gimple_call_arg (stmt, i);
2096 basic_block bb = gimple_bb (stmt);
2097 tree base;
2099 /* Global invariants neve change. */
2100 if (is_gimple_min_invariant (op))
2101 return 0;
2102 /* We would have to do non-trivial analysis to really work out what
2103 is the probability of value to change (i.e. when init statement
2104 is in a sibling loop of the call).
2106 We do an conservative estimate: when call is executed N times more often
2107 than the statement defining value, we take the frequency 1/N. */
2108 if (TREE_CODE (op) == SSA_NAME)
2110 int init_freq;
2112 if (!bb->frequency)
2113 return REG_BR_PROB_BASE;
2115 if (SSA_NAME_IS_DEFAULT_DEF (op))
2116 init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2117 else
2118 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2120 if (!init_freq)
2121 init_freq = 1;
2122 if (init_freq < bb->frequency)
2123 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2124 else
2125 return REG_BR_PROB_BASE;
2128 base = get_base_address (op);
2129 if (base)
2131 ao_ref refd;
2132 int max;
2133 struct record_modified_bb_info info;
2134 bitmap_iterator bi;
2135 unsigned index;
2136 tree init = ctor_for_folding (base);
2138 if (init != error_mark_node)
2139 return 0;
2140 if (!bb->frequency)
2141 return REG_BR_PROB_BASE;
2142 ao_ref_init (&refd, op);
2143 info.stmt = stmt;
2144 info.bb_set = BITMAP_ALLOC (NULL);
2145 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2146 NULL);
2147 if (bitmap_bit_p (info.bb_set, bb->index))
2149 BITMAP_FREE (info.bb_set);
2150 return REG_BR_PROB_BASE;
2153 /* Assume that every memory is initialized at entry.
2154 TODO: Can we easilly determine if value is always defined
2155 and thus we may skip entry block? */
2156 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
2157 max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2158 else
2159 max = 1;
2161 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2162 max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
2164 BITMAP_FREE (info.bb_set);
2165 if (max < bb->frequency)
2166 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2167 else
2168 return REG_BR_PROB_BASE;
2170 return REG_BR_PROB_BASE;
2173 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2174 sub-graph and if the predicate the condition depends on is known. If so,
2175 return true and store the pointer the predicate in *P. */
2177 static bool
2178 phi_result_unknown_predicate (struct ipa_node_params *info,
2179 struct inline_summary *summary, basic_block bb,
2180 struct predicate *p,
2181 vec<predicate_t> nonconstant_names)
2183 edge e;
2184 edge_iterator ei;
2185 basic_block first_bb = NULL;
2186 gimple stmt;
2188 if (single_pred_p (bb))
2190 *p = false_predicate ();
2191 return true;
2194 FOR_EACH_EDGE (e, ei, bb->preds)
2196 if (single_succ_p (e->src))
2198 if (!single_pred_p (e->src))
2199 return false;
2200 if (!first_bb)
2201 first_bb = single_pred (e->src);
2202 else if (single_pred (e->src) != first_bb)
2203 return false;
2205 else
2207 if (!first_bb)
2208 first_bb = e->src;
2209 else if (e->src != first_bb)
2210 return false;
2214 if (!first_bb)
2215 return false;
2217 stmt = last_stmt (first_bb);
2218 if (!stmt
2219 || gimple_code (stmt) != GIMPLE_COND
2220 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2221 return false;
2223 *p = will_be_nonconstant_expr_predicate (info, summary,
2224 gimple_cond_lhs (stmt),
2225 nonconstant_names);
2226 if (true_predicate_p (p))
2227 return false;
2228 else
2229 return true;
2232 /* Given a PHI statement in a function described by inline properties SUMMARY
2233 and *P being the predicate describing whether the selected PHI argument is
2234 known, store a predicate for the result of the PHI statement into
2235 NONCONSTANT_NAMES, if possible. */
2237 static void
2238 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2239 struct predicate *p,
2240 vec<predicate_t> nonconstant_names)
2242 unsigned i;
2244 for (i = 0; i < gimple_phi_num_args (phi); i++)
2246 tree arg = gimple_phi_arg (phi, i)->def;
2247 if (!is_gimple_min_invariant (arg))
2249 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2250 *p = or_predicates (summary->conds, p,
2251 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2252 if (true_predicate_p (p))
2253 return;
2257 if (dump_file && (dump_flags & TDF_DETAILS))
2259 fprintf (dump_file, "\t\tphi predicate: ");
2260 dump_predicate (dump_file, summary->conds, p);
2262 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2265 /* Return predicate specifying when array index in access OP becomes non-constant. */
2267 static struct predicate
2268 array_index_predicate (struct inline_summary *info,
2269 vec< predicate_t> nonconstant_names, tree op)
2271 struct predicate p = false_predicate ();
2272 while (handled_component_p (op))
2274 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2276 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2277 p = or_predicates (info->conds, &p,
2278 &nonconstant_names[SSA_NAME_VERSION
2279 (TREE_OPERAND (op, 1))]);
2281 op = TREE_OPERAND (op, 0);
2283 return p;
2286 /* For a typical usage of __builtin_expect (a<b, 1), we
2287 may introduce an extra relation stmt:
2288 With the builtin, we have
2289 t1 = a <= b;
2290 t2 = (long int) t1;
2291 t3 = __builtin_expect (t2, 1);
2292 if (t3 != 0)
2293 goto ...
2294 Without the builtin, we have
2295 if (a<=b)
2296 goto...
2297 This affects the size/time estimation and may have
2298 an impact on the earlier inlining.
2299 Here find this pattern and fix it up later. */
2301 static gimple
2302 find_foldable_builtin_expect (basic_block bb)
2304 gimple_stmt_iterator bsi;
2306 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2308 gimple stmt = gsi_stmt (bsi);
2309 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2310 || (is_gimple_call (stmt)
2311 && gimple_call_internal_p (stmt)
2312 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
2314 tree var = gimple_call_lhs (stmt);
2315 tree arg = gimple_call_arg (stmt, 0);
2316 use_operand_p use_p;
2317 gimple use_stmt;
2318 bool match = false;
2319 bool done = false;
2321 if (!var || !arg)
2322 continue;
2323 gcc_assert (TREE_CODE (var) == SSA_NAME);
2325 while (TREE_CODE (arg) == SSA_NAME)
2327 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2328 if (!is_gimple_assign (stmt_tmp))
2329 break;
2330 switch (gimple_assign_rhs_code (stmt_tmp))
2332 case LT_EXPR:
2333 case LE_EXPR:
2334 case GT_EXPR:
2335 case GE_EXPR:
2336 case EQ_EXPR:
2337 case NE_EXPR:
2338 match = true;
2339 done = true;
2340 break;
2341 case NOP_EXPR:
2342 break;
2343 default:
2344 done = true;
2345 break;
2347 if (done)
2348 break;
2349 arg = gimple_assign_rhs1 (stmt_tmp);
2352 if (match && single_imm_use (var, &use_p, &use_stmt)
2353 && gimple_code (use_stmt) == GIMPLE_COND)
2354 return use_stmt;
2357 return NULL;
2360 /* Return true when the basic blocks contains only clobbers followed by RESX.
2361 Such BBs are kept around to make removal of dead stores possible with
2362 presence of EH and will be optimized out by optimize_clobbers later in the
2363 game.
2365 NEED_EH is used to recurse in case the clobber has non-EH predecestors
2366 that can be clobber only, too.. When it is false, the RESX is not necessary
2367 on the end of basic block. */
2369 static bool
2370 clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2372 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2373 edge_iterator ei;
2374 edge e;
2376 if (need_eh)
2378 if (gsi_end_p (gsi))
2379 return false;
2380 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2381 return false;
2382 gsi_prev (&gsi);
2384 else if (!single_succ_p (bb))
2385 return false;
2387 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2389 gimple stmt = gsi_stmt (gsi);
2390 if (is_gimple_debug (stmt))
2391 continue;
2392 if (gimple_clobber_p (stmt))
2393 continue;
2394 if (gimple_code (stmt) == GIMPLE_LABEL)
2395 break;
2396 return false;
2399 /* See if all predecestors are either throws or clobber only BBs. */
2400 FOR_EACH_EDGE (e, ei, bb->preds)
2401 if (!(e->flags & EDGE_EH)
2402 && !clobber_only_eh_bb_p (e->src, false))
2403 return false;
2405 return true;
2408 /* Compute function body size parameters for NODE.
2409 When EARLY is true, we compute only simple summaries without
2410 non-trivial predicates to drive the early inliner. */
2412 static void
2413 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2415 gcov_type time = 0;
2416 /* Estimate static overhead for function prologue/epilogue and alignment. */
2417 int size = 2;
2418 /* Benefits are scaled by probability of elimination that is in range
2419 <0,2>. */
2420 basic_block bb;
2421 gimple_stmt_iterator bsi;
2422 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2423 int freq;
2424 struct inline_summary *info = inline_summary (node);
2425 struct predicate bb_predicate;
2426 struct ipa_node_params *parms_info = NULL;
2427 vec<predicate_t> nonconstant_names = vNULL;
2428 int nblocks, n;
2429 int *order;
2430 predicate array_index = true_predicate ();
2431 gimple fix_builtin_expect_stmt;
2433 info->conds = NULL;
2434 info->entry = NULL;
2436 if (optimize && !early)
2438 calculate_dominance_info (CDI_DOMINATORS);
2439 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2441 if (ipa_node_params_vector.exists ())
2443 parms_info = IPA_NODE_REF (node);
2444 nonconstant_names.safe_grow_cleared
2445 (SSANAMES (my_function)->length ());
2449 if (dump_file)
2450 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2451 node->name ());
2453 /* When we run into maximal number of entries, we assign everything to the
2454 constant truth case. Be sure to have it in list. */
2455 bb_predicate = true_predicate ();
2456 account_size_time (info, 0, 0, &bb_predicate);
2458 bb_predicate = not_inlined_predicate ();
2459 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2461 gcc_assert (my_function && my_function->cfg);
2462 if (parms_info)
2463 compute_bb_predicates (node, parms_info, info);
2464 gcc_assert (cfun == my_function);
2465 order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2466 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2467 for (n = 0; n < nblocks; n++)
2469 bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2470 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2471 if (clobber_only_eh_bb_p (bb))
2473 if (dump_file && (dump_flags & TDF_DETAILS))
2474 fprintf (dump_file, "\n Ignoring BB %i;"
2475 " it will be optimized away by cleanup_clobbers\n",
2476 bb->index);
2477 continue;
2480 /* TODO: Obviously predicates can be propagated down across CFG. */
2481 if (parms_info)
2483 if (bb->aux)
2484 bb_predicate = *(struct predicate *) bb->aux;
2485 else
2486 bb_predicate = false_predicate ();
2488 else
2489 bb_predicate = true_predicate ();
2491 if (dump_file && (dump_flags & TDF_DETAILS))
2493 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2494 dump_predicate (dump_file, info->conds, &bb_predicate);
2497 if (parms_info && nonconstant_names.exists ())
2499 struct predicate phi_predicate;
2500 bool first_phi = true;
2502 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2504 if (first_phi
2505 && !phi_result_unknown_predicate (parms_info, info, bb,
2506 &phi_predicate,
2507 nonconstant_names))
2508 break;
2509 first_phi = false;
2510 if (dump_file && (dump_flags & TDF_DETAILS))
2512 fprintf (dump_file, " ");
2513 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2515 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2516 nonconstant_names);
2520 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2522 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2524 gimple stmt = gsi_stmt (bsi);
2525 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2526 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2527 int prob;
2528 struct predicate will_be_nonconstant;
2530 /* This relation stmt should be folded after we remove
2531 buildin_expect call. Adjust the cost here. */
2532 if (stmt == fix_builtin_expect_stmt)
2534 this_size--;
2535 this_time--;
2538 if (dump_file && (dump_flags & TDF_DETAILS))
2540 fprintf (dump_file, " ");
2541 print_gimple_stmt (dump_file, stmt, 0, 0);
2542 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2543 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2544 this_time);
2547 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2549 struct predicate this_array_index;
2550 this_array_index =
2551 array_index_predicate (info, nonconstant_names,
2552 gimple_assign_rhs1 (stmt));
2553 if (!false_predicate_p (&this_array_index))
2554 array_index =
2555 and_predicates (info->conds, &array_index,
2556 &this_array_index);
2558 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2560 struct predicate this_array_index;
2561 this_array_index =
2562 array_index_predicate (info, nonconstant_names,
2563 gimple_get_lhs (stmt));
2564 if (!false_predicate_p (&this_array_index))
2565 array_index =
2566 and_predicates (info->conds, &array_index,
2567 &this_array_index);
2571 if (is_gimple_call (stmt)
2572 && !gimple_call_internal_p (stmt))
2574 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2575 struct inline_edge_summary *es = inline_edge_summary (edge);
2577 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2578 resolved as constant. We however don't want to optimize
2579 out the cgraph edges. */
2580 if (nonconstant_names.exists ()
2581 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2582 && gimple_call_lhs (stmt)
2583 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2585 struct predicate false_p = false_predicate ();
2586 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2587 = false_p;
2589 if (ipa_node_params_vector.exists ())
2591 int count = gimple_call_num_args (stmt);
2592 int i;
2594 if (count)
2595 es->param.safe_grow_cleared (count);
2596 for (i = 0; i < count; i++)
2598 int prob = param_change_prob (stmt, i);
2599 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2600 es->param[i].change_prob = prob;
2604 es->call_stmt_size = this_size;
2605 es->call_stmt_time = this_time;
2606 es->loop_depth = bb_loop_depth (bb);
2607 edge_set_predicate (edge, &bb_predicate);
2610 /* TODO: When conditional jump or swithc is known to be constant, but
2611 we did not translate it into the predicates, we really can account
2612 just maximum of the possible paths. */
2613 if (parms_info)
2614 will_be_nonconstant
2615 = will_be_nonconstant_predicate (parms_info, info,
2616 stmt, nonconstant_names);
2617 if (this_time || this_size)
2619 struct predicate p;
2621 this_time *= freq;
2623 prob = eliminated_by_inlining_prob (stmt);
2624 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2625 fprintf (dump_file,
2626 "\t\t50%% will be eliminated by inlining\n");
2627 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2628 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2630 if (parms_info)
2631 p = and_predicates (info->conds, &bb_predicate,
2632 &will_be_nonconstant);
2633 else
2634 p = true_predicate ();
2636 if (!false_predicate_p (&p))
2638 time += this_time;
2639 size += this_size;
2640 if (time > MAX_TIME * INLINE_TIME_SCALE)
2641 time = MAX_TIME * INLINE_TIME_SCALE;
2644 /* We account everything but the calls. Calls have their own
2645 size/time info attached to cgraph edges. This is necessary
2646 in order to make the cost disappear after inlining. */
2647 if (!is_gimple_call (stmt))
2649 if (prob)
2651 struct predicate ip = not_inlined_predicate ();
2652 ip = and_predicates (info->conds, &ip, &p);
2653 account_size_time (info, this_size * prob,
2654 this_time * prob, &ip);
2656 if (prob != 2)
2657 account_size_time (info, this_size * (2 - prob),
2658 this_time * (2 - prob), &p);
2661 gcc_assert (time >= 0);
2662 gcc_assert (size >= 0);
2666 set_hint_predicate (&inline_summary (node)->array_index, array_index);
2667 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2668 if (time > MAX_TIME)
2669 time = MAX_TIME;
2670 free (order);
2672 if (!early && nonconstant_names.exists ())
2674 struct loop *loop;
2675 predicate loop_iterations = true_predicate ();
2676 predicate loop_stride = true_predicate ();
2678 if (dump_file && (dump_flags & TDF_DETAILS))
2679 flow_loops_dump (dump_file, NULL, 0);
2680 scev_initialize ();
2681 FOR_EACH_LOOP (loop, 0)
2683 vec<edge> exits;
2684 edge ex;
2685 unsigned int j, i;
2686 struct tree_niter_desc niter_desc;
2687 basic_block *body = get_loop_body (loop);
2688 bb_predicate = *(struct predicate *) loop->header->aux;
2690 exits = get_loop_exit_edges (loop);
2691 FOR_EACH_VEC_ELT (exits, j, ex)
2692 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2693 && !is_gimple_min_invariant (niter_desc.niter))
2695 predicate will_be_nonconstant
2696 = will_be_nonconstant_expr_predicate (parms_info, info,
2697 niter_desc.niter,
2698 nonconstant_names);
2699 if (!true_predicate_p (&will_be_nonconstant))
2700 will_be_nonconstant = and_predicates (info->conds,
2701 &bb_predicate,
2702 &will_be_nonconstant);
2703 if (!true_predicate_p (&will_be_nonconstant)
2704 && !false_predicate_p (&will_be_nonconstant))
2705 /* This is slightly inprecise. We may want to represent each
2706 loop with independent predicate. */
2707 loop_iterations =
2708 and_predicates (info->conds, &loop_iterations,
2709 &will_be_nonconstant);
2711 exits.release ();
2713 for (i = 0; i < loop->num_nodes; i++)
2715 gimple_stmt_iterator gsi;
2716 bb_predicate = *(struct predicate *) body[i]->aux;
2717 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2718 gsi_next (&gsi))
2720 gimple stmt = gsi_stmt (gsi);
2721 affine_iv iv;
2722 ssa_op_iter iter;
2723 tree use;
2725 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2727 predicate will_be_nonconstant;
2729 if (!simple_iv
2730 (loop, loop_containing_stmt (stmt), use, &iv, true)
2731 || is_gimple_min_invariant (iv.step))
2732 continue;
2733 will_be_nonconstant
2734 = will_be_nonconstant_expr_predicate (parms_info, info,
2735 iv.step,
2736 nonconstant_names);
2737 if (!true_predicate_p (&will_be_nonconstant))
2738 will_be_nonconstant
2739 = and_predicates (info->conds,
2740 &bb_predicate,
2741 &will_be_nonconstant);
2742 if (!true_predicate_p (&will_be_nonconstant)
2743 && !false_predicate_p (&will_be_nonconstant))
2744 /* This is slightly inprecise. We may want to represent
2745 each loop with independent predicate. */
2746 loop_stride =
2747 and_predicates (info->conds, &loop_stride,
2748 &will_be_nonconstant);
2752 free (body);
2754 set_hint_predicate (&inline_summary (node)->loop_iterations,
2755 loop_iterations);
2756 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2757 scev_finalize ();
2759 FOR_ALL_BB_FN (bb, my_function)
2761 edge e;
2762 edge_iterator ei;
2764 if (bb->aux)
2765 pool_free (edge_predicate_pool, bb->aux);
2766 bb->aux = NULL;
2767 FOR_EACH_EDGE (e, ei, bb->succs)
2769 if (e->aux)
2770 pool_free (edge_predicate_pool, e->aux);
2771 e->aux = NULL;
2774 inline_summary (node)->self_time = time;
2775 inline_summary (node)->self_size = size;
2776 nonconstant_names.release ();
2777 if (optimize && !early)
2779 loop_optimizer_finalize ();
2780 free_dominance_info (CDI_DOMINATORS);
2782 if (dump_file)
2784 fprintf (dump_file, "\n");
2785 dump_inline_summary (dump_file, node);
2790 /* Compute parameters of functions used by inliner.
2791 EARLY is true when we compute parameters for the early inliner */
2793 void
2794 compute_inline_parameters (struct cgraph_node *node, bool early)
2796 HOST_WIDE_INT self_stack_size;
2797 struct cgraph_edge *e;
2798 struct inline_summary *info;
2800 gcc_assert (!node->global.inlined_to);
2802 inline_summary_alloc ();
2804 info = inline_summary (node);
2805 reset_inline_summary (node);
2807 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2808 Once this happen, we will need to more curefully predict call
2809 statement size. */
2810 if (node->thunk.thunk_p)
2812 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2813 struct predicate t = true_predicate ();
2815 info->inlinable = 0;
2816 node->callees->call_stmt_cannot_inline_p = true;
2817 node->local.can_change_signature = false;
2818 es->call_stmt_time = 1;
2819 es->call_stmt_size = 1;
2820 account_size_time (info, 0, 0, &t);
2821 return;
2824 /* Even is_gimple_min_invariant rely on current_function_decl. */
2825 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2827 /* Estimate the stack size for the function if we're optimizing. */
2828 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2829 info->estimated_self_stack_size = self_stack_size;
2830 info->estimated_stack_size = self_stack_size;
2831 info->stack_frame_offset = 0;
2833 /* Can this function be inlined at all? */
2834 if (!optimize && !lookup_attribute ("always_inline",
2835 DECL_ATTRIBUTES (node->decl)))
2836 info->inlinable = false;
2837 else
2838 info->inlinable = tree_inlinable_function_p (node->decl);
2840 /* Type attributes can use parameter indices to describe them. */
2841 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2842 node->local.can_change_signature = false;
2843 else
2845 /* Otherwise, inlinable functions always can change signature. */
2846 if (info->inlinable)
2847 node->local.can_change_signature = true;
2848 else
2850 /* Functions calling builtin_apply can not change signature. */
2851 for (e = node->callees; e; e = e->next_callee)
2853 tree cdecl = e->callee->decl;
2854 if (DECL_BUILT_IN (cdecl)
2855 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2856 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2857 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2858 break;
2860 node->local.can_change_signature = !e;
2863 estimate_function_body_sizes (node, early);
2865 for (e = node->callees; e; e = e->next_callee)
2866 if (symtab_comdat_local_p (e->callee))
2867 break;
2868 node->calls_comdat_local = (e != NULL);
2870 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2871 info->time = info->self_time;
2872 info->size = info->self_size;
2873 info->stack_frame_offset = 0;
2874 info->estimated_stack_size = info->estimated_self_stack_size;
2875 #ifdef ENABLE_CHECKING
2876 inline_update_overall_summary (node);
2877 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2878 #endif
2880 pop_cfun ();
2884 /* Compute parameters of functions used by inliner using
2885 current_function_decl. */
2887 static unsigned int
2888 compute_inline_parameters_for_current (void)
2890 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2891 return 0;
2894 namespace {
2896 const pass_data pass_data_inline_parameters =
2898 GIMPLE_PASS, /* type */
2899 "inline_param", /* name */
2900 OPTGROUP_INLINE, /* optinfo_flags */
2901 false, /* has_gate */
2902 true, /* has_execute */
2903 TV_INLINE_PARAMETERS, /* tv_id */
2904 0, /* properties_required */
2905 0, /* properties_provided */
2906 0, /* properties_destroyed */
2907 0, /* todo_flags_start */
2908 0, /* todo_flags_finish */
2911 class pass_inline_parameters : public gimple_opt_pass
2913 public:
2914 pass_inline_parameters (gcc::context *ctxt)
2915 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
2918 /* opt_pass methods: */
2919 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
2920 unsigned int execute () {
2921 return compute_inline_parameters_for_current ();
2924 }; // class pass_inline_parameters
2926 } // anon namespace
2928 gimple_opt_pass *
2929 make_pass_inline_parameters (gcc::context *ctxt)
2931 return new pass_inline_parameters (ctxt);
2935 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2936 KNOWN_BINFOS. */
2938 static bool
2939 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2940 int *size, int *time,
2941 vec<tree> known_vals,
2942 vec<tree> known_binfos,
2943 vec<ipa_agg_jump_function_p> known_aggs)
2945 tree target;
2946 struct cgraph_node *callee;
2947 struct inline_summary *isummary;
2949 if (!known_vals.exists () && !known_binfos.exists ())
2950 return false;
2951 if (!flag_indirect_inlining)
2952 return false;
2954 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2955 known_aggs);
2956 if (!target)
2957 return false;
2959 /* Account for difference in cost between indirect and direct calls. */
2960 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
2961 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
2962 gcc_checking_assert (*time >= 0);
2963 gcc_checking_assert (*size >= 0);
2965 callee = cgraph_get_node (target);
2966 if (!callee || !callee->definition)
2967 return false;
2968 isummary = inline_summary (callee);
2969 return isummary->inlinable;
2972 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2974 static inline void
2975 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2976 int prob,
2977 vec<tree> known_vals,
2978 vec<tree> known_binfos,
2979 vec<ipa_agg_jump_function_p> known_aggs,
2980 inline_hints *hints)
2982 struct inline_edge_summary *es = inline_edge_summary (e);
2983 int call_size = es->call_stmt_size;
2984 int call_time = es->call_stmt_time;
2985 if (!e->callee
2986 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
2987 known_vals, known_binfos, known_aggs)
2988 && hints && cgraph_maybe_hot_edge_p (e))
2989 *hints |= INLINE_HINT_indirect_call;
2990 *size += call_size * INLINE_SIZE_SCALE;
2991 *time += apply_probability ((gcov_type) call_time, prob)
2992 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
2993 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2994 *time = MAX_TIME * INLINE_TIME_SCALE;
2999 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.
3000 POSSIBLE_TRUTHS, KNOWN_VALS and KNOWN_BINFOS describe context of the call
3001 site. */
3003 static void
3004 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
3005 inline_hints *hints,
3006 clause_t possible_truths,
3007 vec<tree> known_vals,
3008 vec<tree> known_binfos,
3009 vec<ipa_agg_jump_function_p> known_aggs)
3011 struct cgraph_edge *e;
3012 for (e = node->callees; e; e = e->next_callee)
3014 struct inline_edge_summary *es = inline_edge_summary (e);
3015 if (!es->predicate
3016 || evaluate_predicate (es->predicate, possible_truths))
3018 if (e->inline_failed)
3020 /* Predicates of calls shall not use NOT_CHANGED codes,
3021 sowe do not need to compute probabilities. */
3022 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
3023 known_vals, known_binfos,
3024 known_aggs, hints);
3026 else
3027 estimate_calls_size_and_time (e->callee, size, time, hints,
3028 possible_truths,
3029 known_vals, known_binfos,
3030 known_aggs);
3033 for (e = node->indirect_calls; e; e = e->next_callee)
3035 struct inline_edge_summary *es = inline_edge_summary (e);
3036 if (!es->predicate
3037 || evaluate_predicate (es->predicate, possible_truths))
3038 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
3039 known_vals, known_binfos, known_aggs,
3040 hints);
3045 /* Estimate size and time needed to execute NODE assuming
3046 POSSIBLE_TRUTHS clause, and KNOWN_VALS and KNOWN_BINFOS information
3047 about NODE's arguments. */
3049 static void
3050 estimate_node_size_and_time (struct cgraph_node *node,
3051 clause_t possible_truths,
3052 vec<tree> known_vals,
3053 vec<tree> known_binfos,
3054 vec<ipa_agg_jump_function_p> known_aggs,
3055 int *ret_size, int *ret_time,
3056 inline_hints *ret_hints,
3057 vec<inline_param_summary>
3058 inline_param_summary)
3060 struct inline_summary *info = inline_summary (node);
3061 size_time_entry *e;
3062 int size = 0;
3063 int time = 0;
3064 inline_hints hints = 0;
3065 int i;
3067 if (dump_file && (dump_flags & TDF_DETAILS))
3069 bool found = false;
3070 fprintf (dump_file, " Estimating body: %s/%i\n"
3071 " Known to be false: ", node->name (),
3072 node->order);
3074 for (i = predicate_not_inlined_condition;
3075 i < (predicate_first_dynamic_condition
3076 + (int) vec_safe_length (info->conds)); i++)
3077 if (!(possible_truths & (1 << i)))
3079 if (found)
3080 fprintf (dump_file, ", ");
3081 found = true;
3082 dump_condition (dump_file, info->conds, i);
3086 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3087 if (evaluate_predicate (&e->predicate, possible_truths))
3089 size += e->size;
3090 gcc_checking_assert (e->time >= 0);
3091 gcc_checking_assert (time >= 0);
3092 if (!inline_param_summary.exists ())
3093 time += e->time;
3094 else
3096 int prob = predicate_probability (info->conds,
3097 &e->predicate,
3098 possible_truths,
3099 inline_param_summary);
3100 gcc_checking_assert (prob >= 0);
3101 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3102 time += apply_probability ((gcov_type) e->time, prob);
3104 if (time > MAX_TIME * INLINE_TIME_SCALE)
3105 time = MAX_TIME * INLINE_TIME_SCALE;
3106 gcc_checking_assert (time >= 0);
3109 gcc_checking_assert (size >= 0);
3110 gcc_checking_assert (time >= 0);
3112 if (info->loop_iterations
3113 && !evaluate_predicate (info->loop_iterations, possible_truths))
3114 hints |= INLINE_HINT_loop_iterations;
3115 if (info->loop_stride
3116 && !evaluate_predicate (info->loop_stride, possible_truths))
3117 hints |= INLINE_HINT_loop_stride;
3118 if (info->array_index
3119 && !evaluate_predicate (info->array_index, possible_truths))
3120 hints |= INLINE_HINT_array_index;
3121 if (info->scc_no)
3122 hints |= INLINE_HINT_in_scc;
3123 if (DECL_DECLARED_INLINE_P (node->decl))
3124 hints |= INLINE_HINT_declared_inline;
3126 estimate_calls_size_and_time (node, &size, &time, &hints, possible_truths,
3127 known_vals, known_binfos, known_aggs);
3128 gcc_checking_assert (size >= 0);
3129 gcc_checking_assert (time >= 0);
3130 time = RDIV (time, INLINE_TIME_SCALE);
3131 size = RDIV (size, INLINE_SIZE_SCALE);
3133 if (dump_file && (dump_flags & TDF_DETAILS))
3134 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3135 if (ret_time)
3136 *ret_time = time;
3137 if (ret_size)
3138 *ret_size = size;
3139 if (ret_hints)
3140 *ret_hints = hints;
3141 return;
3145 /* Estimate size and time needed to execute callee of EDGE assuming that
3146 parameters known to be constant at caller of EDGE are propagated.
3147 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
3148 and types for parameters. */
3150 void
3151 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3152 vec<tree> known_vals,
3153 vec<tree> known_binfos,
3154 vec<ipa_agg_jump_function_p> known_aggs,
3155 int *ret_size, int *ret_time,
3156 inline_hints *hints)
3158 clause_t clause;
3160 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3161 known_aggs);
3162 estimate_node_size_and_time (node, clause, known_vals, known_binfos,
3163 known_aggs, ret_size, ret_time, hints, vNULL);
3166 /* Translate all conditions from callee representation into caller
3167 representation and symbolically evaluate predicate P into new predicate.
3169 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3170 is summary of function predicate P is from. OPERAND_MAP is array giving
3171 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3172 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3173 predicate under which callee is executed. OFFSET_MAP is an array of of
3174 offsets that need to be added to conditions, negative offset means that
3175 conditions relying on values passed by reference have to be discarded
3176 because they might not be preserved (and should be considered offset zero
3177 for other purposes). */
3179 static struct predicate
3180 remap_predicate (struct inline_summary *info,
3181 struct inline_summary *callee_info,
3182 struct predicate *p,
3183 vec<int> operand_map,
3184 vec<int> offset_map,
3185 clause_t possible_truths, struct predicate *toplev_predicate)
3187 int i;
3188 struct predicate out = true_predicate ();
3190 /* True predicate is easy. */
3191 if (true_predicate_p (p))
3192 return *toplev_predicate;
3193 for (i = 0; p->clause[i]; i++)
3195 clause_t clause = p->clause[i];
3196 int cond;
3197 struct predicate clause_predicate = false_predicate ();
3199 gcc_assert (i < MAX_CLAUSES);
3201 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3202 /* Do we have condition we can't disprove? */
3203 if (clause & possible_truths & (1 << cond))
3205 struct predicate cond_predicate;
3206 /* Work out if the condition can translate to predicate in the
3207 inlined function. */
3208 if (cond >= predicate_first_dynamic_condition)
3210 struct condition *c;
3212 c = &(*callee_info->conds)[cond
3214 predicate_first_dynamic_condition];
3215 /* See if we can remap condition operand to caller's operand.
3216 Otherwise give up. */
3217 if (!operand_map.exists ()
3218 || (int) operand_map.length () <= c->operand_num
3219 || operand_map[c->operand_num] == -1
3220 /* TODO: For non-aggregate conditions, adding an offset is
3221 basically an arithmetic jump function processing which
3222 we should support in future. */
3223 || ((!c->agg_contents || !c->by_ref)
3224 && offset_map[c->operand_num] > 0)
3225 || (c->agg_contents && c->by_ref
3226 && offset_map[c->operand_num] < 0))
3227 cond_predicate = true_predicate ();
3228 else
3230 struct agg_position_info ap;
3231 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3232 if (offset_delta < 0)
3234 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3235 offset_delta = 0;
3237 gcc_assert (!c->agg_contents
3238 || c->by_ref || offset_delta == 0);
3239 ap.offset = c->offset + offset_delta;
3240 ap.agg_contents = c->agg_contents;
3241 ap.by_ref = c->by_ref;
3242 cond_predicate = add_condition (info,
3243 operand_map[c->operand_num],
3244 &ap, c->code, c->val);
3247 /* Fixed conditions remains same, construct single
3248 condition predicate. */
3249 else
3251 cond_predicate.clause[0] = 1 << cond;
3252 cond_predicate.clause[1] = 0;
3254 clause_predicate = or_predicates (info->conds, &clause_predicate,
3255 &cond_predicate);
3257 out = and_predicates (info->conds, &out, &clause_predicate);
3259 return and_predicates (info->conds, &out, toplev_predicate);
3263 /* Update summary information of inline clones after inlining.
3264 Compute peak stack usage. */
3266 static void
3267 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3269 struct cgraph_edge *e;
3270 struct inline_summary *callee_info = inline_summary (node);
3271 struct inline_summary *caller_info = inline_summary (node->callers->caller);
3272 HOST_WIDE_INT peak;
3274 callee_info->stack_frame_offset
3275 = caller_info->stack_frame_offset
3276 + caller_info->estimated_self_stack_size;
3277 peak = callee_info->stack_frame_offset
3278 + callee_info->estimated_self_stack_size;
3279 if (inline_summary (node->global.inlined_to)->estimated_stack_size < peak)
3280 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
3281 ipa_propagate_frequency (node);
3282 for (e = node->callees; e; e = e->next_callee)
3284 if (!e->inline_failed)
3285 inline_update_callee_summaries (e->callee, depth);
3286 inline_edge_summary (e)->loop_depth += depth;
3288 for (e = node->indirect_calls; e; e = e->next_callee)
3289 inline_edge_summary (e)->loop_depth += depth;
3292 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3293 When functoin A is inlined in B and A calls C with parameter that
3294 changes with probability PROB1 and C is known to be passthroug
3295 of argument if B that change with probability PROB2, the probability
3296 of change is now PROB1*PROB2. */
3298 static void
3299 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3300 struct cgraph_edge *edge)
3302 if (ipa_node_params_vector.exists ())
3304 int i;
3305 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3306 struct inline_edge_summary *es = inline_edge_summary (edge);
3307 struct inline_edge_summary *inlined_es
3308 = inline_edge_summary (inlined_edge);
3310 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3312 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3313 if (jfunc->type == IPA_JF_PASS_THROUGH
3314 && (ipa_get_jf_pass_through_formal_id (jfunc)
3315 < (int) inlined_es->param.length ()))
3317 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3318 int prob1 = es->param[i].change_prob;
3319 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3320 int prob = combine_probabilities (prob1, prob2);
3322 if (prob1 && prob2 && !prob)
3323 prob = 1;
3325 es->param[i].change_prob = prob;
3331 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3333 Remap predicates of callees of NODE. Rest of arguments match
3334 remap_predicate.
3336 Also update change probabilities. */
3338 static void
3339 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3340 struct cgraph_node *node,
3341 struct inline_summary *info,
3342 struct inline_summary *callee_info,
3343 vec<int> operand_map,
3344 vec<int> offset_map,
3345 clause_t possible_truths,
3346 struct predicate *toplev_predicate)
3348 struct cgraph_edge *e;
3349 for (e = node->callees; e; e = e->next_callee)
3351 struct inline_edge_summary *es = inline_edge_summary (e);
3352 struct predicate p;
3354 if (e->inline_failed)
3356 remap_edge_change_prob (inlined_edge, e);
3358 if (es->predicate)
3360 p = remap_predicate (info, callee_info,
3361 es->predicate, operand_map, offset_map,
3362 possible_truths, toplev_predicate);
3363 edge_set_predicate (e, &p);
3364 /* TODO: We should remove the edge for code that will be
3365 optimized out, but we need to keep verifiers and tree-inline
3366 happy. Make it cold for now. */
3367 if (false_predicate_p (&p))
3369 e->count = 0;
3370 e->frequency = 0;
3373 else
3374 edge_set_predicate (e, toplev_predicate);
3376 else
3377 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3378 operand_map, offset_map, possible_truths,
3379 toplev_predicate);
3381 for (e = node->indirect_calls; e; e = e->next_callee)
3383 struct inline_edge_summary *es = inline_edge_summary (e);
3384 struct predicate p;
3386 remap_edge_change_prob (inlined_edge, e);
3387 if (es->predicate)
3389 p = remap_predicate (info, callee_info,
3390 es->predicate, operand_map, offset_map,
3391 possible_truths, toplev_predicate);
3392 edge_set_predicate (e, &p);
3393 /* TODO: We should remove the edge for code that will be optimized
3394 out, but we need to keep verifiers and tree-inline happy.
3395 Make it cold for now. */
3396 if (false_predicate_p (&p))
3398 e->count = 0;
3399 e->frequency = 0;
3402 else
3403 edge_set_predicate (e, toplev_predicate);
3407 /* Same as remap_predicate, but set result into hint *HINT. */
3409 static void
3410 remap_hint_predicate (struct inline_summary *info,
3411 struct inline_summary *callee_info,
3412 struct predicate **hint,
3413 vec<int> operand_map,
3414 vec<int> offset_map,
3415 clause_t possible_truths,
3416 struct predicate *toplev_predicate)
3418 predicate p;
3420 if (!*hint)
3421 return;
3422 p = remap_predicate (info, callee_info,
3423 *hint,
3424 operand_map, offset_map,
3425 possible_truths, toplev_predicate);
3426 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3428 if (!*hint)
3429 set_hint_predicate (hint, p);
3430 else
3431 **hint = and_predicates (info->conds, *hint, &p);
3435 /* We inlined EDGE. Update summary of the function we inlined into. */
3437 void
3438 inline_merge_summary (struct cgraph_edge *edge)
3440 struct inline_summary *callee_info = inline_summary (edge->callee);
3441 struct cgraph_node *to = (edge->caller->global.inlined_to
3442 ? edge->caller->global.inlined_to : edge->caller);
3443 struct inline_summary *info = inline_summary (to);
3444 clause_t clause = 0; /* not_inline is known to be false. */
3445 size_time_entry *e;
3446 vec<int> operand_map = vNULL;
3447 vec<int> offset_map = vNULL;
3448 int i;
3449 struct predicate toplev_predicate;
3450 struct predicate true_p = true_predicate ();
3451 struct inline_edge_summary *es = inline_edge_summary (edge);
3453 if (es->predicate)
3454 toplev_predicate = *es->predicate;
3455 else
3456 toplev_predicate = true_predicate ();
3458 if (ipa_node_params_vector.exists () && callee_info->conds)
3460 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3461 int count = ipa_get_cs_argument_count (args);
3462 int i;
3464 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3465 if (count)
3467 operand_map.safe_grow_cleared (count);
3468 offset_map.safe_grow_cleared (count);
3470 for (i = 0; i < count; i++)
3472 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3473 int map = -1;
3475 /* TODO: handle non-NOPs when merging. */
3476 if (jfunc->type == IPA_JF_PASS_THROUGH)
3478 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3479 map = ipa_get_jf_pass_through_formal_id (jfunc);
3480 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3481 offset_map[i] = -1;
3483 else if (jfunc->type == IPA_JF_ANCESTOR)
3485 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3486 if (offset >= 0 && offset < INT_MAX)
3488 map = ipa_get_jf_ancestor_formal_id (jfunc);
3489 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3490 offset = -1;
3491 offset_map[i] = offset;
3494 operand_map[i] = map;
3495 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3498 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3500 struct predicate p = remap_predicate (info, callee_info,
3501 &e->predicate, operand_map,
3502 offset_map, clause,
3503 &toplev_predicate);
3504 if (!false_predicate_p (&p))
3506 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3507 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3508 int prob = predicate_probability (callee_info->conds,
3509 &e->predicate,
3510 clause, es->param);
3511 add_time = apply_probability ((gcov_type) add_time, prob);
3512 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3513 add_time = MAX_TIME * INLINE_TIME_SCALE;
3514 if (prob != REG_BR_PROB_BASE
3515 && dump_file && (dump_flags & TDF_DETAILS))
3517 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3518 (double) prob / REG_BR_PROB_BASE);
3520 account_size_time (info, e->size, add_time, &p);
3523 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3524 offset_map, clause, &toplev_predicate);
3525 remap_hint_predicate (info, callee_info,
3526 &callee_info->loop_iterations,
3527 operand_map, offset_map, clause, &toplev_predicate);
3528 remap_hint_predicate (info, callee_info,
3529 &callee_info->loop_stride,
3530 operand_map, offset_map, clause, &toplev_predicate);
3531 remap_hint_predicate (info, callee_info,
3532 &callee_info->array_index,
3533 operand_map, offset_map, clause, &toplev_predicate);
3535 inline_update_callee_summaries (edge->callee,
3536 inline_edge_summary (edge)->loop_depth);
3538 /* We do not maintain predicates of inlined edges, free it. */
3539 edge_set_predicate (edge, &true_p);
3540 /* Similarly remove param summaries. */
3541 es->param.release ();
3542 operand_map.release ();
3543 offset_map.release ();
3546 /* For performance reasons inline_merge_summary is not updating overall size
3547 and time. Recompute it. */
3549 void
3550 inline_update_overall_summary (struct cgraph_node *node)
3552 struct inline_summary *info = inline_summary (node);
3553 size_time_entry *e;
3554 int i;
3556 info->size = 0;
3557 info->time = 0;
3558 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3560 info->size += e->size, info->time += e->time;
3561 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3562 info->time = MAX_TIME * INLINE_TIME_SCALE;
3564 estimate_calls_size_and_time (node, &info->size, &info->time, NULL,
3565 ~(clause_t) (1 << predicate_false_condition),
3566 vNULL, vNULL, vNULL);
3567 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3568 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3571 /* Return hints derrived from EDGE. */
3573 simple_edge_hints (struct cgraph_edge *edge)
3575 int hints = 0;
3576 struct cgraph_node *to = (edge->caller->global.inlined_to
3577 ? edge->caller->global.inlined_to : edge->caller);
3578 if (inline_summary (to)->scc_no
3579 && inline_summary (to)->scc_no == inline_summary (edge->callee)->scc_no
3580 && !cgraph_edge_recursive_p (edge))
3581 hints |= INLINE_HINT_same_scc;
3583 if (to->lto_file_data && edge->callee->lto_file_data
3584 && to->lto_file_data != edge->callee->lto_file_data)
3585 hints |= INLINE_HINT_cross_module;
3587 return hints;
3590 /* Estimate the time cost for the caller when inlining EDGE.
3591 Only to be called via estimate_edge_time, that handles the
3592 caching mechanism.
3594 When caching, also update the cache entry. Compute both time and
3595 size, since we always need both metrics eventually. */
3598 do_estimate_edge_time (struct cgraph_edge *edge)
3600 int time;
3601 int size;
3602 inline_hints hints;
3603 struct cgraph_node *callee;
3604 clause_t clause;
3605 vec<tree> known_vals;
3606 vec<tree> known_binfos;
3607 vec<ipa_agg_jump_function_p> known_aggs;
3608 struct inline_edge_summary *es = inline_edge_summary (edge);
3610 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3612 gcc_checking_assert (edge->inline_failed);
3613 evaluate_properties_for_edge (edge, true,
3614 &clause, &known_vals, &known_binfos,
3615 &known_aggs);
3616 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3617 known_aggs, &size, &time, &hints, es->param);
3618 known_vals.release ();
3619 known_binfos.release ();
3620 known_aggs.release ();
3621 gcc_checking_assert (size >= 0);
3622 gcc_checking_assert (time >= 0);
3624 /* When caching, update the cache entry. */
3625 if (edge_growth_cache.exists ())
3627 if ((int) edge_growth_cache.length () <= edge->uid)
3628 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
3629 edge_growth_cache[edge->uid].time = time + (time >= 0);
3631 edge_growth_cache[edge->uid].size = size + (size >= 0);
3632 hints |= simple_edge_hints (edge);
3633 edge_growth_cache[edge->uid].hints = hints + 1;
3635 return time;
3639 /* Return estimated callee growth after inlining EDGE.
3640 Only to be called via estimate_edge_size. */
3643 do_estimate_edge_size (struct cgraph_edge *edge)
3645 int size;
3646 struct cgraph_node *callee;
3647 clause_t clause;
3648 vec<tree> known_vals;
3649 vec<tree> known_binfos;
3650 vec<ipa_agg_jump_function_p> known_aggs;
3652 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3654 if (edge_growth_cache.exists ())
3656 do_estimate_edge_time (edge);
3657 size = edge_growth_cache[edge->uid].size;
3658 gcc_checking_assert (size);
3659 return size - (size > 0);
3662 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3664 /* Early inliner runs without caching, go ahead and do the dirty work. */
3665 gcc_checking_assert (edge->inline_failed);
3666 evaluate_properties_for_edge (edge, true,
3667 &clause, &known_vals, &known_binfos,
3668 &known_aggs);
3669 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3670 known_aggs, &size, NULL, NULL, vNULL);
3671 known_vals.release ();
3672 known_binfos.release ();
3673 known_aggs.release ();
3674 return size;
3678 /* Estimate the growth of the caller when inlining EDGE.
3679 Only to be called via estimate_edge_size. */
3681 inline_hints
3682 do_estimate_edge_hints (struct cgraph_edge *edge)
3684 inline_hints hints;
3685 struct cgraph_node *callee;
3686 clause_t clause;
3687 vec<tree> known_vals;
3688 vec<tree> known_binfos;
3689 vec<ipa_agg_jump_function_p> known_aggs;
3691 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3693 if (edge_growth_cache.exists ())
3695 do_estimate_edge_time (edge);
3696 hints = edge_growth_cache[edge->uid].hints;
3697 gcc_checking_assert (hints);
3698 return hints - 1;
3701 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3703 /* Early inliner runs without caching, go ahead and do the dirty work. */
3704 gcc_checking_assert (edge->inline_failed);
3705 evaluate_properties_for_edge (edge, true,
3706 &clause, &known_vals, &known_binfos,
3707 &known_aggs);
3708 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3709 known_aggs, NULL, NULL, &hints, vNULL);
3710 known_vals.release ();
3711 known_binfos.release ();
3712 known_aggs.release ();
3713 hints |= simple_edge_hints (edge);
3714 return hints;
3718 /* Estimate self time of the function NODE after inlining EDGE. */
3721 estimate_time_after_inlining (struct cgraph_node *node,
3722 struct cgraph_edge *edge)
3724 struct inline_edge_summary *es = inline_edge_summary (edge);
3725 if (!es->predicate || !false_predicate_p (es->predicate))
3727 gcov_type time =
3728 inline_summary (node)->time + estimate_edge_time (edge);
3729 if (time < 0)
3730 time = 0;
3731 if (time > MAX_TIME)
3732 time = MAX_TIME;
3733 return time;
3735 return inline_summary (node)->time;
3739 /* Estimate the size of NODE after inlining EDGE which should be an
3740 edge to either NODE or a call inlined into NODE. */
3743 estimate_size_after_inlining (struct cgraph_node *node,
3744 struct cgraph_edge *edge)
3746 struct inline_edge_summary *es = inline_edge_summary (edge);
3747 if (!es->predicate || !false_predicate_p (es->predicate))
3749 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3750 gcc_assert (size >= 0);
3751 return size;
3753 return inline_summary (node)->size;
3757 struct growth_data
3759 struct cgraph_node *node;
3760 bool self_recursive;
3761 int growth;
3765 /* Worker for do_estimate_growth. Collect growth for all callers. */
3767 static bool
3768 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3770 struct cgraph_edge *e;
3771 struct growth_data *d = (struct growth_data *) data;
3773 for (e = node->callers; e; e = e->next_caller)
3775 gcc_checking_assert (e->inline_failed);
3777 if (e->caller == d->node
3778 || (e->caller->global.inlined_to
3779 && e->caller->global.inlined_to == d->node))
3780 d->self_recursive = true;
3781 d->growth += estimate_edge_growth (e);
3783 return false;
3787 /* Estimate the growth caused by inlining NODE into all callees. */
3790 do_estimate_growth (struct cgraph_node *node)
3792 struct growth_data d = { node, 0, false };
3793 struct inline_summary *info = inline_summary (node);
3795 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3797 /* For self recursive functions the growth estimation really should be
3798 infinity. We don't want to return very large values because the growth
3799 plays various roles in badness computation fractions. Be sure to not
3800 return zero or negative growths. */
3801 if (d.self_recursive)
3802 d.growth = d.growth < info->size ? info->size : d.growth;
3803 else if (DECL_EXTERNAL (node->decl))
3805 else
3807 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3808 d.growth -= info->size;
3809 /* COMDAT functions are very often not shared across multiple units
3810 since they come from various template instantiations.
3811 Take this into account. */
3812 else if (DECL_COMDAT (node->decl)
3813 && cgraph_can_remove_if_no_direct_calls_p (node))
3814 d.growth -= (info->size
3815 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3816 + 50) / 100;
3819 if (node_growth_cache.exists ())
3821 if ((int) node_growth_cache.length () <= node->uid)
3822 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
3823 node_growth_cache[node->uid] = d.growth + (d.growth >= 0);
3825 return d.growth;
3829 /* This function performs intraprocedural analysis in NODE that is required to
3830 inline indirect calls. */
3832 static void
3833 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3835 ipa_analyze_node (node);
3836 if (dump_file && (dump_flags & TDF_DETAILS))
3838 ipa_print_node_params (dump_file, node);
3839 ipa_print_node_jump_functions (dump_file, node);
3844 /* Note function body size. */
3846 static void
3847 inline_analyze_function (struct cgraph_node *node)
3849 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3851 if (dump_file)
3852 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3853 node->name (), node->order);
3854 if (optimize && !node->thunk.thunk_p)
3855 inline_indirect_intraprocedural_analysis (node);
3856 compute_inline_parameters (node, false);
3857 if (!optimize)
3859 struct cgraph_edge *e;
3860 for (e = node->callees; e; e = e->next_callee)
3862 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3863 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3864 e->call_stmt_cannot_inline_p = true;
3866 for (e = node->indirect_calls; e; e = e->next_callee)
3868 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3869 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3870 e->call_stmt_cannot_inline_p = true;
3874 pop_cfun ();
3878 /* Called when new function is inserted to callgraph late. */
3880 static void
3881 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3883 inline_analyze_function (node);
3887 /* Note function body size. */
3889 void
3890 inline_generate_summary (void)
3892 struct cgraph_node *node;
3894 /* When not optimizing, do not bother to analyze. Inlining is still done
3895 because edge redirection needs to happen there. */
3896 if (!optimize && !flag_lto && !flag_wpa)
3897 return;
3899 function_insertion_hook_holder =
3900 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3902 ipa_register_cgraph_hooks ();
3903 inline_free_summary ();
3905 FOR_EACH_DEFINED_FUNCTION (node)
3906 if (!node->alias)
3907 inline_analyze_function (node);
3911 /* Read predicate from IB. */
3913 static struct predicate
3914 read_predicate (struct lto_input_block *ib)
3916 struct predicate out;
3917 clause_t clause;
3918 int k = 0;
3922 gcc_assert (k <= MAX_CLAUSES);
3923 clause = out.clause[k++] = streamer_read_uhwi (ib);
3925 while (clause);
3927 /* Zero-initialize the remaining clauses in OUT. */
3928 while (k <= MAX_CLAUSES)
3929 out.clause[k++] = 0;
3931 return out;
3935 /* Write inline summary for edge E to OB. */
3937 static void
3938 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
3940 struct inline_edge_summary *es = inline_edge_summary (e);
3941 struct predicate p;
3942 int length, i;
3944 es->call_stmt_size = streamer_read_uhwi (ib);
3945 es->call_stmt_time = streamer_read_uhwi (ib);
3946 es->loop_depth = streamer_read_uhwi (ib);
3947 p = read_predicate (ib);
3948 edge_set_predicate (e, &p);
3949 length = streamer_read_uhwi (ib);
3950 if (length)
3952 es->param.safe_grow_cleared (length);
3953 for (i = 0; i < length; i++)
3954 es->param[i].change_prob = streamer_read_uhwi (ib);
3959 /* Stream in inline summaries from the section. */
3961 static void
3962 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3963 size_t len)
3965 const struct lto_function_header *header =
3966 (const struct lto_function_header *) data;
3967 const int cfg_offset = sizeof (struct lto_function_header);
3968 const int main_offset = cfg_offset + header->cfg_size;
3969 const int string_offset = main_offset + header->main_size;
3970 struct data_in *data_in;
3971 struct lto_input_block ib;
3972 unsigned int i, count2, j;
3973 unsigned int f_count;
3975 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
3976 header->main_size);
3978 data_in =
3979 lto_data_in_create (file_data, (const char *) data + string_offset,
3980 header->string_size, vNULL);
3981 f_count = streamer_read_uhwi (&ib);
3982 for (i = 0; i < f_count; i++)
3984 unsigned int index;
3985 struct cgraph_node *node;
3986 struct inline_summary *info;
3987 lto_symtab_encoder_t encoder;
3988 struct bitpack_d bp;
3989 struct cgraph_edge *e;
3990 predicate p;
3992 index = streamer_read_uhwi (&ib);
3993 encoder = file_data->symtab_node_encoder;
3994 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3995 info = inline_summary (node);
3997 info->estimated_stack_size
3998 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
3999 info->size = info->self_size = streamer_read_uhwi (&ib);
4000 info->time = info->self_time = streamer_read_uhwi (&ib);
4002 bp = streamer_read_bitpack (&ib);
4003 info->inlinable = bp_unpack_value (&bp, 1);
4005 count2 = streamer_read_uhwi (&ib);
4006 gcc_assert (!info->conds);
4007 for (j = 0; j < count2; j++)
4009 struct condition c;
4010 c.operand_num = streamer_read_uhwi (&ib);
4011 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4012 c.val = stream_read_tree (&ib, data_in);
4013 bp = streamer_read_bitpack (&ib);
4014 c.agg_contents = bp_unpack_value (&bp, 1);
4015 c.by_ref = bp_unpack_value (&bp, 1);
4016 if (c.agg_contents)
4017 c.offset = streamer_read_uhwi (&ib);
4018 vec_safe_push (info->conds, c);
4020 count2 = streamer_read_uhwi (&ib);
4021 gcc_assert (!info->entry);
4022 for (j = 0; j < count2; j++)
4024 struct size_time_entry e;
4026 e.size = streamer_read_uhwi (&ib);
4027 e.time = streamer_read_uhwi (&ib);
4028 e.predicate = read_predicate (&ib);
4030 vec_safe_push (info->entry, e);
4033 p = read_predicate (&ib);
4034 set_hint_predicate (&info->loop_iterations, p);
4035 p = read_predicate (&ib);
4036 set_hint_predicate (&info->loop_stride, p);
4037 p = read_predicate (&ib);
4038 set_hint_predicate (&info->array_index, p);
4039 for (e = node->callees; e; e = e->next_callee)
4040 read_inline_edge_summary (&ib, e);
4041 for (e = node->indirect_calls; e; e = e->next_callee)
4042 read_inline_edge_summary (&ib, e);
4045 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
4046 len);
4047 lto_data_in_delete (data_in);
4051 /* Read inline summary. Jump functions are shared among ipa-cp
4052 and inliner, so when ipa-cp is active, we don't need to write them
4053 twice. */
4055 void
4056 inline_read_summary (void)
4058 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4059 struct lto_file_decl_data *file_data;
4060 unsigned int j = 0;
4062 inline_summary_alloc ();
4064 while ((file_data = file_data_vec[j++]))
4066 size_t len;
4067 const char *data = lto_get_section_data (file_data,
4068 LTO_section_inline_summary,
4069 NULL, &len);
4070 if (data)
4071 inline_read_section (file_data, data, len);
4072 else
4073 /* Fatal error here. We do not want to support compiling ltrans units
4074 with different version of compiler or different flags than the WPA
4075 unit, so this should never happen. */
4076 fatal_error ("ipa inline summary is missing in input file");
4078 if (optimize)
4080 ipa_register_cgraph_hooks ();
4081 if (!flag_ipa_cp)
4082 ipa_prop_read_jump_functions ();
4084 function_insertion_hook_holder =
4085 cgraph_add_function_insertion_hook (&add_new_function, NULL);
4089 /* Write predicate P to OB. */
4091 static void
4092 write_predicate (struct output_block *ob, struct predicate *p)
4094 int j;
4095 if (p)
4096 for (j = 0; p->clause[j]; j++)
4098 gcc_assert (j < MAX_CLAUSES);
4099 streamer_write_uhwi (ob, p->clause[j]);
4101 streamer_write_uhwi (ob, 0);
4105 /* Write inline summary for edge E to OB. */
4107 static void
4108 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4110 struct inline_edge_summary *es = inline_edge_summary (e);
4111 int i;
4113 streamer_write_uhwi (ob, es->call_stmt_size);
4114 streamer_write_uhwi (ob, es->call_stmt_time);
4115 streamer_write_uhwi (ob, es->loop_depth);
4116 write_predicate (ob, es->predicate);
4117 streamer_write_uhwi (ob, es->param.length ());
4118 for (i = 0; i < (int) es->param.length (); i++)
4119 streamer_write_uhwi (ob, es->param[i].change_prob);
4123 /* Write inline summary for node in SET.
4124 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4125 active, we don't need to write them twice. */
4127 void
4128 inline_write_summary (void)
4130 struct cgraph_node *node;
4131 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4132 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4133 unsigned int count = 0;
4134 int i;
4136 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4138 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4139 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4140 if (cnode && cnode->definition && !cnode->alias)
4141 count++;
4143 streamer_write_uhwi (ob, count);
4145 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4147 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4148 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4149 if (cnode && (node = cnode)->definition && !node->alias)
4151 struct inline_summary *info = inline_summary (node);
4152 struct bitpack_d bp;
4153 struct cgraph_edge *edge;
4154 int i;
4155 size_time_entry *e;
4156 struct condition *c;
4158 streamer_write_uhwi (ob,
4159 lto_symtab_encoder_encode (encoder,
4161 node));
4162 streamer_write_hwi (ob, info->estimated_self_stack_size);
4163 streamer_write_hwi (ob, info->self_size);
4164 streamer_write_hwi (ob, info->self_time);
4165 bp = bitpack_create (ob->main_stream);
4166 bp_pack_value (&bp, info->inlinable, 1);
4167 streamer_write_bitpack (&bp);
4168 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4169 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4171 streamer_write_uhwi (ob, c->operand_num);
4172 streamer_write_uhwi (ob, c->code);
4173 stream_write_tree (ob, c->val, true);
4174 bp = bitpack_create (ob->main_stream);
4175 bp_pack_value (&bp, c->agg_contents, 1);
4176 bp_pack_value (&bp, c->by_ref, 1);
4177 streamer_write_bitpack (&bp);
4178 if (c->agg_contents)
4179 streamer_write_uhwi (ob, c->offset);
4181 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4182 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4184 streamer_write_uhwi (ob, e->size);
4185 streamer_write_uhwi (ob, e->time);
4186 write_predicate (ob, &e->predicate);
4188 write_predicate (ob, info->loop_iterations);
4189 write_predicate (ob, info->loop_stride);
4190 write_predicate (ob, info->array_index);
4191 for (edge = node->callees; edge; edge = edge->next_callee)
4192 write_inline_edge_summary (ob, edge);
4193 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4194 write_inline_edge_summary (ob, edge);
4197 streamer_write_char_stream (ob->main_stream, 0);
4198 produce_asm (ob, NULL);
4199 destroy_output_block (ob);
4201 if (optimize && !flag_ipa_cp)
4202 ipa_prop_write_jump_functions ();
4206 /* Release inline summary. */
4208 void
4209 inline_free_summary (void)
4211 struct cgraph_node *node;
4212 if (!inline_edge_summary_vec.exists ())
4213 return;
4214 FOR_EACH_DEFINED_FUNCTION (node)
4215 if (!node->alias)
4216 reset_inline_summary (node);
4217 if (function_insertion_hook_holder)
4218 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
4219 function_insertion_hook_holder = NULL;
4220 if (node_removal_hook_holder)
4221 cgraph_remove_node_removal_hook (node_removal_hook_holder);
4222 node_removal_hook_holder = NULL;
4223 if (edge_removal_hook_holder)
4224 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
4225 edge_removal_hook_holder = NULL;
4226 if (node_duplication_hook_holder)
4227 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
4228 node_duplication_hook_holder = NULL;
4229 if (edge_duplication_hook_holder)
4230 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
4231 edge_duplication_hook_holder = NULL;
4232 vec_free (inline_summary_vec);
4233 inline_edge_summary_vec.release ();
4234 if (edge_predicate_pool)
4235 free_alloc_pool (edge_predicate_pool);
4236 edge_predicate_pool = 0;