re PR c++/19476 (Missed null checking elimination with new)
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob304f9f5d7cc8a03c8fc16187cb049c6bc3a21df2
1 /* Inlining decision heuristics.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Analysis used by the inliner and other passes limiting code size growth.
23 We estimate for each function
24 - function body size
25 - average function execution time
26 - inlining size benefit (that is how much of function body size
27 and its call sequence is expected to disappear by inlining)
28 - inlining time benefit
29 - function frame size
30 For each call
31 - call statement size and time
33 inlinie_summary datastructures store above information locally (i.e.
34 parameters of the function itself) and globally (i.e. parameters of
35 the function created by applying all the inline decisions already
36 present in the callgraph).
38 We provide accestor to the inline_summary datastructure and
39 basic logic updating the parameters when inlining is performed.
41 The summaries are context sensitive. Context means
42 1) partial assignment of known constant values of operands
43 2) whether function is inlined into the call or not.
44 It is easy to add more variants. To represent function size and time
45 that depends on context (i.e. it is known to be optimized away when
46 context is known either by inlining or from IP-CP and clonning),
47 we use predicates. Predicates are logical formulas in
48 conjunctive-disjunctive form consisting of clauses. Clauses are bitmaps
49 specifying what conditions must be true. Conditions are simple test
50 of the form described above.
52 In order to make predicate (possibly) true, all of its clauses must
53 be (possibly) true. To make clause (possibly) true, one of conditions
54 it mentions must be (possibly) true. There are fixed bounds on
55 number of clauses and conditions and all the manipulation functions
56 are conservative in positive direction. I.e. we may lose precision
57 by thinking that predicate may be true even when it is not.
59 estimate_edge_size and estimate_edge_growth can be used to query
60 function size/time in the given context. inline_merge_summary merges
61 properties of caller and callee after inlining.
63 Finally pass_inline_parameters is exported. This is used to drive
64 computation of function parameters used by the early inliner. IPA
65 inlined performs analysis via its analyze_function method. */
67 #include "config.h"
68 #include "system.h"
69 #include "coretypes.h"
70 #include "tm.h"
71 #include "tree.h"
72 #include "tree-inline.h"
73 #include "langhooks.h"
74 #include "flags.h"
75 #include "cgraph.h"
76 #include "diagnostic.h"
77 #include "gimple-pretty-print.h"
78 #include "params.h"
79 #include "tree-pass.h"
80 #include "coverage.h"
81 #include "ggc.h"
82 #include "tree-ssa.h"
83 #include "ipa-prop.h"
84 #include "lto-streamer.h"
85 #include "data-streamer.h"
86 #include "tree-streamer.h"
87 #include "ipa-inline.h"
88 #include "alloc-pool.h"
89 #include "cfgloop.h"
90 #include "tree-scalar-evolution.h"
91 #include "ipa-utils.h"
93 /* Estimate runtime of function can easilly run into huge numbers with many
94 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
95 integer. For anything larger we use gcov_type. */
96 #define MAX_TIME 500000
98 /* Number of bits in integer, but we really want to be stable across different
99 hosts. */
100 #define NUM_CONDITIONS 32
102 enum predicate_conditions
104 predicate_false_condition = 0,
105 predicate_not_inlined_condition = 1,
106 predicate_first_dynamic_condition = 2
109 /* Special condition code we use to represent test that operand is compile time
110 constant. */
111 #define IS_NOT_CONSTANT ERROR_MARK
112 /* Special condition code we use to represent test that operand is not changed
113 across invocation of the function. When operand IS_NOT_CONSTANT it is always
114 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
115 of executions even when they are not compile time constants. */
116 #define CHANGED IDENTIFIER_NODE
118 /* Holders of ipa cgraph hooks: */
119 static struct cgraph_node_hook_list *function_insertion_hook_holder;
120 static struct cgraph_node_hook_list *node_removal_hook_holder;
121 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
122 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
123 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
124 static void inline_node_removal_hook (struct cgraph_node *, void *);
125 static void inline_node_duplication_hook (struct cgraph_node *,
126 struct cgraph_node *, void *);
127 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
128 static void inline_edge_duplication_hook (struct cgraph_edge *,
129 struct cgraph_edge *, void *);
131 /* VECtor holding inline summaries.
132 In GGC memory because conditions might point to constant trees. */
133 vec<inline_summary_t, va_gc> *inline_summary_vec;
134 vec<inline_edge_summary_t> inline_edge_summary_vec;
136 /* Cached node/edge growths. */
137 vec<int> node_growth_cache;
138 vec<edge_growth_cache_entry> edge_growth_cache;
140 /* Edge predicates goes here. */
141 static alloc_pool edge_predicate_pool;
143 /* Return true predicate (tautology).
144 We represent it by empty list of clauses. */
146 static inline struct predicate
147 true_predicate (void)
149 struct predicate p;
150 p.clause[0] = 0;
151 return p;
155 /* Return predicate testing single condition number COND. */
157 static inline struct predicate
158 single_cond_predicate (int cond)
160 struct predicate p;
161 p.clause[0] = 1 << cond;
162 p.clause[1] = 0;
163 return p;
167 /* Return false predicate. First clause require false condition. */
169 static inline struct predicate
170 false_predicate (void)
172 return single_cond_predicate (predicate_false_condition);
176 /* Return true if P is (false). */
178 static inline bool
179 true_predicate_p (struct predicate *p)
181 return !p->clause[0];
185 /* Return true if P is (false). */
187 static inline bool
188 false_predicate_p (struct predicate *p)
190 if (p->clause[0] == (1 << predicate_false_condition))
192 gcc_checking_assert (!p->clause[1]
193 && p->clause[0] == 1 << predicate_false_condition);
194 return true;
196 return false;
200 /* Return predicate that is set true when function is not inlined. */
202 static inline struct predicate
203 not_inlined_predicate (void)
205 return single_cond_predicate (predicate_not_inlined_condition);
208 /* Simple description of whether a memory load or a condition refers to a load
209 from an aggregate and if so, how and where from in the aggregate.
210 Individual fields have the same meaning like fields with the same name in
211 struct condition. */
213 struct agg_position_info
215 HOST_WIDE_INT offset;
216 bool agg_contents;
217 bool by_ref;
220 /* Add condition to condition list CONDS. AGGPOS describes whether the used
221 oprand is loaded from an aggregate and where in the aggregate it is. It can
222 be NULL, which means this not a load from an aggregate. */
224 static struct predicate
225 add_condition (struct inline_summary *summary, int operand_num,
226 struct agg_position_info *aggpos,
227 enum tree_code code, tree val)
229 int i;
230 struct condition *c;
231 struct condition new_cond;
232 HOST_WIDE_INT offset;
233 bool agg_contents, by_ref;
235 if (aggpos)
237 offset = aggpos->offset;
238 agg_contents = aggpos->agg_contents;
239 by_ref = aggpos->by_ref;
241 else
243 offset = 0;
244 agg_contents = false;
245 by_ref = false;
248 gcc_checking_assert (operand_num >= 0);
249 for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
251 if (c->operand_num == operand_num
252 && c->code == code
253 && c->val == val
254 && c->agg_contents == agg_contents
255 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
256 return single_cond_predicate (i + predicate_first_dynamic_condition);
258 /* Too many conditions. Give up and return constant true. */
259 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
260 return true_predicate ();
262 new_cond.operand_num = operand_num;
263 new_cond.code = code;
264 new_cond.val = val;
265 new_cond.agg_contents = agg_contents;
266 new_cond.by_ref = by_ref;
267 new_cond.offset = offset;
268 vec_safe_push (summary->conds, new_cond);
269 return single_cond_predicate (i + predicate_first_dynamic_condition);
273 /* Add clause CLAUSE into the predicate P. */
275 static inline void
276 add_clause (conditions conditions, struct predicate *p, clause_t clause)
278 int i;
279 int i2;
280 int insert_here = -1;
281 int c1, c2;
283 /* True clause. */
284 if (!clause)
285 return;
287 /* False clause makes the whole predicate false. Kill the other variants. */
288 if (clause == (1 << predicate_false_condition))
290 p->clause[0] = (1 << predicate_false_condition);
291 p->clause[1] = 0;
292 return;
294 if (false_predicate_p (p))
295 return;
297 /* No one should be sily enough to add false into nontrivial clauses. */
298 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
300 /* Look where to insert the clause. At the same time prune out
301 clauses of P that are implied by the new clause and thus
302 redundant. */
303 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
305 p->clause[i2] = p->clause[i];
307 if (!p->clause[i])
308 break;
310 /* If p->clause[i] implies clause, there is nothing to add. */
311 if ((p->clause[i] & clause) == p->clause[i])
313 /* We had nothing to add, none of clauses should've become
314 redundant. */
315 gcc_checking_assert (i == i2);
316 return;
319 if (p->clause[i] < clause && insert_here < 0)
320 insert_here = i2;
322 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
323 Otherwise the p->clause[i] has to stay. */
324 if ((p->clause[i] & clause) != clause)
325 i2++;
328 /* Look for clauses that are obviously true. I.e.
329 op0 == 5 || op0 != 5. */
330 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
332 condition *cc1;
333 if (!(clause & (1 << c1)))
334 continue;
335 cc1 = &(*conditions)[c1 - predicate_first_dynamic_condition];
336 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
337 and thus there is no point for looking for them. */
338 if (cc1->code == CHANGED || cc1->code == IS_NOT_CONSTANT)
339 continue;
340 for (c2 = c1 + 1; c2 < NUM_CONDITIONS; c2++)
341 if (clause & (1 << c2))
343 condition *cc1 =
344 &(*conditions)[c1 - predicate_first_dynamic_condition];
345 condition *cc2 =
346 &(*conditions)[c2 - predicate_first_dynamic_condition];
347 if (cc1->operand_num == cc2->operand_num
348 && cc1->val == cc2->val
349 && cc2->code != IS_NOT_CONSTANT
350 && cc2->code != CHANGED
351 && cc1->code == invert_tree_comparison
352 (cc2->code,
353 HONOR_NANS (TYPE_MODE (TREE_TYPE (cc1->val)))))
354 return;
359 /* We run out of variants. Be conservative in positive direction. */
360 if (i2 == MAX_CLAUSES)
361 return;
362 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
363 p->clause[i2 + 1] = 0;
364 if (insert_here >= 0)
365 for (; i2 > insert_here; i2--)
366 p->clause[i2] = p->clause[i2 - 1];
367 else
368 insert_here = i2;
369 p->clause[insert_here] = clause;
373 /* Return P & P2. */
375 static struct predicate
376 and_predicates (conditions conditions,
377 struct predicate *p, struct predicate *p2)
379 struct predicate out = *p;
380 int i;
382 /* Avoid busy work. */
383 if (false_predicate_p (p2) || true_predicate_p (p))
384 return *p2;
385 if (false_predicate_p (p) || true_predicate_p (p2))
386 return *p;
388 /* See how far predicates match. */
389 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
391 gcc_checking_assert (i < MAX_CLAUSES);
394 /* Combine the predicates rest. */
395 for (; p2->clause[i]; i++)
397 gcc_checking_assert (i < MAX_CLAUSES);
398 add_clause (conditions, &out, p2->clause[i]);
400 return out;
404 /* Return true if predicates are obviously equal. */
406 static inline bool
407 predicates_equal_p (struct predicate *p, struct predicate *p2)
409 int i;
410 for (i = 0; p->clause[i]; i++)
412 gcc_checking_assert (i < MAX_CLAUSES);
413 gcc_checking_assert (p->clause[i] > p->clause[i + 1]);
414 gcc_checking_assert (!p2->clause[i]
415 || p2->clause[i] > p2->clause[i + 1]);
416 if (p->clause[i] != p2->clause[i])
417 return false;
419 return !p2->clause[i];
423 /* Return P | P2. */
425 static struct predicate
426 or_predicates (conditions conditions,
427 struct predicate *p, struct predicate *p2)
429 struct predicate out = true_predicate ();
430 int i, j;
432 /* Avoid busy work. */
433 if (false_predicate_p (p2) || true_predicate_p (p))
434 return *p;
435 if (false_predicate_p (p) || true_predicate_p (p2))
436 return *p2;
437 if (predicates_equal_p (p, p2))
438 return *p;
440 /* OK, combine the predicates. */
441 for (i = 0; p->clause[i]; i++)
442 for (j = 0; p2->clause[j]; j++)
444 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
445 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
447 return out;
451 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
452 if predicate P is known to be false. */
454 static bool
455 evaluate_predicate (struct predicate *p, clause_t possible_truths)
457 int i;
459 /* True remains true. */
460 if (true_predicate_p (p))
461 return true;
463 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
465 /* See if we can find clause we can disprove. */
466 for (i = 0; p->clause[i]; i++)
468 gcc_checking_assert (i < MAX_CLAUSES);
469 if (!(p->clause[i] & possible_truths))
470 return false;
472 return true;
475 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
476 instruction will be recomputed per invocation of the inlined call. */
478 static int
479 predicate_probability (conditions conds,
480 struct predicate *p, clause_t possible_truths,
481 vec<inline_param_summary_t> inline_param_summary)
483 int i;
484 int combined_prob = REG_BR_PROB_BASE;
486 /* True remains true. */
487 if (true_predicate_p (p))
488 return REG_BR_PROB_BASE;
490 if (false_predicate_p (p))
491 return 0;
493 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
495 /* See if we can find clause we can disprove. */
496 for (i = 0; p->clause[i]; i++)
498 gcc_checking_assert (i < MAX_CLAUSES);
499 if (!(p->clause[i] & possible_truths))
500 return 0;
501 else
503 int this_prob = 0;
504 int i2;
505 if (!inline_param_summary.exists ())
506 return REG_BR_PROB_BASE;
507 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
508 if ((p->clause[i] & possible_truths) & (1 << i2))
510 if (i2 >= predicate_first_dynamic_condition)
512 condition *c =
513 &(*conds)[i2 - predicate_first_dynamic_condition];
514 if (c->code == CHANGED
515 && (c->operand_num <
516 (int) inline_param_summary.length ()))
518 int iprob =
519 inline_param_summary[c->operand_num].change_prob;
520 this_prob = MAX (this_prob, iprob);
522 else
523 this_prob = REG_BR_PROB_BASE;
525 else
526 this_prob = REG_BR_PROB_BASE;
528 combined_prob = MIN (this_prob, combined_prob);
529 if (!combined_prob)
530 return 0;
533 return combined_prob;
537 /* Dump conditional COND. */
539 static void
540 dump_condition (FILE *f, conditions conditions, int cond)
542 condition *c;
543 if (cond == predicate_false_condition)
544 fprintf (f, "false");
545 else if (cond == predicate_not_inlined_condition)
546 fprintf (f, "not inlined");
547 else
549 c = &(*conditions)[cond - predicate_first_dynamic_condition];
550 fprintf (f, "op%i", c->operand_num);
551 if (c->agg_contents)
552 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
553 c->by_ref ? "ref " : "", c->offset);
554 if (c->code == IS_NOT_CONSTANT)
556 fprintf (f, " not constant");
557 return;
559 if (c->code == CHANGED)
561 fprintf (f, " changed");
562 return;
564 fprintf (f, " %s ", op_symbol_code (c->code));
565 print_generic_expr (f, c->val, 1);
570 /* Dump clause CLAUSE. */
572 static void
573 dump_clause (FILE *f, conditions conds, clause_t clause)
575 int i;
576 bool found = false;
577 fprintf (f, "(");
578 if (!clause)
579 fprintf (f, "true");
580 for (i = 0; i < NUM_CONDITIONS; i++)
581 if (clause & (1 << i))
583 if (found)
584 fprintf (f, " || ");
585 found = true;
586 dump_condition (f, conds, i);
588 fprintf (f, ")");
592 /* Dump predicate PREDICATE. */
594 static void
595 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
597 int i;
598 if (true_predicate_p (pred))
599 dump_clause (f, conds, 0);
600 else
601 for (i = 0; pred->clause[i]; i++)
603 if (i)
604 fprintf (f, " && ");
605 dump_clause (f, conds, pred->clause[i]);
607 fprintf (f, "\n");
611 /* Dump inline hints. */
612 void
613 dump_inline_hints (FILE *f, inline_hints hints)
615 if (!hints)
616 return;
617 fprintf (f, "inline hints:");
618 if (hints & INLINE_HINT_indirect_call)
620 hints &= ~INLINE_HINT_indirect_call;
621 fprintf (f, " indirect_call");
623 if (hints & INLINE_HINT_loop_iterations)
625 hints &= ~INLINE_HINT_loop_iterations;
626 fprintf (f, " loop_iterations");
628 if (hints & INLINE_HINT_loop_stride)
630 hints &= ~INLINE_HINT_loop_stride;
631 fprintf (f, " loop_stride");
633 if (hints & INLINE_HINT_same_scc)
635 hints &= ~INLINE_HINT_same_scc;
636 fprintf (f, " same_scc");
638 if (hints & INLINE_HINT_in_scc)
640 hints &= ~INLINE_HINT_in_scc;
641 fprintf (f, " in_scc");
643 if (hints & INLINE_HINT_cross_module)
645 hints &= ~INLINE_HINT_cross_module;
646 fprintf (f, " cross_module");
648 if (hints & INLINE_HINT_declared_inline)
650 hints &= ~INLINE_HINT_declared_inline;
651 fprintf (f, " declared_inline");
653 if (hints & INLINE_HINT_array_index)
655 hints &= ~INLINE_HINT_array_index;
656 fprintf (f, " array_index");
658 gcc_assert (!hints);
662 /* Record SIZE and TIME under condition PRED into the inline summary. */
664 static void
665 account_size_time (struct inline_summary *summary, int size, int time,
666 struct predicate *pred)
668 size_time_entry *e;
669 bool found = false;
670 int i;
672 if (false_predicate_p (pred))
673 return;
675 /* We need to create initial empty unconitional clause, but otherwie
676 we don't need to account empty times and sizes. */
677 if (!size && !time && summary->entry)
678 return;
680 /* Watch overflow that might result from insane profiles. */
681 if (time > MAX_TIME * INLINE_TIME_SCALE)
682 time = MAX_TIME * INLINE_TIME_SCALE;
683 gcc_assert (time >= 0);
685 for (i = 0; vec_safe_iterate (summary->entry, i, &e); i++)
686 if (predicates_equal_p (&e->predicate, pred))
688 found = true;
689 break;
691 if (i == 256)
693 i = 0;
694 found = true;
695 e = &(*summary->entry)[0];
696 gcc_assert (!e->predicate.clause[0]);
697 if (dump_file && (dump_flags & TDF_DETAILS))
698 fprintf (dump_file,
699 "\t\tReached limit on number of entries, "
700 "ignoring the predicate.");
702 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
704 fprintf (dump_file,
705 "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
706 ((double) size) / INLINE_SIZE_SCALE,
707 ((double) time) / INLINE_TIME_SCALE, found ? "" : "new ");
708 dump_predicate (dump_file, summary->conds, pred);
710 if (!found)
712 struct size_time_entry new_entry;
713 new_entry.size = size;
714 new_entry.time = time;
715 new_entry.predicate = *pred;
716 vec_safe_push (summary->entry, new_entry);
718 else
720 e->size += size;
721 e->time += time;
722 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
723 e->time = MAX_TIME * INLINE_TIME_SCALE;
727 /* Set predicate for edge E. */
729 static void
730 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
732 struct inline_edge_summary *es = inline_edge_summary (e);
733 if (predicate && !true_predicate_p (predicate))
735 if (!es->predicate)
736 es->predicate = (struct predicate *) pool_alloc (edge_predicate_pool);
737 *es->predicate = *predicate;
739 else
741 if (es->predicate)
742 pool_free (edge_predicate_pool, es->predicate);
743 es->predicate = NULL;
747 /* Set predicate for hint *P. */
749 static void
750 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
752 if (false_predicate_p (&new_predicate) || true_predicate_p (&new_predicate))
754 if (*p)
755 pool_free (edge_predicate_pool, *p);
756 *p = NULL;
758 else
760 if (!*p)
761 *p = (struct predicate *) pool_alloc (edge_predicate_pool);
762 **p = new_predicate;
767 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
768 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
769 Return clause of possible truths. When INLINE_P is true, assume that we are
770 inlining.
772 ERROR_MARK means compile time invariant. */
774 static clause_t
775 evaluate_conditions_for_known_args (struct cgraph_node *node,
776 bool inline_p,
777 vec<tree> known_vals,
778 vec<ipa_agg_jump_function_p>
779 known_aggs)
781 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
782 struct inline_summary *info = inline_summary (node);
783 int i;
784 struct condition *c;
786 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
788 tree val;
789 tree res;
791 /* We allow call stmt to have fewer arguments than the callee function
792 (especially for K&R style programs). So bound check here (we assume
793 known_aggs vector, if non-NULL, has the same length as
794 known_vals). */
795 gcc_checking_assert (!known_aggs.exists ()
796 || (known_vals.length () == known_aggs.length ()));
797 if (c->operand_num >= (int) known_vals.length ())
799 clause |= 1 << (i + predicate_first_dynamic_condition);
800 continue;
803 if (c->agg_contents)
805 struct ipa_agg_jump_function *agg;
807 if (c->code == CHANGED
808 && !c->by_ref
809 && (known_vals[c->operand_num] == error_mark_node))
810 continue;
812 if (known_aggs.exists ())
814 agg = known_aggs[c->operand_num];
815 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
817 else
818 val = NULL_TREE;
820 else
822 val = known_vals[c->operand_num];
823 if (val == error_mark_node && c->code != CHANGED)
824 val = NULL_TREE;
827 if (!val)
829 clause |= 1 << (i + predicate_first_dynamic_condition);
830 continue;
832 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
833 continue;
834 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
835 if (res && integer_zerop (res))
836 continue;
837 clause |= 1 << (i + predicate_first_dynamic_condition);
839 return clause;
843 /* Work out what conditions might be true at invocation of E. */
845 static void
846 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
847 clause_t *clause_ptr,
848 vec<tree> *known_vals_ptr,
849 vec<tree> *known_binfos_ptr,
850 vec<ipa_agg_jump_function_p> *known_aggs_ptr)
852 struct cgraph_node *callee =
853 cgraph_function_or_thunk_node (e->callee, NULL);
854 struct inline_summary *info = inline_summary (callee);
855 vec<tree> known_vals = vNULL;
856 vec<ipa_agg_jump_function_p> known_aggs = vNULL;
858 if (clause_ptr)
859 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
860 if (known_vals_ptr)
861 known_vals_ptr->create (0);
862 if (known_binfos_ptr)
863 known_binfos_ptr->create (0);
865 if (ipa_node_params_vector.exists ()
866 && !e->call_stmt_cannot_inline_p
867 && ((clause_ptr && info->conds) || known_vals_ptr || known_binfos_ptr))
869 struct ipa_node_params *parms_info;
870 struct ipa_edge_args *args = IPA_EDGE_REF (e);
871 struct inline_edge_summary *es = inline_edge_summary (e);
872 int i, count = ipa_get_cs_argument_count (args);
874 if (e->caller->global.inlined_to)
875 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
876 else
877 parms_info = IPA_NODE_REF (e->caller);
879 if (count && (info->conds || known_vals_ptr))
880 known_vals.safe_grow_cleared (count);
881 if (count && (info->conds || known_aggs_ptr))
882 known_aggs.safe_grow_cleared (count);
883 if (count && known_binfos_ptr)
884 known_binfos_ptr->safe_grow_cleared (count);
886 for (i = 0; i < count; i++)
888 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
889 tree cst = ipa_value_from_jfunc (parms_info, jf);
890 if (cst)
892 if (known_vals.exists () && TREE_CODE (cst) != TREE_BINFO)
893 known_vals[i] = cst;
894 else if (known_binfos_ptr != NULL
895 && TREE_CODE (cst) == TREE_BINFO)
896 (*known_binfos_ptr)[i] = cst;
898 else if (inline_p && !es->param[i].change_prob)
899 known_vals[i] = error_mark_node;
900 /* TODO: When IPA-CP starts propagating and merging aggregate jump
901 functions, use its knowledge of the caller too, just like the
902 scalar case above. */
903 known_aggs[i] = &jf->agg;
907 if (clause_ptr)
908 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
909 known_vals, known_aggs);
911 if (known_vals_ptr)
912 *known_vals_ptr = known_vals;
913 else
914 known_vals.release ();
916 if (known_aggs_ptr)
917 *known_aggs_ptr = known_aggs;
918 else
919 known_aggs.release ();
923 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
925 static void
926 inline_summary_alloc (void)
928 if (!node_removal_hook_holder)
929 node_removal_hook_holder =
930 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
931 if (!edge_removal_hook_holder)
932 edge_removal_hook_holder =
933 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
934 if (!node_duplication_hook_holder)
935 node_duplication_hook_holder =
936 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
937 if (!edge_duplication_hook_holder)
938 edge_duplication_hook_holder =
939 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
941 if (vec_safe_length (inline_summary_vec) <= (unsigned) cgraph_max_uid)
942 vec_safe_grow_cleared (inline_summary_vec, cgraph_max_uid + 1);
943 if (inline_edge_summary_vec.length () <= (unsigned) cgraph_edge_max_uid)
944 inline_edge_summary_vec.safe_grow_cleared (cgraph_edge_max_uid + 1);
945 if (!edge_predicate_pool)
946 edge_predicate_pool = create_alloc_pool ("edge predicates",
947 sizeof (struct predicate), 10);
950 /* We are called multiple time for given function; clear
951 data from previous run so they are not cumulated. */
953 static void
954 reset_inline_edge_summary (struct cgraph_edge *e)
956 if (e->uid < (int) inline_edge_summary_vec.length ())
958 struct inline_edge_summary *es = inline_edge_summary (e);
960 es->call_stmt_size = es->call_stmt_time = 0;
961 if (es->predicate)
962 pool_free (edge_predicate_pool, es->predicate);
963 es->predicate = NULL;
964 es->param.release ();
968 /* We are called multiple time for given function; clear
969 data from previous run so they are not cumulated. */
971 static void
972 reset_inline_summary (struct cgraph_node *node)
974 struct inline_summary *info = inline_summary (node);
975 struct cgraph_edge *e;
977 info->self_size = info->self_time = 0;
978 info->estimated_stack_size = 0;
979 info->estimated_self_stack_size = 0;
980 info->stack_frame_offset = 0;
981 info->size = 0;
982 info->time = 0;
983 info->growth = 0;
984 info->scc_no = 0;
985 if (info->loop_iterations)
987 pool_free (edge_predicate_pool, info->loop_iterations);
988 info->loop_iterations = NULL;
990 if (info->loop_stride)
992 pool_free (edge_predicate_pool, info->loop_stride);
993 info->loop_stride = NULL;
995 if (info->array_index)
997 pool_free (edge_predicate_pool, info->array_index);
998 info->array_index = NULL;
1000 vec_free (info->conds);
1001 vec_free (info->entry);
1002 for (e = node->callees; e; e = e->next_callee)
1003 reset_inline_edge_summary (e);
1004 for (e = node->indirect_calls; e; e = e->next_callee)
1005 reset_inline_edge_summary (e);
1008 /* Hook that is called by cgraph.c when a node is removed. */
1010 static void
1011 inline_node_removal_hook (struct cgraph_node *node,
1012 void *data ATTRIBUTE_UNUSED)
1014 struct inline_summary *info;
1015 if (vec_safe_length (inline_summary_vec) <= (unsigned) node->uid)
1016 return;
1017 info = inline_summary (node);
1018 reset_inline_summary (node);
1019 memset (info, 0, sizeof (inline_summary_t));
1022 /* Remap predicate P of former function to be predicate of duplicated functoin.
1023 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1024 INFO is inline summary of the duplicated node. */
1026 static struct predicate
1027 remap_predicate_after_duplication (struct predicate *p,
1028 clause_t possible_truths,
1029 struct inline_summary *info)
1031 struct predicate new_predicate = true_predicate ();
1032 int j;
1033 for (j = 0; p->clause[j]; j++)
1034 if (!(possible_truths & p->clause[j]))
1036 new_predicate = false_predicate ();
1037 break;
1039 else
1040 add_clause (info->conds, &new_predicate,
1041 possible_truths & p->clause[j]);
1042 return new_predicate;
1045 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1046 Additionally care about allocating new memory slot for updated predicate
1047 and set it to NULL when it becomes true or false (and thus uninteresting).
1050 static void
1051 remap_hint_predicate_after_duplication (struct predicate **p,
1052 clause_t possible_truths,
1053 struct inline_summary *info)
1055 struct predicate new_predicate;
1057 if (!*p)
1058 return;
1060 new_predicate = remap_predicate_after_duplication (*p,
1061 possible_truths, info);
1062 /* We do not want to free previous predicate; it is used by node origin. */
1063 *p = NULL;
1064 set_hint_predicate (p, new_predicate);
1068 /* Hook that is called by cgraph.c when a node is duplicated. */
1070 static void
1071 inline_node_duplication_hook (struct cgraph_node *src,
1072 struct cgraph_node *dst,
1073 ATTRIBUTE_UNUSED void *data)
1075 struct inline_summary *info;
1076 inline_summary_alloc ();
1077 info = inline_summary (dst);
1078 memcpy (info, inline_summary (src), sizeof (struct inline_summary));
1079 /* TODO: as an optimization, we may avoid copying conditions
1080 that are known to be false or true. */
1081 info->conds = vec_safe_copy (info->conds);
1083 /* When there are any replacements in the function body, see if we can figure
1084 out that something was optimized out. */
1085 if (ipa_node_params_vector.exists () && dst->clone.tree_map)
1087 vec<size_time_entry, va_gc> *entry = info->entry;
1088 /* Use SRC parm info since it may not be copied yet. */
1089 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1090 vec<tree> known_vals = vNULL;
1091 int count = ipa_get_param_count (parms_info);
1092 int i, j;
1093 clause_t possible_truths;
1094 struct predicate true_pred = true_predicate ();
1095 size_time_entry *e;
1096 int optimized_out_size = 0;
1097 bool inlined_to_p = false;
1098 struct cgraph_edge *edge;
1100 info->entry = 0;
1101 known_vals.safe_grow_cleared (count);
1102 for (i = 0; i < count; i++)
1104 struct ipa_replace_map *r;
1106 for (j = 0; vec_safe_iterate (dst->clone.tree_map, j, &r); j++)
1108 if (((!r->old_tree && r->parm_num == i)
1109 || (r->old_tree && r->old_tree == ipa_get_param (parms_info, i)))
1110 && r->replace_p && !r->ref_p)
1112 known_vals[i] = r->new_tree;
1113 break;
1117 possible_truths = evaluate_conditions_for_known_args (dst, false,
1118 known_vals,
1119 vNULL);
1120 known_vals.release ();
1122 account_size_time (info, 0, 0, &true_pred);
1124 /* Remap size_time vectors.
1125 Simplify the predicate by prunning out alternatives that are known
1126 to be false.
1127 TODO: as on optimization, we can also eliminate conditions known
1128 to be true. */
1129 for (i = 0; vec_safe_iterate (entry, i, &e); i++)
1131 struct predicate new_predicate;
1132 new_predicate = remap_predicate_after_duplication (&e->predicate,
1133 possible_truths,
1134 info);
1135 if (false_predicate_p (&new_predicate))
1136 optimized_out_size += e->size;
1137 else
1138 account_size_time (info, e->size, e->time, &new_predicate);
1141 /* Remap edge predicates with the same simplification as above.
1142 Also copy constantness arrays. */
1143 for (edge = dst->callees; edge; edge = edge->next_callee)
1145 struct predicate new_predicate;
1146 struct inline_edge_summary *es = inline_edge_summary (edge);
1148 if (!edge->inline_failed)
1149 inlined_to_p = true;
1150 if (!es->predicate)
1151 continue;
1152 new_predicate = remap_predicate_after_duplication (es->predicate,
1153 possible_truths,
1154 info);
1155 if (false_predicate_p (&new_predicate)
1156 && !false_predicate_p (es->predicate))
1158 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1159 edge->frequency = 0;
1161 edge_set_predicate (edge, &new_predicate);
1164 /* Remap indirect edge predicates with the same simplificaiton as above.
1165 Also copy constantness arrays. */
1166 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1168 struct predicate new_predicate;
1169 struct inline_edge_summary *es = inline_edge_summary (edge);
1171 gcc_checking_assert (edge->inline_failed);
1172 if (!es->predicate)
1173 continue;
1174 new_predicate = remap_predicate_after_duplication (es->predicate,
1175 possible_truths,
1176 info);
1177 if (false_predicate_p (&new_predicate)
1178 && !false_predicate_p (es->predicate))
1180 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1181 edge->frequency = 0;
1183 edge_set_predicate (edge, &new_predicate);
1185 remap_hint_predicate_after_duplication (&info->loop_iterations,
1186 possible_truths, info);
1187 remap_hint_predicate_after_duplication (&info->loop_stride,
1188 possible_truths, info);
1189 remap_hint_predicate_after_duplication (&info->array_index,
1190 possible_truths, info);
1192 /* If inliner or someone after inliner will ever start producing
1193 non-trivial clones, we will get trouble with lack of information
1194 about updating self sizes, because size vectors already contains
1195 sizes of the calees. */
1196 gcc_assert (!inlined_to_p || !optimized_out_size);
1198 else
1200 info->entry = vec_safe_copy (info->entry);
1201 if (info->loop_iterations)
1203 predicate p = *info->loop_iterations;
1204 info->loop_iterations = NULL;
1205 set_hint_predicate (&info->loop_iterations, p);
1207 if (info->loop_stride)
1209 predicate p = *info->loop_stride;
1210 info->loop_stride = NULL;
1211 set_hint_predicate (&info->loop_stride, p);
1213 if (info->array_index)
1215 predicate p = *info->array_index;
1216 info->array_index = NULL;
1217 set_hint_predicate (&info->array_index, p);
1220 inline_update_overall_summary (dst);
1224 /* Hook that is called by cgraph.c when a node is duplicated. */
1226 static void
1227 inline_edge_duplication_hook (struct cgraph_edge *src,
1228 struct cgraph_edge *dst,
1229 ATTRIBUTE_UNUSED void *data)
1231 struct inline_edge_summary *info;
1232 struct inline_edge_summary *srcinfo;
1233 inline_summary_alloc ();
1234 info = inline_edge_summary (dst);
1235 srcinfo = inline_edge_summary (src);
1236 memcpy (info, srcinfo, sizeof (struct inline_edge_summary));
1237 info->predicate = NULL;
1238 edge_set_predicate (dst, srcinfo->predicate);
1239 info->param = srcinfo->param.copy ();
1243 /* Keep edge cache consistent across edge removal. */
1245 static void
1246 inline_edge_removal_hook (struct cgraph_edge *edge,
1247 void *data ATTRIBUTE_UNUSED)
1249 if (edge_growth_cache.exists ())
1250 reset_edge_growth_cache (edge);
1251 reset_inline_edge_summary (edge);
1255 /* Initialize growth caches. */
1257 void
1258 initialize_growth_caches (void)
1260 if (cgraph_edge_max_uid)
1261 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
1262 if (cgraph_max_uid)
1263 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
1267 /* Free growth caches. */
1269 void
1270 free_growth_caches (void)
1272 edge_growth_cache.release ();
1273 node_growth_cache.release ();
1277 /* Dump edge summaries associated to NODE and recursively to all clones.
1278 Indent by INDENT. */
1280 static void
1281 dump_inline_edge_summary (FILE *f, int indent, struct cgraph_node *node,
1282 struct inline_summary *info)
1284 struct cgraph_edge *edge;
1285 for (edge = node->callees; edge; edge = edge->next_callee)
1287 struct inline_edge_summary *es = inline_edge_summary (edge);
1288 struct cgraph_node *callee =
1289 cgraph_function_or_thunk_node (edge->callee, NULL);
1290 int i;
1292 fprintf (f,
1293 "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i"
1294 " time: %2i callee size:%2i stack:%2i",
1295 indent, "", cgraph_node_name (callee), callee->symbol.order,
1296 !edge->inline_failed
1297 ? "inlined" : cgraph_inline_failed_string (edge-> inline_failed),
1298 indent, "", es->loop_depth, edge->frequency,
1299 es->call_stmt_size, es->call_stmt_time,
1300 (int) inline_summary (callee)->size / INLINE_SIZE_SCALE,
1301 (int) inline_summary (callee)->estimated_stack_size);
1303 if (es->predicate)
1305 fprintf (f, " predicate: ");
1306 dump_predicate (f, info->conds, es->predicate);
1308 else
1309 fprintf (f, "\n");
1310 if (es->param.exists ())
1311 for (i = 0; i < (int) es->param.length (); i++)
1313 int prob = es->param[i].change_prob;
1315 if (!prob)
1316 fprintf (f, "%*s op%i is compile time invariant\n",
1317 indent + 2, "", i);
1318 else if (prob != REG_BR_PROB_BASE)
1319 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1320 prob * 100.0 / REG_BR_PROB_BASE);
1322 if (!edge->inline_failed)
1324 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1325 " callee size %i\n",
1326 indent + 2, "",
1327 (int) inline_summary (callee)->stack_frame_offset,
1328 (int) inline_summary (callee)->estimated_self_stack_size,
1329 (int) inline_summary (callee)->estimated_stack_size);
1330 dump_inline_edge_summary (f, indent + 2, callee, info);
1333 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1335 struct inline_edge_summary *es = inline_edge_summary (edge);
1336 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1337 " time: %2i",
1338 indent, "",
1339 es->loop_depth,
1340 edge->frequency, es->call_stmt_size, es->call_stmt_time);
1341 if (es->predicate)
1343 fprintf (f, "predicate: ");
1344 dump_predicate (f, info->conds, es->predicate);
1346 else
1347 fprintf (f, "\n");
1352 void
1353 dump_inline_summary (FILE *f, struct cgraph_node *node)
1355 if (node->symbol.definition)
1357 struct inline_summary *s = inline_summary (node);
1358 size_time_entry *e;
1359 int i;
1360 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
1361 node->symbol.order);
1362 if (DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl))
1363 fprintf (f, " always_inline");
1364 if (s->inlinable)
1365 fprintf (f, " inlinable");
1366 fprintf (f, "\n self time: %i\n", s->self_time);
1367 fprintf (f, " global time: %i\n", s->time);
1368 fprintf (f, " self size: %i\n", s->self_size);
1369 fprintf (f, " global size: %i\n", s->size);
1370 fprintf (f, " self stack: %i\n",
1371 (int) s->estimated_self_stack_size);
1372 fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size);
1373 if (s->growth)
1374 fprintf (f, " estimated growth:%i\n", (int) s->growth);
1375 if (s->scc_no)
1376 fprintf (f, " In SCC: %i\n", (int) s->scc_no);
1377 for (i = 0; vec_safe_iterate (s->entry, i, &e); i++)
1379 fprintf (f, " size:%f, time:%f, predicate:",
1380 (double) e->size / INLINE_SIZE_SCALE,
1381 (double) e->time / INLINE_TIME_SCALE);
1382 dump_predicate (f, s->conds, &e->predicate);
1384 if (s->loop_iterations)
1386 fprintf (f, " loop iterations:");
1387 dump_predicate (f, s->conds, s->loop_iterations);
1389 if (s->loop_stride)
1391 fprintf (f, " loop stride:");
1392 dump_predicate (f, s->conds, s->loop_stride);
1394 if (s->array_index)
1396 fprintf (f, " array index:");
1397 dump_predicate (f, s->conds, s->array_index);
1399 fprintf (f, " calls:\n");
1400 dump_inline_edge_summary (f, 4, node, s);
1401 fprintf (f, "\n");
1405 DEBUG_FUNCTION void
1406 debug_inline_summary (struct cgraph_node *node)
1408 dump_inline_summary (stderr, node);
1411 void
1412 dump_inline_summaries (FILE *f)
1414 struct cgraph_node *node;
1416 FOR_EACH_DEFINED_FUNCTION (node)
1417 if (!node->global.inlined_to)
1418 dump_inline_summary (f, node);
1421 /* Give initial reasons why inlining would fail on EDGE. This gets either
1422 nullified or usually overwritten by more precise reasons later. */
1424 void
1425 initialize_inline_failed (struct cgraph_edge *e)
1427 struct cgraph_node *callee = e->callee;
1429 if (e->indirect_unknown_callee)
1430 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1431 else if (!callee->symbol.definition)
1432 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1433 else if (callee->local.redefined_extern_inline)
1434 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1435 else if (e->call_stmt_cannot_inline_p)
1436 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1437 else
1438 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1441 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1442 boolean variable pointed to by DATA. */
1444 static bool
1445 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1446 void *data)
1448 bool *b = (bool *) data;
1449 *b = true;
1450 return true;
1453 /* If OP refers to value of function parameter, return the corresponding
1454 parameter. */
1456 static tree
1457 unmodified_parm_1 (gimple stmt, tree op)
1459 /* SSA_NAME referring to parm default def? */
1460 if (TREE_CODE (op) == SSA_NAME
1461 && SSA_NAME_IS_DEFAULT_DEF (op)
1462 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1463 return SSA_NAME_VAR (op);
1464 /* Non-SSA parm reference? */
1465 if (TREE_CODE (op) == PARM_DECL)
1467 bool modified = false;
1469 ao_ref refd;
1470 ao_ref_init (&refd, op);
1471 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1472 NULL);
1473 if (!modified)
1474 return op;
1476 return NULL_TREE;
1479 /* If OP refers to value of function parameter, return the corresponding
1480 parameter. Also traverse chains of SSA register assignments. */
1482 static tree
1483 unmodified_parm (gimple stmt, tree op)
1485 tree res = unmodified_parm_1 (stmt, op);
1486 if (res)
1487 return res;
1489 if (TREE_CODE (op) == SSA_NAME
1490 && !SSA_NAME_IS_DEFAULT_DEF (op)
1491 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1492 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1493 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1494 return NULL_TREE;
1497 /* If OP refers to a value of a function parameter or value loaded from an
1498 aggregate passed to a parameter (either by value or reference), return TRUE
1499 and store the number of the parameter to *INDEX_P and information whether
1500 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1501 the function parameters, STMT is the statement in which OP is used or
1502 loaded. */
1504 static bool
1505 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1506 gimple stmt, tree op, int *index_p,
1507 struct agg_position_info *aggpos)
1509 tree res = unmodified_parm_1 (stmt, op);
1511 gcc_checking_assert (aggpos);
1512 if (res)
1514 *index_p = ipa_get_param_decl_index (info, res);
1515 if (*index_p < 0)
1516 return false;
1517 aggpos->agg_contents = false;
1518 aggpos->by_ref = false;
1519 return true;
1522 if (TREE_CODE (op) == SSA_NAME)
1524 if (SSA_NAME_IS_DEFAULT_DEF (op)
1525 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1526 return false;
1527 stmt = SSA_NAME_DEF_STMT (op);
1528 op = gimple_assign_rhs1 (stmt);
1529 if (!REFERENCE_CLASS_P (op))
1530 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1531 aggpos);
1534 aggpos->agg_contents = true;
1535 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1536 &aggpos->by_ref);
1539 /* See if statement might disappear after inlining.
1540 0 - means not eliminated
1541 1 - half of statements goes away
1542 2 - for sure it is eliminated.
1543 We are not terribly sophisticated, basically looking for simple abstraction
1544 penalty wrappers. */
1546 static int
1547 eliminated_by_inlining_prob (gimple stmt)
1549 enum gimple_code code = gimple_code (stmt);
1550 enum tree_code rhs_code;
1552 if (!optimize)
1553 return 0;
1555 switch (code)
1557 case GIMPLE_RETURN:
1558 return 2;
1559 case GIMPLE_ASSIGN:
1560 if (gimple_num_ops (stmt) != 2)
1561 return 0;
1563 rhs_code = gimple_assign_rhs_code (stmt);
1565 /* Casts of parameters, loads from parameters passed by reference
1566 and stores to return value or parameters are often free after
1567 inlining dua to SRA and further combining.
1568 Assume that half of statements goes away. */
1569 if (rhs_code == CONVERT_EXPR
1570 || rhs_code == NOP_EXPR
1571 || rhs_code == VIEW_CONVERT_EXPR
1572 || rhs_code == ADDR_EXPR
1573 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1575 tree rhs = gimple_assign_rhs1 (stmt);
1576 tree lhs = gimple_assign_lhs (stmt);
1577 tree inner_rhs = get_base_address (rhs);
1578 tree inner_lhs = get_base_address (lhs);
1579 bool rhs_free = false;
1580 bool lhs_free = false;
1582 if (!inner_rhs)
1583 inner_rhs = rhs;
1584 if (!inner_lhs)
1585 inner_lhs = lhs;
1587 /* Reads of parameter are expected to be free. */
1588 if (unmodified_parm (stmt, inner_rhs))
1589 rhs_free = true;
1590 /* Match expressions of form &this->field. Those will most likely
1591 combine with something upstream after inlining. */
1592 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1594 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1595 if (TREE_CODE (op) == PARM_DECL)
1596 rhs_free = true;
1597 else if (TREE_CODE (op) == MEM_REF
1598 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1599 rhs_free = true;
1602 /* When parameter is not SSA register because its address is taken
1603 and it is just copied into one, the statement will be completely
1604 free after inlining (we will copy propagate backward). */
1605 if (rhs_free && is_gimple_reg (lhs))
1606 return 2;
1608 /* Reads of parameters passed by reference
1609 expected to be free (i.e. optimized out after inlining). */
1610 if (TREE_CODE (inner_rhs) == MEM_REF
1611 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1612 rhs_free = true;
1614 /* Copying parameter passed by reference into gimple register is
1615 probably also going to copy propagate, but we can't be quite
1616 sure. */
1617 if (rhs_free && is_gimple_reg (lhs))
1618 lhs_free = true;
1620 /* Writes to parameters, parameters passed by value and return value
1621 (either dirrectly or passed via invisible reference) are free.
1623 TODO: We ought to handle testcase like
1624 struct a {int a,b;};
1625 struct a
1626 retrurnsturct (void)
1628 struct a a ={1,2};
1629 return a;
1632 This translate into:
1634 retrurnsturct ()
1636 int a$b;
1637 int a$a;
1638 struct a a;
1639 struct a D.2739;
1641 <bb 2>:
1642 D.2739.a = 1;
1643 D.2739.b = 2;
1644 return D.2739;
1647 For that we either need to copy ipa-split logic detecting writes
1648 to return value. */
1649 if (TREE_CODE (inner_lhs) == PARM_DECL
1650 || TREE_CODE (inner_lhs) == RESULT_DECL
1651 || (TREE_CODE (inner_lhs) == MEM_REF
1652 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1653 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1654 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1655 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1656 (inner_lhs,
1657 0))) == RESULT_DECL))))
1658 lhs_free = true;
1659 if (lhs_free
1660 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1661 rhs_free = true;
1662 if (lhs_free && rhs_free)
1663 return 1;
1665 return 0;
1666 default:
1667 return 0;
1672 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1673 predicates to the CFG edges. */
1675 static void
1676 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1677 struct inline_summary *summary,
1678 basic_block bb)
1680 gimple last;
1681 tree op;
1682 int index;
1683 struct agg_position_info aggpos;
1684 enum tree_code code, inverted_code;
1685 edge e;
1686 edge_iterator ei;
1687 gimple set_stmt;
1688 tree op2;
1690 last = last_stmt (bb);
1691 if (!last || gimple_code (last) != GIMPLE_COND)
1692 return;
1693 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1694 return;
1695 op = gimple_cond_lhs (last);
1696 /* TODO: handle conditionals like
1697 var = op0 < 4;
1698 if (var != 0). */
1699 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1701 code = gimple_cond_code (last);
1702 inverted_code
1703 = invert_tree_comparison (code,
1704 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1706 FOR_EACH_EDGE (e, ei, bb->succs)
1708 struct predicate p = add_condition (summary, index, &aggpos,
1709 e->flags & EDGE_TRUE_VALUE
1710 ? code : inverted_code,
1711 gimple_cond_rhs (last));
1712 e->aux = pool_alloc (edge_predicate_pool);
1713 *(struct predicate *) e->aux = p;
1717 if (TREE_CODE (op) != SSA_NAME)
1718 return;
1719 /* Special case
1720 if (builtin_constant_p (op))
1721 constant_code
1722 else
1723 nonconstant_code.
1724 Here we can predicate nonconstant_code. We can't
1725 really handle constant_code since we have no predicate
1726 for this and also the constant code is not known to be
1727 optimized away when inliner doen't see operand is constant.
1728 Other optimizers might think otherwise. */
1729 if (gimple_cond_code (last) != NE_EXPR
1730 || !integer_zerop (gimple_cond_rhs (last)))
1731 return;
1732 set_stmt = SSA_NAME_DEF_STMT (op);
1733 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1734 || gimple_call_num_args (set_stmt) != 1)
1735 return;
1736 op2 = gimple_call_arg (set_stmt, 0);
1737 if (!unmodified_parm_or_parm_agg_item
1738 (info, set_stmt, op2, &index, &aggpos))
1739 return;
1740 FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE)
1742 struct predicate p = add_condition (summary, index, &aggpos,
1743 IS_NOT_CONSTANT, NULL_TREE);
1744 e->aux = pool_alloc (edge_predicate_pool);
1745 *(struct predicate *) e->aux = p;
1750 /* If BB ends by a switch we can turn into predicates, attach corresponding
1751 predicates to the CFG edges. */
1753 static void
1754 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1755 struct inline_summary *summary,
1756 basic_block bb)
1758 gimple last;
1759 tree op;
1760 int index;
1761 struct agg_position_info aggpos;
1762 edge e;
1763 edge_iterator ei;
1764 size_t n;
1765 size_t case_idx;
1767 last = last_stmt (bb);
1768 if (!last || gimple_code (last) != GIMPLE_SWITCH)
1769 return;
1770 op = gimple_switch_index (last);
1771 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1772 return;
1774 FOR_EACH_EDGE (e, ei, bb->succs)
1776 e->aux = pool_alloc (edge_predicate_pool);
1777 *(struct predicate *) e->aux = false_predicate ();
1779 n = gimple_switch_num_labels (last);
1780 for (case_idx = 0; case_idx < n; ++case_idx)
1782 tree cl = gimple_switch_label (last, case_idx);
1783 tree min, max;
1784 struct predicate p;
1786 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1787 min = CASE_LOW (cl);
1788 max = CASE_HIGH (cl);
1790 /* For default we might want to construct predicate that none
1791 of cases is met, but it is bit hard to do not having negations
1792 of conditionals handy. */
1793 if (!min && !max)
1794 p = true_predicate ();
1795 else if (!max)
1796 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1797 else
1799 struct predicate p1, p2;
1800 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1801 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1802 p = and_predicates (summary->conds, &p1, &p2);
1804 *(struct predicate *) e->aux
1805 = or_predicates (summary->conds, &p, (struct predicate *) e->aux);
1810 /* For each BB in NODE attach to its AUX pointer predicate under
1811 which it is executable. */
1813 static void
1814 compute_bb_predicates (struct cgraph_node *node,
1815 struct ipa_node_params *parms_info,
1816 struct inline_summary *summary)
1818 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
1819 bool done = false;
1820 basic_block bb;
1822 FOR_EACH_BB_FN (bb, my_function)
1824 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1825 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1828 /* Entry block is always executable. */
1829 ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1830 = pool_alloc (edge_predicate_pool);
1831 *(struct predicate *) ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1832 = true_predicate ();
1834 /* A simple dataflow propagation of predicates forward in the CFG.
1835 TODO: work in reverse postorder. */
1836 while (!done)
1838 done = true;
1839 FOR_EACH_BB_FN (bb, my_function)
1841 struct predicate p = false_predicate ();
1842 edge e;
1843 edge_iterator ei;
1844 FOR_EACH_EDGE (e, ei, bb->preds)
1846 if (e->src->aux)
1848 struct predicate this_bb_predicate
1849 = *(struct predicate *) e->src->aux;
1850 if (e->aux)
1851 this_bb_predicate
1852 = and_predicates (summary->conds, &this_bb_predicate,
1853 (struct predicate *) e->aux);
1854 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1855 if (true_predicate_p (&p))
1856 break;
1859 if (false_predicate_p (&p))
1860 gcc_assert (!bb->aux);
1861 else
1863 if (!bb->aux)
1865 done = false;
1866 bb->aux = pool_alloc (edge_predicate_pool);
1867 *((struct predicate *) bb->aux) = p;
1869 else if (!predicates_equal_p (&p, (struct predicate *) bb->aux))
1871 done = false;
1872 *((struct predicate *) bb->aux) = p;
1880 /* We keep info about constantness of SSA names. */
1882 typedef struct predicate predicate_t;
1883 /* Return predicate specifying when the STMT might have result that is not
1884 a compile time constant. */
1886 static struct predicate
1887 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1888 struct inline_summary *summary,
1889 tree expr,
1890 vec<predicate_t> nonconstant_names)
1892 tree parm;
1893 int index;
1895 while (UNARY_CLASS_P (expr))
1896 expr = TREE_OPERAND (expr, 0);
1898 parm = unmodified_parm (NULL, expr);
1899 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1900 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1901 if (is_gimple_min_invariant (expr))
1902 return false_predicate ();
1903 if (TREE_CODE (expr) == SSA_NAME)
1904 return nonconstant_names[SSA_NAME_VERSION (expr)];
1905 if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr))
1907 struct predicate p1 = will_be_nonconstant_expr_predicate
1908 (info, summary, TREE_OPERAND (expr, 0),
1909 nonconstant_names);
1910 struct predicate p2;
1911 if (true_predicate_p (&p1))
1912 return p1;
1913 p2 = will_be_nonconstant_expr_predicate (info, summary,
1914 TREE_OPERAND (expr, 1),
1915 nonconstant_names);
1916 return or_predicates (summary->conds, &p1, &p2);
1918 else if (TREE_CODE (expr) == COND_EXPR)
1920 struct predicate p1 = will_be_nonconstant_expr_predicate
1921 (info, summary, TREE_OPERAND (expr, 0),
1922 nonconstant_names);
1923 struct predicate p2;
1924 if (true_predicate_p (&p1))
1925 return p1;
1926 p2 = will_be_nonconstant_expr_predicate (info, summary,
1927 TREE_OPERAND (expr, 1),
1928 nonconstant_names);
1929 if (true_predicate_p (&p2))
1930 return p2;
1931 p1 = or_predicates (summary->conds, &p1, &p2);
1932 p2 = will_be_nonconstant_expr_predicate (info, summary,
1933 TREE_OPERAND (expr, 2),
1934 nonconstant_names);
1935 return or_predicates (summary->conds, &p1, &p2);
1937 else
1939 debug_tree (expr);
1940 gcc_unreachable ();
1942 return false_predicate ();
1946 /* Return predicate specifying when the STMT might have result that is not
1947 a compile time constant. */
1949 static struct predicate
1950 will_be_nonconstant_predicate (struct ipa_node_params *info,
1951 struct inline_summary *summary,
1952 gimple stmt,
1953 vec<predicate_t> nonconstant_names)
1955 struct predicate p = true_predicate ();
1956 ssa_op_iter iter;
1957 tree use;
1958 struct predicate op_non_const;
1959 bool is_load;
1960 int base_index;
1961 struct agg_position_info aggpos;
1963 /* What statments might be optimized away
1964 when their arguments are constant
1965 TODO: also trivial builtins.
1966 builtin_constant_p is already handled later. */
1967 if (gimple_code (stmt) != GIMPLE_ASSIGN
1968 && gimple_code (stmt) != GIMPLE_COND
1969 && gimple_code (stmt) != GIMPLE_SWITCH)
1970 return p;
1972 /* Stores will stay anyway. */
1973 if (gimple_store_p (stmt))
1974 return p;
1976 is_load = gimple_assign_load_p (stmt);
1978 /* Loads can be optimized when the value is known. */
1979 if (is_load)
1981 tree op;
1982 gcc_assert (gimple_assign_single_p (stmt));
1983 op = gimple_assign_rhs1 (stmt);
1984 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
1985 &aggpos))
1986 return p;
1988 else
1989 base_index = -1;
1991 /* See if we understand all operands before we start
1992 adding conditionals. */
1993 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1995 tree parm = unmodified_parm (stmt, use);
1996 /* For arguments we can build a condition. */
1997 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
1998 continue;
1999 if (TREE_CODE (use) != SSA_NAME)
2000 return p;
2001 /* If we know when operand is constant,
2002 we still can say something useful. */
2003 if (!true_predicate_p (&nonconstant_names[SSA_NAME_VERSION (use)]))
2004 continue;
2005 return p;
2008 if (is_load)
2009 op_non_const =
2010 add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2011 else
2012 op_non_const = false_predicate ();
2013 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2015 tree parm = unmodified_parm (stmt, use);
2016 int index;
2018 if (parm && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2020 if (index != base_index)
2021 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2022 else
2023 continue;
2025 else
2026 p = nonconstant_names[SSA_NAME_VERSION (use)];
2027 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2029 if (gimple_code (stmt) == GIMPLE_ASSIGN
2030 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2031 nonconstant_names[SSA_NAME_VERSION (gimple_assign_lhs (stmt))]
2032 = op_non_const;
2033 return op_non_const;
2036 struct record_modified_bb_info
2038 bitmap bb_set;
2039 gimple stmt;
2042 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2043 set except for info->stmt. */
2045 static bool
2046 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
2048 struct record_modified_bb_info *info =
2049 (struct record_modified_bb_info *) data;
2050 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2051 return false;
2052 bitmap_set_bit (info->bb_set,
2053 SSA_NAME_IS_DEFAULT_DEF (vdef)
2054 ? ENTRY_BLOCK_PTR->index
2055 : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2056 return false;
2059 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2060 will change since last invocation of STMT.
2062 Value 0 is reserved for compile time invariants.
2063 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2064 ought to be REG_BR_PROB_BASE / estimated_iters. */
2066 static int
2067 param_change_prob (gimple stmt, int i)
2069 tree op = gimple_call_arg (stmt, i);
2070 basic_block bb = gimple_bb (stmt);
2071 tree base;
2073 /* Global invariants neve change. */
2074 if (is_gimple_min_invariant (op))
2075 return 0;
2076 /* We would have to do non-trivial analysis to really work out what
2077 is the probability of value to change (i.e. when init statement
2078 is in a sibling loop of the call).
2080 We do an conservative estimate: when call is executed N times more often
2081 than the statement defining value, we take the frequency 1/N. */
2082 if (TREE_CODE (op) == SSA_NAME)
2084 int init_freq;
2086 if (!bb->frequency)
2087 return REG_BR_PROB_BASE;
2089 if (SSA_NAME_IS_DEFAULT_DEF (op))
2090 init_freq = ENTRY_BLOCK_PTR->frequency;
2091 else
2092 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2094 if (!init_freq)
2095 init_freq = 1;
2096 if (init_freq < bb->frequency)
2097 return MAX (GCOV_COMPUTE_SCALE (init_freq, bb->frequency), 1);
2098 else
2099 return REG_BR_PROB_BASE;
2102 base = get_base_address (op);
2103 if (base)
2105 ao_ref refd;
2106 int max;
2107 struct record_modified_bb_info info;
2108 bitmap_iterator bi;
2109 unsigned index;
2110 tree init = ctor_for_folding (base);
2112 if (init != error_mark_node)
2113 return 0;
2114 if (!bb->frequency)
2115 return REG_BR_PROB_BASE;
2116 ao_ref_init (&refd, op);
2117 info.stmt = stmt;
2118 info.bb_set = BITMAP_ALLOC (NULL);
2119 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2120 NULL);
2121 if (bitmap_bit_p (info.bb_set, bb->index))
2123 BITMAP_FREE (info.bb_set);
2124 return REG_BR_PROB_BASE;
2127 /* Assume that every memory is initialized at entry.
2128 TODO: Can we easilly determine if value is always defined
2129 and thus we may skip entry block? */
2130 if (ENTRY_BLOCK_PTR->frequency)
2131 max = ENTRY_BLOCK_PTR->frequency;
2132 else
2133 max = 1;
2135 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2136 max = MIN (max, BASIC_BLOCK (index)->frequency);
2138 BITMAP_FREE (info.bb_set);
2139 if (max < bb->frequency)
2140 return MAX (GCOV_COMPUTE_SCALE (max, bb->frequency), 1);
2141 else
2142 return REG_BR_PROB_BASE;
2144 return REG_BR_PROB_BASE;
2147 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2148 sub-graph and if the predicate the condition depends on is known. If so,
2149 return true and store the pointer the predicate in *P. */
2151 static bool
2152 phi_result_unknown_predicate (struct ipa_node_params *info,
2153 struct inline_summary *summary, basic_block bb,
2154 struct predicate *p,
2155 vec<predicate_t> nonconstant_names)
2157 edge e;
2158 edge_iterator ei;
2159 basic_block first_bb = NULL;
2160 gimple stmt;
2162 if (single_pred_p (bb))
2164 *p = false_predicate ();
2165 return true;
2168 FOR_EACH_EDGE (e, ei, bb->preds)
2170 if (single_succ_p (e->src))
2172 if (!single_pred_p (e->src))
2173 return false;
2174 if (!first_bb)
2175 first_bb = single_pred (e->src);
2176 else if (single_pred (e->src) != first_bb)
2177 return false;
2179 else
2181 if (!first_bb)
2182 first_bb = e->src;
2183 else if (e->src != first_bb)
2184 return false;
2188 if (!first_bb)
2189 return false;
2191 stmt = last_stmt (first_bb);
2192 if (!stmt
2193 || gimple_code (stmt) != GIMPLE_COND
2194 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2195 return false;
2197 *p = will_be_nonconstant_expr_predicate (info, summary,
2198 gimple_cond_lhs (stmt),
2199 nonconstant_names);
2200 if (true_predicate_p (p))
2201 return false;
2202 else
2203 return true;
2206 /* Given a PHI statement in a function described by inline properties SUMMARY
2207 and *P being the predicate describing whether the selected PHI argument is
2208 known, store a predicate for the result of the PHI statement into
2209 NONCONSTANT_NAMES, if possible. */
2211 static void
2212 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2213 struct predicate *p,
2214 vec<predicate_t> nonconstant_names)
2216 unsigned i;
2218 for (i = 0; i < gimple_phi_num_args (phi); i++)
2220 tree arg = gimple_phi_arg (phi, i)->def;
2221 if (!is_gimple_min_invariant (arg))
2223 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2224 *p = or_predicates (summary->conds, p,
2225 &nonconstant_names[SSA_NAME_VERSION (arg)]);
2226 if (true_predicate_p (p))
2227 return;
2231 if (dump_file && (dump_flags & TDF_DETAILS))
2233 fprintf (dump_file, "\t\tphi predicate: ");
2234 dump_predicate (dump_file, summary->conds, p);
2236 nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p;
2239 /* Return predicate specifying when array index in access OP becomes non-constant. */
2241 static struct predicate
2242 array_index_predicate (struct inline_summary *info,
2243 vec< predicate_t> nonconstant_names, tree op)
2245 struct predicate p = false_predicate ();
2246 while (handled_component_p (op))
2248 if (TREE_CODE (op) == ARRAY_REF || TREE_CODE (op) == ARRAY_RANGE_REF)
2250 if (TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME)
2251 p = or_predicates (info->conds, &p,
2252 &nonconstant_names[SSA_NAME_VERSION
2253 (TREE_OPERAND (op, 1))]);
2255 op = TREE_OPERAND (op, 0);
2257 return p;
2260 /* For a typical usage of __builtin_expect (a<b, 1), we
2261 may introduce an extra relation stmt:
2262 With the builtin, we have
2263 t1 = a <= b;
2264 t2 = (long int) t1;
2265 t3 = __builtin_expect (t2, 1);
2266 if (t3 != 0)
2267 goto ...
2268 Without the builtin, we have
2269 if (a<=b)
2270 goto...
2271 This affects the size/time estimation and may have
2272 an impact on the earlier inlining.
2273 Here find this pattern and fix it up later. */
2275 static gimple
2276 find_foldable_builtin_expect (basic_block bb)
2278 gimple_stmt_iterator bsi;
2280 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2282 gimple stmt = gsi_stmt (bsi);
2283 if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT))
2285 tree var = gimple_call_lhs (stmt);
2286 tree arg = gimple_call_arg (stmt, 0);
2287 use_operand_p use_p;
2288 gimple use_stmt;
2289 bool match = false;
2290 bool done = false;
2292 if (!var || !arg)
2293 continue;
2294 gcc_assert (TREE_CODE (var) == SSA_NAME);
2296 while (TREE_CODE (arg) == SSA_NAME)
2298 gimple stmt_tmp = SSA_NAME_DEF_STMT (arg);
2299 if (!is_gimple_assign (stmt_tmp))
2300 break;
2301 switch (gimple_assign_rhs_code (stmt_tmp))
2303 case LT_EXPR:
2304 case LE_EXPR:
2305 case GT_EXPR:
2306 case GE_EXPR:
2307 case EQ_EXPR:
2308 case NE_EXPR:
2309 match = true;
2310 done = true;
2311 break;
2312 case NOP_EXPR:
2313 break;
2314 default:
2315 done = true;
2316 break;
2318 if (done)
2319 break;
2320 arg = gimple_assign_rhs1 (stmt_tmp);
2323 if (match && single_imm_use (var, &use_p, &use_stmt)
2324 && gimple_code (use_stmt) == GIMPLE_COND)
2325 return use_stmt;
2328 return NULL;
2331 /* Compute function body size parameters for NODE.
2332 When EARLY is true, we compute only simple summaries without
2333 non-trivial predicates to drive the early inliner. */
2335 static void
2336 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2338 gcov_type time = 0;
2339 /* Estimate static overhead for function prologue/epilogue and alignment. */
2340 int size = 2;
2341 /* Benefits are scaled by probability of elimination that is in range
2342 <0,2>. */
2343 basic_block bb;
2344 gimple_stmt_iterator bsi;
2345 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
2346 int freq;
2347 struct inline_summary *info = inline_summary (node);
2348 struct predicate bb_predicate;
2349 struct ipa_node_params *parms_info = NULL;
2350 vec<predicate_t> nonconstant_names = vNULL;
2351 int nblocks, n;
2352 int *order;
2353 predicate array_index = true_predicate ();
2354 gimple fix_builtin_expect_stmt;
2356 info->conds = NULL;
2357 info->entry = NULL;
2359 if (optimize && !early)
2361 calculate_dominance_info (CDI_DOMINATORS);
2362 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2364 if (ipa_node_params_vector.exists ())
2366 parms_info = IPA_NODE_REF (node);
2367 nonconstant_names.safe_grow_cleared
2368 (SSANAMES (my_function)->length ());
2372 if (dump_file)
2373 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2374 cgraph_node_name (node));
2376 /* When we run into maximal number of entries, we assign everything to the
2377 constant truth case. Be sure to have it in list. */
2378 bb_predicate = true_predicate ();
2379 account_size_time (info, 0, 0, &bb_predicate);
2381 bb_predicate = not_inlined_predicate ();
2382 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2384 gcc_assert (my_function && my_function->cfg);
2385 if (parms_info)
2386 compute_bb_predicates (node, parms_info, info);
2387 gcc_assert (cfun == my_function);
2388 order = XNEWVEC (int, n_basic_blocks);
2389 nblocks = pre_and_rev_post_order_compute (NULL, order, false);
2390 for (n = 0; n < nblocks; n++)
2392 bb = BASIC_BLOCK (order[n]);
2393 freq = compute_call_stmt_bb_frequency (node->symbol.decl, bb);
2395 /* TODO: Obviously predicates can be propagated down across CFG. */
2396 if (parms_info)
2398 if (bb->aux)
2399 bb_predicate = *(struct predicate *) bb->aux;
2400 else
2401 bb_predicate = false_predicate ();
2403 else
2404 bb_predicate = true_predicate ();
2406 if (dump_file && (dump_flags & TDF_DETAILS))
2408 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2409 dump_predicate (dump_file, info->conds, &bb_predicate);
2412 if (parms_info && nonconstant_names.exists ())
2414 struct predicate phi_predicate;
2415 bool first_phi = true;
2417 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2419 if (first_phi
2420 && !phi_result_unknown_predicate (parms_info, info, bb,
2421 &phi_predicate,
2422 nonconstant_names))
2423 break;
2424 first_phi = false;
2425 if (dump_file && (dump_flags & TDF_DETAILS))
2427 fprintf (dump_file, " ");
2428 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2430 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2431 nonconstant_names);
2435 fix_builtin_expect_stmt = find_foldable_builtin_expect (bb);
2437 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2439 gimple stmt = gsi_stmt (bsi);
2440 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2441 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2442 int prob;
2443 struct predicate will_be_nonconstant;
2445 /* This relation stmt should be folded after we remove
2446 buildin_expect call. Adjust the cost here. */
2447 if (stmt == fix_builtin_expect_stmt)
2449 this_size--;
2450 this_time--;
2453 if (dump_file && (dump_flags & TDF_DETAILS))
2455 fprintf (dump_file, " ");
2456 print_gimple_stmt (dump_file, stmt, 0, 0);
2457 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2458 ((double) freq) / CGRAPH_FREQ_BASE, this_size,
2459 this_time);
2462 if (gimple_assign_load_p (stmt) && nonconstant_names.exists ())
2464 struct predicate this_array_index;
2465 this_array_index =
2466 array_index_predicate (info, nonconstant_names,
2467 gimple_assign_rhs1 (stmt));
2468 if (!false_predicate_p (&this_array_index))
2469 array_index =
2470 and_predicates (info->conds, &array_index,
2471 &this_array_index);
2473 if (gimple_store_p (stmt) && nonconstant_names.exists ())
2475 struct predicate this_array_index;
2476 this_array_index =
2477 array_index_predicate (info, nonconstant_names,
2478 gimple_get_lhs (stmt));
2479 if (!false_predicate_p (&this_array_index))
2480 array_index =
2481 and_predicates (info->conds, &array_index,
2482 &this_array_index);
2486 if (is_gimple_call (stmt))
2488 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2489 struct inline_edge_summary *es = inline_edge_summary (edge);
2491 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2492 resolved as constant. We however don't want to optimize
2493 out the cgraph edges. */
2494 if (nonconstant_names.exists ()
2495 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2496 && gimple_call_lhs (stmt)
2497 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2499 struct predicate false_p = false_predicate ();
2500 nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))]
2501 = false_p;
2503 if (ipa_node_params_vector.exists ())
2505 int count = gimple_call_num_args (stmt);
2506 int i;
2508 if (count)
2509 es->param.safe_grow_cleared (count);
2510 for (i = 0; i < count; i++)
2512 int prob = param_change_prob (stmt, i);
2513 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2514 es->param[i].change_prob = prob;
2518 es->call_stmt_size = this_size;
2519 es->call_stmt_time = this_time;
2520 es->loop_depth = bb_loop_depth (bb);
2521 edge_set_predicate (edge, &bb_predicate);
2524 /* TODO: When conditional jump or swithc is known to be constant, but
2525 we did not translate it into the predicates, we really can account
2526 just maximum of the possible paths. */
2527 if (parms_info)
2528 will_be_nonconstant
2529 = will_be_nonconstant_predicate (parms_info, info,
2530 stmt, nonconstant_names);
2531 if (this_time || this_size)
2533 struct predicate p;
2535 this_time *= freq;
2537 prob = eliminated_by_inlining_prob (stmt);
2538 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2539 fprintf (dump_file,
2540 "\t\t50%% will be eliminated by inlining\n");
2541 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2542 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2544 if (parms_info)
2545 p = and_predicates (info->conds, &bb_predicate,
2546 &will_be_nonconstant);
2547 else
2548 p = true_predicate ();
2550 if (!false_predicate_p (&p))
2552 time += this_time;
2553 size += this_size;
2554 if (time > MAX_TIME * INLINE_TIME_SCALE)
2555 time = MAX_TIME * INLINE_TIME_SCALE;
2558 /* We account everything but the calls. Calls have their own
2559 size/time info attached to cgraph edges. This is necessary
2560 in order to make the cost disappear after inlining. */
2561 if (!is_gimple_call (stmt))
2563 if (prob)
2565 struct predicate ip = not_inlined_predicate ();
2566 ip = and_predicates (info->conds, &ip, &p);
2567 account_size_time (info, this_size * prob,
2568 this_time * prob, &ip);
2570 if (prob != 2)
2571 account_size_time (info, this_size * (2 - prob),
2572 this_time * (2 - prob), &p);
2575 gcc_assert (time >= 0);
2576 gcc_assert (size >= 0);
2580 set_hint_predicate (&inline_summary (node)->array_index, array_index);
2581 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2582 if (time > MAX_TIME)
2583 time = MAX_TIME;
2584 free (order);
2586 if (!early && nonconstant_names.exists ())
2588 struct loop *loop;
2589 loop_iterator li;
2590 predicate loop_iterations = true_predicate ();
2591 predicate loop_stride = true_predicate ();
2593 if (dump_file && (dump_flags & TDF_DETAILS))
2594 flow_loops_dump (dump_file, NULL, 0);
2595 scev_initialize ();
2596 FOR_EACH_LOOP (li, loop, 0)
2598 vec<edge> exits;
2599 edge ex;
2600 unsigned int j, i;
2601 struct tree_niter_desc niter_desc;
2602 basic_block *body = get_loop_body (loop);
2603 bb_predicate = *(struct predicate *) loop->header->aux;
2605 exits = get_loop_exit_edges (loop);
2606 FOR_EACH_VEC_ELT (exits, j, ex)
2607 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2608 && !is_gimple_min_invariant (niter_desc.niter))
2610 predicate will_be_nonconstant
2611 = will_be_nonconstant_expr_predicate (parms_info, info,
2612 niter_desc.niter,
2613 nonconstant_names);
2614 if (!true_predicate_p (&will_be_nonconstant))
2615 will_be_nonconstant = and_predicates (info->conds,
2616 &bb_predicate,
2617 &will_be_nonconstant);
2618 if (!true_predicate_p (&will_be_nonconstant)
2619 && !false_predicate_p (&will_be_nonconstant))
2620 /* This is slightly inprecise. We may want to represent each
2621 loop with independent predicate. */
2622 loop_iterations =
2623 and_predicates (info->conds, &loop_iterations,
2624 &will_be_nonconstant);
2626 exits.release ();
2628 for (i = 0; i < loop->num_nodes; i++)
2630 gimple_stmt_iterator gsi;
2631 bb_predicate = *(struct predicate *) body[i]->aux;
2632 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi);
2633 gsi_next (&gsi))
2635 gimple stmt = gsi_stmt (gsi);
2636 affine_iv iv;
2637 ssa_op_iter iter;
2638 tree use;
2640 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2642 predicate will_be_nonconstant;
2644 if (!simple_iv
2645 (loop, loop_containing_stmt (stmt), use, &iv, true)
2646 || is_gimple_min_invariant (iv.step))
2647 continue;
2648 will_be_nonconstant
2649 = will_be_nonconstant_expr_predicate (parms_info, info,
2650 iv.step,
2651 nonconstant_names);
2652 if (!true_predicate_p (&will_be_nonconstant))
2653 will_be_nonconstant
2654 = and_predicates (info->conds,
2655 &bb_predicate,
2656 &will_be_nonconstant);
2657 if (!true_predicate_p (&will_be_nonconstant)
2658 && !false_predicate_p (&will_be_nonconstant))
2659 /* This is slightly inprecise. We may want to represent
2660 each loop with independent predicate. */
2661 loop_stride =
2662 and_predicates (info->conds, &loop_stride,
2663 &will_be_nonconstant);
2667 free (body);
2669 set_hint_predicate (&inline_summary (node)->loop_iterations,
2670 loop_iterations);
2671 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2672 scev_finalize ();
2674 FOR_ALL_BB_FN (bb, my_function)
2676 edge e;
2677 edge_iterator ei;
2679 if (bb->aux)
2680 pool_free (edge_predicate_pool, bb->aux);
2681 bb->aux = NULL;
2682 FOR_EACH_EDGE (e, ei, bb->succs)
2684 if (e->aux)
2685 pool_free (edge_predicate_pool, e->aux);
2686 e->aux = NULL;
2689 inline_summary (node)->self_time = time;
2690 inline_summary (node)->self_size = size;
2691 nonconstant_names.release ();
2692 if (optimize && !early)
2694 loop_optimizer_finalize ();
2695 free_dominance_info (CDI_DOMINATORS);
2697 if (dump_file)
2699 fprintf (dump_file, "\n");
2700 dump_inline_summary (dump_file, node);
2705 /* Compute parameters of functions used by inliner.
2706 EARLY is true when we compute parameters for the early inliner */
2708 void
2709 compute_inline_parameters (struct cgraph_node *node, bool early)
2711 HOST_WIDE_INT self_stack_size;
2712 struct cgraph_edge *e;
2713 struct inline_summary *info;
2715 gcc_assert (!node->global.inlined_to);
2717 inline_summary_alloc ();
2719 info = inline_summary (node);
2720 reset_inline_summary (node);
2722 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2723 Once this happen, we will need to more curefully predict call
2724 statement size. */
2725 if (node->thunk.thunk_p)
2727 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2728 struct predicate t = true_predicate ();
2730 info->inlinable = 0;
2731 node->callees->call_stmt_cannot_inline_p = true;
2732 node->local.can_change_signature = false;
2733 es->call_stmt_time = 1;
2734 es->call_stmt_size = 1;
2735 account_size_time (info, 0, 0, &t);
2736 return;
2739 /* Even is_gimple_min_invariant rely on current_function_decl. */
2740 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2742 /* Estimate the stack size for the function if we're optimizing. */
2743 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2744 info->estimated_self_stack_size = self_stack_size;
2745 info->estimated_stack_size = self_stack_size;
2746 info->stack_frame_offset = 0;
2748 /* Can this function be inlined at all? */
2749 if (!optimize && !lookup_attribute ("always_inline",
2750 DECL_ATTRIBUTES (node->symbol.decl)))
2751 info->inlinable = false;
2752 else
2753 info->inlinable = tree_inlinable_function_p (node->symbol.decl);
2755 /* Type attributes can use parameter indices to describe them. */
2756 if (TYPE_ATTRIBUTES (TREE_TYPE (node->symbol.decl)))
2757 node->local.can_change_signature = false;
2758 else
2760 /* Otherwise, inlinable functions always can change signature. */
2761 if (info->inlinable)
2762 node->local.can_change_signature = true;
2763 else
2765 /* Functions calling builtin_apply can not change signature. */
2766 for (e = node->callees; e; e = e->next_callee)
2768 tree cdecl = e->callee->symbol.decl;
2769 if (DECL_BUILT_IN (cdecl)
2770 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2771 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2772 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2773 break;
2775 node->local.can_change_signature = !e;
2778 estimate_function_body_sizes (node, early);
2780 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2781 info->time = info->self_time;
2782 info->size = info->self_size;
2783 info->stack_frame_offset = 0;
2784 info->estimated_stack_size = info->estimated_self_stack_size;
2785 #ifdef ENABLE_CHECKING
2786 inline_update_overall_summary (node);
2787 gcc_assert (info->time == info->self_time && info->size == info->self_size);
2788 #endif
2790 pop_cfun ();
2794 /* Compute parameters of functions used by inliner using
2795 current_function_decl. */
2797 static unsigned int
2798 compute_inline_parameters_for_current (void)
2800 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2801 return 0;
2804 namespace {
2806 const pass_data pass_data_inline_parameters =
2808 GIMPLE_PASS, /* type */
2809 "inline_param", /* name */
2810 OPTGROUP_INLINE, /* optinfo_flags */
2811 false, /* has_gate */
2812 true, /* has_execute */
2813 TV_INLINE_PARAMETERS, /* tv_id */
2814 0, /* properties_required */
2815 0, /* properties_provided */
2816 0, /* properties_destroyed */
2817 0, /* todo_flags_start */
2818 0, /* todo_flags_finish */
2821 class pass_inline_parameters : public gimple_opt_pass
2823 public:
2824 pass_inline_parameters (gcc::context *ctxt)
2825 : gimple_opt_pass (pass_data_inline_parameters, ctxt)
2828 /* opt_pass methods: */
2829 opt_pass * clone () { return new pass_inline_parameters (m_ctxt); }
2830 unsigned int execute () {
2831 return compute_inline_parameters_for_current ();
2834 }; // class pass_inline_parameters
2836 } // anon namespace
2838 gimple_opt_pass *
2839 make_pass_inline_parameters (gcc::context *ctxt)
2841 return new pass_inline_parameters (ctxt);
2845 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2846 KNOWN_BINFOS. */
2848 static bool
2849 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2850 int *size, int *time,
2851 vec<tree> known_vals,
2852 vec<tree> known_binfos,
2853 vec<ipa_agg_jump_function_p> known_aggs)
2855 tree target;
2856 struct cgraph_node *callee;
2857 struct inline_summary *isummary;
2859 if (!known_vals.exists () && !known_binfos.exists ())
2860 return false;
2861 if (!flag_indirect_inlining)
2862 return false;
2864 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2865 known_aggs);
2866 if (!target)
2867 return false;
2869 /* Account for difference in cost between indirect and direct calls. */
2870 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
2871 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
2872 gcc_checking_assert (*time >= 0);
2873 gcc_checking_assert (*size >= 0);
2875 callee = cgraph_get_node (target);
2876 if (!callee || !callee->symbol.definition)
2877 return false;
2878 isummary = inline_summary (callee);
2879 return isummary->inlinable;
2882 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2884 static inline void
2885 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2886 int prob,
2887 vec<tree> known_vals,
2888 vec<tree> known_binfos,
2889 vec<ipa_agg_jump_function_p> known_aggs,
2890 inline_hints *hints)
2892 struct inline_edge_summary *es = inline_edge_summary (e);
2893 int call_size = es->call_stmt_size;
2894 int call_time = es->call_stmt_time;
2895 if (!e->callee
2896 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
2897 known_vals, known_binfos, known_aggs)
2898 && hints && cgraph_maybe_hot_edge_p (e))
2899 *hints |= INLINE_HINT_indirect_call;
2900 *size += call_size * INLINE_SIZE_SCALE;
2901 *time += apply_probability ((gcov_type) call_time, prob)
2902 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
2903 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2904 *time = MAX_TIME * INLINE_TIME_SCALE;
2909 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.
2910 POSSIBLE_TRUTHS, KNOWN_VALS and KNOWN_BINFOS describe context of the call
2911 site. */
2913 static void
2914 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
2915 inline_hints *hints,
2916 clause_t possible_truths,
2917 vec<tree> known_vals,
2918 vec<tree> known_binfos,
2919 vec<ipa_agg_jump_function_p> known_aggs)
2921 struct cgraph_edge *e;
2922 for (e = node->callees; e; e = e->next_callee)
2924 struct inline_edge_summary *es = inline_edge_summary (e);
2925 if (!es->predicate
2926 || evaluate_predicate (es->predicate, possible_truths))
2928 if (e->inline_failed)
2930 /* Predicates of calls shall not use NOT_CHANGED codes,
2931 sowe do not need to compute probabilities. */
2932 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
2933 known_vals, known_binfos,
2934 known_aggs, hints);
2936 else
2937 estimate_calls_size_and_time (e->callee, size, time, hints,
2938 possible_truths,
2939 known_vals, known_binfos,
2940 known_aggs);
2943 for (e = node->indirect_calls; e; e = e->next_callee)
2945 struct inline_edge_summary *es = inline_edge_summary (e);
2946 if (!es->predicate
2947 || evaluate_predicate (es->predicate, possible_truths))
2948 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
2949 known_vals, known_binfos, known_aggs,
2950 hints);
2955 /* Estimate size and time needed to execute NODE assuming
2956 POSSIBLE_TRUTHS clause, and KNOWN_VALS and KNOWN_BINFOS information
2957 about NODE's arguments. */
2959 static void
2960 estimate_node_size_and_time (struct cgraph_node *node,
2961 clause_t possible_truths,
2962 vec<tree> known_vals,
2963 vec<tree> known_binfos,
2964 vec<ipa_agg_jump_function_p> known_aggs,
2965 int *ret_size, int *ret_time,
2966 inline_hints *ret_hints,
2967 vec<inline_param_summary_t>
2968 inline_param_summary)
2970 struct inline_summary *info = inline_summary (node);
2971 size_time_entry *e;
2972 int size = 0;
2973 int time = 0;
2974 inline_hints hints = 0;
2975 int i;
2977 if (dump_file && (dump_flags & TDF_DETAILS))
2979 bool found = false;
2980 fprintf (dump_file, " Estimating body: %s/%i\n"
2981 " Known to be false: ", cgraph_node_name (node),
2982 node->symbol.order);
2984 for (i = predicate_not_inlined_condition;
2985 i < (predicate_first_dynamic_condition
2986 + (int) vec_safe_length (info->conds)); i++)
2987 if (!(possible_truths & (1 << i)))
2989 if (found)
2990 fprintf (dump_file, ", ");
2991 found = true;
2992 dump_condition (dump_file, info->conds, i);
2996 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
2997 if (evaluate_predicate (&e->predicate, possible_truths))
2999 size += e->size;
3000 gcc_checking_assert (e->time >= 0);
3001 gcc_checking_assert (time >= 0);
3002 if (!inline_param_summary.exists ())
3003 time += e->time;
3004 else
3006 int prob = predicate_probability (info->conds,
3007 &e->predicate,
3008 possible_truths,
3009 inline_param_summary);
3010 gcc_checking_assert (prob >= 0);
3011 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
3012 time += apply_probability ((gcov_type) e->time, prob);
3014 if (time > MAX_TIME * INLINE_TIME_SCALE)
3015 time = MAX_TIME * INLINE_TIME_SCALE;
3016 gcc_checking_assert (time >= 0);
3019 gcc_checking_assert (size >= 0);
3020 gcc_checking_assert (time >= 0);
3022 if (info->loop_iterations
3023 && !evaluate_predicate (info->loop_iterations, possible_truths))
3024 hints |= INLINE_HINT_loop_iterations;
3025 if (info->loop_stride
3026 && !evaluate_predicate (info->loop_stride, possible_truths))
3027 hints |= INLINE_HINT_loop_stride;
3028 if (info->array_index
3029 && !evaluate_predicate (info->array_index, possible_truths))
3030 hints |= INLINE_HINT_array_index;
3031 if (info->scc_no)
3032 hints |= INLINE_HINT_in_scc;
3033 if (DECL_DECLARED_INLINE_P (node->symbol.decl))
3034 hints |= INLINE_HINT_declared_inline;
3036 estimate_calls_size_and_time (node, &size, &time, &hints, possible_truths,
3037 known_vals, known_binfos, known_aggs);
3038 gcc_checking_assert (size >= 0);
3039 gcc_checking_assert (time >= 0);
3040 time = RDIV (time, INLINE_TIME_SCALE);
3041 size = RDIV (size, INLINE_SIZE_SCALE);
3043 if (dump_file && (dump_flags & TDF_DETAILS))
3044 fprintf (dump_file, "\n size:%i time:%i\n", (int) size, (int) time);
3045 if (ret_time)
3046 *ret_time = time;
3047 if (ret_size)
3048 *ret_size = size;
3049 if (ret_hints)
3050 *ret_hints = hints;
3051 return;
3055 /* Estimate size and time needed to execute callee of EDGE assuming that
3056 parameters known to be constant at caller of EDGE are propagated.
3057 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
3058 and types for parameters. */
3060 void
3061 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
3062 vec<tree> known_vals,
3063 vec<tree> known_binfos,
3064 vec<ipa_agg_jump_function_p> known_aggs,
3065 int *ret_size, int *ret_time,
3066 inline_hints *hints)
3068 clause_t clause;
3070 clause = evaluate_conditions_for_known_args (node, false, known_vals,
3071 known_aggs);
3072 estimate_node_size_and_time (node, clause, known_vals, known_binfos,
3073 known_aggs, ret_size, ret_time, hints, vNULL);
3076 /* Translate all conditions from callee representation into caller
3077 representation and symbolically evaluate predicate P into new predicate.
3079 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
3080 is summary of function predicate P is from. OPERAND_MAP is array giving
3081 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
3082 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
3083 predicate under which callee is executed. OFFSET_MAP is an array of of
3084 offsets that need to be added to conditions, negative offset means that
3085 conditions relying on values passed by reference have to be discarded
3086 because they might not be preserved (and should be considered offset zero
3087 for other purposes). */
3089 static struct predicate
3090 remap_predicate (struct inline_summary *info,
3091 struct inline_summary *callee_info,
3092 struct predicate *p,
3093 vec<int> operand_map,
3094 vec<int> offset_map,
3095 clause_t possible_truths, struct predicate *toplev_predicate)
3097 int i;
3098 struct predicate out = true_predicate ();
3100 /* True predicate is easy. */
3101 if (true_predicate_p (p))
3102 return *toplev_predicate;
3103 for (i = 0; p->clause[i]; i++)
3105 clause_t clause = p->clause[i];
3106 int cond;
3107 struct predicate clause_predicate = false_predicate ();
3109 gcc_assert (i < MAX_CLAUSES);
3111 for (cond = 0; cond < NUM_CONDITIONS; cond++)
3112 /* Do we have condition we can't disprove? */
3113 if (clause & possible_truths & (1 << cond))
3115 struct predicate cond_predicate;
3116 /* Work out if the condition can translate to predicate in the
3117 inlined function. */
3118 if (cond >= predicate_first_dynamic_condition)
3120 struct condition *c;
3122 c = &(*callee_info->conds)[cond
3124 predicate_first_dynamic_condition];
3125 /* See if we can remap condition operand to caller's operand.
3126 Otherwise give up. */
3127 if (!operand_map.exists ()
3128 || (int) operand_map.length () <= c->operand_num
3129 || operand_map[c->operand_num] == -1
3130 /* TODO: For non-aggregate conditions, adding an offset is
3131 basically an arithmetic jump function processing which
3132 we should support in future. */
3133 || ((!c->agg_contents || !c->by_ref)
3134 && offset_map[c->operand_num] > 0)
3135 || (c->agg_contents && c->by_ref
3136 && offset_map[c->operand_num] < 0))
3137 cond_predicate = true_predicate ();
3138 else
3140 struct agg_position_info ap;
3141 HOST_WIDE_INT offset_delta = offset_map[c->operand_num];
3142 if (offset_delta < 0)
3144 gcc_checking_assert (!c->agg_contents || !c->by_ref);
3145 offset_delta = 0;
3147 gcc_assert (!c->agg_contents
3148 || c->by_ref || offset_delta == 0);
3149 ap.offset = c->offset + offset_delta;
3150 ap.agg_contents = c->agg_contents;
3151 ap.by_ref = c->by_ref;
3152 cond_predicate = add_condition (info,
3153 operand_map[c->operand_num],
3154 &ap, c->code, c->val);
3157 /* Fixed conditions remains same, construct single
3158 condition predicate. */
3159 else
3161 cond_predicate.clause[0] = 1 << cond;
3162 cond_predicate.clause[1] = 0;
3164 clause_predicate = or_predicates (info->conds, &clause_predicate,
3165 &cond_predicate);
3167 out = and_predicates (info->conds, &out, &clause_predicate);
3169 return and_predicates (info->conds, &out, toplev_predicate);
3173 /* Update summary information of inline clones after inlining.
3174 Compute peak stack usage. */
3176 static void
3177 inline_update_callee_summaries (struct cgraph_node *node, int depth)
3179 struct cgraph_edge *e;
3180 struct inline_summary *callee_info = inline_summary (node);
3181 struct inline_summary *caller_info = inline_summary (node->callers->caller);
3182 HOST_WIDE_INT peak;
3184 callee_info->stack_frame_offset
3185 = caller_info->stack_frame_offset
3186 + caller_info->estimated_self_stack_size;
3187 peak = callee_info->stack_frame_offset
3188 + callee_info->estimated_self_stack_size;
3189 if (inline_summary (node->global.inlined_to)->estimated_stack_size < peak)
3190 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
3191 ipa_propagate_frequency (node);
3192 for (e = node->callees; e; e = e->next_callee)
3194 if (!e->inline_failed)
3195 inline_update_callee_summaries (e->callee, depth);
3196 inline_edge_summary (e)->loop_depth += depth;
3198 for (e = node->indirect_calls; e; e = e->next_callee)
3199 inline_edge_summary (e)->loop_depth += depth;
3202 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3203 When functoin A is inlined in B and A calls C with parameter that
3204 changes with probability PROB1 and C is known to be passthroug
3205 of argument if B that change with probability PROB2, the probability
3206 of change is now PROB1*PROB2. */
3208 static void
3209 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3210 struct cgraph_edge *edge)
3212 if (ipa_node_params_vector.exists ())
3214 int i;
3215 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3216 struct inline_edge_summary *es = inline_edge_summary (edge);
3217 struct inline_edge_summary *inlined_es
3218 = inline_edge_summary (inlined_edge);
3220 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3222 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3223 if (jfunc->type == IPA_JF_PASS_THROUGH
3224 && (ipa_get_jf_pass_through_formal_id (jfunc)
3225 < (int) inlined_es->param.length ()))
3227 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3228 int prob1 = es->param[i].change_prob;
3229 int prob2 = inlined_es->param[jf_formal_id].change_prob;
3230 int prob = combine_probabilities (prob1, prob2);
3232 if (prob1 && prob2 && !prob)
3233 prob = 1;
3235 es->param[i].change_prob = prob;
3241 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3243 Remap predicates of callees of NODE. Rest of arguments match
3244 remap_predicate.
3246 Also update change probabilities. */
3248 static void
3249 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3250 struct cgraph_node *node,
3251 struct inline_summary *info,
3252 struct inline_summary *callee_info,
3253 vec<int> operand_map,
3254 vec<int> offset_map,
3255 clause_t possible_truths,
3256 struct predicate *toplev_predicate)
3258 struct cgraph_edge *e;
3259 for (e = node->callees; e; e = e->next_callee)
3261 struct inline_edge_summary *es = inline_edge_summary (e);
3262 struct predicate p;
3264 if (e->inline_failed)
3266 remap_edge_change_prob (inlined_edge, e);
3268 if (es->predicate)
3270 p = remap_predicate (info, callee_info,
3271 es->predicate, operand_map, offset_map,
3272 possible_truths, toplev_predicate);
3273 edge_set_predicate (e, &p);
3274 /* TODO: We should remove the edge for code that will be
3275 optimized out, but we need to keep verifiers and tree-inline
3276 happy. Make it cold for now. */
3277 if (false_predicate_p (&p))
3279 e->count = 0;
3280 e->frequency = 0;
3283 else
3284 edge_set_predicate (e, toplev_predicate);
3286 else
3287 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3288 operand_map, offset_map, possible_truths,
3289 toplev_predicate);
3291 for (e = node->indirect_calls; e; e = e->next_callee)
3293 struct inline_edge_summary *es = inline_edge_summary (e);
3294 struct predicate p;
3296 remap_edge_change_prob (inlined_edge, e);
3297 if (es->predicate)
3299 p = remap_predicate (info, callee_info,
3300 es->predicate, operand_map, offset_map,
3301 possible_truths, toplev_predicate);
3302 edge_set_predicate (e, &p);
3303 /* TODO: We should remove the edge for code that will be optimized
3304 out, but we need to keep verifiers and tree-inline happy.
3305 Make it cold for now. */
3306 if (false_predicate_p (&p))
3308 e->count = 0;
3309 e->frequency = 0;
3312 else
3313 edge_set_predicate (e, toplev_predicate);
3317 /* Same as remap_predicate, but set result into hint *HINT. */
3319 static void
3320 remap_hint_predicate (struct inline_summary *info,
3321 struct inline_summary *callee_info,
3322 struct predicate **hint,
3323 vec<int> operand_map,
3324 vec<int> offset_map,
3325 clause_t possible_truths,
3326 struct predicate *toplev_predicate)
3328 predicate p;
3330 if (!*hint)
3331 return;
3332 p = remap_predicate (info, callee_info,
3333 *hint,
3334 operand_map, offset_map,
3335 possible_truths, toplev_predicate);
3336 if (!false_predicate_p (&p) && !true_predicate_p (&p))
3338 if (!*hint)
3339 set_hint_predicate (hint, p);
3340 else
3341 **hint = and_predicates (info->conds, *hint, &p);
3345 /* We inlined EDGE. Update summary of the function we inlined into. */
3347 void
3348 inline_merge_summary (struct cgraph_edge *edge)
3350 struct inline_summary *callee_info = inline_summary (edge->callee);
3351 struct cgraph_node *to = (edge->caller->global.inlined_to
3352 ? edge->caller->global.inlined_to : edge->caller);
3353 struct inline_summary *info = inline_summary (to);
3354 clause_t clause = 0; /* not_inline is known to be false. */
3355 size_time_entry *e;
3356 vec<int> operand_map = vNULL;
3357 vec<int> offset_map = vNULL;
3358 int i;
3359 struct predicate toplev_predicate;
3360 struct predicate true_p = true_predicate ();
3361 struct inline_edge_summary *es = inline_edge_summary (edge);
3363 if (es->predicate)
3364 toplev_predicate = *es->predicate;
3365 else
3366 toplev_predicate = true_predicate ();
3368 if (ipa_node_params_vector.exists () && callee_info->conds)
3370 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3371 int count = ipa_get_cs_argument_count (args);
3372 int i;
3374 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3375 if (count)
3377 operand_map.safe_grow_cleared (count);
3378 offset_map.safe_grow_cleared (count);
3380 for (i = 0; i < count; i++)
3382 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3383 int map = -1;
3385 /* TODO: handle non-NOPs when merging. */
3386 if (jfunc->type == IPA_JF_PASS_THROUGH)
3388 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3389 map = ipa_get_jf_pass_through_formal_id (jfunc);
3390 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3391 offset_map[i] = -1;
3393 else if (jfunc->type == IPA_JF_ANCESTOR)
3395 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3396 if (offset >= 0 && offset < INT_MAX)
3398 map = ipa_get_jf_ancestor_formal_id (jfunc);
3399 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3400 offset = -1;
3401 offset_map[i] = offset;
3404 operand_map[i] = map;
3405 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3408 for (i = 0; vec_safe_iterate (callee_info->entry, i, &e); i++)
3410 struct predicate p = remap_predicate (info, callee_info,
3411 &e->predicate, operand_map,
3412 offset_map, clause,
3413 &toplev_predicate);
3414 if (!false_predicate_p (&p))
3416 gcov_type add_time = ((gcov_type) e->time * edge->frequency
3417 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3418 int prob = predicate_probability (callee_info->conds,
3419 &e->predicate,
3420 clause, es->param);
3421 add_time = apply_probability ((gcov_type) add_time, prob);
3422 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3423 add_time = MAX_TIME * INLINE_TIME_SCALE;
3424 if (prob != REG_BR_PROB_BASE
3425 && dump_file && (dump_flags & TDF_DETAILS))
3427 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3428 (double) prob / REG_BR_PROB_BASE);
3430 account_size_time (info, e->size, add_time, &p);
3433 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3434 offset_map, clause, &toplev_predicate);
3435 remap_hint_predicate (info, callee_info,
3436 &callee_info->loop_iterations,
3437 operand_map, offset_map, clause, &toplev_predicate);
3438 remap_hint_predicate (info, callee_info,
3439 &callee_info->loop_stride,
3440 operand_map, offset_map, clause, &toplev_predicate);
3441 remap_hint_predicate (info, callee_info,
3442 &callee_info->array_index,
3443 operand_map, offset_map, clause, &toplev_predicate);
3445 inline_update_callee_summaries (edge->callee,
3446 inline_edge_summary (edge)->loop_depth);
3448 /* We do not maintain predicates of inlined edges, free it. */
3449 edge_set_predicate (edge, &true_p);
3450 /* Similarly remove param summaries. */
3451 es->param.release ();
3452 operand_map.release ();
3453 offset_map.release ();
3456 /* For performance reasons inline_merge_summary is not updating overall size
3457 and time. Recompute it. */
3459 void
3460 inline_update_overall_summary (struct cgraph_node *node)
3462 struct inline_summary *info = inline_summary (node);
3463 size_time_entry *e;
3464 int i;
3466 info->size = 0;
3467 info->time = 0;
3468 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
3470 info->size += e->size, info->time += e->time;
3471 if (info->time > MAX_TIME * INLINE_TIME_SCALE)
3472 info->time = MAX_TIME * INLINE_TIME_SCALE;
3474 estimate_calls_size_and_time (node, &info->size, &info->time, NULL,
3475 ~(clause_t) (1 << predicate_false_condition),
3476 vNULL, vNULL, vNULL);
3477 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3478 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3481 /* Return hints derrived from EDGE. */
3483 simple_edge_hints (struct cgraph_edge *edge)
3485 int hints = 0;
3486 struct cgraph_node *to = (edge->caller->global.inlined_to
3487 ? edge->caller->global.inlined_to : edge->caller);
3488 if (inline_summary (to)->scc_no
3489 && inline_summary (to)->scc_no == inline_summary (edge->callee)->scc_no
3490 && !cgraph_edge_recursive_p (edge))
3491 hints |= INLINE_HINT_same_scc;
3493 if (to->symbol.lto_file_data && edge->callee->symbol.lto_file_data
3494 && to->symbol.lto_file_data != edge->callee->symbol.lto_file_data)
3495 hints |= INLINE_HINT_cross_module;
3497 return hints;
3500 /* Estimate the time cost for the caller when inlining EDGE.
3501 Only to be called via estimate_edge_time, that handles the
3502 caching mechanism.
3504 When caching, also update the cache entry. Compute both time and
3505 size, since we always need both metrics eventually. */
3508 do_estimate_edge_time (struct cgraph_edge *edge)
3510 int time;
3511 int size;
3512 inline_hints hints;
3513 struct cgraph_node *callee;
3514 clause_t clause;
3515 vec<tree> known_vals;
3516 vec<tree> known_binfos;
3517 vec<ipa_agg_jump_function_p> known_aggs;
3518 struct inline_edge_summary *es = inline_edge_summary (edge);
3520 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3522 gcc_checking_assert (edge->inline_failed);
3523 evaluate_properties_for_edge (edge, true,
3524 &clause, &known_vals, &known_binfos,
3525 &known_aggs);
3526 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3527 known_aggs, &size, &time, &hints, es->param);
3528 known_vals.release ();
3529 known_binfos.release ();
3530 known_aggs.release ();
3531 gcc_checking_assert (size >= 0);
3532 gcc_checking_assert (time >= 0);
3534 /* When caching, update the cache entry. */
3535 if (edge_growth_cache.exists ())
3537 if ((int) edge_growth_cache.length () <= edge->uid)
3538 edge_growth_cache.safe_grow_cleared (cgraph_edge_max_uid);
3539 edge_growth_cache[edge->uid].time = time + (time >= 0);
3541 edge_growth_cache[edge->uid].size = size + (size >= 0);
3542 hints |= simple_edge_hints (edge);
3543 edge_growth_cache[edge->uid].hints = hints + 1;
3545 return time;
3549 /* Return estimated callee growth after inlining EDGE.
3550 Only to be called via estimate_edge_size. */
3553 do_estimate_edge_size (struct cgraph_edge *edge)
3555 int size;
3556 struct cgraph_node *callee;
3557 clause_t clause;
3558 vec<tree> known_vals;
3559 vec<tree> known_binfos;
3560 vec<ipa_agg_jump_function_p> known_aggs;
3562 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3564 if (edge_growth_cache.exists ())
3566 do_estimate_edge_time (edge);
3567 size = edge_growth_cache[edge->uid].size;
3568 gcc_checking_assert (size);
3569 return size - (size > 0);
3572 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3574 /* Early inliner runs without caching, go ahead and do the dirty work. */
3575 gcc_checking_assert (edge->inline_failed);
3576 evaluate_properties_for_edge (edge, true,
3577 &clause, &known_vals, &known_binfos,
3578 &known_aggs);
3579 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3580 known_aggs, &size, NULL, NULL, vNULL);
3581 known_vals.release ();
3582 known_binfos.release ();
3583 known_aggs.release ();
3584 return size;
3588 /* Estimate the growth of the caller when inlining EDGE.
3589 Only to be called via estimate_edge_size. */
3591 inline_hints
3592 do_estimate_edge_hints (struct cgraph_edge *edge)
3594 inline_hints hints;
3595 struct cgraph_node *callee;
3596 clause_t clause;
3597 vec<tree> known_vals;
3598 vec<tree> known_binfos;
3599 vec<ipa_agg_jump_function_p> known_aggs;
3601 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3603 if (edge_growth_cache.exists ())
3605 do_estimate_edge_time (edge);
3606 hints = edge_growth_cache[edge->uid].hints;
3607 gcc_checking_assert (hints);
3608 return hints - 1;
3611 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3613 /* Early inliner runs without caching, go ahead and do the dirty work. */
3614 gcc_checking_assert (edge->inline_failed);
3615 evaluate_properties_for_edge (edge, true,
3616 &clause, &known_vals, &known_binfos,
3617 &known_aggs);
3618 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3619 known_aggs, NULL, NULL, &hints, vNULL);
3620 known_vals.release ();
3621 known_binfos.release ();
3622 known_aggs.release ();
3623 hints |= simple_edge_hints (edge);
3624 return hints;
3628 /* Estimate self time of the function NODE after inlining EDGE. */
3631 estimate_time_after_inlining (struct cgraph_node *node,
3632 struct cgraph_edge *edge)
3634 struct inline_edge_summary *es = inline_edge_summary (edge);
3635 if (!es->predicate || !false_predicate_p (es->predicate))
3637 gcov_type time =
3638 inline_summary (node)->time + estimate_edge_time (edge);
3639 if (time < 0)
3640 time = 0;
3641 if (time > MAX_TIME)
3642 time = MAX_TIME;
3643 return time;
3645 return inline_summary (node)->time;
3649 /* Estimate the size of NODE after inlining EDGE which should be an
3650 edge to either NODE or a call inlined into NODE. */
3653 estimate_size_after_inlining (struct cgraph_node *node,
3654 struct cgraph_edge *edge)
3656 struct inline_edge_summary *es = inline_edge_summary (edge);
3657 if (!es->predicate || !false_predicate_p (es->predicate))
3659 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3660 gcc_assert (size >= 0);
3661 return size;
3663 return inline_summary (node)->size;
3667 struct growth_data
3669 struct cgraph_node *node;
3670 bool self_recursive;
3671 int growth;
3675 /* Worker for do_estimate_growth. Collect growth for all callers. */
3677 static bool
3678 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3680 struct cgraph_edge *e;
3681 struct growth_data *d = (struct growth_data *) data;
3683 for (e = node->callers; e; e = e->next_caller)
3685 gcc_checking_assert (e->inline_failed);
3687 if (e->caller == d->node
3688 || (e->caller->global.inlined_to
3689 && e->caller->global.inlined_to == d->node))
3690 d->self_recursive = true;
3691 d->growth += estimate_edge_growth (e);
3693 return false;
3697 /* Estimate the growth caused by inlining NODE into all callees. */
3700 do_estimate_growth (struct cgraph_node *node)
3702 struct growth_data d = { node, 0, false };
3703 struct inline_summary *info = inline_summary (node);
3705 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3707 /* For self recursive functions the growth estimation really should be
3708 infinity. We don't want to return very large values because the growth
3709 plays various roles in badness computation fractions. Be sure to not
3710 return zero or negative growths. */
3711 if (d.self_recursive)
3712 d.growth = d.growth < info->size ? info->size : d.growth;
3713 else if (DECL_EXTERNAL (node->symbol.decl))
3715 else
3717 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3718 d.growth -= info->size;
3719 /* COMDAT functions are very often not shared across multiple units
3720 since they come from various template instantiations.
3721 Take this into account. */
3722 else if (DECL_COMDAT (node->symbol.decl)
3723 && cgraph_can_remove_if_no_direct_calls_p (node))
3724 d.growth -= (info->size
3725 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3726 + 50) / 100;
3729 if (node_growth_cache.exists ())
3731 if ((int) node_growth_cache.length () <= node->uid)
3732 node_growth_cache.safe_grow_cleared (cgraph_max_uid);
3733 node_growth_cache[node->uid] = d.growth + (d.growth >= 0);
3735 return d.growth;
3739 /* This function performs intraprocedural analysis in NODE that is required to
3740 inline indirect calls. */
3742 static void
3743 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3745 ipa_analyze_node (node);
3746 if (dump_file && (dump_flags & TDF_DETAILS))
3748 ipa_print_node_params (dump_file, node);
3749 ipa_print_node_jump_functions (dump_file, node);
3754 /* Note function body size. */
3756 static void
3757 inline_analyze_function (struct cgraph_node *node)
3759 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
3761 if (dump_file)
3762 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3763 cgraph_node_name (node), node->symbol.order);
3764 if (optimize && !node->thunk.thunk_p)
3765 inline_indirect_intraprocedural_analysis (node);
3766 compute_inline_parameters (node, false);
3767 if (!optimize)
3769 struct cgraph_edge *e;
3770 for (e = node->callees; e; e = e->next_callee)
3772 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3773 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3774 e->call_stmt_cannot_inline_p = true;
3776 for (e = node->indirect_calls; e; e = e->next_callee)
3778 if (e->inline_failed == CIF_FUNCTION_NOT_CONSIDERED)
3779 e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED;
3780 e->call_stmt_cannot_inline_p = true;
3784 pop_cfun ();
3788 /* Called when new function is inserted to callgraph late. */
3790 static void
3791 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3793 inline_analyze_function (node);
3797 /* Note function body size. */
3799 void
3800 inline_generate_summary (void)
3802 struct cgraph_node *node;
3804 /* When not optimizing, do not bother to analyze. Inlining is still done
3805 because edge redirection needs to happen there. */
3806 if (!optimize && !flag_lto && !flag_wpa)
3807 return;
3809 function_insertion_hook_holder =
3810 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3812 ipa_register_cgraph_hooks ();
3813 inline_free_summary ();
3815 FOR_EACH_DEFINED_FUNCTION (node)
3816 if (!node->symbol.alias)
3817 inline_analyze_function (node);
3821 /* Read predicate from IB. */
3823 static struct predicate
3824 read_predicate (struct lto_input_block *ib)
3826 struct predicate out;
3827 clause_t clause;
3828 int k = 0;
3832 gcc_assert (k <= MAX_CLAUSES);
3833 clause = out.clause[k++] = streamer_read_uhwi (ib);
3835 while (clause);
3837 /* Zero-initialize the remaining clauses in OUT. */
3838 while (k <= MAX_CLAUSES)
3839 out.clause[k++] = 0;
3841 return out;
3845 /* Write inline summary for edge E to OB. */
3847 static void
3848 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
3850 struct inline_edge_summary *es = inline_edge_summary (e);
3851 struct predicate p;
3852 int length, i;
3854 es->call_stmt_size = streamer_read_uhwi (ib);
3855 es->call_stmt_time = streamer_read_uhwi (ib);
3856 es->loop_depth = streamer_read_uhwi (ib);
3857 p = read_predicate (ib);
3858 edge_set_predicate (e, &p);
3859 length = streamer_read_uhwi (ib);
3860 if (length)
3862 es->param.safe_grow_cleared (length);
3863 for (i = 0; i < length; i++)
3864 es->param[i].change_prob = streamer_read_uhwi (ib);
3869 /* Stream in inline summaries from the section. */
3871 static void
3872 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3873 size_t len)
3875 const struct lto_function_header *header =
3876 (const struct lto_function_header *) data;
3877 const int cfg_offset = sizeof (struct lto_function_header);
3878 const int main_offset = cfg_offset + header->cfg_size;
3879 const int string_offset = main_offset + header->main_size;
3880 struct data_in *data_in;
3881 struct lto_input_block ib;
3882 unsigned int i, count2, j;
3883 unsigned int f_count;
3885 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
3886 header->main_size);
3888 data_in =
3889 lto_data_in_create (file_data, (const char *) data + string_offset,
3890 header->string_size, vNULL);
3891 f_count = streamer_read_uhwi (&ib);
3892 for (i = 0; i < f_count; i++)
3894 unsigned int index;
3895 struct cgraph_node *node;
3896 struct inline_summary *info;
3897 lto_symtab_encoder_t encoder;
3898 struct bitpack_d bp;
3899 struct cgraph_edge *e;
3900 predicate p;
3902 index = streamer_read_uhwi (&ib);
3903 encoder = file_data->symtab_node_encoder;
3904 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3905 info = inline_summary (node);
3907 info->estimated_stack_size
3908 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
3909 info->size = info->self_size = streamer_read_uhwi (&ib);
3910 info->time = info->self_time = streamer_read_uhwi (&ib);
3912 bp = streamer_read_bitpack (&ib);
3913 info->inlinable = bp_unpack_value (&bp, 1);
3915 count2 = streamer_read_uhwi (&ib);
3916 gcc_assert (!info->conds);
3917 for (j = 0; j < count2; j++)
3919 struct condition c;
3920 c.operand_num = streamer_read_uhwi (&ib);
3921 c.code = (enum tree_code) streamer_read_uhwi (&ib);
3922 c.val = stream_read_tree (&ib, data_in);
3923 bp = streamer_read_bitpack (&ib);
3924 c.agg_contents = bp_unpack_value (&bp, 1);
3925 c.by_ref = bp_unpack_value (&bp, 1);
3926 if (c.agg_contents)
3927 c.offset = streamer_read_uhwi (&ib);
3928 vec_safe_push (info->conds, c);
3930 count2 = streamer_read_uhwi (&ib);
3931 gcc_assert (!info->entry);
3932 for (j = 0; j < count2; j++)
3934 struct size_time_entry e;
3936 e.size = streamer_read_uhwi (&ib);
3937 e.time = streamer_read_uhwi (&ib);
3938 e.predicate = read_predicate (&ib);
3940 vec_safe_push (info->entry, e);
3943 p = read_predicate (&ib);
3944 set_hint_predicate (&info->loop_iterations, p);
3945 p = read_predicate (&ib);
3946 set_hint_predicate (&info->loop_stride, p);
3947 p = read_predicate (&ib);
3948 set_hint_predicate (&info->array_index, p);
3949 for (e = node->callees; e; e = e->next_callee)
3950 read_inline_edge_summary (&ib, e);
3951 for (e = node->indirect_calls; e; e = e->next_callee)
3952 read_inline_edge_summary (&ib, e);
3955 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
3956 len);
3957 lto_data_in_delete (data_in);
3961 /* Read inline summary. Jump functions are shared among ipa-cp
3962 and inliner, so when ipa-cp is active, we don't need to write them
3963 twice. */
3965 void
3966 inline_read_summary (void)
3968 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3969 struct lto_file_decl_data *file_data;
3970 unsigned int j = 0;
3972 inline_summary_alloc ();
3974 while ((file_data = file_data_vec[j++]))
3976 size_t len;
3977 const char *data = lto_get_section_data (file_data,
3978 LTO_section_inline_summary,
3979 NULL, &len);
3980 if (data)
3981 inline_read_section (file_data, data, len);
3982 else
3983 /* Fatal error here. We do not want to support compiling ltrans units
3984 with different version of compiler or different flags than the WPA
3985 unit, so this should never happen. */
3986 fatal_error ("ipa inline summary is missing in input file");
3988 if (optimize)
3990 ipa_register_cgraph_hooks ();
3991 if (!flag_ipa_cp)
3992 ipa_prop_read_jump_functions ();
3994 function_insertion_hook_holder =
3995 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3999 /* Write predicate P to OB. */
4001 static void
4002 write_predicate (struct output_block *ob, struct predicate *p)
4004 int j;
4005 if (p)
4006 for (j = 0; p->clause[j]; j++)
4008 gcc_assert (j < MAX_CLAUSES);
4009 streamer_write_uhwi (ob, p->clause[j]);
4011 streamer_write_uhwi (ob, 0);
4015 /* Write inline summary for edge E to OB. */
4017 static void
4018 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
4020 struct inline_edge_summary *es = inline_edge_summary (e);
4021 int i;
4023 streamer_write_uhwi (ob, es->call_stmt_size);
4024 streamer_write_uhwi (ob, es->call_stmt_time);
4025 streamer_write_uhwi (ob, es->loop_depth);
4026 write_predicate (ob, es->predicate);
4027 streamer_write_uhwi (ob, es->param.length ());
4028 for (i = 0; i < (int) es->param.length (); i++)
4029 streamer_write_uhwi (ob, es->param[i].change_prob);
4033 /* Write inline summary for node in SET.
4034 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
4035 active, we don't need to write them twice. */
4037 void
4038 inline_write_summary (void)
4040 struct cgraph_node *node;
4041 struct output_block *ob = create_output_block (LTO_section_inline_summary);
4042 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
4043 unsigned int count = 0;
4044 int i;
4046 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4048 symtab_node snode = lto_symtab_encoder_deref (encoder, i);
4049 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4050 if (cnode && cnode->symbol.definition && !cnode->symbol.alias)
4051 count++;
4053 streamer_write_uhwi (ob, count);
4055 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
4057 symtab_node snode = lto_symtab_encoder_deref (encoder, i);
4058 cgraph_node *cnode = dyn_cast <cgraph_node> (snode);
4059 if (cnode && (node = cnode)->symbol.definition && !node->symbol.alias)
4061 struct inline_summary *info = inline_summary (node);
4062 struct bitpack_d bp;
4063 struct cgraph_edge *edge;
4064 int i;
4065 size_time_entry *e;
4066 struct condition *c;
4068 streamer_write_uhwi (ob,
4069 lto_symtab_encoder_encode (encoder,
4070 (symtab_node)
4071 node));
4072 streamer_write_hwi (ob, info->estimated_self_stack_size);
4073 streamer_write_hwi (ob, info->self_size);
4074 streamer_write_hwi (ob, info->self_time);
4075 bp = bitpack_create (ob->main_stream);
4076 bp_pack_value (&bp, info->inlinable, 1);
4077 streamer_write_bitpack (&bp);
4078 streamer_write_uhwi (ob, vec_safe_length (info->conds));
4079 for (i = 0; vec_safe_iterate (info->conds, i, &c); i++)
4081 streamer_write_uhwi (ob, c->operand_num);
4082 streamer_write_uhwi (ob, c->code);
4083 stream_write_tree (ob, c->val, true);
4084 bp = bitpack_create (ob->main_stream);
4085 bp_pack_value (&bp, c->agg_contents, 1);
4086 bp_pack_value (&bp, c->by_ref, 1);
4087 streamer_write_bitpack (&bp);
4088 if (c->agg_contents)
4089 streamer_write_uhwi (ob, c->offset);
4091 streamer_write_uhwi (ob, vec_safe_length (info->entry));
4092 for (i = 0; vec_safe_iterate (info->entry, i, &e); i++)
4094 streamer_write_uhwi (ob, e->size);
4095 streamer_write_uhwi (ob, e->time);
4096 write_predicate (ob, &e->predicate);
4098 write_predicate (ob, info->loop_iterations);
4099 write_predicate (ob, info->loop_stride);
4100 write_predicate (ob, info->array_index);
4101 for (edge = node->callees; edge; edge = edge->next_callee)
4102 write_inline_edge_summary (ob, edge);
4103 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
4104 write_inline_edge_summary (ob, edge);
4107 streamer_write_char_stream (ob->main_stream, 0);
4108 produce_asm (ob, NULL);
4109 destroy_output_block (ob);
4111 if (optimize && !flag_ipa_cp)
4112 ipa_prop_write_jump_functions ();
4116 /* Release inline summary. */
4118 void
4119 inline_free_summary (void)
4121 struct cgraph_node *node;
4122 if (!inline_edge_summary_vec.exists ())
4123 return;
4124 FOR_EACH_DEFINED_FUNCTION (node)
4125 reset_inline_summary (node);
4126 if (function_insertion_hook_holder)
4127 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
4128 function_insertion_hook_holder = NULL;
4129 if (node_removal_hook_holder)
4130 cgraph_remove_node_removal_hook (node_removal_hook_holder);
4131 node_removal_hook_holder = NULL;
4132 if (edge_removal_hook_holder)
4133 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
4134 edge_removal_hook_holder = NULL;
4135 if (node_duplication_hook_holder)
4136 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
4137 node_duplication_hook_holder = NULL;
4138 if (edge_duplication_hook_holder)
4139 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
4140 edge_duplication_hook_holder = NULL;
4141 vec_free (inline_summary_vec);
4142 inline_edge_summary_vec.release ();
4143 if (edge_predicate_pool)
4144 free_alloc_pool (edge_predicate_pool);
4145 edge_predicate_pool = 0;