2008-08-17 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cgraphunit.c
blobeb7de103d4b33a5ecf4c8b5f7efcb77bc1421167
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"
139 static void cgraph_expand_all_functions (void);
140 static void cgraph_mark_functions_to_output (void);
141 static void cgraph_expand_function (struct cgraph_node *);
142 static void cgraph_output_pending_asms (void);
143 static void cgraph_optimize (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 static bool
318 decide_is_function_needed (struct cgraph_node *node, tree decl)
320 if (MAIN_NAME_P (DECL_NAME (decl))
321 && TREE_PUBLIC (decl))
323 node->local.externally_visible = true;
324 return true;
327 /* If the user told us it is used, then it must be so. */
328 if (node->local.externally_visible)
329 return true;
331 /* ??? If the assembler name is set by hand, it is possible to assemble
332 the name later after finalizing the function and the fact is noticed
333 in assemble_name then. This is arguably a bug. */
334 if (DECL_ASSEMBLER_NAME_SET_P (decl)
335 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
336 return true;
338 /* With -fkeep-inline-functions we are keeping all inline functions except
339 for extern inline ones. */
340 if (flag_keep_inline_functions
341 && DECL_DECLARED_INLINE_P (decl)
342 && !DECL_EXTERNAL (decl)
343 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)))
344 return true;
346 /* If we decided it was needed before, but at the time we didn't have
347 the body of the function available, then it's still needed. We have
348 to go back and re-check its dependencies now. */
349 if (node->needed)
350 return true;
352 /* Externally visible functions must be output. The exception is
353 COMDAT functions that must be output only when they are needed.
355 When not optimizing, also output the static functions. (see
356 PR24561), but don't do so for always_inline functions, functions
357 declared inline and nested functions. These was optimized out
358 in the original implementation and it is unclear whether we want
359 to change the behavior here. */
360 if (((TREE_PUBLIC (decl)
361 || (!optimize && !node->local.disregard_inline_limits
362 && !DECL_DECLARED_INLINE_P (decl)
363 && !node->origin))
364 && !flag_whole_program)
365 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
366 return true;
368 /* Constructors and destructors are reachable from the runtime by
369 some mechanism. */
370 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
371 return true;
373 return false;
376 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
377 functions into callgraph in a way so they look like ordinary reachable
378 functions inserted into callgraph already at construction time. */
380 bool
381 cgraph_process_new_functions (void)
383 bool output = false;
384 tree fndecl;
385 struct cgraph_node *node;
387 /* Note that this queue may grow as its being processed, as the new
388 functions may generate new ones. */
389 while (cgraph_new_nodes)
391 node = cgraph_new_nodes;
392 fndecl = node->decl;
393 cgraph_new_nodes = cgraph_new_nodes->next_needed;
394 switch (cgraph_state)
396 case CGRAPH_STATE_CONSTRUCTION:
397 /* At construction time we just need to finalize function and move
398 it into reachable functions list. */
400 node->next_needed = NULL;
401 cgraph_finalize_function (fndecl, false);
402 cgraph_mark_reachable_node (node);
403 output = true;
404 break;
406 case CGRAPH_STATE_IPA:
407 case CGRAPH_STATE_IPA_SSA:
408 /* When IPA optimization already started, do all essential
409 transformations that has been already performed on the whole
410 cgraph but not on this function. */
412 gimple_register_cfg_hooks ();
413 if (!node->analyzed)
414 cgraph_analyze_function (node);
415 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
416 current_function_decl = fndecl;
417 compute_inline_parameters (node);
418 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
419 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
420 /* When not optimizing, be sure we run early local passes anyway
421 to expand OMP. */
422 || !optimize)
423 execute_pass_list (pass_early_local_passes.pass.sub);
424 free_dominance_info (CDI_POST_DOMINATORS);
425 free_dominance_info (CDI_DOMINATORS);
426 pop_cfun ();
427 current_function_decl = NULL;
428 break;
430 case CGRAPH_STATE_EXPANSION:
431 /* Functions created during expansion shall be compiled
432 directly. */
433 node->process = 0;
434 cgraph_expand_function (node);
435 break;
437 default:
438 gcc_unreachable ();
439 break;
441 cgraph_call_function_insertion_hooks (node);
443 return output;
446 /* As an GCC extension we allow redefinition of the function. The
447 semantics when both copies of bodies differ is not well defined.
448 We replace the old body with new body so in unit at a time mode
449 we always use new body, while in normal mode we may end up with
450 old body inlined into some functions and new body expanded and
451 inlined in others.
453 ??? It may make more sense to use one body for inlining and other
454 body for expanding the function but this is difficult to do. */
456 static void
457 cgraph_reset_node (struct cgraph_node *node)
459 /* If node->process is set, then we have already begun whole-unit analysis.
460 This is *not* testing for whether we've already emitted the function.
461 That case can be sort-of legitimately seen with real function redefinition
462 errors. I would argue that the front end should never present us with
463 such a case, but don't enforce that for now. */
464 gcc_assert (!node->process);
466 /* Reset our data structures so we can analyze the function again. */
467 memset (&node->local, 0, sizeof (node->local));
468 memset (&node->global, 0, sizeof (node->global));
469 memset (&node->rtl, 0, sizeof (node->rtl));
470 node->analyzed = false;
471 node->local.redefined_extern_inline = true;
472 node->local.finalized = false;
474 cgraph_node_remove_callees (node);
476 /* We may need to re-queue the node for assembling in case
477 we already proceeded it and ignored as not needed or got
478 a re-declaration in IMA mode. */
479 if (node->reachable)
481 struct cgraph_node *n;
483 for (n = cgraph_nodes_queue; n; n = n->next_needed)
484 if (n == node)
485 break;
486 if (!n)
487 node->reachable = 0;
491 static void
492 cgraph_lower_function (struct cgraph_node *node)
494 if (node->lowered)
495 return;
497 if (node->nested)
498 lower_nested_functions (node->decl);
499 gcc_assert (!node->nested);
501 tree_lowering_passes (node->decl);
502 node->lowered = true;
505 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
506 logic in effect. If NESTED is true, then our caller cannot stand to have
507 the garbage collector run at the moment. We would need to either create
508 a new GC context, or just not compile right now. */
510 void
511 cgraph_finalize_function (tree decl, bool nested)
513 struct cgraph_node *node = cgraph_node (decl);
515 if (node->local.finalized)
516 cgraph_reset_node (node);
518 node->pid = cgraph_max_pid ++;
519 notice_global_symbol (decl);
520 node->local.finalized = true;
521 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
522 node->finalized_by_frontend = true;
523 record_cdtor_fn (node->decl);
525 if (decide_is_function_needed (node, decl))
526 cgraph_mark_needed_node (node);
528 /* Since we reclaim unreachable nodes at the end of every language
529 level unit, we need to be conservative about possible entry points
530 there. */
531 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)))
532 cgraph_mark_reachable_node (node);
534 /* If we've not yet emitted decl, tell the debug info about it. */
535 if (!TREE_ASM_WRITTEN (decl))
536 (*debug_hooks->deferred_inline_function) (decl);
538 /* Possibly warn about unused parameters. */
539 if (warn_unused_parameter)
540 do_warn_unused_parameter (decl);
542 if (!nested)
543 ggc_collect ();
546 /* C99 extern inline keywords allow changing of declaration after function
547 has been finalized. We need to re-decide if we want to mark the function as
548 needed then. */
550 void
551 cgraph_mark_if_needed (tree decl)
553 struct cgraph_node *node = cgraph_node (decl);
554 if (node->local.finalized && decide_is_function_needed (node, decl))
555 cgraph_mark_needed_node (node);
558 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
559 static bool
560 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
562 while (node != node2 && node2)
563 node2 = node2->clone_of;
564 return node2 != NULL;
567 /* Verify cgraph nodes of given cgraph node. */
568 void
569 verify_cgraph_node (struct cgraph_node *node)
571 struct cgraph_edge *e;
572 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
573 struct function *saved_cfun = cfun;
574 basic_block this_block;
575 gimple_stmt_iterator gsi;
576 bool error_found = false;
578 if (errorcount || sorrycount)
579 return;
581 timevar_push (TV_CGRAPH_VERIFY);
582 /* debug_generic_stmt needs correct cfun */
583 set_cfun (this_cfun);
584 for (e = node->callees; e; e = e->next_callee)
585 if (e->aux)
587 error ("aux field set for edge %s->%s",
588 identifier_to_locale (cgraph_node_name (e->caller)),
589 identifier_to_locale (cgraph_node_name (e->callee)));
590 error_found = true;
592 if (node->count < 0)
594 error ("Execution count is negative");
595 error_found = true;
597 for (e = node->callers; e; e = e->next_caller)
599 if (e->count < 0)
601 error ("caller edge count is negative");
602 error_found = true;
604 if (e->frequency < 0)
606 error ("caller edge frequency is negative");
607 error_found = true;
609 if (e->frequency > CGRAPH_FREQ_MAX)
611 error ("caller edge frequency is too large");
612 error_found = true;
614 if (!e->inline_failed)
616 if (node->global.inlined_to
617 != (e->caller->global.inlined_to
618 ? e->caller->global.inlined_to : e->caller))
620 error ("inlined_to pointer is wrong");
621 error_found = true;
623 if (node->callers->next_caller)
625 error ("multiple inline callers");
626 error_found = true;
629 else
630 if (node->global.inlined_to)
632 error ("inlined_to pointer set for noninline callers");
633 error_found = true;
636 if (!node->callers && node->global.inlined_to)
638 error ("inlined_to pointer is set but no predecessors found");
639 error_found = true;
641 if (node->global.inlined_to == node)
643 error ("inlined_to pointer refers to itself");
644 error_found = true;
647 if (!cgraph_node (node->decl))
649 error ("node not found in cgraph_hash");
650 error_found = true;
653 if (node->clone_of)
655 struct cgraph_node *n;
656 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
657 if (n == node)
658 break;
659 if (!n)
661 error ("node has wrong clone_of");
662 error_found = true;
665 if (node->clones)
667 struct cgraph_node *n;
668 for (n = node->clones; n; n = n->next_sibling_clone)
669 if (n->clone_of != node)
670 break;
671 if (n)
673 error ("node has wrong clone list");
674 error_found = true;
677 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
679 error ("node is in clone list but it is not clone");
680 error_found = true;
682 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
684 error ("node has wrong prev_clone pointer");
685 error_found = true;
687 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
689 error ("double linked list of clones corrupted");
690 error_found = true;
693 if (node->analyzed && gimple_has_body_p (node->decl)
694 && !TREE_ASM_WRITTEN (node->decl)
695 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
697 if (this_cfun->cfg)
699 /* The nodes we're interested in are never shared, so walk
700 the tree ignoring duplicates. */
701 struct pointer_set_t *visited_nodes = pointer_set_create ();
702 /* Reach the trees by walking over the CFG, and note the
703 enclosing basic-blocks in the call edges. */
704 FOR_EACH_BB_FN (this_block, this_cfun)
705 for (gsi = gsi_start_bb (this_block);
706 !gsi_end_p (gsi);
707 gsi_next (&gsi))
709 gimple stmt = gsi_stmt (gsi);
710 tree decl;
711 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
713 struct cgraph_edge *e = cgraph_edge (node, stmt);
714 if (e)
716 if (e->aux)
718 error ("shared call_stmt:");
719 debug_gimple_stmt (stmt);
720 error_found = true;
722 if (!clone_of_p (cgraph_node (decl), e->callee)
723 && !e->callee->global.inlined_to)
725 error ("edge points to wrong declaration:");
726 debug_tree (e->callee->decl);
727 fprintf (stderr," Instead of:");
728 debug_tree (decl);
730 e->aux = (void *)1;
732 else
734 error ("missing callgraph edge for call stmt:");
735 debug_gimple_stmt (stmt);
736 error_found = true;
740 pointer_set_destroy (visited_nodes);
742 else
743 /* No CFG available?! */
744 gcc_unreachable ();
746 for (e = node->callees; e; e = e->next_callee)
748 if (!e->aux && !e->indirect_call)
750 error ("edge %s->%s has no corresponding call_stmt",
751 identifier_to_locale (cgraph_node_name (e->caller)),
752 identifier_to_locale (cgraph_node_name (e->callee)));
753 debug_gimple_stmt (e->call_stmt);
754 error_found = true;
756 e->aux = 0;
759 if (error_found)
761 dump_cgraph_node (stderr, node);
762 internal_error ("verify_cgraph_node failed");
764 set_cfun (saved_cfun);
765 timevar_pop (TV_CGRAPH_VERIFY);
768 /* Verify whole cgraph structure. */
769 void
770 verify_cgraph (void)
772 struct cgraph_node *node;
774 if (sorrycount || errorcount)
775 return;
777 for (node = cgraph_nodes; node; node = node->next)
778 verify_cgraph_node (node);
781 /* Output all asm statements we have stored up to be output. */
783 static void
784 cgraph_output_pending_asms (void)
786 struct cgraph_asm_node *can;
788 if (errorcount || sorrycount)
789 return;
791 for (can = cgraph_asm_nodes; can; can = can->next)
792 assemble_asm (can->asm_str);
793 cgraph_asm_nodes = NULL;
796 /* Analyze the function scheduled to be output. */
797 static void
798 cgraph_analyze_function (struct cgraph_node *node)
800 tree save = current_function_decl;
801 tree decl = node->decl;
803 current_function_decl = decl;
804 push_cfun (DECL_STRUCT_FUNCTION (decl));
806 /* Make sure to gimplify bodies only once. During analyzing a
807 function we lower it, which will require gimplified nested
808 functions, so we can end up here with an already gimplified
809 body. */
810 if (!gimple_body (decl))
811 gimplify_function_tree (decl);
812 dump_function (TDI_generic, decl);
814 cgraph_lower_function (node);
815 node->analyzed = true;
817 pop_cfun ();
818 current_function_decl = save;
821 /* Look for externally_visible and used attributes and mark cgraph nodes
822 accordingly.
824 We cannot mark the nodes at the point the attributes are processed (in
825 handle_*_attribute) because the copy of the declarations available at that
826 point may not be canonical. For example, in:
828 void f();
829 void f() __attribute__((used));
831 the declaration we see in handle_used_attribute will be the second
832 declaration -- but the front end will subsequently merge that declaration
833 with the original declaration and discard the second declaration.
835 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
837 void f() {}
838 void f() __attribute__((externally_visible));
840 is valid.
842 So, we walk the nodes at the end of the translation unit, applying the
843 attributes at that point. */
845 static void
846 process_function_and_variable_attributes (struct cgraph_node *first,
847 struct varpool_node *first_var)
849 struct cgraph_node *node;
850 struct varpool_node *vnode;
852 for (node = cgraph_nodes; node != first; node = node->next)
854 tree decl = node->decl;
855 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
857 mark_decl_referenced (decl);
858 if (node->local.finalized)
859 cgraph_mark_needed_node (node);
861 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
863 if (! TREE_PUBLIC (node->decl))
864 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
865 "%<externally_visible%>"
866 " attribute have effect only on public objects");
867 else
869 if (node->local.finalized)
870 cgraph_mark_needed_node (node);
871 node->local.externally_visible = true;
875 for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
877 tree decl = vnode->decl;
878 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
880 mark_decl_referenced (decl);
881 if (vnode->finalized)
882 varpool_mark_needed_node (vnode);
884 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
886 if (! TREE_PUBLIC (vnode->decl))
887 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
888 "%<externally_visible%>"
889 " attribute have effect only on public objects");
890 else
892 if (vnode->finalized)
893 varpool_mark_needed_node (vnode);
894 vnode->externally_visible = true;
900 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
901 each reachable functions) and build cgraph.
902 The function can be called multiple times after inserting new nodes
903 into beginning of queue. Just the new part of queue is re-scanned then. */
905 static void
906 cgraph_analyze_functions (void)
908 /* Keep track of already processed nodes when called multiple times for
909 intermodule optimization. */
910 static struct cgraph_node *first_analyzed;
911 struct cgraph_node *first_processed = first_analyzed;
912 static struct varpool_node *first_analyzed_var;
913 struct cgraph_node *node, *next;
915 process_function_and_variable_attributes (first_processed,
916 first_analyzed_var);
917 first_processed = cgraph_nodes;
918 first_analyzed_var = varpool_nodes;
919 varpool_analyze_pending_decls ();
920 if (cgraph_dump_file)
922 fprintf (cgraph_dump_file, "Initial entry points:");
923 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
924 if (node->needed)
925 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
926 fprintf (cgraph_dump_file, "\n");
928 cgraph_process_new_functions ();
930 /* Propagate reachability flag and lower representation of all reachable
931 functions. In the future, lowering will introduce new functions and
932 new entry points on the way (by template instantiation and virtual
933 method table generation for instance). */
934 while (cgraph_nodes_queue)
936 struct cgraph_edge *edge;
937 tree decl = cgraph_nodes_queue->decl;
939 node = cgraph_nodes_queue;
940 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
941 node->next_needed = NULL;
943 /* ??? It is possible to create extern inline function and later using
944 weak alias attribute to kill its body. See
945 gcc.c-torture/compile/20011119-1.c */
946 if (!DECL_STRUCT_FUNCTION (decl))
948 cgraph_reset_node (node);
949 continue;
952 gcc_assert (!node->analyzed && node->reachable);
953 cgraph_analyze_function (node);
955 for (edge = node->callees; edge; edge = edge->next_callee)
956 if (!edge->callee->reachable)
957 cgraph_mark_reachable_node (edge->callee);
959 /* If decl is a clone of an abstract function, mark that abstract
960 function so that we don't release its body. The DECL_INITIAL() of that
961 abstract function declaration will be later needed to output debug info. */
962 if (DECL_ABSTRACT_ORIGIN (decl))
964 struct cgraph_node *origin_node = cgraph_node (DECL_ABSTRACT_ORIGIN (decl));
965 origin_node->abstract_and_needed = true;
968 /* We finalize local static variables during constructing callgraph
969 edges. Process their attributes too. */
970 process_function_and_variable_attributes (first_processed,
971 first_analyzed_var);
972 first_processed = cgraph_nodes;
973 first_analyzed_var = varpool_nodes;
974 varpool_analyze_pending_decls ();
975 cgraph_process_new_functions ();
978 /* Collect entry points to the unit. */
979 if (cgraph_dump_file)
981 fprintf (cgraph_dump_file, "Unit entry points:");
982 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
983 if (node->needed)
984 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
985 fprintf (cgraph_dump_file, "\n\nInitial ");
986 dump_cgraph (cgraph_dump_file);
989 if (cgraph_dump_file)
990 fprintf (cgraph_dump_file, "\nReclaiming functions:");
992 for (node = cgraph_nodes; node != first_analyzed; node = next)
994 tree decl = node->decl;
995 next = node->next;
997 if (node->local.finalized && !gimple_has_body_p (decl))
998 cgraph_reset_node (node);
1000 if (!node->reachable && gimple_has_body_p (decl))
1002 if (cgraph_dump_file)
1003 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1004 cgraph_remove_node (node);
1005 continue;
1007 else
1008 node->next_needed = NULL;
1009 gcc_assert (!node->local.finalized || gimple_has_body_p (decl));
1010 gcc_assert (node->analyzed == node->local.finalized);
1012 if (cgraph_dump_file)
1014 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1015 dump_cgraph (cgraph_dump_file);
1017 first_analyzed = cgraph_nodes;
1018 ggc_collect ();
1021 /* Analyze the whole compilation unit once it is parsed completely. */
1023 void
1024 cgraph_finalize_compilation_unit (void)
1026 /* Do not skip analyzing the functions if there were errors, we
1027 miss diagnostics for following functions otherwise. */
1029 finalize_size_functions ();
1030 finish_aliases_1 ();
1032 if (!quiet_flag)
1034 fprintf (stderr, "\nAnalyzing compilation unit\n");
1035 fflush (stderr);
1038 timevar_push (TV_CGRAPH);
1039 cgraph_analyze_functions ();
1040 timevar_pop (TV_CGRAPH);
1042 cgraph_optimize ();
1046 /* Figure out what functions we want to assemble. */
1048 static void
1049 cgraph_mark_functions_to_output (void)
1051 struct cgraph_node *node;
1053 for (node = cgraph_nodes; node; node = node->next)
1055 tree decl = node->decl;
1056 struct cgraph_edge *e;
1058 gcc_assert (!node->process);
1060 for (e = node->callers; e; e = e->next_caller)
1061 if (e->inline_failed)
1062 break;
1064 /* We need to output all local functions that are used and not
1065 always inlined, as well as those that are reachable from
1066 outside the current compilation unit. */
1067 if (node->analyzed
1068 && !node->global.inlined_to
1069 && (node->needed
1070 || (e && node->reachable))
1071 && !TREE_ASM_WRITTEN (decl)
1072 && !DECL_EXTERNAL (decl))
1073 node->process = 1;
1074 else
1076 /* We should've reclaimed all functions that are not needed. */
1077 #ifdef ENABLE_CHECKING
1078 if (!node->global.inlined_to
1079 && gimple_has_body_p (decl)
1080 && !DECL_EXTERNAL (decl))
1082 dump_cgraph_node (stderr, node);
1083 internal_error ("failed to reclaim unneeded function");
1085 #endif
1086 gcc_assert (node->global.inlined_to
1087 || !gimple_has_body_p (decl)
1088 || DECL_EXTERNAL (decl));
1095 /* Expand function specified by NODE. */
1097 static void
1098 cgraph_expand_function (struct cgraph_node *node)
1100 tree decl = node->decl;
1102 /* We ought to not compile any inline clones. */
1103 gcc_assert (!node->global.inlined_to);
1105 announce_function (decl);
1106 node->process = 0;
1108 gcc_assert (node->lowered);
1110 /* Generate RTL for the body of DECL. */
1111 if (lang_hooks.callgraph.emit_associated_thunks
1112 && node->finalized_by_frontend)
1113 lang_hooks.callgraph.emit_associated_thunks (decl);
1114 tree_rest_of_compilation (decl);
1116 /* Make sure that BE didn't give up on compiling. */
1117 gcc_assert (TREE_ASM_WRITTEN (decl));
1118 current_function_decl = NULL;
1119 gcc_assert (!cgraph_preserve_function_body_p (decl));
1120 cgraph_release_function_body (node);
1121 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1122 points to the dead function body. */
1123 cgraph_node_remove_callees (node);
1125 cgraph_function_flags_ready = true;
1128 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1130 bool
1131 cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
1133 *reason = e->inline_failed;
1134 return !e->inline_failed;
1139 /* Expand all functions that must be output.
1141 Attempt to topologically sort the nodes so function is output when
1142 all called functions are already assembled to allow data to be
1143 propagated across the callgraph. Use a stack to get smaller distance
1144 between a function and its callees (later we may choose to use a more
1145 sophisticated algorithm for function reordering; we will likely want
1146 to use subsections to make the output functions appear in top-down
1147 order). */
1149 static void
1150 cgraph_expand_all_functions (void)
1152 struct cgraph_node *node;
1153 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1154 int order_pos, new_order_pos = 0;
1155 int i;
1157 order_pos = cgraph_postorder (order);
1158 gcc_assert (order_pos == cgraph_n_nodes);
1160 /* Garbage collector may remove inline clones we eliminate during
1161 optimization. So we must be sure to not reference them. */
1162 for (i = 0; i < order_pos; i++)
1163 if (order[i]->process)
1164 order[new_order_pos++] = order[i];
1166 for (i = new_order_pos - 1; i >= 0; i--)
1168 node = order[i];
1169 if (node->process)
1171 gcc_assert (node->reachable);
1172 node->process = 0;
1173 cgraph_expand_function (node);
1176 cgraph_process_new_functions ();
1178 free (order);
1182 /* This is used to sort the node types by the cgraph order number. */
1184 enum cgraph_order_sort_kind
1186 ORDER_UNDEFINED = 0,
1187 ORDER_FUNCTION,
1188 ORDER_VAR,
1189 ORDER_ASM
1192 struct cgraph_order_sort
1194 enum cgraph_order_sort_kind kind;
1195 union
1197 struct cgraph_node *f;
1198 struct varpool_node *v;
1199 struct cgraph_asm_node *a;
1200 } u;
1203 /* Output all functions, variables, and asm statements in the order
1204 according to their order fields, which is the order in which they
1205 appeared in the file. This implements -fno-toplevel-reorder. In
1206 this mode we may output functions and variables which don't really
1207 need to be output. */
1209 static void
1210 cgraph_output_in_order (void)
1212 int max;
1213 size_t size;
1214 struct cgraph_order_sort *nodes;
1215 int i;
1216 struct cgraph_node *pf;
1217 struct varpool_node *pv;
1218 struct cgraph_asm_node *pa;
1220 max = cgraph_order;
1221 size = max * sizeof (struct cgraph_order_sort);
1222 nodes = (struct cgraph_order_sort *) alloca (size);
1223 memset (nodes, 0, size);
1225 varpool_analyze_pending_decls ();
1227 for (pf = cgraph_nodes; pf; pf = pf->next)
1229 if (pf->process)
1231 i = pf->order;
1232 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1233 nodes[i].kind = ORDER_FUNCTION;
1234 nodes[i].u.f = pf;
1238 for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1240 i = pv->order;
1241 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1242 nodes[i].kind = ORDER_VAR;
1243 nodes[i].u.v = pv;
1246 for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1248 i = pa->order;
1249 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1250 nodes[i].kind = ORDER_ASM;
1251 nodes[i].u.a = pa;
1254 /* In toplevel reorder mode we output all statics; mark them as needed. */
1255 for (i = 0; i < max; ++i)
1257 if (nodes[i].kind == ORDER_VAR)
1259 varpool_mark_needed_node (nodes[i].u.v);
1262 varpool_empty_needed_queue ();
1264 for (i = 0; i < max; ++i)
1266 switch (nodes[i].kind)
1268 case ORDER_FUNCTION:
1269 nodes[i].u.f->process = 0;
1270 cgraph_expand_function (nodes[i].u.f);
1271 break;
1273 case ORDER_VAR:
1274 varpool_assemble_decl (nodes[i].u.v);
1275 break;
1277 case ORDER_ASM:
1278 assemble_asm (nodes[i].u.a->asm_str);
1279 break;
1281 case ORDER_UNDEFINED:
1282 break;
1284 default:
1285 gcc_unreachable ();
1289 cgraph_asm_nodes = NULL;
1292 /* Return true when function body of DECL still needs to be kept around
1293 for later re-use. */
1294 bool
1295 cgraph_preserve_function_body_p (tree decl)
1297 struct cgraph_node *node;
1299 gcc_assert (cgraph_global_info_ready);
1300 /* Look if there is any clone around. */
1301 node = cgraph_node (decl);
1302 if (node->clones)
1303 return true;
1304 return false;
1307 static void
1308 ipa_passes (void)
1310 set_cfun (NULL);
1311 current_function_decl = NULL;
1312 gimple_register_cfg_hooks ();
1313 bitmap_obstack_initialize (NULL);
1314 execute_ipa_pass_list (all_ipa_passes);
1316 /* Generate coverage variables and constructors. */
1317 coverage_finish ();
1319 /* Process new functions added. */
1320 set_cfun (NULL);
1321 current_function_decl = NULL;
1322 cgraph_process_new_functions ();
1324 bitmap_obstack_release (NULL);
1327 /* Perform simple optimizations based on callgraph. */
1329 static void
1330 cgraph_optimize (void)
1332 if (errorcount || sorrycount)
1333 return;
1335 #ifdef ENABLE_CHECKING
1336 verify_cgraph ();
1337 #endif
1339 /* Call functions declared with the "constructor" or "destructor"
1340 attribute. */
1341 cgraph_build_cdtor_fns ();
1343 /* Frontend may output common variables after the unit has been finalized.
1344 It is safe to deal with them here as they are always zero initialized. */
1345 varpool_analyze_pending_decls ();
1346 cgraph_analyze_functions ();
1348 timevar_push (TV_CGRAPHOPT);
1349 if (pre_ipa_mem_report)
1351 fprintf (stderr, "Memory consumption before IPA\n");
1352 dump_memory_report (false);
1354 if (!quiet_flag)
1355 fprintf (stderr, "Performing interprocedural optimizations\n");
1356 cgraph_state = CGRAPH_STATE_IPA;
1358 /* Don't run the IPA passes if there was any error or sorry messages. */
1359 if (errorcount == 0 && sorrycount == 0)
1360 ipa_passes ();
1362 /* This pass remove bodies of extern inline functions we never inlined.
1363 Do this later so other IPA passes see what is really going on. */
1364 cgraph_remove_unreachable_nodes (false, dump_file);
1365 cgraph_global_info_ready = true;
1366 if (cgraph_dump_file)
1368 fprintf (cgraph_dump_file, "Optimized ");
1369 dump_cgraph (cgraph_dump_file);
1370 dump_varpool (cgraph_dump_file);
1372 if (post_ipa_mem_report)
1374 fprintf (stderr, "Memory consumption after IPA\n");
1375 dump_memory_report (false);
1377 timevar_pop (TV_CGRAPHOPT);
1379 /* Output everything. */
1380 if (!quiet_flag)
1381 fprintf (stderr, "Assembling functions:\n");
1382 #ifdef ENABLE_CHECKING
1383 verify_cgraph ();
1384 #endif
1386 cgraph_materialize_all_clones ();
1387 cgraph_mark_functions_to_output ();
1389 cgraph_state = CGRAPH_STATE_EXPANSION;
1390 if (!flag_toplevel_reorder)
1391 cgraph_output_in_order ();
1392 else
1394 cgraph_output_pending_asms ();
1396 cgraph_expand_all_functions ();
1397 varpool_remove_unreferenced_decls ();
1399 varpool_assemble_pending_decls ();
1401 cgraph_process_new_functions ();
1402 cgraph_state = CGRAPH_STATE_FINISHED;
1404 if (cgraph_dump_file)
1406 fprintf (cgraph_dump_file, "\nFinal ");
1407 dump_cgraph (cgraph_dump_file);
1409 #ifdef ENABLE_CHECKING
1410 verify_cgraph ();
1411 /* Double check that all inline clones are gone and that all
1412 function bodies have been released from memory. */
1413 if (!(sorrycount || errorcount))
1415 struct cgraph_node *node;
1416 bool error_found = false;
1418 for (node = cgraph_nodes; node; node = node->next)
1419 if (node->analyzed
1420 && (node->global.inlined_to
1421 || gimple_has_body_p (node->decl)))
1423 error_found = true;
1424 dump_cgraph_node (stderr, node);
1426 if (error_found)
1427 internal_error ("nodes with unreleased memory found");
1429 #endif
1431 /* Generate and emit a static constructor or destructor. WHICH must
1432 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1433 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1434 initialization priority for this constructor or destructor. */
1436 void
1437 cgraph_build_static_cdtor (char which, tree body, int priority)
1439 static int counter = 0;
1440 char which_buf[16];
1441 tree decl, name, resdecl;
1443 /* The priority is encoded in the constructor or destructor name.
1444 collect2 will sort the names and arrange that they are called at
1445 program startup. */
1446 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1447 name = get_file_function_name (which_buf);
1449 decl = build_decl (input_location, FUNCTION_DECL, name,
1450 build_function_type (void_type_node, void_list_node));
1451 current_function_decl = decl;
1453 resdecl = build_decl (input_location,
1454 RESULT_DECL, NULL_TREE, void_type_node);
1455 DECL_ARTIFICIAL (resdecl) = 1;
1456 DECL_RESULT (decl) = resdecl;
1457 DECL_CONTEXT (resdecl) = decl;
1459 allocate_struct_function (decl, false);
1461 TREE_STATIC (decl) = 1;
1462 TREE_USED (decl) = 1;
1463 DECL_ARTIFICIAL (decl) = 1;
1464 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1465 DECL_SAVED_TREE (decl) = body;
1466 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1467 DECL_UNINLINABLE (decl) = 1;
1469 DECL_INITIAL (decl) = make_node (BLOCK);
1470 TREE_USED (DECL_INITIAL (decl)) = 1;
1472 DECL_SOURCE_LOCATION (decl) = input_location;
1473 cfun->function_end_locus = input_location;
1475 switch (which)
1477 case 'I':
1478 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1479 decl_init_priority_insert (decl, priority);
1480 break;
1481 case 'D':
1482 DECL_STATIC_DESTRUCTOR (decl) = 1;
1483 decl_fini_priority_insert (decl, priority);
1484 break;
1485 default:
1486 gcc_unreachable ();
1489 gimplify_function_tree (decl);
1491 cgraph_add_new_function (decl, false);
1492 cgraph_mark_needed_node (cgraph_node (decl));
1493 set_cfun (NULL);
1496 void
1497 init_cgraph (void)
1499 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1502 /* The edges representing the callers of the NEW_VERSION node were
1503 fixed by cgraph_function_versioning (), now the call_expr in their
1504 respective tree code should be updated to call the NEW_VERSION. */
1506 static void
1507 update_call_expr (struct cgraph_node *new_version)
1509 struct cgraph_edge *e;
1511 gcc_assert (new_version);
1513 /* Update the call expr on the edges to call the new version. */
1514 for (e = new_version->callers; e; e = e->next_caller)
1516 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
1517 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
1518 /* Update EH information too, just in case. */
1519 if (!stmt_could_throw_p (e->call_stmt)
1520 && lookup_stmt_eh_region_fn (inner_function, e->call_stmt))
1521 remove_stmt_from_eh_region_fn (inner_function, e->call_stmt);
1526 /* Create a new cgraph node which is the new version of
1527 OLD_VERSION node. REDIRECT_CALLERS holds the callers
1528 edges which should be redirected to point to
1529 NEW_VERSION. ALL the callees edges of OLD_VERSION
1530 are cloned to the new version node. Return the new
1531 version node. */
1533 static struct cgraph_node *
1534 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
1535 tree new_decl,
1536 VEC(cgraph_edge_p,heap) *redirect_callers)
1538 struct cgraph_node *new_version;
1539 struct cgraph_edge *e, *new_e;
1540 struct cgraph_edge *next_callee;
1541 unsigned i;
1543 gcc_assert (old_version);
1545 new_version = cgraph_node (new_decl);
1547 new_version->analyzed = true;
1548 new_version->local = old_version->local;
1549 new_version->global = old_version->global;
1550 new_version->rtl = new_version->rtl;
1551 new_version->reachable = true;
1552 new_version->count = old_version->count;
1554 /* Clone the old node callees. Recursive calls are
1555 also cloned. */
1556 for (e = old_version->callees;e; e=e->next_callee)
1558 new_e = cgraph_clone_edge (e, new_version, e->call_stmt, 0, e->frequency,
1559 e->loop_nest, true);
1560 new_e->count = e->count;
1562 /* Fix recursive calls.
1563 If OLD_VERSION has a recursive call after the
1564 previous edge cloning, the new version will have an edge
1565 pointing to the old version, which is wrong;
1566 Redirect it to point to the new version. */
1567 for (e = new_version->callees ; e; e = next_callee)
1569 next_callee = e->next_callee;
1570 if (e->callee == old_version)
1571 cgraph_redirect_edge_callee (e, new_version);
1573 if (!next_callee)
1574 break;
1576 for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1578 /* Redirect calls to the old version node to point to its new
1579 version. */
1580 cgraph_redirect_edge_callee (e, new_version);
1583 return new_version;
1586 /* Perform function versioning.
1587 Function versioning includes copying of the tree and
1588 a callgraph update (creating a new cgraph node and updating
1589 its callees and callers).
1591 REDIRECT_CALLERS varray includes the edges to be redirected
1592 to the new version.
1594 TREE_MAP is a mapping of tree nodes we want to replace with
1595 new ones (according to results of prior analysis).
1596 OLD_VERSION_NODE is the node that is versioned.
1597 It returns the new version's cgraph node.
1598 ARGS_TO_SKIP lists arguments to be omitted from functions
1601 struct cgraph_node *
1602 cgraph_function_versioning (struct cgraph_node *old_version_node,
1603 VEC(cgraph_edge_p,heap) *redirect_callers,
1604 VEC (ipa_replace_map_p,gc)* tree_map,
1605 bitmap args_to_skip)
1607 tree old_decl = old_version_node->decl;
1608 struct cgraph_node *new_version_node = NULL;
1609 tree new_decl;
1611 if (!tree_versionable_function_p (old_decl))
1612 return NULL;
1614 /* Make a new FUNCTION_DECL tree node for the
1615 new version. */
1616 if (!args_to_skip)
1617 new_decl = copy_node (old_decl);
1618 else
1619 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
1621 /* Create the new version's call-graph node.
1622 and update the edges of the new node. */
1623 new_version_node =
1624 cgraph_copy_node_for_versioning (old_version_node, new_decl,
1625 redirect_callers);
1627 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1628 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip);
1630 /* Update the new version's properties.
1631 Make The new version visible only within this translation unit. Make sure
1632 that is not weak also.
1633 ??? We cannot use COMDAT linkage because there is no
1634 ABI support for this. */
1635 DECL_EXTERNAL (new_version_node->decl) = 0;
1636 DECL_COMDAT_GROUP (new_version_node->decl) = NULL_TREE;
1637 TREE_PUBLIC (new_version_node->decl) = 0;
1638 DECL_COMDAT (new_version_node->decl) = 0;
1639 DECL_WEAK (new_version_node->decl) = 0;
1640 DECL_VIRTUAL_P (new_version_node->decl) = 0;
1641 new_version_node->local.externally_visible = 0;
1642 new_version_node->local.local = 1;
1643 new_version_node->lowered = true;
1645 /* Update the call_expr on the edges to call the new version node. */
1646 update_call_expr (new_version_node);
1648 cgraph_call_function_insertion_hooks (new_version_node);
1649 return new_version_node;
1652 /* Produce separate function body for inline clones so the offline copy can be
1653 modified without affecting them. */
1654 struct cgraph_node *
1655 save_inline_function_body (struct cgraph_node *node)
1657 struct cgraph_node *first_clone, *n;
1659 gcc_assert (node == cgraph_node (node->decl));
1661 cgraph_lower_function (node);
1663 first_clone = node->clones;
1665 first_clone->decl = copy_node (node->decl);
1666 cgraph_insert_node_to_hashtable (first_clone);
1667 gcc_assert (first_clone == cgraph_node (first_clone->decl));
1668 if (first_clone->next_sibling_clone)
1670 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
1671 n->clone_of = first_clone;
1672 n->clone_of = first_clone;
1673 n->next_sibling_clone = first_clone->clones;
1674 if (first_clone->clones)
1675 first_clone->clones->prev_sibling_clone = n;
1676 first_clone->clones = first_clone->next_sibling_clone;
1677 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
1678 first_clone->next_sibling_clone = NULL;
1679 gcc_assert (!first_clone->prev_sibling_clone);
1681 first_clone->clone_of = NULL;
1682 node->clones = NULL;
1684 if (first_clone->clones)
1685 for (n = first_clone->clones; n != first_clone;)
1687 gcc_assert (n->decl == node->decl);
1688 n->decl = first_clone->decl;
1689 if (n->clones)
1690 n = n->clones;
1691 else if (n->next_sibling_clone)
1692 n = n->next_sibling_clone;
1693 else
1695 while (n != first_clone && !n->next_sibling_clone)
1696 n = n->clone_of;
1697 if (n != first_clone)
1698 n = n->next_sibling_clone;
1702 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1703 tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL);
1705 DECL_EXTERNAL (first_clone->decl) = 0;
1706 DECL_COMDAT_GROUP (first_clone->decl) = NULL_TREE;
1707 TREE_PUBLIC (first_clone->decl) = 0;
1708 DECL_COMDAT (first_clone->decl) = 0;
1709 VEC_free (ipa_opt_pass, heap,
1710 DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply);
1711 DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply = NULL;
1713 #ifdef ENABLE_CHECKING
1714 verify_cgraph_node (first_clone);
1715 #endif
1716 return first_clone;
1719 /* Given virtual clone, turn it into actual clone. */
1720 static void
1721 cgraph_materialize_clone (struct cgraph_node *node)
1723 bitmap_obstack_initialize (NULL);
1724 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1725 tree_function_versioning (node->clone_of->decl, node->decl,
1726 node->clone.tree_map, true,
1727 node->clone.args_to_skip);
1728 if (cgraph_dump_file)
1730 dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags);
1731 dump_function_to_file (node->decl, cgraph_dump_file, dump_flags);
1734 /* Function is no longer clone. */
1735 if (node->next_sibling_clone)
1736 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1737 if (node->prev_sibling_clone)
1738 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1739 else
1740 node->clone_of->clones = node->next_sibling_clone;
1741 node->next_sibling_clone = NULL;
1742 node->prev_sibling_clone = NULL;
1743 node->clone_of = NULL;
1744 bitmap_obstack_release (NULL);
1747 /* Once all functions from compilation unit are in memory, produce all clones
1748 and update all calls.
1749 We might also do this on demand if we don't want to bring all functions to
1750 memory prior compilation, but current WHOPR implementation does that and it is
1751 is bit easier to keep everything right in this order. */
1752 void
1753 cgraph_materialize_all_clones (void)
1755 struct cgraph_node *node;
1756 bool stabilized = false;
1758 if (cgraph_dump_file)
1759 fprintf (cgraph_dump_file, "Materializing clones\n");
1760 #ifdef ENABLE_CHECKING
1761 verify_cgraph ();
1762 #endif
1764 /* We can also do topological order, but number of iterations should be
1765 bounded by number of IPA passes since single IPA pass is probably not
1766 going to create clones of clones it created itself. */
1767 while (!stabilized)
1769 stabilized = true;
1770 for (node = cgraph_nodes; node; node = node->next)
1772 if (node->clone_of && node->decl != node->clone_of->decl
1773 && !gimple_has_body_p (node->decl))
1775 if (gimple_has_body_p (node->clone_of->decl))
1777 if (cgraph_dump_file)
1779 fprintf (cgraph_dump_file, "clonning %s to %s\n",
1780 cgraph_node_name (node->clone_of),
1781 cgraph_node_name (node));
1782 if (node->clone.tree_map)
1784 unsigned int i;
1785 fprintf (cgraph_dump_file, " replace map: ");
1786 for (i = 0; i < VEC_length (ipa_replace_map_p,
1787 node->clone.tree_map);
1788 i++)
1790 struct ipa_replace_map *replace_info;
1791 replace_info = VEC_index (ipa_replace_map_p,
1792 node->clone.tree_map,
1794 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
1795 fprintf (cgraph_dump_file, " -> ");
1796 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
1797 fprintf (cgraph_dump_file, "%s%s;",
1798 replace_info->replace_p ? "(replace)":"",
1799 replace_info->ref_p ? "(ref)":"");
1801 fprintf (cgraph_dump_file, "\n");
1803 if (node->clone.args_to_skip)
1805 fprintf (cgraph_dump_file, " args_to_skip: ");
1806 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
1808 if (node->clone.args_to_skip)
1810 fprintf (cgraph_dump_file, " combined_args_to_skip:");
1811 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
1814 cgraph_materialize_clone (node);
1816 else
1817 stabilized = false;
1821 if (cgraph_dump_file)
1822 fprintf (cgraph_dump_file, "Updating call sites\n");
1823 for (node = cgraph_nodes; node; node = node->next)
1824 if (node->analyzed && gimple_has_body_p (node->decl)
1825 && (!node->clone_of || node->clone_of->decl != node->decl))
1827 struct cgraph_edge *e;
1829 current_function_decl = node->decl;
1830 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1831 for (e = node->callees; e; e = e->next_callee)
1833 tree decl = gimple_call_fndecl (e->call_stmt);
1834 /* When function gets inlined, indirect inlining might've invented
1835 new edge for orginally indirect stmt. Since we are not
1836 preserving clones in the original form, we must not update here
1837 since other inline clones don't need to contain call to the same
1838 call. Inliner will do the substitution for us later. */
1839 if (decl && decl != e->callee->decl)
1841 gimple new_stmt;
1842 gimple_stmt_iterator gsi;
1844 if (cgraph_dump_file)
1846 fprintf (cgraph_dump_file, "updating call of %s in %s:",
1847 cgraph_node_name (node),
1848 cgraph_node_name (e->callee));
1849 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1852 if (e->callee->clone.combined_args_to_skip)
1853 new_stmt = gimple_call_copy_skip_args (e->call_stmt,
1854 e->callee->clone.combined_args_to_skip);
1855 else
1856 new_stmt = e->call_stmt;
1857 if (gimple_vdef (new_stmt)
1858 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1859 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1860 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1862 gsi = gsi_for_stmt (e->call_stmt);
1863 gsi_replace (&gsi, new_stmt, true);
1865 /* Update EH information too, just in case. */
1866 if (!stmt_could_throw_p (new_stmt)
1867 && lookup_stmt_eh_region (new_stmt))
1868 remove_stmt_from_eh_region (new_stmt);
1870 cgraph_set_call_stmt_including_clones (node, e->call_stmt, new_stmt);
1872 if (cgraph_dump_file)
1874 fprintf (cgraph_dump_file, " updated to:");
1875 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1879 pop_cfun ();
1880 current_function_decl = NULL;
1881 #ifdef ENABLE_CHECKING
1882 verify_cgraph_node (node);
1883 #endif
1885 #ifdef ENABLE_CHECKING
1886 verify_cgraph ();
1887 #endif
1888 cgraph_remove_unreachable_nodes (false, cgraph_dump_file);
1891 #include "gt-cgraphunit.h"