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