]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-inline-analysis.c
inline-10.c: New testcase.
[thirdparty/gcc.git] / gcc / ipa-inline-analysis.c
CommitLineData
03dfc36d
JH
1/* Inlining decision heuristics.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* Analysis used by the inliner and other passes limiting code size growth.
23
24 We estimate for each function
25 - function body size
10a5dd5d 26 - average function execution time
03dfc36d
JH
27 - inlining size benefit (that is how much of function body size
28 and its call sequence is expected to disappear by inlining)
29 - inlining time benefit
30 - function frame size
31 For each call
10a5dd5d 32 - call statement size and time
03dfc36d
JH
33
34 inlinie_summary datastructures store above information locally (i.e.
35 parameters of the function itself) and globally (i.e. parameters of
36 the function created by applying all the inline decisions already
37 present in the callgraph).
38
632b4f8e 39 We provide accestor to the inline_summary datastructure and
03dfc36d
JH
40 basic logic updating the parameters when inlining is performed.
41
632b4f8e
JH
42 The summaries are context sensitive. Context means
43 1) partial assignment of known constant values of operands
44 2) whether function is inlined into the call or not.
45 It is easy to add more variants. To represent function size and time
46 that depends on context (i.e. it is known to be optimized away when
47 context is known either by inlining or from IP-CP and clonning),
48 we use predicates. Predicates are logical formulas in
49 conjunctive-disjunctive form consisting of clauses. Clauses are bitmaps
50 specifying what conditions must be true. Conditions are simple test
51 of the form described above.
52
53 In order to make predicate (possibly) true, all of its clauses must
54 be (possibly) true. To make clause (possibly) true, one of conditions
55 it mentions must be (possibly) true. There are fixed bounds on
56 number of clauses and conditions and all the manipulation functions
57 are conservative in positive direction. I.e. we may lose precision
58 by thinking that predicate may be true even when it is not.
59
60 estimate_edge_size and estimate_edge_growth can be used to query
61 function size/time in the given context. inline_merge_summary merges
62 properties of caller and callee after inlining.
63
03dfc36d
JH
64 Finally pass_inline_parameters is exported. This is used to drive
65 computation of function parameters used by the early inliner. IPA
66 inlined performs analysis via its analyze_function method. */
67
68#include "config.h"
69#include "system.h"
70#include "coretypes.h"
71#include "tm.h"
72#include "tree.h"
73#include "tree-inline.h"
74#include "langhooks.h"
75#include "flags.h"
76#include "cgraph.h"
77#include "diagnostic.h"
78#include "gimple-pretty-print.h"
79#include "timevar.h"
80#include "params.h"
81#include "tree-pass.h"
82#include "coverage.h"
83#include "ggc.h"
84#include "tree-flow.h"
85#include "ipa-prop.h"
10a5dd5d 86#include "lto-streamer.h"
03dfc36d 87#include "ipa-inline.h"
991278ab 88#include "alloc-pool.h"
03dfc36d 89
632b4f8e
JH
90/* Estimate runtime of function can easilly run into huge numbers with many
91 nested loops. Be sure we can compute time * INLINE_SIZE_SCALE in integer.
92 For anything larger we use gcov_type. */
93#define MAX_TIME 1000000
94
95/* Number of bits in integer, but we really want to be stable across different
96 hosts. */
97#define NUM_CONDITIONS 32
98
99enum predicate_conditions
100{
101 predicate_false_condition = 0,
102 predicate_not_inlined_condition = 1,
103 predicate_first_dynamic_condition = 2
104};
105
106/* Special condition code we use to represent test that operand is compile time
107 constant. */
108#define IS_NOT_CONSTANT ERROR_MARK
03dfc36d
JH
109
110/* Holders of ipa cgraph hooks: */
111static struct cgraph_node_hook_list *function_insertion_hook_holder;
10a5dd5d
JH
112static struct cgraph_node_hook_list *node_removal_hook_holder;
113static struct cgraph_2node_hook_list *node_duplication_hook_holder;
898b8927 114static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
632b4f8e 115static struct cgraph_edge_hook_list *edge_removal_hook_holder;
10a5dd5d
JH
116static void inline_node_removal_hook (struct cgraph_node *, void *);
117static void inline_node_duplication_hook (struct cgraph_node *,
118 struct cgraph_node *, void *);
898b8927
JH
119static void inline_edge_removal_hook (struct cgraph_edge *, void *);
120static void inline_edge_duplication_hook (struct cgraph_edge *,
121 struct cgraph_edge *,
122 void *);
10a5dd5d 123
632b4f8e
JH
124/* VECtor holding inline summaries.
125 In GGC memory because conditions might point to constant trees. */
126VEC(inline_summary_t,gc) *inline_summary_vec;
898b8927 127VEC(inline_edge_summary_t,heap) *inline_edge_summary_vec;
632b4f8e
JH
128
129/* Cached node/edge growths. */
130VEC(int,heap) *node_growth_cache;
131VEC(edge_growth_cache_entry,heap) *edge_growth_cache;
132
991278ab
JH
133/* Edge predicates goes here. */
134static alloc_pool edge_predicate_pool;
632b4f8e
JH
135
136/* Return true predicate (tautology).
137 We represent it by empty list of clauses. */
138
139static inline struct predicate
140true_predicate (void)
141{
142 struct predicate p;
143 p.clause[0]=0;
144 return p;
145}
146
147
148/* Return predicate testing single condition number COND. */
149
150static inline struct predicate
151single_cond_predicate (int cond)
152{
153 struct predicate p;
154 p.clause[0]=1 << cond;
155 p.clause[1]=0;
156 return p;
157}
158
159
160/* Return false predicate. First clause require false condition. */
161
162static inline struct predicate
163false_predicate (void)
164{
165 return single_cond_predicate (predicate_false_condition);
166}
167
168
991278ab
JH
169/* Return true if P is (false). */
170
171static inline bool
172true_predicate_p (struct predicate *p)
173{
174 return !p->clause[0];
175}
176
177
178/* Return true if P is (false). */
179
180static inline bool
181false_predicate_p (struct predicate *p)
182{
183 if (p->clause[0] == (1 << predicate_false_condition))
184 {
185 gcc_checking_assert (!p->clause[1]
186 && p->clause[0] == 1 << predicate_false_condition);
187 return true;
188 }
189 return false;
190}
191
192
632b4f8e
JH
193/* Return predicate that is set true when function is not inlined. */
194static inline struct predicate
195not_inlined_predicate (void)
196{
197 return single_cond_predicate (predicate_not_inlined_condition);
198}
199
200
201/* Add condition to condition list CONDS. */
202
203static struct predicate
204add_condition (struct inline_summary *summary, int operand_num,
205 enum tree_code code, tree val)
206{
207 int i;
208 struct condition *c;
209 struct condition new_cond;
210
211 for (i = 0; VEC_iterate (condition, summary->conds, i, c); i++)
212 {
213 if (c->operand_num == operand_num
214 && c->code == code
215 && c->val == val)
216 return single_cond_predicate (i + predicate_first_dynamic_condition);
217 }
218 /* Too many conditions. Give up and return constant true. */
219 if (i == NUM_CONDITIONS - predicate_first_dynamic_condition)
220 return true_predicate ();
221
222 new_cond.operand_num = operand_num;
223 new_cond.code = code;
224 new_cond.val = val;
225 VEC_safe_push (condition, gc, summary->conds, &new_cond);
226 return single_cond_predicate (i + predicate_first_dynamic_condition);
227}
228
229
b15c64ee 230/* Add clause CLAUSE into the predicate P. */
632b4f8e
JH
231
232static inline void
233add_clause (struct predicate *p, clause_t clause)
234{
235 int i;
b15c64ee 236 int i2;
f3181aa2 237 int insert_here = -1;
991278ab 238
632b4f8e
JH
239 /* True clause. */
240 if (!clause)
241 return;
242
b15c64ee 243 /* False clause makes the whole predicate false. Kill the other variants. */
991278ab 244 if (clause == (1 << predicate_false_condition))
632b4f8e
JH
245 {
246 p->clause[0] = (1 << predicate_false_condition);
247 p->clause[1] = 0;
991278ab 248 return;
632b4f8e 249 }
991278ab
JH
250 if (false_predicate_p (p))
251 return;
b15c64ee
JH
252
253 /* No one should be sily enough to add false into nontrivial clauses. */
254 gcc_checking_assert (!(clause & (1 << predicate_false_condition)));
255
256 /* Look where to insert the clause. At the same time prune out
257 clauses of P that are implied by the new clause and thus
258 redundant. */
259 for (i = 0, i2 = 0; i <= MAX_CLAUSES; i++)
632b4f8e 260 {
b15c64ee
JH
261 p->clause[i2] = p->clause[i];
262
632b4f8e
JH
263 if (!p->clause[i])
264 break;
b15c64ee
JH
265
266 /* If p->clause[i] implies clause, there is nothing to add. */
267 if ((p->clause[i] & clause) == p->clause[i])
268 {
269 /* We had nothing to add, none of clauses should've become redundant. */
270 gcc_checking_assert (i == i2);
271 return;
272 }
273
274 if (p->clause[i] < clause && insert_here < 0)
275 insert_here = i2;
276
277 /* If clause implies p->clause[i], then p->clause[i] becomes redundant.
278 Otherwise the p->clause[i] has to stay. */
279 if ((p->clause[i] & clause) != clause)
280 i2++;
632b4f8e 281 }
b15c64ee
JH
282 /* We run out of variants. Be conservative in positive direction. */
283 if (i2 == MAX_CLAUSES)
632b4f8e 284 return;
b15c64ee
JH
285 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
286 p->clause[i2 + 1] = 0;
f3181aa2 287 if (insert_here >= 0)
b15c64ee
JH
288 for (;i2 > insert_here; i2--)
289 p->clause[i2] = p->clause[i2 - 1];
f3181aa2 290 else
b15c64ee 291 insert_here = i2;
632b4f8e
JH
292 p->clause[insert_here] = clause;
293}
294
295
296/* Return P & P2. */
297
298static struct predicate
299and_predicates (struct predicate *p, struct predicate *p2)
300{
301 struct predicate out = *p;
302 int i;
991278ab 303
b15c64ee
JH
304 /* Avoid busy work. */
305 if (false_predicate_p (p2) || true_predicate_p (p))
306 return *p2;
307 if (false_predicate_p (p) || true_predicate_p (p2))
308 return *p;
309
310 /* See how far predicates match. */
311 for (i = 0; p->clause[i] && p->clause[i] == p2->clause[i]; i++)
312 {
313 gcc_checking_assert (i < MAX_CLAUSES);
314 }
315
316 /* Combine the predicates rest. */
317 for (; p2->clause[i]; i++)
f3181aa2
JH
318 {
319 gcc_checking_assert (i < MAX_CLAUSES);
320 add_clause (&out, p2->clause[i]);
321 }
632b4f8e
JH
322 return out;
323}
324
325
b15c64ee
JH
326/* Return true if predicates are obviously equal. */
327
328static inline bool
329predicates_equal_p (struct predicate *p, struct predicate *p2)
330{
331 int i;
332 for (i = 0; p->clause[i]; i++)
333 {
334 gcc_checking_assert (i < MAX_CLAUSES);
335 gcc_checking_assert (p->clause [i] > p->clause[i + 1]);
336 gcc_checking_assert (!p2->clause[i] || p2->clause [i] > p2->clause[i + 1]);
337 if (p->clause[i] != p2->clause[i])
338 return false;
339 }
340 return !p2->clause[i];
341}
342
343
632b4f8e
JH
344/* Return P | P2. */
345
346static struct predicate
347or_predicates (struct predicate *p, struct predicate *p2)
348{
349 struct predicate out = true_predicate ();
350 int i,j;
991278ab 351
b15c64ee
JH
352 /* Avoid busy work. */
353 if (false_predicate_p (p2) || true_predicate_p (p))
991278ab 354 return *p;
b15c64ee 355 if (false_predicate_p (p) || true_predicate_p (p2))
991278ab 356 return *p2;
b15c64ee
JH
357 if (predicates_equal_p (p, p2))
358 return *p;
359
360 /* OK, combine the predicates. */
632b4f8e
JH
361 for (i = 0; p->clause[i]; i++)
362 for (j = 0; p2->clause[j]; j++)
f3181aa2
JH
363 {
364 gcc_checking_assert (i < MAX_CLAUSES && j < MAX_CLAUSES);
365 add_clause (&out, p->clause[i] | p2->clause[j]);
366 }
632b4f8e
JH
367 return out;
368}
369
370
632b4f8e
JH
371/* Having partial truth assignment in POSSIBLE_TRUTHS, return false if predicate P
372 to be false. */
373
374static bool
991278ab 375evaluate_predicate (struct predicate *p, clause_t possible_truths)
632b4f8e
JH
376{
377 int i;
378
379 /* True remains true. */
991278ab 380 if (true_predicate_p (p))
632b4f8e
JH
381 return true;
382
991278ab
JH
383 gcc_assert (!(possible_truths & (1 << predicate_false_condition)));
384
632b4f8e
JH
385 /* See if we can find clause we can disprove. */
386 for (i = 0; p->clause[i]; i++)
f3181aa2
JH
387 {
388 gcc_checking_assert (i < MAX_CLAUSES);
389 if (!(p->clause[i] & possible_truths))
390 return false;
391 }
632b4f8e
JH
392 return true;
393}
394
395
396/* Dump conditional COND. */
397
398static void
399dump_condition (FILE *f, conditions conditions, int cond)
400{
401 condition *c;
402 if (cond == predicate_false_condition)
403 fprintf (f, "false");
404 else if (cond == predicate_not_inlined_condition)
405 fprintf (f, "not inlined");
406 else
407 {
408 c = VEC_index (condition, conditions, cond - predicate_first_dynamic_condition);
409 fprintf (f, "op%i", c->operand_num);
410 if (c->code == IS_NOT_CONSTANT)
411 {
412 fprintf (f, " not constant");
413 return;
414 }
415 fprintf (f, " %s ", op_symbol_code (c->code));
416 print_generic_expr (f, c->val, 1);
417 }
418}
419
420
421/* Dump clause CLAUSE. */
422
423static void
424dump_clause (FILE *f, conditions conds, clause_t clause)
425{
426 int i;
427 bool found = false;
428 fprintf (f, "(");
429 if (!clause)
430 fprintf (f, "true");
431 for (i = 0; i < NUM_CONDITIONS; i++)
432 if (clause & (1 << i))
433 {
434 if (found)
435 fprintf (f, " || ");
436 found = true;
437 dump_condition (f, conds, i);
438 }
439 fprintf (f, ")");
440}
441
442
443/* Dump predicate PREDICATE. */
444
445static void
446dump_predicate (FILE *f, conditions conds, struct predicate *pred)
447{
448 int i;
991278ab 449 if (true_predicate_p (pred))
632b4f8e
JH
450 dump_clause (f, conds, 0);
451 else
452 for (i = 0; pred->clause[i]; i++)
453 {
454 if (i)
455 fprintf (f, " && ");
456 dump_clause (f, conds, pred->clause[i]);
457 }
458 fprintf (f, "\n");
459}
460
461
462/* Record SIZE and TIME under condition PRED into the inline summary. */
463
464static void
465account_size_time (struct inline_summary *summary, int size, int time, struct predicate *pred)
466{
467 size_time_entry *e;
468 bool found = false;
469 int i;
470
991278ab 471 if (false_predicate_p (pred))
632b4f8e
JH
472 return;
473
474 /* We need to create initial empty unconitional clause, but otherwie
475 we don't need to account empty times and sizes. */
476 if (!size && !time && summary->conds)
477 return;
478
479 /* Watch overflow that might result from insane profiles. */
480 if (time > MAX_TIME * INLINE_TIME_SCALE)
481 time = MAX_TIME * INLINE_TIME_SCALE;
482 gcc_assert (time >= 0);
483
484 for (i = 0; VEC_iterate (size_time_entry, summary->entry, i, e); i++)
485 if (predicates_equal_p (&e->predicate, pred))
486 {
487 found = true;
488 break;
489 }
490 if (i == 32)
491 {
492 i = 0;
493 found = true;
494 e = VEC_index (size_time_entry, summary->entry, 0);
495 gcc_assert (!e->predicate.clause[0]);
496 }
497 if (dump_file && (dump_flags & TDF_DETAILS) && (time || size))
498 {
499 fprintf (dump_file, "\t\tAccounting size:%3.2f, time:%3.2f on %spredicate:",
500 ((double)size) / INLINE_SIZE_SCALE, ((double)time) / INLINE_TIME_SCALE,
501 found ? "" : "new ");
502 dump_predicate (dump_file, summary->conds, pred);
503 }
504 if (!found)
505 {
506 struct size_time_entry new_entry;
507 new_entry.size = size;
508 new_entry.time = time;
509 new_entry.predicate = *pred;
510 VEC_safe_push (size_time_entry, gc, summary->entry, &new_entry);
511 }
512 else
513 {
514 e->size += size;
515 e->time += time;
516 if (e->time > MAX_TIME * INLINE_TIME_SCALE)
517 e->time = MAX_TIME * INLINE_TIME_SCALE;
518 }
519}
520
991278ab
JH
521/* Set predicate for edge E. */
522
523static void
524edge_set_predicate (struct cgraph_edge *e, struct predicate *predicate)
525{
526 struct inline_edge_summary *es = inline_edge_summary (e);
527 if (predicate && !true_predicate_p (predicate))
528 {
529 if (!es->predicate)
530 es->predicate = (struct predicate *)pool_alloc (edge_predicate_pool);
531 *es->predicate = *predicate;
532 }
533 else
534 {
535 if (es->predicate)
536 pool_free (edge_predicate_pool, es->predicate);
537 es->predicate = NULL;
538 }
539}
540
632b4f8e
JH
541
542/* Work out what conditions might be true at invocation of E. */
543
544static clause_t
991278ab 545evaluate_conditions_for_edge (struct cgraph_edge *e, bool inline_p)
632b4f8e
JH
546{
547 clause_t clause = inline_p ? 0 : 1 << predicate_not_inlined_condition;
548 struct inline_summary *info = inline_summary (e->callee);
549 int i;
550
551 if (ipa_node_params_vector && info->conds
552 /* FIXME: it seems that we forget to get argument count in some cases,
553 probaby for previously indirect edges or so. */
554 && ipa_get_cs_argument_count (IPA_EDGE_REF (e)))
555 {
556 struct ipa_node_params *parms_info;
557 struct ipa_edge_args *args = IPA_EDGE_REF (e);
558 int i, count = ipa_get_cs_argument_count (args);
559 struct ipcp_lattice lat;
560 struct condition *c;
561 VEC (tree, heap) *known_vals = NULL;
562
563 if (e->caller->global.inlined_to)
564 parms_info = IPA_NODE_REF (e->caller->global.inlined_to);
565 else
566 parms_info = IPA_NODE_REF (e->caller);
567
568 VEC_safe_grow_cleared (tree, heap, known_vals, count);
569 for (i = 0; i < count; i++)
570 {
571 ipa_lattice_from_jfunc (parms_info, &lat, ipa_get_ith_jump_func (args, i));
572 if (lat.type == IPA_CONST_VALUE)
573 VEC_replace (tree, known_vals, i, lat.constant);
574 }
575 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
576 {
577 tree val = VEC_index (tree, known_vals, c->operand_num);
578 tree res;
579
580 if (!val)
581 {
582 clause |= 1 << (i + predicate_first_dynamic_condition);
583 continue;
584 }
585 if (c->code == IS_NOT_CONSTANT)
586 continue;
587 res = fold_binary_to_constant (c->code, boolean_type_node, val, c->val);
588 if (res
589 && integer_zerop (res))
590 continue;
591 clause |= 1 << (i + predicate_first_dynamic_condition);
592 }
593 VEC_free (tree, heap, known_vals);
594 }
595 else
596 for (i = 0; i < (int)VEC_length (condition, info->conds); i++)
597 clause |= 1 << (i + predicate_first_dynamic_condition);
598
599 return clause;
600}
601
10a5dd5d
JH
602
603/* Allocate the inline summary vector or resize it to cover all cgraph nodes. */
604
605static void
606inline_summary_alloc (void)
607{
608 if (!node_removal_hook_holder)
609 node_removal_hook_holder =
610 cgraph_add_node_removal_hook (&inline_node_removal_hook, NULL);
898b8927
JH
611 if (!edge_removal_hook_holder)
612 edge_removal_hook_holder =
613 cgraph_add_edge_removal_hook (&inline_edge_removal_hook, NULL);
10a5dd5d
JH
614 if (!node_duplication_hook_holder)
615 node_duplication_hook_holder =
616 cgraph_add_node_duplication_hook (&inline_node_duplication_hook, NULL);
898b8927
JH
617 if (!edge_duplication_hook_holder)
618 edge_duplication_hook_holder =
619 cgraph_add_edge_duplication_hook (&inline_edge_duplication_hook, NULL);
10a5dd5d
JH
620
621 if (VEC_length (inline_summary_t, inline_summary_vec)
622 <= (unsigned) cgraph_max_uid)
632b4f8e 623 VEC_safe_grow_cleared (inline_summary_t, gc,
10a5dd5d 624 inline_summary_vec, cgraph_max_uid + 1);
898b8927
JH
625 if (VEC_length (inline_edge_summary_t, inline_edge_summary_vec)
626 <= (unsigned) cgraph_edge_max_uid)
627 VEC_safe_grow_cleared (inline_edge_summary_t, heap,
628 inline_edge_summary_vec, cgraph_edge_max_uid + 1);
991278ab
JH
629 if (!edge_predicate_pool)
630 edge_predicate_pool = create_alloc_pool ("edge predicates", sizeof (struct predicate),
631 10);
10a5dd5d
JH
632}
633
634/* Hook that is called by cgraph.c when a node is removed. */
635
636static void
637inline_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
638{
e7f23018 639 struct inline_summary *info;
10a5dd5d
JH
640 if (VEC_length (inline_summary_t, inline_summary_vec)
641 <= (unsigned)node->uid)
642 return;
e7f23018 643 info = inline_summary (node);
632b4f8e
JH
644 reset_node_growth_cache (node);
645 VEC_free (condition, gc, info->conds);
646 VEC_free (size_time_entry, gc, info->entry);
647 info->conds = NULL;
648 info->entry = NULL;
e7f23018 649 memset (info, 0, sizeof (inline_summary_t));
10a5dd5d
JH
650}
651
898b8927 652
10a5dd5d
JH
653/* Hook that is called by cgraph.c when a node is duplicated. */
654
655static void
656inline_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
657 ATTRIBUTE_UNUSED void *data)
658{
e7f23018 659 struct inline_summary *info;
10a5dd5d 660 inline_summary_alloc ();
e7f23018
JH
661 info = inline_summary (dst);
662 memcpy (info, inline_summary (src),
10a5dd5d 663 sizeof (struct inline_summary));
632b4f8e
JH
664 info->conds = VEC_copy (condition, gc, info->conds);
665 info->entry = VEC_copy (size_time_entry, gc, info->entry);
666}
667
668
898b8927
JH
669/* Hook that is called by cgraph.c when a node is duplicated. */
670
671static void
672inline_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
673 ATTRIBUTE_UNUSED void *data)
674{
675 struct inline_edge_summary *info;
991278ab 676 struct inline_edge_summary *srcinfo;
898b8927
JH
677 inline_summary_alloc ();
678 info = inline_edge_summary (dst);
991278ab
JH
679 srcinfo = inline_edge_summary (src);
680 memcpy (info, srcinfo,
898b8927 681 sizeof (struct inline_edge_summary));
991278ab
JH
682 info->predicate = NULL;
683 edge_set_predicate (dst, srcinfo->predicate);
898b8927
JH
684}
685
686
632b4f8e
JH
687/* Keep edge cache consistent across edge removal. */
688
689static void
690inline_edge_removal_hook (struct cgraph_edge *edge, void *data ATTRIBUTE_UNUSED)
691{
898b8927
JH
692 if (edge_growth_cache)
693 reset_edge_growth_cache (edge);
694 if (edge->uid < (int)VEC_length (inline_edge_summary_t, inline_edge_summary_vec))
991278ab
JH
695 {
696 edge_set_predicate (edge, NULL);
697 memset (inline_edge_summary (edge), 0, sizeof (struct inline_edge_summary));
698 }
632b4f8e
JH
699}
700
701
702/* Initialize growth caches. */
703
704void
705initialize_growth_caches (void)
706{
632b4f8e
JH
707 if (cgraph_edge_max_uid)
708 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
709 cgraph_edge_max_uid);
710 if (cgraph_max_uid)
711 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
712}
713
714
715/* Free growth caches. */
716
717void
718free_growth_caches (void)
719{
632b4f8e
JH
720 VEC_free (edge_growth_cache_entry, heap, edge_growth_cache);
721 edge_growth_cache = 0;
722 VEC_free (int, heap, node_growth_cache);
723 node_growth_cache = 0;
10a5dd5d
JH
724}
725
632b4f8e 726
898b8927
JH
727/* Dump edge summaries associated to NODE and recursively to all clones.
728 Indent by INDENT. */
729
730static void
991278ab
JH
731dump_inline_edge_summary (FILE * f, int indent, struct cgraph_node *node,
732 struct inline_summary *info)
898b8927
JH
733{
734 struct cgraph_edge *edge;
735 for (edge = node->callees; edge; edge = edge->next_callee)
736 {
737 struct inline_edge_summary *es = inline_edge_summary (edge);
991278ab 738 fprintf (f, "%*s%s/%i %s\n%*s loop depth:%2i freq:%4i size:%2i time: %2i",
898b8927
JH
739 indent, "", cgraph_node_name (edge->callee),
740 edge->callee->uid,
991278ab 741 !edge->inline_failed ? "inlined"
898b8927
JH
742 : cgraph_inline_failed_string (edge->inline_failed),
743 indent, "",
744 es->loop_depth,
745 edge->frequency,
746 es->call_stmt_size,
747 es->call_stmt_time);
991278ab
JH
748 if (es->predicate)
749 {
750 fprintf (f, " predicate: ");
751 dump_predicate (f, info->conds, es->predicate);
752 }
753 else
754 fprintf (f, "\n");
898b8927 755 if (!edge->inline_failed)
991278ab 756 dump_inline_edge_summary (f, indent+2, edge->callee, info);
898b8927
JH
757 }
758 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
759 {
760 struct inline_edge_summary *es = inline_edge_summary (edge);
761 fprintf (f, "%*sindirect call loop depth:%2i freq:%4i size:%2i time: %2i\n",
762 indent, "",
763 es->loop_depth,
764 edge->frequency,
765 es->call_stmt_size,
766 es->call_stmt_time);
991278ab
JH
767 if (es->predicate)
768 {
769 fprintf (f, "predicate: ");
770 dump_predicate (f, info->conds, es->predicate);
771 }
772 else
773 fprintf (f, "\n");
898b8927
JH
774 }
775}
776
777
10a5dd5d 778static void
632b4f8e 779dump_inline_summary (FILE * f, struct cgraph_node *node)
10a5dd5d
JH
780{
781 if (node->analyzed)
782 {
783 struct inline_summary *s = inline_summary (node);
632b4f8e
JH
784 size_time_entry *e;
785 int i;
e7f23018 786 fprintf (f, "Inline summary for %s/%i", cgraph_node_name (node),
10a5dd5d 787 node->uid);
4c0f7679 788 if (DECL_DISREGARD_INLINE_LIMITS (node->decl))
e7f23018
JH
789 fprintf (f, " always_inline");
790 if (s->inlinable)
791 fprintf (f, " inlinable");
792 if (s->versionable)
793 fprintf (f, " versionable");
632b4f8e
JH
794 fprintf (f, "\n self time: %i\n",
795 s->self_time);
e7f23018 796 fprintf (f, " global time: %i\n", s->time);
632b4f8e
JH
797 fprintf (f, " self size: %i\n",
798 s->self_size);
4c0f7679 799 fprintf (f, " global size: %i\n", s->size);
10a5dd5d 800 fprintf (f, " self stack: %i\n",
632b4f8e
JH
801 (int) s->estimated_self_stack_size);
802 fprintf (f, " global stack: %i\n",
803 (int) s->estimated_stack_size);
804 for (i = 0;
805 VEC_iterate (size_time_entry, s->entry, i, e);
806 i++)
807 {
808 fprintf (f, " size:%f, time:%f, predicate:",
809 (double) e->size / INLINE_SIZE_SCALE,
810 (double) e->time / INLINE_TIME_SCALE);
811 dump_predicate (f, s->conds, &e->predicate);
812 }
898b8927 813 fprintf (f, " calls:\n");
991278ab 814 dump_inline_edge_summary (f, 4, node, s);
632b4f8e 815 fprintf (f, "\n");
10a5dd5d
JH
816 }
817}
818
819void
820debug_inline_summary (struct cgraph_node *node)
821{
822 dump_inline_summary (stderr, node);
823}
824
825void
826dump_inline_summaries (FILE *f)
827{
828 struct cgraph_node *node;
829
830 for (node = cgraph_nodes; node; node = node->next)
898b8927 831 if (node->analyzed && !node->global.inlined_to)
10a5dd5d
JH
832 dump_inline_summary (f, node);
833}
03dfc36d 834
e7f23018
JH
835/* Give initial reasons why inlining would fail on EDGE. This gets either
836 nullified or usually overwritten by more precise reasons later. */
837
838void
839initialize_inline_failed (struct cgraph_edge *e)
840{
841 struct cgraph_node *callee = e->callee;
842
843 if (e->indirect_unknown_callee)
844 e->inline_failed = CIF_INDIRECT_UNKNOWN_CALL;
845 else if (!callee->analyzed)
846 e->inline_failed = CIF_BODY_NOT_AVAILABLE;
847 else if (callee->local.redefined_extern_inline)
848 e->inline_failed = CIF_REDEFINED_EXTERN_INLINE;
849 else if (e->call_stmt && gimple_call_cannot_inline_p (e->call_stmt))
850 e->inline_failed = CIF_MISMATCHED_ARGUMENTS;
851 else
852 e->inline_failed = CIF_FUNCTION_NOT_CONSIDERED;
853}
854
03dfc36d
JH
855/* See if statement might disappear after inlining.
856 0 - means not eliminated
857 1 - half of statements goes away
858 2 - for sure it is eliminated.
859 We are not terribly sophisticated, basically looking for simple abstraction
860 penalty wrappers. */
861
862static int
863eliminated_by_inlining_prob (gimple stmt)
864{
865 enum gimple_code code = gimple_code (stmt);
866 switch (code)
867 {
868 case GIMPLE_RETURN:
869 return 2;
870 case GIMPLE_ASSIGN:
871 if (gimple_num_ops (stmt) != 2)
872 return 0;
873
874 /* Casts of parameters, loads from parameters passed by reference
875 and stores to return value or parameters are often free after
876 inlining dua to SRA and further combining.
877 Assume that half of statements goes away. */
878 if (gimple_assign_rhs_code (stmt) == CONVERT_EXPR
879 || gimple_assign_rhs_code (stmt) == NOP_EXPR
880 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
881 || gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
882 {
883 tree rhs = gimple_assign_rhs1 (stmt);
884 tree lhs = gimple_assign_lhs (stmt);
885 tree inner_rhs = rhs;
886 tree inner_lhs = lhs;
887 bool rhs_free = false;
888 bool lhs_free = false;
889
890 while (handled_component_p (inner_lhs)
891 || TREE_CODE (inner_lhs) == MEM_REF)
892 inner_lhs = TREE_OPERAND (inner_lhs, 0);
893 while (handled_component_p (inner_rhs)
894 || TREE_CODE (inner_rhs) == ADDR_EXPR
895 || TREE_CODE (inner_rhs) == MEM_REF)
896 inner_rhs = TREE_OPERAND (inner_rhs, 0);
897
898
899 if (TREE_CODE (inner_rhs) == PARM_DECL
900 || (TREE_CODE (inner_rhs) == SSA_NAME
901 && SSA_NAME_IS_DEFAULT_DEF (inner_rhs)
902 && TREE_CODE (SSA_NAME_VAR (inner_rhs)) == PARM_DECL))
903 rhs_free = true;
904 if (rhs_free && is_gimple_reg (lhs))
905 lhs_free = true;
906 if (((TREE_CODE (inner_lhs) == PARM_DECL
907 || (TREE_CODE (inner_lhs) == SSA_NAME
908 && SSA_NAME_IS_DEFAULT_DEF (inner_lhs)
909 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == PARM_DECL))
910 && inner_lhs != lhs)
911 || TREE_CODE (inner_lhs) == RESULT_DECL
912 || (TREE_CODE (inner_lhs) == SSA_NAME
913 && TREE_CODE (SSA_NAME_VAR (inner_lhs)) == RESULT_DECL))
914 lhs_free = true;
915 if (lhs_free
916 && (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs)))
917 rhs_free = true;
918 if (lhs_free && rhs_free)
919 return 1;
920 }
921 return 0;
922 default:
923 return 0;
924 }
925}
926
927
b15c64ee
JH
928/* If BB ends by a conditional we can turn into predicates, attach corresponding
929 predicates to the CFG edges. */
632b4f8e 930
b15c64ee
JH
931static void
932set_cond_stmt_execution_predicate (struct ipa_node_params *info,
933 struct inline_summary *summary,
934 basic_block bb)
632b4f8e 935{
632b4f8e
JH
936 gimple last;
937 tree op;
938 int index;
b15c64ee
JH
939 enum tree_code code, inverted_code;
940 edge e;
941 edge_iterator ei;
942 gimple set_stmt;
943 tree op2;
632b4f8e 944
b15c64ee 945 last = last_stmt (bb);
632b4f8e
JH
946 if (!last
947 || gimple_code (last) != GIMPLE_COND)
b15c64ee 948 return;
632b4f8e 949 if (!is_gimple_ip_invariant (gimple_cond_rhs (last)))
b15c64ee 950 return;
632b4f8e
JH
951 op = gimple_cond_lhs (last);
952 /* TODO: handle conditionals like
953 var = op0 < 4;
b15c64ee
JH
954 if (var != 0). */
955 if (TREE_CODE (op) != SSA_NAME)
956 return;
957 if (SSA_NAME_IS_DEFAULT_DEF (op))
958 {
959 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
960 if (index == -1)
961 return;
962 code = gimple_cond_code (last);
963 inverted_code = invert_tree_comparison (code,
964 HONOR_NANS (TYPE_MODE (TREE_TYPE (op))));
965
966 FOR_EACH_EDGE (e, ei, bb->succs)
967 {
968 struct predicate p = add_condition (summary,
969 index,
970 e->flags & EDGE_TRUE_VALUE
971 ? code : inverted_code,
972 gimple_cond_rhs (last));
973 e->aux = pool_alloc (edge_predicate_pool);
974 *(struct predicate *)e->aux = p;
975 }
976 }
977
978 /* Special case
979 if (builtin_constant_p (op))
980 constant_code
981 else
982 nonconstant_code.
983 Here we can predicate nonconstant_code. We can't
984 really handle constant_code since we have no predicate
985 for this and also the constant code is not known to be
986 optimized away when inliner doen't see operand is constant.
987 Other optimizers might think otherwise. */
988 set_stmt = SSA_NAME_DEF_STMT (op);
989 if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P)
990 || gimple_call_num_args (set_stmt) != 1)
991 return;
992 op2 = gimple_call_arg (set_stmt, 0);
993 if (!SSA_NAME_IS_DEFAULT_DEF (op2))
994 return;
995 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op2));
996 if (index == -1)
997 return;
998 if (gimple_cond_code (last) != NE_EXPR
999 || !integer_zerop (gimple_cond_rhs (last)))
1000 return;
1001 FOR_EACH_EDGE (e, ei, bb->succs)
1002 if (e->flags & EDGE_FALSE_VALUE)
1003 {
1004 struct predicate p = add_condition (summary,
1005 index,
1006 IS_NOT_CONSTANT,
1007 NULL);
1008 e->aux = pool_alloc (edge_predicate_pool);
1009 *(struct predicate *)e->aux = p;
1010 }
1011}
1012
1013
1014/* If BB ends by a switch we can turn into predicates, attach corresponding
1015 predicates to the CFG edges. */
1016
1017static void
1018set_switch_stmt_execution_predicate (struct ipa_node_params *info,
1019 struct inline_summary *summary,
1020 basic_block bb)
1021{
1022 gimple last;
1023 tree op;
1024 int index;
1025 edge e;
1026 edge_iterator ei;
1027 size_t n;
1028 size_t case_idx;
1029
1030 last = last_stmt (bb);
1031 if (!last
1032 || gimple_code (last) != GIMPLE_SWITCH)
1033 return;
1034 op = gimple_switch_index (last);
632b4f8e
JH
1035 if (TREE_CODE (op) != SSA_NAME
1036 || !SSA_NAME_IS_DEFAULT_DEF (op))
b15c64ee 1037 return;
632b4f8e
JH
1038 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op));
1039 if (index == -1)
b15c64ee 1040 return;
632b4f8e 1041
b15c64ee
JH
1042 FOR_EACH_EDGE (e, ei, bb->succs)
1043 {
1044 e->aux = pool_alloc (edge_predicate_pool);
1045 *(struct predicate *)e->aux = false_predicate ();
1046 }
1047 n = gimple_switch_num_labels(last);
1048 for (case_idx = 0; case_idx < n; ++case_idx)
1049 {
1050 tree cl = gimple_switch_label (last, case_idx);
1051 tree min, max;
1052 struct predicate p;
632b4f8e 1053
b15c64ee
JH
1054 e = find_edge (bb, label_to_block (CASE_LABEL (cl)));
1055 min = CASE_LOW (cl);
1056 max = CASE_HIGH (cl);
1057
1058 /* For default we might want to construct predicate that none
1059 of cases is met, but it is bit hard to do not having negations
1060 of conditionals handy. */
1061 if (!min && !max)
1062 p = true_predicate ();
1063 else if (!max)
1064 p = add_condition (summary, index,
1065 EQ_EXPR,
1066 min);
1067 else
1068 {
1069 struct predicate p1, p2;
1070 p1 = add_condition (summary, index,
1071 GE_EXPR,
1072 min);
1073 p2 = add_condition (summary, index,
1074 LE_EXPR,
1075 max);
1076 p = and_predicates (&p1, &p2);
1077 }
1078 *(struct predicate *)e->aux
1079 = or_predicates (&p, (struct predicate *)e->aux);
1080 }
1081}
1082
1083
1084/* For each BB in NODE attach to its AUX pointer predicate under
1085 which it is executable. */
1086
1087static void
1088compute_bb_predicates (struct cgraph_node *node,
1089 struct ipa_node_params *parms_info,
1090 struct inline_summary *summary)
1091{
1092 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1093 bool done = false;
1094 basic_block bb;
1095
1096 FOR_EACH_BB_FN (bb, my_function)
1097 {
1098 set_cond_stmt_execution_predicate (parms_info, summary, bb);
1099 set_switch_stmt_execution_predicate (parms_info, summary, bb);
1100 }
1101
1102 /* Entry block is always executable. */
1103 ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux = pool_alloc (edge_predicate_pool);
1104 *(struct predicate *)ENTRY_BLOCK_PTR_FOR_FUNCTION (my_function)->aux
1105 = true_predicate ();
1106
1107 /* A simple dataflow propagation of predicates forward in the CFG.
1108 TODO: work in reverse postorder. */
1109 while (!done)
1110 {
1111 done = true;
1112 FOR_EACH_BB_FN (bb, my_function)
1113 {
1114 struct predicate p = false_predicate ();
1115 edge e;
1116 edge_iterator ei;
1117 FOR_EACH_EDGE (e, ei, bb->preds)
1118 {
1119 if (e->src->aux)
1120 {
1121 struct predicate this_bb_predicate = *(struct predicate *)e->src->aux;
1122 if (e->aux)
1123 this_bb_predicate = and_predicates (&this_bb_predicate,
1124 (struct predicate *)e->aux);
1125 p = or_predicates (&p, &this_bb_predicate);
1126 if (true_predicate_p (&p))
1127 break;
1128 }
1129 }
1130 if (false_predicate_p (&p))
1131 gcc_assert (!bb->aux);
1132 else
1133 {
1134 if (!bb->aux)
1135 {
1136 done = false;
1137 bb->aux = pool_alloc (edge_predicate_pool);
1138 *((struct predicate *)bb->aux) = p;
1139 }
1140 else if (!predicates_equal_p (&p, (struct predicate *)bb->aux))
1141 {
1142 done = false;
1143 *((struct predicate *)bb->aux) = p;
1144 }
1145 }
1146 }
1147 }
632b4f8e
JH
1148}
1149
970dabbd
JH
1150
1151/* We keep info about constantness of SSA names. */
1152
1153typedef struct predicate predicate_t;
1154DEF_VEC_O (predicate_t);
1155DEF_VEC_ALLOC_O (predicate_t, heap);
1156
1157
1158/* Return predicate specifying when the STMT might have result that is not a compile
1159 time constant. */
1160
632b4f8e
JH
1161static struct predicate
1162will_be_nonconstant_predicate (struct ipa_node_params *info,
1163 struct inline_summary *summary,
970dabbd
JH
1164 gimple stmt,
1165 VEC (predicate_t, heap) *nonconstant_names)
1166
632b4f8e
JH
1167{
1168 struct predicate p = true_predicate ();
1169 ssa_op_iter iter;
1170 tree use;
1171 struct predicate op_non_const;
1172
1173 /* What statments might be optimized away
1174 when their arguments are constant
b15c64ee
JH
1175 TODO: also trivial builtins.
1176 builtin_constant_p is already handled later. */
632b4f8e
JH
1177 if (gimple_code (stmt) != GIMPLE_ASSIGN
1178 && gimple_code (stmt) != GIMPLE_COND
1179 && gimple_code (stmt) != GIMPLE_SWITCH)
1180 return p;
1181
970dabbd
JH
1182 /* Stores and loads will stay anyway.
1183 TODO: Constant memory accesses could be handled here, too. */
632b4f8e
JH
1184 if (gimple_vuse (stmt))
1185 return p;
1186
1187 /* See if we understand all operands before we start
1188 adding conditionals. */
1189 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1190 {
970dabbd 1191 if (TREE_CODE (use) != SSA_NAME)
632b4f8e 1192 return p;
970dabbd
JH
1193 /* For arguments we can build a condition. */
1194 if (SSA_NAME_IS_DEFAULT_DEF (use)
1195 && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
1196 continue;
1197 /* If we know when operand is constant,
1198 we still can say something useful. */
1199 if (!true_predicate_p (VEC_index (predicate_t, nonconstant_names,
1200 SSA_NAME_VERSION (use))))
1201 continue;
1202 return p;
632b4f8e
JH
1203 }
1204 op_non_const = false_predicate ();
1205 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
1206 {
970dabbd
JH
1207 if (SSA_NAME_IS_DEFAULT_DEF (use)
1208 && ipa_get_param_decl_index (info, SSA_NAME_VAR (use)) >= 0)
1209 p = add_condition (summary,
1210 ipa_get_param_decl_index (info, SSA_NAME_VAR (use)),
1211 IS_NOT_CONSTANT, NULL);
1212 else
1213 p = *VEC_index (predicate_t, nonconstant_names,
1214 SSA_NAME_VERSION (use));
632b4f8e
JH
1215 op_non_const = or_predicates (&p, &op_non_const);
1216 }
970dabbd
JH
1217 if (gimple_code (stmt) == GIMPLE_ASSIGN
1218 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
1219 VEC_replace (predicate_t, nonconstant_names,
1220 SSA_NAME_VERSION (gimple_assign_lhs (stmt)), &op_non_const);
632b4f8e
JH
1221 return op_non_const;
1222}
1223
1224
1225/* Compute function body size parameters for NODE.
1226 When EARLY is true, we compute only simple summaries without
1227 non-trivial predicates to drive the early inliner. */
03dfc36d
JH
1228
1229static void
632b4f8e 1230estimate_function_body_sizes (struct cgraph_node *node, bool early)
03dfc36d
JH
1231{
1232 gcov_type time = 0;
03dfc36d
JH
1233 /* Estimate static overhead for function prologue/epilogue and alignment. */
1234 int size = 2;
1235 /* Benefits are scaled by probability of elimination that is in range
1236 <0,2>. */
03dfc36d
JH
1237 basic_block bb;
1238 gimple_stmt_iterator bsi;
1239 struct function *my_function = DECL_STRUCT_FUNCTION (node->decl);
1240 int freq;
632b4f8e
JH
1241 struct inline_summary *info = inline_summary (node);
1242 struct predicate bb_predicate;
970dabbd
JH
1243 struct ipa_node_params *parms_info = NULL;
1244 VEC (predicate_t, heap) *nonconstant_names = NULL;
632b4f8e 1245
970dabbd
JH
1246 if (ipa_node_params_vector && !early && optimize)
1247 {
1248 parms_info = IPA_NODE_REF (node);
1249 VEC_safe_grow_cleared (predicate_t, heap, nonconstant_names,
1250 VEC_length (tree, SSANAMES (my_function)));
1251 }
632b4f8e
JH
1252
1253 info->conds = 0;
1254 info->entry = 0;
1255
03dfc36d
JH
1256
1257 if (dump_file)
632b4f8e 1258 fprintf (dump_file, "\nAnalyzing function body size: %s\n",
03dfc36d
JH
1259 cgraph_node_name (node));
1260
632b4f8e
JH
1261 /* When we run into maximal number of entries, we assign everything to the
1262 constant truth case. Be sure to have it in list. */
1263 bb_predicate = true_predicate ();
1264 account_size_time (info, 0, 0, &bb_predicate);
1265
1266 bb_predicate = not_inlined_predicate ();
1267 account_size_time (info, 2 * INLINE_SIZE_SCALE, 0, &bb_predicate);
1268
03dfc36d 1269 gcc_assert (my_function && my_function->cfg);
b15c64ee
JH
1270 if (parms_info)
1271 compute_bb_predicates (node, parms_info, info);
03dfc36d
JH
1272 FOR_EACH_BB_FN (bb, my_function)
1273 {
1274 freq = compute_call_stmt_bb_frequency (node->decl, bb);
632b4f8e
JH
1275
1276 /* TODO: Obviously predicates can be propagated down across CFG. */
1277 if (parms_info)
1278 {
b15c64ee
JH
1279 if (bb->aux)
1280 bb_predicate = *(struct predicate *)bb->aux;
1281 else
1282 bb_predicate = false_predicate ();
632b4f8e
JH
1283 }
1284 else
1285 bb_predicate = true_predicate ();
1286
1287 if (dump_file && (dump_flags & TDF_DETAILS))
1288 {
1289 fprintf (dump_file, "\n BB %i predicate:", bb->index);
1290 dump_predicate (dump_file, info->conds, &bb_predicate);
1291 }
1292
03dfc36d
JH
1293 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1294 {
1295 gimple stmt = gsi_stmt (bsi);
1296 int this_size = estimate_num_insns (stmt, &eni_size_weights);
1297 int this_time = estimate_num_insns (stmt, &eni_time_weights);
1298 int prob;
b15c64ee 1299 struct predicate will_be_nonconstant;
03dfc36d
JH
1300
1301 if (dump_file && (dump_flags & TDF_DETAILS))
1302 {
632b4f8e 1303 fprintf (dump_file, " ");
03dfc36d 1304 print_gimple_stmt (dump_file, stmt, 0, 0);
632b4f8e
JH
1305 fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n",
1306 ((double)freq)/CGRAPH_FREQ_BASE, this_size, this_time);
03dfc36d 1307 }
10a5dd5d
JH
1308
1309 if (is_gimple_call (stmt))
1310 {
1311 struct cgraph_edge *edge = cgraph_edge (node, stmt);
898b8927
JH
1312 struct inline_edge_summary *es = inline_edge_summary (edge);
1313
970dabbd
JH
1314 /* Special case: results of BUILT_IN_CONSTANT_P will be always
1315 resolved as constant. We however don't want to optimize
1316 out the cgraph edges. */
1317 if (nonconstant_names
1318 && gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P)
1319 && gimple_call_lhs (stmt)
1320 && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
1321 {
1322 struct predicate false_p = false_predicate ();
1323 VEC_replace (predicate_t, nonconstant_names,
1324 SSA_NAME_VERSION (gimple_call_lhs (stmt)), &false_p);
1325 }
1326
898b8927
JH
1327 es->call_stmt_size = this_size;
1328 es->call_stmt_time = this_time;
1329 es->loop_depth = bb->loop_depth;
991278ab 1330 edge_set_predicate (edge, &bb_predicate);
4c0f7679 1331
632b4f8e
JH
1332 /* Do not inline calls where we cannot triviall work around
1333 mismatches in argument or return types. */
4c0f7679
JH
1334 if (edge->callee
1335 && !gimple_check_call_matching_types (stmt, edge->callee->decl))
1336 {
1337 edge->call_stmt_cannot_inline_p = true;
1338 gimple_call_set_cannot_inline (stmt, true);
1339 }
1340 else
1341 gcc_assert (!gimple_call_cannot_inline_p (stmt));
10a5dd5d
JH
1342 }
1343
b15c64ee
JH
1344 /* TODO: When conditional jump or swithc is known to be constant, but
1345 we did not translate it into the predicates, we really can account
1346 just maximum of the possible paths. */
1347 if (parms_info)
1348 will_be_nonconstant
1349 = will_be_nonconstant_predicate (parms_info, info,
1350 stmt, nonconstant_names);
632b4f8e
JH
1351 if (this_time || this_size)
1352 {
632b4f8e
JH
1353 struct predicate p;
1354
1355 this_time *= freq;
1356 time += this_time;
1357 size += this_size;
10a5dd5d 1358
632b4f8e
JH
1359 prob = eliminated_by_inlining_prob (stmt);
1360 if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS))
1361 fprintf (dump_file, "\t\t50%% will be eliminated by inlining\n");
1362 if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS))
1363 fprintf (dump_file, "\t\twill eliminated by inlining\n");
1364
1365 if (parms_info)
b15c64ee 1366 p = and_predicates (&bb_predicate, &will_be_nonconstant);
632b4f8e
JH
1367 else
1368 p = true_predicate ();
10a5dd5d 1369
632b4f8e
JH
1370 /* We account everything but the calls. Calls have their own
1371 size/time info attached to cgraph edges. This is neccesary
1372 in order to make the cost disappear after inlining. */
1373 if (!is_gimple_call (stmt))
1374 {
1375 if (prob)
1376 {
1377 struct predicate ip = not_inlined_predicate ();
1378 ip = and_predicates (&ip, &p);
1379 account_size_time (info, this_size * prob,
1380 this_time * prob, &ip);
1381 }
1382 if (prob != 2)
1383 account_size_time (info, this_size * (2 - prob),
1384 this_time * (2 - prob), &p);
1385 }
10a5dd5d 1386
632b4f8e
JH
1387 gcc_assert (time >= 0);
1388 gcc_assert (size >= 0);
1389 }
03dfc36d
JH
1390 }
1391 }
b15c64ee
JH
1392 FOR_ALL_BB_FN (bb, my_function)
1393 {
1394 edge e;
1395 edge_iterator ei;
1396
1397 if (bb->aux)
1398 pool_free (edge_predicate_pool, bb->aux);
1399 bb->aux = NULL;
1400 FOR_EACH_EDGE (e, ei, bb->succs)
1401 {
1402 if (e->aux)
1403 pool_free (edge_predicate_pool, e->aux);
1404 e->aux = NULL;
1405 }
1406 }
03dfc36d 1407 time = (time + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
03dfc36d
JH
1408 if (time > MAX_TIME)
1409 time = MAX_TIME;
03dfc36d
JH
1410 inline_summary (node)->self_time = time;
1411 inline_summary (node)->self_size = size;
970dabbd 1412 VEC_free (predicate_t, heap, nonconstant_names);
632b4f8e
JH
1413 if (dump_file)
1414 {
1415 fprintf (dump_file, "\n");
1416 dump_inline_summary (dump_file, node);
1417 }
03dfc36d
JH
1418}
1419
1420
632b4f8e
JH
1421/* Compute parameters of functions used by inliner.
1422 EARLY is true when we compute parameters for the early inliner */
03dfc36d
JH
1423
1424void
632b4f8e 1425compute_inline_parameters (struct cgraph_node *node, bool early)
03dfc36d
JH
1426{
1427 HOST_WIDE_INT self_stack_size;
1428 struct cgraph_edge *e;
e7f23018 1429 struct inline_summary *info;
03dfc36d
JH
1430
1431 gcc_assert (!node->global.inlined_to);
1432
10a5dd5d
JH
1433 inline_summary_alloc ();
1434
e7f23018
JH
1435 info = inline_summary (node);
1436
03dfc36d
JH
1437 /* Estimate the stack size for the function if we're optimizing. */
1438 self_stack_size = optimize ? estimated_stack_frame_size (node) : 0;
e7f23018
JH
1439 info->estimated_self_stack_size = self_stack_size;
1440 info->estimated_stack_size = self_stack_size;
1441 info->stack_frame_offset = 0;
03dfc36d
JH
1442
1443 /* Can this function be inlined at all? */
e7f23018 1444 info->inlinable = tree_inlinable_function_p (node->decl);
03dfc36d
JH
1445
1446 /* Inlinable functions always can change signature. */
e7f23018 1447 if (info->inlinable)
03dfc36d
JH
1448 node->local.can_change_signature = true;
1449 else
1450 {
1451 /* Functions calling builtin_apply can not change signature. */
1452 for (e = node->callees; e; e = e->next_callee)
1453 if (DECL_BUILT_IN (e->callee->decl)
1454 && DECL_BUILT_IN_CLASS (e->callee->decl) == BUILT_IN_NORMAL
1455 && DECL_FUNCTION_CODE (e->callee->decl) == BUILT_IN_APPLY_ARGS)
1456 break;
1457 node->local.can_change_signature = !e;
1458 }
632b4f8e 1459 estimate_function_body_sizes (node, early);
10a5dd5d 1460
03dfc36d 1461 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
e7f23018
JH
1462 info->time = info->self_time;
1463 info->size = info->self_size;
e7f23018
JH
1464 info->stack_frame_offset = 0;
1465 info->estimated_stack_size = info->estimated_self_stack_size;
03dfc36d
JH
1466}
1467
1468
1469/* Compute parameters of functions used by inliner using
1470 current_function_decl. */
1471
1472static unsigned int
1473compute_inline_parameters_for_current (void)
1474{
632b4f8e 1475 compute_inline_parameters (cgraph_get_node (current_function_decl), true);
03dfc36d
JH
1476 return 0;
1477}
1478
1479struct gimple_opt_pass pass_inline_parameters =
1480{
1481 {
1482 GIMPLE_PASS,
1483 "inline_param", /* name */
1484 NULL, /* gate */
1485 compute_inline_parameters_for_current,/* execute */
1486 NULL, /* sub */
1487 NULL, /* next */
1488 0, /* static_pass_number */
1489 TV_INLINE_HEURISTICS, /* tv_id */
1490 0, /* properties_required */
1491 0, /* properties_provided */
1492 0, /* properties_destroyed */
1493 0, /* todo_flags_start */
1494 0 /* todo_flags_finish */
1495 }
1496};
1497
1498
632b4f8e
JH
1499/* Increase SIZE and TIME for size and time needed to handle edge E. */
1500
1501static void
1502estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *time)
1503{
898b8927
JH
1504 struct inline_edge_summary *es = inline_edge_summary (e);
1505 *size += es->call_stmt_size * INLINE_SIZE_SCALE;
1506 *time += (es->call_stmt_time
632b4f8e
JH
1507 * e->frequency * (INLINE_TIME_SCALE / CGRAPH_FREQ_BASE));
1508 if (*time > MAX_TIME * INLINE_TIME_SCALE)
1509 *time = MAX_TIME * INLINE_TIME_SCALE;
1510}
1511
1512
1513/* Increase SIZE and TIME for size and time needed to handle all calls in NODE. */
1514
1515static void
991278ab
JH
1516estimate_calls_size_and_time (struct cgraph_node *node, int *size, int *time,
1517 clause_t possible_truths)
632b4f8e
JH
1518{
1519 struct cgraph_edge *e;
1520 for (e = node->callees; e; e = e->next_callee)
991278ab
JH
1521 {
1522 struct inline_edge_summary *es = inline_edge_summary (e);
1523 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1524 {
1525 if (e->inline_failed)
1526 estimate_edge_size_and_time (e, size, time);
1527 else
1528 estimate_calls_size_and_time (e->callee, size, time,
1529 possible_truths);
1530 }
1531 }
632b4f8e
JH
1532 /* TODO: look for devirtualizing oppurtunities. */
1533 for (e = node->indirect_calls; e; e = e->next_callee)
991278ab
JH
1534 {
1535 struct inline_edge_summary *es = inline_edge_summary (e);
1536 if (!es->predicate || evaluate_predicate (es->predicate, possible_truths))
1537 estimate_edge_size_and_time (e, size, time);
1538 }
632b4f8e
JH
1539}
1540
1541
1542/* Estimate size and time needed to execute callee of EDGE assuming
1543 that parameters known to be constant at caller of EDGE are
1544 propagated. If INLINE_P is true, it is assumed that call will
1545 be inlined. */
03dfc36d 1546
632b4f8e
JH
1547static void
1548estimate_callee_size_and_time (struct cgraph_edge *edge, bool inline_p,
1549 int *ret_size, int *ret_time)
03dfc36d 1550{
e7f23018 1551 struct inline_summary *info = inline_summary (edge->callee);
991278ab 1552 clause_t clause = evaluate_conditions_for_edge (edge, inline_p);
632b4f8e
JH
1553 size_time_entry *e;
1554 int size = 0, time = 0;
1555 int i;
1556
1557 if (dump_file
1558 && (dump_flags & TDF_DETAILS))
1559 {
1560 bool found = false;
1561 fprintf (dump_file, " Estimating callee body: %s/%i\n"
1562 " Known to be false: ",
1563 cgraph_node_name (edge->callee),
1564 edge->callee->uid);
1565
1566 for (i = predicate_not_inlined_condition;
1567 i < (predicate_first_dynamic_condition
1568 + (int)VEC_length (condition, info->conds)); i++)
1569 if (!(clause & (1 << i)))
1570 {
1571 if (found)
1572 fprintf (dump_file, ", ");
1573 found = true;
1574 dump_condition (dump_file, info->conds, i);
1575 }
1576 }
1577
1578 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
991278ab 1579 if (evaluate_predicate (&e->predicate, clause))
632b4f8e 1580 time += e->time, size += e->size;
e7f23018 1581
632b4f8e
JH
1582 if (time > MAX_TIME * INLINE_TIME_SCALE)
1583 time = MAX_TIME * INLINE_TIME_SCALE;
1584
991278ab 1585 estimate_calls_size_and_time (edge->callee, &size, &time, clause);
632b4f8e
JH
1586 time = (time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
1587 size = (size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
1588
1589
1590 if (dump_file
1591 && (dump_flags & TDF_DETAILS))
1592 fprintf (dump_file, "\n size:%i time:%i\n", size, time);
1593 if (ret_time)
1594 *ret_time = time;
1595 if (ret_size)
1596 *ret_size = size;
1597 return;
1598}
1599
1600
991278ab
JH
1601/* Translate all conditions from callee representation into caller representation and
1602 symbolically evaluate predicate P into new predicate.
1603
1604 INFO is inline_summary of function we are adding predicate into, CALLEE_INFO is summary
1605 of function predicate P is from. OPERAND_MAP is array giving callee formal IDs the
1606 caller formal IDs. POSSSIBLE_TRUTHS is clausule of all callee conditions that
1607 may be true in caller context. TOPLEV_PREDICATE is predicate under which callee
1608 is executed. */
632b4f8e
JH
1609
1610static struct predicate
1611remap_predicate (struct inline_summary *info, struct inline_summary *callee_info,
1612 struct predicate *p,
1613 VEC (int, heap) *operand_map,
991278ab
JH
1614 clause_t possible_truths,
1615 struct predicate *toplev_predicate)
632b4f8e
JH
1616{
1617 int i;
1618 struct predicate out = true_predicate ();
1619
1620 /* True predicate is easy. */
991278ab
JH
1621 if (true_predicate_p (p))
1622 return *toplev_predicate;
632b4f8e
JH
1623 for (i = 0; p->clause[i]; i++)
1624 {
1625 clause_t clause = p->clause[i];
1626 int cond;
1627 struct predicate clause_predicate = false_predicate ();
1628
f3181aa2
JH
1629 gcc_assert (i < MAX_CLAUSES);
1630
632b4f8e
JH
1631 for (cond = 0; cond < NUM_CONDITIONS; cond ++)
1632 /* Do we have condition we can't disprove? */
1633 if (clause & possible_truths & (1 << cond))
1634 {
1635 struct predicate cond_predicate;
1636 /* Work out if the condition can translate to predicate in the
1637 inlined function. */
1638 if (cond >= predicate_first_dynamic_condition)
1639 {
1640 struct condition *c;
1641
1642 c = VEC_index (condition, callee_info->conds,
1643 cond - predicate_first_dynamic_condition);
1644 /* See if we can remap condition operand to caller's operand.
1645 Otherwise give up. */
1646 if (!operand_map
1647 || VEC_index (int, operand_map, c->operand_num) == -1)
1648 cond_predicate = true_predicate ();
1649 else
1650 cond_predicate = add_condition (info,
1651 VEC_index (int, operand_map,
1652 c->operand_num),
1653 c->code, c->val);
1654 }
1655 /* Fixed conditions remains same, construct single
1656 condition predicate. */
1657 else
1658 {
1659 cond_predicate.clause[0] = 1 << cond;
1660 cond_predicate.clause[1] = 0;
1661 }
1662 clause_predicate = or_predicates (&clause_predicate, &cond_predicate);
1663 }
1664 out = and_predicates (&out, &clause_predicate);
1665 }
991278ab 1666 return and_predicates (&out, toplev_predicate);
632b4f8e
JH
1667}
1668
1669
898b8927
JH
1670/* Update summary information of inline clones after inlining.
1671 Compute peak stack usage. */
1672
1673static void
1674inline_update_callee_summaries (struct cgraph_node *node,
1675 int depth)
1676{
1677 struct cgraph_edge *e;
1678 struct inline_summary *callee_info = inline_summary (node);
1679 struct inline_summary *caller_info = inline_summary (node->callers->caller);
1680 HOST_WIDE_INT peak;
1681
1682 callee_info->stack_frame_offset
1683 = caller_info->stack_frame_offset
1684 + caller_info->estimated_self_stack_size;
1685 peak = callee_info->stack_frame_offset
1686 + callee_info->estimated_self_stack_size;
1687 if (inline_summary (node->global.inlined_to)->estimated_stack_size
1688 < peak)
1689 inline_summary (node->global.inlined_to)->estimated_stack_size = peak;
1690 cgraph_propagate_frequency (node);
1691 for (e = node->callees; e; e = e->next_callee)
1692 {
1693 if (!e->inline_failed)
1694 inline_update_callee_summaries (e->callee, depth);
1695 inline_edge_summary (e)->loop_depth += depth;
1696 }
1697 for (e = node->indirect_calls; e; e = e->next_callee)
1698 inline_edge_summary (e)->loop_depth += depth;
1699}
1700
1701
991278ab
JH
1702/* Remap predicates of callees of NODE. Rest of arguments match
1703 remap_predicate. */
1704
1705static void
1706remap_edge_predicates (struct cgraph_node *node,
1707 struct inline_summary *info,
1708 struct inline_summary *callee_info,
1709 VEC (int, heap) *operand_map,
1710 clause_t possible_truths,
1711 struct predicate *toplev_predicate)
1712{
1713 struct cgraph_edge *e;
1714 for (e = node->callees; e; e = e->next_callee)
1715 {
1716 struct inline_edge_summary *es = inline_edge_summary (e);
1717 struct predicate p;
1718 if (es->predicate)
1719 {
1720 p = remap_predicate (info, callee_info,
1721 es->predicate, operand_map, possible_truths,
1722 toplev_predicate);
1723 edge_set_predicate (e, &p);
1724 /* TODO: We should remove the edge for code that will be optimized out,
1725 but we need to keep verifiers and tree-inline happy.
1726 Make it cold for now. */
1727 if (false_predicate_p (&p))
1728 {
1729 e->count = 0;
1730 e->frequency = 0;
1731 }
1732 }
1733 if (!e->inline_failed)
1734 remap_edge_predicates (e->callee, info, callee_info, operand_map,
1735 possible_truths, toplev_predicate);
1736 }
1737 for (e = node->indirect_calls; e; e = e->next_callee)
1738 {
1739 struct inline_edge_summary *es = inline_edge_summary (e);
1740 struct predicate p;
1741 if (es->predicate)
1742 {
1743 p = remap_predicate (info, callee_info,
1744 es->predicate, operand_map, possible_truths,
1745 toplev_predicate);
1746 edge_set_predicate (e, &p);
1747 /* TODO: We should remove the edge for code that will be optimized out,
1748 but we need to keep verifiers and tree-inline happy.
1749 Make it cold for now. */
1750 if (false_predicate_p (&p))
1751 {
1752 e->count = 0;
1753 e->frequency = 0;
1754 }
1755 }
1756 }
1757}
1758
1759
632b4f8e
JH
1760/* We inlined EDGE. Update summary of the function we inlined into. */
1761
1762void
1763inline_merge_summary (struct cgraph_edge *edge)
1764{
1765 struct inline_summary *callee_info = inline_summary (edge->callee);
1766 struct cgraph_node *to = (edge->caller->global.inlined_to
1767 ? edge->caller->global.inlined_to : edge->caller);
1768 struct inline_summary *info = inline_summary (to);
1769 clause_t clause = 0; /* not_inline is known to be false. */
1770 size_time_entry *e;
1771 VEC (int, heap) *operand_map = NULL;
1772 int i;
991278ab
JH
1773 struct predicate toplev_predicate;
1774 struct inline_edge_summary *es = inline_edge_summary (edge);
1775
1776 if (es->predicate)
1777 toplev_predicate = *es->predicate;
1778 else
1779 toplev_predicate = true_predicate ();
632b4f8e
JH
1780
1781 if (ipa_node_params_vector && callee_info->conds
1782 /* FIXME: it seems that we forget to get argument count in some cases,
1783 probaby for previously indirect edges or so.
1784 Removing the test leads to ICE on tramp3d. */
1785 && ipa_get_cs_argument_count (IPA_EDGE_REF (edge)))
1786 {
1787 struct ipa_edge_args *args = IPA_EDGE_REF (edge);
1788 int count = ipa_get_cs_argument_count (args);
1789 int i;
1790
991278ab 1791 clause = evaluate_conditions_for_edge (edge, true);
632b4f8e
JH
1792 VEC_safe_grow_cleared (int, heap, operand_map, count);
1793 for (i = 0; i < count; i++)
1794 {
1795 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
1796 int map = -1;
1797 /* TODO: handle non-NOPs when merging. */
1798 if (jfunc->type == IPA_JF_PASS_THROUGH
1799 && jfunc->value.pass_through.operation == NOP_EXPR)
1800 map = jfunc->value.pass_through.formal_id;
1801 VEC_replace (int, operand_map, i, map);
f3181aa2 1802 gcc_assert (map < ipa_get_param_count (IPA_NODE_REF (to)));
632b4f8e
JH
1803 }
1804 }
1805 for (i = 0; VEC_iterate (size_time_entry, callee_info->entry, i, e); i++)
1806 {
1807 struct predicate p = remap_predicate (info, callee_info,
991278ab
JH
1808 &e->predicate, operand_map, clause,
1809 &toplev_predicate);
632b4f8e
JH
1810 gcov_type add_time = ((gcov_type)e->time * edge->frequency
1811 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1812 if (add_time > MAX_TIME)
1813 add_time = MAX_TIME;
1814 account_size_time (info, e->size, add_time, &p);
1815 }
991278ab
JH
1816 remap_edge_predicates (edge->callee, info, callee_info, operand_map,
1817 clause, &toplev_predicate);
632b4f8e
JH
1818 info->size = 0;
1819 info->time = 0;
1820 for (i = 0; VEC_iterate (size_time_entry, info->entry, i, e); i++)
1821 info->size += e->size, info->time += e->time;
991278ab
JH
1822 estimate_calls_size_and_time (to, &info->size, &info->time,
1823 ~(clause_t)(1 << predicate_false_condition));
898b8927
JH
1824
1825 inline_update_callee_summaries (edge->callee,
1826 inline_edge_summary (edge)->loop_depth);
1827
632b4f8e
JH
1828 info->time = (info->time + INLINE_TIME_SCALE / 2) / INLINE_TIME_SCALE;
1829 info->size = (info->size + INLINE_SIZE_SCALE / 2) / INLINE_SIZE_SCALE;
1830}
1831
1832
1833/* Estimate the time cost for the caller when inlining EDGE.
1834 Only to be called via estimate_edge_time, that handles the
1835 caching mechanism.
1836
1837 When caching, also update the cache entry. Compute both time and
1838 size, since we always need both metrics eventually. */
1839
1840int
1841do_estimate_edge_time (struct cgraph_edge *edge)
1842{
1843 int time;
1844 int size;
1845 gcov_type ret;
898b8927 1846 struct inline_edge_summary *es = inline_edge_summary (edge);
632b4f8e
JH
1847
1848 gcc_checking_assert (edge->inline_failed);
1849 estimate_callee_size_and_time (edge, true, &size, &time);
1850
898b8927 1851 ret = (((gcov_type)time - es->call_stmt_time) * edge->frequency
632b4f8e
JH
1852 + CGRAPH_FREQ_BASE / 2) / CGRAPH_FREQ_BASE;
1853 if (ret > MAX_TIME)
1854 ret = MAX_TIME;
1855
1856 /* When caching, update the cache entry. */
1857 if (edge_growth_cache)
1858 {
1859 int ret_size;
1860 if ((int)VEC_length (edge_growth_cache_entry, edge_growth_cache)
1861 <= edge->uid)
1862 VEC_safe_grow_cleared (edge_growth_cache_entry, heap, edge_growth_cache,
1863 cgraph_edge_max_uid);
1864 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->time
1865 = ret + (ret >= 0);
1866
898b8927
JH
1867 ret_size = size - es->call_stmt_size;
1868 gcc_checking_assert (es->call_stmt_size);
632b4f8e
JH
1869 VEC_index (edge_growth_cache_entry, edge_growth_cache, edge->uid)->size
1870 = ret_size + (ret_size >= 0);
1871 }
1872 return ret;
1873}
1874
1875
1876/* Estimate the growth of the caller when inlining EDGE.
1877 Only to be called via estimate_edge_size. */
1878
1879int
1880do_estimate_edge_growth (struct cgraph_edge *edge)
1881{
1882 int size;
1883
1884 /* When we do caching, use do_estimate_edge_time to populate the entry. */
1885
1886 if (edge_growth_cache)
1887 {
1888 do_estimate_edge_time (edge);
1889 size = VEC_index (edge_growth_cache_entry,
1890 edge_growth_cache,
1891 edge->uid)->size;
1892 gcc_checking_assert (size);
1893 return size - (size > 0);
1894 }
1895
1896 /* Early inliner runs without caching, go ahead and do the dirty work. */
1897 gcc_checking_assert (edge->inline_failed);
1898 estimate_callee_size_and_time (edge, true, &size, NULL);
898b8927
JH
1899 gcc_checking_assert (inline_edge_summary (edge)->call_stmt_size);
1900 return size - inline_edge_summary (edge)->call_stmt_size;
03dfc36d
JH
1901}
1902
1903
1904/* Estimate self time of the function NODE after inlining EDGE. */
1905
1906int
1907estimate_time_after_inlining (struct cgraph_node *node,
1908 struct cgraph_edge *edge)
1909{
b15c64ee
JH
1910 struct inline_edge_summary *es = inline_edge_summary (edge);
1911 if (!es->predicate || !false_predicate_p (es->predicate))
1912 {
1913 gcov_type time = inline_summary (node)->time + estimate_edge_time (edge);
1914 if (time < 0)
1915 time = 0;
1916 if (time > MAX_TIME)
1917 time = MAX_TIME;
1918 return time;
1919 }
1920 return inline_summary (node)->time;
03dfc36d
JH
1921}
1922
1923
1924/* Estimate the size of NODE after inlining EDGE which should be an
1925 edge to either NODE or a call inlined into NODE. */
1926
1927int
1928estimate_size_after_inlining (struct cgraph_node *node,
10a5dd5d 1929 struct cgraph_edge *edge)
03dfc36d 1930{
b15c64ee
JH
1931 struct inline_edge_summary *es = inline_edge_summary (edge);
1932 if (!es->predicate || !false_predicate_p (es->predicate))
1933 {
1934 int size = inline_summary (node)->size + estimate_edge_growth (edge);
1935 gcc_assert (size >= 0);
1936 return size;
1937 }
1938 return inline_summary (node)->size;
03dfc36d
JH
1939}
1940
1941
1942/* Estimate the growth caused by inlining NODE into all callees. */
1943
1944int
632b4f8e 1945do_estimate_growth (struct cgraph_node *node)
03dfc36d
JH
1946{
1947 int growth = 0;
1948 struct cgraph_edge *e;
1949 bool self_recursive = false;
e7f23018 1950 struct inline_summary *info = inline_summary (node);
03dfc36d 1951
03dfc36d
JH
1952 for (e = node->callers; e; e = e->next_caller)
1953 {
4c0f7679
JH
1954 gcc_checking_assert (e->inline_failed);
1955
1956 if (e->caller == node
1957 || (e->caller->global.inlined_to
1958 && e->caller->global.inlined_to == node))
03dfc36d 1959 self_recursive = true;
4c0f7679
JH
1960 growth += estimate_edge_growth (e);
1961 }
1962
1963
1964 /* For self recursive functions the growth estimation really should be
1965 infinity. We don't want to return very large values because the growth
1966 plays various roles in badness computation fractions. Be sure to not
1967 return zero or negative growths. */
1968 if (self_recursive)
1969 growth = growth < info->size ? info->size : growth;
1970 else
1971 {
1972 if (cgraph_will_be_removed_from_program_if_no_direct_calls (node)
1973 && !DECL_EXTERNAL (node->decl))
1974 growth -= info->size;
1975 /* COMDAT functions are very often not shared across multiple units since they
1976 come from various template instantiations. Take this into account. */
1977 else if (DECL_COMDAT (node->decl)
1978 && cgraph_can_remove_if_no_direct_calls_p (node))
1979 growth -= (info->size
1980 * (100 - PARAM_VALUE (PARAM_COMDAT_SHARING_PROBABILITY)) + 50) / 100;
03dfc36d 1981 }
03dfc36d 1982
632b4f8e
JH
1983 if (node_growth_cache)
1984 {
1985 if ((int)VEC_length (int, node_growth_cache) <= node->uid)
1986 VEC_safe_grow_cleared (int, heap, node_growth_cache, cgraph_max_uid);
1987 VEC_replace (int, node_growth_cache, node->uid, growth + (growth >= 0));
1988 }
03dfc36d
JH
1989 return growth;
1990}
1991
10a5dd5d 1992
03dfc36d
JH
1993/* This function performs intraprocedural analysis in NODE that is required to
1994 inline indirect calls. */
10a5dd5d 1995
03dfc36d
JH
1996static void
1997inline_indirect_intraprocedural_analysis (struct cgraph_node *node)
1998{
1999 ipa_analyze_node (node);
2000 if (dump_file && (dump_flags & TDF_DETAILS))
2001 {
2002 ipa_print_node_params (dump_file, node);
2003 ipa_print_node_jump_functions (dump_file, node);
2004 }
2005}
2006
2007
2008/* Note function body size. */
2009
2010static void
2011inline_analyze_function (struct cgraph_node *node)
2012{
2013 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2014 current_function_decl = node->decl;
2015
632b4f8e
JH
2016 if (dump_file)
2017 fprintf (dump_file, "\nAnalyzing function: %s/%u\n",
2018 cgraph_node_name (node), node->uid);
03dfc36d
JH
2019 /* FIXME: We should remove the optimize check after we ensure we never run
2020 IPA passes when not optimizing. */
2021 if (flag_indirect_inlining && optimize)
2022 inline_indirect_intraprocedural_analysis (node);
632b4f8e 2023 compute_inline_parameters (node, false);
03dfc36d
JH
2024
2025 current_function_decl = NULL;
2026 pop_cfun ();
2027}
2028
2029
2030/* Called when new function is inserted to callgraph late. */
2031
2032static void
2033add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2034{
2035 inline_analyze_function (node);
2036}
2037
2038
2039/* Note function body size. */
2040
2041void
2042inline_generate_summary (void)
2043{
2044 struct cgraph_node *node;
2045
2046 function_insertion_hook_holder =
2047 cgraph_add_function_insertion_hook (&add_new_function, NULL);
2048
2049 if (flag_indirect_inlining)
2050 ipa_register_cgraph_hooks ();
2051
2052 for (node = cgraph_nodes; node; node = node->next)
2053 if (node->analyzed)
2054 inline_analyze_function (node);
03dfc36d
JH
2055}
2056
2057
991278ab
JH
2058/* Read predicate from IB. */
2059
2060static struct predicate
2061read_predicate (struct lto_input_block *ib)
2062{
2063 struct predicate out;
2064 clause_t clause;
2065 int k = 0;
2066
2067 do
2068 {
b15c64ee 2069 gcc_assert (k <= MAX_CLAUSES);
991278ab 2070 clause = out.clause[k++] = lto_input_uleb128 (ib);
991278ab
JH
2071 }
2072 while (clause);
2073 return out;
2074}
2075
2076
898b8927
JH
2077/* Write inline summary for edge E to OB. */
2078
2079static void
2080read_inline_edge_summary (struct lto_input_block *ib, struct cgraph_edge *e)
2081{
2082 struct inline_edge_summary *es = inline_edge_summary (e);
991278ab
JH
2083 struct predicate p;
2084
898b8927
JH
2085 es->call_stmt_size = lto_input_uleb128 (ib);
2086 es->call_stmt_time = lto_input_uleb128 (ib);
2087 es->loop_depth = lto_input_uleb128 (ib);
991278ab
JH
2088 p = read_predicate (ib);
2089 edge_set_predicate (e, &p);
898b8927
JH
2090}
2091
2092
632b4f8e
JH
2093/* Stream in inline summaries from the section. */
2094
2095static void
2096inline_read_section (struct lto_file_decl_data *file_data, const char *data,
2097 size_t len)
2098{
2099 const struct lto_function_header *header =
2100 (const struct lto_function_header *) data;
2101 const int32_t cfg_offset = sizeof (struct lto_function_header);
2102 const int32_t main_offset = cfg_offset + header->cfg_size;
2103 const int32_t string_offset = main_offset + header->main_size;
2104 struct data_in *data_in;
2105 struct lto_input_block ib;
2106 unsigned int i, count2, j;
2107 unsigned int f_count;
2108
2109 LTO_INIT_INPUT_BLOCK (ib, (const char *) data + main_offset, 0,
2110 header->main_size);
2111
2112 data_in =
2113 lto_data_in_create (file_data, (const char *) data + string_offset,
2114 header->string_size, NULL);
2115 f_count = lto_input_uleb128 (&ib);
2116 for (i = 0; i < f_count; i++)
2117 {
2118 unsigned int index;
2119 struct cgraph_node *node;
2120 struct inline_summary *info;
2121 lto_cgraph_encoder_t encoder;
2122 struct bitpack_d bp;
898b8927 2123 struct cgraph_edge *e;
632b4f8e
JH
2124
2125 index = lto_input_uleb128 (&ib);
2126 encoder = file_data->cgraph_node_encoder;
2127 node = lto_cgraph_encoder_deref (encoder, index);
2128 info = inline_summary (node);
2129
2130 info->estimated_stack_size
2131 = info->estimated_self_stack_size = lto_input_uleb128 (&ib);
2132 info->size = info->self_size = lto_input_uleb128 (&ib);
2133 info->time = info->self_time = lto_input_uleb128 (&ib);
2134
2135 bp = lto_input_bitpack (&ib);
2136 info->inlinable = bp_unpack_value (&bp, 1);
2137 info->versionable = bp_unpack_value (&bp, 1);
2138
2139 count2 = lto_input_uleb128 (&ib);
2140 gcc_assert (!info->conds);
2141 for (j = 0; j < count2; j++)
2142 {
2143 struct condition c;
2144 c.operand_num = lto_input_uleb128 (&ib);
2145 c.code = (enum tree_code) lto_input_uleb128 (&ib);
2146 c.val = lto_input_tree (&ib, data_in);
2147 VEC_safe_push (condition, gc, info->conds, &c);
2148 }
2149 count2 = lto_input_uleb128 (&ib);
2150 gcc_assert (!info->entry);
2151 for (j = 0; j < count2; j++)
2152 {
2153 struct size_time_entry e;
632b4f8e
JH
2154
2155 e.size = lto_input_uleb128 (&ib);
2156 e.time = lto_input_uleb128 (&ib);
991278ab 2157 e.predicate = read_predicate (&ib);
632b4f8e
JH
2158
2159 VEC_safe_push (size_time_entry, gc, info->entry, &e);
2160 }
898b8927
JH
2161 for (e = node->callees; e; e = e->next_callee)
2162 read_inline_edge_summary (&ib, e);
2163 for (e = node->indirect_calls; e; e = e->next_callee)
2164 read_inline_edge_summary (&ib, e);
632b4f8e
JH
2165 }
2166
2167 lto_free_section_data (file_data, LTO_section_inline_summary, NULL, data,
2168 len);
2169 lto_data_in_delete (data_in);
2170}
2171
2172
03dfc36d
JH
2173/* Read inline summary. Jump functions are shared among ipa-cp
2174 and inliner, so when ipa-cp is active, we don't need to write them
2175 twice. */
2176
2177void
2178inline_read_summary (void)
2179{
10a5dd5d
JH
2180 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2181 struct lto_file_decl_data *file_data;
2182 unsigned int j = 0;
2183
2184 inline_summary_alloc ();
2185
2186 while ((file_data = file_data_vec[j++]))
2187 {
2188 size_t len;
2189 const char *data = lto_get_section_data (file_data, LTO_section_inline_summary, NULL, &len);
632b4f8e
JH
2190 if (data)
2191 inline_read_section (file_data, data, len);
10a5dd5d
JH
2192 else
2193 /* Fatal error here. We do not want to support compiling ltrans units with
2194 different version of compiler or different flags than the WPA unit, so
2195 this should never happen. */
2196 fatal_error ("ipa inline summary is missing in input file");
2197 }
03dfc36d
JH
2198 if (flag_indirect_inlining)
2199 {
2200 ipa_register_cgraph_hooks ();
2201 if (!flag_ipa_cp)
2202 ipa_prop_read_jump_functions ();
2203 }
2204 function_insertion_hook_holder =
2205 cgraph_add_function_insertion_hook (&add_new_function, NULL);
2206}
2207
991278ab
JH
2208
2209/* Write predicate P to OB. */
2210
2211static void
2212write_predicate (struct output_block *ob, struct predicate *p)
2213{
2214 int j;
2215 if (p)
2216 for (j = 0; p->clause[j]; j++)
2217 {
2218 gcc_assert (j < MAX_CLAUSES);
2219 lto_output_uleb128_stream (ob->main_stream,
2220 p->clause[j]);
2221 }
2222 lto_output_uleb128_stream (ob->main_stream, 0);
2223}
2224
2225
898b8927
JH
2226/* Write inline summary for edge E to OB. */
2227
2228static void
2229write_inline_edge_summary (struct output_block *ob, struct cgraph_edge *e)
2230{
2231 struct inline_edge_summary *es = inline_edge_summary (e);
2232 lto_output_uleb128_stream (ob->main_stream, es->call_stmt_size);
2233 lto_output_uleb128_stream (ob->main_stream, es->call_stmt_time);
2234 lto_output_uleb128_stream (ob->main_stream, es->loop_depth);
991278ab 2235 write_predicate (ob, es->predicate);
898b8927
JH
2236}
2237
03dfc36d
JH
2238
2239/* Write inline summary for node in SET.
2240 Jump functions are shared among ipa-cp and inliner, so when ipa-cp is
2241 active, we don't need to write them twice. */
2242
2243void
2244inline_write_summary (cgraph_node_set set,
2245 varpool_node_set vset ATTRIBUTE_UNUSED)
2246{
10a5dd5d 2247 struct cgraph_node *node;
632b4f8e 2248 struct output_block *ob = create_output_block (LTO_section_inline_summary);
10a5dd5d
JH
2249 lto_cgraph_encoder_t encoder = ob->decl_state->cgraph_node_encoder;
2250 unsigned int count = 0;
2251 int i;
2252
2253 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2254 if (lto_cgraph_encoder_deref (encoder, i)->analyzed)
2255 count++;
2256 lto_output_uleb128_stream (ob->main_stream, count);
2257
2258 for (i = 0; i < lto_cgraph_encoder_size (encoder); i++)
2259 {
2260 node = lto_cgraph_encoder_deref (encoder, i);
2261 if (node->analyzed)
2262 {
2263 struct inline_summary *info = inline_summary (node);
e7f23018 2264 struct bitpack_d bp;
898b8927 2265 struct cgraph_edge *edge;
632b4f8e
JH
2266 int i;
2267 size_time_entry *e;
2268 struct condition *c;
2269
e7f23018 2270
10a5dd5d
JH
2271 lto_output_uleb128_stream (ob->main_stream,
2272 lto_cgraph_encoder_encode (encoder, node));
2273 lto_output_sleb128_stream (ob->main_stream,
2274 info->estimated_self_stack_size);
2275 lto_output_sleb128_stream (ob->main_stream,
2276 info->self_size);
10a5dd5d
JH
2277 lto_output_sleb128_stream (ob->main_stream,
2278 info->self_time);
e7f23018
JH
2279 bp = bitpack_create (ob->main_stream);
2280 bp_pack_value (&bp, info->inlinable, 1);
2281 bp_pack_value (&bp, info->versionable, 1);
e7f23018 2282 lto_output_bitpack (&bp);
632b4f8e
JH
2283 lto_output_uleb128_stream (ob->main_stream,
2284 VEC_length (condition, info->conds));
2285 for (i = 0; VEC_iterate (condition, info->conds, i, c); i++)
2286 {
2287 lto_output_uleb128_stream (ob->main_stream,
2288 c->operand_num);
2289 lto_output_uleb128_stream (ob->main_stream,
2290 c->code);
2291 lto_output_tree (ob, c->val, true);
2292 }
2293 lto_output_uleb128_stream (ob->main_stream,
2294 VEC_length (size_time_entry, info->entry));
2295 for (i = 0;
2296 VEC_iterate (size_time_entry, info->entry, i, e);
2297 i++)
2298 {
632b4f8e
JH
2299 lto_output_uleb128_stream (ob->main_stream,
2300 e->size);
7ee28a74
JH
2301 lto_output_uleb128_stream (ob->main_stream,
2302 e->time);
991278ab 2303 write_predicate (ob, &e->predicate);
632b4f8e 2304 }
898b8927
JH
2305 for (edge = node->callees; edge; edge = edge->next_callee)
2306 write_inline_edge_summary (ob, edge);
2307 for (edge = node->indirect_calls; edge; edge = edge->next_callee)
2308 write_inline_edge_summary (ob, edge);
10a5dd5d
JH
2309 }
2310 }
632b4f8e
JH
2311 lto_output_1_stream (ob->main_stream, 0);
2312 produce_asm (ob, NULL);
2313 destroy_output_block (ob);
10a5dd5d 2314
03dfc36d
JH
2315 if (flag_indirect_inlining && !flag_ipa_cp)
2316 ipa_prop_write_jump_functions (set);
2317}
2318
10a5dd5d 2319
03dfc36d
JH
2320/* Release inline summary. */
2321
2322void
2323inline_free_summary (void)
2324{
10a5dd5d
JH
2325 if (function_insertion_hook_holder)
2326 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2327 function_insertion_hook_holder = NULL;
2328 if (node_removal_hook_holder)
2329 cgraph_remove_node_removal_hook (node_removal_hook_holder);
898b8927
JH
2330 if (edge_removal_hook_holder)
2331 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
10a5dd5d
JH
2332 node_removal_hook_holder = NULL;
2333 if (node_duplication_hook_holder)
2334 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
898b8927
JH
2335 if (edge_duplication_hook_holder)
2336 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
10a5dd5d 2337 node_duplication_hook_holder = NULL;
632b4f8e
JH
2338 VEC_free (inline_summary_t, gc, inline_summary_vec);
2339 inline_summary_vec = NULL;
991278ab
JH
2340 VEC_free (inline_edge_summary_t, heap, inline_edge_summary_vec);
2341 inline_edge_summary_vec = NULL;
2342 if (edge_predicate_pool)
2343 free_alloc_pool (edge_predicate_pool);
2344 edge_predicate_pool = 0;
03dfc36d 2345}