Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / ipa-inline-analysis.c
blob49c905859dbdb68a314402356ee76a584211bdf1
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);
750 /* If the edge is determined to be never executed, redirect it
751 to BUILTIN_UNREACHABLE to save inliner from inlining into it. */
752 if (predicate && false_predicate_p (predicate) && e->callee)
754 struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
756 cgraph_redirect_edge_callee (e,
757 cgraph_get_create_node
758 (builtin_decl_implicit (BUILT_IN_UNREACHABLE)));
759 e->inline_failed = CIF_UNREACHABLE;
760 if (callee)
761 cgraph_remove_node_and_inline_clones (callee, NULL);
763 if (predicate && !true_predicate_p (predicate))
765 if (!es->predicate)
766 es->predicate = (struct predicate *) pool_alloc (edge_predicate_pool);
767 *es->predicate = *predicate;
769 else
771 if (es->predicate)
772 pool_free (edge_predicate_pool, es->predicate);
773 es->predicate = NULL;
777 /* Set predicate for hint *P. */
779 static void
780 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
782 if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
784 if (*p)
785 pool_free (edge_predicate_pool, *p);
786 *p = NULL;
788 else
790 if (!*p)
791 *p = (struct predicate *) pool_alloc (edge_predicate_pool);
792 **p = new_predicate;
797 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
798 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
799 Return clause of possible truths. When INLINE_P is true, assume that we are
800 inlining.
802 ERROR_MARK means compile time invariant. */
804 static clause_t
805 evaluate_conditions_for_known_args (struct cgraph_node *node,
806 bool inline_p,
807 vec<tree> known_vals,
808 vec<ipa_agg_jump_function_p>
809 known_aggs)
811 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
812 struct inline_summary *info = inline_summary (node);
813 int i;
814 struct condition *c;
816 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
818 tree val;
819 tree res;
821 /* We allow call stmt to have fewer arguments than the callee function
822 (especially for K&R style programs). So bound check here (we assume
823 known_aggs vector, if non-NULL, has the same length as
824 known_vals). */
825 gcc_checking_assert (!known_aggs.exists ()
826 || (known_vals.length () == known_aggs.length ()));
827 if (c->operand_num >= (int) known_vals.length ())
829 clause |= 1 << (i + predicate_first_dynamic_condition);
830 continue;
833 if (c->agg_contents)
835 struct ipa_agg_jump_function *agg;
837 if (c->code == CHANGED
838 && !c->by_ref
839 && (known_vals[c->operand_num] == error_mark_node))
840 continue;
842 if (known_aggs.exists ())
844 agg = known_aggs[c->operand_num];
845 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
847 else
848 val = NULL_TREE;
850 else
852 val = known_vals[c->operand_num];
853 if (val == error_mark_node && c->code != CHANGED)
854 val = NULL_TREE;
857 if (!val)
859 clause |= 1 << (i + predicate_first_dynamic_condition);
860 continue;
862 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
863 continue;
864 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
865 if (res && integer_zerop (res))
866 continue;
867 clause |= 1 << (i + predicate_first_dynamic_condition);
869 return clause;
873 /* Work out what conditions might be true at invocation of E. */
875 static void
876 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
877 clause_t *clause_ptr,
878 vec<tree> *known_vals_ptr,
879 vec<tree> *known_binfos_ptr,
880 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
882 struct cgraph_node *callee =
883 cgraph_function_or_thunk_node (e->callee, NULL);
884 struct inline_summary *info = inline_summary (callee);
885 vec<tree> known_vals = vNULL;
886 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
888 if (clause_ptr)
889 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
890 if (known_vals_ptr)
891 known_vals_ptr->create (0);
892 if (known_binfos_ptr)
893 known_binfos_ptr->create (0);
895 if (ipa_node_params_vector.exists ()
896 && !e->call_stmt_cannot_inline_p
897 && ((clause_ptr && info->conds) || known_vals_ptr || known_binfos_ptr))
899 struct ipa_node_params *parms_info;
900 struct ipa_edge_args *args = IPA_EDGE_REF (e);
901 struct inline_edge_summary *es = inline_edge_summary (e);
902 int i, count = ipa_get_cs_argument_count (args);
904 if (e->caller->global.inlined_to)
905 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
906 else
907 parms_info = IPA_NODE_REF (e->caller);
909 if (count && (info->conds || known_vals_ptr))
910 known_vals.safe_grow_cleared (count);
911 if (count && (info->conds || known_aggs_ptr))
912 known_aggs.safe_grow_cleared (count);
913 if (count && known_binfos_ptr)
914 known_binfos_ptr->safe_grow_cleared (count);
916 for (i = 0; i < count; i++)
918 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
919 tree cst = ipa_value_from_jfunc (parms_info, jf);
920 if (cst)
922 if (known_vals.exists () && TREE_CODE (cst) != TREE_BINFO)
923 known_vals[i] = cst;
924 else if (known_binfos_ptr != NULL
925 && TREE_CODE (cst) == TREE_BINFO)
926 (*known_binfos_ptr)[i] = cst;
928 else if (inline_p && !es->param[i].change_prob)
929 known_vals[i] = error_mark_node;
930 /* TODO: When IPA-CP starts propagating and merging aggregate jump
931 functions, use its knowledge of the caller too, just like the
932 scalar case above. */
933 known_aggs[i] = &jf->agg;
937 if (clause_ptr)
938 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
939 known_vals, known_aggs);
941 if (known_vals_ptr)
942 *known_vals_ptr = known_vals;
943 else
944 known_vals.release ();
946 if (known_aggs_ptr)
947 *known_aggs_ptr = known_aggs;
948 else
949 known_aggs.release ();
953 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
955 static void
956 inline_summary_alloc (void)
958 if (!node_removal_hook_holder)
959 node_removal_hook_holder =
960 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
961 if (!edge_removal_hook_holder)
962 edge_removal_hook_holder =
963 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
964 if (!node_duplication_hook_holder)
965 node_duplication_hook_holder =
966 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
967 if (!edge_duplication_hook_holder)
968 edge_duplication_hook_holder =
969 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
971 if (vec_safe_length (inline_summary_vec) <= (unsigned) cgraph_max_uid)
972 vec_safe_grow_cleared (inline_summary_vec, cgraph_max_uid + 1);
973 if (inline_edge_summary_vec.length () <= (unsigned) cgraph_edge_max_uid)
974 inline_edge_summary_vec.safe_grow_cleared (cgraph_edge_max_uid + 1);
975 if (!edge_predicate_pool)
976 edge_predicate_pool = create_alloc_pool ("edge predicates",
977 sizeof (struct predicate), 10);
980 /* We are called multiple time for given function; clear
981 data from previous run so they are not cumulated. */
983 static void
984 reset_inline_edge_summary (struct cgraph_edge *e)
986 if (e->uid < (int) inline_edge_summary_vec.length ())
988 struct inline_edge_summary *es = inline_edge_summary (e);
990 es->call_stmt_size = es->call_stmt_time = 0;
991 if (es->predicate)
992 pool_free (edge_predicate_pool, es->predicate);
993 es->predicate = NULL;
994 es->param.release ();
998 /* We are called multiple time for given function; clear
999 data from previous run so they are not cumulated. */
1001 static void
1002 reset_inline_summary (struct cgraph_node *node)
1004 struct inline_summary *info = inline_summary (node);
1005 struct cgraph_edge *e;
1007 info->self_size = info->self_time = 0;
1008 info->estimated_stack_size = 0;
1009 info->estimated_self_stack_size = 0;
1010 info->size = 0;
1011 info->time = 0;
1012 info->growth = 0;
1013 info->scc_no = 0;
1014 if (info->loop_iterations)
1016 pool_free (edge_predicate_pool, info->loop_iterations);
1017 info->loop_iterations = NULL;
1019 if (info->loop_stride)
1021 pool_free (edge_predicate_pool, info->loop_stride);
1022 info->loop_stride = NULL;
1024 if (info->array_index)
1026 pool_free (edge_predicate_pool, info->array_index);
1027 info->array_index = NULL;
1029 vec_free (info->conds);
1030 vec_free (info->entry);
1031 for (e = node->callees; e; e = e->next_callee)
1032 reset_inline_edge_summary (e);
1033 for (e = node->indirect_calls; e; e = e->next_callee)
1034 reset_inline_edge_summary (e);
1037 /* Hook that is called by cgraph.c when a node is removed. */
1039 static void
1040 inline_node_removal_hook (struct cgraph_node *node,
1041 void *data ATTRIBUTE_UNUSED)
1043 struct inline_summary *info;
1044 if (vec_safe_length (inline_summary_vec) <= (unsigned) node->uid)
1045 return;
1046 info = inline_summary (node);
1047 reset_inline_summary (node);
1048 memset (info, 0, sizeof (inline_summary_t));
1051 /* Remap predicate P of former function to be predicate of duplicated function.
1052 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1053 INFO is inline summary of the duplicated node. */
1055 static struct predicate
1056 remap_predicate_after_duplication (struct predicate *p,
1057 clause_t possible_truths,
1058 struct inline_summary *info)
1060 struct predicate new_predicate = true_predicate ();
1061 int j;
1062 for (j = 0; p->clause[j]; j++)
1063 if (!(possible_truths & p->clause[j]))
1065 new_predicate = false_predicate ();
1066 break;
1068 else
1069 add_clause (info->conds, &new_predicate,
1070 possible_truths & p->clause[j]);
1071 return new_predicate;
1074 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1075 Additionally care about allocating new memory slot for updated predicate
1076 and set it to NULL when it becomes true or false (and thus uninteresting).
1079 static void
1080 remap_hint_predicate_after_duplication (struct predicate **p,
1081 clause_t possible_truths,
1082 struct inline_summary *info)
1084 struct predicate new_predicate;
1086 if (!*p)
1087 return;
1089 new_predicate = remap_predicate_after_duplication (*p,
1090 possible_truths, info);
1091 /* We do not want to free previous predicate; it is used by node origin. */
1092 *p = NULL;
1093 set_hint_predicate (p, new_predicate);
1097 /* Hook that is called by cgraph.c when a node is duplicated. */
1099 static void
1100 inline_node_duplication_hook (struct cgraph_node *src,
1101 struct cgraph_node *dst,
1102 ATTRIBUTE_UNUSED void *data)
1104 struct inline_summary *info;
1105 inline_summary_alloc ();
1106 info = inline_summary (dst);
1107 memcpy (info, inline_summary (src), sizeof (struct inline_summary));
1108 /* TODO: as an optimization, we may avoid copying conditions
1109 that are known to be false or true. */
1110 info->conds = vec_safe_copy (info->conds);
1112 /* When there are any replacements in the function body, see if we can figure
1113 out that something was optimized out. */
1114 if (ipa_node_params_vector.exists () && dst->clone.tree_map)
1116 vec<size_time_entry, va_gc> *entry = info->entry;
1117 /* Use SRC parm info since it may not be copied yet. */
1118 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1119 vec<tree> known_vals = vNULL;
1120 int count = ipa_get_param_count (parms_info);
1121 int i, j;
1122 clause_t possible_truths;
1123 struct predicate true_pred = true_predicate ();
1124 size_time_entry *e;
1125 int optimized_out_size = 0;
1126 bool inlined_to_p = false;
1127 struct cgraph_edge *edge;
1129 info->entry = 0;
1130 known_vals.safe_grow_cleared (count);
1131 for (i = 0; i < count; i++)
1133 struct ipa_replace_map *r;
1135 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1137 if (((!r->old_tree && r->parm_num == i)
1138 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1139 && r->replace_p && !r->ref_p)
1141 known_vals[i] = r->new_tree;
1142 break;
1146 possible_truths = evaluate_conditions_for_known_args (dst, false,
1147 known_vals,
1148 vNULL);
1149 known_vals.release ();
1151 account_size_time (info, 0, 0, &true_pred);
1153 /* Remap size_time vectors.
1154 Simplify the predicate by prunning out alternatives that are known
1155 to be false.
1156 TODO: as on optimization, we can also eliminate conditions known
1157 to be true. */
1158 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1160 struct predicate new_predicate;
1161 new_predicate = remap_predicate_after_duplication (&e->predicate,
1162 possible_truths,
1163 info);
1164 if (false_predicate_p (&new_predicate))
1165 optimized_out_size += e->size;
1166 else
1167 account_size_time (info, e->size, e->time, &new_predicate);
1170 /* Remap edge predicates with the same simplification as above.
1171 Also copy constantness arrays. */
1172 for (edge = dst->callees; edge; edge = edge->next_callee)
1174 struct predicate new_predicate;
1175 struct inline_edge_summary *es = inline_edge_summary (edge);
1177 if (!edge->inline_failed)
1178 inlined_to_p = true;
1179 if (!es->predicate)
1180 continue;
1181 new_predicate = remap_predicate_after_duplication (es->predicate,
1182 possible_truths,
1183 info);
1184 if (false_predicate_p (&new_predicate)
1185 && !false_predicate_p (es->predicate))
1187 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1188 edge->frequency = 0;
1190 edge_set_predicate (edge, &new_predicate);
1193 /* Remap indirect edge predicates with the same simplificaiton as above.
1194 Also copy constantness arrays. */
1195 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1197 struct predicate new_predicate;
1198 struct inline_edge_summary *es = inline_edge_summary (edge);
1200 gcc_checking_assert (edge->inline_failed);
1201 if (!es->predicate)
1202 continue;
1203 new_predicate = remap_predicate_after_duplication (es->predicate,
1204 possible_truths,
1205 info);
1206 if (false_predicate_p (&new_predicate)
1207 && !false_predicate_p (es->predicate))
1209 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1210 edge->frequency = 0;
1212 edge_set_predicate (edge, &new_predicate);
1214 remap_hint_predicate_after_duplication (&info->loop_iterations,
1215 possible_truths, info);
1216 remap_hint_predicate_after_duplication (&info->loop_stride,
1217 possible_truths, info);
1218 remap_hint_predicate_after_duplication (&info->array_index,
1219 possible_truths, info);
1221 /* If inliner or someone after inliner will ever start producing
1222 non-trivial clones, we will get trouble with lack of information
1223 about updating self sizes, because size vectors already contains
1224 sizes of the calees. */
1225 gcc_assert (!inlined_to_p || !optimized_out_size);
1227 else
1229 info->entry = vec_safe_copy (info->entry);
1230 if (info->loop_iterations)
1232 predicate p = *info->loop_iterations;
1233 info->loop_iterations = NULL;
1234 set_hint_predicate (&info->loop_iterations, p);
1236 if (info->loop_stride)
1238 predicate p = *info->loop_stride;
1239 info->loop_stride = NULL;
1240 set_hint_predicate (&info->loop_stride, p);
1242 if (info->array_index)
1244 predicate p = *info->array_index;
1245 info->array_index = NULL;
1246 set_hint_predicate (&info->array_index, p);
1249 inline_update_overall_summary (dst);
1253 /* Hook that is called by cgraph.c when a node is duplicated. */
1255 static void
1256 inline_edge_duplication_hook (struct cgraph_edge *src,
1257 struct cgraph_edge *dst,
1258 ATTRIBUTE_UNUSED void *data)
1260 struct inline_edge_summary *info;
1261 struct inline_edge_summary *srcinfo;
1262 inline_summary_alloc ();
1263 info = inline_edge_summary (dst);
1264 srcinfo = inline_edge_summary (src);
1265 memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
1266 info->predicate = NULL;
1267 edge_set_predicate (dst, srcinfo->predicate);
1268 info->param = srcinfo->param.copy ();
1272 /* Keep edge cache consistent across edge removal. */
1274 static void
1275 inline_edge_removal_hook (struct cgraph_edge *edge,
1276 void *data ATTRIBUTE_UNUSED)
1278 if (edge_growth_cache.exists ())
1279 reset_edge_growth_cache (edge);
1280 reset_inline_edge_summary (edge);
1284 /* Initialize growth caches. */
1286 void
1287 initialize_growth_caches (void)
1289 if (cgraph_edge_max_uid)
1290 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
1291 if (cgraph_max_uid)
1292 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
1296 /* Free growth caches. */
1298 void
1299 free_growth_caches (void)
1301 edge_growth_cache.release ();
1302 node_growth_cache.release ();
1306 /* Dump edge summaries associated to NODE and recursively to all clones.
1307 Indent by INDENT. */
1309 static void
1310 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1311 struct inline_summary *info)
1313 struct cgraph_edge *edge;
1314 for (edge = node->callees; edge; edge = edge->next_callee)
1316 struct inline_edge_summary *es = inline_edge_summary (edge);
1317 struct cgraph_node *callee =
1318 cgraph_function_or_thunk_node (edge->callee, NULL);
1319 int i;
1321 fprintf (f,
1322 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1323 " time: %2i callee size:%2i stack:%2i",
1324 indent, "", callee->name (), callee->order,
1325 !edge->inline_failed
1326 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1327 indent, "", es->loop_depth, edge->frequency,
1328 es->call_stmt_size, es->call_stmt_time,
1329 (int) inline_summary (callee)->size / INLINE_SIZE_SCALE,
1330 (int) inline_summary (callee)->estimated_stack_size);
1332 if (es->predicate)
1334 fprintf (f, " predicate: ");
1335 dump_predicate (f, info->conds, es->predicate);
1337 else
1338 fprintf (f, "\n");
1339 if (es->param.exists ())
1340 for (i = 0; i < (int) es->param.length (); i++)
1342 int prob = es->param[i].change_prob;
1344 if (!prob)
1345 fprintf (f, "%*s op%i is compile time invariant\n",
1346 indent + 2, "", i);
1347 else if (prob != REG_BR_PROB_BASE)
1348 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1349 prob * 100.0 / REG_BR_PROB_BASE);
1351 if (!edge->inline_failed)
1353 fprintf (f, "%*sCallee self size %i,"
1354 " callee size %i\n",
1355 indent + 2, "",
1356 (int) inline_summary (callee)->estimated_self_stack_size,
1357 (int) inline_summary (callee)->estimated_stack_size);
1358 dump_inline_edge_summary (f, indent + 2, callee, info);
1361 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1363 struct inline_edge_summary *es = inline_edge_summary (edge);
1364 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1365 " time: %2i",
1366 indent, "",
1367 es->loop_depth,
1368 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1369 if (es->predicate)
1371 fprintf (f, "predicate: ");
1372 dump_predicate (f, info->conds, es->predicate);
1374 else
1375 fprintf (f, "\n");
1380 void
1381 dump_inline_summary (FILE *f, struct cgraph_node *node)
1383 if (node->definition)
1385 struct inline_summary *s = inline_summary (node);
1386 size_time_entry *e;
1387 int i;
1388 fprintf (f, "Inline summary for %s/%i", node->name (),
1389 node->order);
1390 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1391 fprintf (f, " always_inline");
1392 if (s->inlinable)
1393 fprintf (f, " inlinable");
1394 fprintf (f, "\n self time: %i\n", s->self_time);
1395 fprintf (f, " global time: %i\n", s->time);
1396 fprintf (f, " self size: %i\n", s->self_size);
1397 fprintf (f, " global size: %i\n", s->size);
1398 fprintf (f, " min size: %i\n", s->min_size);
1399 fprintf (f, " self stack: %i\n",
1400 (int) s->estimated_self_stack_size);
1401 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1402 if (s->growth)
1403 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1404 if (s->scc_no)
1405 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1406 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1408 fprintf (f, " size:%f, time:%f, predicate:",
1409 (double) e->size / INLINE_SIZE_SCALE,
1410 (double) e->time / INLINE_TIME_SCALE);
1411 dump_predicate (f, s->conds, &e->predicate);
1413 if (s->loop_iterations)
1415 fprintf (f, " loop iterations:");
1416 dump_predicate (f, s->conds, s->loop_iterations);
1418 if (s->loop_stride)
1420 fprintf (f, " loop stride:");
1421 dump_predicate (f, s->conds, s->loop_stride);
1423 if (s->array_index)
1425 fprintf (f, " array index:");
1426 dump_predicate (f, s->conds, s->array_index);
1428 fprintf (f, " calls:\n");
1429 dump_inline_edge_summary (f, 4, node, s);
1430 fprintf (f, "\n");
1434 DEBUG_FUNCTION void
1435 debug_inline_summary (struct cgraph_node *node)
1437 dump_inline_summary (stderr, node);
1440 void
1441 dump_inline_summaries (FILE *f)
1443 struct cgraph_node *node;
1445 FOR_EACH_DEFINED_FUNCTION (node)
1446 if (!node->global.inlined_to)
1447 dump_inline_summary (f, node);
1450 /* Give initial reasons why inlining would fail on EDGE. This gets either
1451 nullified or usually overwritten by more precise reasons later. */
1453 void
1454 initialize_inline_failed (struct cgraph_edge *e)
1456 struct cgraph_node *callee = e->callee;
1458 if (e->indirect_unknown_callee)
1459 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1460 else if (!callee->definition)
1461 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1462 else if (callee->local.redefined_extern_inline)
1463 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1464 else if (e->call_stmt_cannot_inline_p)
1465 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1466 else if (cfun && fn_contains_cilk_spawn_p (cfun))
1467 /* We can't inline if the function is spawing a function. */
1468 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
1469 else
1470 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1473 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1474 boolean variable pointed to by DATA. */
1476 static bool
1477 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1478 void *data)
1480 bool *b = (bool *) data;
1481 *b = true;
1482 return true;
1485 /* If OP refers to value of function parameter, return the corresponding
1486 parameter. */
1488 static tree
1489 unmodified_parm_1 (gimple stmt, tree op)
1491 /* SSA_NAME referring to parm default def? */
1492 if (TREE_CODE (op) == SSA_NAME
1493 && SSA_NAME_IS_DEFAULT_DEF (op)
1494 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1495 return SSA_NAME_VAR (op);
1496 /* Non-SSA parm reference? */
1497 if (TREE_CODE (op) == PARM_DECL)
1499 bool modified = false;
1501 ao_ref refd;
1502 ao_ref_init (&refd, op);
1503 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1504 NULL);
1505 if (!modified)
1506 return op;
1508 return NULL_TREE;
1511 /* If OP refers to value of function parameter, return the corresponding
1512 parameter. Also traverse chains of SSA register assignments. */
1514 static tree
1515 unmodified_parm (gimple stmt, tree op)
1517 tree res = unmodified_parm_1 (stmt, op);
1518 if (res)
1519 return res;
1521 if (TREE_CODE (op) == SSA_NAME
1522 && !SSA_NAME_IS_DEFAULT_DEF (op)
1523 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1524 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1525 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1526 return NULL_TREE;
1529 /* If OP refers to a value of a function parameter or value loaded from an
1530 aggregate passed to a parameter (either by value or reference), return TRUE
1531 and store the number of the parameter to *INDEX_P and information whether
1532 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1533 the function parameters, STMT is the statement in which OP is used or
1534 loaded. */
1536 static bool
1537 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1538 gimple stmt, tree op, int *index_p,
1539 struct agg_position_info *aggpos)
1541 tree res = unmodified_parm_1 (stmt, op);
1543 gcc_checking_assert (aggpos);
1544 if (res)
1546 *index_p = ipa_get_param_decl_index (info, res);
1547 if (*index_p < 0)
1548 return false;
1549 aggpos->agg_contents = false;
1550 aggpos->by_ref = false;
1551 return true;
1554 if (TREE_CODE (op) == SSA_NAME)
1556 if (SSA_NAME_IS_DEFAULT_DEF (op)
1557 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1558 return false;
1559 stmt = SSA_NAME_DEF_STMT (op);
1560 op = gimple_assign_rhs1 (stmt);
1561 if (!REFERENCE_CLASS_P (op))
1562 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1563 aggpos);
1566 aggpos->agg_contents = true;
1567 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1568 &aggpos->by_ref);
1571 /* See if statement might disappear after inlining.
1572 0 - means not eliminated
1573 1 - half of statements goes away
1574 2 - for sure it is eliminated.
1575 We are not terribly sophisticated, basically looking for simple abstraction
1576 penalty wrappers. */
1578 static int
1579 eliminated_by_inlining_prob (gimple stmt)
1581 enum gimple_code code = gimple_code (stmt);
1582 enum tree_code rhs_code;
1584 if (!optimize)
1585 return 0;
1587 switch (code)
1589 case GIMPLE_RETURN:
1590 return 2;
1591 case GIMPLE_ASSIGN:
1592 if (gimple_num_ops (stmt) != 2)
1593 return 0;
1595 rhs_code = gimple_assign_rhs_code (stmt);
1597 /* Casts of parameters, loads from parameters passed by reference
1598 and stores to return value or parameters are often free after
1599 inlining dua to SRA and further combining.
1600 Assume that half of statements goes away. */
1601 if (rhs_code == CONVERT_EXPR
1602 || rhs_code == NOP_EXPR
1603 || rhs_code == VIEW_CONVERT_EXPR
1604 || rhs_code == ADDR_EXPR
1605 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1607 tree rhs = gimple_assign_rhs1 (stmt);
1608 tree lhs = gimple_assign_lhs (stmt);
1609 tree inner_rhs = get_base_address (rhs);
1610 tree inner_lhs = get_base_address (lhs);
1611 bool rhs_free = false;
1612 bool lhs_free = false;
1614 if (!inner_rhs)
1615 inner_rhs = rhs;
1616 if (!inner_lhs)
1617 inner_lhs = lhs;
1619 /* Reads of parameter are expected to be free. */
1620 if (unmodified_parm (stmt, inner_rhs))
1621 rhs_free = true;
1622 /* Match expressions of form &this->field. Those will most likely
1623 combine with something upstream after inlining. */
1624 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1626 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1627 if (TREE_CODE (op) == PARM_DECL)
1628 rhs_free = true;
1629 else if (TREE_CODE (op) == MEM_REF
1630 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1631 rhs_free = true;
1634 /* When parameter is not SSA register because its address is taken
1635 and it is just copied into one, the statement will be completely
1636 free after inlining (we will copy propagate backward). */
1637 if (rhs_free && is_gimple_reg (lhs))
1638 return 2;
1640 /* Reads of parameters passed by reference
1641 expected to be free (i.e. optimized out after inlining). */
1642 if (TREE_CODE (inner_rhs) == MEM_REF
1643 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1644 rhs_free = true;
1646 /* Copying parameter passed by reference into gimple register is
1647 probably also going to copy propagate, but we can't be quite
1648 sure. */
1649 if (rhs_free && is_gimple_reg (lhs))
1650 lhs_free = true;
1652 /* Writes to parameters, parameters passed by value and return value
1653 (either dirrectly or passed via invisible reference) are free.
1655 TODO: We ought to handle testcase like
1656 struct a {int a,b;};
1657 struct a
1658 retrurnsturct (void)
1660 struct a a ={1,2};
1661 return a;
1664 This translate into:
1666 retrurnsturct ()
1668 int a$b;
1669 int a$a;
1670 struct a a;
1671 struct a D.2739;
1673 <bb 2>:
1674 D.2739.a = 1;
1675 D.2739.b = 2;
1676 return D.2739;
1679 For that we either need to copy ipa-split logic detecting writes
1680 to return value. */
1681 if (TREE_CODE (inner_lhs) == PARM_DECL
1682 || TREE_CODE (inner_lhs) == RESULT_DECL
1683 || (TREE_CODE (inner_lhs) == MEM_REF
1684 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1685 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1686 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1687 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1688 (inner_lhs,
1689 0))) == RESULT_DECL))))
1690 lhs_free = true;
1691 if (lhs_free
1692 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1693 rhs_free = true;
1694 if (lhs_free && rhs_free)
1695 return 1;
1697 return 0;
1698 default:
1699 return 0;
1704 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1705 predicates to the CFG edges. */
1707 static void
1708 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1709 struct inline_summary *summary,
1710 basic_block bb)
1712 gimple last;
1713 tree op;
1714 int index;
1715 struct agg_position_info aggpos;
1716 enum tree_code code, inverted_code;
1717 edge e;
1718 edge_iterator ei;
1719 gimple set_stmt;
1720 tree op2;
1722 last = last_stmt (bb);
1723 if (!last || gimple_code (last) != GIMPLE_COND)
1724 return;
1725 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1726 return;
1727 op = gimple_cond_lhs (last);
1728 /* TODO: handle conditionals like
1729 var = op0 < 4;
1730 if (var != 0). */
1731 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1733 code = gimple_cond_code (last);
1734 inverted_code
1735 = invert_tree_comparison (code,
1736 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1738 FOR_EACH_EDGE (e, ei, bb->succs)
1740 enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1741 ? code : inverted_code);
1742 /* invert_tree_comparison will return ERROR_MARK on FP
1743 comparsions that are not EQ/NE instead of returning proper
1744 unordered one. Be sure it is not confused with NON_CONSTANT. */
1745 if (this_code != ERROR_MARK)
1747 struct predicate p = add_condition (summary, index, &aggpos,
1748 this_code,
1749 gimple_cond_rhs (last));
1750 e->aux = pool_alloc (edge_predicate_pool);
1751 *(struct predicate *) e->aux = p;
1756 if (TREE_CODE (op) != SSA_NAME)
1757 return;
1758 /* Special case
1759 if (builtin_constant_p (op))
1760 constant_code
1761 else
1762 nonconstant_code.
1763 Here we can predicate nonconstant_code. We can't
1764 really handle constant_code since we have no predicate
1765 for this and also the constant code is not known to be
1766 optimized away when inliner doen't see operand is constant.
1767 Other optimizers might think otherwise. */
1768 if (gimple_cond_code (last) != NE_EXPR
1769 || !integer_zerop (gimple_cond_rhs (last)))
1770 return;
1771 set_stmt = SSA_NAME_DEF_STMT (op);
1772 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1773 || gimple_call_num_args (set_stmt) != 1)
1774 return;
1775 op2 = gimple_call_arg (set_stmt, 0);
1776 if (!unmodified_parm_or_parm_agg_item
1777 (info, set_stmt, op2, &index, &aggpos))
1778 return;
1779 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1781 struct predicate p = add_condition (summary, index, &aggpos,
1782 IS_NOT_CONSTANT, NULL_TREE);
1783 e->aux = pool_alloc (edge_predicate_pool);
1784 *(struct predicate *) e->aux = p;
1789 /* If BB ends by a switch we can turn into predicates, attach corresponding
1790 predicates to the CFG edges. */
1792 static void
1793 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1794 struct inline_summary *summary,
1795 basic_block bb)
1797 gimple last;
1798 tree op;
1799 int index;
1800 struct agg_position_info aggpos;
1801 edge e;
1802 edge_iterator ei;
1803 size_t n;
1804 size_t case_idx;
1806 last = last_stmt (bb);
1807 if (!last || gimple_code (last) != GIMPLE_SWITCH)
1808 return;
1809 op = gimple_switch_index (last);
1810 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1811 return;
1813 FOR_EACH_EDGE (e, ei, bb->succs)
1815 e->aux = pool_alloc (edge_predicate_pool);
1816 *(struct predicate *) e->aux = false_predicate ();
1818 n = gimple_switch_num_labels (last);
1819 for (case_idx = 0; case_idx < n; ++case_idx)
1821 tree cl = gimple_switch_label (last, case_idx);
1822 tree min, max;
1823 struct predicate p;
1825 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1826 min = CASE_LOW (cl);
1827 max = CASE_HIGH (cl);
1829 /* For default we might want to construct predicate that none
1830 of cases is met, but it is bit hard to do not having negations
1831 of conditionals handy. */
1832 if (!min && !max)
1833 p = true_predicate ();
1834 else if (!max)
1835 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1836 else
1838 struct predicate p1, p2;
1839 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1840 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1841 p = and_predicates (summary->conds, &p1, &p2);
1843 *(struct predicate *) e->aux
1844 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1849 /* For each BB in NODE attach to its AUX pointer predicate under
1850 which it is executable. */
1852 static void
1853 compute_bb_predicates (struct cgraph_node *node,
1854 struct ipa_node_params *parms_info,
1855 struct inline_summary *summary)
1857 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1858 bool done = false;
1859 basic_block bb;
1861 FOR_EACH_BB_FN (bb, my_function)
1863 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1864 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1867 /* Entry block is always executable. */
1868 ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1869 = pool_alloc (edge_predicate_pool);
1870 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1871 = true_predicate ();
1873 /* A simple dataflow propagation of predicates forward in the CFG.
1874 TODO: work in reverse postorder. */
1875 while (!done)
1877 done = true;
1878 FOR_EACH_BB_FN (bb, my_function)
1880 struct predicate p = false_predicate ();
1881 edge e;
1882 edge_iterator ei;
1883 FOR_EACH_EDGE (e, ei, bb->preds)
1885 if (e->src->aux)
1887 struct predicate this_bb_predicate
1888 = *(struct predicate *) e->src->aux;
1889 if (e->aux)
1890 this_bb_predicate
1891 = and_predicates (summary->conds, &this_bb_predicate,
1892 (struct predicate *) e->aux);
1893 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1894 if (true_predicate_p (&p))
1895 break;
1898 if (false_predicate_p (&p))
1899 gcc_assert (!bb->aux);
1900 else
1902 if (!bb->aux)
1904 done = false;
1905 bb->aux = pool_alloc (edge_predicate_pool);
1906 *((struct predicate *) bb->aux) = p;
1908 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1910 /* This OR operation is needed to ensure monotonous data flow
1911 in the case we hit the limit on number of clauses and the
1912 and/or operations above give approximate answers. */
1913 p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
1914 if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1916 done = false;
1917 *((struct predicate *) bb->aux) = p;
1926 /* We keep info about constantness of SSA names. */
1928 typedef struct predicate predicate_t;
1929 /* Return predicate specifying when the STMT might have result that is not
1930 a compile time constant. */
1932 static struct predicate
1933 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1934 struct inline_summary *summary,
1935 tree expr,
1936 vec<predicate_t> nonconstant_names)
1938 tree parm;
1939 int index;
1941 while (UNARY_CLASS_P (expr))
1942 expr = TREE_OPERAND (expr, 0);
1944 parm = unmodified_parm (NULL, expr);
1945 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1946 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1947 if (is_gimple_min_invariant (expr))
1948 return false_predicate ();
1949 if (TREE_CODE (expr) == SSA_NAME)
1950 return nonconstant_names[SSA_NAME_VERSION (expr)];
1951 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
1953 struct predicate p1 = will_be_nonconstant_expr_predicate
1954 (info, summary, TREE_OPERAND (expr, 0),
1955 nonconstant_names);
1956 struct predicate p2;
1957 if (true_predicate_p (&p1))
1958 return p1;
1959 p2 = will_be_nonconstant_expr_predicate (info, summary,
1960 TREE_OPERAND (expr, 1),
1961 nonconstant_names);
1962 return or_predicates (summary->conds, &p1, &p2);
1964 else if (TREE_CODE (expr) == COND_EXPR)
1966 struct predicate p1 = will_be_nonconstant_expr_predicate
1967 (info, summary, TREE_OPERAND (expr, 0),
1968 nonconstant_names);
1969 struct predicate p2;
1970 if (true_predicate_p (&p1))
1971 return p1;
1972 p2 = will_be_nonconstant_expr_predicate (info, summary,
1973 TREE_OPERAND (expr, 1),
1974 nonconstant_names);
1975 if (true_predicate_p (&p2))
1976 return p2;
1977 p1 = or_predicates (summary->conds, &p1, &p2);
1978 p2 = will_be_nonconstant_expr_predicate (info, summary,
1979 TREE_OPERAND (expr, 2),
1980 nonconstant_names);
1981 return or_predicates (summary->conds, &p1, &p2);
1983 else
1985 debug_tree (expr);
1986 gcc_unreachable ();
1988 return false_predicate ();
1992 /* Return predicate specifying when the STMT might have result that is not
1993 a compile time constant. */
1995 static struct predicate
1996 will_be_nonconstant_predicate (struct ipa_node_params *info,
1997 struct inline_summary *summary,
1998 gimple stmt,
1999 vec<predicate_t> nonconstant_names)
2001 struct predicate p = true_predicate ();
2002 ssa_op_iter iter;
2003 tree use;
2004 struct predicate op_non_const;
2005 bool is_load;
2006 int base_index;
2007 struct agg_position_info aggpos;
2009 /* What statments might be optimized away
2010 when their arguments are constant
2011 TODO: also trivial builtins.
2012 builtin_constant_p is already handled later. */
2013 if (gimple_code (stmt) != GIMPLE_ASSIGN
2014 && gimple_code (stmt) != GIMPLE_COND
2015 && gimple_code (stmt) != GIMPLE_SWITCH)
2016 return p;
2018 /* Stores will stay anyway. */
2019 if (gimple_store_p (stmt))
2020 return p;
2022 is_load = gimple_assign_load_p (stmt);
2024 /* Loads can be optimized when the value is known. */
2025 if (is_load)
2027 tree op;
2028 gcc_assert (gimple_assign_single_p (stmt));
2029 op = gimple_assign_rhs1 (stmt);
2030 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
2031 &aggpos))
2032 return p;
2034 else
2035 base_index = -1;
2037 /* See if we understand all operands before we start
2038 adding conditionals. */
2039 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2041 tree parm = unmodified_parm (stmt, use);
2042 /* For arguments we can build a condition. */
2043 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2044 continue;
2045 if (TREE_CODE (use) != SSA_NAME)
2046 return p;
2047 /* If we know when operand is constant,
2048 we still can say something useful. */
2049 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2050 continue;
2051 return p;
2054 if (is_load)
2055 op_non_const =
2056 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2057 else
2058 op_non_const = false_predicate ();
2059 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2061 tree parm = unmodified_parm (stmt, use);
2062 int index;
2064 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2066 if (index != base_index)
2067 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2068 else
2069 continue;
2071 else
2072 p = nonconstant_names[SSA_NAME_VERSION (use)];
2073 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2075 if (gimple_code (stmt) == GIMPLE_ASSIGN
2076 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2077 nonconstant_names[SSA_NAME_VERSION (gimple_assign_lhs (stmt))]
2078 = op_non_const;
2079 return op_non_const;
2082 struct record_modified_bb_info
2084 bitmap bb_set;
2085 gimple stmt;
2088 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2089 set except for info->stmt. */
2091 static bool
2092 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2094 struct record_modified_bb_info *info =
2095 (struct record_modified_bb_info *) data;
2096 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2097 return false;
2098 bitmap_set_bit (info->bb_set,
2099 SSA_NAME_IS_DEFAULT_DEF (vdef)
2100 ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2101 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2102 return false;
2105 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2106 will change since last invocation of STMT.
2108 Value 0 is reserved for compile time invariants.
2109 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2110 ought to be REG_BR_PROB_BASE / estimated_iters. */
2112 static int
2113 param_change_prob (gimple stmt, int i)
2115 tree op = gimple_call_arg (stmt, i);
2116 basic_block bb = gimple_bb (stmt);
2117 tree base;
2119 /* Global invariants neve change. */
2120 if (is_gimple_min_invariant (op))
2121 return 0;
2122 /* We would have to do non-trivial analysis to really work out what
2123 is the probability of value to change (i.e. when init statement
2124 is in a sibling loop of the call).
2126 We do an conservative estimate: when call is executed N times more often
2127 than the statement defining value, we take the frequency 1/N. */
2128 if (TREE_CODE (op) == SSA_NAME)
2130 int init_freq;
2132 if (!bb->frequency)
2133 return REG_BR_PROB_BASE;
2135 if (SSA_NAME_IS_DEFAULT_DEF (op))
2136 init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2137 else
2138 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2140 if (!init_freq)
2141 init_freq = 1;
2142 if (init_freq < bb->frequency)
2143 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2144 else
2145 return REG_BR_PROB_BASE;
2148 base = get_base_address (op);
2149 if (base)
2151 ao_ref refd;
2152 int max;
2153 struct record_modified_bb_info info;
2154 bitmap_iterator bi;
2155 unsigned index;
2156 tree init = ctor_for_folding (base);
2158 if (init != error_mark_node)
2159 return 0;
2160 if (!bb->frequency)
2161 return REG_BR_PROB_BASE;
2162 ao_ref_init (&refd, op);
2163 info.stmt = stmt;
2164 info.bb_set = BITMAP_ALLOC (NULL);
2165 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2166 NULL);
2167 if (bitmap_bit_p (info.bb_set, bb->index))
2169 BITMAP_FREE (info.bb_set);
2170 return REG_BR_PROB_BASE;
2173 /* Assume that every memory is initialized at entry.
2174 TODO: Can we easilly determine if value is always defined
2175 and thus we may skip entry block? */
2176 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
2177 max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2178 else
2179 max = 1;
2181 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2182 max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
2184 BITMAP_FREE (info.bb_set);
2185 if (max < bb->frequency)
2186 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2187 else
2188 return REG_BR_PROB_BASE;
2190 return REG_BR_PROB_BASE;
2193 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2194 sub-graph and if the predicate the condition depends on is known. If so,
2195 return true and store the pointer the predicate in *P. */
2197 static bool
2198 phi_result_unknown_predicate (struct ipa_node_params *info,
2199 struct inline_summary *summary, basic_block bb,
2200 struct predicate *p,
2201 vec<predicate_t> nonconstant_names)
2203 edge e;
2204 edge_iterator ei;
2205 basic_block first_bb = NULL;
2206 gimple stmt;
2208 if (single_pred_p (bb))
2210 *p = false_predicate ();
2211 return true;
2214 FOR_EACH_EDGE (e, ei, bb->preds)
2216 if (single_succ_p (e->src))
2218 if (!single_pred_p (e->src))
2219 return false;
2220 if (!first_bb)
2221 first_bb = single_pred (e->src);
2222 else if (single_pred (e->src) != first_bb)
2223 return false;
2225 else
2227 if (!first_bb)
2228 first_bb = e->src;
2229 else if (e->src != first_bb)
2230 return false;
2234 if (!first_bb)
2235 return false;
2237 stmt = last_stmt (first_bb);
2238 if (!stmt
2239 || gimple_code (stmt) != GIMPLE_COND
2240 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2241 return false;
2243 *p = will_be_nonconstant_expr_predicate (info, summary,
2244 gimple_cond_lhs (stmt),
2245 nonconstant_names);
2246 if (true_predicate_p (p))
2247 return false;
2248 else
2249 return true;
2252 /* Given a PHI statement in a function described by inline properties SUMMARY
2253 and *P being the predicate describing whether the selected PHI argument is
2254 known, store a predicate for the result of the PHI statement into
2255 NONCONSTANT_NAMES, if possible. */
2257 static void
2258 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2259 struct predicate *p,
2260 vec<predicate_t> nonconstant_names)
2262 unsigned i;
2264 for (i = 0; i < gimple_phi_num_args (phi); i++)
2266 tree arg = gimple_phi_arg (phi, i)->def;
2267 if (!is_gimple_min_invariant (arg))
2269 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2270 *p = or_predicates (summary->conds, p,
2271 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2272 if (true_predicate_p (p))
2273 return;
2277 if (dump_file && (dump_flags & TDF_DETAILS))
2279 fprintf (dump_file, "\t\tphi predicate: ");
2280 dump_predicate (dump_file, summary->conds, p);
2282 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2285 /* Return predicate specifying when array index in access OP becomes non-constant. */
2287 static struct predicate
2288 array_index_predicate (struct inline_summary *info,
2289 vec< predicate_t> nonconstant_names, tree op)
2291 struct predicate p = false_predicate ();
2292 while (handled_component_p (op))
2294 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2296 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2297 p = or_predicates (info->conds, &p,
2298 &nonconstant_names[SSA_NAME_VERSION
2299 (TREE_OPERAND (op, 1))]);
2301 op = TREE_OPERAND (op, 0);
2303 return p;
2306 /* For a typical usage of __builtin_expect (a<b, 1), we
2307 may introduce an extra relation stmt:
2308 With the builtin, we have
2309 t1 = a <= b;
2310 t2 = (long int) t1;
2311 t3 = __builtin_expect (t2, 1);
2312 if (t3 != 0)
2313 goto ...
2314 Without the builtin, we have
2315 if (a<=b)
2316 goto...
2317 This affects the size/time estimation and may have
2318 an impact on the earlier inlining.
2319 Here find this pattern and fix it up later. */
2321 static gimple
2322 find_foldable_builtin_expect (basic_block bb)
2324 gimple_stmt_iterator bsi;
2326 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2328 gimple stmt = gsi_stmt (bsi);
2329 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2330 || (is_gimple_call (stmt)
2331 && gimple_call_internal_p (stmt)
2332 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
2334 tree var = gimple_call_lhs (stmt);
2335 tree arg = gimple_call_arg (stmt, 0);
2336 use_operand_p use_p;
2337 gimple use_stmt;
2338 bool match = false;
2339 bool done = false;
2341 if (!var || !arg)
2342 continue;
2343 gcc_assert (TREE_CODE (var) == SSA_NAME);
2345 while (TREE_CODE (arg) == SSA_NAME)
2347 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2348 if (!is_gimple_assign (stmt_tmp))
2349 break;
2350 switch (gimple_assign_rhs_code (stmt_tmp))
2352 case LT_EXPR:
2353 case LE_EXPR:
2354 case GT_EXPR:
2355 case GE_EXPR:
2356 case EQ_EXPR:
2357 case NE_EXPR:
2358 match = true;
2359 done = true;
2360 break;
2361 case NOP_EXPR:
2362 break;
2363 default:
2364 done = true;
2365 break;
2367 if (done)
2368 break;
2369 arg = gimple_assign_rhs1 (stmt_tmp);
2372 if (match && single_imm_use (var, &use_p, &use_stmt)
2373 && gimple_code (use_stmt) == GIMPLE_COND)
2374 return use_stmt;
2377 return NULL;
2380 /* Return true when the basic blocks contains only clobbers followed by RESX.
2381 Such BBs are kept around to make removal of dead stores possible with
2382 presence of EH and will be optimized out by optimize_clobbers later in the
2383 game.
2385 NEED_EH is used to recurse in case the clobber has non-EH predecestors
2386 that can be clobber only, too.. When it is false, the RESX is not necessary
2387 on the end of basic block. */
2389 static bool
2390 clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2392 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2393 edge_iterator ei;
2394 edge e;
2396 if (need_eh)
2398 if (gsi_end_p (gsi))
2399 return false;
2400 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2401 return false;
2402 gsi_prev (&gsi);
2404 else if (!single_succ_p (bb))
2405 return false;
2407 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2409 gimple stmt = gsi_stmt (gsi);
2410 if (is_gimple_debug (stmt))
2411 continue;
2412 if (gimple_clobber_p (stmt))
2413 continue;
2414 if (gimple_code (stmt) == GIMPLE_LABEL)
2415 break;
2416 return false;
2419 /* See if all predecestors are either throws or clobber only BBs. */
2420 FOR_EACH_EDGE (e, ei, bb->preds)
2421 if (!(e->flags & EDGE_EH)
2422 && !clobber_only_eh_bb_p (e->src, false))
2423 return false;
2425 return true;
2428 /* Compute function body size parameters for NODE.
2429 When EARLY is true, we compute only simple summaries without
2430 non-trivial predicates to drive the early inliner. */
2432 static void
2433 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2435 gcov_type time = 0;
2436 /* Estimate static overhead for function prologue/epilogue and alignment. */
2437 int overhead = PARAM_VALUE (PARAM_INLINE_FUNCTION_OVERHEAD_SIZE);
2438 int size = overhead;
2439 /* Benefits are scaled by probability of elimination that is in range
2440 <0,2>. */
2441 basic_block bb;
2442 gimple_stmt_iterator bsi;
2443 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2444 int freq;
2445 struct inline_summary *info = inline_summary (node);
2446 struct predicate bb_predicate;
2447 struct ipa_node_params *parms_info = NULL;
2448 vec<predicate_t> nonconstant_names = vNULL;
2449 int nblocks, n;
2450 int *order;
2451 predicate array_index = true_predicate ();
2452 gimple fix_builtin_expect_stmt;
2454 info->conds = NULL;
2455 info->entry = NULL;
2457 if (optimize && !early)
2459 calculate_dominance_info (CDI_DOMINATORS);
2460 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2462 if (ipa_node_params_vector.exists ())
2464 parms_info = IPA_NODE_REF (node);
2465 nonconstant_names.safe_grow_cleared
2466 (SSANAMES (my_function)->length ());
2470 if (dump_file)
2471 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2472 node->name ());
2474 /* When we run into maximal number of entries, we assign everything to the
2475 constant truth case. Be sure to have it in list. */
2476 bb_predicate = true_predicate ();
2477 account_size_time (info, 0, 0, &bb_predicate);
2479 bb_predicate = not_inlined_predicate ();
2480 account_size_time (info, overhead * INLINE_SIZE_SCALE, 0, &bb_predicate);
2482 gcc_assert (my_function && my_function->cfg);
2483 if (parms_info)
2484 compute_bb_predicates (node, parms_info, info);
2485 gcc_assert (cfun == my_function);
2486 order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2487 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2488 for (n = 0; n < nblocks; n++)
2490 bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2491 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2492 if (clobber_only_eh_bb_p (bb))
2494 if (dump_file && (dump_flags & TDF_DETAILS))
2495 fprintf (dump_file, "\n Ignoring BB %i;"
2496 " it will be optimized away by cleanup_clobbers\n",
2497 bb->index);
2498 continue;
2501 /* TODO: Obviously predicates can be propagated down across CFG. */
2502 if (parms_info)
2504 if (bb->aux)
2505 bb_predicate = *(struct predicate *) bb->aux;
2506 else
2507 bb_predicate = false_predicate ();
2509 else
2510 bb_predicate = true_predicate ();
2512 if (dump_file && (dump_flags & TDF_DETAILS))
2514 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2515 dump_predicate (dump_file, info->conds, &bb_predicate);
2518 if (parms_info && nonconstant_names.exists ())
2520 struct predicate phi_predicate;
2521 bool first_phi = true;
2523 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2525 if (first_phi
2526 && !phi_result_unknown_predicate (parms_info, info, bb,
2527 &phi_predicate,
2528 nonconstant_names))
2529 break;
2530 first_phi = false;
2531 if (dump_file && (dump_flags & TDF_DETAILS))
2533 fprintf (dump_file, " ");
2534 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2536 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2537 nonconstant_names);
2541 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2543 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2545 gimple stmt = gsi_stmt (bsi);
2546 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2547 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2548 int prob;
2549 struct predicate will_be_nonconstant;
2551 /* This relation stmt should be folded after we remove
2552 buildin_expect call. Adjust the cost here. */
2553 if (stmt == fix_builtin_expect_stmt)
2555 this_size--;
2556 this_time--;
2559 if (dump_file && (dump_flags & TDF_DETAILS))
2561 fprintf (dump_file, " ");
2562 print_gimple_stmt (dump_file, stmt, 0, 0);
2563 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2564 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2565 this_time);
2568 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2570 struct predicate this_array_index;
2571 this_array_index =
2572 array_index_predicate (info, nonconstant_names,
2573 gimple_assign_rhs1 (stmt));
2574 if (!false_predicate_p (&this_array_index))
2575 array_index =
2576 and_predicates (info->conds, &array_index,
2577 &this_array_index);
2579 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2581 struct predicate this_array_index;
2582 this_array_index =
2583 array_index_predicate (info, nonconstant_names,
2584 gimple_get_lhs (stmt));
2585 if (!false_predicate_p (&this_array_index))
2586 array_index =
2587 and_predicates (info->conds, &array_index,
2588 &this_array_index);
2592 if (is_gimple_call (stmt)
2593 && !gimple_call_internal_p (stmt))
2595 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2596 struct inline_edge_summary *es = inline_edge_summary (edge);
2598 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2599 resolved as constant. We however don't want to optimize
2600 out the cgraph edges. */
2601 if (nonconstant_names.exists ()
2602 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2603 && gimple_call_lhs (stmt)
2604 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2606 struct predicate false_p = false_predicate ();
2607 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2608 = false_p;
2610 if (ipa_node_params_vector.exists ())
2612 int count = gimple_call_num_args (stmt);
2613 int i;
2615 if (count)
2616 es->param.safe_grow_cleared (count);
2617 for (i = 0; i < count; i++)
2619 int prob = param_change_prob (stmt, i);
2620 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2621 es->param[i].change_prob = prob;
2625 es->call_stmt_size = this_size;
2626 es->call_stmt_time = this_time;
2627 es->loop_depth = bb_loop_depth (bb);
2628 edge_set_predicate (edge, &bb_predicate);
2631 /* TODO: When conditional jump or swithc is known to be constant, but
2632 we did not translate it into the predicates, we really can account
2633 just maximum of the possible paths. */
2634 if (parms_info)
2635 will_be_nonconstant
2636 = will_be_nonconstant_predicate (parms_info, info,
2637 stmt, nonconstant_names);
2638 if (this_time || this_size)
2640 struct predicate p;
2642 this_time *= freq;
2644 prob = eliminated_by_inlining_prob (stmt);
2645 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2646 fprintf (dump_file,
2647 "\t\t50%% will be eliminated by inlining\n");
2648 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2649 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2651 if (parms_info)
2652 p = and_predicates (info->conds, &bb_predicate,
2653 &will_be_nonconstant);
2654 else
2655 p = true_predicate ();
2657 if (!false_predicate_p (&p))
2659 time += this_time;
2660 size += this_size;
2661 if (time > MAX_TIME * INLINE_TIME_SCALE)
2662 time = MAX_TIME * INLINE_TIME_SCALE;
2665 /* We account everything but the calls. Calls have their own
2666 size/time info attached to cgraph edges. This is necessary
2667 in order to make the cost disappear after inlining. */
2668 if (!is_gimple_call (stmt))
2670 if (prob)
2672 struct predicate ip = not_inlined_predicate ();
2673 ip = and_predicates (info->conds, &ip, &p);
2674 account_size_time (info, this_size * prob,
2675 this_time * prob, &ip);
2677 if (prob != 2)
2678 account_size_time (info, this_size * (2 - prob),
2679 this_time * (2 - prob), &p);
2682 gcc_assert (time >= 0);
2683 gcc_assert (size >= 0);
2687 set_hint_predicate (&inline_summary (node)->array_index, array_index);
2688 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2689 if (time > MAX_TIME)
2690 time = MAX_TIME;
2691 free (order);
2693 if (!early && nonconstant_names.exists ())
2695 struct loop *loop;
2696 predicate loop_iterations = true_predicate ();
2697 predicate loop_stride = true_predicate ();
2699 if (dump_file && (dump_flags & TDF_DETAILS))
2700 flow_loops_dump (dump_file, NULL, 0);
2701 scev_initialize ();
2702 FOR_EACH_LOOP (loop, 0)
2704 vec<edge> exits;
2705 edge ex;
2706 unsigned int j, i;
2707 struct tree_niter_desc niter_desc;
2708 basic_block *body = get_loop_body (loop);
2709 bb_predicate = *(struct predicate *) loop->header->aux;
2711 exits = get_loop_exit_edges (loop);
2712 FOR_EACH_VEC_ELT (exits, j, ex)
2713 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2714 && !is_gimple_min_invariant (niter_desc.niter))
2716 predicate will_be_nonconstant
2717 = will_be_nonconstant_expr_predicate (parms_info, info,
2718 niter_desc.niter,
2719 nonconstant_names);
2720 if (!true_predicate_p (&will_be_nonconstant))
2721 will_be_nonconstant = and_predicates (info->conds,
2722 &bb_predicate,
2723 &will_be_nonconstant);
2724 if (!true_predicate_p (&will_be_nonconstant)
2725 && !false_predicate_p (&will_be_nonconstant))
2726 /* This is slightly inprecise. We may want to represent each
2727 loop with independent predicate. */
2728 loop_iterations =
2729 and_predicates (info->conds, &loop_iterations,
2730 &will_be_nonconstant);
2732 exits.release ();
2734 for (i = 0; i < loop->num_nodes; i++)
2736 gimple_stmt_iterator gsi;
2737 bb_predicate = *(struct predicate *) body[i]->aux;
2738 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2739 gsi_next (&gsi))
2741 gimple stmt = gsi_stmt (gsi);
2742 affine_iv iv;
2743 ssa_op_iter iter;
2744 tree use;
2746 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2748 predicate will_be_nonconstant;
2750 if (!simple_iv
2751 (loop, loop_containing_stmt (stmt), use, &iv, true)
2752 || is_gimple_min_invariant (iv.step))
2753 continue;
2754 will_be_nonconstant
2755 = will_be_nonconstant_expr_predicate (parms_info, info,
2756 iv.step,
2757 nonconstant_names);
2758 if (!true_predicate_p (&will_be_nonconstant))
2759 will_be_nonconstant
2760 = and_predicates (info->conds,
2761 &bb_predicate,
2762 &will_be_nonconstant);
2763 if (!true_predicate_p (&will_be_nonconstant)
2764 && !false_predicate_p (&will_be_nonconstant))
2765 /* This is slightly inprecise. We may want to represent
2766 each loop with independent predicate. */
2767 loop_stride =
2768 and_predicates (info->conds, &loop_stride,
2769 &will_be_nonconstant);
2773 free (body);
2775 set_hint_predicate (&inline_summary (node)->loop_iterations,
2776 loop_iterations);
2777 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2778 scev_finalize ();
2780 FOR_ALL_BB_FN (bb, my_function)
2782 edge e;
2783 edge_iterator ei;
2785 if (bb->aux)
2786 pool_free (edge_predicate_pool, bb->aux);
2787 bb->aux = NULL;
2788 FOR_EACH_EDGE (e, ei, bb->succs)
2790 if (e->aux)
2791 pool_free (edge_predicate_pool, e->aux);
2792 e->aux = NULL;
2795 inline_summary (node)->self_time = time;
2796 inline_summary (node)->self_size = size;
2797 nonconstant_names.release ();
2798 if (optimize && !early)
2800 loop_optimizer_finalize ();
2801 free_dominance_info (CDI_DOMINATORS);
2803 if (dump_file)
2805 fprintf (dump_file, "\n");
2806 dump_inline_summary (dump_file, node);
2811 /* Compute parameters of functions used by inliner.
2812 EARLY is true when we compute parameters for the early inliner */
2814 void
2815 compute_inline_parameters (struct cgraph_node *node, bool early)
2817 HOST_WIDE_INT self_stack_size;
2818 struct cgraph_edge *e;
2819 struct inline_summary *info;
2821 gcc_assert (!node->global.inlined_to);
2823 inline_summary_alloc ();
2825 info = inline_summary (node);
2826 reset_inline_summary (node);
2828 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2829 Once this happen, we will need to more curefully predict call
2830 statement size. */
2831 if (node->thunk.thunk_p)
2833 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2834 struct predicate t = true_predicate ();
2836 info->inlinable = 0;
2837 node->callees->call_stmt_cannot_inline_p = true;
2838 node->local.can_change_signature = false;
2839 es->call_stmt_time = 1;
2840 es->call_stmt_size = 1;
2841 account_size_time (info, 0, 0, &t);
2842 return;
2845 /* Even is_gimple_min_invariant rely on current_function_decl. */
2846 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2848 /* Estimate the stack size for the function if we're optimizing. */
2849 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2850 info->estimated_self_stack_size = self_stack_size;
2851 info->estimated_stack_size = self_stack_size;
2853 /* Can this function be inlined at all? */
2854 if (!optimize && !lookup_attribute ("always_inline",
2855 DECL_ATTRIBUTES (node->decl)))
2856 info->inlinable = false;
2857 else
2858 info->inlinable = tree_inlinable_function_p (node->decl);
2860 /* Type attributes can use parameter indices to describe them. */
2861 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2862 node->local.can_change_signature = false;
2863 else
2865 /* Otherwise, inlinable functions always can change signature. */
2866 if (info->inlinable)
2867 node->local.can_change_signature = true;
2868 else
2870 /* Functions calling builtin_apply can not change signature. */
2871 for (e = node->callees; e; e = e->next_callee)
2873 tree cdecl = e->callee->decl;
2874 if (DECL_BUILT_IN (cdecl)
2875 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2876 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2877 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2878 break;
2880 node->local.can_change_signature = !e;
2883 estimate_function_body_sizes (node, early);
2885 for (e = node->callees; e; e = e->next_callee)
2886 if (symtab_comdat_local_p (e->callee))
2887 break;
2888 node->calls_comdat_local = (e != NULL);
2890 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2891 info->time = info->self_time;
2892 info->size = info->self_size;
2893 info->estimated_stack_size = info->estimated_self_stack_size;
2894 #ifdef ENABLE_CHECKING
2895 inline_update_overall_summary (node);
2896 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2897 #endif
2899 pop_cfun ();
2903 /* Compute parameters of functions used by inliner using
2904 current_function_decl. */
2906 static unsigned int
2907 compute_inline_parameters_for_current (void)
2909 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2910 return 0;
2913 namespace {
2915 const pass_data pass_data_inline_parameters =
2917 GIMPLE_PASS, /* type */
2918 "inline_param", /* name */
2919 OPTGROUP_INLINE, /* optinfo_flags */
2920 false, /* has_gate */
2921 true, /* has_execute */
2922 TV_INLINE_PARAMETERS, /* tv_id */
2923 0, /* properties_required */
2924 0, /* properties_provided */
2925 0, /* properties_destroyed */
2926 0, /* todo_flags_start */
2927 0, /* todo_flags_finish */
2930 class pass_inline_parameters : public gimple_opt_pass
2932 public:
2933 pass_inline_parameters (gcc::context *ctxt)
2934 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
2937 /* opt_pass methods: */
2938 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
2939 unsigned int execute () {
2940 return compute_inline_parameters_for_current ();
2943 }; // class pass_inline_parameters
2945 } // anon namespace
2947 gimple_opt_pass *
2948 make_pass_inline_parameters (gcc::context *ctxt)
2950 return new pass_inline_parameters (ctxt);
2954 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2955 KNOWN_BINFOS. */
2957 static bool
2958 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2959 int *size, int *time,
2960 vec<tree> known_vals,
2961 vec<tree> known_binfos,
2962 vec<ipa_agg_jump_function_p> known_aggs)
2964 tree target;
2965 struct cgraph_node *callee;
2966 struct inline_summary *isummary;
2968 if (!known_vals.exists () && !known_binfos.exists ())
2969 return false;
2970 if (!flag_indirect_inlining)
2971 return false;
2973 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2974 known_aggs);
2975 if (!target)
2976 return false;
2978 /* Account for difference in cost between indirect and direct calls. */
2979 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
2980 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
2981 gcc_checking_assert (*time >= 0);
2982 gcc_checking_assert (*size >= 0);
2984 callee = cgraph_get_node (target);
2985 if (!callee || !callee->definition)
2986 return false;
2987 isummary = inline_summary (callee);
2988 return isummary->inlinable;
2991 /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
2992 handle edge E with probability PROB.
2993 Set HINTS if edge may be devirtualized.
2994 KNOWN_VALS, KNOWN_AGGS and KNOWN_BINFOS describe context of the call
2995 site. */
2997 static inline void
2998 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
2999 int *time,
3000 int prob,
3001 vec<tree> known_vals,
3002 vec<tree> known_binfos,
3003 vec<ipa_agg_jump_function_p> known_aggs,
3004 inline_hints *hints)
3006 struct inline_edge_summary *es = inline_edge_summary (e);
3007 int call_size = es->call_stmt_size;
3008 int call_time = es->call_stmt_time;
3009 int cur_size;
3010 if (!e->callee
3011 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
3012 known_vals, known_binfos, known_aggs)
3013 && hints && cgraph_maybe_hot_edge_p (e))
3014 *hints |= INLINE_HINT_indirect_call;
3015 cur_size = call_size * INLINE_SIZE_SCALE;
3016 *size += cur_size;
3017 if (min_size)
3018 *min_size += cur_size;
3019 *time += apply_probability ((gcov_type) call_time, prob)
3020 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
3021 if (*time > MAX_TIME * INLINE_TIME_SCALE)
3022 *time = MAX_TIME * INLINE_TIME_SCALE;
3027 /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3028 calls in NODE.
3029 POSSIBLE_TRUTHS, KNOWN_VALS, KNOWN_AGGS and KNOWN_BINFOS describe context of
3030 the call site. */
3032 static void
3033 estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3034 int *min_size, int *time,
3035 inline_hints *hints,
3036 clause_t possible_truths,
3037 vec<tree> known_vals,
3038 vec<tree> known_binfos,
3039 vec<ipa_agg_jump_function_p> known_aggs)
3041 struct cgraph_edge *e;
3042 for (e = node->callees; e; e = e->next_callee)
3044 struct inline_edge_summary *es = inline_edge_summary (e);
3045 if (!es->predicate
3046 || evaluate_predicate (es->predicate, possible_truths))
3048 if (e->inline_failed)
3050 /* Predicates of calls shall not use NOT_CHANGED codes,
3051 sowe do not need to compute probabilities. */
3052 estimate_edge_size_and_time (e, size,
3053 es->predicate ? NULL : min_size,
3054 time, REG_BR_PROB_BASE,
3055 known_vals, known_binfos,
3056 known_aggs, hints);
3058 else
3059 estimate_calls_size_and_time (e->callee, size, min_size, time,
3060 hints,
3061 possible_truths,
3062 known_vals, known_binfos,
3063 known_aggs);
3066 for (e = node->indirect_calls; e; e = e->next_callee)
3068 struct inline_edge_summary *es = inline_edge_summary (e);
3069 if (!es->predicate
3070 || evaluate_predicate (es->predicate, possible_truths))
3071 estimate_edge_size_and_time (e, size,
3072 es->predicate ? NULL : min_size,
3073 time, REG_BR_PROB_BASE,
3074 known_vals, known_binfos, known_aggs,
3075 hints);
3080 /* Estimate size and time needed to execute NODE assuming
3081 POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_BINFOS
3082 information about NODE's arguments. If non-NULL use also probability
3083 information present in INLINE_PARAM_SUMMARY vector.
3084 Additionally detemine hints determined by the context. Finally compute
3085 minimal size needed for the call that is independent on the call context and
3086 can be used for fast estimates. Return the values in RET_SIZE,
3087 RET_MIN_SIZE, RET_TIME and RET_HINTS. */
3089 static void
3090 estimate_node_size_and_time (struct cgraph_node *node,
3091 clause_t possible_truths,
3092 vec<tree> known_vals,
3093 vec<tree> known_binfos,
3094 vec<ipa_agg_jump_function_p> known_aggs,
3095 int *ret_size, int *ret_min_size, int *ret_time,
3096 inline_hints *ret_hints,
3097 vec<inline_param_summary>
3098 inline_param_summary)
3100 struct inline_summary *info = inline_summary (node);
3101 size_time_entry *e;
3102 int size = 0;
3103 int time = 0;
3104 int min_size = 0;
3105 inline_hints hints = 0;
3106 int i;
3108 if (dump_file && (dump_flags & TDF_DETAILS))
3110 bool found = false;
3111 fprintf (dump_file, " Estimating body: %s/%i\n"
3112 " Known to be false: ", node->name (),
3113 node->order);
3115 for (i = predicate_not_inlined_condition;
3116 i < (predicate_first_dynamic_condition
3117 + (int) vec_safe_length (info->conds)); i++)
3118 if (!(possible_truths & (1 << i)))
3120 if (found)
3121 fprintf (dump_file, ", ");
3122 found = true;
3123 dump_condition (dump_file, info->conds, i);
3127 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3128 if (evaluate_predicate (&e->predicate, possible_truths))
3130 size += e->size;
3131 gcc_checking_assert (e->time >= 0);
3132 gcc_checking_assert (time >= 0);
3133 if (!inline_param_summary.exists ())
3134 time += e->time;
3135 else
3137 int prob = predicate_probability (info->conds,
3138 &e->predicate,
3139 possible_truths,
3140 inline_param_summary);
3141 gcc_checking_assert (prob >= 0);
3142 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3143 time += apply_probability ((gcov_type) e->time, prob);
3145 if (time > MAX_TIME * INLINE_TIME_SCALE)
3146 time = MAX_TIME * INLINE_TIME_SCALE;
3147 gcc_checking_assert (time >= 0);
3150 gcc_checking_assert (true_predicate_p (&(*info->entry)[0].predicate));
3151 min_size = (*info->entry)[0].size;
3152 gcc_checking_assert (size >= 0);
3153 gcc_checking_assert (time >= 0);
3155 if (info->loop_iterations
3156 && !evaluate_predicate (info->loop_iterations, possible_truths))
3157 hints |= INLINE_HINT_loop_iterations;
3158 if (info->loop_stride
3159 && !evaluate_predicate (info->loop_stride, possible_truths))
3160 hints |= INLINE_HINT_loop_stride;
3161 if (info->array_index
3162 && !evaluate_predicate (info->array_index, possible_truths))
3163 hints |= INLINE_HINT_array_index;
3164 if (info->scc_no)
3165 hints |= INLINE_HINT_in_scc;
3166 if (DECL_DECLARED_INLINE_P (node->decl))
3167 hints |= INLINE_HINT_declared_inline;
3169 estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
3170 known_vals, known_binfos, known_aggs);
3171 gcc_checking_assert (size >= 0);
3172 gcc_checking_assert (time >= 0);
3173 time = RDIV (time, INLINE_TIME_SCALE);
3174 size = RDIV (size, INLINE_SIZE_SCALE);
3175 min_size = RDIV (min_size, INLINE_SIZE_SCALE);
3177 if (dump_file && (dump_flags & TDF_DETAILS))
3178 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3179 if (ret_time)
3180 *ret_time = time;
3181 if (ret_size)
3182 *ret_size = size;
3183 if (ret_min_size)
3184 *ret_min_size = min_size;
3185 if (ret_hints)
3186 *ret_hints = hints;
3187 return;
3191 /* Estimate size and time needed to execute callee of EDGE assuming that
3192 parameters known to be constant at caller of EDGE are propagated.
3193 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
3194 and types for parameters. */
3196 void
3197 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3198 vec<tree> known_vals,
3199 vec<tree> known_binfos,
3200 vec<ipa_agg_jump_function_p> known_aggs,
3201 int *ret_size, int *ret_time,
3202 inline_hints *hints)
3204 clause_t clause;
3206 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3207 known_aggs);
3208 estimate_node_size_and_time (node, clause, known_vals, known_binfos,
3209 known_aggs, ret_size, NULL, ret_time, hints, vNULL);
3212 /* Translate all conditions from callee representation into caller
3213 representation and symbolically evaluate predicate P into new predicate.
3215 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3216 is summary of function predicate P is from. OPERAND_MAP is array giving
3217 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3218 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3219 predicate under which callee is executed. OFFSET_MAP is an array of of
3220 offsets that need to be added to conditions, negative offset means that
3221 conditions relying on values passed by reference have to be discarded
3222 because they might not be preserved (and should be considered offset zero
3223 for other purposes). */
3225 static struct predicate
3226 remap_predicate (struct inline_summary *info,
3227 struct inline_summary *callee_info,
3228 struct predicate *p,
3229 vec<int> operand_map,
3230 vec<int> offset_map,
3231 clause_t possible_truths, struct predicate *toplev_predicate)
3233 int i;
3234 struct predicate out = true_predicate ();
3236 /* True predicate is easy. */
3237 if (true_predicate_p (p))
3238 return *toplev_predicate;
3239 for (i = 0; p->clause[i]; i++)
3241 clause_t clause = p->clause[i];
3242 int cond;
3243 struct predicate clause_predicate = false_predicate ();
3245 gcc_assert (i < MAX_CLAUSES);
3247 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3248 /* Do we have condition we can't disprove? */
3249 if (clause & possible_truths & (1 << cond))
3251 struct predicate cond_predicate;
3252 /* Work out if the condition can translate to predicate in the
3253 inlined function. */
3254 if (cond >= predicate_first_dynamic_condition)
3256 struct condition *c;
3258 c = &(*callee_info->conds)[cond
3260 predicate_first_dynamic_condition];
3261 /* See if we can remap condition operand to caller's operand.
3262 Otherwise give up. */
3263 if (!operand_map.exists ()
3264 || (int) operand_map.length () <= c->operand_num
3265 || operand_map[c->operand_num] == -1
3266 /* TODO: For non-aggregate conditions, adding an offset is
3267 basically an arithmetic jump function processing which
3268 we should support in future. */
3269 || ((!c->agg_contents || !c->by_ref)
3270 && offset_map[c->operand_num] > 0)
3271 || (c->agg_contents && c->by_ref
3272 && offset_map[c->operand_num] < 0))
3273 cond_predicate = true_predicate ();
3274 else
3276 struct agg_position_info ap;
3277 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3278 if (offset_delta < 0)
3280 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3281 offset_delta = 0;
3283 gcc_assert (!c->agg_contents
3284 || c->by_ref || offset_delta == 0);
3285 ap.offset = c->offset + offset_delta;
3286 ap.agg_contents = c->agg_contents;
3287 ap.by_ref = c->by_ref;
3288 cond_predicate = add_condition (info,
3289 operand_map[c->operand_num],
3290 &ap, c->code, c->val);
3293 /* Fixed conditions remains same, construct single
3294 condition predicate. */
3295 else
3297 cond_predicate.clause[0] = 1 << cond;
3298 cond_predicate.clause[1] = 0;
3300 clause_predicate = or_predicates (info->conds, &clause_predicate,
3301 &cond_predicate);
3303 out = and_predicates (info->conds, &out, &clause_predicate);
3305 return and_predicates (info->conds, &out, toplev_predicate);
3309 /* Update summary information of inline clones after inlining.
3310 Compute peak stack usage. */
3312 static void
3313 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3315 struct cgraph_edge *e;
3316 struct inline_summary *callee_info = inline_summary (node);
3318 /* Pessimistically assume no sharing of stack space. That is, the
3319 frame size of a function is estimated as the original frame size
3320 plus the sum of the frame sizes of all inlined callees. */
3321 inline_summary (node->global.inlined_to)->estimated_stack_size +=
3322 callee_info->estimated_self_stack_size;
3323 ipa_propagate_frequency (node);
3324 for (e = node->callees; e; e = e->next_callee)
3326 if (!e->inline_failed)
3327 inline_update_callee_summaries (e->callee, depth);
3328 inline_edge_summary (e)->loop_depth += depth;
3330 for (e = node->indirect_calls; e; e = e->next_callee)
3331 inline_edge_summary (e)->loop_depth += depth;
3334 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3335 When functoin A is inlined in B and A calls C with parameter that
3336 changes with probability PROB1 and C is known to be passthroug
3337 of argument if B that change with probability PROB2, the probability
3338 of change is now PROB1*PROB2. */
3340 static void
3341 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3342 struct cgraph_edge *edge)
3344 if (ipa_node_params_vector.exists ())
3346 int i;
3347 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3348 struct inline_edge_summary *es = inline_edge_summary (edge);
3349 struct inline_edge_summary *inlined_es
3350 = inline_edge_summary (inlined_edge);
3352 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3354 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3355 if (jfunc->type == IPA_JF_PASS_THROUGH
3356 && (ipa_get_jf_pass_through_formal_id (jfunc)
3357 < (int) inlined_es->param.length ()))
3359 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3360 int prob1 = es->param[i].change_prob;
3361 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3362 int prob = combine_probabilities (prob1, prob2);
3364 if (prob1 && prob2 && !prob)
3365 prob = 1;
3367 es->param[i].change_prob = prob;
3373 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3375 Remap predicates of callees of NODE. Rest of arguments match
3376 remap_predicate.
3378 Also update change probabilities. */
3380 static void
3381 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3382 struct cgraph_node *node,
3383 struct inline_summary *info,
3384 struct inline_summary *callee_info,
3385 vec<int> operand_map,
3386 vec<int> offset_map,
3387 clause_t possible_truths,
3388 struct predicate *toplev_predicate)
3390 struct cgraph_edge *e;
3391 for (e = node->callees; e; e = e->next_callee)
3393 struct inline_edge_summary *es = inline_edge_summary (e);
3394 struct predicate p;
3396 if (e->inline_failed)
3398 remap_edge_change_prob (inlined_edge, e);
3400 if (es->predicate)
3402 p = remap_predicate (info, callee_info,
3403 es->predicate, operand_map, offset_map,
3404 possible_truths, toplev_predicate);
3405 edge_set_predicate (e, &p);
3406 /* TODO: We should remove the edge for code that will be
3407 optimized out, but we need to keep verifiers and tree-inline
3408 happy. Make it cold for now. */
3409 if (false_predicate_p (&p))
3411 e->count = 0;
3412 e->frequency = 0;
3415 else
3416 edge_set_predicate (e, toplev_predicate);
3418 else
3419 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3420 operand_map, offset_map, possible_truths,
3421 toplev_predicate);
3423 for (e = node->indirect_calls; e; e = e->next_callee)
3425 struct inline_edge_summary *es = inline_edge_summary (e);
3426 struct predicate p;
3428 remap_edge_change_prob (inlined_edge, e);
3429 if (es->predicate)
3431 p = remap_predicate (info, callee_info,
3432 es->predicate, operand_map, offset_map,
3433 possible_truths, toplev_predicate);
3434 edge_set_predicate (e, &p);
3435 /* TODO: We should remove the edge for code that will be optimized
3436 out, but we need to keep verifiers and tree-inline happy.
3437 Make it cold for now. */
3438 if (false_predicate_p (&p))
3440 e->count = 0;
3441 e->frequency = 0;
3444 else
3445 edge_set_predicate (e, toplev_predicate);
3449 /* Same as remap_predicate, but set result into hint *HINT. */
3451 static void
3452 remap_hint_predicate (struct inline_summary *info,
3453 struct inline_summary *callee_info,
3454 struct predicate **hint,
3455 vec<int> operand_map,
3456 vec<int> offset_map,
3457 clause_t possible_truths,
3458 struct predicate *toplev_predicate)
3460 predicate p;
3462 if (!*hint)
3463 return;
3464 p = remap_predicate (info, callee_info,
3465 *hint,
3466 operand_map, offset_map,
3467 possible_truths, toplev_predicate);
3468 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3470 if (!*hint)
3471 set_hint_predicate (hint, p);
3472 else
3473 **hint = and_predicates (info->conds, *hint, &p);
3477 /* We inlined EDGE. Update summary of the function we inlined into. */
3479 void
3480 inline_merge_summary (struct cgraph_edge *edge)
3482 struct inline_summary *callee_info = inline_summary (edge->callee);
3483 struct cgraph_node *to = (edge->caller->global.inlined_to
3484 ? edge->caller->global.inlined_to : edge->caller);
3485 struct inline_summary *info = inline_summary (to);
3486 clause_t clause = 0; /* not_inline is known to be false. */
3487 size_time_entry *e;
3488 vec<int> operand_map = vNULL;
3489 vec<int> offset_map = vNULL;
3490 int i;
3491 struct predicate toplev_predicate;
3492 struct predicate true_p = true_predicate ();
3493 struct inline_edge_summary *es = inline_edge_summary (edge);
3495 if (es->predicate)
3496 toplev_predicate = *es->predicate;
3497 else
3498 toplev_predicate = true_predicate ();
3500 if (ipa_node_params_vector.exists () && callee_info->conds)
3502 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3503 int count = ipa_get_cs_argument_count (args);
3504 int i;
3506 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3507 if (count)
3509 operand_map.safe_grow_cleared (count);
3510 offset_map.safe_grow_cleared (count);
3512 for (i = 0; i < count; i++)
3514 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3515 int map = -1;
3517 /* TODO: handle non-NOPs when merging. */
3518 if (jfunc->type == IPA_JF_PASS_THROUGH)
3520 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3521 map = ipa_get_jf_pass_through_formal_id (jfunc);
3522 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3523 offset_map[i] = -1;
3525 else if (jfunc->type == IPA_JF_ANCESTOR)
3527 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3528 if (offset >= 0 && offset < INT_MAX)
3530 map = ipa_get_jf_ancestor_formal_id (jfunc);
3531 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3532 offset = -1;
3533 offset_map[i] = offset;
3536 operand_map[i] = map;
3537 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3540 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3542 struct predicate p = remap_predicate (info, callee_info,
3543 &e->predicate, operand_map,
3544 offset_map, clause,
3545 &toplev_predicate);
3546 if (!false_predicate_p (&p))
3548 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3549 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3550 int prob = predicate_probability (callee_info->conds,
3551 &e->predicate,
3552 clause, es->param);
3553 add_time = apply_probability ((gcov_type) add_time, prob);
3554 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3555 add_time = MAX_TIME * INLINE_TIME_SCALE;
3556 if (prob != REG_BR_PROB_BASE
3557 && dump_file && (dump_flags & TDF_DETAILS))
3559 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3560 (double) prob / REG_BR_PROB_BASE);
3562 account_size_time (info, e->size, add_time, &p);
3565 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3566 offset_map, clause, &toplev_predicate);
3567 remap_hint_predicate (info, callee_info,
3568 &callee_info->loop_iterations,
3569 operand_map, offset_map, clause, &toplev_predicate);
3570 remap_hint_predicate (info, callee_info,
3571 &callee_info->loop_stride,
3572 operand_map, offset_map, clause, &toplev_predicate);
3573 remap_hint_predicate (info, callee_info,
3574 &callee_info->array_index,
3575 operand_map, offset_map, clause, &toplev_predicate);
3577 inline_update_callee_summaries (edge->callee,
3578 inline_edge_summary (edge)->loop_depth);
3580 /* We do not maintain predicates of inlined edges, free it. */
3581 edge_set_predicate (edge, &true_p);
3582 /* Similarly remove param summaries. */
3583 es->param.release ();
3584 operand_map.release ();
3585 offset_map.release ();
3588 /* For performance reasons inline_merge_summary is not updating overall size
3589 and time. Recompute it. */
3591 void
3592 inline_update_overall_summary (struct cgraph_node *node)
3594 struct inline_summary *info = inline_summary (node);
3595 size_time_entry *e;
3596 int i;
3598 info->size = 0;
3599 info->time = 0;
3600 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3602 info->size += e->size, info->time += e->time;
3603 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3604 info->time = MAX_TIME * INLINE_TIME_SCALE;
3606 estimate_calls_size_and_time (node, &info->size, &info->min_size,
3607 &info->time, NULL,
3608 ~(clause_t) (1 << predicate_false_condition),
3609 vNULL, vNULL, vNULL);
3610 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3611 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3614 /* Return hints derrived from EDGE. */
3616 simple_edge_hints (struct cgraph_edge *edge)
3618 int hints = 0;
3619 struct cgraph_node *to = (edge->caller->global.inlined_to
3620 ? edge->caller->global.inlined_to : edge->caller);
3621 if (inline_summary (to)->scc_no
3622 && inline_summary (to)->scc_no == inline_summary (edge->callee)->scc_no
3623 && !cgraph_edge_recursive_p (edge))
3624 hints |= INLINE_HINT_same_scc;
3626 if (to->lto_file_data && edge->callee->lto_file_data
3627 && to->lto_file_data != edge->callee->lto_file_data)
3628 hints |= INLINE_HINT_cross_module;
3630 return hints;
3633 /* Estimate the time cost for the caller when inlining EDGE.
3634 Only to be called via estimate_edge_time, that handles the
3635 caching mechanism.
3637 When caching, also update the cache entry. Compute both time and
3638 size, since we always need both metrics eventually. */
3641 do_estimate_edge_time (struct cgraph_edge *edge)
3643 int time;
3644 int size;
3645 inline_hints hints;
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;
3651 struct inline_edge_summary *es = inline_edge_summary (edge);
3652 int min_size;
3654 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3656 gcc_checking_assert (edge->inline_failed);
3657 evaluate_properties_for_edge (edge, true,
3658 &clause, &known_vals, &known_binfos,
3659 &known_aggs);
3660 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3661 known_aggs, &size, &min_size, &time, &hints, es->param);
3662 known_vals.release ();
3663 known_binfos.release ();
3664 known_aggs.release ();
3665 gcc_checking_assert (size >= 0);
3666 gcc_checking_assert (time >= 0);
3668 /* When caching, update the cache entry. */
3669 if (edge_growth_cache.exists ())
3671 inline_summary (edge->callee)->min_size = min_size;
3672 if ((int) edge_growth_cache.length () <= edge->uid)
3673 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
3674 edge_growth_cache[edge->uid].time = time + (time >= 0);
3676 edge_growth_cache[edge->uid].size = size + (size >= 0);
3677 hints |= simple_edge_hints (edge);
3678 edge_growth_cache[edge->uid].hints = hints + 1;
3680 return time;
3684 /* Return estimated callee growth after inlining EDGE.
3685 Only to be called via estimate_edge_size. */
3688 do_estimate_edge_size (struct cgraph_edge *edge)
3690 int size;
3691 struct cgraph_node *callee;
3692 clause_t clause;
3693 vec<tree> known_vals;
3694 vec<tree> known_binfos;
3695 vec<ipa_agg_jump_function_p> known_aggs;
3697 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3699 if (edge_growth_cache.exists ())
3701 do_estimate_edge_time (edge);
3702 size = edge_growth_cache[edge->uid].size;
3703 gcc_checking_assert (size);
3704 return size - (size > 0);
3707 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3709 /* Early inliner runs without caching, go ahead and do the dirty work. */
3710 gcc_checking_assert (edge->inline_failed);
3711 evaluate_properties_for_edge (edge, true,
3712 &clause, &known_vals, &known_binfos,
3713 &known_aggs);
3714 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3715 known_aggs, &size, NULL, NULL, NULL, vNULL);
3716 known_vals.release ();
3717 known_binfos.release ();
3718 known_aggs.release ();
3719 return size;
3723 /* Estimate the growth of the caller when inlining EDGE.
3724 Only to be called via estimate_edge_size. */
3726 inline_hints
3727 do_estimate_edge_hints (struct cgraph_edge *edge)
3729 inline_hints hints;
3730 struct cgraph_node *callee;
3731 clause_t clause;
3732 vec<tree> known_vals;
3733 vec<tree> known_binfos;
3734 vec<ipa_agg_jump_function_p> known_aggs;
3736 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3738 if (edge_growth_cache.exists ())
3740 do_estimate_edge_time (edge);
3741 hints = edge_growth_cache[edge->uid].hints;
3742 gcc_checking_assert (hints);
3743 return hints - 1;
3746 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3748 /* Early inliner runs without caching, go ahead and do the dirty work. */
3749 gcc_checking_assert (edge->inline_failed);
3750 evaluate_properties_for_edge (edge, true,
3751 &clause, &known_vals, &known_binfos,
3752 &known_aggs);
3753 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3754 known_aggs, NULL, NULL, NULL, &hints, vNULL);
3755 known_vals.release ();
3756 known_binfos.release ();
3757 known_aggs.release ();
3758 hints |= simple_edge_hints (edge);
3759 return hints;
3763 /* Estimate self time of the function NODE after inlining EDGE. */
3766 estimate_time_after_inlining (struct cgraph_node *node,
3767 struct cgraph_edge *edge)
3769 struct inline_edge_summary *es = inline_edge_summary (edge);
3770 if (!es->predicate || !false_predicate_p (es->predicate))
3772 gcov_type time =
3773 inline_summary (node)->time + estimate_edge_time (edge);
3774 if (time < 0)
3775 time = 0;
3776 if (time > MAX_TIME)
3777 time = MAX_TIME;
3778 return time;
3780 return inline_summary (node)->time;
3784 /* Estimate the size of NODE after inlining EDGE which should be an
3785 edge to either NODE or a call inlined into NODE. */
3788 estimate_size_after_inlining (struct cgraph_node *node,
3789 struct cgraph_edge *edge)
3791 struct inline_edge_summary *es = inline_edge_summary (edge);
3792 if (!es->predicate || !false_predicate_p (es->predicate))
3794 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3795 gcc_assert (size >= 0);
3796 return size;
3798 return inline_summary (node)->size;
3802 struct growth_data
3804 struct cgraph_node *node;
3805 bool self_recursive;
3806 int growth;
3810 /* Worker for do_estimate_growth. Collect growth for all callers. */
3812 static bool
3813 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3815 struct cgraph_edge *e;
3816 struct growth_data *d = (struct growth_data *) data;
3818 for (e = node->callers; e; e = e->next_caller)
3820 gcc_checking_assert (e->inline_failed);
3822 if (e->caller == d->node
3823 || (e->caller->global.inlined_to
3824 && e->caller->global.inlined_to == d->node))
3825 d->self_recursive = true;
3826 d->growth += estimate_edge_growth (e);
3828 return false;
3832 /* Estimate the growth caused by inlining NODE into all callees. */
3835 do_estimate_growth (struct cgraph_node *node)
3837 struct growth_data d = { node, 0, false };
3838 struct inline_summary *info = inline_summary (node);
3840 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3842 /* For self recursive functions the growth estimation really should be
3843 infinity. We don't want to return very large values because the growth
3844 plays various roles in badness computation fractions. Be sure to not
3845 return zero or negative growths. */
3846 if (d.self_recursive)
3847 d.growth = d.growth < info->size ? info->size : d.growth;
3848 else if (DECL_EXTERNAL (node->decl))
3850 else
3852 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3853 d.growth -= info->size;
3854 /* COMDAT functions are very often not shared across multiple units
3855 since they come from various template instantiations.
3856 Take this into account. */
3857 else if (DECL_COMDAT (node->decl)
3858 && cgraph_can_remove_if_no_direct_calls_p (node))
3859 d.growth -= (info->size
3860 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3861 + 50) / 100;
3864 if (node_growth_cache.exists ())
3866 if ((int) node_growth_cache.length () <= node->uid)
3867 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
3868 node_growth_cache[node->uid] = d.growth + (d.growth >= 0);
3870 return d.growth;
3874 /* Make cheap estimation if growth of NODE is likely positive knowing
3875 EDGE_GROWTH of one particular edge.
3876 We assume that most of other edges will have similar growth
3877 and skip computation if there are too many callers. */
3879 bool
3880 growth_likely_positive (struct cgraph_node *node, int edge_growth ATTRIBUTE_UNUSED)
3882 int max_callers;
3883 int ret;
3884 struct cgraph_edge *e;
3885 gcc_checking_assert (edge_growth > 0);
3887 /* Unlike for functions called once, we play unsafe with
3888 COMDATs. We can allow that since we know functions
3889 in consideration are small (and thus risk is small) and
3890 moreover grow estimates already accounts that COMDAT
3891 functions may or may not disappear when eliminated from
3892 current unit. With good probability making aggressive
3893 choice in all units is going to make overall program
3894 smaller.
3896 Consequently we ask cgraph_can_remove_if_no_direct_calls_p
3897 instead of
3898 cgraph_will_be_removed_from_program_if_no_direct_calls */
3899 if (DECL_EXTERNAL (node->decl)
3900 || !cgraph_can_remove_if_no_direct_calls_p (node))
3901 return true;
3903 /* If there is cached value, just go ahead. */
3904 if ((int)node_growth_cache.length () > node->uid
3905 && (ret = node_growth_cache[node->uid]))
3906 return ret > 0;
3907 if (!cgraph_will_be_removed_from_program_if_no_direct_calls (node)
3908 && (!DECL_COMDAT (node->decl)
3909 || !cgraph_can_remove_if_no_direct_calls_p (node)))
3910 return true;
3911 max_callers = inline_summary (node)->size * 4 / edge_growth + 2;
3913 for (e = node->callers; e; e = e->next_caller)
3915 max_callers--;
3916 if (!max_callers)
3917 return true;
3919 return estimate_growth (node) > 0;
3923 /* This function performs intraprocedural analysis in NODE that is required to
3924 inline indirect calls. */
3926 static void
3927 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3929 ipa_analyze_node (node);
3930 if (dump_file && (dump_flags & TDF_DETAILS))
3932 ipa_print_node_params (dump_file, node);
3933 ipa_print_node_jump_functions (dump_file, node);
3938 /* Note function body size. */
3940 static void
3941 inline_analyze_function (struct cgraph_node *node)
3943 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3945 if (dump_file)
3946 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3947 node->name (), node->order);
3948 if (optimize && !node->thunk.thunk_p)
3949 inline_indirect_intraprocedural_analysis (node);
3950 compute_inline_parameters (node, false);
3951 if (!optimize)
3953 struct cgraph_edge *e;
3954 for (e = node->callees; e; e = e->next_callee)
3956 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3957 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3958 e->call_stmt_cannot_inline_p = true;
3960 for (e = node->indirect_calls; e; e = e->next_callee)
3962 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3963 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3964 e->call_stmt_cannot_inline_p = true;
3968 pop_cfun ();
3972 /* Called when new function is inserted to callgraph late. */
3974 static void
3975 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3977 inline_analyze_function (node);
3981 /* Note function body size. */
3983 void
3984 inline_generate_summary (void)
3986 struct cgraph_node *node;
3988 /* When not optimizing, do not bother to analyze. Inlining is still done
3989 because edge redirection needs to happen there. */
3990 if (!optimize && !flag_lto && !flag_wpa)
3991 return;
3993 function_insertion_hook_holder =
3994 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3996 ipa_register_cgraph_hooks ();
3997 inline_free_summary ();
3999 FOR_EACH_DEFINED_FUNCTION (node)
4000 if (!node->alias)
4001 inline_analyze_function (node);
4005 /* Read predicate from IB. */
4007 static struct predicate
4008 read_predicate (struct lto_input_block *ib)
4010 struct predicate out;
4011 clause_t clause;
4012 int k = 0;
4016 gcc_assert (k <= MAX_CLAUSES);
4017 clause = out.clause[k++] = streamer_read_uhwi (ib);
4019 while (clause);
4021 /* Zero-initialize the remaining clauses in OUT. */
4022 while (k <= MAX_CLAUSES)
4023 out.clause[k++] = 0;
4025 return out;
4029 /* Write inline summary for edge E to OB. */
4031 static void
4032 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
4034 struct inline_edge_summary *es = inline_edge_summary (e);
4035 struct predicate p;
4036 int length, i;
4038 es->call_stmt_size = streamer_read_uhwi (ib);
4039 es->call_stmt_time = streamer_read_uhwi (ib);
4040 es->loop_depth = streamer_read_uhwi (ib);
4041 p = read_predicate (ib);
4042 edge_set_predicate (e, &p);
4043 length = streamer_read_uhwi (ib);
4044 if (length)
4046 es->param.safe_grow_cleared (length);
4047 for (i = 0; i < length; i++)
4048 es->param[i].change_prob = streamer_read_uhwi (ib);
4053 /* Stream in inline summaries from the section. */
4055 static void
4056 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4057 size_t len)
4059 const struct lto_function_header *header =
4060 (const struct lto_function_header *) data;
4061 const int cfg_offset = sizeof (struct lto_function_header);
4062 const int main_offset = cfg_offset + header->cfg_size;
4063 const int string_offset = main_offset + header->main_size;
4064 struct data_in *data_in;
4065 struct lto_input_block ib;
4066 unsigned int i, count2, j;
4067 unsigned int f_count;
4069 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
4070 header->main_size);
4072 data_in =
4073 lto_data_in_create (file_data, (const char *) data + string_offset,
4074 header->string_size, vNULL);
4075 f_count = streamer_read_uhwi (&ib);
4076 for (i = 0; i < f_count; i++)
4078 unsigned int index;
4079 struct cgraph_node *node;
4080 struct inline_summary *info;
4081 lto_symtab_encoder_t encoder;
4082 struct bitpack_d bp;
4083 struct cgraph_edge *e;
4084 predicate p;
4086 index = streamer_read_uhwi (&ib);
4087 encoder = file_data->symtab_node_encoder;
4088 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4089 info = inline_summary (node);
4091 info->estimated_stack_size
4092 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
4093 info->size = info->self_size = streamer_read_uhwi (&ib);
4094 info->time = info->self_time = streamer_read_uhwi (&ib);
4096 bp = streamer_read_bitpack (&ib);
4097 info->inlinable = bp_unpack_value (&bp, 1);
4099 count2 = streamer_read_uhwi (&ib);
4100 gcc_assert (!info->conds);
4101 for (j = 0; j < count2; j++)
4103 struct condition c;
4104 c.operand_num = streamer_read_uhwi (&ib);
4105 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4106 c.val = stream_read_tree (&ib, data_in);
4107 bp = streamer_read_bitpack (&ib);
4108 c.agg_contents = bp_unpack_value (&bp, 1);
4109 c.by_ref = bp_unpack_value (&bp, 1);
4110 if (c.agg_contents)
4111 c.offset = streamer_read_uhwi (&ib);
4112 vec_safe_push (info->conds, c);
4114 count2 = streamer_read_uhwi (&ib);
4115 gcc_assert (!info->entry);
4116 for (j = 0; j < count2; j++)
4118 struct size_time_entry e;
4120 e.size = streamer_read_uhwi (&ib);
4121 e.time = streamer_read_uhwi (&ib);
4122 e.predicate = read_predicate (&ib);
4124 vec_safe_push (info->entry, e);
4127 p = read_predicate (&ib);
4128 set_hint_predicate (&info->loop_iterations, p);
4129 p = read_predicate (&ib);
4130 set_hint_predicate (&info->loop_stride, p);
4131 p = read_predicate (&ib);
4132 set_hint_predicate (&info->array_index, p);
4133 for (e = node->callees; e; e = e->next_callee)
4134 read_inline_edge_summary (&ib, e);
4135 for (e = node->indirect_calls; e; e = e->next_callee)
4136 read_inline_edge_summary (&ib, e);
4139 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
4140 len);
4141 lto_data_in_delete (data_in);
4145 /* Read inline summary. Jump functions are shared among ipa-cp
4146 and inliner, so when ipa-cp is active, we don't need to write them
4147 twice. */
4149 void
4150 inline_read_summary (void)
4152 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4153 struct lto_file_decl_data *file_data;
4154 unsigned int j = 0;
4156 inline_summary_alloc ();
4158 while ((file_data = file_data_vec[j++]))
4160 size_t len;
4161 const char *data = lto_get_section_data (file_data,
4162 LTO_section_inline_summary,
4163 NULL, &len);
4164 if (data)
4165 inline_read_section (file_data, data, len);
4166 else
4167 /* Fatal error here. We do not want to support compiling ltrans units
4168 with different version of compiler or different flags than the WPA
4169 unit, so this should never happen. */
4170 fatal_error ("ipa inline summary is missing in input file");
4172 if (optimize)
4174 ipa_register_cgraph_hooks ();
4175 if (!flag_ipa_cp)
4176 ipa_prop_read_jump_functions ();
4178 function_insertion_hook_holder =
4179 cgraph_add_function_insertion_hook (&add_new_function, NULL);
4183 /* Write predicate P to OB. */
4185 static void
4186 write_predicate (struct output_block *ob, struct predicate *p)
4188 int j;
4189 if (p)
4190 for (j = 0; p->clause[j]; j++)
4192 gcc_assert (j < MAX_CLAUSES);
4193 streamer_write_uhwi (ob, p->clause[j]);
4195 streamer_write_uhwi (ob, 0);
4199 /* Write inline summary for edge E to OB. */
4201 static void
4202 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4204 struct inline_edge_summary *es = inline_edge_summary (e);
4205 int i;
4207 streamer_write_uhwi (ob, es->call_stmt_size);
4208 streamer_write_uhwi (ob, es->call_stmt_time);
4209 streamer_write_uhwi (ob, es->loop_depth);
4210 write_predicate (ob, es->predicate);
4211 streamer_write_uhwi (ob, es->param.length ());
4212 for (i = 0; i < (int) es->param.length (); i++)
4213 streamer_write_uhwi (ob, es->param[i].change_prob);
4217 /* Write inline summary for node in SET.
4218 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4219 active, we don't need to write them twice. */
4221 void
4222 inline_write_summary (void)
4224 struct cgraph_node *node;
4225 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4226 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4227 unsigned int count = 0;
4228 int i;
4230 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4232 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4233 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4234 if (cnode && cnode->definition && !cnode->alias)
4235 count++;
4237 streamer_write_uhwi (ob, count);
4239 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4241 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4242 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4243 if (cnode && (node = cnode)->definition && !node->alias)
4245 struct inline_summary *info = inline_summary (node);
4246 struct bitpack_d bp;
4247 struct cgraph_edge *edge;
4248 int i;
4249 size_time_entry *e;
4250 struct condition *c;
4252 streamer_write_uhwi (ob,
4253 lto_symtab_encoder_encode (encoder,
4255 node));
4256 streamer_write_hwi (ob, info->estimated_self_stack_size);
4257 streamer_write_hwi (ob, info->self_size);
4258 streamer_write_hwi (ob, info->self_time);
4259 bp = bitpack_create (ob->main_stream);
4260 bp_pack_value (&bp, info->inlinable, 1);
4261 streamer_write_bitpack (&bp);
4262 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4263 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4265 streamer_write_uhwi (ob, c->operand_num);
4266 streamer_write_uhwi (ob, c->code);
4267 stream_write_tree (ob, c->val, true);
4268 bp = bitpack_create (ob->main_stream);
4269 bp_pack_value (&bp, c->agg_contents, 1);
4270 bp_pack_value (&bp, c->by_ref, 1);
4271 streamer_write_bitpack (&bp);
4272 if (c->agg_contents)
4273 streamer_write_uhwi (ob, c->offset);
4275 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4276 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4278 streamer_write_uhwi (ob, e->size);
4279 streamer_write_uhwi (ob, e->time);
4280 write_predicate (ob, &e->predicate);
4282 write_predicate (ob, info->loop_iterations);
4283 write_predicate (ob, info->loop_stride);
4284 write_predicate (ob, info->array_index);
4285 for (edge = node->callees; edge; edge = edge->next_callee)
4286 write_inline_edge_summary (ob, edge);
4287 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4288 write_inline_edge_summary (ob, edge);
4291 streamer_write_char_stream (ob->main_stream, 0);
4292 produce_asm (ob, NULL);
4293 destroy_output_block (ob);
4295 if (optimize && !flag_ipa_cp)
4296 ipa_prop_write_jump_functions ();
4300 /* Release inline summary. */
4302 void
4303 inline_free_summary (void)
4305 struct cgraph_node *node;
4306 if (!inline_edge_summary_vec.exists ())
4307 return;
4308 FOR_EACH_DEFINED_FUNCTION (node)
4309 if (!node->alias)
4310 reset_inline_summary (node);
4311 if (function_insertion_hook_holder)
4312 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
4313 function_insertion_hook_holder = NULL;
4314 if (node_removal_hook_holder)
4315 cgraph_remove_node_removal_hook (node_removal_hook_holder);
4316 node_removal_hook_holder = NULL;
4317 if (edge_removal_hook_holder)
4318 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
4319 edge_removal_hook_holder = NULL;
4320 if (node_duplication_hook_holder)
4321 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
4322 node_duplication_hook_holder = NULL;
4323 if (edge_duplication_hook_holder)
4324 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
4325 edge_duplication_hook_holder = NULL;
4326 vec_free (inline_summary_vec);
4327 inline_edge_summary_vec.release ();
4328 if (edge_predicate_pool)
4329 free_alloc_pool (edge_predicate_pool);
4330 edge_predicate_pool = 0;