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