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