* configure: Regenerated.
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob268f0777cfde0aa5f985d2d0123c175c0f04902b
1 /* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Analysis used by the inliner and other passes limiting code size growth.
24 We estimate for each function
25 - function body size
26 - average function execution time
27 - inlining size benefit (that is how much of function body size
28 and its call sequence is expected to disappear by inlining)
29 - inlining time benefit
30 - function frame size
31 For each call
32 - call statement size and time
34 inlinie_summary datastructures store above information locally (i.e.
35 parameters of the function itself) and globally (i.e. parameters of
36 the function created by applying all the inline decisions already
37 present in the callgraph).
39 We provide accestor to the inline_summary datastructure and
40 basic logic updating the parameters when inlining is performed.
42 The summaries are context sensitive. Context means
43 1) partial assignment of known constant values of operands
44 2) whether function is inlined into the call or not.
45 It is easy to add more variants. To represent function size and time
46 that depends on context (i.e. it is known to be optimized away when
47 context is known either by inlining or from IP-CP and clonning),
48 we use predicates. Predicates are logical formulas in
49 conjunctive-disjunctive form consisting of clauses. Clauses are bitmaps
50 specifying what conditions must be true. Conditions are simple test
51 of the form described above.
53 In order to make predicate (possibly) true, all of its clauses must
54 be (possibly) true. To make clause (possibly) true, one of conditions
55 it mentions must be (possibly) true. There are fixed bounds on
56 number of clauses and conditions and all the manipulation functions
57 are conservative in positive direction. I.e. we may lose precision
58 by thinking that predicate may be true even when it is not.
60 estimate_edge_size and estimate_edge_growth can be used to query
61 function size/time in the given context. inline_merge_summary merges
62 properties of caller and callee after inlining.
64 Finally pass_inline_parameters is exported. This is used to drive
65 computation of function parameters used by the early inliner. IPA
66 inlined performs analysis via its analyze_function method. */
68 #include "config.h"
69 #include "system.h"
70 #include "coretypes.h"
71 #include "tm.h"
72 #include "tree.h"
73 #include "tree-inline.h"
74 #include "langhooks.h"
75 #include "flags.h"
76 #include "cgraph.h"
77 #include "diagnostic.h"
78 #include "gimple-pretty-print.h"
79 #include "params.h"
80 #include "tree-pass.h"
81 #include "coverage.h"
82 #include "ggc.h"
83 #include "tree-flow.h"
84 #include "ipa-prop.h"
85 #include "lto-streamer.h"
86 #include "data-streamer.h"
87 #include "tree-streamer.h"
88 #include "ipa-inline.h"
89 #include "alloc-pool.h"
90 #include "cfgloop.h"
91 #include "cfgloop.h"
92 #include "tree-scalar-evolution.h"
94 /* Estimate runtime of function can easilly run into huge numbers with many
95 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE * 2 in an
96 integer. For anything larger we use gcov_type. */
97 #define MAX_TIME 500000
99 /* Number of bits in integer, but we really want to be stable across different
100 hosts. */
101 #define NUM_CONDITIONS 32
103 enum predicate_conditions
105 predicate_false_condition = 0,
106 predicate_not_inlined_condition = 1,
107 predicate_first_dynamic_condition = 2
110 /* Special condition code we use to represent test that operand is compile time
111 constant. */
112 #define IS_NOT_CONSTANT ERROR_MARK
113 /* Special condition code we use to represent test that operand is not changed
114 across invocation of the function. When operand IS_NOT_CONSTANT it is always
115 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
116 of executions even when they are not compile time constants. */
117 #define CHANGED IDENTIFIER_NODE
119 /* Holders of ipa cgraph hooks: */
120 static struct cgraph_node_hook_list *function_insertion_hook_holder;
121 static struct cgraph_node_hook_list *node_removal_hook_holder;
122 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
123 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
124 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
125 static void inline_node_removal_hook (struct cgraph_node *, void *);
126 static void inline_node_duplication_hook (struct cgraph_node *,
127 struct cgraph_node *, void *);
128 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
129 static void inline_edge_duplication_hook (struct cgraph_edge *,
130 struct cgraph_edge *,
131 void *);
133 /* VECtor holding inline summaries.
134 In GGC memory because conditions might point to constant trees. */
135 VEC(inline_summary_t,gc) *inline_summary_vec;
136 VEC(inline_edge_summary_t,heap) *inline_edge_summary_vec;
138 /* Cached node/edge growths. */
139 VEC(int,heap) *node_growth_cache;
140 VEC(edge_growth_cache_entry,heap) *edge_growth_cache;
142 /* Edge predicates goes here. */
143 static alloc_pool edge_predicate_pool;
145 /* Return true predicate (tautology).
146 We represent it by empty list of clauses. */
148 static inline struct predicate
149 true_predicate (void)
151 struct predicate p;
152 p.clause[0]=0;
153 return p;
157 /* Return predicate testing single condition number COND. */
159 static inline struct predicate
160 single_cond_predicate (int cond)
162 struct predicate p;
163 p.clause[0]=1 << cond;
164 p.clause[1]=0;
165 return p;
169 /* Return false predicate. First clause require false condition. */
171 static inline struct predicate
172 false_predicate (void)
174 return single_cond_predicate (predicate_false_condition);
178 /* Return true if P is (false). */
180 static inline bool
181 true_predicate_p (struct predicate *p)
183 return !p->clause[0];
187 /* Return true if P is (false). */
189 static inline bool
190 false_predicate_p (struct predicate *p)
192 if (p->clause[0] == (1 << predicate_false_condition))
194 gcc_checking_assert (!p->clause[1]
195 && p->clause[0] == 1 << predicate_false_condition);
196 return true;
198 return false;
202 /* Return predicate that is set true when function is not inlined. */
203 static inline struct predicate
204 not_inlined_predicate (void)
206 return single_cond_predicate (predicate_not_inlined_condition);
209 /* Simple description of whether a memory load or a condition refers to a load
210 from an aggregate and if so, how and where from in the aggregate.
211 Individual fields have the same meaning like fields with the same name in
212 struct condition. */
214 struct agg_position_info
216 HOST_WIDE_INT offset;
217 bool agg_contents;
218 bool by_ref;
221 /* Add condition to condition list CONDS. AGGPOS describes whether the used
222 oprand is loaded from an aggregate and where in the aggregate it is. It can
223 be NULL, which means this not a load from an aggregate. */
225 static struct predicate
226 add_condition (struct inline_summary *summary, int operand_num,
227 struct agg_position_info *aggpos,
228 enum tree_code code, tree val)
230 int i;
231 struct condition *c;
232 struct condition new_cond;
233 HOST_WIDE_INT offset;
234 bool agg_contents, by_ref;
236 if (aggpos)
238 offset = aggpos->offset;
239 agg_contents = aggpos->agg_contents;
240 by_ref = aggpos->by_ref;
242 else
244 offset = 0;
245 agg_contents = false;
246 by_ref = false;
249 gcc_checking_assert (operand_num >= 0);
250 for (i = 0; VEC_iterate (condition, summary->conds, i, c); i++)
252 if (c->operand_num == operand_num
253 && c->code == code
254 && c->val == val
255 && c->agg_contents == agg_contents
256 && (!agg_contents || (c->offset == offset && c->by_ref == by_ref)))
257 return single_cond_predicate (i + predicate_first_dynamic_condition);
259 /* Too many conditions. Give up and return constant true. */
260 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
261 return true_predicate ();
263 new_cond.operand_num = operand_num;
264 new_cond.code = code;
265 new_cond.val = val;
266 new_cond.agg_contents = agg_contents;
267 new_cond.by_ref = by_ref;
268 new_cond.offset = offset;
269 VEC_safe_push (condition, gc, summary->conds, new_cond);
270 return single_cond_predicate (i + predicate_first_dynamic_condition);
274 /* Add clause CLAUSE into the predicate P. */
276 static inline void
277 add_clause (conditions conditions, struct predicate *p, clause_t clause)
279 int i;
280 int i2;
281 int insert_here = -1;
282 int c1, c2;
284 /* True clause. */
285 if (!clause)
286 return;
288 /* False clause makes the whole predicate false. Kill the other variants. */
289 if (clause == (1 << predicate_false_condition))
291 p->clause[0] = (1 << predicate_false_condition);
292 p->clause[1] = 0;
293 return;
295 if (false_predicate_p (p))
296 return;
298 /* No one should be sily enough to add false into nontrivial clauses. */
299 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
301 /* Look where to insert the clause. At the same time prune out
302 clauses of P that are implied by the new clause and thus
303 redundant. */
304 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
306 p->clause[i2] = p->clause[i];
308 if (!p->clause[i])
309 break;
311 /* If p->clause[i] implies clause, there is nothing to add. */
312 if ((p->clause[i] & clause) == p->clause[i])
314 /* We had nothing to add, none of clauses should've become
315 redundant. */
316 gcc_checking_assert (i == i2);
317 return;
320 if (p->clause[i] < clause && insert_here < 0)
321 insert_here = i2;
323 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
324 Otherwise the p->clause[i] has to stay. */
325 if ((p->clause[i] & clause) != clause)
326 i2++;
329 /* Look for clauses that are obviously true. I.e.
330 op0 == 5 || op0 != 5. */
331 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
333 condition *cc1;
334 if (!(clause & (1 << c1)))
335 continue;
336 cc1 = &VEC_index (condition,
337 conditions,
338 c1 - predicate_first_dynamic_condition);
339 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
340 and thus there is no point for looking for them. */
341 if (cc1->code == CHANGED
342 || cc1->code == IS_NOT_CONSTANT)
343 continue;
344 for (c2 = c1 + 1; c2 <= NUM_CONDITIONS; c2++)
345 if (clause & (1 << c2))
347 condition *cc1 = &VEC_index (condition,
348 conditions,
349 c1 - predicate_first_dynamic_condition);
350 condition *cc2 = &VEC_index (condition,
351 conditions,
352 c2 - predicate_first_dynamic_condition);
353 if (cc1->operand_num == cc2->operand_num
354 && cc1->val == cc2->val
355 && cc2->code != IS_NOT_CONSTANT
356 && cc2->code != CHANGED
357 && cc1->code == invert_tree_comparison
358 (cc2->code,
359 HONOR_NANS (TYPE_MODE (TREE_TYPE (cc1->val)))))
360 return;
365 /* We run out of variants. Be conservative in positive direction. */
366 if (i2 == MAX_CLAUSES)
367 return;
368 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
369 p->clause[i2 + 1] = 0;
370 if (insert_here >= 0)
371 for (;i2 > insert_here; i2--)
372 p->clause[i2] = p->clause[i2 - 1];
373 else
374 insert_here = i2;
375 p->clause[insert_here] = clause;
379 /* Return P & P2. */
381 static struct predicate
382 and_predicates (conditions conditions,
383 struct predicate *p, struct predicate *p2)
385 struct predicate out = *p;
386 int i;
388 /* Avoid busy work. */
389 if (false_predicate_p (p2) || true_predicate_p (p))
390 return *p2;
391 if (false_predicate_p (p) || true_predicate_p (p2))
392 return *p;
394 /* See how far predicates match. */
395 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
397 gcc_checking_assert (i < MAX_CLAUSES);
400 /* Combine the predicates rest. */
401 for (; p2->clause[i]; i++)
403 gcc_checking_assert (i < MAX_CLAUSES);
404 add_clause (conditions, &out, p2->clause[i]);
406 return out;
410 /* Return true if predicates are obviously equal. */
412 static inline bool
413 predicates_equal_p (struct predicate *p, struct predicate *p2)
415 int i;
416 for (i = 0; p->clause[i]; i++)
418 gcc_checking_assert (i < MAX_CLAUSES);
419 gcc_checking_assert (p->clause [i] > p->clause[i + 1]);
420 gcc_checking_assert (!p2->clause[i]
421 || p2->clause [i] > p2->clause[i + 1]);
422 if (p->clause[i] != p2->clause[i])
423 return false;
425 return !p2->clause[i];
429 /* Return P | P2. */
431 static struct predicate
432 or_predicates (conditions conditions, struct predicate *p, struct predicate *p2)
434 struct predicate out = true_predicate ();
435 int i,j;
437 /* Avoid busy work. */
438 if (false_predicate_p (p2) || true_predicate_p (p))
439 return *p;
440 if (false_predicate_p (p) || true_predicate_p (p2))
441 return *p2;
442 if (predicates_equal_p (p, p2))
443 return *p;
445 /* OK, combine the predicates. */
446 for (i = 0; p->clause[i]; i++)
447 for (j = 0; p2->clause[j]; j++)
449 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
450 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
452 return out;
456 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
457 if predicate P is known to be false. */
459 static bool
460 evaluate_predicate (struct predicate *p, clause_t possible_truths)
462 int i;
464 /* True remains true. */
465 if (true_predicate_p (p))
466 return true;
468 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
470 /* See if we can find clause we can disprove. */
471 for (i = 0; p->clause[i]; i++)
473 gcc_checking_assert (i < MAX_CLAUSES);
474 if (!(p->clause[i] & possible_truths))
475 return false;
477 return true;
480 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
481 instruction will be recomputed per invocation of the inlined call. */
483 static int
484 predicate_probability (conditions conds,
485 struct predicate *p, clause_t possible_truths,
486 VEC (inline_param_summary_t, heap) *inline_param_summary)
488 int i;
489 int combined_prob = REG_BR_PROB_BASE;
491 /* True remains true. */
492 if (true_predicate_p (p))
493 return REG_BR_PROB_BASE;
495 if (false_predicate_p (p))
496 return 0;
498 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
500 /* See if we can find clause we can disprove. */
501 for (i = 0; p->clause[i]; i++)
503 gcc_checking_assert (i < MAX_CLAUSES);
504 if (!(p->clause[i] & possible_truths))
505 return 0;
506 else
508 int this_prob = 0;
509 int i2;
510 if (!inline_param_summary)
511 return REG_BR_PROB_BASE;
512 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
513 if ((p->clause[i] & possible_truths) & (1 << i2))
515 if (i2 >= predicate_first_dynamic_condition)
517 condition *c = &VEC_index
518 (condition, conds,
519 i2 - predicate_first_dynamic_condition);
520 if (c->code == CHANGED
521 && (c->operand_num
522 < (int) VEC_length (inline_param_summary_t,
523 inline_param_summary)))
525 int iprob = VEC_index (inline_param_summary_t,
526 inline_param_summary,
527 c->operand_num).change_prob;
528 this_prob = MAX (this_prob, iprob);
530 else
531 this_prob = REG_BR_PROB_BASE;
533 else
534 this_prob = REG_BR_PROB_BASE;
536 combined_prob = MIN (this_prob, combined_prob);
537 if (!combined_prob)
538 return 0;
541 return combined_prob;
545 /* Dump conditional COND. */
547 static void
548 dump_condition (FILE *f, conditions conditions, int cond)
550 condition *c;
551 if (cond == predicate_false_condition)
552 fprintf (f, "false");
553 else if (cond == predicate_not_inlined_condition)
554 fprintf (f, "not inlined");
555 else
557 c = &VEC_index (condition, conditions,
558 cond - predicate_first_dynamic_condition);
559 fprintf (f, "op%i", c->operand_num);
560 if (c->agg_contents)
561 fprintf (f, "[%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
562 c->by_ref ? "ref " : "", c->offset);
563 if (c->code == IS_NOT_CONSTANT)
565 fprintf (f, " not constant");
566 return;
568 if (c->code == CHANGED)
570 fprintf (f, " changed");
571 return;
573 fprintf (f, " %s ", op_symbol_code (c->code));
574 print_generic_expr (f, c->val, 1);
579 /* Dump clause CLAUSE. */
581 static void
582 dump_clause (FILE *f, conditions conds, clause_t clause)
584 int i;
585 bool found = false;
586 fprintf (f, "(");
587 if (!clause)
588 fprintf (f, "true");
589 for (i = 0; i < NUM_CONDITIONS; i++)
590 if (clause & (1 << i))
592 if (found)
593 fprintf (f, " || ");
594 found = true;
595 dump_condition (f, conds, i);
597 fprintf (f, ")");
601 /* Dump predicate PREDICATE. */
603 static void
604 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
606 int i;
607 if (true_predicate_p (pred))
608 dump_clause (f, conds, 0);
609 else
610 for (i = 0; pred->clause[i]; i++)
612 if (i)
613 fprintf (f, " && ");
614 dump_clause (f, conds, pred->clause[i]);
616 fprintf (f, "\n");
620 /* Dump inline hints. */
621 void
622 dump_inline_hints (FILE *f, inline_hints hints)
624 if (!hints)
625 return;
626 fprintf (f, "inline hints:");
627 if (hints & INLINE_HINT_indirect_call)
629 hints &= ~INLINE_HINT_indirect_call;
630 fprintf (f, " indirect_call");
632 if (hints & INLINE_HINT_loop_iterations)
634 hints &= ~INLINE_HINT_loop_iterations;
635 fprintf (f, " loop_iterations");
637 if (hints & INLINE_HINT_loop_stride)
639 hints &= ~INLINE_HINT_loop_stride;
640 fprintf (f, " loop_stride");
642 gcc_assert (!hints);
646 /* Record SIZE and TIME under condition PRED into the inline summary. */
648 static void
649 account_size_time (struct inline_summary *summary, int size, int time,
650 struct predicate *pred)
652 size_time_entry *e;
653 bool found = false;
654 int i;
656 if (false_predicate_p (pred))
657 return;
659 /* We need to create initial empty unconitional clause, but otherwie
660 we don't need to account empty times and sizes. */
661 if (!size && !time && summary->entry)
662 return;
664 /* Watch overflow that might result from insane profiles. */
665 if (time > MAX_TIME * INLINE_TIME_SCALE)
666 time = MAX_TIME * INLINE_TIME_SCALE;
667 gcc_assert (time >= 0);
669 for (i = 0; VEC_iterate (size_time_entry, summary->entry, i, e); i++)
670 if (predicates_equal_p (&e->predicate, pred))
672 found = true;
673 break;
675 if (i == 32)
677 i = 0;
678 found = true;
679 e = &VEC_index (size_time_entry, summary->entry, 0);
680 gcc_assert (!e->predicate.clause[0]);
682 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
684 fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
685 ((double)size) / INLINE_SIZE_SCALE,
686 ((double)time) / INLINE_TIME_SCALE,
687 found ? "" : "new ");
688 dump_predicate (dump_file, summary->conds, pred);
690 if (!found)
692 struct size_time_entry new_entry;
693 new_entry.size = size;
694 new_entry.time = time;
695 new_entry.predicate = *pred;
696 VEC_safe_push (size_time_entry, gc, summary->entry, new_entry);
698 else
700 e->size += size;
701 e->time += time;
702 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
703 e->time = MAX_TIME * INLINE_TIME_SCALE;
707 /* Set predicate for edge E. */
709 static void
710 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
712 struct inline_edge_summary *es = inline_edge_summary (e);
713 if (predicate && !true_predicate_p (predicate))
715 if (!es->predicate)
716 es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
717 *es->predicate = *predicate;
719 else
721 if (es->predicate)
722 pool_free (edge_predicate_pool, es->predicate);
723 es->predicate = NULL;
727 /* Set predicate for hint *P. */
729 static void
730 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
732 if (false_predicate_p (&new_predicate)
733 || true_predicate_p (&new_predicate))
735 if (*p)
736 pool_free (edge_predicate_pool, *p);
737 *p = NULL;
739 else
741 if (!*p)
742 *p = (struct predicate *)pool_alloc (edge_predicate_pool);
743 **p = new_predicate;
748 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
749 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
750 Return clause of possible truths. When INLINE_P is true, assume that we are
751 inlining.
753 ERROR_MARK means compile time invariant. */
755 static clause_t
756 evaluate_conditions_for_known_args (struct cgraph_node *node,
757 bool inline_p,
758 VEC (tree, heap) *known_vals,
759 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
761 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
762 struct inline_summary *info = inline_summary (node);
763 int i;
764 struct condition *c;
766 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
768 tree val;
769 tree res;
771 /* We allow call stmt to have fewer arguments than the callee function
772 (especially for K&R style programs). So bound check here (we assume
773 known_aggs vector, if non-NULL, has the same length as
774 known_vals). */
775 gcc_checking_assert (!known_aggs
776 || (VEC_length (tree, known_vals)
777 == VEC_length (ipa_agg_jump_function_p,
778 known_aggs)));
779 if (c->operand_num >= (int) VEC_length (tree, known_vals))
781 clause |= 1 << (i + predicate_first_dynamic_condition);
782 continue;
785 if (c->agg_contents)
787 struct ipa_agg_jump_function *agg;
789 if (c->code == CHANGED
790 && !c->by_ref
791 && (VEC_index (tree, known_vals, c->operand_num)
792 == error_mark_node))
793 continue;
795 if (known_aggs)
797 agg = VEC_index (ipa_agg_jump_function_p, known_aggs,
798 c->operand_num);
799 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
801 else
802 val = NULL_TREE;
804 else
806 val = VEC_index (tree, known_vals, c->operand_num);
807 if (val == error_mark_node && c->code != CHANGED)
808 val = NULL_TREE;
811 if (!val)
813 clause |= 1 << (i + predicate_first_dynamic_condition);
814 continue;
816 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
817 continue;
818 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
819 if (res
820 && integer_zerop (res))
821 continue;
822 clause |= 1 << (i + predicate_first_dynamic_condition);
824 return clause;
828 /* Work out what conditions might be true at invocation of E. */
830 static void
831 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
832 clause_t *clause_ptr,
833 VEC (tree, heap) **known_vals_ptr,
834 VEC (tree, heap) **known_binfos_ptr,
835 VEC (ipa_agg_jump_function_p, heap) **known_aggs_ptr)
837 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
838 struct inline_summary *info = inline_summary (callee);
839 VEC (tree, heap) *known_vals = NULL;
840 VEC (ipa_agg_jump_function_p, heap) *known_aggs = NULL;
842 if (clause_ptr)
843 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
844 if (known_vals_ptr)
845 *known_vals_ptr = NULL;
846 if (known_binfos_ptr)
847 *known_binfos_ptr = NULL;
849 if (ipa_node_params_vector
850 && !e->call_stmt_cannot_inline_p
851 && ((clause_ptr && info->conds) || known_vals_ptr || known_binfos_ptr))
853 struct ipa_node_params *parms_info;
854 struct ipa_edge_args *args = IPA_EDGE_REF (e);
855 struct inline_edge_summary *es = inline_edge_summary (e);
856 int i, count = ipa_get_cs_argument_count (args);
858 if (e->caller->global.inlined_to)
859 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
860 else
861 parms_info = IPA_NODE_REF (e->caller);
863 if (count && (info->conds || known_vals_ptr))
864 VEC_safe_grow_cleared (tree, heap, known_vals, count);
865 if (count && (info->conds || known_aggs_ptr))
866 VEC_safe_grow_cleared (ipa_agg_jump_function_p, heap, known_aggs,
867 count);
868 if (count && known_binfos_ptr)
869 VEC_safe_grow_cleared (tree, heap, *known_binfos_ptr, count);
871 for (i = 0; i < count; i++)
873 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
874 tree cst = ipa_value_from_jfunc (parms_info, jf);
875 if (cst)
877 if (known_vals && TREE_CODE (cst) != TREE_BINFO)
878 VEC_replace (tree, known_vals, i, cst);
879 else if (known_binfos_ptr != NULL && TREE_CODE (cst) == TREE_BINFO)
880 VEC_replace (tree, *known_binfos_ptr, i, cst);
882 else if (inline_p
883 && !VEC_index (inline_param_summary_t,
884 es->param,
885 i).change_prob)
886 VEC_replace (tree, known_vals, i, error_mark_node);
887 /* TODO: When IPA-CP starts propagating and merging aggregate jump
888 functions, use its knowledge of the caller too, just like the
889 scalar case above. */
890 VEC_replace (ipa_agg_jump_function_p, known_aggs, i, &jf->agg);
894 if (clause_ptr)
895 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
896 known_vals, known_aggs);
898 if (known_vals_ptr)
899 *known_vals_ptr = known_vals;
900 else
901 VEC_free (tree, heap, known_vals);
903 if (known_aggs_ptr)
904 *known_aggs_ptr = known_aggs;
905 else
906 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
910 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
912 static void
913 inline_summary_alloc (void)
915 if (!node_removal_hook_holder)
916 node_removal_hook_holder =
917 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
918 if (!edge_removal_hook_holder)
919 edge_removal_hook_holder =
920 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
921 if (!node_duplication_hook_holder)
922 node_duplication_hook_holder =
923 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
924 if (!edge_duplication_hook_holder)
925 edge_duplication_hook_holder =
926 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
928 if (VEC_length (inline_summary_t, inline_summary_vec)
929 <= (unsigned) cgraph_max_uid)
930 VEC_safe_grow_cleared (inline_summary_t, gc,
931 inline_summary_vec, cgraph_max_uid + 1);
932 if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
933 <= (unsigned) cgraph_edge_max_uid)
934 VEC_safe_grow_cleared (inline_edge_summary_t, heap,
935 inline_edge_summary_vec, cgraph_edge_max_uid + 1);
936 if (!edge_predicate_pool)
937 edge_predicate_pool = create_alloc_pool ("edge predicates",
938 sizeof (struct predicate),
939 10);
942 /* We are called multiple time for given function; clear
943 data from previous run so they are not cumulated. */
945 static void
946 reset_inline_edge_summary (struct cgraph_edge *e)
948 if (e->uid
949 < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
951 struct inline_edge_summary *es = inline_edge_summary (e);
953 es->call_stmt_size = es->call_stmt_time =0;
954 if (es->predicate)
955 pool_free (edge_predicate_pool, es->predicate);
956 es->predicate = NULL;
957 VEC_free (inline_param_summary_t, heap, es->param);
961 /* We are called multiple time for given function; clear
962 data from previous run so they are not cumulated. */
964 static void
965 reset_inline_summary (struct cgraph_node *node)
967 struct inline_summary *info = inline_summary (node);
968 struct cgraph_edge *e;
970 info->self_size = info->self_time = 0;
971 info->estimated_stack_size = 0;
972 info->estimated_self_stack_size = 0;
973 info->stack_frame_offset = 0;
974 info->size = 0;
975 info->time = 0;
976 if (info->loop_iterations)
978 pool_free (edge_predicate_pool, info->loop_iterations);
979 info->loop_iterations = NULL;
981 if (info->loop_stride)
983 pool_free (edge_predicate_pool, info->loop_stride);
984 info->loop_stride = NULL;
986 VEC_free (condition, gc, info->conds);
987 VEC_free (size_time_entry,gc, info->entry);
988 for (e = node->callees; e; e = e->next_callee)
989 reset_inline_edge_summary (e);
990 for (e = node->indirect_calls; e; e = e->next_callee)
991 reset_inline_edge_summary (e);
994 /* Hook that is called by cgraph.c when a node is removed. */
996 static void
997 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
999 struct inline_summary *info;
1000 if (VEC_length (inline_summary_t, inline_summary_vec)
1001 <= (unsigned)node->uid)
1002 return;
1003 info = inline_summary (node);
1004 reset_inline_summary (node);
1005 memset (info, 0, sizeof (inline_summary_t));
1008 /* Remap predicate P of former function to be predicate of duplicated functoin.
1009 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1010 INFO is inline summary of the duplicated node. */
1012 static struct predicate
1013 remap_predicate_after_duplication (struct predicate *p,
1014 clause_t possible_truths,
1015 struct inline_summary *info)
1017 struct predicate new_predicate = true_predicate ();
1018 int j;
1019 for (j = 0; p->clause[j]; j++)
1020 if (!(possible_truths & p->clause[j]))
1022 new_predicate = false_predicate ();
1023 break;
1025 else
1026 add_clause (info->conds, &new_predicate,
1027 possible_truths & p->clause[j]);
1028 return new_predicate;
1031 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1032 Additionally care about allocating new memory slot for updated predicate
1033 and set it to NULL when it becomes true or false (and thus uninteresting).
1036 static void
1037 remap_hint_predicate_after_duplication (struct predicate **p,
1038 clause_t possible_truths,
1039 struct inline_summary *info)
1041 struct predicate new_predicate;
1043 if (!*p)
1044 return;
1046 new_predicate = remap_predicate_after_duplication (*p,
1047 possible_truths,
1048 info);
1049 /* We do not want to free previous predicate; it is used by node origin. */
1050 *p = NULL;
1051 set_hint_predicate (p, new_predicate);
1055 /* Hook that is called by cgraph.c when a node is duplicated. */
1057 static void
1058 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1059 ATTRIBUTE_UNUSED void *data)
1061 struct inline_summary *info;
1062 inline_summary_alloc ();
1063 info = inline_summary (dst);
1064 memcpy (info, inline_summary (src),
1065 sizeof (struct inline_summary));
1066 /* TODO: as an optimization, we may avoid copying conditions
1067 that are known to be false or true. */
1068 info->conds = VEC_copy (condition, gc, info->conds);
1070 /* When there are any replacements in the function body, see if we can figure
1071 out that something was optimized out. */
1072 if (ipa_node_params_vector && dst->clone.tree_map)
1074 VEC(size_time_entry,gc) *entry = info->entry;
1075 /* Use SRC parm info since it may not be copied yet. */
1076 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1077 VEC (tree, heap) *known_vals = NULL;
1078 int count = ipa_get_param_count (parms_info);
1079 int i,j;
1080 clause_t possible_truths;
1081 struct predicate true_pred = true_predicate ();
1082 size_time_entry *e;
1083 int optimized_out_size = 0;
1084 gcov_type optimized_out_time = 0;
1085 bool inlined_to_p = false;
1086 struct cgraph_edge *edge;
1088 info->entry = 0;
1089 VEC_safe_grow_cleared (tree, heap, known_vals, count);
1090 for (i = 0; i < count; i++)
1092 tree t = ipa_get_param (parms_info, i);
1093 struct ipa_replace_map *r;
1095 for (j = 0;
1096 VEC_iterate (ipa_replace_map_p, dst->clone.tree_map, j, r);
1097 j++)
1099 if (r->old_tree == t
1100 && r->replace_p
1101 && !r->ref_p)
1103 VEC_replace (tree, known_vals, i, r->new_tree);
1104 break;
1108 possible_truths = evaluate_conditions_for_known_args (dst, false,
1109 known_vals, NULL);
1110 VEC_free (tree, heap, known_vals);
1112 account_size_time (info, 0, 0, &true_pred);
1114 /* Remap size_time vectors.
1115 Simplify the predicate by prunning out alternatives that are known
1116 to be false.
1117 TODO: as on optimization, we can also eliminate conditions known
1118 to be true. */
1119 for (i = 0; VEC_iterate (size_time_entry, entry, i, e); i++)
1121 struct predicate new_predicate;
1122 new_predicate = remap_predicate_after_duplication (&e->predicate,
1123 possible_truths,
1124 info);
1125 if (false_predicate_p (&new_predicate))
1127 optimized_out_size += e->size;
1128 optimized_out_time += e->time;
1130 else
1131 account_size_time (info, e->size, e->time, &new_predicate);
1134 /* Remap edge predicates with the same simplification as above.
1135 Also copy constantness arrays. */
1136 for (edge = dst->callees; edge; edge = edge->next_callee)
1138 struct predicate new_predicate;
1139 struct inline_edge_summary *es = inline_edge_summary (edge);
1141 if (!edge->inline_failed)
1142 inlined_to_p = true;
1143 if (!es->predicate)
1144 continue;
1145 new_predicate = remap_predicate_after_duplication (es->predicate,
1146 possible_truths,
1147 info);
1148 if (false_predicate_p (&new_predicate)
1149 && !false_predicate_p (es->predicate))
1151 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1152 optimized_out_time += (es->call_stmt_time
1153 * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
1154 * edge->frequency);
1155 edge->frequency = 0;
1157 edge_set_predicate (edge, &new_predicate);
1160 /* Remap indirect edge predicates with the same simplificaiton as above.
1161 Also copy constantness arrays. */
1162 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1164 struct predicate new_predicate;
1165 struct inline_edge_summary *es = inline_edge_summary (edge);
1167 gcc_checking_assert (edge->inline_failed);
1168 if (!es->predicate)
1169 continue;
1170 new_predicate = remap_predicate_after_duplication (es->predicate,
1171 possible_truths,
1172 info);
1173 if (false_predicate_p (&new_predicate)
1174 && !false_predicate_p (es->predicate))
1176 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1177 optimized_out_time += (es->call_stmt_time
1178 * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
1179 * edge->frequency);
1180 edge->frequency = 0;
1182 edge_set_predicate (edge, &new_predicate);
1184 remap_hint_predicate_after_duplication (&info->loop_iterations,
1185 possible_truths,
1186 info);
1187 remap_hint_predicate_after_duplication (&info->loop_stride,
1188 possible_truths,
1189 info);
1191 /* If inliner or someone after inliner will ever start producing
1192 non-trivial clones, we will get trouble with lack of information
1193 about updating self sizes, because size vectors already contains
1194 sizes of the calees. */
1195 gcc_assert (!inlined_to_p
1196 || (!optimized_out_size && !optimized_out_time));
1198 info->size -= optimized_out_size / INLINE_SIZE_SCALE;
1199 info->self_size -= optimized_out_size / INLINE_SIZE_SCALE;
1200 gcc_assert (info->size > 0);
1201 gcc_assert (info->self_size > 0);
1203 optimized_out_time /= INLINE_TIME_SCALE;
1204 if (optimized_out_time > MAX_TIME)
1205 optimized_out_time = MAX_TIME;
1206 info->time -= optimized_out_time;
1207 info->self_time -= optimized_out_time;
1208 if (info->time < 0)
1209 info->time = 0;
1210 if (info->self_time < 0)
1211 info->self_time = 0;
1213 else
1215 info->entry = VEC_copy (size_time_entry, gc, info->entry);
1216 if (info->loop_iterations)
1218 predicate p = *info->loop_iterations;
1219 info->loop_iterations = NULL;
1220 set_hint_predicate (&info->loop_iterations, p);
1222 if (info->loop_stride)
1224 predicate p = *info->loop_stride;
1225 info->loop_stride = NULL;
1226 set_hint_predicate (&info->loop_stride, p);
1232 /* Hook that is called by cgraph.c when a node is duplicated. */
1234 static void
1235 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1236 ATTRIBUTE_UNUSED void *data)
1238 struct inline_edge_summary *info;
1239 struct inline_edge_summary *srcinfo;
1240 inline_summary_alloc ();
1241 info = inline_edge_summary (dst);
1242 srcinfo = inline_edge_summary (src);
1243 memcpy (info, srcinfo,
1244 sizeof (struct inline_edge_summary));
1245 info->predicate = NULL;
1246 edge_set_predicate (dst, srcinfo->predicate);
1247 info->param = VEC_copy (inline_param_summary_t, heap, srcinfo->param);
1251 /* Keep edge cache consistent across edge removal. */
1253 static void
1254 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
1256 if (edge_growth_cache)
1257 reset_edge_growth_cache (edge);
1258 reset_inline_edge_summary (edge);
1262 /* Initialize growth caches. */
1264 void
1265 initialize_growth_caches (void)
1267 if (cgraph_edge_max_uid)
1268 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
1269 cgraph_edge_max_uid);
1270 if (cgraph_max_uid)
1271 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
1275 /* Free growth caches. */
1277 void
1278 free_growth_caches (void)
1280 VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
1281 edge_growth_cache = 0;
1282 VEC_free (int, heap, node_growth_cache);
1283 node_growth_cache = 0;
1287 /* Dump edge summaries associated to NODE and recursively to all clones.
1288 Indent by INDENT. */
1290 static void
1291 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
1292 struct inline_summary *info)
1294 struct cgraph_edge *edge;
1295 for (edge = node->callees; edge; edge = edge->next_callee)
1297 struct inline_edge_summary *es = inline_edge_summary (edge);
1298 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1299 int i;
1301 fprintf (f, "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i time: %2i callee size:%2i stack:%2i",
1302 indent, "", cgraph_node_name (callee),
1303 callee->uid,
1304 !edge->inline_failed ? "inlined"
1305 : cgraph_inline_failed_string (edge->inline_failed),
1306 indent, "",
1307 es->loop_depth,
1308 edge->frequency,
1309 es->call_stmt_size,
1310 es->call_stmt_time,
1311 (int)inline_summary (callee)->size / INLINE_SIZE_SCALE,
1312 (int)inline_summary (callee)->estimated_stack_size);
1314 if (es->predicate)
1316 fprintf (f, " predicate: ");
1317 dump_predicate (f, info->conds, es->predicate);
1319 else
1320 fprintf (f, "\n");
1321 if (es->param)
1322 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param);
1323 i++)
1325 int prob = VEC_index (inline_param_summary_t,
1326 es->param, i).change_prob;
1328 if (!prob)
1329 fprintf (f, "%*s op%i is compile time invariant\n",
1330 indent + 2, "", i);
1331 else if (prob != REG_BR_PROB_BASE)
1332 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1333 prob * 100.0 / REG_BR_PROB_BASE);
1335 if (!edge->inline_failed)
1337 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1338 " callee size %i\n",
1339 indent+2, "",
1340 (int)inline_summary (callee)->stack_frame_offset,
1341 (int)inline_summary (callee)->estimated_self_stack_size,
1342 (int)inline_summary (callee)->estimated_stack_size);
1343 dump_inline_edge_summary (f, indent+2, callee, info);
1346 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1348 struct inline_edge_summary *es = inline_edge_summary (edge);
1349 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1350 " time: %2i",
1351 indent, "",
1352 es->loop_depth,
1353 edge->frequency,
1354 es->call_stmt_size,
1355 es->call_stmt_time);
1356 if (es->predicate)
1358 fprintf (f, "predicate: ");
1359 dump_predicate (f, info->conds, es->predicate);
1361 else
1362 fprintf (f, "\n");
1367 void
1368 dump_inline_summary (FILE * f, struct cgraph_node *node)
1370 if (node->analyzed)
1372 struct inline_summary *s = inline_summary (node);
1373 size_time_entry *e;
1374 int i;
1375 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
1376 node->uid);
1377 if (DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl))
1378 fprintf (f, " always_inline");
1379 if (s->inlinable)
1380 fprintf (f, " inlinable");
1381 fprintf (f, "\n self time: %i\n",
1382 s->self_time);
1383 fprintf (f, " global time: %i\n", s->time);
1384 fprintf (f, " self size: %i\n",
1385 s->self_size);
1386 fprintf (f, " global size: %i\n", s->size);
1387 fprintf (f, " self stack: %i\n",
1388 (int) s->estimated_self_stack_size);
1389 fprintf (f, " global stack: %i\n",
1390 (int) s->estimated_stack_size);
1391 for (i = 0;
1392 VEC_iterate (size_time_entry, s->entry, i, e);
1393 i++)
1395 fprintf (f, " size:%f, time:%f, predicate:",
1396 (double) e->size / INLINE_SIZE_SCALE,
1397 (double) e->time / INLINE_TIME_SCALE);
1398 dump_predicate (f, s->conds, &e->predicate);
1400 if (s->loop_iterations)
1402 fprintf (f, " loop iterations:");
1403 dump_predicate (f, s->conds, s->loop_iterations);
1405 if (s->loop_stride)
1407 fprintf (f, " loop stride:");
1408 dump_predicate (f, s->conds, s->loop_stride);
1410 fprintf (f, " calls:\n");
1411 dump_inline_edge_summary (f, 4, node, s);
1412 fprintf (f, "\n");
1416 DEBUG_FUNCTION void
1417 debug_inline_summary (struct cgraph_node *node)
1419 dump_inline_summary (stderr, node);
1422 void
1423 dump_inline_summaries (FILE *f)
1425 struct cgraph_node *node;
1427 FOR_EACH_DEFINED_FUNCTION (node)
1428 if (!node->global.inlined_to)
1429 dump_inline_summary (f, node);
1432 /* Give initial reasons why inlining would fail on EDGE. This gets either
1433 nullified or usually overwritten by more precise reasons later. */
1435 void
1436 initialize_inline_failed (struct cgraph_edge *e)
1438 struct cgraph_node *callee = e->callee;
1440 if (e->indirect_unknown_callee)
1441 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1442 else if (!callee->analyzed)
1443 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1444 else if (callee->local.redefined_extern_inline)
1445 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1446 else if (e->call_stmt_cannot_inline_p)
1447 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1448 else
1449 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1452 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1453 boolean variable pointed to by DATA. */
1455 static bool
1456 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1457 void *data)
1459 bool *b = (bool *) data;
1460 *b = true;
1461 return true;
1464 /* If OP refers to value of function parameter, return the corresponding
1465 parameter. */
1467 static tree
1468 unmodified_parm_1 (gimple stmt, tree op)
1470 /* SSA_NAME referring to parm default def? */
1471 if (TREE_CODE (op) == SSA_NAME
1472 && SSA_NAME_IS_DEFAULT_DEF (op)
1473 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1474 return SSA_NAME_VAR (op);
1475 /* Non-SSA parm reference? */
1476 if (TREE_CODE (op) == PARM_DECL)
1478 bool modified = false;
1480 ao_ref refd;
1481 ao_ref_init (&refd, op);
1482 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1483 NULL);
1484 if (!modified)
1485 return op;
1487 return NULL_TREE;
1490 /* If OP refers to value of function parameter, return the corresponding
1491 parameter. Also traverse chains of SSA register assignments. */
1493 static tree
1494 unmodified_parm (gimple stmt, tree op)
1496 tree res = unmodified_parm_1 (stmt, op);
1497 if (res)
1498 return res;
1500 if (TREE_CODE (op) == SSA_NAME
1501 && !SSA_NAME_IS_DEFAULT_DEF (op)
1502 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1503 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1504 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1505 return NULL_TREE;
1508 /* If OP refers to a value of a function parameter or value loaded from an
1509 aggregate passed to a parameter (either by value or reference), return TRUE
1510 and store the number of the parameter to *INDEX_P and information whether
1511 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1512 the function parameters, STMT is the statement in which OP is used or
1513 loaded. */
1515 static bool
1516 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1517 gimple stmt, tree op, int *index_p,
1518 struct agg_position_info *aggpos)
1520 tree res = unmodified_parm_1 (stmt, op);
1522 gcc_checking_assert (aggpos);
1523 if (res)
1525 *index_p = ipa_get_param_decl_index (info, res);
1526 if (*index_p < 0)
1527 return false;
1528 aggpos->agg_contents = false;
1529 aggpos->by_ref = false;
1530 return true;
1533 if (TREE_CODE (op) == SSA_NAME)
1535 if (SSA_NAME_IS_DEFAULT_DEF (op)
1536 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1537 return false;
1538 stmt = SSA_NAME_DEF_STMT (op);
1539 op = gimple_assign_rhs1 (stmt);
1540 if (!REFERENCE_CLASS_P (op))
1541 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1542 aggpos);
1545 aggpos->agg_contents = true;
1546 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1547 &aggpos->by_ref);
1550 /* See if statement might disappear after inlining.
1551 0 - means not eliminated
1552 1 - half of statements goes away
1553 2 - for sure it is eliminated.
1554 We are not terribly sophisticated, basically looking for simple abstraction
1555 penalty wrappers. */
1557 static int
1558 eliminated_by_inlining_prob (gimple stmt)
1560 enum gimple_code code = gimple_code (stmt);
1562 if (!optimize)
1563 return 0;
1565 switch (code)
1567 case GIMPLE_RETURN:
1568 return 2;
1569 case GIMPLE_ASSIGN:
1570 if (gimple_num_ops (stmt) != 2)
1571 return 0;
1573 /* Casts of parameters, loads from parameters passed by reference
1574 and stores to return value or parameters are often free after
1575 inlining dua to SRA and further combining.
1576 Assume that half of statements goes away. */
1577 if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1578 || gimple_assign_rhs_code (stmt) == NOP_EXPR
1579 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1580 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1582 tree rhs = gimple_assign_rhs1 (stmt);
1583 tree lhs = gimple_assign_lhs (stmt);
1584 tree inner_rhs = get_base_address (rhs);
1585 tree inner_lhs = get_base_address (lhs);
1586 bool rhs_free = false;
1587 bool lhs_free = false;
1589 if (!inner_rhs)
1590 inner_rhs = rhs;
1591 if (!inner_lhs)
1592 inner_lhs = lhs;
1594 /* Reads of parameter are expected to be free. */
1595 if (unmodified_parm (stmt, inner_rhs))
1596 rhs_free = true;
1598 /* When parameter is not SSA register because its address is taken
1599 and it is just copied into one, the statement will be completely
1600 free after inlining (we will copy propagate backward). */
1601 if (rhs_free && is_gimple_reg (lhs))
1602 return 2;
1604 /* Reads of parameters passed by reference
1605 expected to be free (i.e. optimized out after inlining). */
1606 if (TREE_CODE(inner_rhs) == MEM_REF
1607 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1608 rhs_free = true;
1610 /* Copying parameter passed by reference into gimple register is
1611 probably also going to copy propagate, but we can't be quite
1612 sure. */
1613 if (rhs_free && is_gimple_reg (lhs))
1614 lhs_free = true;
1616 /* Writes to parameters, parameters passed by value and return value
1617 (either dirrectly or passed via invisible reference) are free.
1619 TODO: We ought to handle testcase like
1620 struct a {int a,b;};
1621 struct a
1622 retrurnsturct (void)
1624 struct a a ={1,2};
1625 return a;
1628 This translate into:
1630 retrurnsturct ()
1632 int a$b;
1633 int a$a;
1634 struct a a;
1635 struct a D.2739;
1637 <bb 2>:
1638 D.2739.a = 1;
1639 D.2739.b = 2;
1640 return D.2739;
1643 For that we either need to copy ipa-split logic detecting writes
1644 to return value. */
1645 if (TREE_CODE (inner_lhs) == PARM_DECL
1646 || TREE_CODE (inner_lhs) == RESULT_DECL
1647 || (TREE_CODE(inner_lhs) == MEM_REF
1648 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1649 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1650 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1651 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1652 (inner_lhs, 0))) == RESULT_DECL))))
1653 lhs_free = true;
1654 if (lhs_free
1655 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1656 rhs_free = true;
1657 if (lhs_free && rhs_free)
1658 return 1;
1660 return 0;
1661 default:
1662 return 0;
1667 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1668 predicates to the CFG edges. */
1670 static void
1671 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1672 struct inline_summary *summary,
1673 basic_block bb)
1675 gimple last;
1676 tree op;
1677 int index;
1678 struct agg_position_info aggpos;
1679 enum tree_code code, inverted_code;
1680 edge e;
1681 edge_iterator ei;
1682 gimple set_stmt;
1683 tree op2;
1685 last = last_stmt (bb);
1686 if (!last
1687 || gimple_code (last) != GIMPLE_COND)
1688 return;
1689 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1690 return;
1691 op = gimple_cond_lhs (last);
1692 /* TODO: handle conditionals like
1693 var = op0 < 4;
1694 if (var != 0). */
1695 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1697 code = gimple_cond_code (last);
1698 inverted_code
1699 = invert_tree_comparison (code,
1700 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1702 FOR_EACH_EDGE (e, ei, bb->succs)
1704 struct predicate p = add_condition (summary, index, &aggpos,
1705 e->flags & EDGE_TRUE_VALUE
1706 ? code : inverted_code,
1707 gimple_cond_rhs (last));
1708 e->aux = pool_alloc (edge_predicate_pool);
1709 *(struct predicate *)e->aux = p;
1713 if (TREE_CODE (op) != SSA_NAME)
1714 return;
1715 /* Special case
1716 if (builtin_constant_p (op))
1717 constant_code
1718 else
1719 nonconstant_code.
1720 Here we can predicate nonconstant_code. We can't
1721 really handle constant_code since we have no predicate
1722 for this and also the constant code is not known to be
1723 optimized away when inliner doen't see operand is constant.
1724 Other optimizers might think otherwise. */
1725 if (gimple_cond_code (last) != NE_EXPR
1726 || !integer_zerop (gimple_cond_rhs (last)))
1727 return;
1728 set_stmt = SSA_NAME_DEF_STMT (op);
1729 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1730 || gimple_call_num_args (set_stmt) != 1)
1731 return;
1732 op2 = gimple_call_arg (set_stmt, 0);
1733 if (!unmodified_parm_or_parm_agg_item (info, set_stmt, op2, &index, &aggpos))
1734 return;
1735 FOR_EACH_EDGE (e, ei, bb->succs)
1736 if (e->flags & EDGE_FALSE_VALUE)
1738 struct predicate p = add_condition (summary, index, &aggpos,
1739 IS_NOT_CONSTANT, NULL_TREE);
1740 e->aux = pool_alloc (edge_predicate_pool);
1741 *(struct predicate *)e->aux = p;
1746 /* If BB ends by a switch we can turn into predicates, attach corresponding
1747 predicates to the CFG edges. */
1749 static void
1750 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1751 struct inline_summary *summary,
1752 basic_block bb)
1754 gimple last;
1755 tree op;
1756 int index;
1757 struct agg_position_info aggpos;
1758 edge e;
1759 edge_iterator ei;
1760 size_t n;
1761 size_t case_idx;
1763 last = last_stmt (bb);
1764 if (!last
1765 || gimple_code (last) != GIMPLE_SWITCH)
1766 return;
1767 op = gimple_switch_index (last);
1768 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1769 return;
1771 FOR_EACH_EDGE (e, ei, bb->succs)
1773 e->aux = pool_alloc (edge_predicate_pool);
1774 *(struct predicate *)e->aux = false_predicate ();
1776 n = gimple_switch_num_labels(last);
1777 for (case_idx = 0; case_idx < n; ++case_idx)
1779 tree cl = gimple_switch_label (last, case_idx);
1780 tree min, max;
1781 struct predicate p;
1783 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1784 min = CASE_LOW (cl);
1785 max = CASE_HIGH (cl);
1787 /* For default we might want to construct predicate that none
1788 of cases is met, but it is bit hard to do not having negations
1789 of conditionals handy. */
1790 if (!min && !max)
1791 p = true_predicate ();
1792 else if (!max)
1793 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1794 else
1796 struct predicate p1, p2;
1797 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1798 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1799 p = and_predicates (summary->conds, &p1, &p2);
1801 *(struct predicate *)e->aux
1802 = or_predicates (summary->conds, &p, (struct predicate *)e->aux);
1807 /* For each BB in NODE attach to its AUX pointer predicate under
1808 which it is executable. */
1810 static void
1811 compute_bb_predicates (struct cgraph_node *node,
1812 struct ipa_node_params *parms_info,
1813 struct inline_summary *summary)
1815 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
1816 bool done = false;
1817 basic_block bb;
1819 FOR_EACH_BB_FN (bb, my_function)
1821 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1822 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1825 /* Entry block is always executable. */
1826 ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1827 = pool_alloc (edge_predicate_pool);
1828 *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1829 = true_predicate ();
1831 /* A simple dataflow propagation of predicates forward in the CFG.
1832 TODO: work in reverse postorder. */
1833 while (!done)
1835 done = true;
1836 FOR_EACH_BB_FN (bb, my_function)
1838 struct predicate p = false_predicate ();
1839 edge e;
1840 edge_iterator ei;
1841 FOR_EACH_EDGE (e, ei, bb->preds)
1843 if (e->src->aux)
1845 struct predicate this_bb_predicate
1846 = *(struct predicate *)e->src->aux;
1847 if (e->aux)
1848 this_bb_predicate
1849 = and_predicates (summary->conds, &this_bb_predicate,
1850 (struct predicate *)e->aux);
1851 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1852 if (true_predicate_p (&p))
1853 break;
1856 if (false_predicate_p (&p))
1857 gcc_assert (!bb->aux);
1858 else
1860 if (!bb->aux)
1862 done = false;
1863 bb->aux = pool_alloc (edge_predicate_pool);
1864 *((struct predicate *)bb->aux) = p;
1866 else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1868 done = false;
1869 *((struct predicate *)bb->aux) = p;
1877 /* We keep info about constantness of SSA names. */
1879 typedef struct predicate predicate_t;
1880 DEF_VEC_O (predicate_t);
1881 DEF_VEC_ALLOC_O (predicate_t, heap);
1882 /* Return predicate specifying when the STMT might have result that is not
1883 a compile time constant. */
1885 static struct predicate
1886 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1887 struct inline_summary *summary,
1888 tree expr,
1889 VEC (predicate_t, heap) *nonconstant_names)
1891 tree parm;
1892 int index;
1894 while (UNARY_CLASS_P (expr))
1895 expr = TREE_OPERAND (expr, 0);
1897 parm = unmodified_parm (NULL, expr);
1898 if (parm
1899 && (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 VEC_index (predicate_t, nonconstant_names,
1905 SSA_NAME_VERSION (expr));
1906 if (BINARY_CLASS_P (expr)
1907 || COMPARISON_CLASS_P (expr))
1909 struct predicate p1 = will_be_nonconstant_expr_predicate
1910 (info, summary, TREE_OPERAND (expr, 0),
1911 nonconstant_names);
1912 struct predicate p2;
1913 if (true_predicate_p (&p1))
1914 return p1;
1915 p2 = will_be_nonconstant_expr_predicate (info, summary,
1916 TREE_OPERAND (expr, 1),
1917 nonconstant_names);
1918 return or_predicates (summary->conds, &p1, &p2);
1920 else if (TREE_CODE (expr) == COND_EXPR)
1922 struct predicate p1 = will_be_nonconstant_expr_predicate
1923 (info, summary, TREE_OPERAND (expr, 0),
1924 nonconstant_names);
1925 struct predicate p2;
1926 if (true_predicate_p (&p1))
1927 return p1;
1928 p2 = will_be_nonconstant_expr_predicate (info, summary,
1929 TREE_OPERAND (expr, 1),
1930 nonconstant_names);
1931 if (true_predicate_p (&p2))
1932 return p2;
1933 p1 = or_predicates (summary->conds, &p1, &p2);
1934 p2 = will_be_nonconstant_expr_predicate (info, summary,
1935 TREE_OPERAND (expr, 2),
1936 nonconstant_names);
1937 return or_predicates (summary->conds, &p1, &p2);
1939 else
1941 debug_tree (expr);
1942 gcc_unreachable ();
1944 return false_predicate ();
1948 /* Return predicate specifying when the STMT might have result that is not
1949 a compile time constant. */
1951 static struct predicate
1952 will_be_nonconstant_predicate (struct ipa_node_params *info,
1953 struct inline_summary *summary,
1954 gimple stmt,
1955 VEC (predicate_t, heap) *nonconstant_names)
1957 struct predicate p = true_predicate ();
1958 ssa_op_iter iter;
1959 tree use;
1960 struct predicate op_non_const;
1961 bool is_load;
1962 int base_index;
1963 struct agg_position_info aggpos;
1965 /* What statments might be optimized away
1966 when their arguments are constant
1967 TODO: also trivial builtins.
1968 builtin_constant_p is already handled later. */
1969 if (gimple_code (stmt) != GIMPLE_ASSIGN
1970 && gimple_code (stmt) != GIMPLE_COND
1971 && gimple_code (stmt) != GIMPLE_SWITCH)
1972 return p;
1974 /* Stores will stay anyway. */
1975 if (gimple_vdef (stmt))
1976 return p;
1978 is_load = gimple_vuse (stmt) != NULL;
1979 /* Loads can be optimized when the value is known. */
1980 if (is_load)
1982 tree op;
1983 gcc_assert (gimple_assign_single_p (stmt));
1984 op = gimple_assign_rhs1 (stmt);
1985 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
1986 &aggpos))
1987 return p;
1989 else
1990 base_index = -1;
1992 /* See if we understand all operands before we start
1993 adding conditionals. */
1994 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1996 tree parm = unmodified_parm (stmt, use);
1997 /* For arguments we can build a condition. */
1998 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
1999 continue;
2000 if (TREE_CODE (use) != SSA_NAME)
2001 return p;
2002 /* If we know when operand is constant,
2003 we still can say something useful. */
2004 if (!true_predicate_p (&VEC_index (predicate_t, nonconstant_names,
2005 SSA_NAME_VERSION (use))))
2006 continue;
2007 return p;
2010 if (is_load)
2011 op_non_const = add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2012 else
2013 op_non_const = false_predicate ();
2014 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2016 tree parm = unmodified_parm (stmt, use);
2017 int index;
2019 if (parm
2020 && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2022 if (index != base_index)
2023 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2024 else
2025 continue;
2027 else
2028 p = VEC_index (predicate_t, nonconstant_names,
2029 SSA_NAME_VERSION (use));
2030 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2032 if (gimple_code (stmt) == GIMPLE_ASSIGN
2033 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2034 VEC_replace (predicate_t, nonconstant_names,
2035 SSA_NAME_VERSION (gimple_assign_lhs (stmt)), op_non_const);
2036 return op_non_const;
2039 struct record_modified_bb_info
2041 bitmap bb_set;
2042 gimple stmt;
2045 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2046 set except for info->stmt. */
2048 static bool
2049 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef,
2050 void *data)
2052 struct record_modified_bb_info *info = (struct record_modified_bb_info *) data;
2053 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2054 return false;
2055 bitmap_set_bit (info->bb_set,
2056 SSA_NAME_IS_DEFAULT_DEF (vdef)
2057 ? ENTRY_BLOCK_PTR->index : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2058 return false;
2061 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2062 will change since last invocation of STMT.
2064 Value 0 is reserved for compile time invariants.
2065 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2066 ought to be REG_BR_PROB_BASE / estimated_iters. */
2068 static int
2069 param_change_prob (gimple stmt, int i)
2071 tree op = gimple_call_arg (stmt, i);
2072 basic_block bb = gimple_bb (stmt);
2073 tree base;
2075 if (is_gimple_min_invariant (op))
2076 return 0;
2077 /* We would have to do non-trivial analysis to really work out what
2078 is the probability of value to change (i.e. when init statement
2079 is in a sibling loop of the call).
2081 We do an conservative estimate: when call is executed N times more often
2082 than the statement defining value, we take the frequency 1/N. */
2083 if (TREE_CODE (op) == SSA_NAME)
2085 int init_freq;
2087 if (!bb->frequency)
2088 return REG_BR_PROB_BASE;
2090 if (SSA_NAME_IS_DEFAULT_DEF (op))
2091 init_freq = ENTRY_BLOCK_PTR->frequency;
2092 else
2093 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2095 if (!init_freq)
2096 init_freq = 1;
2097 if (init_freq < bb->frequency)
2098 return MAX ((init_freq * REG_BR_PROB_BASE +
2099 bb->frequency / 2) / bb->frequency, 1);
2100 else
2101 return REG_BR_PROB_BASE;
2104 base = get_base_address (op);
2105 if (base)
2107 ao_ref refd;
2108 int max;
2109 struct record_modified_bb_info info;
2110 bitmap_iterator bi;
2111 unsigned index;
2113 if (const_value_known_p (base))
2114 return 0;
2115 if (!bb->frequency)
2116 return REG_BR_PROB_BASE;
2117 ao_ref_init (&refd, op);
2118 info.stmt = stmt;
2119 info.bb_set = BITMAP_ALLOC (NULL);
2120 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2121 NULL);
2122 if (bitmap_bit_p (info.bb_set, bb->index))
2124 BITMAP_FREE (info.bb_set);
2125 return REG_BR_PROB_BASE;
2128 /* Assume that every memory is initialized at entry.
2129 TODO: Can we easilly determine if value is always defined
2130 and thus we may skip entry block? */
2131 if (ENTRY_BLOCK_PTR->frequency)
2132 max = ENTRY_BLOCK_PTR->frequency;
2133 else
2134 max = 1;
2136 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2137 max = MIN (max, BASIC_BLOCK (index)->frequency);
2139 BITMAP_FREE (info.bb_set);
2140 if (max < bb->frequency)
2141 return MAX ((max * REG_BR_PROB_BASE +
2142 bb->frequency / 2) / bb->frequency, 1);
2143 else
2144 return REG_BR_PROB_BASE;
2146 return REG_BR_PROB_BASE;
2149 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2150 sub-graph and if the predicate the condition depends on is known. If so,
2151 return true and store the pointer the predicate in *P. */
2153 static bool
2154 phi_result_unknown_predicate (struct ipa_node_params *info,
2155 struct inline_summary *summary, basic_block bb,
2156 struct predicate *p,
2157 VEC (predicate_t, heap) *nonconstant_names)
2159 edge e;
2160 edge_iterator ei;
2161 basic_block first_bb = NULL;
2162 gimple stmt;
2164 if (single_pred_p (bb))
2166 *p = false_predicate ();
2167 return true;
2170 FOR_EACH_EDGE (e, ei, bb->preds)
2172 if (single_succ_p (e->src))
2174 if (!single_pred_p (e->src))
2175 return false;
2176 if (!first_bb)
2177 first_bb = single_pred (e->src);
2178 else if (single_pred (e->src) != first_bb)
2179 return false;
2181 else
2183 if (!first_bb)
2184 first_bb = e->src;
2185 else if (e->src != first_bb)
2186 return false;
2190 if (!first_bb)
2191 return false;
2193 stmt = last_stmt (first_bb);
2194 if (!stmt
2195 || gimple_code (stmt) != GIMPLE_COND
2196 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2197 return false;
2199 *p = will_be_nonconstant_expr_predicate (info, summary,
2200 gimple_cond_lhs (stmt),
2201 nonconstant_names);
2202 if (true_predicate_p (p))
2203 return false;
2204 else
2205 return true;
2208 /* Given a PHI statement in a function described by inline properties SUMMARY
2209 and *P being the predicate describing whether the selected PHI argument is
2210 known, store a predicate for the result of the PHI statement into
2211 NONCONSTANT_NAMES, if possible. */
2213 static void
2214 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2215 struct predicate *p,
2216 VEC (predicate_t, heap) *nonconstant_names)
2218 unsigned i;
2220 for (i = 0; i < gimple_phi_num_args (phi); i++)
2222 tree arg = gimple_phi_arg (phi, i)->def;
2223 if (!is_gimple_min_invariant (arg))
2225 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2226 *p = or_predicates (summary->conds, p,
2227 &VEC_index (predicate_t, nonconstant_names,
2228 SSA_NAME_VERSION (arg)));
2229 if (true_predicate_p (p))
2230 return;
2234 if (dump_file && (dump_flags & TDF_DETAILS))
2236 fprintf (dump_file, "\t\tphi predicate: ");
2237 dump_predicate (dump_file, summary->conds, p);
2239 VEC_replace (predicate_t, nonconstant_names,
2240 SSA_NAME_VERSION (gimple_phi_result (phi)), *p);
2243 /* Compute function body size parameters for NODE.
2244 When EARLY is true, we compute only simple summaries without
2245 non-trivial predicates to drive the early inliner. */
2247 static void
2248 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2250 gcov_type time = 0;
2251 /* Estimate static overhead for function prologue/epilogue and alignment. */
2252 int size = 2;
2253 /* Benefits are scaled by probability of elimination that is in range
2254 <0,2>. */
2255 basic_block bb;
2256 gimple_stmt_iterator bsi;
2257 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
2258 int freq;
2259 struct inline_summary *info = inline_summary (node);
2260 struct predicate bb_predicate;
2261 struct ipa_node_params *parms_info = NULL;
2262 VEC (predicate_t, heap) *nonconstant_names = NULL;
2264 info->conds = 0;
2265 info->entry = 0;
2267 if (optimize && !early)
2269 calculate_dominance_info (CDI_DOMINATORS);
2270 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2272 if (ipa_node_params_vector)
2274 parms_info = IPA_NODE_REF (node);
2275 VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
2276 VEC_length (tree, SSANAMES (my_function)));
2280 if (dump_file)
2281 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2282 cgraph_node_name (node));
2284 /* When we run into maximal number of entries, we assign everything to the
2285 constant truth case. Be sure to have it in list. */
2286 bb_predicate = true_predicate ();
2287 account_size_time (info, 0, 0, &bb_predicate);
2289 bb_predicate = not_inlined_predicate ();
2290 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2292 gcc_assert (my_function && my_function->cfg);
2293 if (parms_info)
2294 compute_bb_predicates (node, parms_info, info);
2295 FOR_EACH_BB_FN (bb, my_function)
2297 freq = compute_call_stmt_bb_frequency (node->symbol.decl, bb);
2299 /* TODO: Obviously predicates can be propagated down across CFG. */
2300 if (parms_info)
2302 if (bb->aux)
2303 bb_predicate = *(struct predicate *)bb->aux;
2304 else
2305 bb_predicate = false_predicate ();
2307 else
2308 bb_predicate = true_predicate ();
2310 if (dump_file && (dump_flags & TDF_DETAILS))
2312 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2313 dump_predicate (dump_file, info->conds, &bb_predicate);
2316 if (parms_info && nonconstant_names)
2318 struct predicate phi_predicate;
2319 bool first_phi = true;
2321 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2323 if (first_phi
2324 && !phi_result_unknown_predicate (parms_info, info, bb,
2325 &phi_predicate,
2326 nonconstant_names))
2327 break;
2328 first_phi = false;
2329 if (dump_file && (dump_flags & TDF_DETAILS))
2331 fprintf (dump_file, " ");
2332 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2334 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2335 nonconstant_names);
2339 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2341 gimple stmt = gsi_stmt (bsi);
2342 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2343 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2344 int prob;
2345 struct predicate will_be_nonconstant;
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2349 fprintf (dump_file, " ");
2350 print_gimple_stmt (dump_file, stmt, 0, 0);
2351 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2352 ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
2355 if (is_gimple_call (stmt))
2357 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2358 struct inline_edge_summary *es = inline_edge_summary (edge);
2360 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2361 resolved as constant. We however don't want to optimize
2362 out the cgraph edges. */
2363 if (nonconstant_names
2364 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2365 && gimple_call_lhs (stmt)
2366 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2368 struct predicate false_p = false_predicate ();
2369 VEC_replace (predicate_t, nonconstant_names,
2370 SSA_NAME_VERSION (gimple_call_lhs (stmt)),
2371 false_p);
2373 if (ipa_node_params_vector)
2375 int count = gimple_call_num_args (stmt);
2376 int i;
2378 if (count)
2379 VEC_safe_grow_cleared (inline_param_summary_t, heap,
2380 es->param, count);
2381 for (i = 0; i < count; i++)
2383 int prob = param_change_prob (stmt, i);
2384 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2385 VEC_index (inline_param_summary_t,
2386 es->param, i).change_prob = prob;
2390 es->call_stmt_size = this_size;
2391 es->call_stmt_time = this_time;
2392 es->loop_depth = bb_loop_depth (bb);
2393 edge_set_predicate (edge, &bb_predicate);
2396 /* TODO: When conditional jump or swithc is known to be constant, but
2397 we did not translate it into the predicates, we really can account
2398 just maximum of the possible paths. */
2399 if (parms_info)
2400 will_be_nonconstant
2401 = will_be_nonconstant_predicate (parms_info, info,
2402 stmt, nonconstant_names);
2403 if (this_time || this_size)
2405 struct predicate p;
2407 this_time *= freq;
2408 time += this_time;
2409 size += this_size;
2411 prob = eliminated_by_inlining_prob (stmt);
2412 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2413 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
2414 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2415 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2417 if (parms_info)
2418 p = and_predicates (info->conds, &bb_predicate,
2419 &will_be_nonconstant);
2420 else
2421 p = true_predicate ();
2423 /* We account everything but the calls. Calls have their own
2424 size/time info attached to cgraph edges. This is necessary
2425 in order to make the cost disappear after inlining. */
2426 if (!is_gimple_call (stmt))
2428 if (prob)
2430 struct predicate ip = not_inlined_predicate ();
2431 ip = and_predicates (info->conds, &ip, &p);
2432 account_size_time (info, this_size * prob,
2433 this_time * prob, &ip);
2435 if (prob != 2)
2436 account_size_time (info, this_size * (2 - prob),
2437 this_time * (2 - prob), &p);
2440 gcc_assert (time >= 0);
2441 gcc_assert (size >= 0);
2445 FOR_ALL_BB_FN (bb, my_function)
2447 edge e;
2448 edge_iterator ei;
2450 if (bb->aux)
2451 pool_free (edge_predicate_pool, bb->aux);
2452 bb->aux = NULL;
2453 FOR_EACH_EDGE (e, ei, bb->succs)
2455 if (e->aux)
2456 pool_free (edge_predicate_pool, e->aux);
2457 e->aux = NULL;
2460 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2461 if (time > MAX_TIME)
2462 time = MAX_TIME;
2464 if (!early && nonconstant_names)
2466 struct loop *loop;
2467 loop_iterator li;
2468 predicate loop_iterations = true_predicate ();
2469 predicate loop_stride = true_predicate ();
2471 if (dump_file && (dump_flags & TDF_DETAILS))
2472 flow_loops_dump (dump_file, NULL, 0);
2473 scev_initialize ();
2474 FOR_EACH_LOOP (li, loop, 0)
2476 VEC (edge, heap) *exits;
2477 edge ex;
2478 unsigned int j, i;
2479 struct tree_niter_desc niter_desc;
2480 basic_block *body = get_loop_body (loop);
2482 exits = get_loop_exit_edges (loop);
2483 FOR_EACH_VEC_ELT (edge, exits, j, ex)
2484 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2485 && !is_gimple_min_invariant (niter_desc.niter))
2487 predicate will_be_nonconstant
2488 = will_be_nonconstant_expr_predicate (parms_info, info,
2489 niter_desc.niter, nonconstant_names);
2490 if (!true_predicate_p (&will_be_nonconstant)
2491 && !false_predicate_p (&will_be_nonconstant))
2492 /* This is slightly inprecise. We may want to represent each loop with
2493 independent predicate. */
2494 loop_iterations = and_predicates (info->conds, &loop_iterations, &will_be_nonconstant);
2496 VEC_free (edge, heap, exits);
2498 for (i = 0; i < loop->num_nodes; i++)
2500 gimple_stmt_iterator gsi;
2501 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
2503 gimple stmt = gsi_stmt (gsi);
2504 affine_iv iv;
2505 ssa_op_iter iter;
2506 tree use;
2508 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2510 predicate will_be_nonconstant;
2512 if (!simple_iv (loop, loop_containing_stmt (stmt), use, &iv, true)
2513 || is_gimple_min_invariant (iv.step))
2514 continue;
2515 will_be_nonconstant
2516 = will_be_nonconstant_expr_predicate (parms_info, info,
2517 iv.step, nonconstant_names);
2518 if (!true_predicate_p (&will_be_nonconstant)
2519 && !false_predicate_p (&will_be_nonconstant))
2520 /* This is slightly inprecise. We may want to represent each loop with
2521 independent predicate. */
2522 loop_stride = and_predicates (info->conds, &loop_stride, &will_be_nonconstant);
2526 free (body);
2528 set_hint_predicate (&inline_summary (node)->loop_iterations, loop_iterations);
2529 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2530 scev_finalize ();
2532 inline_summary (node)->self_time = time;
2533 inline_summary (node)->self_size = size;
2534 VEC_free (predicate_t, heap, nonconstant_names);
2535 if (optimize && !early)
2537 loop_optimizer_finalize ();
2538 free_dominance_info (CDI_DOMINATORS);
2540 if (dump_file)
2542 fprintf (dump_file, "\n");
2543 dump_inline_summary (dump_file, node);
2548 /* Compute parameters of functions used by inliner.
2549 EARLY is true when we compute parameters for the early inliner */
2551 void
2552 compute_inline_parameters (struct cgraph_node *node, bool early)
2554 HOST_WIDE_INT self_stack_size;
2555 struct cgraph_edge *e;
2556 struct inline_summary *info;
2557 tree old_decl = current_function_decl;
2559 gcc_assert (!node->global.inlined_to);
2561 inline_summary_alloc ();
2563 info = inline_summary (node);
2564 reset_inline_summary (node);
2566 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2567 Once this happen, we will need to more curefully predict call
2568 statement size. */
2569 if (node->thunk.thunk_p)
2571 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2572 struct predicate t = true_predicate ();
2574 info->inlinable = 0;
2575 node->callees->call_stmt_cannot_inline_p = true;
2576 node->local.can_change_signature = false;
2577 es->call_stmt_time = 1;
2578 es->call_stmt_size = 1;
2579 account_size_time (info, 0, 0, &t);
2580 return;
2583 /* Even is_gimple_min_invariant rely on current_function_decl. */
2584 current_function_decl = node->symbol.decl;
2585 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2587 /* Estimate the stack size for the function if we're optimizing. */
2588 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2589 info->estimated_self_stack_size = self_stack_size;
2590 info->estimated_stack_size = self_stack_size;
2591 info->stack_frame_offset = 0;
2593 /* Can this function be inlined at all? */
2594 info->inlinable = tree_inlinable_function_p (node->symbol.decl);
2596 /* Type attributes can use parameter indices to describe them. */
2597 if (TYPE_ATTRIBUTES (TREE_TYPE (node->symbol.decl)))
2598 node->local.can_change_signature = false;
2599 else
2601 /* Otherwise, inlinable functions always can change signature. */
2602 if (info->inlinable)
2603 node->local.can_change_signature = true;
2604 else
2606 /* Functions calling builtin_apply can not change signature. */
2607 for (e = node->callees; e; e = e->next_callee)
2609 tree cdecl = e->callee->symbol.decl;
2610 if (DECL_BUILT_IN (cdecl)
2611 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2612 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2613 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2614 break;
2616 node->local.can_change_signature = !e;
2619 estimate_function_body_sizes (node, early);
2621 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2622 info->time = info->self_time;
2623 info->size = info->self_size;
2624 info->stack_frame_offset = 0;
2625 info->estimated_stack_size = info->estimated_self_stack_size;
2626 current_function_decl = old_decl;
2627 pop_cfun ();
2631 /* Compute parameters of functions used by inliner using
2632 current_function_decl. */
2634 static unsigned int
2635 compute_inline_parameters_for_current (void)
2637 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2638 return 0;
2641 struct gimple_opt_pass pass_inline_parameters =
2644 GIMPLE_PASS,
2645 "inline_param", /* name */
2646 NULL, /* gate */
2647 compute_inline_parameters_for_current,/* execute */
2648 NULL, /* sub */
2649 NULL, /* next */
2650 0, /* static_pass_number */
2651 TV_INLINE_PARAMETERS, /* tv_id */
2652 0, /* properties_required */
2653 0, /* properties_provided */
2654 0, /* properties_destroyed */
2655 0, /* todo_flags_start */
2656 0 /* todo_flags_finish */
2661 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2663 static void
2664 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2665 int prob)
2667 struct inline_edge_summary *es = inline_edge_summary (e);
2668 *size += es->call_stmt_size * INLINE_SIZE_SCALE;
2669 *time += (es->call_stmt_time * prob / REG_BR_PROB_BASE
2670 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
2671 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2672 *time = MAX_TIME * INLINE_TIME_SCALE;
2676 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2677 KNOWN_BINFOS. */
2679 static bool
2680 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2681 int *size, int *time, int prob,
2682 VEC (tree, heap) *known_vals,
2683 VEC (tree, heap) *known_binfos,
2684 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
2686 tree target;
2687 int time_diff, size_diff;
2688 struct cgraph_node *callee;
2689 struct inline_summary *isummary;
2691 if (!known_vals && !known_binfos)
2692 return false;
2694 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2695 known_aggs);
2696 if (!target)
2697 return false;
2699 /* Account for difference in cost between indirect and direct calls. */
2700 size_diff = ((eni_size_weights.indirect_call_cost - eni_size_weights.call_cost)
2701 * INLINE_SIZE_SCALE);
2702 *size -= size_diff;
2703 time_diff = ((eni_time_weights.indirect_call_cost - eni_time_weights.call_cost)
2704 * INLINE_TIME_SCALE * prob / REG_BR_PROB_BASE);
2705 *time -= time_diff;
2707 callee = cgraph_get_node (target);
2708 if (!callee || !callee->analyzed)
2709 return false;
2710 isummary = inline_summary (callee);
2711 return isummary->inlinable;
2715 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.
2716 POSSIBLE_TRUTHS, KNOWN_VALS and KNOWN_BINFOS describe context of the call
2717 site. */
2719 static void
2720 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
2721 inline_hints *hints,
2722 clause_t possible_truths,
2723 VEC (tree, heap) *known_vals,
2724 VEC (tree, heap) *known_binfos,
2725 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
2727 struct cgraph_edge *e;
2728 for (e = node->callees; e; e = e->next_callee)
2730 struct inline_edge_summary *es = inline_edge_summary (e);
2731 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2733 if (e->inline_failed)
2735 /* Predicates of calls shall not use NOT_CHANGED codes,
2736 sowe do not need to compute probabilities. */
2737 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE);
2739 else
2740 estimate_calls_size_and_time (e->callee, size, time, hints,
2741 possible_truths,
2742 known_vals, known_binfos, known_aggs);
2745 for (e = node->indirect_calls; e; e = e->next_callee)
2747 struct inline_edge_summary *es = inline_edge_summary (e);
2748 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2750 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE);
2751 if (estimate_edge_devirt_benefit (e, size, time, REG_BR_PROB_BASE,
2752 known_vals, known_binfos, known_aggs)
2753 && hints
2754 && cgraph_maybe_hot_edge_p (e))
2755 *hints |= INLINE_HINT_indirect_call;
2761 /* Estimate size and time needed to execute NODE assuming
2762 POSSIBLE_TRUTHS clause, and KNOWN_VALS and KNOWN_BINFOS information
2763 about NODE's arguments. */
2765 static void
2766 estimate_node_size_and_time (struct cgraph_node *node,
2767 clause_t possible_truths,
2768 VEC (tree, heap) *known_vals,
2769 VEC (tree, heap) *known_binfos,
2770 VEC (ipa_agg_jump_function_p, heap) *known_aggs,
2771 int *ret_size, int *ret_time,
2772 inline_hints *ret_hints,
2773 VEC (inline_param_summary_t, heap)
2774 *inline_param_summary)
2776 struct inline_summary *info = inline_summary (node);
2777 size_time_entry *e;
2778 int size = 0, time = 0;
2779 inline_hints hints = 0;
2780 int i;
2782 if (dump_file
2783 && (dump_flags & TDF_DETAILS))
2785 bool found = false;
2786 fprintf (dump_file, " Estimating body: %s/%i\n"
2787 " Known to be false: ",
2788 cgraph_node_name (node),
2789 node->uid);
2791 for (i = predicate_not_inlined_condition;
2792 i < (predicate_first_dynamic_condition
2793 + (int)VEC_length (condition, info->conds)); i++)
2794 if (!(possible_truths & (1 << i)))
2796 if (found)
2797 fprintf (dump_file, ", ");
2798 found = true;
2799 dump_condition (dump_file, info->conds, i);
2803 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2804 if (evaluate_predicate (&e->predicate, possible_truths))
2806 size += e->size;
2807 if (!inline_param_summary)
2808 time += e->time;
2809 else
2811 int prob = predicate_probability (info->conds,
2812 &e->predicate,
2813 possible_truths,
2814 inline_param_summary);
2815 time += e->time * prob / REG_BR_PROB_BASE;
2820 if (info->loop_iterations
2821 && !evaluate_predicate (info->loop_iterations, possible_truths))
2822 hints |=INLINE_HINT_loop_iterations;
2823 if (info->loop_stride
2824 && !evaluate_predicate (info->loop_stride, possible_truths))
2825 hints |=INLINE_HINT_loop_stride;
2827 if (time > MAX_TIME * INLINE_TIME_SCALE)
2828 time = MAX_TIME * INLINE_TIME_SCALE;
2830 estimate_calls_size_and_time (node, &size, &time, &hints, possible_truths,
2831 known_vals, known_binfos, known_aggs);
2832 time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
2833 size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
2836 if (dump_file
2837 && (dump_flags & TDF_DETAILS))
2838 fprintf (dump_file, "\n size:%i time:%i\n", size, time);
2839 if (ret_time)
2840 *ret_time = time;
2841 if (ret_size)
2842 *ret_size = size;
2843 if (ret_hints)
2844 *ret_hints = hints;
2845 return;
2849 /* Estimate size and time needed to execute callee of EDGE assuming that
2850 parameters known to be constant at caller of EDGE are propagated.
2851 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
2852 and types for parameters. */
2854 void
2855 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
2856 VEC (tree, heap) *known_vals,
2857 VEC (tree, heap) *known_binfos,
2858 int *ret_size, int *ret_time)
2860 clause_t clause;
2862 clause = evaluate_conditions_for_known_args (node, false, known_vals, NULL);
2863 estimate_node_size_and_time (node, clause, known_vals, known_binfos, NULL,
2864 ret_size, ret_time, NULL,
2865 NULL);
2868 /* Translate all conditions from callee representation into caller
2869 representation and symbolically evaluate predicate P into new predicate.
2871 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
2872 is summary of function predicate P is from. OPERAND_MAP is array giving
2873 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
2874 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
2875 predicate under which callee is executed. OFFSET_MAP is an array of of
2876 offsets that need to be added to conditions, negative offset means that
2877 conditions relying on values passed by reference have to be discarded
2878 because they might not be preserved (and should be considered offset zero
2879 for other purposes). */
2881 static struct predicate
2882 remap_predicate (struct inline_summary *info,
2883 struct inline_summary *callee_info,
2884 struct predicate *p,
2885 VEC (int, heap) *operand_map,
2886 VEC (int, heap) *offset_map,
2887 clause_t possible_truths,
2888 struct predicate *toplev_predicate)
2890 int i;
2891 struct predicate out = true_predicate ();
2893 /* True predicate is easy. */
2894 if (true_predicate_p (p))
2895 return *toplev_predicate;
2896 for (i = 0; p->clause[i]; i++)
2898 clause_t clause = p->clause[i];
2899 int cond;
2900 struct predicate clause_predicate = false_predicate ();
2902 gcc_assert (i < MAX_CLAUSES);
2904 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
2905 /* Do we have condition we can't disprove? */
2906 if (clause & possible_truths & (1 << cond))
2908 struct predicate cond_predicate;
2909 /* Work out if the condition can translate to predicate in the
2910 inlined function. */
2911 if (cond >= predicate_first_dynamic_condition)
2913 struct condition *c;
2915 c = &VEC_index (condition, callee_info->conds,
2916 cond - predicate_first_dynamic_condition);
2917 /* See if we can remap condition operand to caller's operand.
2918 Otherwise give up. */
2919 if (!operand_map
2920 || (int)VEC_length (int, operand_map) <= c->operand_num
2921 || VEC_index (int, operand_map, c->operand_num) == -1
2922 /* TODO: For non-aggregate conditions, adding an offset is
2923 basically an arithmetic jump function processing which
2924 we should support in future. */
2925 || ((!c->agg_contents || !c->by_ref)
2926 && VEC_index (int, offset_map, c->operand_num) > 0)
2927 || (c->agg_contents && c->by_ref
2928 && VEC_index (int, offset_map, c->operand_num) < 0))
2929 cond_predicate = true_predicate ();
2930 else
2932 struct agg_position_info ap;
2933 HOST_WIDE_INT offset_delta = VEC_index (int, offset_map,
2934 c->operand_num);
2935 if (offset_delta < 0)
2937 gcc_checking_assert (!c->agg_contents || !c->by_ref);
2938 offset_delta = 0;
2940 gcc_assert (!c->agg_contents
2941 || c->by_ref
2942 || offset_delta == 0);
2943 ap.offset = c->offset + offset_delta;
2944 ap.agg_contents = c->agg_contents;
2945 ap.by_ref = c->by_ref;
2946 cond_predicate = add_condition (info,
2947 VEC_index (int,
2948 operand_map,
2949 c->operand_num),
2950 &ap, c->code, c->val);
2953 /* Fixed conditions remains same, construct single
2954 condition predicate. */
2955 else
2957 cond_predicate.clause[0] = 1 << cond;
2958 cond_predicate.clause[1] = 0;
2960 clause_predicate = or_predicates (info->conds, &clause_predicate,
2961 &cond_predicate);
2963 out = and_predicates (info->conds, &out, &clause_predicate);
2965 return and_predicates (info->conds, &out, toplev_predicate);
2969 /* Update summary information of inline clones after inlining.
2970 Compute peak stack usage. */
2972 static void
2973 inline_update_callee_summaries (struct cgraph_node *node,
2974 int depth)
2976 struct cgraph_edge *e;
2977 struct inline_summary *callee_info = inline_summary (node);
2978 struct inline_summary *caller_info = inline_summary (node->callers->caller);
2979 HOST_WIDE_INT peak;
2981 callee_info->stack_frame_offset
2982 = caller_info->stack_frame_offset
2983 + caller_info->estimated_self_stack_size;
2984 peak = callee_info->stack_frame_offset
2985 + callee_info->estimated_self_stack_size;
2986 if (inline_summary (node->global.inlined_to)->estimated_stack_size
2987 < peak)
2988 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
2989 cgraph_propagate_frequency (node);
2990 for (e = node->callees; e; e = e->next_callee)
2992 if (!e->inline_failed)
2993 inline_update_callee_summaries (e->callee, depth);
2994 inline_edge_summary (e)->loop_depth += depth;
2996 for (e = node->indirect_calls; e; e = e->next_callee)
2997 inline_edge_summary (e)->loop_depth += depth;
3000 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3001 When functoin A is inlined in B and A calls C with parameter that
3002 changes with probability PROB1 and C is known to be passthroug
3003 of argument if B that change with probability PROB2, the probability
3004 of change is now PROB1*PROB2. */
3006 static void
3007 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3008 struct cgraph_edge *edge)
3010 if (ipa_node_params_vector)
3012 int i;
3013 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3014 struct inline_edge_summary *es = inline_edge_summary (edge);
3015 struct inline_edge_summary *inlined_es
3016 = inline_edge_summary (inlined_edge);
3018 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3020 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3021 if (jfunc->type == IPA_JF_PASS_THROUGH
3022 && (ipa_get_jf_pass_through_formal_id (jfunc)
3023 < (int) VEC_length (inline_param_summary_t,
3024 inlined_es->param)))
3026 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3027 int prob1 = VEC_index (inline_param_summary_t,
3028 es->param, i).change_prob;
3029 int prob2 = VEC_index
3030 (inline_param_summary_t,
3031 inlined_es->param, jf_formal_id).change_prob;
3032 int prob = ((prob1 * prob2 + REG_BR_PROB_BASE / 2)
3033 / REG_BR_PROB_BASE);
3035 if (prob1 && prob2 && !prob)
3036 prob = 1;
3038 VEC_index (inline_param_summary_t,
3039 es->param, i).change_prob = prob;
3045 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3047 Remap predicates of callees of NODE. Rest of arguments match
3048 remap_predicate.
3050 Also update change probabilities. */
3052 static void
3053 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3054 struct cgraph_node *node,
3055 struct inline_summary *info,
3056 struct inline_summary *callee_info,
3057 VEC (int, heap) *operand_map,
3058 VEC (int, heap) *offset_map,
3059 clause_t possible_truths,
3060 struct predicate *toplev_predicate)
3062 struct cgraph_edge *e;
3063 for (e = node->callees; e; e = e->next_callee)
3065 struct inline_edge_summary *es = inline_edge_summary (e);
3066 struct predicate p;
3068 if (e->inline_failed)
3070 remap_edge_change_prob (inlined_edge, e);
3072 if (es->predicate)
3074 p = remap_predicate (info, callee_info,
3075 es->predicate, operand_map, offset_map,
3076 possible_truths,
3077 toplev_predicate);
3078 edge_set_predicate (e, &p);
3079 /* TODO: We should remove the edge for code that will be
3080 optimized out, but we need to keep verifiers and tree-inline
3081 happy. Make it cold for now. */
3082 if (false_predicate_p (&p))
3084 e->count = 0;
3085 e->frequency = 0;
3088 else
3089 edge_set_predicate (e, toplev_predicate);
3091 else
3092 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3093 operand_map, offset_map, possible_truths,
3094 toplev_predicate);
3096 for (e = node->indirect_calls; e; e = e->next_callee)
3098 struct inline_edge_summary *es = inline_edge_summary (e);
3099 struct predicate p;
3101 remap_edge_change_prob (inlined_edge, e);
3102 if (es->predicate)
3104 p = remap_predicate (info, callee_info,
3105 es->predicate, operand_map, offset_map,
3106 possible_truths, toplev_predicate);
3107 edge_set_predicate (e, &p);
3108 /* TODO: We should remove the edge for code that will be optimized
3109 out, but we need to keep verifiers and tree-inline happy.
3110 Make it cold for now. */
3111 if (false_predicate_p (&p))
3113 e->count = 0;
3114 e->frequency = 0;
3117 else
3118 edge_set_predicate (e, toplev_predicate);
3122 /* Same as remap_predicate, but set result into hint *HINT. */
3124 static void
3125 remap_hint_predicate (struct inline_summary *info,
3126 struct inline_summary *callee_info,
3127 struct predicate **hint,
3128 VEC (int, heap) *operand_map,
3129 VEC (int, heap) *offset_map,
3130 clause_t possible_truths,
3131 struct predicate *toplev_predicate)
3133 predicate p;
3135 if (!*hint)
3136 return;
3137 p = remap_predicate (info, callee_info,
3138 *hint,
3139 operand_map, offset_map,
3140 possible_truths,
3141 toplev_predicate);
3142 if (!false_predicate_p (&p)
3143 && !true_predicate_p (&p))
3145 if (!*hint)
3146 set_hint_predicate (hint, p);
3147 else
3148 **hint = and_predicates (info->conds,
3149 *hint,
3150 &p);
3154 /* We inlined EDGE. Update summary of the function we inlined into. */
3156 void
3157 inline_merge_summary (struct cgraph_edge *edge)
3159 struct inline_summary *callee_info = inline_summary (edge->callee);
3160 struct cgraph_node *to = (edge->caller->global.inlined_to
3161 ? edge->caller->global.inlined_to : edge->caller);
3162 struct inline_summary *info = inline_summary (to);
3163 clause_t clause = 0; /* not_inline is known to be false. */
3164 size_time_entry *e;
3165 VEC (int, heap) *operand_map = NULL;
3166 VEC (int, heap) *offset_map = NULL;
3167 int i;
3168 struct predicate toplev_predicate;
3169 struct predicate true_p = true_predicate ();
3170 struct inline_edge_summary *es = inline_edge_summary (edge);
3172 if (es->predicate)
3173 toplev_predicate = *es->predicate;
3174 else
3175 toplev_predicate = true_predicate ();
3177 if (ipa_node_params_vector && callee_info->conds)
3179 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3180 int count = ipa_get_cs_argument_count (args);
3181 int i;
3183 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3184 if (count)
3186 VEC_safe_grow_cleared (int, heap, operand_map, count);
3187 VEC_safe_grow_cleared (int, heap, offset_map, count);
3189 for (i = 0; i < count; i++)
3191 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3192 int map = -1;
3194 /* TODO: handle non-NOPs when merging. */
3195 if (jfunc->type == IPA_JF_PASS_THROUGH)
3197 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3198 map = ipa_get_jf_pass_through_formal_id (jfunc);
3199 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3200 VEC_replace (int, offset_map, i, -1);
3202 else if (jfunc->type == IPA_JF_ANCESTOR)
3204 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3205 if (offset >= 0 && offset < INT_MAX)
3207 map = ipa_get_jf_ancestor_formal_id (jfunc);
3208 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3209 offset = -1;
3210 VEC_replace (int, offset_map, i, offset);
3213 VEC_replace (int, operand_map, i, map);
3214 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3217 for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
3219 struct predicate p = remap_predicate (info, callee_info,
3220 &e->predicate, operand_map,
3221 offset_map, clause,
3222 &toplev_predicate);
3223 if (!false_predicate_p (&p))
3225 gcov_type add_time = ((gcov_type)e->time * edge->frequency
3226 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3227 int prob = predicate_probability (callee_info->conds,
3228 &e->predicate,
3229 clause, es->param);
3230 add_time = add_time * prob / REG_BR_PROB_BASE;
3231 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3232 add_time = MAX_TIME * INLINE_TIME_SCALE;
3233 if (prob != REG_BR_PROB_BASE
3234 && dump_file && (dump_flags & TDF_DETAILS))
3236 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3237 (double)prob / REG_BR_PROB_BASE);
3239 account_size_time (info, e->size, add_time, &p);
3242 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3243 offset_map, clause, &toplev_predicate);
3244 remap_hint_predicate (info, callee_info,
3245 &callee_info->loop_iterations,
3246 operand_map, offset_map,
3247 clause, &toplev_predicate);
3248 remap_hint_predicate (info, callee_info,
3249 &callee_info->loop_stride,
3250 operand_map, offset_map,
3251 clause, &toplev_predicate);
3253 inline_update_callee_summaries (edge->callee,
3254 inline_edge_summary (edge)->loop_depth);
3256 /* We do not maintain predicates of inlined edges, free it. */
3257 edge_set_predicate (edge, &true_p);
3258 /* Similarly remove param summaries. */
3259 VEC_free (inline_param_summary_t, heap, es->param);
3260 VEC_free (int, heap, operand_map);
3261 VEC_free (int, heap, offset_map);
3264 /* For performance reasons inline_merge_summary is not updating overall size
3265 and time. Recompute it. */
3267 void
3268 inline_update_overall_summary (struct cgraph_node *node)
3270 struct inline_summary *info = inline_summary (node);
3271 size_time_entry *e;
3272 int i;
3274 info->size = 0;
3275 info->time = 0;
3276 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
3277 info->size += e->size, info->time += e->time;
3278 estimate_calls_size_and_time (node, &info->size, &info->time, NULL,
3279 ~(clause_t)(1 << predicate_false_condition),
3280 NULL, NULL, NULL);
3281 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3282 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3285 /* Estimate the time cost for the caller when inlining EDGE.
3286 Only to be called via estimate_edge_time, that handles the
3287 caching mechanism.
3289 When caching, also update the cache entry. Compute both time and
3290 size, since we always need both metrics eventually. */
3293 do_estimate_edge_time (struct cgraph_edge *edge)
3295 int time;
3296 int size;
3297 inline_hints hints;
3298 gcov_type ret;
3299 struct cgraph_node *callee;
3300 clause_t clause;
3301 VEC (tree, heap) *known_vals;
3302 VEC (tree, heap) *known_binfos;
3303 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3304 struct inline_edge_summary *es = inline_edge_summary (edge);
3306 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3308 gcc_checking_assert (edge->inline_failed);
3309 evaluate_properties_for_edge (edge, true,
3310 &clause, &known_vals, &known_binfos,
3311 &known_aggs);
3312 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3313 known_aggs, &size, &time, &hints, es->param);
3314 VEC_free (tree, heap, known_vals);
3315 VEC_free (tree, heap, known_binfos);
3316 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3318 ret = (((gcov_type)time
3319 - es->call_stmt_time) * edge->frequency
3320 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3322 /* When caching, update the cache entry. */
3323 if (edge_growth_cache)
3325 int ret_size;
3326 if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
3327 <= edge->uid)
3328 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
3329 cgraph_edge_max_uid);
3330 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).time
3331 = ret + (ret >= 0);
3333 ret_size = size - es->call_stmt_size;
3334 gcc_checking_assert (es->call_stmt_size);
3335 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).size
3336 = ret_size + (ret_size >= 0);
3337 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).hints
3338 = hints + 1;
3340 return ret;
3344 /* Estimate the growth of the caller when inlining EDGE.
3345 Only to be called via estimate_edge_size. */
3348 do_estimate_edge_growth (struct cgraph_edge *edge)
3350 int size;
3351 struct cgraph_node *callee;
3352 clause_t clause;
3353 VEC (tree, heap) *known_vals;
3354 VEC (tree, heap) *known_binfos;
3355 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3357 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3359 if (edge_growth_cache)
3361 do_estimate_edge_time (edge);
3362 size = VEC_index (edge_growth_cache_entry,
3363 edge_growth_cache,
3364 edge->uid).size;
3365 gcc_checking_assert (size);
3366 return size - (size > 0);
3369 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3371 /* Early inliner runs without caching, go ahead and do the dirty work. */
3372 gcc_checking_assert (edge->inline_failed);
3373 evaluate_properties_for_edge (edge, true,
3374 &clause, &known_vals, &known_binfos,
3375 &known_aggs);
3376 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3377 known_aggs, &size, NULL, NULL, NULL);
3378 VEC_free (tree, heap, known_vals);
3379 VEC_free (tree, heap, known_binfos);
3380 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3381 gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
3382 return size - inline_edge_summary (edge)->call_stmt_size;
3386 /* Estimate the growth of the caller when inlining EDGE.
3387 Only to be called via estimate_edge_size. */
3389 inline_hints
3390 do_estimate_edge_hints (struct cgraph_edge *edge)
3392 inline_hints hints;
3393 struct cgraph_node *callee;
3394 clause_t clause;
3395 VEC (tree, heap) *known_vals;
3396 VEC (tree, heap) *known_binfos;
3397 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3399 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3401 if (edge_growth_cache)
3403 do_estimate_edge_time (edge);
3404 hints = VEC_index (edge_growth_cache_entry,
3405 edge_growth_cache,
3406 edge->uid).hints;
3407 gcc_checking_assert (hints);
3408 return hints - 1;
3411 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3413 /* Early inliner runs without caching, go ahead and do the dirty work. */
3414 gcc_checking_assert (edge->inline_failed);
3415 evaluate_properties_for_edge (edge, true,
3416 &clause, &known_vals, &known_binfos,
3417 &known_aggs);
3418 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3419 known_aggs, NULL, NULL, &hints, NULL);
3420 VEC_free (tree, heap, known_vals);
3421 VEC_free (tree, heap, known_binfos);
3422 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3423 return hints;
3427 /* Estimate self time of the function NODE after inlining EDGE. */
3430 estimate_time_after_inlining (struct cgraph_node *node,
3431 struct cgraph_edge *edge)
3433 struct inline_edge_summary *es = inline_edge_summary (edge);
3434 if (!es->predicate || !false_predicate_p (es->predicate))
3436 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
3437 if (time < 0)
3438 time = 0;
3439 if (time > MAX_TIME)
3440 time = MAX_TIME;
3441 return time;
3443 return inline_summary (node)->time;
3447 /* Estimate the size of NODE after inlining EDGE which should be an
3448 edge to either NODE or a call inlined into NODE. */
3451 estimate_size_after_inlining (struct cgraph_node *node,
3452 struct cgraph_edge *edge)
3454 struct inline_edge_summary *es = inline_edge_summary (edge);
3455 if (!es->predicate || !false_predicate_p (es->predicate))
3457 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3458 gcc_assert (size >= 0);
3459 return size;
3461 return inline_summary (node)->size;
3465 struct growth_data
3467 bool self_recursive;
3468 int growth;
3472 /* Worker for do_estimate_growth. Collect growth for all callers. */
3474 static bool
3475 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3477 struct cgraph_edge *e;
3478 struct growth_data *d = (struct growth_data *) data;
3480 for (e = node->callers; e; e = e->next_caller)
3482 gcc_checking_assert (e->inline_failed);
3484 if (e->caller == node
3485 || (e->caller->global.inlined_to
3486 && e->caller->global.inlined_to == node))
3487 d->self_recursive = true;
3488 d->growth += estimate_edge_growth (e);
3490 return false;
3494 /* Estimate the growth caused by inlining NODE into all callees. */
3497 do_estimate_growth (struct cgraph_node *node)
3499 struct growth_data d = {0, false};
3500 struct inline_summary *info = inline_summary (node);
3502 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3504 /* For self recursive functions the growth estimation really should be
3505 infinity. We don't want to return very large values because the growth
3506 plays various roles in badness computation fractions. Be sure to not
3507 return zero or negative growths. */
3508 if (d.self_recursive)
3509 d.growth = d.growth < info->size ? info->size : d.growth;
3510 else
3512 if (!DECL_EXTERNAL (node->symbol.decl)
3513 && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3514 d.growth -= info->size;
3515 /* COMDAT functions are very often not shared across multiple units
3516 since they come from various template instantiations.
3517 Take this into account. */
3518 else if (DECL_COMDAT (node->symbol.decl)
3519 && cgraph_can_remove_if_no_direct_calls_p (node))
3520 d.growth -= (info->size
3521 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3522 + 50) / 100;
3525 if (node_growth_cache)
3527 if ((int)VEC_length (int, node_growth_cache) <= node->uid)
3528 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
3529 VEC_replace (int, node_growth_cache, node->uid,
3530 d.growth + (d.growth >= 0));
3532 return d.growth;
3536 /* This function performs intraprocedural analysis in NODE that is required to
3537 inline indirect calls. */
3539 static void
3540 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3542 ipa_analyze_node (node);
3543 if (dump_file && (dump_flags & TDF_DETAILS))
3545 ipa_print_node_params (dump_file, node);
3546 ipa_print_node_jump_functions (dump_file, node);
3551 /* Note function body size. */
3553 static void
3554 inline_analyze_function (struct cgraph_node *node)
3556 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
3557 current_function_decl = node->symbol.decl;
3559 if (dump_file)
3560 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3561 cgraph_node_name (node), node->uid);
3562 if (optimize && !node->thunk.thunk_p)
3563 inline_indirect_intraprocedural_analysis (node);
3564 compute_inline_parameters (node, false);
3566 current_function_decl = NULL;
3567 pop_cfun ();
3571 /* Called when new function is inserted to callgraph late. */
3573 static void
3574 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3576 inline_analyze_function (node);
3580 /* Note function body size. */
3582 void
3583 inline_generate_summary (void)
3585 struct cgraph_node *node;
3587 function_insertion_hook_holder =
3588 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3590 ipa_register_cgraph_hooks ();
3591 inline_free_summary ();
3593 FOR_EACH_DEFINED_FUNCTION (node)
3594 if (!node->alias)
3595 inline_analyze_function (node);
3599 /* Read predicate from IB. */
3601 static struct predicate
3602 read_predicate (struct lto_input_block *ib)
3604 struct predicate out;
3605 clause_t clause;
3606 int k = 0;
3610 gcc_assert (k <= MAX_CLAUSES);
3611 clause = out.clause[k++] = streamer_read_uhwi (ib);
3613 while (clause);
3615 /* Zero-initialize the remaining clauses in OUT. */
3616 while (k <= MAX_CLAUSES)
3617 out.clause[k++] = 0;
3619 return out;
3623 /* Write inline summary for edge E to OB. */
3625 static void
3626 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
3628 struct inline_edge_summary *es = inline_edge_summary (e);
3629 struct predicate p;
3630 int length, i;
3632 es->call_stmt_size = streamer_read_uhwi (ib);
3633 es->call_stmt_time = streamer_read_uhwi (ib);
3634 es->loop_depth = streamer_read_uhwi (ib);
3635 p = read_predicate (ib);
3636 edge_set_predicate (e, &p);
3637 length = streamer_read_uhwi (ib);
3638 if (length)
3640 VEC_safe_grow_cleared (inline_param_summary_t, heap, es->param, length);
3641 for (i = 0; i < length; i++)
3642 VEC_index (inline_param_summary_t, es->param, i).change_prob
3643 = streamer_read_uhwi (ib);
3648 /* Stream in inline summaries from the section. */
3650 static void
3651 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3652 size_t len)
3654 const struct lto_function_header *header =
3655 (const struct lto_function_header *) data;
3656 const int cfg_offset = sizeof (struct lto_function_header);
3657 const int main_offset = cfg_offset + header->cfg_size;
3658 const int string_offset = main_offset + header->main_size;
3659 struct data_in *data_in;
3660 struct lto_input_block ib;
3661 unsigned int i, count2, j;
3662 unsigned int f_count;
3664 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
3665 header->main_size);
3667 data_in =
3668 lto_data_in_create (file_data, (const char *) data + string_offset,
3669 header->string_size, NULL);
3670 f_count = streamer_read_uhwi (&ib);
3671 for (i = 0; i < f_count; i++)
3673 unsigned int index;
3674 struct cgraph_node *node;
3675 struct inline_summary *info;
3676 lto_symtab_encoder_t encoder;
3677 struct bitpack_d bp;
3678 struct cgraph_edge *e;
3679 predicate p;
3681 index = streamer_read_uhwi (&ib);
3682 encoder = file_data->symtab_node_encoder;
3683 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3684 info = inline_summary (node);
3686 info->estimated_stack_size
3687 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
3688 info->size = info->self_size = streamer_read_uhwi (&ib);
3689 info->time = info->self_time = streamer_read_uhwi (&ib);
3691 bp = streamer_read_bitpack (&ib);
3692 info->inlinable = bp_unpack_value (&bp, 1);
3694 count2 = streamer_read_uhwi (&ib);
3695 gcc_assert (!info->conds);
3696 for (j = 0; j < count2; j++)
3698 struct condition c;
3699 c.operand_num = streamer_read_uhwi (&ib);
3700 c.code = (enum tree_code) streamer_read_uhwi (&ib);
3701 c.val = stream_read_tree (&ib, data_in);
3702 bp = streamer_read_bitpack (&ib);
3703 c.agg_contents = bp_unpack_value (&bp, 1);
3704 c.by_ref = bp_unpack_value (&bp, 1);
3705 if (c.agg_contents)
3706 c.offset = streamer_read_uhwi (&ib);
3707 VEC_safe_push (condition, gc, info->conds, c);
3709 count2 = streamer_read_uhwi (&ib);
3710 gcc_assert (!info->entry);
3711 for (j = 0; j < count2; j++)
3713 struct size_time_entry e;
3715 e.size = streamer_read_uhwi (&ib);
3716 e.time = streamer_read_uhwi (&ib);
3717 e.predicate = read_predicate (&ib);
3719 VEC_safe_push (size_time_entry, gc, info->entry, e);
3722 p = read_predicate (&ib);
3723 set_hint_predicate (&info->loop_iterations, p);
3724 p = read_predicate (&ib);
3725 set_hint_predicate (&info->loop_stride, p);
3726 for (e = node->callees; e; e = e->next_callee)
3727 read_inline_edge_summary (&ib, e);
3728 for (e = node->indirect_calls; e; e = e->next_callee)
3729 read_inline_edge_summary (&ib, e);
3732 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
3733 len);
3734 lto_data_in_delete (data_in);
3738 /* Read inline summary. Jump functions are shared among ipa-cp
3739 and inliner, so when ipa-cp is active, we don't need to write them
3740 twice. */
3742 void
3743 inline_read_summary (void)
3745 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3746 struct lto_file_decl_data *file_data;
3747 unsigned int j = 0;
3749 inline_summary_alloc ();
3751 while ((file_data = file_data_vec[j++]))
3753 size_t len;
3754 const char *data = lto_get_section_data (file_data,
3755 LTO_section_inline_summary,
3756 NULL, &len);
3757 if (data)
3758 inline_read_section (file_data, data, len);
3759 else
3760 /* Fatal error here. We do not want to support compiling ltrans units
3761 with different version of compiler or different flags than the WPA
3762 unit, so this should never happen. */
3763 fatal_error ("ipa inline summary is missing in input file");
3765 if (optimize)
3767 ipa_register_cgraph_hooks ();
3768 if (!flag_ipa_cp)
3769 ipa_prop_read_jump_functions ();
3771 function_insertion_hook_holder =
3772 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3776 /* Write predicate P to OB. */
3778 static void
3779 write_predicate (struct output_block *ob, struct predicate *p)
3781 int j;
3782 if (p)
3783 for (j = 0; p->clause[j]; j++)
3785 gcc_assert (j < MAX_CLAUSES);
3786 streamer_write_uhwi (ob, p->clause[j]);
3788 streamer_write_uhwi (ob, 0);
3792 /* Write inline summary for edge E to OB. */
3794 static void
3795 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
3797 struct inline_edge_summary *es = inline_edge_summary (e);
3798 int i;
3800 streamer_write_uhwi (ob, es->call_stmt_size);
3801 streamer_write_uhwi (ob, es->call_stmt_time);
3802 streamer_write_uhwi (ob, es->loop_depth);
3803 write_predicate (ob, es->predicate);
3804 streamer_write_uhwi (ob, VEC_length (inline_param_summary_t, es->param));
3805 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param); i++)
3806 streamer_write_uhwi (ob, VEC_index (inline_param_summary_t,
3807 es->param, i).change_prob);
3811 /* Write inline summary for node in SET.
3812 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
3813 active, we don't need to write them twice. */
3815 void
3816 inline_write_summary (void)
3818 struct cgraph_node *node;
3819 symtab_node snode;
3820 struct output_block *ob = create_output_block (LTO_section_inline_summary);
3821 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3822 unsigned int count = 0;
3823 int i;
3825 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3826 if (symtab_function_p (snode = lto_symtab_encoder_deref (encoder, i))
3827 && cgraph (snode)->analyzed)
3828 count++;
3829 streamer_write_uhwi (ob, count);
3831 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3833 if (symtab_function_p (snode = lto_symtab_encoder_deref (encoder, i))
3834 && (node = cgraph (snode))->analyzed)
3836 struct inline_summary *info = inline_summary (node);
3837 struct bitpack_d bp;
3838 struct cgraph_edge *edge;
3839 int i;
3840 size_time_entry *e;
3841 struct condition *c;
3843 streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, (symtab_node)node));
3844 streamer_write_hwi (ob, info->estimated_self_stack_size);
3845 streamer_write_hwi (ob, info->self_size);
3846 streamer_write_hwi (ob, info->self_time);
3847 bp = bitpack_create (ob->main_stream);
3848 bp_pack_value (&bp, info->inlinable, 1);
3849 streamer_write_bitpack (&bp);
3850 streamer_write_uhwi (ob, VEC_length (condition, info->conds));
3851 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
3853 streamer_write_uhwi (ob, c->operand_num);
3854 streamer_write_uhwi (ob, c->code);
3855 stream_write_tree (ob, c->val, true);
3856 bp = bitpack_create (ob->main_stream);
3857 bp_pack_value (&bp, c->agg_contents, 1);
3858 bp_pack_value (&bp, c->by_ref, 1);
3859 streamer_write_bitpack (&bp);
3860 if (c->agg_contents)
3861 streamer_write_uhwi (ob, c->offset);
3863 streamer_write_uhwi (ob, VEC_length (size_time_entry, info->entry));
3864 for (i = 0;
3865 VEC_iterate (size_time_entry, info->entry, i, e);
3866 i++)
3868 streamer_write_uhwi (ob, e->size);
3869 streamer_write_uhwi (ob, e->time);
3870 write_predicate (ob, &e->predicate);
3872 write_predicate (ob, info->loop_iterations);
3873 write_predicate (ob, info->loop_stride);
3874 for (edge = node->callees; edge; edge = edge->next_callee)
3875 write_inline_edge_summary (ob, edge);
3876 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
3877 write_inline_edge_summary (ob, edge);
3880 streamer_write_char_stream (ob->main_stream, 0);
3881 produce_asm (ob, NULL);
3882 destroy_output_block (ob);
3884 if (optimize && !flag_ipa_cp)
3885 ipa_prop_write_jump_functions ();
3889 /* Release inline summary. */
3891 void
3892 inline_free_summary (void)
3894 struct cgraph_node *node;
3895 if (inline_edge_summary_vec == NULL)
3896 return;
3897 FOR_EACH_DEFINED_FUNCTION (node)
3898 reset_inline_summary (node);
3899 if (function_insertion_hook_holder)
3900 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3901 function_insertion_hook_holder = NULL;
3902 if (node_removal_hook_holder)
3903 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3904 node_removal_hook_holder = NULL;
3905 if (edge_removal_hook_holder)
3906 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3907 edge_removal_hook_holder = NULL;
3908 if (node_duplication_hook_holder)
3909 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3910 node_duplication_hook_holder = NULL;
3911 if (edge_duplication_hook_holder)
3912 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3913 edge_duplication_hook_holder = NULL;
3914 VEC_free (inline_summary_t, gc, inline_summary_vec);
3915 inline_summary_vec = NULL;
3916 VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
3917 inline_edge_summary_vec = NULL;
3918 if (edge_predicate_pool)
3919 free_alloc_pool (edge_predicate_pool);
3920 edge_predicate_pool = 0;