* Makefile.in (s-header-vars): New rule.
[official-gcc.git] / gcc / cgraphunit.c
blob07d82f8800f5935f1381a1dc518f32cfd28e8f8b
1 /* Callgraph based interprocedural optimizations.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module implements main driver of compilation process as well as
23 few basic interprocedural optimizers.
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)
28 The front-end is supposed to use following functionality:
30 - cgraph_finalize_function
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.
35 (There is one exception needed for implementing GCC extern inline
36 function.)
38 - varpool_finalize_variable
40 This function has same behavior as the above but is used for static
41 variables.
43 - cgraph_finalize_compilation_unit
45 This function is called once (source level) compilation unit is finalized
46 and it will no longer change.
48 In the the call-graph construction and local function
49 analysis takes place here. Bodies of unreachable functions are released
50 to conserve memory usage.
52 The function can be called multiple times when multiple source level
53 compilation units are combined (such as in C frontend)
55 - cgraph_optimize
57 In this unit-at-a-time compilation the intra procedural analysis takes
58 place here. In particular the static functions whose address is never
59 taken are marked as local. Backend can then use this information to
60 modify calling conventions, do better inlining or similar optimizations.
62 - cgraph_mark_needed_node
63 - varpool_mark_needed_node
65 When function or variable is referenced by some hidden way the call-graph
66 data structure must be updated accordingly by this function.
67 There should be little need to call this function and all the references
68 should be made explicit to cgraph code. At present these functions are
69 used by C++ frontend to explicitly mark the keyed methods.
71 - analyze_expr callback
73 This function is responsible for lowering tree nodes not understood by
74 generic code into understandable ones or alternatively marking
75 callgraph and varpool nodes referenced by the as needed.
77 ??? On the tree-ssa genericizing should take place here and we will avoid
78 need for these hooks (replacing them by genericizing hook)
80 Analyzing of all functions is deferred
81 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
83 In cgraph_finalize_compilation_unit the reachable functions are
84 analyzed. During analysis the call-graph edges from reachable
85 functions are constructed and their destinations are marked as
86 reachable. References to functions and variables are discovered too
87 and variables found to be needed output to the assembly file. Via
88 mark_referenced call in assemble_variable functions referenced by
89 static variables are noticed too.
91 The intra-procedural information is produced and its existence
92 indicated by global_info_ready. Once this flag is set it is impossible
93 to change function from !reachable to reachable and thus
94 assemble_variable no longer call mark_referenced.
96 Finally the call-graph is topologically sorted and all reachable functions
97 that has not been completely inlined or are not external are output.
99 ??? It is possible that reference to function or variable is optimized
100 out. We can not deal with this nicely because topological order is not
101 suitable for it. For tree-ssa we may consider another pass doing
102 optimization and re-discovering reachable functions.
104 ??? Reorganize code so variables are output very last and only if they
105 really has been referenced by produced code, so we catch more cases
106 where reference has been optimized out. */
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "tree.h"
114 #include "rtl.h"
115 #include "tree-flow.h"
116 #include "tree-inline.h"
117 #include "langhooks.h"
118 #include "pointer-set.h"
119 #include "toplev.h"
120 #include "flags.h"
121 #include "ggc.h"
122 #include "debug.h"
123 #include "target.h"
124 #include "cgraph.h"
125 #include "diagnostic.h"
126 #include "timevar.h"
127 #include "params.h"
128 #include "fibheap.h"
129 #include "intl.h"
130 #include "function.h"
131 #include "ipa-prop.h"
132 #include "gimple.h"
133 #include "tree-iterator.h"
134 #include "tree-pass.h"
135 #include "tree-dump.h"
136 #include "output.h"
137 #include "coverage.h"
138 #include "plugin.h"
140 static void cgraph_expand_all_functions (void);
141 static void cgraph_mark_functions_to_output (void);
142 static void cgraph_expand_function (struct cgraph_node *);
143 static void cgraph_output_pending_asms (void);
144 static void cgraph_analyze_function (struct cgraph_node *);
146 static FILE *cgraph_dump_file;
148 /* A vector of FUNCTION_DECLs declared as static constructors. */
149 static GTY (()) VEC(tree, gc) *static_ctors;
150 /* A vector of FUNCTION_DECLs declared as static destructors. */
151 static GTY (()) VEC(tree, gc) *static_dtors;
153 /* When target does not have ctors and dtors, we call all constructor
154 and destructor by special initialization/destruction function
155 recognized by collect2.
157 When we are going to build this function, collect all constructors and
158 destructors and turn them into normal functions. */
160 static void
161 record_cdtor_fn (tree fndecl)
163 struct cgraph_node *node;
164 if (targetm.have_ctors_dtors
165 || (!DECL_STATIC_CONSTRUCTOR (fndecl)
166 && !DECL_STATIC_DESTRUCTOR (fndecl)))
167 return;
169 if (DECL_STATIC_CONSTRUCTOR (fndecl))
171 VEC_safe_push (tree, gc, static_ctors, fndecl);
172 DECL_STATIC_CONSTRUCTOR (fndecl) = 0;
174 if (DECL_STATIC_DESTRUCTOR (fndecl))
176 VEC_safe_push (tree, gc, static_dtors, fndecl);
177 DECL_STATIC_DESTRUCTOR (fndecl) = 0;
179 node = cgraph_node (fndecl);
180 node->local.disregard_inline_limits = 1;
181 cgraph_mark_reachable_node (node);
184 /* Define global constructors/destructor functions for the CDTORS, of
185 which they are LEN. The CDTORS are sorted by initialization
186 priority. If CTOR_P is true, these are constructors; otherwise,
187 they are destructors. */
189 static void
190 build_cdtor (bool ctor_p, tree *cdtors, size_t len)
192 size_t i;
194 i = 0;
195 while (i < len)
197 tree body;
198 tree fn;
199 priority_type priority;
201 priority = 0;
202 body = NULL_TREE;
203 /* Find the next batch of constructors/destructors with the same
204 initialization priority. */
207 priority_type p;
208 fn = cdtors[i];
209 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
210 if (!body)
211 priority = p;
212 else if (p != priority)
213 break;
214 append_to_statement_list (build_function_call_expr (UNKNOWN_LOCATION,
215 fn, 0),
216 &body);
217 ++i;
219 while (i < len);
220 gcc_assert (body != NULL_TREE);
221 /* Generate a function to call all the function of like
222 priority. */
223 cgraph_build_static_cdtor (ctor_p ? 'I' : 'D', body, priority);
227 /* Comparison function for qsort. P1 and P2 are actually of type
228 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
229 used to determine the sort order. */
231 static int
232 compare_ctor (const void *p1, const void *p2)
234 tree f1;
235 tree f2;
236 int priority1;
237 int priority2;
239 f1 = *(const tree *)p1;
240 f2 = *(const tree *)p2;
241 priority1 = DECL_INIT_PRIORITY (f1);
242 priority2 = DECL_INIT_PRIORITY (f2);
244 if (priority1 < priority2)
245 return -1;
246 else if (priority1 > priority2)
247 return 1;
248 else
249 /* Ensure a stable sort. */
250 return (const tree *)p1 - (const tree *)p2;
253 /* Comparison function for qsort. P1 and P2 are actually of type
254 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
255 used to determine the sort order. */
257 static int
258 compare_dtor (const void *p1, const void *p2)
260 tree f1;
261 tree f2;
262 int priority1;
263 int priority2;
265 f1 = *(const tree *)p1;
266 f2 = *(const tree *)p2;
267 priority1 = DECL_FINI_PRIORITY (f1);
268 priority2 = DECL_FINI_PRIORITY (f2);
270 if (priority1 < priority2)
271 return -1;
272 else if (priority1 > priority2)
273 return 1;
274 else
275 /* Ensure a stable sort. */
276 return (const tree *)p1 - (const tree *)p2;
279 /* Generate functions to call static constructors and destructors
280 for targets that do not support .ctors/.dtors sections. These
281 functions have magic names which are detected by collect2. */
283 static void
284 cgraph_build_cdtor_fns (void)
286 if (!VEC_empty (tree, static_ctors))
288 gcc_assert (!targetm.have_ctors_dtors);
289 qsort (VEC_address (tree, static_ctors),
290 VEC_length (tree, static_ctors),
291 sizeof (tree),
292 compare_ctor);
293 build_cdtor (/*ctor_p=*/true,
294 VEC_address (tree, static_ctors),
295 VEC_length (tree, static_ctors));
296 VEC_truncate (tree, static_ctors, 0);
299 if (!VEC_empty (tree, static_dtors))
301 gcc_assert (!targetm.have_ctors_dtors);
302 qsort (VEC_address (tree, static_dtors),
303 VEC_length (tree, static_dtors),
304 sizeof (tree),
305 compare_dtor);
306 build_cdtor (/*ctor_p=*/false,
307 VEC_address (tree, static_dtors),
308 VEC_length (tree, static_dtors));
309 VEC_truncate (tree, static_dtors, 0);
313 /* Determine if function DECL is needed. That is, visible to something
314 either outside this translation unit, something magic in the system
315 configury. */
317 bool
318 cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
320 /* If the user told us it is used, then it must be so. */
321 if (node->local.externally_visible)
322 return true;
324 /* ??? If the assembler name is set by hand, it is possible to assemble
325 the name later after finalizing the function and the fact is noticed
326 in assemble_name then. This is arguably a bug. */
327 if (DECL_ASSEMBLER_NAME_SET_P (decl)
328 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
329 return true;
331 /* With -fkeep-inline-functions we are keeping all inline functions except
332 for extern inline ones. */
333 if (flag_keep_inline_functions
334 && DECL_DECLARED_INLINE_P (decl)
335 && !DECL_EXTERNAL (decl)
336 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)))
337 return true;
339 /* If we decided it was needed before, but at the time we didn't have
340 the body of the function available, then it's still needed. We have
341 to go back and re-check its dependencies now. */
342 if (node->needed)
343 return true;
345 /* Externally visible functions must be output. The exception is
346 COMDAT functions that must be output only when they are needed.
348 When not optimizing, also output the static functions. (see
349 PR24561), but don't do so for always_inline functions, functions
350 declared inline and nested functions. These was optimized out
351 in the original implementation and it is unclear whether we want
352 to change the behavior here. */
353 if (((TREE_PUBLIC (decl)
354 || (!optimize && !node->local.disregard_inline_limits
355 && !DECL_DECLARED_INLINE_P (decl)
356 && !node->origin))
357 && !flag_whole_program
358 && !flag_lto
359 && !flag_whopr)
360 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
361 return true;
363 /* Constructors and destructors are reachable from the runtime by
364 some mechanism. */
365 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
366 return true;
368 return false;
371 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
372 functions into callgraph in a way so they look like ordinary reachable
373 functions inserted into callgraph already at construction time. */
375 bool
376 cgraph_process_new_functions (void)
378 bool output = false;
379 tree fndecl;
380 struct cgraph_node *node;
382 /* Note that this queue may grow as its being processed, as the new
383 functions may generate new ones. */
384 while (cgraph_new_nodes)
386 node = cgraph_new_nodes;
387 fndecl = node->decl;
388 cgraph_new_nodes = cgraph_new_nodes->next_needed;
389 switch (cgraph_state)
391 case CGRAPH_STATE_CONSTRUCTION:
392 /* At construction time we just need to finalize function and move
393 it into reachable functions list. */
395 node->next_needed = NULL;
396 cgraph_finalize_function (fndecl, false);
397 cgraph_mark_reachable_node (node);
398 output = true;
399 break;
401 case CGRAPH_STATE_IPA:
402 case CGRAPH_STATE_IPA_SSA:
403 /* When IPA optimization already started, do all essential
404 transformations that has been already performed on the whole
405 cgraph but not on this function. */
407 gimple_register_cfg_hooks ();
408 if (!node->analyzed)
409 cgraph_analyze_function (node);
410 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
411 current_function_decl = fndecl;
412 compute_inline_parameters (node);
413 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
414 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
415 /* When not optimizing, be sure we run early local passes anyway
416 to expand OMP. */
417 || !optimize)
418 execute_pass_list (pass_early_local_passes.pass.sub);
419 free_dominance_info (CDI_POST_DOMINATORS);
420 free_dominance_info (CDI_DOMINATORS);
421 pop_cfun ();
422 current_function_decl = NULL;
423 break;
425 case CGRAPH_STATE_EXPANSION:
426 /* Functions created during expansion shall be compiled
427 directly. */
428 node->process = 0;
429 cgraph_expand_function (node);
430 break;
432 default:
433 gcc_unreachable ();
434 break;
436 cgraph_call_function_insertion_hooks (node);
438 return output;
441 /* As an GCC extension we allow redefinition of the function. The
442 semantics when both copies of bodies differ is not well defined.
443 We replace the old body with new body so in unit at a time mode
444 we always use new body, while in normal mode we may end up with
445 old body inlined into some functions and new body expanded and
446 inlined in others.
448 ??? It may make more sense to use one body for inlining and other
449 body for expanding the function but this is difficult to do. */
451 static void
452 cgraph_reset_node (struct cgraph_node *node)
454 /* If node->process is set, then we have already begun whole-unit analysis.
455 This is *not* testing for whether we've already emitted the function.
456 That case can be sort-of legitimately seen with real function redefinition
457 errors. I would argue that the front end should never present us with
458 such a case, but don't enforce that for now. */
459 gcc_assert (!node->process);
461 /* Reset our data structures so we can analyze the function again. */
462 memset (&node->local, 0, sizeof (node->local));
463 memset (&node->global, 0, sizeof (node->global));
464 memset (&node->rtl, 0, sizeof (node->rtl));
465 node->analyzed = false;
466 node->local.redefined_extern_inline = true;
467 node->local.finalized = false;
469 cgraph_node_remove_callees (node);
471 /* We may need to re-queue the node for assembling in case
472 we already proceeded it and ignored as not needed or got
473 a re-declaration in IMA mode. */
474 if (node->reachable)
476 struct cgraph_node *n;
478 for (n = cgraph_nodes_queue; n; n = n->next_needed)
479 if (n == node)
480 break;
481 if (!n)
482 node->reachable = 0;
486 static void
487 cgraph_lower_function (struct cgraph_node *node)
489 if (node->lowered)
490 return;
492 if (node->nested)
493 lower_nested_functions (node->decl);
494 gcc_assert (!node->nested);
496 tree_lowering_passes (node->decl);
497 node->lowered = true;
500 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
501 logic in effect. If NESTED is true, then our caller cannot stand to have
502 the garbage collector run at the moment. We would need to either create
503 a new GC context, or just not compile right now. */
505 void
506 cgraph_finalize_function (tree decl, bool nested)
508 struct cgraph_node *node = cgraph_node (decl);
510 if (node->local.finalized)
511 cgraph_reset_node (node);
513 node->pid = cgraph_max_pid ++;
514 notice_global_symbol (decl);
515 node->local.finalized = true;
516 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
517 node->finalized_by_frontend = true;
518 record_cdtor_fn (node->decl);
520 if (cgraph_decide_is_function_needed (node, decl))
521 cgraph_mark_needed_node (node);
523 /* Since we reclaim unreachable nodes at the end of every language
524 level unit, we need to be conservative about possible entry points
525 there. */
526 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)))
527 cgraph_mark_reachable_node (node);
529 /* If we've not yet emitted decl, tell the debug info about it. */
530 if (!TREE_ASM_WRITTEN (decl))
531 (*debug_hooks->deferred_inline_function) (decl);
533 /* Possibly warn about unused parameters. */
534 if (warn_unused_parameter)
535 do_warn_unused_parameter (decl);
537 if (!nested)
538 ggc_collect ();
541 /* C99 extern inline keywords allow changing of declaration after function
542 has been finalized. We need to re-decide if we want to mark the function as
543 needed then. */
545 void
546 cgraph_mark_if_needed (tree decl)
548 struct cgraph_node *node = cgraph_node (decl);
549 if (node->local.finalized && cgraph_decide_is_function_needed (node, decl))
550 cgraph_mark_needed_node (node);
553 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
554 static bool
555 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
557 while (node != node2 && node2)
558 node2 = node2->clone_of;
559 return node2 != NULL;
562 /* Verify cgraph nodes of given cgraph node. */
563 void
564 verify_cgraph_node (struct cgraph_node *node)
566 struct cgraph_edge *e;
567 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
568 struct function *saved_cfun = cfun;
569 basic_block this_block;
570 gimple_stmt_iterator gsi;
571 bool error_found = false;
573 if (errorcount || sorrycount)
574 return;
576 timevar_push (TV_CGRAPH_VERIFY);
577 /* debug_generic_stmt needs correct cfun */
578 set_cfun (this_cfun);
579 for (e = node->callees; e; e = e->next_callee)
580 if (e->aux)
582 error ("aux field set for edge %s->%s",
583 identifier_to_locale (cgraph_node_name (e->caller)),
584 identifier_to_locale (cgraph_node_name (e->callee)));
585 error_found = true;
587 if (node->count < 0)
589 error ("Execution count is negative");
590 error_found = true;
592 if (node->global.inlined_to && node->local.externally_visible)
594 error ("Externally visible inline clone");
595 error_found = true;
597 if (node->global.inlined_to && node->address_taken)
599 error ("Inline clone with address taken");
600 error_found = true;
602 if (node->global.inlined_to && node->needed)
604 error ("Inline clone is needed");
605 error_found = true;
607 for (e = node->callers; e; e = e->next_caller)
609 if (e->count < 0)
611 error ("caller edge count is negative");
612 error_found = true;
614 if (e->frequency < 0)
616 error ("caller edge frequency is negative");
617 error_found = true;
619 if (e->frequency > CGRAPH_FREQ_MAX)
621 error ("caller edge frequency is too large");
622 error_found = true;
624 if (!e->inline_failed)
626 if (node->global.inlined_to
627 != (e->caller->global.inlined_to
628 ? e->caller->global.inlined_to : e->caller))
630 error ("inlined_to pointer is wrong");
631 error_found = true;
633 if (node->callers->next_caller)
635 error ("multiple inline callers");
636 error_found = true;
639 else
640 if (node->global.inlined_to)
642 error ("inlined_to pointer set for noninline callers");
643 error_found = true;
646 if (!node->callers && node->global.inlined_to)
648 error ("inlined_to pointer is set but no predecessors found");
649 error_found = true;
651 if (node->global.inlined_to == node)
653 error ("inlined_to pointer refers to itself");
654 error_found = true;
657 if (!cgraph_node (node->decl))
659 error ("node not found in cgraph_hash");
660 error_found = true;
663 if (node->clone_of)
665 struct cgraph_node *n;
666 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
667 if (n == node)
668 break;
669 if (!n)
671 error ("node has wrong clone_of");
672 error_found = true;
675 if (node->clones)
677 struct cgraph_node *n;
678 for (n = node->clones; n; n = n->next_sibling_clone)
679 if (n->clone_of != node)
680 break;
681 if (n)
683 error ("node has wrong clone list");
684 error_found = true;
687 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
689 error ("node is in clone list but it is not clone");
690 error_found = true;
692 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
694 error ("node has wrong prev_clone pointer");
695 error_found = true;
697 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
699 error ("double linked list of clones corrupted");
700 error_found = true;
703 if (node->analyzed && gimple_has_body_p (node->decl)
704 && !TREE_ASM_WRITTEN (node->decl)
705 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
706 && !flag_wpa)
708 if (this_cfun->cfg)
710 /* The nodes we're interested in are never shared, so walk
711 the tree ignoring duplicates. */
712 struct pointer_set_t *visited_nodes = pointer_set_create ();
713 /* Reach the trees by walking over the CFG, and note the
714 enclosing basic-blocks in the call edges. */
715 FOR_EACH_BB_FN (this_block, this_cfun)
716 for (gsi = gsi_start_bb (this_block);
717 !gsi_end_p (gsi);
718 gsi_next (&gsi))
720 gimple stmt = gsi_stmt (gsi);
721 tree decl;
722 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
724 struct cgraph_edge *e = cgraph_edge (node, stmt);
725 if (e)
727 if (e->aux)
729 error ("shared call_stmt:");
730 debug_gimple_stmt (stmt);
731 error_found = true;
733 if (!clone_of_p (cgraph_node (decl), e->callee)
734 && !e->callee->global.inlined_to)
736 error ("edge points to wrong declaration:");
737 debug_tree (e->callee->decl);
738 fprintf (stderr," Instead of:");
739 debug_tree (decl);
741 e->aux = (void *)1;
743 else
745 error ("missing callgraph edge for call stmt:");
746 debug_gimple_stmt (stmt);
747 error_found = true;
751 pointer_set_destroy (visited_nodes);
753 else
754 /* No CFG available?! */
755 gcc_unreachable ();
757 for (e = node->callees; e; e = e->next_callee)
759 if (!e->aux && !e->indirect_call)
761 error ("edge %s->%s has no corresponding call_stmt",
762 identifier_to_locale (cgraph_node_name (e->caller)),
763 identifier_to_locale (cgraph_node_name (e->callee)));
764 debug_gimple_stmt (e->call_stmt);
765 error_found = true;
767 e->aux = 0;
770 if (error_found)
772 dump_cgraph_node (stderr, node);
773 internal_error ("verify_cgraph_node failed");
775 set_cfun (saved_cfun);
776 timevar_pop (TV_CGRAPH_VERIFY);
779 /* Verify whole cgraph structure. */
780 void
781 verify_cgraph (void)
783 struct cgraph_node *node;
785 if (sorrycount || errorcount)
786 return;
788 for (node = cgraph_nodes; node; node = node->next)
789 verify_cgraph_node (node);
792 /* Output all asm statements we have stored up to be output. */
794 static void
795 cgraph_output_pending_asms (void)
797 struct cgraph_asm_node *can;
799 if (errorcount || sorrycount)
800 return;
802 for (can = cgraph_asm_nodes; can; can = can->next)
803 assemble_asm (can->asm_str);
804 cgraph_asm_nodes = NULL;
807 /* Analyze the function scheduled to be output. */
808 static void
809 cgraph_analyze_function (struct cgraph_node *node)
811 tree save = current_function_decl;
812 tree decl = node->decl;
814 current_function_decl = decl;
815 push_cfun (DECL_STRUCT_FUNCTION (decl));
817 /* Make sure to gimplify bodies only once. During analyzing a
818 function we lower it, which will require gimplified nested
819 functions, so we can end up here with an already gimplified
820 body. */
821 if (!gimple_body (decl))
822 gimplify_function_tree (decl);
823 dump_function (TDI_generic, decl);
825 cgraph_lower_function (node);
826 node->analyzed = true;
828 pop_cfun ();
829 current_function_decl = save;
832 /* Look for externally_visible and used attributes and mark cgraph nodes
833 accordingly.
835 We cannot mark the nodes at the point the attributes are processed (in
836 handle_*_attribute) because the copy of the declarations available at that
837 point may not be canonical. For example, in:
839 void f();
840 void f() __attribute__((used));
842 the declaration we see in handle_used_attribute will be the second
843 declaration -- but the front end will subsequently merge that declaration
844 with the original declaration and discard the second declaration.
846 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
848 void f() {}
849 void f() __attribute__((externally_visible));
851 is valid.
853 So, we walk the nodes at the end of the translation unit, applying the
854 attributes at that point. */
856 static void
857 process_function_and_variable_attributes (struct cgraph_node *first,
858 struct varpool_node *first_var)
860 struct cgraph_node *node;
861 struct varpool_node *vnode;
863 for (node = cgraph_nodes; node != first; node = node->next)
865 tree decl = node->decl;
866 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
868 mark_decl_referenced (decl);
869 if (node->local.finalized)
870 cgraph_mark_needed_node (node);
872 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
874 if (! TREE_PUBLIC (node->decl))
875 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
876 "%<externally_visible%>"
877 " attribute have effect only on public objects");
878 else if (node->local.finalized)
879 cgraph_mark_needed_node (node);
882 for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
884 tree decl = vnode->decl;
885 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
887 mark_decl_referenced (decl);
888 if (vnode->finalized)
889 varpool_mark_needed_node (vnode);
891 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
893 if (! TREE_PUBLIC (vnode->decl))
894 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
895 "%<externally_visible%>"
896 " attribute have effect only on public objects");
897 else if (vnode->finalized)
898 varpool_mark_needed_node (vnode);
903 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
904 each reachable functions) and build cgraph.
905 The function can be called multiple times after inserting new nodes
906 into beginning of queue. Just the new part of queue is re-scanned then. */
908 static void
909 cgraph_analyze_functions (void)
911 /* Keep track of already processed nodes when called multiple times for
912 intermodule optimization. */
913 static struct cgraph_node *first_analyzed;
914 struct cgraph_node *first_processed = first_analyzed;
915 static struct varpool_node *first_analyzed_var;
916 struct cgraph_node *node, *next;
918 process_function_and_variable_attributes (first_processed,
919 first_analyzed_var);
920 first_processed = cgraph_nodes;
921 first_analyzed_var = varpool_nodes;
922 varpool_analyze_pending_decls ();
923 if (cgraph_dump_file)
925 fprintf (cgraph_dump_file, "Initial entry points:");
926 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
927 if (node->needed)
928 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
929 fprintf (cgraph_dump_file, "\n");
931 cgraph_process_new_functions ();
933 /* Propagate reachability flag and lower representation of all reachable
934 functions. In the future, lowering will introduce new functions and
935 new entry points on the way (by template instantiation and virtual
936 method table generation for instance). */
937 while (cgraph_nodes_queue)
939 struct cgraph_edge *edge;
940 tree decl = cgraph_nodes_queue->decl;
942 node = cgraph_nodes_queue;
943 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
944 node->next_needed = NULL;
946 /* ??? It is possible to create extern inline function and later using
947 weak alias attribute to kill its body. See
948 gcc.c-torture/compile/20011119-1.c */
949 if (!DECL_STRUCT_FUNCTION (decl))
951 cgraph_reset_node (node);
952 continue;
955 if (!node->analyzed)
956 cgraph_analyze_function (node);
958 for (edge = node->callees; edge; edge = edge->next_callee)
959 if (!edge->callee->reachable)
960 cgraph_mark_reachable_node (edge->callee);
962 /* If decl is a clone of an abstract function, mark that abstract
963 function so that we don't release its body. The DECL_INITIAL() of that
964 abstract function declaration will be later needed to output debug info. */
965 if (DECL_ABSTRACT_ORIGIN (decl))
967 struct cgraph_node *origin_node = cgraph_node (DECL_ABSTRACT_ORIGIN (decl));
968 origin_node->abstract_and_needed = true;
971 /* We finalize local static variables during constructing callgraph
972 edges. Process their attributes too. */
973 process_function_and_variable_attributes (first_processed,
974 first_analyzed_var);
975 first_processed = cgraph_nodes;
976 first_analyzed_var = varpool_nodes;
977 varpool_analyze_pending_decls ();
978 cgraph_process_new_functions ();
981 /* Collect entry points to the unit. */
982 if (cgraph_dump_file)
984 fprintf (cgraph_dump_file, "Unit entry points:");
985 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
986 if (node->needed)
987 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
988 fprintf (cgraph_dump_file, "\n\nInitial ");
989 dump_cgraph (cgraph_dump_file);
992 if (cgraph_dump_file)
993 fprintf (cgraph_dump_file, "\nReclaiming functions:");
995 for (node = cgraph_nodes; node != first_analyzed; node = next)
997 tree decl = node->decl;
998 next = node->next;
1000 if (node->local.finalized && !gimple_has_body_p (decl))
1001 cgraph_reset_node (node);
1003 if (!node->reachable && gimple_has_body_p (decl))
1005 if (cgraph_dump_file)
1006 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1007 cgraph_remove_node (node);
1008 continue;
1010 else
1011 node->next_needed = NULL;
1012 gcc_assert (!node->local.finalized || gimple_has_body_p (decl));
1013 gcc_assert (node->analyzed == node->local.finalized);
1015 if (cgraph_dump_file)
1017 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1018 dump_cgraph (cgraph_dump_file);
1020 first_analyzed = cgraph_nodes;
1021 ggc_collect ();
1025 /* Emit thunks for every node in the cgraph.
1026 FIXME: We really ought to emit thunks only for functions that are needed. */
1028 static void
1029 cgraph_emit_thunks (void)
1031 struct cgraph_node *n;
1033 for (n = cgraph_nodes; n; n = n->next)
1035 /* Only emit thunks on functions defined in this TU.
1036 Note that this may emit more thunks than strictly necessary.
1037 During optimization some nodes may disappear. It would be
1038 nice to only emit thunks only for the functions that will be
1039 emitted, but we cannot know that until the inliner and other
1040 IPA passes have run (see the sequencing of the call to
1041 cgraph_mark_functions_to_output in cgraph_optimize). */
1042 if (n->reachable
1043 && !DECL_EXTERNAL (n->decl))
1044 lang_hooks.callgraph.emit_associated_thunks (n->decl);
1049 /* Analyze the whole compilation unit once it is parsed completely. */
1051 void
1052 cgraph_finalize_compilation_unit (void)
1054 timevar_push (TV_CGRAPH);
1056 /* Do not skip analyzing the functions if there were errors, we
1057 miss diagnostics for following functions otherwise. */
1059 /* Emit size functions we didn't inline. */
1060 finalize_size_functions ();
1062 /* Call functions declared with the "constructor" or "destructor"
1063 attribute. */
1064 cgraph_build_cdtor_fns ();
1066 /* Mark alias targets necessary and emit diagnostics. */
1067 finish_aliases_1 ();
1069 if (!quiet_flag)
1071 fprintf (stderr, "\nAnalyzing compilation unit\n");
1072 fflush (stderr);
1075 /* Gimplify and lower all functions, compute reachability and
1076 remove unreachable nodes. */
1077 cgraph_analyze_functions ();
1079 /* Emit thunks for reachable nodes, if needed. */
1080 if (lang_hooks.callgraph.emit_associated_thunks)
1081 cgraph_emit_thunks ();
1083 /* Mark alias targets necessary and emit diagnostics. */
1084 finish_aliases_1 ();
1086 /* Gimplify and lower thunks. */
1087 cgraph_analyze_functions ();
1089 /* Finally drive the pass manager. */
1090 cgraph_optimize ();
1092 timevar_pop (TV_CGRAPH);
1096 /* Figure out what functions we want to assemble. */
1098 static void
1099 cgraph_mark_functions_to_output (void)
1101 struct cgraph_node *node;
1103 for (node = cgraph_nodes; node; node = node->next)
1105 tree decl = node->decl;
1106 struct cgraph_edge *e;
1108 gcc_assert (!node->process);
1110 for (e = node->callers; e; e = e->next_caller)
1111 if (e->inline_failed)
1112 break;
1114 /* We need to output all local functions that are used and not
1115 always inlined, as well as those that are reachable from
1116 outside the current compilation unit. */
1117 if (node->analyzed
1118 && !node->global.inlined_to
1119 && (node->needed
1120 || (e && node->reachable))
1121 && !TREE_ASM_WRITTEN (decl)
1122 && !DECL_EXTERNAL (decl))
1123 node->process = 1;
1124 else
1126 /* We should've reclaimed all functions that are not needed. */
1127 #ifdef ENABLE_CHECKING
1128 if (!node->global.inlined_to
1129 && gimple_has_body_p (decl)
1130 && !DECL_EXTERNAL (decl))
1132 dump_cgraph_node (stderr, node);
1133 internal_error ("failed to reclaim unneeded function");
1135 #endif
1136 gcc_assert (node->global.inlined_to
1137 || !gimple_has_body_p (decl)
1138 || DECL_EXTERNAL (decl));
1145 /* Expand function specified by NODE. */
1147 static void
1148 cgraph_expand_function (struct cgraph_node *node)
1150 tree decl = node->decl;
1152 /* We ought to not compile any inline clones. */
1153 gcc_assert (!node->global.inlined_to);
1155 announce_function (decl);
1156 node->process = 0;
1158 gcc_assert (node->lowered);
1160 /* Generate RTL for the body of DECL. */
1161 tree_rest_of_compilation (decl);
1163 /* Make sure that BE didn't give up on compiling. */
1164 gcc_assert (TREE_ASM_WRITTEN (decl));
1165 current_function_decl = NULL;
1166 gcc_assert (!cgraph_preserve_function_body_p (decl));
1167 cgraph_release_function_body (node);
1168 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1169 points to the dead function body. */
1170 cgraph_node_remove_callees (node);
1172 cgraph_function_flags_ready = true;
1175 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1177 bool
1178 cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
1180 *reason = e->inline_failed;
1181 return !e->inline_failed;
1186 /* Expand all functions that must be output.
1188 Attempt to topologically sort the nodes so function is output when
1189 all called functions are already assembled to allow data to be
1190 propagated across the callgraph. Use a stack to get smaller distance
1191 between a function and its callees (later we may choose to use a more
1192 sophisticated algorithm for function reordering; we will likely want
1193 to use subsections to make the output functions appear in top-down
1194 order). */
1196 static void
1197 cgraph_expand_all_functions (void)
1199 struct cgraph_node *node;
1200 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1201 int order_pos, new_order_pos = 0;
1202 int i;
1204 order_pos = cgraph_postorder (order);
1205 gcc_assert (order_pos == cgraph_n_nodes);
1207 /* Garbage collector may remove inline clones we eliminate during
1208 optimization. So we must be sure to not reference them. */
1209 for (i = 0; i < order_pos; i++)
1210 if (order[i]->process)
1211 order[new_order_pos++] = order[i];
1213 for (i = new_order_pos - 1; i >= 0; i--)
1215 node = order[i];
1216 if (node->process)
1218 gcc_assert (node->reachable);
1219 node->process = 0;
1220 cgraph_expand_function (node);
1223 cgraph_process_new_functions ();
1225 free (order);
1229 /* This is used to sort the node types by the cgraph order number. */
1231 enum cgraph_order_sort_kind
1233 ORDER_UNDEFINED = 0,
1234 ORDER_FUNCTION,
1235 ORDER_VAR,
1236 ORDER_ASM
1239 struct cgraph_order_sort
1241 enum cgraph_order_sort_kind kind;
1242 union
1244 struct cgraph_node *f;
1245 struct varpool_node *v;
1246 struct cgraph_asm_node *a;
1247 } u;
1250 /* Output all functions, variables, and asm statements in the order
1251 according to their order fields, which is the order in which they
1252 appeared in the file. This implements -fno-toplevel-reorder. In
1253 this mode we may output functions and variables which don't really
1254 need to be output. */
1256 static void
1257 cgraph_output_in_order (void)
1259 int max;
1260 size_t size;
1261 struct cgraph_order_sort *nodes;
1262 int i;
1263 struct cgraph_node *pf;
1264 struct varpool_node *pv;
1265 struct cgraph_asm_node *pa;
1267 max = cgraph_order;
1268 size = max * sizeof (struct cgraph_order_sort);
1269 nodes = (struct cgraph_order_sort *) alloca (size);
1270 memset (nodes, 0, size);
1272 varpool_analyze_pending_decls ();
1274 for (pf = cgraph_nodes; pf; pf = pf->next)
1276 if (pf->process)
1278 i = pf->order;
1279 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1280 nodes[i].kind = ORDER_FUNCTION;
1281 nodes[i].u.f = pf;
1285 for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1287 i = pv->order;
1288 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1289 nodes[i].kind = ORDER_VAR;
1290 nodes[i].u.v = pv;
1293 for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1295 i = pa->order;
1296 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1297 nodes[i].kind = ORDER_ASM;
1298 nodes[i].u.a = pa;
1301 /* In toplevel reorder mode we output all statics; mark them as needed. */
1302 for (i = 0; i < max; ++i)
1304 if (nodes[i].kind == ORDER_VAR)
1306 varpool_mark_needed_node (nodes[i].u.v);
1309 varpool_empty_needed_queue ();
1311 for (i = 0; i < max; ++i)
1313 switch (nodes[i].kind)
1315 case ORDER_FUNCTION:
1316 nodes[i].u.f->process = 0;
1317 cgraph_expand_function (nodes[i].u.f);
1318 break;
1320 case ORDER_VAR:
1321 varpool_assemble_decl (nodes[i].u.v);
1322 break;
1324 case ORDER_ASM:
1325 assemble_asm (nodes[i].u.a->asm_str);
1326 break;
1328 case ORDER_UNDEFINED:
1329 break;
1331 default:
1332 gcc_unreachable ();
1336 cgraph_asm_nodes = NULL;
1339 /* Return true when function body of DECL still needs to be kept around
1340 for later re-use. */
1341 bool
1342 cgraph_preserve_function_body_p (tree decl)
1344 struct cgraph_node *node;
1346 gcc_assert (cgraph_global_info_ready);
1347 /* Look if there is any clone around. */
1348 node = cgraph_node (decl);
1349 if (node->clones)
1350 return true;
1351 return false;
1354 static void
1355 ipa_passes (void)
1357 int executed_p = 0;
1359 set_cfun (NULL);
1360 current_function_decl = NULL;
1361 gimple_register_cfg_hooks ();
1362 bitmap_obstack_initialize (NULL);
1364 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1366 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_EXECUTION, &executed_p);
1367 if (!executed_p)
1369 if (!in_lto_p)
1370 execute_ipa_pass_list (all_small_ipa_passes);
1372 /* If pass_all_early_optimizations was not scheduled, the state of
1373 the cgraph will not be properly updated. Update it now. */
1374 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1375 cgraph_state = CGRAPH_STATE_IPA_SSA;
1377 if (!in_lto_p)
1379 /* Generate coverage variables and constructors. */
1380 coverage_finish ();
1382 /* Process new functions added. */
1383 set_cfun (NULL);
1384 current_function_decl = NULL;
1385 cgraph_process_new_functions ();
1387 execute_ipa_summary_passes
1388 ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
1390 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
1392 if (!in_lto_p)
1393 ipa_write_summaries ();
1395 if (!flag_ltrans)
1396 execute_ipa_pass_list (all_regular_ipa_passes);
1398 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
1400 bitmap_obstack_release (NULL);
1404 /* Perform simple optimizations based on callgraph. */
1406 void
1407 cgraph_optimize (void)
1409 if (errorcount || sorrycount)
1410 return;
1412 #ifdef ENABLE_CHECKING
1413 verify_cgraph ();
1414 #endif
1416 /* Frontend may output common variables after the unit has been finalized.
1417 It is safe to deal with them here as they are always zero initialized. */
1418 varpool_analyze_pending_decls ();
1420 timevar_push (TV_CGRAPHOPT);
1421 if (pre_ipa_mem_report)
1423 fprintf (stderr, "Memory consumption before IPA\n");
1424 dump_memory_report (false);
1426 if (!quiet_flag)
1427 fprintf (stderr, "Performing interprocedural optimizations\n");
1428 cgraph_state = CGRAPH_STATE_IPA;
1430 /* Don't run the IPA passes if there was any error or sorry messages. */
1431 if (errorcount == 0 && sorrycount == 0)
1432 ipa_passes ();
1434 /* Do nothing else if any IPA pass found errors. */
1435 if (errorcount || sorrycount)
1437 timevar_pop (TV_CGRAPHOPT);
1438 return;
1441 /* This pass remove bodies of extern inline functions we never inlined.
1442 Do this later so other IPA passes see what is really going on. */
1443 cgraph_remove_unreachable_nodes (false, dump_file);
1444 cgraph_global_info_ready = true;
1445 if (cgraph_dump_file)
1447 fprintf (cgraph_dump_file, "Optimized ");
1448 dump_cgraph (cgraph_dump_file);
1449 dump_varpool (cgraph_dump_file);
1451 if (post_ipa_mem_report)
1453 fprintf (stderr, "Memory consumption after IPA\n");
1454 dump_memory_report (false);
1456 timevar_pop (TV_CGRAPHOPT);
1458 /* Output everything. */
1459 (*debug_hooks->assembly_start) ();
1460 if (!quiet_flag)
1461 fprintf (stderr, "Assembling functions:\n");
1462 #ifdef ENABLE_CHECKING
1463 verify_cgraph ();
1464 #endif
1466 cgraph_materialize_all_clones ();
1467 cgraph_mark_functions_to_output ();
1469 cgraph_state = CGRAPH_STATE_EXPANSION;
1470 if (!flag_toplevel_reorder)
1471 cgraph_output_in_order ();
1472 else
1474 cgraph_output_pending_asms ();
1476 cgraph_expand_all_functions ();
1477 varpool_remove_unreferenced_decls ();
1479 varpool_assemble_pending_decls ();
1481 cgraph_process_new_functions ();
1482 cgraph_state = CGRAPH_STATE_FINISHED;
1484 if (cgraph_dump_file)
1486 fprintf (cgraph_dump_file, "\nFinal ");
1487 dump_cgraph (cgraph_dump_file);
1489 #ifdef ENABLE_CHECKING
1490 verify_cgraph ();
1491 /* Double check that all inline clones are gone and that all
1492 function bodies have been released from memory. */
1493 if (!(sorrycount || errorcount))
1495 struct cgraph_node *node;
1496 bool error_found = false;
1498 for (node = cgraph_nodes; node; node = node->next)
1499 if (node->analyzed
1500 && (node->global.inlined_to
1501 || gimple_has_body_p (node->decl)))
1503 error_found = true;
1504 dump_cgraph_node (stderr, node);
1506 if (error_found)
1507 internal_error ("nodes with unreleased memory found");
1509 #endif
1513 /* Generate and emit a static constructor or destructor. WHICH must
1514 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1515 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1516 initialization priority for this constructor or destructor. */
1518 void
1519 cgraph_build_static_cdtor (char which, tree body, int priority)
1521 static int counter = 0;
1522 char which_buf[16];
1523 tree decl, name, resdecl;
1525 /* The priority is encoded in the constructor or destructor name.
1526 collect2 will sort the names and arrange that they are called at
1527 program startup. */
1528 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1529 name = get_file_function_name (which_buf);
1531 decl = build_decl (input_location, FUNCTION_DECL, name,
1532 build_function_type (void_type_node, void_list_node));
1533 current_function_decl = decl;
1535 resdecl = build_decl (input_location,
1536 RESULT_DECL, NULL_TREE, void_type_node);
1537 DECL_ARTIFICIAL (resdecl) = 1;
1538 DECL_RESULT (decl) = resdecl;
1539 DECL_CONTEXT (resdecl) = decl;
1541 allocate_struct_function (decl, false);
1543 TREE_STATIC (decl) = 1;
1544 TREE_USED (decl) = 1;
1545 DECL_ARTIFICIAL (decl) = 1;
1546 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1547 DECL_SAVED_TREE (decl) = body;
1548 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1549 DECL_UNINLINABLE (decl) = 1;
1551 DECL_INITIAL (decl) = make_node (BLOCK);
1552 TREE_USED (DECL_INITIAL (decl)) = 1;
1554 DECL_SOURCE_LOCATION (decl) = input_location;
1555 cfun->function_end_locus = input_location;
1557 switch (which)
1559 case 'I':
1560 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1561 decl_init_priority_insert (decl, priority);
1562 break;
1563 case 'D':
1564 DECL_STATIC_DESTRUCTOR (decl) = 1;
1565 decl_fini_priority_insert (decl, priority);
1566 break;
1567 default:
1568 gcc_unreachable ();
1571 gimplify_function_tree (decl);
1573 cgraph_add_new_function (decl, false);
1574 cgraph_mark_needed_node (cgraph_node (decl));
1575 set_cfun (NULL);
1578 void
1579 init_cgraph (void)
1581 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1584 /* The edges representing the callers of the NEW_VERSION node were
1585 fixed by cgraph_function_versioning (), now the call_expr in their
1586 respective tree code should be updated to call the NEW_VERSION. */
1588 static void
1589 update_call_expr (struct cgraph_node *new_version)
1591 struct cgraph_edge *e;
1593 gcc_assert (new_version);
1595 /* Update the call expr on the edges to call the new version. */
1596 for (e = new_version->callers; e; e = e->next_caller)
1598 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
1599 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
1600 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
1605 /* Create a new cgraph node which is the new version of
1606 OLD_VERSION node. REDIRECT_CALLERS holds the callers
1607 edges which should be redirected to point to
1608 NEW_VERSION. ALL the callees edges of OLD_VERSION
1609 are cloned to the new version node. Return the new
1610 version node. */
1612 static struct cgraph_node *
1613 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
1614 tree new_decl,
1615 VEC(cgraph_edge_p,heap) *redirect_callers)
1617 struct cgraph_node *new_version;
1618 struct cgraph_edge *e, *new_e;
1619 struct cgraph_edge *next_callee;
1620 unsigned i;
1622 gcc_assert (old_version);
1624 new_version = cgraph_node (new_decl);
1626 new_version->analyzed = true;
1627 new_version->local = old_version->local;
1628 new_version->global = old_version->global;
1629 new_version->rtl = new_version->rtl;
1630 new_version->reachable = true;
1631 new_version->count = old_version->count;
1633 /* Clone the old node callees. Recursive calls are
1634 also cloned. */
1635 for (e = old_version->callees;e; e=e->next_callee)
1637 new_e = cgraph_clone_edge (e, new_version, e->call_stmt,
1638 e->lto_stmt_uid, 0, e->frequency,
1639 e->loop_nest, true);
1640 new_e->count = e->count;
1642 /* Fix recursive calls.
1643 If OLD_VERSION has a recursive call after the
1644 previous edge cloning, the new version will have an edge
1645 pointing to the old version, which is wrong;
1646 Redirect it to point to the new version. */
1647 for (e = new_version->callees ; e; e = next_callee)
1649 next_callee = e->next_callee;
1650 if (e->callee == old_version)
1651 cgraph_redirect_edge_callee (e, new_version);
1653 if (!next_callee)
1654 break;
1656 for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1658 /* Redirect calls to the old version node to point to its new
1659 version. */
1660 cgraph_redirect_edge_callee (e, new_version);
1663 return new_version;
1666 /* Perform function versioning.
1667 Function versioning includes copying of the tree and
1668 a callgraph update (creating a new cgraph node and updating
1669 its callees and callers).
1671 REDIRECT_CALLERS varray includes the edges to be redirected
1672 to the new version.
1674 TREE_MAP is a mapping of tree nodes we want to replace with
1675 new ones (according to results of prior analysis).
1676 OLD_VERSION_NODE is the node that is versioned.
1677 It returns the new version's cgraph node.
1678 ARGS_TO_SKIP lists arguments to be omitted from functions
1681 struct cgraph_node *
1682 cgraph_function_versioning (struct cgraph_node *old_version_node,
1683 VEC(cgraph_edge_p,heap) *redirect_callers,
1684 VEC (ipa_replace_map_p,gc)* tree_map,
1685 bitmap args_to_skip)
1687 tree old_decl = old_version_node->decl;
1688 struct cgraph_node *new_version_node = NULL;
1689 tree new_decl;
1691 if (!tree_versionable_function_p (old_decl))
1692 return NULL;
1694 /* Make a new FUNCTION_DECL tree node for the
1695 new version. */
1696 if (!args_to_skip)
1697 new_decl = copy_node (old_decl);
1698 else
1699 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
1701 /* Create the new version's call-graph node.
1702 and update the edges of the new node. */
1703 new_version_node =
1704 cgraph_copy_node_for_versioning (old_version_node, new_decl,
1705 redirect_callers);
1707 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1708 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip);
1710 /* Update the new version's properties.
1711 Make The new version visible only within this translation unit. Make sure
1712 that is not weak also.
1713 ??? We cannot use COMDAT linkage because there is no
1714 ABI support for this. */
1715 DECL_EXTERNAL (new_version_node->decl) = 0;
1716 DECL_COMDAT_GROUP (new_version_node->decl) = NULL_TREE;
1717 TREE_PUBLIC (new_version_node->decl) = 0;
1718 DECL_COMDAT (new_version_node->decl) = 0;
1719 DECL_WEAK (new_version_node->decl) = 0;
1720 DECL_VIRTUAL_P (new_version_node->decl) = 0;
1721 new_version_node->local.externally_visible = 0;
1722 new_version_node->local.local = 1;
1723 new_version_node->lowered = true;
1725 /* Update the call_expr on the edges to call the new version node. */
1726 update_call_expr (new_version_node);
1728 cgraph_call_function_insertion_hooks (new_version_node);
1729 return new_version_node;
1732 /* Produce separate function body for inline clones so the offline copy can be
1733 modified without affecting them. */
1734 struct cgraph_node *
1735 save_inline_function_body (struct cgraph_node *node)
1737 struct cgraph_node *first_clone, *n;
1739 gcc_assert (node == cgraph_node (node->decl));
1741 cgraph_lower_function (node);
1743 first_clone = node->clones;
1745 first_clone->decl = copy_node (node->decl);
1746 cgraph_insert_node_to_hashtable (first_clone);
1747 gcc_assert (first_clone == cgraph_node (first_clone->decl));
1748 if (first_clone->next_sibling_clone)
1750 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
1751 n->clone_of = first_clone;
1752 n->clone_of = first_clone;
1753 n->next_sibling_clone = first_clone->clones;
1754 if (first_clone->clones)
1755 first_clone->clones->prev_sibling_clone = n;
1756 first_clone->clones = first_clone->next_sibling_clone;
1757 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
1758 first_clone->next_sibling_clone = NULL;
1759 gcc_assert (!first_clone->prev_sibling_clone);
1761 first_clone->clone_of = NULL;
1762 node->clones = NULL;
1764 if (first_clone->clones)
1765 for (n = first_clone->clones; n != first_clone;)
1767 gcc_assert (n->decl == node->decl);
1768 n->decl = first_clone->decl;
1769 if (n->clones)
1770 n = n->clones;
1771 else if (n->next_sibling_clone)
1772 n = n->next_sibling_clone;
1773 else
1775 while (n != first_clone && !n->next_sibling_clone)
1776 n = n->clone_of;
1777 if (n != first_clone)
1778 n = n->next_sibling_clone;
1782 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1783 tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL);
1785 DECL_EXTERNAL (first_clone->decl) = 0;
1786 DECL_COMDAT_GROUP (first_clone->decl) = NULL_TREE;
1787 TREE_PUBLIC (first_clone->decl) = 0;
1788 DECL_COMDAT (first_clone->decl) = 0;
1789 VEC_free (ipa_opt_pass, heap,
1790 DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply);
1791 DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply = NULL;
1793 #ifdef ENABLE_CHECKING
1794 verify_cgraph_node (first_clone);
1795 #endif
1796 return first_clone;
1799 /* Given virtual clone, turn it into actual clone. */
1800 static void
1801 cgraph_materialize_clone (struct cgraph_node *node)
1803 bitmap_obstack_initialize (NULL);
1804 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1805 tree_function_versioning (node->clone_of->decl, node->decl,
1806 node->clone.tree_map, true,
1807 node->clone.args_to_skip);
1808 if (cgraph_dump_file)
1810 dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags);
1811 dump_function_to_file (node->decl, cgraph_dump_file, dump_flags);
1814 /* Function is no longer clone. */
1815 if (node->next_sibling_clone)
1816 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1817 if (node->prev_sibling_clone)
1818 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1819 else
1820 node->clone_of->clones = node->next_sibling_clone;
1821 node->next_sibling_clone = NULL;
1822 node->prev_sibling_clone = NULL;
1823 node->clone_of = NULL;
1824 bitmap_obstack_release (NULL);
1827 /* Once all functions from compilation unit are in memory, produce all clones
1828 and update all calls.
1829 We might also do this on demand if we don't want to bring all functions to
1830 memory prior compilation, but current WHOPR implementation does that and it is
1831 is bit easier to keep everything right in this order. */
1832 void
1833 cgraph_materialize_all_clones (void)
1835 struct cgraph_node *node;
1836 bool stabilized = false;
1838 if (cgraph_dump_file)
1839 fprintf (cgraph_dump_file, "Materializing clones\n");
1840 #ifdef ENABLE_CHECKING
1841 verify_cgraph ();
1842 #endif
1844 /* We can also do topological order, but number of iterations should be
1845 bounded by number of IPA passes since single IPA pass is probably not
1846 going to create clones of clones it created itself. */
1847 while (!stabilized)
1849 stabilized = true;
1850 for (node = cgraph_nodes; node; node = node->next)
1852 if (node->clone_of && node->decl != node->clone_of->decl
1853 && !gimple_has_body_p (node->decl))
1855 if (gimple_has_body_p (node->clone_of->decl))
1857 if (cgraph_dump_file)
1859 fprintf (cgraph_dump_file, "clonning %s to %s\n",
1860 cgraph_node_name (node->clone_of),
1861 cgraph_node_name (node));
1862 if (node->clone.tree_map)
1864 unsigned int i;
1865 fprintf (cgraph_dump_file, " replace map: ");
1866 for (i = 0; i < VEC_length (ipa_replace_map_p,
1867 node->clone.tree_map);
1868 i++)
1870 struct ipa_replace_map *replace_info;
1871 replace_info = VEC_index (ipa_replace_map_p,
1872 node->clone.tree_map,
1874 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
1875 fprintf (cgraph_dump_file, " -> ");
1876 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
1877 fprintf (cgraph_dump_file, "%s%s;",
1878 replace_info->replace_p ? "(replace)":"",
1879 replace_info->ref_p ? "(ref)":"");
1881 fprintf (cgraph_dump_file, "\n");
1883 if (node->clone.args_to_skip)
1885 fprintf (cgraph_dump_file, " args_to_skip: ");
1886 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
1888 if (node->clone.args_to_skip)
1890 fprintf (cgraph_dump_file, " combined_args_to_skip:");
1891 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
1894 cgraph_materialize_clone (node);
1896 else
1897 stabilized = false;
1901 if (cgraph_dump_file)
1902 fprintf (cgraph_dump_file, "Updating call sites\n");
1903 for (node = cgraph_nodes; node; node = node->next)
1904 if (node->analyzed && gimple_has_body_p (node->decl)
1905 && (!node->clone_of || node->clone_of->decl != node->decl))
1907 struct cgraph_edge *e;
1909 current_function_decl = node->decl;
1910 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1911 for (e = node->callees; e; e = e->next_callee)
1913 tree decl = gimple_call_fndecl (e->call_stmt);
1914 /* When function gets inlined, indirect inlining might've invented
1915 new edge for orginally indirect stmt. Since we are not
1916 preserving clones in the original form, we must not update here
1917 since other inline clones don't need to contain call to the same
1918 call. Inliner will do the substitution for us later. */
1919 if (decl && decl != e->callee->decl)
1921 gimple new_stmt;
1922 gimple_stmt_iterator gsi;
1924 if (cgraph_dump_file)
1926 fprintf (cgraph_dump_file, "updating call of %s in %s:",
1927 cgraph_node_name (node),
1928 cgraph_node_name (e->callee));
1929 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1932 if (e->callee->clone.combined_args_to_skip)
1933 new_stmt = gimple_call_copy_skip_args (e->call_stmt,
1934 e->callee->clone.combined_args_to_skip);
1935 else
1936 new_stmt = e->call_stmt;
1937 if (gimple_vdef (new_stmt)
1938 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1939 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1940 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1942 gsi = gsi_for_stmt (e->call_stmt);
1943 gsi_replace (&gsi, new_stmt, true);
1945 /* Update EH information too, just in case. */
1946 maybe_clean_or_replace_eh_stmt (e->call_stmt, new_stmt);
1948 cgraph_set_call_stmt_including_clones (node, e->call_stmt, new_stmt);
1950 if (cgraph_dump_file)
1952 fprintf (cgraph_dump_file, " updated to:");
1953 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1957 pop_cfun ();
1958 current_function_decl = NULL;
1959 #ifdef ENABLE_CHECKING
1960 verify_cgraph_node (node);
1961 #endif
1963 #ifdef ENABLE_CHECKING
1964 verify_cgraph ();
1965 #endif
1966 cgraph_remove_unreachable_nodes (false, cgraph_dump_file);
1969 #include "gt-cgraphunit.h"