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