jit: API change to gcc_jit_context_new_global
[official-gcc.git] / gcc / ipa-inline-analysis.c
blobec91d8ec9af1fb8b7d55244072f54a809b3708ab
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 "hash-set.h"
72 #include "machmode.h"
73 #include "vec.h"
74 #include "double-int.h"
75 #include "input.h"
76 #include "alias.h"
77 #include "symtab.h"
78 #include "wide-int.h"
79 #include "inchash.h"
80 #include "real.h"
81 #include "tree.h"
82 #include "fold-const.h"
83 #include "stor-layout.h"
84 #include "stringpool.h"
85 #include "print-tree.h"
86 #include "tree-inline.h"
87 #include "langhooks.h"
88 #include "flags.h"
89 #include "diagnostic.h"
90 #include "gimple-pretty-print.h"
91 #include "params.h"
92 #include "tree-pass.h"
93 #include "coverage.h"
94 #include "predict.h"
95 #include "hard-reg-set.h"
96 #include "input.h"
97 #include "function.h"
98 #include "dominance.h"
99 #include "cfg.h"
100 #include "cfganal.h"
101 #include "basic-block.h"
102 #include "tree-ssa-alias.h"
103 #include "internal-fn.h"
104 #include "gimple-expr.h"
105 #include "is-a.h"
106 #include "gimple.h"
107 #include "gimple-iterator.h"
108 #include "gimple-ssa.h"
109 #include "tree-cfg.h"
110 #include "tree-phinodes.h"
111 #include "ssa-iterators.h"
112 #include "tree-ssanames.h"
113 #include "tree-ssa-loop-niter.h"
114 #include "tree-ssa-loop.h"
115 #include "hash-map.h"
116 #include "plugin-api.h"
117 #include "ipa-ref.h"
118 #include "cgraph.h"
119 #include "alloc-pool.h"
120 #include "symbol-summary.h"
121 #include "ipa-prop.h"
122 #include "lto-streamer.h"
123 #include "data-streamer.h"
124 #include "tree-streamer.h"
125 #include "ipa-inline.h"
126 #include "cfgloop.h"
127 #include "tree-scalar-evolution.h"
128 #include "ipa-utils.h"
129 #include "cilk.h"
130 #include "cfgexpand.h"
132 /* Estimate runtime of function can easilly run into huge numbers with many
133 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
134 integer. For anything larger we use gcov_type. */
135 #define MAX_TIME 500000
137 /* Number of bits in integer, but we really want to be stable across different
138 hosts. */
139 #define NUM_CONDITIONS 32
141 enum predicate_conditions
143 predicate_false_condition = 0,
144 predicate_not_inlined_condition = 1,
145 predicate_first_dynamic_condition = 2
148 /* Special condition code we use to represent test that operand is compile time
149 constant. */
150 #define IS_NOT_CONSTANT ERROR_MARK
151 /* Special condition code we use to represent test that operand is not changed
152 across invocation of the function. When operand IS_NOT_CONSTANT it is always
153 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
154 of executions even when they are not compile time constants. */
155 #define CHANGED IDENTIFIER_NODE
157 /* Holders of ipa cgraph hooks: */
158 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
159 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
160 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
161 static void inline_edge_duplication_hook (struct cgraph_edge *,
162 struct cgraph_edge *, void *);
164 /* VECtor holding inline summaries.
165 In GGC memory because conditions might point to constant trees. */
166 function_summary <inline_summary *> *inline_summaries;
167 vec<inline_edge_summary_t> inline_edge_summary_vec;
169 /* Cached node/edge growths. */
170 vec<int> node_growth_cache;
171 vec<edge_growth_cache_entry> edge_growth_cache;
173 /* Edge predicates goes here. */
174 static alloc_pool edge_predicate_pool;
176 /* Return true predicate (tautology).
177 We represent it by empty list of clauses. */
179 static inline struct predicate
180 true_predicate (void)
182 struct predicate p;
183 p.clause[0] = 0;
184 return p;
188 /* Return predicate testing single condition number COND. */
190 static inline struct predicate
191 single_cond_predicate (int cond)
193 struct predicate p;
194 p.clause[0] = 1 << cond;
195 p.clause[1] = 0;
196 return p;
200 /* Return false predicate. First clause require false condition. */
202 static inline struct predicate
203 false_predicate (void)
205 return single_cond_predicate (predicate_false_condition);
209 /* Return true if P is (true). */
211 static inline bool
212 true_predicate_p (struct predicate *p)
214 return !p->clause[0];
218 /* Return true if P is (false). */
220 static inline bool
221 false_predicate_p (struct predicate *p)
223 if (p->clause[0] == (1 << predicate_false_condition))
225 gcc_checking_assert (!p->clause[1]
226 && p->clause[0] == 1 << predicate_false_condition);
227 return true;
229 return false;
233 /* Return predicate that is set true when function is not inlined. */
235 static inline struct predicate
236 not_inlined_predicate (void)
238 return single_cond_predicate (predicate_not_inlined_condition);
241 /* Simple description of whether a memory load or a condition refers to a load
242 from an aggregate and if so, how and where from in the aggregate.
243 Individual fields have the same meaning like fields with the same name in
244 struct condition. */
246 struct agg_position_info
248 HOST_WIDE_INT offset;
249 bool agg_contents;
250 bool by_ref;
253 /* Add condition to condition list CONDS. AGGPOS describes whether the used
254 oprand is loaded from an aggregate and where in the aggregate it is. It can
255 be NULL, which means this not a load from an aggregate. */
257 static struct predicate
258 add_condition (struct inline_summary *summary, int operand_num,
259 struct agg_position_info *aggpos,
260 enum tree_code code, tree val)
262 int i;
263 struct condition *c;
264 struct condition new_cond;
265 HOST_WIDE_INT offset;
266 bool agg_contents, by_ref;
268 if (aggpos)
270 offset = aggpos->offset;
271 agg_contents = aggpos->agg_contents;
272 by_ref = aggpos->by_ref;
274 else
276 offset = 0;
277 agg_contents = false;
278 by_ref = false;
281 gcc_checking_assert (operand_num >= 0);
282 for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
284 if (c->operand_num == operand_num
285 && c->code == code
286 && c->val == val
287 && c->agg_contents == agg_contents
288 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
289 return single_cond_predicate (i + predicate_first_dynamic_condition);
291 /* Too many conditions. Give up and return constant true. */
292 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
293 return true_predicate ();
295 new_cond.operand_num = operand_num;
296 new_cond.code = code;
297 new_cond.val = val;
298 new_cond.agg_contents = agg_contents;
299 new_cond.by_ref = by_ref;
300 new_cond.offset = offset;
301 vec_safe_push (summary->conds, new_cond);
302 return single_cond_predicate (i + predicate_first_dynamic_condition);
306 /* Add clause CLAUSE into the predicate P. */
308 static inline void
309 add_clause (conditions conditions, struct predicate *p, clause_t clause)
311 int i;
312 int i2;
313 int insert_here = -1;
314 int c1, c2;
316 /* True clause. */
317 if (!clause)
318 return;
320 /* False clause makes the whole predicate false. Kill the other variants. */
321 if (clause == (1 << predicate_false_condition))
323 p->clause[0] = (1 << predicate_false_condition);
324 p->clause[1] = 0;
325 return;
327 if (false_predicate_p (p))
328 return;
330 /* No one should be silly enough to add false into nontrivial clauses. */
331 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
333 /* Look where to insert the clause. At the same time prune out
334 clauses of P that are implied by the new clause and thus
335 redundant. */
336 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
338 p->clause[i2] = p->clause[i];
340 if (!p->clause[i])
341 break;
343 /* If p->clause[i] implies clause, there is nothing to add. */
344 if ((p->clause[i] & clause) == p->clause[i])
346 /* We had nothing to add, none of clauses should've become
347 redundant. */
348 gcc_checking_assert (i == i2);
349 return;
352 if (p->clause[i] < clause && insert_here < 0)
353 insert_here = i2;
355 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
356 Otherwise the p->clause[i] has to stay. */
357 if ((p->clause[i] & clause) != clause)
358 i2++;
361 /* Look for clauses that are obviously true. I.e.
362 op0 == 5 || op0 != 5. */
363 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
365 condition *cc1;
366 if (!(clause & (1 << c1)))
367 continue;
368 cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
369 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
370 and thus there is no point for looking for them. */
371 if (cc1->code == CHANGED || cc1->code == IS_NOT_CONSTANT)
372 continue;
373 for (c2 = c1 + 1; c2 < NUM_CONDITIONS; c2++)
374 if (clause & (1 << c2))
376 condition *cc1 =
377 &(*conditions)[c1 - predicate_first_dynamic_condition];
378 condition *cc2 =
379 &(*conditions)[c2 - predicate_first_dynamic_condition];
380 if (cc1->operand_num == cc2->operand_num
381 && cc1->val == cc2->val
382 && cc2->code != IS_NOT_CONSTANT
383 && cc2->code != CHANGED
384 && cc1->code == invert_tree_comparison (cc2->code,
385 HONOR_NANS (cc1->val)))
386 return;
391 /* We run out of variants. Be conservative in positive direction. */
392 if (i2 == MAX_CLAUSES)
393 return;
394 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
395 p->clause[i2 + 1] = 0;
396 if (insert_here >= 0)
397 for (; i2 > insert_here; i2--)
398 p->clause[i2] = p->clause[i2 - 1];
399 else
400 insert_here = i2;
401 p->clause[insert_here] = clause;
405 /* Return P & P2. */
407 static struct predicate
408 and_predicates (conditions conditions,
409 struct predicate *p, struct predicate *p2)
411 struct predicate out = *p;
412 int i;
414 /* Avoid busy work. */
415 if (false_predicate_p (p2) || true_predicate_p (p))
416 return *p2;
417 if (false_predicate_p (p) || true_predicate_p (p2))
418 return *p;
420 /* See how far predicates match. */
421 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
423 gcc_checking_assert (i < MAX_CLAUSES);
426 /* Combine the predicates rest. */
427 for (; p2->clause[i]; i++)
429 gcc_checking_assert (i < MAX_CLAUSES);
430 add_clause (conditions, &out, p2->clause[i]);
432 return out;
436 /* Return true if predicates are obviously equal. */
438 static inline bool
439 predicates_equal_p (struct predicate *p, struct predicate *p2)
441 int i;
442 for (i = 0; p->clause[i]; i++)
444 gcc_checking_assert (i < MAX_CLAUSES);
445 gcc_checking_assert (p->clause[i] > p->clause[i + 1]);
446 gcc_checking_assert (!p2->clause[i]
447 || p2->clause[i] > p2->clause[i + 1]);
448 if (p->clause[i] != p2->clause[i])
449 return false;
451 return !p2->clause[i];
455 /* Return P | P2. */
457 static struct predicate
458 or_predicates (conditions conditions,
459 struct predicate *p, struct predicate *p2)
461 struct predicate out = true_predicate ();
462 int i, j;
464 /* Avoid busy work. */
465 if (false_predicate_p (p2) || true_predicate_p (p))
466 return *p;
467 if (false_predicate_p (p) || true_predicate_p (p2))
468 return *p2;
469 if (predicates_equal_p (p, p2))
470 return *p;
472 /* OK, combine the predicates. */
473 for (i = 0; p->clause[i]; i++)
474 for (j = 0; p2->clause[j]; j++)
476 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
477 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
479 return out;
483 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
484 if predicate P is known to be false. */
486 static bool
487 evaluate_predicate (struct predicate *p, clause_t possible_truths)
489 int i;
491 /* True remains true. */
492 if (true_predicate_p (p))
493 return true;
495 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
497 /* See if we can find clause we can disprove. */
498 for (i = 0; p->clause[i]; i++)
500 gcc_checking_assert (i < MAX_CLAUSES);
501 if (!(p->clause[i] & possible_truths))
502 return false;
504 return true;
507 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
508 instruction will be recomputed per invocation of the inlined call. */
510 static int
511 predicate_probability (conditions conds,
512 struct predicate *p, clause_t possible_truths,
513 vec<inline_param_summary> inline_param_summary)
515 int i;
516 int combined_prob = REG_BR_PROB_BASE;
518 /* True remains true. */
519 if (true_predicate_p (p))
520 return REG_BR_PROB_BASE;
522 if (false_predicate_p (p))
523 return 0;
525 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
527 /* See if we can find clause we can disprove. */
528 for (i = 0; p->clause[i]; i++)
530 gcc_checking_assert (i < MAX_CLAUSES);
531 if (!(p->clause[i] & possible_truths))
532 return 0;
533 else
535 int this_prob = 0;
536 int i2;
537 if (!inline_param_summary.exists ())
538 return REG_BR_PROB_BASE;
539 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
540 if ((p->clause[i] & possible_truths) & (1 << i2))
542 if (i2 >= predicate_first_dynamic_condition)
544 condition *c =
545 &(*conds)[i2 - predicate_first_dynamic_condition];
546 if (c->code == CHANGED
547 && (c->operand_num <
548 (int) inline_param_summary.length ()))
550 int iprob =
551 inline_param_summary[c->operand_num].change_prob;
552 this_prob = MAX (this_prob, iprob);
554 else
555 this_prob = REG_BR_PROB_BASE;
557 else
558 this_prob = REG_BR_PROB_BASE;
560 combined_prob = MIN (this_prob, combined_prob);
561 if (!combined_prob)
562 return 0;
565 return combined_prob;
569 /* Dump conditional COND. */
571 static void
572 dump_condition (FILE *f, conditions conditions, int cond)
574 condition *c;
575 if (cond == predicate_false_condition)
576 fprintf (f, "false");
577 else if (cond == predicate_not_inlined_condition)
578 fprintf (f, "not inlined");
579 else
581 c = &(*conditions)[cond - predicate_first_dynamic_condition];
582 fprintf (f, "op%i", c->operand_num);
583 if (c->agg_contents)
584 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
585 c->by_ref ? "ref " : "", c->offset);
586 if (c->code == IS_NOT_CONSTANT)
588 fprintf (f, " not constant");
589 return;
591 if (c->code == CHANGED)
593 fprintf (f, " changed");
594 return;
596 fprintf (f, " %s ", op_symbol_code (c->code));
597 print_generic_expr (f, c->val, 1);
602 /* Dump clause CLAUSE. */
604 static void
605 dump_clause (FILE *f, conditions conds, clause_t clause)
607 int i;
608 bool found = false;
609 fprintf (f, "(");
610 if (!clause)
611 fprintf (f, "true");
612 for (i = 0; i < NUM_CONDITIONS; i++)
613 if (clause & (1 << i))
615 if (found)
616 fprintf (f, " || ");
617 found = true;
618 dump_condition (f, conds, i);
620 fprintf (f, ")");
624 /* Dump predicate PREDICATE. */
626 static void
627 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
629 int i;
630 if (true_predicate_p (pred))
631 dump_clause (f, conds, 0);
632 else
633 for (i = 0; pred->clause[i]; i++)
635 if (i)
636 fprintf (f, " && ");
637 dump_clause (f, conds, pred->clause[i]);
639 fprintf (f, "\n");
643 /* Dump inline hints. */
644 void
645 dump_inline_hints (FILE *f, inline_hints hints)
647 if (!hints)
648 return;
649 fprintf (f, "inline hints:");
650 if (hints & INLINE_HINT_indirect_call)
652 hints &= ~INLINE_HINT_indirect_call;
653 fprintf (f, " indirect_call");
655 if (hints & INLINE_HINT_loop_iterations)
657 hints &= ~INLINE_HINT_loop_iterations;
658 fprintf (f, " loop_iterations");
660 if (hints & INLINE_HINT_loop_stride)
662 hints &= ~INLINE_HINT_loop_stride;
663 fprintf (f, " loop_stride");
665 if (hints & INLINE_HINT_same_scc)
667 hints &= ~INLINE_HINT_same_scc;
668 fprintf (f, " same_scc");
670 if (hints & INLINE_HINT_in_scc)
672 hints &= ~INLINE_HINT_in_scc;
673 fprintf (f, " in_scc");
675 if (hints & INLINE_HINT_cross_module)
677 hints &= ~INLINE_HINT_cross_module;
678 fprintf (f, " cross_module");
680 if (hints & INLINE_HINT_declared_inline)
682 hints &= ~INLINE_HINT_declared_inline;
683 fprintf (f, " declared_inline");
685 if (hints & INLINE_HINT_array_index)
687 hints &= ~INLINE_HINT_array_index;
688 fprintf (f, " array_index");
690 if (hints & INLINE_HINT_known_hot)
692 hints &= ~INLINE_HINT_known_hot;
693 fprintf (f, " known_hot");
695 gcc_assert (!hints);
699 /* Record SIZE and TIME under condition PRED into the inline summary. */
701 static void
702 account_size_time (struct inline_summary *summary, int size, int time,
703 struct predicate *pred)
705 size_time_entry *e;
706 bool found = false;
707 int i;
709 if (false_predicate_p (pred))
710 return;
712 /* We need to create initial empty unconitional clause, but otherwie
713 we don't need to account empty times and sizes. */
714 if (!size && !time && summary->entry)
715 return;
717 /* Watch overflow that might result from insane profiles. */
718 if (time > MAX_TIME * INLINE_TIME_SCALE)
719 time = MAX_TIME * INLINE_TIME_SCALE;
720 gcc_assert (time >= 0);
722 for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
723 if (predicates_equal_p (&e->predicate, pred))
725 found = true;
726 break;
728 if (i == 256)
730 i = 0;
731 found = true;
732 e = &(*summary->entry)[0];
733 gcc_assert (!e->predicate.clause[0]);
734 if (dump_file && (dump_flags & TDF_DETAILS))
735 fprintf (dump_file,
736 "\t\tReached limit on number of entries, "
737 "ignoring the predicate.");
739 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
741 fprintf (dump_file,
742 "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
743 ((double) size) / INLINE_SIZE_SCALE,
744 ((double) time) / INLINE_TIME_SCALE, found ? "" : "new ");
745 dump_predicate (dump_file, summary->conds, pred);
747 if (!found)
749 struct size_time_entry new_entry;
750 new_entry.size = size;
751 new_entry.time = time;
752 new_entry.predicate = *pred;
753 vec_safe_push (summary->entry, new_entry);
755 else
757 e->size += size;
758 e->time += time;
759 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
760 e->time = MAX_TIME * INLINE_TIME_SCALE;
764 /* Set predicate for edge E. */
766 static void
767 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
769 struct inline_edge_summary *es = inline_edge_summary (e);
771 /* If the edge is determined to be never executed, redirect it
772 to BUILTIN_UNREACHABLE to save inliner from inlining into it. */
773 if (predicate && false_predicate_p (predicate) && e->callee)
775 struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL;
777 e->redirect_callee (cgraph_node::get_create
778 (builtin_decl_implicit (BUILT_IN_UNREACHABLE)));
779 e->inline_failed = CIF_UNREACHABLE;
780 es->call_stmt_size = 0;
781 es->call_stmt_time = 0;
782 if (callee)
783 callee->remove_symbol_and_inline_clones ();
785 if (predicate && !true_predicate_p (predicate))
787 if (!es->predicate)
788 es->predicate = (struct predicate *) pool_alloc (edge_predicate_pool);
789 *es->predicate = *predicate;
791 else
793 if (es->predicate)
794 pool_free (edge_predicate_pool, es->predicate);
795 es->predicate = NULL;
799 /* Set predicate for hint *P. */
801 static void
802 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
804 if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
806 if (*p)
807 pool_free (edge_predicate_pool, *p);
808 *p = NULL;
810 else
812 if (!*p)
813 *p = (struct predicate *) pool_alloc (edge_predicate_pool);
814 **p = new_predicate;
819 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
820 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
821 Return clause of possible truths. When INLINE_P is true, assume that we are
822 inlining.
824 ERROR_MARK means compile time invariant. */
826 static clause_t
827 evaluate_conditions_for_known_args (struct cgraph_node *node,
828 bool inline_p,
829 vec<tree> known_vals,
830 vec<ipa_agg_jump_function_p>
831 known_aggs)
833 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
834 struct inline_summary *info = inline_summaries->get (node);
835 int i;
836 struct condition *c;
838 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
840 tree val;
841 tree res;
843 /* We allow call stmt to have fewer arguments than the callee function
844 (especially for K&R style programs). So bound check here (we assume
845 known_aggs vector, if non-NULL, has the same length as
846 known_vals). */
847 gcc_checking_assert (!known_aggs.exists ()
848 || (known_vals.length () == known_aggs.length ()));
849 if (c->operand_num >= (int) known_vals.length ())
851 clause |= 1 << (i + predicate_first_dynamic_condition);
852 continue;
855 if (c->agg_contents)
857 struct ipa_agg_jump_function *agg;
859 if (c->code == CHANGED
860 && !c->by_ref
861 && (known_vals[c->operand_num] == error_mark_node))
862 continue;
864 if (known_aggs.exists ())
866 agg = known_aggs[c->operand_num];
867 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
869 else
870 val = NULL_TREE;
872 else
874 val = known_vals[c->operand_num];
875 if (val == error_mark_node && c->code != CHANGED)
876 val = NULL_TREE;
879 if (!val)
881 clause |= 1 << (i + predicate_first_dynamic_condition);
882 continue;
884 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
885 continue;
887 if (operand_equal_p (TYPE_SIZE (TREE_TYPE (c->val)),
888 TYPE_SIZE (TREE_TYPE (val)), 0))
890 val = fold_unary (VIEW_CONVERT_EXPR, TREE_TYPE (c->val), val);
892 res = val
893 ? fold_binary_to_constant (c->code, boolean_type_node, val, c->val)
894 : NULL;
896 if (res && integer_zerop (res))
897 continue;
899 clause |= 1 << (i + predicate_first_dynamic_condition);
901 return clause;
905 /* Work out what conditions might be true at invocation of E. */
907 static void
908 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
909 clause_t *clause_ptr,
910 vec<tree> *known_vals_ptr,
911 vec<ipa_polymorphic_call_context>
912 *known_contexts_ptr,
913 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
915 struct cgraph_node *callee = e->callee->ultimate_alias_target ();
916 struct inline_summary *info = inline_summaries->get (callee);
917 vec<tree> known_vals = vNULL;
918 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
920 if (clause_ptr)
921 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
922 if (known_vals_ptr)
923 known_vals_ptr->create (0);
924 if (known_contexts_ptr)
925 known_contexts_ptr->create (0);
927 if (ipa_node_params_sum
928 && !e->call_stmt_cannot_inline_p
929 && ((clause_ptr && info->conds) || known_vals_ptr || known_contexts_ptr))
931 struct ipa_node_params *parms_info;
932 struct ipa_edge_args *args = IPA_EDGE_REF (e);
933 struct inline_edge_summary *es = inline_edge_summary (e);
934 int i, count = ipa_get_cs_argument_count (args);
936 if (e->caller->global.inlined_to)
937 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
938 else
939 parms_info = IPA_NODE_REF (e->caller);
941 if (count && (info->conds || known_vals_ptr))
942 known_vals.safe_grow_cleared (count);
943 if (count && (info->conds || known_aggs_ptr))
944 known_aggs.safe_grow_cleared (count);
945 if (count && known_contexts_ptr)
946 known_contexts_ptr->safe_grow_cleared (count);
948 for (i = 0; i < count; i++)
950 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
951 tree cst = ipa_value_from_jfunc (parms_info, jf);
953 if (!cst && e->call_stmt
954 && i < (int)gimple_call_num_args (e->call_stmt))
956 cst = gimple_call_arg (e->call_stmt, i);
957 if (!is_gimple_min_invariant (cst))
958 cst = NULL;
960 if (cst)
962 gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO);
963 if (known_vals.exists ())
964 known_vals[i] = cst;
966 else if (inline_p && !es->param[i].change_prob)
967 known_vals[i] = error_mark_node;
969 if (known_contexts_ptr)
970 (*known_contexts_ptr)[i] = ipa_context_from_jfunc (parms_info, e,
971 i, jf);
972 /* TODO: When IPA-CP starts propagating and merging aggregate jump
973 functions, use its knowledge of the caller too, just like the
974 scalar case above. */
975 known_aggs[i] = &jf->agg;
978 else if (e->call_stmt && !e->call_stmt_cannot_inline_p
979 && ((clause_ptr && info->conds) || known_vals_ptr))
981 int i, count = (int)gimple_call_num_args (e->call_stmt);
983 if (count && (info->conds || known_vals_ptr))
984 known_vals.safe_grow_cleared (count);
985 for (i = 0; i < count; i++)
987 tree cst = gimple_call_arg (e->call_stmt, i);
988 if (!is_gimple_min_invariant (cst))
989 cst = NULL;
990 if (cst)
991 known_vals[i] = cst;
995 if (clause_ptr)
996 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
997 known_vals, known_aggs);
999 if (known_vals_ptr)
1000 *known_vals_ptr = known_vals;
1001 else
1002 known_vals.release ();
1004 if (known_aggs_ptr)
1005 *known_aggs_ptr = known_aggs;
1006 else
1007 known_aggs.release ();
1011 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
1013 static void
1014 inline_summary_alloc (void)
1016 if (!edge_removal_hook_holder)
1017 edge_removal_hook_holder =
1018 symtab->add_edge_removal_hook (&inline_edge_removal_hook, NULL);
1019 if (!edge_duplication_hook_holder)
1020 edge_duplication_hook_holder =
1021 symtab->add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
1023 if (!inline_summaries)
1024 inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
1026 if (inline_edge_summary_vec.length () <= (unsigned) symtab->edges_max_uid)
1027 inline_edge_summary_vec.safe_grow_cleared (symtab->edges_max_uid + 1);
1028 if (!edge_predicate_pool)
1029 edge_predicate_pool = create_alloc_pool ("edge predicates",
1030 sizeof (struct predicate), 10);
1033 /* We are called multiple time for given function; clear
1034 data from previous run so they are not cumulated. */
1036 static void
1037 reset_inline_edge_summary (struct cgraph_edge *e)
1039 if (e->uid < (int) inline_edge_summary_vec.length ())
1041 struct inline_edge_summary *es = inline_edge_summary (e);
1043 es->call_stmt_size = es->call_stmt_time = 0;
1044 if (es->predicate)
1045 pool_free (edge_predicate_pool, es->predicate);
1046 es->predicate = NULL;
1047 es->param.release ();
1051 /* We are called multiple time for given function; clear
1052 data from previous run so they are not cumulated. */
1054 static void
1055 reset_inline_summary (struct cgraph_node *node,
1056 inline_summary *info)
1058 struct cgraph_edge *e;
1060 info->self_size = info->self_time = 0;
1061 info->estimated_stack_size = 0;
1062 info->estimated_self_stack_size = 0;
1063 info->stack_frame_offset = 0;
1064 info->size = 0;
1065 info->time = 0;
1066 info->growth = 0;
1067 info->scc_no = 0;
1068 if (info->loop_iterations)
1070 pool_free (edge_predicate_pool, info->loop_iterations);
1071 info->loop_iterations = NULL;
1073 if (info->loop_stride)
1075 pool_free (edge_predicate_pool, info->loop_stride);
1076 info->loop_stride = NULL;
1078 if (info->array_index)
1080 pool_free (edge_predicate_pool, info->array_index);
1081 info->array_index = NULL;
1083 vec_free (info->conds);
1084 vec_free (info->entry);
1085 for (e = node->callees; e; e = e->next_callee)
1086 reset_inline_edge_summary (e);
1087 for (e = node->indirect_calls; e; e = e->next_callee)
1088 reset_inline_edge_summary (e);
1091 /* Hook that is called by cgraph.c when a node is removed. */
1093 void
1094 inline_summary_t::remove (cgraph_node *node, inline_summary *info)
1096 reset_inline_summary (node, info);
1099 /* Remap predicate P of former function to be predicate of duplicated function.
1100 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1101 INFO is inline summary of the duplicated node. */
1103 static struct predicate
1104 remap_predicate_after_duplication (struct predicate *p,
1105 clause_t possible_truths,
1106 struct inline_summary *info)
1108 struct predicate new_predicate = true_predicate ();
1109 int j;
1110 for (j = 0; p->clause[j]; j++)
1111 if (!(possible_truths & p->clause[j]))
1113 new_predicate = false_predicate ();
1114 break;
1116 else
1117 add_clause (info->conds, &new_predicate,
1118 possible_truths & p->clause[j]);
1119 return new_predicate;
1122 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1123 Additionally care about allocating new memory slot for updated predicate
1124 and set it to NULL when it becomes true or false (and thus uninteresting).
1127 static void
1128 remap_hint_predicate_after_duplication (struct predicate **p,
1129 clause_t possible_truths,
1130 struct inline_summary *info)
1132 struct predicate new_predicate;
1134 if (!*p)
1135 return;
1137 new_predicate = remap_predicate_after_duplication (*p,
1138 possible_truths, info);
1139 /* We do not want to free previous predicate; it is used by node origin. */
1140 *p = NULL;
1141 set_hint_predicate (p, new_predicate);
1145 /* Hook that is called by cgraph.c when a node is duplicated. */
1146 void
1147 inline_summary_t::duplicate (cgraph_node *src,
1148 cgraph_node *dst,
1149 inline_summary *,
1150 inline_summary *info)
1152 inline_summary_alloc ();
1153 memcpy (info, inline_summaries->get (src), sizeof (inline_summary));
1154 /* TODO: as an optimization, we may avoid copying conditions
1155 that are known to be false or true. */
1156 info->conds = vec_safe_copy (info->conds);
1158 /* When there are any replacements in the function body, see if we can figure
1159 out that something was optimized out. */
1160 if (ipa_node_params_sum && dst->clone.tree_map)
1162 vec<size_time_entry, va_gc> *entry = info->entry;
1163 /* Use SRC parm info since it may not be copied yet. */
1164 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1165 vec<tree> known_vals = vNULL;
1166 int count = ipa_get_param_count (parms_info);
1167 int i, j;
1168 clause_t possible_truths;
1169 struct predicate true_pred = true_predicate ();
1170 size_time_entry *e;
1171 int optimized_out_size = 0;
1172 bool inlined_to_p = false;
1173 struct cgraph_edge *edge;
1175 info->entry = 0;
1176 known_vals.safe_grow_cleared (count);
1177 for (i = 0; i < count; i++)
1179 struct ipa_replace_map *r;
1181 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1183 if (((!r->old_tree && r->parm_num == i)
1184 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1185 && r->replace_p && !r->ref_p)
1187 known_vals[i] = r->new_tree;
1188 break;
1192 possible_truths = evaluate_conditions_for_known_args (dst, false,
1193 known_vals,
1194 vNULL);
1195 known_vals.release ();
1197 account_size_time (info, 0, 0, &true_pred);
1199 /* Remap size_time vectors.
1200 Simplify the predicate by prunning out alternatives that are known
1201 to be false.
1202 TODO: as on optimization, we can also eliminate conditions known
1203 to be true. */
1204 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1206 struct predicate new_predicate;
1207 new_predicate = remap_predicate_after_duplication (&e->predicate,
1208 possible_truths,
1209 info);
1210 if (false_predicate_p (&new_predicate))
1211 optimized_out_size += e->size;
1212 else
1213 account_size_time (info, e->size, e->time, &new_predicate);
1216 /* Remap edge predicates with the same simplification as above.
1217 Also copy constantness arrays. */
1218 for (edge = dst->callees; edge; edge = edge->next_callee)
1220 struct predicate new_predicate;
1221 struct inline_edge_summary *es = inline_edge_summary (edge);
1223 if (!edge->inline_failed)
1224 inlined_to_p = true;
1225 if (!es->predicate)
1226 continue;
1227 new_predicate = remap_predicate_after_duplication (es->predicate,
1228 possible_truths,
1229 info);
1230 if (false_predicate_p (&new_predicate)
1231 && !false_predicate_p (es->predicate))
1233 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1234 edge->frequency = 0;
1236 edge_set_predicate (edge, &new_predicate);
1239 /* Remap indirect edge predicates with the same simplificaiton as above.
1240 Also copy constantness arrays. */
1241 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1243 struct predicate new_predicate;
1244 struct inline_edge_summary *es = inline_edge_summary (edge);
1246 gcc_checking_assert (edge->inline_failed);
1247 if (!es->predicate)
1248 continue;
1249 new_predicate = remap_predicate_after_duplication (es->predicate,
1250 possible_truths,
1251 info);
1252 if (false_predicate_p (&new_predicate)
1253 && !false_predicate_p (es->predicate))
1255 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1256 edge->frequency = 0;
1258 edge_set_predicate (edge, &new_predicate);
1260 remap_hint_predicate_after_duplication (&info->loop_iterations,
1261 possible_truths, info);
1262 remap_hint_predicate_after_duplication (&info->loop_stride,
1263 possible_truths, info);
1264 remap_hint_predicate_after_duplication (&info->array_index,
1265 possible_truths, info);
1267 /* If inliner or someone after inliner will ever start producing
1268 non-trivial clones, we will get trouble with lack of information
1269 about updating self sizes, because size vectors already contains
1270 sizes of the calees. */
1271 gcc_assert (!inlined_to_p || !optimized_out_size);
1273 else
1275 info->entry = vec_safe_copy (info->entry);
1276 if (info->loop_iterations)
1278 predicate p = *info->loop_iterations;
1279 info->loop_iterations = NULL;
1280 set_hint_predicate (&info->loop_iterations, p);
1282 if (info->loop_stride)
1284 predicate p = *info->loop_stride;
1285 info->loop_stride = NULL;
1286 set_hint_predicate (&info->loop_stride, p);
1288 if (info->array_index)
1290 predicate p = *info->array_index;
1291 info->array_index = NULL;
1292 set_hint_predicate (&info->array_index, p);
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);
1344 if (symtab->cgraph_max_uid)
1345 node_growth_cache.safe_grow_cleared (symtab->cgraph_max_uid);
1349 /* Free growth caches. */
1351 void
1352 free_growth_caches (void)
1354 edge_growth_cache.release ();
1355 node_growth_cache.release ();
1359 /* Dump edge summaries associated to NODE and recursively to all clones.
1360 Indent by INDENT. */
1362 static void
1363 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1364 struct inline_summary *info)
1366 struct cgraph_edge *edge;
1367 for (edge = node->callees; edge; edge = edge->next_callee)
1369 struct inline_edge_summary *es = inline_edge_summary (edge);
1370 struct cgraph_node *callee = edge->callee->ultimate_alias_target ();
1371 int i;
1373 fprintf (f,
1374 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1375 " time: %2i callee size:%2i stack:%2i",
1376 indent, "", callee->name (), callee->order,
1377 !edge->inline_failed
1378 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1379 indent, "", es->loop_depth, edge->frequency,
1380 es->call_stmt_size, es->call_stmt_time,
1381 (int) inline_summaries->get (callee)->size / INLINE_SIZE_SCALE,
1382 (int) inline_summaries->get (callee)->estimated_stack_size);
1384 if (es->predicate)
1386 fprintf (f, " predicate: ");
1387 dump_predicate (f, info->conds, es->predicate);
1389 else
1390 fprintf (f, "\n");
1391 if (es->param.exists ())
1392 for (i = 0; i < (int) es->param.length (); i++)
1394 int prob = es->param[i].change_prob;
1396 if (!prob)
1397 fprintf (f, "%*s op%i is compile time invariant\n",
1398 indent + 2, "", i);
1399 else if (prob != REG_BR_PROB_BASE)
1400 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1401 prob * 100.0 / REG_BR_PROB_BASE);
1403 if (!edge->inline_failed)
1405 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1406 " callee size %i\n",
1407 indent + 2, "",
1408 (int) inline_summaries->get (callee)->stack_frame_offset,
1409 (int) inline_summaries->get (callee)->estimated_self_stack_size,
1410 (int) inline_summaries->get (callee)->estimated_stack_size);
1411 dump_inline_edge_summary (f, indent + 2, callee, info);
1414 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1416 struct inline_edge_summary *es = inline_edge_summary (edge);
1417 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1418 " time: %2i",
1419 indent, "",
1420 es->loop_depth,
1421 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1422 if (es->predicate)
1424 fprintf (f, "predicate: ");
1425 dump_predicate (f, info->conds, es->predicate);
1427 else
1428 fprintf (f, "\n");
1433 void
1434 dump_inline_summary (FILE *f, struct cgraph_node *node)
1436 if (node->definition)
1438 struct inline_summary *s = inline_summaries->get (node);
1439 size_time_entry *e;
1440 int i;
1441 fprintf (f, "Inline summary for %s/%i", node->name (),
1442 node->order);
1443 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1444 fprintf (f, " always_inline");
1445 if (s->inlinable)
1446 fprintf (f, " inlinable");
1447 fprintf (f, "\n self time: %i\n", s->self_time);
1448 fprintf (f, " global time: %i\n", s->time);
1449 fprintf (f, " self size: %i\n", s->self_size);
1450 fprintf (f, " global size: %i\n", s->size);
1451 fprintf (f, " min size: %i\n", s->min_size);
1452 fprintf (f, " self stack: %i\n",
1453 (int) s->estimated_self_stack_size);
1454 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1455 if (s->growth)
1456 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1457 if (s->scc_no)
1458 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1459 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1461 fprintf (f, " size:%f, time:%f, predicate:",
1462 (double) e->size / INLINE_SIZE_SCALE,
1463 (double) e->time / INLINE_TIME_SCALE);
1464 dump_predicate (f, s->conds, &e->predicate);
1466 if (s->loop_iterations)
1468 fprintf (f, " loop iterations:");
1469 dump_predicate (f, s->conds, s->loop_iterations);
1471 if (s->loop_stride)
1473 fprintf (f, " loop stride:");
1474 dump_predicate (f, s->conds, s->loop_stride);
1476 if (s->array_index)
1478 fprintf (f, " array index:");
1479 dump_predicate (f, s->conds, s->array_index);
1481 fprintf (f, " calls:\n");
1482 dump_inline_edge_summary (f, 4, node, s);
1483 fprintf (f, "\n");
1487 DEBUG_FUNCTION void
1488 debug_inline_summary (struct cgraph_node *node)
1490 dump_inline_summary (stderr, node);
1493 void
1494 dump_inline_summaries (FILE *f)
1496 struct cgraph_node *node;
1498 FOR_EACH_DEFINED_FUNCTION (node)
1499 if (!node->global.inlined_to)
1500 dump_inline_summary (f, node);
1503 /* Give initial reasons why inlining would fail on EDGE. This gets either
1504 nullified or usually overwritten by more precise reasons later. */
1506 void
1507 initialize_inline_failed (struct cgraph_edge *e)
1509 struct cgraph_node *callee = e->callee;
1511 if (e->indirect_unknown_callee)
1512 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1513 else if (!callee->definition)
1514 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1515 else if (callee->local.redefined_extern_inline)
1516 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1517 else if (e->call_stmt_cannot_inline_p)
1518 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1519 else if (cfun && fn_contains_cilk_spawn_p (cfun))
1520 /* We can't inline if the function is spawing a function. */
1521 e->inline_failed = CIF_FUNCTION_NOT_INLINABLE;
1522 else
1523 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1526 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1527 boolean variable pointed to by DATA. */
1529 static bool
1530 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1531 void *data)
1533 bool *b = (bool *) data;
1534 *b = true;
1535 return true;
1538 /* If OP refers to value of function parameter, return the corresponding
1539 parameter. */
1541 static tree
1542 unmodified_parm_1 (gimple stmt, tree op)
1544 /* SSA_NAME referring to parm default def? */
1545 if (TREE_CODE (op) == SSA_NAME
1546 && SSA_NAME_IS_DEFAULT_DEF (op)
1547 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1548 return SSA_NAME_VAR (op);
1549 /* Non-SSA parm reference? */
1550 if (TREE_CODE (op) == PARM_DECL)
1552 bool modified = false;
1554 ao_ref refd;
1555 ao_ref_init (&refd, op);
1556 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1557 NULL);
1558 if (!modified)
1559 return op;
1561 return NULL_TREE;
1564 /* If OP refers to value of function parameter, return the corresponding
1565 parameter. Also traverse chains of SSA register assignments. */
1567 static tree
1568 unmodified_parm (gimple stmt, tree op)
1570 tree res = unmodified_parm_1 (stmt, op);
1571 if (res)
1572 return res;
1574 if (TREE_CODE (op) == SSA_NAME
1575 && !SSA_NAME_IS_DEFAULT_DEF (op)
1576 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1577 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1578 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1579 return NULL_TREE;
1582 /* If OP refers to a value of a function parameter or value loaded from an
1583 aggregate passed to a parameter (either by value or reference), return TRUE
1584 and store the number of the parameter to *INDEX_P and information whether
1585 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1586 the function parameters, STMT is the statement in which OP is used or
1587 loaded. */
1589 static bool
1590 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1591 gimple stmt, tree op, int *index_p,
1592 struct agg_position_info *aggpos)
1594 tree res = unmodified_parm_1 (stmt, op);
1596 gcc_checking_assert (aggpos);
1597 if (res)
1599 *index_p = ipa_get_param_decl_index (info, res);
1600 if (*index_p < 0)
1601 return false;
1602 aggpos->agg_contents = false;
1603 aggpos->by_ref = false;
1604 return true;
1607 if (TREE_CODE (op) == SSA_NAME)
1609 if (SSA_NAME_IS_DEFAULT_DEF (op)
1610 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1611 return false;
1612 stmt = SSA_NAME_DEF_STMT (op);
1613 op = gimple_assign_rhs1 (stmt);
1614 if (!REFERENCE_CLASS_P (op))
1615 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1616 aggpos);
1619 aggpos->agg_contents = true;
1620 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1621 &aggpos->by_ref);
1624 /* See if statement might disappear after inlining.
1625 0 - means not eliminated
1626 1 - half of statements goes away
1627 2 - for sure it is eliminated.
1628 We are not terribly sophisticated, basically looking for simple abstraction
1629 penalty wrappers. */
1631 static int
1632 eliminated_by_inlining_prob (gimple stmt)
1634 enum gimple_code code = gimple_code (stmt);
1635 enum tree_code rhs_code;
1637 if (!optimize)
1638 return 0;
1640 switch (code)
1642 case GIMPLE_RETURN:
1643 return 2;
1644 case GIMPLE_ASSIGN:
1645 if (gimple_num_ops (stmt) != 2)
1646 return 0;
1648 rhs_code = gimple_assign_rhs_code (stmt);
1650 /* Casts of parameters, loads from parameters passed by reference
1651 and stores to return value or parameters are often free after
1652 inlining dua to SRA and further combining.
1653 Assume that half of statements goes away. */
1654 if (CONVERT_EXPR_CODE_P (rhs_code)
1655 || rhs_code == VIEW_CONVERT_EXPR
1656 || rhs_code == ADDR_EXPR
1657 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1659 tree rhs = gimple_assign_rhs1 (stmt);
1660 tree lhs = gimple_assign_lhs (stmt);
1661 tree inner_rhs = get_base_address (rhs);
1662 tree inner_lhs = get_base_address (lhs);
1663 bool rhs_free = false;
1664 bool lhs_free = false;
1666 if (!inner_rhs)
1667 inner_rhs = rhs;
1668 if (!inner_lhs)
1669 inner_lhs = lhs;
1671 /* Reads of parameter are expected to be free. */
1672 if (unmodified_parm (stmt, inner_rhs))
1673 rhs_free = true;
1674 /* Match expressions of form &this->field. Those will most likely
1675 combine with something upstream after inlining. */
1676 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1678 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1679 if (TREE_CODE (op) == PARM_DECL)
1680 rhs_free = true;
1681 else if (TREE_CODE (op) == MEM_REF
1682 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1683 rhs_free = true;
1686 /* When parameter is not SSA register because its address is taken
1687 and it is just copied into one, the statement will be completely
1688 free after inlining (we will copy propagate backward). */
1689 if (rhs_free && is_gimple_reg (lhs))
1690 return 2;
1692 /* Reads of parameters passed by reference
1693 expected to be free (i.e. optimized out after inlining). */
1694 if (TREE_CODE (inner_rhs) == MEM_REF
1695 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1696 rhs_free = true;
1698 /* Copying parameter passed by reference into gimple register is
1699 probably also going to copy propagate, but we can't be quite
1700 sure. */
1701 if (rhs_free && is_gimple_reg (lhs))
1702 lhs_free = true;
1704 /* Writes to parameters, parameters passed by value and return value
1705 (either dirrectly or passed via invisible reference) are free.
1707 TODO: We ought to handle testcase like
1708 struct a {int a,b;};
1709 struct a
1710 retrurnsturct (void)
1712 struct a a ={1,2};
1713 return a;
1716 This translate into:
1718 retrurnsturct ()
1720 int a$b;
1721 int a$a;
1722 struct a a;
1723 struct a D.2739;
1725 <bb 2>:
1726 D.2739.a = 1;
1727 D.2739.b = 2;
1728 return D.2739;
1731 For that we either need to copy ipa-split logic detecting writes
1732 to return value. */
1733 if (TREE_CODE (inner_lhs) == PARM_DECL
1734 || TREE_CODE (inner_lhs) == RESULT_DECL
1735 || (TREE_CODE (inner_lhs) == MEM_REF
1736 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1737 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1738 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1739 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1740 (inner_lhs,
1741 0))) == RESULT_DECL))))
1742 lhs_free = true;
1743 if (lhs_free
1744 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1745 rhs_free = true;
1746 if (lhs_free && rhs_free)
1747 return 1;
1749 return 0;
1750 default:
1751 return 0;
1756 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1757 predicates to the CFG edges. */
1759 static void
1760 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1761 struct inline_summary *summary,
1762 basic_block bb)
1764 gimple last;
1765 tree op;
1766 int index;
1767 struct agg_position_info aggpos;
1768 enum tree_code code, inverted_code;
1769 edge e;
1770 edge_iterator ei;
1771 gimple set_stmt;
1772 tree op2;
1774 last = last_stmt (bb);
1775 if (!last || gimple_code (last) != GIMPLE_COND)
1776 return;
1777 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1778 return;
1779 op = gimple_cond_lhs (last);
1780 /* TODO: handle conditionals like
1781 var = op0 < 4;
1782 if (var != 0). */
1783 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1785 code = gimple_cond_code (last);
1786 inverted_code = invert_tree_comparison (code, HONOR_NANS (op));
1788 FOR_EACH_EDGE (e, ei, bb->succs)
1790 enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE
1791 ? code : inverted_code);
1792 /* invert_tree_comparison will return ERROR_MARK on FP
1793 comparsions that are not EQ/NE instead of returning proper
1794 unordered one. Be sure it is not confused with NON_CONSTANT. */
1795 if (this_code != ERROR_MARK)
1797 struct predicate p = add_condition (summary, index, &aggpos,
1798 this_code,
1799 gimple_cond_rhs (last));
1800 e->aux = pool_alloc (edge_predicate_pool);
1801 *(struct predicate *) e->aux = p;
1806 if (TREE_CODE (op) != SSA_NAME)
1807 return;
1808 /* Special case
1809 if (builtin_constant_p (op))
1810 constant_code
1811 else
1812 nonconstant_code.
1813 Here we can predicate nonconstant_code. We can't
1814 really handle constant_code since we have no predicate
1815 for this and also the constant code is not known to be
1816 optimized away when inliner doen't see operand is constant.
1817 Other optimizers might think otherwise. */
1818 if (gimple_cond_code (last) != NE_EXPR
1819 || !integer_zerop (gimple_cond_rhs (last)))
1820 return;
1821 set_stmt = SSA_NAME_DEF_STMT (op);
1822 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1823 || gimple_call_num_args (set_stmt) != 1)
1824 return;
1825 op2 = gimple_call_arg (set_stmt, 0);
1826 if (!unmodified_parm_or_parm_agg_item
1827 (info, set_stmt, op2, &index, &aggpos))
1828 return;
1829 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1831 struct predicate p = add_condition (summary, index, &aggpos,
1832 IS_NOT_CONSTANT, NULL_TREE);
1833 e->aux = pool_alloc (edge_predicate_pool);
1834 *(struct predicate *) e->aux = p;
1839 /* If BB ends by a switch we can turn into predicates, attach corresponding
1840 predicates to the CFG edges. */
1842 static void
1843 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1844 struct inline_summary *summary,
1845 basic_block bb)
1847 gimple lastg;
1848 tree op;
1849 int index;
1850 struct agg_position_info aggpos;
1851 edge e;
1852 edge_iterator ei;
1853 size_t n;
1854 size_t case_idx;
1856 lastg = last_stmt (bb);
1857 if (!lastg || gimple_code (lastg) != GIMPLE_SWITCH)
1858 return;
1859 gswitch *last = as_a <gswitch *> (lastg);
1860 op = gimple_switch_index (last);
1861 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1862 return;
1864 FOR_EACH_EDGE (e, ei, bb->succs)
1866 e->aux = pool_alloc (edge_predicate_pool);
1867 *(struct predicate *) e->aux = false_predicate ();
1869 n = gimple_switch_num_labels (last);
1870 for (case_idx = 0; case_idx < n; ++case_idx)
1872 tree cl = gimple_switch_label (last, case_idx);
1873 tree min, max;
1874 struct predicate p;
1876 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1877 min = CASE_LOW (cl);
1878 max = CASE_HIGH (cl);
1880 /* For default we might want to construct predicate that none
1881 of cases is met, but it is bit hard to do not having negations
1882 of conditionals handy. */
1883 if (!min && !max)
1884 p = true_predicate ();
1885 else if (!max)
1886 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1887 else
1889 struct predicate p1, p2;
1890 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1891 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1892 p = and_predicates (summary->conds, &p1, &p2);
1894 *(struct predicate *) e->aux
1895 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1900 /* For each BB in NODE attach to its AUX pointer predicate under
1901 which it is executable. */
1903 static void
1904 compute_bb_predicates (struct cgraph_node *node,
1905 struct ipa_node_params *parms_info,
1906 struct inline_summary *summary)
1908 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1909 bool done = false;
1910 basic_block bb;
1912 FOR_EACH_BB_FN (bb, my_function)
1914 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1915 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1918 /* Entry block is always executable. */
1919 ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1920 = pool_alloc (edge_predicate_pool);
1921 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux
1922 = true_predicate ();
1924 /* A simple dataflow propagation of predicates forward in the CFG.
1925 TODO: work in reverse postorder. */
1926 while (!done)
1928 done = true;
1929 FOR_EACH_BB_FN (bb, my_function)
1931 struct predicate p = false_predicate ();
1932 edge e;
1933 edge_iterator ei;
1934 FOR_EACH_EDGE (e, ei, bb->preds)
1936 if (e->src->aux)
1938 struct predicate this_bb_predicate
1939 = *(struct predicate *) e->src->aux;
1940 if (e->aux)
1941 this_bb_predicate
1942 = and_predicates (summary->conds, &this_bb_predicate,
1943 (struct predicate *) e->aux);
1944 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1945 if (true_predicate_p (&p))
1946 break;
1949 if (false_predicate_p (&p))
1950 gcc_assert (!bb->aux);
1951 else
1953 if (!bb->aux)
1955 done = false;
1956 bb->aux = pool_alloc (edge_predicate_pool);
1957 *((struct predicate *) bb->aux) = p;
1959 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1961 /* This OR operation is needed to ensure monotonous data flow
1962 in the case we hit the limit on number of clauses and the
1963 and/or operations above give approximate answers. */
1964 p = or_predicates (summary->conds, &p, (struct predicate *)bb->aux);
1965 if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1967 done = false;
1968 *((struct predicate *) bb->aux) = p;
1977 /* We keep info about constantness of SSA names. */
1979 typedef struct predicate predicate_t;
1980 /* Return predicate specifying when the STMT might have result that is not
1981 a compile time constant. */
1983 static struct predicate
1984 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1985 struct inline_summary *summary,
1986 tree expr,
1987 vec<predicate_t> nonconstant_names)
1989 tree parm;
1990 int index;
1992 while (UNARY_CLASS_P (expr))
1993 expr = TREE_OPERAND (expr, 0);
1995 parm = unmodified_parm (NULL, expr);
1996 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1997 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1998 if (is_gimple_min_invariant (expr))
1999 return false_predicate ();
2000 if (TREE_CODE (expr) == SSA_NAME)
2001 return nonconstant_names[SSA_NAME_VERSION (expr)];
2002 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
2004 struct predicate p1 = will_be_nonconstant_expr_predicate
2005 (info, summary, TREE_OPERAND (expr, 0),
2006 nonconstant_names);
2007 struct predicate p2;
2008 if (true_predicate_p (&p1))
2009 return p1;
2010 p2 = will_be_nonconstant_expr_predicate (info, summary,
2011 TREE_OPERAND (expr, 1),
2012 nonconstant_names);
2013 return or_predicates (summary->conds, &p1, &p2);
2015 else if (TREE_CODE (expr) == COND_EXPR)
2017 struct predicate p1 = will_be_nonconstant_expr_predicate
2018 (info, summary, TREE_OPERAND (expr, 0),
2019 nonconstant_names);
2020 struct predicate p2;
2021 if (true_predicate_p (&p1))
2022 return p1;
2023 p2 = will_be_nonconstant_expr_predicate (info, summary,
2024 TREE_OPERAND (expr, 1),
2025 nonconstant_names);
2026 if (true_predicate_p (&p2))
2027 return p2;
2028 p1 = or_predicates (summary->conds, &p1, &p2);
2029 p2 = will_be_nonconstant_expr_predicate (info, summary,
2030 TREE_OPERAND (expr, 2),
2031 nonconstant_names);
2032 return or_predicates (summary->conds, &p1, &p2);
2034 else
2036 debug_tree (expr);
2037 gcc_unreachable ();
2039 return false_predicate ();
2043 /* Return predicate specifying when the STMT might have result that is not
2044 a compile time constant. */
2046 static struct predicate
2047 will_be_nonconstant_predicate (struct ipa_node_params *info,
2048 struct inline_summary *summary,
2049 gimple stmt,
2050 vec<predicate_t> nonconstant_names)
2052 struct predicate p = true_predicate ();
2053 ssa_op_iter iter;
2054 tree use;
2055 struct predicate op_non_const;
2056 bool is_load;
2057 int base_index;
2058 struct agg_position_info aggpos;
2060 /* What statments might be optimized away
2061 when their arguments are constant. */
2062 if (gimple_code (stmt) != GIMPLE_ASSIGN
2063 && gimple_code (stmt) != GIMPLE_COND
2064 && gimple_code (stmt) != GIMPLE_SWITCH
2065 && (gimple_code (stmt) != GIMPLE_CALL
2066 || !(gimple_call_flags (stmt) & ECF_CONST)))
2067 return p;
2069 /* Stores will stay anyway. */
2070 if (gimple_store_p (stmt))
2071 return p;
2073 is_load = gimple_assign_load_p (stmt);
2075 /* Loads can be optimized when the value is known. */
2076 if (is_load)
2078 tree op;
2079 gcc_assert (gimple_assign_single_p (stmt));
2080 op = gimple_assign_rhs1 (stmt);
2081 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
2082 &aggpos))
2083 return p;
2085 else
2086 base_index = -1;
2088 /* See if we understand all operands before we start
2089 adding conditionals. */
2090 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2092 tree parm = unmodified_parm (stmt, use);
2093 /* For arguments we can build a condition. */
2094 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2095 continue;
2096 if (TREE_CODE (use) != SSA_NAME)
2097 return p;
2098 /* If we know when operand is constant,
2099 we still can say something useful. */
2100 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2101 continue;
2102 return p;
2105 if (is_load)
2106 op_non_const =
2107 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2108 else
2109 op_non_const = false_predicate ();
2110 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2112 tree parm = unmodified_parm (stmt, use);
2113 int index;
2115 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2117 if (index != base_index)
2118 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2119 else
2120 continue;
2122 else
2123 p = nonconstant_names[SSA_NAME_VERSION (use)];
2124 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2126 if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL)
2127 && gimple_op (stmt, 0)
2128 && TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME)
2129 nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))]
2130 = op_non_const;
2131 return op_non_const;
2134 struct record_modified_bb_info
2136 bitmap bb_set;
2137 gimple stmt;
2140 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2141 set except for info->stmt. */
2143 static bool
2144 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2146 struct record_modified_bb_info *info =
2147 (struct record_modified_bb_info *) data;
2148 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2149 return false;
2150 bitmap_set_bit (info->bb_set,
2151 SSA_NAME_IS_DEFAULT_DEF (vdef)
2152 ? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index
2153 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2154 return false;
2157 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2158 will change since last invocation of STMT.
2160 Value 0 is reserved for compile time invariants.
2161 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2162 ought to be REG_BR_PROB_BASE / estimated_iters. */
2164 static int
2165 param_change_prob (gimple stmt, int i)
2167 tree op = gimple_call_arg (stmt, i);
2168 basic_block bb = gimple_bb (stmt);
2169 tree base;
2171 /* Global invariants neve change. */
2172 if (is_gimple_min_invariant (op))
2173 return 0;
2174 /* We would have to do non-trivial analysis to really work out what
2175 is the probability of value to change (i.e. when init statement
2176 is in a sibling loop of the call).
2178 We do an conservative estimate: when call is executed N times more often
2179 than the statement defining value, we take the frequency 1/N. */
2180 if (TREE_CODE (op) == SSA_NAME)
2182 int init_freq;
2184 if (!bb->frequency)
2185 return REG_BR_PROB_BASE;
2187 if (SSA_NAME_IS_DEFAULT_DEF (op))
2188 init_freq = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2189 else
2190 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2192 if (!init_freq)
2193 init_freq = 1;
2194 if (init_freq < bb->frequency)
2195 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2196 else
2197 return REG_BR_PROB_BASE;
2200 base = get_base_address (op);
2201 if (base)
2203 ao_ref refd;
2204 int max;
2205 struct record_modified_bb_info info;
2206 bitmap_iterator bi;
2207 unsigned index;
2208 tree init = ctor_for_folding (base);
2210 if (init != error_mark_node)
2211 return 0;
2212 if (!bb->frequency)
2213 return REG_BR_PROB_BASE;
2214 ao_ref_init (&refd, op);
2215 info.stmt = stmt;
2216 info.bb_set = BITMAP_ALLOC (NULL);
2217 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2218 NULL);
2219 if (bitmap_bit_p (info.bb_set, bb->index))
2221 BITMAP_FREE (info.bb_set);
2222 return REG_BR_PROB_BASE;
2225 /* Assume that every memory is initialized at entry.
2226 TODO: Can we easilly determine if value is always defined
2227 and thus we may skip entry block? */
2228 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency)
2229 max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
2230 else
2231 max = 1;
2233 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2234 max = MIN (max, BASIC_BLOCK_FOR_FN (cfun, index)->frequency);
2236 BITMAP_FREE (info.bb_set);
2237 if (max < bb->frequency)
2238 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2239 else
2240 return REG_BR_PROB_BASE;
2242 return REG_BR_PROB_BASE;
2245 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2246 sub-graph and if the predicate the condition depends on is known. If so,
2247 return true and store the pointer the predicate in *P. */
2249 static bool
2250 phi_result_unknown_predicate (struct ipa_node_params *info,
2251 inline_summary *summary, basic_block bb,
2252 struct predicate *p,
2253 vec<predicate_t> nonconstant_names)
2255 edge e;
2256 edge_iterator ei;
2257 basic_block first_bb = NULL;
2258 gimple stmt;
2260 if (single_pred_p (bb))
2262 *p = false_predicate ();
2263 return true;
2266 FOR_EACH_EDGE (e, ei, bb->preds)
2268 if (single_succ_p (e->src))
2270 if (!single_pred_p (e->src))
2271 return false;
2272 if (!first_bb)
2273 first_bb = single_pred (e->src);
2274 else if (single_pred (e->src) != first_bb)
2275 return false;
2277 else
2279 if (!first_bb)
2280 first_bb = e->src;
2281 else if (e->src != first_bb)
2282 return false;
2286 if (!first_bb)
2287 return false;
2289 stmt = last_stmt (first_bb);
2290 if (!stmt
2291 || gimple_code (stmt) != GIMPLE_COND
2292 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2293 return false;
2295 *p = will_be_nonconstant_expr_predicate (info, summary,
2296 gimple_cond_lhs (stmt),
2297 nonconstant_names);
2298 if (true_predicate_p (p))
2299 return false;
2300 else
2301 return true;
2304 /* Given a PHI statement in a function described by inline properties SUMMARY
2305 and *P being the predicate describing whether the selected PHI argument is
2306 known, store a predicate for the result of the PHI statement into
2307 NONCONSTANT_NAMES, if possible. */
2309 static void
2310 predicate_for_phi_result (struct inline_summary *summary, gphi *phi,
2311 struct predicate *p,
2312 vec<predicate_t> nonconstant_names)
2314 unsigned i;
2316 for (i = 0; i < gimple_phi_num_args (phi); i++)
2318 tree arg = gimple_phi_arg (phi, i)->def;
2319 if (!is_gimple_min_invariant (arg))
2321 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2322 *p = or_predicates (summary->conds, p,
2323 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2324 if (true_predicate_p (p))
2325 return;
2329 if (dump_file && (dump_flags & TDF_DETAILS))
2331 fprintf (dump_file, "\t\tphi predicate: ");
2332 dump_predicate (dump_file, summary->conds, p);
2334 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2337 /* Return predicate specifying when array index in access OP becomes non-constant. */
2339 static struct predicate
2340 array_index_predicate (inline_summary *info,
2341 vec< predicate_t> nonconstant_names, tree op)
2343 struct predicate p = false_predicate ();
2344 while (handled_component_p (op))
2346 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2348 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2349 p = or_predicates (info->conds, &p,
2350 &nonconstant_names[SSA_NAME_VERSION
2351 (TREE_OPERAND (op, 1))]);
2353 op = TREE_OPERAND (op, 0);
2355 return p;
2358 /* For a typical usage of __builtin_expect (a<b, 1), we
2359 may introduce an extra relation stmt:
2360 With the builtin, we have
2361 t1 = a <= b;
2362 t2 = (long int) t1;
2363 t3 = __builtin_expect (t2, 1);
2364 if (t3 != 0)
2365 goto ...
2366 Without the builtin, we have
2367 if (a<=b)
2368 goto...
2369 This affects the size/time estimation and may have
2370 an impact on the earlier inlining.
2371 Here find this pattern and fix it up later. */
2373 static gimple
2374 find_foldable_builtin_expect (basic_block bb)
2376 gimple_stmt_iterator bsi;
2378 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2380 gimple stmt = gsi_stmt (bsi);
2381 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT)
2382 || (is_gimple_call (stmt)
2383 && gimple_call_internal_p (stmt)
2384 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
2386 tree var = gimple_call_lhs (stmt);
2387 tree arg = gimple_call_arg (stmt, 0);
2388 use_operand_p use_p;
2389 gimple use_stmt;
2390 bool match = false;
2391 bool done = false;
2393 if (!var || !arg)
2394 continue;
2395 gcc_assert (TREE_CODE (var) == SSA_NAME);
2397 while (TREE_CODE (arg) == SSA_NAME)
2399 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2400 if (!is_gimple_assign (stmt_tmp))
2401 break;
2402 switch (gimple_assign_rhs_code (stmt_tmp))
2404 case LT_EXPR:
2405 case LE_EXPR:
2406 case GT_EXPR:
2407 case GE_EXPR:
2408 case EQ_EXPR:
2409 case NE_EXPR:
2410 match = true;
2411 done = true;
2412 break;
2413 CASE_CONVERT:
2414 break;
2415 default:
2416 done = true;
2417 break;
2419 if (done)
2420 break;
2421 arg = gimple_assign_rhs1 (stmt_tmp);
2424 if (match && single_imm_use (var, &use_p, &use_stmt)
2425 && gimple_code (use_stmt) == GIMPLE_COND)
2426 return use_stmt;
2429 return NULL;
2432 /* Return true when the basic blocks contains only clobbers followed by RESX.
2433 Such BBs are kept around to make removal of dead stores possible with
2434 presence of EH and will be optimized out by optimize_clobbers later in the
2435 game.
2437 NEED_EH is used to recurse in case the clobber has non-EH predecestors
2438 that can be clobber only, too.. When it is false, the RESX is not necessary
2439 on the end of basic block. */
2441 static bool
2442 clobber_only_eh_bb_p (basic_block bb, bool need_eh = true)
2444 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2445 edge_iterator ei;
2446 edge e;
2448 if (need_eh)
2450 if (gsi_end_p (gsi))
2451 return false;
2452 if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX)
2453 return false;
2454 gsi_prev (&gsi);
2456 else if (!single_succ_p (bb))
2457 return false;
2459 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2461 gimple stmt = gsi_stmt (gsi);
2462 if (is_gimple_debug (stmt))
2463 continue;
2464 if (gimple_clobber_p (stmt))
2465 continue;
2466 if (gimple_code (stmt) == GIMPLE_LABEL)
2467 break;
2468 return false;
2471 /* See if all predecestors are either throws or clobber only BBs. */
2472 FOR_EACH_EDGE (e, ei, bb->preds)
2473 if (!(e->flags & EDGE_EH)
2474 && !clobber_only_eh_bb_p (e->src, false))
2475 return false;
2477 return true;
2480 /* Compute function body size parameters for NODE.
2481 When EARLY is true, we compute only simple summaries without
2482 non-trivial predicates to drive the early inliner. */
2484 static void
2485 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2487 gcov_type time = 0;
2488 /* Estimate static overhead for function prologue/epilogue and alignment. */
2489 int size = 2;
2490 /* Benefits are scaled by probability of elimination that is in range
2491 <0,2>. */
2492 basic_block bb;
2493 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
2494 int freq;
2495 struct inline_summary *info = inline_summaries->get (node);
2496 struct predicate bb_predicate;
2497 struct ipa_node_params *parms_info = NULL;
2498 vec<predicate_t> nonconstant_names = vNULL;
2499 int nblocks, n;
2500 int *order;
2501 predicate array_index = true_predicate ();
2502 gimple fix_builtin_expect_stmt;
2504 info->conds = NULL;
2505 info->entry = NULL;
2507 /* When optimizing and analyzing for IPA inliner, initialize loop optimizer
2508 so we can produce proper inline hints.
2510 When optimizing and analyzing for early inliner, initialize node params
2511 so we can produce correct BB predicates. */
2513 if (opt_for_fn (node->decl, optimize))
2515 calculate_dominance_info (CDI_DOMINATORS);
2516 if (!early)
2517 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2518 else
2520 ipa_check_create_node_params ();
2521 ipa_initialize_node_params (node);
2524 if (ipa_node_params_sum)
2526 parms_info = IPA_NODE_REF (node);
2527 nonconstant_names.safe_grow_cleared
2528 (SSANAMES (my_function)->length ());
2532 if (dump_file)
2533 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2534 node->name ());
2536 /* When we run into maximal number of entries, we assign everything to the
2537 constant truth case. Be sure to have it in list. */
2538 bb_predicate = true_predicate ();
2539 account_size_time (info, 0, 0, &bb_predicate);
2541 bb_predicate = not_inlined_predicate ();
2542 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2544 gcc_assert (my_function && my_function->cfg);
2545 if (parms_info)
2546 compute_bb_predicates (node, parms_info, info);
2547 gcc_assert (cfun == my_function);
2548 order = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2549 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2550 for (n = 0; n < nblocks; n++)
2552 bb = BASIC_BLOCK_FOR_FN (cfun, order[n]);
2553 freq = compute_call_stmt_bb_frequency (node->decl, bb);
2554 if (clobber_only_eh_bb_p (bb))
2556 if (dump_file && (dump_flags & TDF_DETAILS))
2557 fprintf (dump_file, "\n Ignoring BB %i;"
2558 " it will be optimized away by cleanup_clobbers\n",
2559 bb->index);
2560 continue;
2563 /* TODO: Obviously predicates can be propagated down across CFG. */
2564 if (parms_info)
2566 if (bb->aux)
2567 bb_predicate = *(struct predicate *) bb->aux;
2568 else
2569 bb_predicate = false_predicate ();
2571 else
2572 bb_predicate = true_predicate ();
2574 if (dump_file && (dump_flags & TDF_DETAILS))
2576 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2577 dump_predicate (dump_file, info->conds, &bb_predicate);
2580 if (parms_info && nonconstant_names.exists ())
2582 struct predicate phi_predicate;
2583 bool first_phi = true;
2585 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
2586 gsi_next (&bsi))
2588 if (first_phi
2589 && !phi_result_unknown_predicate (parms_info, info, bb,
2590 &phi_predicate,
2591 nonconstant_names))
2592 break;
2593 first_phi = false;
2594 if (dump_file && (dump_flags & TDF_DETAILS))
2596 fprintf (dump_file, " ");
2597 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2599 predicate_for_phi_result (info, bsi.phi (), &phi_predicate,
2600 nonconstant_names);
2604 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2606 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
2607 gsi_next (&bsi))
2609 gimple stmt = gsi_stmt (bsi);
2610 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2611 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2612 int prob;
2613 struct predicate will_be_nonconstant;
2615 /* This relation stmt should be folded after we remove
2616 buildin_expect call. Adjust the cost here. */
2617 if (stmt == fix_builtin_expect_stmt)
2619 this_size--;
2620 this_time--;
2623 if (dump_file && (dump_flags & TDF_DETAILS))
2625 fprintf (dump_file, " ");
2626 print_gimple_stmt (dump_file, stmt, 0, 0);
2627 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2628 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2629 this_time);
2632 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2634 struct predicate this_array_index;
2635 this_array_index =
2636 array_index_predicate (info, nonconstant_names,
2637 gimple_assign_rhs1 (stmt));
2638 if (!false_predicate_p (&this_array_index))
2639 array_index =
2640 and_predicates (info->conds, &array_index,
2641 &this_array_index);
2643 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2645 struct predicate this_array_index;
2646 this_array_index =
2647 array_index_predicate (info, nonconstant_names,
2648 gimple_get_lhs (stmt));
2649 if (!false_predicate_p (&this_array_index))
2650 array_index =
2651 and_predicates (info->conds, &array_index,
2652 &this_array_index);
2656 if (is_gimple_call (stmt)
2657 && !gimple_call_internal_p (stmt))
2659 struct cgraph_edge *edge = node->get_edge (stmt);
2660 struct inline_edge_summary *es = inline_edge_summary (edge);
2662 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2663 resolved as constant. We however don't want to optimize
2664 out the cgraph edges. */
2665 if (nonconstant_names.exists ()
2666 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2667 && gimple_call_lhs (stmt)
2668 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2670 struct predicate false_p = false_predicate ();
2671 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2672 = false_p;
2674 if (ipa_node_params_sum)
2676 int count = gimple_call_num_args (stmt);
2677 int i;
2679 if (count)
2680 es->param.safe_grow_cleared (count);
2681 for (i = 0; i < count; i++)
2683 int prob = param_change_prob (stmt, i);
2684 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2685 es->param[i].change_prob = prob;
2689 es->call_stmt_size = this_size;
2690 es->call_stmt_time = this_time;
2691 es->loop_depth = bb_loop_depth (bb);
2692 edge_set_predicate (edge, &bb_predicate);
2695 /* TODO: When conditional jump or swithc is known to be constant, but
2696 we did not translate it into the predicates, we really can account
2697 just maximum of the possible paths. */
2698 if (parms_info)
2699 will_be_nonconstant
2700 = will_be_nonconstant_predicate (parms_info, info,
2701 stmt, nonconstant_names);
2702 if (this_time || this_size)
2704 struct predicate p;
2706 this_time *= freq;
2708 prob = eliminated_by_inlining_prob (stmt);
2709 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2710 fprintf (dump_file,
2711 "\t\t50%% will be eliminated by inlining\n");
2712 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2713 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2715 if (parms_info)
2716 p = and_predicates (info->conds, &bb_predicate,
2717 &will_be_nonconstant);
2718 else
2719 p = true_predicate ();
2721 if (!false_predicate_p (&p)
2722 || (is_gimple_call (stmt)
2723 && !false_predicate_p (&bb_predicate)))
2725 time += this_time;
2726 size += this_size;
2727 if (time > MAX_TIME * INLINE_TIME_SCALE)
2728 time = MAX_TIME * INLINE_TIME_SCALE;
2731 /* We account everything but the calls. Calls have their own
2732 size/time info attached to cgraph edges. This is necessary
2733 in order to make the cost disappear after inlining. */
2734 if (!is_gimple_call (stmt))
2736 if (prob)
2738 struct predicate ip = not_inlined_predicate ();
2739 ip = and_predicates (info->conds, &ip, &p);
2740 account_size_time (info, this_size * prob,
2741 this_time * prob, &ip);
2743 if (prob != 2)
2744 account_size_time (info, this_size * (2 - prob),
2745 this_time * (2 - prob), &p);
2748 gcc_assert (time >= 0);
2749 gcc_assert (size >= 0);
2753 set_hint_predicate (&inline_summaries->get (node)->array_index, array_index);
2754 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2755 if (time > MAX_TIME)
2756 time = MAX_TIME;
2757 free (order);
2759 if (nonconstant_names.exists () && !early)
2761 struct loop *loop;
2762 predicate loop_iterations = true_predicate ();
2763 predicate loop_stride = true_predicate ();
2765 if (dump_file && (dump_flags & TDF_DETAILS))
2766 flow_loops_dump (dump_file, NULL, 0);
2767 scev_initialize ();
2768 FOR_EACH_LOOP (loop, 0)
2770 vec<edge> exits;
2771 edge ex;
2772 unsigned int j, i;
2773 struct tree_niter_desc niter_desc;
2774 basic_block *body = get_loop_body (loop);
2775 bb_predicate = *(struct predicate *) loop->header->aux;
2777 exits = get_loop_exit_edges (loop);
2778 FOR_EACH_VEC_ELT (exits, j, ex)
2779 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2780 && !is_gimple_min_invariant (niter_desc.niter))
2782 predicate will_be_nonconstant
2783 = will_be_nonconstant_expr_predicate (parms_info, info,
2784 niter_desc.niter,
2785 nonconstant_names);
2786 if (!true_predicate_p (&will_be_nonconstant))
2787 will_be_nonconstant = and_predicates (info->conds,
2788 &bb_predicate,
2789 &will_be_nonconstant);
2790 if (!true_predicate_p (&will_be_nonconstant)
2791 && !false_predicate_p (&will_be_nonconstant))
2792 /* This is slightly inprecise. We may want to represent each
2793 loop with independent predicate. */
2794 loop_iterations =
2795 and_predicates (info->conds, &loop_iterations,
2796 &will_be_nonconstant);
2798 exits.release ();
2800 for (i = 0; i < loop->num_nodes; i++)
2802 gimple_stmt_iterator gsi;
2803 bb_predicate = *(struct predicate *) body[i]->aux;
2804 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2805 gsi_next (&gsi))
2807 gimple stmt = gsi_stmt (gsi);
2808 affine_iv iv;
2809 ssa_op_iter iter;
2810 tree use;
2812 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2814 predicate will_be_nonconstant;
2816 if (!simple_iv
2817 (loop, loop_containing_stmt (stmt), use, &iv, true)
2818 || is_gimple_min_invariant (iv.step))
2819 continue;
2820 will_be_nonconstant
2821 = will_be_nonconstant_expr_predicate (parms_info, info,
2822 iv.step,
2823 nonconstant_names);
2824 if (!true_predicate_p (&will_be_nonconstant))
2825 will_be_nonconstant
2826 = and_predicates (info->conds,
2827 &bb_predicate,
2828 &will_be_nonconstant);
2829 if (!true_predicate_p (&will_be_nonconstant)
2830 && !false_predicate_p (&will_be_nonconstant))
2831 /* This is slightly inprecise. We may want to represent
2832 each loop with independent predicate. */
2833 loop_stride =
2834 and_predicates (info->conds, &loop_stride,
2835 &will_be_nonconstant);
2839 free (body);
2841 set_hint_predicate (&inline_summaries->get (node)->loop_iterations,
2842 loop_iterations);
2843 set_hint_predicate (&inline_summaries->get (node)->loop_stride, loop_stride);
2844 scev_finalize ();
2846 FOR_ALL_BB_FN (bb, my_function)
2848 edge e;
2849 edge_iterator ei;
2851 if (bb->aux)
2852 pool_free (edge_predicate_pool, bb->aux);
2853 bb->aux = NULL;
2854 FOR_EACH_EDGE (e, ei, bb->succs)
2856 if (e->aux)
2857 pool_free (edge_predicate_pool, e->aux);
2858 e->aux = NULL;
2861 inline_summaries->get (node)->self_time = time;
2862 inline_summaries->get (node)->self_size = size;
2863 nonconstant_names.release ();
2864 if (opt_for_fn (node->decl, optimize))
2866 if (!early)
2867 loop_optimizer_finalize ();
2868 else if (!ipa_edge_args_vector)
2869 ipa_free_all_node_params ();
2870 free_dominance_info (CDI_DOMINATORS);
2872 if (dump_file)
2874 fprintf (dump_file, "\n");
2875 dump_inline_summary (dump_file, node);
2880 /* Compute parameters of functions used by inliner.
2881 EARLY is true when we compute parameters for the early inliner */
2883 void
2884 compute_inline_parameters (struct cgraph_node *node, bool early)
2886 HOST_WIDE_INT self_stack_size;
2887 struct cgraph_edge *e;
2888 struct inline_summary *info;
2890 gcc_assert (!node->global.inlined_to);
2892 inline_summary_alloc ();
2894 info = inline_summaries->get (node);
2895 reset_inline_summary (node, info);
2897 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2898 Once this happen, we will need to more curefully predict call
2899 statement size. */
2900 if (node->thunk.thunk_p)
2902 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2903 struct predicate t = true_predicate ();
2905 info->inlinable = 0;
2906 node->callees->call_stmt_cannot_inline_p = true;
2907 node->local.can_change_signature = false;
2908 es->call_stmt_time = 1;
2909 es->call_stmt_size = 1;
2910 account_size_time (info, 0, 0, &t);
2911 return;
2914 /* Even is_gimple_min_invariant rely on current_function_decl. */
2915 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2917 /* Estimate the stack size for the function if we're optimizing. */
2918 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2919 info->estimated_self_stack_size = self_stack_size;
2920 info->estimated_stack_size = self_stack_size;
2921 info->stack_frame_offset = 0;
2923 /* Can this function be inlined at all? */
2924 if (!opt_for_fn (node->decl, optimize)
2925 && !lookup_attribute ("always_inline",
2926 DECL_ATTRIBUTES (node->decl)))
2927 info->inlinable = false;
2928 else
2929 info->inlinable = tree_inlinable_function_p (node->decl);
2931 /* Type attributes can use parameter indices to describe them. */
2932 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2933 node->local.can_change_signature = false;
2934 else
2936 /* Otherwise, inlinable functions always can change signature. */
2937 if (info->inlinable)
2938 node->local.can_change_signature = true;
2939 else
2941 /* Functions calling builtin_apply can not change signature. */
2942 for (e = node->callees; e; e = e->next_callee)
2944 tree cdecl = e->callee->decl;
2945 if (DECL_BUILT_IN (cdecl)
2946 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2947 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2948 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2949 break;
2951 node->local.can_change_signature = !e;
2954 estimate_function_body_sizes (node, early);
2956 for (e = node->callees; e; e = e->next_callee)
2957 if (e->callee->comdat_local_p ())
2958 break;
2959 node->calls_comdat_local = (e != NULL);
2961 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2962 info->time = info->self_time;
2963 info->size = info->self_size;
2964 info->stack_frame_offset = 0;
2965 info->estimated_stack_size = info->estimated_self_stack_size;
2966 #ifdef ENABLE_CHECKING
2967 inline_update_overall_summary (node);
2968 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2969 #endif
2971 pop_cfun ();
2975 /* Compute parameters of functions used by inliner using
2976 current_function_decl. */
2978 static unsigned int
2979 compute_inline_parameters_for_current (void)
2981 compute_inline_parameters (cgraph_node::get (current_function_decl), true);
2982 return 0;
2985 namespace {
2987 const pass_data pass_data_inline_parameters =
2989 GIMPLE_PASS, /* type */
2990 "inline_param", /* name */
2991 OPTGROUP_INLINE, /* optinfo_flags */
2992 TV_INLINE_PARAMETERS, /* tv_id */
2993 0, /* properties_required */
2994 0, /* properties_provided */
2995 0, /* properties_destroyed */
2996 0, /* todo_flags_start */
2997 0, /* todo_flags_finish */
3000 class pass_inline_parameters : public gimple_opt_pass
3002 public:
3003 pass_inline_parameters (gcc::context *ctxt)
3004 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
3007 /* opt_pass methods: */
3008 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
3009 virtual unsigned int execute (function *)
3011 return compute_inline_parameters_for_current ();
3014 }; // class pass_inline_parameters
3016 } // anon namespace
3018 gimple_opt_pass *
3019 make_pass_inline_parameters (gcc::context *ctxt)
3021 return new pass_inline_parameters (ctxt);
3025 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS,
3026 KNOWN_CONTEXTS and KNOWN_AGGS. */
3028 static bool
3029 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
3030 int *size, int *time,
3031 vec<tree> known_vals,
3032 vec<ipa_polymorphic_call_context> known_contexts,
3033 vec<ipa_agg_jump_function_p> known_aggs)
3035 tree target;
3036 struct cgraph_node *callee;
3037 struct inline_summary *isummary;
3038 enum availability avail;
3039 bool speculative;
3041 if (!known_vals.exists () && !known_contexts.exists ())
3042 return false;
3043 if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining))
3044 return false;
3046 target = ipa_get_indirect_edge_target (ie, known_vals, known_contexts,
3047 known_aggs, &speculative);
3048 if (!target || speculative)
3049 return false;
3051 /* Account for difference in cost between indirect and direct calls. */
3052 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
3053 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
3054 gcc_checking_assert (*time >= 0);
3055 gcc_checking_assert (*size >= 0);
3057 callee = cgraph_node::get (target);
3058 if (!callee || !callee->definition)
3059 return false;
3060 callee = callee->function_symbol (&avail);
3061 if (avail < AVAIL_AVAILABLE)
3062 return false;
3063 isummary = inline_summaries->get (callee);
3064 return isummary->inlinable;
3067 /* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to
3068 handle edge E with probability PROB.
3069 Set HINTS if edge may be devirtualized.
3070 KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS describe context of the call
3071 site. */
3073 static inline void
3074 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size,
3075 int *time,
3076 int prob,
3077 vec<tree> known_vals,
3078 vec<ipa_polymorphic_call_context> known_contexts,
3079 vec<ipa_agg_jump_function_p> known_aggs,
3080 inline_hints *hints)
3082 struct inline_edge_summary *es = inline_edge_summary (e);
3083 int call_size = es->call_stmt_size;
3084 int call_time = es->call_stmt_time;
3085 int cur_size;
3086 if (!e->callee
3087 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
3088 known_vals, known_contexts, known_aggs)
3089 && hints && e->maybe_hot_p ())
3090 *hints |= INLINE_HINT_indirect_call;
3091 cur_size = call_size * INLINE_SIZE_SCALE;
3092 *size += cur_size;
3093 if (min_size)
3094 *min_size += cur_size;
3095 *time += apply_probability ((gcov_type) call_time, prob)
3096 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
3097 if (*time > MAX_TIME * INLINE_TIME_SCALE)
3098 *time = MAX_TIME * INLINE_TIME_SCALE;
3103 /* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all
3104 calls in NODE. POSSIBLE_TRUTHS, KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
3105 describe context of the call site. */
3107 static void
3108 estimate_calls_size_and_time (struct cgraph_node *node, int *size,
3109 int *min_size, int *time,
3110 inline_hints *hints,
3111 clause_t possible_truths,
3112 vec<tree> known_vals,
3113 vec<ipa_polymorphic_call_context> known_contexts,
3114 vec<ipa_agg_jump_function_p> known_aggs)
3116 struct cgraph_edge *e;
3117 for (e = node->callees; e; e = e->next_callee)
3119 struct inline_edge_summary *es = inline_edge_summary (e);
3121 /* Do not care about zero sized builtins. */
3122 if (e->inline_failed && !es->call_stmt_size)
3124 gcc_checking_assert (!es->call_stmt_time);
3125 continue;
3127 if (!es->predicate
3128 || evaluate_predicate (es->predicate, possible_truths))
3130 if (e->inline_failed)
3132 /* Predicates of calls shall not use NOT_CHANGED codes,
3133 sowe do not need to compute probabilities. */
3134 estimate_edge_size_and_time (e, size,
3135 es->predicate ? NULL : min_size,
3136 time, REG_BR_PROB_BASE,
3137 known_vals, known_contexts,
3138 known_aggs, hints);
3140 else
3141 estimate_calls_size_and_time (e->callee, size, min_size, time,
3142 hints,
3143 possible_truths,
3144 known_vals, known_contexts,
3145 known_aggs);
3148 for (e = node->indirect_calls; e; e = e->next_callee)
3150 struct inline_edge_summary *es = inline_edge_summary (e);
3151 if (!es->predicate
3152 || evaluate_predicate (es->predicate, possible_truths))
3153 estimate_edge_size_and_time (e, size,
3154 es->predicate ? NULL : min_size,
3155 time, REG_BR_PROB_BASE,
3156 known_vals, known_contexts, known_aggs,
3157 hints);
3162 /* Estimate size and time needed to execute NODE assuming
3163 POSSIBLE_TRUTHS clause, and KNOWN_VALS, KNOWN_AGGS and KNOWN_CONTEXTS
3164 information about NODE's arguments. If non-NULL use also probability
3165 information present in INLINE_PARAM_SUMMARY vector.
3166 Additionally detemine hints determined by the context. Finally compute
3167 minimal size needed for the call that is independent on the call context and
3168 can be used for fast estimates. Return the values in RET_SIZE,
3169 RET_MIN_SIZE, RET_TIME and RET_HINTS. */
3171 static void
3172 estimate_node_size_and_time (struct cgraph_node *node,
3173 clause_t possible_truths,
3174 vec<tree> known_vals,
3175 vec<ipa_polymorphic_call_context> known_contexts,
3176 vec<ipa_agg_jump_function_p> known_aggs,
3177 int *ret_size, int *ret_min_size, int *ret_time,
3178 inline_hints *ret_hints,
3179 vec<inline_param_summary>
3180 inline_param_summary)
3182 struct inline_summary *info = inline_summaries->get (node);
3183 size_time_entry *e;
3184 int size = 0;
3185 int time = 0;
3186 int min_size = 0;
3187 inline_hints hints = 0;
3188 int i;
3190 if (dump_file && (dump_flags & TDF_DETAILS))
3192 bool found = false;
3193 fprintf (dump_file, " Estimating body: %s/%i\n"
3194 " Known to be false: ", node->name (),
3195 node->order);
3197 for (i = predicate_not_inlined_condition;
3198 i < (predicate_first_dynamic_condition
3199 + (int) vec_safe_length (info->conds)); i++)
3200 if (!(possible_truths & (1 << i)))
3202 if (found)
3203 fprintf (dump_file, ", ");
3204 found = true;
3205 dump_condition (dump_file, info->conds, i);
3209 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3210 if (evaluate_predicate (&e->predicate, possible_truths))
3212 size += e->size;
3213 gcc_checking_assert (e->time >= 0);
3214 gcc_checking_assert (time >= 0);
3215 if (!inline_param_summary.exists ())
3216 time += e->time;
3217 else
3219 int prob = predicate_probability (info->conds,
3220 &e->predicate,
3221 possible_truths,
3222 inline_param_summary);
3223 gcc_checking_assert (prob >= 0);
3224 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3225 time += apply_probability ((gcov_type) e->time, prob);
3227 if (time > MAX_TIME * INLINE_TIME_SCALE)
3228 time = MAX_TIME * INLINE_TIME_SCALE;
3229 gcc_checking_assert (time >= 0);
3232 gcc_checking_assert (true_predicate_p (&(*info->entry)[0].predicate));
3233 min_size = (*info->entry)[0].size;
3234 gcc_checking_assert (size >= 0);
3235 gcc_checking_assert (time >= 0);
3237 if (info->loop_iterations
3238 && !evaluate_predicate (info->loop_iterations, possible_truths))
3239 hints |= INLINE_HINT_loop_iterations;
3240 if (info->loop_stride
3241 && !evaluate_predicate (info->loop_stride, possible_truths))
3242 hints |= INLINE_HINT_loop_stride;
3243 if (info->array_index
3244 && !evaluate_predicate (info->array_index, possible_truths))
3245 hints |= INLINE_HINT_array_index;
3246 if (info->scc_no)
3247 hints |= INLINE_HINT_in_scc;
3248 if (DECL_DECLARED_INLINE_P (node->decl))
3249 hints |= INLINE_HINT_declared_inline;
3251 estimate_calls_size_and_time (node, &size, &min_size, &time, &hints, possible_truths,
3252 known_vals, known_contexts, known_aggs);
3253 gcc_checking_assert (size >= 0);
3254 gcc_checking_assert (time >= 0);
3255 time = RDIV (time, INLINE_TIME_SCALE);
3256 size = RDIV (size, INLINE_SIZE_SCALE);
3257 min_size = RDIV (min_size, INLINE_SIZE_SCALE);
3259 if (dump_file && (dump_flags & TDF_DETAILS))
3260 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3261 if (ret_time)
3262 *ret_time = time;
3263 if (ret_size)
3264 *ret_size = size;
3265 if (ret_min_size)
3266 *ret_min_size = min_size;
3267 if (ret_hints)
3268 *ret_hints = hints;
3269 return;
3273 /* Estimate size and time needed to execute callee of EDGE assuming that
3274 parameters known to be constant at caller of EDGE are propagated.
3275 KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values
3276 and types for parameters. */
3278 void
3279 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3280 vec<tree> known_vals,
3281 vec<ipa_polymorphic_call_context>
3282 known_contexts,
3283 vec<ipa_agg_jump_function_p> known_aggs,
3284 int *ret_size, int *ret_time,
3285 inline_hints *hints)
3287 clause_t clause;
3289 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3290 known_aggs);
3291 estimate_node_size_and_time (node, clause, known_vals, known_contexts,
3292 known_aggs, ret_size, NULL, ret_time, hints, vNULL);
3295 /* Translate all conditions from callee representation into caller
3296 representation and symbolically evaluate predicate P into new predicate.
3298 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3299 is summary of function predicate P is from. OPERAND_MAP is array giving
3300 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3301 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3302 predicate under which callee is executed. OFFSET_MAP is an array of of
3303 offsets that need to be added to conditions, negative offset means that
3304 conditions relying on values passed by reference have to be discarded
3305 because they might not be preserved (and should be considered offset zero
3306 for other purposes). */
3308 static struct predicate
3309 remap_predicate (struct inline_summary *info,
3310 struct inline_summary *callee_info,
3311 struct predicate *p,
3312 vec<int> operand_map,
3313 vec<int> offset_map,
3314 clause_t possible_truths, struct predicate *toplev_predicate)
3316 int i;
3317 struct predicate out = true_predicate ();
3319 /* True predicate is easy. */
3320 if (true_predicate_p (p))
3321 return *toplev_predicate;
3322 for (i = 0; p->clause[i]; i++)
3324 clause_t clause = p->clause[i];
3325 int cond;
3326 struct predicate clause_predicate = false_predicate ();
3328 gcc_assert (i < MAX_CLAUSES);
3330 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3331 /* Do we have condition we can't disprove? */
3332 if (clause & possible_truths & (1 << cond))
3334 struct predicate cond_predicate;
3335 /* Work out if the condition can translate to predicate in the
3336 inlined function. */
3337 if (cond >= predicate_first_dynamic_condition)
3339 struct condition *c;
3341 c = &(*callee_info->conds)[cond
3343 predicate_first_dynamic_condition];
3344 /* See if we can remap condition operand to caller's operand.
3345 Otherwise give up. */
3346 if (!operand_map.exists ()
3347 || (int) operand_map.length () <= c->operand_num
3348 || operand_map[c->operand_num] == -1
3349 /* TODO: For non-aggregate conditions, adding an offset is
3350 basically an arithmetic jump function processing which
3351 we should support in future. */
3352 || ((!c->agg_contents || !c->by_ref)
3353 && offset_map[c->operand_num] > 0)
3354 || (c->agg_contents && c->by_ref
3355 && offset_map[c->operand_num] < 0))
3356 cond_predicate = true_predicate ();
3357 else
3359 struct agg_position_info ap;
3360 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3361 if (offset_delta < 0)
3363 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3364 offset_delta = 0;
3366 gcc_assert (!c->agg_contents
3367 || c->by_ref || offset_delta == 0);
3368 ap.offset = c->offset + offset_delta;
3369 ap.agg_contents = c->agg_contents;
3370 ap.by_ref = c->by_ref;
3371 cond_predicate = add_condition (info,
3372 operand_map[c->operand_num],
3373 &ap, c->code, c->val);
3376 /* Fixed conditions remains same, construct single
3377 condition predicate. */
3378 else
3380 cond_predicate.clause[0] = 1 << cond;
3381 cond_predicate.clause[1] = 0;
3383 clause_predicate = or_predicates (info->conds, &clause_predicate,
3384 &cond_predicate);
3386 out = and_predicates (info->conds, &out, &clause_predicate);
3388 return and_predicates (info->conds, &out, toplev_predicate);
3392 /* Update summary information of inline clones after inlining.
3393 Compute peak stack usage. */
3395 static void
3396 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3398 struct cgraph_edge *e;
3399 struct inline_summary *callee_info = inline_summaries->get (node);
3400 struct inline_summary *caller_info = inline_summaries->get (node->callers->caller);
3401 HOST_WIDE_INT peak;
3403 callee_info->stack_frame_offset
3404 = caller_info->stack_frame_offset
3405 + caller_info->estimated_self_stack_size;
3406 peak = callee_info->stack_frame_offset
3407 + callee_info->estimated_self_stack_size;
3408 if (inline_summaries->get (node->global.inlined_to)->estimated_stack_size < peak)
3409 inline_summaries->get (node->global.inlined_to)->estimated_stack_size = peak;
3410 ipa_propagate_frequency (node);
3411 for (e = node->callees; e; e = e->next_callee)
3413 if (!e->inline_failed)
3414 inline_update_callee_summaries (e->callee, depth);
3415 inline_edge_summary (e)->loop_depth += depth;
3417 for (e = node->indirect_calls; e; e = e->next_callee)
3418 inline_edge_summary (e)->loop_depth += depth;
3421 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3422 When functoin A is inlined in B and A calls C with parameter that
3423 changes with probability PROB1 and C is known to be passthroug
3424 of argument if B that change with probability PROB2, the probability
3425 of change is now PROB1*PROB2. */
3427 static void
3428 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3429 struct cgraph_edge *edge)
3431 if (ipa_node_params_sum)
3433 int i;
3434 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3435 struct inline_edge_summary *es = inline_edge_summary (edge);
3436 struct inline_edge_summary *inlined_es
3437 = inline_edge_summary (inlined_edge);
3439 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3441 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3442 if (jfunc->type == IPA_JF_PASS_THROUGH
3443 && (ipa_get_jf_pass_through_formal_id (jfunc)
3444 < (int) inlined_es->param.length ()))
3446 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3447 int prob1 = es->param[i].change_prob;
3448 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3449 int prob = combine_probabilities (prob1, prob2);
3451 if (prob1 && prob2 && !prob)
3452 prob = 1;
3454 es->param[i].change_prob = prob;
3460 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3462 Remap predicates of callees of NODE. Rest of arguments match
3463 remap_predicate.
3465 Also update change probabilities. */
3467 static void
3468 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3469 struct cgraph_node *node,
3470 struct inline_summary *info,
3471 struct inline_summary *callee_info,
3472 vec<int> operand_map,
3473 vec<int> offset_map,
3474 clause_t possible_truths,
3475 struct predicate *toplev_predicate)
3477 struct cgraph_edge *e;
3478 for (e = node->callees; e; e = e->next_callee)
3480 struct inline_edge_summary *es = inline_edge_summary (e);
3481 struct predicate p;
3483 if (e->inline_failed)
3485 remap_edge_change_prob (inlined_edge, e);
3487 if (es->predicate)
3489 p = remap_predicate (info, callee_info,
3490 es->predicate, operand_map, offset_map,
3491 possible_truths, toplev_predicate);
3492 edge_set_predicate (e, &p);
3493 /* TODO: We should remove the edge for code that will be
3494 optimized out, but we need to keep verifiers and tree-inline
3495 happy. Make it cold for now. */
3496 if (false_predicate_p (&p))
3498 e->count = 0;
3499 e->frequency = 0;
3502 else
3503 edge_set_predicate (e, toplev_predicate);
3505 else
3506 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3507 operand_map, offset_map, possible_truths,
3508 toplev_predicate);
3510 for (e = node->indirect_calls; e; e = e->next_callee)
3512 struct inline_edge_summary *es = inline_edge_summary (e);
3513 struct predicate p;
3515 remap_edge_change_prob (inlined_edge, e);
3516 if (es->predicate)
3518 p = remap_predicate (info, callee_info,
3519 es->predicate, operand_map, offset_map,
3520 possible_truths, toplev_predicate);
3521 edge_set_predicate (e, &p);
3522 /* TODO: We should remove the edge for code that will be optimized
3523 out, but we need to keep verifiers and tree-inline happy.
3524 Make it cold for now. */
3525 if (false_predicate_p (&p))
3527 e->count = 0;
3528 e->frequency = 0;
3531 else
3532 edge_set_predicate (e, toplev_predicate);
3536 /* Same as remap_predicate, but set result into hint *HINT. */
3538 static void
3539 remap_hint_predicate (struct inline_summary *info,
3540 struct inline_summary *callee_info,
3541 struct predicate **hint,
3542 vec<int> operand_map,
3543 vec<int> offset_map,
3544 clause_t possible_truths,
3545 struct predicate *toplev_predicate)
3547 predicate p;
3549 if (!*hint)
3550 return;
3551 p = remap_predicate (info, callee_info,
3552 *hint,
3553 operand_map, offset_map,
3554 possible_truths, toplev_predicate);
3555 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3557 if (!*hint)
3558 set_hint_predicate (hint, p);
3559 else
3560 **hint = and_predicates (info->conds, *hint, &p);
3564 /* We inlined EDGE. Update summary of the function we inlined into. */
3566 void
3567 inline_merge_summary (struct cgraph_edge *edge)
3569 struct inline_summary *callee_info = inline_summaries->get (edge->callee);
3570 struct cgraph_node *to = (edge->caller->global.inlined_to
3571 ? edge->caller->global.inlined_to : edge->caller);
3572 struct inline_summary *info = inline_summaries->get (to);
3573 clause_t clause = 0; /* not_inline is known to be false. */
3574 size_time_entry *e;
3575 vec<int> operand_map = vNULL;
3576 vec<int> offset_map = vNULL;
3577 int i;
3578 struct predicate toplev_predicate;
3579 struct predicate true_p = true_predicate ();
3580 struct inline_edge_summary *es = inline_edge_summary (edge);
3582 if (es->predicate)
3583 toplev_predicate = *es->predicate;
3584 else
3585 toplev_predicate = true_predicate ();
3587 if (callee_info->conds)
3588 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3589 if (ipa_node_params_sum && callee_info->conds)
3591 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3592 int count = ipa_get_cs_argument_count (args);
3593 int i;
3595 if (count)
3597 operand_map.safe_grow_cleared (count);
3598 offset_map.safe_grow_cleared (count);
3600 for (i = 0; i < count; i++)
3602 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3603 int map = -1;
3605 /* TODO: handle non-NOPs when merging. */
3606 if (jfunc->type == IPA_JF_PASS_THROUGH)
3608 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3609 map = ipa_get_jf_pass_through_formal_id (jfunc);
3610 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3611 offset_map[i] = -1;
3613 else if (jfunc->type == IPA_JF_ANCESTOR)
3615 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3616 if (offset >= 0 && offset < INT_MAX)
3618 map = ipa_get_jf_ancestor_formal_id (jfunc);
3619 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3620 offset = -1;
3621 offset_map[i] = offset;
3624 operand_map[i] = map;
3625 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3628 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3630 struct predicate p = remap_predicate (info, callee_info,
3631 &e->predicate, operand_map,
3632 offset_map, clause,
3633 &toplev_predicate);
3634 if (!false_predicate_p (&p))
3636 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3637 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3638 int prob = predicate_probability (callee_info->conds,
3639 &e->predicate,
3640 clause, es->param);
3641 add_time = apply_probability ((gcov_type) add_time, prob);
3642 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3643 add_time = MAX_TIME * INLINE_TIME_SCALE;
3644 if (prob != REG_BR_PROB_BASE
3645 && dump_file && (dump_flags & TDF_DETAILS))
3647 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3648 (double) prob / REG_BR_PROB_BASE);
3650 account_size_time (info, e->size, add_time, &p);
3653 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3654 offset_map, clause, &toplev_predicate);
3655 remap_hint_predicate (info, callee_info,
3656 &callee_info->loop_iterations,
3657 operand_map, offset_map, clause, &toplev_predicate);
3658 remap_hint_predicate (info, callee_info,
3659 &callee_info->loop_stride,
3660 operand_map, offset_map, clause, &toplev_predicate);
3661 remap_hint_predicate (info, callee_info,
3662 &callee_info->array_index,
3663 operand_map, offset_map, clause, &toplev_predicate);
3665 inline_update_callee_summaries (edge->callee,
3666 inline_edge_summary (edge)->loop_depth);
3668 /* We do not maintain predicates of inlined edges, free it. */
3669 edge_set_predicate (edge, &true_p);
3670 /* Similarly remove param summaries. */
3671 es->param.release ();
3672 operand_map.release ();
3673 offset_map.release ();
3676 /* For performance reasons inline_merge_summary is not updating overall size
3677 and time. Recompute it. */
3679 void
3680 inline_update_overall_summary (struct cgraph_node *node)
3682 struct inline_summary *info = inline_summaries->get (node);
3683 size_time_entry *e;
3684 int i;
3686 info->size = 0;
3687 info->time = 0;
3688 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3690 info->size += e->size, info->time += e->time;
3691 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3692 info->time = MAX_TIME * INLINE_TIME_SCALE;
3694 estimate_calls_size_and_time (node, &info->size, &info->min_size,
3695 &info->time, NULL,
3696 ~(clause_t) (1 << predicate_false_condition),
3697 vNULL, vNULL, vNULL);
3698 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3699 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3702 /* Return hints derrived from EDGE. */
3704 simple_edge_hints (struct cgraph_edge *edge)
3706 int hints = 0;
3707 struct cgraph_node *to = (edge->caller->global.inlined_to
3708 ? edge->caller->global.inlined_to : edge->caller);
3709 if (inline_summaries->get (to)->scc_no
3710 && inline_summaries->get (to)->scc_no == inline_summaries->get (edge->callee)->scc_no
3711 && !edge->recursive_p ())
3712 hints |= INLINE_HINT_same_scc;
3714 if (to->lto_file_data && edge->callee->lto_file_data
3715 && to->lto_file_data != edge->callee->lto_file_data)
3716 hints |= INLINE_HINT_cross_module;
3718 return hints;
3721 /* Estimate the time cost for the caller when inlining EDGE.
3722 Only to be called via estimate_edge_time, that handles the
3723 caching mechanism.
3725 When caching, also update the cache entry. Compute both time and
3726 size, since we always need both metrics eventually. */
3729 do_estimate_edge_time (struct cgraph_edge *edge)
3731 int time;
3732 int size;
3733 inline_hints hints;
3734 struct cgraph_node *callee;
3735 clause_t clause;
3736 vec<tree> known_vals;
3737 vec<ipa_polymorphic_call_context> known_contexts;
3738 vec<ipa_agg_jump_function_p> known_aggs;
3739 struct inline_edge_summary *es = inline_edge_summary (edge);
3740 int min_size;
3742 callee = edge->callee->ultimate_alias_target ();
3744 gcc_checking_assert (edge->inline_failed);
3745 evaluate_properties_for_edge (edge, true,
3746 &clause, &known_vals, &known_contexts,
3747 &known_aggs);
3748 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3749 known_aggs, &size, &min_size, &time, &hints, es->param);
3751 /* When we have profile feedback, we can quite safely identify hot
3752 edges and for those we disable size limits. Don't do that when
3753 probability that caller will call the callee is low however, since it
3754 may hurt optimization of the caller's hot path. */
3755 if (edge->count && edge->maybe_hot_p ()
3756 && (edge->count * 2
3757 > (edge->caller->global.inlined_to
3758 ? edge->caller->global.inlined_to->count : edge->caller->count)))
3759 hints |= INLINE_HINT_known_hot;
3761 known_vals.release ();
3762 known_contexts.release ();
3763 known_aggs.release ();
3764 gcc_checking_assert (size >= 0);
3765 gcc_checking_assert (time >= 0);
3767 /* When caching, update the cache entry. */
3768 if (edge_growth_cache.exists ())
3770 inline_summaries->get (edge->callee)->min_size = min_size;
3771 if ((int) edge_growth_cache.length () <= edge->uid)
3772 edge_growth_cache.safe_grow_cleared (symtab->edges_max_uid);
3773 edge_growth_cache[edge->uid].time = time + (time >= 0);
3775 edge_growth_cache[edge->uid].size = size + (size >= 0);
3776 hints |= simple_edge_hints (edge);
3777 edge_growth_cache[edge->uid].hints = hints + 1;
3779 return time;
3783 /* Return estimated callee growth after inlining EDGE.
3784 Only to be called via estimate_edge_size. */
3787 do_estimate_edge_size (struct cgraph_edge *edge)
3789 int size;
3790 struct cgraph_node *callee;
3791 clause_t clause;
3792 vec<tree> known_vals;
3793 vec<ipa_polymorphic_call_context> known_contexts;
3794 vec<ipa_agg_jump_function_p> known_aggs;
3796 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3798 if (edge_growth_cache.exists ())
3800 do_estimate_edge_time (edge);
3801 size = edge_growth_cache[edge->uid].size;
3802 gcc_checking_assert (size);
3803 return size - (size > 0);
3806 callee = edge->callee->ultimate_alias_target ();
3808 /* Early inliner runs without caching, go ahead and do the dirty work. */
3809 gcc_checking_assert (edge->inline_failed);
3810 evaluate_properties_for_edge (edge, true,
3811 &clause, &known_vals, &known_contexts,
3812 &known_aggs);
3813 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3814 known_aggs, &size, NULL, NULL, NULL, vNULL);
3815 known_vals.release ();
3816 known_contexts.release ();
3817 known_aggs.release ();
3818 return size;
3822 /* Estimate the growth of the caller when inlining EDGE.
3823 Only to be called via estimate_edge_size. */
3825 inline_hints
3826 do_estimate_edge_hints (struct cgraph_edge *edge)
3828 inline_hints hints;
3829 struct cgraph_node *callee;
3830 clause_t clause;
3831 vec<tree> known_vals;
3832 vec<ipa_polymorphic_call_context> known_contexts;
3833 vec<ipa_agg_jump_function_p> known_aggs;
3835 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3837 if (edge_growth_cache.exists ())
3839 do_estimate_edge_time (edge);
3840 hints = edge_growth_cache[edge->uid].hints;
3841 gcc_checking_assert (hints);
3842 return hints - 1;
3845 callee = edge->callee->ultimate_alias_target ();
3847 /* Early inliner runs without caching, go ahead and do the dirty work. */
3848 gcc_checking_assert (edge->inline_failed);
3849 evaluate_properties_for_edge (edge, true,
3850 &clause, &known_vals, &known_contexts,
3851 &known_aggs);
3852 estimate_node_size_and_time (callee, clause, known_vals, known_contexts,
3853 known_aggs, NULL, NULL, NULL, &hints, vNULL);
3854 known_vals.release ();
3855 known_contexts.release ();
3856 known_aggs.release ();
3857 hints |= simple_edge_hints (edge);
3858 return hints;
3862 /* Estimate self time of the function NODE after inlining EDGE. */
3865 estimate_time_after_inlining (struct cgraph_node *node,
3866 struct cgraph_edge *edge)
3868 struct inline_edge_summary *es = inline_edge_summary (edge);
3869 if (!es->predicate || !false_predicate_p (es->predicate))
3871 gcov_type time =
3872 inline_summaries->get (node)->time + estimate_edge_time (edge);
3873 if (time < 0)
3874 time = 0;
3875 if (time > MAX_TIME)
3876 time = MAX_TIME;
3877 return time;
3879 return inline_summaries->get (node)->time;
3883 /* Estimate the size of NODE after inlining EDGE which should be an
3884 edge to either NODE or a call inlined into NODE. */
3887 estimate_size_after_inlining (struct cgraph_node *node,
3888 struct cgraph_edge *edge)
3890 struct inline_edge_summary *es = inline_edge_summary (edge);
3891 if (!es->predicate || !false_predicate_p (es->predicate))
3893 int size = inline_summaries->get (node)->size + estimate_edge_growth (edge);
3894 gcc_assert (size >= 0);
3895 return size;
3897 return inline_summaries->get (node)->size;
3901 struct growth_data
3903 struct cgraph_node *node;
3904 bool self_recursive;
3905 int growth;
3909 /* Worker for do_estimate_growth. Collect growth for all callers. */
3911 static bool
3912 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3914 struct cgraph_edge *e;
3915 struct growth_data *d = (struct growth_data *) data;
3917 for (e = node->callers; e; e = e->next_caller)
3919 gcc_checking_assert (e->inline_failed);
3921 if (e->caller == d->node
3922 || (e->caller->global.inlined_to
3923 && e->caller->global.inlined_to == d->node))
3924 d->self_recursive = true;
3925 d->growth += estimate_edge_growth (e);
3927 return false;
3931 /* Estimate the growth caused by inlining NODE into all callees. */
3934 do_estimate_growth (struct cgraph_node *node)
3936 struct growth_data d = { node, 0, false };
3937 struct inline_summary *info = inline_summaries->get (node);
3939 node->call_for_symbol_thunks_and_aliases (do_estimate_growth_1, &d, true);
3941 /* For self recursive functions the growth estimation really should be
3942 infinity. We don't want to return very large values because the growth
3943 plays various roles in badness computation fractions. Be sure to not
3944 return zero or negative growths. */
3945 if (d.self_recursive)
3946 d.growth = d.growth < info->size ? info->size : d.growth;
3947 else if (DECL_EXTERNAL (node->decl))
3949 else
3951 if (node->will_be_removed_from_program_if_no_direct_calls_p ())
3952 d.growth -= info->size;
3953 /* COMDAT functions are very often not shared across multiple units
3954 since they come from various template instantiations.
3955 Take this into account. */
3956 else if (DECL_COMDAT (node->decl)
3957 && node->can_remove_if_no_direct_calls_p ())
3958 d.growth -= (info->size
3959 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3960 + 50) / 100;
3963 if (node_growth_cache.exists ())
3965 if ((int) node_growth_cache.length () <= node->uid)
3966 node_growth_cache.safe_grow_cleared (symtab->cgraph_max_uid);
3967 node_growth_cache[node->uid] = d.growth + (d.growth >= 0);
3969 return d.growth;
3973 /* Make cheap estimation if growth of NODE is likely positive knowing
3974 EDGE_GROWTH of one particular edge.
3975 We assume that most of other edges will have similar growth
3976 and skip computation if there are too many callers. */
3978 bool
3979 growth_likely_positive (struct cgraph_node *node, int edge_growth ATTRIBUTE_UNUSED)
3981 int max_callers;
3982 int ret;
3983 struct cgraph_edge *e;
3984 gcc_checking_assert (edge_growth > 0);
3986 /* Unlike for functions called once, we play unsafe with
3987 COMDATs. We can allow that since we know functions
3988 in consideration are small (and thus risk is small) and
3989 moreover grow estimates already accounts that COMDAT
3990 functions may or may not disappear when eliminated from
3991 current unit. With good probability making aggressive
3992 choice in all units is going to make overall program
3993 smaller.
3995 Consequently we ask cgraph_can_remove_if_no_direct_calls_p
3996 instead of
3997 cgraph_will_be_removed_from_program_if_no_direct_calls */
3998 if (DECL_EXTERNAL (node->decl)
3999 || !node->can_remove_if_no_direct_calls_p ())
4000 return true;
4002 /* If there is cached value, just go ahead. */
4003 if ((int)node_growth_cache.length () > node->uid
4004 && (ret = node_growth_cache[node->uid]))
4005 return ret > 0;
4006 if (!node->will_be_removed_from_program_if_no_direct_calls_p ()
4007 && (!DECL_COMDAT (node->decl)
4008 || !node->can_remove_if_no_direct_calls_p ()))
4009 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 return true;
4018 return estimate_growth (node) > 0;
4022 /* This function performs intraprocedural analysis in NODE that is required to
4023 inline indirect calls. */
4025 static void
4026 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
4028 ipa_analyze_node (node);
4029 if (dump_file && (dump_flags & TDF_DETAILS))
4031 ipa_print_node_params (dump_file, node);
4032 ipa_print_node_jump_functions (dump_file, node);
4037 /* Note function body size. */
4039 void
4040 inline_analyze_function (struct cgraph_node *node)
4042 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
4044 if (dump_file)
4045 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
4046 node->name (), node->order);
4047 if (opt_for_fn (node->decl, optimize) && !node->thunk.thunk_p)
4048 inline_indirect_intraprocedural_analysis (node);
4049 compute_inline_parameters (node, false);
4050 if (!optimize)
4052 struct cgraph_edge *e;
4053 for (e = node->callees; e; e = e->next_callee)
4055 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
4056 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4057 e->call_stmt_cannot_inline_p = true;
4059 for (e = node->indirect_calls; e; e = e->next_callee)
4061 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
4062 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
4063 e->call_stmt_cannot_inline_p = true;
4067 pop_cfun ();
4071 /* Called when new function is inserted to callgraph late. */
4073 void
4074 inline_summary_t::insert (struct cgraph_node *node, inline_summary *)
4076 inline_analyze_function (node);
4079 /* Note function body size. */
4081 void
4082 inline_generate_summary (void)
4084 struct cgraph_node *node;
4086 /* When not optimizing, do not bother to analyze. Inlining is still done
4087 because edge redirection needs to happen there. */
4088 if (!optimize && !flag_generate_lto && !flag_generate_offload && !flag_wpa)
4089 return;
4091 if (!inline_summaries)
4092 inline_summaries = (inline_summary_t*) inline_summary_t::create_ggc (symtab);
4094 inline_summaries->enable_insertion_hook ();
4096 ipa_register_cgraph_hooks ();
4097 inline_free_summary ();
4099 FOR_EACH_DEFINED_FUNCTION (node)
4100 if (!node->alias)
4101 inline_analyze_function (node);
4105 /* Read predicate from IB. */
4107 static struct predicate
4108 read_predicate (struct lto_input_block *ib)
4110 struct predicate out;
4111 clause_t clause;
4112 int k = 0;
4116 gcc_assert (k <= MAX_CLAUSES);
4117 clause = out.clause[k++] = streamer_read_uhwi (ib);
4119 while (clause);
4121 /* Zero-initialize the remaining clauses in OUT. */
4122 while (k <= MAX_CLAUSES)
4123 out.clause[k++] = 0;
4125 return out;
4129 /* Write inline summary for edge E to OB. */
4131 static void
4132 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
4134 struct inline_edge_summary *es = inline_edge_summary (e);
4135 struct predicate p;
4136 int length, i;
4138 es->call_stmt_size = streamer_read_uhwi (ib);
4139 es->call_stmt_time = streamer_read_uhwi (ib);
4140 es->loop_depth = streamer_read_uhwi (ib);
4141 p = read_predicate (ib);
4142 edge_set_predicate (e, &p);
4143 length = streamer_read_uhwi (ib);
4144 if (length)
4146 es->param.safe_grow_cleared (length);
4147 for (i = 0; i < length; i++)
4148 es->param[i].change_prob = streamer_read_uhwi (ib);
4153 /* Stream in inline summaries from the section. */
4155 static void
4156 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
4157 size_t len)
4159 const struct lto_function_header *header =
4160 (const struct lto_function_header *) data;
4161 const int cfg_offset = sizeof (struct lto_function_header);
4162 const int main_offset = cfg_offset + header->cfg_size;
4163 const int string_offset = main_offset + header->main_size;
4164 struct data_in *data_in;
4165 unsigned int i, count2, j;
4166 unsigned int f_count;
4168 lto_input_block ib ((const char *) data + main_offset, header->main_size);
4170 data_in =
4171 lto_data_in_create (file_data, (const char *) data + string_offset,
4172 header->string_size, vNULL);
4173 f_count = streamer_read_uhwi (&ib);
4174 for (i = 0; i < f_count; i++)
4176 unsigned int index;
4177 struct cgraph_node *node;
4178 struct inline_summary *info;
4179 lto_symtab_encoder_t encoder;
4180 struct bitpack_d bp;
4181 struct cgraph_edge *e;
4182 predicate p;
4184 index = streamer_read_uhwi (&ib);
4185 encoder = file_data->symtab_node_encoder;
4186 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
4187 index));
4188 info = inline_summaries->get (node);
4190 info->estimated_stack_size
4191 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
4192 info->size = info->self_size = streamer_read_uhwi (&ib);
4193 info->time = info->self_time = streamer_read_uhwi (&ib);
4195 bp = streamer_read_bitpack (&ib);
4196 info->inlinable = bp_unpack_value (&bp, 1);
4198 count2 = streamer_read_uhwi (&ib);
4199 gcc_assert (!info->conds);
4200 for (j = 0; j < count2; j++)
4202 struct condition c;
4203 c.operand_num = streamer_read_uhwi (&ib);
4204 c.code = (enum tree_code) streamer_read_uhwi (&ib);
4205 c.val = stream_read_tree (&ib, data_in);
4206 bp = streamer_read_bitpack (&ib);
4207 c.agg_contents = bp_unpack_value (&bp, 1);
4208 c.by_ref = bp_unpack_value (&bp, 1);
4209 if (c.agg_contents)
4210 c.offset = streamer_read_uhwi (&ib);
4211 vec_safe_push (info->conds, c);
4213 count2 = streamer_read_uhwi (&ib);
4214 gcc_assert (!info->entry);
4215 for (j = 0; j < count2; j++)
4217 struct size_time_entry e;
4219 e.size = streamer_read_uhwi (&ib);
4220 e.time = streamer_read_uhwi (&ib);
4221 e.predicate = read_predicate (&ib);
4223 vec_safe_push (info->entry, e);
4226 p = read_predicate (&ib);
4227 set_hint_predicate (&info->loop_iterations, p);
4228 p = read_predicate (&ib);
4229 set_hint_predicate (&info->loop_stride, p);
4230 p = read_predicate (&ib);
4231 set_hint_predicate (&info->array_index, p);
4232 for (e = node->callees; e; e = e->next_callee)
4233 read_inline_edge_summary (&ib, e);
4234 for (e = node->indirect_calls; e; e = e->next_callee)
4235 read_inline_edge_summary (&ib, e);
4238 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
4239 len);
4240 lto_data_in_delete (data_in);
4244 /* Read inline summary. Jump functions are shared among ipa-cp
4245 and inliner, so when ipa-cp is active, we don't need to write them
4246 twice. */
4248 void
4249 inline_read_summary (void)
4251 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4252 struct lto_file_decl_data *file_data;
4253 unsigned int j = 0;
4255 inline_summary_alloc ();
4257 while ((file_data = file_data_vec[j++]))
4259 size_t len;
4260 const char *data = lto_get_section_data (file_data,
4261 LTO_section_inline_summary,
4262 NULL, &len);
4263 if (data)
4264 inline_read_section (file_data, data, len);
4265 else
4266 /* Fatal error here. We do not want to support compiling ltrans units
4267 with different version of compiler or different flags than the WPA
4268 unit, so this should never happen. */
4269 fatal_error ("ipa inline summary is missing in input file");
4271 if (optimize)
4273 ipa_register_cgraph_hooks ();
4274 if (!flag_ipa_cp)
4275 ipa_prop_read_jump_functions ();
4278 gcc_assert (inline_summaries);
4279 inline_summaries->enable_insertion_hook ();
4283 /* Write predicate P to OB. */
4285 static void
4286 write_predicate (struct output_block *ob, struct predicate *p)
4288 int j;
4289 if (p)
4290 for (j = 0; p->clause[j]; j++)
4292 gcc_assert (j < MAX_CLAUSES);
4293 streamer_write_uhwi (ob, p->clause[j]);
4295 streamer_write_uhwi (ob, 0);
4299 /* Write inline summary for edge E to OB. */
4301 static void
4302 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4304 struct inline_edge_summary *es = inline_edge_summary (e);
4305 int i;
4307 streamer_write_uhwi (ob, es->call_stmt_size);
4308 streamer_write_uhwi (ob, es->call_stmt_time);
4309 streamer_write_uhwi (ob, es->loop_depth);
4310 write_predicate (ob, es->predicate);
4311 streamer_write_uhwi (ob, es->param.length ());
4312 for (i = 0; i < (int) es->param.length (); i++)
4313 streamer_write_uhwi (ob, es->param[i].change_prob);
4317 /* Write inline summary for node in SET.
4318 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4319 active, we don't need to write them twice. */
4321 void
4322 inline_write_summary (void)
4324 struct cgraph_node *node;
4325 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4326 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4327 unsigned int count = 0;
4328 int i;
4330 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4332 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4333 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
4334 if (cnode && cnode->definition && !cnode->alias)
4335 count++;
4337 streamer_write_uhwi (ob, count);
4339 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4341 symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
4342 cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
4343 if (cnode && (node = cnode)->definition && !node->alias)
4345 struct inline_summary *info = inline_summaries->get (node);
4346 struct bitpack_d bp;
4347 struct cgraph_edge *edge;
4348 int i;
4349 size_time_entry *e;
4350 struct condition *c;
4352 streamer_write_uhwi (ob,
4353 lto_symtab_encoder_encode (encoder,
4355 node));
4356 streamer_write_hwi (ob, info->estimated_self_stack_size);
4357 streamer_write_hwi (ob, info->self_size);
4358 streamer_write_hwi (ob, info->self_time);
4359 bp = bitpack_create (ob->main_stream);
4360 bp_pack_value (&bp, info->inlinable, 1);
4361 streamer_write_bitpack (&bp);
4362 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4363 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4365 streamer_write_uhwi (ob, c->operand_num);
4366 streamer_write_uhwi (ob, c->code);
4367 stream_write_tree (ob, c->val, true);
4368 bp = bitpack_create (ob->main_stream);
4369 bp_pack_value (&bp, c->agg_contents, 1);
4370 bp_pack_value (&bp, c->by_ref, 1);
4371 streamer_write_bitpack (&bp);
4372 if (c->agg_contents)
4373 streamer_write_uhwi (ob, c->offset);
4375 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4376 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4378 streamer_write_uhwi (ob, e->size);
4379 streamer_write_uhwi (ob, e->time);
4380 write_predicate (ob, &e->predicate);
4382 write_predicate (ob, info->loop_iterations);
4383 write_predicate (ob, info->loop_stride);
4384 write_predicate (ob, info->array_index);
4385 for (edge = node->callees; edge; edge = edge->next_callee)
4386 write_inline_edge_summary (ob, edge);
4387 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4388 write_inline_edge_summary (ob, edge);
4391 streamer_write_char_stream (ob->main_stream, 0);
4392 produce_asm (ob, NULL);
4393 destroy_output_block (ob);
4395 if (optimize && !flag_ipa_cp)
4396 ipa_prop_write_jump_functions ();
4400 /* Release inline summary. */
4402 void
4403 inline_free_summary (void)
4405 struct cgraph_node *node;
4406 if (edge_removal_hook_holder)
4407 symtab->remove_edge_removal_hook (edge_removal_hook_holder);
4408 edge_removal_hook_holder = NULL;
4409 if (edge_duplication_hook_holder)
4410 symtab->remove_edge_duplication_hook (edge_duplication_hook_holder);
4411 edge_duplication_hook_holder = NULL;
4412 if (!inline_edge_summary_vec.exists ())
4413 return;
4414 FOR_EACH_DEFINED_FUNCTION (node)
4415 if (!node->alias)
4416 reset_inline_summary (node, inline_summaries->get (node));
4417 inline_summaries->release ();
4418 inline_summaries = NULL;
4419 inline_edge_summary_vec.release ();
4420 if (edge_predicate_pool)
4421 free_alloc_pool (edge_predicate_pool);
4422 edge_predicate_pool = 0;