PR c++/54955 - Fail to parse alignas expr at the beginning of a declaration
[official-gcc.git] / gcc / ipa-inline-analysis.c
blob8baaced2bd997addd0040a3053bf85b4549799f6
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 if (hints & INLINE_HINT_same_scc)
644 hints &= ~INLINE_HINT_same_scc;
645 fprintf (f, " same_scc");
647 if (hints & INLINE_HINT_in_scc)
649 hints &= ~INLINE_HINT_in_scc;
650 fprintf (f, " in_scc");
652 gcc_assert (!hints);
656 /* Record SIZE and TIME under condition PRED into the inline summary. */
658 static void
659 account_size_time (struct inline_summary *summary, int size, int time,
660 struct predicate *pred)
662 size_time_entry *e;
663 bool found = false;
664 int i;
666 if (false_predicate_p (pred))
667 return;
669 /* We need to create initial empty unconitional clause, but otherwie
670 we don't need to account empty times and sizes. */
671 if (!size && !time && summary->entry)
672 return;
674 /* Watch overflow that might result from insane profiles. */
675 if (time > MAX_TIME * INLINE_TIME_SCALE)
676 time = MAX_TIME * INLINE_TIME_SCALE;
677 gcc_assert (time >= 0);
679 for (i = 0; VEC_iterate (size_time_entry, summary->entry, i, e); i++)
680 if (predicates_equal_p (&e->predicate, pred))
682 found = true;
683 break;
685 if (i == 32)
687 i = 0;
688 found = true;
689 e = &VEC_index (size_time_entry, summary->entry, 0);
690 gcc_assert (!e->predicate.clause[0]);
692 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
694 fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
695 ((double)size) / INLINE_SIZE_SCALE,
696 ((double)time) / INLINE_TIME_SCALE,
697 found ? "" : "new ");
698 dump_predicate (dump_file, summary->conds, pred);
700 if (!found)
702 struct size_time_entry new_entry;
703 new_entry.size = size;
704 new_entry.time = time;
705 new_entry.predicate = *pred;
706 VEC_safe_push (size_time_entry, gc, summary->entry, new_entry);
708 else
710 e->size += size;
711 e->time += time;
712 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
713 e->time = MAX_TIME * INLINE_TIME_SCALE;
717 /* Set predicate for edge E. */
719 static void
720 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
722 struct inline_edge_summary *es = inline_edge_summary (e);
723 if (predicate && !true_predicate_p (predicate))
725 if (!es->predicate)
726 es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
727 *es->predicate = *predicate;
729 else
731 if (es->predicate)
732 pool_free (edge_predicate_pool, es->predicate);
733 es->predicate = NULL;
737 /* Set predicate for hint *P. */
739 static void
740 set_hint_predicate (struct predicate **p, struct predicate new_predicate)
742 if (false_predicate_p (&new_predicate)
743 || true_predicate_p (&new_predicate))
745 if (*p)
746 pool_free (edge_predicate_pool, *p);
747 *p = NULL;
749 else
751 if (!*p)
752 *p = (struct predicate *)pool_alloc (edge_predicate_pool);
753 **p = new_predicate;
758 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
759 KNOWN_AGGS is a vector of aggreggate jump functions for each parameter.
760 Return clause of possible truths. When INLINE_P is true, assume that we are
761 inlining.
763 ERROR_MARK means compile time invariant. */
765 static clause_t
766 evaluate_conditions_for_known_args (struct cgraph_node *node,
767 bool inline_p,
768 VEC (tree, heap) *known_vals,
769 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
771 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
772 struct inline_summary *info = inline_summary (node);
773 int i;
774 struct condition *c;
776 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
778 tree val;
779 tree res;
781 /* We allow call stmt to have fewer arguments than the callee function
782 (especially for K&R style programs). So bound check here (we assume
783 known_aggs vector, if non-NULL, has the same length as
784 known_vals). */
785 gcc_checking_assert (!known_aggs
786 || (VEC_length (tree, known_vals)
787 == VEC_length (ipa_agg_jump_function_p,
788 known_aggs)));
789 if (c->operand_num >= (int) VEC_length (tree, known_vals))
791 clause |= 1 << (i + predicate_first_dynamic_condition);
792 continue;
795 if (c->agg_contents)
797 struct ipa_agg_jump_function *agg;
799 if (c->code == CHANGED
800 && !c->by_ref
801 && (VEC_index (tree, known_vals, c->operand_num)
802 == error_mark_node))
803 continue;
805 if (known_aggs)
807 agg = VEC_index (ipa_agg_jump_function_p, known_aggs,
808 c->operand_num);
809 val = ipa_find_agg_cst_for_param (agg, c->offset, c->by_ref);
811 else
812 val = NULL_TREE;
814 else
816 val = VEC_index (tree, known_vals, c->operand_num);
817 if (val == error_mark_node && c->code != CHANGED)
818 val = NULL_TREE;
821 if (!val)
823 clause |= 1 << (i + predicate_first_dynamic_condition);
824 continue;
826 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
827 continue;
828 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
829 if (res
830 && integer_zerop (res))
831 continue;
832 clause |= 1 << (i + predicate_first_dynamic_condition);
834 return clause;
838 /* Work out what conditions might be true at invocation of E. */
840 static void
841 evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
842 clause_t *clause_ptr,
843 VEC (tree, heap) **known_vals_ptr,
844 VEC (tree, heap) **known_binfos_ptr,
845 VEC (ipa_agg_jump_function_p, heap) **known_aggs_ptr)
847 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
848 struct inline_summary *info = inline_summary (callee);
849 VEC (tree, heap) *known_vals = NULL;
850 VEC (ipa_agg_jump_function_p, heap) *known_aggs = NULL;
852 if (clause_ptr)
853 *clause_ptr = inline_p ? 0 : 1 << predicate_not_inlined_condition;
854 if (known_vals_ptr)
855 *known_vals_ptr = NULL;
856 if (known_binfos_ptr)
857 *known_binfos_ptr = NULL;
859 if (ipa_node_params_vector
860 && !e->call_stmt_cannot_inline_p
861 && ((clause_ptr && info->conds) || known_vals_ptr || known_binfos_ptr))
863 struct ipa_node_params *parms_info;
864 struct ipa_edge_args *args = IPA_EDGE_REF (e);
865 struct inline_edge_summary *es = inline_edge_summary (e);
866 int i, count = ipa_get_cs_argument_count (args);
868 if (e->caller->global.inlined_to)
869 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
870 else
871 parms_info = IPA_NODE_REF (e->caller);
873 if (count && (info->conds || known_vals_ptr))
874 VEC_safe_grow_cleared (tree, heap, known_vals, count);
875 if (count && (info->conds || known_aggs_ptr))
876 VEC_safe_grow_cleared (ipa_agg_jump_function_p, heap, known_aggs,
877 count);
878 if (count && known_binfos_ptr)
879 VEC_safe_grow_cleared (tree, heap, *known_binfos_ptr, count);
881 for (i = 0; i < count; i++)
883 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
884 tree cst = ipa_value_from_jfunc (parms_info, jf);
885 if (cst)
887 if (known_vals && TREE_CODE (cst) != TREE_BINFO)
888 VEC_replace (tree, known_vals, i, cst);
889 else if (known_binfos_ptr != NULL && TREE_CODE (cst) == TREE_BINFO)
890 VEC_replace (tree, *known_binfos_ptr, i, cst);
892 else if (inline_p
893 && !VEC_index (inline_param_summary_t,
894 es->param,
895 i).change_prob)
896 VEC_replace (tree, known_vals, i, error_mark_node);
897 /* TODO: When IPA-CP starts propagating and merging aggregate jump
898 functions, use its knowledge of the caller too, just like the
899 scalar case above. */
900 VEC_replace (ipa_agg_jump_function_p, known_aggs, i, &jf->agg);
904 if (clause_ptr)
905 *clause_ptr = evaluate_conditions_for_known_args (callee, inline_p,
906 known_vals, known_aggs);
908 if (known_vals_ptr)
909 *known_vals_ptr = known_vals;
910 else
911 VEC_free (tree, heap, known_vals);
913 if (known_aggs_ptr)
914 *known_aggs_ptr = known_aggs;
915 else
916 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
920 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
922 static void
923 inline_summary_alloc (void)
925 if (!node_removal_hook_holder)
926 node_removal_hook_holder =
927 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
928 if (!edge_removal_hook_holder)
929 edge_removal_hook_holder =
930 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
931 if (!node_duplication_hook_holder)
932 node_duplication_hook_holder =
933 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
934 if (!edge_duplication_hook_holder)
935 edge_duplication_hook_holder =
936 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
938 if (VEC_length (inline_summary_t, inline_summary_vec)
939 <= (unsigned) cgraph_max_uid)
940 VEC_safe_grow_cleared (inline_summary_t, gc,
941 inline_summary_vec, cgraph_max_uid + 1);
942 if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
943 <= (unsigned) cgraph_edge_max_uid)
944 VEC_safe_grow_cleared (inline_edge_summary_t, heap,
945 inline_edge_summary_vec, cgraph_edge_max_uid + 1);
946 if (!edge_predicate_pool)
947 edge_predicate_pool = create_alloc_pool ("edge predicates",
948 sizeof (struct predicate),
949 10);
952 /* We are called multiple time for given function; clear
953 data from previous run so they are not cumulated. */
955 static void
956 reset_inline_edge_summary (struct cgraph_edge *e)
958 if (e->uid
959 < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
961 struct inline_edge_summary *es = inline_edge_summary (e);
963 es->call_stmt_size = es->call_stmt_time =0;
964 if (es->predicate)
965 pool_free (edge_predicate_pool, es->predicate);
966 es->predicate = NULL;
967 VEC_free (inline_param_summary_t, heap, es->param);
971 /* We are called multiple time for given function; clear
972 data from previous run so they are not cumulated. */
974 static void
975 reset_inline_summary (struct cgraph_node *node)
977 struct inline_summary *info = inline_summary (node);
978 struct cgraph_edge *e;
980 info->self_size = info->self_time = 0;
981 info->estimated_stack_size = 0;
982 info->estimated_self_stack_size = 0;
983 info->stack_frame_offset = 0;
984 info->size = 0;
985 info->time = 0;
986 info->scc_no = 0;
987 if (info->loop_iterations)
989 pool_free (edge_predicate_pool, info->loop_iterations);
990 info->loop_iterations = NULL;
992 if (info->loop_stride)
994 pool_free (edge_predicate_pool, info->loop_stride);
995 info->loop_stride = NULL;
997 VEC_free (condition, gc, info->conds);
998 VEC_free (size_time_entry,gc, info->entry);
999 for (e = node->callees; e; e = e->next_callee)
1000 reset_inline_edge_summary (e);
1001 for (e = node->indirect_calls; e; e = e->next_callee)
1002 reset_inline_edge_summary (e);
1005 /* Hook that is called by cgraph.c when a node is removed. */
1007 static void
1008 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1010 struct inline_summary *info;
1011 if (VEC_length (inline_summary_t, inline_summary_vec)
1012 <= (unsigned)node->uid)
1013 return;
1014 info = inline_summary (node);
1015 reset_inline_summary (node);
1016 memset (info, 0, sizeof (inline_summary_t));
1019 /* Remap predicate P of former function to be predicate of duplicated functoin.
1020 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
1021 INFO is inline summary of the duplicated node. */
1023 static struct predicate
1024 remap_predicate_after_duplication (struct predicate *p,
1025 clause_t possible_truths,
1026 struct inline_summary *info)
1028 struct predicate new_predicate = true_predicate ();
1029 int j;
1030 for (j = 0; p->clause[j]; j++)
1031 if (!(possible_truths & p->clause[j]))
1033 new_predicate = false_predicate ();
1034 break;
1036 else
1037 add_clause (info->conds, &new_predicate,
1038 possible_truths & p->clause[j]);
1039 return new_predicate;
1042 /* Same as remap_predicate_after_duplication but handle hint predicate *P.
1043 Additionally care about allocating new memory slot for updated predicate
1044 and set it to NULL when it becomes true or false (and thus uninteresting).
1047 static void
1048 remap_hint_predicate_after_duplication (struct predicate **p,
1049 clause_t possible_truths,
1050 struct inline_summary *info)
1052 struct predicate new_predicate;
1054 if (!*p)
1055 return;
1057 new_predicate = remap_predicate_after_duplication (*p,
1058 possible_truths,
1059 info);
1060 /* We do not want to free previous predicate; it is used by node origin. */
1061 *p = NULL;
1062 set_hint_predicate (p, new_predicate);
1066 /* Hook that is called by cgraph.c when a node is duplicated. */
1068 static void
1069 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1070 ATTRIBUTE_UNUSED void *data)
1072 struct inline_summary *info;
1073 inline_summary_alloc ();
1074 info = inline_summary (dst);
1075 memcpy (info, inline_summary (src),
1076 sizeof (struct inline_summary));
1077 /* TODO: as an optimization, we may avoid copying conditions
1078 that are known to be false or true. */
1079 info->conds = VEC_copy (condition, gc, info->conds);
1081 /* When there are any replacements in the function body, see if we can figure
1082 out that something was optimized out. */
1083 if (ipa_node_params_vector && dst->clone.tree_map)
1085 VEC(size_time_entry,gc) *entry = info->entry;
1086 /* Use SRC parm info since it may not be copied yet. */
1087 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
1088 VEC (tree, heap) *known_vals = NULL;
1089 int count = ipa_get_param_count (parms_info);
1090 int i,j;
1091 clause_t possible_truths;
1092 struct predicate true_pred = true_predicate ();
1093 size_time_entry *e;
1094 int optimized_out_size = 0;
1095 bool inlined_to_p = false;
1096 struct cgraph_edge *edge;
1098 info->entry = 0;
1099 VEC_safe_grow_cleared (tree, heap, known_vals, count);
1100 for (i = 0; i < count; i++)
1102 tree t = ipa_get_param (parms_info, i);
1103 struct ipa_replace_map *r;
1105 for (j = 0;
1106 VEC_iterate (ipa_replace_map_p, dst->clone.tree_map, j, r);
1107 j++)
1109 if (r->old_tree == t
1110 && r->replace_p
1111 && !r->ref_p)
1113 VEC_replace (tree, known_vals, i, r->new_tree);
1114 break;
1118 possible_truths = evaluate_conditions_for_known_args (dst, false,
1119 known_vals, NULL);
1120 VEC_free (tree, heap, known_vals);
1122 account_size_time (info, 0, 0, &true_pred);
1124 /* Remap size_time vectors.
1125 Simplify the predicate by prunning out alternatives that are known
1126 to be false.
1127 TODO: as on optimization, we can also eliminate conditions known
1128 to be true. */
1129 for (i = 0; VEC_iterate (size_time_entry, entry, i, e); i++)
1131 struct predicate new_predicate;
1132 new_predicate = remap_predicate_after_duplication (&e->predicate,
1133 possible_truths,
1134 info);
1135 if (false_predicate_p (&new_predicate))
1136 optimized_out_size += e->size;
1137 else
1138 account_size_time (info, e->size, e->time, &new_predicate);
1141 /* Remap edge predicates with the same simplification as above.
1142 Also copy constantness arrays. */
1143 for (edge = dst->callees; edge; edge = edge->next_callee)
1145 struct predicate new_predicate;
1146 struct inline_edge_summary *es = inline_edge_summary (edge);
1148 if (!edge->inline_failed)
1149 inlined_to_p = true;
1150 if (!es->predicate)
1151 continue;
1152 new_predicate = remap_predicate_after_duplication (es->predicate,
1153 possible_truths,
1154 info);
1155 if (false_predicate_p (&new_predicate)
1156 && !false_predicate_p (es->predicate))
1158 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1159 edge->frequency = 0;
1161 edge_set_predicate (edge, &new_predicate);
1164 /* Remap indirect edge predicates with the same simplificaiton as above.
1165 Also copy constantness arrays. */
1166 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
1168 struct predicate new_predicate;
1169 struct inline_edge_summary *es = inline_edge_summary (edge);
1171 gcc_checking_assert (edge->inline_failed);
1172 if (!es->predicate)
1173 continue;
1174 new_predicate = remap_predicate_after_duplication (es->predicate,
1175 possible_truths,
1176 info);
1177 if (false_predicate_p (&new_predicate)
1178 && !false_predicate_p (es->predicate))
1180 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
1181 edge->frequency = 0;
1183 edge_set_predicate (edge, &new_predicate);
1185 remap_hint_predicate_after_duplication (&info->loop_iterations,
1186 possible_truths,
1187 info);
1188 remap_hint_predicate_after_duplication (&info->loop_stride,
1189 possible_truths,
1190 info);
1192 /* If inliner or someone after inliner will ever start producing
1193 non-trivial clones, we will get trouble with lack of information
1194 about updating self sizes, because size vectors already contains
1195 sizes of the calees. */
1196 gcc_assert (!inlined_to_p
1197 || !optimized_out_size);
1199 else
1201 info->entry = VEC_copy (size_time_entry, gc, info->entry);
1202 if (info->loop_iterations)
1204 predicate p = *info->loop_iterations;
1205 info->loop_iterations = NULL;
1206 set_hint_predicate (&info->loop_iterations, p);
1208 if (info->loop_stride)
1210 predicate p = *info->loop_stride;
1211 info->loop_stride = NULL;
1212 set_hint_predicate (&info->loop_stride, p);
1215 inline_update_overall_summary (dst);
1219 /* Hook that is called by cgraph.c when a node is duplicated. */
1221 static void
1222 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1223 ATTRIBUTE_UNUSED void *data)
1225 struct inline_edge_summary *info;
1226 struct inline_edge_summary *srcinfo;
1227 inline_summary_alloc ();
1228 info = inline_edge_summary (dst);
1229 srcinfo = inline_edge_summary (src);
1230 memcpy (info, srcinfo,
1231 sizeof (struct inline_edge_summary));
1232 info->predicate = NULL;
1233 edge_set_predicate (dst, srcinfo->predicate);
1234 info->param = VEC_copy (inline_param_summary_t, heap, srcinfo->param);
1238 /* Keep edge cache consistent across edge removal. */
1240 static void
1241 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
1243 if (edge_growth_cache)
1244 reset_edge_growth_cache (edge);
1245 reset_inline_edge_summary (edge);
1249 /* Initialize growth caches. */
1251 void
1252 initialize_growth_caches (void)
1254 if (cgraph_edge_max_uid)
1255 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
1256 cgraph_edge_max_uid);
1257 if (cgraph_max_uid)
1258 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
1262 /* Free growth caches. */
1264 void
1265 free_growth_caches (void)
1267 VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
1268 edge_growth_cache = 0;
1269 VEC_free (int, heap, node_growth_cache);
1270 node_growth_cache = 0;
1274 /* Dump edge summaries associated to NODE and recursively to all clones.
1275 Indent by INDENT. */
1277 static void
1278 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
1279 struct inline_summary *info)
1281 struct cgraph_edge *edge;
1282 for (edge = node->callees; edge; edge = edge->next_callee)
1284 struct inline_edge_summary *es = inline_edge_summary (edge);
1285 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1286 int i;
1288 fprintf (f, "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i time: %2i callee size:%2i stack:%2i",
1289 indent, "", cgraph_node_name (callee),
1290 callee->uid,
1291 !edge->inline_failed ? "inlined"
1292 : cgraph_inline_failed_string (edge->inline_failed),
1293 indent, "",
1294 es->loop_depth,
1295 edge->frequency,
1296 es->call_stmt_size,
1297 es->call_stmt_time,
1298 (int)inline_summary (callee)->size / INLINE_SIZE_SCALE,
1299 (int)inline_summary (callee)->estimated_stack_size);
1301 if (es->predicate)
1303 fprintf (f, " predicate: ");
1304 dump_predicate (f, info->conds, es->predicate);
1306 else
1307 fprintf (f, "\n");
1308 if (es->param)
1309 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param);
1310 i++)
1312 int prob = VEC_index (inline_param_summary_t,
1313 es->param, i).change_prob;
1315 if (!prob)
1316 fprintf (f, "%*s op%i is compile time invariant\n",
1317 indent + 2, "", i);
1318 else if (prob != REG_BR_PROB_BASE)
1319 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1320 prob * 100.0 / REG_BR_PROB_BASE);
1322 if (!edge->inline_failed)
1324 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1325 " callee size %i\n",
1326 indent+2, "",
1327 (int)inline_summary (callee)->stack_frame_offset,
1328 (int)inline_summary (callee)->estimated_self_stack_size,
1329 (int)inline_summary (callee)->estimated_stack_size);
1330 dump_inline_edge_summary (f, indent+2, callee, info);
1333 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1335 struct inline_edge_summary *es = inline_edge_summary (edge);
1336 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1337 " time: %2i",
1338 indent, "",
1339 es->loop_depth,
1340 edge->frequency,
1341 es->call_stmt_size,
1342 es->call_stmt_time);
1343 if (es->predicate)
1345 fprintf (f, "predicate: ");
1346 dump_predicate (f, info->conds, es->predicate);
1348 else
1349 fprintf (f, "\n");
1354 void
1355 dump_inline_summary (FILE * f, struct cgraph_node *node)
1357 if (node->analyzed)
1359 struct inline_summary *s = inline_summary (node);
1360 size_time_entry *e;
1361 int i;
1362 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
1363 node->uid);
1364 if (DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl))
1365 fprintf (f, " always_inline");
1366 if (s->inlinable)
1367 fprintf (f, " inlinable");
1368 fprintf (f, "\n self time: %i\n",
1369 s->self_time);
1370 fprintf (f, " global time: %i\n", s->time);
1371 fprintf (f, " self size: %i\n",
1372 s->self_size);
1373 fprintf (f, " global size: %i\n", s->size);
1374 fprintf (f, " self stack: %i\n",
1375 (int) s->estimated_self_stack_size);
1376 fprintf (f, " global stack: %i\n",
1377 (int) s->estimated_stack_size);
1378 if (s->scc_no)
1379 fprintf (f, " In SCC: %i\n",
1380 (int) s->scc_no);
1381 for (i = 0;
1382 VEC_iterate (size_time_entry, s->entry, i, e);
1383 i++)
1385 fprintf (f, " size:%f, time:%f, predicate:",
1386 (double) e->size / INLINE_SIZE_SCALE,
1387 (double) e->time / INLINE_TIME_SCALE);
1388 dump_predicate (f, s->conds, &e->predicate);
1390 if (s->loop_iterations)
1392 fprintf (f, " loop iterations:");
1393 dump_predicate (f, s->conds, s->loop_iterations);
1395 if (s->loop_stride)
1397 fprintf (f, " loop stride:");
1398 dump_predicate (f, s->conds, s->loop_stride);
1400 fprintf (f, " calls:\n");
1401 dump_inline_edge_summary (f, 4, node, s);
1402 fprintf (f, "\n");
1406 DEBUG_FUNCTION void
1407 debug_inline_summary (struct cgraph_node *node)
1409 dump_inline_summary (stderr, node);
1412 void
1413 dump_inline_summaries (FILE *f)
1415 struct cgraph_node *node;
1417 FOR_EACH_DEFINED_FUNCTION (node)
1418 if (!node->global.inlined_to)
1419 dump_inline_summary (f, node);
1422 /* Give initial reasons why inlining would fail on EDGE. This gets either
1423 nullified or usually overwritten by more precise reasons later. */
1425 void
1426 initialize_inline_failed (struct cgraph_edge *e)
1428 struct cgraph_node *callee = e->callee;
1430 if (e->indirect_unknown_callee)
1431 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1432 else if (!callee->analyzed)
1433 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1434 else if (callee->local.redefined_extern_inline)
1435 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1436 else if (e->call_stmt_cannot_inline_p)
1437 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1438 else
1439 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1442 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1443 boolean variable pointed to by DATA. */
1445 static bool
1446 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1447 void *data)
1449 bool *b = (bool *) data;
1450 *b = true;
1451 return true;
1454 /* If OP refers to value of function parameter, return the corresponding
1455 parameter. */
1457 static tree
1458 unmodified_parm_1 (gimple stmt, tree op)
1460 /* SSA_NAME referring to parm default def? */
1461 if (TREE_CODE (op) == SSA_NAME
1462 && SSA_NAME_IS_DEFAULT_DEF (op)
1463 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1464 return SSA_NAME_VAR (op);
1465 /* Non-SSA parm reference? */
1466 if (TREE_CODE (op) == PARM_DECL)
1468 bool modified = false;
1470 ao_ref refd;
1471 ao_ref_init (&refd, op);
1472 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1473 NULL);
1474 if (!modified)
1475 return op;
1477 return NULL_TREE;
1480 /* If OP refers to value of function parameter, return the corresponding
1481 parameter. Also traverse chains of SSA register assignments. */
1483 static tree
1484 unmodified_parm (gimple stmt, tree op)
1486 tree res = unmodified_parm_1 (stmt, op);
1487 if (res)
1488 return res;
1490 if (TREE_CODE (op) == SSA_NAME
1491 && !SSA_NAME_IS_DEFAULT_DEF (op)
1492 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1493 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1494 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1495 return NULL_TREE;
1498 /* If OP refers to a value of a function parameter or value loaded from an
1499 aggregate passed to a parameter (either by value or reference), return TRUE
1500 and store the number of the parameter to *INDEX_P and information whether
1501 and how it has been loaded from an aggregate into *AGGPOS. INFO describes
1502 the function parameters, STMT is the statement in which OP is used or
1503 loaded. */
1505 static bool
1506 unmodified_parm_or_parm_agg_item (struct ipa_node_params *info,
1507 gimple stmt, tree op, int *index_p,
1508 struct agg_position_info *aggpos)
1510 tree res = unmodified_parm_1 (stmt, op);
1512 gcc_checking_assert (aggpos);
1513 if (res)
1515 *index_p = ipa_get_param_decl_index (info, res);
1516 if (*index_p < 0)
1517 return false;
1518 aggpos->agg_contents = false;
1519 aggpos->by_ref = false;
1520 return true;
1523 if (TREE_CODE (op) == SSA_NAME)
1525 if (SSA_NAME_IS_DEFAULT_DEF (op)
1526 || !gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1527 return false;
1528 stmt = SSA_NAME_DEF_STMT (op);
1529 op = gimple_assign_rhs1 (stmt);
1530 if (!REFERENCE_CLASS_P (op))
1531 return unmodified_parm_or_parm_agg_item (info, stmt, op, index_p,
1532 aggpos);
1535 aggpos->agg_contents = true;
1536 return ipa_load_from_parm_agg (info, stmt, op, index_p, &aggpos->offset,
1537 &aggpos->by_ref);
1540 /* See if statement might disappear after inlining.
1541 0 - means not eliminated
1542 1 - half of statements goes away
1543 2 - for sure it is eliminated.
1544 We are not terribly sophisticated, basically looking for simple abstraction
1545 penalty wrappers. */
1547 static int
1548 eliminated_by_inlining_prob (gimple stmt)
1550 enum gimple_code code = gimple_code (stmt);
1551 enum tree_code rhs_code;
1553 if (!optimize)
1554 return 0;
1556 switch (code)
1558 case GIMPLE_RETURN:
1559 return 2;
1560 case GIMPLE_ASSIGN:
1561 if (gimple_num_ops (stmt) != 2)
1562 return 0;
1564 rhs_code = gimple_assign_rhs_code (stmt);
1566 /* Casts of parameters, loads from parameters passed by reference
1567 and stores to return value or parameters are often free after
1568 inlining dua to SRA and further combining.
1569 Assume that half of statements goes away. */
1570 if (rhs_code == CONVERT_EXPR
1571 || rhs_code == NOP_EXPR
1572 || rhs_code == VIEW_CONVERT_EXPR
1573 || rhs_code == ADDR_EXPR
1574 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1576 tree rhs = gimple_assign_rhs1 (stmt);
1577 tree lhs = gimple_assign_lhs (stmt);
1578 tree inner_rhs = get_base_address (rhs);
1579 tree inner_lhs = get_base_address (lhs);
1580 bool rhs_free = false;
1581 bool lhs_free = false;
1583 if (!inner_rhs)
1584 inner_rhs = rhs;
1585 if (!inner_lhs)
1586 inner_lhs = lhs;
1588 /* Reads of parameter are expected to be free. */
1589 if (unmodified_parm (stmt, inner_rhs))
1590 rhs_free = true;
1591 /* Match expressions of form &this->field. Those will most likely
1592 combine with something upstream after inlining. */
1593 else if (TREE_CODE (inner_rhs) == ADDR_EXPR)
1595 tree op = get_base_address (TREE_OPERAND (inner_rhs, 0));
1596 if (TREE_CODE (op) == PARM_DECL)
1597 rhs_free = true;
1598 else if (TREE_CODE (op) == MEM_REF
1599 && unmodified_parm (stmt, TREE_OPERAND (op, 0)))
1600 rhs_free = true;
1603 /* When parameter is not SSA register because its address is taken
1604 and it is just copied into one, the statement will be completely
1605 free after inlining (we will copy propagate backward). */
1606 if (rhs_free && is_gimple_reg (lhs))
1607 return 2;
1609 /* Reads of parameters passed by reference
1610 expected to be free (i.e. optimized out after inlining). */
1611 if (TREE_CODE(inner_rhs) == MEM_REF
1612 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1613 rhs_free = true;
1615 /* Copying parameter passed by reference into gimple register is
1616 probably also going to copy propagate, but we can't be quite
1617 sure. */
1618 if (rhs_free && is_gimple_reg (lhs))
1619 lhs_free = true;
1621 /* Writes to parameters, parameters passed by value and return value
1622 (either dirrectly or passed via invisible reference) are free.
1624 TODO: We ought to handle testcase like
1625 struct a {int a,b;};
1626 struct a
1627 retrurnsturct (void)
1629 struct a a ={1,2};
1630 return a;
1633 This translate into:
1635 retrurnsturct ()
1637 int a$b;
1638 int a$a;
1639 struct a a;
1640 struct a D.2739;
1642 <bb 2>:
1643 D.2739.a = 1;
1644 D.2739.b = 2;
1645 return D.2739;
1648 For that we either need to copy ipa-split logic detecting writes
1649 to return value. */
1650 if (TREE_CODE (inner_lhs) == PARM_DECL
1651 || TREE_CODE (inner_lhs) == RESULT_DECL
1652 || (TREE_CODE(inner_lhs) == MEM_REF
1653 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1654 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1655 && SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0))
1656 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND
1657 (inner_lhs, 0))) == RESULT_DECL))))
1658 lhs_free = true;
1659 if (lhs_free
1660 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1661 rhs_free = true;
1662 if (lhs_free && rhs_free)
1663 return 1;
1665 return 0;
1666 default:
1667 return 0;
1672 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1673 predicates to the CFG edges. */
1675 static void
1676 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1677 struct inline_summary *summary,
1678 basic_block bb)
1680 gimple last;
1681 tree op;
1682 int index;
1683 struct agg_position_info aggpos;
1684 enum tree_code code, inverted_code;
1685 edge e;
1686 edge_iterator ei;
1687 gimple set_stmt;
1688 tree op2;
1690 last = last_stmt (bb);
1691 if (!last
1692 || gimple_code (last) != GIMPLE_COND)
1693 return;
1694 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1695 return;
1696 op = gimple_cond_lhs (last);
1697 /* TODO: handle conditionals like
1698 var = op0 < 4;
1699 if (var != 0). */
1700 if (unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1702 code = gimple_cond_code (last);
1703 inverted_code
1704 = invert_tree_comparison (code,
1705 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1707 FOR_EACH_EDGE (e, ei, bb->succs)
1709 struct predicate p = add_condition (summary, index, &aggpos,
1710 e->flags & EDGE_TRUE_VALUE
1711 ? code : inverted_code,
1712 gimple_cond_rhs (last));
1713 e->aux = pool_alloc (edge_predicate_pool);
1714 *(struct predicate *)e->aux = p;
1718 if (TREE_CODE (op) != SSA_NAME)
1719 return;
1720 /* Special case
1721 if (builtin_constant_p (op))
1722 constant_code
1723 else
1724 nonconstant_code.
1725 Here we can predicate nonconstant_code. We can't
1726 really handle constant_code since we have no predicate
1727 for this and also the constant code is not known to be
1728 optimized away when inliner doen't see operand is constant.
1729 Other optimizers might think otherwise. */
1730 if (gimple_cond_code (last) != NE_EXPR
1731 || !integer_zerop (gimple_cond_rhs (last)))
1732 return;
1733 set_stmt = SSA_NAME_DEF_STMT (op);
1734 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1735 || gimple_call_num_args (set_stmt) != 1)
1736 return;
1737 op2 = gimple_call_arg (set_stmt, 0);
1738 if (!unmodified_parm_or_parm_agg_item (info, set_stmt, op2, &index, &aggpos))
1739 return;
1740 FOR_EACH_EDGE (e, ei, bb->succs)
1741 if (e->flags & EDGE_FALSE_VALUE)
1743 struct predicate p = add_condition (summary, index, &aggpos,
1744 IS_NOT_CONSTANT, NULL_TREE);
1745 e->aux = pool_alloc (edge_predicate_pool);
1746 *(struct predicate *)e->aux = p;
1751 /* If BB ends by a switch we can turn into predicates, attach corresponding
1752 predicates to the CFG edges. */
1754 static void
1755 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1756 struct inline_summary *summary,
1757 basic_block bb)
1759 gimple last;
1760 tree op;
1761 int index;
1762 struct agg_position_info aggpos;
1763 edge e;
1764 edge_iterator ei;
1765 size_t n;
1766 size_t case_idx;
1768 last = last_stmt (bb);
1769 if (!last
1770 || gimple_code (last) != GIMPLE_SWITCH)
1771 return;
1772 op = gimple_switch_index (last);
1773 if (!unmodified_parm_or_parm_agg_item (info, last, op, &index, &aggpos))
1774 return;
1776 FOR_EACH_EDGE (e, ei, bb->succs)
1778 e->aux = pool_alloc (edge_predicate_pool);
1779 *(struct predicate *)e->aux = false_predicate ();
1781 n = gimple_switch_num_labels(last);
1782 for (case_idx = 0; case_idx < n; ++case_idx)
1784 tree cl = gimple_switch_label (last, case_idx);
1785 tree min, max;
1786 struct predicate p;
1788 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1789 min = CASE_LOW (cl);
1790 max = CASE_HIGH (cl);
1792 /* For default we might want to construct predicate that none
1793 of cases is met, but it is bit hard to do not having negations
1794 of conditionals handy. */
1795 if (!min && !max)
1796 p = true_predicate ();
1797 else if (!max)
1798 p = add_condition (summary, index, &aggpos, EQ_EXPR, min);
1799 else
1801 struct predicate p1, p2;
1802 p1 = add_condition (summary, index, &aggpos, GE_EXPR, min);
1803 p2 = add_condition (summary, index, &aggpos, LE_EXPR, max);
1804 p = and_predicates (summary->conds, &p1, &p2);
1806 *(struct predicate *)e->aux
1807 = or_predicates (summary->conds, &p, (struct predicate *)e->aux);
1812 /* For each BB in NODE attach to its AUX pointer predicate under
1813 which it is executable. */
1815 static void
1816 compute_bb_predicates (struct cgraph_node *node,
1817 struct ipa_node_params *parms_info,
1818 struct inline_summary *summary)
1820 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
1821 bool done = false;
1822 basic_block bb;
1824 FOR_EACH_BB_FN (bb, my_function)
1826 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1827 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1830 /* Entry block is always executable. */
1831 ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1832 = pool_alloc (edge_predicate_pool);
1833 *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1834 = true_predicate ();
1836 /* A simple dataflow propagation of predicates forward in the CFG.
1837 TODO: work in reverse postorder. */
1838 while (!done)
1840 done = true;
1841 FOR_EACH_BB_FN (bb, my_function)
1843 struct predicate p = false_predicate ();
1844 edge e;
1845 edge_iterator ei;
1846 FOR_EACH_EDGE (e, ei, bb->preds)
1848 if (e->src->aux)
1850 struct predicate this_bb_predicate
1851 = *(struct predicate *)e->src->aux;
1852 if (e->aux)
1853 this_bb_predicate
1854 = and_predicates (summary->conds, &this_bb_predicate,
1855 (struct predicate *)e->aux);
1856 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1857 if (true_predicate_p (&p))
1858 break;
1861 if (false_predicate_p (&p))
1862 gcc_assert (!bb->aux);
1863 else
1865 if (!bb->aux)
1867 done = false;
1868 bb->aux = pool_alloc (edge_predicate_pool);
1869 *((struct predicate *)bb->aux) = p;
1871 else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1873 done = false;
1874 *((struct predicate *)bb->aux) = p;
1882 /* We keep info about constantness of SSA names. */
1884 typedef struct predicate predicate_t;
1885 DEF_VEC_O (predicate_t);
1886 DEF_VEC_ALLOC_O (predicate_t, heap);
1887 /* Return predicate specifying when the STMT might have result that is not
1888 a compile time constant. */
1890 static struct predicate
1891 will_be_nonconstant_expr_predicate (struct ipa_node_params *info,
1892 struct inline_summary *summary,
1893 tree expr,
1894 VEC (predicate_t, heap) *nonconstant_names)
1896 tree parm;
1897 int index;
1899 while (UNARY_CLASS_P (expr))
1900 expr = TREE_OPERAND (expr, 0);
1902 parm = unmodified_parm (NULL, expr);
1903 if (parm
1904 && (index = ipa_get_param_decl_index (info, parm)) >= 0)
1905 return add_condition (summary, index, NULL, CHANGED, NULL_TREE);
1906 if (is_gimple_min_invariant (expr))
1907 return false_predicate ();
1908 if (TREE_CODE (expr) == SSA_NAME)
1909 return VEC_index (predicate_t, nonconstant_names,
1910 SSA_NAME_VERSION (expr));
1911 if (BINARY_CLASS_P (expr)
1912 || COMPARISON_CLASS_P (expr))
1914 struct predicate p1 = will_be_nonconstant_expr_predicate
1915 (info, summary, TREE_OPERAND (expr, 0),
1916 nonconstant_names);
1917 struct predicate p2;
1918 if (true_predicate_p (&p1))
1919 return p1;
1920 p2 = will_be_nonconstant_expr_predicate (info, summary,
1921 TREE_OPERAND (expr, 1),
1922 nonconstant_names);
1923 return or_predicates (summary->conds, &p1, &p2);
1925 else if (TREE_CODE (expr) == COND_EXPR)
1927 struct predicate p1 = will_be_nonconstant_expr_predicate
1928 (info, summary, TREE_OPERAND (expr, 0),
1929 nonconstant_names);
1930 struct predicate p2;
1931 if (true_predicate_p (&p1))
1932 return p1;
1933 p2 = will_be_nonconstant_expr_predicate (info, summary,
1934 TREE_OPERAND (expr, 1),
1935 nonconstant_names);
1936 if (true_predicate_p (&p2))
1937 return p2;
1938 p1 = or_predicates (summary->conds, &p1, &p2);
1939 p2 = will_be_nonconstant_expr_predicate (info, summary,
1940 TREE_OPERAND (expr, 2),
1941 nonconstant_names);
1942 return or_predicates (summary->conds, &p1, &p2);
1944 else
1946 debug_tree (expr);
1947 gcc_unreachable ();
1949 return false_predicate ();
1953 /* Return predicate specifying when the STMT might have result that is not
1954 a compile time constant. */
1956 static struct predicate
1957 will_be_nonconstant_predicate (struct ipa_node_params *info,
1958 struct inline_summary *summary,
1959 gimple stmt,
1960 VEC (predicate_t, heap) *nonconstant_names)
1962 struct predicate p = true_predicate ();
1963 ssa_op_iter iter;
1964 tree use;
1965 struct predicate op_non_const;
1966 bool is_load;
1967 int base_index;
1968 struct agg_position_info aggpos;
1970 /* What statments might be optimized away
1971 when their arguments are constant
1972 TODO: also trivial builtins.
1973 builtin_constant_p is already handled later. */
1974 if (gimple_code (stmt) != GIMPLE_ASSIGN
1975 && gimple_code (stmt) != GIMPLE_COND
1976 && gimple_code (stmt) != GIMPLE_SWITCH)
1977 return p;
1979 /* Stores will stay anyway. */
1980 if (gimple_vdef (stmt))
1981 return p;
1983 is_load = gimple_vuse (stmt) != NULL;
1984 /* Loads can be optimized when the value is known. */
1985 if (is_load)
1987 tree op;
1988 gcc_assert (gimple_assign_single_p (stmt));
1989 op = gimple_assign_rhs1 (stmt);
1990 if (!unmodified_parm_or_parm_agg_item (info, stmt, op, &base_index,
1991 &aggpos))
1992 return p;
1994 else
1995 base_index = -1;
1997 /* See if we understand all operands before we start
1998 adding conditionals. */
1999 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2001 tree parm = unmodified_parm (stmt, use);
2002 /* For arguments we can build a condition. */
2003 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
2004 continue;
2005 if (TREE_CODE (use) != SSA_NAME)
2006 return p;
2007 /* If we know when operand is constant,
2008 we still can say something useful. */
2009 if (!true_predicate_p (&VEC_index (predicate_t, nonconstant_names,
2010 SSA_NAME_VERSION (use))))
2011 continue;
2012 return p;
2015 if (is_load)
2016 op_non_const = add_condition (summary, base_index, &aggpos, CHANGED, NULL);
2017 else
2018 op_non_const = false_predicate ();
2019 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2021 tree parm = unmodified_parm (stmt, use);
2022 int index;
2024 if (parm
2025 && (index = ipa_get_param_decl_index (info, parm)) >= 0)
2027 if (index != base_index)
2028 p = add_condition (summary, index, NULL, CHANGED, NULL_TREE);
2029 else
2030 continue;
2032 else
2033 p = VEC_index (predicate_t, nonconstant_names,
2034 SSA_NAME_VERSION (use));
2035 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
2037 if (gimple_code (stmt) == GIMPLE_ASSIGN
2038 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
2039 VEC_replace (predicate_t, nonconstant_names,
2040 SSA_NAME_VERSION (gimple_assign_lhs (stmt)), op_non_const);
2041 return op_non_const;
2044 struct record_modified_bb_info
2046 bitmap bb_set;
2047 gimple stmt;
2050 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
2051 set except for info->stmt. */
2053 static bool
2054 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef,
2055 void *data)
2057 struct record_modified_bb_info *info = (struct record_modified_bb_info *) data;
2058 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
2059 return false;
2060 bitmap_set_bit (info->bb_set,
2061 SSA_NAME_IS_DEFAULT_DEF (vdef)
2062 ? ENTRY_BLOCK_PTR->index : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
2063 return false;
2066 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
2067 will change since last invocation of STMT.
2069 Value 0 is reserved for compile time invariants.
2070 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
2071 ought to be REG_BR_PROB_BASE / estimated_iters. */
2073 static int
2074 param_change_prob (gimple stmt, int i)
2076 tree op = gimple_call_arg (stmt, i);
2077 basic_block bb = gimple_bb (stmt);
2078 tree base;
2080 if (is_gimple_min_invariant (op))
2081 return 0;
2082 /* We would have to do non-trivial analysis to really work out what
2083 is the probability of value to change (i.e. when init statement
2084 is in a sibling loop of the call).
2086 We do an conservative estimate: when call is executed N times more often
2087 than the statement defining value, we take the frequency 1/N. */
2088 if (TREE_CODE (op) == SSA_NAME)
2090 int init_freq;
2092 if (!bb->frequency)
2093 return REG_BR_PROB_BASE;
2095 if (SSA_NAME_IS_DEFAULT_DEF (op))
2096 init_freq = ENTRY_BLOCK_PTR->frequency;
2097 else
2098 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
2100 if (!init_freq)
2101 init_freq = 1;
2102 if (init_freq < bb->frequency)
2103 return MAX ((init_freq * REG_BR_PROB_BASE +
2104 bb->frequency / 2) / bb->frequency, 1);
2105 else
2106 return REG_BR_PROB_BASE;
2109 base = get_base_address (op);
2110 if (base)
2112 ao_ref refd;
2113 int max;
2114 struct record_modified_bb_info info;
2115 bitmap_iterator bi;
2116 unsigned index;
2118 if (const_value_known_p (base))
2119 return 0;
2120 if (!bb->frequency)
2121 return REG_BR_PROB_BASE;
2122 ao_ref_init (&refd, op);
2123 info.stmt = stmt;
2124 info.bb_set = BITMAP_ALLOC (NULL);
2125 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
2126 NULL);
2127 if (bitmap_bit_p (info.bb_set, bb->index))
2129 BITMAP_FREE (info.bb_set);
2130 return REG_BR_PROB_BASE;
2133 /* Assume that every memory is initialized at entry.
2134 TODO: Can we easilly determine if value is always defined
2135 and thus we may skip entry block? */
2136 if (ENTRY_BLOCK_PTR->frequency)
2137 max = ENTRY_BLOCK_PTR->frequency;
2138 else
2139 max = 1;
2141 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
2142 max = MIN (max, BASIC_BLOCK (index)->frequency);
2144 BITMAP_FREE (info.bb_set);
2145 if (max < bb->frequency)
2146 return MAX ((max * REG_BR_PROB_BASE +
2147 bb->frequency / 2) / bb->frequency, 1);
2148 else
2149 return REG_BR_PROB_BASE;
2151 return REG_BR_PROB_BASE;
2154 /* Find whether a basic block BB is the final block of a (half) diamond CFG
2155 sub-graph and if the predicate the condition depends on is known. If so,
2156 return true and store the pointer the predicate in *P. */
2158 static bool
2159 phi_result_unknown_predicate (struct ipa_node_params *info,
2160 struct inline_summary *summary, basic_block bb,
2161 struct predicate *p,
2162 VEC (predicate_t, heap) *nonconstant_names)
2164 edge e;
2165 edge_iterator ei;
2166 basic_block first_bb = NULL;
2167 gimple stmt;
2169 if (single_pred_p (bb))
2171 *p = false_predicate ();
2172 return true;
2175 FOR_EACH_EDGE (e, ei, bb->preds)
2177 if (single_succ_p (e->src))
2179 if (!single_pred_p (e->src))
2180 return false;
2181 if (!first_bb)
2182 first_bb = single_pred (e->src);
2183 else if (single_pred (e->src) != first_bb)
2184 return false;
2186 else
2188 if (!first_bb)
2189 first_bb = e->src;
2190 else if (e->src != first_bb)
2191 return false;
2195 if (!first_bb)
2196 return false;
2198 stmt = last_stmt (first_bb);
2199 if (!stmt
2200 || gimple_code (stmt) != GIMPLE_COND
2201 || !is_gimple_ip_invariant (gimple_cond_rhs (stmt)))
2202 return false;
2204 *p = will_be_nonconstant_expr_predicate (info, summary,
2205 gimple_cond_lhs (stmt),
2206 nonconstant_names);
2207 if (true_predicate_p (p))
2208 return false;
2209 else
2210 return true;
2213 /* Given a PHI statement in a function described by inline properties SUMMARY
2214 and *P being the predicate describing whether the selected PHI argument is
2215 known, store a predicate for the result of the PHI statement into
2216 NONCONSTANT_NAMES, if possible. */
2218 static void
2219 predicate_for_phi_result (struct inline_summary *summary, gimple phi,
2220 struct predicate *p,
2221 VEC (predicate_t, heap) *nonconstant_names)
2223 unsigned i;
2225 for (i = 0; i < gimple_phi_num_args (phi); i++)
2227 tree arg = gimple_phi_arg (phi, i)->def;
2228 if (!is_gimple_min_invariant (arg))
2230 gcc_assert (TREE_CODE (arg) == SSA_NAME);
2231 *p = or_predicates (summary->conds, p,
2232 &VEC_index (predicate_t, nonconstant_names,
2233 SSA_NAME_VERSION (arg)));
2234 if (true_predicate_p (p))
2235 return;
2239 if (dump_file && (dump_flags & TDF_DETAILS))
2241 fprintf (dump_file, "\t\tphi predicate: ");
2242 dump_predicate (dump_file, summary->conds, p);
2244 VEC_replace (predicate_t, nonconstant_names,
2245 SSA_NAME_VERSION (gimple_phi_result (phi)), *p);
2248 /* Compute function body size parameters for NODE.
2249 When EARLY is true, we compute only simple summaries without
2250 non-trivial predicates to drive the early inliner. */
2252 static void
2253 estimate_function_body_sizes (struct cgraph_node *node, bool early)
2255 gcov_type time = 0;
2256 /* Estimate static overhead for function prologue/epilogue and alignment. */
2257 int size = 2;
2258 /* Benefits are scaled by probability of elimination that is in range
2259 <0,2>. */
2260 basic_block bb;
2261 gimple_stmt_iterator bsi;
2262 struct function *my_function = DECL_STRUCT_FUNCTION (node->symbol.decl);
2263 int freq;
2264 struct inline_summary *info = inline_summary (node);
2265 struct predicate bb_predicate;
2266 struct ipa_node_params *parms_info = NULL;
2267 VEC (predicate_t, heap) *nonconstant_names = NULL;
2269 info->conds = 0;
2270 info->entry = 0;
2272 if (optimize && !early)
2274 calculate_dominance_info (CDI_DOMINATORS);
2275 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
2277 if (ipa_node_params_vector)
2279 parms_info = IPA_NODE_REF (node);
2280 VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
2281 VEC_length (tree, SSANAMES (my_function)));
2285 if (dump_file)
2286 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
2287 cgraph_node_name (node));
2289 /* When we run into maximal number of entries, we assign everything to the
2290 constant truth case. Be sure to have it in list. */
2291 bb_predicate = true_predicate ();
2292 account_size_time (info, 0, 0, &bb_predicate);
2294 bb_predicate = not_inlined_predicate ();
2295 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
2297 gcc_assert (my_function && my_function->cfg);
2298 if (parms_info)
2299 compute_bb_predicates (node, parms_info, info);
2300 FOR_EACH_BB_FN (bb, my_function)
2302 freq = compute_call_stmt_bb_frequency (node->symbol.decl, bb);
2304 /* TODO: Obviously predicates can be propagated down across CFG. */
2305 if (parms_info)
2307 if (bb->aux)
2308 bb_predicate = *(struct predicate *)bb->aux;
2309 else
2310 bb_predicate = false_predicate ();
2312 else
2313 bb_predicate = true_predicate ();
2315 if (dump_file && (dump_flags & TDF_DETAILS))
2317 fprintf (dump_file, "\n BB %i predicate:", bb->index);
2318 dump_predicate (dump_file, info->conds, &bb_predicate);
2321 if (parms_info && nonconstant_names)
2323 struct predicate phi_predicate;
2324 bool first_phi = true;
2326 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2328 if (first_phi
2329 && !phi_result_unknown_predicate (parms_info, info, bb,
2330 &phi_predicate,
2331 nonconstant_names))
2332 break;
2333 first_phi = false;
2334 if (dump_file && (dump_flags & TDF_DETAILS))
2336 fprintf (dump_file, " ");
2337 print_gimple_stmt (dump_file, gsi_stmt (bsi), 0, 0);
2339 predicate_for_phi_result (info, gsi_stmt (bsi), &phi_predicate,
2340 nonconstant_names);
2344 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2346 gimple stmt = gsi_stmt (bsi);
2347 int this_size = estimate_num_insns (stmt, &eni_size_weights);
2348 int this_time = estimate_num_insns (stmt, &eni_time_weights);
2349 int prob;
2350 struct predicate will_be_nonconstant;
2352 if (dump_file && (dump_flags & TDF_DETAILS))
2354 fprintf (dump_file, " ");
2355 print_gimple_stmt (dump_file, stmt, 0, 0);
2356 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
2357 ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
2360 if (is_gimple_call (stmt))
2362 struct cgraph_edge *edge = cgraph_edge (node, stmt);
2363 struct inline_edge_summary *es = inline_edge_summary (edge);
2365 /* Special case: results of BUILT_IN_CONSTANT_P will be always
2366 resolved as constant. We however don't want to optimize
2367 out the cgraph edges. */
2368 if (nonconstant_names
2369 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
2370 && gimple_call_lhs (stmt)
2371 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
2373 struct predicate false_p = false_predicate ();
2374 VEC_replace (predicate_t, nonconstant_names,
2375 SSA_NAME_VERSION (gimple_call_lhs (stmt)),
2376 false_p);
2378 if (ipa_node_params_vector)
2380 int count = gimple_call_num_args (stmt);
2381 int i;
2383 if (count)
2384 VEC_safe_grow_cleared (inline_param_summary_t, heap,
2385 es->param, count);
2386 for (i = 0; i < count; i++)
2388 int prob = param_change_prob (stmt, i);
2389 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
2390 VEC_index (inline_param_summary_t,
2391 es->param, i).change_prob = prob;
2395 es->call_stmt_size = this_size;
2396 es->call_stmt_time = this_time;
2397 es->loop_depth = bb_loop_depth (bb);
2398 edge_set_predicate (edge, &bb_predicate);
2401 /* TODO: When conditional jump or swithc is known to be constant, but
2402 we did not translate it into the predicates, we really can account
2403 just maximum of the possible paths. */
2404 if (parms_info)
2405 will_be_nonconstant
2406 = will_be_nonconstant_predicate (parms_info, info,
2407 stmt, nonconstant_names);
2408 if (this_time || this_size)
2410 struct predicate p;
2412 this_time *= freq;
2414 prob = eliminated_by_inlining_prob (stmt);
2415 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
2416 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
2417 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
2418 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
2420 if (parms_info)
2421 p = and_predicates (info->conds, &bb_predicate,
2422 &will_be_nonconstant);
2423 else
2424 p = true_predicate ();
2426 if (!false_predicate_p (&p))
2428 time += this_time;
2429 size += this_size;
2432 /* We account everything but the calls. Calls have their own
2433 size/time info attached to cgraph edges. This is necessary
2434 in order to make the cost disappear after inlining. */
2435 if (!is_gimple_call (stmt))
2437 if (prob)
2439 struct predicate ip = not_inlined_predicate ();
2440 ip = and_predicates (info->conds, &ip, &p);
2441 account_size_time (info, this_size * prob,
2442 this_time * prob, &ip);
2444 if (prob != 2)
2445 account_size_time (info, this_size * (2 - prob),
2446 this_time * (2 - prob), &p);
2449 gcc_assert (time >= 0);
2450 gcc_assert (size >= 0);
2454 FOR_ALL_BB_FN (bb, my_function)
2456 edge e;
2457 edge_iterator ei;
2459 if (bb->aux)
2460 pool_free (edge_predicate_pool, bb->aux);
2461 bb->aux = NULL;
2462 FOR_EACH_EDGE (e, ei, bb->succs)
2464 if (e->aux)
2465 pool_free (edge_predicate_pool, e->aux);
2466 e->aux = NULL;
2469 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2470 if (time > MAX_TIME)
2471 time = MAX_TIME;
2473 if (!early && nonconstant_names)
2475 struct loop *loop;
2476 loop_iterator li;
2477 predicate loop_iterations = true_predicate ();
2478 predicate loop_stride = true_predicate ();
2480 if (dump_file && (dump_flags & TDF_DETAILS))
2481 flow_loops_dump (dump_file, NULL, 0);
2482 scev_initialize ();
2483 FOR_EACH_LOOP (li, loop, 0)
2485 VEC (edge, heap) *exits;
2486 edge ex;
2487 unsigned int j, i;
2488 struct tree_niter_desc niter_desc;
2489 basic_block *body = get_loop_body (loop);
2491 exits = get_loop_exit_edges (loop);
2492 FOR_EACH_VEC_ELT (edge, exits, j, ex)
2493 if (number_of_iterations_exit (loop, ex, &niter_desc, false)
2494 && !is_gimple_min_invariant (niter_desc.niter))
2496 predicate will_be_nonconstant
2497 = will_be_nonconstant_expr_predicate (parms_info, info,
2498 niter_desc.niter, nonconstant_names);
2499 if (!true_predicate_p (&will_be_nonconstant)
2500 && !false_predicate_p (&will_be_nonconstant))
2501 /* This is slightly inprecise. We may want to represent each loop with
2502 independent predicate. */
2503 loop_iterations = and_predicates (info->conds, &loop_iterations, &will_be_nonconstant);
2505 VEC_free (edge, heap, exits);
2507 for (i = 0; i < loop->num_nodes; i++)
2509 gimple_stmt_iterator gsi;
2510 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
2512 gimple stmt = gsi_stmt (gsi);
2513 affine_iv iv;
2514 ssa_op_iter iter;
2515 tree use;
2517 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
2519 predicate will_be_nonconstant;
2521 if (!simple_iv (loop, loop_containing_stmt (stmt), use, &iv, true)
2522 || is_gimple_min_invariant (iv.step))
2523 continue;
2524 will_be_nonconstant
2525 = will_be_nonconstant_expr_predicate (parms_info, info,
2526 iv.step, nonconstant_names);
2527 if (!true_predicate_p (&will_be_nonconstant)
2528 && !false_predicate_p (&will_be_nonconstant))
2529 /* This is slightly inprecise. We may want to represent each loop with
2530 independent predicate. */
2531 loop_stride = and_predicates (info->conds, &loop_stride, &will_be_nonconstant);
2535 free (body);
2537 set_hint_predicate (&inline_summary (node)->loop_iterations, loop_iterations);
2538 set_hint_predicate (&inline_summary (node)->loop_stride, loop_stride);
2539 scev_finalize ();
2541 inline_summary (node)->self_time = time;
2542 inline_summary (node)->self_size = size;
2543 VEC_free (predicate_t, heap, nonconstant_names);
2544 if (optimize && !early)
2546 loop_optimizer_finalize ();
2547 free_dominance_info (CDI_DOMINATORS);
2549 if (dump_file)
2551 fprintf (dump_file, "\n");
2552 dump_inline_summary (dump_file, node);
2557 /* Compute parameters of functions used by inliner.
2558 EARLY is true when we compute parameters for the early inliner */
2560 void
2561 compute_inline_parameters (struct cgraph_node *node, bool early)
2563 HOST_WIDE_INT self_stack_size;
2564 struct cgraph_edge *e;
2565 struct inline_summary *info;
2567 gcc_assert (!node->global.inlined_to);
2569 inline_summary_alloc ();
2571 info = inline_summary (node);
2572 reset_inline_summary (node);
2574 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2575 Once this happen, we will need to more curefully predict call
2576 statement size. */
2577 if (node->thunk.thunk_p)
2579 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2580 struct predicate t = true_predicate ();
2582 info->inlinable = 0;
2583 node->callees->call_stmt_cannot_inline_p = true;
2584 node->local.can_change_signature = false;
2585 es->call_stmt_time = 1;
2586 es->call_stmt_size = 1;
2587 account_size_time (info, 0, 0, &t);
2588 return;
2591 /* Even is_gimple_min_invariant rely on current_function_decl. */
2592 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2594 /* Estimate the stack size for the function if we're optimizing. */
2595 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2596 info->estimated_self_stack_size = self_stack_size;
2597 info->estimated_stack_size = self_stack_size;
2598 info->stack_frame_offset = 0;
2600 /* Can this function be inlined at all? */
2601 info->inlinable = tree_inlinable_function_p (node->symbol.decl);
2603 /* Type attributes can use parameter indices to describe them. */
2604 if (TYPE_ATTRIBUTES (TREE_TYPE (node->symbol.decl)))
2605 node->local.can_change_signature = false;
2606 else
2608 /* Otherwise, inlinable functions always can change signature. */
2609 if (info->inlinable)
2610 node->local.can_change_signature = true;
2611 else
2613 /* Functions calling builtin_apply can not change signature. */
2614 for (e = node->callees; e; e = e->next_callee)
2616 tree cdecl = e->callee->symbol.decl;
2617 if (DECL_BUILT_IN (cdecl)
2618 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2619 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2620 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2621 break;
2623 node->local.can_change_signature = !e;
2626 estimate_function_body_sizes (node, early);
2628 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2629 info->time = info->self_time;
2630 info->size = info->self_size;
2631 info->stack_frame_offset = 0;
2632 info->estimated_stack_size = info->estimated_self_stack_size;
2633 #ifdef ENABLE_CHECKING
2634 inline_update_overall_summary (node);
2635 gcc_assert (info->time == info->self_time
2636 && info->size == info->self_size);
2637 #endif
2639 pop_cfun ();
2643 /* Compute parameters of functions used by inliner using
2644 current_function_decl. */
2646 static unsigned int
2647 compute_inline_parameters_for_current (void)
2649 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2650 return 0;
2653 struct gimple_opt_pass pass_inline_parameters =
2656 GIMPLE_PASS,
2657 "inline_param", /* name */
2658 NULL, /* gate */
2659 compute_inline_parameters_for_current,/* execute */
2660 NULL, /* sub */
2661 NULL, /* next */
2662 0, /* static_pass_number */
2663 TV_INLINE_PARAMETERS, /* tv_id */
2664 0, /* properties_required */
2665 0, /* properties_provided */
2666 0, /* properties_destroyed */
2667 0, /* todo_flags_start */
2668 0 /* todo_flags_finish */
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,
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 struct cgraph_node *callee;
2685 struct inline_summary *isummary;
2687 if (!known_vals && !known_binfos)
2688 return false;
2689 if (!flag_indirect_inlining)
2690 return false;
2692 target = ipa_get_indirect_edge_target (ie, known_vals, known_binfos,
2693 known_aggs);
2694 if (!target)
2695 return false;
2697 /* Account for difference in cost between indirect and direct calls. */
2698 *size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost);
2699 *time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost);
2700 gcc_checking_assert (*time >= 0);
2701 gcc_checking_assert (*size >= 0);
2703 callee = cgraph_get_node (target);
2704 if (!callee || !callee->analyzed)
2705 return false;
2706 isummary = inline_summary (callee);
2707 return isummary->inlinable;
2710 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2712 static inline void
2713 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2714 int prob,
2715 VEC (tree, heap) *known_vals,
2716 VEC (tree, heap) *known_binfos,
2717 VEC (ipa_agg_jump_function_p, heap) *known_aggs,
2718 inline_hints *hints)
2721 struct inline_edge_summary *es = inline_edge_summary (e);
2722 int call_size = es->call_stmt_size;
2723 int call_time = es->call_stmt_time;
2724 if (!e->callee
2725 && estimate_edge_devirt_benefit (e, &call_size, &call_time,
2726 known_vals, known_binfos, known_aggs)
2727 && hints
2728 && cgraph_maybe_hot_edge_p (e))
2729 *hints |= INLINE_HINT_indirect_call;
2730 *size += call_size * INLINE_SIZE_SCALE;
2731 *time += call_time * prob / REG_BR_PROB_BASE
2732 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE);
2733 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2734 *time = MAX_TIME * INLINE_TIME_SCALE;
2739 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE.
2740 POSSIBLE_TRUTHS, KNOWN_VALS and KNOWN_BINFOS describe context of the call
2741 site. */
2743 static void
2744 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
2745 inline_hints *hints,
2746 clause_t possible_truths,
2747 VEC (tree, heap) *known_vals,
2748 VEC (tree, heap) *known_binfos,
2749 VEC (ipa_agg_jump_function_p, heap) *known_aggs)
2751 struct cgraph_edge *e;
2752 for (e = node->callees; e; e = e->next_callee)
2754 struct inline_edge_summary *es = inline_edge_summary (e);
2755 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2757 if (e->inline_failed)
2759 /* Predicates of calls shall not use NOT_CHANGED codes,
2760 sowe do not need to compute probabilities. */
2761 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
2762 known_vals, known_binfos, known_aggs,
2763 hints);
2765 else
2766 estimate_calls_size_and_time (e->callee, size, time, hints,
2767 possible_truths,
2768 known_vals, known_binfos, known_aggs);
2771 for (e = node->indirect_calls; e; e = e->next_callee)
2773 struct inline_edge_summary *es = inline_edge_summary (e);
2774 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2775 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE,
2776 known_vals, known_binfos, known_aggs,
2777 hints);
2782 /* Estimate size and time needed to execute NODE assuming
2783 POSSIBLE_TRUTHS clause, and KNOWN_VALS and KNOWN_BINFOS information
2784 about NODE's arguments. */
2786 static void
2787 estimate_node_size_and_time (struct cgraph_node *node,
2788 clause_t possible_truths,
2789 VEC (tree, heap) *known_vals,
2790 VEC (tree, heap) *known_binfos,
2791 VEC (ipa_agg_jump_function_p, heap) *known_aggs,
2792 int *ret_size, int *ret_time,
2793 inline_hints *ret_hints,
2794 VEC (inline_param_summary_t, heap)
2795 *inline_param_summary)
2797 struct inline_summary *info = inline_summary (node);
2798 size_time_entry *e;
2799 int size = 0;
2800 int time = 0;
2801 inline_hints hints = 0;
2802 int i;
2804 if (dump_file
2805 && (dump_flags & TDF_DETAILS))
2807 bool found = false;
2808 fprintf (dump_file, " Estimating body: %s/%i\n"
2809 " Known to be false: ",
2810 cgraph_node_name (node),
2811 node->uid);
2813 for (i = predicate_not_inlined_condition;
2814 i < (predicate_first_dynamic_condition
2815 + (int)VEC_length (condition, info->conds)); i++)
2816 if (!(possible_truths & (1 << i)))
2818 if (found)
2819 fprintf (dump_file, ", ");
2820 found = true;
2821 dump_condition (dump_file, info->conds, i);
2825 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2826 if (evaluate_predicate (&e->predicate, possible_truths))
2828 size += e->size;
2829 gcc_checking_assert (e->time >= 0);
2830 gcc_checking_assert (time >= 0);
2831 if (!inline_param_summary)
2832 time += e->time;
2833 else
2835 int prob = predicate_probability (info->conds,
2836 &e->predicate,
2837 possible_truths,
2838 inline_param_summary);
2839 gcc_checking_assert (prob >= 0);
2840 gcc_checking_assert (prob <= REG_BR_PROB_BASE);
2841 time += ((gcov_type)e->time * prob) / REG_BR_PROB_BASE;
2843 if (time > MAX_TIME * INLINE_TIME_SCALE)
2844 time = MAX_TIME * INLINE_TIME_SCALE;
2845 gcc_checking_assert (time >= 0);
2848 gcc_checking_assert (size >= 0);
2849 gcc_checking_assert (time >= 0);
2851 if (info->loop_iterations
2852 && !evaluate_predicate (info->loop_iterations, possible_truths))
2853 hints |=INLINE_HINT_loop_iterations;
2854 if (info->loop_stride
2855 && !evaluate_predicate (info->loop_stride, possible_truths))
2856 hints |=INLINE_HINT_loop_stride;
2857 if (info->scc_no)
2858 hints |= INLINE_HINT_in_scc;
2860 estimate_calls_size_and_time (node, &size, &time, &hints, possible_truths,
2861 known_vals, known_binfos, known_aggs);
2862 gcc_checking_assert (size >= 0);
2863 gcc_checking_assert (time >= 0);
2864 time = RDIV (time, INLINE_TIME_SCALE);
2865 size = RDIV (size, INLINE_SIZE_SCALE);
2868 if (dump_file
2869 && (dump_flags & TDF_DETAILS))
2870 fprintf (dump_file, "\n size:%i time:%i\n", (int)size, (int)time);
2871 if (ret_time)
2872 *ret_time = time;
2873 if (ret_size)
2874 *ret_size = size;
2875 if (ret_hints)
2876 *ret_hints = hints;
2877 return;
2881 /* Estimate size and time needed to execute callee of EDGE assuming that
2882 parameters known to be constant at caller of EDGE are propagated.
2883 KNOWN_VALS and KNOWN_BINFOS are vectors of assumed known constant values
2884 and types for parameters. */
2886 void
2887 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
2888 VEC (tree, heap) *known_vals,
2889 VEC (tree, heap) *known_binfos,
2890 int *ret_size, int *ret_time)
2892 clause_t clause;
2894 clause = evaluate_conditions_for_known_args (node, false, known_vals, NULL);
2895 estimate_node_size_and_time (node, clause, known_vals, known_binfos, NULL,
2896 ret_size, ret_time, NULL,
2897 NULL);
2900 /* Translate all conditions from callee representation into caller
2901 representation and symbolically evaluate predicate P into new predicate.
2903 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO
2904 is summary of function predicate P is from. OPERAND_MAP is array giving
2905 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clausule of all
2906 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
2907 predicate under which callee is executed. OFFSET_MAP is an array of of
2908 offsets that need to be added to conditions, negative offset means that
2909 conditions relying on values passed by reference have to be discarded
2910 because they might not be preserved (and should be considered offset zero
2911 for other purposes). */
2913 static struct predicate
2914 remap_predicate (struct inline_summary *info,
2915 struct inline_summary *callee_info,
2916 struct predicate *p,
2917 VEC (int, heap) *operand_map,
2918 VEC (int, heap) *offset_map,
2919 clause_t possible_truths,
2920 struct predicate *toplev_predicate)
2922 int i;
2923 struct predicate out = true_predicate ();
2925 /* True predicate is easy. */
2926 if (true_predicate_p (p))
2927 return *toplev_predicate;
2928 for (i = 0; p->clause[i]; i++)
2930 clause_t clause = p->clause[i];
2931 int cond;
2932 struct predicate clause_predicate = false_predicate ();
2934 gcc_assert (i < MAX_CLAUSES);
2936 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
2937 /* Do we have condition we can't disprove? */
2938 if (clause & possible_truths & (1 << cond))
2940 struct predicate cond_predicate;
2941 /* Work out if the condition can translate to predicate in the
2942 inlined function. */
2943 if (cond >= predicate_first_dynamic_condition)
2945 struct condition *c;
2947 c = &VEC_index (condition, callee_info->conds,
2948 cond - predicate_first_dynamic_condition);
2949 /* See if we can remap condition operand to caller's operand.
2950 Otherwise give up. */
2951 if (!operand_map
2952 || (int)VEC_length (int, operand_map) <= c->operand_num
2953 || VEC_index (int, operand_map, c->operand_num) == -1
2954 /* TODO: For non-aggregate conditions, adding an offset is
2955 basically an arithmetic jump function processing which
2956 we should support in future. */
2957 || ((!c->agg_contents || !c->by_ref)
2958 && VEC_index (int, offset_map, c->operand_num) > 0)
2959 || (c->agg_contents && c->by_ref
2960 && VEC_index (int, offset_map, c->operand_num) < 0))
2961 cond_predicate = true_predicate ();
2962 else
2964 struct agg_position_info ap;
2965 HOST_WIDE_INT offset_delta = VEC_index (int, offset_map,
2966 c->operand_num);
2967 if (offset_delta < 0)
2969 gcc_checking_assert (!c->agg_contents || !c->by_ref);
2970 offset_delta = 0;
2972 gcc_assert (!c->agg_contents
2973 || c->by_ref
2974 || offset_delta == 0);
2975 ap.offset = c->offset + offset_delta;
2976 ap.agg_contents = c->agg_contents;
2977 ap.by_ref = c->by_ref;
2978 cond_predicate = add_condition (info,
2979 VEC_index (int,
2980 operand_map,
2981 c->operand_num),
2982 &ap, c->code, c->val);
2985 /* Fixed conditions remains same, construct single
2986 condition predicate. */
2987 else
2989 cond_predicate.clause[0] = 1 << cond;
2990 cond_predicate.clause[1] = 0;
2992 clause_predicate = or_predicates (info->conds, &clause_predicate,
2993 &cond_predicate);
2995 out = and_predicates (info->conds, &out, &clause_predicate);
2997 return and_predicates (info->conds, &out, toplev_predicate);
3001 /* Update summary information of inline clones after inlining.
3002 Compute peak stack usage. */
3004 static void
3005 inline_update_callee_summaries (struct cgraph_node *node,
3006 int depth)
3008 struct cgraph_edge *e;
3009 struct inline_summary *callee_info = inline_summary (node);
3010 struct inline_summary *caller_info = inline_summary (node->callers->caller);
3011 HOST_WIDE_INT peak;
3013 callee_info->stack_frame_offset
3014 = caller_info->stack_frame_offset
3015 + caller_info->estimated_self_stack_size;
3016 peak = callee_info->stack_frame_offset
3017 + callee_info->estimated_self_stack_size;
3018 if (inline_summary (node->global.inlined_to)->estimated_stack_size
3019 < peak)
3020 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
3021 cgraph_propagate_frequency (node);
3022 for (e = node->callees; e; e = e->next_callee)
3024 if (!e->inline_failed)
3025 inline_update_callee_summaries (e->callee, depth);
3026 inline_edge_summary (e)->loop_depth += depth;
3028 for (e = node->indirect_calls; e; e = e->next_callee)
3029 inline_edge_summary (e)->loop_depth += depth;
3032 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
3033 When functoin A is inlined in B and A calls C with parameter that
3034 changes with probability PROB1 and C is known to be passthroug
3035 of argument if B that change with probability PROB2, the probability
3036 of change is now PROB1*PROB2. */
3038 static void
3039 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
3040 struct cgraph_edge *edge)
3042 if (ipa_node_params_vector)
3044 int i;
3045 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3046 struct inline_edge_summary *es = inline_edge_summary (edge);
3047 struct inline_edge_summary *inlined_es
3048 = inline_edge_summary (inlined_edge);
3050 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
3052 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3053 if (jfunc->type == IPA_JF_PASS_THROUGH
3054 && (ipa_get_jf_pass_through_formal_id (jfunc)
3055 < (int) VEC_length (inline_param_summary_t,
3056 inlined_es->param)))
3058 int jf_formal_id = ipa_get_jf_pass_through_formal_id (jfunc);
3059 int prob1 = VEC_index (inline_param_summary_t,
3060 es->param, i).change_prob;
3061 int prob2 = VEC_index
3062 (inline_param_summary_t,
3063 inlined_es->param, jf_formal_id).change_prob;
3064 int prob = ((prob1 * prob2 + REG_BR_PROB_BASE / 2)
3065 / REG_BR_PROB_BASE);
3067 if (prob1 && prob2 && !prob)
3068 prob = 1;
3070 VEC_index (inline_param_summary_t,
3071 es->param, i).change_prob = prob;
3077 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
3079 Remap predicates of callees of NODE. Rest of arguments match
3080 remap_predicate.
3082 Also update change probabilities. */
3084 static void
3085 remap_edge_summaries (struct cgraph_edge *inlined_edge,
3086 struct cgraph_node *node,
3087 struct inline_summary *info,
3088 struct inline_summary *callee_info,
3089 VEC (int, heap) *operand_map,
3090 VEC (int, heap) *offset_map,
3091 clause_t possible_truths,
3092 struct predicate *toplev_predicate)
3094 struct cgraph_edge *e;
3095 for (e = node->callees; e; e = e->next_callee)
3097 struct inline_edge_summary *es = inline_edge_summary (e);
3098 struct predicate p;
3100 if (e->inline_failed)
3102 remap_edge_change_prob (inlined_edge, e);
3104 if (es->predicate)
3106 p = remap_predicate (info, callee_info,
3107 es->predicate, operand_map, offset_map,
3108 possible_truths,
3109 toplev_predicate);
3110 edge_set_predicate (e, &p);
3111 /* TODO: We should remove the edge for code that will be
3112 optimized out, but we need to keep verifiers and tree-inline
3113 happy. Make it cold for now. */
3114 if (false_predicate_p (&p))
3116 e->count = 0;
3117 e->frequency = 0;
3120 else
3121 edge_set_predicate (e, toplev_predicate);
3123 else
3124 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
3125 operand_map, offset_map, possible_truths,
3126 toplev_predicate);
3128 for (e = node->indirect_calls; e; e = e->next_callee)
3130 struct inline_edge_summary *es = inline_edge_summary (e);
3131 struct predicate p;
3133 remap_edge_change_prob (inlined_edge, e);
3134 if (es->predicate)
3136 p = remap_predicate (info, callee_info,
3137 es->predicate, operand_map, offset_map,
3138 possible_truths, toplev_predicate);
3139 edge_set_predicate (e, &p);
3140 /* TODO: We should remove the edge for code that will be optimized
3141 out, but we need to keep verifiers and tree-inline happy.
3142 Make it cold for now. */
3143 if (false_predicate_p (&p))
3145 e->count = 0;
3146 e->frequency = 0;
3149 else
3150 edge_set_predicate (e, toplev_predicate);
3154 /* Same as remap_predicate, but set result into hint *HINT. */
3156 static void
3157 remap_hint_predicate (struct inline_summary *info,
3158 struct inline_summary *callee_info,
3159 struct predicate **hint,
3160 VEC (int, heap) *operand_map,
3161 VEC (int, heap) *offset_map,
3162 clause_t possible_truths,
3163 struct predicate *toplev_predicate)
3165 predicate p;
3167 if (!*hint)
3168 return;
3169 p = remap_predicate (info, callee_info,
3170 *hint,
3171 operand_map, offset_map,
3172 possible_truths,
3173 toplev_predicate);
3174 if (!false_predicate_p (&p)
3175 && !true_predicate_p (&p))
3177 if (!*hint)
3178 set_hint_predicate (hint, p);
3179 else
3180 **hint = and_predicates (info->conds,
3181 *hint,
3182 &p);
3186 /* We inlined EDGE. Update summary of the function we inlined into. */
3188 void
3189 inline_merge_summary (struct cgraph_edge *edge)
3191 struct inline_summary *callee_info = inline_summary (edge->callee);
3192 struct cgraph_node *to = (edge->caller->global.inlined_to
3193 ? edge->caller->global.inlined_to : edge->caller);
3194 struct inline_summary *info = inline_summary (to);
3195 clause_t clause = 0; /* not_inline is known to be false. */
3196 size_time_entry *e;
3197 VEC (int, heap) *operand_map = NULL;
3198 VEC (int, heap) *offset_map = NULL;
3199 int i;
3200 struct predicate toplev_predicate;
3201 struct predicate true_p = true_predicate ();
3202 struct inline_edge_summary *es = inline_edge_summary (edge);
3204 if (es->predicate)
3205 toplev_predicate = *es->predicate;
3206 else
3207 toplev_predicate = true_predicate ();
3209 if (ipa_node_params_vector && callee_info->conds)
3211 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
3212 int count = ipa_get_cs_argument_count (args);
3213 int i;
3215 evaluate_properties_for_edge (edge, true, &clause, NULL, NULL, NULL);
3216 if (count)
3218 VEC_safe_grow_cleared (int, heap, operand_map, count);
3219 VEC_safe_grow_cleared (int, heap, offset_map, count);
3221 for (i = 0; i < count; i++)
3223 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
3224 int map = -1;
3226 /* TODO: handle non-NOPs when merging. */
3227 if (jfunc->type == IPA_JF_PASS_THROUGH)
3229 if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3230 map = ipa_get_jf_pass_through_formal_id (jfunc);
3231 if (!ipa_get_jf_pass_through_agg_preserved (jfunc))
3232 VEC_replace (int, offset_map, i, -1);
3234 else if (jfunc->type == IPA_JF_ANCESTOR)
3236 HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc);
3237 if (offset >= 0 && offset < INT_MAX)
3239 map = ipa_get_jf_ancestor_formal_id (jfunc);
3240 if (!ipa_get_jf_ancestor_agg_preserved (jfunc))
3241 offset = -1;
3242 VEC_replace (int, offset_map, i, offset);
3245 VEC_replace (int, operand_map, i, map);
3246 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
3249 for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
3251 struct predicate p = remap_predicate (info, callee_info,
3252 &e->predicate, operand_map,
3253 offset_map, clause,
3254 &toplev_predicate);
3255 if (!false_predicate_p (&p))
3257 gcov_type add_time = ((gcov_type)e->time * edge->frequency
3258 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
3259 int prob = predicate_probability (callee_info->conds,
3260 &e->predicate,
3261 clause, es->param);
3262 add_time = ((gcov_type)add_time * prob) / REG_BR_PROB_BASE;
3263 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
3264 add_time = MAX_TIME * INLINE_TIME_SCALE;
3265 if (prob != REG_BR_PROB_BASE
3266 && dump_file && (dump_flags & TDF_DETAILS))
3268 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
3269 (double)prob / REG_BR_PROB_BASE);
3271 account_size_time (info, e->size, add_time, &p);
3274 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
3275 offset_map, clause, &toplev_predicate);
3276 remap_hint_predicate (info, callee_info,
3277 &callee_info->loop_iterations,
3278 operand_map, offset_map,
3279 clause, &toplev_predicate);
3280 remap_hint_predicate (info, callee_info,
3281 &callee_info->loop_stride,
3282 operand_map, offset_map,
3283 clause, &toplev_predicate);
3285 inline_update_callee_summaries (edge->callee,
3286 inline_edge_summary (edge)->loop_depth);
3288 /* We do not maintain predicates of inlined edges, free it. */
3289 edge_set_predicate (edge, &true_p);
3290 /* Similarly remove param summaries. */
3291 VEC_free (inline_param_summary_t, heap, es->param);
3292 VEC_free (int, heap, operand_map);
3293 VEC_free (int, heap, offset_map);
3296 /* For performance reasons inline_merge_summary is not updating overall size
3297 and time. Recompute it. */
3299 void
3300 inline_update_overall_summary (struct cgraph_node *node)
3302 struct inline_summary *info = inline_summary (node);
3303 size_time_entry *e;
3304 int i;
3306 info->size = 0;
3307 info->time = 0;
3308 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
3309 info->size += e->size, info->time += e->time;
3310 estimate_calls_size_and_time (node, &info->size, &info->time, NULL,
3311 ~(clause_t)(1 << predicate_false_condition),
3312 NULL, NULL, NULL);
3313 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
3314 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
3317 /* Estimate the time cost for the caller when inlining EDGE.
3318 Only to be called via estimate_edge_time, that handles the
3319 caching mechanism.
3321 When caching, also update the cache entry. Compute both time and
3322 size, since we always need both metrics eventually. */
3325 do_estimate_edge_time (struct cgraph_edge *edge)
3327 int time;
3328 int size;
3329 inline_hints hints;
3330 gcov_type ret;
3331 struct cgraph_node *callee;
3332 clause_t clause;
3333 VEC (tree, heap) *known_vals;
3334 VEC (tree, heap) *known_binfos;
3335 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3336 struct inline_edge_summary *es = inline_edge_summary (edge);
3338 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3340 gcc_checking_assert (edge->inline_failed);
3341 evaluate_properties_for_edge (edge, true,
3342 &clause, &known_vals, &known_binfos,
3343 &known_aggs);
3344 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3345 known_aggs, &size, &time, &hints, es->param);
3346 VEC_free (tree, heap, known_vals);
3347 VEC_free (tree, heap, known_binfos);
3348 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3350 ret = RDIV ((gcov_type)time * edge->frequency,
3351 CGRAPH_FREQ_BASE);
3353 /* When caching, update the cache entry. */
3354 if (edge_growth_cache)
3356 struct cgraph_node *to = (edge->caller->global.inlined_to
3357 ? edge->caller->global.inlined_to
3358 : edge->caller);
3359 if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
3360 <= edge->uid)
3361 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
3362 cgraph_edge_max_uid);
3363 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).time
3364 = ret + (ret >= 0);
3366 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).size
3367 = size + (size >= 0);
3368 if (inline_summary (to)->scc_no
3369 && inline_summary (to)->scc_no == inline_summary (callee)->scc_no
3370 && !cgraph_edge_recursive_p (edge))
3371 hints |= INLINE_HINT_same_scc;
3372 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid).hints
3373 = hints + 1;
3375 return ret;
3379 /* Return estimated callee growth after inlining EDGE.
3380 Only to be called via estimate_edge_size. */
3383 do_estimate_edge_size (struct cgraph_edge *edge)
3385 int size;
3386 struct cgraph_node *callee;
3387 clause_t clause;
3388 VEC (tree, heap) *known_vals;
3389 VEC (tree, heap) *known_binfos;
3390 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3392 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3394 if (edge_growth_cache)
3396 do_estimate_edge_time (edge);
3397 size = VEC_index (edge_growth_cache_entry,
3398 edge_growth_cache,
3399 edge->uid).size;
3400 gcc_checking_assert (size);
3401 return size - (size > 0);
3404 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3406 /* Early inliner runs without caching, go ahead and do the dirty work. */
3407 gcc_checking_assert (edge->inline_failed);
3408 evaluate_properties_for_edge (edge, true,
3409 &clause, &known_vals, &known_binfos,
3410 &known_aggs);
3411 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3412 known_aggs, &size, NULL, NULL, NULL);
3413 VEC_free (tree, heap, known_vals);
3414 VEC_free (tree, heap, known_binfos);
3415 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3416 return size;
3420 /* Estimate the growth of the caller when inlining EDGE.
3421 Only to be called via estimate_edge_size. */
3423 inline_hints
3424 do_estimate_edge_hints (struct cgraph_edge *edge)
3426 inline_hints hints;
3427 struct cgraph_node *callee;
3428 clause_t clause;
3429 VEC (tree, heap) *known_vals;
3430 VEC (tree, heap) *known_binfos;
3431 VEC (ipa_agg_jump_function_p, heap) *known_aggs;
3432 struct cgraph_node *to = (edge->caller->global.inlined_to
3433 ? edge->caller->global.inlined_to
3434 : edge->caller);
3436 /* When we do caching, use do_estimate_edge_time to populate the entry. */
3438 if (edge_growth_cache)
3440 do_estimate_edge_time (edge);
3441 hints = VEC_index (edge_growth_cache_entry,
3442 edge_growth_cache,
3443 edge->uid).hints;
3444 gcc_checking_assert (hints);
3445 return hints - 1;
3448 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
3450 /* Early inliner runs without caching, go ahead and do the dirty work. */
3451 gcc_checking_assert (edge->inline_failed);
3452 evaluate_properties_for_edge (edge, true,
3453 &clause, &known_vals, &known_binfos,
3454 &known_aggs);
3455 estimate_node_size_and_time (callee, clause, known_vals, known_binfos,
3456 known_aggs, NULL, NULL, &hints, NULL);
3457 VEC_free (tree, heap, known_vals);
3458 VEC_free (tree, heap, known_binfos);
3459 VEC_free (ipa_agg_jump_function_p, heap, known_aggs);
3460 if (inline_summary (to)->scc_no
3461 && inline_summary (to)->scc_no == inline_summary (callee)->scc_no
3462 && !cgraph_edge_recursive_p (edge))
3463 hints |= INLINE_HINT_same_scc;
3464 return hints;
3468 /* Estimate self time of the function NODE after inlining EDGE. */
3471 estimate_time_after_inlining (struct cgraph_node *node,
3472 struct cgraph_edge *edge)
3474 struct inline_edge_summary *es = inline_edge_summary (edge);
3475 if (!es->predicate || !false_predicate_p (es->predicate))
3477 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
3478 if (time < 0)
3479 time = 0;
3480 if (time > MAX_TIME)
3481 time = MAX_TIME;
3482 return time;
3484 return inline_summary (node)->time;
3488 /* Estimate the size of NODE after inlining EDGE which should be an
3489 edge to either NODE or a call inlined into NODE. */
3492 estimate_size_after_inlining (struct cgraph_node *node,
3493 struct cgraph_edge *edge)
3495 struct inline_edge_summary *es = inline_edge_summary (edge);
3496 if (!es->predicate || !false_predicate_p (es->predicate))
3498 int size = inline_summary (node)->size + estimate_edge_growth (edge);
3499 gcc_assert (size >= 0);
3500 return size;
3502 return inline_summary (node)->size;
3506 struct growth_data
3508 bool self_recursive;
3509 int growth;
3513 /* Worker for do_estimate_growth. Collect growth for all callers. */
3515 static bool
3516 do_estimate_growth_1 (struct cgraph_node *node, void *data)
3518 struct cgraph_edge *e;
3519 struct growth_data *d = (struct growth_data *) data;
3521 for (e = node->callers; e; e = e->next_caller)
3523 gcc_checking_assert (e->inline_failed);
3525 if (e->caller == node
3526 || (e->caller->global.inlined_to
3527 && e->caller->global.inlined_to == node))
3528 d->self_recursive = true;
3529 d->growth += estimate_edge_growth (e);
3531 return false;
3535 /* Estimate the growth caused by inlining NODE into all callees. */
3538 do_estimate_growth (struct cgraph_node *node)
3540 struct growth_data d = {0, false};
3541 struct inline_summary *info = inline_summary (node);
3543 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
3545 /* For self recursive functions the growth estimation really should be
3546 infinity. We don't want to return very large values because the growth
3547 plays various roles in badness computation fractions. Be sure to not
3548 return zero or negative growths. */
3549 if (d.self_recursive)
3550 d.growth = d.growth < info->size ? info->size : d.growth;
3551 else
3553 if (!DECL_EXTERNAL (node->symbol.decl)
3554 && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
3555 d.growth -= info->size;
3556 /* COMDAT functions are very often not shared across multiple units
3557 since they come from various template instantiations.
3558 Take this into account. */
3559 else if (DECL_COMDAT (node->symbol.decl)
3560 && cgraph_can_remove_if_no_direct_calls_p (node))
3561 d.growth -= (info->size
3562 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
3563 + 50) / 100;
3566 if (node_growth_cache)
3568 if ((int)VEC_length (int, node_growth_cache) <= node->uid)
3569 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
3570 VEC_replace (int, node_growth_cache, node->uid,
3571 d.growth + (d.growth >= 0));
3573 return d.growth;
3577 /* This function performs intraprocedural analysis in NODE that is required to
3578 inline indirect calls. */
3580 static void
3581 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
3583 ipa_analyze_node (node);
3584 if (dump_file && (dump_flags & TDF_DETAILS))
3586 ipa_print_node_params (dump_file, node);
3587 ipa_print_node_jump_functions (dump_file, node);
3592 /* Note function body size. */
3594 static void
3595 inline_analyze_function (struct cgraph_node *node)
3597 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
3599 if (dump_file)
3600 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
3601 cgraph_node_name (node), node->uid);
3602 if (optimize && !node->thunk.thunk_p)
3603 inline_indirect_intraprocedural_analysis (node);
3604 compute_inline_parameters (node, false);
3606 pop_cfun ();
3610 /* Called when new function is inserted to callgraph late. */
3612 static void
3613 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3615 inline_analyze_function (node);
3619 /* Note function body size. */
3621 void
3622 inline_generate_summary (void)
3624 struct cgraph_node *node;
3626 function_insertion_hook_holder =
3627 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3629 ipa_register_cgraph_hooks ();
3630 inline_free_summary ();
3632 FOR_EACH_DEFINED_FUNCTION (node)
3633 if (!node->alias)
3634 inline_analyze_function (node);
3638 /* Read predicate from IB. */
3640 static struct predicate
3641 read_predicate (struct lto_input_block *ib)
3643 struct predicate out;
3644 clause_t clause;
3645 int k = 0;
3649 gcc_assert (k <= MAX_CLAUSES);
3650 clause = out.clause[k++] = streamer_read_uhwi (ib);
3652 while (clause);
3654 /* Zero-initialize the remaining clauses in OUT. */
3655 while (k <= MAX_CLAUSES)
3656 out.clause[k++] = 0;
3658 return out;
3662 /* Write inline summary for edge E to OB. */
3664 static void
3665 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
3667 struct inline_edge_summary *es = inline_edge_summary (e);
3668 struct predicate p;
3669 int length, i;
3671 es->call_stmt_size = streamer_read_uhwi (ib);
3672 es->call_stmt_time = streamer_read_uhwi (ib);
3673 es->loop_depth = streamer_read_uhwi (ib);
3674 p = read_predicate (ib);
3675 edge_set_predicate (e, &p);
3676 length = streamer_read_uhwi (ib);
3677 if (length)
3679 VEC_safe_grow_cleared (inline_param_summary_t, heap, es->param, length);
3680 for (i = 0; i < length; i++)
3681 VEC_index (inline_param_summary_t, es->param, i).change_prob
3682 = streamer_read_uhwi (ib);
3687 /* Stream in inline summaries from the section. */
3689 static void
3690 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
3691 size_t len)
3693 const struct lto_function_header *header =
3694 (const struct lto_function_header *) data;
3695 const int cfg_offset = sizeof (struct lto_function_header);
3696 const int main_offset = cfg_offset + header->cfg_size;
3697 const int string_offset = main_offset + header->main_size;
3698 struct data_in *data_in;
3699 struct lto_input_block ib;
3700 unsigned int i, count2, j;
3701 unsigned int f_count;
3703 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
3704 header->main_size);
3706 data_in =
3707 lto_data_in_create (file_data, (const char *) data + string_offset,
3708 header->string_size, NULL);
3709 f_count = streamer_read_uhwi (&ib);
3710 for (i = 0; i < f_count; i++)
3712 unsigned int index;
3713 struct cgraph_node *node;
3714 struct inline_summary *info;
3715 lto_symtab_encoder_t encoder;
3716 struct bitpack_d bp;
3717 struct cgraph_edge *e;
3718 predicate p;
3720 index = streamer_read_uhwi (&ib);
3721 encoder = file_data->symtab_node_encoder;
3722 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3723 info = inline_summary (node);
3725 info->estimated_stack_size
3726 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
3727 info->size = info->self_size = streamer_read_uhwi (&ib);
3728 info->time = info->self_time = streamer_read_uhwi (&ib);
3730 bp = streamer_read_bitpack (&ib);
3731 info->inlinable = bp_unpack_value (&bp, 1);
3733 count2 = streamer_read_uhwi (&ib);
3734 gcc_assert (!info->conds);
3735 for (j = 0; j < count2; j++)
3737 struct condition c;
3738 c.operand_num = streamer_read_uhwi (&ib);
3739 c.code = (enum tree_code) streamer_read_uhwi (&ib);
3740 c.val = stream_read_tree (&ib, data_in);
3741 bp = streamer_read_bitpack (&ib);
3742 c.agg_contents = bp_unpack_value (&bp, 1);
3743 c.by_ref = bp_unpack_value (&bp, 1);
3744 if (c.agg_contents)
3745 c.offset = streamer_read_uhwi (&ib);
3746 VEC_safe_push (condition, gc, info->conds, c);
3748 count2 = streamer_read_uhwi (&ib);
3749 gcc_assert (!info->entry);
3750 for (j = 0; j < count2; j++)
3752 struct size_time_entry e;
3754 e.size = streamer_read_uhwi (&ib);
3755 e.time = streamer_read_uhwi (&ib);
3756 e.predicate = read_predicate (&ib);
3758 VEC_safe_push (size_time_entry, gc, info->entry, e);
3761 p = read_predicate (&ib);
3762 set_hint_predicate (&info->loop_iterations, p);
3763 p = read_predicate (&ib);
3764 set_hint_predicate (&info->loop_stride, p);
3765 for (e = node->callees; e; e = e->next_callee)
3766 read_inline_edge_summary (&ib, e);
3767 for (e = node->indirect_calls; e; e = e->next_callee)
3768 read_inline_edge_summary (&ib, e);
3771 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
3772 len);
3773 lto_data_in_delete (data_in);
3777 /* Read inline summary. Jump functions are shared among ipa-cp
3778 and inliner, so when ipa-cp is active, we don't need to write them
3779 twice. */
3781 void
3782 inline_read_summary (void)
3784 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3785 struct lto_file_decl_data *file_data;
3786 unsigned int j = 0;
3788 inline_summary_alloc ();
3790 while ((file_data = file_data_vec[j++]))
3792 size_t len;
3793 const char *data = lto_get_section_data (file_data,
3794 LTO_section_inline_summary,
3795 NULL, &len);
3796 if (data)
3797 inline_read_section (file_data, data, len);
3798 else
3799 /* Fatal error here. We do not want to support compiling ltrans units
3800 with different version of compiler or different flags than the WPA
3801 unit, so this should never happen. */
3802 fatal_error ("ipa inline summary is missing in input file");
3804 if (optimize)
3806 ipa_register_cgraph_hooks ();
3807 if (!flag_ipa_cp)
3808 ipa_prop_read_jump_functions ();
3810 function_insertion_hook_holder =
3811 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3815 /* Write predicate P to OB. */
3817 static void
3818 write_predicate (struct output_block *ob, struct predicate *p)
3820 int j;
3821 if (p)
3822 for (j = 0; p->clause[j]; j++)
3824 gcc_assert (j < MAX_CLAUSES);
3825 streamer_write_uhwi (ob, p->clause[j]);
3827 streamer_write_uhwi (ob, 0);
3831 /* Write inline summary for edge E to OB. */
3833 static void
3834 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
3836 struct inline_edge_summary *es = inline_edge_summary (e);
3837 int i;
3839 streamer_write_uhwi (ob, es->call_stmt_size);
3840 streamer_write_uhwi (ob, es->call_stmt_time);
3841 streamer_write_uhwi (ob, es->loop_depth);
3842 write_predicate (ob, es->predicate);
3843 streamer_write_uhwi (ob, VEC_length (inline_param_summary_t, es->param));
3844 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param); i++)
3845 streamer_write_uhwi (ob, VEC_index (inline_param_summary_t,
3846 es->param, i).change_prob);
3850 /* Write inline summary for node in SET.
3851 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
3852 active, we don't need to write them twice. */
3854 void
3855 inline_write_summary (void)
3857 struct cgraph_node *node;
3858 symtab_node snode;
3859 struct output_block *ob = create_output_block (LTO_section_inline_summary);
3860 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
3861 unsigned int count = 0;
3862 int i;
3864 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3865 if (symtab_function_p (snode = lto_symtab_encoder_deref (encoder, i))
3866 && cgraph (snode)->analyzed)
3867 count++;
3868 streamer_write_uhwi (ob, count);
3870 for (i = 0; i < lto_symtab_encoder_size (encoder); i++)
3872 if (symtab_function_p (snode = lto_symtab_encoder_deref (encoder, i))
3873 && (node = cgraph (snode))->analyzed)
3875 struct inline_summary *info = inline_summary (node);
3876 struct bitpack_d bp;
3877 struct cgraph_edge *edge;
3878 int i;
3879 size_time_entry *e;
3880 struct condition *c;
3882 streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, (symtab_node)node));
3883 streamer_write_hwi (ob, info->estimated_self_stack_size);
3884 streamer_write_hwi (ob, info->self_size);
3885 streamer_write_hwi (ob, info->self_time);
3886 bp = bitpack_create (ob->main_stream);
3887 bp_pack_value (&bp, info->inlinable, 1);
3888 streamer_write_bitpack (&bp);
3889 streamer_write_uhwi (ob, VEC_length (condition, info->conds));
3890 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
3892 streamer_write_uhwi (ob, c->operand_num);
3893 streamer_write_uhwi (ob, c->code);
3894 stream_write_tree (ob, c->val, true);
3895 bp = bitpack_create (ob->main_stream);
3896 bp_pack_value (&bp, c->agg_contents, 1);
3897 bp_pack_value (&bp, c->by_ref, 1);
3898 streamer_write_bitpack (&bp);
3899 if (c->agg_contents)
3900 streamer_write_uhwi (ob, c->offset);
3902 streamer_write_uhwi (ob, VEC_length (size_time_entry, info->entry));
3903 for (i = 0;
3904 VEC_iterate (size_time_entry, info->entry, i, e);
3905 i++)
3907 streamer_write_uhwi (ob, e->size);
3908 streamer_write_uhwi (ob, e->time);
3909 write_predicate (ob, &e->predicate);
3911 write_predicate (ob, info->loop_iterations);
3912 write_predicate (ob, info->loop_stride);
3913 for (edge = node->callees; edge; edge = edge->next_callee)
3914 write_inline_edge_summary (ob, edge);
3915 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
3916 write_inline_edge_summary (ob, edge);
3919 streamer_write_char_stream (ob->main_stream, 0);
3920 produce_asm (ob, NULL);
3921 destroy_output_block (ob);
3923 if (optimize && !flag_ipa_cp)
3924 ipa_prop_write_jump_functions ();
3928 /* Release inline summary. */
3930 void
3931 inline_free_summary (void)
3933 struct cgraph_node *node;
3934 if (inline_edge_summary_vec == NULL)
3935 return;
3936 FOR_EACH_DEFINED_FUNCTION (node)
3937 reset_inline_summary (node);
3938 if (function_insertion_hook_holder)
3939 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3940 function_insertion_hook_holder = NULL;
3941 if (node_removal_hook_holder)
3942 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3943 node_removal_hook_holder = NULL;
3944 if (edge_removal_hook_holder)
3945 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3946 edge_removal_hook_holder = NULL;
3947 if (node_duplication_hook_holder)
3948 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3949 node_duplication_hook_holder = NULL;
3950 if (edge_duplication_hook_holder)
3951 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3952 edge_duplication_hook_holder = NULL;
3953 VEC_free (inline_summary_t, gc, inline_summary_vec);
3954 inline_summary_vec = NULL;
3955 VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
3956 inline_edge_summary_vec = NULL;
3957 if (edge_predicate_pool)
3958 free_alloc_pool (edge_predicate_pool);
3959 edge_predicate_pool = 0;