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