Make Linaro GCC4.9-2015.06.
[official-gcc.git] / gcc-4_9-branch / gcc / ipa-inline-analysis.c
blob9e71f43e28d7b6762399745f0b7b5321e01f35cd
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;
865 if (operand_equal_p (TYPE_SIZE (TREE_TYPE (c->val)),
866 TYPE_SIZE (TREE_TYPE (val)), 0))
868 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (c->val), val);
870 res = val
871 ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
872 : NULL;
874 if (res && integer_zerop (res))
875 continue;
877 clause |= 1 << (i + predicate_first_dynamic_condition);
879 return clause;
883 /* Work out what conditions might be true at invocation of E. */
885 static void
886 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
887 clause_t *clause_ptr,
888 vec<tree> *known_vals_ptr,
889 vec<tree> *known_binfos_ptr,
890 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
892 struct cgraph_node *callee =
893 cgraph_function_or_thunk_node (e->callee, NULL);
894 struct inline_summary *info = inline_summary (callee);
895 vec<tree> known_vals = vNULL;
896 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
898 if (clause_ptr)
899 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
900 if (known_vals_ptr)
901 known_vals_ptr->create (0);
902 if (known_binfos_ptr)
903 known_binfos_ptr->create (0);
905 if (ipa_node_params_vector.exists ()
906 && !e->call_stmt_cannot_inline_p
907 && ((clause_ptr && info->conds) || known_vals_ptr || known_binfos_ptr))
909 struct ipa_node_params *parms_info;
910 struct ipa_edge_args *args = IPA_EDGE_REF (e);
911 struct inline_edge_summary *es = inline_edge_summary (e);
912 int i, count = ipa_get_cs_argument_count (args);
914 if (e->caller->global.inlined_to)
915 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
916 else
917 parms_info = IPA_NODE_REF (e->caller);
919 if (count && (info->conds || known_vals_ptr))
920 known_vals.safe_grow_cleared (count);
921 if (count && (info->conds || known_aggs_ptr))
922 known_aggs.safe_grow_cleared (count);
923 if (count && known_binfos_ptr)
924 known_binfos_ptr->safe_grow_cleared (count);
926 for (i = 0; i < count; i++)
928 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
929 tree cst = ipa_value_from_jfunc (parms_info, jf);
930 if (cst)
932 if (known_vals.exists () && TREE_CODE (cst) != TREE_BINFO)
933 known_vals[i] = cst;
934 else if (known_binfos_ptr != NULL
935 && TREE_CODE (cst) == TREE_BINFO)
936 (*known_binfos_ptr)[i] = cst;
938 else if (inline_p && !es->param[i].change_prob)
939 known_vals[i] = error_mark_node;
940 /* TODO: When IPA-CP starts propagating and merging aggregate jump
941 functions, use its knowledge of the caller too, just like the
942 scalar case above. */
943 known_aggs[i] = &jf->agg;
947 if (clause_ptr)
948 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
949 known_vals, known_aggs);
951 if (known_vals_ptr)
952 *known_vals_ptr = known_vals;
953 else
954 known_vals.release ();
956 if (known_aggs_ptr)
957 *known_aggs_ptr = known_aggs;
958 else
959 known_aggs.release ();
963 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
965 static void
966 inline_summary_alloc (void)
968 if (!node_removal_hook_holder)
969 node_removal_hook_holder =
970 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
971 if (!edge_removal_hook_holder)
972 edge_removal_hook_holder =
973 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
974 if (!node_duplication_hook_holder)
975 node_duplication_hook_holder =
976 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
977 if (!edge_duplication_hook_holder)
978 edge_duplication_hook_holder =
979 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
981 if (vec_safe_length (inline_summary_vec) <= (unsigned) cgraph_max_uid)
982 vec_safe_grow_cleared (inline_summary_vec, cgraph_max_uid + 1);
983 if (inline_edge_summary_vec.length () <= (unsigned) cgraph_edge_max_uid)
984 inline_edge_summary_vec.safe_grow_cleared (cgraph_edge_max_uid + 1);
985 if (!edge_predicate_pool)
986 edge_predicate_pool = create_alloc_pool ("edge predicates",
987 sizeof (struct predicate), 10);
990 /* We are called multiple time for given function; clear
991 data from previous run so they are not cumulated. */
993 static void
994 reset_inline_edge_summary (struct cgraph_edge *e)
996 if (e->uid < (int) inline_edge_summary_vec.length ())
998 struct inline_edge_summary *es = inline_edge_summary (e);
1000 es->call_stmt_size = es->call_stmt_time = 0;
1001 if (es->predicate)
1002 pool_free (edge_predicate_pool, es->predicate);
1003 es->predicate = NULL;
1004 es->param.release ();
1008 /* We are called multiple time for given function; clear
1009 data from previous run so they are not cumulated. */
1011 static void
1012 reset_inline_summary (struct cgraph_node *node)
1014 struct inline_summary *info = inline_summary (node);
1015 struct cgraph_edge *e;
1017 info->self_size = info->self_time = 0;
1018 info->estimated_stack_size = 0;
1019 info->estimated_self_stack_size = 0;
1020 info->stack_frame_offset = 0;
1021 info->size = 0;
1022 info->time = 0;
1023 info->growth = 0;
1024 info->scc_no = 0;
1025 if (info->loop_iterations)
1027 pool_free (edge_predicate_pool, info->loop_iterations);
1028 info->loop_iterations = NULL;
1030 if (info->loop_stride)
1032 pool_free (edge_predicate_pool, info->loop_stride);
1033 info->loop_stride = NULL;
1035 if (info->array_index)
1037 pool_free (edge_predicate_pool, info->array_index);
1038 info->array_index = NULL;
1040 vec_free (info->conds);
1041 vec_free (info->entry);
1042 for (e = node->callees; e; e = e->next_callee)
1043 reset_inline_edge_summary (e);
1044 for (e = node->indirect_calls; e; e = e->next_callee)
1045 reset_inline_edge_summary (e);
1048 /* Hook that is called by cgraph.c when a node is removed. */
1050 static void
1051 inline_node_removal_hook (struct cgraph_node *node,
1052 void *data ATTRIBUTE_UNUSED)
1054 struct inline_summary *info;
1055 if (vec_safe_length (inline_summary_vec) <= (unsigned) node->uid)
1056 return;
1057 info = inline_summary (node);
1058 reset_inline_summary (node);
1059 memset (info, 0, sizeof (inline_summary_t));
1062 /* Remap predicate P of former function to be predicate of duplicated function.
1063 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1064 INFO is inline summary of the duplicated node. */
1066 static struct predicate
1067 remap_predicate_after_duplication (struct predicate *p,
1068 clause_t possible_truths,
1069 struct inline_summary *info)
1071 struct predicate new_predicate = true_predicate ();
1072 int j;
1073 for (j = 0; p->clause[j]; j++)
1074 if (!(possible_truths & p->clause[j]))
1076 new_predicate = false_predicate ();
1077 break;
1079 else
1080 add_clause (info->conds, &new_predicate,
1081 possible_truths & p->clause[j]);
1082 return new_predicate;
1085 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1086 Additionally care about allocating new memory slot for updated predicate
1087 and set it to NULL when it becomes true or false (and thus uninteresting).
1090 static void
1091 remap_hint_predicate_after_duplication (struct predicate **p,
1092 clause_t possible_truths,
1093 struct inline_summary *info)
1095 struct predicate new_predicate;
1097 if (!*p)
1098 return;
1100 new_predicate = remap_predicate_after_duplication (*p,
1101 possible_truths, info);
1102 /* We do not want to free previous predicate; it is used by node origin. */
1103 *p = NULL;
1104 set_hint_predicate (p, new_predicate);
1108 /* Hook that is called by cgraph.c when a node is duplicated. */
1110 static void
1111 inline_node_duplication_hook (struct cgraph_node *src,
1112 struct cgraph_node *dst,
1113 ATTRIBUTE_UNUSED void *data)
1115 struct inline_summary *info;
1116 inline_summary_alloc ();
1117 info = inline_summary (dst);
1118 memcpy (info, inline_summary (src), sizeof (struct inline_summary));
1119 /* TODO: as an optimization, we may avoid copying conditions
1120 that are known to be false or true. */
1121 info->conds = vec_safe_copy (info->conds);
1123 /* When there are any replacements in the function body, see if we can figure
1124 out that something was optimized out. */
1125 if (ipa_node_params_vector.exists () && dst->clone.tree_map)
1127 vec<size_time_entry, va_gc> *entry = info->entry;
1128 /* Use SRC parm info since it may not be copied yet. */
1129 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1130 vec<tree> known_vals = vNULL;
1131 int count = ipa_get_param_count (parms_info);
1132 int i, j;
1133 clause_t possible_truths;
1134 struct predicate true_pred = true_predicate ();
1135 size_time_entry *e;
1136 int optimized_out_size = 0;
1137 bool inlined_to_p = false;
1138 struct cgraph_edge *edge;
1140 info->entry = 0;
1141 known_vals.safe_grow_cleared (count);
1142 for (i = 0; i < count; i++)
1144 struct ipa_replace_map *r;
1146 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1148 if (((!r->old_tree && r->parm_num == i)
1149 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1150 && r->replace_p && !r->ref_p)
1152 known_vals[i] = r->new_tree;
1153 break;
1157 possible_truths = evaluate_conditions_for_known_args (dst, false,
1158 known_vals,
1159 vNULL);
1160 known_vals.release ();
1162 account_size_time (info, 0, 0, &true_pred);
1164 /* Remap size_time vectors.
1165 Simplify the predicate by prunning out alternatives that are known
1166 to be false.
1167 TODO: as on optimization, we can also eliminate conditions known
1168 to be true. */
1169 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1171 struct predicate new_predicate;
1172 new_predicate = remap_predicate_after_duplication (&e->predicate,
1173 possible_truths,
1174 info);
1175 if (false_predicate_p (&new_predicate))
1176 optimized_out_size += e->size;
1177 else
1178 account_size_time (info, e->size, e->time, &new_predicate);
1181 /* Remap edge predicates with the same simplification as above.
1182 Also copy constantness arrays. */
1183 for (edge = dst->callees; edge; edge = edge->next_callee)
1185 struct predicate new_predicate;
1186 struct inline_edge_summary *es = inline_edge_summary (edge);
1188 if (!edge->inline_failed)
1189 inlined_to_p = true;
1190 if (!es->predicate)
1191 continue;
1192 new_predicate = remap_predicate_after_duplication (es->predicate,
1193 possible_truths,
1194 info);
1195 if (false_predicate_p (&new_predicate)
1196 && !false_predicate_p (es->predicate))
1198 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1199 edge->frequency = 0;
1201 edge_set_predicate (edge, &new_predicate);
1204 /* Remap indirect edge predicates with the same simplificaiton as above.
1205 Also copy constantness arrays. */
1206 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1208 struct predicate new_predicate;
1209 struct inline_edge_summary *es = inline_edge_summary (edge);
1211 gcc_checking_assert (edge->inline_failed);
1212 if (!es->predicate)
1213 continue;
1214 new_predicate = remap_predicate_after_duplication (es->predicate,
1215 possible_truths,
1216 info);
1217 if (false_predicate_p (&new_predicate)
1218 && !false_predicate_p (es->predicate))
1220 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1221 edge->frequency = 0;
1223 edge_set_predicate (edge, &new_predicate);
1225 remap_hint_predicate_after_duplication (&info->loop_iterations,
1226 possible_truths, info);
1227 remap_hint_predicate_after_duplication (&info->loop_stride,
1228 possible_truths, info);
1229 remap_hint_predicate_after_duplication (&info->array_index,
1230 possible_truths, info);
1232 /* If inliner or someone after inliner will ever start producing
1233 non-trivial clones, we will get trouble with lack of information
1234 about updating self sizes, because size vectors already contains
1235 sizes of the calees. */
1236 gcc_assert (!inlined_to_p || !optimized_out_size);
1238 else
1240 info->entry = vec_safe_copy (info->entry);
1241 if (info->loop_iterations)
1243 predicate p = *info->loop_iterations;
1244 info->loop_iterations = NULL;
1245 set_hint_predicate (&info->loop_iterations, p);
1247 if (info->loop_stride)
1249 predicate p = *info->loop_stride;
1250 info->loop_stride = NULL;
1251 set_hint_predicate (&info->loop_stride, p);
1253 if (info->array_index)
1255 predicate p = *info->array_index;
1256 info->array_index = NULL;
1257 set_hint_predicate (&info->array_index, p);
1260 inline_update_overall_summary (dst);
1264 /* Hook that is called by cgraph.c when a node is duplicated. */
1266 static void
1267 inline_edge_duplication_hook (struct cgraph_edge *src,
1268 struct cgraph_edge *dst,
1269 ATTRIBUTE_UNUSED void *data)
1271 struct inline_edge_summary *info;
1272 struct inline_edge_summary *srcinfo;
1273 inline_summary_alloc ();
1274 info = inline_edge_summary (dst);
1275 srcinfo = inline_edge_summary (src);
1276 memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
1277 info->predicate = NULL;
1278 edge_set_predicate (dst, srcinfo->predicate);
1279 info->param = srcinfo->param.copy ();
1283 /* Keep edge cache consistent across edge removal. */
1285 static void
1286 inline_edge_removal_hook (struct cgraph_edge *edge,
1287 void *data ATTRIBUTE_UNUSED)
1289 if (edge_growth_cache.exists ())
1290 reset_edge_growth_cache (edge);
1291 reset_inline_edge_summary (edge);
1295 /* Initialize growth caches. */
1297 void
1298 initialize_growth_caches (void)
1300 if (cgraph_edge_max_uid)
1301 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
1302 if (cgraph_max_uid)
1303 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
1307 /* Free growth caches. */
1309 void
1310 free_growth_caches (void)
1312 edge_growth_cache.release ();
1313 node_growth_cache.release ();
1317 /* Dump edge summaries associated to NODE and recursively to all clones.
1318 Indent by INDENT. */
1320 static void
1321 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1322 struct inline_summary *info)
1324 struct cgraph_edge *edge;
1325 for (edge = node->callees; edge; edge = edge->next_callee)
1327 struct inline_edge_summary *es = inline_edge_summary (edge);
1328 struct cgraph_node *callee =
1329 cgraph_function_or_thunk_node (edge->callee, NULL);
1330 int i;
1332 fprintf (f,
1333 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1334 " time: %2i callee size:%2i stack:%2i",
1335 indent, "", callee->name (), callee->order,
1336 !edge->inline_failed
1337 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1338 indent, "", es->loop_depth, edge->frequency,
1339 es->call_stmt_size, es->call_stmt_time,
1340 (int) inline_summary (callee)->size / INLINE_SIZE_SCALE,
1341 (int) inline_summary (callee)->estimated_stack_size);
1343 if (es->predicate)
1345 fprintf (f, " predicate: ");
1346 dump_predicate (f, info->conds, es->predicate);
1348 else
1349 fprintf (f, "\n");
1350 if (es->param.exists ())
1351 for (i = 0; i < (int) es->param.length (); i++)
1353 int prob = es->param[i].change_prob;
1355 if (!prob)
1356 fprintf (f, "%*s op%i is compile time invariant\n",
1357 indent + 2, "", i);
1358 else if (prob != REG_BR_PROB_BASE)
1359 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1360 prob * 100.0 / REG_BR_PROB_BASE);
1362 if (!edge->inline_failed)
1364 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1365 " callee size %i\n",
1366 indent + 2, "",
1367 (int) inline_summary (callee)->stack_frame_offset,
1368 (int) inline_summary (callee)->estimated_self_stack_size,
1369 (int) inline_summary (callee)->estimated_stack_size);
1370 dump_inline_edge_summary (f, indent + 2, callee, info);
1373 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1375 struct inline_edge_summary *es = inline_edge_summary (edge);
1376 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1377 " time: %2i",
1378 indent, "",
1379 es->loop_depth,
1380 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1381 if (es->predicate)
1383 fprintf (f, "predicate: ");
1384 dump_predicate (f, info->conds, es->predicate);
1386 else
1387 fprintf (f, "\n");
1392 void
1393 dump_inline_summary (FILE *f, struct cgraph_node *node)
1395 if (node->definition)
1397 struct inline_summary *s = inline_summary (node);
1398 size_time_entry *e;
1399 int i;
1400 fprintf (f, "Inline summary for %s/%i", node->name (),
1401 node->order);
1402 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1403 fprintf (f, " always_inline");
1404 if (s->inlinable)
1405 fprintf (f, " inlinable");
1406 fprintf (f, "\n self time: %i\n", s->self_time);
1407 fprintf (f, " global time: %i\n", s->time);
1408 fprintf (f, " self size: %i\n", s->self_size);
1409 fprintf (f, " global size: %i\n", s->size);
1410 fprintf (f, " min size: %i\n", s->min_size);
1411 fprintf (f, " self stack: %i\n",
1412 (int) s->estimated_self_stack_size);
1413 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1414 if (s->growth)
1415 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1416 if (s->scc_no)
1417 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1418 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1420 fprintf (f, " size:%f, time:%f, predicate:",
1421 (double) e->size / INLINE_SIZE_SCALE,
1422 (double) e->time / INLINE_TIME_SCALE);
1423 dump_predicate (f, s->conds, &e->predicate);
1425 if (s->loop_iterations)
1427 fprintf (f, " loop iterations:");
1428 dump_predicate (f, s->conds, s->loop_iterations);
1430 if (s->loop_stride)
1432 fprintf (f, " loop stride:");
1433 dump_predicate (f, s->conds, s->loop_stride);
1435 if (s->array_index)
1437 fprintf (f, " array index:");
1438 dump_predicate (f, s->conds, s->array_index);
1440 fprintf (f, " calls:\n");
1441 dump_inline_edge_summary (f, 4, node, s);
1442 fprintf (f, "\n");
1446 DEBUG_FUNCTION void
1447 debug_inline_summary (struct cgraph_node *node)
1449 dump_inline_summary (stderr, node);
1452 void
1453 dump_inline_summaries (FILE *f)
1455 struct cgraph_node *node;
1457 FOR_EACH_DEFINED_FUNCTION (node)
1458 if (!node->global.inlined_to)
1459 dump_inline_summary (f, node);
1462 /* Give initial reasons why inlining would fail on EDGE. This gets either
1463 nullified or usually overwritten by more precise reasons later. */
1465 void
1466 initialize_inline_failed (struct cgraph_edge *e)
1468 struct cgraph_node *callee = e->callee;
1470 if (e->indirect_unknown_callee)
1471 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1472 else if (!callee->definition)
1473 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1474 else if (callee->local.redefined_extern_inline)
1475 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1476 else if (e->call_stmt_cannot_inline_p)
1477 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1478 else if (cfun && fn_contains_cilk_spawn_p (cfun))
1479 /* We can't inline if the function is spawing a function. */
1480 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
1481 else
1482 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1485 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1486 boolean variable pointed to by DATA. */
1488 static bool
1489 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1490 void *data)
1492 bool *b = (bool *) data;
1493 *b = true;
1494 return true;
1497 /* If OP refers to value of function parameter, return the corresponding
1498 parameter. */
1500 static tree
1501 unmodified_parm_1 (gimple stmt, tree op)
1503 /* SSA_NAME referring to parm default def? */
1504 if (TREE_CODE (op) == SSA_NAME
1505 && SSA_NAME_IS_DEFAULT_DEF (op)
1506 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1507 return SSA_NAME_VAR (op);
1508 /* Non-SSA parm reference? */
1509 if (TREE_CODE (op) == PARM_DECL)
1511 bool modified = false;
1513 ao_ref refd;
1514 ao_ref_init (&refd, op);
1515 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1516 NULL);
1517 if (!modified)
1518 return op;
1520 return NULL_TREE;
1523 /* If OP refers to value of function parameter, return the corresponding
1524 parameter. Also traverse chains of SSA register assignments. */
1526 static tree
1527 unmodified_parm (gimple stmt, tree op)
1529 tree res = unmodified_parm_1 (stmt, op);
1530 if (res)
1531 return res;
1533 if (TREE_CODE (op) == SSA_NAME
1534 && !SSA_NAME_IS_DEFAULT_DEF (op)
1535 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1536 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1537 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1538 return NULL_TREE;
1541 /* If OP refers to a value of a function parameter or value loaded from an
1542 aggregate passed to a parameter (either by value or reference), return TRUE
1543 and store the number of the parameter to *INDEX_P and information whether
1544 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1545 the function parameters, STMT is the statement in which OP is used or
1546 loaded. */
1548 static bool
1549 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1550 gimple stmt, tree op, int *index_p,
1551 struct agg_position_info *aggpos)
1553 tree res = unmodified_parm_1 (stmt, op);
1555 gcc_checking_assert (aggpos);
1556 if (res)
1558 *index_p = ipa_get_param_decl_index (info, res);
1559 if (*index_p < 0)
1560 return false;
1561 aggpos->agg_contents = false;
1562 aggpos->by_ref = false;
1563 return true;
1566 if (TREE_CODE (op) == SSA_NAME)
1568 if (SSA_NAME_IS_DEFAULT_DEF (op)
1569 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1570 return false;
1571 stmt = SSA_NAME_DEF_STMT (op);
1572 op = gimple_assign_rhs1 (stmt);
1573 if (!REFERENCE_CLASS_P (op))
1574 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1575 aggpos);
1578 aggpos->agg_contents = true;
1579 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1580 &aggpos->by_ref);
1583 /* See if statement might disappear after inlining.
1584 0 - means not eliminated
1585 1 - half of statements goes away
1586 2 - for sure it is eliminated.
1587 We are not terribly sophisticated, basically looking for simple abstraction
1588 penalty wrappers. */
1590 static int
1591 eliminated_by_inlining_prob (gimple stmt)
1593 enum gimple_code code = gimple_code (stmt);
1594 enum tree_code rhs_code;
1596 if (!optimize)
1597 return 0;
1599 switch (code)
1601 case GIMPLE_RETURN:
1602 return 2;
1603 case GIMPLE_ASSIGN:
1604 if (gimple_num_ops (stmt) != 2)
1605 return 0;
1607 rhs_code = gimple_assign_rhs_code (stmt);
1609 /* Casts of parameters, loads from parameters passed by reference
1610 and stores to return value or parameters are often free after
1611 inlining dua to SRA and further combining.
1612 Assume that half of statements goes away. */
1613 if (rhs_code == CONVERT_EXPR
1614 || rhs_code == NOP_EXPR
1615 || rhs_code == VIEW_CONVERT_EXPR
1616 || rhs_code == ADDR_EXPR
1617 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1619 tree rhs = gimple_assign_rhs1 (stmt);
1620 tree lhs = gimple_assign_lhs (stmt);
1621 tree inner_rhs = get_base_address (rhs);
1622 tree inner_lhs = get_base_address (lhs);
1623 bool rhs_free = false;
1624 bool lhs_free = false;
1626 if (!inner_rhs)
1627 inner_rhs = rhs;
1628 if (!inner_lhs)
1629 inner_lhs = lhs;
1631 /* Reads of parameter are expected to be free. */
1632 if (unmodified_parm (stmt, inner_rhs))
1633 rhs_free = true;
1634 /* Match expressions of form &this->field. Those will most likely
1635 combine with something upstream after inlining. */
1636 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1638 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1639 if (TREE_CODE (op) == PARM_DECL)
1640 rhs_free = true;
1641 else if (TREE_CODE (op) == MEM_REF
1642 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1643 rhs_free = true;
1646 /* When parameter is not SSA register because its address is taken
1647 and it is just copied into one, the statement will be completely
1648 free after inlining (we will copy propagate backward). */
1649 if (rhs_free && is_gimple_reg (lhs))
1650 return 2;
1652 /* Reads of parameters passed by reference
1653 expected to be free (i.e. optimized out after inlining). */
1654 if (TREE_CODE (inner_rhs) == MEM_REF
1655 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1656 rhs_free = true;
1658 /* Copying parameter passed by reference into gimple register is
1659 probably also going to copy propagate, but we can't be quite
1660 sure. */
1661 if (rhs_free && is_gimple_reg (lhs))
1662 lhs_free = true;
1664 /* Writes to parameters, parameters passed by value and return value
1665 (either dirrectly or passed via invisible reference) are free.
1667 TODO: We ought to handle testcase like
1668 struct a {int a,b;};
1669 struct a
1670 retrurnsturct (void)
1672 struct a a ={1,2};
1673 return a;
1676 This translate into:
1678 retrurnsturct ()
1680 int a$b;
1681 int a$a;
1682 struct a a;
1683 struct a D.2739;
1685 <bb 2>:
1686 D.2739.a = 1;
1687 D.2739.b = 2;
1688 return D.2739;
1691 For that we either need to copy ipa-split logic detecting writes
1692 to return value. */
1693 if (TREE_CODE (inner_lhs) == PARM_DECL
1694 || TREE_CODE (inner_lhs) == RESULT_DECL
1695 || (TREE_CODE (inner_lhs) == MEM_REF
1696 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1697 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1698 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1699 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1700 (inner_lhs,
1701 0))) == RESULT_DECL))))
1702 lhs_free = true;
1703 if (lhs_free
1704 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1705 rhs_free = true;
1706 if (lhs_free && rhs_free)
1707 return 1;
1709 return 0;
1710 default:
1711 return 0;
1716 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1717 predicates to the CFG edges. */
1719 static void
1720 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1721 struct inline_summary *summary,
1722 basic_block bb)
1724 gimple last;
1725 tree op;
1726 int index;
1727 struct agg_position_info aggpos;
1728 enum tree_code code, inverted_code;
1729 edge e;
1730 edge_iterator ei;
1731 gimple set_stmt;
1732 tree op2;
1734 last = last_stmt (bb);
1735 if (!last || gimple_code (last) != GIMPLE_COND)
1736 return;
1737 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1738 return;
1739 op = gimple_cond_lhs (last);
1740 /* TODO: handle conditionals like
1741 var = op0 < 4;
1742 if (var != 0). */
1743 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1745 code = gimple_cond_code (last);
1746 inverted_code
1747 = invert_tree_comparison (code,
1748 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1750 FOR_EACH_EDGE (e, ei, bb->succs)
1752 enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1753 ? code : inverted_code);
1754 /* invert_tree_comparison will return ERROR_MARK on FP
1755 comparsions that are not EQ/NE instead of returning proper
1756 unordered one. Be sure it is not confused with NON_CONSTANT. */
1757 if (this_code != ERROR_MARK)
1759 struct predicate p = add_condition (summary, index, &aggpos,
1760 this_code,
1761 gimple_cond_rhs (last));
1762 e->aux = pool_alloc (edge_predicate_pool);
1763 *(struct predicate *) e->aux = p;
1768 if (TREE_CODE (op) != SSA_NAME)
1769 return;
1770 /* Special case
1771 if (builtin_constant_p (op))
1772 constant_code
1773 else
1774 nonconstant_code.
1775 Here we can predicate nonconstant_code. We can't
1776 really handle constant_code since we have no predicate
1777 for this and also the constant code is not known to be
1778 optimized away when inliner doen't see operand is constant.
1779 Other optimizers might think otherwise. */
1780 if (gimple_cond_code (last) != NE_EXPR
1781 || !integer_zerop (gimple_cond_rhs (last)))
1782 return;
1783 set_stmt = SSA_NAME_DEF_STMT (op);
1784 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1785 || gimple_call_num_args (set_stmt) != 1)
1786 return;
1787 op2 = gimple_call_arg (set_stmt, 0);
1788 if (!unmodified_parm_or_parm_agg_item
1789 (info, set_stmt, op2, &index, &aggpos))
1790 return;
1791 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1793 struct predicate p = add_condition (summary, index, &aggpos,
1794 IS_NOT_CONSTANT, NULL_TREE);
1795 e->aux = pool_alloc (edge_predicate_pool);
1796 *(struct predicate *) e->aux = p;
1801 /* If BB ends by a switch we can turn into predicates, attach corresponding
1802 predicates to the CFG edges. */
1804 static void
1805 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1806 struct inline_summary *summary,
1807 basic_block bb)
1809 gimple last;
1810 tree op;
1811 int index;
1812 struct agg_position_info aggpos;
1813 edge e;
1814 edge_iterator ei;
1815 size_t n;
1816 size_t case_idx;
1818 last = last_stmt (bb);
1819 if (!last || gimple_code (last) != GIMPLE_SWITCH)
1820 return;
1821 op = gimple_switch_index (last);
1822 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1823 return;
1825 FOR_EACH_EDGE (e, ei, bb->succs)
1827 e->aux = pool_alloc (edge_predicate_pool);
1828 *(struct predicate *) e->aux = false_predicate ();
1830 n = gimple_switch_num_labels (last);
1831 for (case_idx = 0; case_idx < n; ++case_idx)
1833 tree cl = gimple_switch_label (last, case_idx);
1834 tree min, max;
1835 struct predicate p;
1837 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1838 min = CASE_LOW (cl);
1839 max = CASE_HIGH (cl);
1841 /* For default we might want to construct predicate that none
1842 of cases is met, but it is bit hard to do not having negations
1843 of conditionals handy. */
1844 if (!min && !max)
1845 p = true_predicate ();
1846 else if (!max)
1847 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1848 else
1850 struct predicate p1, p2;
1851 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1852 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1853 p = and_predicates (summary->conds, &p1, &p2);
1855 *(struct predicate *) e->aux
1856 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1861 /* For each BB in NODE attach to its AUX pointer predicate under
1862 which it is executable. */
1864 static void
1865 compute_bb_predicates (struct cgraph_node *node,
1866 struct ipa_node_params *parms_info,
1867 struct inline_summary *summary)
1869 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1870 bool done = false;
1871 basic_block bb;
1873 FOR_EACH_BB_FN (bb, my_function)
1875 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1876 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1879 /* Entry block is always executable. */
1880 ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1881 = pool_alloc (edge_predicate_pool);
1882 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1883 = true_predicate ();
1885 /* A simple dataflow propagation of predicates forward in the CFG.
1886 TODO: work in reverse postorder. */
1887 while (!done)
1889 done = true;
1890 FOR_EACH_BB_FN (bb, my_function)
1892 struct predicate p = false_predicate ();
1893 edge e;
1894 edge_iterator ei;
1895 FOR_EACH_EDGE (e, ei, bb->preds)
1897 if (e->src->aux)
1899 struct predicate this_bb_predicate
1900 = *(struct predicate *) e->src->aux;
1901 if (e->aux)
1902 this_bb_predicate
1903 = and_predicates (summary->conds, &this_bb_predicate,
1904 (struct predicate *) e->aux);
1905 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1906 if (true_predicate_p (&p))
1907 break;
1910 if (false_predicate_p (&p))
1911 gcc_assert (!bb->aux);
1912 else
1914 if (!bb->aux)
1916 done = false;
1917 bb->aux = pool_alloc (edge_predicate_pool);
1918 *((struct predicate *) bb->aux) = p;
1920 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1922 /* This OR operation is needed to ensure monotonous data flow
1923 in the case we hit the limit on number of clauses and the
1924 and/or operations above give approximate answers. */
1925 p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
1926 if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1928 done = false;
1929 *((struct predicate *) bb->aux) = p;
1938 /* We keep info about constantness of SSA names. */
1940 typedef struct predicate predicate_t;
1941 /* Return predicate specifying when the STMT might have result that is not
1942 a compile time constant. */
1944 static struct predicate
1945 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1946 struct inline_summary *summary,
1947 tree expr,
1948 vec<predicate_t> nonconstant_names)
1950 tree parm;
1951 int index;
1953 while (UNARY_CLASS_P (expr))
1954 expr = TREE_OPERAND (expr, 0);
1956 parm = unmodified_parm (NULL, expr);
1957 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1958 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1959 if (is_gimple_min_invariant (expr))
1960 return false_predicate ();
1961 if (TREE_CODE (expr) == SSA_NAME)
1962 return nonconstant_names[SSA_NAME_VERSION (expr)];
1963 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
1965 struct predicate p1 = will_be_nonconstant_expr_predicate
1966 (info, summary, TREE_OPERAND (expr, 0),
1967 nonconstant_names);
1968 struct predicate p2;
1969 if (true_predicate_p (&p1))
1970 return p1;
1971 p2 = will_be_nonconstant_expr_predicate (info, summary,
1972 TREE_OPERAND (expr, 1),
1973 nonconstant_names);
1974 return or_predicates (summary->conds, &p1, &p2);
1976 else if (TREE_CODE (expr) == COND_EXPR)
1978 struct predicate p1 = will_be_nonconstant_expr_predicate
1979 (info, summary, TREE_OPERAND (expr, 0),
1980 nonconstant_names);
1981 struct predicate p2;
1982 if (true_predicate_p (&p1))
1983 return p1;
1984 p2 = will_be_nonconstant_expr_predicate (info, summary,
1985 TREE_OPERAND (expr, 1),
1986 nonconstant_names);
1987 if (true_predicate_p (&p2))
1988 return p2;
1989 p1 = or_predicates (summary->conds, &p1, &p2);
1990 p2 = will_be_nonconstant_expr_predicate (info, summary,
1991 TREE_OPERAND (expr, 2),
1992 nonconstant_names);
1993 return or_predicates (summary->conds, &p1, &p2);
1995 else
1997 debug_tree (expr);
1998 gcc_unreachable ();
2000 return false_predicate ();
2004 /* Return predicate specifying when the STMT might have result that is not
2005 a compile time constant. */
2007 static struct predicate
2008 will_be_nonconstant_predicate (struct ipa_node_params *info,
2009 struct inline_summary *summary,
2010 gimple stmt,
2011 vec<predicate_t> nonconstant_names)
2013 struct predicate p = true_predicate ();
2014 ssa_op_iter iter;
2015 tree use;
2016 struct predicate op_non_const;
2017 bool is_load;
2018 int base_index;
2019 struct agg_position_info aggpos;
2021 /* What statments might be optimized away
2022 when their arguments are constant
2023 TODO: also trivial builtins.
2024 builtin_constant_p is already handled later. */
2025 if (gimple_code (stmt) != GIMPLE_ASSIGN
2026 && gimple_code (stmt) != GIMPLE_COND
2027 && gimple_code (stmt) != GIMPLE_SWITCH)
2028 return p;
2030 /* Stores will stay anyway. */
2031 if (gimple_store_p (stmt))
2032 return p;
2034 is_load = gimple_assign_load_p (stmt);
2036 /* Loads can be optimized when the value is known. */
2037 if (is_load)
2039 tree op;
2040 gcc_assert (gimple_assign_single_p (stmt));
2041 op = gimple_assign_rhs1 (stmt);
2042 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
2043 &aggpos))
2044 return p;
2046 else
2047 base_index = -1;
2049 /* See if we understand all operands before we start
2050 adding conditionals. */
2051 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2053 tree parm = unmodified_parm (stmt, use);
2054 /* For arguments we can build a condition. */
2055 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2056 continue;
2057 if (TREE_CODE (use) != SSA_NAME)
2058 return p;
2059 /* If we know when operand is constant,
2060 we still can say something useful. */
2061 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2062 continue;
2063 return p;
2066 if (is_load)
2067 op_non_const =
2068 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2069 else
2070 op_non_const = false_predicate ();
2071 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2073 tree parm = unmodified_parm (stmt, use);
2074 int index;
2076 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2078 if (index != base_index)
2079 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2080 else
2081 continue;
2083 else
2084 p = nonconstant_names[SSA_NAME_VERSION (use)];
2085 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2087 if (gimple_code (stmt) == GIMPLE_ASSIGN
2088 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2089 nonconstant_names[SSA_NAME_VERSION (gimple_assign_lhs (stmt))]
2090 = op_non_const;
2091 return op_non_const;
2094 struct record_modified_bb_info
2096 bitmap bb_set;
2097 gimple stmt;
2100 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2101 set except for info->stmt. */
2103 static bool
2104 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2106 struct record_modified_bb_info *info =
2107 (struct record_modified_bb_info *) data;
2108 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2109 return false;
2110 bitmap_set_bit (info->bb_set,
2111 SSA_NAME_IS_DEFAULT_DEF (vdef)
2112 ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2113 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2114 return false;
2117 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2118 will change since last invocation of STMT.
2120 Value 0 is reserved for compile time invariants.
2121 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2122 ought to be REG_BR_PROB_BASE / estimated_iters. */
2124 static int
2125 param_change_prob (gimple stmt, int i)
2127 tree op = gimple_call_arg (stmt, i);
2128 basic_block bb = gimple_bb (stmt);
2129 tree base;
2131 /* Global invariants neve change. */
2132 if (is_gimple_min_invariant (op))
2133 return 0;
2134 /* We would have to do non-trivial analysis to really work out what
2135 is the probability of value to change (i.e. when init statement
2136 is in a sibling loop of the call).
2138 We do an conservative estimate: when call is executed N times more often
2139 than the statement defining value, we take the frequency 1/N. */
2140 if (TREE_CODE (op) == SSA_NAME)
2142 int init_freq;
2144 if (!bb->frequency)
2145 return REG_BR_PROB_BASE;
2147 if (SSA_NAME_IS_DEFAULT_DEF (op))
2148 init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2149 else
2150 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2152 if (!init_freq)
2153 init_freq = 1;
2154 if (init_freq < bb->frequency)
2155 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2156 else
2157 return REG_BR_PROB_BASE;
2160 base = get_base_address (op);
2161 if (base)
2163 ao_ref refd;
2164 int max;
2165 struct record_modified_bb_info info;
2166 bitmap_iterator bi;
2167 unsigned index;
2168 tree init = ctor_for_folding (base);
2170 if (init != error_mark_node)
2171 return 0;
2172 if (!bb->frequency)
2173 return REG_BR_PROB_BASE;
2174 ao_ref_init (&refd, op);
2175 info.stmt = stmt;
2176 info.bb_set = BITMAP_ALLOC (NULL);
2177 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2178 NULL);
2179 if (bitmap_bit_p (info.bb_set, bb->index))
2181 BITMAP_FREE (info.bb_set);
2182 return REG_BR_PROB_BASE;
2185 /* Assume that every memory is initialized at entry.
2186 TODO: Can we easilly determine if value is always defined
2187 and thus we may skip entry block? */
2188 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
2189 max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2190 else
2191 max = 1;
2193 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2194 max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
2196 BITMAP_FREE (info.bb_set);
2197 if (max < bb->frequency)
2198 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2199 else
2200 return REG_BR_PROB_BASE;
2202 return REG_BR_PROB_BASE;
2205 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2206 sub-graph and if the predicate the condition depends on is known. If so,
2207 return true and store the pointer the predicate in *P. */
2209 static bool
2210 phi_result_unknown_predicate (struct ipa_node_params *info,
2211 struct inline_summary *summary, basic_block bb,
2212 struct predicate *p,
2213 vec<predicate_t> nonconstant_names)
2215 edge e;
2216 edge_iterator ei;
2217 basic_block first_bb = NULL;
2218 gimple stmt;
2220 if (single_pred_p (bb))
2222 *p = false_predicate ();
2223 return true;
2226 FOR_EACH_EDGE (e, ei, bb->preds)
2228 if (single_succ_p (e->src))
2230 if (!single_pred_p (e->src))
2231 return false;
2232 if (!first_bb)
2233 first_bb = single_pred (e->src);
2234 else if (single_pred (e->src) != first_bb)
2235 return false;
2237 else
2239 if (!first_bb)
2240 first_bb = e->src;
2241 else if (e->src != first_bb)
2242 return false;
2246 if (!first_bb)
2247 return false;
2249 stmt = last_stmt (first_bb);
2250 if (!stmt
2251 || gimple_code (stmt) != GIMPLE_COND
2252 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2253 return false;
2255 *p = will_be_nonconstant_expr_predicate (info, summary,
2256 gimple_cond_lhs (stmt),
2257 nonconstant_names);
2258 if (true_predicate_p (p))
2259 return false;
2260 else
2261 return true;
2264 /* Given a PHI statement in a function described by inline properties SUMMARY
2265 and *P being the predicate describing whether the selected PHI argument is
2266 known, store a predicate for the result of the PHI statement into
2267 NONCONSTANT_NAMES, if possible. */
2269 static void
2270 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2271 struct predicate *p,
2272 vec<predicate_t> nonconstant_names)
2274 unsigned i;
2276 for (i = 0; i < gimple_phi_num_args (phi); i++)
2278 tree arg = gimple_phi_arg (phi, i)->def;
2279 if (!is_gimple_min_invariant (arg))
2281 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2282 *p = or_predicates (summary->conds, p,
2283 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2284 if (true_predicate_p (p))
2285 return;
2289 if (dump_file && (dump_flags & TDF_DETAILS))
2291 fprintf (dump_file, "\t\tphi predicate: ");
2292 dump_predicate (dump_file, summary->conds, p);
2294 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2297 /* Return predicate specifying when array index in access OP becomes non-constant. */
2299 static struct predicate
2300 array_index_predicate (struct inline_summary *info,
2301 vec< predicate_t> nonconstant_names, tree op)
2303 struct predicate p = false_predicate ();
2304 while (handled_component_p (op))
2306 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2308 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2309 p = or_predicates (info->conds, &p,
2310 &nonconstant_names[SSA_NAME_VERSION
2311 (TREE_OPERAND (op, 1))]);
2313 op = TREE_OPERAND (op, 0);
2315 return p;
2318 /* For a typical usage of __builtin_expect (a<b, 1), we
2319 may introduce an extra relation stmt:
2320 With the builtin, we have
2321 t1 = a <= b;
2322 t2 = (long int) t1;
2323 t3 = __builtin_expect (t2, 1);
2324 if (t3 != 0)
2325 goto ...
2326 Without the builtin, we have
2327 if (a<=b)
2328 goto...
2329 This affects the size/time estimation and may have
2330 an impact on the earlier inlining.
2331 Here find this pattern and fix it up later. */
2333 static gimple
2334 find_foldable_builtin_expect (basic_block bb)
2336 gimple_stmt_iterator bsi;
2338 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2340 gimple stmt = gsi_stmt (bsi);
2341 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2342 || (is_gimple_call (stmt)
2343 && gimple_call_internal_p (stmt)
2344 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
2346 tree var = gimple_call_lhs (stmt);
2347 tree arg = gimple_call_arg (stmt, 0);
2348 use_operand_p use_p;
2349 gimple use_stmt;
2350 bool match = false;
2351 bool done = false;
2353 if (!var || !arg)
2354 continue;
2355 gcc_assert (TREE_CODE (var) == SSA_NAME);
2357 while (TREE_CODE (arg) == SSA_NAME)
2359 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2360 if (!is_gimple_assign (stmt_tmp))
2361 break;
2362 switch (gimple_assign_rhs_code (stmt_tmp))
2364 case LT_EXPR:
2365 case LE_EXPR:
2366 case GT_EXPR:
2367 case GE_EXPR:
2368 case EQ_EXPR:
2369 case NE_EXPR:
2370 match = true;
2371 done = true;
2372 break;
2373 case NOP_EXPR:
2374 break;
2375 default:
2376 done = true;
2377 break;
2379 if (done)
2380 break;
2381 arg = gimple_assign_rhs1 (stmt_tmp);
2384 if (match && single_imm_use (var, &use_p, &use_stmt)
2385 && gimple_code (use_stmt) == GIMPLE_COND)
2386 return use_stmt;
2389 return NULL;
2392 /* Return true when the basic blocks contains only clobbers followed by RESX.
2393 Such BBs are kept around to make removal of dead stores possible with
2394 presence of EH and will be optimized out by optimize_clobbers later in the
2395 game.
2397 NEED_EH is used to recurse in case the clobber has non-EH predecestors
2398 that can be clobber only, too.. When it is false, the RESX is not necessary
2399 on the end of basic block. */
2401 static bool
2402 clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2404 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2405 edge_iterator ei;
2406 edge e;
2408 if (need_eh)
2410 if (gsi_end_p (gsi))
2411 return false;
2412 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2413 return false;
2414 gsi_prev (&gsi);
2416 else if (!single_succ_p (bb))
2417 return false;
2419 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2421 gimple stmt = gsi_stmt (gsi);
2422 if (is_gimple_debug (stmt))
2423 continue;
2424 if (gimple_clobber_p (stmt))
2425 continue;
2426 if (gimple_code (stmt) == GIMPLE_LABEL)
2427 break;
2428 return false;
2431 /* See if all predecestors are either throws or clobber only BBs. */
2432 FOR_EACH_EDGE (e, ei, bb->preds)
2433 if (!(e->flags & EDGE_EH)
2434 && !clobber_only_eh_bb_p (e->src, false))
2435 return false;
2437 return true;
2440 /* Compute function body size parameters for NODE.
2441 When EARLY is true, we compute only simple summaries without
2442 non-trivial predicates to drive the early inliner. */
2444 static void
2445 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2447 gcov_type time = 0;
2448 /* Estimate static overhead for function prologue/epilogue and alignment. */
2449 int size = 2;
2450 /* Benefits are scaled by probability of elimination that is in range
2451 <0,2>. */
2452 basic_block bb;
2453 gimple_stmt_iterator bsi;
2454 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2455 int freq;
2456 struct inline_summary *info = inline_summary (node);
2457 struct predicate bb_predicate;
2458 struct ipa_node_params *parms_info = NULL;
2459 vec<predicate_t> nonconstant_names = vNULL;
2460 int nblocks, n;
2461 int *order;
2462 predicate array_index = true_predicate ();
2463 gimple fix_builtin_expect_stmt;
2465 info->conds = NULL;
2466 info->entry = NULL;
2468 if (optimize && !early)
2470 calculate_dominance_info (CDI_DOMINATORS);
2471 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2473 if (ipa_node_params_vector.exists ())
2475 parms_info = IPA_NODE_REF (node);
2476 nonconstant_names.safe_grow_cleared
2477 (SSANAMES (my_function)->length ());
2481 if (dump_file)
2482 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2483 node->name ());
2485 /* When we run into maximal number of entries, we assign everything to the
2486 constant truth case. Be sure to have it in list. */
2487 bb_predicate = true_predicate ();
2488 account_size_time (info, 0, 0, &bb_predicate);
2490 bb_predicate = not_inlined_predicate ();
2491 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2493 gcc_assert (my_function && my_function->cfg);
2494 if (parms_info)
2495 compute_bb_predicates (node, parms_info, info);
2496 gcc_assert (cfun == my_function);
2497 order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2498 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2499 for (n = 0; n < nblocks; n++)
2501 bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2502 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2503 if (clobber_only_eh_bb_p (bb))
2505 if (dump_file && (dump_flags & TDF_DETAILS))
2506 fprintf (dump_file, "\n Ignoring BB %i;"
2507 " it will be optimized away by cleanup_clobbers\n",
2508 bb->index);
2509 continue;
2512 /* TODO: Obviously predicates can be propagated down across CFG. */
2513 if (parms_info)
2515 if (bb->aux)
2516 bb_predicate = *(struct predicate *) bb->aux;
2517 else
2518 bb_predicate = false_predicate ();
2520 else
2521 bb_predicate = true_predicate ();
2523 if (dump_file && (dump_flags & TDF_DETAILS))
2525 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2526 dump_predicate (dump_file, info->conds, &bb_predicate);
2529 if (parms_info && nonconstant_names.exists ())
2531 struct predicate phi_predicate;
2532 bool first_phi = true;
2534 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2536 if (first_phi
2537 && !phi_result_unknown_predicate (parms_info, info, bb,
2538 &phi_predicate,
2539 nonconstant_names))
2540 break;
2541 first_phi = false;
2542 if (dump_file && (dump_flags & TDF_DETAILS))
2544 fprintf (dump_file, " ");
2545 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2547 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2548 nonconstant_names);
2552 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2554 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2556 gimple stmt = gsi_stmt (bsi);
2557 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2558 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2559 int prob;
2560 struct predicate will_be_nonconstant;
2562 /* This relation stmt should be folded after we remove
2563 buildin_expect call. Adjust the cost here. */
2564 if (stmt == fix_builtin_expect_stmt)
2566 this_size--;
2567 this_time--;
2570 if (dump_file && (dump_flags & TDF_DETAILS))
2572 fprintf (dump_file, " ");
2573 print_gimple_stmt (dump_file, stmt, 0, 0);
2574 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2575 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2576 this_time);
2579 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2581 struct predicate this_array_index;
2582 this_array_index =
2583 array_index_predicate (info, nonconstant_names,
2584 gimple_assign_rhs1 (stmt));
2585 if (!false_predicate_p (&this_array_index))
2586 array_index =
2587 and_predicates (info->conds, &array_index,
2588 &this_array_index);
2590 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2592 struct predicate this_array_index;
2593 this_array_index =
2594 array_index_predicate (info, nonconstant_names,
2595 gimple_get_lhs (stmt));
2596 if (!false_predicate_p (&this_array_index))
2597 array_index =
2598 and_predicates (info->conds, &array_index,
2599 &this_array_index);
2603 if (is_gimple_call (stmt)
2604 && !gimple_call_internal_p (stmt))
2606 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2607 struct inline_edge_summary *es = inline_edge_summary (edge);
2609 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2610 resolved as constant. We however don't want to optimize
2611 out the cgraph edges. */
2612 if (nonconstant_names.exists ()
2613 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2614 && gimple_call_lhs (stmt)
2615 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2617 struct predicate false_p = false_predicate ();
2618 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2619 = false_p;
2621 if (ipa_node_params_vector.exists ())
2623 int count = gimple_call_num_args (stmt);
2624 int i;
2626 if (count)
2627 es->param.safe_grow_cleared (count);
2628 for (i = 0; i < count; i++)
2630 int prob = param_change_prob (stmt, i);
2631 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2632 es->param[i].change_prob = prob;
2636 es->call_stmt_size = this_size;
2637 es->call_stmt_time = this_time;
2638 es->loop_depth = bb_loop_depth (bb);
2639 edge_set_predicate (edge, &bb_predicate);
2642 /* TODO: When conditional jump or swithc is known to be constant, but
2643 we did not translate it into the predicates, we really can account
2644 just maximum of the possible paths. */
2645 if (parms_info)
2646 will_be_nonconstant
2647 = will_be_nonconstant_predicate (parms_info, info,
2648 stmt, nonconstant_names);
2649 if (this_time || this_size)
2651 struct predicate p;
2653 this_time *= freq;
2655 prob = eliminated_by_inlining_prob (stmt);
2656 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2657 fprintf (dump_file,
2658 "\t\t50%% will be eliminated by inlining\n");
2659 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2660 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2662 if (parms_info)
2663 p = and_predicates (info->conds, &bb_predicate,
2664 &will_be_nonconstant);
2665 else
2666 p = true_predicate ();
2668 if (!false_predicate_p (&p))
2670 time += this_time;
2671 size += this_size;
2672 if (time > MAX_TIME * INLINE_TIME_SCALE)
2673 time = MAX_TIME * INLINE_TIME_SCALE;
2676 /* We account everything but the calls. Calls have their own
2677 size/time info attached to cgraph edges. This is necessary
2678 in order to make the cost disappear after inlining. */
2679 if (!is_gimple_call (stmt))
2681 if (prob)
2683 struct predicate ip = not_inlined_predicate ();
2684 ip = and_predicates (info->conds, &ip, &p);
2685 account_size_time (info, this_size * prob,
2686 this_time * prob, &ip);
2688 if (prob != 2)
2689 account_size_time (info, this_size * (2 - prob),
2690 this_time * (2 - prob), &p);
2693 gcc_assert (time >= 0);
2694 gcc_assert (size >= 0);
2698 set_hint_predicate (&inline_summary (node)->array_index, array_index);
2699 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2700 if (time > MAX_TIME)
2701 time = MAX_TIME;
2702 free (order);
2704 if (!early && nonconstant_names.exists ())
2706 struct loop *loop;
2707 predicate loop_iterations = true_predicate ();
2708 predicate loop_stride = true_predicate ();
2710 if (dump_file && (dump_flags & TDF_DETAILS))
2711 flow_loops_dump (dump_file, NULL, 0);
2712 scev_initialize ();
2713 FOR_EACH_LOOP (loop, 0)
2715 vec<edge> exits;
2716 edge ex;
2717 unsigned int j, i;
2718 struct tree_niter_desc niter_desc;
2719 basic_block *body = get_loop_body (loop);
2720 bb_predicate = *(struct predicate *) loop->header->aux;
2722 exits = get_loop_exit_edges (loop);
2723 FOR_EACH_VEC_ELT (exits, j, ex)
2724 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2725 && !is_gimple_min_invariant (niter_desc.niter))
2727 predicate will_be_nonconstant
2728 = will_be_nonconstant_expr_predicate (parms_info, info,
2729 niter_desc.niter,
2730 nonconstant_names);
2731 if (!true_predicate_p (&will_be_nonconstant))
2732 will_be_nonconstant = and_predicates (info->conds,
2733 &bb_predicate,
2734 &will_be_nonconstant);
2735 if (!true_predicate_p (&will_be_nonconstant)
2736 && !false_predicate_p (&will_be_nonconstant))
2737 /* This is slightly inprecise. We may want to represent each
2738 loop with independent predicate. */
2739 loop_iterations =
2740 and_predicates (info->conds, &loop_iterations,
2741 &will_be_nonconstant);
2743 exits.release ();
2745 for (i = 0; i < loop->num_nodes; i++)
2747 gimple_stmt_iterator gsi;
2748 bb_predicate = *(struct predicate *) body[i]->aux;
2749 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2750 gsi_next (&gsi))
2752 gimple stmt = gsi_stmt (gsi);
2753 affine_iv iv;
2754 ssa_op_iter iter;
2755 tree use;
2757 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2759 predicate will_be_nonconstant;
2761 if (!simple_iv
2762 (loop, loop_containing_stmt (stmt), use, &iv, true)
2763 || is_gimple_min_invariant (iv.step))
2764 continue;
2765 will_be_nonconstant
2766 = will_be_nonconstant_expr_predicate (parms_info, info,
2767 iv.step,
2768 nonconstant_names);
2769 if (!true_predicate_p (&will_be_nonconstant))
2770 will_be_nonconstant
2771 = and_predicates (info->conds,
2772 &bb_predicate,
2773 &will_be_nonconstant);
2774 if (!true_predicate_p (&will_be_nonconstant)
2775 && !false_predicate_p (&will_be_nonconstant))
2776 /* This is slightly inprecise. We may want to represent
2777 each loop with independent predicate. */
2778 loop_stride =
2779 and_predicates (info->conds, &loop_stride,
2780 &will_be_nonconstant);
2784 free (body);
2786 set_hint_predicate (&inline_summary (node)->loop_iterations,
2787 loop_iterations);
2788 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2789 scev_finalize ();
2791 FOR_ALL_BB_FN (bb, my_function)
2793 edge e;
2794 edge_iterator ei;
2796 if (bb->aux)
2797 pool_free (edge_predicate_pool, bb->aux);
2798 bb->aux = NULL;
2799 FOR_EACH_EDGE (e, ei, bb->succs)
2801 if (e->aux)
2802 pool_free (edge_predicate_pool, e->aux);
2803 e->aux = NULL;
2806 inline_summary (node)->self_time = time;
2807 inline_summary (node)->self_size = size;
2808 nonconstant_names.release ();
2809 if (optimize && !early)
2811 loop_optimizer_finalize ();
2812 free_dominance_info (CDI_DOMINATORS);
2814 if (dump_file)
2816 fprintf (dump_file, "\n");
2817 dump_inline_summary (dump_file, node);
2822 /* Compute parameters of functions used by inliner.
2823 EARLY is true when we compute parameters for the early inliner */
2825 void
2826 compute_inline_parameters (struct cgraph_node *node, bool early)
2828 HOST_WIDE_INT self_stack_size;
2829 struct cgraph_edge *e;
2830 struct inline_summary *info;
2832 gcc_assert (!node->global.inlined_to);
2834 inline_summary_alloc ();
2836 info = inline_summary (node);
2837 reset_inline_summary (node);
2839 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2840 Once this happen, we will need to more curefully predict call
2841 statement size. */
2842 if (node->thunk.thunk_p)
2844 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2845 struct predicate t = true_predicate ();
2847 info->inlinable = 0;
2848 node->callees->call_stmt_cannot_inline_p = true;
2849 node->local.can_change_signature = false;
2850 es->call_stmt_time = 1;
2851 es->call_stmt_size = 1;
2852 account_size_time (info, 0, 0, &t);
2853 return;
2856 /* Even is_gimple_min_invariant rely on current_function_decl. */
2857 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2859 /* Estimate the stack size for the function if we're optimizing. */
2860 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2861 info->estimated_self_stack_size = self_stack_size;
2862 info->estimated_stack_size = self_stack_size;
2863 info->stack_frame_offset = 0;
2865 /* Can this function be inlined at all? */
2866 if (!optimize && !lookup_attribute ("always_inline",
2867 DECL_ATTRIBUTES (node->decl)))
2868 info->inlinable = false;
2869 else
2870 info->inlinable = tree_inlinable_function_p (node->decl);
2872 /* Type attributes can use parameter indices to describe them. */
2873 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2874 node->local.can_change_signature = false;
2875 else
2877 /* Otherwise, inlinable functions always can change signature. */
2878 if (info->inlinable)
2879 node->local.can_change_signature = true;
2880 else
2882 /* Functions calling builtin_apply can not change signature. */
2883 for (e = node->callees; e; e = e->next_callee)
2885 tree cdecl = e->callee->decl;
2886 if (DECL_BUILT_IN (cdecl)
2887 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2888 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2889 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2890 break;
2892 node->local.can_change_signature = !e;
2895 estimate_function_body_sizes (node, early);
2897 for (e = node->callees; e; e = e->next_callee)
2898 if (symtab_comdat_local_p (e->callee))
2899 break;
2900 node->calls_comdat_local = (e != NULL);
2902 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2903 info->time = info->self_time;
2904 info->size = info->self_size;
2905 info->stack_frame_offset = 0;
2906 info->estimated_stack_size = info->estimated_self_stack_size;
2907 #ifdef ENABLE_CHECKING
2908 inline_update_overall_summary (node);
2909 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2910 #endif
2912 pop_cfun ();
2916 /* Compute parameters of functions used by inliner using
2917 current_function_decl. */
2919 static unsigned int
2920 compute_inline_parameters_for_current (void)
2922 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2923 return 0;
2926 namespace {
2928 const pass_data pass_data_inline_parameters =
2930 GIMPLE_PASS, /* type */
2931 "inline_param", /* name */
2932 OPTGROUP_INLINE, /* optinfo_flags */
2933 false, /* has_gate */
2934 true, /* has_execute */
2935 TV_INLINE_PARAMETERS, /* tv_id */
2936 0, /* properties_required */
2937 0, /* properties_provided */
2938 0, /* properties_destroyed */
2939 0, /* todo_flags_start */
2940 0, /* todo_flags_finish */
2943 class pass_inline_parameters : public gimple_opt_pass
2945 public:
2946 pass_inline_parameters (gcc::context *ctxt)
2947 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
2950 /* opt_pass methods: */
2951 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
2952 unsigned int execute () {
2953 return compute_inline_parameters_for_current ();
2956 }; // class pass_inline_parameters
2958 } // anon namespace
2960 gimple_opt_pass *
2961 make_pass_inline_parameters (gcc::context *ctxt)
2963 return new pass_inline_parameters (ctxt);
2967 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2968 KNOWN_BINFOS. */
2970 static bool
2971 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2972 int *size, int *time,
2973 vec<tree> known_vals,
2974 vec<tree> known_binfos,
2975 vec<ipa_agg_jump_function_p> known_aggs)
2977 tree target;
2978 struct cgraph_node *callee;
2979 struct inline_summary *isummary;
2981 if (!known_vals.exists () && !known_binfos.exists ())
2982 return false;
2983 if (!flag_indirect_inlining)
2984 return false;
2986 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2987 known_aggs);
2988 if (!target)
2989 return false;
2991 /* Account for difference in cost between indirect and direct calls. */
2992 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
2993 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
2994 gcc_checking_assert (*time >= 0);
2995 gcc_checking_assert (*size >= 0);
2997 callee = cgraph_get_node (target);
2998 if (!callee || !callee->definition)
2999 return false;
3000 isummary = inline_summary (callee);
3001 return isummary->inlinable;
3004 /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3005 handle edge E with probability PROB.
3006 Set HINTS if edge may be devirtualized.
3007 KNOWN_VALS, KNOWN_AGGS and KNOWN_BINFOS describe context of the call
3008 site. */
3010 static inline void
3011 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3012 int *time,
3013 int prob,
3014 vec<tree> known_vals,
3015 vec<tree> known_binfos,
3016 vec<ipa_agg_jump_function_p> known_aggs,
3017 inline_hints *hints)
3019 struct inline_edge_summary *es = inline_edge_summary (e);
3020 int call_size = es->call_stmt_size;
3021 int call_time = es->call_stmt_time;
3022 int cur_size;
3023 if (!e->callee
3024 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
3025 known_vals, known_binfos, known_aggs)
3026 && hints && cgraph_maybe_hot_edge_p (e))
3027 *hints |= INLINE_HINT_indirect_call;
3028 cur_size = call_size * INLINE_SIZE_SCALE;
3029 *size += cur_size;
3030 if (min_size)
3031 *min_size += cur_size;
3032 *time += apply_probability ((gcov_type) call_time, prob)
3033 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
3034 if (*time > MAX_TIME * INLINE_TIME_SCALE)
3035 *time = MAX_TIME * INLINE_TIME_SCALE;
3040 /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3041 calls in NODE.
3042 POSSIBLE_TRUTHS, KNOWN_VALS, KNOWN_AGGS and KNOWN_BINFOS describe context of
3043 the call site. */
3045 static void
3046 estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3047 int *min_size, int *time,
3048 inline_hints *hints,
3049 clause_t possible_truths,
3050 vec<tree> known_vals,
3051 vec<tree> known_binfos,
3052 vec<ipa_agg_jump_function_p> known_aggs)
3054 struct cgraph_edge *e;
3055 for (e = node->callees; e; e = e->next_callee)
3057 struct inline_edge_summary *es = inline_edge_summary (e);
3058 if (!es->predicate
3059 || evaluate_predicate (es->predicate, possible_truths))
3061 if (e->inline_failed)
3063 /* Predicates of calls shall not use NOT_CHANGED codes,
3064 sowe do not need to compute probabilities. */
3065 estimate_edge_size_and_time (e, size,
3066 es->predicate ? NULL : min_size,
3067 time, REG_BR_PROB_BASE,
3068 known_vals, known_binfos,
3069 known_aggs, hints);
3071 else
3072 estimate_calls_size_and_time (e->callee, size, min_size, time,
3073 hints,
3074 possible_truths,
3075 known_vals, known_binfos,
3076 known_aggs);
3079 for (e = node->indirect_calls; e; e = e->next_callee)
3081 struct inline_edge_summary *es = inline_edge_summary (e);
3082 if (!es->predicate
3083 || evaluate_predicate (es->predicate, possible_truths))
3084 estimate_edge_size_and_time (e, size,
3085 es->predicate ? NULL : min_size,
3086 time, REG_BR_PROB_BASE,
3087 known_vals, known_binfos, known_aggs,
3088 hints);
3093 /* Estimate size and time needed to execute NODE assuming
3094 POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_BINFOS
3095 information about NODE's arguments. If non-NULL use also probability
3096 information present in INLINE_PARAM_SUMMARY vector.
3097 Additionally detemine hints determined by the context. Finally compute
3098 minimal size needed for the call that is independent on the call context and
3099 can be used for fast estimates. Return the values in RET_SIZE,
3100 RET_MIN_SIZE, RET_TIME and RET_HINTS. */
3102 static void
3103 estimate_node_size_and_time (struct cgraph_node *node,
3104 clause_t possible_truths,
3105 vec<tree> known_vals,
3106 vec<tree> known_binfos,
3107 vec<ipa_agg_jump_function_p> known_aggs,
3108 int *ret_size, int *ret_min_size, int *ret_time,
3109 inline_hints *ret_hints,
3110 vec<inline_param_summary>
3111 inline_param_summary)
3113 struct inline_summary *info = inline_summary (node);
3114 size_time_entry *e;
3115 int size = 0;
3116 int time = 0;
3117 int min_size = 0;
3118 inline_hints hints = 0;
3119 int i;
3121 if (dump_file && (dump_flags & TDF_DETAILS))
3123 bool found = false;
3124 fprintf (dump_file, " Estimating body: %s/%i\n"
3125 " Known to be false: ", node->name (),
3126 node->order);
3128 for (i = predicate_not_inlined_condition;
3129 i < (predicate_first_dynamic_condition
3130 + (int) vec_safe_length (info->conds)); i++)
3131 if (!(possible_truths & (1 << i)))
3133 if (found)
3134 fprintf (dump_file, ", ");
3135 found = true;
3136 dump_condition (dump_file, info->conds, i);
3140 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3141 if (evaluate_predicate (&e->predicate, possible_truths))
3143 size += e->size;
3144 gcc_checking_assert (e->time >= 0);
3145 gcc_checking_assert (time >= 0);
3146 if (!inline_param_summary.exists ())
3147 time += e->time;
3148 else
3150 int prob = predicate_probability (info->conds,
3151 &e->predicate,
3152 possible_truths,
3153 inline_param_summary);
3154 gcc_checking_assert (prob >= 0);
3155 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3156 time += apply_probability ((gcov_type) e->time, prob);
3158 if (time > MAX_TIME * INLINE_TIME_SCALE)
3159 time = MAX_TIME * INLINE_TIME_SCALE;
3160 gcc_checking_assert (time >= 0);
3163 gcc_checking_assert (true_predicate_p (&(*info->entry)[0].predicate));
3164 min_size = (*info->entry)[0].size;
3165 gcc_checking_assert (size >= 0);
3166 gcc_checking_assert (time >= 0);
3168 if (info->loop_iterations
3169 && !evaluate_predicate (info->loop_iterations, possible_truths))
3170 hints |= INLINE_HINT_loop_iterations;
3171 if (info->loop_stride
3172 && !evaluate_predicate (info->loop_stride, possible_truths))
3173 hints |= INLINE_HINT_loop_stride;
3174 if (info->array_index
3175 && !evaluate_predicate (info->array_index, possible_truths))
3176 hints |= INLINE_HINT_array_index;
3177 if (info->scc_no)
3178 hints |= INLINE_HINT_in_scc;
3179 if (DECL_DECLARED_INLINE_P (node->decl))
3180 hints |= INLINE_HINT_declared_inline;
3182 estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
3183 known_vals, known_binfos, known_aggs);
3184 gcc_checking_assert (size >= 0);
3185 gcc_checking_assert (time >= 0);
3186 time = RDIV (time, INLINE_TIME_SCALE);
3187 size = RDIV (size, INLINE_SIZE_SCALE);
3188 min_size = RDIV (min_size, INLINE_SIZE_SCALE);
3190 if (dump_file && (dump_flags & TDF_DETAILS))
3191 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3192 if (ret_time)
3193 *ret_time = time;
3194 if (ret_size)
3195 *ret_size = size;
3196 if (ret_min_size)
3197 *ret_min_size = min_size;
3198 if (ret_hints)
3199 *ret_hints = hints;
3200 return;
3204 /* Estimate size and time needed to execute callee of EDGE assuming that
3205 parameters known to be constant at caller of EDGE are propagated.
3206 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
3207 and types for parameters. */
3209 void
3210 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3211 vec<tree> known_vals,
3212 vec<tree> known_binfos,
3213 vec<ipa_agg_jump_function_p> known_aggs,
3214 int *ret_size, int *ret_time,
3215 inline_hints *hints)
3217 clause_t clause;
3219 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3220 known_aggs);
3221 estimate_node_size_and_time (node, clause, known_vals, known_binfos,
3222 known_aggs, ret_size, NULL, ret_time, hints, vNULL);
3225 /* Translate all conditions from callee representation into caller
3226 representation and symbolically evaluate predicate P into new predicate.
3228 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3229 is summary of function predicate P is from. OPERAND_MAP is array giving
3230 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3231 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3232 predicate under which callee is executed. OFFSET_MAP is an array of of
3233 offsets that need to be added to conditions, negative offset means that
3234 conditions relying on values passed by reference have to be discarded
3235 because they might not be preserved (and should be considered offset zero
3236 for other purposes). */
3238 static struct predicate
3239 remap_predicate (struct inline_summary *info,
3240 struct inline_summary *callee_info,
3241 struct predicate *p,
3242 vec<int> operand_map,
3243 vec<int> offset_map,
3244 clause_t possible_truths, struct predicate *toplev_predicate)
3246 int i;
3247 struct predicate out = true_predicate ();
3249 /* True predicate is easy. */
3250 if (true_predicate_p (p))
3251 return *toplev_predicate;
3252 for (i = 0; p->clause[i]; i++)
3254 clause_t clause = p->clause[i];
3255 int cond;
3256 struct predicate clause_predicate = false_predicate ();
3258 gcc_assert (i < MAX_CLAUSES);
3260 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3261 /* Do we have condition we can't disprove? */
3262 if (clause & possible_truths & (1 << cond))
3264 struct predicate cond_predicate;
3265 /* Work out if the condition can translate to predicate in the
3266 inlined function. */
3267 if (cond >= predicate_first_dynamic_condition)
3269 struct condition *c;
3271 c = &(*callee_info->conds)[cond
3273 predicate_first_dynamic_condition];
3274 /* See if we can remap condition operand to caller's operand.
3275 Otherwise give up. */
3276 if (!operand_map.exists ()
3277 || (int) operand_map.length () <= c->operand_num
3278 || operand_map[c->operand_num] == -1
3279 /* TODO: For non-aggregate conditions, adding an offset is
3280 basically an arithmetic jump function processing which
3281 we should support in future. */
3282 || ((!c->agg_contents || !c->by_ref)
3283 && offset_map[c->operand_num] > 0)
3284 || (c->agg_contents && c->by_ref
3285 && offset_map[c->operand_num] < 0))
3286 cond_predicate = true_predicate ();
3287 else
3289 struct agg_position_info ap;
3290 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3291 if (offset_delta < 0)
3293 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3294 offset_delta = 0;
3296 gcc_assert (!c->agg_contents
3297 || c->by_ref || offset_delta == 0);
3298 ap.offset = c->offset + offset_delta;
3299 ap.agg_contents = c->agg_contents;
3300 ap.by_ref = c->by_ref;
3301 cond_predicate = add_condition (info,
3302 operand_map[c->operand_num],
3303 &ap, c->code, c->val);
3306 /* Fixed conditions remains same, construct single
3307 condition predicate. */
3308 else
3310 cond_predicate.clause[0] = 1 << cond;
3311 cond_predicate.clause[1] = 0;
3313 clause_predicate = or_predicates (info->conds, &clause_predicate,
3314 &cond_predicate);
3316 out = and_predicates (info->conds, &out, &clause_predicate);
3318 return and_predicates (info->conds, &out, toplev_predicate);
3322 /* Update summary information of inline clones after inlining.
3323 Compute peak stack usage. */
3325 static void
3326 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3328 struct cgraph_edge *e;
3329 struct inline_summary *callee_info = inline_summary (node);
3330 struct inline_summary *caller_info = inline_summary (node->callers->caller);
3331 HOST_WIDE_INT peak;
3333 callee_info->stack_frame_offset
3334 = caller_info->stack_frame_offset
3335 + caller_info->estimated_self_stack_size;
3336 peak = callee_info->stack_frame_offset
3337 + callee_info->estimated_self_stack_size;
3338 if (inline_summary (node->global.inlined_to)->estimated_stack_size < peak)
3339 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
3340 ipa_propagate_frequency (node);
3341 for (e = node->callees; e; e = e->next_callee)
3343 if (!e->inline_failed)
3344 inline_update_callee_summaries (e->callee, depth);
3345 inline_edge_summary (e)->loop_depth += depth;
3347 for (e = node->indirect_calls; e; e = e->next_callee)
3348 inline_edge_summary (e)->loop_depth += depth;
3351 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3352 When functoin A is inlined in B and A calls C with parameter that
3353 changes with probability PROB1 and C is known to be passthroug
3354 of argument if B that change with probability PROB2, the probability
3355 of change is now PROB1*PROB2. */
3357 static void
3358 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3359 struct cgraph_edge *edge)
3361 if (ipa_node_params_vector.exists ())
3363 int i;
3364 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3365 struct inline_edge_summary *es = inline_edge_summary (edge);
3366 struct inline_edge_summary *inlined_es
3367 = inline_edge_summary (inlined_edge);
3369 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3371 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3372 if (jfunc->type == IPA_JF_PASS_THROUGH
3373 && (ipa_get_jf_pass_through_formal_id (jfunc)
3374 < (int) inlined_es->param.length ()))
3376 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3377 int prob1 = es->param[i].change_prob;
3378 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3379 int prob = combine_probabilities (prob1, prob2);
3381 if (prob1 && prob2 && !prob)
3382 prob = 1;
3384 es->param[i].change_prob = prob;
3390 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3392 Remap predicates of callees of NODE. Rest of arguments match
3393 remap_predicate.
3395 Also update change probabilities. */
3397 static void
3398 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3399 struct cgraph_node *node,
3400 struct inline_summary *info,
3401 struct inline_summary *callee_info,
3402 vec<int> operand_map,
3403 vec<int> offset_map,
3404 clause_t possible_truths,
3405 struct predicate *toplev_predicate)
3407 struct cgraph_edge *e;
3408 for (e = node->callees; e; e = e->next_callee)
3410 struct inline_edge_summary *es = inline_edge_summary (e);
3411 struct predicate p;
3413 if (e->inline_failed)
3415 remap_edge_change_prob (inlined_edge, e);
3417 if (es->predicate)
3419 p = remap_predicate (info, callee_info,
3420 es->predicate, operand_map, offset_map,
3421 possible_truths, toplev_predicate);
3422 edge_set_predicate (e, &p);
3423 /* TODO: We should remove the edge for code that will be
3424 optimized out, but we need to keep verifiers and tree-inline
3425 happy. Make it cold for now. */
3426 if (false_predicate_p (&p))
3428 e->count = 0;
3429 e->frequency = 0;
3432 else
3433 edge_set_predicate (e, toplev_predicate);
3435 else
3436 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3437 operand_map, offset_map, possible_truths,
3438 toplev_predicate);
3440 for (e = node->indirect_calls; e; e = e->next_callee)
3442 struct inline_edge_summary *es = inline_edge_summary (e);
3443 struct predicate p;
3445 remap_edge_change_prob (inlined_edge, e);
3446 if (es->predicate)
3448 p = remap_predicate (info, callee_info,
3449 es->predicate, operand_map, offset_map,
3450 possible_truths, toplev_predicate);
3451 edge_set_predicate (e, &p);
3452 /* TODO: We should remove the edge for code that will be optimized
3453 out, but we need to keep verifiers and tree-inline happy.
3454 Make it cold for now. */
3455 if (false_predicate_p (&p))
3457 e->count = 0;
3458 e->frequency = 0;
3461 else
3462 edge_set_predicate (e, toplev_predicate);
3466 /* Same as remap_predicate, but set result into hint *HINT. */
3468 static void
3469 remap_hint_predicate (struct inline_summary *info,
3470 struct inline_summary *callee_info,
3471 struct predicate **hint,
3472 vec<int> operand_map,
3473 vec<int> offset_map,
3474 clause_t possible_truths,
3475 struct predicate *toplev_predicate)
3477 predicate p;
3479 if (!*hint)
3480 return;
3481 p = remap_predicate (info, callee_info,
3482 *hint,
3483 operand_map, offset_map,
3484 possible_truths, toplev_predicate);
3485 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3487 if (!*hint)
3488 set_hint_predicate (hint, p);
3489 else
3490 **hint = and_predicates (info->conds, *hint, &p);
3494 /* We inlined EDGE. Update summary of the function we inlined into. */
3496 void
3497 inline_merge_summary (struct cgraph_edge *edge)
3499 struct inline_summary *callee_info = inline_summary (edge->callee);
3500 struct cgraph_node *to = (edge->caller->global.inlined_to
3501 ? edge->caller->global.inlined_to : edge->caller);
3502 struct inline_summary *info = inline_summary (to);
3503 clause_t clause = 0; /* not_inline is known to be false. */
3504 size_time_entry *e;
3505 vec<int> operand_map = vNULL;
3506 vec<int> offset_map = vNULL;
3507 int i;
3508 struct predicate toplev_predicate;
3509 struct predicate true_p = true_predicate ();
3510 struct inline_edge_summary *es = inline_edge_summary (edge);
3512 if (es->predicate)
3513 toplev_predicate = *es->predicate;
3514 else
3515 toplev_predicate = true_predicate ();
3517 if (ipa_node_params_vector.exists () && callee_info->conds)
3519 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3520 int count = ipa_get_cs_argument_count (args);
3521 int i;
3523 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3524 if (count)
3526 operand_map.safe_grow_cleared (count);
3527 offset_map.safe_grow_cleared (count);
3529 for (i = 0; i < count; i++)
3531 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3532 int map = -1;
3534 /* TODO: handle non-NOPs when merging. */
3535 if (jfunc->type == IPA_JF_PASS_THROUGH)
3537 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3538 map = ipa_get_jf_pass_through_formal_id (jfunc);
3539 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3540 offset_map[i] = -1;
3542 else if (jfunc->type == IPA_JF_ANCESTOR)
3544 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3545 if (offset >= 0 && offset < INT_MAX)
3547 map = ipa_get_jf_ancestor_formal_id (jfunc);
3548 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3549 offset = -1;
3550 offset_map[i] = offset;
3553 operand_map[i] = map;
3554 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3557 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3559 struct predicate p = remap_predicate (info, callee_info,
3560 &e->predicate, operand_map,
3561 offset_map, clause,
3562 &toplev_predicate);
3563 if (!false_predicate_p (&p))
3565 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3566 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3567 int prob = predicate_probability (callee_info->conds,
3568 &e->predicate,
3569 clause, es->param);
3570 add_time = apply_probability ((gcov_type) add_time, prob);
3571 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3572 add_time = MAX_TIME * INLINE_TIME_SCALE;
3573 if (prob != REG_BR_PROB_BASE
3574 && dump_file && (dump_flags & TDF_DETAILS))
3576 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3577 (double) prob / REG_BR_PROB_BASE);
3579 account_size_time (info, e->size, add_time, &p);
3582 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3583 offset_map, clause, &toplev_predicate);
3584 remap_hint_predicate (info, callee_info,
3585 &callee_info->loop_iterations,
3586 operand_map, offset_map, clause, &toplev_predicate);
3587 remap_hint_predicate (info, callee_info,
3588 &callee_info->loop_stride,
3589 operand_map, offset_map, clause, &toplev_predicate);
3590 remap_hint_predicate (info, callee_info,
3591 &callee_info->array_index,
3592 operand_map, offset_map, clause, &toplev_predicate);
3594 inline_update_callee_summaries (edge->callee,
3595 inline_edge_summary (edge)->loop_depth);
3597 /* We do not maintain predicates of inlined edges, free it. */
3598 edge_set_predicate (edge, &true_p);
3599 /* Similarly remove param summaries. */
3600 es->param.release ();
3601 operand_map.release ();
3602 offset_map.release ();
3605 /* For performance reasons inline_merge_summary is not updating overall size
3606 and time. Recompute it. */
3608 void
3609 inline_update_overall_summary (struct cgraph_node *node)
3611 struct inline_summary *info = inline_summary (node);
3612 size_time_entry *e;
3613 int i;
3615 info->size = 0;
3616 info->time = 0;
3617 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3619 info->size += e->size, info->time += e->time;
3620 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3621 info->time = MAX_TIME * INLINE_TIME_SCALE;
3623 estimate_calls_size_and_time (node, &info->size, &info->min_size,
3624 &info->time, NULL,
3625 ~(clause_t) (1 << predicate_false_condition),
3626 vNULL, vNULL, vNULL);
3627 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3628 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3631 /* Return hints derrived from EDGE. */
3633 simple_edge_hints (struct cgraph_edge *edge)
3635 int hints = 0;
3636 struct cgraph_node *to = (edge->caller->global.inlined_to
3637 ? edge->caller->global.inlined_to : edge->caller);
3638 if (inline_summary (to)->scc_no
3639 && inline_summary (to)->scc_no == inline_summary (edge->callee)->scc_no
3640 && !cgraph_edge_recursive_p (edge))
3641 hints |= INLINE_HINT_same_scc;
3643 if (to->lto_file_data && edge->callee->lto_file_data
3644 && to->lto_file_data != edge->callee->lto_file_data)
3645 hints |= INLINE_HINT_cross_module;
3647 return hints;
3650 /* Estimate the time cost for the caller when inlining EDGE.
3651 Only to be called via estimate_edge_time, that handles the
3652 caching mechanism.
3654 When caching, also update the cache entry. Compute both time and
3655 size, since we always need both metrics eventually. */
3658 do_estimate_edge_time (struct cgraph_edge *edge)
3660 int time;
3661 int size;
3662 inline_hints hints;
3663 struct cgraph_node *callee;
3664 clause_t clause;
3665 vec<tree> known_vals;
3666 vec<tree> known_binfos;
3667 vec<ipa_agg_jump_function_p> known_aggs;
3668 struct inline_edge_summary *es = inline_edge_summary (edge);
3669 int min_size;
3671 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3673 gcc_checking_assert (edge->inline_failed);
3674 evaluate_properties_for_edge (edge, true,
3675 &clause, &known_vals, &known_binfos,
3676 &known_aggs);
3677 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3678 known_aggs, &size, &min_size, &time, &hints, es->param);
3679 known_vals.release ();
3680 known_binfos.release ();
3681 known_aggs.release ();
3682 gcc_checking_assert (size >= 0);
3683 gcc_checking_assert (time >= 0);
3685 /* When caching, update the cache entry. */
3686 if (edge_growth_cache.exists ())
3688 inline_summary (edge->callee)->min_size = min_size;
3689 if ((int) edge_growth_cache.length () <= edge->uid)
3690 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
3691 edge_growth_cache[edge->uid].time = time + (time >= 0);
3693 edge_growth_cache[edge->uid].size = size + (size >= 0);
3694 hints |= simple_edge_hints (edge);
3695 edge_growth_cache[edge->uid].hints = hints + 1;
3697 return time;
3701 /* Return estimated callee growth after inlining EDGE.
3702 Only to be called via estimate_edge_size. */
3705 do_estimate_edge_size (struct cgraph_edge *edge)
3707 int size;
3708 struct cgraph_node *callee;
3709 clause_t clause;
3710 vec<tree> known_vals;
3711 vec<tree> known_binfos;
3712 vec<ipa_agg_jump_function_p> known_aggs;
3714 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3716 if (edge_growth_cache.exists ())
3718 do_estimate_edge_time (edge);
3719 size = edge_growth_cache[edge->uid].size;
3720 gcc_checking_assert (size);
3721 return size - (size > 0);
3724 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3726 /* Early inliner runs without caching, go ahead and do the dirty work. */
3727 gcc_checking_assert (edge->inline_failed);
3728 evaluate_properties_for_edge (edge, true,
3729 &clause, &known_vals, &known_binfos,
3730 &known_aggs);
3731 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3732 known_aggs, &size, NULL, NULL, NULL, vNULL);
3733 known_vals.release ();
3734 known_binfos.release ();
3735 known_aggs.release ();
3736 return size;
3740 /* Estimate the growth of the caller when inlining EDGE.
3741 Only to be called via estimate_edge_size. */
3743 inline_hints
3744 do_estimate_edge_hints (struct cgraph_edge *edge)
3746 inline_hints hints;
3747 struct cgraph_node *callee;
3748 clause_t clause;
3749 vec<tree> known_vals;
3750 vec<tree> known_binfos;
3751 vec<ipa_agg_jump_function_p> known_aggs;
3753 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3755 if (edge_growth_cache.exists ())
3757 do_estimate_edge_time (edge);
3758 hints = edge_growth_cache[edge->uid].hints;
3759 gcc_checking_assert (hints);
3760 return hints - 1;
3763 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3765 /* Early inliner runs without caching, go ahead and do the dirty work. */
3766 gcc_checking_assert (edge->inline_failed);
3767 evaluate_properties_for_edge (edge, true,
3768 &clause, &known_vals, &known_binfos,
3769 &known_aggs);
3770 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3771 known_aggs, NULL, NULL, NULL, &hints, vNULL);
3772 known_vals.release ();
3773 known_binfos.release ();
3774 known_aggs.release ();
3775 hints |= simple_edge_hints (edge);
3776 return hints;
3780 /* Estimate self time of the function NODE after inlining EDGE. */
3783 estimate_time_after_inlining (struct cgraph_node *node,
3784 struct cgraph_edge *edge)
3786 struct inline_edge_summary *es = inline_edge_summary (edge);
3787 if (!es->predicate || !false_predicate_p (es->predicate))
3789 gcov_type time =
3790 inline_summary (node)->time + estimate_edge_time (edge);
3791 if (time < 0)
3792 time = 0;
3793 if (time > MAX_TIME)
3794 time = MAX_TIME;
3795 return time;
3797 return inline_summary (node)->time;
3801 /* Estimate the size of NODE after inlining EDGE which should be an
3802 edge to either NODE or a call inlined into NODE. */
3805 estimate_size_after_inlining (struct cgraph_node *node,
3806 struct cgraph_edge *edge)
3808 struct inline_edge_summary *es = inline_edge_summary (edge);
3809 if (!es->predicate || !false_predicate_p (es->predicate))
3811 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3812 gcc_assert (size >= 0);
3813 return size;
3815 return inline_summary (node)->size;
3819 struct growth_data
3821 struct cgraph_node *node;
3822 bool self_recursive;
3823 int growth;
3827 /* Worker for do_estimate_growth. Collect growth for all callers. */
3829 static bool
3830 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3832 struct cgraph_edge *e;
3833 struct growth_data *d = (struct growth_data *) data;
3835 for (e = node->callers; e; e = e->next_caller)
3837 gcc_checking_assert (e->inline_failed);
3839 if (e->caller == d->node
3840 || (e->caller->global.inlined_to
3841 && e->caller->global.inlined_to == d->node))
3842 d->self_recursive = true;
3843 d->growth += estimate_edge_growth (e);
3845 return false;
3849 /* Estimate the growth caused by inlining NODE into all callees. */
3852 do_estimate_growth (struct cgraph_node *node)
3854 struct growth_data d = { node, 0, false };
3855 struct inline_summary *info = inline_summary (node);
3857 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3859 /* For self recursive functions the growth estimation really should be
3860 infinity. We don't want to return very large values because the growth
3861 plays various roles in badness computation fractions. Be sure to not
3862 return zero or negative growths. */
3863 if (d.self_recursive)
3864 d.growth = d.growth < info->size ? info->size : d.growth;
3865 else if (DECL_EXTERNAL (node->decl))
3867 else
3869 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3870 d.growth -= info->size;
3871 /* COMDAT functions are very often not shared across multiple units
3872 since they come from various template instantiations.
3873 Take this into account. */
3874 else if (DECL_COMDAT (node->decl)
3875 && cgraph_can_remove_if_no_direct_calls_p (node))
3876 d.growth -= (info->size
3877 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3878 + 50) / 100;
3881 if (node_growth_cache.exists ())
3883 if ((int) node_growth_cache.length () <= node->uid)
3884 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
3885 node_growth_cache[node->uid] = d.growth + (d.growth >= 0);
3887 return d.growth;
3891 /* Make cheap estimation if growth of NODE is likely positive knowing
3892 EDGE_GROWTH of one particular edge.
3893 We assume that most of other edges will have similar growth
3894 and skip computation if there are too many callers. */
3896 bool
3897 growth_likely_positive (struct cgraph_node *node, int edge_growth ATTRIBUTE_UNUSED)
3899 int max_callers;
3900 int ret;
3901 struct cgraph_edge *e;
3902 gcc_checking_assert (edge_growth > 0);
3904 /* Unlike for functions called once, we play unsafe with
3905 COMDATs. We can allow that since we know functions
3906 in consideration are small (and thus risk is small) and
3907 moreover grow estimates already accounts that COMDAT
3908 functions may or may not disappear when eliminated from
3909 current unit. With good probability making aggressive
3910 choice in all units is going to make overall program
3911 smaller.
3913 Consequently we ask cgraph_can_remove_if_no_direct_calls_p
3914 instead of
3915 cgraph_will_be_removed_from_program_if_no_direct_calls */
3916 if (DECL_EXTERNAL (node->decl)
3917 || !cgraph_can_remove_if_no_direct_calls_p (node))
3918 return true;
3920 /* If there is cached value, just go ahead. */
3921 if ((int)node_growth_cache.length () > node->uid
3922 && (ret = node_growth_cache[node->uid]))
3923 return ret > 0;
3924 if (!cgraph_will_be_removed_from_program_if_no_direct_calls (node)
3925 && (!DECL_COMDAT (node->decl)
3926 || !cgraph_can_remove_if_no_direct_calls_p (node)))
3927 return true;
3928 max_callers = inline_summary (node)->size * 4 / edge_growth + 2;
3930 for (e = node->callers; e; e = e->next_caller)
3932 max_callers--;
3933 if (!max_callers)
3934 return true;
3936 return estimate_growth (node) > 0;
3940 /* This function performs intraprocedural analysis in NODE that is required to
3941 inline indirect calls. */
3943 static void
3944 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3946 ipa_analyze_node (node);
3947 if (dump_file && (dump_flags & TDF_DETAILS))
3949 ipa_print_node_params (dump_file, node);
3950 ipa_print_node_jump_functions (dump_file, node);
3955 /* Note function body size. */
3957 static void
3958 inline_analyze_function (struct cgraph_node *node)
3960 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3962 if (dump_file)
3963 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3964 node->name (), node->order);
3965 if (optimize && !node->thunk.thunk_p)
3966 inline_indirect_intraprocedural_analysis (node);
3967 compute_inline_parameters (node, false);
3968 if (!optimize)
3970 struct cgraph_edge *e;
3971 for (e = node->callees; e; e = e->next_callee)
3973 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3974 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3975 e->call_stmt_cannot_inline_p = true;
3977 for (e = node->indirect_calls; e; e = e->next_callee)
3979 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3980 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3981 e->call_stmt_cannot_inline_p = true;
3985 pop_cfun ();
3989 /* Called when new function is inserted to callgraph late. */
3991 static void
3992 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3994 inline_analyze_function (node);
3998 /* Note function body size. */
4000 void
4001 inline_generate_summary (void)
4003 struct cgraph_node *node;
4005 /* When not optimizing, do not bother to analyze. Inlining is still done
4006 because edge redirection needs to happen there. */
4007 if (!optimize && !flag_lto && !flag_wpa)
4008 return;
4010 function_insertion_hook_holder =
4011 cgraph_add_function_insertion_hook (&add_new_function, NULL);
4013 ipa_register_cgraph_hooks ();
4014 inline_free_summary ();
4016 FOR_EACH_DEFINED_FUNCTION (node)
4017 if (!node->alias)
4018 inline_analyze_function (node);
4022 /* Read predicate from IB. */
4024 static struct predicate
4025 read_predicate (struct lto_input_block *ib)
4027 struct predicate out;
4028 clause_t clause;
4029 int k = 0;
4033 gcc_assert (k <= MAX_CLAUSES);
4034 clause = out.clause[k++] = streamer_read_uhwi (ib);
4036 while (clause);
4038 /* Zero-initialize the remaining clauses in OUT. */
4039 while (k <= MAX_CLAUSES)
4040 out.clause[k++] = 0;
4042 return out;
4046 /* Write inline summary for edge E to OB. */
4048 static void
4049 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
4051 struct inline_edge_summary *es = inline_edge_summary (e);
4052 struct predicate p;
4053 int length, i;
4055 es->call_stmt_size = streamer_read_uhwi (ib);
4056 es->call_stmt_time = streamer_read_uhwi (ib);
4057 es->loop_depth = streamer_read_uhwi (ib);
4058 p = read_predicate (ib);
4059 edge_set_predicate (e, &p);
4060 length = streamer_read_uhwi (ib);
4061 if (length)
4063 es->param.safe_grow_cleared (length);
4064 for (i = 0; i < length; i++)
4065 es->param[i].change_prob = streamer_read_uhwi (ib);
4070 /* Stream in inline summaries from the section. */
4072 static void
4073 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4074 size_t len)
4076 const struct lto_function_header *header =
4077 (const struct lto_function_header *) data;
4078 const int cfg_offset = sizeof (struct lto_function_header);
4079 const int main_offset = cfg_offset + header->cfg_size;
4080 const int string_offset = main_offset + header->main_size;
4081 struct data_in *data_in;
4082 struct lto_input_block ib;
4083 unsigned int i, count2, j;
4084 unsigned int f_count;
4086 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
4087 header->main_size);
4089 data_in =
4090 lto_data_in_create (file_data, (const char *) data + string_offset,
4091 header->string_size, vNULL);
4092 f_count = streamer_read_uhwi (&ib);
4093 for (i = 0; i < f_count; i++)
4095 unsigned int index;
4096 struct cgraph_node *node;
4097 struct inline_summary *info;
4098 lto_symtab_encoder_t encoder;
4099 struct bitpack_d bp;
4100 struct cgraph_edge *e;
4101 predicate p;
4103 index = streamer_read_uhwi (&ib);
4104 encoder = file_data->symtab_node_encoder;
4105 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4106 info = inline_summary (node);
4108 info->estimated_stack_size
4109 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
4110 info->size = info->self_size = streamer_read_uhwi (&ib);
4111 info->time = info->self_time = streamer_read_uhwi (&ib);
4113 bp = streamer_read_bitpack (&ib);
4114 info->inlinable = bp_unpack_value (&bp, 1);
4116 count2 = streamer_read_uhwi (&ib);
4117 gcc_assert (!info->conds);
4118 for (j = 0; j < count2; j++)
4120 struct condition c;
4121 c.operand_num = streamer_read_uhwi (&ib);
4122 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4123 c.val = stream_read_tree (&ib, data_in);
4124 bp = streamer_read_bitpack (&ib);
4125 c.agg_contents = bp_unpack_value (&bp, 1);
4126 c.by_ref = bp_unpack_value (&bp, 1);
4127 if (c.agg_contents)
4128 c.offset = streamer_read_uhwi (&ib);
4129 vec_safe_push (info->conds, c);
4131 count2 = streamer_read_uhwi (&ib);
4132 gcc_assert (!info->entry);
4133 for (j = 0; j < count2; j++)
4135 struct size_time_entry e;
4137 e.size = streamer_read_uhwi (&ib);
4138 e.time = streamer_read_uhwi (&ib);
4139 e.predicate = read_predicate (&ib);
4141 vec_safe_push (info->entry, e);
4144 p = read_predicate (&ib);
4145 set_hint_predicate (&info->loop_iterations, p);
4146 p = read_predicate (&ib);
4147 set_hint_predicate (&info->loop_stride, p);
4148 p = read_predicate (&ib);
4149 set_hint_predicate (&info->array_index, p);
4150 for (e = node->callees; e; e = e->next_callee)
4151 read_inline_edge_summary (&ib, e);
4152 for (e = node->indirect_calls; e; e = e->next_callee)
4153 read_inline_edge_summary (&ib, e);
4156 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
4157 len);
4158 lto_data_in_delete (data_in);
4162 /* Read inline summary. Jump functions are shared among ipa-cp
4163 and inliner, so when ipa-cp is active, we don't need to write them
4164 twice. */
4166 void
4167 inline_read_summary (void)
4169 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4170 struct lto_file_decl_data *file_data;
4171 unsigned int j = 0;
4173 inline_summary_alloc ();
4175 while ((file_data = file_data_vec[j++]))
4177 size_t len;
4178 const char *data = lto_get_section_data (file_data,
4179 LTO_section_inline_summary,
4180 NULL, &len);
4181 if (data)
4182 inline_read_section (file_data, data, len);
4183 else
4184 /* Fatal error here. We do not want to support compiling ltrans units
4185 with different version of compiler or different flags than the WPA
4186 unit, so this should never happen. */
4187 fatal_error ("ipa inline summary is missing in input file");
4189 if (optimize)
4191 ipa_register_cgraph_hooks ();
4192 if (!flag_ipa_cp)
4193 ipa_prop_read_jump_functions ();
4195 function_insertion_hook_holder =
4196 cgraph_add_function_insertion_hook (&add_new_function, NULL);
4200 /* Write predicate P to OB. */
4202 static void
4203 write_predicate (struct output_block *ob, struct predicate *p)
4205 int j;
4206 if (p)
4207 for (j = 0; p->clause[j]; j++)
4209 gcc_assert (j < MAX_CLAUSES);
4210 streamer_write_uhwi (ob, p->clause[j]);
4212 streamer_write_uhwi (ob, 0);
4216 /* Write inline summary for edge E to OB. */
4218 static void
4219 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4221 struct inline_edge_summary *es = inline_edge_summary (e);
4222 int i;
4224 streamer_write_uhwi (ob, es->call_stmt_size);
4225 streamer_write_uhwi (ob, es->call_stmt_time);
4226 streamer_write_uhwi (ob, es->loop_depth);
4227 write_predicate (ob, es->predicate);
4228 streamer_write_uhwi (ob, es->param.length ());
4229 for (i = 0; i < (int) es->param.length (); i++)
4230 streamer_write_uhwi (ob, es->param[i].change_prob);
4234 /* Write inline summary for node in SET.
4235 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4236 active, we don't need to write them twice. */
4238 void
4239 inline_write_summary (void)
4241 struct cgraph_node *node;
4242 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4243 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4244 unsigned int count = 0;
4245 int i;
4247 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4249 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4250 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4251 if (cnode && cnode->definition && !cnode->alias)
4252 count++;
4254 streamer_write_uhwi (ob, count);
4256 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4258 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4259 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4260 if (cnode && (node = cnode)->definition && !node->alias)
4262 struct inline_summary *info = inline_summary (node);
4263 struct bitpack_d bp;
4264 struct cgraph_edge *edge;
4265 int i;
4266 size_time_entry *e;
4267 struct condition *c;
4269 streamer_write_uhwi (ob,
4270 lto_symtab_encoder_encode (encoder,
4272 node));
4273 streamer_write_hwi (ob, info->estimated_self_stack_size);
4274 streamer_write_hwi (ob, info->self_size);
4275 streamer_write_hwi (ob, info->self_time);
4276 bp = bitpack_create (ob->main_stream);
4277 bp_pack_value (&bp, info->inlinable, 1);
4278 streamer_write_bitpack (&bp);
4279 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4280 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4282 streamer_write_uhwi (ob, c->operand_num);
4283 streamer_write_uhwi (ob, c->code);
4284 stream_write_tree (ob, c->val, true);
4285 bp = bitpack_create (ob->main_stream);
4286 bp_pack_value (&bp, c->agg_contents, 1);
4287 bp_pack_value (&bp, c->by_ref, 1);
4288 streamer_write_bitpack (&bp);
4289 if (c->agg_contents)
4290 streamer_write_uhwi (ob, c->offset);
4292 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4293 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4295 streamer_write_uhwi (ob, e->size);
4296 streamer_write_uhwi (ob, e->time);
4297 write_predicate (ob, &e->predicate);
4299 write_predicate (ob, info->loop_iterations);
4300 write_predicate (ob, info->loop_stride);
4301 write_predicate (ob, info->array_index);
4302 for (edge = node->callees; edge; edge = edge->next_callee)
4303 write_inline_edge_summary (ob, edge);
4304 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4305 write_inline_edge_summary (ob, edge);
4308 streamer_write_char_stream (ob->main_stream, 0);
4309 produce_asm (ob, NULL);
4310 destroy_output_block (ob);
4312 if (optimize && !flag_ipa_cp)
4313 ipa_prop_write_jump_functions ();
4317 /* Release inline summary. */
4319 void
4320 inline_free_summary (void)
4322 struct cgraph_node *node;
4323 if (!inline_edge_summary_vec.exists ())
4324 return;
4325 FOR_EACH_DEFINED_FUNCTION (node)
4326 if (!node->alias)
4327 reset_inline_summary (node);
4328 if (function_insertion_hook_holder)
4329 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
4330 function_insertion_hook_holder = NULL;
4331 if (node_removal_hook_holder)
4332 cgraph_remove_node_removal_hook (node_removal_hook_holder);
4333 node_removal_hook_holder = NULL;
4334 if (edge_removal_hook_holder)
4335 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
4336 edge_removal_hook_holder = NULL;
4337 if (node_duplication_hook_holder)
4338 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
4339 node_duplication_hook_holder = NULL;
4340 if (edge_duplication_hook_holder)
4341 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
4342 edge_duplication_hook_holder = NULL;
4343 vec_free (inline_summary_vec);
4344 inline_edge_summary_vec.release ();
4345 if (edge_predicate_pool)
4346 free_alloc_pool (edge_predicate_pool);
4347 edge_predicate_pool = 0;