2012-10-06 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob31ecec9af9903ba6ac3f45c784cd72f260895e08
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;
2558 gcc_assert (!node->global.inlined_to);
2560 inline_summary_alloc ();
2562 info = inline_summary (node);
2563 reset_inline_summary (node);
2565 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2566 Once this happen, we will need to more curefully predict call
2567 statement size. */
2568 if (node->thunk.thunk_p)
2570 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2571 struct predicate t = true_predicate ();
2573 info->inlinable = 0;
2574 node->callees->call_stmt_cannot_inline_p = true;
2575 node->local.can_change_signature = false;
2576 es->call_stmt_time = 1;
2577 es->call_stmt_size = 1;
2578 account_size_time (info, 0, 0, &t);
2579 return;
2582 /* Even is_gimple_min_invariant rely on current_function_decl. */
2583 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2585 /* Estimate the stack size for the function if we're optimizing. */
2586 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2587 info->estimated_self_stack_size = self_stack_size;
2588 info->estimated_stack_size = self_stack_size;
2589 info->stack_frame_offset = 0;
2591 /* Can this function be inlined at all? */
2592 info->inlinable = tree_inlinable_function_p (node->symbol.decl);
2594 /* Type attributes can use parameter indices to describe them. */
2595 if (TYPE_ATTRIBUTES (TREE_TYPE (node->symbol.decl)))
2596 node->local.can_change_signature = false;
2597 else
2599 /* Otherwise, inlinable functions always can change signature. */
2600 if (info->inlinable)
2601 node->local.can_change_signature = true;
2602 else
2604 /* Functions calling builtin_apply can not change signature. */
2605 for (e = node->callees; e; e = e->next_callee)
2607 tree cdecl = e->callee->symbol.decl;
2608 if (DECL_BUILT_IN (cdecl)
2609 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2610 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2611 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2612 break;
2614 node->local.can_change_signature = !e;
2617 estimate_function_body_sizes (node, early);
2619 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2620 info->time = info->self_time;
2621 info->size = info->self_size;
2622 info->stack_frame_offset = 0;
2623 info->estimated_stack_size = info->estimated_self_stack_size;
2624 pop_cfun ();
2628 /* Compute parameters of functions used by inliner using
2629 current_function_decl. */
2631 static unsigned int
2632 compute_inline_parameters_for_current (void)
2634 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2635 return 0;
2638 struct gimple_opt_pass pass_inline_parameters =
2641 GIMPLE_PASS,
2642 "inline_param", /* name */
2643 NULL, /* gate */
2644 compute_inline_parameters_for_current,/* execute */
2645 NULL, /* sub */
2646 NULL, /* next */
2647 0, /* static_pass_number */
2648 TV_INLINE_PARAMETERS, /* tv_id */
2649 0, /* properties_required */
2650 0, /* properties_provided */
2651 0, /* properties_destroyed */
2652 0, /* todo_flags_start */
2653 0 /* todo_flags_finish */
2658 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2660 static void
2661 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2662 int prob)
2664 struct inline_edge_summary *es = inline_edge_summary (e);
2665 *size += es->call_stmt_size * INLINE_SIZE_SCALE;
2666 *time += (es->call_stmt_time * prob / REG_BR_PROB_BASE
2667 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
2668 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2669 *time = MAX_TIME * INLINE_TIME_SCALE;
2673 /* Estimate benefit devirtualizing indirect edge IE, provided KNOWN_VALS and
2674 KNOWN_BINFOS. */
2676 static bool
2677 estimate_edge_devirt_benefit (struct cgraph_edge *ie,
2678 int *size, int *time, int prob,
2679 VEC (tree, heap) *known_vals,
2680 VEC (tree, heap) *known_binfos,
2681 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
2683 tree target;
2684 int time_diff, size_diff;
2685 struct cgraph_node *callee;
2686 struct inline_summary *isummary;
2688 if (!known_vals && !known_binfos)
2689 return false;
2691 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2692 known_aggs);
2693 if (!target)
2694 return false;
2696 /* Account for difference in cost between indirect and direct calls. */
2697 size_diff = ((eni_size_weights.indirect_call_cost - eni_size_weights.call_cost)
2698 * INLINE_SIZE_SCALE);
2699 *size -= size_diff;
2700 time_diff = ((eni_time_weights.indirect_call_cost - eni_time_weights.call_cost)
2701 * INLINE_TIME_SCALE * prob / REG_BR_PROB_BASE);
2702 *time -= time_diff;
2704 callee = cgraph_get_node (target);
2705 if (!callee || !callee->analyzed)
2706 return false;
2707 isummary = inline_summary (callee);
2708 return isummary->inlinable;
2712 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.
2713 POSSIBLE_TRUTHS, KNOWN_VALS and KNOWN_BINFOS describe context of the call
2714 site. */
2716 static void
2717 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
2718 inline_hints *hints,
2719 clause_t possible_truths,
2720 VEC (tree, heap) *known_vals,
2721 VEC (tree, heap) *known_binfos,
2722 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
2724 struct cgraph_edge *e;
2725 for (e = node->callees; e; e = e->next_callee)
2727 struct inline_edge_summary *es = inline_edge_summary (e);
2728 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2730 if (e->inline_failed)
2732 /* Predicates of calls shall not use NOT_CHANGED codes,
2733 sowe do not need to compute probabilities. */
2734 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE);
2736 else
2737 estimate_calls_size_and_time (e->callee, size, time, hints,
2738 possible_truths,
2739 known_vals, known_binfos, known_aggs);
2742 for (e = node->indirect_calls; e; e = e->next_callee)
2744 struct inline_edge_summary *es = inline_edge_summary (e);
2745 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2747 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE);
2748 if (estimate_edge_devirt_benefit (e, size, time, REG_BR_PROB_BASE,
2749 known_vals, known_binfos, known_aggs)
2750 && hints
2751 && cgraph_maybe_hot_edge_p (e))
2752 *hints |= INLINE_HINT_indirect_call;
2758 /* Estimate size and time needed to execute NODE assuming
2759 POSSIBLE_TRUTHS clause, and KNOWN_VALS and KNOWN_BINFOS information
2760 about NODE's arguments. */
2762 static void
2763 estimate_node_size_and_time (struct cgraph_node *node,
2764 clause_t possible_truths,
2765 VEC (tree, heap) *known_vals,
2766 VEC (tree, heap) *known_binfos,
2767 VEC (ipa_agg_jump_function_p, heap) *known_aggs,
2768 int *ret_size, int *ret_time,
2769 inline_hints *ret_hints,
2770 VEC (inline_param_summary_t, heap)
2771 *inline_param_summary)
2773 struct inline_summary *info = inline_summary (node);
2774 size_time_entry *e;
2775 int size = 0, time = 0;
2776 inline_hints hints = 0;
2777 int i;
2779 if (dump_file
2780 && (dump_flags & TDF_DETAILS))
2782 bool found = false;
2783 fprintf (dump_file, " Estimating body: %s/%i\n"
2784 " Known to be false: ",
2785 cgraph_node_name (node),
2786 node->uid);
2788 for (i = predicate_not_inlined_condition;
2789 i < (predicate_first_dynamic_condition
2790 + (int)VEC_length (condition, info->conds)); i++)
2791 if (!(possible_truths & (1 << i)))
2793 if (found)
2794 fprintf (dump_file, ", ");
2795 found = true;
2796 dump_condition (dump_file, info->conds, i);
2800 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2801 if (evaluate_predicate (&e->predicate, possible_truths))
2803 size += e->size;
2804 if (!inline_param_summary)
2805 time += e->time;
2806 else
2808 int prob = predicate_probability (info->conds,
2809 &e->predicate,
2810 possible_truths,
2811 inline_param_summary);
2812 time += e->time * prob / REG_BR_PROB_BASE;
2817 if (info->loop_iterations
2818 && !evaluate_predicate (info->loop_iterations, possible_truths))
2819 hints |=INLINE_HINT_loop_iterations;
2820 if (info->loop_stride
2821 && !evaluate_predicate (info->loop_stride, possible_truths))
2822 hints |=INLINE_HINT_loop_stride;
2824 if (time > MAX_TIME * INLINE_TIME_SCALE)
2825 time = MAX_TIME * INLINE_TIME_SCALE;
2827 estimate_calls_size_and_time (node, &size, &time, &hints, possible_truths,
2828 known_vals, known_binfos, known_aggs);
2829 time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
2830 size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
2833 if (dump_file
2834 && (dump_flags & TDF_DETAILS))
2835 fprintf (dump_file, "\n size:%i time:%i\n", size, time);
2836 if (ret_time)
2837 *ret_time = time;
2838 if (ret_size)
2839 *ret_size = size;
2840 if (ret_hints)
2841 *ret_hints = hints;
2842 return;
2846 /* Estimate size and time needed to execute callee of EDGE assuming that
2847 parameters known to be constant at caller of EDGE are propagated.
2848 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
2849 and types for parameters. */
2851 void
2852 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
2853 VEC (tree, heap) *known_vals,
2854 VEC (tree, heap) *known_binfos,
2855 int *ret_size, int *ret_time)
2857 clause_t clause;
2859 clause = evaluate_conditions_for_known_args (node, false, known_vals, NULL);
2860 estimate_node_size_and_time (node, clause, known_vals, known_binfos, NULL,
2861 ret_size, ret_time, NULL,
2862 NULL);
2865 /* Translate all conditions from callee representation into caller
2866 representation and symbolically evaluate predicate P into new predicate.
2868 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
2869 is summary of function predicate P is from. OPERAND_MAP is array giving
2870 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
2871 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
2872 predicate under which callee is executed. OFFSET_MAP is an array of of
2873 offsets that need to be added to conditions, negative offset means that
2874 conditions relying on values passed by reference have to be discarded
2875 because they might not be preserved (and should be considered offset zero
2876 for other purposes). */
2878 static struct predicate
2879 remap_predicate (struct inline_summary *info,
2880 struct inline_summary *callee_info,
2881 struct predicate *p,
2882 VEC (int, heap) *operand_map,
2883 VEC (int, heap) *offset_map,
2884 clause_t possible_truths,
2885 struct predicate *toplev_predicate)
2887 int i;
2888 struct predicate out = true_predicate ();
2890 /* True predicate is easy. */
2891 if (true_predicate_p (p))
2892 return *toplev_predicate;
2893 for (i = 0; p->clause[i]; i++)
2895 clause_t clause = p->clause[i];
2896 int cond;
2897 struct predicate clause_predicate = false_predicate ();
2899 gcc_assert (i < MAX_CLAUSES);
2901 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
2902 /* Do we have condition we can't disprove? */
2903 if (clause & possible_truths & (1 << cond))
2905 struct predicate cond_predicate;
2906 /* Work out if the condition can translate to predicate in the
2907 inlined function. */
2908 if (cond >= predicate_first_dynamic_condition)
2910 struct condition *c;
2912 c = &VEC_index (condition, callee_info->conds,
2913 cond - predicate_first_dynamic_condition);
2914 /* See if we can remap condition operand to caller's operand.
2915 Otherwise give up. */
2916 if (!operand_map
2917 || (int)VEC_length (int, operand_map) <= c->operand_num
2918 || VEC_index (int, operand_map, c->operand_num) == -1
2919 /* TODO: For non-aggregate conditions, adding an offset is
2920 basically an arithmetic jump function processing which
2921 we should support in future. */
2922 || ((!c->agg_contents || !c->by_ref)
2923 && VEC_index (int, offset_map, c->operand_num) > 0)
2924 || (c->agg_contents && c->by_ref
2925 && VEC_index (int, offset_map, c->operand_num) < 0))
2926 cond_predicate = true_predicate ();
2927 else
2929 struct agg_position_info ap;
2930 HOST_WIDE_INT offset_delta = VEC_index (int, offset_map,
2931 c->operand_num);
2932 if (offset_delta < 0)
2934 gcc_checking_assert (!c->agg_contents || !c->by_ref);
2935 offset_delta = 0;
2937 gcc_assert (!c->agg_contents
2938 || c->by_ref
2939 || offset_delta == 0);
2940 ap.offset = c->offset + offset_delta;
2941 ap.agg_contents = c->agg_contents;
2942 ap.by_ref = c->by_ref;
2943 cond_predicate = add_condition (info,
2944 VEC_index (int,
2945 operand_map,
2946 c->operand_num),
2947 &ap, c->code, c->val);
2950 /* Fixed conditions remains same, construct single
2951 condition predicate. */
2952 else
2954 cond_predicate.clause[0] = 1 << cond;
2955 cond_predicate.clause[1] = 0;
2957 clause_predicate = or_predicates (info->conds, &clause_predicate,
2958 &cond_predicate);
2960 out = and_predicates (info->conds, &out, &clause_predicate);
2962 return and_predicates (info->conds, &out, toplev_predicate);
2966 /* Update summary information of inline clones after inlining.
2967 Compute peak stack usage. */
2969 static void
2970 inline_update_callee_summaries (struct cgraph_node *node,
2971 int depth)
2973 struct cgraph_edge *e;
2974 struct inline_summary *callee_info = inline_summary (node);
2975 struct inline_summary *caller_info = inline_summary (node->callers->caller);
2976 HOST_WIDE_INT peak;
2978 callee_info->stack_frame_offset
2979 = caller_info->stack_frame_offset
2980 + caller_info->estimated_self_stack_size;
2981 peak = callee_info->stack_frame_offset
2982 + callee_info->estimated_self_stack_size;
2983 if (inline_summary (node->global.inlined_to)->estimated_stack_size
2984 < peak)
2985 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
2986 cgraph_propagate_frequency (node);
2987 for (e = node->callees; e; e = e->next_callee)
2989 if (!e->inline_failed)
2990 inline_update_callee_summaries (e->callee, depth);
2991 inline_edge_summary (e)->loop_depth += depth;
2993 for (e = node->indirect_calls; e; e = e->next_callee)
2994 inline_edge_summary (e)->loop_depth += depth;
2997 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
2998 When functoin A is inlined in B and A calls C with parameter that
2999 changes with probability PROB1 and C is known to be passthroug
3000 of argument if B that change with probability PROB2, the probability
3001 of change is now PROB1*PROB2. */
3003 static void
3004 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3005 struct cgraph_edge *edge)
3007 if (ipa_node_params_vector)
3009 int i;
3010 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3011 struct inline_edge_summary *es = inline_edge_summary (edge);
3012 struct inline_edge_summary *inlined_es
3013 = inline_edge_summary (inlined_edge);
3015 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3017 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3018 if (jfunc->type == IPA_JF_PASS_THROUGH
3019 && (ipa_get_jf_pass_through_formal_id (jfunc)
3020 < (int) VEC_length (inline_param_summary_t,
3021 inlined_es->param)))
3023 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3024 int prob1 = VEC_index (inline_param_summary_t,
3025 es->param, i).change_prob;
3026 int prob2 = VEC_index
3027 (inline_param_summary_t,
3028 inlined_es->param, jf_formal_id).change_prob;
3029 int prob = ((prob1 * prob2 + REG_BR_PROB_BASE / 2)
3030 / REG_BR_PROB_BASE);
3032 if (prob1 && prob2 && !prob)
3033 prob = 1;
3035 VEC_index (inline_param_summary_t,
3036 es->param, i).change_prob = prob;
3042 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3044 Remap predicates of callees of NODE. Rest of arguments match
3045 remap_predicate.
3047 Also update change probabilities. */
3049 static void
3050 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3051 struct cgraph_node *node,
3052 struct inline_summary *info,
3053 struct inline_summary *callee_info,
3054 VEC (int, heap) *operand_map,
3055 VEC (int, heap) *offset_map,
3056 clause_t possible_truths,
3057 struct predicate *toplev_predicate)
3059 struct cgraph_edge *e;
3060 for (e = node->callees; e; e = e->next_callee)
3062 struct inline_edge_summary *es = inline_edge_summary (e);
3063 struct predicate p;
3065 if (e->inline_failed)
3067 remap_edge_change_prob (inlined_edge, e);
3069 if (es->predicate)
3071 p = remap_predicate (info, callee_info,
3072 es->predicate, operand_map, offset_map,
3073 possible_truths,
3074 toplev_predicate);
3075 edge_set_predicate (e, &p);
3076 /* TODO: We should remove the edge for code that will be
3077 optimized out, but we need to keep verifiers and tree-inline
3078 happy. Make it cold for now. */
3079 if (false_predicate_p (&p))
3081 e->count = 0;
3082 e->frequency = 0;
3085 else
3086 edge_set_predicate (e, toplev_predicate);
3088 else
3089 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3090 operand_map, offset_map, possible_truths,
3091 toplev_predicate);
3093 for (e = node->indirect_calls; e; e = e->next_callee)
3095 struct inline_edge_summary *es = inline_edge_summary (e);
3096 struct predicate p;
3098 remap_edge_change_prob (inlined_edge, e);
3099 if (es->predicate)
3101 p = remap_predicate (info, callee_info,
3102 es->predicate, operand_map, offset_map,
3103 possible_truths, toplev_predicate);
3104 edge_set_predicate (e, &p);
3105 /* TODO: We should remove the edge for code that will be optimized
3106 out, but we need to keep verifiers and tree-inline happy.
3107 Make it cold for now. */
3108 if (false_predicate_p (&p))
3110 e->count = 0;
3111 e->frequency = 0;
3114 else
3115 edge_set_predicate (e, toplev_predicate);
3119 /* Same as remap_predicate, but set result into hint *HINT. */
3121 static void
3122 remap_hint_predicate (struct inline_summary *info,
3123 struct inline_summary *callee_info,
3124 struct predicate **hint,
3125 VEC (int, heap) *operand_map,
3126 VEC (int, heap) *offset_map,
3127 clause_t possible_truths,
3128 struct predicate *toplev_predicate)
3130 predicate p;
3132 if (!*hint)
3133 return;
3134 p = remap_predicate (info, callee_info,
3135 *hint,
3136 operand_map, offset_map,
3137 possible_truths,
3138 toplev_predicate);
3139 if (!false_predicate_p (&p)
3140 && !true_predicate_p (&p))
3142 if (!*hint)
3143 set_hint_predicate (hint, p);
3144 else
3145 **hint = and_predicates (info->conds,
3146 *hint,
3147 &p);
3151 /* We inlined EDGE. Update summary of the function we inlined into. */
3153 void
3154 inline_merge_summary (struct cgraph_edge *edge)
3156 struct inline_summary *callee_info = inline_summary (edge->callee);
3157 struct cgraph_node *to = (edge->caller->global.inlined_to
3158 ? edge->caller->global.inlined_to : edge->caller);
3159 struct inline_summary *info = inline_summary (to);
3160 clause_t clause = 0; /* not_inline is known to be false. */
3161 size_time_entry *e;
3162 VEC (int, heap) *operand_map = NULL;
3163 VEC (int, heap) *offset_map = NULL;
3164 int i;
3165 struct predicate toplev_predicate;
3166 struct predicate true_p = true_predicate ();
3167 struct inline_edge_summary *es = inline_edge_summary (edge);
3169 if (es->predicate)
3170 toplev_predicate = *es->predicate;
3171 else
3172 toplev_predicate = true_predicate ();
3174 if (ipa_node_params_vector && callee_info->conds)
3176 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3177 int count = ipa_get_cs_argument_count (args);
3178 int i;
3180 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3181 if (count)
3183 VEC_safe_grow_cleared (int, heap, operand_map, count);
3184 VEC_safe_grow_cleared (int, heap, offset_map, count);
3186 for (i = 0; i < count; i++)
3188 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3189 int map = -1;
3191 /* TODO: handle non-NOPs when merging. */
3192 if (jfunc->type == IPA_JF_PASS_THROUGH)
3194 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3195 map = ipa_get_jf_pass_through_formal_id (jfunc);
3196 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3197 VEC_replace (int, offset_map, i, -1);
3199 else if (jfunc->type == IPA_JF_ANCESTOR)
3201 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3202 if (offset >= 0 && offset < INT_MAX)
3204 map = ipa_get_jf_ancestor_formal_id (jfunc);
3205 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3206 offset = -1;
3207 VEC_replace (int, offset_map, i, offset);
3210 VEC_replace (int, operand_map, i, map);
3211 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3214 for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
3216 struct predicate p = remap_predicate (info, callee_info,
3217 &e->predicate, operand_map,
3218 offset_map, clause,
3219 &toplev_predicate);
3220 if (!false_predicate_p (&p))
3222 gcov_type add_time = ((gcov_type)e->time * edge->frequency
3223 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3224 int prob = predicate_probability (callee_info->conds,
3225 &e->predicate,
3226 clause, es->param);
3227 add_time = add_time * prob / REG_BR_PROB_BASE;
3228 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3229 add_time = MAX_TIME * INLINE_TIME_SCALE;
3230 if (prob != REG_BR_PROB_BASE
3231 && dump_file && (dump_flags & TDF_DETAILS))
3233 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3234 (double)prob / REG_BR_PROB_BASE);
3236 account_size_time (info, e->size, add_time, &p);
3239 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3240 offset_map, clause, &toplev_predicate);
3241 remap_hint_predicate (info, callee_info,
3242 &callee_info->loop_iterations,
3243 operand_map, offset_map,
3244 clause, &toplev_predicate);
3245 remap_hint_predicate (info, callee_info,
3246 &callee_info->loop_stride,
3247 operand_map, offset_map,
3248 clause, &toplev_predicate);
3250 inline_update_callee_summaries (edge->callee,
3251 inline_edge_summary (edge)->loop_depth);
3253 /* We do not maintain predicates of inlined edges, free it. */
3254 edge_set_predicate (edge, &true_p);
3255 /* Similarly remove param summaries. */
3256 VEC_free (inline_param_summary_t, heap, es->param);
3257 VEC_free (int, heap, operand_map);
3258 VEC_free (int, heap, offset_map);
3261 /* For performance reasons inline_merge_summary is not updating overall size
3262 and time. Recompute it. */
3264 void
3265 inline_update_overall_summary (struct cgraph_node *node)
3267 struct inline_summary *info = inline_summary (node);
3268 size_time_entry *e;
3269 int i;
3271 info->size = 0;
3272 info->time = 0;
3273 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
3274 info->size += e->size, info->time += e->time;
3275 estimate_calls_size_and_time (node, &info->size, &info->time, NULL,
3276 ~(clause_t)(1 << predicate_false_condition),
3277 NULL, NULL, NULL);
3278 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3279 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3282 /* Estimate the time cost for the caller when inlining EDGE.
3283 Only to be called via estimate_edge_time, that handles the
3284 caching mechanism.
3286 When caching, also update the cache entry. Compute both time and
3287 size, since we always need both metrics eventually. */
3290 do_estimate_edge_time (struct cgraph_edge *edge)
3292 int time;
3293 int size;
3294 inline_hints hints;
3295 gcov_type ret;
3296 struct cgraph_node *callee;
3297 clause_t clause;
3298 VEC (tree, heap) *known_vals;
3299 VEC (tree, heap) *known_binfos;
3300 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3301 struct inline_edge_summary *es = inline_edge_summary (edge);
3303 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3305 gcc_checking_assert (edge->inline_failed);
3306 evaluate_properties_for_edge (edge, true,
3307 &clause, &known_vals, &known_binfos,
3308 &known_aggs);
3309 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3310 known_aggs, &size, &time, &hints, es->param);
3311 VEC_free (tree, heap, known_vals);
3312 VEC_free (tree, heap, known_binfos);
3313 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3315 ret = (((gcov_type)time
3316 - es->call_stmt_time) * edge->frequency
3317 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3319 /* When caching, update the cache entry. */
3320 if (edge_growth_cache)
3322 int ret_size;
3323 if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
3324 <= edge->uid)
3325 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
3326 cgraph_edge_max_uid);
3327 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).time
3328 = ret + (ret >= 0);
3330 ret_size = size - es->call_stmt_size;
3331 gcc_checking_assert (es->call_stmt_size);
3332 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).size
3333 = ret_size + (ret_size >= 0);
3334 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).hints
3335 = hints + 1;
3337 return ret;
3341 /* Estimate the growth of the caller when inlining EDGE.
3342 Only to be called via estimate_edge_size. */
3345 do_estimate_edge_growth (struct cgraph_edge *edge)
3347 int size;
3348 struct cgraph_node *callee;
3349 clause_t clause;
3350 VEC (tree, heap) *known_vals;
3351 VEC (tree, heap) *known_binfos;
3352 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3354 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3356 if (edge_growth_cache)
3358 do_estimate_edge_time (edge);
3359 size = VEC_index (edge_growth_cache_entry,
3360 edge_growth_cache,
3361 edge->uid).size;
3362 gcc_checking_assert (size);
3363 return size - (size > 0);
3366 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3368 /* Early inliner runs without caching, go ahead and do the dirty work. */
3369 gcc_checking_assert (edge->inline_failed);
3370 evaluate_properties_for_edge (edge, true,
3371 &clause, &known_vals, &known_binfos,
3372 &known_aggs);
3373 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3374 known_aggs, &size, NULL, NULL, NULL);
3375 VEC_free (tree, heap, known_vals);
3376 VEC_free (tree, heap, known_binfos);
3377 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3378 gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
3379 return size - inline_edge_summary (edge)->call_stmt_size;
3383 /* Estimate the growth of the caller when inlining EDGE.
3384 Only to be called via estimate_edge_size. */
3386 inline_hints
3387 do_estimate_edge_hints (struct cgraph_edge *edge)
3389 inline_hints hints;
3390 struct cgraph_node *callee;
3391 clause_t clause;
3392 VEC (tree, heap) *known_vals;
3393 VEC (tree, heap) *known_binfos;
3394 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3396 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3398 if (edge_growth_cache)
3400 do_estimate_edge_time (edge);
3401 hints = VEC_index (edge_growth_cache_entry,
3402 edge_growth_cache,
3403 edge->uid).hints;
3404 gcc_checking_assert (hints);
3405 return hints - 1;
3408 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3410 /* Early inliner runs without caching, go ahead and do the dirty work. */
3411 gcc_checking_assert (edge->inline_failed);
3412 evaluate_properties_for_edge (edge, true,
3413 &clause, &known_vals, &known_binfos,
3414 &known_aggs);
3415 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3416 known_aggs, NULL, NULL, &hints, NULL);
3417 VEC_free (tree, heap, known_vals);
3418 VEC_free (tree, heap, known_binfos);
3419 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3420 return hints;
3424 /* Estimate self time of the function NODE after inlining EDGE. */
3427 estimate_time_after_inlining (struct cgraph_node *node,
3428 struct cgraph_edge *edge)
3430 struct inline_edge_summary *es = inline_edge_summary (edge);
3431 if (!es->predicate || !false_predicate_p (es->predicate))
3433 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
3434 if (time < 0)
3435 time = 0;
3436 if (time > MAX_TIME)
3437 time = MAX_TIME;
3438 return time;
3440 return inline_summary (node)->time;
3444 /* Estimate the size of NODE after inlining EDGE which should be an
3445 edge to either NODE or a call inlined into NODE. */
3448 estimate_size_after_inlining (struct cgraph_node *node,
3449 struct cgraph_edge *edge)
3451 struct inline_edge_summary *es = inline_edge_summary (edge);
3452 if (!es->predicate || !false_predicate_p (es->predicate))
3454 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3455 gcc_assert (size >= 0);
3456 return size;
3458 return inline_summary (node)->size;
3462 struct growth_data
3464 bool self_recursive;
3465 int growth;
3469 /* Worker for do_estimate_growth. Collect growth for all callers. */
3471 static bool
3472 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3474 struct cgraph_edge *e;
3475 struct growth_data *d = (struct growth_data *) data;
3477 for (e = node->callers; e; e = e->next_caller)
3479 gcc_checking_assert (e->inline_failed);
3481 if (e->caller == node
3482 || (e->caller->global.inlined_to
3483 && e->caller->global.inlined_to == node))
3484 d->self_recursive = true;
3485 d->growth += estimate_edge_growth (e);
3487 return false;
3491 /* Estimate the growth caused by inlining NODE into all callees. */
3494 do_estimate_growth (struct cgraph_node *node)
3496 struct growth_data d = {0, false};
3497 struct inline_summary *info = inline_summary (node);
3499 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3501 /* For self recursive functions the growth estimation really should be
3502 infinity. We don't want to return very large values because the growth
3503 plays various roles in badness computation fractions. Be sure to not
3504 return zero or negative growths. */
3505 if (d.self_recursive)
3506 d.growth = d.growth < info->size ? info->size : d.growth;
3507 else
3509 if (!DECL_EXTERNAL (node->symbol.decl)
3510 && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3511 d.growth -= info->size;
3512 /* COMDAT functions are very often not shared across multiple units
3513 since they come from various template instantiations.
3514 Take this into account. */
3515 else if (DECL_COMDAT (node->symbol.decl)
3516 && cgraph_can_remove_if_no_direct_calls_p (node))
3517 d.growth -= (info->size
3518 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3519 + 50) / 100;
3522 if (node_growth_cache)
3524 if ((int)VEC_length (int, node_growth_cache) <= node->uid)
3525 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
3526 VEC_replace (int, node_growth_cache, node->uid,
3527 d.growth + (d.growth >= 0));
3529 return d.growth;
3533 /* This function performs intraprocedural analysis in NODE that is required to
3534 inline indirect calls. */
3536 static void
3537 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3539 ipa_analyze_node (node);
3540 if (dump_file && (dump_flags & TDF_DETAILS))
3542 ipa_print_node_params (dump_file, node);
3543 ipa_print_node_jump_functions (dump_file, node);
3548 /* Note function body size. */
3550 static void
3551 inline_analyze_function (struct cgraph_node *node)
3553 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
3555 if (dump_file)
3556 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3557 cgraph_node_name (node), node->uid);
3558 if (optimize && !node->thunk.thunk_p)
3559 inline_indirect_intraprocedural_analysis (node);
3560 compute_inline_parameters (node, false);
3562 pop_cfun ();
3566 /* Called when new function is inserted to callgraph late. */
3568 static void
3569 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3571 inline_analyze_function (node);
3575 /* Note function body size. */
3577 void
3578 inline_generate_summary (void)
3580 struct cgraph_node *node;
3582 function_insertion_hook_holder =
3583 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3585 ipa_register_cgraph_hooks ();
3586 inline_free_summary ();
3588 FOR_EACH_DEFINED_FUNCTION (node)
3589 if (!node->alias)
3590 inline_analyze_function (node);
3594 /* Read predicate from IB. */
3596 static struct predicate
3597 read_predicate (struct lto_input_block *ib)
3599 struct predicate out;
3600 clause_t clause;
3601 int k = 0;
3605 gcc_assert (k <= MAX_CLAUSES);
3606 clause = out.clause[k++] = streamer_read_uhwi (ib);
3608 while (clause);
3610 /* Zero-initialize the remaining clauses in OUT. */
3611 while (k <= MAX_CLAUSES)
3612 out.clause[k++] = 0;
3614 return out;
3618 /* Write inline summary for edge E to OB. */
3620 static void
3621 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
3623 struct inline_edge_summary *es = inline_edge_summary (e);
3624 struct predicate p;
3625 int length, i;
3627 es->call_stmt_size = streamer_read_uhwi (ib);
3628 es->call_stmt_time = streamer_read_uhwi (ib);
3629 es->loop_depth = streamer_read_uhwi (ib);
3630 p = read_predicate (ib);
3631 edge_set_predicate (e, &p);
3632 length = streamer_read_uhwi (ib);
3633 if (length)
3635 VEC_safe_grow_cleared (inline_param_summary_t, heap, es->param, length);
3636 for (i = 0; i < length; i++)
3637 VEC_index (inline_param_summary_t, es->param, i).change_prob
3638 = streamer_read_uhwi (ib);
3643 /* Stream in inline summaries from the section. */
3645 static void
3646 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3647 size_t len)
3649 const struct lto_function_header *header =
3650 (const struct lto_function_header *) data;
3651 const int cfg_offset = sizeof (struct lto_function_header);
3652 const int main_offset = cfg_offset + header->cfg_size;
3653 const int string_offset = main_offset + header->main_size;
3654 struct data_in *data_in;
3655 struct lto_input_block ib;
3656 unsigned int i, count2, j;
3657 unsigned int f_count;
3659 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
3660 header->main_size);
3662 data_in =
3663 lto_data_in_create (file_data, (const char *) data + string_offset,
3664 header->string_size, NULL);
3665 f_count = streamer_read_uhwi (&ib);
3666 for (i = 0; i < f_count; i++)
3668 unsigned int index;
3669 struct cgraph_node *node;
3670 struct inline_summary *info;
3671 lto_symtab_encoder_t encoder;
3672 struct bitpack_d bp;
3673 struct cgraph_edge *e;
3674 predicate p;
3676 index = streamer_read_uhwi (&ib);
3677 encoder = file_data->symtab_node_encoder;
3678 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3679 info = inline_summary (node);
3681 info->estimated_stack_size
3682 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
3683 info->size = info->self_size = streamer_read_uhwi (&ib);
3684 info->time = info->self_time = streamer_read_uhwi (&ib);
3686 bp = streamer_read_bitpack (&ib);
3687 info->inlinable = bp_unpack_value (&bp, 1);
3689 count2 = streamer_read_uhwi (&ib);
3690 gcc_assert (!info->conds);
3691 for (j = 0; j < count2; j++)
3693 struct condition c;
3694 c.operand_num = streamer_read_uhwi (&ib);
3695 c.code = (enum tree_code) streamer_read_uhwi (&ib);
3696 c.val = stream_read_tree (&ib, data_in);
3697 bp = streamer_read_bitpack (&ib);
3698 c.agg_contents = bp_unpack_value (&bp, 1);
3699 c.by_ref = bp_unpack_value (&bp, 1);
3700 if (c.agg_contents)
3701 c.offset = streamer_read_uhwi (&ib);
3702 VEC_safe_push (condition, gc, info->conds, c);
3704 count2 = streamer_read_uhwi (&ib);
3705 gcc_assert (!info->entry);
3706 for (j = 0; j < count2; j++)
3708 struct size_time_entry e;
3710 e.size = streamer_read_uhwi (&ib);
3711 e.time = streamer_read_uhwi (&ib);
3712 e.predicate = read_predicate (&ib);
3714 VEC_safe_push (size_time_entry, gc, info->entry, e);
3717 p = read_predicate (&ib);
3718 set_hint_predicate (&info->loop_iterations, p);
3719 p = read_predicate (&ib);
3720 set_hint_predicate (&info->loop_stride, p);
3721 for (e = node->callees; e; e = e->next_callee)
3722 read_inline_edge_summary (&ib, e);
3723 for (e = node->indirect_calls; e; e = e->next_callee)
3724 read_inline_edge_summary (&ib, e);
3727 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
3728 len);
3729 lto_data_in_delete (data_in);
3733 /* Read inline summary. Jump functions are shared among ipa-cp
3734 and inliner, so when ipa-cp is active, we don't need to write them
3735 twice. */
3737 void
3738 inline_read_summary (void)
3740 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3741 struct lto_file_decl_data *file_data;
3742 unsigned int j = 0;
3744 inline_summary_alloc ();
3746 while ((file_data = file_data_vec[j++]))
3748 size_t len;
3749 const char *data = lto_get_section_data (file_data,
3750 LTO_section_inline_summary,
3751 NULL, &len);
3752 if (data)
3753 inline_read_section (file_data, data, len);
3754 else
3755 /* Fatal error here. We do not want to support compiling ltrans units
3756 with different version of compiler or different flags than the WPA
3757 unit, so this should never happen. */
3758 fatal_error ("ipa inline summary is missing in input file");
3760 if (optimize)
3762 ipa_register_cgraph_hooks ();
3763 if (!flag_ipa_cp)
3764 ipa_prop_read_jump_functions ();
3766 function_insertion_hook_holder =
3767 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3771 /* Write predicate P to OB. */
3773 static void
3774 write_predicate (struct output_block *ob, struct predicate *p)
3776 int j;
3777 if (p)
3778 for (j = 0; p->clause[j]; j++)
3780 gcc_assert (j < MAX_CLAUSES);
3781 streamer_write_uhwi (ob, p->clause[j]);
3783 streamer_write_uhwi (ob, 0);
3787 /* Write inline summary for edge E to OB. */
3789 static void
3790 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
3792 struct inline_edge_summary *es = inline_edge_summary (e);
3793 int i;
3795 streamer_write_uhwi (ob, es->call_stmt_size);
3796 streamer_write_uhwi (ob, es->call_stmt_time);
3797 streamer_write_uhwi (ob, es->loop_depth);
3798 write_predicate (ob, es->predicate);
3799 streamer_write_uhwi (ob, VEC_length (inline_param_summary_t, es->param));
3800 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param); i++)
3801 streamer_write_uhwi (ob, VEC_index (inline_param_summary_t,
3802 es->param, i).change_prob);
3806 /* Write inline summary for node in SET.
3807 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
3808 active, we don't need to write them twice. */
3810 void
3811 inline_write_summary (void)
3813 struct cgraph_node *node;
3814 symtab_node snode;
3815 struct output_block *ob = create_output_block (LTO_section_inline_summary);
3816 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3817 unsigned int count = 0;
3818 int i;
3820 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3821 if (symtab_function_p (snode = lto_symtab_encoder_deref (encoder, i))
3822 && cgraph (snode)->analyzed)
3823 count++;
3824 streamer_write_uhwi (ob, count);
3826 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3828 if (symtab_function_p (snode = lto_symtab_encoder_deref (encoder, i))
3829 && (node = cgraph (snode))->analyzed)
3831 struct inline_summary *info = inline_summary (node);
3832 struct bitpack_d bp;
3833 struct cgraph_edge *edge;
3834 int i;
3835 size_time_entry *e;
3836 struct condition *c;
3838 streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, (symtab_node)node));
3839 streamer_write_hwi (ob, info->estimated_self_stack_size);
3840 streamer_write_hwi (ob, info->self_size);
3841 streamer_write_hwi (ob, info->self_time);
3842 bp = bitpack_create (ob->main_stream);
3843 bp_pack_value (&bp, info->inlinable, 1);
3844 streamer_write_bitpack (&bp);
3845 streamer_write_uhwi (ob, VEC_length (condition, info->conds));
3846 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
3848 streamer_write_uhwi (ob, c->operand_num);
3849 streamer_write_uhwi (ob, c->code);
3850 stream_write_tree (ob, c->val, true);
3851 bp = bitpack_create (ob->main_stream);
3852 bp_pack_value (&bp, c->agg_contents, 1);
3853 bp_pack_value (&bp, c->by_ref, 1);
3854 streamer_write_bitpack (&bp);
3855 if (c->agg_contents)
3856 streamer_write_uhwi (ob, c->offset);
3858 streamer_write_uhwi (ob, VEC_length (size_time_entry, info->entry));
3859 for (i = 0;
3860 VEC_iterate (size_time_entry, info->entry, i, e);
3861 i++)
3863 streamer_write_uhwi (ob, e->size);
3864 streamer_write_uhwi (ob, e->time);
3865 write_predicate (ob, &e->predicate);
3867 write_predicate (ob, info->loop_iterations);
3868 write_predicate (ob, info->loop_stride);
3869 for (edge = node->callees; edge; edge = edge->next_callee)
3870 write_inline_edge_summary (ob, edge);
3871 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
3872 write_inline_edge_summary (ob, edge);
3875 streamer_write_char_stream (ob->main_stream, 0);
3876 produce_asm (ob, NULL);
3877 destroy_output_block (ob);
3879 if (optimize && !flag_ipa_cp)
3880 ipa_prop_write_jump_functions ();
3884 /* Release inline summary. */
3886 void
3887 inline_free_summary (void)
3889 struct cgraph_node *node;
3890 if (inline_edge_summary_vec == NULL)
3891 return;
3892 FOR_EACH_DEFINED_FUNCTION (node)
3893 reset_inline_summary (node);
3894 if (function_insertion_hook_holder)
3895 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3896 function_insertion_hook_holder = NULL;
3897 if (node_removal_hook_holder)
3898 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3899 node_removal_hook_holder = NULL;
3900 if (edge_removal_hook_holder)
3901 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3902 edge_removal_hook_holder = NULL;
3903 if (node_duplication_hook_holder)
3904 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3905 node_duplication_hook_holder = NULL;
3906 if (edge_duplication_hook_holder)
3907 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3908 edge_duplication_hook_holder = NULL;
3909 VEC_free (inline_summary_t, gc, inline_summary_vec);
3910 inline_summary_vec = NULL;
3911 VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
3912 inline_edge_summary_vec = NULL;
3913 if (edge_predicate_pool)
3914 free_alloc_pool (edge_predicate_pool);
3915 edge_predicate_pool = 0;