ipa-inline-analysis.c (reset_inline_edge_summary): New function.
[gcc.git] / gcc / ipa-inline-analysis.c
1 /* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 /* Analysis used by the inliner and other passes limiting code size growth.
23
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
33
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).
38
39 We provide accestor to the inline_summary datastructure and
40 basic logic updating the parameters when inlining is performed.
41
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.
52
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.
59
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.
63
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. */
67
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 "timevar.h"
80 #include "params.h"
81 #include "tree-pass.h"
82 #include "coverage.h"
83 #include "ggc.h"
84 #include "tree-flow.h"
85 #include "ipa-prop.h"
86 #include "lto-streamer.h"
87 #include "data-streamer.h"
88 #include "tree-streamer.h"
89 #include "ipa-inline.h"
90 #include "alloc-pool.h"
91
92 /* Estimate runtime of function can easilly run into huge numbers with many
93 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE in integer.
94 For anything larger we use gcov_type. */
95 #define MAX_TIME 500000
96
97 /* Number of bits in integer, but we really want to be stable across different
98 hosts. */
99 #define NUM_CONDITIONS 32
100
101 enum predicate_conditions
102 {
103 predicate_false_condition = 0,
104 predicate_not_inlined_condition = 1,
105 predicate_first_dynamic_condition = 2
106 };
107
108 /* Special condition code we use to represent test that operand is compile time
109 constant. */
110 #define IS_NOT_CONSTANT ERROR_MARK
111 /* Special condition code we use to represent test that operand is not changed
112 across invocation of the function. When operand IS_NOT_CONSTANT it is always
113 CHANGED, however i.e. loop invariants can be NOT_CHANGED given percentage
114 of executions even when they are not compile time constants. */
115 #define CHANGED IDENTIFIER_NODE
116
117 /* Holders of ipa cgraph hooks: */
118 static struct cgraph_node_hook_list *function_insertion_hook_holder;
119 static struct cgraph_node_hook_list *node_removal_hook_holder;
120 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
121 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
122 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
123 static void inline_node_removal_hook (struct cgraph_node *, void *);
124 static void inline_node_duplication_hook (struct cgraph_node *,
125 struct cgraph_node *, void *);
126 static void inline_edge_removal_hook (struct cgraph_edge *, void *);
127 static void inline_edge_duplication_hook (struct cgraph_edge *,
128 struct cgraph_edge *,
129 void *);
130
131 /* VECtor holding inline summaries.
132 In GGC memory because conditions might point to constant trees. */
133 VEC(inline_summary_t,gc) *inline_summary_vec;
134 VEC(inline_edge_summary_t,heap) *inline_edge_summary_vec;
135
136 /* Cached node/edge growths. */
137 VEC(int,heap) *node_growth_cache;
138 VEC(edge_growth_cache_entry,heap) *edge_growth_cache;
139
140 /* Edge predicates goes here. */
141 static alloc_pool edge_predicate_pool;
142
143 /* Return true predicate (tautology).
144 We represent it by empty list of clauses. */
145
146 static inline struct predicate
147 true_predicate (void)
148 {
149 struct predicate p;
150 p.clause[0]=0;
151 return p;
152 }
153
154
155 /* Return predicate testing single condition number COND. */
156
157 static inline struct predicate
158 single_cond_predicate (int cond)
159 {
160 struct predicate p;
161 p.clause[0]=1 << cond;
162 p.clause[1]=0;
163 return p;
164 }
165
166
167 /* Return false predicate. First clause require false condition. */
168
169 static inline struct predicate
170 false_predicate (void)
171 {
172 return single_cond_predicate (predicate_false_condition);
173 }
174
175
176 /* Return true if P is (false). */
177
178 static inline bool
179 true_predicate_p (struct predicate *p)
180 {
181 return !p->clause[0];
182 }
183
184
185 /* Return true if P is (false). */
186
187 static inline bool
188 false_predicate_p (struct predicate *p)
189 {
190 if (p->clause[0] == (1 << predicate_false_condition))
191 {
192 gcc_checking_assert (!p->clause[1]
193 && p->clause[0] == 1 << predicate_false_condition);
194 return true;
195 }
196 return false;
197 }
198
199
200 /* Return predicate that is set true when function is not inlined. */
201 static inline struct predicate
202 not_inlined_predicate (void)
203 {
204 return single_cond_predicate (predicate_not_inlined_condition);
205 }
206
207
208 /* Add condition to condition list CONDS. */
209
210 static struct predicate
211 add_condition (struct inline_summary *summary, int operand_num,
212 enum tree_code code, tree val)
213 {
214 int i;
215 struct condition *c;
216 struct condition new_cond;
217
218 for (i = 0; VEC_iterate (condition, summary->conds, i, c); i++)
219 {
220 if (c->operand_num == operand_num
221 && c->code == code
222 && c->val == val)
223 return single_cond_predicate (i + predicate_first_dynamic_condition);
224 }
225 /* Too many conditions. Give up and return constant true. */
226 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
227 return true_predicate ();
228
229 new_cond.operand_num = operand_num;
230 new_cond.code = code;
231 new_cond.val = val;
232 VEC_safe_push (condition, gc, summary->conds, &new_cond);
233 return single_cond_predicate (i + predicate_first_dynamic_condition);
234 }
235
236
237 /* Add clause CLAUSE into the predicate P. */
238
239 static inline void
240 add_clause (conditions conditions, struct predicate *p, clause_t clause)
241 {
242 int i;
243 int i2;
244 int insert_here = -1;
245 int c1, c2;
246
247 /* True clause. */
248 if (!clause)
249 return;
250
251 /* False clause makes the whole predicate false. Kill the other variants. */
252 if (clause == (1 << predicate_false_condition))
253 {
254 p->clause[0] = (1 << predicate_false_condition);
255 p->clause[1] = 0;
256 return;
257 }
258 if (false_predicate_p (p))
259 return;
260
261 /* No one should be sily enough to add false into nontrivial clauses. */
262 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
263
264 /* Look where to insert the clause. At the same time prune out
265 clauses of P that are implied by the new clause and thus
266 redundant. */
267 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
268 {
269 p->clause[i2] = p->clause[i];
270
271 if (!p->clause[i])
272 break;
273
274 /* If p->clause[i] implies clause, there is nothing to add. */
275 if ((p->clause[i] & clause) == p->clause[i])
276 {
277 /* We had nothing to add, none of clauses should've become
278 redundant. */
279 gcc_checking_assert (i == i2);
280 return;
281 }
282
283 if (p->clause[i] < clause && insert_here < 0)
284 insert_here = i2;
285
286 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
287 Otherwise the p->clause[i] has to stay. */
288 if ((p->clause[i] & clause) != clause)
289 i2++;
290 }
291
292 /* Look for clauses that are obviously true. I.e.
293 op0 == 5 || op0 != 5. */
294 for (c1 = predicate_first_dynamic_condition; c1 < NUM_CONDITIONS; c1++)
295 {
296 condition *cc1;
297 if (!(clause & (1 << c1)))
298 continue;
299 cc1 = VEC_index (condition,
300 conditions,
301 c1 - predicate_first_dynamic_condition);
302 /* We have no way to represent !CHANGED and !IS_NOT_CONSTANT
303 and thus there is no point for looking for them. */
304 if (cc1->code == CHANGED
305 || cc1->code == IS_NOT_CONSTANT)
306 continue;
307 for (c2 = c1 + 1; c2 <= NUM_CONDITIONS; c2++)
308 if (clause & (1 << c2))
309 {
310 condition *cc1 = VEC_index (condition,
311 conditions,
312 c1 - predicate_first_dynamic_condition);
313 condition *cc2 = VEC_index (condition,
314 conditions,
315 c2 - predicate_first_dynamic_condition);
316 if (cc1->operand_num == cc2->operand_num
317 && cc1->val == cc2->val
318 && cc2->code != IS_NOT_CONSTANT
319 && cc2->code != CHANGED
320 && cc1->code == invert_tree_comparison
321 (cc2->code,
322 HONOR_NANS (TYPE_MODE (TREE_TYPE (cc1->val)))))
323 return;
324 }
325 }
326
327
328 /* We run out of variants. Be conservative in positive direction. */
329 if (i2 == MAX_CLAUSES)
330 return;
331 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
332 p->clause[i2 + 1] = 0;
333 if (insert_here >= 0)
334 for (;i2 > insert_here; i2--)
335 p->clause[i2] = p->clause[i2 - 1];
336 else
337 insert_here = i2;
338 p->clause[insert_here] = clause;
339 }
340
341
342 /* Return P & P2. */
343
344 static struct predicate
345 and_predicates (conditions conditions,
346 struct predicate *p, struct predicate *p2)
347 {
348 struct predicate out = *p;
349 int i;
350
351 /* Avoid busy work. */
352 if (false_predicate_p (p2) || true_predicate_p (p))
353 return *p2;
354 if (false_predicate_p (p) || true_predicate_p (p2))
355 return *p;
356
357 /* See how far predicates match. */
358 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
359 {
360 gcc_checking_assert (i < MAX_CLAUSES);
361 }
362
363 /* Combine the predicates rest. */
364 for (; p2->clause[i]; i++)
365 {
366 gcc_checking_assert (i < MAX_CLAUSES);
367 add_clause (conditions, &out, p2->clause[i]);
368 }
369 return out;
370 }
371
372
373 /* Return true if predicates are obviously equal. */
374
375 static inline bool
376 predicates_equal_p (struct predicate *p, struct predicate *p2)
377 {
378 int i;
379 for (i = 0; p->clause[i]; i++)
380 {
381 gcc_checking_assert (i < MAX_CLAUSES);
382 gcc_checking_assert (p->clause [i] > p->clause[i + 1]);
383 gcc_checking_assert (!p2->clause[i]
384 || p2->clause [i] > p2->clause[i + 1]);
385 if (p->clause[i] != p2->clause[i])
386 return false;
387 }
388 return !p2->clause[i];
389 }
390
391
392 /* Return P | P2. */
393
394 static struct predicate
395 or_predicates (conditions conditions, struct predicate *p, struct predicate *p2)
396 {
397 struct predicate out = true_predicate ();
398 int i,j;
399
400 /* Avoid busy work. */
401 if (false_predicate_p (p2) || true_predicate_p (p))
402 return *p;
403 if (false_predicate_p (p) || true_predicate_p (p2))
404 return *p2;
405 if (predicates_equal_p (p, p2))
406 return *p;
407
408 /* OK, combine the predicates. */
409 for (i = 0; p->clause[i]; i++)
410 for (j = 0; p2->clause[j]; j++)
411 {
412 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
413 add_clause (conditions, &out, p->clause[i] | p2->clause[j]);
414 }
415 return out;
416 }
417
418
419 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
420 if predicate P is known to be false. */
421
422 static bool
423 evaluate_predicate (struct predicate *p, clause_t possible_truths)
424 {
425 int i;
426
427 /* True remains true. */
428 if (true_predicate_p (p))
429 return true;
430
431 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
432
433 /* See if we can find clause we can disprove. */
434 for (i = 0; p->clause[i]; i++)
435 {
436 gcc_checking_assert (i < MAX_CLAUSES);
437 if (!(p->clause[i] & possible_truths))
438 return false;
439 }
440 return true;
441 }
442
443 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
444 instruction will be recomputed per invocation of the inlined call. */
445
446 static int
447 predicate_probability (conditions conds,
448 struct predicate *p, clause_t possible_truths,
449 VEC (inline_param_summary_t, heap) *inline_param_summary)
450 {
451 int i;
452 int combined_prob = REG_BR_PROB_BASE;
453
454 /* True remains true. */
455 if (true_predicate_p (p))
456 return REG_BR_PROB_BASE;
457
458 if (false_predicate_p (p))
459 return 0;
460
461 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
462
463 /* See if we can find clause we can disprove. */
464 for (i = 0; p->clause[i]; i++)
465 {
466 gcc_checking_assert (i < MAX_CLAUSES);
467 if (!(p->clause[i] & possible_truths))
468 return 0;
469 else
470 {
471 int this_prob = 0;
472 int i2;
473 if (!inline_param_summary)
474 return REG_BR_PROB_BASE;
475 for (i2 = 0; i2 < NUM_CONDITIONS; i2++)
476 if ((p->clause[i] & possible_truths) & (1 << i2))
477 {
478 if (i2 >= predicate_first_dynamic_condition)
479 {
480 condition *c = VEC_index
481 (condition, conds,
482 i2 - predicate_first_dynamic_condition);
483 if (c->code == CHANGED
484 && (c->operand_num
485 < (int) VEC_length (inline_param_summary_t,
486 inline_param_summary)))
487 {
488 int iprob = VEC_index (inline_param_summary_t,
489 inline_param_summary,
490 c->operand_num)->change_prob;
491 this_prob = MAX (this_prob, iprob);
492 }
493 else
494 this_prob = REG_BR_PROB_BASE;
495 }
496 else
497 this_prob = REG_BR_PROB_BASE;
498 }
499 combined_prob = MIN (this_prob, combined_prob);
500 if (!combined_prob)
501 return 0;
502 }
503 }
504 return combined_prob;
505 }
506
507
508 /* Dump conditional COND. */
509
510 static void
511 dump_condition (FILE *f, conditions conditions, int cond)
512 {
513 condition *c;
514 if (cond == predicate_false_condition)
515 fprintf (f, "false");
516 else if (cond == predicate_not_inlined_condition)
517 fprintf (f, "not inlined");
518 else
519 {
520 c = VEC_index (condition, conditions,
521 cond - predicate_first_dynamic_condition);
522 fprintf (f, "op%i", c->operand_num);
523 if (c->code == IS_NOT_CONSTANT)
524 {
525 fprintf (f, " not constant");
526 return;
527 }
528 if (c->code == CHANGED)
529 {
530 fprintf (f, " changed");
531 return;
532 }
533 fprintf (f, " %s ", op_symbol_code (c->code));
534 print_generic_expr (f, c->val, 1);
535 }
536 }
537
538
539 /* Dump clause CLAUSE. */
540
541 static void
542 dump_clause (FILE *f, conditions conds, clause_t clause)
543 {
544 int i;
545 bool found = false;
546 fprintf (f, "(");
547 if (!clause)
548 fprintf (f, "true");
549 for (i = 0; i < NUM_CONDITIONS; i++)
550 if (clause & (1 << i))
551 {
552 if (found)
553 fprintf (f, " || ");
554 found = true;
555 dump_condition (f, conds, i);
556 }
557 fprintf (f, ")");
558 }
559
560
561 /* Dump predicate PREDICATE. */
562
563 static void
564 dump_predicate (FILE *f, conditions conds, struct predicate *pred)
565 {
566 int i;
567 if (true_predicate_p (pred))
568 dump_clause (f, conds, 0);
569 else
570 for (i = 0; pred->clause[i]; i++)
571 {
572 if (i)
573 fprintf (f, " && ");
574 dump_clause (f, conds, pred->clause[i]);
575 }
576 fprintf (f, "\n");
577 }
578
579
580 /* Record SIZE and TIME under condition PRED into the inline summary. */
581
582 static void
583 account_size_time (struct inline_summary *summary, int size, int time,
584 struct predicate *pred)
585 {
586 size_time_entry *e;
587 bool found = false;
588 int i;
589
590 if (false_predicate_p (pred))
591 return;
592
593 /* We need to create initial empty unconitional clause, but otherwie
594 we don't need to account empty times and sizes. */
595 if (!size && !time && summary->entry)
596 return;
597
598 /* Watch overflow that might result from insane profiles. */
599 if (time > MAX_TIME * INLINE_TIME_SCALE)
600 time = MAX_TIME * INLINE_TIME_SCALE;
601 gcc_assert (time >= 0);
602
603 for (i = 0; VEC_iterate (size_time_entry, summary->entry, i, e); i++)
604 if (predicates_equal_p (&e->predicate, pred))
605 {
606 found = true;
607 break;
608 }
609 if (i == 32)
610 {
611 i = 0;
612 found = true;
613 e = VEC_index (size_time_entry, summary->entry, 0);
614 gcc_assert (!e->predicate.clause[0]);
615 }
616 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
617 {
618 fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
619 ((double)size) / INLINE_SIZE_SCALE,
620 ((double)time) / INLINE_TIME_SCALE,
621 found ? "" : "new ");
622 dump_predicate (dump_file, summary->conds, pred);
623 }
624 if (!found)
625 {
626 struct size_time_entry new_entry;
627 new_entry.size = size;
628 new_entry.time = time;
629 new_entry.predicate = *pred;
630 VEC_safe_push (size_time_entry, gc, summary->entry, &new_entry);
631 }
632 else
633 {
634 e->size += size;
635 e->time += time;
636 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
637 e->time = MAX_TIME * INLINE_TIME_SCALE;
638 }
639 }
640
641 /* Set predicate for edge E. */
642
643 static void
644 edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
645 {
646 struct inline_edge_summary *es = inline_edge_summary (e);
647 if (predicate && !true_predicate_p (predicate))
648 {
649 if (!es->predicate)
650 es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
651 *es->predicate = *predicate;
652 }
653 else
654 {
655 if (es->predicate)
656 pool_free (edge_predicate_pool, es->predicate);
657 es->predicate = NULL;
658 }
659 }
660
661
662 /* KNOWN_VALS is partial mapping of parameters of NODE to constant values.
663 Return clause of possible truths. When INLINE_P is true, assume that
664 we are inlining.
665
666 ERROR_MARK means compile time invariant. */
667
668 static clause_t
669 evaluate_conditions_for_known_args (struct cgraph_node *node,
670 bool inline_p,
671 VEC (tree, heap) *known_vals)
672 {
673 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
674 struct inline_summary *info = inline_summary (node);
675 int i;
676 struct condition *c;
677
678 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
679 {
680 tree val;
681 tree res;
682
683 /* We allow call stmt to have fewer arguments than the callee
684 function (especially for K&R style programs). So bound
685 check here. */
686 if (c->operand_num < (int)VEC_length (tree, known_vals))
687 val = VEC_index (tree, known_vals, c->operand_num);
688 else
689 val = NULL;
690
691 if (val == error_mark_node && c->code != CHANGED)
692 val = NULL;
693
694 if (!val)
695 {
696 clause |= 1 << (i + predicate_first_dynamic_condition);
697 continue;
698 }
699 if (c->code == IS_NOT_CONSTANT || c->code == CHANGED)
700 continue;
701 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
702 if (res
703 && integer_zerop (res))
704 continue;
705 clause |= 1 << (i + predicate_first_dynamic_condition);
706 }
707 return clause;
708 }
709
710
711 /* Work out what conditions might be true at invocation of E. */
712
713 static clause_t
714 evaluate_conditions_for_edge (struct cgraph_edge *e, bool inline_p)
715 {
716 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
717 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
718 struct inline_summary *info = inline_summary (callee);
719 int i;
720
721 if (ipa_node_params_vector && info->conds)
722 {
723 struct ipa_node_params *parms_info;
724 struct ipa_edge_args *args = IPA_EDGE_REF (e);
725 struct inline_edge_summary *es = inline_edge_summary (e);
726 int i, count = ipa_get_cs_argument_count (args);
727 VEC (tree, heap) *known_vals = NULL;
728
729 if (e->caller->global.inlined_to)
730 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
731 else
732 parms_info = IPA_NODE_REF (e->caller);
733
734 if (count)
735 VEC_safe_grow_cleared (tree, heap, known_vals, count);
736 for (i = 0; i < count; i++)
737 {
738 tree cst = ipa_cst_from_jfunc (parms_info,
739 ipa_get_ith_jump_func (args, i));
740 if (cst)
741 VEC_replace (tree, known_vals, i, cst);
742 else if (inline_p
743 && !VEC_index (inline_param_summary_t,
744 es->param,
745 i)->change_prob)
746 VEC_replace (tree, known_vals, i, error_mark_node);
747 }
748 clause = evaluate_conditions_for_known_args (callee,
749 inline_p, known_vals);
750 VEC_free (tree, heap, known_vals);
751 }
752 else
753 for (i = 0; i < (int)VEC_length (condition, info->conds); i++)
754 clause |= 1 << (i + predicate_first_dynamic_condition);
755
756 return clause;
757 }
758
759
760 /* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
761
762 static void
763 inline_summary_alloc (void)
764 {
765 if (!node_removal_hook_holder)
766 node_removal_hook_holder =
767 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
768 if (!edge_removal_hook_holder)
769 edge_removal_hook_holder =
770 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
771 if (!node_duplication_hook_holder)
772 node_duplication_hook_holder =
773 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
774 if (!edge_duplication_hook_holder)
775 edge_duplication_hook_holder =
776 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
777
778 if (VEC_length (inline_summary_t, inline_summary_vec)
779 <= (unsigned) cgraph_max_uid)
780 VEC_safe_grow_cleared (inline_summary_t, gc,
781 inline_summary_vec, cgraph_max_uid + 1);
782 if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
783 <= (unsigned) cgraph_edge_max_uid)
784 VEC_safe_grow_cleared (inline_edge_summary_t, heap,
785 inline_edge_summary_vec, cgraph_edge_max_uid + 1);
786 if (!edge_predicate_pool)
787 edge_predicate_pool = create_alloc_pool ("edge predicates",
788 sizeof (struct predicate),
789 10);
790 }
791
792 /* We are called multiple time for given function; clear
793 data from previous run so they are not cumulated. */
794
795 static void
796 reset_inline_edge_summary (struct cgraph_edge *e)
797 {
798 struct inline_edge_summary *es = inline_edge_summary (e);
799
800 es->call_stmt_size = es->call_stmt_time =0;
801 if (es->predicate)
802 pool_free (edge_predicate_pool, es->predicate);
803 es->predicate = NULL;
804 VEC_free (inline_param_summary_t, heap, es->param);
805 }
806
807 /* We are called multiple time for given function; clear
808 data from previous run so they are not cumulated. */
809
810 static void
811 reset_inline_summary (struct cgraph_node *node)
812 {
813 struct inline_summary *info = inline_summary (node);
814 struct cgraph_edge *e;
815
816 info->self_size = info->self_time = 0;
817 info->estimated_stack_size = 0;
818 info->estimated_self_stack_size = 0;
819 info->stack_frame_offset = 0;
820 info->size = 0;
821 info->time = 0;
822 VEC_free (condition, gc, info->conds);
823 VEC_free (size_time_entry,gc, info->entry);
824 for (e = node->callees; e; e = e->next_callee)
825 reset_inline_edge_summary (e);
826 for (e = node->indirect_calls; e; e = e->next_callee)
827 reset_inline_edge_summary (e);
828 }
829
830 /* Hook that is called by cgraph.c when a node is removed. */
831
832 static void
833 inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
834 {
835 struct inline_summary *info;
836 if (VEC_length (inline_summary_t, inline_summary_vec)
837 <= (unsigned)node->uid)
838 return;
839 info = inline_summary (node);
840 reset_inline_summary (node);
841 memset (info, 0, sizeof (inline_summary_t));
842 }
843
844
845 /* Hook that is called by cgraph.c when a node is duplicated. */
846
847 static void
848 inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
849 ATTRIBUTE_UNUSED void *data)
850 {
851 struct inline_summary *info;
852 inline_summary_alloc ();
853 info = inline_summary (dst);
854 memcpy (info, inline_summary (src),
855 sizeof (struct inline_summary));
856 /* TODO: as an optimization, we may avoid copying conditions
857 that are known to be false or true. */
858 info->conds = VEC_copy (condition, gc, info->conds);
859
860 /* When there are any replacements in the function body, see if we can figure
861 out that something was optimized out. */
862 if (ipa_node_params_vector && dst->clone.tree_map)
863 {
864 VEC(size_time_entry,gc) *entry = info->entry;
865 /* Use SRC parm info since it may not be copied yet. */
866 struct ipa_node_params *parms_info = IPA_NODE_REF (src);
867 VEC (tree, heap) *known_vals = NULL;
868 int count = ipa_get_param_count (parms_info);
869 int i,j;
870 clause_t possible_truths;
871 struct predicate true_pred = true_predicate ();
872 size_time_entry *e;
873 int optimized_out_size = 0;
874 gcov_type optimized_out_time = 0;
875 bool inlined_to_p = false;
876 struct cgraph_edge *edge;
877
878 info->entry = 0;
879 VEC_safe_grow_cleared (tree, heap, known_vals, count);
880 for (i = 0; i < count; i++)
881 {
882 tree t = ipa_get_param (parms_info, i);
883 struct ipa_replace_map *r;
884
885 for (j = 0;
886 VEC_iterate (ipa_replace_map_p, dst->clone.tree_map, j, r);
887 j++)
888 {
889 if (r->old_tree == t
890 && r->replace_p
891 && !r->ref_p)
892 {
893 VEC_replace (tree, known_vals, i, r->new_tree);
894 break;
895 }
896 }
897 }
898 possible_truths = evaluate_conditions_for_known_args (dst,
899 false, known_vals);
900 VEC_free (tree, heap, known_vals);
901
902 account_size_time (info, 0, 0, &true_pred);
903
904 /* Remap size_time vectors.
905 Simplify the predicate by prunning out alternatives that are known
906 to be false.
907 TODO: as on optimization, we can also eliminate conditions known
908 to be true. */
909 for (i = 0; VEC_iterate (size_time_entry, entry, i, e); i++)
910 {
911 struct predicate new_predicate = true_predicate ();
912 for (j = 0; e->predicate.clause[j]; j++)
913 if (!(possible_truths & e->predicate.clause[j]))
914 {
915 new_predicate = false_predicate ();
916 break;
917 }
918 else
919 add_clause (info->conds, &new_predicate,
920 possible_truths & e->predicate.clause[j]);
921 if (false_predicate_p (&new_predicate))
922 {
923 optimized_out_size += e->size;
924 optimized_out_time += e->time;
925 }
926 else
927 account_size_time (info, e->size, e->time, &new_predicate);
928 }
929
930 /* Remap edge predicates with the same simplification as above.
931 Also copy constantness arrays. */
932 for (edge = dst->callees; edge; edge = edge->next_callee)
933 {
934 struct predicate new_predicate = true_predicate ();
935 struct inline_edge_summary *es = inline_edge_summary (edge);
936
937 if (!edge->inline_failed)
938 inlined_to_p = true;
939 if (!es->predicate)
940 continue;
941 for (j = 0; es->predicate->clause[j]; j++)
942 if (!(possible_truths & es->predicate->clause[j]))
943 {
944 new_predicate = false_predicate ();
945 break;
946 }
947 else
948 add_clause (info->conds, &new_predicate,
949 possible_truths & es->predicate->clause[j]);
950 if (false_predicate_p (&new_predicate)
951 && !false_predicate_p (es->predicate))
952 {
953 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
954 optimized_out_time += (es->call_stmt_time
955 * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
956 * edge->frequency);
957 edge->frequency = 0;
958 }
959 *es->predicate = new_predicate;
960 }
961
962 /* Remap indirect edge predicates with the same simplificaiton as above.
963 Also copy constantness arrays. */
964 for (edge = dst->indirect_calls; edge; edge = edge->next_callee)
965 {
966 struct predicate new_predicate = true_predicate ();
967 struct inline_edge_summary *es = inline_edge_summary (edge);
968
969 if (!edge->inline_failed)
970 inlined_to_p = true;
971 if (!es->predicate)
972 continue;
973 for (j = 0; es->predicate->clause[j]; j++)
974 if (!(possible_truths & es->predicate->clause[j]))
975 {
976 new_predicate = false_predicate ();
977 break;
978 }
979 else
980 add_clause (info->conds, &new_predicate,
981 possible_truths & es->predicate->clause[j]);
982 if (false_predicate_p (&new_predicate)
983 && !false_predicate_p (es->predicate))
984 {
985 optimized_out_size += es->call_stmt_size * INLINE_SIZE_SCALE;
986 optimized_out_time += (es->call_stmt_time
987 * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE)
988 * edge->frequency);
989 edge->frequency = 0;
990 }
991 *es->predicate = new_predicate;
992 }
993
994 /* If inliner or someone after inliner will ever start producing
995 non-trivial clones, we will get trouble with lack of information
996 about updating self sizes, because size vectors already contains
997 sizes of the calees. */
998 gcc_assert (!inlined_to_p
999 || (!optimized_out_size && !optimized_out_time));
1000
1001 info->size -= optimized_out_size / INLINE_SIZE_SCALE;
1002 info->self_size -= optimized_out_size / INLINE_SIZE_SCALE;
1003 gcc_assert (info->size > 0);
1004 gcc_assert (info->self_size > 0);
1005
1006 optimized_out_time /= INLINE_TIME_SCALE;
1007 if (optimized_out_time > MAX_TIME)
1008 optimized_out_time = MAX_TIME;
1009 info->time -= optimized_out_time;
1010 info->self_time -= optimized_out_time;
1011 if (info->time < 0)
1012 info->time = 0;
1013 if (info->self_time < 0)
1014 info->self_time = 0;
1015 }
1016 else
1017 info->entry = VEC_copy (size_time_entry, gc, info->entry);
1018 }
1019
1020
1021 /* Hook that is called by cgraph.c when a node is duplicated. */
1022
1023 static void
1024 inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1025 ATTRIBUTE_UNUSED void *data)
1026 {
1027 struct inline_edge_summary *info;
1028 struct inline_edge_summary *srcinfo;
1029 inline_summary_alloc ();
1030 info = inline_edge_summary (dst);
1031 srcinfo = inline_edge_summary (src);
1032 memcpy (info, srcinfo,
1033 sizeof (struct inline_edge_summary));
1034 info->predicate = NULL;
1035 edge_set_predicate (dst, srcinfo->predicate);
1036 info->param = VEC_copy (inline_param_summary_t, heap, srcinfo->param);
1037 }
1038
1039
1040 /* Keep edge cache consistent across edge removal. */
1041
1042 static void
1043 inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
1044 {
1045 if (edge_growth_cache)
1046 reset_edge_growth_cache (edge);
1047 if (edge->uid
1048 < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
1049 reset_inline_edge_summary (edge);
1050 }
1051
1052
1053 /* Initialize growth caches. */
1054
1055 void
1056 initialize_growth_caches (void)
1057 {
1058 if (cgraph_edge_max_uid)
1059 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
1060 cgraph_edge_max_uid);
1061 if (cgraph_max_uid)
1062 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
1063 }
1064
1065
1066 /* Free growth caches. */
1067
1068 void
1069 free_growth_caches (void)
1070 {
1071 VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
1072 edge_growth_cache = 0;
1073 VEC_free (int, heap, node_growth_cache);
1074 node_growth_cache = 0;
1075 }
1076
1077
1078 /* Dump edge summaries associated to NODE and recursively to all clones.
1079 Indent by INDENT. */
1080
1081 static void
1082 dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
1083 struct inline_summary *info)
1084 {
1085 struct cgraph_edge *edge;
1086 for (edge = node->callees; edge; edge = edge->next_callee)
1087 {
1088 struct inline_edge_summary *es = inline_edge_summary (edge);
1089 struct cgraph_node *callee = cgraph_function_or_thunk_node (edge->callee, NULL);
1090 int i;
1091
1092 fprintf (f, "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i time: %2i callee size:%2i stack:%2i",
1093 indent, "", cgraph_node_name (callee),
1094 callee->uid,
1095 !edge->inline_failed ? "inlined"
1096 : cgraph_inline_failed_string (edge->inline_failed),
1097 indent, "",
1098 es->loop_depth,
1099 edge->frequency,
1100 es->call_stmt_size,
1101 es->call_stmt_time,
1102 (int)inline_summary (callee)->size / INLINE_SIZE_SCALE,
1103 (int)inline_summary (callee)->estimated_stack_size);
1104
1105 if (es->predicate)
1106 {
1107 fprintf (f, " predicate: ");
1108 dump_predicate (f, info->conds, es->predicate);
1109 }
1110 else
1111 fprintf (f, "\n");
1112 if (es->param)
1113 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param);
1114 i++)
1115 {
1116 int prob = VEC_index (inline_param_summary_t,
1117 es->param, i)->change_prob;
1118
1119 if (!prob)
1120 fprintf (f, "%*s op%i is compile time invariant\n",
1121 indent + 2, "", i);
1122 else if (prob != REG_BR_PROB_BASE)
1123 fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i,
1124 prob * 100.0 / REG_BR_PROB_BASE);
1125 }
1126 if (!edge->inline_failed)
1127 {
1128 fprintf (f, "%*sStack frame offset %i, callee self size %i,"
1129 " callee size %i\n",
1130 indent+2, "",
1131 (int)inline_summary (callee)->stack_frame_offset,
1132 (int)inline_summary (callee)->estimated_self_stack_size,
1133 (int)inline_summary (callee)->estimated_stack_size);
1134 dump_inline_edge_summary (f, indent+2, callee, info);
1135 }
1136 }
1137 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
1138 {
1139 struct inline_edge_summary *es = inline_edge_summary (edge);
1140 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i"
1141 " time: %2i",
1142 indent, "",
1143 es->loop_depth,
1144 edge->frequency,
1145 es->call_stmt_size,
1146 es->call_stmt_time);
1147 if (es->predicate)
1148 {
1149 fprintf (f, "predicate: ");
1150 dump_predicate (f, info->conds, es->predicate);
1151 }
1152 else
1153 fprintf (f, "\n");
1154 }
1155 }
1156
1157
1158 void
1159 dump_inline_summary (FILE * f, struct cgraph_node *node)
1160 {
1161 if (node->analyzed)
1162 {
1163 struct inline_summary *s = inline_summary (node);
1164 size_time_entry *e;
1165 int i;
1166 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
1167 node->uid);
1168 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
1169 fprintf (f, " always_inline");
1170 if (s->inlinable)
1171 fprintf (f, " inlinable");
1172 fprintf (f, "\n self time: %i\n",
1173 s->self_time);
1174 fprintf (f, " global time: %i\n", s->time);
1175 fprintf (f, " self size: %i\n",
1176 s->self_size);
1177 fprintf (f, " global size: %i\n", s->size);
1178 fprintf (f, " self stack: %i\n",
1179 (int) s->estimated_self_stack_size);
1180 fprintf (f, " global stack: %i\n",
1181 (int) s->estimated_stack_size);
1182 for (i = 0;
1183 VEC_iterate (size_time_entry, s->entry, i, e);
1184 i++)
1185 {
1186 fprintf (f, " size:%f, time:%f, predicate:",
1187 (double) e->size / INLINE_SIZE_SCALE,
1188 (double) e->time / INLINE_TIME_SCALE);
1189 dump_predicate (f, s->conds, &e->predicate);
1190 }
1191 fprintf (f, " calls:\n");
1192 dump_inline_edge_summary (f, 4, node, s);
1193 fprintf (f, "\n");
1194 }
1195 }
1196
1197 DEBUG_FUNCTION void
1198 debug_inline_summary (struct cgraph_node *node)
1199 {
1200 dump_inline_summary (stderr, node);
1201 }
1202
1203 void
1204 dump_inline_summaries (FILE *f)
1205 {
1206 struct cgraph_node *node;
1207
1208 for (node = cgraph_nodes; node; node = node->next)
1209 if (node->analyzed && !node->global.inlined_to)
1210 dump_inline_summary (f, node);
1211 }
1212
1213 /* Give initial reasons why inlining would fail on EDGE. This gets either
1214 nullified or usually overwritten by more precise reasons later. */
1215
1216 void
1217 initialize_inline_failed (struct cgraph_edge *e)
1218 {
1219 struct cgraph_node *callee = e->callee;
1220
1221 if (e->indirect_unknown_callee)
1222 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
1223 else if (!callee->analyzed)
1224 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
1225 else if (callee->local.redefined_extern_inline)
1226 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
1227 else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
1228 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
1229 else
1230 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
1231 }
1232
1233 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1234 boolean variable pointed to by DATA. */
1235
1236 static bool
1237 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1238 void *data)
1239 {
1240 bool *b = (bool *) data;
1241 *b = true;
1242 return true;
1243 }
1244
1245 /* If OP reffers to value of function parameter, return
1246 the corresponding parameter. */
1247
1248 static tree
1249 unmodified_parm (gimple stmt, tree op)
1250 {
1251 /* SSA_NAME referring to parm default def? */
1252 if (TREE_CODE (op) == SSA_NAME
1253 && SSA_NAME_IS_DEFAULT_DEF (op)
1254 && TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL)
1255 return SSA_NAME_VAR (op);
1256 /* Non-SSA parm reference? */
1257 if (TREE_CODE (op) == PARM_DECL)
1258 {
1259 bool modified = false;
1260
1261 ao_ref refd;
1262 ao_ref_init (&refd, op);
1263 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
1264 NULL);
1265 if (!modified)
1266 return op;
1267 }
1268 /* Assignment from a parameter? */
1269 if (TREE_CODE (op) == SSA_NAME
1270 && !SSA_NAME_IS_DEFAULT_DEF (op)
1271 && gimple_assign_single_p (SSA_NAME_DEF_STMT (op)))
1272 return unmodified_parm (SSA_NAME_DEF_STMT (op),
1273 gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)));
1274 return NULL;
1275 }
1276
1277 /* See if statement might disappear after inlining.
1278 0 - means not eliminated
1279 1 - half of statements goes away
1280 2 - for sure it is eliminated.
1281 We are not terribly sophisticated, basically looking for simple abstraction
1282 penalty wrappers. */
1283
1284 static int
1285 eliminated_by_inlining_prob (gimple stmt)
1286 {
1287 enum gimple_code code = gimple_code (stmt);
1288
1289 if (!optimize)
1290 return 0;
1291
1292 switch (code)
1293 {
1294 case GIMPLE_RETURN:
1295 return 2;
1296 case GIMPLE_ASSIGN:
1297 if (gimple_num_ops (stmt) != 2)
1298 return 0;
1299
1300 /* Casts of parameters, loads from parameters passed by reference
1301 and stores to return value or parameters are often free after
1302 inlining dua to SRA and further combining.
1303 Assume that half of statements goes away. */
1304 if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
1305 || gimple_assign_rhs_code (stmt) == NOP_EXPR
1306 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
1307 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1308 {
1309 tree rhs = gimple_assign_rhs1 (stmt);
1310 tree lhs = gimple_assign_lhs (stmt);
1311 tree inner_rhs = get_base_address (rhs);
1312 tree inner_lhs = get_base_address (lhs);
1313 bool rhs_free = false;
1314 bool lhs_free = false;
1315
1316 if (!inner_rhs)
1317 inner_rhs = rhs;
1318 if (!inner_lhs)
1319 inner_lhs = lhs;
1320
1321 /* Reads of parameter are expected to be free. */
1322 if (unmodified_parm (stmt, inner_rhs))
1323 rhs_free = true;
1324
1325 /* When parameter is not SSA register because its address is taken
1326 and it is just copied into one, the statement will be completely
1327 free after inlining (we will copy propagate backward). */
1328 if (rhs_free && is_gimple_reg (lhs))
1329 return 2;
1330
1331 /* Reads of parameters passed by reference
1332 expected to be free (i.e. optimized out after inlining). */
1333 if (TREE_CODE(inner_rhs) == MEM_REF
1334 && unmodified_parm (stmt, TREE_OPERAND (inner_rhs, 0)))
1335 rhs_free = true;
1336
1337 /* Copying parameter passed by reference into gimple register is
1338 probably also going to copy propagate, but we can't be quite
1339 sure. */
1340 if (rhs_free && is_gimple_reg (lhs))
1341 lhs_free = true;
1342
1343 /* Writes to parameters, parameters passed by value and return value
1344 (either dirrectly or passed via invisible reference) are free.
1345
1346 TODO: We ought to handle testcase like
1347 struct a {int a,b;};
1348 struct a
1349 retrurnsturct (void)
1350 {
1351 struct a a ={1,2};
1352 return a;
1353 }
1354
1355 This translate into:
1356
1357 retrurnsturct ()
1358 {
1359 int a$b;
1360 int a$a;
1361 struct a a;
1362 struct a D.2739;
1363
1364 <bb 2>:
1365 D.2739.a = 1;
1366 D.2739.b = 2;
1367 return D.2739;
1368
1369 }
1370 For that we either need to copy ipa-split logic detecting writes
1371 to return value. */
1372 if (TREE_CODE (inner_lhs) == PARM_DECL
1373 || TREE_CODE (inner_lhs) == RESULT_DECL
1374 || (TREE_CODE(inner_lhs) == MEM_REF
1375 && (unmodified_parm (stmt, TREE_OPERAND (inner_lhs, 0))
1376 || (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME
1377 && TREE_CODE (SSA_NAME_VAR
1378 (TREE_OPERAND (inner_lhs, 0)))
1379 == RESULT_DECL))))
1380 lhs_free = true;
1381 if (lhs_free
1382 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
1383 rhs_free = true;
1384 if (lhs_free && rhs_free)
1385 return 1;
1386 }
1387 return 0;
1388 default:
1389 return 0;
1390 }
1391 }
1392
1393
1394 /* If BB ends by a conditional we can turn into predicates, attach corresponding
1395 predicates to the CFG edges. */
1396
1397 static void
1398 set_cond_stmt_execution_predicate (struct ipa_node_params *info,
1399 struct inline_summary *summary,
1400 basic_block bb)
1401 {
1402 gimple last;
1403 tree op;
1404 int index;
1405 enum tree_code code, inverted_code;
1406 edge e;
1407 edge_iterator ei;
1408 gimple set_stmt;
1409 tree op2;
1410 tree parm;
1411 tree base;
1412
1413 last = last_stmt (bb);
1414 if (!last
1415 || gimple_code (last) != GIMPLE_COND)
1416 return;
1417 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
1418 return;
1419 op = gimple_cond_lhs (last);
1420 /* TODO: handle conditionals like
1421 var = op0 < 4;
1422 if (var != 0). */
1423 parm = unmodified_parm (last, op);
1424 if (parm)
1425 {
1426 index = ipa_get_param_decl_index (info, parm);
1427 if (index == -1)
1428 return;
1429 code = gimple_cond_code (last);
1430 inverted_code
1431 = invert_tree_comparison (code,
1432 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
1433
1434 FOR_EACH_EDGE (e, ei, bb->succs)
1435 {
1436 struct predicate p = add_condition (summary,
1437 index,
1438 e->flags & EDGE_TRUE_VALUE
1439 ? code : inverted_code,
1440 gimple_cond_rhs (last));
1441 e->aux = pool_alloc (edge_predicate_pool);
1442 *(struct predicate *)e->aux = p;
1443 }
1444 }
1445
1446 if (TREE_CODE (op) != SSA_NAME)
1447 return;
1448 /* Special case
1449 if (builtin_constant_p (op))
1450 constant_code
1451 else
1452 nonconstant_code.
1453 Here we can predicate nonconstant_code. We can't
1454 really handle constant_code since we have no predicate
1455 for this and also the constant code is not known to be
1456 optimized away when inliner doen't see operand is constant.
1457 Other optimizers might think otherwise. */
1458 set_stmt = SSA_NAME_DEF_STMT (op);
1459 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
1460 || gimple_call_num_args (set_stmt) != 1)
1461 return;
1462 op2 = gimple_call_arg (set_stmt, 0);
1463 base = get_base_address (op2);
1464 parm = unmodified_parm (set_stmt, base ? base : op2);
1465 if (!parm)
1466 return;
1467 index = ipa_get_param_decl_index (info, parm);
1468 if (index == -1)
1469 return;
1470 if (gimple_cond_code (last) != NE_EXPR
1471 || !integer_zerop (gimple_cond_rhs (last)))
1472 return;
1473 FOR_EACH_EDGE (e, ei, bb->succs)
1474 if (e->flags & EDGE_FALSE_VALUE)
1475 {
1476 struct predicate p = add_condition (summary,
1477 index,
1478 IS_NOT_CONSTANT,
1479 NULL);
1480 e->aux = pool_alloc (edge_predicate_pool);
1481 *(struct predicate *)e->aux = p;
1482 }
1483 }
1484
1485
1486 /* If BB ends by a switch we can turn into predicates, attach corresponding
1487 predicates to the CFG edges. */
1488
1489 static void
1490 set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1491 struct inline_summary *summary,
1492 basic_block bb)
1493 {
1494 gimple last;
1495 tree op;
1496 int index;
1497 edge e;
1498 edge_iterator ei;
1499 size_t n;
1500 size_t case_idx;
1501 tree parm;
1502
1503 last = last_stmt (bb);
1504 if (!last
1505 || gimple_code (last) != GIMPLE_SWITCH)
1506 return;
1507 op = gimple_switch_index (last);
1508 parm = unmodified_parm (last, op);
1509 if (!parm)
1510 return;
1511 index = ipa_get_param_decl_index (info, parm);
1512 if (index == -1)
1513 return;
1514
1515 FOR_EACH_EDGE (e, ei, bb->succs)
1516 {
1517 e->aux = pool_alloc (edge_predicate_pool);
1518 *(struct predicate *)e->aux = false_predicate ();
1519 }
1520 n = gimple_switch_num_labels(last);
1521 for (case_idx = 0; case_idx < n; ++case_idx)
1522 {
1523 tree cl = gimple_switch_label (last, case_idx);
1524 tree min, max;
1525 struct predicate p;
1526
1527 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1528 min = CASE_LOW (cl);
1529 max = CASE_HIGH (cl);
1530
1531 /* For default we might want to construct predicate that none
1532 of cases is met, but it is bit hard to do not having negations
1533 of conditionals handy. */
1534 if (!min && !max)
1535 p = true_predicate ();
1536 else if (!max)
1537 p = add_condition (summary, index,
1538 EQ_EXPR,
1539 min);
1540 else
1541 {
1542 struct predicate p1, p2;
1543 p1 = add_condition (summary, index,
1544 GE_EXPR,
1545 min);
1546 p2 = add_condition (summary, index,
1547 LE_EXPR,
1548 max);
1549 p = and_predicates (summary->conds, &p1, &p2);
1550 }
1551 *(struct predicate *)e->aux
1552 = or_predicates (summary->conds, &p, (struct predicate *)e->aux);
1553 }
1554 }
1555
1556
1557 /* For each BB in NODE attach to its AUX pointer predicate under
1558 which it is executable. */
1559
1560 static void
1561 compute_bb_predicates (struct cgraph_node *node,
1562 struct ipa_node_params *parms_info,
1563 struct inline_summary *summary)
1564 {
1565 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1566 bool done = false;
1567 basic_block bb;
1568
1569 FOR_EACH_BB_FN (bb, my_function)
1570 {
1571 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1572 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1573 }
1574
1575 /* Entry block is always executable. */
1576 ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1577 = pool_alloc (edge_predicate_pool);
1578 *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1579 = true_predicate ();
1580
1581 /* A simple dataflow propagation of predicates forward in the CFG.
1582 TODO: work in reverse postorder. */
1583 while (!done)
1584 {
1585 done = true;
1586 FOR_EACH_BB_FN (bb, my_function)
1587 {
1588 struct predicate p = false_predicate ();
1589 edge e;
1590 edge_iterator ei;
1591 FOR_EACH_EDGE (e, ei, bb->preds)
1592 {
1593 if (e->src->aux)
1594 {
1595 struct predicate this_bb_predicate
1596 = *(struct predicate *)e->src->aux;
1597 if (e->aux)
1598 this_bb_predicate
1599 = and_predicates (summary->conds, &this_bb_predicate,
1600 (struct predicate *)e->aux);
1601 p = or_predicates (summary->conds, &p, &this_bb_predicate);
1602 if (true_predicate_p (&p))
1603 break;
1604 }
1605 }
1606 if (false_predicate_p (&p))
1607 gcc_assert (!bb->aux);
1608 else
1609 {
1610 if (!bb->aux)
1611 {
1612 done = false;
1613 bb->aux = pool_alloc (edge_predicate_pool);
1614 *((struct predicate *)bb->aux) = p;
1615 }
1616 else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1617 {
1618 done = false;
1619 *((struct predicate *)bb->aux) = p;
1620 }
1621 }
1622 }
1623 }
1624 }
1625
1626
1627 /* We keep info about constantness of SSA names. */
1628
1629 typedef struct predicate predicate_t;
1630 DEF_VEC_O (predicate_t);
1631 DEF_VEC_ALLOC_O (predicate_t, heap);
1632
1633
1634 /* Return predicate specifying when the STMT might have result that is not
1635 a compile time constant. */
1636
1637 static struct predicate
1638 will_be_nonconstant_predicate (struct ipa_node_params *info,
1639 struct inline_summary *summary,
1640 gimple stmt,
1641 VEC (predicate_t, heap) *nonconstant_names)
1642
1643 {
1644 struct predicate p = true_predicate ();
1645 ssa_op_iter iter;
1646 tree use;
1647 struct predicate op_non_const;
1648 bool is_load;
1649
1650 /* What statments might be optimized away
1651 when their arguments are constant
1652 TODO: also trivial builtins.
1653 builtin_constant_p is already handled later. */
1654 if (gimple_code (stmt) != GIMPLE_ASSIGN
1655 && gimple_code (stmt) != GIMPLE_COND
1656 && gimple_code (stmt) != GIMPLE_SWITCH)
1657 return p;
1658
1659 /* Stores will stay anyway. */
1660 if (gimple_vdef (stmt))
1661 return p;
1662
1663 is_load = gimple_vuse (stmt) != NULL;
1664
1665 /* Loads can be optimized when the value is known. */
1666 if (is_load)
1667 {
1668 tree op = gimple_assign_rhs1 (stmt);
1669 tree base = get_base_address (op);
1670 tree parm;
1671
1672 gcc_assert (gimple_assign_single_p (stmt));
1673 if (!base)
1674 return p;
1675 parm = unmodified_parm (stmt, base);
1676 if (!parm )
1677 return p;
1678 if (ipa_get_param_decl_index (info, parm) < 0)
1679 return p;
1680 }
1681
1682 /* See if we understand all operands before we start
1683 adding conditionals. */
1684 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1685 {
1686 tree parm = unmodified_parm (stmt, use);
1687 /* For arguments we can build a condition. */
1688 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
1689 continue;
1690 if (TREE_CODE (use) != SSA_NAME)
1691 return p;
1692 /* If we know when operand is constant,
1693 we still can say something useful. */
1694 if (!true_predicate_p (VEC_index (predicate_t, nonconstant_names,
1695 SSA_NAME_VERSION (use))))
1696 continue;
1697 return p;
1698 }
1699 op_non_const = false_predicate ();
1700 if (is_load)
1701 {
1702 tree parm = unmodified_parm
1703 (stmt, get_base_address (gimple_assign_rhs1 (stmt)));
1704 p = add_condition (summary,
1705 ipa_get_param_decl_index (info, parm),
1706 CHANGED, NULL);
1707 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
1708 }
1709 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1710 {
1711 tree parm = unmodified_parm (stmt, use);
1712 if (parm && ipa_get_param_decl_index (info, parm) >= 0)
1713 p = add_condition (summary,
1714 ipa_get_param_decl_index (info, parm),
1715 CHANGED, NULL);
1716 else
1717 p = *VEC_index (predicate_t, nonconstant_names,
1718 SSA_NAME_VERSION (use));
1719 op_non_const = or_predicates (summary->conds, &p, &op_non_const);
1720 }
1721 if (gimple_code (stmt) == GIMPLE_ASSIGN
1722 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
1723 VEC_replace (predicate_t, nonconstant_names,
1724 SSA_NAME_VERSION (gimple_assign_lhs (stmt)), &op_non_const);
1725 return op_non_const;
1726 }
1727
1728 struct record_modified_bb_info
1729 {
1730 bitmap bb_set;
1731 gimple stmt;
1732 };
1733
1734 /* Callback of walk_aliased_vdefs. Records basic blocks where the value may be
1735 set except for info->stmt. */
1736
1737 static bool
1738 record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef,
1739 void *data)
1740 {
1741 struct record_modified_bb_info *info = (struct record_modified_bb_info *) data;
1742 if (SSA_NAME_DEF_STMT (vdef) == info->stmt)
1743 return false;
1744 bitmap_set_bit (info->bb_set,
1745 SSA_NAME_IS_DEFAULT_DEF (vdef)
1746 ? ENTRY_BLOCK_PTR->index : gimple_bb (SSA_NAME_DEF_STMT (vdef))->index);
1747 return false;
1748 }
1749
1750 /* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT
1751 will change since last invocation of STMT.
1752
1753 Value 0 is reserved for compile time invariants.
1754 For common parameters it is REG_BR_PROB_BASE. For loop invariants it
1755 ought to be REG_BR_PROB_BASE / estimated_iters. */
1756
1757 static int
1758 param_change_prob (gimple stmt, int i)
1759 {
1760 tree op = gimple_call_arg (stmt, i);
1761 basic_block bb = gimple_bb (stmt);
1762 tree base;
1763
1764 if (is_gimple_min_invariant (op))
1765 return 0;
1766 /* We would have to do non-trivial analysis to really work out what
1767 is the probability of value to change (i.e. when init statement
1768 is in a sibling loop of the call).
1769
1770 We do an conservative estimate: when call is executed N times more often
1771 than the statement defining value, we take the frequency 1/N. */
1772 if (TREE_CODE (op) == SSA_NAME)
1773 {
1774 int init_freq;
1775
1776 if (!bb->frequency)
1777 return REG_BR_PROB_BASE;
1778
1779 if (SSA_NAME_IS_DEFAULT_DEF (op))
1780 init_freq = ENTRY_BLOCK_PTR->frequency;
1781 else
1782 init_freq = gimple_bb (SSA_NAME_DEF_STMT (op))->frequency;
1783
1784 if (!init_freq)
1785 init_freq = 1;
1786 if (init_freq < bb->frequency)
1787 return MAX ((init_freq * REG_BR_PROB_BASE +
1788 bb->frequency / 2) / bb->frequency, 1);
1789 else
1790 return REG_BR_PROB_BASE;
1791 }
1792
1793 base = get_base_address (op);
1794 if (base)
1795 {
1796 ao_ref refd;
1797 int max;
1798 struct record_modified_bb_info info;
1799 bitmap_iterator bi;
1800 unsigned index;
1801
1802 if (const_value_known_p (base))
1803 return 0;
1804 if (!bb->frequency)
1805 return REG_BR_PROB_BASE;
1806 ao_ref_init (&refd, op);
1807 info.stmt = stmt;
1808 info.bb_set = BITMAP_ALLOC (NULL);
1809 walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info,
1810 NULL);
1811 if (bitmap_bit_p (info.bb_set, bb->index))
1812 {
1813 BITMAP_FREE (info.bb_set);
1814 return REG_BR_PROB_BASE;
1815 }
1816
1817 /* Assume that every memory is initialized at entry.
1818 TODO: Can we easilly determine if value is always defined
1819 and thus we may skip entry block? */
1820 if (ENTRY_BLOCK_PTR->frequency)
1821 max = ENTRY_BLOCK_PTR->frequency;
1822 else
1823 max = 1;
1824
1825 EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi)
1826 max = MIN (max, BASIC_BLOCK (index)->frequency);
1827
1828 BITMAP_FREE (info.bb_set);
1829 if (max < bb->frequency)
1830 return MAX ((max * REG_BR_PROB_BASE +
1831 bb->frequency / 2) / bb->frequency, 1);
1832 else
1833 return REG_BR_PROB_BASE;
1834 }
1835 return REG_BR_PROB_BASE;
1836 }
1837
1838
1839 /* Compute function body size parameters for NODE.
1840 When EARLY is true, we compute only simple summaries without
1841 non-trivial predicates to drive the early inliner. */
1842
1843 static void
1844 estimate_function_body_sizes (struct cgraph_node *node, bool early)
1845 {
1846 gcov_type time = 0;
1847 /* Estimate static overhead for function prologue/epilogue and alignment. */
1848 int size = 2;
1849 /* Benefits are scaled by probability of elimination that is in range
1850 <0,2>. */
1851 basic_block bb;
1852 gimple_stmt_iterator bsi;
1853 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1854 int freq;
1855 struct inline_summary *info = inline_summary (node);
1856 struct predicate bb_predicate;
1857 struct ipa_node_params *parms_info = NULL;
1858 VEC (predicate_t, heap) *nonconstant_names = NULL;
1859
1860 if (ipa_node_params_vector && !early && optimize)
1861 {
1862 parms_info = IPA_NODE_REF (node);
1863 VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
1864 VEC_length (tree, SSANAMES (my_function)));
1865 }
1866
1867 info->conds = 0;
1868 info->entry = 0;
1869
1870
1871 if (dump_file)
1872 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
1873 cgraph_node_name (node));
1874
1875 /* When we run into maximal number of entries, we assign everything to the
1876 constant truth case. Be sure to have it in list. */
1877 bb_predicate = true_predicate ();
1878 account_size_time (info, 0, 0, &bb_predicate);
1879
1880 bb_predicate = not_inlined_predicate ();
1881 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
1882
1883 gcc_assert (my_function && my_function->cfg);
1884 if (parms_info)
1885 compute_bb_predicates (node, parms_info, info);
1886 FOR_EACH_BB_FN (bb, my_function)
1887 {
1888 freq = compute_call_stmt_bb_frequency (node->decl, bb);
1889
1890 /* TODO: Obviously predicates can be propagated down across CFG. */
1891 if (parms_info)
1892 {
1893 if (bb->aux)
1894 bb_predicate = *(struct predicate *)bb->aux;
1895 else
1896 bb_predicate = false_predicate ();
1897 }
1898 else
1899 bb_predicate = true_predicate ();
1900
1901 if (dump_file && (dump_flags & TDF_DETAILS))
1902 {
1903 fprintf (dump_file, "\n BB %i predicate:", bb->index);
1904 dump_predicate (dump_file, info->conds, &bb_predicate);
1905 }
1906
1907 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1908 {
1909 gimple stmt = gsi_stmt (bsi);
1910 int this_size = estimate_num_insns (stmt, &eni_size_weights);
1911 int this_time = estimate_num_insns (stmt, &eni_time_weights);
1912 int prob;
1913 struct predicate will_be_nonconstant;
1914
1915 if (dump_file && (dump_flags & TDF_DETAILS))
1916 {
1917 fprintf (dump_file, " ");
1918 print_gimple_stmt (dump_file, stmt, 0, 0);
1919 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
1920 ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
1921 }
1922
1923 if (is_gimple_call (stmt))
1924 {
1925 struct cgraph_edge *edge = cgraph_edge (node, stmt);
1926 struct inline_edge_summary *es = inline_edge_summary (edge);
1927
1928 /* Special case: results of BUILT_IN_CONSTANT_P will be always
1929 resolved as constant. We however don't want to optimize
1930 out the cgraph edges. */
1931 if (nonconstant_names
1932 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
1933 && gimple_call_lhs (stmt)
1934 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
1935 {
1936 struct predicate false_p = false_predicate ();
1937 VEC_replace (predicate_t, nonconstant_names,
1938 SSA_NAME_VERSION (gimple_call_lhs (stmt)),
1939 &false_p);
1940 }
1941 if (ipa_node_params_vector)
1942 {
1943 int count = gimple_call_num_args (stmt);
1944 int i;
1945
1946 if (count)
1947 VEC_safe_grow_cleared (inline_param_summary_t, heap,
1948 es->param, count);
1949 for (i = 0; i < count; i++)
1950 {
1951 int prob = param_change_prob (stmt, i);
1952 gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE);
1953 VEC_index (inline_param_summary_t,
1954 es->param, i)->change_prob = prob;
1955 }
1956 }
1957
1958 es->call_stmt_size = this_size;
1959 es->call_stmt_time = this_time;
1960 es->loop_depth = bb->loop_depth;
1961 edge_set_predicate (edge, &bb_predicate);
1962
1963 /* Do not inline calls where we cannot triviall work around
1964 mismatches in argument or return types. */
1965 if (edge->callee
1966 && cgraph_function_or_thunk_node (edge->callee, NULL)
1967 && !gimple_check_call_matching_types
1968 (stmt, cgraph_function_or_thunk_node (edge->callee,
1969 NULL)->decl))
1970 {
1971 edge->call_stmt_cannot_inline_p = true;
1972 gimple_call_set_cannot_inline (stmt, true);
1973 }
1974 else
1975 gcc_assert (!gimple_call_cannot_inline_p (stmt));
1976 }
1977
1978 /* TODO: When conditional jump or swithc is known to be constant, but
1979 we did not translate it into the predicates, we really can account
1980 just maximum of the possible paths. */
1981 if (parms_info)
1982 will_be_nonconstant
1983 = will_be_nonconstant_predicate (parms_info, info,
1984 stmt, nonconstant_names);
1985 if (this_time || this_size)
1986 {
1987 struct predicate p;
1988
1989 this_time *= freq;
1990 time += this_time;
1991 size += this_size;
1992
1993 prob = eliminated_by_inlining_prob (stmt);
1994 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1995 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
1996 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1997 fprintf (dump_file, "\t\tWill be eliminated by inlining\n");
1998
1999 if (parms_info)
2000 p = and_predicates (info->conds, &bb_predicate,
2001 &will_be_nonconstant);
2002 else
2003 p = true_predicate ();
2004
2005 /* We account everything but the calls. Calls have their own
2006 size/time info attached to cgraph edges. This is neccesary
2007 in order to make the cost disappear after inlining. */
2008 if (!is_gimple_call (stmt))
2009 {
2010 if (prob)
2011 {
2012 struct predicate ip = not_inlined_predicate ();
2013 ip = and_predicates (info->conds, &ip, &p);
2014 account_size_time (info, this_size * prob,
2015 this_time * prob, &ip);
2016 }
2017 if (prob != 2)
2018 account_size_time (info, this_size * (2 - prob),
2019 this_time * (2 - prob), &p);
2020 }
2021
2022 gcc_assert (time >= 0);
2023 gcc_assert (size >= 0);
2024 }
2025 }
2026 }
2027 FOR_ALL_BB_FN (bb, my_function)
2028 {
2029 edge e;
2030 edge_iterator ei;
2031
2032 if (bb->aux)
2033 pool_free (edge_predicate_pool, bb->aux);
2034 bb->aux = NULL;
2035 FOR_EACH_EDGE (e, ei, bb->succs)
2036 {
2037 if (e->aux)
2038 pool_free (edge_predicate_pool, e->aux);
2039 e->aux = NULL;
2040 }
2041 }
2042 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2043 if (time > MAX_TIME)
2044 time = MAX_TIME;
2045 inline_summary (node)->self_time = time;
2046 inline_summary (node)->self_size = size;
2047 VEC_free (predicate_t, heap, nonconstant_names);
2048 if (dump_file)
2049 {
2050 fprintf (dump_file, "\n");
2051 dump_inline_summary (dump_file, node);
2052 }
2053 }
2054
2055
2056 /* Compute parameters of functions used by inliner.
2057 EARLY is true when we compute parameters for the early inliner */
2058
2059 void
2060 compute_inline_parameters (struct cgraph_node *node, bool early)
2061 {
2062 HOST_WIDE_INT self_stack_size;
2063 struct cgraph_edge *e;
2064 struct inline_summary *info;
2065 tree old_decl = current_function_decl;
2066
2067 gcc_assert (!node->global.inlined_to);
2068
2069 inline_summary_alloc ();
2070
2071 info = inline_summary (node);
2072 reset_inline_summary (node);
2073
2074 /* FIXME: Thunks are inlinable, but tree-inline don't know how to do that.
2075 Once this happen, we will need to more curefully predict call
2076 statement size. */
2077 if (node->thunk.thunk_p)
2078 {
2079 struct inline_edge_summary *es = inline_edge_summary (node->callees);
2080 struct predicate t = true_predicate ();
2081
2082 info->inlinable = 0;
2083 node->callees->call_stmt_cannot_inline_p = true;
2084 node->local.can_change_signature = false;
2085 es->call_stmt_time = 1;
2086 es->call_stmt_size = 1;
2087 account_size_time (info, 0, 0, &t);
2088 return;
2089 }
2090
2091 /* Even is_gimple_min_invariant rely on current_function_decl. */
2092 current_function_decl = node->decl;
2093 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2094
2095 /* Estimate the stack size for the function if we're optimizing. */
2096 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
2097 info->estimated_self_stack_size = self_stack_size;
2098 info->estimated_stack_size = self_stack_size;
2099 info->stack_frame_offset = 0;
2100
2101 /* Can this function be inlined at all? */
2102 info->inlinable = tree_inlinable_function_p (node->decl);
2103
2104 /* Type attributes can use parameter indices to describe them. */
2105 if (TYPE_ATTRIBUTES (TREE_TYPE (node->decl)))
2106 node->local.can_change_signature = false;
2107 else
2108 {
2109 /* Otherwise, inlinable functions always can change signature. */
2110 if (info->inlinable)
2111 node->local.can_change_signature = true;
2112 else
2113 {
2114 /* Functions calling builtin_apply can not change signature. */
2115 for (e = node->callees; e; e = e->next_callee)
2116 {
2117 tree cdecl = e->callee->decl;
2118 if (DECL_BUILT_IN (cdecl)
2119 && DECL_BUILT_IN_CLASS (cdecl) == BUILT_IN_NORMAL
2120 && (DECL_FUNCTION_CODE (cdecl) == BUILT_IN_APPLY_ARGS
2121 || DECL_FUNCTION_CODE (cdecl) == BUILT_IN_VA_START))
2122 break;
2123 }
2124 node->local.can_change_signature = !e;
2125 }
2126 }
2127 estimate_function_body_sizes (node, early);
2128
2129 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
2130 info->time = info->self_time;
2131 info->size = info->self_size;
2132 info->stack_frame_offset = 0;
2133 info->estimated_stack_size = info->estimated_self_stack_size;
2134 current_function_decl = old_decl;
2135 pop_cfun ();
2136 }
2137
2138
2139 /* Compute parameters of functions used by inliner using
2140 current_function_decl. */
2141
2142 static unsigned int
2143 compute_inline_parameters_for_current (void)
2144 {
2145 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
2146 return 0;
2147 }
2148
2149 struct gimple_opt_pass pass_inline_parameters =
2150 {
2151 {
2152 GIMPLE_PASS,
2153 "inline_param", /* name */
2154 NULL, /* gate */
2155 compute_inline_parameters_for_current,/* execute */
2156 NULL, /* sub */
2157 NULL, /* next */
2158 0, /* static_pass_number */
2159 TV_INLINE_HEURISTICS, /* tv_id */
2160 0, /* properties_required */
2161 0, /* properties_provided */
2162 0, /* properties_destroyed */
2163 0, /* todo_flags_start */
2164 0 /* todo_flags_finish */
2165 }
2166 };
2167
2168
2169 /* Increase SIZE and TIME for size and time needed to handle edge E. */
2170
2171 static void
2172 estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time,
2173 int prob)
2174 {
2175 struct inline_edge_summary *es = inline_edge_summary (e);
2176 *size += es->call_stmt_size * INLINE_SIZE_SCALE;
2177 *time += (es->call_stmt_time * prob / REG_BR_PROB_BASE
2178 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
2179 if (*time > MAX_TIME * INLINE_TIME_SCALE)
2180 *time = MAX_TIME * INLINE_TIME_SCALE;
2181 }
2182
2183
2184 /* Increase SIZE and TIME for size and time needed to handle all calls in NODE. */
2185
2186 static void
2187 estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
2188 clause_t possible_truths)
2189 {
2190 struct cgraph_edge *e;
2191 for (e = node->callees; e; e = e->next_callee)
2192 {
2193 struct inline_edge_summary *es = inline_edge_summary (e);
2194 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2195 {
2196 if (e->inline_failed)
2197 {
2198 /* Predicates of calls shall not use NOT_CHANGED codes,
2199 sowe do not need to compute probabilities. */
2200 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE);
2201 }
2202 else
2203 estimate_calls_size_and_time (e->callee, size, time,
2204 possible_truths);
2205 }
2206 }
2207 /* TODO: look for devirtualizing oppurtunities. */
2208 for (e = node->indirect_calls; e; e = e->next_callee)
2209 {
2210 struct inline_edge_summary *es = inline_edge_summary (e);
2211 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
2212 estimate_edge_size_and_time (e, size, time, REG_BR_PROB_BASE);
2213 }
2214 }
2215
2216
2217 /* Estimate size and time needed to execute NODE assuming
2218 POSSIBLE_TRUTHS clause. */
2219
2220 static void
2221 estimate_node_size_and_time (struct cgraph_node *node,
2222 clause_t possible_truths,
2223 int *ret_size, int *ret_time,
2224 VEC (inline_param_summary_t, heap)
2225 *inline_param_summary)
2226 {
2227 struct inline_summary *info = inline_summary (node);
2228 size_time_entry *e;
2229 int size = 0, time = 0;
2230 int i;
2231
2232 if (dump_file
2233 && (dump_flags & TDF_DETAILS))
2234 {
2235 bool found = false;
2236 fprintf (dump_file, " Estimating body: %s/%i\n"
2237 " Known to be false: ",
2238 cgraph_node_name (node),
2239 node->uid);
2240
2241 for (i = predicate_not_inlined_condition;
2242 i < (predicate_first_dynamic_condition
2243 + (int)VEC_length (condition, info->conds)); i++)
2244 if (!(possible_truths & (1 << i)))
2245 {
2246 if (found)
2247 fprintf (dump_file, ", ");
2248 found = true;
2249 dump_condition (dump_file, info->conds, i);
2250 }
2251 }
2252
2253 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2254 if (evaluate_predicate (&e->predicate, possible_truths))
2255 {
2256 size += e->size;
2257 if (!inline_param_summary)
2258 time += e->time;
2259 else
2260 {
2261 int prob = predicate_probability (info->conds,
2262 &e->predicate,
2263 possible_truths,
2264 inline_param_summary);
2265 time += e->time * prob / REG_BR_PROB_BASE;
2266 }
2267
2268 }
2269
2270 if (time > MAX_TIME * INLINE_TIME_SCALE)
2271 time = MAX_TIME * INLINE_TIME_SCALE;
2272
2273 estimate_calls_size_and_time (node, &size, &time, possible_truths);
2274 time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
2275 size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
2276
2277
2278 if (dump_file
2279 && (dump_flags & TDF_DETAILS))
2280 fprintf (dump_file, "\n size:%i time:%i\n", size, time);
2281 if (ret_time)
2282 *ret_time = time;
2283 if (ret_size)
2284 *ret_size = size;
2285 return;
2286 }
2287
2288
2289 /* Estimate size and time needed to execute callee of EDGE assuming that
2290 parameters known to be constant at caller of EDGE are propagated.
2291 KNOWN_VALs is a vector of assumed known constant values for parameters. */
2292
2293 void
2294 estimate_ipcp_clone_size_and_time (struct cgraph_node *node,
2295 VEC (tree, heap) *known_vals,
2296 int *ret_size, int *ret_time)
2297 {
2298 clause_t clause;
2299
2300 clause = evaluate_conditions_for_known_args (node, false, known_vals);
2301 estimate_node_size_and_time (node, clause, ret_size, ret_time,
2302 NULL);
2303 }
2304
2305
2306 /* Translate all conditions from callee representation into caller
2307 representation and symbolically evaluate predicate P into new predicate.
2308
2309 INFO is inline_summary of function we are adding predicate into,
2310 CALLEE_INFO is summary of function predicate P is from. OPERAND_MAP is
2311 array giving callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is
2312 clausule of all callee conditions that may be true in caller context.
2313 TOPLEV_PREDICATE is predicate under which callee is executed. */
2314
2315 static struct predicate
2316 remap_predicate (struct inline_summary *info,
2317 struct inline_summary *callee_info,
2318 struct predicate *p,
2319 VEC (int, heap) *operand_map,
2320 clause_t possible_truths,
2321 struct predicate *toplev_predicate)
2322 {
2323 int i;
2324 struct predicate out = true_predicate ();
2325
2326 /* True predicate is easy. */
2327 if (true_predicate_p (p))
2328 return *toplev_predicate;
2329 for (i = 0; p->clause[i]; i++)
2330 {
2331 clause_t clause = p->clause[i];
2332 int cond;
2333 struct predicate clause_predicate = false_predicate ();
2334
2335 gcc_assert (i < MAX_CLAUSES);
2336
2337 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
2338 /* Do we have condition we can't disprove? */
2339 if (clause & possible_truths & (1 << cond))
2340 {
2341 struct predicate cond_predicate;
2342 /* Work out if the condition can translate to predicate in the
2343 inlined function. */
2344 if (cond >= predicate_first_dynamic_condition)
2345 {
2346 struct condition *c;
2347
2348 c = VEC_index (condition, callee_info->conds,
2349 cond - predicate_first_dynamic_condition);
2350 /* See if we can remap condition operand to caller's operand.
2351 Otherwise give up. */
2352 if (!operand_map
2353 || (int)VEC_length (int, operand_map) <= c->operand_num
2354 || VEC_index (int, operand_map, c->operand_num) == -1)
2355 cond_predicate = true_predicate ();
2356 else
2357 cond_predicate = add_condition (info,
2358 VEC_index (int, operand_map,
2359 c->operand_num),
2360 c->code, c->val);
2361 }
2362 /* Fixed conditions remains same, construct single
2363 condition predicate. */
2364 else
2365 {
2366 cond_predicate.clause[0] = 1 << cond;
2367 cond_predicate.clause[1] = 0;
2368 }
2369 clause_predicate = or_predicates (info->conds, &clause_predicate,
2370 &cond_predicate);
2371 }
2372 out = and_predicates (info->conds, &out, &clause_predicate);
2373 }
2374 return and_predicates (info->conds, &out, toplev_predicate);
2375 }
2376
2377
2378 /* Update summary information of inline clones after inlining.
2379 Compute peak stack usage. */
2380
2381 static void
2382 inline_update_callee_summaries (struct cgraph_node *node,
2383 int depth)
2384 {
2385 struct cgraph_edge *e;
2386 struct inline_summary *callee_info = inline_summary (node);
2387 struct inline_summary *caller_info = inline_summary (node->callers->caller);
2388 HOST_WIDE_INT peak;
2389
2390 callee_info->stack_frame_offset
2391 = caller_info->stack_frame_offset
2392 + caller_info->estimated_self_stack_size;
2393 peak = callee_info->stack_frame_offset
2394 + callee_info->estimated_self_stack_size;
2395 if (inline_summary (node->global.inlined_to)->estimated_stack_size
2396 < peak)
2397 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
2398 cgraph_propagate_frequency (node);
2399 for (e = node->callees; e; e = e->next_callee)
2400 {
2401 if (!e->inline_failed)
2402 inline_update_callee_summaries (e->callee, depth);
2403 inline_edge_summary (e)->loop_depth += depth;
2404 }
2405 for (e = node->indirect_calls; e; e = e->next_callee)
2406 inline_edge_summary (e)->loop_depth += depth;
2407 }
2408
2409 /* Update change_prob of EDGE after INLINED_EDGE has been inlined.
2410 When functoin A is inlined in B and A calls C with parameter that
2411 changes with probability PROB1 and C is known to be passthroug
2412 of argument if B that change with probability PROB2, the probability
2413 of change is now PROB1*PROB2. */
2414
2415 static void
2416 remap_edge_change_prob (struct cgraph_edge *inlined_edge,
2417 struct cgraph_edge *edge)
2418 {
2419 if (ipa_node_params_vector)
2420 {
2421 int i;
2422 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
2423 struct inline_edge_summary *es = inline_edge_summary (edge);
2424 struct inline_edge_summary *inlined_es
2425 = inline_edge_summary (inlined_edge);
2426
2427 for (i = 0; i < ipa_get_cs_argument_count (args); i++)
2428 {
2429 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
2430 if (jfunc->type == IPA_JF_PASS_THROUGH
2431 && (jfunc->value.pass_through.formal_id
2432 < (int) VEC_length (inline_param_summary_t,
2433 inlined_es->param)))
2434 {
2435 int prob1 = VEC_index (inline_param_summary_t,
2436 es->param, i)->change_prob;
2437 int prob2 = VEC_index
2438 (inline_param_summary_t,
2439 inlined_es->param,
2440 jfunc->value.pass_through.formal_id)->change_prob;
2441 int prob = ((prob1 * prob2 + REG_BR_PROB_BASE / 2)
2442 / REG_BR_PROB_BASE);
2443
2444 if (prob1 && prob2 && !prob)
2445 prob = 1;
2446
2447 VEC_index (inline_param_summary_t,
2448 es->param, i)->change_prob = prob;
2449 }
2450 }
2451 }
2452 }
2453
2454 /* Update edge summaries of NODE after INLINED_EDGE has been inlined.
2455
2456 Remap predicates of callees of NODE. Rest of arguments match
2457 remap_predicate.
2458
2459 Also update change probabilities. */
2460
2461 static void
2462 remap_edge_summaries (struct cgraph_edge *inlined_edge,
2463 struct cgraph_node *node,
2464 struct inline_summary *info,
2465 struct inline_summary *callee_info,
2466 VEC (int, heap) *operand_map,
2467 clause_t possible_truths,
2468 struct predicate *toplev_predicate)
2469 {
2470 struct cgraph_edge *e;
2471 for (e = node->callees; e; e = e->next_callee)
2472 {
2473 struct inline_edge_summary *es = inline_edge_summary (e);
2474 struct predicate p;
2475
2476 if (e->inline_failed)
2477 {
2478 remap_edge_change_prob (inlined_edge, e);
2479
2480 if (es->predicate)
2481 {
2482 p = remap_predicate (info, callee_info,
2483 es->predicate, operand_map, possible_truths,
2484 toplev_predicate);
2485 edge_set_predicate (e, &p);
2486 /* TODO: We should remove the edge for code that will be
2487 optimized out, but we need to keep verifiers and tree-inline
2488 happy. Make it cold for now. */
2489 if (false_predicate_p (&p))
2490 {
2491 e->count = 0;
2492 e->frequency = 0;
2493 }
2494 }
2495 else
2496 edge_set_predicate (e, toplev_predicate);
2497 }
2498 else
2499 remap_edge_summaries (inlined_edge, e->callee, info, callee_info,
2500 operand_map, possible_truths, toplev_predicate);
2501 }
2502 for (e = node->indirect_calls; e; e = e->next_callee)
2503 {
2504 struct inline_edge_summary *es = inline_edge_summary (e);
2505 struct predicate p;
2506
2507 remap_edge_change_prob (inlined_edge, e);
2508 if (es->predicate)
2509 {
2510 p = remap_predicate (info, callee_info,
2511 es->predicate, operand_map, possible_truths,
2512 toplev_predicate);
2513 edge_set_predicate (e, &p);
2514 /* TODO: We should remove the edge for code that will be optimized
2515 out, but we need to keep verifiers and tree-inline happy.
2516 Make it cold for now. */
2517 if (false_predicate_p (&p))
2518 {
2519 e->count = 0;
2520 e->frequency = 0;
2521 }
2522 }
2523 else
2524 edge_set_predicate (e, toplev_predicate);
2525 }
2526 }
2527
2528
2529 /* We inlined EDGE. Update summary of the function we inlined into. */
2530
2531 void
2532 inline_merge_summary (struct cgraph_edge *edge)
2533 {
2534 struct inline_summary *callee_info = inline_summary (edge->callee);
2535 struct cgraph_node *to = (edge->caller->global.inlined_to
2536 ? edge->caller->global.inlined_to : edge->caller);
2537 struct inline_summary *info = inline_summary (to);
2538 clause_t clause = 0; /* not_inline is known to be false. */
2539 size_time_entry *e;
2540 VEC (int, heap) *operand_map = NULL;
2541 int i;
2542 struct predicate toplev_predicate;
2543 struct predicate true_p = true_predicate ();
2544 struct inline_edge_summary *es = inline_edge_summary (edge);
2545
2546 if (es->predicate)
2547 toplev_predicate = *es->predicate;
2548 else
2549 toplev_predicate = true_predicate ();
2550
2551 if (ipa_node_params_vector && callee_info->conds)
2552 {
2553 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
2554 int count = ipa_get_cs_argument_count (args);
2555 int i;
2556
2557 clause = evaluate_conditions_for_edge (edge, true);
2558 if (count)
2559 VEC_safe_grow_cleared (int, heap, operand_map, count);
2560 for (i = 0; i < count; i++)
2561 {
2562 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
2563 int map = -1;
2564 /* TODO: handle non-NOPs when merging. */
2565 if (jfunc->type == IPA_JF_PASS_THROUGH
2566 && jfunc->value.pass_through.operation == NOP_EXPR)
2567 map = jfunc->value.pass_through.formal_id;
2568 VEC_replace (int, operand_map, i, map);
2569 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
2570 }
2571 }
2572 for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
2573 {
2574 struct predicate p = remap_predicate (info, callee_info,
2575 &e->predicate, operand_map, clause,
2576 &toplev_predicate);
2577 if (!false_predicate_p (&p))
2578 {
2579 gcov_type add_time = ((gcov_type)e->time * edge->frequency
2580 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2581 int prob = predicate_probability (callee_info->conds,
2582 &e->predicate,
2583 clause, es->param);
2584 add_time = add_time * prob / REG_BR_PROB_BASE;
2585 if (add_time > MAX_TIME * INLINE_TIME_SCALE)
2586 add_time = MAX_TIME * INLINE_TIME_SCALE;
2587 if (prob != REG_BR_PROB_BASE
2588 && dump_file && (dump_flags & TDF_DETAILS))
2589 {
2590 fprintf (dump_file, "\t\tScaling time by probability:%f\n",
2591 (double)prob / REG_BR_PROB_BASE);
2592 }
2593 account_size_time (info, e->size, add_time, &p);
2594 }
2595 }
2596 remap_edge_summaries (edge, edge->callee, info, callee_info, operand_map,
2597 clause, &toplev_predicate);
2598 info->size = 0;
2599 info->time = 0;
2600 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
2601 info->size += e->size, info->time += e->time;
2602 estimate_calls_size_and_time (to, &info->size, &info->time,
2603 ~(clause_t)(1 << predicate_false_condition));
2604
2605 inline_update_callee_summaries (edge->callee,
2606 inline_edge_summary (edge)->loop_depth);
2607
2608 /* We do not maintain predicates of inlined edges, free it. */
2609 edge_set_predicate (edge, &true_p);
2610 /* Similarly remove param summaries. */
2611 VEC_free (inline_param_summary_t, heap, es->param);
2612
2613 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
2614 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
2615 }
2616
2617
2618 /* Estimate the time cost for the caller when inlining EDGE.
2619 Only to be called via estimate_edge_time, that handles the
2620 caching mechanism.
2621
2622 When caching, also update the cache entry. Compute both time and
2623 size, since we always need both metrics eventually. */
2624
2625 int
2626 do_estimate_edge_time (struct cgraph_edge *edge)
2627 {
2628 int time;
2629 int size;
2630 gcov_type ret;
2631 struct inline_edge_summary *es = inline_edge_summary (edge);
2632
2633 gcc_checking_assert (edge->inline_failed);
2634 estimate_node_size_and_time (cgraph_function_or_thunk_node (edge->callee,
2635 NULL),
2636 evaluate_conditions_for_edge (edge, true),
2637 &size, &time, es->param);
2638
2639 ret = (((gcov_type)time
2640 - es->call_stmt_time) * edge->frequency
2641 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
2642
2643 /* When caching, update the cache entry. */
2644 if (edge_growth_cache)
2645 {
2646 int ret_size;
2647 if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
2648 <= edge->uid)
2649 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
2650 cgraph_edge_max_uid);
2651 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->time
2652 = ret + (ret >= 0);
2653
2654 ret_size = size - es->call_stmt_size;
2655 gcc_checking_assert (es->call_stmt_size);
2656 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->size
2657 = ret_size + (ret_size >= 0);
2658 }
2659 return ret;
2660 }
2661
2662
2663 /* Estimate the growth of the caller when inlining EDGE.
2664 Only to be called via estimate_edge_size. */
2665
2666 int
2667 do_estimate_edge_growth (struct cgraph_edge *edge)
2668 {
2669 int size;
2670 struct cgraph_node *callee;
2671
2672 /* When we do caching, use do_estimate_edge_time to populate the entry. */
2673
2674 if (edge_growth_cache)
2675 {
2676 do_estimate_edge_time (edge);
2677 size = VEC_index (edge_growth_cache_entry,
2678 edge_growth_cache,
2679 edge->uid)->size;
2680 gcc_checking_assert (size);
2681 return size - (size > 0);
2682 }
2683 callee = cgraph_function_or_thunk_node (edge->callee, NULL);
2684
2685 /* Early inliner runs without caching, go ahead and do the dirty work. */
2686 gcc_checking_assert (edge->inline_failed);
2687 estimate_node_size_and_time (callee,
2688 evaluate_conditions_for_edge (edge, true),
2689 &size, NULL, NULL);
2690 gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
2691 return size - inline_edge_summary (edge)->call_stmt_size;
2692 }
2693
2694
2695 /* Estimate self time of the function NODE after inlining EDGE. */
2696
2697 int
2698 estimate_time_after_inlining (struct cgraph_node *node,
2699 struct cgraph_edge *edge)
2700 {
2701 struct inline_edge_summary *es = inline_edge_summary (edge);
2702 if (!es->predicate || !false_predicate_p (es->predicate))
2703 {
2704 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
2705 if (time < 0)
2706 time = 0;
2707 if (time > MAX_TIME)
2708 time = MAX_TIME;
2709 return time;
2710 }
2711 return inline_summary (node)->time;
2712 }
2713
2714
2715 /* Estimate the size of NODE after inlining EDGE which should be an
2716 edge to either NODE or a call inlined into NODE. */
2717
2718 int
2719 estimate_size_after_inlining (struct cgraph_node *node,
2720 struct cgraph_edge *edge)
2721 {
2722 struct inline_edge_summary *es = inline_edge_summary (edge);
2723 if (!es->predicate || !false_predicate_p (es->predicate))
2724 {
2725 int size = inline_summary (node)->size + estimate_edge_growth (edge);
2726 gcc_assert (size >= 0);
2727 return size;
2728 }
2729 return inline_summary (node)->size;
2730 }
2731
2732
2733 struct growth_data
2734 {
2735 bool self_recursive;
2736 int growth;
2737 };
2738
2739
2740 /* Worker for do_estimate_growth. Collect growth for all callers. */
2741
2742 static bool
2743 do_estimate_growth_1 (struct cgraph_node *node, void *data)
2744 {
2745 struct cgraph_edge *e;
2746 struct growth_data *d = (struct growth_data *) data;
2747
2748 for (e = node->callers; e; e = e->next_caller)
2749 {
2750 gcc_checking_assert (e->inline_failed);
2751
2752 if (e->caller == node
2753 || (e->caller->global.inlined_to
2754 && e->caller->global.inlined_to == node))
2755 d->self_recursive = true;
2756 d->growth += estimate_edge_growth (e);
2757 }
2758 return false;
2759 }
2760
2761
2762 /* Estimate the growth caused by inlining NODE into all callees. */
2763
2764 int
2765 do_estimate_growth (struct cgraph_node *node)
2766 {
2767 struct growth_data d = {0, false};
2768 struct inline_summary *info = inline_summary (node);
2769
2770 cgraph_for_node_and_aliases (node, do_estimate_growth_1, &d, true);
2771
2772 /* For self recursive functions the growth estimation really should be
2773 infinity. We don't want to return very large values because the growth
2774 plays various roles in badness computation fractions. Be sure to not
2775 return zero or negative growths. */
2776 if (d.self_recursive)
2777 d.growth = d.growth < info->size ? info->size : d.growth;
2778 else
2779 {
2780 if (!DECL_EXTERNAL (node->decl)
2781 && cgraph_will_be_removed_from_program_if_no_direct_calls (node))
2782 d.growth -= info->size;
2783 /* COMDAT functions are very often not shared across multiple units
2784 since they come from various template instantiations.
2785 Take this into account. */
2786 else if (DECL_COMDAT (node->decl)
2787 && cgraph_can_remove_if_no_direct_calls_p (node))
2788 d.growth -= (info->size
2789 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY))
2790 + 50) / 100;
2791 }
2792
2793 if (node_growth_cache)
2794 {
2795 if ((int)VEC_length (int, node_growth_cache) <= node->uid)
2796 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
2797 VEC_replace (int, node_growth_cache, node->uid,
2798 d.growth + (d.growth >= 0));
2799 }
2800 return d.growth;
2801 }
2802
2803
2804 /* This function performs intraprocedural analysis in NODE that is required to
2805 inline indirect calls. */
2806
2807 static void
2808 inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
2809 {
2810 ipa_analyze_node (node);
2811 if (dump_file && (dump_flags & TDF_DETAILS))
2812 {
2813 ipa_print_node_params (dump_file, node);
2814 ipa_print_node_jump_functions (dump_file, node);
2815 }
2816 }
2817
2818
2819 /* Note function body size. */
2820
2821 static void
2822 inline_analyze_function (struct cgraph_node *node)
2823 {
2824 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2825 current_function_decl = node->decl;
2826
2827 if (dump_file)
2828 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
2829 cgraph_node_name (node), node->uid);
2830 if (optimize && !node->thunk.thunk_p)
2831 inline_indirect_intraprocedural_analysis (node);
2832 compute_inline_parameters (node, false);
2833
2834 current_function_decl = NULL;
2835 pop_cfun ();
2836 }
2837
2838
2839 /* Called when new function is inserted to callgraph late. */
2840
2841 static void
2842 add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2843 {
2844 inline_analyze_function (node);
2845 }
2846
2847
2848 /* Note function body size. */
2849
2850 void
2851 inline_generate_summary (void)
2852 {
2853 struct cgraph_node *node;
2854
2855 function_insertion_hook_holder =
2856 cgraph_add_function_insertion_hook (&add_new_function, NULL);
2857
2858 ipa_register_cgraph_hooks ();
2859 inline_free_summary ();
2860
2861 FOR_EACH_DEFINED_FUNCTION (node)
2862 if (!node->alias)
2863 inline_analyze_function (node);
2864 }
2865
2866
2867 /* Read predicate from IB. */
2868
2869 static struct predicate
2870 read_predicate (struct lto_input_block *ib)
2871 {
2872 struct predicate out;
2873 clause_t clause;
2874 int k = 0;
2875
2876 do
2877 {
2878 gcc_assert (k <= MAX_CLAUSES);
2879 clause = out.clause[k++] = streamer_read_uhwi (ib);
2880 }
2881 while (clause);
2882
2883 /* Zero-initialize the remaining clauses in OUT. */
2884 while (k <= MAX_CLAUSES)
2885 out.clause[k++] = 0;
2886
2887 return out;
2888 }
2889
2890
2891 /* Write inline summary for edge E to OB. */
2892
2893 static void
2894 read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
2895 {
2896 struct inline_edge_summary *es = inline_edge_summary (e);
2897 struct predicate p;
2898 int length, i;
2899
2900 es->call_stmt_size = streamer_read_uhwi (ib);
2901 es->call_stmt_time = streamer_read_uhwi (ib);
2902 es->loop_depth = streamer_read_uhwi (ib);
2903 p = read_predicate (ib);
2904 edge_set_predicate (e, &p);
2905 length = streamer_read_uhwi (ib);
2906 if (length)
2907 {
2908 VEC_safe_grow_cleared (inline_param_summary_t, heap, es->param, length);
2909 for (i = 0; i < length; i++)
2910 VEC_index (inline_param_summary_t, es->param, i)->change_prob
2911 = streamer_read_uhwi (ib);
2912 }
2913 }
2914
2915
2916 /* Stream in inline summaries from the section. */
2917
2918 static void
2919 inline_read_section (struct lto_file_decl_data *file_data, const char *data,
2920 size_t len)
2921 {
2922 const struct lto_function_header *header =
2923 (const struct lto_function_header *) data;
2924 const int32_t cfg_offset = sizeof (struct lto_function_header);
2925 const int32_t main_offset = cfg_offset + header->cfg_size;
2926 const int32_t string_offset = main_offset + header->main_size;
2927 struct data_in *data_in;
2928 struct lto_input_block ib;
2929 unsigned int i, count2, j;
2930 unsigned int f_count;
2931
2932 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
2933 header->main_size);
2934
2935 data_in =
2936 lto_data_in_create (file_data, (const char *) data + string_offset,
2937 header->string_size, NULL);
2938 f_count = streamer_read_uhwi (&ib);
2939 for (i = 0; i < f_count; i++)
2940 {
2941 unsigned int index;
2942 struct cgraph_node *node;
2943 struct inline_summary *info;
2944 lto_cgraph_encoder_t encoder;
2945 struct bitpack_d bp;
2946 struct cgraph_edge *e;
2947
2948 index = streamer_read_uhwi (&ib);
2949 encoder = file_data->cgraph_node_encoder;
2950 node = lto_cgraph_encoder_deref (encoder, index);
2951 info = inline_summary (node);
2952
2953 info->estimated_stack_size
2954 = info->estimated_self_stack_size = streamer_read_uhwi (&ib);
2955 info->size = info->self_size = streamer_read_uhwi (&ib);
2956 info->time = info->self_time = streamer_read_uhwi (&ib);
2957
2958 bp = streamer_read_bitpack (&ib);
2959 info->inlinable = bp_unpack_value (&bp, 1);
2960
2961 count2 = streamer_read_uhwi (&ib);
2962 gcc_assert (!info->conds);
2963 for (j = 0; j < count2; j++)
2964 {
2965 struct condition c;
2966 c.operand_num = streamer_read_uhwi (&ib);
2967 c.code = (enum tree_code) streamer_read_uhwi (&ib);
2968 c.val = stream_read_tree (&ib, data_in);
2969 VEC_safe_push (condition, gc, info->conds, &c);
2970 }
2971 count2 = streamer_read_uhwi (&ib);
2972 gcc_assert (!info->entry);
2973 for (j = 0; j < count2; j++)
2974 {
2975 struct size_time_entry e;
2976
2977 e.size = streamer_read_uhwi (&ib);
2978 e.time = streamer_read_uhwi (&ib);
2979 e.predicate = read_predicate (&ib);
2980
2981 VEC_safe_push (size_time_entry, gc, info->entry, &e);
2982 }
2983 for (e = node->callees; e; e = e->next_callee)
2984 read_inline_edge_summary (&ib, e);
2985 for (e = node->indirect_calls; e; e = e->next_callee)
2986 read_inline_edge_summary (&ib, e);
2987 }
2988
2989 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
2990 len);
2991 lto_data_in_delete (data_in);
2992 }
2993
2994
2995 /* Read inline summary. Jump functions are shared among ipa-cp
2996 and inliner, so when ipa-cp is active, we don't need to write them
2997 twice. */
2998
2999 void
3000 inline_read_summary (void)
3001 {
3002 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3003 struct lto_file_decl_data *file_data;
3004 unsigned int j = 0;
3005
3006 inline_summary_alloc ();
3007
3008 while ((file_data = file_data_vec[j++]))
3009 {
3010 size_t len;
3011 const char *data = lto_get_section_data (file_data,
3012 LTO_section_inline_summary,
3013 NULL, &len);
3014 if (data)
3015 inline_read_section (file_data, data, len);
3016 else
3017 /* Fatal error here. We do not want to support compiling ltrans units
3018 with different version of compiler or different flags than the WPA
3019 unit, so this should never happen. */
3020 fatal_error ("ipa inline summary is missing in input file");
3021 }
3022 if (optimize)
3023 {
3024 ipa_register_cgraph_hooks ();
3025 if (!flag_ipa_cp)
3026 ipa_prop_read_jump_functions ();
3027 }
3028 function_insertion_hook_holder =
3029 cgraph_add_function_insertion_hook (&add_new_function, NULL);
3030 }
3031
3032
3033 /* Write predicate P to OB. */
3034
3035 static void
3036 write_predicate (struct output_block *ob, struct predicate *p)
3037 {
3038 int j;
3039 if (p)
3040 for (j = 0; p->clause[j]; j++)
3041 {
3042 gcc_assert (j < MAX_CLAUSES);
3043 streamer_write_uhwi (ob, p->clause[j]);
3044 }
3045 streamer_write_uhwi (ob, 0);
3046 }
3047
3048
3049 /* Write inline summary for edge E to OB. */
3050
3051 static void
3052 write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
3053 {
3054 struct inline_edge_summary *es = inline_edge_summary (e);
3055 int i;
3056
3057 streamer_write_uhwi (ob, es->call_stmt_size);
3058 streamer_write_uhwi (ob, es->call_stmt_time);
3059 streamer_write_uhwi (ob, es->loop_depth);
3060 write_predicate (ob, es->predicate);
3061 streamer_write_uhwi (ob, VEC_length (inline_param_summary_t, es->param));
3062 for (i = 0; i < (int)VEC_length (inline_param_summary_t, es->param); i++)
3063 streamer_write_uhwi (ob, VEC_index (inline_param_summary_t,
3064 es->param, i)->change_prob);
3065 }
3066
3067
3068 /* Write inline summary for node in SET.
3069 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
3070 active, we don't need to write them twice. */
3071
3072 void
3073 inline_write_summary (cgraph_node_set set,
3074 varpool_node_set vset ATTRIBUTE_UNUSED)
3075 {
3076 struct cgraph_node *node;
3077 struct output_block *ob = create_output_block (LTO_section_inline_summary);
3078 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
3079 unsigned int count = 0;
3080 int i;
3081
3082 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
3083 if (lto_cgraph_encoder_deref (encoder, i)->analyzed)
3084 count++;
3085 streamer_write_uhwi (ob, count);
3086
3087 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
3088 {
3089 node = lto_cgraph_encoder_deref (encoder, i);
3090 if (node->analyzed)
3091 {
3092 struct inline_summary *info = inline_summary (node);
3093 struct bitpack_d bp;
3094 struct cgraph_edge *edge;
3095 int i;
3096 size_time_entry *e;
3097 struct condition *c;
3098
3099 streamer_write_uhwi (ob, lto_cgraph_encoder_encode (encoder, node));
3100 streamer_write_hwi (ob, info->estimated_self_stack_size);
3101 streamer_write_hwi (ob, info->self_size);
3102 streamer_write_hwi (ob, info->self_time);
3103 bp = bitpack_create (ob->main_stream);
3104 bp_pack_value (&bp, info->inlinable, 1);
3105 streamer_write_bitpack (&bp);
3106 streamer_write_uhwi (ob, VEC_length (condition, info->conds));
3107 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
3108 {
3109 streamer_write_uhwi (ob, c->operand_num);
3110 streamer_write_uhwi (ob, c->code);
3111 stream_write_tree (ob, c->val, true);
3112 }
3113 streamer_write_uhwi (ob, VEC_length (size_time_entry, info->entry));
3114 for (i = 0;
3115 VEC_iterate (size_time_entry, info->entry, i, e);
3116 i++)
3117 {
3118 streamer_write_uhwi (ob, e->size);
3119 streamer_write_uhwi (ob, e->time);
3120 write_predicate (ob, &e->predicate);
3121 }
3122 for (edge = node->callees; edge; edge = edge->next_callee)
3123 write_inline_edge_summary (ob, edge);
3124 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
3125 write_inline_edge_summary (ob, edge);
3126 }
3127 }
3128 streamer_write_char_stream (ob->main_stream, 0);
3129 produce_asm (ob, NULL);
3130 destroy_output_block (ob);
3131
3132 if (optimize && !flag_ipa_cp)
3133 ipa_prop_write_jump_functions (set);
3134 }
3135
3136
3137 /* Release inline summary. */
3138
3139 void
3140 inline_free_summary (void)
3141 {
3142 struct cgraph_node *node;
3143 FOR_EACH_DEFINED_FUNCTION (node)
3144 reset_inline_summary (node);
3145 if (function_insertion_hook_holder)
3146 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3147 function_insertion_hook_holder = NULL;
3148 if (node_removal_hook_holder)
3149 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3150 node_removal_hook_holder = NULL;
3151 if (edge_removal_hook_holder)
3152 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3153 edge_removal_hook_holder = NULL;
3154 if (node_duplication_hook_holder)
3155 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3156 node_duplication_hook_holder = NULL;
3157 if (edge_duplication_hook_holder)
3158 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3159 edge_duplication_hook_holder = NULL;
3160 VEC_free (inline_summary_t, gc, inline_summary_vec);
3161 inline_summary_vec = NULL;
3162 VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
3163 inline_edge_summary_vec = NULL;
3164 if (edge_predicate_pool)
3165 free_alloc_pool (edge_predicate_pool);
3166 edge_predicate_pool = 0;
3167 }