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