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