2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / ipa-inline-analysis.c
blobd062d7ad847eebbd43fa62003fcab5c979de8d83
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2015 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 "alias.h"
72 #include "symtab.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stor-layout.h"
76 #include "stringpool.h"
77 #include "print-tree.h"
78 #include "tree-inline.h"
79 #include "langhooks.h"
80 #include "flags.h"
81 #include "diagnostic.h"
82 #include "gimple-pretty-print.h"
83 #include "params.h"
84 #include "tree-pass.h"
85 #include "coverage.h"
86 #include "predict.h"
87 #include "hard-reg-set.h"
88 #include "function.h"
89 #include "dominance.h"
90 #include "cfg.h"
91 #include "cfganal.h"
92 #include "basic-block.h"
93 #include "tree-ssa-alias.h"
94 #include "internal-fn.h"
95 #include "gimple-expr.h"
96 #include "gimple.h"
97 #include "gimple-iterator.h"
98 #include "gimple-ssa.h"
99 #include "tree-cfg.h"
100 #include "tree-phinodes.h"
101 #include "ssa-iterators.h"
102 #include "tree-ssanames.h"
103 #include "tree-ssa-loop-niter.h"
104 #include "tree-ssa-loop.h"
105 #include "plugin-api.h"
106 #include "ipa-ref.h"
107 #include "cgraph.h"
108 #include "alloc-pool.h"
109 #include "symbol-summary.h"
110 #include "ipa-prop.h"
111 #include "lto-streamer.h"
112 #include "data-streamer.h"
113 #include "tree-streamer.h"
114 #include "ipa-inline.h"
115 #include "cfgloop.h"
116 #include "tree-scalar-evolution.h"
117 #include "ipa-utils.h"
118 #include "cilk.h"
119 #include "cfgexpand.h"
121 /* Estimate runtime of function can easilly run into huge numbers with many
122 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
123 integer. For anything larger we use gcov_type. */
124 #define MAX_TIME 500000
126 /* Number of bits in integer, but we really want to be stable across different
127 hosts. */
128 #define NUM_CONDITIONS 32
130 enum predicate_conditions
132 predicate_false_condition = 0,
133 predicate_not_inlined_condition = 1,
134 predicate_first_dynamic_condition = 2
137 /* Special condition code we use to represent test that operand is compile time
138 constant. */
139 #define IS_NOT_CONSTANT ERROR_MARK
140 /* Special condition code we use to represent test that operand is not changed
141 across invocation of the function. When operand IS_NOT_CONSTANT it is always
142 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
143 of executions even when they are not compile time constants. */
144 #define CHANGED IDENTIFIER_NODE
146 /* Holders of ipa cgraph hooks: */
147 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
148 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
149 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
150 static void inline_edge_duplication_hook (struct cgraph_edge *,
151 struct cgraph_edge *, void *);
153 /* VECtor holding inline summaries.
154 In GGC memory because conditions might point to constant trees. */
155 function_summary <inline_summary *> *inline_summaries;
156 vec<inline_edge_summary_t> inline_edge_summary_vec;
158 /* Cached node/edge growths. */
159 vec<edge_growth_cache_entry> edge_growth_cache;
161 /* Edge predicates goes here. */
162 static pool_allocator<predicate> edge_predicate_pool ("edge predicates", 10);
164 /* Return true predicate (tautology).
165 We represent it by empty list of clauses. */
167 static inline struct predicate
168 true_predicate (void)
170 struct predicate p;
171 p.clause[0] = 0;
172 return p;
176 /* Return predicate testing single condition number COND. */
178 static inline struct predicate
179 single_cond_predicate (int cond)
181 struct predicate p;
182 p.clause[0] = 1 << cond;
183 p.clause[1] = 0;
184 return p;
188 /* Return false predicate. First clause require false condition. */
190 static inline struct predicate
191 false_predicate (void)
193 return single_cond_predicate (predicate_false_condition);
197 /* Return true if P is (true). */
199 static inline bool
200 true_predicate_p (struct predicate *p)
202 return !p->clause[0];
206 /* Return true if P is (false). */
208 static inline bool
209 false_predicate_p (struct predicate *p)
211 if (p->clause[0] == (1 << predicate_false_condition))
213 gcc_checking_assert (!p->clause[1]
214 && p->clause[0] == 1 << predicate_false_condition);
215 return true;
217 return false;
221 /* Return predicate that is set true when function is not inlined. */
223 static inline struct predicate
224 not_inlined_predicate (void)
226 return single_cond_predicate (predicate_not_inlined_condition);
229 /* Simple description of whether a memory load or a condition refers to a load
230 from an aggregate and if so, how and where from in the aggregate.
231 Individual fields have the same meaning like fields with the same name in
232 struct condition. */
234 struct agg_position_info
236 HOST_WIDE_INT offset;
237 bool agg_contents;
238 bool by_ref;
241 /* Add condition to condition list CONDS. AGGPOS describes whether the used
242 oprand is loaded from an aggregate and where in the aggregate it is. It can
243 be NULL, which means this not a load from an aggregate. */
245 static struct predicate
246 add_condition (struct inline_summary *summary, int operand_num,
247 struct agg_position_info *aggpos,
248 enum tree_code code, tree val)
250 int i;
251 struct condition *c;
252 struct condition new_cond;
253 HOST_WIDE_INT offset;
254 bool agg_contents, by_ref;
256 if (aggpos)
258 offset = aggpos->offset;
259 agg_contents = aggpos->agg_contents;
260 by_ref = aggpos->by_ref;
262 else
264 offset = 0;
265 agg_contents = false;
266 by_ref = false;
269 gcc_checking_assert (operand_num >= 0);
270 for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
272 if (c->operand_num == operand_num
273 && c->code == code
274 && c->val == val
275 && c->agg_contents == agg_contents
276 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
277 return single_cond_predicate (i + predicate_first_dynamic_condition);
279 /* Too many conditions. Give up and return constant true. */
280 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
281 return true_predicate ();
283 new_cond.operand_num = operand_num;
284 new_cond.code = code;
285 new_cond.val = val;
286 new_cond.agg_contents = agg_contents;
287 new_cond.by_ref = by_ref;
288 new_cond.offset = offset;
289 vec_safe_push (summary->conds, new_cond);
290 return single_cond_predicate (i + predicate_first_dynamic_condition);
294 /* Add clause CLAUSE into the predicate P. */
296 static inline void
297 add_clause (conditions conditions, struct predicate *p, clause_t clause)
299 int i;
300 int i2;
301 int insert_here = -1;
302 int c1, c2;
304 /* True clause. */
305 if (!clause)
306 return;
308 /* False clause makes the whole predicate false. Kill the other variants. */
309 if (clause == (1 << predicate_false_condition))
311 p->clause[0] = (1 << predicate_false_condition);
312 p->clause[1] = 0;
313 return;
315 if (false_predicate_p (p))
316 return;
318 /* No one should be silly enough to add false into nontrivial clauses. */
319 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
321 /* Look where to insert the clause. At the same time prune out
322 clauses of P that are implied by the new clause and thus
323 redundant. */
324 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
326 p->clause[i2] = p->clause[i];
328 if (!p->clause[i])
329 break;
331 /* If p->clause[i] implies clause, there is nothing to add. */
332 if ((p->clause[i] & clause) == p->clause[i])
334 /* We had nothing to add, none of clauses should've become
335 redundant. */
336 gcc_checking_assert (i == i2);
337 return;
340 if (p->clause[i] < clause && insert_here < 0)
341 insert_here = i2;
343 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
344 Otherwise the p->clause[i] has to stay. */
345 if ((p->clause[i] & clause) != clause)
346 i2++;
349 /* Look for clauses that are obviously true. I.e.
350 op0 == 5 || op0 != 5. */
351 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
353 condition *cc1;
354 if (!(clause & (1 << c1)))
355 continue;
356 cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
357 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
358 and thus there is no point for looking for them. */
359 if (cc1->code == CHANGED || cc1->code == IS_NOT_CONSTANT)
360 continue;
361 for (c2 = c1 + 1; c2 < NUM_CONDITIONS; c2++)
362 if (clause & (1 << c2))
364 condition *cc1 =
365 &(*conditions)[c1 - predicate_first_dynamic_condition];
366 condition *cc2 =
367 &(*conditions)[c2 - predicate_first_dynamic_condition];
368 if (cc1->operand_num == cc2->operand_num
369 && cc1->val == cc2->val
370 && cc2->code != IS_NOT_CONSTANT
371 && cc2->code != CHANGED
372 && cc1->code == invert_tree_comparison (cc2->code,
373 HONOR_NANS (cc1->val)))
374 return;
379 /* We run out of variants. Be conservative in positive direction. */
380 if (i2 == MAX_CLAUSES)
381 return;
382 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
383 p->clause[i2 + 1] = 0;
384 if (insert_here >= 0)
385 for (; i2 > insert_here; i2--)
386 p->clause[i2] = p->clause[i2 - 1];
387 else
388 insert_here = i2;
389 p->clause[insert_here] = clause;
393 /* Return P & P2. */
395 static struct predicate
396 and_predicates (conditions conditions,
397 struct predicate *p, struct predicate *p2)
399 struct predicate out = *p;
400 int i;
402 /* Avoid busy work. */
403 if (false_predicate_p (p2) || true_predicate_p (p))
404 return *p2;
405 if (false_predicate_p (p) || true_predicate_p (p2))
406 return *p;
408 /* See how far predicates match. */
409 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
411 gcc_checking_assert (i < MAX_CLAUSES);
414 /* Combine the predicates rest. */
415 for (; p2->clause[i]; i++)
417 gcc_checking_assert (i < MAX_CLAUSES);
418 add_clause (conditions, &out, p2->clause[i]);
420 return out;
424 /* Return true if predicates are obviously equal. */
426 static inline bool
427 predicates_equal_p (struct predicate *p, struct predicate *p2)
429 int i;
430 for (i = 0; p->clause[i]; i++)
432 gcc_checking_assert (i < MAX_CLAUSES);
433 gcc_checking_assert (p->clause[i] > p->clause[i + 1]);
434 gcc_checking_assert (!p2->clause[i]
435 || p2->clause[i] > p2->clause[i + 1]);
436 if (p->clause[i] != p2->clause[i])
437 return false;
439 return !p2->clause[i];
443 /* Return P | P2. */
445 static struct predicate
446 or_predicates (conditions conditions,
447 struct predicate *p, struct predicate *p2)
449 struct predicate out = true_predicate ();
450 int i, j;
452 /* Avoid busy work. */
453 if (false_predicate_p (p2) || true_predicate_p (p))
454 return *p;
455 if (false_predicate_p (p) || true_predicate_p (p2))
456 return *p2;
457 if (predicates_equal_p (p, p2))
458 return *p;
460 /* OK, combine the predicates. */
461 for (i = 0; p->clause[i]; i++)
462 for (j = 0; p2->clause[j]; j++)
464 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
465 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
467 return out;
471 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
472 if predicate P is known to be false. */
474 static bool
475 evaluate_predicate (struct predicate *p, clause_t possible_truths)
477 int i;
479 /* True remains true. */
480 if (true_predicate_p (p))
481 return true;
483 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
485 /* See if we can find clause we can disprove. */
486 for (i = 0; p->clause[i]; i++)
488 gcc_checking_assert (i < MAX_CLAUSES);
489 if (!(p->clause[i] & possible_truths))
490 return false;
492 return true;
495 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
496 instruction will be recomputed per invocation of the inlined call. */
498 static int
499 predicate_probability (conditions conds,
500 struct predicate *p, clause_t possible_truths,
501 vec<inline_param_summary> inline_param_summary)
503 int i;
504 int combined_prob = REG_BR_PROB_BASE;
506 /* True remains true. */
507 if (true_predicate_p (p))
508 return REG_BR_PROB_BASE;
510 if (false_predicate_p (p))
511 return 0;
513 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
515 /* See if we can find clause we can disprove. */
516 for (i = 0; p->clause[i]; i++)
518 gcc_checking_assert (i < MAX_CLAUSES);
519 if (!(p->clause[i] & possible_truths))
520 return 0;
521 else
523 int this_prob = 0;
524 int i2;
525 if (!inline_param_summary.exists ())
526 return REG_BR_PROB_BASE;
527 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
528 if ((p->clause[i] & possible_truths) & (1 << i2))
530 if (i2 >= predicate_first_dynamic_condition)
532 condition *c =
533 &(*conds)[i2 - predicate_first_dynamic_condition];
534 if (c->code == CHANGED
535 && (c->operand_num <
536 (int) inline_param_summary.length ()))
538 int iprob =
539 inline_param_summary[c->operand_num].change_prob;
540 this_prob = MAX (this_prob, iprob);
542 else
543 this_prob = REG_BR_PROB_BASE;
545 else
546 this_prob = REG_BR_PROB_BASE;
548 combined_prob = MIN (this_prob, combined_prob);
549 if (!combined_prob)
550 return 0;
553 return combined_prob;
557 /* Dump conditional COND. */
559 static void
560 dump_condition (FILE *f, conditions conditions, int cond)
562 condition *c;
563 if (cond == predicate_false_condition)
564 fprintf (f, "false");
565 else if (cond == predicate_not_inlined_condition)
566 fprintf (f, "not inlined");
567 else
569 c = &(*conditions)[cond - predicate_first_dynamic_condition];
570 fprintf (f, "op%i", c->operand_num);
571 if (c->agg_contents)
572 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
573 c->by_ref ? "ref " : "", c->offset);
574 if (c->code == IS_NOT_CONSTANT)
576 fprintf (f, " not constant");
577 return;
579 if (c->code == CHANGED)
581 fprintf (f, " changed");
582 return;
584 fprintf (f, " %s ", op_symbol_code (c->code));
585 print_generic_expr (f, c->val, 1);
590 /* Dump clause CLAUSE. */
592 static void
593 dump_clause (FILE *f, conditions conds, clause_t clause)
595 int i;
596 bool found = false;
597 fprintf (f, "(");
598 if (!clause)
599 fprintf (f, "true");
600 for (i = 0; i < NUM_CONDITIONS; i++)
601 if (clause & (1 << i))
603 if (found)
604 fprintf (f, " || ");
605 found = true;
606 dump_condition (f, conds, i);
608 fprintf (f, ")");
612 /* Dump predicate PREDICATE. */
614 static void
615 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
617 int i;
618 if (true_predicate_p (pred))
619 dump_clause (f, conds, 0);
620 else
621 for (i = 0; pred->clause[i]; i++)
623 if (i)
624 fprintf (f, " && ");
625 dump_clause (f, conds, pred->clause[i]);
627 fprintf (f, "\n");
631 /* Dump inline hints. */
632 void
633 dump_inline_hints (FILE *f, inline_hints hints)
635 if (!hints)
636 return;
637 fprintf (f, "inline hints:");
638 if (hints & INLINE_HINT_indirect_call)
640 hints &= ~INLINE_HINT_indirect_call;
641 fprintf (f, " indirect_call");
643 if (hints & INLINE_HINT_loop_iterations)
645 hints &= ~INLINE_HINT_loop_iterations;
646 fprintf (f, " loop_iterations");
648 if (hints & INLINE_HINT_loop_stride)
650 hints &= ~INLINE_HINT_loop_stride;
651 fprintf (f, " loop_stride");
653 if (hints & INLINE_HINT_same_scc)
655 hints &= ~INLINE_HINT_same_scc;
656 fprintf (f, " same_scc");
658 if (hints & INLINE_HINT_in_scc)
660 hints &= ~INLINE_HINT_in_scc;
661 fprintf (f, " in_scc");
663 if (hints & INLINE_HINT_cross_module)
665 hints &= ~INLINE_HINT_cross_module;
666 fprintf (f, " cross_module");
668 if (hints & INLINE_HINT_declared_inline)
670 hints &= ~INLINE_HINT_declared_inline;
671 fprintf (f, " declared_inline");
673 if (hints & INLINE_HINT_array_index)
675 hints &= ~INLINE_HINT_array_index;
676 fprintf (f, " array_index");
678 if (hints & INLINE_HINT_known_hot)
680 hints &= ~INLINE_HINT_known_hot;
681 fprintf (f, " known_hot");
683 gcc_assert (!hints);
687 /* Record SIZE and TIME under condition PRED into the inline summary. */
689 static void
690 account_size_time (struct inline_summary *summary, int size, int time,
691 struct predicate *pred)
693 size_time_entry *e;
694 bool found = false;
695 int i;
697 if (false_predicate_p (pred))
698 return;
700 /* We need to create initial empty unconitional clause, but otherwie
701 we don't need to account empty times and sizes. */
702 if (!size && !time && summary->entry)
703 return;
705 /* Watch overflow that might result from insane profiles. */
706 if (time > MAX_TIME * INLINE_TIME_SCALE)
707 time = MAX_TIME * INLINE_TIME_SCALE;
708 gcc_assert (time >= 0);
710 for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
711 if (predicates_equal_p (&e->predicate, pred))
713 found = true;
714 break;
716 if (i == 256)
718 i = 0;
719 found = true;
720 e = &(*summary->entry)[0];
721 gcc_assert (!e->predicate.clause[0]);
722 if (dump_file && (dump_flags & TDF_DETAILS))
723 fprintf (dump_file,
724 "\t\tReached limit on number of entries, "
725 "ignoring the predicate.");
727 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
729 fprintf (dump_file,
730 "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
731 ((double) size) / INLINE_SIZE_SCALE,
732 ((double) time) / INLINE_TIME_SCALE, found ? "" : "new ");
733 dump_predicate (dump_file, summary->conds, pred);
735 if (!found)
737 struct size_time_entry new_entry;
738 new_entry.size = size;
739 new_entry.time = time;
740 new_entry.predicate = *pred;
741 vec_safe_push (summary->entry, new_entry);
743 else
745 e->size += size;
746 e->time += time;
747 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
748 e->time = MAX_TIME * INLINE_TIME_SCALE;
752 /* We proved E to be unreachable, redirect it to __bultin_unreachable. */
754 static struct cgraph_edge *
755 redirect_to_unreachable (struct cgraph_edge *e)
757 struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
758 struct cgraph_node *target = cgraph_node::get_create
759 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
761 if (e->speculative)
762 e = e->resolve_speculation (target->decl);
763 else if (!e->callee)
764 e->make_direct (target);
765 else
766 e->redirect_callee (target);
767 struct inline_edge_summary *es = inline_edge_summary (e);
768 e->inline_failed = CIF_UNREACHABLE;
769 e->frequency = 0;
770 e->count = 0;
771 es->call_stmt_size = 0;
772 es->call_stmt_time = 0;
773 if (callee)
774 callee->remove_symbol_and_inline_clones ();
775 return e;
778 /* Set predicate for edge E. */
780 static void
781 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
783 /* If the edge is determined to be never executed, redirect it
784 to BUILTIN_UNREACHABLE to save inliner from inlining into it. */
785 if (predicate && false_predicate_p (predicate)
786 /* When handling speculative edges, we need to do the redirection
787 just once. Do it always on the direct edge, so we do not
788 attempt to resolve speculation while duplicating the edge. */
789 && (!e->speculative || e->callee))
790 e = redirect_to_unreachable (e);
792 struct inline_edge_summary *es = inline_edge_summary (e);
793 if (predicate && !true_predicate_p (predicate))
795 if (!es->predicate)
796 es->predicate = edge_predicate_pool.allocate ();
797 *es->predicate = *predicate;
799 else
801 if (es->predicate)
802 edge_predicate_pool.remove (es->predicate);
803 es->predicate = NULL;
807 /* Set predicate for hint *P. */
809 static void
810 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
812 if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
814 if (*p)
815 edge_predicate_pool.remove (*p);
816 *p = NULL;
818 else
820 if (!*p)
821 *p = edge_predicate_pool.allocate ();
822 **p = new_predicate;
827 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
828 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
829 Return clause of possible truths. When INLINE_P is true, assume that we are
830 inlining.
832 ERROR_MARK means compile time invariant. */
834 static clause_t
835 evaluate_conditions_for_known_args (struct cgraph_node *node,
836 bool inline_p,
837 vec<tree> known_vals,
838 vec<ipa_agg_jump_function_p>
839 known_aggs)
841 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
842 struct inline_summary *info = inline_summaries->get (node);
843 int i;
844 struct condition *c;
846 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
848 tree val;
849 tree res;
851 /* We allow call stmt to have fewer arguments than the callee function
852 (especially for K&R style programs). So bound check here (we assume
853 known_aggs vector, if non-NULL, has the same length as
854 known_vals). */
855 gcc_checking_assert (!known_aggs.exists ()
856 || (known_vals.length () == known_aggs.length ()));
857 if (c->operand_num >= (int) known_vals.length ())
859 clause |= 1 << (i + predicate_first_dynamic_condition);
860 continue;
863 if (c->agg_contents)
865 struct ipa_agg_jump_function *agg;
867 if (c->code == CHANGED
868 && !c->by_ref
869 && (known_vals[c->operand_num] == error_mark_node))
870 continue;
872 if (known_aggs.exists ())
874 agg = known_aggs[c->operand_num];
875 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
877 else
878 val = NULL_TREE;
880 else
882 val = known_vals[c->operand_num];
883 if (val == error_mark_node && c->code != CHANGED)
884 val = NULL_TREE;
887 if (!val)
889 clause |= 1 << (i + predicate_first_dynamic_condition);
890 continue;
892 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
893 continue;
895 if (operand_equal_p (TYPE_SIZE (TREE_TYPE (c->val)),
896 TYPE_SIZE (TREE_TYPE (val)), 0))
898 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (c->val), val);
900 res = val
901 ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
902 : NULL;
904 if (res && integer_zerop (res))
905 continue;
907 clause |= 1 << (i + predicate_first_dynamic_condition);
909 return clause;
913 /* Work out what conditions might be true at invocation of E. */
915 static void
916 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
917 clause_t *clause_ptr,
918 vec<tree> *known_vals_ptr,
919 vec<ipa_polymorphic_call_context>
920 *known_contexts_ptr,
921 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
923 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
924 struct inline_summary *info = inline_summaries->get (callee);
925 vec<tree> known_vals = vNULL;
926 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
928 if (clause_ptr)
929 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
930 if (known_vals_ptr)
931 known_vals_ptr->create (0);
932 if (known_contexts_ptr)
933 known_contexts_ptr->create (0);
935 if (ipa_node_params_sum
936 && !e->call_stmt_cannot_inline_p
937 && ((clause_ptr && info->conds) || known_vals_ptr || known_contexts_ptr))
939 struct ipa_node_params *parms_info;
940 struct ipa_edge_args *args = IPA_EDGE_REF (e);
941 struct inline_edge_summary *es = inline_edge_summary (e);
942 int i, count = ipa_get_cs_argument_count (args);
944 if (e->caller->global.inlined_to)
945 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
946 else
947 parms_info = IPA_NODE_REF (e->caller);
949 if (count && (info->conds || known_vals_ptr))
950 known_vals.safe_grow_cleared (count);
951 if (count && (info->conds || known_aggs_ptr))
952 known_aggs.safe_grow_cleared (count);
953 if (count && known_contexts_ptr)
954 known_contexts_ptr->safe_grow_cleared (count);
956 for (i = 0; i < count; i++)
958 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
959 tree cst = ipa_value_from_jfunc (parms_info, jf);
961 if (!cst && e->call_stmt
962 && i < (int)gimple_call_num_args (e->call_stmt))
964 cst = gimple_call_arg (e->call_stmt, i);
965 if (!is_gimple_min_invariant (cst))
966 cst = NULL;
968 if (cst)
970 gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
971 if (known_vals.exists ())
972 known_vals[i] = cst;
974 else if (inline_p && !es->param[i].change_prob)
975 known_vals[i] = error_mark_node;
977 if (known_contexts_ptr)
978 (*known_contexts_ptr)[i] = ipa_context_from_jfunc (parms_info, e,
979 i, jf);
980 /* TODO: When IPA-CP starts propagating and merging aggregate jump
981 functions, use its knowledge of the caller too, just like the
982 scalar case above. */
983 known_aggs[i] = &jf->agg;
986 else if (e->call_stmt && !e->call_stmt_cannot_inline_p
987 && ((clause_ptr && info->conds) || known_vals_ptr))
989 int i, count = (int)gimple_call_num_args (e->call_stmt);
991 if (count && (info->conds || known_vals_ptr))
992 known_vals.safe_grow_cleared (count);
993 for (i = 0; i < count; i++)
995 tree cst = gimple_call_arg (e->call_stmt, i);
996 if (!is_gimple_min_invariant (cst))
997 cst = NULL;
998 if (cst)
999 known_vals[i] = cst;
1003 if (clause_ptr)
1004 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
1005 known_vals, known_aggs);
1007 if (known_vals_ptr)
1008 *known_vals_ptr = known_vals;
1009 else
1010 known_vals.release ();
1012 if (known_aggs_ptr)
1013 *known_aggs_ptr = known_aggs;
1014 else
1015 known_aggs.release ();
1019 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
1021 static void
1022 inline_summary_alloc (void)
1024 if (!edge_removal_hook_holder)
1025 edge_removal_hook_holder =
1026 symtab->add_edge_removal_hook (&inline_edge_removal_hook, NULL);
1027 if (!edge_duplication_hook_holder)
1028 edge_duplication_hook_holder =
1029 symtab->add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
1031 if (!inline_summaries)
1032 inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
1034 if (inline_edge_summary_vec.length () <= (unsigned) symtab->edges_max_uid)
1035 inline_edge_summary_vec.safe_grow_cleared (symtab->edges_max_uid + 1);
1038 /* We are called multiple time for given function; clear
1039 data from previous run so they are not cumulated. */
1041 static void
1042 reset_inline_edge_summary (struct cgraph_edge *e)
1044 if (e->uid < (int) inline_edge_summary_vec.length ())
1046 struct inline_edge_summary *es = inline_edge_summary (e);
1048 es->call_stmt_size = es->call_stmt_time = 0;
1049 if (es->predicate)
1050 edge_predicate_pool.remove (es->predicate);
1051 es->predicate = NULL;
1052 es->param.release ();
1056 /* We are called multiple time for given function; clear
1057 data from previous run so they are not cumulated. */
1059 static void
1060 reset_inline_summary (struct cgraph_node *node,
1061 inline_summary *info)
1063 struct cgraph_edge *e;
1065 info->self_size = info->self_time = 0;
1066 info->estimated_stack_size = 0;
1067 info->estimated_self_stack_size = 0;
1068 info->stack_frame_offset = 0;
1069 info->size = 0;
1070 info->time = 0;
1071 info->growth = 0;
1072 info->scc_no = 0;
1073 if (info->loop_iterations)
1075 edge_predicate_pool.remove (info->loop_iterations);
1076 info->loop_iterations = NULL;
1078 if (info->loop_stride)
1080 edge_predicate_pool.remove (info->loop_stride);
1081 info->loop_stride = NULL;
1083 if (info->array_index)
1085 edge_predicate_pool.remove (info->array_index);
1086 info->array_index = NULL;
1088 vec_free (info->conds);
1089 vec_free (info->entry);
1090 for (e = node->callees; e; e = e->next_callee)
1091 reset_inline_edge_summary (e);
1092 for (e = node->indirect_calls; e; e = e->next_callee)
1093 reset_inline_edge_summary (e);
1096 /* Hook that is called by cgraph.c when a node is removed. */
1098 void
1099 inline_summary_t::remove (cgraph_node *node, inline_summary *info)
1101 reset_inline_summary (node, info);
1104 /* Remap predicate P of former function to be predicate of duplicated function.
1105 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1106 INFO is inline summary of the duplicated node. */
1108 static struct predicate
1109 remap_predicate_after_duplication (struct predicate *p,
1110 clause_t possible_truths,
1111 struct inline_summary *info)
1113 struct predicate new_predicate = true_predicate ();
1114 int j;
1115 for (j = 0; p->clause[j]; j++)
1116 if (!(possible_truths & p->clause[j]))
1118 new_predicate = false_predicate ();
1119 break;
1121 else
1122 add_clause (info->conds, &new_predicate,
1123 possible_truths & p->clause[j]);
1124 return new_predicate;
1127 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1128 Additionally care about allocating new memory slot for updated predicate
1129 and set it to NULL when it becomes true or false (and thus uninteresting).
1132 static void
1133 remap_hint_predicate_after_duplication (struct predicate **p,
1134 clause_t possible_truths,
1135 struct inline_summary *info)
1137 struct predicate new_predicate;
1139 if (!*p)
1140 return;
1142 new_predicate = remap_predicate_after_duplication (*p,
1143 possible_truths, info);
1144 /* We do not want to free previous predicate; it is used by node origin. */
1145 *p = NULL;
1146 set_hint_predicate (p, new_predicate);
1150 /* Hook that is called by cgraph.c when a node is duplicated. */
1151 void
1152 inline_summary_t::duplicate (cgraph_node *src,
1153 cgraph_node *dst,
1154 inline_summary *,
1155 inline_summary *info)
1157 inline_summary_alloc ();
1158 memcpy (info, inline_summaries->get (src), sizeof (inline_summary));
1159 /* TODO: as an optimization, we may avoid copying conditions
1160 that are known to be false or true. */
1161 info->conds = vec_safe_copy (info->conds);
1163 /* When there are any replacements in the function body, see if we can figure
1164 out that something was optimized out. */
1165 if (ipa_node_params_sum && dst->clone.tree_map)
1167 vec<size_time_entry, va_gc> *entry = info->entry;
1168 /* Use SRC parm info since it may not be copied yet. */
1169 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1170 vec<tree> known_vals = vNULL;
1171 int count = ipa_get_param_count (parms_info);
1172 int i, j;
1173 clause_t possible_truths;
1174 struct predicate true_pred = true_predicate ();
1175 size_time_entry *e;
1176 int optimized_out_size = 0;
1177 bool inlined_to_p = false;
1178 struct cgraph_edge *edge, *next;
1180 info->entry = 0;
1181 known_vals.safe_grow_cleared (count);
1182 for (i = 0; i < count; i++)
1184 struct ipa_replace_map *r;
1186 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1188 if (((!r->old_tree && r->parm_num == i)
1189 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1190 && r->replace_p && !r->ref_p)
1192 known_vals[i] = r->new_tree;
1193 break;
1197 possible_truths = evaluate_conditions_for_known_args (dst, false,
1198 known_vals,
1199 vNULL);
1200 known_vals.release ();
1202 account_size_time (info, 0, 0, &true_pred);
1204 /* Remap size_time vectors.
1205 Simplify the predicate by prunning out alternatives that are known
1206 to be false.
1207 TODO: as on optimization, we can also eliminate conditions known
1208 to be true. */
1209 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1211 struct predicate new_predicate;
1212 new_predicate = remap_predicate_after_duplication (&e->predicate,
1213 possible_truths,
1214 info);
1215 if (false_predicate_p (&new_predicate))
1216 optimized_out_size += e->size;
1217 else
1218 account_size_time (info, e->size, e->time, &new_predicate);
1221 /* Remap edge predicates with the same simplification as above.
1222 Also copy constantness arrays. */
1223 for (edge = dst->callees; edge; edge = next)
1225 struct predicate new_predicate;
1226 struct inline_edge_summary *es = inline_edge_summary (edge);
1227 next = edge->next_callee;
1229 if (!edge->inline_failed)
1230 inlined_to_p = true;
1231 if (!es->predicate)
1232 continue;
1233 new_predicate = remap_predicate_after_duplication (es->predicate,
1234 possible_truths,
1235 info);
1236 if (false_predicate_p (&new_predicate)
1237 && !false_predicate_p (es->predicate))
1238 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1239 edge_set_predicate (edge, &new_predicate);
1242 /* Remap indirect edge predicates with the same simplificaiton as above.
1243 Also copy constantness arrays. */
1244 for (edge = dst->indirect_calls; edge; edge = next)
1246 struct predicate new_predicate;
1247 struct inline_edge_summary *es = inline_edge_summary (edge);
1248 next = edge->next_callee;
1250 gcc_checking_assert (edge->inline_failed);
1251 if (!es->predicate)
1252 continue;
1253 new_predicate = remap_predicate_after_duplication (es->predicate,
1254 possible_truths,
1255 info);
1256 if (false_predicate_p (&new_predicate)
1257 && !false_predicate_p (es->predicate))
1258 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1259 edge_set_predicate (edge, &new_predicate);
1261 remap_hint_predicate_after_duplication (&info->loop_iterations,
1262 possible_truths, info);
1263 remap_hint_predicate_after_duplication (&info->loop_stride,
1264 possible_truths, info);
1265 remap_hint_predicate_after_duplication (&info->array_index,
1266 possible_truths, info);
1268 /* If inliner or someone after inliner will ever start producing
1269 non-trivial clones, we will get trouble with lack of information
1270 about updating self sizes, because size vectors already contains
1271 sizes of the calees. */
1272 gcc_assert (!inlined_to_p || !optimized_out_size);
1274 else
1276 info->entry = vec_safe_copy (info->entry);
1277 if (info->loop_iterations)
1279 predicate p = *info->loop_iterations;
1280 info->loop_iterations = NULL;
1281 set_hint_predicate (&info->loop_iterations, p);
1283 if (info->loop_stride)
1285 predicate p = *info->loop_stride;
1286 info->loop_stride = NULL;
1287 set_hint_predicate (&info->loop_stride, p);
1289 if (info->array_index)
1291 predicate p = *info->array_index;
1292 info->array_index = NULL;
1293 set_hint_predicate (&info->array_index, p);
1296 if (!dst->global.inlined_to)
1297 inline_update_overall_summary (dst);
1301 /* Hook that is called by cgraph.c when a node is duplicated. */
1303 static void
1304 inline_edge_duplication_hook (struct cgraph_edge *src,
1305 struct cgraph_edge *dst,
1306 ATTRIBUTE_UNUSED void *data)
1308 struct inline_edge_summary *info;
1309 struct inline_edge_summary *srcinfo;
1310 inline_summary_alloc ();
1311 info = inline_edge_summary (dst);
1312 srcinfo = inline_edge_summary (src);
1313 memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
1314 info->predicate = NULL;
1315 edge_set_predicate (dst, srcinfo->predicate);
1316 info->param = srcinfo->param.copy ();
1317 if (!dst->indirect_unknown_callee && src->indirect_unknown_callee)
1319 info->call_stmt_size -= (eni_size_weights.indirect_call_cost
1320 - eni_size_weights.call_cost);
1321 info->call_stmt_time -= (eni_time_weights.indirect_call_cost
1322 - eni_time_weights.call_cost);
1327 /* Keep edge cache consistent across edge removal. */
1329 static void
1330 inline_edge_removal_hook (struct cgraph_edge *edge,
1331 void *data ATTRIBUTE_UNUSED)
1333 if (edge_growth_cache.exists ())
1334 reset_edge_growth_cache (edge);
1335 reset_inline_edge_summary (edge);
1339 /* Initialize growth caches. */
1341 void
1342 initialize_growth_caches (void)
1344 if (symtab->edges_max_uid)
1345 edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
1349 /* Free growth caches. */
1351 void
1352 free_growth_caches (void)
1354 edge_growth_cache.release ();
1358 /* Dump edge summaries associated to NODE and recursively to all clones.
1359 Indent by INDENT. */
1361 static void
1362 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1363 struct inline_summary *info)
1365 struct cgraph_edge *edge;
1366 for (edge = node->callees; edge; edge = edge->next_callee)
1368 struct inline_edge_summary *es = inline_edge_summary (edge);
1369 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1370 int i;
1372 fprintf (f,
1373 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1374 " time: %2i callee size:%2i stack:%2i",
1375 indent, "", callee->name (), callee->order,
1376 !edge->inline_failed
1377 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1378 indent, "", es->loop_depth, edge->frequency,
1379 es->call_stmt_size, es->call_stmt_time,
1380 (int) inline_summaries->get (callee)->size / INLINE_SIZE_SCALE,
1381 (int) inline_summaries->get (callee)->estimated_stack_size);
1383 if (es->predicate)
1385 fprintf (f, " predicate: ");
1386 dump_predicate (f, info->conds, es->predicate);
1388 else
1389 fprintf (f, "\n");
1390 if (es->param.exists ())
1391 for (i = 0; i < (int) es->param.length (); i++)
1393 int prob = es->param[i].change_prob;
1395 if (!prob)
1396 fprintf (f, "%*s op%i is compile time invariant\n",
1397 indent + 2, "", i);
1398 else if (prob != REG_BR_PROB_BASE)
1399 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1400 prob * 100.0 / REG_BR_PROB_BASE);
1402 if (!edge->inline_failed)
1404 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1405 " callee size %i\n",
1406 indent + 2, "",
1407 (int) inline_summaries->get (callee)->stack_frame_offset,
1408 (int) inline_summaries->get (callee)->estimated_self_stack_size,
1409 (int) inline_summaries->get (callee)->estimated_stack_size);
1410 dump_inline_edge_summary (f, indent + 2, callee, info);
1413 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1415 struct inline_edge_summary *es = inline_edge_summary (edge);
1416 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1417 " time: %2i",
1418 indent, "",
1419 es->loop_depth,
1420 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1421 if (es->predicate)
1423 fprintf (f, "predicate: ");
1424 dump_predicate (f, info->conds, es->predicate);
1426 else
1427 fprintf (f, "\n");
1432 void
1433 dump_inline_summary (FILE *f, struct cgraph_node *node)
1435 if (node->definition)
1437 struct inline_summary *s = inline_summaries->get (node);
1438 size_time_entry *e;
1439 int i;
1440 fprintf (f, "Inline summary for %s/%i", node->name (),
1441 node->order);
1442 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1443 fprintf (f, " always_inline");
1444 if (s->inlinable)
1445 fprintf (f, " inlinable");
1446 if (s->contains_cilk_spawn)
1447 fprintf (f, " contains_cilk_spawn");
1448 fprintf (f, "\n self time: %i\n", s->self_time);
1449 fprintf (f, " global time: %i\n", s->time);
1450 fprintf (f, " self size: %i\n", s->self_size);
1451 fprintf (f, " global size: %i\n", s->size);
1452 fprintf (f, " min size: %i\n", s->min_size);
1453 fprintf (f, " self stack: %i\n",
1454 (int) s->estimated_self_stack_size);
1455 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1456 if (s->growth)
1457 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1458 if (s->scc_no)
1459 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1460 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1462 fprintf (f, " size:%f, time:%f, predicate:",
1463 (double) e->size / INLINE_SIZE_SCALE,
1464 (double) e->time / INLINE_TIME_SCALE);
1465 dump_predicate (f, s->conds, &e->predicate);
1467 if (s->loop_iterations)
1469 fprintf (f, " loop iterations:");
1470 dump_predicate (f, s->conds, s->loop_iterations);
1472 if (s->loop_stride)
1474 fprintf (f, " loop stride:");
1475 dump_predicate (f, s->conds, s->loop_stride);
1477 if (s->array_index)
1479 fprintf (f, " array index:");
1480 dump_predicate (f, s->conds, s->array_index);
1482 fprintf (f, " calls:\n");
1483 dump_inline_edge_summary (f, 4, node, s);
1484 fprintf (f, "\n");
1488 DEBUG_FUNCTION void
1489 debug_inline_summary (struct cgraph_node *node)
1491 dump_inline_summary (stderr, node);
1494 void
1495 dump_inline_summaries (FILE *f)
1497 struct cgraph_node *node;
1499 FOR_EACH_DEFINED_FUNCTION (node)
1500 if (!node->global.inlined_to)
1501 dump_inline_summary (f, node);
1504 /* Give initial reasons why inlining would fail on EDGE. This gets either
1505 nullified or usually overwritten by more precise reasons later. */
1507 void
1508 initialize_inline_failed (struct cgraph_edge *e)
1510 struct cgraph_node *callee = e->callee;
1512 if (e->indirect_unknown_callee)
1513 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1514 else if (!callee->definition)
1515 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1516 else if (callee->local.redefined_extern_inline)
1517 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1518 else if (e->call_stmt_cannot_inline_p)
1519 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1520 else if (cfun && fn_contains_cilk_spawn_p (cfun))
1521 /* We can't inline if the function is spawing a function. */
1522 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
1523 else
1524 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1527 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1528 boolean variable pointed to by DATA. */
1530 static bool
1531 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1532 void *data)
1534 bool *b = (bool *) data;
1535 *b = true;
1536 return true;
1539 /* If OP refers to value of function parameter, return the corresponding
1540 parameter. */
1542 static tree
1543 unmodified_parm_1 (gimple stmt, tree op)
1545 /* SSA_NAME referring to parm default def? */
1546 if (TREE_CODE (op) == SSA_NAME
1547 && SSA_NAME_IS_DEFAULT_DEF (op)
1548 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1549 return SSA_NAME_VAR (op);
1550 /* Non-SSA parm reference? */
1551 if (TREE_CODE (op) == PARM_DECL)
1553 bool modified = false;
1555 ao_ref refd;
1556 ao_ref_init (&refd, op);
1557 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1558 NULL);
1559 if (!modified)
1560 return op;
1562 return NULL_TREE;
1565 /* If OP refers to value of function parameter, return the corresponding
1566 parameter. Also traverse chains of SSA register assignments. */
1568 static tree
1569 unmodified_parm (gimple stmt, tree op)
1571 tree res = unmodified_parm_1 (stmt, op);
1572 if (res)
1573 return res;
1575 if (TREE_CODE (op) == SSA_NAME
1576 && !SSA_NAME_IS_DEFAULT_DEF (op)
1577 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1578 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1579 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1580 return NULL_TREE;
1583 /* If OP refers to a value of a function parameter or value loaded from an
1584 aggregate passed to a parameter (either by value or reference), return TRUE
1585 and store the number of the parameter to *INDEX_P and information whether
1586 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1587 the function parameters, STMT is the statement in which OP is used or
1588 loaded. */
1590 static bool
1591 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1592 gimple stmt, tree op, int *index_p,
1593 struct agg_position_info *aggpos)
1595 tree res = unmodified_parm_1 (stmt, op);
1597 gcc_checking_assert (aggpos);
1598 if (res)
1600 *index_p = ipa_get_param_decl_index (info, res);
1601 if (*index_p < 0)
1602 return false;
1603 aggpos->agg_contents = false;
1604 aggpos->by_ref = false;
1605 return true;
1608 if (TREE_CODE (op) == SSA_NAME)
1610 if (SSA_NAME_IS_DEFAULT_DEF (op)
1611 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1612 return false;
1613 stmt = SSA_NAME_DEF_STMT (op);
1614 op = gimple_assign_rhs1 (stmt);
1615 if (!REFERENCE_CLASS_P (op))
1616 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1617 aggpos);
1620 aggpos->agg_contents = true;
1621 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1622 &aggpos->by_ref);
1625 /* See if statement might disappear after inlining.
1626 0 - means not eliminated
1627 1 - half of statements goes away
1628 2 - for sure it is eliminated.
1629 We are not terribly sophisticated, basically looking for simple abstraction
1630 penalty wrappers. */
1632 static int
1633 eliminated_by_inlining_prob (gimple stmt)
1635 enum gimple_code code = gimple_code (stmt);
1636 enum tree_code rhs_code;
1638 if (!optimize)
1639 return 0;
1641 switch (code)
1643 case GIMPLE_RETURN:
1644 return 2;
1645 case GIMPLE_ASSIGN:
1646 if (gimple_num_ops (stmt) != 2)
1647 return 0;
1649 rhs_code = gimple_assign_rhs_code (stmt);
1651 /* Casts of parameters, loads from parameters passed by reference
1652 and stores to return value or parameters are often free after
1653 inlining dua to SRA and further combining.
1654 Assume that half of statements goes away. */
1655 if (CONVERT_EXPR_CODE_P (rhs_code)
1656 || rhs_code == VIEW_CONVERT_EXPR
1657 || rhs_code == ADDR_EXPR
1658 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1660 tree rhs = gimple_assign_rhs1 (stmt);
1661 tree lhs = gimple_assign_lhs (stmt);
1662 tree inner_rhs = get_base_address (rhs);
1663 tree inner_lhs = get_base_address (lhs);
1664 bool rhs_free = false;
1665 bool lhs_free = false;
1667 if (!inner_rhs)
1668 inner_rhs = rhs;
1669 if (!inner_lhs)
1670 inner_lhs = lhs;
1672 /* Reads of parameter are expected to be free. */
1673 if (unmodified_parm (stmt, inner_rhs))
1674 rhs_free = true;
1675 /* Match expressions of form &this->field. Those will most likely
1676 combine with something upstream after inlining. */
1677 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1679 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1680 if (TREE_CODE (op) == PARM_DECL)
1681 rhs_free = true;
1682 else if (TREE_CODE (op) == MEM_REF
1683 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1684 rhs_free = true;
1687 /* When parameter is not SSA register because its address is taken
1688 and it is just copied into one, the statement will be completely
1689 free after inlining (we will copy propagate backward). */
1690 if (rhs_free && is_gimple_reg (lhs))
1691 return 2;
1693 /* Reads of parameters passed by reference
1694 expected to be free (i.e. optimized out after inlining). */
1695 if (TREE_CODE (inner_rhs) == MEM_REF
1696 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1697 rhs_free = true;
1699 /* Copying parameter passed by reference into gimple register is
1700 probably also going to copy propagate, but we can't be quite
1701 sure. */
1702 if (rhs_free && is_gimple_reg (lhs))
1703 lhs_free = true;
1705 /* Writes to parameters, parameters passed by value and return value
1706 (either dirrectly or passed via invisible reference) are free.
1708 TODO: We ought to handle testcase like
1709 struct a {int a,b;};
1710 struct a
1711 retrurnsturct (void)
1713 struct a a ={1,2};
1714 return a;
1717 This translate into:
1719 retrurnsturct ()
1721 int a$b;
1722 int a$a;
1723 struct a a;
1724 struct a D.2739;
1726 <bb 2>:
1727 D.2739.a = 1;
1728 D.2739.b = 2;
1729 return D.2739;
1732 For that we either need to copy ipa-split logic detecting writes
1733 to return value. */
1734 if (TREE_CODE (inner_lhs) == PARM_DECL
1735 || TREE_CODE (inner_lhs) == RESULT_DECL
1736 || (TREE_CODE (inner_lhs) == MEM_REF
1737 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1738 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1739 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1740 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1741 (inner_lhs,
1742 0))) == RESULT_DECL))))
1743 lhs_free = true;
1744 if (lhs_free
1745 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1746 rhs_free = true;
1747 if (lhs_free && rhs_free)
1748 return 1;
1750 return 0;
1751 default:
1752 return 0;
1757 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1758 predicates to the CFG edges. */
1760 static void
1761 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1762 struct inline_summary *summary,
1763 basic_block bb)
1765 gimple last;
1766 tree op;
1767 int index;
1768 struct agg_position_info aggpos;
1769 enum tree_code code, inverted_code;
1770 edge e;
1771 edge_iterator ei;
1772 gimple set_stmt;
1773 tree op2;
1775 last = last_stmt (bb);
1776 if (!last || gimple_code (last) != GIMPLE_COND)
1777 return;
1778 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1779 return;
1780 op = gimple_cond_lhs (last);
1781 /* TODO: handle conditionals like
1782 var = op0 < 4;
1783 if (var != 0). */
1784 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1786 code = gimple_cond_code (last);
1787 inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1789 FOR_EACH_EDGE (e, ei, bb->succs)
1791 enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1792 ? code : inverted_code);
1793 /* invert_tree_comparison will return ERROR_MARK on FP
1794 comparsions that are not EQ/NE instead of returning proper
1795 unordered one. Be sure it is not confused with NON_CONSTANT. */
1796 if (this_code != ERROR_MARK)
1798 struct predicate p = add_condition (summary, index, &aggpos,
1799 this_code,
1800 gimple_cond_rhs (last));
1801 e->aux = edge_predicate_pool.allocate ();
1802 *(struct predicate *) e->aux = p;
1807 if (TREE_CODE (op) != SSA_NAME)
1808 return;
1809 /* Special case
1810 if (builtin_constant_p (op))
1811 constant_code
1812 else
1813 nonconstant_code.
1814 Here we can predicate nonconstant_code. We can't
1815 really handle constant_code since we have no predicate
1816 for this and also the constant code is not known to be
1817 optimized away when inliner doen't see operand is constant.
1818 Other optimizers might think otherwise. */
1819 if (gimple_cond_code (last) != NE_EXPR
1820 || !integer_zerop (gimple_cond_rhs (last)))
1821 return;
1822 set_stmt = SSA_NAME_DEF_STMT (op);
1823 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1824 || gimple_call_num_args (set_stmt) != 1)
1825 return;
1826 op2 = gimple_call_arg (set_stmt, 0);
1827 if (!unmodified_parm_or_parm_agg_item
1828 (info, set_stmt, op2, &index, &aggpos))
1829 return;
1830 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1832 struct predicate p = add_condition (summary, index, &aggpos,
1833 IS_NOT_CONSTANT, NULL_TREE);
1834 e->aux = edge_predicate_pool.allocate ();
1835 *(struct predicate *) e->aux = p;
1840 /* If BB ends by a switch we can turn into predicates, attach corresponding
1841 predicates to the CFG edges. */
1843 static void
1844 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1845 struct inline_summary *summary,
1846 basic_block bb)
1848 gimple lastg;
1849 tree op;
1850 int index;
1851 struct agg_position_info aggpos;
1852 edge e;
1853 edge_iterator ei;
1854 size_t n;
1855 size_t case_idx;
1857 lastg = last_stmt (bb);
1858 if (!lastg || gimple_code (lastg) != GIMPLE_SWITCH)
1859 return;
1860 gswitch *last = as_a <gswitch *> (lastg);
1861 op = gimple_switch_index (last);
1862 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1863 return;
1865 FOR_EACH_EDGE (e, ei, bb->succs)
1867 e->aux = edge_predicate_pool.allocate ();
1868 *(struct predicate *) e->aux = false_predicate ();
1870 n = gimple_switch_num_labels (last);
1871 for (case_idx = 0; case_idx < n; ++case_idx)
1873 tree cl = gimple_switch_label (last, case_idx);
1874 tree min, max;
1875 struct predicate p;
1877 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1878 min = CASE_LOW (cl);
1879 max = CASE_HIGH (cl);
1881 /* For default we might want to construct predicate that none
1882 of cases is met, but it is bit hard to do not having negations
1883 of conditionals handy. */
1884 if (!min && !max)
1885 p = true_predicate ();
1886 else if (!max)
1887 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1888 else
1890 struct predicate p1, p2;
1891 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1892 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1893 p = and_predicates (summary->conds, &p1, &p2);
1895 *(struct predicate *) e->aux
1896 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1901 /* For each BB in NODE attach to its AUX pointer predicate under
1902 which it is executable. */
1904 static void
1905 compute_bb_predicates (struct cgraph_node *node,
1906 struct ipa_node_params *parms_info,
1907 struct inline_summary *summary)
1909 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1910 bool done = false;
1911 basic_block bb;
1913 FOR_EACH_BB_FN (bb, my_function)
1915 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1916 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1919 /* Entry block is always executable. */
1920 ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1921 = edge_predicate_pool.allocate ();
1922 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1923 = true_predicate ();
1925 /* A simple dataflow propagation of predicates forward in the CFG.
1926 TODO: work in reverse postorder. */
1927 while (!done)
1929 done = true;
1930 FOR_EACH_BB_FN (bb, my_function)
1932 struct predicate p = false_predicate ();
1933 edge e;
1934 edge_iterator ei;
1935 FOR_EACH_EDGE (e, ei, bb->preds)
1937 if (e->src->aux)
1939 struct predicate this_bb_predicate
1940 = *(struct predicate *) e->src->aux;
1941 if (e->aux)
1942 this_bb_predicate
1943 = and_predicates (summary->conds, &this_bb_predicate,
1944 (struct predicate *) e->aux);
1945 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1946 if (true_predicate_p (&p))
1947 break;
1950 if (false_predicate_p (&p))
1951 gcc_assert (!bb->aux);
1952 else
1954 if (!bb->aux)
1956 done = false;
1957 bb->aux = edge_predicate_pool.allocate ();
1958 *((struct predicate *) bb->aux) = p;
1960 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1962 /* This OR operation is needed to ensure monotonous data flow
1963 in the case we hit the limit on number of clauses and the
1964 and/or operations above give approximate answers. */
1965 p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
1966 if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1968 done = false;
1969 *((struct predicate *) bb->aux) = p;
1978 /* We keep info about constantness of SSA names. */
1980 typedef struct predicate predicate_t;
1981 /* Return predicate specifying when the STMT might have result that is not
1982 a compile time constant. */
1984 static struct predicate
1985 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1986 struct inline_summary *summary,
1987 tree expr,
1988 vec<predicate_t> nonconstant_names)
1990 tree parm;
1991 int index;
1993 while (UNARY_CLASS_P (expr))
1994 expr = TREE_OPERAND (expr, 0);
1996 parm = unmodified_parm (NULL, expr);
1997 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1998 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1999 if (is_gimple_min_invariant (expr))
2000 return false_predicate ();
2001 if (TREE_CODE (expr) == SSA_NAME)
2002 return nonconstant_names[SSA_NAME_VERSION (expr)];
2003 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2005 struct predicate p1 = will_be_nonconstant_expr_predicate
2006 (info, summary, TREE_OPERAND (expr, 0),
2007 nonconstant_names);
2008 struct predicate p2;
2009 if (true_predicate_p (&p1))
2010 return p1;
2011 p2 = will_be_nonconstant_expr_predicate (info, summary,
2012 TREE_OPERAND (expr, 1),
2013 nonconstant_names);
2014 return or_predicates (summary->conds, &p1, &p2);
2016 else if (TREE_CODE (expr) == COND_EXPR)
2018 struct predicate p1 = will_be_nonconstant_expr_predicate
2019 (info, summary, TREE_OPERAND (expr, 0),
2020 nonconstant_names);
2021 struct predicate p2;
2022 if (true_predicate_p (&p1))
2023 return p1;
2024 p2 = will_be_nonconstant_expr_predicate (info, summary,
2025 TREE_OPERAND (expr, 1),
2026 nonconstant_names);
2027 if (true_predicate_p (&p2))
2028 return p2;
2029 p1 = or_predicates (summary->conds, &p1, &p2);
2030 p2 = will_be_nonconstant_expr_predicate (info, summary,
2031 TREE_OPERAND (expr, 2),
2032 nonconstant_names);
2033 return or_predicates (summary->conds, &p1, &p2);
2035 else
2037 debug_tree (expr);
2038 gcc_unreachable ();
2040 return false_predicate ();
2044 /* Return predicate specifying when the STMT might have result that is not
2045 a compile time constant. */
2047 static struct predicate
2048 will_be_nonconstant_predicate (struct ipa_node_params *info,
2049 struct inline_summary *summary,
2050 gimple stmt,
2051 vec<predicate_t> nonconstant_names)
2053 struct predicate p = true_predicate ();
2054 ssa_op_iter iter;
2055 tree use;
2056 struct predicate op_non_const;
2057 bool is_load;
2058 int base_index;
2059 struct agg_position_info aggpos;
2061 /* What statments might be optimized away
2062 when their arguments are constant. */
2063 if (gimple_code (stmt) != GIMPLE_ASSIGN
2064 && gimple_code (stmt) != GIMPLE_COND
2065 && gimple_code (stmt) != GIMPLE_SWITCH
2066 && (gimple_code (stmt) != GIMPLE_CALL
2067 || !(gimple_call_flags (stmt) & ECF_CONST)))
2068 return p;
2070 /* Stores will stay anyway. */
2071 if (gimple_store_p (stmt))
2072 return p;
2074 is_load = gimple_assign_load_p (stmt);
2076 /* Loads can be optimized when the value is known. */
2077 if (is_load)
2079 tree op;
2080 gcc_assert (gimple_assign_single_p (stmt));
2081 op = gimple_assign_rhs1 (stmt);
2082 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
2083 &aggpos))
2084 return p;
2086 else
2087 base_index = -1;
2089 /* See if we understand all operands before we start
2090 adding conditionals. */
2091 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2093 tree parm = unmodified_parm (stmt, use);
2094 /* For arguments we can build a condition. */
2095 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2096 continue;
2097 if (TREE_CODE (use) != SSA_NAME)
2098 return p;
2099 /* If we know when operand is constant,
2100 we still can say something useful. */
2101 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2102 continue;
2103 return p;
2106 if (is_load)
2107 op_non_const =
2108 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2109 else
2110 op_non_const = false_predicate ();
2111 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2113 tree parm = unmodified_parm (stmt, use);
2114 int index;
2116 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2118 if (index != base_index)
2119 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2120 else
2121 continue;
2123 else
2124 p = nonconstant_names[SSA_NAME_VERSION (use)];
2125 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2127 if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2128 && gimple_op (stmt, 0)
2129 && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2130 nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2131 = op_non_const;
2132 return op_non_const;
2135 struct record_modified_bb_info
2137 bitmap bb_set;
2138 gimple stmt;
2141 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2142 set except for info->stmt. */
2144 static bool
2145 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2147 struct record_modified_bb_info *info =
2148 (struct record_modified_bb_info *) data;
2149 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2150 return false;
2151 bitmap_set_bit (info->bb_set,
2152 SSA_NAME_IS_DEFAULT_DEF (vdef)
2153 ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2154 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2155 return false;
2158 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2159 will change since last invocation of STMT.
2161 Value 0 is reserved for compile time invariants.
2162 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2163 ought to be REG_BR_PROB_BASE / estimated_iters. */
2165 static int
2166 param_change_prob (gimple stmt, int i)
2168 tree op = gimple_call_arg (stmt, i);
2169 basic_block bb = gimple_bb (stmt);
2170 tree base;
2172 /* Global invariants neve change. */
2173 if (is_gimple_min_invariant (op))
2174 return 0;
2175 /* We would have to do non-trivial analysis to really work out what
2176 is the probability of value to change (i.e. when init statement
2177 is in a sibling loop of the call).
2179 We do an conservative estimate: when call is executed N times more often
2180 than the statement defining value, we take the frequency 1/N. */
2181 if (TREE_CODE (op) == SSA_NAME)
2183 int init_freq;
2185 if (!bb->frequency)
2186 return REG_BR_PROB_BASE;
2188 if (SSA_NAME_IS_DEFAULT_DEF (op))
2189 init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2190 else
2191 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2193 if (!init_freq)
2194 init_freq = 1;
2195 if (init_freq < bb->frequency)
2196 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2197 else
2198 return REG_BR_PROB_BASE;
2201 base = get_base_address (op);
2202 if (base)
2204 ao_ref refd;
2205 int max;
2206 struct record_modified_bb_info info;
2207 bitmap_iterator bi;
2208 unsigned index;
2209 tree init = ctor_for_folding (base);
2211 if (init != error_mark_node)
2212 return 0;
2213 if (!bb->frequency)
2214 return REG_BR_PROB_BASE;
2215 ao_ref_init (&refd, op);
2216 info.stmt = stmt;
2217 info.bb_set = BITMAP_ALLOC (NULL);
2218 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2219 NULL);
2220 if (bitmap_bit_p (info.bb_set, bb->index))
2222 BITMAP_FREE (info.bb_set);
2223 return REG_BR_PROB_BASE;
2226 /* Assume that every memory is initialized at entry.
2227 TODO: Can we easilly determine if value is always defined
2228 and thus we may skip entry block? */
2229 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
2230 max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2231 else
2232 max = 1;
2234 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2235 max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
2237 BITMAP_FREE (info.bb_set);
2238 if (max < bb->frequency)
2239 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2240 else
2241 return REG_BR_PROB_BASE;
2243 return REG_BR_PROB_BASE;
2246 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2247 sub-graph and if the predicate the condition depends on is known. If so,
2248 return true and store the pointer the predicate in *P. */
2250 static bool
2251 phi_result_unknown_predicate (struct ipa_node_params *info,
2252 inline_summary *summary, basic_block bb,
2253 struct predicate *p,
2254 vec<predicate_t> nonconstant_names)
2256 edge e;
2257 edge_iterator ei;
2258 basic_block first_bb = NULL;
2259 gimple stmt;
2261 if (single_pred_p (bb))
2263 *p = false_predicate ();
2264 return true;
2267 FOR_EACH_EDGE (e, ei, bb->preds)
2269 if (single_succ_p (e->src))
2271 if (!single_pred_p (e->src))
2272 return false;
2273 if (!first_bb)
2274 first_bb = single_pred (e->src);
2275 else if (single_pred (e->src) != first_bb)
2276 return false;
2278 else
2280 if (!first_bb)
2281 first_bb = e->src;
2282 else if (e->src != first_bb)
2283 return false;
2287 if (!first_bb)
2288 return false;
2290 stmt = last_stmt (first_bb);
2291 if (!stmt
2292 || gimple_code (stmt) != GIMPLE_COND
2293 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2294 return false;
2296 *p = will_be_nonconstant_expr_predicate (info, summary,
2297 gimple_cond_lhs (stmt),
2298 nonconstant_names);
2299 if (true_predicate_p (p))
2300 return false;
2301 else
2302 return true;
2305 /* Given a PHI statement in a function described by inline properties SUMMARY
2306 and *P being the predicate describing whether the selected PHI argument is
2307 known, store a predicate for the result of the PHI statement into
2308 NONCONSTANT_NAMES, if possible. */
2310 static void
2311 predicate_for_phi_result (struct inline_summary *summary, gphi *phi,
2312 struct predicate *p,
2313 vec<predicate_t> nonconstant_names)
2315 unsigned i;
2317 for (i = 0; i < gimple_phi_num_args (phi); i++)
2319 tree arg = gimple_phi_arg (phi, i)->def;
2320 if (!is_gimple_min_invariant (arg))
2322 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2323 *p = or_predicates (summary->conds, p,
2324 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2325 if (true_predicate_p (p))
2326 return;
2330 if (dump_file && (dump_flags & TDF_DETAILS))
2332 fprintf (dump_file, "\t\tphi predicate: ");
2333 dump_predicate (dump_file, summary->conds, p);
2335 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2338 /* Return predicate specifying when array index in access OP becomes non-constant. */
2340 static struct predicate
2341 array_index_predicate (inline_summary *info,
2342 vec< predicate_t> nonconstant_names, tree op)
2344 struct predicate p = false_predicate ();
2345 while (handled_component_p (op))
2347 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2349 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2350 p = or_predicates (info->conds, &p,
2351 &nonconstant_names[SSA_NAME_VERSION
2352 (TREE_OPERAND (op, 1))]);
2354 op = TREE_OPERAND (op, 0);
2356 return p;
2359 /* For a typical usage of __builtin_expect (a<b, 1), we
2360 may introduce an extra relation stmt:
2361 With the builtin, we have
2362 t1 = a <= b;
2363 t2 = (long int) t1;
2364 t3 = __builtin_expect (t2, 1);
2365 if (t3 != 0)
2366 goto ...
2367 Without the builtin, we have
2368 if (a<=b)
2369 goto...
2370 This affects the size/time estimation and may have
2371 an impact on the earlier inlining.
2372 Here find this pattern and fix it up later. */
2374 static gimple
2375 find_foldable_builtin_expect (basic_block bb)
2377 gimple_stmt_iterator bsi;
2379 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2381 gimple stmt = gsi_stmt (bsi);
2382 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2383 || (is_gimple_call (stmt)
2384 && gimple_call_internal_p (stmt)
2385 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
2387 tree var = gimple_call_lhs (stmt);
2388 tree arg = gimple_call_arg (stmt, 0);
2389 use_operand_p use_p;
2390 gimple use_stmt;
2391 bool match = false;
2392 bool done = false;
2394 if (!var || !arg)
2395 continue;
2396 gcc_assert (TREE_CODE (var) == SSA_NAME);
2398 while (TREE_CODE (arg) == SSA_NAME)
2400 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2401 if (!is_gimple_assign (stmt_tmp))
2402 break;
2403 switch (gimple_assign_rhs_code (stmt_tmp))
2405 case LT_EXPR:
2406 case LE_EXPR:
2407 case GT_EXPR:
2408 case GE_EXPR:
2409 case EQ_EXPR:
2410 case NE_EXPR:
2411 match = true;
2412 done = true;
2413 break;
2414 CASE_CONVERT:
2415 break;
2416 default:
2417 done = true;
2418 break;
2420 if (done)
2421 break;
2422 arg = gimple_assign_rhs1 (stmt_tmp);
2425 if (match && single_imm_use (var, &use_p, &use_stmt)
2426 && gimple_code (use_stmt) == GIMPLE_COND)
2427 return use_stmt;
2430 return NULL;
2433 /* Return true when the basic blocks contains only clobbers followed by RESX.
2434 Such BBs are kept around to make removal of dead stores possible with
2435 presence of EH and will be optimized out by optimize_clobbers later in the
2436 game.
2438 NEED_EH is used to recurse in case the clobber has non-EH predecestors
2439 that can be clobber only, too.. When it is false, the RESX is not necessary
2440 on the end of basic block. */
2442 static bool
2443 clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2445 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2446 edge_iterator ei;
2447 edge e;
2449 if (need_eh)
2451 if (gsi_end_p (gsi))
2452 return false;
2453 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2454 return false;
2455 gsi_prev (&gsi);
2457 else if (!single_succ_p (bb))
2458 return false;
2460 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2462 gimple stmt = gsi_stmt (gsi);
2463 if (is_gimple_debug (stmt))
2464 continue;
2465 if (gimple_clobber_p (stmt))
2466 continue;
2467 if (gimple_code (stmt) == GIMPLE_LABEL)
2468 break;
2469 return false;
2472 /* See if all predecestors are either throws or clobber only BBs. */
2473 FOR_EACH_EDGE (e, ei, bb->preds)
2474 if (!(e->flags & EDGE_EH)
2475 && !clobber_only_eh_bb_p (e->src, false))
2476 return false;
2478 return true;
2481 /* Compute function body size parameters for NODE.
2482 When EARLY is true, we compute only simple summaries without
2483 non-trivial predicates to drive the early inliner. */
2485 static void
2486 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2488 gcov_type time = 0;
2489 /* Estimate static overhead for function prologue/epilogue and alignment. */
2490 int size = 2;
2491 /* Benefits are scaled by probability of elimination that is in range
2492 <0,2>. */
2493 basic_block bb;
2494 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2495 int freq;
2496 struct inline_summary *info = inline_summaries->get (node);
2497 struct predicate bb_predicate;
2498 struct ipa_node_params *parms_info = NULL;
2499 vec<predicate_t> nonconstant_names = vNULL;
2500 int nblocks, n;
2501 int *order;
2502 predicate array_index = true_predicate ();
2503 gimple fix_builtin_expect_stmt;
2505 info->conds = NULL;
2506 info->entry = NULL;
2508 /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2509 so we can produce proper inline hints.
2511 When optimizing and analyzing for early inliner, initialize node params
2512 so we can produce correct BB predicates. */
2514 if (opt_for_fn (node->decl, optimize))
2516 calculate_dominance_info (CDI_DOMINATORS);
2517 if (!early)
2518 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2519 else
2521 ipa_check_create_node_params ();
2522 ipa_initialize_node_params (node);
2525 if (ipa_node_params_sum)
2527 parms_info = IPA_NODE_REF (node);
2528 nonconstant_names.safe_grow_cleared
2529 (SSANAMES (my_function)->length ());
2533 if (dump_file)
2534 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2535 node->name ());
2537 /* When we run into maximal number of entries, we assign everything to the
2538 constant truth case. Be sure to have it in list. */
2539 bb_predicate = true_predicate ();
2540 account_size_time (info, 0, 0, &bb_predicate);
2542 bb_predicate = not_inlined_predicate ();
2543 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2545 gcc_assert (my_function && my_function->cfg);
2546 if (parms_info)
2547 compute_bb_predicates (node, parms_info, info);
2548 gcc_assert (cfun == my_function);
2549 order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2550 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2551 for (n = 0; n < nblocks; n++)
2553 bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2554 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2555 if (clobber_only_eh_bb_p (bb))
2557 if (dump_file && (dump_flags & TDF_DETAILS))
2558 fprintf (dump_file, "\n Ignoring BB %i;"
2559 " it will be optimized away by cleanup_clobbers\n",
2560 bb->index);
2561 continue;
2564 /* TODO: Obviously predicates can be propagated down across CFG. */
2565 if (parms_info)
2567 if (bb->aux)
2568 bb_predicate = *(struct predicate *) bb->aux;
2569 else
2570 bb_predicate = false_predicate ();
2572 else
2573 bb_predicate = true_predicate ();
2575 if (dump_file && (dump_flags & TDF_DETAILS))
2577 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2578 dump_predicate (dump_file, info->conds, &bb_predicate);
2581 if (parms_info && nonconstant_names.exists ())
2583 struct predicate phi_predicate;
2584 bool first_phi = true;
2586 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2587 gsi_next (&bsi))
2589 if (first_phi
2590 && !phi_result_unknown_predicate (parms_info, info, bb,
2591 &phi_predicate,
2592 nonconstant_names))
2593 break;
2594 first_phi = false;
2595 if (dump_file && (dump_flags & TDF_DETAILS))
2597 fprintf (dump_file, " ");
2598 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2600 predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2601 nonconstant_names);
2605 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2607 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2608 gsi_next (&bsi))
2610 gimple stmt = gsi_stmt (bsi);
2611 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2612 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2613 int prob;
2614 struct predicate will_be_nonconstant;
2616 /* This relation stmt should be folded after we remove
2617 buildin_expect call. Adjust the cost here. */
2618 if (stmt == fix_builtin_expect_stmt)
2620 this_size--;
2621 this_time--;
2624 if (dump_file && (dump_flags & TDF_DETAILS))
2626 fprintf (dump_file, " ");
2627 print_gimple_stmt (dump_file, stmt, 0, 0);
2628 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2629 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2630 this_time);
2633 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2635 struct predicate this_array_index;
2636 this_array_index =
2637 array_index_predicate (info, nonconstant_names,
2638 gimple_assign_rhs1 (stmt));
2639 if (!false_predicate_p (&this_array_index))
2640 array_index =
2641 and_predicates (info->conds, &array_index,
2642 &this_array_index);
2644 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2646 struct predicate this_array_index;
2647 this_array_index =
2648 array_index_predicate (info, nonconstant_names,
2649 gimple_get_lhs (stmt));
2650 if (!false_predicate_p (&this_array_index))
2651 array_index =
2652 and_predicates (info->conds, &array_index,
2653 &this_array_index);
2657 if (is_gimple_call (stmt)
2658 && !gimple_call_internal_p (stmt))
2660 struct cgraph_edge *edge = node->get_edge (stmt);
2661 struct inline_edge_summary *es = inline_edge_summary (edge);
2663 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2664 resolved as constant. We however don't want to optimize
2665 out the cgraph edges. */
2666 if (nonconstant_names.exists ()
2667 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2668 && gimple_call_lhs (stmt)
2669 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2671 struct predicate false_p = false_predicate ();
2672 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2673 = false_p;
2675 if (ipa_node_params_sum)
2677 int count = gimple_call_num_args (stmt);
2678 int i;
2680 if (count)
2681 es->param.safe_grow_cleared (count);
2682 for (i = 0; i < count; i++)
2684 int prob = param_change_prob (stmt, i);
2685 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2686 es->param[i].change_prob = prob;
2690 es->call_stmt_size = this_size;
2691 es->call_stmt_time = this_time;
2692 es->loop_depth = bb_loop_depth (bb);
2693 edge_set_predicate (edge, &bb_predicate);
2696 /* TODO: When conditional jump or swithc is known to be constant, but
2697 we did not translate it into the predicates, we really can account
2698 just maximum of the possible paths. */
2699 if (parms_info)
2700 will_be_nonconstant
2701 = will_be_nonconstant_predicate (parms_info, info,
2702 stmt, nonconstant_names);
2703 if (this_time || this_size)
2705 struct predicate p;
2707 this_time *= freq;
2709 prob = eliminated_by_inlining_prob (stmt);
2710 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2711 fprintf (dump_file,
2712 "\t\t50%% will be eliminated by inlining\n");
2713 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2714 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2716 if (parms_info)
2717 p = and_predicates (info->conds, &bb_predicate,
2718 &will_be_nonconstant);
2719 else
2720 p = true_predicate ();
2722 if (!false_predicate_p (&p)
2723 || (is_gimple_call (stmt)
2724 && !false_predicate_p (&bb_predicate)))
2726 time += this_time;
2727 size += this_size;
2728 if (time > MAX_TIME * INLINE_TIME_SCALE)
2729 time = MAX_TIME * INLINE_TIME_SCALE;
2732 /* We account everything but the calls. Calls have their own
2733 size/time info attached to cgraph edges. This is necessary
2734 in order to make the cost disappear after inlining. */
2735 if (!is_gimple_call (stmt))
2737 if (prob)
2739 struct predicate ip = not_inlined_predicate ();
2740 ip = and_predicates (info->conds, &ip, &p);
2741 account_size_time (info, this_size * prob,
2742 this_time * prob, &ip);
2744 if (prob != 2)
2745 account_size_time (info, this_size * (2 - prob),
2746 this_time * (2 - prob), &p);
2749 gcc_assert (time >= 0);
2750 gcc_assert (size >= 0);
2754 set_hint_predicate (&inline_summaries->get (node)->array_index, array_index);
2755 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2756 if (time > MAX_TIME)
2757 time = MAX_TIME;
2758 free (order);
2760 if (nonconstant_names.exists () && !early)
2762 struct loop *loop;
2763 predicate loop_iterations = true_predicate ();
2764 predicate loop_stride = true_predicate ();
2766 if (dump_file && (dump_flags & TDF_DETAILS))
2767 flow_loops_dump (dump_file, NULL, 0);
2768 scev_initialize ();
2769 FOR_EACH_LOOP (loop, 0)
2771 vec<edge> exits;
2772 edge ex;
2773 unsigned int j, i;
2774 struct tree_niter_desc niter_desc;
2775 basic_block *body = get_loop_body (loop);
2776 bb_predicate = *(struct predicate *) loop->header->aux;
2778 exits = get_loop_exit_edges (loop);
2779 FOR_EACH_VEC_ELT (exits, j, ex)
2780 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2781 && !is_gimple_min_invariant (niter_desc.niter))
2783 predicate will_be_nonconstant
2784 = will_be_nonconstant_expr_predicate (parms_info, info,
2785 niter_desc.niter,
2786 nonconstant_names);
2787 if (!true_predicate_p (&will_be_nonconstant))
2788 will_be_nonconstant = and_predicates (info->conds,
2789 &bb_predicate,
2790 &will_be_nonconstant);
2791 if (!true_predicate_p (&will_be_nonconstant)
2792 && !false_predicate_p (&will_be_nonconstant))
2793 /* This is slightly inprecise. We may want to represent each
2794 loop with independent predicate. */
2795 loop_iterations =
2796 and_predicates (info->conds, &loop_iterations,
2797 &will_be_nonconstant);
2799 exits.release ();
2801 for (i = 0; i < loop->num_nodes; i++)
2803 gimple_stmt_iterator gsi;
2804 bb_predicate = *(struct predicate *) body[i]->aux;
2805 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2806 gsi_next (&gsi))
2808 gimple stmt = gsi_stmt (gsi);
2809 affine_iv iv;
2810 ssa_op_iter iter;
2811 tree use;
2813 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2815 predicate will_be_nonconstant;
2817 if (!simple_iv
2818 (loop, loop_containing_stmt (stmt), use, &iv, true)
2819 || is_gimple_min_invariant (iv.step))
2820 continue;
2821 will_be_nonconstant
2822 = will_be_nonconstant_expr_predicate (parms_info, info,
2823 iv.step,
2824 nonconstant_names);
2825 if (!true_predicate_p (&will_be_nonconstant))
2826 will_be_nonconstant
2827 = and_predicates (info->conds,
2828 &bb_predicate,
2829 &will_be_nonconstant);
2830 if (!true_predicate_p (&will_be_nonconstant)
2831 && !false_predicate_p (&will_be_nonconstant))
2832 /* This is slightly inprecise. We may want to represent
2833 each loop with independent predicate. */
2834 loop_stride =
2835 and_predicates (info->conds, &loop_stride,
2836 &will_be_nonconstant);
2840 free (body);
2842 set_hint_predicate (&inline_summaries->get (node)->loop_iterations,
2843 loop_iterations);
2844 set_hint_predicate (&inline_summaries->get (node)->loop_stride, loop_stride);
2845 scev_finalize ();
2847 FOR_ALL_BB_FN (bb, my_function)
2849 edge e;
2850 edge_iterator ei;
2852 if (bb->aux)
2853 edge_predicate_pool.remove ((predicate *)bb->aux);
2854 bb->aux = NULL;
2855 FOR_EACH_EDGE (e, ei, bb->succs)
2857 if (e->aux)
2858 edge_predicate_pool.remove ((predicate *) e->aux);
2859 e->aux = NULL;
2862 inline_summaries->get (node)->self_time = time;
2863 inline_summaries->get (node)->self_size = size;
2864 nonconstant_names.release ();
2865 if (opt_for_fn (node->decl, optimize))
2867 if (!early)
2868 loop_optimizer_finalize ();
2869 else if (!ipa_edge_args_vector)
2870 ipa_free_all_node_params ();
2871 free_dominance_info (CDI_DOMINATORS);
2873 if (dump_file)
2875 fprintf (dump_file, "\n");
2876 dump_inline_summary (dump_file, node);
2881 /* Compute parameters of functions used by inliner.
2882 EARLY is true when we compute parameters for the early inliner */
2884 void
2885 compute_inline_parameters (struct cgraph_node *node, bool early)
2887 HOST_WIDE_INT self_stack_size;
2888 struct cgraph_edge *e;
2889 struct inline_summary *info;
2891 gcc_assert (!node->global.inlined_to);
2893 inline_summary_alloc ();
2895 info = inline_summaries->get (node);
2896 reset_inline_summary (node, info);
2898 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2899 Once this happen, we will need to more curefully predict call
2900 statement size. */
2901 if (node->thunk.thunk_p)
2903 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2904 struct predicate t = true_predicate ();
2906 info->inlinable = 0;
2907 node->callees->call_stmt_cannot_inline_p = true;
2908 node->local.can_change_signature = false;
2909 es->call_stmt_time = 1;
2910 es->call_stmt_size = 1;
2911 account_size_time (info, 0, 0, &t);
2912 return;
2915 /* Even is_gimple_min_invariant rely on current_function_decl. */
2916 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2918 /* Estimate the stack size for the function if we're optimizing. */
2919 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2920 info->estimated_self_stack_size = self_stack_size;
2921 info->estimated_stack_size = self_stack_size;
2922 info->stack_frame_offset = 0;
2924 /* Can this function be inlined at all? */
2925 if (!opt_for_fn (node->decl, optimize)
2926 && !lookup_attribute ("always_inline",
2927 DECL_ATTRIBUTES (node->decl)))
2928 info->inlinable = false;
2929 else
2930 info->inlinable = tree_inlinable_function_p (node->decl);
2932 info->contains_cilk_spawn = fn_contains_cilk_spawn_p (cfun);
2934 /* Type attributes can use parameter indices to describe them. */
2935 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2936 node->local.can_change_signature = false;
2937 else
2939 /* Otherwise, inlinable functions always can change signature. */
2940 if (info->inlinable)
2941 node->local.can_change_signature = true;
2942 else
2944 /* Functions calling builtin_apply can not change signature. */
2945 for (e = node->callees; e; e = e->next_callee)
2947 tree cdecl = e->callee->decl;
2948 if (DECL_BUILT_IN (cdecl)
2949 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2950 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2951 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2952 break;
2954 node->local.can_change_signature = !e;
2957 estimate_function_body_sizes (node, early);
2959 for (e = node->callees; e; e = e->next_callee)
2960 if (e->callee->comdat_local_p ())
2961 break;
2962 node->calls_comdat_local = (e != NULL);
2964 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2965 info->time = info->self_time;
2966 info->size = info->self_size;
2967 info->stack_frame_offset = 0;
2968 info->estimated_stack_size = info->estimated_self_stack_size;
2969 #ifdef ENABLE_CHECKING
2970 inline_update_overall_summary (node);
2971 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2972 #endif
2974 pop_cfun ();
2978 /* Compute parameters of functions used by inliner using
2979 current_function_decl. */
2981 static unsigned int
2982 compute_inline_parameters_for_current (void)
2984 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
2985 return 0;
2988 namespace {
2990 const pass_data pass_data_inline_parameters =
2992 GIMPLE_PASS, /* type */
2993 "inline_param", /* name */
2994 OPTGROUP_INLINE, /* optinfo_flags */
2995 TV_INLINE_PARAMETERS, /* tv_id */
2996 0, /* properties_required */
2997 0, /* properties_provided */
2998 0, /* properties_destroyed */
2999 0, /* todo_flags_start */
3000 0, /* todo_flags_finish */
3003 class pass_inline_parameters : public gimple_opt_pass
3005 public:
3006 pass_inline_parameters (gcc::context *ctxt)
3007 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
3010 /* opt_pass methods: */
3011 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
3012 virtual unsigned int execute (function *)
3014 return compute_inline_parameters_for_current ();
3017 }; // class pass_inline_parameters
3019 } // anon namespace
3021 gimple_opt_pass *
3022 make_pass_inline_parameters (gcc::context *ctxt)
3024 return new pass_inline_parameters (ctxt);
3028 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS,
3029 KNOWN_CONTEXTS and KNOWN_AGGS. */
3031 static bool
3032 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3033 int *size, int *time,
3034 vec<tree> known_vals,
3035 vec<ipa_polymorphic_call_context> known_contexts,
3036 vec<ipa_agg_jump_function_p> known_aggs)
3038 tree target;
3039 struct cgraph_node *callee;
3040 struct inline_summary *isummary;
3041 enum availability avail;
3042 bool speculative;
3044 if (!known_vals.exists () && !known_contexts.exists ())
3045 return false;
3046 if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3047 return false;
3049 target = ipa_get_indirect_edge_target (ie, known_vals, known_contexts,
3050 known_aggs, &speculative);
3051 if (!target || speculative)
3052 return false;
3054 /* Account for difference in cost between indirect and direct calls. */
3055 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3056 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3057 gcc_checking_assert (*time >= 0);
3058 gcc_checking_assert (*size >= 0);
3060 callee = cgraph_node::get (target);
3061 if (!callee || !callee->definition)
3062 return false;
3063 callee = callee->function_symbol (&avail);
3064 if (avail < AVAIL_AVAILABLE)
3065 return false;
3066 isummary = inline_summaries->get (callee);
3067 return isummary->inlinable;
3070 /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3071 handle edge E with probability PROB.
3072 Set HINTS if edge may be devirtualized.
3073 KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS describe context of the call
3074 site. */
3076 static inline void
3077 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3078 int *time,
3079 int prob,
3080 vec<tree> known_vals,
3081 vec<ipa_polymorphic_call_context> known_contexts,
3082 vec<ipa_agg_jump_function_p> known_aggs,
3083 inline_hints *hints)
3085 struct inline_edge_summary *es = inline_edge_summary (e);
3086 int call_size = es->call_stmt_size;
3087 int call_time = es->call_stmt_time;
3088 int cur_size;
3089 if (!e->callee
3090 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
3091 known_vals, known_contexts, known_aggs)
3092 && hints && e->maybe_hot_p ())
3093 *hints |= INLINE_HINT_indirect_call;
3094 cur_size = call_size * INLINE_SIZE_SCALE;
3095 *size += cur_size;
3096 if (min_size)
3097 *min_size += cur_size;
3098 *time += apply_probability ((gcov_type) call_time, prob)
3099 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
3100 if (*time > MAX_TIME * INLINE_TIME_SCALE)
3101 *time = MAX_TIME * INLINE_TIME_SCALE;
3106 /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3107 calls in NODE. POSSIBLE_TRUTHS, KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
3108 describe context of the call site. */
3110 static void
3111 estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3112 int *min_size, int *time,
3113 inline_hints *hints,
3114 clause_t possible_truths,
3115 vec<tree> known_vals,
3116 vec<ipa_polymorphic_call_context> known_contexts,
3117 vec<ipa_agg_jump_function_p> known_aggs)
3119 struct cgraph_edge *e;
3120 for (e = node->callees; e; e = e->next_callee)
3122 struct inline_edge_summary *es = inline_edge_summary (e);
3124 /* Do not care about zero sized builtins. */
3125 if (e->inline_failed && !es->call_stmt_size)
3127 gcc_checking_assert (!es->call_stmt_time);
3128 continue;
3130 if (!es->predicate
3131 || evaluate_predicate (es->predicate, possible_truths))
3133 if (e->inline_failed)
3135 /* Predicates of calls shall not use NOT_CHANGED codes,
3136 sowe do not need to compute probabilities. */
3137 estimate_edge_size_and_time (e, size,
3138 es->predicate ? NULL : min_size,
3139 time, REG_BR_PROB_BASE,
3140 known_vals, known_contexts,
3141 known_aggs, hints);
3143 else
3144 estimate_calls_size_and_time (e->callee, size, min_size, time,
3145 hints,
3146 possible_truths,
3147 known_vals, known_contexts,
3148 known_aggs);
3151 for (e = node->indirect_calls; e; e = e->next_callee)
3153 struct inline_edge_summary *es = inline_edge_summary (e);
3154 if (!es->predicate
3155 || evaluate_predicate (es->predicate, possible_truths))
3156 estimate_edge_size_and_time (e, size,
3157 es->predicate ? NULL : min_size,
3158 time, REG_BR_PROB_BASE,
3159 known_vals, known_contexts, known_aggs,
3160 hints);
3165 /* Estimate size and time needed to execute NODE assuming
3166 POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
3167 information about NODE's arguments. If non-NULL use also probability
3168 information present in INLINE_PARAM_SUMMARY vector.
3169 Additionally detemine hints determined by the context. Finally compute
3170 minimal size needed for the call that is independent on the call context and
3171 can be used for fast estimates. Return the values in RET_SIZE,
3172 RET_MIN_SIZE, RET_TIME and RET_HINTS. */
3174 static void
3175 estimate_node_size_and_time (struct cgraph_node *node,
3176 clause_t possible_truths,
3177 vec<tree> known_vals,
3178 vec<ipa_polymorphic_call_context> known_contexts,
3179 vec<ipa_agg_jump_function_p> known_aggs,
3180 int *ret_size, int *ret_min_size, int *ret_time,
3181 inline_hints *ret_hints,
3182 vec<inline_param_summary>
3183 inline_param_summary)
3185 struct inline_summary *info = inline_summaries->get (node);
3186 size_time_entry *e;
3187 int size = 0;
3188 int time = 0;
3189 int min_size = 0;
3190 inline_hints hints = 0;
3191 int i;
3193 if (dump_file && (dump_flags & TDF_DETAILS))
3195 bool found = false;
3196 fprintf (dump_file, " Estimating body: %s/%i\n"
3197 " Known to be false: ", node->name (),
3198 node->order);
3200 for (i = predicate_not_inlined_condition;
3201 i < (predicate_first_dynamic_condition
3202 + (int) vec_safe_length (info->conds)); i++)
3203 if (!(possible_truths & (1 << i)))
3205 if (found)
3206 fprintf (dump_file, ", ");
3207 found = true;
3208 dump_condition (dump_file, info->conds, i);
3212 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3213 if (evaluate_predicate (&e->predicate, possible_truths))
3215 size += e->size;
3216 gcc_checking_assert (e->time >= 0);
3217 gcc_checking_assert (time >= 0);
3218 if (!inline_param_summary.exists ())
3219 time += e->time;
3220 else
3222 int prob = predicate_probability (info->conds,
3223 &e->predicate,
3224 possible_truths,
3225 inline_param_summary);
3226 gcc_checking_assert (prob >= 0);
3227 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3228 time += apply_probability ((gcov_type) e->time, prob);
3230 if (time > MAX_TIME * INLINE_TIME_SCALE)
3231 time = MAX_TIME * INLINE_TIME_SCALE;
3232 gcc_checking_assert (time >= 0);
3235 gcc_checking_assert (true_predicate_p (&(*info->entry)[0].predicate));
3236 min_size = (*info->entry)[0].size;
3237 gcc_checking_assert (size >= 0);
3238 gcc_checking_assert (time >= 0);
3240 if (info->loop_iterations
3241 && !evaluate_predicate (info->loop_iterations, possible_truths))
3242 hints |= INLINE_HINT_loop_iterations;
3243 if (info->loop_stride
3244 && !evaluate_predicate (info->loop_stride, possible_truths))
3245 hints |= INLINE_HINT_loop_stride;
3246 if (info->array_index
3247 && !evaluate_predicate (info->array_index, possible_truths))
3248 hints |= INLINE_HINT_array_index;
3249 if (info->scc_no)
3250 hints |= INLINE_HINT_in_scc;
3251 if (DECL_DECLARED_INLINE_P (node->decl))
3252 hints |= INLINE_HINT_declared_inline;
3254 estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
3255 known_vals, known_contexts, known_aggs);
3256 gcc_checking_assert (size >= 0);
3257 gcc_checking_assert (time >= 0);
3258 time = RDIV (time, INLINE_TIME_SCALE);
3259 size = RDIV (size, INLINE_SIZE_SCALE);
3260 min_size = RDIV (min_size, INLINE_SIZE_SCALE);
3262 if (dump_file && (dump_flags & TDF_DETAILS))
3263 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3264 if (ret_time)
3265 *ret_time = time;
3266 if (ret_size)
3267 *ret_size = size;
3268 if (ret_min_size)
3269 *ret_min_size = min_size;
3270 if (ret_hints)
3271 *ret_hints = hints;
3272 return;
3276 /* Estimate size and time needed to execute callee of EDGE assuming that
3277 parameters known to be constant at caller of EDGE are propagated.
3278 KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
3279 and types for parameters. */
3281 void
3282 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3283 vec<tree> known_vals,
3284 vec<ipa_polymorphic_call_context>
3285 known_contexts,
3286 vec<ipa_agg_jump_function_p> known_aggs,
3287 int *ret_size, int *ret_time,
3288 inline_hints *hints)
3290 clause_t clause;
3292 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3293 known_aggs);
3294 estimate_node_size_and_time (node, clause, known_vals, known_contexts,
3295 known_aggs, ret_size, NULL, ret_time, hints, vNULL);
3298 /* Translate all conditions from callee representation into caller
3299 representation and symbolically evaluate predicate P into new predicate.
3301 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3302 is summary of function predicate P is from. OPERAND_MAP is array giving
3303 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3304 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3305 predicate under which callee is executed. OFFSET_MAP is an array of of
3306 offsets that need to be added to conditions, negative offset means that
3307 conditions relying on values passed by reference have to be discarded
3308 because they might not be preserved (and should be considered offset zero
3309 for other purposes). */
3311 static struct predicate
3312 remap_predicate (struct inline_summary *info,
3313 struct inline_summary *callee_info,
3314 struct predicate *p,
3315 vec<int> operand_map,
3316 vec<int> offset_map,
3317 clause_t possible_truths, struct predicate *toplev_predicate)
3319 int i;
3320 struct predicate out = true_predicate ();
3322 /* True predicate is easy. */
3323 if (true_predicate_p (p))
3324 return *toplev_predicate;
3325 for (i = 0; p->clause[i]; i++)
3327 clause_t clause = p->clause[i];
3328 int cond;
3329 struct predicate clause_predicate = false_predicate ();
3331 gcc_assert (i < MAX_CLAUSES);
3333 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3334 /* Do we have condition we can't disprove? */
3335 if (clause & possible_truths & (1 << cond))
3337 struct predicate cond_predicate;
3338 /* Work out if the condition can translate to predicate in the
3339 inlined function. */
3340 if (cond >= predicate_first_dynamic_condition)
3342 struct condition *c;
3344 c = &(*callee_info->conds)[cond
3346 predicate_first_dynamic_condition];
3347 /* See if we can remap condition operand to caller's operand.
3348 Otherwise give up. */
3349 if (!operand_map.exists ()
3350 || (int) operand_map.length () <= c->operand_num
3351 || operand_map[c->operand_num] == -1
3352 /* TODO: For non-aggregate conditions, adding an offset is
3353 basically an arithmetic jump function processing which
3354 we should support in future. */
3355 || ((!c->agg_contents || !c->by_ref)
3356 && offset_map[c->operand_num] > 0)
3357 || (c->agg_contents && c->by_ref
3358 && offset_map[c->operand_num] < 0))
3359 cond_predicate = true_predicate ();
3360 else
3362 struct agg_position_info ap;
3363 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3364 if (offset_delta < 0)
3366 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3367 offset_delta = 0;
3369 gcc_assert (!c->agg_contents
3370 || c->by_ref || offset_delta == 0);
3371 ap.offset = c->offset + offset_delta;
3372 ap.agg_contents = c->agg_contents;
3373 ap.by_ref = c->by_ref;
3374 cond_predicate = add_condition (info,
3375 operand_map[c->operand_num],
3376 &ap, c->code, c->val);
3379 /* Fixed conditions remains same, construct single
3380 condition predicate. */
3381 else
3383 cond_predicate.clause[0] = 1 << cond;
3384 cond_predicate.clause[1] = 0;
3386 clause_predicate = or_predicates (info->conds, &clause_predicate,
3387 &cond_predicate);
3389 out = and_predicates (info->conds, &out, &clause_predicate);
3391 return and_predicates (info->conds, &out, toplev_predicate);
3395 /* Update summary information of inline clones after inlining.
3396 Compute peak stack usage. */
3398 static void
3399 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3401 struct cgraph_edge *e;
3402 struct inline_summary *callee_info = inline_summaries->get (node);
3403 struct inline_summary *caller_info = inline_summaries->get (node->callers->caller);
3404 HOST_WIDE_INT peak;
3406 callee_info->stack_frame_offset
3407 = caller_info->stack_frame_offset
3408 + caller_info->estimated_self_stack_size;
3409 peak = callee_info->stack_frame_offset
3410 + callee_info->estimated_self_stack_size;
3411 if (inline_summaries->get (node->global.inlined_to)->estimated_stack_size < peak)
3412 inline_summaries->get (node->global.inlined_to)->estimated_stack_size = peak;
3413 ipa_propagate_frequency (node);
3414 for (e = node->callees; e; e = e->next_callee)
3416 if (!e->inline_failed)
3417 inline_update_callee_summaries (e->callee, depth);
3418 inline_edge_summary (e)->loop_depth += depth;
3420 for (e = node->indirect_calls; e; e = e->next_callee)
3421 inline_edge_summary (e)->loop_depth += depth;
3424 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3425 When functoin A is inlined in B and A calls C with parameter that
3426 changes with probability PROB1 and C is known to be passthroug
3427 of argument if B that change with probability PROB2, the probability
3428 of change is now PROB1*PROB2. */
3430 static void
3431 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3432 struct cgraph_edge *edge)
3434 if (ipa_node_params_sum)
3436 int i;
3437 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3438 struct inline_edge_summary *es = inline_edge_summary (edge);
3439 struct inline_edge_summary *inlined_es
3440 = inline_edge_summary (inlined_edge);
3442 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3444 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3445 if (jfunc->type == IPA_JF_PASS_THROUGH
3446 && (ipa_get_jf_pass_through_formal_id (jfunc)
3447 < (int) inlined_es->param.length ()))
3449 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3450 int prob1 = es->param[i].change_prob;
3451 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3452 int prob = combine_probabilities (prob1, prob2);
3454 if (prob1 && prob2 && !prob)
3455 prob = 1;
3457 es->param[i].change_prob = prob;
3463 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3465 Remap predicates of callees of NODE. Rest of arguments match
3466 remap_predicate.
3468 Also update change probabilities. */
3470 static void
3471 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3472 struct cgraph_node *node,
3473 struct inline_summary *info,
3474 struct inline_summary *callee_info,
3475 vec<int> operand_map,
3476 vec<int> offset_map,
3477 clause_t possible_truths,
3478 struct predicate *toplev_predicate)
3480 struct cgraph_edge *e, *next;
3481 for (e = node->callees; e; e = next)
3483 struct inline_edge_summary *es = inline_edge_summary (e);
3484 struct predicate p;
3485 next = e->next_callee;
3487 if (e->inline_failed)
3489 remap_edge_change_prob (inlined_edge, e);
3491 if (es->predicate)
3493 p = remap_predicate (info, callee_info,
3494 es->predicate, operand_map, offset_map,
3495 possible_truths, toplev_predicate);
3496 edge_set_predicate (e, &p);
3498 else
3499 edge_set_predicate (e, toplev_predicate);
3501 else
3502 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3503 operand_map, offset_map, possible_truths,
3504 toplev_predicate);
3506 for (e = node->indirect_calls; e; e = next)
3508 struct inline_edge_summary *es = inline_edge_summary (e);
3509 struct predicate p;
3510 next = e->next_callee;
3512 remap_edge_change_prob (inlined_edge, e);
3513 if (es->predicate)
3515 p = remap_predicate (info, callee_info,
3516 es->predicate, operand_map, offset_map,
3517 possible_truths, toplev_predicate);
3518 edge_set_predicate (e, &p);
3520 else
3521 edge_set_predicate (e, toplev_predicate);
3525 /* Same as remap_predicate, but set result into hint *HINT. */
3527 static void
3528 remap_hint_predicate (struct inline_summary *info,
3529 struct inline_summary *callee_info,
3530 struct predicate **hint,
3531 vec<int> operand_map,
3532 vec<int> offset_map,
3533 clause_t possible_truths,
3534 struct predicate *toplev_predicate)
3536 predicate p;
3538 if (!*hint)
3539 return;
3540 p = remap_predicate (info, callee_info,
3541 *hint,
3542 operand_map, offset_map,
3543 possible_truths, toplev_predicate);
3544 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3546 if (!*hint)
3547 set_hint_predicate (hint, p);
3548 else
3549 **hint = and_predicates (info->conds, *hint, &p);
3553 /* We inlined EDGE. Update summary of the function we inlined into. */
3555 void
3556 inline_merge_summary (struct cgraph_edge *edge)
3558 struct inline_summary *callee_info = inline_summaries->get (edge->callee);
3559 struct cgraph_node *to = (edge->caller->global.inlined_to
3560 ? edge->caller->global.inlined_to : edge->caller);
3561 struct inline_summary *info = inline_summaries->get (to);
3562 clause_t clause = 0; /* not_inline is known to be false. */
3563 size_time_entry *e;
3564 vec<int> operand_map = vNULL;
3565 vec<int> offset_map = vNULL;
3566 int i;
3567 struct predicate toplev_predicate;
3568 struct predicate true_p = true_predicate ();
3569 struct inline_edge_summary *es = inline_edge_summary (edge);
3571 if (es->predicate)
3572 toplev_predicate = *es->predicate;
3573 else
3574 toplev_predicate = true_predicate ();
3576 if (callee_info->conds)
3577 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3578 if (ipa_node_params_sum && callee_info->conds)
3580 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3581 int count = ipa_get_cs_argument_count (args);
3582 int i;
3584 if (count)
3586 operand_map.safe_grow_cleared (count);
3587 offset_map.safe_grow_cleared (count);
3589 for (i = 0; i < count; i++)
3591 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3592 int map = -1;
3594 /* TODO: handle non-NOPs when merging. */
3595 if (jfunc->type == IPA_JF_PASS_THROUGH)
3597 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3598 map = ipa_get_jf_pass_through_formal_id (jfunc);
3599 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3600 offset_map[i] = -1;
3602 else if (jfunc->type == IPA_JF_ANCESTOR)
3604 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3605 if (offset >= 0 && offset < INT_MAX)
3607 map = ipa_get_jf_ancestor_formal_id (jfunc);
3608 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3609 offset = -1;
3610 offset_map[i] = offset;
3613 operand_map[i] = map;
3614 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3617 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3619 struct predicate p = remap_predicate (info, callee_info,
3620 &e->predicate, operand_map,
3621 offset_map, clause,
3622 &toplev_predicate);
3623 if (!false_predicate_p (&p))
3625 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3626 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3627 int prob = predicate_probability (callee_info->conds,
3628 &e->predicate,
3629 clause, es->param);
3630 add_time = apply_probability ((gcov_type) add_time, prob);
3631 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3632 add_time = MAX_TIME * INLINE_TIME_SCALE;
3633 if (prob != REG_BR_PROB_BASE
3634 && dump_file && (dump_flags & TDF_DETAILS))
3636 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3637 (double) prob / REG_BR_PROB_BASE);
3639 account_size_time (info, e->size, add_time, &p);
3642 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3643 offset_map, clause, &toplev_predicate);
3644 remap_hint_predicate (info, callee_info,
3645 &callee_info->loop_iterations,
3646 operand_map, offset_map, clause, &toplev_predicate);
3647 remap_hint_predicate (info, callee_info,
3648 &callee_info->loop_stride,
3649 operand_map, offset_map, clause, &toplev_predicate);
3650 remap_hint_predicate (info, callee_info,
3651 &callee_info->array_index,
3652 operand_map, offset_map, clause, &toplev_predicate);
3654 inline_update_callee_summaries (edge->callee,
3655 inline_edge_summary (edge)->loop_depth);
3657 /* We do not maintain predicates of inlined edges, free it. */
3658 edge_set_predicate (edge, &true_p);
3659 /* Similarly remove param summaries. */
3660 es->param.release ();
3661 operand_map.release ();
3662 offset_map.release ();
3665 /* For performance reasons inline_merge_summary is not updating overall size
3666 and time. Recompute it. */
3668 void
3669 inline_update_overall_summary (struct cgraph_node *node)
3671 struct inline_summary *info = inline_summaries->get (node);
3672 size_time_entry *e;
3673 int i;
3675 info->size = 0;
3676 info->time = 0;
3677 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3679 info->size += e->size, info->time += e->time;
3680 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3681 info->time = MAX_TIME * INLINE_TIME_SCALE;
3683 estimate_calls_size_and_time (node, &info->size, &info->min_size,
3684 &info->time, NULL,
3685 ~(clause_t) (1 << predicate_false_condition),
3686 vNULL, vNULL, vNULL);
3687 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3688 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3691 /* Return hints derrived from EDGE. */
3693 simple_edge_hints (struct cgraph_edge *edge)
3695 int hints = 0;
3696 struct cgraph_node *to = (edge->caller->global.inlined_to
3697 ? edge->caller->global.inlined_to : edge->caller);
3698 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
3699 if (inline_summaries->get (to)->scc_no
3700 && inline_summaries->get (to)->scc_no
3701 == inline_summaries->get (callee)->scc_no
3702 && !edge->recursive_p ())
3703 hints |= INLINE_HINT_same_scc;
3705 if (callee->lto_file_data && edge->caller->lto_file_data
3706 && edge->caller->lto_file_data != callee->lto_file_data
3707 && !callee->merged)
3708 hints |= INLINE_HINT_cross_module;
3710 return hints;
3713 /* Estimate the time cost for the caller when inlining EDGE.
3714 Only to be called via estimate_edge_time, that handles the
3715 caching mechanism.
3717 When caching, also update the cache entry. Compute both time and
3718 size, since we always need both metrics eventually. */
3721 do_estimate_edge_time (struct cgraph_edge *edge)
3723 int time;
3724 int size;
3725 inline_hints hints;
3726 struct cgraph_node *callee;
3727 clause_t clause;
3728 vec<tree> known_vals;
3729 vec<ipa_polymorphic_call_context> known_contexts;
3730 vec<ipa_agg_jump_function_p> known_aggs;
3731 struct inline_edge_summary *es = inline_edge_summary (edge);
3732 int min_size;
3734 callee = edge->callee->ultimate_alias_target ();
3736 gcc_checking_assert (edge->inline_failed);
3737 evaluate_properties_for_edge (edge, true,
3738 &clause, &known_vals, &known_contexts,
3739 &known_aggs);
3740 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3741 known_aggs, &size, &min_size, &time, &hints, es->param);
3743 /* When we have profile feedback, we can quite safely identify hot
3744 edges and for those we disable size limits. Don't do that when
3745 probability that caller will call the callee is low however, since it
3746 may hurt optimization of the caller's hot path. */
3747 if (edge->count && edge->maybe_hot_p ()
3748 && (edge->count * 2
3749 > (edge->caller->global.inlined_to
3750 ? edge->caller->global.inlined_to->count : edge->caller->count)))
3751 hints |= INLINE_HINT_known_hot;
3753 known_vals.release ();
3754 known_contexts.release ();
3755 known_aggs.release ();
3756 gcc_checking_assert (size >= 0);
3757 gcc_checking_assert (time >= 0);
3759 /* When caching, update the cache entry. */
3760 if (edge_growth_cache.exists ())
3762 inline_summaries->get (edge->callee)->min_size = min_size;
3763 if ((int) edge_growth_cache.length () <= edge->uid)
3764 edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
3765 edge_growth_cache[edge->uid].time = time + (time >= 0);
3767 edge_growth_cache[edge->uid].size = size + (size >= 0);
3768 hints |= simple_edge_hints (edge);
3769 edge_growth_cache[edge->uid].hints = hints + 1;
3771 return time;
3775 /* Return estimated callee growth after inlining EDGE.
3776 Only to be called via estimate_edge_size. */
3779 do_estimate_edge_size (struct cgraph_edge *edge)
3781 int size;
3782 struct cgraph_node *callee;
3783 clause_t clause;
3784 vec<tree> known_vals;
3785 vec<ipa_polymorphic_call_context> known_contexts;
3786 vec<ipa_agg_jump_function_p> known_aggs;
3788 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3790 if (edge_growth_cache.exists ())
3792 do_estimate_edge_time (edge);
3793 size = edge_growth_cache[edge->uid].size;
3794 gcc_checking_assert (size);
3795 return size - (size > 0);
3798 callee = edge->callee->ultimate_alias_target ();
3800 /* Early inliner runs without caching, go ahead and do the dirty work. */
3801 gcc_checking_assert (edge->inline_failed);
3802 evaluate_properties_for_edge (edge, true,
3803 &clause, &known_vals, &known_contexts,
3804 &known_aggs);
3805 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3806 known_aggs, &size, NULL, NULL, NULL, vNULL);
3807 known_vals.release ();
3808 known_contexts.release ();
3809 known_aggs.release ();
3810 return size;
3814 /* Estimate the growth of the caller when inlining EDGE.
3815 Only to be called via estimate_edge_size. */
3817 inline_hints
3818 do_estimate_edge_hints (struct cgraph_edge *edge)
3820 inline_hints hints;
3821 struct cgraph_node *callee;
3822 clause_t clause;
3823 vec<tree> known_vals;
3824 vec<ipa_polymorphic_call_context> known_contexts;
3825 vec<ipa_agg_jump_function_p> known_aggs;
3827 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3829 if (edge_growth_cache.exists ())
3831 do_estimate_edge_time (edge);
3832 hints = edge_growth_cache[edge->uid].hints;
3833 gcc_checking_assert (hints);
3834 return hints - 1;
3837 callee = edge->callee->ultimate_alias_target ();
3839 /* Early inliner runs without caching, go ahead and do the dirty work. */
3840 gcc_checking_assert (edge->inline_failed);
3841 evaluate_properties_for_edge (edge, true,
3842 &clause, &known_vals, &known_contexts,
3843 &known_aggs);
3844 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3845 known_aggs, NULL, NULL, NULL, &hints, vNULL);
3846 known_vals.release ();
3847 known_contexts.release ();
3848 known_aggs.release ();
3849 hints |= simple_edge_hints (edge);
3850 return hints;
3854 /* Estimate self time of the function NODE after inlining EDGE. */
3857 estimate_time_after_inlining (struct cgraph_node *node,
3858 struct cgraph_edge *edge)
3860 struct inline_edge_summary *es = inline_edge_summary (edge);
3861 if (!es->predicate || !false_predicate_p (es->predicate))
3863 gcov_type time =
3864 inline_summaries->get (node)->time + estimate_edge_time (edge);
3865 if (time < 0)
3866 time = 0;
3867 if (time > MAX_TIME)
3868 time = MAX_TIME;
3869 return time;
3871 return inline_summaries->get (node)->time;
3875 /* Estimate the size of NODE after inlining EDGE which should be an
3876 edge to either NODE or a call inlined into NODE. */
3879 estimate_size_after_inlining (struct cgraph_node *node,
3880 struct cgraph_edge *edge)
3882 struct inline_edge_summary *es = inline_edge_summary (edge);
3883 if (!es->predicate || !false_predicate_p (es->predicate))
3885 int size = inline_summaries->get (node)->size + estimate_edge_growth (edge);
3886 gcc_assert (size >= 0);
3887 return size;
3889 return inline_summaries->get (node)->size;
3893 struct growth_data
3895 struct cgraph_node *node;
3896 bool self_recursive;
3897 bool uninlinable;
3898 int growth;
3902 /* Worker for do_estimate_growth. Collect growth for all callers. */
3904 static bool
3905 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3907 struct cgraph_edge *e;
3908 struct growth_data *d = (struct growth_data *) data;
3910 for (e = node->callers; e; e = e->next_caller)
3912 gcc_checking_assert (e->inline_failed);
3914 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
3916 d->uninlinable = true;
3917 continue;
3920 if (e->recursive_p ())
3922 d->self_recursive = true;
3923 continue;
3925 d->growth += estimate_edge_growth (e);
3927 return false;
3931 /* Estimate the growth caused by inlining NODE into all callees. */
3934 estimate_growth (struct cgraph_node *node)
3936 struct growth_data d = { node, false, false, 0 };
3937 struct inline_summary *info = inline_summaries->get (node);
3939 node->call_for_symbol_and_aliases (do_estimate_growth_1, &d, true);
3941 /* For self recursive functions the growth estimation really should be
3942 infinity. We don't want to return very large values because the growth
3943 plays various roles in badness computation fractions. Be sure to not
3944 return zero or negative growths. */
3945 if (d.self_recursive)
3946 d.growth = d.growth < info->size ? info->size : d.growth;
3947 else if (DECL_EXTERNAL (node->decl) || d.uninlinable)
3949 else
3951 if (node->will_be_removed_from_program_if_no_direct_calls_p ())
3952 d.growth -= info->size;
3953 /* COMDAT functions are very often not shared across multiple units
3954 since they come from various template instantiations.
3955 Take this into account. */
3956 else if (DECL_COMDAT (node->decl)
3957 && node->can_remove_if_no_direct_calls_p ())
3958 d.growth -= (info->size
3959 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3960 + 50) / 100;
3963 return d.growth;
3966 /* Verify if there are fewer than MAX_CALLERS. */
3968 static bool
3969 check_callers (cgraph_node *node, int *max_callers)
3971 ipa_ref *ref;
3973 if (!node->can_remove_if_no_direct_calls_and_refs_p ())
3974 return true;
3976 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3978 (*max_callers)--;
3979 if (!*max_callers
3980 || cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
3981 return true;
3984 FOR_EACH_ALIAS (node, ref)
3985 if (check_callers (dyn_cast <cgraph_node *> (ref->referring), max_callers))
3986 return true;
3988 return false;
3992 /* Make cheap estimation if growth of NODE is likely positive knowing
3993 EDGE_GROWTH of one particular edge.
3994 We assume that most of other edges will have similar growth
3995 and skip computation if there are too many callers. */
3997 bool
3998 growth_likely_positive (struct cgraph_node *node,
3999 int edge_growth)
4001 int max_callers;
4002 struct cgraph_edge *e;
4003 gcc_checking_assert (edge_growth > 0);
4005 /* First quickly check if NODE is removable at all. */
4006 if (DECL_EXTERNAL (node->decl))
4007 return true;
4008 if (!node->can_remove_if_no_direct_calls_and_refs_p ()
4009 || node->address_taken)
4010 return true;
4012 max_callers = inline_summaries->get (node)->size * 4 / edge_growth + 2;
4014 for (e = node->callers; e; e = e->next_caller)
4016 max_callers--;
4017 if (!max_callers
4018 || cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
4019 return true;
4022 ipa_ref *ref;
4023 FOR_EACH_ALIAS (node, ref)
4024 if (check_callers (dyn_cast <cgraph_node *> (ref->referring), &max_callers))
4025 return true;
4027 /* Unlike for functions called once, we play unsafe with
4028 COMDATs. We can allow that since we know functions
4029 in consideration are small (and thus risk is small) and
4030 moreover grow estimates already accounts that COMDAT
4031 functions may or may not disappear when eliminated from
4032 current unit. With good probability making aggressive
4033 choice in all units is going to make overall program
4034 smaller. */
4035 if (DECL_COMDAT (node->decl))
4037 if (!node->can_remove_if_no_direct_calls_p ())
4038 return true;
4040 else if (!node->will_be_removed_from_program_if_no_direct_calls_p ())
4041 return true;
4043 return estimate_growth (node) > 0;
4047 /* This function performs intraprocedural analysis in NODE that is required to
4048 inline indirect calls. */
4050 static void
4051 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4053 ipa_analyze_node (node);
4054 if (dump_file && (dump_flags & TDF_DETAILS))
4056 ipa_print_node_params (dump_file, node);
4057 ipa_print_node_jump_functions (dump_file, node);
4062 /* Note function body size. */
4064 void
4065 inline_analyze_function (struct cgraph_node *node)
4067 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4069 if (dump_file)
4070 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
4071 node->name (), node->order);
4072 if (opt_for_fn (node->decl, optimize) && !node->thunk.thunk_p)
4073 inline_indirect_intraprocedural_analysis (node);
4074 compute_inline_parameters (node, false);
4075 if (!optimize)
4077 struct cgraph_edge *e;
4078 for (e = node->callees; e; e = e->next_callee)
4080 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
4081 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4082 e->call_stmt_cannot_inline_p = true;
4084 for (e = node->indirect_calls; e; e = e->next_callee)
4086 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
4087 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4088 e->call_stmt_cannot_inline_p = true;
4092 pop_cfun ();
4096 /* Called when new function is inserted to callgraph late. */
4098 void
4099 inline_summary_t::insert (struct cgraph_node *node, inline_summary *)
4101 inline_analyze_function (node);
4104 /* Note function body size. */
4106 void
4107 inline_generate_summary (void)
4109 struct cgraph_node *node;
4111 /* When not optimizing, do not bother to analyze. Inlining is still done
4112 because edge redirection needs to happen there. */
4113 if (!optimize && !flag_generate_lto && !flag_generate_offload && !flag_wpa)
4114 return;
4116 if (!inline_summaries)
4117 inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
4119 inline_summaries->enable_insertion_hook ();
4121 ipa_register_cgraph_hooks ();
4122 inline_free_summary ();
4124 FOR_EACH_DEFINED_FUNCTION (node)
4125 if (!node->alias)
4126 inline_analyze_function (node);
4130 /* Read predicate from IB. */
4132 static struct predicate
4133 read_predicate (struct lto_input_block *ib)
4135 struct predicate out;
4136 clause_t clause;
4137 int k = 0;
4141 gcc_assert (k <= MAX_CLAUSES);
4142 clause = out.clause[k++] = streamer_read_uhwi (ib);
4144 while (clause);
4146 /* Zero-initialize the remaining clauses in OUT. */
4147 while (k <= MAX_CLAUSES)
4148 out.clause[k++] = 0;
4150 return out;
4154 /* Write inline summary for edge E to OB. */
4156 static void
4157 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
4159 struct inline_edge_summary *es = inline_edge_summary (e);
4160 struct predicate p;
4161 int length, i;
4163 es->call_stmt_size = streamer_read_uhwi (ib);
4164 es->call_stmt_time = streamer_read_uhwi (ib);
4165 es->loop_depth = streamer_read_uhwi (ib);
4166 p = read_predicate (ib);
4167 edge_set_predicate (e, &p);
4168 length = streamer_read_uhwi (ib);
4169 if (length)
4171 es->param.safe_grow_cleared (length);
4172 for (i = 0; i < length; i++)
4173 es->param[i].change_prob = streamer_read_uhwi (ib);
4178 /* Stream in inline summaries from the section. */
4180 static void
4181 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4182 size_t len)
4184 const struct lto_function_header *header =
4185 (const struct lto_function_header *) data;
4186 const int cfg_offset = sizeof (struct lto_function_header);
4187 const int main_offset = cfg_offset + header->cfg_size;
4188 const int string_offset = main_offset + header->main_size;
4189 struct data_in *data_in;
4190 unsigned int i, count2, j;
4191 unsigned int f_count;
4193 lto_input_block ib ((const char *) data + main_offset, header->main_size,
4194 file_data->mode_table);
4196 data_in =
4197 lto_data_in_create (file_data, (const char *) data + string_offset,
4198 header->string_size, vNULL);
4199 f_count = streamer_read_uhwi (&ib);
4200 for (i = 0; i < f_count; i++)
4202 unsigned int index;
4203 struct cgraph_node *node;
4204 struct inline_summary *info;
4205 lto_symtab_encoder_t encoder;
4206 struct bitpack_d bp;
4207 struct cgraph_edge *e;
4208 predicate p;
4210 index = streamer_read_uhwi (&ib);
4211 encoder = file_data->symtab_node_encoder;
4212 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4213 index));
4214 info = inline_summaries->get (node);
4216 info->estimated_stack_size
4217 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
4218 info->size = info->self_size = streamer_read_uhwi (&ib);
4219 info->time = info->self_time = streamer_read_uhwi (&ib);
4221 bp = streamer_read_bitpack (&ib);
4222 info->inlinable = bp_unpack_value (&bp, 1);
4223 info->contains_cilk_spawn = bp_unpack_value (&bp, 1);
4225 count2 = streamer_read_uhwi (&ib);
4226 gcc_assert (!info->conds);
4227 for (j = 0; j < count2; j++)
4229 struct condition c;
4230 c.operand_num = streamer_read_uhwi (&ib);
4231 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4232 c.val = stream_read_tree (&ib, data_in);
4233 bp = streamer_read_bitpack (&ib);
4234 c.agg_contents = bp_unpack_value (&bp, 1);
4235 c.by_ref = bp_unpack_value (&bp, 1);
4236 if (c.agg_contents)
4237 c.offset = streamer_read_uhwi (&ib);
4238 vec_safe_push (info->conds, c);
4240 count2 = streamer_read_uhwi (&ib);
4241 gcc_assert (!info->entry);
4242 for (j = 0; j < count2; j++)
4244 struct size_time_entry e;
4246 e.size = streamer_read_uhwi (&ib);
4247 e.time = streamer_read_uhwi (&ib);
4248 e.predicate = read_predicate (&ib);
4250 vec_safe_push (info->entry, e);
4253 p = read_predicate (&ib);
4254 set_hint_predicate (&info->loop_iterations, p);
4255 p = read_predicate (&ib);
4256 set_hint_predicate (&info->loop_stride, p);
4257 p = read_predicate (&ib);
4258 set_hint_predicate (&info->array_index, p);
4259 for (e = node->callees; e; e = e->next_callee)
4260 read_inline_edge_summary (&ib, e);
4261 for (e = node->indirect_calls; e; e = e->next_callee)
4262 read_inline_edge_summary (&ib, e);
4265 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
4266 len);
4267 lto_data_in_delete (data_in);
4271 /* Read inline summary. Jump functions are shared among ipa-cp
4272 and inliner, so when ipa-cp is active, we don't need to write them
4273 twice. */
4275 void
4276 inline_read_summary (void)
4278 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4279 struct lto_file_decl_data *file_data;
4280 unsigned int j = 0;
4282 inline_summary_alloc ();
4284 while ((file_data = file_data_vec[j++]))
4286 size_t len;
4287 const char *data = lto_get_section_data (file_data,
4288 LTO_section_inline_summary,
4289 NULL, &len);
4290 if (data)
4291 inline_read_section (file_data, data, len);
4292 else
4293 /* Fatal error here. We do not want to support compiling ltrans units
4294 with different version of compiler or different flags than the WPA
4295 unit, so this should never happen. */
4296 fatal_error (input_location,
4297 "ipa inline summary is missing in input file");
4299 if (optimize)
4301 ipa_register_cgraph_hooks ();
4302 if (!flag_ipa_cp)
4303 ipa_prop_read_jump_functions ();
4306 gcc_assert (inline_summaries);
4307 inline_summaries->enable_insertion_hook ();
4311 /* Write predicate P to OB. */
4313 static void
4314 write_predicate (struct output_block *ob, struct predicate *p)
4316 int j;
4317 if (p)
4318 for (j = 0; p->clause[j]; j++)
4320 gcc_assert (j < MAX_CLAUSES);
4321 streamer_write_uhwi (ob, p->clause[j]);
4323 streamer_write_uhwi (ob, 0);
4327 /* Write inline summary for edge E to OB. */
4329 static void
4330 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4332 struct inline_edge_summary *es = inline_edge_summary (e);
4333 int i;
4335 streamer_write_uhwi (ob, es->call_stmt_size);
4336 streamer_write_uhwi (ob, es->call_stmt_time);
4337 streamer_write_uhwi (ob, es->loop_depth);
4338 write_predicate (ob, es->predicate);
4339 streamer_write_uhwi (ob, es->param.length ());
4340 for (i = 0; i < (int) es->param.length (); i++)
4341 streamer_write_uhwi (ob, es->param[i].change_prob);
4345 /* Write inline summary for node in SET.
4346 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4347 active, we don't need to write them twice. */
4349 void
4350 inline_write_summary (void)
4352 struct cgraph_node *node;
4353 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4354 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4355 unsigned int count = 0;
4356 int i;
4358 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4360 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4361 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
4362 if (cnode && cnode->definition && !cnode->alias)
4363 count++;
4365 streamer_write_uhwi (ob, count);
4367 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4369 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4370 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
4371 if (cnode && (node = cnode)->definition && !node->alias)
4373 struct inline_summary *info = inline_summaries->get (node);
4374 struct bitpack_d bp;
4375 struct cgraph_edge *edge;
4376 int i;
4377 size_time_entry *e;
4378 struct condition *c;
4380 streamer_write_uhwi (ob,
4381 lto_symtab_encoder_encode (encoder,
4383 node));
4384 streamer_write_hwi (ob, info->estimated_self_stack_size);
4385 streamer_write_hwi (ob, info->self_size);
4386 streamer_write_hwi (ob, info->self_time);
4387 bp = bitpack_create (ob->main_stream);
4388 bp_pack_value (&bp, info->inlinable, 1);
4389 bp_pack_value (&bp, info->contains_cilk_spawn, 1);
4390 streamer_write_bitpack (&bp);
4391 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4392 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4394 streamer_write_uhwi (ob, c->operand_num);
4395 streamer_write_uhwi (ob, c->code);
4396 stream_write_tree (ob, c->val, true);
4397 bp = bitpack_create (ob->main_stream);
4398 bp_pack_value (&bp, c->agg_contents, 1);
4399 bp_pack_value (&bp, c->by_ref, 1);
4400 streamer_write_bitpack (&bp);
4401 if (c->agg_contents)
4402 streamer_write_uhwi (ob, c->offset);
4404 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4405 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4407 streamer_write_uhwi (ob, e->size);
4408 streamer_write_uhwi (ob, e->time);
4409 write_predicate (ob, &e->predicate);
4411 write_predicate (ob, info->loop_iterations);
4412 write_predicate (ob, info->loop_stride);
4413 write_predicate (ob, info->array_index);
4414 for (edge = node->callees; edge; edge = edge->next_callee)
4415 write_inline_edge_summary (ob, edge);
4416 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4417 write_inline_edge_summary (ob, edge);
4420 streamer_write_char_stream (ob->main_stream, 0);
4421 produce_asm (ob, NULL);
4422 destroy_output_block (ob);
4424 if (optimize && !flag_ipa_cp)
4425 ipa_prop_write_jump_functions ();
4429 /* Release inline summary. */
4431 void
4432 inline_free_summary (void)
4434 struct cgraph_node *node;
4435 if (edge_removal_hook_holder)
4436 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4437 edge_removal_hook_holder = NULL;
4438 if (edge_duplication_hook_holder)
4439 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4440 edge_duplication_hook_holder = NULL;
4441 if (!inline_edge_summary_vec.exists ())
4442 return;
4443 FOR_EACH_DEFINED_FUNCTION (node)
4444 if (!node->alias)
4445 reset_inline_summary (node, inline_summaries->get (node));
4446 inline_summaries->release ();
4447 inline_summaries = NULL;
4448 inline_edge_summary_vec.release ();
4449 edge_predicate_pool.release ();