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