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