]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cgraphunit.c
48cb2398f724afb0dd2b0bbe35baa52f85f70c24
[thirdparty/gcc.git] / gcc / cgraphunit.c
1 /* Callgraph based intraprocedural optimizations.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This module implements main driver of compilation process as well as
23 few basic intraprocedural optimizers.
24
25 The main scope of this file is to act as an interface in between
26 tree based frontends and the backend (and middle end)
27
28 The front-end is supposed to use following functionality:
29
30 - cgraph_finalize_function
31
32 This function is called once front-end has parsed whole body of function
33 and it is certain that the function body nor the declaration will change.
34
35 (There is one exception needed for implementing GCC extern inline function.)
36
37 - cgraph_varpool_finalize_variable
38
39 This function has same behavior as the above but is used for static
40 variables.
41
42 - cgraph_finalize_compilation_unit
43
44 This function is called once compilation unit is finalized and it will
45 no longer change.
46
47 In the unit-at-a-time the call-graph construction and local function
48 analysis takes place here. Bodies of unreachable functions are released
49 to conserve memory usage.
50
51 ??? The compilation unit in this point of view should be compilation
52 unit as defined by the language - for instance C frontend allows multiple
53 compilation units to be parsed at once and it should call function each
54 time parsing is done so we save memory.
55
56 - cgraph_optimize
57
58 In this unit-at-a-time compilation the intra procedural analysis takes
59 place here. In particular the static functions whose address is never
60 taken are marked as local. Backend can then use this information to
61 modify calling conventions, do better inlining or similar optimizations.
62
63 - cgraph_assemble_pending_functions
64 - cgraph_varpool_assemble_pending_variables
65
66 In non-unit-at-a-time mode these functions can be used to force compilation
67 of functions or variables that are known to be needed at given stage
68 of compilation
69
70 - cgraph_mark_needed_node
71 - cgraph_varpool_mark_needed_node
72
73 When function or variable is referenced by some hidden way (for instance
74 via assembly code and marked by attribute "used"), the call-graph data structure
75 must be updated accordingly by this function.
76
77 - analyze_expr callback
78
79 This function is responsible for lowering tree nodes not understood by
80 generic code into understandable ones or alternatively marking
81 callgraph and varpool nodes referenced by the as needed.
82
83 ??? On the tree-ssa genericizing should take place here and we will avoid
84 need for these hooks (replacing them by genericizing hook)
85
86 - expand_function callback
87
88 This function is used to expand function and pass it into RTL back-end.
89 Front-end should not make any assumptions about when this function can be
90 called. In particular cgraph_assemble_pending_functions,
91 cgraph_varpool_assemble_pending_variables, cgraph_finalize_function,
92 cgraph_varpool_finalize_function, cgraph_optimize can cause arbitrarily
93 previously finalized functions to be expanded.
94
95 We implement two compilation modes.
96
97 - unit-at-a-time: In this mode analyzing of all functions is deferred
98 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
99
100 In cgraph_finalize_compilation_unit the reachable functions are
101 analyzed. During analysis the call-graph edges from reachable
102 functions are constructed and their destinations are marked as
103 reachable. References to functions and variables are discovered too
104 and variables found to be needed output to the assembly file. Via
105 mark_referenced call in assemble_variable functions referenced by
106 static variables are noticed too.
107
108 The intra-procedural information is produced and it's existence
109 indicated by global_info_ready. Once this flag is set it is impossible
110 to change function from !reachable to reachable and thus
111 assemble_variable no longer call mark_referenced.
112
113 Finally the call-graph is topologically sorted and all reachable functions
114 that has not been completely inlined or are not external are output.
115
116 ??? It is possible that reference to function or variable is optimized
117 out. We can not deal with this nicely because topological order is not
118 suitable for it. For tree-ssa we may consider another pass doing
119 optimization and re-discovering reachable functions.
120
121 ??? Reorganize code so variables are output very last and only if they
122 really has been referenced by produced code, so we catch more cases
123 where reference has been optimized out.
124
125 - non-unit-at-a-time
126
127 All functions are variables are output as early as possible to conserve
128 memory consumption. This may or may not result in less memory used but
129 it is still needed for some legacy code that rely on particular ordering
130 of things output from the compiler.
131
132 Varpool data structures are not used and variables are output directly.
133
134 Functions are output early using call of
135 cgraph_assemble_pending_function from cgraph_finalize_function. The
136 decision on whether function is needed is made more conservative so
137 uninlininable static functions are needed too. During the call-graph
138 construction the edge destinations are not marked as reachable and it
139 is completely relied upn assemble_variable to mark them.
140
141 Inlining decision heuristics
142 ??? Move this to separate file after tree-ssa merge.
143
144 We separate inlining decisions from the inliner itself and store it
145 inside callgraph as so called inline plan. Reffer to cgraph.c
146 documentation about particular representation of inline plans in the
147 callgraph
148
149 The implementation of particular heuristics is separated from
150 the rest of code to make it easier to replace it with more complicated
151 implementation in the future. The rest of inlining code acts as a
152 library aimed to modify the callgraph and verify that the parameters
153 on code size growth fits.
154
155 To mark given call inline, use cgraph_mark_inline function, the
156 verification is performed by cgraph_default_inline_p and
157 cgraph_check_inline_limits.
158
159 The heuristics implements simple knapsack style algorithm ordering
160 all functions by their "profitability" (estimated by code size growth)
161 and inlining them in priority order.
162
163 cgraph_decide_inlining implements heuristics taking whole callgraph
164 into account, while cgraph_decide_inlining_incrementally considers
165 only one function at a time and is used in non-unit-at-a-time mode. */
166 #include "config.h"
167 #include "system.h"
168 #include "coretypes.h"
169 #include "tm.h"
170 #include "tree.h"
171 #include "rtl.h"
172 #include "tree-inline.h"
173 #include "langhooks.h"
174 #include "hashtab.h"
175 #include "toplev.h"
176 #include "flags.h"
177 #include "ggc.h"
178 #include "debug.h"
179 #include "target.h"
180 #include "cgraph.h"
181 #include "diagnostic.h"
182 #include "timevar.h"
183 #include "params.h"
184 #include "fibheap.h"
185 #include "c-common.h"
186 #include "intl.h"
187 #include "function.h"
188
189 #define INSNS_PER_CALL 10
190
191 static void cgraph_expand_all_functions (void);
192 static void cgraph_mark_functions_to_output (void);
193 static void cgraph_expand_function (struct cgraph_node *);
194 static tree record_call_1 (tree *, int *, void *);
195 static void cgraph_mark_local_functions (void);
196 static bool cgraph_default_inline_p (struct cgraph_node *n);
197 static void cgraph_analyze_function (struct cgraph_node *node);
198 static void cgraph_decide_inlining_incrementally (struct cgraph_node *);
199
200 /* Statistics we collect about inlining algorithm. */
201 static int ncalls_inlined;
202 static int nfunctions_inlined;
203 static int initial_insns;
204 static int overall_insns;
205
206 /* Records tree nodes seen in cgraph_create_edges. Simply using
207 walk_tree_without_duplicates doesn't guarantee each node is visited
208 once because it gets a new htab upon each recursive call from
209 record_calls_1. */
210 static htab_t visited_nodes;
211
212 /* Determine if function DECL is needed. That is, visible to something
213 either outside this translation unit, something magic in the system
214 configury, or (if not doing unit-at-a-time) to something we havn't
215 seen yet. */
216
217 static bool
218 decide_is_function_needed (struct cgraph_node *node, tree decl)
219 {
220 struct cgraph_node *origin;
221
222 /* If we decided it was needed before, but at the time we didn't have
223 the body of the function available, then it's still needed. We have
224 to go back and re-check its dependencies now. */
225 if (node->needed)
226 return true;
227
228 /* Externally visible functions must be output. The exception is
229 COMDAT functions that must be output only when they are needed. */
230 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
231 return true;
232
233 /* Constructors and destructors are reachable from the runtime by
234 some mechanism. */
235 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
236 return true;
237
238 /* If the user told us it is used, then it must be so. */
239 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
240 return true;
241
242 /* ??? If the assembler name is set by hand, it is possible to assemble
243 the name later after finalizing the function and the fact is noticed
244 in assemble_name then. This is arguably a bug. */
245 if (DECL_ASSEMBLER_NAME_SET_P (decl)
246 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
247 return true;
248
249 if (flag_unit_at_a_time)
250 return false;
251
252 /* If not doing unit at a time, then we'll only defer this function
253 if its marked for inlining. Otherwise we want to emit it now. */
254
255 /* "extern inline" functions are never output locally. */
256 if (DECL_EXTERNAL (decl))
257 return false;
258 /* Nested functions of extern inline function shall not be emit unless
259 we inlined the origin. */
260 for (origin = node->origin; origin; origin = origin->origin)
261 if (DECL_EXTERNAL (origin->decl))
262 return false;
263 /* We want to emit COMDAT functions only when absolutely necessary. */
264 if (DECL_COMDAT (decl))
265 return false;
266 if (!DECL_INLINE (decl)
267 || (!node->local.disregard_inline_limits
268 /* When declared inline, defer even the uninlinable functions.
269 This allows them to be eliminated when unused. */
270 && !DECL_DECLARED_INLINE_P (decl)
271 && (!node->local.inlinable || !cgraph_default_inline_p (node))))
272 return true;
273
274 return false;
275 }
276
277 /* When not doing unit-at-a-time, output all functions enqueued.
278 Return true when such a functions were found. */
279
280 bool
281 cgraph_assemble_pending_functions (void)
282 {
283 bool output = false;
284
285 if (flag_unit_at_a_time)
286 return false;
287
288 while (cgraph_nodes_queue)
289 {
290 struct cgraph_node *n = cgraph_nodes_queue;
291
292 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
293 n->next_needed = NULL;
294 if (!n->global.inlined_to && !DECL_EXTERNAL (n->decl))
295 {
296 cgraph_expand_function (n);
297 output = true;
298 }
299 }
300
301 return output;
302 }
303
304 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
305 logic in effect. If NESTED is true, then our caller cannot stand to have
306 the garbage collector run at the moment. We would need to either create
307 a new GC context, or just not compile right now. */
308
309 void
310 cgraph_finalize_function (tree decl, bool nested)
311 {
312 struct cgraph_node *node = cgraph_node (decl);
313
314 if (node->local.finalized)
315 {
316 /* As an GCC extension we allow redefinition of the function. The
317 semantics when both copies of bodies differ is not well defined.
318 We replace the old body with new body so in unit at a time mode
319 we always use new body, while in normal mode we may end up with
320 old body inlined into some functions and new body expanded and
321 inlined in others.
322
323 ??? It may make more sense to use one body for inlining and other
324 body for expanding the function but this is difficult to do. */
325
326 /* If node->output is set, then this is a unit-at-a-time compilation
327 and we have already begun whole-unit analysis. This is *not*
328 testing for whether we've already emitted the function. That
329 case can be sort-of legitimately seen with real function
330 redefinition errors. I would argue that the front end should
331 never present us with such a case, but don't enforce that for now. */
332 if (node->output)
333 abort ();
334
335 /* Reset our data structures so we can analyze the function again. */
336 memset (&node->local, 0, sizeof (node->local));
337 memset (&node->global, 0, sizeof (node->global));
338 memset (&node->rtl, 0, sizeof (node->rtl));
339 node->analyzed = false;
340 node->local.redefined_extern_inline = true;
341 while (node->callees)
342 cgraph_remove_edge (node->callees);
343
344 /* We may need to re-queue the node for assembling in case
345 we already proceeded it and ignored as not needed. */
346 if (node->reachable && !flag_unit_at_a_time)
347 {
348 struct cgraph_node *n;
349
350 for (n = cgraph_nodes_queue; n; n = n->next_needed)
351 if (n == node)
352 break;
353 if (!n)
354 node->reachable = 0;
355 }
356 }
357
358 notice_global_symbol (decl);
359 node->decl = decl;
360 node->local.finalized = true;
361
362 /* If not unit at a time, then we need to create the call graph
363 now, so that called functions can be queued and emitted now. */
364 if (!flag_unit_at_a_time)
365 {
366 cgraph_analyze_function (node);
367 cgraph_decide_inlining_incrementally (node);
368 }
369
370 if (decide_is_function_needed (node, decl))
371 cgraph_mark_needed_node (node);
372
373 /* If not unit at a time, go ahead and emit everything we've found
374 to be reachable at this time. */
375 if (!nested)
376 {
377 if (!cgraph_assemble_pending_functions ())
378 ggc_collect ();
379 }
380
381 /* If we've not yet emitted decl, tell the debug info about it. */
382 if (!TREE_ASM_WRITTEN (decl))
383 (*debug_hooks->deferred_inline_function) (decl);
384
385 /* Possibly warn about unused parameters. */
386 if (warn_unused_parameter)
387 do_warn_unused_parameter (decl);
388 }
389
390 /* Walk tree and record all calls. Called via walk_tree. */
391 static tree
392 record_call_1 (tree *tp, int *walk_subtrees, void *data)
393 {
394 tree t = *tp;
395
396 switch (TREE_CODE (t))
397 {
398 case VAR_DECL:
399 /* ??? Really, we should mark this decl as *potentially* referenced
400 by this function and re-examine whether the decl is actually used
401 after rtl has been generated. */
402 if (TREE_STATIC (t))
403 {
404 cgraph_varpool_mark_needed_node (cgraph_varpool_node (t));
405 if (lang_hooks.callgraph.analyze_expr)
406 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees,
407 data);
408 }
409 break;
410
411 case ADDR_EXPR:
412 if (flag_unit_at_a_time)
413 {
414 /* Record dereferences to the functions. This makes the
415 functions reachable unconditionally. */
416 tree decl = TREE_OPERAND (*tp, 0);
417 if (TREE_CODE (decl) == FUNCTION_DECL)
418 cgraph_mark_needed_node (cgraph_node (decl));
419 }
420 break;
421
422 case CALL_EXPR:
423 {
424 tree decl = get_callee_fndecl (*tp);
425 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
426 {
427 cgraph_create_edge (data, cgraph_node (decl), *tp);
428
429 /* When we see a function call, we don't want to look at the
430 function reference in the ADDR_EXPR that is hanging from
431 the CALL_EXPR we're examining here, because we would
432 conclude incorrectly that the function's address could be
433 taken by something that is not a function call. So only
434 walk the function parameter list, skip the other subtrees. */
435
436 walk_tree (&TREE_OPERAND (*tp, 1), record_call_1, data,
437 visited_nodes);
438 *walk_subtrees = 0;
439 }
440 break;
441 }
442
443 default:
444 /* Save some cycles by not walking types and declaration as we
445 won't find anything useful there anyway. */
446 if (DECL_P (*tp) || TYPE_P (*tp))
447 {
448 *walk_subtrees = 0;
449 break;
450 }
451
452 if ((unsigned int) TREE_CODE (t) >= LAST_AND_UNUSED_TREE_CODE)
453 return lang_hooks.callgraph.analyze_expr (tp, walk_subtrees, data);
454 break;
455 }
456
457 return NULL;
458 }
459
460 /* Create cgraph edges for function calls inside BODY from NODE. */
461
462 void
463 cgraph_create_edges (struct cgraph_node *node, tree body)
464 {
465 /* The nodes we're interested in are never shared, so walk
466 the tree ignoring duplicates. */
467 visited_nodes = htab_create (37, htab_hash_pointer,
468 htab_eq_pointer, NULL);
469 walk_tree (&body, record_call_1, node, visited_nodes);
470 htab_delete (visited_nodes);
471 visited_nodes = NULL;
472 }
473
474 static bool error_found;
475
476 /* Callbrack of verify_cgraph_node. Check that all call_exprs have cgraph
477 nodes. */
478
479 static tree
480 verify_cgraph_node_1 (tree *tp, int *walk_subtrees, void *data)
481 {
482 tree t = *tp;
483 tree decl;
484
485 if (TREE_CODE (t) == CALL_EXPR && (decl = get_callee_fndecl (t)))
486 {
487 struct cgraph_edge *e = cgraph_edge (data, t);
488 if (e)
489 {
490 if (e->aux)
491 {
492 error ("Shared call_expr:");
493 debug_tree (t);
494 error_found = true;
495 }
496 if (e->callee->decl != cgraph_node (decl)->decl)
497 {
498 error ("Edge points to wrong declaration:");
499 debug_tree (e->callee->decl);
500 fprintf (stderr," Instead of:");
501 debug_tree (decl);
502 }
503 e->aux = (void *)1;
504 }
505 else
506 {
507 error ("Missing callgraph edge for call expr:");
508 debug_tree (t);
509 error_found = true;
510 }
511 }
512
513 /* Save some cycles by not walking types and declaration as we
514 won't find anything useful there anyway. */
515 if (DECL_P (*tp) || TYPE_P (*tp))
516 *walk_subtrees = 0;
517
518 return NULL_TREE;
519 }
520
521 /* Verify cgraph nodes of given cgraph node. */
522 void
523 verify_cgraph_node (struct cgraph_node *node)
524 {
525 struct cgraph_edge *e;
526 struct cgraph_node *main_clone;
527
528 timevar_push (TV_CGRAPH_VERIFY);
529 error_found = false;
530 for (e = node->callees; e; e = e->next_callee)
531 if (e->aux)
532 {
533 error ("Aux field set for edge %s->%s",
534 cgraph_node_name (e->caller), cgraph_node_name (e->callee));
535 error_found = true;
536 }
537 for (e = node->callers; e; e = e->next_caller)
538 {
539 if (!e->inline_failed)
540 {
541 if (node->global.inlined_to
542 != (e->caller->global.inlined_to
543 ? e->caller->global.inlined_to : e->caller))
544 {
545 error ("Inlined_to pointer is wrong");
546 error_found = true;
547 }
548 if (node->callers->next_caller)
549 {
550 error ("Multiple inline callers");
551 error_found = true;
552 }
553 }
554 else
555 if (node->global.inlined_to)
556 {
557 error ("Inlined_to pointer set for noninline callers");
558 error_found = true;
559 }
560 }
561 if (!node->callers && node->global.inlined_to)
562 {
563 error ("Inlined_to pointer is set but no predecesors found");
564 error_found = true;
565 }
566 if (node->global.inlined_to == node)
567 {
568 error ("Inlined_to pointer reffers to itself");
569 error_found = true;
570 }
571
572 for (main_clone = cgraph_node (node->decl); main_clone;
573 main_clone = main_clone->next_clone)
574 if (main_clone == node)
575 break;
576 if (!node)
577 {
578 error ("Node not found in DECL_ASSEMBLER_NAME hash");
579 error_found = true;
580 }
581
582 if (node->analyzed
583 && DECL_SAVED_TREE (node->decl) && !TREE_ASM_WRITTEN (node->decl)
584 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
585 {
586 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl),
587 verify_cgraph_node_1, node);
588 for (e = node->callees; e; e = e->next_callee)
589 {
590 if (!e->aux)
591 {
592 error ("Edge %s->%s has no corresponding call_expr",
593 cgraph_node_name (e->caller),
594 cgraph_node_name (e->callee));
595 error_found = true;
596 }
597 e->aux = 0;
598 }
599 }
600 if (error_found)
601 {
602 dump_cgraph_node (stderr, node);
603 internal_error ("verify_cgraph_node failed.");
604 }
605 timevar_pop (TV_CGRAPH_VERIFY);
606 }
607
608 /* Verify whole cgraph structure. */
609 void
610 verify_cgraph (void)
611 {
612 struct cgraph_node *node;
613
614 for (node = cgraph_nodes; node; node = node->next)
615 verify_cgraph_node (node);
616 }
617
618 /* Analyze the function scheduled to be output. */
619 static void
620 cgraph_analyze_function (struct cgraph_node *node)
621 {
622 tree decl = node->decl;
623 struct cgraph_edge *e;
624
625 current_function_decl = decl;
626
627 /* First kill forward declaration so reverse inlining works properly. */
628 cgraph_create_edges (node, DECL_SAVED_TREE (decl));
629
630 node->local.inlinable = tree_inlinable_function_p (decl);
631 node->local.self_insns = estimate_num_insns (DECL_SAVED_TREE (decl));
632 if (node->local.inlinable)
633 node->local.disregard_inline_limits
634 = lang_hooks.tree_inlining.disregard_inline_limits (decl);
635 for (e = node->callers; e; e = e->next_caller)
636 {
637 if (node->local.redefined_extern_inline)
638 e->inline_failed = N_("redefined extern inline functions are not "
639 "considered for inlining");
640 else if (!node->local.inlinable)
641 e->inline_failed = N_("function not inlinable");
642 else
643 e->inline_failed = N_("function not considered for inlining");
644 }
645 if (flag_really_no_inline && !node->local.disregard_inline_limits)
646 node->local.inlinable = 0;
647 /* Inlining characteristics are maintained by the cgraph_mark_inline. */
648 node->global.insns = node->local.self_insns;
649
650 node->analyzed = true;
651 current_function_decl = NULL;
652 }
653
654 /* Analyze the whole compilation unit once it is parsed completely. */
655
656 void
657 cgraph_finalize_compilation_unit (void)
658 {
659 struct cgraph_node *node;
660
661 if (!flag_unit_at_a_time)
662 {
663 cgraph_assemble_pending_functions ();
664 return;
665 }
666
667 cgraph_varpool_assemble_pending_decls ();
668 if (!quiet_flag)
669 fprintf (stderr, "\nAnalyzing compilation unit\n");
670
671 timevar_push (TV_CGRAPH);
672 if (cgraph_dump_file)
673 {
674 fprintf (cgraph_dump_file, "Initial entry points:");
675 for (node = cgraph_nodes; node; node = node->next)
676 if (node->needed && DECL_SAVED_TREE (node->decl))
677 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
678 fprintf (cgraph_dump_file, "\n");
679 }
680
681 /* Propagate reachability flag and lower representation of all reachable
682 functions. In the future, lowering will introduce new functions and
683 new entry points on the way (by template instantiation and virtual
684 method table generation for instance). */
685 while (cgraph_nodes_queue)
686 {
687 struct cgraph_edge *edge;
688 tree decl = cgraph_nodes_queue->decl;
689
690 node = cgraph_nodes_queue;
691 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
692 node->next_needed = NULL;
693
694 /* ??? It is possible to create extern inline function and later using
695 weak alas attribute to kill its body. See
696 gcc.c-torture/compile/20011119-1.c */
697 if (!DECL_SAVED_TREE (decl))
698 continue;
699
700 if (node->analyzed || !node->reachable || !DECL_SAVED_TREE (decl))
701 abort ();
702
703 cgraph_analyze_function (node);
704
705 for (edge = node->callees; edge; edge = edge->next_callee)
706 if (!edge->callee->reachable)
707 cgraph_mark_reachable_node (edge->callee);
708
709 cgraph_varpool_assemble_pending_decls ();
710 }
711
712 /* Collect entry points to the unit. */
713
714 if (cgraph_dump_file)
715 {
716 fprintf (cgraph_dump_file, "Unit entry points:");
717 for (node = cgraph_nodes; node; node = node->next)
718 if (node->needed && DECL_SAVED_TREE (node->decl))
719 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
720 fprintf (cgraph_dump_file, "\n\nInitial ");
721 dump_cgraph (cgraph_dump_file);
722 }
723
724 if (cgraph_dump_file)
725 fprintf (cgraph_dump_file, "\nReclaiming functions:");
726
727 for (node = cgraph_nodes; node; node = node->next)
728 {
729 tree decl = node->decl;
730
731 if (!node->reachable && DECL_SAVED_TREE (decl))
732 {
733 if (cgraph_dump_file)
734 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
735 cgraph_remove_node (node);
736 }
737 else
738 node->next_needed = NULL;
739 }
740 if (cgraph_dump_file)
741 {
742 fprintf (cgraph_dump_file, "\n\nReclaimed ");
743 dump_cgraph (cgraph_dump_file);
744 }
745 ggc_collect ();
746 timevar_pop (TV_CGRAPH);
747 }
748 /* Figure out what functions we want to assemble. */
749
750 static void
751 cgraph_mark_functions_to_output (void)
752 {
753 struct cgraph_node *node;
754
755 for (node = cgraph_nodes; node; node = node->next)
756 {
757 tree decl = node->decl;
758 struct cgraph_edge *e;
759 if (node->output)
760 abort ();
761
762 for (e = node->callers; e; e = e->next_caller)
763 if (e->inline_failed)
764 break;
765
766 /* We need to output all local functions that are used and not
767 always inlined, as well as those that are reachable from
768 outside the current compilation unit. */
769 if (DECL_SAVED_TREE (decl)
770 && !node->global.inlined_to
771 && (node->needed
772 || (e && node->reachable))
773 && !TREE_ASM_WRITTEN (decl)
774 && !DECL_EXTERNAL (decl))
775 node->output = 1;
776 /* We should've reclaimed all functions that are not needed. */
777 else if (!node->global.inlined_to && DECL_SAVED_TREE (decl)
778 && !DECL_EXTERNAL (decl))
779 {
780 dump_cgraph_node (stderr, node);
781 abort ();
782 }
783 }
784 }
785
786 /* Expand function specified by NODE. */
787
788 static void
789 cgraph_expand_function (struct cgraph_node *node)
790 {
791 tree decl = node->decl;
792
793 /* We ought to not compile any inline clones. */
794 if (node->global.inlined_to)
795 abort ();
796
797 if (flag_unit_at_a_time)
798 announce_function (decl);
799
800 /* Generate RTL for the body of DECL. */
801 lang_hooks.callgraph.expand_function (decl);
802
803 /* Make sure that BE didn't give up on compiling. */
804 /* ??? Can happen with nested function of extern inline. */
805 if (!TREE_ASM_WRITTEN (node->decl))
806 abort ();
807
808 current_function_decl = NULL;
809 if (DECL_SAVED_TREE (node->decl)
810 && !cgraph_preserve_function_body_p (node->decl))
811 {
812 DECL_SAVED_TREE (node->decl) = NULL;
813 DECL_STRUCT_FUNCTION (node->decl) = NULL;
814 DECL_INITIAL (node->decl) = error_mark_node;
815 }
816 }
817
818 /* Fill array order with all nodes with output flag set in the reverse
819 topological order. */
820
821 static int
822 cgraph_postorder (struct cgraph_node **order)
823 {
824 struct cgraph_node *node, *node2;
825 int stack_size = 0;
826 int order_pos = 0;
827 struct cgraph_edge *edge, last;
828
829 struct cgraph_node **stack =
830 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
831
832 /* We have to deal with cycles nicely, so use a depth first traversal
833 output algorithm. Ignore the fact that some functions won't need
834 to be output and put them into order as well, so we get dependencies
835 right through intline functions. */
836 for (node = cgraph_nodes; node; node = node->next)
837 node->aux = NULL;
838 for (node = cgraph_nodes; node; node = node->next)
839 if (!node->aux)
840 {
841 node2 = node;
842 if (!node->callers)
843 node->aux = &last;
844 else
845 node->aux = node->callers;
846 while (node2)
847 {
848 while (node2->aux != &last)
849 {
850 edge = node2->aux;
851 if (edge->next_caller)
852 node2->aux = edge->next_caller;
853 else
854 node2->aux = &last;
855 if (!edge->caller->aux)
856 {
857 if (!edge->caller->callers)
858 edge->caller->aux = &last;
859 else
860 edge->caller->aux = edge->caller->callers;
861 stack[stack_size++] = node2;
862 node2 = edge->caller;
863 break;
864 }
865 }
866 if (node2->aux == &last)
867 {
868 order[order_pos++] = node2;
869 if (stack_size)
870 node2 = stack[--stack_size];
871 else
872 node2 = NULL;
873 }
874 }
875 }
876 free (stack);
877 return order_pos;
878 }
879
880 /* Perform reachability analysis and reclaim all unreachable nodes.
881 This function also remove unneeded bodies of extern inline functions
882 and thus needs to be done only after inlining decisions has been made. */
883 static bool
884 cgraph_remove_unreachable_nodes (void)
885 {
886 struct cgraph_node *first = (void *) 1;
887 struct cgraph_node *node;
888 bool changed = false;
889 int insns = 0;
890
891 #ifdef ENABLE_CHECKING
892 verify_cgraph ();
893 #endif
894 if (cgraph_dump_file)
895 fprintf (cgraph_dump_file, "\nReclaiming functions:");
896 #ifdef ENABLE_CHECKING
897 for (node = cgraph_nodes; node; node = node->next)
898 if (node->aux)
899 abort ();
900 #endif
901 for (node = cgraph_nodes; node; node = node->next)
902 if (node->needed && !node->global.inlined_to
903 && (!DECL_EXTERNAL (node->decl) || !node->analyzed))
904 {
905 node->aux = first;
906 first = node;
907 }
908 else if (node->aux)
909 abort ();
910
911 /* Perform reachability analysis. As a special case do not consider
912 extern inline functions not inlined as live because we won't output
913 them at all. */
914 while (first != (void *) 1)
915 {
916 struct cgraph_edge *e;
917 node = first;
918 first = first->aux;
919
920 for (e = node->callees; e; e = e->next_callee)
921 if (!e->callee->aux
922 && node->analyzed
923 && (!e->inline_failed || !e->callee->analyzed
924 || !DECL_EXTERNAL (e->callee->decl)))
925 {
926 e->callee->aux = first;
927 first = e->callee;
928 }
929 }
930
931 /* Remove unreachable nodes. Extern inline functions need special care;
932 Unreachable extern inline functions shall be removed.
933 Reachable extern inline functions we never inlined shall get their bodies
934 eliminated.
935 Reachable extern inline functions we sometimes inlined will be turned into
936 unanalyzed nodes so they look like for true extern functions to the rest
937 of code. Body of such functions is released via remove_node once the
938 inline clones are eliminated. */
939 for (node = cgraph_nodes; node; node = node->next)
940 {
941 if (!node->aux)
942 {
943 int local_insns;
944 tree decl = node->decl;
945
946 node->global.inlined_to = NULL;
947 if (DECL_STRUCT_FUNCTION (decl))
948 local_insns = node->local.self_insns;
949 else
950 local_insns = 0;
951 if (cgraph_dump_file)
952 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
953 if (!node->analyzed || !DECL_EXTERNAL (node->decl))
954 cgraph_remove_node (node);
955 else
956 {
957 struct cgraph_edge *e;
958
959 for (e = node->callers; e; e = e->next_caller)
960 if (e->caller->aux)
961 break;
962 if (e || node->needed)
963 {
964 struct cgraph_node *clone;
965
966 for (clone = node->next_clone; clone;
967 clone = clone->next_clone)
968 if (clone->aux)
969 break;
970 if (!clone)
971 {
972 DECL_SAVED_TREE (node->decl) = NULL;
973 DECL_STRUCT_FUNCTION (node->decl) = NULL;
974 DECL_INITIAL (node->decl) = error_mark_node;
975 }
976 while (node->callees)
977 cgraph_remove_edge (node->callees);
978 node->analyzed = false;
979 }
980 else
981 cgraph_remove_node (node);
982 }
983 if (!DECL_SAVED_TREE (decl))
984 insns += local_insns;
985 changed = true;
986 }
987 }
988 for (node = cgraph_nodes; node; node = node->next)
989 node->aux = NULL;
990 if (cgraph_dump_file)
991 fprintf (cgraph_dump_file, "\nReclaimed %i insns", insns);
992 return changed;
993 }
994
995 /* Estimate size of the function after inlining WHAT into TO. */
996
997 static int
998 cgraph_estimate_size_after_inlining (int times, struct cgraph_node *to,
999 struct cgraph_node *what)
1000 {
1001 return (what->global.insns - INSNS_PER_CALL) * times + to->global.insns;
1002 }
1003
1004 /* Estimate the growth caused by inlining NODE into all callees. */
1005
1006 static int
1007 cgraph_estimate_growth (struct cgraph_node *node)
1008 {
1009 int growth = 0;
1010 struct cgraph_edge *e;
1011
1012 for (e = node->callers; e; e = e->next_caller)
1013 if (e->inline_failed)
1014 growth += (cgraph_estimate_size_after_inlining (1, e->caller, node)
1015 - e->caller->global.insns);
1016
1017 /* ??? Wrong for self recursive functions or cases where we decide to not
1018 inline for different reasons, but it is not big deal as in that case
1019 we will keep the body around, but we will also avoid some inlining. */
1020 if (!node->needed && !DECL_EXTERNAL (node->decl))
1021 growth -= node->global.insns;
1022
1023 return growth;
1024 }
1025
1026 /* E is expected to be an edge being inlined. Clone destination node of
1027 the edge and redirect it to the new clone.
1028 DUPLICATE is used for bookkeeping on whether we are actually creating new
1029 clones or re-using node originally representing out-of-line function call.
1030 */
1031 void
1032 cgraph_clone_inlined_nodes (struct cgraph_edge *e, bool duplicate)
1033 {
1034 struct cgraph_node *n;
1035
1036 /* We may eliminate the need for out-of-line copy to be output. In that
1037 case just go ahead and re-use it. */
1038 if (!e->callee->callers->next_caller
1039 && (!e->callee->needed || DECL_EXTERNAL (e->callee->decl))
1040 && duplicate
1041 && flag_unit_at_a_time)
1042 {
1043 if (e->callee->global.inlined_to)
1044 abort ();
1045 if (!DECL_EXTERNAL (e->callee->decl))
1046 overall_insns -= e->callee->global.insns, nfunctions_inlined++;
1047 duplicate = 0;
1048 }
1049 else if (duplicate)
1050 {
1051 n = cgraph_clone_node (e->callee);
1052 cgraph_redirect_edge_callee (e, n);
1053 }
1054
1055 if (e->caller->global.inlined_to)
1056 e->callee->global.inlined_to = e->caller->global.inlined_to;
1057 else
1058 e->callee->global.inlined_to = e->caller;
1059
1060 /* Recursively clone all bodies. */
1061 for (e = e->callee->callees; e; e = e->next_callee)
1062 if (!e->inline_failed)
1063 cgraph_clone_inlined_nodes (e, duplicate);
1064 }
1065
1066 /* Mark edge E as inlined and update callgraph accordingly. */
1067
1068 void
1069 cgraph_mark_inline_edge (struct cgraph_edge *e)
1070 {
1071 int old_insns = 0, new_insns = 0;
1072 struct cgraph_node *to = NULL, *what;
1073
1074 if (!e->inline_failed)
1075 abort ();
1076 e->inline_failed = NULL;
1077
1078 if (!e->callee->global.inlined && flag_unit_at_a_time)
1079 DECL_POSSIBLY_INLINED (e->callee->decl) = true;
1080 e->callee->global.inlined = true;
1081
1082 cgraph_clone_inlined_nodes (e, true);
1083
1084 what = e->callee;
1085
1086 /* Now update size of caller and all functions caller is inlined into. */
1087 for (;e && !e->inline_failed; e = e->caller->callers)
1088 {
1089 old_insns = e->caller->global.insns;
1090 new_insns = cgraph_estimate_size_after_inlining (1, e->caller,
1091 what);
1092 if (new_insns < 0)
1093 abort ();
1094 to = e->caller;
1095 to->global.insns = new_insns;
1096 }
1097 if (what->global.inlined_to != to)
1098 abort ();
1099 overall_insns += new_insns - old_insns;
1100 ncalls_inlined++;
1101 }
1102
1103 /* Mark all calls of EDGE->CALLEE inlined into EDGE->CALLER.
1104 Return following unredirected edge in the list of callers
1105 of EDGE->CALLEE */
1106
1107 static struct cgraph_edge *
1108 cgraph_mark_inline (struct cgraph_edge *edge)
1109 {
1110 struct cgraph_node *to = edge->caller;
1111 struct cgraph_node *what = edge->callee;
1112 struct cgraph_edge *e, *next;
1113 int times = 0;
1114
1115 /* Look for all calls, mark them inline and clone recursively
1116 all inlined functions. */
1117 for (e = what->callers; e; e = next)
1118 {
1119 next = e->next_caller;
1120 if (e->caller == to && e->inline_failed)
1121 {
1122 cgraph_mark_inline_edge (e);
1123 if (e == edge)
1124 edge = next;
1125 times ++;
1126 }
1127 }
1128 if (!times)
1129 abort ();
1130 return edge;
1131 }
1132
1133 /* Return false when inlining WHAT into TO is not good idea
1134 as it would cause too large growth of function bodies. */
1135
1136 static bool
1137 cgraph_check_inline_limits (struct cgraph_node *to, struct cgraph_node *what,
1138 const char **reason)
1139 {
1140 int times = 0;
1141 struct cgraph_edge *e;
1142 int newsize;
1143 int limit;
1144
1145 if (to->global.inlined_to)
1146 to = to->global.inlined_to;
1147
1148 for (e = to->callees; e; e = e->next_callee)
1149 if (e->callee == what)
1150 times++;
1151
1152 /* When inlining large function body called once into small function,
1153 take the inlined function as base for limiting the growth. */
1154 if (to->local.self_insns > what->local.self_insns)
1155 limit = to->local.self_insns;
1156 else
1157 limit = what->local.self_insns;
1158
1159 limit += limit * PARAM_VALUE (PARAM_LARGE_FUNCTION_GROWTH) / 100;
1160
1161 newsize = cgraph_estimate_size_after_inlining (times, to, what);
1162 if (newsize > PARAM_VALUE (PARAM_LARGE_FUNCTION_INSNS)
1163 && newsize > limit)
1164 {
1165 if (reason)
1166 *reason = N_("--param large-function-growth limit reached");
1167 return false;
1168 }
1169 return true;
1170 }
1171
1172 /* Return true when function N is small enough to be inlined. */
1173
1174 static bool
1175 cgraph_default_inline_p (struct cgraph_node *n)
1176 {
1177 if (!DECL_INLINE (n->decl) || !DECL_SAVED_TREE (n->decl))
1178 return false;
1179 if (DECL_DECLARED_INLINE_P (n->decl))
1180 return n->global.insns < MAX_INLINE_INSNS_SINGLE;
1181 else
1182 return n->global.insns < MAX_INLINE_INSNS_AUTO;
1183 }
1184
1185 /* Return true when inlining WHAT would create recursive inlining.
1186 We call recursive inlining all cases where same function appears more than
1187 once in the single recursion nest path in the inline graph. */
1188
1189 static bool
1190 cgraph_recursive_inlining_p (struct cgraph_node *to,
1191 struct cgraph_node *what,
1192 const char **reason)
1193 {
1194 bool recursive;
1195 if (to->global.inlined_to)
1196 recursive = what->decl == to->global.inlined_to->decl;
1197 else
1198 recursive = what->decl == to->decl;
1199 /* Marking recursive function inline has sane semantic and thus we should
1200 not warn on it. */
1201 if (recursive && reason)
1202 *reason = (what->local.disregard_inline_limits
1203 ? N_("recursive inlining") : "");
1204 return recursive;
1205 }
1206
1207 /* Recompute heap nodes for each of callees. */
1208 static void
1209 update_callee_keys (fibheap_t heap, struct fibnode **heap_node,
1210 struct cgraph_node *node)
1211 {
1212 struct cgraph_edge *e;
1213
1214 for (e = node->callees; e; e = e->next_callee)
1215 if (e->inline_failed && heap_node[e->callee->uid])
1216 fibheap_replace_key (heap, heap_node[e->callee->uid],
1217 cgraph_estimate_growth (e->callee));
1218 else if (!e->inline_failed)
1219 update_callee_keys (heap, heap_node, e->callee);
1220 }
1221
1222 /* Enqueue all recursive calls from NODE into queue linked via aux pointers
1223 in between FIRST and LAST. WHERE is used for bookkeeping while looking
1224 int calls inlined within NODE. */
1225 static void
1226 lookup_recursive_calls (struct cgraph_node *node, struct cgraph_node *where,
1227 struct cgraph_edge **first, struct cgraph_edge **last)
1228 {
1229 struct cgraph_edge *e;
1230 for (e = where->callees; e; e = e->next_callee)
1231 if (e->callee == node)
1232 {
1233 if (!*first)
1234 *first = e;
1235 else
1236 (*last)->aux = e;
1237 *last = e;
1238 }
1239 for (e = where->callees; e; e = e->next_callee)
1240 if (!e->inline_failed)
1241 lookup_recursive_calls (node, e->callee, first, last);
1242 }
1243
1244 /* Decide on recursive inlining: in the case function has recursive calls,
1245 inline until body size reaches given argument. */
1246 static void
1247 cgraph_decide_recursive_inlining (struct cgraph_node *node)
1248 {
1249 int limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE_AUTO);
1250 int max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH_AUTO);
1251 struct cgraph_edge *first_call = NULL, *last_call = NULL;
1252 struct cgraph_edge *last_in_current_depth;
1253 struct cgraph_edge *e;
1254 struct cgraph_node *master_clone;
1255 int depth = 0;
1256 int n = 0;
1257
1258 if (DECL_DECLARED_INLINE_P (node->decl))
1259 {
1260 limit = PARAM_VALUE (PARAM_MAX_INLINE_INSNS_RECURSIVE);
1261 max_depth = PARAM_VALUE (PARAM_MAX_INLINE_RECURSIVE_DEPTH);
1262 }
1263
1264 /* Make sure that function is small enough to be considered for inlining. */
1265 if (!max_depth
1266 || cgraph_estimate_size_after_inlining (1, node, node) >= limit)
1267 return;
1268 lookup_recursive_calls (node, node, &first_call, &last_call);
1269 if (!first_call)
1270 return;
1271
1272 if (cgraph_dump_file)
1273 fprintf (cgraph_dump_file,
1274 "\nPerforming recursive inlining on %s\n",
1275 cgraph_node_name (node));
1276
1277 /* We need original clone to copy around. */
1278 master_clone = cgraph_clone_node (node);
1279 master_clone->needed = true;
1280 for (e = master_clone->callees; e; e = e->next_callee)
1281 if (!e->inline_failed)
1282 cgraph_clone_inlined_nodes (e, true);
1283
1284 /* Do the inlining and update list of recursive call during process. */
1285 last_in_current_depth = last_call;
1286 while (first_call
1287 && cgraph_estimate_size_after_inlining (1, node, master_clone) <= limit)
1288 {
1289 struct cgraph_edge *curr = first_call;
1290
1291 first_call = first_call->aux;
1292 curr->aux = NULL;
1293
1294 cgraph_redirect_edge_callee (curr, master_clone);
1295 cgraph_mark_inline_edge (curr);
1296 lookup_recursive_calls (node, curr->callee, &first_call, &last_call);
1297
1298 if (last_in_current_depth
1299 && ++depth >= max_depth)
1300 break;
1301 n++;
1302 }
1303
1304 /* Cleanup queue pointers. */
1305 while (first_call)
1306 {
1307 struct cgraph_edge *next = first_call->aux;
1308 first_call->aux = NULL;
1309 first_call = next;
1310 }
1311 if (cgraph_dump_file)
1312 fprintf (cgraph_dump_file,
1313 "\n Inlined %i times, body grown from %i to %i insns\n", n,
1314 master_clone->global.insns, node->global.insns);
1315
1316 /* Remove master clone we used for inlining. We rely that clones inlined
1317 into master clone gets queued just before master clone so we don't
1318 need recursion. */
1319 for (node = cgraph_nodes; node != master_clone;
1320 node = node->next)
1321 if (node->global.inlined_to == master_clone)
1322 cgraph_remove_node (node);
1323 cgraph_remove_node (master_clone);
1324 }
1325
1326 /* Set inline_failed for all callers of given function to REASON. */
1327
1328 static void
1329 cgraph_set_inline_failed (struct cgraph_node *node, const char *reason)
1330 {
1331 struct cgraph_edge *e;
1332
1333 if (cgraph_dump_file)
1334 fprintf (cgraph_dump_file, "Inlining failed: %s\n", reason);
1335 for (e = node->callers; e; e = e->next_caller)
1336 if (e->inline_failed)
1337 e->inline_failed = reason;
1338 }
1339
1340 /* We use greedy algorithm for inlining of small functions:
1341 All inline candidates are put into prioritized heap based on estimated
1342 growth of the overall number of instructions and then update the estimates.
1343
1344 INLINED and INLINED_CALEES are just pointers to arrays large enough
1345 to be passed to cgraph_inlined_into and cgraph_inlined_callees. */
1346
1347 static void
1348 cgraph_decide_inlining_of_small_functions (void)
1349 {
1350 struct cgraph_node *node;
1351 fibheap_t heap = fibheap_new ();
1352 struct fibnode **heap_node =
1353 xcalloc (cgraph_max_uid, sizeof (struct fibnode *));
1354 int max_insns = ((HOST_WIDEST_INT) initial_insns
1355 * (100 + PARAM_VALUE (PARAM_INLINE_UNIT_GROWTH)) / 100);
1356
1357 /* Put all inline candidates into the heap. */
1358
1359 for (node = cgraph_nodes; node; node = node->next)
1360 {
1361 if (!node->local.inlinable || !node->callers
1362 || node->local.disregard_inline_limits)
1363 continue;
1364
1365 if (!cgraph_default_inline_p (node))
1366 {
1367 cgraph_set_inline_failed (node,
1368 N_("--param max-inline-insns-single limit reached"));
1369 continue;
1370 }
1371 heap_node[node->uid] =
1372 fibheap_insert (heap, cgraph_estimate_growth (node), node);
1373 }
1374
1375 if (cgraph_dump_file)
1376 fprintf (cgraph_dump_file, "\nDeciding on smaller functions:\n");
1377 while (overall_insns <= max_insns && (node = fibheap_extract_min (heap)))
1378 {
1379 struct cgraph_edge *e, *next;
1380 int old_insns = overall_insns;
1381
1382 heap_node[node->uid] = NULL;
1383 if (cgraph_dump_file)
1384 fprintf (cgraph_dump_file,
1385 "\nConsidering %s with %i insns\n"
1386 " Estimated growth is %+i insns.\n",
1387 cgraph_node_name (node), node->global.insns,
1388 cgraph_estimate_growth (node));
1389 if (!cgraph_default_inline_p (node))
1390 {
1391 cgraph_set_inline_failed (node,
1392 N_("--param max-inline-insns-single limit reached after inlining into the callee"));
1393 continue;
1394 }
1395 for (e = node->callers; e; e = next)
1396 {
1397 next = e->next_caller;
1398 if (e->inline_failed)
1399 {
1400 struct cgraph_node *where;
1401
1402 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1403 &e->inline_failed)
1404 || !cgraph_check_inline_limits (e->caller, e->callee,
1405 &e->inline_failed))
1406 {
1407 if (cgraph_dump_file)
1408 fprintf (cgraph_dump_file, " Not inlining into %s:%s.\n",
1409 cgraph_node_name (e->caller), e->inline_failed);
1410 continue;
1411 }
1412 next = cgraph_mark_inline (e);
1413 where = e->caller;
1414 if (where->global.inlined_to)
1415 where = where->global.inlined_to;
1416
1417 if (heap_node[where->uid])
1418 fibheap_replace_key (heap, heap_node[where->uid],
1419 cgraph_estimate_growth (where));
1420
1421 if (cgraph_dump_file)
1422 fprintf (cgraph_dump_file,
1423 " Inlined into %s which now has %i insns.\n",
1424 cgraph_node_name (e->caller),
1425 e->caller->global.insns);
1426 }
1427 }
1428
1429 cgraph_decide_recursive_inlining (node);
1430
1431 /* Similarly all functions called by the function we just inlined
1432 are now called more times; update keys. */
1433 update_callee_keys (heap, heap_node, node);
1434
1435 if (cgraph_dump_file)
1436 fprintf (cgraph_dump_file,
1437 " Inlined for a net change of %+i insns.\n",
1438 overall_insns - old_insns);
1439 }
1440 while ((node = fibheap_extract_min (heap)) != NULL)
1441 if (!node->local.disregard_inline_limits)
1442 cgraph_set_inline_failed (node, N_("--param inline-unit-growth limit reached"));
1443 fibheap_delete (heap);
1444 free (heap_node);
1445 }
1446
1447 /* Decide on the inlining. We do so in the topological order to avoid
1448 expenses on updating data structures. */
1449
1450 static void
1451 cgraph_decide_inlining (void)
1452 {
1453 struct cgraph_node *node;
1454 int nnodes;
1455 struct cgraph_node **order =
1456 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1457 int old_insns = 0;
1458 int i;
1459
1460 for (node = cgraph_nodes; node; node = node->next)
1461 initial_insns += node->local.self_insns;
1462 overall_insns = initial_insns;
1463
1464 nnodes = cgraph_postorder (order);
1465
1466 if (cgraph_dump_file)
1467 fprintf (cgraph_dump_file,
1468 "\nDeciding on inlining. Starting with %i insns.\n",
1469 initial_insns);
1470
1471 for (node = cgraph_nodes; node; node = node->next)
1472 node->aux = 0;
1473
1474 if (cgraph_dump_file)
1475 fprintf (cgraph_dump_file, "\nInlining always_inline functions:\n");
1476
1477 /* In the first pass mark all always_inline edges. Do this with a priority
1478 so none of our later choices will make this impossible. */
1479 for (i = nnodes - 1; i >= 0; i--)
1480 {
1481 struct cgraph_edge *e, *next;
1482
1483 node = order[i];
1484
1485 if (!node->local.disregard_inline_limits)
1486 continue;
1487 if (cgraph_dump_file)
1488 fprintf (cgraph_dump_file,
1489 "\nConsidering %s %i insns (always inline)\n",
1490 cgraph_node_name (node), node->global.insns);
1491 old_insns = overall_insns;
1492 for (e = node->callers; e; e = next)
1493 {
1494 next = e->next_caller;
1495 if (!e->inline_failed)
1496 continue;
1497 if (cgraph_recursive_inlining_p (e->caller, e->callee,
1498 &e->inline_failed))
1499 continue;
1500 cgraph_mark_inline_edge (e);
1501 if (cgraph_dump_file)
1502 fprintf (cgraph_dump_file,
1503 " Inlined into %s which now has %i insns.\n",
1504 cgraph_node_name (e->caller),
1505 e->caller->global.insns);
1506 }
1507 if (cgraph_dump_file)
1508 fprintf (cgraph_dump_file,
1509 " Inlined for a net change of %+i insns.\n",
1510 overall_insns - old_insns);
1511 }
1512
1513 if (!flag_really_no_inline)
1514 {
1515 cgraph_decide_inlining_of_small_functions ();
1516
1517 if (cgraph_dump_file)
1518 fprintf (cgraph_dump_file, "\nDeciding on functions called once:\n");
1519
1520 /* And finally decide what functions are called once. */
1521
1522 for (i = nnodes - 1; i >= 0; i--)
1523 {
1524 node = order[i];
1525
1526 if (node->callers && !node->callers->next_caller && !node->needed
1527 && node->local.inlinable && node->callers->inline_failed
1528 && !DECL_EXTERNAL (node->decl) && !DECL_COMDAT (node->decl))
1529 {
1530 bool ok = true;
1531 struct cgraph_node *node1;
1532
1533 /* Verify that we won't duplicate the caller. */
1534 for (node1 = node->callers->caller;
1535 node1->callers && !node1->callers->inline_failed
1536 && ok; node1 = node1->callers->caller)
1537 if (node1->callers->next_caller || node1->needed)
1538 ok = false;
1539 if (ok)
1540 {
1541 if (cgraph_dump_file)
1542 fprintf (cgraph_dump_file,
1543 "\nConsidering %s %i insns.\n"
1544 " Called once from %s %i insns.\n",
1545 cgraph_node_name (node), node->global.insns,
1546 cgraph_node_name (node->callers->caller),
1547 node->callers->caller->global.insns);
1548
1549 old_insns = overall_insns;
1550
1551 if (cgraph_check_inline_limits (node->callers->caller, node,
1552 NULL))
1553 {
1554 cgraph_mark_inline (node->callers);
1555 if (cgraph_dump_file)
1556 fprintf (cgraph_dump_file,
1557 " Inlined into %s which now has %i insns"
1558 " for a net change of %+i insns.\n",
1559 cgraph_node_name (node->callers->caller),
1560 node->callers->caller->global.insns,
1561 overall_insns - old_insns);
1562 }
1563 else
1564 {
1565 if (cgraph_dump_file)
1566 fprintf (cgraph_dump_file,
1567 " Inline limit reached, not inlined.\n");
1568 }
1569 }
1570 }
1571 }
1572 }
1573
1574 /* We will never output extern functions we didn't inline.
1575 ??? Perhaps we can prevent accounting of growth of external
1576 inline functions. */
1577 cgraph_remove_unreachable_nodes ();
1578
1579 if (cgraph_dump_file)
1580 fprintf (cgraph_dump_file,
1581 "\nInlined %i calls, eliminated %i functions, "
1582 "%i insns turned to %i insns.\n\n",
1583 ncalls_inlined, nfunctions_inlined, initial_insns,
1584 overall_insns);
1585 free (order);
1586 }
1587
1588 /* Decide on the inlining. We do so in the topological order to avoid
1589 expenses on updating data structures. */
1590
1591 static void
1592 cgraph_decide_inlining_incrementally (struct cgraph_node *node)
1593 {
1594 struct cgraph_edge *e;
1595
1596 /* First of all look for always inline functions. */
1597 for (e = node->callees; e; e = e->next_callee)
1598 if (e->callee->local.disregard_inline_limits
1599 && e->inline_failed
1600 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1601 /* ??? It is possible that renaming variable removed the function body
1602 in duplicate_decls. See gcc.c-torture/compile/20011119-2.c */
1603 && DECL_SAVED_TREE (e->callee->decl))
1604 cgraph_mark_inline (e);
1605
1606 /* Now do the automatic inlining. */
1607 if (!flag_really_no_inline)
1608 for (e = node->callees; e; e = e->next_callee)
1609 if (e->callee->local.inlinable
1610 && e->inline_failed
1611 && !e->callee->local.disregard_inline_limits
1612 && !cgraph_recursive_inlining_p (node, e->callee, &e->inline_failed)
1613 && cgraph_check_inline_limits (node, e->callee, &e->inline_failed)
1614 && DECL_SAVED_TREE (e->callee->decl))
1615 {
1616 if (cgraph_default_inline_p (e->callee))
1617 cgraph_mark_inline (e);
1618 else
1619 e->inline_failed
1620 = N_("--param max-inline-insns-single limit reached");
1621 }
1622 }
1623
1624
1625 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1626
1627 bool
1628 cgraph_inline_p (struct cgraph_edge *e, const char **reason)
1629 {
1630 *reason = e->inline_failed;
1631 return !e->inline_failed;
1632 }
1633
1634 /* Expand all functions that must be output.
1635
1636 Attempt to topologically sort the nodes so function is output when
1637 all called functions are already assembled to allow data to be
1638 propagated across the callgraph. Use a stack to get smaller distance
1639 between a function and its callees (later we may choose to use a more
1640 sophisticated algorithm for function reordering; we will likely want
1641 to use subsections to make the output functions appear in top-down
1642 order). */
1643
1644 static void
1645 cgraph_expand_all_functions (void)
1646 {
1647 struct cgraph_node *node;
1648 struct cgraph_node **order =
1649 xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
1650 int order_pos = 0, new_order_pos = 0;
1651 int i;
1652
1653 cgraph_mark_functions_to_output ();
1654
1655 order_pos = cgraph_postorder (order);
1656 if (order_pos != cgraph_n_nodes)
1657 abort ();
1658
1659 /* Garbage collector may remove inline clones we eliminate during
1660 optimization. So we must be sure to not reference them. */
1661 for (i = 0; i < order_pos; i++)
1662 if (order[i]->output)
1663 order[new_order_pos++] = order[i];
1664
1665 for (i = new_order_pos - 1; i >= 0; i--)
1666 {
1667 node = order[i];
1668 if (node->output)
1669 {
1670 if (!node->reachable)
1671 abort ();
1672 node->output = 0;
1673 cgraph_expand_function (node);
1674 }
1675 }
1676 free (order);
1677 }
1678
1679 /* Mark all local functions.
1680
1681 A local function is one whose calls can occur only in the
1682 current compilation unit and all its calls are explicit,
1683 so we can change its calling convention.
1684 We simply mark all static functions whose address is not taken
1685 as local. */
1686
1687 static void
1688 cgraph_mark_local_functions (void)
1689 {
1690 struct cgraph_node *node;
1691
1692 if (cgraph_dump_file)
1693 fprintf (cgraph_dump_file, "\nMarking local functions:");
1694
1695 /* Figure out functions we want to assemble. */
1696 for (node = cgraph_nodes; node; node = node->next)
1697 {
1698 node->local.local = (!node->needed
1699 && DECL_SAVED_TREE (node->decl)
1700 && !TREE_PUBLIC (node->decl));
1701 if (cgraph_dump_file && node->local.local)
1702 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1703 }
1704 if (cgraph_dump_file)
1705 fprintf (cgraph_dump_file, "\n\n");
1706 }
1707
1708 /* Return true when function body of DECL still needs to be kept around
1709 for later re-use. */
1710 bool
1711 cgraph_preserve_function_body_p (tree decl)
1712 {
1713 struct cgraph_node *node;
1714 /* Keep the body; we're going to dump it. */
1715 if (dump_enabled_p (TDI_all))
1716 return true;
1717 if (!cgraph_global_info_ready)
1718 return (DECL_INLINE (decl) && !flag_really_no_inline);
1719 /* Look if there is any clone around. */
1720 for (node = cgraph_node (decl); node; node = node->next_clone)
1721 if (node->global.inlined_to)
1722 return true;
1723 return false;
1724 }
1725
1726 /* Perform simple optimizations based on callgraph. */
1727
1728 void
1729 cgraph_optimize (void)
1730 {
1731 #ifdef ENABLE_CHECKING
1732 verify_cgraph ();
1733 #endif
1734 if (!flag_unit_at_a_time)
1735 return;
1736 timevar_push (TV_CGRAPHOPT);
1737 if (!quiet_flag)
1738 fprintf (stderr, "Performing intraprocedural optimizations\n");
1739
1740 cgraph_mark_local_functions ();
1741 if (cgraph_dump_file)
1742 {
1743 fprintf (cgraph_dump_file, "Marked ");
1744 dump_cgraph (cgraph_dump_file);
1745 }
1746
1747 if (flag_inline_trees)
1748 cgraph_decide_inlining ();
1749 cgraph_global_info_ready = true;
1750 if (cgraph_dump_file)
1751 {
1752 fprintf (cgraph_dump_file, "Optimized ");
1753 dump_cgraph (cgraph_dump_file);
1754 }
1755 timevar_pop (TV_CGRAPHOPT);
1756
1757 /* Output everything. */
1758 if (!quiet_flag)
1759 fprintf (stderr, "Assembling functions:\n");
1760 #ifdef ENABLE_CHECKING
1761 verify_cgraph ();
1762 #endif
1763 cgraph_expand_all_functions ();
1764 if (cgraph_dump_file)
1765 {
1766 fprintf (cgraph_dump_file, "\nFinal ");
1767 dump_cgraph (cgraph_dump_file);
1768 }
1769 #ifdef ENABLE_CHECKING
1770 verify_cgraph ();
1771 /* Double check that all inline clones are gone and that all
1772 function bodies have been released from memory. */
1773 if (flag_unit_at_a_time
1774 && !dump_enabled_p (TDI_all)
1775 && !(sorrycount || errorcount))
1776 {
1777 struct cgraph_node *node;
1778 bool error_found = false;
1779
1780 for (node = cgraph_nodes; node; node = node->next)
1781 if (node->analyzed
1782 && (node->global.inlined_to
1783 || DECL_SAVED_TREE (node->decl)))
1784 {
1785 error_found = true;
1786 dump_cgraph_node (stderr, node);
1787 }
1788 if (error_found)
1789 internal_error ("Nodes with no released memory found.");
1790 }
1791 #endif
1792 }
1793
1794 /* Generate and emit a static constructor or destructor. WHICH must be
1795 one of 'I' or 'D'. BODY should be a STATEMENT_LIST containing
1796 GENERIC statements. */
1797
1798 void
1799 cgraph_build_static_cdtor (char which, tree body, int priority)
1800 {
1801 static int counter = 0;
1802 char which_buf[16];
1803 tree decl, name, resdecl;
1804
1805 sprintf (which_buf, "%c_%d", which, counter++);
1806 name = get_file_function_name_long (which_buf);
1807
1808 decl = build_decl (FUNCTION_DECL, name,
1809 build_function_type (void_type_node, void_list_node));
1810 current_function_decl = decl;
1811
1812 resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1813 DECL_ARTIFICIAL (resdecl) = 1;
1814 DECL_IGNORED_P (resdecl) = 1;
1815 DECL_RESULT (decl) = resdecl;
1816
1817 allocate_struct_function (decl);
1818
1819 TREE_STATIC (decl) = 1;
1820 TREE_USED (decl) = 1;
1821 DECL_ARTIFICIAL (decl) = 1;
1822 DECL_IGNORED_P (decl) = 1;
1823 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1824 DECL_SAVED_TREE (decl) = body;
1825 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1826 DECL_UNINLINABLE (decl) = 1;
1827
1828 DECL_INITIAL (decl) = make_node (BLOCK);
1829 TREE_USED (DECL_INITIAL (decl)) = 1;
1830
1831 DECL_SOURCE_LOCATION (decl) = input_location;
1832 cfun->function_end_locus = input_location;
1833
1834 if (which == 'I')
1835 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1836 else if (which == 'D')
1837 DECL_STATIC_DESTRUCTOR (decl) = 1;
1838 else
1839 abort ();
1840
1841 gimplify_function_tree (decl);
1842
1843 /* ??? We will get called LATE in the compilation process. */
1844 if (cgraph_global_info_ready)
1845 tree_rest_of_compilation (decl, false);
1846 else
1847 cgraph_finalize_function (decl, 0);
1848
1849 if (targetm.have_ctors_dtors)
1850 {
1851 void (*fn) (rtx, int);
1852
1853 if (which == 'I')
1854 fn = targetm.asm_out.constructor;
1855 else
1856 fn = targetm.asm_out.destructor;
1857 fn (XEXP (DECL_RTL (decl), 0), priority);
1858 }
1859 }