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