PR middle-end/66633
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob18bc68a41b38bcf3aae679eb8a824c3466e0bb7d
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 "cgraph.h"
106 #include "alloc-pool.h"
107 #include "symbol-summary.h"
108 #include "ipa-prop.h"
109 #include "lto-streamer.h"
110 #include "data-streamer.h"
111 #include "tree-streamer.h"
112 #include "ipa-inline.h"
113 #include "cfgloop.h"
114 #include "tree-scalar-evolution.h"
115 #include "ipa-utils.h"
116 #include "cilk.h"
117 #include "cfgexpand.h"
119 /* Estimate runtime of function can easilly run into huge numbers with many
120 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
121 integer. For anything larger we use gcov_type. */
122 #define MAX_TIME 500000
124 /* Number of bits in integer, but we really want to be stable across different
125 hosts. */
126 #define NUM_CONDITIONS 32
128 enum predicate_conditions
130 predicate_false_condition = 0,
131 predicate_not_inlined_condition = 1,
132 predicate_first_dynamic_condition = 2
135 /* Special condition code we use to represent test that operand is compile time
136 constant. */
137 #define IS_NOT_CONSTANT ERROR_MARK
138 /* Special condition code we use to represent test that operand is not changed
139 across invocation of the function. When operand IS_NOT_CONSTANT it is always
140 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
141 of executions even when they are not compile time constants. */
142 #define CHANGED IDENTIFIER_NODE
144 /* Holders of ipa cgraph hooks: */
145 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
146 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
147 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
148 static void inline_edge_duplication_hook (struct cgraph_edge *,
149 struct cgraph_edge *, void *);
151 /* VECtor holding inline summaries.
152 In GGC memory because conditions might point to constant trees. */
153 function_summary <inline_summary *> *inline_summaries;
154 vec<inline_edge_summary_t> inline_edge_summary_vec;
156 /* Cached node/edge growths. */
157 vec<edge_growth_cache_entry> edge_growth_cache;
159 /* Edge predicates goes here. */
160 static pool_allocator<predicate> edge_predicate_pool ("edge predicates", 10);
162 /* Return true predicate (tautology).
163 We represent it by empty list of clauses. */
165 static inline struct predicate
166 true_predicate (void)
168 struct predicate p;
169 p.clause[0] = 0;
170 return p;
174 /* Return predicate testing single condition number COND. */
176 static inline struct predicate
177 single_cond_predicate (int cond)
179 struct predicate p;
180 p.clause[0] = 1 << cond;
181 p.clause[1] = 0;
182 return p;
186 /* Return false predicate. First clause require false condition. */
188 static inline struct predicate
189 false_predicate (void)
191 return single_cond_predicate (predicate_false_condition);
195 /* Return true if P is (true). */
197 static inline bool
198 true_predicate_p (struct predicate *p)
200 return !p->clause[0];
204 /* Return true if P is (false). */
206 static inline bool
207 false_predicate_p (struct predicate *p)
209 if (p->clause[0] == (1 << predicate_false_condition))
211 gcc_checking_assert (!p->clause[1]
212 && p->clause[0] == 1 << predicate_false_condition);
213 return true;
215 return false;
219 /* Return predicate that is set true when function is not inlined. */
221 static inline struct predicate
222 not_inlined_predicate (void)
224 return single_cond_predicate (predicate_not_inlined_condition);
227 /* Simple description of whether a memory load or a condition refers to a load
228 from an aggregate and if so, how and where from in the aggregate.
229 Individual fields have the same meaning like fields with the same name in
230 struct condition. */
232 struct agg_position_info
234 HOST_WIDE_INT offset;
235 bool agg_contents;
236 bool by_ref;
239 /* Add condition to condition list CONDS. AGGPOS describes whether the used
240 oprand is loaded from an aggregate and where in the aggregate it is. It can
241 be NULL, which means this not a load from an aggregate. */
243 static struct predicate
244 add_condition (struct inline_summary *summary, int operand_num,
245 struct agg_position_info *aggpos,
246 enum tree_code code, tree val)
248 int i;
249 struct condition *c;
250 struct condition new_cond;
251 HOST_WIDE_INT offset;
252 bool agg_contents, by_ref;
254 if (aggpos)
256 offset = aggpos->offset;
257 agg_contents = aggpos->agg_contents;
258 by_ref = aggpos->by_ref;
260 else
262 offset = 0;
263 agg_contents = false;
264 by_ref = false;
267 gcc_checking_assert (operand_num >= 0);
268 for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
270 if (c->operand_num == operand_num
271 && c->code == code
272 && c->val == val
273 && c->agg_contents == agg_contents
274 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
275 return single_cond_predicate (i + predicate_first_dynamic_condition);
277 /* Too many conditions. Give up and return constant true. */
278 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
279 return true_predicate ();
281 new_cond.operand_num = operand_num;
282 new_cond.code = code;
283 new_cond.val = val;
284 new_cond.agg_contents = agg_contents;
285 new_cond.by_ref = by_ref;
286 new_cond.offset = offset;
287 vec_safe_push (summary->conds, new_cond);
288 return single_cond_predicate (i + predicate_first_dynamic_condition);
292 /* Add clause CLAUSE into the predicate P. */
294 static inline void
295 add_clause (conditions conditions, struct predicate *p, clause_t clause)
297 int i;
298 int i2;
299 int insert_here = -1;
300 int c1, c2;
302 /* True clause. */
303 if (!clause)
304 return;
306 /* False clause makes the whole predicate false. Kill the other variants. */
307 if (clause == (1 << predicate_false_condition))
309 p->clause[0] = (1 << predicate_false_condition);
310 p->clause[1] = 0;
311 return;
313 if (false_predicate_p (p))
314 return;
316 /* No one should be silly enough to add false into nontrivial clauses. */
317 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
319 /* Look where to insert the clause. At the same time prune out
320 clauses of P that are implied by the new clause and thus
321 redundant. */
322 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
324 p->clause[i2] = p->clause[i];
326 if (!p->clause[i])
327 break;
329 /* If p->clause[i] implies clause, there is nothing to add. */
330 if ((p->clause[i] & clause) == p->clause[i])
332 /* We had nothing to add, none of clauses should've become
333 redundant. */
334 gcc_checking_assert (i == i2);
335 return;
338 if (p->clause[i] < clause && insert_here < 0)
339 insert_here = i2;
341 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
342 Otherwise the p->clause[i] has to stay. */
343 if ((p->clause[i] & clause) != clause)
344 i2++;
347 /* Look for clauses that are obviously true. I.e.
348 op0 == 5 || op0 != 5. */
349 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
351 condition *cc1;
352 if (!(clause & (1 << c1)))
353 continue;
354 cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
355 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
356 and thus there is no point for looking for them. */
357 if (cc1->code == CHANGED || cc1->code == IS_NOT_CONSTANT)
358 continue;
359 for (c2 = c1 + 1; c2 < NUM_CONDITIONS; c2++)
360 if (clause & (1 << c2))
362 condition *cc1 =
363 &(*conditions)[c1 - predicate_first_dynamic_condition];
364 condition *cc2 =
365 &(*conditions)[c2 - predicate_first_dynamic_condition];
366 if (cc1->operand_num == cc2->operand_num
367 && cc1->val == cc2->val
368 && cc2->code != IS_NOT_CONSTANT
369 && cc2->code != CHANGED
370 && cc1->code == invert_tree_comparison (cc2->code,
371 HONOR_NANS (cc1->val)))
372 return;
377 /* We run out of variants. Be conservative in positive direction. */
378 if (i2 == MAX_CLAUSES)
379 return;
380 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
381 p->clause[i2 + 1] = 0;
382 if (insert_here >= 0)
383 for (; i2 > insert_here; i2--)
384 p->clause[i2] = p->clause[i2 - 1];
385 else
386 insert_here = i2;
387 p->clause[insert_here] = clause;
391 /* Return P & P2. */
393 static struct predicate
394 and_predicates (conditions conditions,
395 struct predicate *p, struct predicate *p2)
397 struct predicate out = *p;
398 int i;
400 /* Avoid busy work. */
401 if (false_predicate_p (p2) || true_predicate_p (p))
402 return *p2;
403 if (false_predicate_p (p) || true_predicate_p (p2))
404 return *p;
406 /* See how far predicates match. */
407 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
409 gcc_checking_assert (i < MAX_CLAUSES);
412 /* Combine the predicates rest. */
413 for (; p2->clause[i]; i++)
415 gcc_checking_assert (i < MAX_CLAUSES);
416 add_clause (conditions, &out, p2->clause[i]);
418 return out;
422 /* Return true if predicates are obviously equal. */
424 static inline bool
425 predicates_equal_p (struct predicate *p, struct predicate *p2)
427 int i;
428 for (i = 0; p->clause[i]; i++)
430 gcc_checking_assert (i < MAX_CLAUSES);
431 gcc_checking_assert (p->clause[i] > p->clause[i + 1]);
432 gcc_checking_assert (!p2->clause[i]
433 || p2->clause[i] > p2->clause[i + 1]);
434 if (p->clause[i] != p2->clause[i])
435 return false;
437 return !p2->clause[i];
441 /* Return P | P2. */
443 static struct predicate
444 or_predicates (conditions conditions,
445 struct predicate *p, struct predicate *p2)
447 struct predicate out = true_predicate ();
448 int i, j;
450 /* Avoid busy work. */
451 if (false_predicate_p (p2) || true_predicate_p (p))
452 return *p;
453 if (false_predicate_p (p) || true_predicate_p (p2))
454 return *p2;
455 if (predicates_equal_p (p, p2))
456 return *p;
458 /* OK, combine the predicates. */
459 for (i = 0; p->clause[i]; i++)
460 for (j = 0; p2->clause[j]; j++)
462 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
463 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
465 return out;
469 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
470 if predicate P is known to be false. */
472 static bool
473 evaluate_predicate (struct predicate *p, clause_t possible_truths)
475 int i;
477 /* True remains true. */
478 if (true_predicate_p (p))
479 return true;
481 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
483 /* See if we can find clause we can disprove. */
484 for (i = 0; p->clause[i]; i++)
486 gcc_checking_assert (i < MAX_CLAUSES);
487 if (!(p->clause[i] & possible_truths))
488 return false;
490 return true;
493 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
494 instruction will be recomputed per invocation of the inlined call. */
496 static int
497 predicate_probability (conditions conds,
498 struct predicate *p, clause_t possible_truths,
499 vec<inline_param_summary> inline_param_summary)
501 int i;
502 int combined_prob = REG_BR_PROB_BASE;
504 /* True remains true. */
505 if (true_predicate_p (p))
506 return REG_BR_PROB_BASE;
508 if (false_predicate_p (p))
509 return 0;
511 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
513 /* See if we can find clause we can disprove. */
514 for (i = 0; p->clause[i]; i++)
516 gcc_checking_assert (i < MAX_CLAUSES);
517 if (!(p->clause[i] & possible_truths))
518 return 0;
519 else
521 int this_prob = 0;
522 int i2;
523 if (!inline_param_summary.exists ())
524 return REG_BR_PROB_BASE;
525 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
526 if ((p->clause[i] & possible_truths) & (1 << i2))
528 if (i2 >= predicate_first_dynamic_condition)
530 condition *c =
531 &(*conds)[i2 - predicate_first_dynamic_condition];
532 if (c->code == CHANGED
533 && (c->operand_num <
534 (int) inline_param_summary.length ()))
536 int iprob =
537 inline_param_summary[c->operand_num].change_prob;
538 this_prob = MAX (this_prob, iprob);
540 else
541 this_prob = REG_BR_PROB_BASE;
543 else
544 this_prob = REG_BR_PROB_BASE;
546 combined_prob = MIN (this_prob, combined_prob);
547 if (!combined_prob)
548 return 0;
551 return combined_prob;
555 /* Dump conditional COND. */
557 static void
558 dump_condition (FILE *f, conditions conditions, int cond)
560 condition *c;
561 if (cond == predicate_false_condition)
562 fprintf (f, "false");
563 else if (cond == predicate_not_inlined_condition)
564 fprintf (f, "not inlined");
565 else
567 c = &(*conditions)[cond - predicate_first_dynamic_condition];
568 fprintf (f, "op%i", c->operand_num);
569 if (c->agg_contents)
570 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
571 c->by_ref ? "ref " : "", c->offset);
572 if (c->code == IS_NOT_CONSTANT)
574 fprintf (f, " not constant");
575 return;
577 if (c->code == CHANGED)
579 fprintf (f, " changed");
580 return;
582 fprintf (f, " %s ", op_symbol_code (c->code));
583 print_generic_expr (f, c->val, 1);
588 /* Dump clause CLAUSE. */
590 static void
591 dump_clause (FILE *f, conditions conds, clause_t clause)
593 int i;
594 bool found = false;
595 fprintf (f, "(");
596 if (!clause)
597 fprintf (f, "true");
598 for (i = 0; i < NUM_CONDITIONS; i++)
599 if (clause & (1 << i))
601 if (found)
602 fprintf (f, " || ");
603 found = true;
604 dump_condition (f, conds, i);
606 fprintf (f, ")");
610 /* Dump predicate PREDICATE. */
612 static void
613 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
615 int i;
616 if (true_predicate_p (pred))
617 dump_clause (f, conds, 0);
618 else
619 for (i = 0; pred->clause[i]; i++)
621 if (i)
622 fprintf (f, " && ");
623 dump_clause (f, conds, pred->clause[i]);
625 fprintf (f, "\n");
629 /* Dump inline hints. */
630 void
631 dump_inline_hints (FILE *f, inline_hints hints)
633 if (!hints)
634 return;
635 fprintf (f, "inline hints:");
636 if (hints & INLINE_HINT_indirect_call)
638 hints &= ~INLINE_HINT_indirect_call;
639 fprintf (f, " indirect_call");
641 if (hints & INLINE_HINT_loop_iterations)
643 hints &= ~INLINE_HINT_loop_iterations;
644 fprintf (f, " loop_iterations");
646 if (hints & INLINE_HINT_loop_stride)
648 hints &= ~INLINE_HINT_loop_stride;
649 fprintf (f, " loop_stride");
651 if (hints & INLINE_HINT_same_scc)
653 hints &= ~INLINE_HINT_same_scc;
654 fprintf (f, " same_scc");
656 if (hints & INLINE_HINT_in_scc)
658 hints &= ~INLINE_HINT_in_scc;
659 fprintf (f, " in_scc");
661 if (hints & INLINE_HINT_cross_module)
663 hints &= ~INLINE_HINT_cross_module;
664 fprintf (f, " cross_module");
666 if (hints & INLINE_HINT_declared_inline)
668 hints &= ~INLINE_HINT_declared_inline;
669 fprintf (f, " declared_inline");
671 if (hints & INLINE_HINT_array_index)
673 hints &= ~INLINE_HINT_array_index;
674 fprintf (f, " array_index");
676 if (hints & INLINE_HINT_known_hot)
678 hints &= ~INLINE_HINT_known_hot;
679 fprintf (f, " known_hot");
681 gcc_assert (!hints);
685 /* Record SIZE and TIME under condition PRED into the inline summary. */
687 static void
688 account_size_time (struct inline_summary *summary, int size, int time,
689 struct predicate *pred)
691 size_time_entry *e;
692 bool found = false;
693 int i;
695 if (false_predicate_p (pred))
696 return;
698 /* We need to create initial empty unconitional clause, but otherwie
699 we don't need to account empty times and sizes. */
700 if (!size && !time && summary->entry)
701 return;
703 /* Watch overflow that might result from insane profiles. */
704 if (time > MAX_TIME * INLINE_TIME_SCALE)
705 time = MAX_TIME * INLINE_TIME_SCALE;
706 gcc_assert (time >= 0);
708 for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
709 if (predicates_equal_p (&e->predicate, pred))
711 found = true;
712 break;
714 if (i == 256)
716 i = 0;
717 found = true;
718 e = &(*summary->entry)[0];
719 gcc_assert (!e->predicate.clause[0]);
720 if (dump_file && (dump_flags & TDF_DETAILS))
721 fprintf (dump_file,
722 "\t\tReached limit on number of entries, "
723 "ignoring the predicate.");
725 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
727 fprintf (dump_file,
728 "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
729 ((double) size) / INLINE_SIZE_SCALE,
730 ((double) time) / INLINE_TIME_SCALE, found ? "" : "new ");
731 dump_predicate (dump_file, summary->conds, pred);
733 if (!found)
735 struct size_time_entry new_entry;
736 new_entry.size = size;
737 new_entry.time = time;
738 new_entry.predicate = *pred;
739 vec_safe_push (summary->entry, new_entry);
741 else
743 e->size += size;
744 e->time += time;
745 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
746 e->time = MAX_TIME * INLINE_TIME_SCALE;
750 /* We proved E to be unreachable, redirect it to __bultin_unreachable. */
752 static struct cgraph_edge *
753 redirect_to_unreachable (struct cgraph_edge *e)
755 struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
756 struct cgraph_node *target = cgraph_node::get_create
757 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
759 if (e->speculative)
760 e = e->resolve_speculation (target->decl);
761 else if (!e->callee)
762 e->make_direct (target);
763 else
764 e->redirect_callee (target);
765 struct inline_edge_summary *es = inline_edge_summary (e);
766 e->inline_failed = CIF_UNREACHABLE;
767 e->frequency = 0;
768 e->count = 0;
769 es->call_stmt_size = 0;
770 es->call_stmt_time = 0;
771 if (callee)
772 callee->remove_symbol_and_inline_clones ();
773 return e;
776 /* Set predicate for edge E. */
778 static void
779 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
781 /* If the edge is determined to be never executed, redirect it
782 to BUILTIN_UNREACHABLE to save inliner from inlining into it. */
783 if (predicate && false_predicate_p (predicate)
784 /* When handling speculative edges, we need to do the redirection
785 just once. Do it always on the direct edge, so we do not
786 attempt to resolve speculation while duplicating the edge. */
787 && (!e->speculative || e->callee))
788 e = redirect_to_unreachable (e);
790 struct inline_edge_summary *es = inline_edge_summary (e);
791 if (predicate && !true_predicate_p (predicate))
793 if (!es->predicate)
794 es->predicate = edge_predicate_pool.allocate ();
795 *es->predicate = *predicate;
797 else
799 if (es->predicate)
800 edge_predicate_pool.remove (es->predicate);
801 es->predicate = NULL;
805 /* Set predicate for hint *P. */
807 static void
808 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
810 if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
812 if (*p)
813 edge_predicate_pool.remove (*p);
814 *p = NULL;
816 else
818 if (!*p)
819 *p = edge_predicate_pool.allocate ();
820 **p = new_predicate;
825 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
826 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
827 Return clause of possible truths. When INLINE_P is true, assume that we are
828 inlining.
830 ERROR_MARK means compile time invariant. */
832 static clause_t
833 evaluate_conditions_for_known_args (struct cgraph_node *node,
834 bool inline_p,
835 vec<tree> known_vals,
836 vec<ipa_agg_jump_function_p>
837 known_aggs)
839 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
840 struct inline_summary *info = inline_summaries->get (node);
841 int i;
842 struct condition *c;
844 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
846 tree val;
847 tree res;
849 /* We allow call stmt to have fewer arguments than the callee function
850 (especially for K&R style programs). So bound check here (we assume
851 known_aggs vector, if non-NULL, has the same length as
852 known_vals). */
853 gcc_checking_assert (!known_aggs.exists ()
854 || (known_vals.length () == known_aggs.length ()));
855 if (c->operand_num >= (int) known_vals.length ())
857 clause |= 1 << (i + predicate_first_dynamic_condition);
858 continue;
861 if (c->agg_contents)
863 struct ipa_agg_jump_function *agg;
865 if (c->code == CHANGED
866 && !c->by_ref
867 && (known_vals[c->operand_num] == error_mark_node))
868 continue;
870 if (known_aggs.exists ())
872 agg = known_aggs[c->operand_num];
873 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
875 else
876 val = NULL_TREE;
878 else
880 val = known_vals[c->operand_num];
881 if (val == error_mark_node && c->code != CHANGED)
882 val = NULL_TREE;
885 if (!val)
887 clause |= 1 << (i + predicate_first_dynamic_condition);
888 continue;
890 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
891 continue;
893 if (operand_equal_p (TYPE_SIZE (TREE_TYPE (c->val)),
894 TYPE_SIZE (TREE_TYPE (val)), 0))
896 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (c->val), val);
898 res = val
899 ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
900 : NULL;
902 if (res && integer_zerop (res))
903 continue;
905 clause |= 1 << (i + predicate_first_dynamic_condition);
907 return clause;
911 /* Work out what conditions might be true at invocation of E. */
913 static void
914 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
915 clause_t *clause_ptr,
916 vec<tree> *known_vals_ptr,
917 vec<ipa_polymorphic_call_context>
918 *known_contexts_ptr,
919 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
921 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
922 struct inline_summary *info = inline_summaries->get (callee);
923 vec<tree> known_vals = vNULL;
924 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
926 if (clause_ptr)
927 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
928 if (known_vals_ptr)
929 known_vals_ptr->create (0);
930 if (known_contexts_ptr)
931 known_contexts_ptr->create (0);
933 if (ipa_node_params_sum
934 && !e->call_stmt_cannot_inline_p
935 && ((clause_ptr && info->conds) || known_vals_ptr || known_contexts_ptr))
937 struct ipa_node_params *parms_info;
938 struct ipa_edge_args *args = IPA_EDGE_REF (e);
939 struct inline_edge_summary *es = inline_edge_summary (e);
940 int i, count = ipa_get_cs_argument_count (args);
942 if (e->caller->global.inlined_to)
943 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
944 else
945 parms_info = IPA_NODE_REF (e->caller);
947 if (count && (info->conds || known_vals_ptr))
948 known_vals.safe_grow_cleared (count);
949 if (count && (info->conds || known_aggs_ptr))
950 known_aggs.safe_grow_cleared (count);
951 if (count && known_contexts_ptr)
952 known_contexts_ptr->safe_grow_cleared (count);
954 for (i = 0; i < count; i++)
956 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
957 tree cst = ipa_value_from_jfunc (parms_info, jf);
959 if (!cst && e->call_stmt
960 && i < (int)gimple_call_num_args (e->call_stmt))
962 cst = gimple_call_arg (e->call_stmt, i);
963 if (!is_gimple_min_invariant (cst))
964 cst = NULL;
966 if (cst)
968 gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
969 if (known_vals.exists ())
970 known_vals[i] = cst;
972 else if (inline_p && !es->param[i].change_prob)
973 known_vals[i] = error_mark_node;
975 if (known_contexts_ptr)
976 (*known_contexts_ptr)[i] = ipa_context_from_jfunc (parms_info, e,
977 i, jf);
978 /* TODO: When IPA-CP starts propagating and merging aggregate jump
979 functions, use its knowledge of the caller too, just like the
980 scalar case above. */
981 known_aggs[i] = &jf->agg;
984 else if (e->call_stmt && !e->call_stmt_cannot_inline_p
985 && ((clause_ptr && info->conds) || known_vals_ptr))
987 int i, count = (int)gimple_call_num_args (e->call_stmt);
989 if (count && (info->conds || known_vals_ptr))
990 known_vals.safe_grow_cleared (count);
991 for (i = 0; i < count; i++)
993 tree cst = gimple_call_arg (e->call_stmt, i);
994 if (!is_gimple_min_invariant (cst))
995 cst = NULL;
996 if (cst)
997 known_vals[i] = cst;
1001 if (clause_ptr)
1002 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
1003 known_vals, known_aggs);
1005 if (known_vals_ptr)
1006 *known_vals_ptr = known_vals;
1007 else
1008 known_vals.release ();
1010 if (known_aggs_ptr)
1011 *known_aggs_ptr = known_aggs;
1012 else
1013 known_aggs.release ();
1017 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
1019 static void
1020 inline_summary_alloc (void)
1022 if (!edge_removal_hook_holder)
1023 edge_removal_hook_holder =
1024 symtab->add_edge_removal_hook (&inline_edge_removal_hook, NULL);
1025 if (!edge_duplication_hook_holder)
1026 edge_duplication_hook_holder =
1027 symtab->add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
1029 if (!inline_summaries)
1030 inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
1032 if (inline_edge_summary_vec.length () <= (unsigned) symtab->edges_max_uid)
1033 inline_edge_summary_vec.safe_grow_cleared (symtab->edges_max_uid + 1);
1036 /* We are called multiple time for given function; clear
1037 data from previous run so they are not cumulated. */
1039 static void
1040 reset_inline_edge_summary (struct cgraph_edge *e)
1042 if (e->uid < (int) inline_edge_summary_vec.length ())
1044 struct inline_edge_summary *es = inline_edge_summary (e);
1046 es->call_stmt_size = es->call_stmt_time = 0;
1047 if (es->predicate)
1048 edge_predicate_pool.remove (es->predicate);
1049 es->predicate = NULL;
1050 es->param.release ();
1054 /* We are called multiple time for given function; clear
1055 data from previous run so they are not cumulated. */
1057 static void
1058 reset_inline_summary (struct cgraph_node *node,
1059 inline_summary *info)
1061 struct cgraph_edge *e;
1063 info->self_size = info->self_time = 0;
1064 info->estimated_stack_size = 0;
1065 info->estimated_self_stack_size = 0;
1066 info->stack_frame_offset = 0;
1067 info->size = 0;
1068 info->time = 0;
1069 info->growth = 0;
1070 info->scc_no = 0;
1071 if (info->loop_iterations)
1073 edge_predicate_pool.remove (info->loop_iterations);
1074 info->loop_iterations = NULL;
1076 if (info->loop_stride)
1078 edge_predicate_pool.remove (info->loop_stride);
1079 info->loop_stride = NULL;
1081 if (info->array_index)
1083 edge_predicate_pool.remove (info->array_index);
1084 info->array_index = NULL;
1086 vec_free (info->conds);
1087 vec_free (info->entry);
1088 for (e = node->callees; e; e = e->next_callee)
1089 reset_inline_edge_summary (e);
1090 for (e = node->indirect_calls; e; e = e->next_callee)
1091 reset_inline_edge_summary (e);
1094 /* Hook that is called by cgraph.c when a node is removed. */
1096 void
1097 inline_summary_t::remove (cgraph_node *node, inline_summary *info)
1099 reset_inline_summary (node, info);
1102 /* Remap predicate P of former function to be predicate of duplicated function.
1103 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1104 INFO is inline summary of the duplicated node. */
1106 static struct predicate
1107 remap_predicate_after_duplication (struct predicate *p,
1108 clause_t possible_truths,
1109 struct inline_summary *info)
1111 struct predicate new_predicate = true_predicate ();
1112 int j;
1113 for (j = 0; p->clause[j]; j++)
1114 if (!(possible_truths & p->clause[j]))
1116 new_predicate = false_predicate ();
1117 break;
1119 else
1120 add_clause (info->conds, &new_predicate,
1121 possible_truths & p->clause[j]);
1122 return new_predicate;
1125 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1126 Additionally care about allocating new memory slot for updated predicate
1127 and set it to NULL when it becomes true or false (and thus uninteresting).
1130 static void
1131 remap_hint_predicate_after_duplication (struct predicate **p,
1132 clause_t possible_truths,
1133 struct inline_summary *info)
1135 struct predicate new_predicate;
1137 if (!*p)
1138 return;
1140 new_predicate = remap_predicate_after_duplication (*p,
1141 possible_truths, info);
1142 /* We do not want to free previous predicate; it is used by node origin. */
1143 *p = NULL;
1144 set_hint_predicate (p, new_predicate);
1148 /* Hook that is called by cgraph.c when a node is duplicated. */
1149 void
1150 inline_summary_t::duplicate (cgraph_node *src,
1151 cgraph_node *dst,
1152 inline_summary *,
1153 inline_summary *info)
1155 inline_summary_alloc ();
1156 memcpy (info, inline_summaries->get (src), sizeof (inline_summary));
1157 /* TODO: as an optimization, we may avoid copying conditions
1158 that are known to be false or true. */
1159 info->conds = vec_safe_copy (info->conds);
1161 /* When there are any replacements in the function body, see if we can figure
1162 out that something was optimized out. */
1163 if (ipa_node_params_sum && dst->clone.tree_map)
1165 vec<size_time_entry, va_gc> *entry = info->entry;
1166 /* Use SRC parm info since it may not be copied yet. */
1167 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1168 vec<tree> known_vals = vNULL;
1169 int count = ipa_get_param_count (parms_info);
1170 int i, j;
1171 clause_t possible_truths;
1172 struct predicate true_pred = true_predicate ();
1173 size_time_entry *e;
1174 int optimized_out_size = 0;
1175 bool inlined_to_p = false;
1176 struct cgraph_edge *edge, *next;
1178 info->entry = 0;
1179 known_vals.safe_grow_cleared (count);
1180 for (i = 0; i < count; i++)
1182 struct ipa_replace_map *r;
1184 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1186 if (((!r->old_tree && r->parm_num == i)
1187 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1188 && r->replace_p && !r->ref_p)
1190 known_vals[i] = r->new_tree;
1191 break;
1195 possible_truths = evaluate_conditions_for_known_args (dst, false,
1196 known_vals,
1197 vNULL);
1198 known_vals.release ();
1200 account_size_time (info, 0, 0, &true_pred);
1202 /* Remap size_time vectors.
1203 Simplify the predicate by prunning out alternatives that are known
1204 to be false.
1205 TODO: as on optimization, we can also eliminate conditions known
1206 to be true. */
1207 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1209 struct predicate new_predicate;
1210 new_predicate = remap_predicate_after_duplication (&e->predicate,
1211 possible_truths,
1212 info);
1213 if (false_predicate_p (&new_predicate))
1214 optimized_out_size += e->size;
1215 else
1216 account_size_time (info, e->size, e->time, &new_predicate);
1219 /* Remap edge predicates with the same simplification as above.
1220 Also copy constantness arrays. */
1221 for (edge = dst->callees; edge; edge = next)
1223 struct predicate new_predicate;
1224 struct inline_edge_summary *es = inline_edge_summary (edge);
1225 next = edge->next_callee;
1227 if (!edge->inline_failed)
1228 inlined_to_p = true;
1229 if (!es->predicate)
1230 continue;
1231 new_predicate = remap_predicate_after_duplication (es->predicate,
1232 possible_truths,
1233 info);
1234 if (false_predicate_p (&new_predicate)
1235 && !false_predicate_p (es->predicate))
1236 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1237 edge_set_predicate (edge, &new_predicate);
1240 /* Remap indirect edge predicates with the same simplificaiton as above.
1241 Also copy constantness arrays. */
1242 for (edge = dst->indirect_calls; edge; edge = next)
1244 struct predicate new_predicate;
1245 struct inline_edge_summary *es = inline_edge_summary (edge);
1246 next = edge->next_callee;
1248 gcc_checking_assert (edge->inline_failed);
1249 if (!es->predicate)
1250 continue;
1251 new_predicate = remap_predicate_after_duplication (es->predicate,
1252 possible_truths,
1253 info);
1254 if (false_predicate_p (&new_predicate)
1255 && !false_predicate_p (es->predicate))
1256 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1257 edge_set_predicate (edge, &new_predicate);
1259 remap_hint_predicate_after_duplication (&info->loop_iterations,
1260 possible_truths, info);
1261 remap_hint_predicate_after_duplication (&info->loop_stride,
1262 possible_truths, info);
1263 remap_hint_predicate_after_duplication (&info->array_index,
1264 possible_truths, info);
1266 /* If inliner or someone after inliner will ever start producing
1267 non-trivial clones, we will get trouble with lack of information
1268 about updating self sizes, because size vectors already contains
1269 sizes of the calees. */
1270 gcc_assert (!inlined_to_p || !optimized_out_size);
1272 else
1274 info->entry = vec_safe_copy (info->entry);
1275 if (info->loop_iterations)
1277 predicate p = *info->loop_iterations;
1278 info->loop_iterations = NULL;
1279 set_hint_predicate (&info->loop_iterations, p);
1281 if (info->loop_stride)
1283 predicate p = *info->loop_stride;
1284 info->loop_stride = NULL;
1285 set_hint_predicate (&info->loop_stride, p);
1287 if (info->array_index)
1289 predicate p = *info->array_index;
1290 info->array_index = NULL;
1291 set_hint_predicate (&info->array_index, p);
1294 if (!dst->global.inlined_to)
1295 inline_update_overall_summary (dst);
1299 /* Hook that is called by cgraph.c when a node is duplicated. */
1301 static void
1302 inline_edge_duplication_hook (struct cgraph_edge *src,
1303 struct cgraph_edge *dst,
1304 ATTRIBUTE_UNUSED void *data)
1306 struct inline_edge_summary *info;
1307 struct inline_edge_summary *srcinfo;
1308 inline_summary_alloc ();
1309 info = inline_edge_summary (dst);
1310 srcinfo = inline_edge_summary (src);
1311 memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
1312 info->predicate = NULL;
1313 edge_set_predicate (dst, srcinfo->predicate);
1314 info->param = srcinfo->param.copy ();
1315 if (!dst->indirect_unknown_callee && src->indirect_unknown_callee)
1317 info->call_stmt_size -= (eni_size_weights.indirect_call_cost
1318 - eni_size_weights.call_cost);
1319 info->call_stmt_time -= (eni_time_weights.indirect_call_cost
1320 - eni_time_weights.call_cost);
1325 /* Keep edge cache consistent across edge removal. */
1327 static void
1328 inline_edge_removal_hook (struct cgraph_edge *edge,
1329 void *data ATTRIBUTE_UNUSED)
1331 if (edge_growth_cache.exists ())
1332 reset_edge_growth_cache (edge);
1333 reset_inline_edge_summary (edge);
1337 /* Initialize growth caches. */
1339 void
1340 initialize_growth_caches (void)
1342 if (symtab->edges_max_uid)
1343 edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
1347 /* Free growth caches. */
1349 void
1350 free_growth_caches (void)
1352 edge_growth_cache.release ();
1356 /* Dump edge summaries associated to NODE and recursively to all clones.
1357 Indent by INDENT. */
1359 static void
1360 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1361 struct inline_summary *info)
1363 struct cgraph_edge *edge;
1364 for (edge = node->callees; edge; edge = edge->next_callee)
1366 struct inline_edge_summary *es = inline_edge_summary (edge);
1367 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1368 int i;
1370 fprintf (f,
1371 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1372 " time: %2i callee size:%2i stack:%2i",
1373 indent, "", callee->name (), callee->order,
1374 !edge->inline_failed
1375 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1376 indent, "", es->loop_depth, edge->frequency,
1377 es->call_stmt_size, es->call_stmt_time,
1378 (int) inline_summaries->get (callee)->size / INLINE_SIZE_SCALE,
1379 (int) inline_summaries->get (callee)->estimated_stack_size);
1381 if (es->predicate)
1383 fprintf (f, " predicate: ");
1384 dump_predicate (f, info->conds, es->predicate);
1386 else
1387 fprintf (f, "\n");
1388 if (es->param.exists ())
1389 for (i = 0; i < (int) es->param.length (); i++)
1391 int prob = es->param[i].change_prob;
1393 if (!prob)
1394 fprintf (f, "%*s op%i is compile time invariant\n",
1395 indent + 2, "", i);
1396 else if (prob != REG_BR_PROB_BASE)
1397 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1398 prob * 100.0 / REG_BR_PROB_BASE);
1400 if (!edge->inline_failed)
1402 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1403 " callee size %i\n",
1404 indent + 2, "",
1405 (int) inline_summaries->get (callee)->stack_frame_offset,
1406 (int) inline_summaries->get (callee)->estimated_self_stack_size,
1407 (int) inline_summaries->get (callee)->estimated_stack_size);
1408 dump_inline_edge_summary (f, indent + 2, callee, info);
1411 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1413 struct inline_edge_summary *es = inline_edge_summary (edge);
1414 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1415 " time: %2i",
1416 indent, "",
1417 es->loop_depth,
1418 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1419 if (es->predicate)
1421 fprintf (f, "predicate: ");
1422 dump_predicate (f, info->conds, es->predicate);
1424 else
1425 fprintf (f, "\n");
1430 void
1431 dump_inline_summary (FILE *f, struct cgraph_node *node)
1433 if (node->definition)
1435 struct inline_summary *s = inline_summaries->get (node);
1436 size_time_entry *e;
1437 int i;
1438 fprintf (f, "Inline summary for %s/%i", node->name (),
1439 node->order);
1440 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1441 fprintf (f, " always_inline");
1442 if (s->inlinable)
1443 fprintf (f, " inlinable");
1444 if (s->contains_cilk_spawn)
1445 fprintf (f, " contains_cilk_spawn");
1446 fprintf (f, "\n self time: %i\n", s->self_time);
1447 fprintf (f, " global time: %i\n", s->time);
1448 fprintf (f, " self size: %i\n", s->self_size);
1449 fprintf (f, " global size: %i\n", s->size);
1450 fprintf (f, " min size: %i\n", s->min_size);
1451 fprintf (f, " self stack: %i\n",
1452 (int) s->estimated_self_stack_size);
1453 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1454 if (s->growth)
1455 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1456 if (s->scc_no)
1457 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1458 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1460 fprintf (f, " size:%f, time:%f, predicate:",
1461 (double) e->size / INLINE_SIZE_SCALE,
1462 (double) e->time / INLINE_TIME_SCALE);
1463 dump_predicate (f, s->conds, &e->predicate);
1465 if (s->loop_iterations)
1467 fprintf (f, " loop iterations:");
1468 dump_predicate (f, s->conds, s->loop_iterations);
1470 if (s->loop_stride)
1472 fprintf (f, " loop stride:");
1473 dump_predicate (f, s->conds, s->loop_stride);
1475 if (s->array_index)
1477 fprintf (f, " array index:");
1478 dump_predicate (f, s->conds, s->array_index);
1480 fprintf (f, " calls:\n");
1481 dump_inline_edge_summary (f, 4, node, s);
1482 fprintf (f, "\n");
1486 DEBUG_FUNCTION void
1487 debug_inline_summary (struct cgraph_node *node)
1489 dump_inline_summary (stderr, node);
1492 void
1493 dump_inline_summaries (FILE *f)
1495 struct cgraph_node *node;
1497 FOR_EACH_DEFINED_FUNCTION (node)
1498 if (!node->global.inlined_to)
1499 dump_inline_summary (f, node);
1502 /* Give initial reasons why inlining would fail on EDGE. This gets either
1503 nullified or usually overwritten by more precise reasons later. */
1505 void
1506 initialize_inline_failed (struct cgraph_edge *e)
1508 struct cgraph_node *callee = e->callee;
1510 if (e->indirect_unknown_callee)
1511 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1512 else if (!callee->definition)
1513 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1514 else if (callee->local.redefined_extern_inline)
1515 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1516 else if (e->call_stmt_cannot_inline_p)
1517 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1518 else if (cfun && fn_contains_cilk_spawn_p (cfun))
1519 /* We can't inline if the function is spawing a function. */
1520 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
1521 else
1522 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1525 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1526 boolean variable pointed to by DATA. */
1528 static bool
1529 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1530 void *data)
1532 bool *b = (bool *) data;
1533 *b = true;
1534 return true;
1537 /* If OP refers to value of function parameter, return the corresponding
1538 parameter. */
1540 static tree
1541 unmodified_parm_1 (gimple stmt, tree op)
1543 /* SSA_NAME referring to parm default def? */
1544 if (TREE_CODE (op) == SSA_NAME
1545 && SSA_NAME_IS_DEFAULT_DEF (op)
1546 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1547 return SSA_NAME_VAR (op);
1548 /* Non-SSA parm reference? */
1549 if (TREE_CODE (op) == PARM_DECL)
1551 bool modified = false;
1553 ao_ref refd;
1554 ao_ref_init (&refd, op);
1555 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1556 NULL);
1557 if (!modified)
1558 return op;
1560 return NULL_TREE;
1563 /* If OP refers to value of function parameter, return the corresponding
1564 parameter. Also traverse chains of SSA register assignments. */
1566 static tree
1567 unmodified_parm (gimple stmt, tree op)
1569 tree res = unmodified_parm_1 (stmt, op);
1570 if (res)
1571 return res;
1573 if (TREE_CODE (op) == SSA_NAME
1574 && !SSA_NAME_IS_DEFAULT_DEF (op)
1575 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1576 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1577 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1578 return NULL_TREE;
1581 /* If OP refers to a value of a function parameter or value loaded from an
1582 aggregate passed to a parameter (either by value or reference), return TRUE
1583 and store the number of the parameter to *INDEX_P and information whether
1584 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1585 the function parameters, STMT is the statement in which OP is used or
1586 loaded. */
1588 static bool
1589 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1590 gimple stmt, tree op, int *index_p,
1591 struct agg_position_info *aggpos)
1593 tree res = unmodified_parm_1 (stmt, op);
1595 gcc_checking_assert (aggpos);
1596 if (res)
1598 *index_p = ipa_get_param_decl_index (info, res);
1599 if (*index_p < 0)
1600 return false;
1601 aggpos->agg_contents = false;
1602 aggpos->by_ref = false;
1603 return true;
1606 if (TREE_CODE (op) == SSA_NAME)
1608 if (SSA_NAME_IS_DEFAULT_DEF (op)
1609 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1610 return false;
1611 stmt = SSA_NAME_DEF_STMT (op);
1612 op = gimple_assign_rhs1 (stmt);
1613 if (!REFERENCE_CLASS_P (op))
1614 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1615 aggpos);
1618 aggpos->agg_contents = true;
1619 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1620 &aggpos->by_ref);
1623 /* See if statement might disappear after inlining.
1624 0 - means not eliminated
1625 1 - half of statements goes away
1626 2 - for sure it is eliminated.
1627 We are not terribly sophisticated, basically looking for simple abstraction
1628 penalty wrappers. */
1630 static int
1631 eliminated_by_inlining_prob (gimple stmt)
1633 enum gimple_code code = gimple_code (stmt);
1634 enum tree_code rhs_code;
1636 if (!optimize)
1637 return 0;
1639 switch (code)
1641 case GIMPLE_RETURN:
1642 return 2;
1643 case GIMPLE_ASSIGN:
1644 if (gimple_num_ops (stmt) != 2)
1645 return 0;
1647 rhs_code = gimple_assign_rhs_code (stmt);
1649 /* Casts of parameters, loads from parameters passed by reference
1650 and stores to return value or parameters are often free after
1651 inlining dua to SRA and further combining.
1652 Assume that half of statements goes away. */
1653 if (CONVERT_EXPR_CODE_P (rhs_code)
1654 || rhs_code == VIEW_CONVERT_EXPR
1655 || rhs_code == ADDR_EXPR
1656 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1658 tree rhs = gimple_assign_rhs1 (stmt);
1659 tree lhs = gimple_assign_lhs (stmt);
1660 tree inner_rhs = get_base_address (rhs);
1661 tree inner_lhs = get_base_address (lhs);
1662 bool rhs_free = false;
1663 bool lhs_free = false;
1665 if (!inner_rhs)
1666 inner_rhs = rhs;
1667 if (!inner_lhs)
1668 inner_lhs = lhs;
1670 /* Reads of parameter are expected to be free. */
1671 if (unmodified_parm (stmt, inner_rhs))
1672 rhs_free = true;
1673 /* Match expressions of form &this->field. Those will most likely
1674 combine with something upstream after inlining. */
1675 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1677 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1678 if (TREE_CODE (op) == PARM_DECL)
1679 rhs_free = true;
1680 else if (TREE_CODE (op) == MEM_REF
1681 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1682 rhs_free = true;
1685 /* When parameter is not SSA register because its address is taken
1686 and it is just copied into one, the statement will be completely
1687 free after inlining (we will copy propagate backward). */
1688 if (rhs_free && is_gimple_reg (lhs))
1689 return 2;
1691 /* Reads of parameters passed by reference
1692 expected to be free (i.e. optimized out after inlining). */
1693 if (TREE_CODE (inner_rhs) == MEM_REF
1694 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1695 rhs_free = true;
1697 /* Copying parameter passed by reference into gimple register is
1698 probably also going to copy propagate, but we can't be quite
1699 sure. */
1700 if (rhs_free && is_gimple_reg (lhs))
1701 lhs_free = true;
1703 /* Writes to parameters, parameters passed by value and return value
1704 (either dirrectly or passed via invisible reference) are free.
1706 TODO: We ought to handle testcase like
1707 struct a {int a,b;};
1708 struct a
1709 retrurnsturct (void)
1711 struct a a ={1,2};
1712 return a;
1715 This translate into:
1717 retrurnsturct ()
1719 int a$b;
1720 int a$a;
1721 struct a a;
1722 struct a D.2739;
1724 <bb 2>:
1725 D.2739.a = 1;
1726 D.2739.b = 2;
1727 return D.2739;
1730 For that we either need to copy ipa-split logic detecting writes
1731 to return value. */
1732 if (TREE_CODE (inner_lhs) == PARM_DECL
1733 || TREE_CODE (inner_lhs) == RESULT_DECL
1734 || (TREE_CODE (inner_lhs) == MEM_REF
1735 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1736 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1737 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1738 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1739 (inner_lhs,
1740 0))) == RESULT_DECL))))
1741 lhs_free = true;
1742 if (lhs_free
1743 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1744 rhs_free = true;
1745 if (lhs_free && rhs_free)
1746 return 1;
1748 return 0;
1749 default:
1750 return 0;
1755 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1756 predicates to the CFG edges. */
1758 static void
1759 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1760 struct inline_summary *summary,
1761 basic_block bb)
1763 gimple last;
1764 tree op;
1765 int index;
1766 struct agg_position_info aggpos;
1767 enum tree_code code, inverted_code;
1768 edge e;
1769 edge_iterator ei;
1770 gimple set_stmt;
1771 tree op2;
1773 last = last_stmt (bb);
1774 if (!last || gimple_code (last) != GIMPLE_COND)
1775 return;
1776 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1777 return;
1778 op = gimple_cond_lhs (last);
1779 /* TODO: handle conditionals like
1780 var = op0 < 4;
1781 if (var != 0). */
1782 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1784 code = gimple_cond_code (last);
1785 inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1787 FOR_EACH_EDGE (e, ei, bb->succs)
1789 enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1790 ? code : inverted_code);
1791 /* invert_tree_comparison will return ERROR_MARK on FP
1792 comparsions that are not EQ/NE instead of returning proper
1793 unordered one. Be sure it is not confused with NON_CONSTANT. */
1794 if (this_code != ERROR_MARK)
1796 struct predicate p = add_condition (summary, index, &aggpos,
1797 this_code,
1798 gimple_cond_rhs (last));
1799 e->aux = edge_predicate_pool.allocate ();
1800 *(struct predicate *) e->aux = p;
1805 if (TREE_CODE (op) != SSA_NAME)
1806 return;
1807 /* Special case
1808 if (builtin_constant_p (op))
1809 constant_code
1810 else
1811 nonconstant_code.
1812 Here we can predicate nonconstant_code. We can't
1813 really handle constant_code since we have no predicate
1814 for this and also the constant code is not known to be
1815 optimized away when inliner doen't see operand is constant.
1816 Other optimizers might think otherwise. */
1817 if (gimple_cond_code (last) != NE_EXPR
1818 || !integer_zerop (gimple_cond_rhs (last)))
1819 return;
1820 set_stmt = SSA_NAME_DEF_STMT (op);
1821 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1822 || gimple_call_num_args (set_stmt) != 1)
1823 return;
1824 op2 = gimple_call_arg (set_stmt, 0);
1825 if (!unmodified_parm_or_parm_agg_item
1826 (info, set_stmt, op2, &index, &aggpos))
1827 return;
1828 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1830 struct predicate p = add_condition (summary, index, &aggpos,
1831 IS_NOT_CONSTANT, NULL_TREE);
1832 e->aux = edge_predicate_pool.allocate ();
1833 *(struct predicate *) e->aux = p;
1838 /* If BB ends by a switch we can turn into predicates, attach corresponding
1839 predicates to the CFG edges. */
1841 static void
1842 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1843 struct inline_summary *summary,
1844 basic_block bb)
1846 gimple lastg;
1847 tree op;
1848 int index;
1849 struct agg_position_info aggpos;
1850 edge e;
1851 edge_iterator ei;
1852 size_t n;
1853 size_t case_idx;
1855 lastg = last_stmt (bb);
1856 if (!lastg || gimple_code (lastg) != GIMPLE_SWITCH)
1857 return;
1858 gswitch *last = as_a <gswitch *> (lastg);
1859 op = gimple_switch_index (last);
1860 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1861 return;
1863 FOR_EACH_EDGE (e, ei, bb->succs)
1865 e->aux = edge_predicate_pool.allocate ();
1866 *(struct predicate *) e->aux = false_predicate ();
1868 n = gimple_switch_num_labels (last);
1869 for (case_idx = 0; case_idx < n; ++case_idx)
1871 tree cl = gimple_switch_label (last, case_idx);
1872 tree min, max;
1873 struct predicate p;
1875 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1876 min = CASE_LOW (cl);
1877 max = CASE_HIGH (cl);
1879 /* For default we might want to construct predicate that none
1880 of cases is met, but it is bit hard to do not having negations
1881 of conditionals handy. */
1882 if (!min && !max)
1883 p = true_predicate ();
1884 else if (!max)
1885 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1886 else
1888 struct predicate p1, p2;
1889 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1890 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1891 p = and_predicates (summary->conds, &p1, &p2);
1893 *(struct predicate *) e->aux
1894 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1899 /* For each BB in NODE attach to its AUX pointer predicate under
1900 which it is executable. */
1902 static void
1903 compute_bb_predicates (struct cgraph_node *node,
1904 struct ipa_node_params *parms_info,
1905 struct inline_summary *summary)
1907 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1908 bool done = false;
1909 basic_block bb;
1911 FOR_EACH_BB_FN (bb, my_function)
1913 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1914 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1917 /* Entry block is always executable. */
1918 ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1919 = edge_predicate_pool.allocate ();
1920 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1921 = true_predicate ();
1923 /* A simple dataflow propagation of predicates forward in the CFG.
1924 TODO: work in reverse postorder. */
1925 while (!done)
1927 done = true;
1928 FOR_EACH_BB_FN (bb, my_function)
1930 struct predicate p = false_predicate ();
1931 edge e;
1932 edge_iterator ei;
1933 FOR_EACH_EDGE (e, ei, bb->preds)
1935 if (e->src->aux)
1937 struct predicate this_bb_predicate
1938 = *(struct predicate *) e->src->aux;
1939 if (e->aux)
1940 this_bb_predicate
1941 = and_predicates (summary->conds, &this_bb_predicate,
1942 (struct predicate *) e->aux);
1943 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1944 if (true_predicate_p (&p))
1945 break;
1948 if (false_predicate_p (&p))
1949 gcc_assert (!bb->aux);
1950 else
1952 if (!bb->aux)
1954 done = false;
1955 bb->aux = edge_predicate_pool.allocate ();
1956 *((struct predicate *) bb->aux) = p;
1958 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1960 /* This OR operation is needed to ensure monotonous data flow
1961 in the case we hit the limit on number of clauses and the
1962 and/or operations above give approximate answers. */
1963 p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
1964 if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1966 done = false;
1967 *((struct predicate *) bb->aux) = p;
1976 /* We keep info about constantness of SSA names. */
1978 typedef struct predicate predicate_t;
1979 /* Return predicate specifying when the STMT might have result that is not
1980 a compile time constant. */
1982 static struct predicate
1983 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1984 struct inline_summary *summary,
1985 tree expr,
1986 vec<predicate_t> nonconstant_names)
1988 tree parm;
1989 int index;
1991 while (UNARY_CLASS_P (expr))
1992 expr = TREE_OPERAND (expr, 0);
1994 parm = unmodified_parm (NULL, expr);
1995 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1996 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1997 if (is_gimple_min_invariant (expr))
1998 return false_predicate ();
1999 if (TREE_CODE (expr) == SSA_NAME)
2000 return nonconstant_names[SSA_NAME_VERSION (expr)];
2001 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2003 struct predicate p1 = will_be_nonconstant_expr_predicate
2004 (info, summary, TREE_OPERAND (expr, 0),
2005 nonconstant_names);
2006 struct predicate p2;
2007 if (true_predicate_p (&p1))
2008 return p1;
2009 p2 = will_be_nonconstant_expr_predicate (info, summary,
2010 TREE_OPERAND (expr, 1),
2011 nonconstant_names);
2012 return or_predicates (summary->conds, &p1, &p2);
2014 else if (TREE_CODE (expr) == COND_EXPR)
2016 struct predicate p1 = will_be_nonconstant_expr_predicate
2017 (info, summary, TREE_OPERAND (expr, 0),
2018 nonconstant_names);
2019 struct predicate p2;
2020 if (true_predicate_p (&p1))
2021 return p1;
2022 p2 = will_be_nonconstant_expr_predicate (info, summary,
2023 TREE_OPERAND (expr, 1),
2024 nonconstant_names);
2025 if (true_predicate_p (&p2))
2026 return p2;
2027 p1 = or_predicates (summary->conds, &p1, &p2);
2028 p2 = will_be_nonconstant_expr_predicate (info, summary,
2029 TREE_OPERAND (expr, 2),
2030 nonconstant_names);
2031 return or_predicates (summary->conds, &p1, &p2);
2033 else
2035 debug_tree (expr);
2036 gcc_unreachable ();
2038 return false_predicate ();
2042 /* Return predicate specifying when the STMT might have result that is not
2043 a compile time constant. */
2045 static struct predicate
2046 will_be_nonconstant_predicate (struct ipa_node_params *info,
2047 struct inline_summary *summary,
2048 gimple stmt,
2049 vec<predicate_t> nonconstant_names)
2051 struct predicate p = true_predicate ();
2052 ssa_op_iter iter;
2053 tree use;
2054 struct predicate op_non_const;
2055 bool is_load;
2056 int base_index;
2057 struct agg_position_info aggpos;
2059 /* What statments might be optimized away
2060 when their arguments are constant. */
2061 if (gimple_code (stmt) != GIMPLE_ASSIGN
2062 && gimple_code (stmt) != GIMPLE_COND
2063 && gimple_code (stmt) != GIMPLE_SWITCH
2064 && (gimple_code (stmt) != GIMPLE_CALL
2065 || !(gimple_call_flags (stmt) & ECF_CONST)))
2066 return p;
2068 /* Stores will stay anyway. */
2069 if (gimple_store_p (stmt))
2070 return p;
2072 is_load = gimple_assign_load_p (stmt);
2074 /* Loads can be optimized when the value is known. */
2075 if (is_load)
2077 tree op;
2078 gcc_assert (gimple_assign_single_p (stmt));
2079 op = gimple_assign_rhs1 (stmt);
2080 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
2081 &aggpos))
2082 return p;
2084 else
2085 base_index = -1;
2087 /* See if we understand all operands before we start
2088 adding conditionals. */
2089 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2091 tree parm = unmodified_parm (stmt, use);
2092 /* For arguments we can build a condition. */
2093 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2094 continue;
2095 if (TREE_CODE (use) != SSA_NAME)
2096 return p;
2097 /* If we know when operand is constant,
2098 we still can say something useful. */
2099 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2100 continue;
2101 return p;
2104 if (is_load)
2105 op_non_const =
2106 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2107 else
2108 op_non_const = false_predicate ();
2109 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2111 tree parm = unmodified_parm (stmt, use);
2112 int index;
2114 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2116 if (index != base_index)
2117 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2118 else
2119 continue;
2121 else
2122 p = nonconstant_names[SSA_NAME_VERSION (use)];
2123 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2125 if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2126 && gimple_op (stmt, 0)
2127 && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2128 nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2129 = op_non_const;
2130 return op_non_const;
2133 struct record_modified_bb_info
2135 bitmap bb_set;
2136 gimple stmt;
2139 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2140 set except for info->stmt. */
2142 static bool
2143 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2145 struct record_modified_bb_info *info =
2146 (struct record_modified_bb_info *) data;
2147 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2148 return false;
2149 bitmap_set_bit (info->bb_set,
2150 SSA_NAME_IS_DEFAULT_DEF (vdef)
2151 ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2152 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2153 return false;
2156 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2157 will change since last invocation of STMT.
2159 Value 0 is reserved for compile time invariants.
2160 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2161 ought to be REG_BR_PROB_BASE / estimated_iters. */
2163 static int
2164 param_change_prob (gimple stmt, int i)
2166 tree op = gimple_call_arg (stmt, i);
2167 basic_block bb = gimple_bb (stmt);
2168 tree base;
2170 /* Global invariants neve change. */
2171 if (is_gimple_min_invariant (op))
2172 return 0;
2173 /* We would have to do non-trivial analysis to really work out what
2174 is the probability of value to change (i.e. when init statement
2175 is in a sibling loop of the call).
2177 We do an conservative estimate: when call is executed N times more often
2178 than the statement defining value, we take the frequency 1/N. */
2179 if (TREE_CODE (op) == SSA_NAME)
2181 int init_freq;
2183 if (!bb->frequency)
2184 return REG_BR_PROB_BASE;
2186 if (SSA_NAME_IS_DEFAULT_DEF (op))
2187 init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2188 else
2189 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2191 if (!init_freq)
2192 init_freq = 1;
2193 if (init_freq < bb->frequency)
2194 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2195 else
2196 return REG_BR_PROB_BASE;
2199 base = get_base_address (op);
2200 if (base)
2202 ao_ref refd;
2203 int max;
2204 struct record_modified_bb_info info;
2205 bitmap_iterator bi;
2206 unsigned index;
2207 tree init = ctor_for_folding (base);
2209 if (init != error_mark_node)
2210 return 0;
2211 if (!bb->frequency)
2212 return REG_BR_PROB_BASE;
2213 ao_ref_init (&refd, op);
2214 info.stmt = stmt;
2215 info.bb_set = BITMAP_ALLOC (NULL);
2216 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2217 NULL);
2218 if (bitmap_bit_p (info.bb_set, bb->index))
2220 BITMAP_FREE (info.bb_set);
2221 return REG_BR_PROB_BASE;
2224 /* Assume that every memory is initialized at entry.
2225 TODO: Can we easilly determine if value is always defined
2226 and thus we may skip entry block? */
2227 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
2228 max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2229 else
2230 max = 1;
2232 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2233 max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
2235 BITMAP_FREE (info.bb_set);
2236 if (max < bb->frequency)
2237 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2238 else
2239 return REG_BR_PROB_BASE;
2241 return REG_BR_PROB_BASE;
2244 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2245 sub-graph and if the predicate the condition depends on is known. If so,
2246 return true and store the pointer the predicate in *P. */
2248 static bool
2249 phi_result_unknown_predicate (struct ipa_node_params *info,
2250 inline_summary *summary, basic_block bb,
2251 struct predicate *p,
2252 vec<predicate_t> nonconstant_names)
2254 edge e;
2255 edge_iterator ei;
2256 basic_block first_bb = NULL;
2257 gimple stmt;
2259 if (single_pred_p (bb))
2261 *p = false_predicate ();
2262 return true;
2265 FOR_EACH_EDGE (e, ei, bb->preds)
2267 if (single_succ_p (e->src))
2269 if (!single_pred_p (e->src))
2270 return false;
2271 if (!first_bb)
2272 first_bb = single_pred (e->src);
2273 else if (single_pred (e->src) != first_bb)
2274 return false;
2276 else
2278 if (!first_bb)
2279 first_bb = e->src;
2280 else if (e->src != first_bb)
2281 return false;
2285 if (!first_bb)
2286 return false;
2288 stmt = last_stmt (first_bb);
2289 if (!stmt
2290 || gimple_code (stmt) != GIMPLE_COND
2291 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2292 return false;
2294 *p = will_be_nonconstant_expr_predicate (info, summary,
2295 gimple_cond_lhs (stmt),
2296 nonconstant_names);
2297 if (true_predicate_p (p))
2298 return false;
2299 else
2300 return true;
2303 /* Given a PHI statement in a function described by inline properties SUMMARY
2304 and *P being the predicate describing whether the selected PHI argument is
2305 known, store a predicate for the result of the PHI statement into
2306 NONCONSTANT_NAMES, if possible. */
2308 static void
2309 predicate_for_phi_result (struct inline_summary *summary, gphi *phi,
2310 struct predicate *p,
2311 vec<predicate_t> nonconstant_names)
2313 unsigned i;
2315 for (i = 0; i < gimple_phi_num_args (phi); i++)
2317 tree arg = gimple_phi_arg (phi, i)->def;
2318 if (!is_gimple_min_invariant (arg))
2320 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2321 *p = or_predicates (summary->conds, p,
2322 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2323 if (true_predicate_p (p))
2324 return;
2328 if (dump_file && (dump_flags & TDF_DETAILS))
2330 fprintf (dump_file, "\t\tphi predicate: ");
2331 dump_predicate (dump_file, summary->conds, p);
2333 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2336 /* Return predicate specifying when array index in access OP becomes non-constant. */
2338 static struct predicate
2339 array_index_predicate (inline_summary *info,
2340 vec< predicate_t> nonconstant_names, tree op)
2342 struct predicate p = false_predicate ();
2343 while (handled_component_p (op))
2345 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2347 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2348 p = or_predicates (info->conds, &p,
2349 &nonconstant_names[SSA_NAME_VERSION
2350 (TREE_OPERAND (op, 1))]);
2352 op = TREE_OPERAND (op, 0);
2354 return p;
2357 /* For a typical usage of __builtin_expect (a<b, 1), we
2358 may introduce an extra relation stmt:
2359 With the builtin, we have
2360 t1 = a <= b;
2361 t2 = (long int) t1;
2362 t3 = __builtin_expect (t2, 1);
2363 if (t3 != 0)
2364 goto ...
2365 Without the builtin, we have
2366 if (a<=b)
2367 goto...
2368 This affects the size/time estimation and may have
2369 an impact on the earlier inlining.
2370 Here find this pattern and fix it up later. */
2372 static gimple
2373 find_foldable_builtin_expect (basic_block bb)
2375 gimple_stmt_iterator bsi;
2377 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2379 gimple stmt = gsi_stmt (bsi);
2380 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2381 || (is_gimple_call (stmt)
2382 && gimple_call_internal_p (stmt)
2383 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
2385 tree var = gimple_call_lhs (stmt);
2386 tree arg = gimple_call_arg (stmt, 0);
2387 use_operand_p use_p;
2388 gimple use_stmt;
2389 bool match = false;
2390 bool done = false;
2392 if (!var || !arg)
2393 continue;
2394 gcc_assert (TREE_CODE (var) == SSA_NAME);
2396 while (TREE_CODE (arg) == SSA_NAME)
2398 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2399 if (!is_gimple_assign (stmt_tmp))
2400 break;
2401 switch (gimple_assign_rhs_code (stmt_tmp))
2403 case LT_EXPR:
2404 case LE_EXPR:
2405 case GT_EXPR:
2406 case GE_EXPR:
2407 case EQ_EXPR:
2408 case NE_EXPR:
2409 match = true;
2410 done = true;
2411 break;
2412 CASE_CONVERT:
2413 break;
2414 default:
2415 done = true;
2416 break;
2418 if (done)
2419 break;
2420 arg = gimple_assign_rhs1 (stmt_tmp);
2423 if (match && single_imm_use (var, &use_p, &use_stmt)
2424 && gimple_code (use_stmt) == GIMPLE_COND)
2425 return use_stmt;
2428 return NULL;
2431 /* Return true when the basic blocks contains only clobbers followed by RESX.
2432 Such BBs are kept around to make removal of dead stores possible with
2433 presence of EH and will be optimized out by optimize_clobbers later in the
2434 game.
2436 NEED_EH is used to recurse in case the clobber has non-EH predecestors
2437 that can be clobber only, too.. When it is false, the RESX is not necessary
2438 on the end of basic block. */
2440 static bool
2441 clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2443 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2444 edge_iterator ei;
2445 edge e;
2447 if (need_eh)
2449 if (gsi_end_p (gsi))
2450 return false;
2451 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2452 return false;
2453 gsi_prev (&gsi);
2455 else if (!single_succ_p (bb))
2456 return false;
2458 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2460 gimple stmt = gsi_stmt (gsi);
2461 if (is_gimple_debug (stmt))
2462 continue;
2463 if (gimple_clobber_p (stmt))
2464 continue;
2465 if (gimple_code (stmt) == GIMPLE_LABEL)
2466 break;
2467 return false;
2470 /* See if all predecestors are either throws or clobber only BBs. */
2471 FOR_EACH_EDGE (e, ei, bb->preds)
2472 if (!(e->flags & EDGE_EH)
2473 && !clobber_only_eh_bb_p (e->src, false))
2474 return false;
2476 return true;
2479 /* Compute function body size parameters for NODE.
2480 When EARLY is true, we compute only simple summaries without
2481 non-trivial predicates to drive the early inliner. */
2483 static void
2484 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2486 gcov_type time = 0;
2487 /* Estimate static overhead for function prologue/epilogue and alignment. */
2488 int size = 2;
2489 /* Benefits are scaled by probability of elimination that is in range
2490 <0,2>. */
2491 basic_block bb;
2492 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2493 int freq;
2494 struct inline_summary *info = inline_summaries->get (node);
2495 struct predicate bb_predicate;
2496 struct ipa_node_params *parms_info = NULL;
2497 vec<predicate_t> nonconstant_names = vNULL;
2498 int nblocks, n;
2499 int *order;
2500 predicate array_index = true_predicate ();
2501 gimple fix_builtin_expect_stmt;
2503 info->conds = NULL;
2504 info->entry = NULL;
2506 /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2507 so we can produce proper inline hints.
2509 When optimizing and analyzing for early inliner, initialize node params
2510 so we can produce correct BB predicates. */
2512 if (opt_for_fn (node->decl, optimize))
2514 calculate_dominance_info (CDI_DOMINATORS);
2515 if (!early)
2516 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2517 else
2519 ipa_check_create_node_params ();
2520 ipa_initialize_node_params (node);
2523 if (ipa_node_params_sum)
2525 parms_info = IPA_NODE_REF (node);
2526 nonconstant_names.safe_grow_cleared
2527 (SSANAMES (my_function)->length ());
2531 if (dump_file)
2532 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2533 node->name ());
2535 /* When we run into maximal number of entries, we assign everything to the
2536 constant truth case. Be sure to have it in list. */
2537 bb_predicate = true_predicate ();
2538 account_size_time (info, 0, 0, &bb_predicate);
2540 bb_predicate = not_inlined_predicate ();
2541 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2543 gcc_assert (my_function && my_function->cfg);
2544 if (parms_info)
2545 compute_bb_predicates (node, parms_info, info);
2546 gcc_assert (cfun == my_function);
2547 order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2548 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2549 for (n = 0; n < nblocks; n++)
2551 bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2552 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2553 if (clobber_only_eh_bb_p (bb))
2555 if (dump_file && (dump_flags & TDF_DETAILS))
2556 fprintf (dump_file, "\n Ignoring BB %i;"
2557 " it will be optimized away by cleanup_clobbers\n",
2558 bb->index);
2559 continue;
2562 /* TODO: Obviously predicates can be propagated down across CFG. */
2563 if (parms_info)
2565 if (bb->aux)
2566 bb_predicate = *(struct predicate *) bb->aux;
2567 else
2568 bb_predicate = false_predicate ();
2570 else
2571 bb_predicate = true_predicate ();
2573 if (dump_file && (dump_flags & TDF_DETAILS))
2575 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2576 dump_predicate (dump_file, info->conds, &bb_predicate);
2579 if (parms_info && nonconstant_names.exists ())
2581 struct predicate phi_predicate;
2582 bool first_phi = true;
2584 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2585 gsi_next (&bsi))
2587 if (first_phi
2588 && !phi_result_unknown_predicate (parms_info, info, bb,
2589 &phi_predicate,
2590 nonconstant_names))
2591 break;
2592 first_phi = false;
2593 if (dump_file && (dump_flags & TDF_DETAILS))
2595 fprintf (dump_file, " ");
2596 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2598 predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2599 nonconstant_names);
2603 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2605 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2606 gsi_next (&bsi))
2608 gimple stmt = gsi_stmt (bsi);
2609 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2610 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2611 int prob;
2612 struct predicate will_be_nonconstant;
2614 /* This relation stmt should be folded after we remove
2615 buildin_expect call. Adjust the cost here. */
2616 if (stmt == fix_builtin_expect_stmt)
2618 this_size--;
2619 this_time--;
2622 if (dump_file && (dump_flags & TDF_DETAILS))
2624 fprintf (dump_file, " ");
2625 print_gimple_stmt (dump_file, stmt, 0, 0);
2626 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2627 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2628 this_time);
2631 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2633 struct predicate this_array_index;
2634 this_array_index =
2635 array_index_predicate (info, nonconstant_names,
2636 gimple_assign_rhs1 (stmt));
2637 if (!false_predicate_p (&this_array_index))
2638 array_index =
2639 and_predicates (info->conds, &array_index,
2640 &this_array_index);
2642 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2644 struct predicate this_array_index;
2645 this_array_index =
2646 array_index_predicate (info, nonconstant_names,
2647 gimple_get_lhs (stmt));
2648 if (!false_predicate_p (&this_array_index))
2649 array_index =
2650 and_predicates (info->conds, &array_index,
2651 &this_array_index);
2655 if (is_gimple_call (stmt)
2656 && !gimple_call_internal_p (stmt))
2658 struct cgraph_edge *edge = node->get_edge (stmt);
2659 struct inline_edge_summary *es = inline_edge_summary (edge);
2661 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2662 resolved as constant. We however don't want to optimize
2663 out the cgraph edges. */
2664 if (nonconstant_names.exists ()
2665 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2666 && gimple_call_lhs (stmt)
2667 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2669 struct predicate false_p = false_predicate ();
2670 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2671 = false_p;
2673 if (ipa_node_params_sum)
2675 int count = gimple_call_num_args (stmt);
2676 int i;
2678 if (count)
2679 es->param.safe_grow_cleared (count);
2680 for (i = 0; i < count; i++)
2682 int prob = param_change_prob (stmt, i);
2683 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2684 es->param[i].change_prob = prob;
2688 es->call_stmt_size = this_size;
2689 es->call_stmt_time = this_time;
2690 es->loop_depth = bb_loop_depth (bb);
2691 edge_set_predicate (edge, &bb_predicate);
2694 /* TODO: When conditional jump or swithc is known to be constant, but
2695 we did not translate it into the predicates, we really can account
2696 just maximum of the possible paths. */
2697 if (parms_info)
2698 will_be_nonconstant
2699 = will_be_nonconstant_predicate (parms_info, info,
2700 stmt, nonconstant_names);
2701 if (this_time || this_size)
2703 struct predicate p;
2705 this_time *= freq;
2707 prob = eliminated_by_inlining_prob (stmt);
2708 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2709 fprintf (dump_file,
2710 "\t\t50%% will be eliminated by inlining\n");
2711 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2712 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2714 if (parms_info)
2715 p = and_predicates (info->conds, &bb_predicate,
2716 &will_be_nonconstant);
2717 else
2718 p = true_predicate ();
2720 if (!false_predicate_p (&p)
2721 || (is_gimple_call (stmt)
2722 && !false_predicate_p (&bb_predicate)))
2724 time += this_time;
2725 size += this_size;
2726 if (time > MAX_TIME * INLINE_TIME_SCALE)
2727 time = MAX_TIME * INLINE_TIME_SCALE;
2730 /* We account everything but the calls. Calls have their own
2731 size/time info attached to cgraph edges. This is necessary
2732 in order to make the cost disappear after inlining. */
2733 if (!is_gimple_call (stmt))
2735 if (prob)
2737 struct predicate ip = not_inlined_predicate ();
2738 ip = and_predicates (info->conds, &ip, &p);
2739 account_size_time (info, this_size * prob,
2740 this_time * prob, &ip);
2742 if (prob != 2)
2743 account_size_time (info, this_size * (2 - prob),
2744 this_time * (2 - prob), &p);
2747 gcc_assert (time >= 0);
2748 gcc_assert (size >= 0);
2752 set_hint_predicate (&inline_summaries->get (node)->array_index, array_index);
2753 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2754 if (time > MAX_TIME)
2755 time = MAX_TIME;
2756 free (order);
2758 if (nonconstant_names.exists () && !early)
2760 struct loop *loop;
2761 predicate loop_iterations = true_predicate ();
2762 predicate loop_stride = true_predicate ();
2764 if (dump_file && (dump_flags & TDF_DETAILS))
2765 flow_loops_dump (dump_file, NULL, 0);
2766 scev_initialize ();
2767 FOR_EACH_LOOP (loop, 0)
2769 vec<edge> exits;
2770 edge ex;
2771 unsigned int j, i;
2772 struct tree_niter_desc niter_desc;
2773 basic_block *body = get_loop_body (loop);
2774 bb_predicate = *(struct predicate *) loop->header->aux;
2776 exits = get_loop_exit_edges (loop);
2777 FOR_EACH_VEC_ELT (exits, j, ex)
2778 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2779 && !is_gimple_min_invariant (niter_desc.niter))
2781 predicate will_be_nonconstant
2782 = will_be_nonconstant_expr_predicate (parms_info, info,
2783 niter_desc.niter,
2784 nonconstant_names);
2785 if (!true_predicate_p (&will_be_nonconstant))
2786 will_be_nonconstant = and_predicates (info->conds,
2787 &bb_predicate,
2788 &will_be_nonconstant);
2789 if (!true_predicate_p (&will_be_nonconstant)
2790 && !false_predicate_p (&will_be_nonconstant))
2791 /* This is slightly inprecise. We may want to represent each
2792 loop with independent predicate. */
2793 loop_iterations =
2794 and_predicates (info->conds, &loop_iterations,
2795 &will_be_nonconstant);
2797 exits.release ();
2799 for (i = 0; i < loop->num_nodes; i++)
2801 gimple_stmt_iterator gsi;
2802 bb_predicate = *(struct predicate *) body[i]->aux;
2803 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2804 gsi_next (&gsi))
2806 gimple stmt = gsi_stmt (gsi);
2807 affine_iv iv;
2808 ssa_op_iter iter;
2809 tree use;
2811 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2813 predicate will_be_nonconstant;
2815 if (!simple_iv
2816 (loop, loop_containing_stmt (stmt), use, &iv, true)
2817 || is_gimple_min_invariant (iv.step))
2818 continue;
2819 will_be_nonconstant
2820 = will_be_nonconstant_expr_predicate (parms_info, info,
2821 iv.step,
2822 nonconstant_names);
2823 if (!true_predicate_p (&will_be_nonconstant))
2824 will_be_nonconstant
2825 = and_predicates (info->conds,
2826 &bb_predicate,
2827 &will_be_nonconstant);
2828 if (!true_predicate_p (&will_be_nonconstant)
2829 && !false_predicate_p (&will_be_nonconstant))
2830 /* This is slightly inprecise. We may want to represent
2831 each loop with independent predicate. */
2832 loop_stride =
2833 and_predicates (info->conds, &loop_stride,
2834 &will_be_nonconstant);
2838 free (body);
2840 set_hint_predicate (&inline_summaries->get (node)->loop_iterations,
2841 loop_iterations);
2842 set_hint_predicate (&inline_summaries->get (node)->loop_stride, loop_stride);
2843 scev_finalize ();
2845 FOR_ALL_BB_FN (bb, my_function)
2847 edge e;
2848 edge_iterator ei;
2850 if (bb->aux)
2851 edge_predicate_pool.remove ((predicate *)bb->aux);
2852 bb->aux = NULL;
2853 FOR_EACH_EDGE (e, ei, bb->succs)
2855 if (e->aux)
2856 edge_predicate_pool.remove ((predicate *) e->aux);
2857 e->aux = NULL;
2860 inline_summaries->get (node)->self_time = time;
2861 inline_summaries->get (node)->self_size = size;
2862 nonconstant_names.release ();
2863 if (opt_for_fn (node->decl, optimize))
2865 if (!early)
2866 loop_optimizer_finalize ();
2867 else if (!ipa_edge_args_vector)
2868 ipa_free_all_node_params ();
2869 free_dominance_info (CDI_DOMINATORS);
2871 if (dump_file)
2873 fprintf (dump_file, "\n");
2874 dump_inline_summary (dump_file, node);
2879 /* Compute parameters of functions used by inliner.
2880 EARLY is true when we compute parameters for the early inliner */
2882 void
2883 compute_inline_parameters (struct cgraph_node *node, bool early)
2885 HOST_WIDE_INT self_stack_size;
2886 struct cgraph_edge *e;
2887 struct inline_summary *info;
2889 gcc_assert (!node->global.inlined_to);
2891 inline_summary_alloc ();
2893 info = inline_summaries->get (node);
2894 reset_inline_summary (node, info);
2896 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2897 Once this happen, we will need to more curefully predict call
2898 statement size. */
2899 if (node->thunk.thunk_p)
2901 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2902 struct predicate t = true_predicate ();
2904 info->inlinable = 0;
2905 node->callees->call_stmt_cannot_inline_p = true;
2906 node->local.can_change_signature = false;
2907 es->call_stmt_time = 1;
2908 es->call_stmt_size = 1;
2909 account_size_time (info, 0, 0, &t);
2910 return;
2913 /* Even is_gimple_min_invariant rely on current_function_decl. */
2914 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2916 /* Estimate the stack size for the function if we're optimizing. */
2917 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2918 info->estimated_self_stack_size = self_stack_size;
2919 info->estimated_stack_size = self_stack_size;
2920 info->stack_frame_offset = 0;
2922 /* Can this function be inlined at all? */
2923 if (!opt_for_fn (node->decl, optimize)
2924 && !lookup_attribute ("always_inline",
2925 DECL_ATTRIBUTES (node->decl)))
2926 info->inlinable = false;
2927 else
2928 info->inlinable = tree_inlinable_function_p (node->decl);
2930 info->contains_cilk_spawn = fn_contains_cilk_spawn_p (cfun);
2932 /* Type attributes can use parameter indices to describe them. */
2933 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2934 node->local.can_change_signature = false;
2935 else
2937 /* Otherwise, inlinable functions always can change signature. */
2938 if (info->inlinable)
2939 node->local.can_change_signature = true;
2940 else
2942 /* Functions calling builtin_apply can not change signature. */
2943 for (e = node->callees; e; e = e->next_callee)
2945 tree cdecl = e->callee->decl;
2946 if (DECL_BUILT_IN (cdecl)
2947 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2948 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2949 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2950 break;
2952 node->local.can_change_signature = !e;
2955 estimate_function_body_sizes (node, early);
2957 for (e = node->callees; e; e = e->next_callee)
2958 if (e->callee->comdat_local_p ())
2959 break;
2960 node->calls_comdat_local = (e != NULL);
2962 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2963 info->time = info->self_time;
2964 info->size = info->self_size;
2965 info->stack_frame_offset = 0;
2966 info->estimated_stack_size = info->estimated_self_stack_size;
2967 #ifdef ENABLE_CHECKING
2968 inline_update_overall_summary (node);
2969 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2970 #endif
2972 pop_cfun ();
2976 /* Compute parameters of functions used by inliner using
2977 current_function_decl. */
2979 static unsigned int
2980 compute_inline_parameters_for_current (void)
2982 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
2983 return 0;
2986 namespace {
2988 const pass_data pass_data_inline_parameters =
2990 GIMPLE_PASS, /* type */
2991 "inline_param", /* name */
2992 OPTGROUP_INLINE, /* optinfo_flags */
2993 TV_INLINE_PARAMETERS, /* tv_id */
2994 0, /* properties_required */
2995 0, /* properties_provided */
2996 0, /* properties_destroyed */
2997 0, /* todo_flags_start */
2998 0, /* todo_flags_finish */
3001 class pass_inline_parameters : public gimple_opt_pass
3003 public:
3004 pass_inline_parameters (gcc::context *ctxt)
3005 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
3008 /* opt_pass methods: */
3009 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
3010 virtual unsigned int execute (function *)
3012 return compute_inline_parameters_for_current ();
3015 }; // class pass_inline_parameters
3017 } // anon namespace
3019 gimple_opt_pass *
3020 make_pass_inline_parameters (gcc::context *ctxt)
3022 return new pass_inline_parameters (ctxt);
3026 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS,
3027 KNOWN_CONTEXTS and KNOWN_AGGS. */
3029 static bool
3030 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3031 int *size, int *time,
3032 vec<tree> known_vals,
3033 vec<ipa_polymorphic_call_context> known_contexts,
3034 vec<ipa_agg_jump_function_p> known_aggs)
3036 tree target;
3037 struct cgraph_node *callee;
3038 struct inline_summary *isummary;
3039 enum availability avail;
3040 bool speculative;
3042 if (!known_vals.exists () && !known_contexts.exists ())
3043 return false;
3044 if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3045 return false;
3047 target = ipa_get_indirect_edge_target (ie, known_vals, known_contexts,
3048 known_aggs, &speculative);
3049 if (!target || speculative)
3050 return false;
3052 /* Account for difference in cost between indirect and direct calls. */
3053 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3054 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3055 gcc_checking_assert (*time >= 0);
3056 gcc_checking_assert (*size >= 0);
3058 callee = cgraph_node::get (target);
3059 if (!callee || !callee->definition)
3060 return false;
3061 callee = callee->function_symbol (&avail);
3062 if (avail < AVAIL_AVAILABLE)
3063 return false;
3064 isummary = inline_summaries->get (callee);
3065 return isummary->inlinable;
3068 /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3069 handle edge E with probability PROB.
3070 Set HINTS if edge may be devirtualized.
3071 KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS describe context of the call
3072 site. */
3074 static inline void
3075 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3076 int *time,
3077 int prob,
3078 vec<tree> known_vals,
3079 vec<ipa_polymorphic_call_context> known_contexts,
3080 vec<ipa_agg_jump_function_p> known_aggs,
3081 inline_hints *hints)
3083 struct inline_edge_summary *es = inline_edge_summary (e);
3084 int call_size = es->call_stmt_size;
3085 int call_time = es->call_stmt_time;
3086 int cur_size;
3087 if (!e->callee
3088 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
3089 known_vals, known_contexts, known_aggs)
3090 && hints && e->maybe_hot_p ())
3091 *hints |= INLINE_HINT_indirect_call;
3092 cur_size = call_size * INLINE_SIZE_SCALE;
3093 *size += cur_size;
3094 if (min_size)
3095 *min_size += cur_size;
3096 *time += apply_probability ((gcov_type) call_time, prob)
3097 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
3098 if (*time > MAX_TIME * INLINE_TIME_SCALE)
3099 *time = MAX_TIME * INLINE_TIME_SCALE;
3104 /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3105 calls in NODE. POSSIBLE_TRUTHS, KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
3106 describe context of the call site. */
3108 static void
3109 estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3110 int *min_size, int *time,
3111 inline_hints *hints,
3112 clause_t possible_truths,
3113 vec<tree> known_vals,
3114 vec<ipa_polymorphic_call_context> known_contexts,
3115 vec<ipa_agg_jump_function_p> known_aggs)
3117 struct cgraph_edge *e;
3118 for (e = node->callees; e; e = e->next_callee)
3120 struct inline_edge_summary *es = inline_edge_summary (e);
3122 /* Do not care about zero sized builtins. */
3123 if (e->inline_failed && !es->call_stmt_size)
3125 gcc_checking_assert (!es->call_stmt_time);
3126 continue;
3128 if (!es->predicate
3129 || evaluate_predicate (es->predicate, possible_truths))
3131 if (e->inline_failed)
3133 /* Predicates of calls shall not use NOT_CHANGED codes,
3134 sowe do not need to compute probabilities. */
3135 estimate_edge_size_and_time (e, size,
3136 es->predicate ? NULL : min_size,
3137 time, REG_BR_PROB_BASE,
3138 known_vals, known_contexts,
3139 known_aggs, hints);
3141 else
3142 estimate_calls_size_and_time (e->callee, size, min_size, time,
3143 hints,
3144 possible_truths,
3145 known_vals, known_contexts,
3146 known_aggs);
3149 for (e = node->indirect_calls; e; e = e->next_callee)
3151 struct inline_edge_summary *es = inline_edge_summary (e);
3152 if (!es->predicate
3153 || evaluate_predicate (es->predicate, possible_truths))
3154 estimate_edge_size_and_time (e, size,
3155 es->predicate ? NULL : min_size,
3156 time, REG_BR_PROB_BASE,
3157 known_vals, known_contexts, known_aggs,
3158 hints);
3163 /* Estimate size and time needed to execute NODE assuming
3164 POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
3165 information about NODE's arguments. If non-NULL use also probability
3166 information present in INLINE_PARAM_SUMMARY vector.
3167 Additionally detemine hints determined by the context. Finally compute
3168 minimal size needed for the call that is independent on the call context and
3169 can be used for fast estimates. Return the values in RET_SIZE,
3170 RET_MIN_SIZE, RET_TIME and RET_HINTS. */
3172 static void
3173 estimate_node_size_and_time (struct cgraph_node *node,
3174 clause_t possible_truths,
3175 vec<tree> known_vals,
3176 vec<ipa_polymorphic_call_context> known_contexts,
3177 vec<ipa_agg_jump_function_p> known_aggs,
3178 int *ret_size, int *ret_min_size, int *ret_time,
3179 inline_hints *ret_hints,
3180 vec<inline_param_summary>
3181 inline_param_summary)
3183 struct inline_summary *info = inline_summaries->get (node);
3184 size_time_entry *e;
3185 int size = 0;
3186 int time = 0;
3187 int min_size = 0;
3188 inline_hints hints = 0;
3189 int i;
3191 if (dump_file && (dump_flags & TDF_DETAILS))
3193 bool found = false;
3194 fprintf (dump_file, " Estimating body: %s/%i\n"
3195 " Known to be false: ", node->name (),
3196 node->order);
3198 for (i = predicate_not_inlined_condition;
3199 i < (predicate_first_dynamic_condition
3200 + (int) vec_safe_length (info->conds)); i++)
3201 if (!(possible_truths & (1 << i)))
3203 if (found)
3204 fprintf (dump_file, ", ");
3205 found = true;
3206 dump_condition (dump_file, info->conds, i);
3210 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3211 if (evaluate_predicate (&e->predicate, possible_truths))
3213 size += e->size;
3214 gcc_checking_assert (e->time >= 0);
3215 gcc_checking_assert (time >= 0);
3216 if (!inline_param_summary.exists ())
3217 time += e->time;
3218 else
3220 int prob = predicate_probability (info->conds,
3221 &e->predicate,
3222 possible_truths,
3223 inline_param_summary);
3224 gcc_checking_assert (prob >= 0);
3225 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3226 time += apply_probability ((gcov_type) e->time, prob);
3228 if (time > MAX_TIME * INLINE_TIME_SCALE)
3229 time = MAX_TIME * INLINE_TIME_SCALE;
3230 gcc_checking_assert (time >= 0);
3233 gcc_checking_assert (true_predicate_p (&(*info->entry)[0].predicate));
3234 min_size = (*info->entry)[0].size;
3235 gcc_checking_assert (size >= 0);
3236 gcc_checking_assert (time >= 0);
3238 if (info->loop_iterations
3239 && !evaluate_predicate (info->loop_iterations, possible_truths))
3240 hints |= INLINE_HINT_loop_iterations;
3241 if (info->loop_stride
3242 && !evaluate_predicate (info->loop_stride, possible_truths))
3243 hints |= INLINE_HINT_loop_stride;
3244 if (info->array_index
3245 && !evaluate_predicate (info->array_index, possible_truths))
3246 hints |= INLINE_HINT_array_index;
3247 if (info->scc_no)
3248 hints |= INLINE_HINT_in_scc;
3249 if (DECL_DECLARED_INLINE_P (node->decl))
3250 hints |= INLINE_HINT_declared_inline;
3252 estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
3253 known_vals, known_contexts, known_aggs);
3254 gcc_checking_assert (size >= 0);
3255 gcc_checking_assert (time >= 0);
3256 time = RDIV (time, INLINE_TIME_SCALE);
3257 size = RDIV (size, INLINE_SIZE_SCALE);
3258 min_size = RDIV (min_size, INLINE_SIZE_SCALE);
3260 if (dump_file && (dump_flags & TDF_DETAILS))
3261 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3262 if (ret_time)
3263 *ret_time = time;
3264 if (ret_size)
3265 *ret_size = size;
3266 if (ret_min_size)
3267 *ret_min_size = min_size;
3268 if (ret_hints)
3269 *ret_hints = hints;
3270 return;
3274 /* Estimate size and time needed to execute callee of EDGE assuming that
3275 parameters known to be constant at caller of EDGE are propagated.
3276 KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
3277 and types for parameters. */
3279 void
3280 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3281 vec<tree> known_vals,
3282 vec<ipa_polymorphic_call_context>
3283 known_contexts,
3284 vec<ipa_agg_jump_function_p> known_aggs,
3285 int *ret_size, int *ret_time,
3286 inline_hints *hints)
3288 clause_t clause;
3290 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3291 known_aggs);
3292 estimate_node_size_and_time (node, clause, known_vals, known_contexts,
3293 known_aggs, ret_size, NULL, ret_time, hints, vNULL);
3296 /* Translate all conditions from callee representation into caller
3297 representation and symbolically evaluate predicate P into new predicate.
3299 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3300 is summary of function predicate P is from. OPERAND_MAP is array giving
3301 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3302 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3303 predicate under which callee is executed. OFFSET_MAP is an array of of
3304 offsets that need to be added to conditions, negative offset means that
3305 conditions relying on values passed by reference have to be discarded
3306 because they might not be preserved (and should be considered offset zero
3307 for other purposes). */
3309 static struct predicate
3310 remap_predicate (struct inline_summary *info,
3311 struct inline_summary *callee_info,
3312 struct predicate *p,
3313 vec<int> operand_map,
3314 vec<int> offset_map,
3315 clause_t possible_truths, struct predicate *toplev_predicate)
3317 int i;
3318 struct predicate out = true_predicate ();
3320 /* True predicate is easy. */
3321 if (true_predicate_p (p))
3322 return *toplev_predicate;
3323 for (i = 0; p->clause[i]; i++)
3325 clause_t clause = p->clause[i];
3326 int cond;
3327 struct predicate clause_predicate = false_predicate ();
3329 gcc_assert (i < MAX_CLAUSES);
3331 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3332 /* Do we have condition we can't disprove? */
3333 if (clause & possible_truths & (1 << cond))
3335 struct predicate cond_predicate;
3336 /* Work out if the condition can translate to predicate in the
3337 inlined function. */
3338 if (cond >= predicate_first_dynamic_condition)
3340 struct condition *c;
3342 c = &(*callee_info->conds)[cond
3344 predicate_first_dynamic_condition];
3345 /* See if we can remap condition operand to caller's operand.
3346 Otherwise give up. */
3347 if (!operand_map.exists ()
3348 || (int) operand_map.length () <= c->operand_num
3349 || operand_map[c->operand_num] == -1
3350 /* TODO: For non-aggregate conditions, adding an offset is
3351 basically an arithmetic jump function processing which
3352 we should support in future. */
3353 || ((!c->agg_contents || !c->by_ref)
3354 && offset_map[c->operand_num] > 0)
3355 || (c->agg_contents && c->by_ref
3356 && offset_map[c->operand_num] < 0))
3357 cond_predicate = true_predicate ();
3358 else
3360 struct agg_position_info ap;
3361 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3362 if (offset_delta < 0)
3364 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3365 offset_delta = 0;
3367 gcc_assert (!c->agg_contents
3368 || c->by_ref || offset_delta == 0);
3369 ap.offset = c->offset + offset_delta;
3370 ap.agg_contents = c->agg_contents;
3371 ap.by_ref = c->by_ref;
3372 cond_predicate = add_condition (info,
3373 operand_map[c->operand_num],
3374 &ap, c->code, c->val);
3377 /* Fixed conditions remains same, construct single
3378 condition predicate. */
3379 else
3381 cond_predicate.clause[0] = 1 << cond;
3382 cond_predicate.clause[1] = 0;
3384 clause_predicate = or_predicates (info->conds, &clause_predicate,
3385 &cond_predicate);
3387 out = and_predicates (info->conds, &out, &clause_predicate);
3389 return and_predicates (info->conds, &out, toplev_predicate);
3393 /* Update summary information of inline clones after inlining.
3394 Compute peak stack usage. */
3396 static void
3397 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3399 struct cgraph_edge *e;
3400 struct inline_summary *callee_info = inline_summaries->get (node);
3401 struct inline_summary *caller_info = inline_summaries->get (node->callers->caller);
3402 HOST_WIDE_INT peak;
3404 callee_info->stack_frame_offset
3405 = caller_info->stack_frame_offset
3406 + caller_info->estimated_self_stack_size;
3407 peak = callee_info->stack_frame_offset
3408 + callee_info->estimated_self_stack_size;
3409 if (inline_summaries->get (node->global.inlined_to)->estimated_stack_size < peak)
3410 inline_summaries->get (node->global.inlined_to)->estimated_stack_size = peak;
3411 ipa_propagate_frequency (node);
3412 for (e = node->callees; e; e = e->next_callee)
3414 if (!e->inline_failed)
3415 inline_update_callee_summaries (e->callee, depth);
3416 inline_edge_summary (e)->loop_depth += depth;
3418 for (e = node->indirect_calls; e; e = e->next_callee)
3419 inline_edge_summary (e)->loop_depth += depth;
3422 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3423 When functoin A is inlined in B and A calls C with parameter that
3424 changes with probability PROB1 and C is known to be passthroug
3425 of argument if B that change with probability PROB2, the probability
3426 of change is now PROB1*PROB2. */
3428 static void
3429 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3430 struct cgraph_edge *edge)
3432 if (ipa_node_params_sum)
3434 int i;
3435 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3436 struct inline_edge_summary *es = inline_edge_summary (edge);
3437 struct inline_edge_summary *inlined_es
3438 = inline_edge_summary (inlined_edge);
3440 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3442 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3443 if (jfunc->type == IPA_JF_PASS_THROUGH
3444 && (ipa_get_jf_pass_through_formal_id (jfunc)
3445 < (int) inlined_es->param.length ()))
3447 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3448 int prob1 = es->param[i].change_prob;
3449 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3450 int prob = combine_probabilities (prob1, prob2);
3452 if (prob1 && prob2 && !prob)
3453 prob = 1;
3455 es->param[i].change_prob = prob;
3461 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3463 Remap predicates of callees of NODE. Rest of arguments match
3464 remap_predicate.
3466 Also update change probabilities. */
3468 static void
3469 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3470 struct cgraph_node *node,
3471 struct inline_summary *info,
3472 struct inline_summary *callee_info,
3473 vec<int> operand_map,
3474 vec<int> offset_map,
3475 clause_t possible_truths,
3476 struct predicate *toplev_predicate)
3478 struct cgraph_edge *e, *next;
3479 for (e = node->callees; e; e = next)
3481 struct inline_edge_summary *es = inline_edge_summary (e);
3482 struct predicate p;
3483 next = e->next_callee;
3485 if (e->inline_failed)
3487 remap_edge_change_prob (inlined_edge, e);
3489 if (es->predicate)
3491 p = remap_predicate (info, callee_info,
3492 es->predicate, operand_map, offset_map,
3493 possible_truths, toplev_predicate);
3494 edge_set_predicate (e, &p);
3496 else
3497 edge_set_predicate (e, toplev_predicate);
3499 else
3500 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3501 operand_map, offset_map, possible_truths,
3502 toplev_predicate);
3504 for (e = node->indirect_calls; e; e = next)
3506 struct inline_edge_summary *es = inline_edge_summary (e);
3507 struct predicate p;
3508 next = e->next_callee;
3510 remap_edge_change_prob (inlined_edge, e);
3511 if (es->predicate)
3513 p = remap_predicate (info, callee_info,
3514 es->predicate, operand_map, offset_map,
3515 possible_truths, toplev_predicate);
3516 edge_set_predicate (e, &p);
3518 else
3519 edge_set_predicate (e, toplev_predicate);
3523 /* Same as remap_predicate, but set result into hint *HINT. */
3525 static void
3526 remap_hint_predicate (struct inline_summary *info,
3527 struct inline_summary *callee_info,
3528 struct predicate **hint,
3529 vec<int> operand_map,
3530 vec<int> offset_map,
3531 clause_t possible_truths,
3532 struct predicate *toplev_predicate)
3534 predicate p;
3536 if (!*hint)
3537 return;
3538 p = remap_predicate (info, callee_info,
3539 *hint,
3540 operand_map, offset_map,
3541 possible_truths, toplev_predicate);
3542 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3544 if (!*hint)
3545 set_hint_predicate (hint, p);
3546 else
3547 **hint = and_predicates (info->conds, *hint, &p);
3551 /* We inlined EDGE. Update summary of the function we inlined into. */
3553 void
3554 inline_merge_summary (struct cgraph_edge *edge)
3556 struct inline_summary *callee_info = inline_summaries->get (edge->callee);
3557 struct cgraph_node *to = (edge->caller->global.inlined_to
3558 ? edge->caller->global.inlined_to : edge->caller);
3559 struct inline_summary *info = inline_summaries->get (to);
3560 clause_t clause = 0; /* not_inline is known to be false. */
3561 size_time_entry *e;
3562 vec<int> operand_map = vNULL;
3563 vec<int> offset_map = vNULL;
3564 int i;
3565 struct predicate toplev_predicate;
3566 struct predicate true_p = true_predicate ();
3567 struct inline_edge_summary *es = inline_edge_summary (edge);
3569 if (es->predicate)
3570 toplev_predicate = *es->predicate;
3571 else
3572 toplev_predicate = true_predicate ();
3574 if (callee_info->conds)
3575 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3576 if (ipa_node_params_sum && callee_info->conds)
3578 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3579 int count = ipa_get_cs_argument_count (args);
3580 int i;
3582 if (count)
3584 operand_map.safe_grow_cleared (count);
3585 offset_map.safe_grow_cleared (count);
3587 for (i = 0; i < count; i++)
3589 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3590 int map = -1;
3592 /* TODO: handle non-NOPs when merging. */
3593 if (jfunc->type == IPA_JF_PASS_THROUGH)
3595 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3596 map = ipa_get_jf_pass_through_formal_id (jfunc);
3597 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3598 offset_map[i] = -1;
3600 else if (jfunc->type == IPA_JF_ANCESTOR)
3602 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3603 if (offset >= 0 && offset < INT_MAX)
3605 map = ipa_get_jf_ancestor_formal_id (jfunc);
3606 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3607 offset = -1;
3608 offset_map[i] = offset;
3611 operand_map[i] = map;
3612 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3615 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3617 struct predicate p = remap_predicate (info, callee_info,
3618 &e->predicate, operand_map,
3619 offset_map, clause,
3620 &toplev_predicate);
3621 if (!false_predicate_p (&p))
3623 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3624 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3625 int prob = predicate_probability (callee_info->conds,
3626 &e->predicate,
3627 clause, es->param);
3628 add_time = apply_probability ((gcov_type) add_time, prob);
3629 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3630 add_time = MAX_TIME * INLINE_TIME_SCALE;
3631 if (prob != REG_BR_PROB_BASE
3632 && dump_file && (dump_flags & TDF_DETAILS))
3634 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3635 (double) prob / REG_BR_PROB_BASE);
3637 account_size_time (info, e->size, add_time, &p);
3640 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3641 offset_map, clause, &toplev_predicate);
3642 remap_hint_predicate (info, callee_info,
3643 &callee_info->loop_iterations,
3644 operand_map, offset_map, clause, &toplev_predicate);
3645 remap_hint_predicate (info, callee_info,
3646 &callee_info->loop_stride,
3647 operand_map, offset_map, clause, &toplev_predicate);
3648 remap_hint_predicate (info, callee_info,
3649 &callee_info->array_index,
3650 operand_map, offset_map, clause, &toplev_predicate);
3652 inline_update_callee_summaries (edge->callee,
3653 inline_edge_summary (edge)->loop_depth);
3655 /* We do not maintain predicates of inlined edges, free it. */
3656 edge_set_predicate (edge, &true_p);
3657 /* Similarly remove param summaries. */
3658 es->param.release ();
3659 operand_map.release ();
3660 offset_map.release ();
3663 /* For performance reasons inline_merge_summary is not updating overall size
3664 and time. Recompute it. */
3666 void
3667 inline_update_overall_summary (struct cgraph_node *node)
3669 struct inline_summary *info = inline_summaries->get (node);
3670 size_time_entry *e;
3671 int i;
3673 info->size = 0;
3674 info->time = 0;
3675 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3677 info->size += e->size, info->time += e->time;
3678 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3679 info->time = MAX_TIME * INLINE_TIME_SCALE;
3681 estimate_calls_size_and_time (node, &info->size, &info->min_size,
3682 &info->time, NULL,
3683 ~(clause_t) (1 << predicate_false_condition),
3684 vNULL, vNULL, vNULL);
3685 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3686 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3689 /* Return hints derrived from EDGE. */
3691 simple_edge_hints (struct cgraph_edge *edge)
3693 int hints = 0;
3694 struct cgraph_node *to = (edge->caller->global.inlined_to
3695 ? edge->caller->global.inlined_to : edge->caller);
3696 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
3697 if (inline_summaries->get (to)->scc_no
3698 && inline_summaries->get (to)->scc_no
3699 == inline_summaries->get (callee)->scc_no
3700 && !edge->recursive_p ())
3701 hints |= INLINE_HINT_same_scc;
3703 if (callee->lto_file_data && edge->caller->lto_file_data
3704 && edge->caller->lto_file_data != callee->lto_file_data
3705 && !callee->merged)
3706 hints |= INLINE_HINT_cross_module;
3708 return hints;
3711 /* Estimate the time cost for the caller when inlining EDGE.
3712 Only to be called via estimate_edge_time, that handles the
3713 caching mechanism.
3715 When caching, also update the cache entry. Compute both time and
3716 size, since we always need both metrics eventually. */
3719 do_estimate_edge_time (struct cgraph_edge *edge)
3721 int time;
3722 int size;
3723 inline_hints hints;
3724 struct cgraph_node *callee;
3725 clause_t clause;
3726 vec<tree> known_vals;
3727 vec<ipa_polymorphic_call_context> known_contexts;
3728 vec<ipa_agg_jump_function_p> known_aggs;
3729 struct inline_edge_summary *es = inline_edge_summary (edge);
3730 int min_size;
3732 callee = edge->callee->ultimate_alias_target ();
3734 gcc_checking_assert (edge->inline_failed);
3735 evaluate_properties_for_edge (edge, true,
3736 &clause, &known_vals, &known_contexts,
3737 &known_aggs);
3738 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3739 known_aggs, &size, &min_size, &time, &hints, es->param);
3741 /* When we have profile feedback, we can quite safely identify hot
3742 edges and for those we disable size limits. Don't do that when
3743 probability that caller will call the callee is low however, since it
3744 may hurt optimization of the caller's hot path. */
3745 if (edge->count && edge->maybe_hot_p ()
3746 && (edge->count * 2
3747 > (edge->caller->global.inlined_to
3748 ? edge->caller->global.inlined_to->count : edge->caller->count)))
3749 hints |= INLINE_HINT_known_hot;
3751 known_vals.release ();
3752 known_contexts.release ();
3753 known_aggs.release ();
3754 gcc_checking_assert (size >= 0);
3755 gcc_checking_assert (time >= 0);
3757 /* When caching, update the cache entry. */
3758 if (edge_growth_cache.exists ())
3760 inline_summaries->get (edge->callee)->min_size = min_size;
3761 if ((int) edge_growth_cache.length () <= edge->uid)
3762 edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
3763 edge_growth_cache[edge->uid].time = time + (time >= 0);
3765 edge_growth_cache[edge->uid].size = size + (size >= 0);
3766 hints |= simple_edge_hints (edge);
3767 edge_growth_cache[edge->uid].hints = hints + 1;
3769 return time;
3773 /* Return estimated callee growth after inlining EDGE.
3774 Only to be called via estimate_edge_size. */
3777 do_estimate_edge_size (struct cgraph_edge *edge)
3779 int size;
3780 struct cgraph_node *callee;
3781 clause_t clause;
3782 vec<tree> known_vals;
3783 vec<ipa_polymorphic_call_context> known_contexts;
3784 vec<ipa_agg_jump_function_p> known_aggs;
3786 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3788 if (edge_growth_cache.exists ())
3790 do_estimate_edge_time (edge);
3791 size = edge_growth_cache[edge->uid].size;
3792 gcc_checking_assert (size);
3793 return size - (size > 0);
3796 callee = edge->callee->ultimate_alias_target ();
3798 /* Early inliner runs without caching, go ahead and do the dirty work. */
3799 gcc_checking_assert (edge->inline_failed);
3800 evaluate_properties_for_edge (edge, true,
3801 &clause, &known_vals, &known_contexts,
3802 &known_aggs);
3803 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3804 known_aggs, &size, NULL, NULL, NULL, vNULL);
3805 known_vals.release ();
3806 known_contexts.release ();
3807 known_aggs.release ();
3808 return size;
3812 /* Estimate the growth of the caller when inlining EDGE.
3813 Only to be called via estimate_edge_size. */
3815 inline_hints
3816 do_estimate_edge_hints (struct cgraph_edge *edge)
3818 inline_hints hints;
3819 struct cgraph_node *callee;
3820 clause_t clause;
3821 vec<tree> known_vals;
3822 vec<ipa_polymorphic_call_context> known_contexts;
3823 vec<ipa_agg_jump_function_p> known_aggs;
3825 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3827 if (edge_growth_cache.exists ())
3829 do_estimate_edge_time (edge);
3830 hints = edge_growth_cache[edge->uid].hints;
3831 gcc_checking_assert (hints);
3832 return hints - 1;
3835 callee = edge->callee->ultimate_alias_target ();
3837 /* Early inliner runs without caching, go ahead and do the dirty work. */
3838 gcc_checking_assert (edge->inline_failed);
3839 evaluate_properties_for_edge (edge, true,
3840 &clause, &known_vals, &known_contexts,
3841 &known_aggs);
3842 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3843 known_aggs, NULL, NULL, NULL, &hints, vNULL);
3844 known_vals.release ();
3845 known_contexts.release ();
3846 known_aggs.release ();
3847 hints |= simple_edge_hints (edge);
3848 return hints;
3852 /* Estimate self time of the function NODE after inlining EDGE. */
3855 estimate_time_after_inlining (struct cgraph_node *node,
3856 struct cgraph_edge *edge)
3858 struct inline_edge_summary *es = inline_edge_summary (edge);
3859 if (!es->predicate || !false_predicate_p (es->predicate))
3861 gcov_type time =
3862 inline_summaries->get (node)->time + estimate_edge_time (edge);
3863 if (time < 0)
3864 time = 0;
3865 if (time > MAX_TIME)
3866 time = MAX_TIME;
3867 return time;
3869 return inline_summaries->get (node)->time;
3873 /* Estimate the size of NODE after inlining EDGE which should be an
3874 edge to either NODE or a call inlined into NODE. */
3877 estimate_size_after_inlining (struct cgraph_node *node,
3878 struct cgraph_edge *edge)
3880 struct inline_edge_summary *es = inline_edge_summary (edge);
3881 if (!es->predicate || !false_predicate_p (es->predicate))
3883 int size = inline_summaries->get (node)->size + estimate_edge_growth (edge);
3884 gcc_assert (size >= 0);
3885 return size;
3887 return inline_summaries->get (node)->size;
3891 struct growth_data
3893 struct cgraph_node *node;
3894 bool self_recursive;
3895 bool uninlinable;
3896 int growth;
3900 /* Worker for do_estimate_growth. Collect growth for all callers. */
3902 static bool
3903 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3905 struct cgraph_edge *e;
3906 struct growth_data *d = (struct growth_data *) data;
3908 for (e = node->callers; e; e = e->next_caller)
3910 gcc_checking_assert (e->inline_failed);
3912 if (cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
3914 d->uninlinable = true;
3915 continue;
3918 if (e->recursive_p ())
3920 d->self_recursive = true;
3921 continue;
3923 d->growth += estimate_edge_growth (e);
3925 return false;
3929 /* Estimate the growth caused by inlining NODE into all callees. */
3932 estimate_growth (struct cgraph_node *node)
3934 struct growth_data d = { node, false, false, 0 };
3935 struct inline_summary *info = inline_summaries->get (node);
3937 node->call_for_symbol_and_aliases (do_estimate_growth_1, &d, true);
3939 /* For self recursive functions the growth estimation really should be
3940 infinity. We don't want to return very large values because the growth
3941 plays various roles in badness computation fractions. Be sure to not
3942 return zero or negative growths. */
3943 if (d.self_recursive)
3944 d.growth = d.growth < info->size ? info->size : d.growth;
3945 else if (DECL_EXTERNAL (node->decl) || d.uninlinable)
3947 else
3949 if (node->will_be_removed_from_program_if_no_direct_calls_p ())
3950 d.growth -= info->size;
3951 /* COMDAT functions are very often not shared across multiple units
3952 since they come from various template instantiations.
3953 Take this into account. */
3954 else if (DECL_COMDAT (node->decl)
3955 && node->can_remove_if_no_direct_calls_p ())
3956 d.growth -= (info->size
3957 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3958 + 50) / 100;
3961 return d.growth;
3964 /* Verify if there are fewer than MAX_CALLERS. */
3966 static bool
3967 check_callers (cgraph_node *node, int *max_callers)
3969 ipa_ref *ref;
3971 if (!node->can_remove_if_no_direct_calls_and_refs_p ())
3972 return true;
3974 for (cgraph_edge *e = node->callers; e; e = e->next_caller)
3976 (*max_callers)--;
3977 if (!*max_callers
3978 || cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
3979 return true;
3982 FOR_EACH_ALIAS (node, ref)
3983 if (check_callers (dyn_cast <cgraph_node *> (ref->referring), max_callers))
3984 return true;
3986 return false;
3990 /* Make cheap estimation if growth of NODE is likely positive knowing
3991 EDGE_GROWTH of one particular edge.
3992 We assume that most of other edges will have similar growth
3993 and skip computation if there are too many callers. */
3995 bool
3996 growth_likely_positive (struct cgraph_node *node,
3997 int edge_growth)
3999 int max_callers;
4000 struct cgraph_edge *e;
4001 gcc_checking_assert (edge_growth > 0);
4003 /* First quickly check if NODE is removable at all. */
4004 if (DECL_EXTERNAL (node->decl))
4005 return true;
4006 if (!node->can_remove_if_no_direct_calls_and_refs_p ()
4007 || node->address_taken)
4008 return true;
4010 max_callers = inline_summaries->get (node)->size * 4 / edge_growth + 2;
4012 for (e = node->callers; e; e = e->next_caller)
4014 max_callers--;
4015 if (!max_callers
4016 || cgraph_inline_failed_type (e->inline_failed) == CIF_FINAL_ERROR)
4017 return true;
4020 ipa_ref *ref;
4021 FOR_EACH_ALIAS (node, ref)
4022 if (check_callers (dyn_cast <cgraph_node *> (ref->referring), &max_callers))
4023 return true;
4025 /* Unlike for functions called once, we play unsafe with
4026 COMDATs. We can allow that since we know functions
4027 in consideration are small (and thus risk is small) and
4028 moreover grow estimates already accounts that COMDAT
4029 functions may or may not disappear when eliminated from
4030 current unit. With good probability making aggressive
4031 choice in all units is going to make overall program
4032 smaller. */
4033 if (DECL_COMDAT (node->decl))
4035 if (!node->can_remove_if_no_direct_calls_p ())
4036 return true;
4038 else if (!node->will_be_removed_from_program_if_no_direct_calls_p ())
4039 return true;
4041 return estimate_growth (node) > 0;
4045 /* This function performs intraprocedural analysis in NODE that is required to
4046 inline indirect calls. */
4048 static void
4049 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4051 ipa_analyze_node (node);
4052 if (dump_file && (dump_flags & TDF_DETAILS))
4054 ipa_print_node_params (dump_file, node);
4055 ipa_print_node_jump_functions (dump_file, node);
4060 /* Note function body size. */
4062 void
4063 inline_analyze_function (struct cgraph_node *node)
4065 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4067 if (dump_file)
4068 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
4069 node->name (), node->order);
4070 if (opt_for_fn (node->decl, optimize) && !node->thunk.thunk_p)
4071 inline_indirect_intraprocedural_analysis (node);
4072 compute_inline_parameters (node, false);
4073 if (!optimize)
4075 struct cgraph_edge *e;
4076 for (e = node->callees; e; e = e->next_callee)
4078 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
4079 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4080 e->call_stmt_cannot_inline_p = true;
4082 for (e = node->indirect_calls; e; e = e->next_callee)
4084 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
4085 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4086 e->call_stmt_cannot_inline_p = true;
4090 pop_cfun ();
4094 /* Called when new function is inserted to callgraph late. */
4096 void
4097 inline_summary_t::insert (struct cgraph_node *node, inline_summary *)
4099 inline_analyze_function (node);
4102 /* Note function body size. */
4104 void
4105 inline_generate_summary (void)
4107 struct cgraph_node *node;
4109 /* When not optimizing, do not bother to analyze. Inlining is still done
4110 because edge redirection needs to happen there. */
4111 if (!optimize && !flag_generate_lto && !flag_generate_offload && !flag_wpa)
4112 return;
4114 if (!inline_summaries)
4115 inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
4117 inline_summaries->enable_insertion_hook ();
4119 ipa_register_cgraph_hooks ();
4120 inline_free_summary ();
4122 FOR_EACH_DEFINED_FUNCTION (node)
4123 if (!node->alias)
4124 inline_analyze_function (node);
4128 /* Read predicate from IB. */
4130 static struct predicate
4131 read_predicate (struct lto_input_block *ib)
4133 struct predicate out;
4134 clause_t clause;
4135 int k = 0;
4139 gcc_assert (k <= MAX_CLAUSES);
4140 clause = out.clause[k++] = streamer_read_uhwi (ib);
4142 while (clause);
4144 /* Zero-initialize the remaining clauses in OUT. */
4145 while (k <= MAX_CLAUSES)
4146 out.clause[k++] = 0;
4148 return out;
4152 /* Write inline summary for edge E to OB. */
4154 static void
4155 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
4157 struct inline_edge_summary *es = inline_edge_summary (e);
4158 struct predicate p;
4159 int length, i;
4161 es->call_stmt_size = streamer_read_uhwi (ib);
4162 es->call_stmt_time = streamer_read_uhwi (ib);
4163 es->loop_depth = streamer_read_uhwi (ib);
4164 p = read_predicate (ib);
4165 edge_set_predicate (e, &p);
4166 length = streamer_read_uhwi (ib);
4167 if (length)
4169 es->param.safe_grow_cleared (length);
4170 for (i = 0; i < length; i++)
4171 es->param[i].change_prob = streamer_read_uhwi (ib);
4176 /* Stream in inline summaries from the section. */
4178 static void
4179 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4180 size_t len)
4182 const struct lto_function_header *header =
4183 (const struct lto_function_header *) data;
4184 const int cfg_offset = sizeof (struct lto_function_header);
4185 const int main_offset = cfg_offset + header->cfg_size;
4186 const int string_offset = main_offset + header->main_size;
4187 struct data_in *data_in;
4188 unsigned int i, count2, j;
4189 unsigned int f_count;
4191 lto_input_block ib ((const char *) data + main_offset, header->main_size,
4192 file_data->mode_table);
4194 data_in =
4195 lto_data_in_create (file_data, (const char *) data + string_offset,
4196 header->string_size, vNULL);
4197 f_count = streamer_read_uhwi (&ib);
4198 for (i = 0; i < f_count; i++)
4200 unsigned int index;
4201 struct cgraph_node *node;
4202 struct inline_summary *info;
4203 lto_symtab_encoder_t encoder;
4204 struct bitpack_d bp;
4205 struct cgraph_edge *e;
4206 predicate p;
4208 index = streamer_read_uhwi (&ib);
4209 encoder = file_data->symtab_node_encoder;
4210 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4211 index));
4212 info = inline_summaries->get (node);
4214 info->estimated_stack_size
4215 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
4216 info->size = info->self_size = streamer_read_uhwi (&ib);
4217 info->time = info->self_time = streamer_read_uhwi (&ib);
4219 bp = streamer_read_bitpack (&ib);
4220 info->inlinable = bp_unpack_value (&bp, 1);
4221 info->contains_cilk_spawn = bp_unpack_value (&bp, 1);
4223 count2 = streamer_read_uhwi (&ib);
4224 gcc_assert (!info->conds);
4225 for (j = 0; j < count2; j++)
4227 struct condition c;
4228 c.operand_num = streamer_read_uhwi (&ib);
4229 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4230 c.val = stream_read_tree (&ib, data_in);
4231 bp = streamer_read_bitpack (&ib);
4232 c.agg_contents = bp_unpack_value (&bp, 1);
4233 c.by_ref = bp_unpack_value (&bp, 1);
4234 if (c.agg_contents)
4235 c.offset = streamer_read_uhwi (&ib);
4236 vec_safe_push (info->conds, c);
4238 count2 = streamer_read_uhwi (&ib);
4239 gcc_assert (!info->entry);
4240 for (j = 0; j < count2; j++)
4242 struct size_time_entry e;
4244 e.size = streamer_read_uhwi (&ib);
4245 e.time = streamer_read_uhwi (&ib);
4246 e.predicate = read_predicate (&ib);
4248 vec_safe_push (info->entry, e);
4251 p = read_predicate (&ib);
4252 set_hint_predicate (&info->loop_iterations, p);
4253 p = read_predicate (&ib);
4254 set_hint_predicate (&info->loop_stride, p);
4255 p = read_predicate (&ib);
4256 set_hint_predicate (&info->array_index, p);
4257 for (e = node->callees; e; e = e->next_callee)
4258 read_inline_edge_summary (&ib, e);
4259 for (e = node->indirect_calls; e; e = e->next_callee)
4260 read_inline_edge_summary (&ib, e);
4263 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
4264 len);
4265 lto_data_in_delete (data_in);
4269 /* Read inline summary. Jump functions are shared among ipa-cp
4270 and inliner, so when ipa-cp is active, we don't need to write them
4271 twice. */
4273 void
4274 inline_read_summary (void)
4276 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4277 struct lto_file_decl_data *file_data;
4278 unsigned int j = 0;
4280 inline_summary_alloc ();
4282 while ((file_data = file_data_vec[j++]))
4284 size_t len;
4285 const char *data = lto_get_section_data (file_data,
4286 LTO_section_inline_summary,
4287 NULL, &len);
4288 if (data)
4289 inline_read_section (file_data, data, len);
4290 else
4291 /* Fatal error here. We do not want to support compiling ltrans units
4292 with different version of compiler or different flags than the WPA
4293 unit, so this should never happen. */
4294 fatal_error (input_location,
4295 "ipa inline summary is missing in input file");
4297 if (optimize)
4299 ipa_register_cgraph_hooks ();
4300 if (!flag_ipa_cp)
4301 ipa_prop_read_jump_functions ();
4304 gcc_assert (inline_summaries);
4305 inline_summaries->enable_insertion_hook ();
4309 /* Write predicate P to OB. */
4311 static void
4312 write_predicate (struct output_block *ob, struct predicate *p)
4314 int j;
4315 if (p)
4316 for (j = 0; p->clause[j]; j++)
4318 gcc_assert (j < MAX_CLAUSES);
4319 streamer_write_uhwi (ob, p->clause[j]);
4321 streamer_write_uhwi (ob, 0);
4325 /* Write inline summary for edge E to OB. */
4327 static void
4328 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4330 struct inline_edge_summary *es = inline_edge_summary (e);
4331 int i;
4333 streamer_write_uhwi (ob, es->call_stmt_size);
4334 streamer_write_uhwi (ob, es->call_stmt_time);
4335 streamer_write_uhwi (ob, es->loop_depth);
4336 write_predicate (ob, es->predicate);
4337 streamer_write_uhwi (ob, es->param.length ());
4338 for (i = 0; i < (int) es->param.length (); i++)
4339 streamer_write_uhwi (ob, es->param[i].change_prob);
4343 /* Write inline summary for node in SET.
4344 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4345 active, we don't need to write them twice. */
4347 void
4348 inline_write_summary (void)
4350 struct cgraph_node *node;
4351 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4352 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4353 unsigned int count = 0;
4354 int i;
4356 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4358 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4359 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
4360 if (cnode && cnode->definition && !cnode->alias)
4361 count++;
4363 streamer_write_uhwi (ob, count);
4365 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4367 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4368 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
4369 if (cnode && (node = cnode)->definition && !node->alias)
4371 struct inline_summary *info = inline_summaries->get (node);
4372 struct bitpack_d bp;
4373 struct cgraph_edge *edge;
4374 int i;
4375 size_time_entry *e;
4376 struct condition *c;
4378 streamer_write_uhwi (ob,
4379 lto_symtab_encoder_encode (encoder,
4381 node));
4382 streamer_write_hwi (ob, info->estimated_self_stack_size);
4383 streamer_write_hwi (ob, info->self_size);
4384 streamer_write_hwi (ob, info->self_time);
4385 bp = bitpack_create (ob->main_stream);
4386 bp_pack_value (&bp, info->inlinable, 1);
4387 bp_pack_value (&bp, info->contains_cilk_spawn, 1);
4388 streamer_write_bitpack (&bp);
4389 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4390 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4392 streamer_write_uhwi (ob, c->operand_num);
4393 streamer_write_uhwi (ob, c->code);
4394 stream_write_tree (ob, c->val, true);
4395 bp = bitpack_create (ob->main_stream);
4396 bp_pack_value (&bp, c->agg_contents, 1);
4397 bp_pack_value (&bp, c->by_ref, 1);
4398 streamer_write_bitpack (&bp);
4399 if (c->agg_contents)
4400 streamer_write_uhwi (ob, c->offset);
4402 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4403 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4405 streamer_write_uhwi (ob, e->size);
4406 streamer_write_uhwi (ob, e->time);
4407 write_predicate (ob, &e->predicate);
4409 write_predicate (ob, info->loop_iterations);
4410 write_predicate (ob, info->loop_stride);
4411 write_predicate (ob, info->array_index);
4412 for (edge = node->callees; edge; edge = edge->next_callee)
4413 write_inline_edge_summary (ob, edge);
4414 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4415 write_inline_edge_summary (ob, edge);
4418 streamer_write_char_stream (ob->main_stream, 0);
4419 produce_asm (ob, NULL);
4420 destroy_output_block (ob);
4422 if (optimize && !flag_ipa_cp)
4423 ipa_prop_write_jump_functions ();
4427 /* Release inline summary. */
4429 void
4430 inline_free_summary (void)
4432 struct cgraph_node *node;
4433 if (edge_removal_hook_holder)
4434 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4435 edge_removal_hook_holder = NULL;
4436 if (edge_duplication_hook_holder)
4437 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4438 edge_duplication_hook_holder = NULL;
4439 if (!inline_edge_summary_vec.exists ())
4440 return;
4441 FOR_EACH_DEFINED_FUNCTION (node)
4442 if (!node->alias)
4443 reset_inline_summary (node, inline_summaries->get (node));
4444 inline_summaries->release ();
4445 inline_summaries = NULL;
4446 inline_edge_summary_vec.release ();
4447 edge_predicate_pool.release ();