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
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
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
38 - varpool_finalize_variable
40 This function has same behavior as the above but is used for static
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)
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. */
111 #include "coretypes.h"
115 #include "tree-flow.h"
116 #include "tree-inline.h"
117 #include "langhooks.h"
118 #include "pointer-set.h"
125 #include "diagnostic.h"
130 #include "function.h"
131 #include "ipa-prop.h"
133 #include "tree-iterator.h"
134 #include "tree-pass.h"
135 #include "tree-dump.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_analyze_function (struct cgraph_node
*);
145 static FILE *cgraph_dump_file
;
147 /* A vector of FUNCTION_DECLs declared as static constructors. */
148 static GTY (()) VEC(tree
, gc
) *static_ctors
;
149 /* A vector of FUNCTION_DECLs declared as static destructors. */
150 static GTY (()) VEC(tree
, gc
) *static_dtors
;
152 /* When target does not have ctors and dtors, we call all constructor
153 and destructor by special initialization/destruction function
154 recognized by collect2.
156 When we are going to build this function, collect all constructors and
157 destructors and turn them into normal functions. */
160 record_cdtor_fn (tree fndecl
)
162 struct cgraph_node
*node
;
163 if (targetm
.have_ctors_dtors
164 || (!DECL_STATIC_CONSTRUCTOR (fndecl
)
165 && !DECL_STATIC_DESTRUCTOR (fndecl
)))
168 if (DECL_STATIC_CONSTRUCTOR (fndecl
))
170 VEC_safe_push (tree
, gc
, static_ctors
, fndecl
);
171 DECL_STATIC_CONSTRUCTOR (fndecl
) = 0;
173 if (DECL_STATIC_DESTRUCTOR (fndecl
))
175 VEC_safe_push (tree
, gc
, static_dtors
, fndecl
);
176 DECL_STATIC_DESTRUCTOR (fndecl
) = 0;
178 node
= cgraph_node (fndecl
);
179 node
->local
.disregard_inline_limits
= 1;
180 cgraph_mark_reachable_node (node
);
183 /* Define global constructors/destructor functions for the CDTORS, of
184 which they are LEN. The CDTORS are sorted by initialization
185 priority. If CTOR_P is true, these are constructors; otherwise,
186 they are destructors. */
189 build_cdtor (bool ctor_p
, tree
*cdtors
, size_t len
)
198 priority_type priority
;
202 /* Find the next batch of constructors/destructors with the same
203 initialization priority. */
208 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
211 else if (p
!= priority
)
213 append_to_statement_list (build_function_call_expr (UNKNOWN_LOCATION
,
219 gcc_assert (body
!= NULL_TREE
);
220 /* Generate a function to call all the function of like
222 cgraph_build_static_cdtor (ctor_p
? 'I' : 'D', body
, priority
);
226 /* Comparison function for qsort. P1 and P2 are actually of type
227 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
228 used to determine the sort order. */
231 compare_ctor (const void *p1
, const void *p2
)
238 f1
= *(const tree
*)p1
;
239 f2
= *(const tree
*)p2
;
240 priority1
= DECL_INIT_PRIORITY (f1
);
241 priority2
= DECL_INIT_PRIORITY (f2
);
243 if (priority1
< priority2
)
245 else if (priority1
> priority2
)
248 /* Ensure a stable sort. */
249 return (const tree
*)p1
- (const tree
*)p2
;
252 /* Comparison function for qsort. P1 and P2 are actually of type
253 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
254 used to determine the sort order. */
257 compare_dtor (const void *p1
, const void *p2
)
264 f1
= *(const tree
*)p1
;
265 f2
= *(const tree
*)p2
;
266 priority1
= DECL_FINI_PRIORITY (f1
);
267 priority2
= DECL_FINI_PRIORITY (f2
);
269 if (priority1
< priority2
)
271 else if (priority1
> priority2
)
274 /* Ensure a stable sort. */
275 return (const tree
*)p1
- (const tree
*)p2
;
278 /* Generate functions to call static constructors and destructors
279 for targets that do not support .ctors/.dtors sections. These
280 functions have magic names which are detected by collect2. */
283 cgraph_build_cdtor_fns (void)
285 if (!VEC_empty (tree
, static_ctors
))
287 gcc_assert (!targetm
.have_ctors_dtors
);
288 qsort (VEC_address (tree
, static_ctors
),
289 VEC_length (tree
, static_ctors
),
292 build_cdtor (/*ctor_p=*/true,
293 VEC_address (tree
, static_ctors
),
294 VEC_length (tree
, static_ctors
));
295 VEC_truncate (tree
, static_ctors
, 0);
298 if (!VEC_empty (tree
, static_dtors
))
300 gcc_assert (!targetm
.have_ctors_dtors
);
301 qsort (VEC_address (tree
, static_dtors
),
302 VEC_length (tree
, static_dtors
),
305 build_cdtor (/*ctor_p=*/false,
306 VEC_address (tree
, static_dtors
),
307 VEC_length (tree
, static_dtors
));
308 VEC_truncate (tree
, static_dtors
, 0);
312 /* Determine if function DECL is needed. That is, visible to something
313 either outside this translation unit, something magic in the system
317 cgraph_decide_is_function_needed (struct cgraph_node
*node
, tree decl
)
319 /* If the user told us it is used, then it must be so. */
320 if (node
->local
.externally_visible
)
323 /* ??? If the assembler name is set by hand, it is possible to assemble
324 the name later after finalizing the function and the fact is noticed
325 in assemble_name then. This is arguably a bug. */
326 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
327 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
330 /* With -fkeep-inline-functions we are keeping all inline functions except
331 for extern inline ones. */
332 if (flag_keep_inline_functions
333 && DECL_DECLARED_INLINE_P (decl
)
334 && !DECL_EXTERNAL (decl
)
335 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
)))
338 /* If we decided it was needed before, but at the time we didn't have
339 the body of the function available, then it's still needed. We have
340 to go back and re-check its dependencies now. */
344 /* Externally visible functions must be output. The exception is
345 COMDAT functions that must be output only when they are needed.
347 When not optimizing, also output the static functions. (see
348 PR24561), but don't do so for always_inline functions, functions
349 declared inline and nested functions. These was optimized out
350 in the original implementation and it is unclear whether we want
351 to change the behavior here. */
352 if (((TREE_PUBLIC (decl
)
353 || (!optimize
&& !node
->local
.disregard_inline_limits
354 && !DECL_DECLARED_INLINE_P (decl
)
356 && !flag_whole_program
359 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
362 /* Constructors and destructors are reachable from the runtime by
364 if (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
))
370 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
371 functions into callgraph in a way so they look like ordinary reachable
372 functions inserted into callgraph already at construction time. */
375 cgraph_process_new_functions (void)
379 struct cgraph_node
*node
;
381 /* Note that this queue may grow as its being processed, as the new
382 functions may generate new ones. */
383 while (cgraph_new_nodes
)
385 node
= cgraph_new_nodes
;
387 cgraph_new_nodes
= cgraph_new_nodes
->next_needed
;
388 switch (cgraph_state
)
390 case CGRAPH_STATE_CONSTRUCTION
:
391 /* At construction time we just need to finalize function and move
392 it into reachable functions list. */
394 node
->next_needed
= NULL
;
395 cgraph_finalize_function (fndecl
, false);
396 cgraph_mark_reachable_node (node
);
400 case CGRAPH_STATE_IPA
:
401 case CGRAPH_STATE_IPA_SSA
:
402 /* When IPA optimization already started, do all essential
403 transformations that has been already performed on the whole
404 cgraph but not on this function. */
406 gimple_register_cfg_hooks ();
408 cgraph_analyze_function (node
);
409 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
410 current_function_decl
= fndecl
;
411 compute_inline_parameters (node
);
412 if ((cgraph_state
== CGRAPH_STATE_IPA_SSA
413 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
414 /* When not optimizing, be sure we run early local passes anyway
417 execute_pass_list (pass_early_local_passes
.pass
.sub
);
418 free_dominance_info (CDI_POST_DOMINATORS
);
419 free_dominance_info (CDI_DOMINATORS
);
421 current_function_decl
= NULL
;
424 case CGRAPH_STATE_EXPANSION
:
425 /* Functions created during expansion shall be compiled
428 cgraph_expand_function (node
);
435 cgraph_call_function_insertion_hooks (node
);
440 /* As an GCC extension we allow redefinition of the function. The
441 semantics when both copies of bodies differ is not well defined.
442 We replace the old body with new body so in unit at a time mode
443 we always use new body, while in normal mode we may end up with
444 old body inlined into some functions and new body expanded and
447 ??? It may make more sense to use one body for inlining and other
448 body for expanding the function but this is difficult to do. */
451 cgraph_reset_node (struct cgraph_node
*node
)
453 /* If node->process is set, then we have already begun whole-unit analysis.
454 This is *not* testing for whether we've already emitted the function.
455 That case can be sort-of legitimately seen with real function redefinition
456 errors. I would argue that the front end should never present us with
457 such a case, but don't enforce that for now. */
458 gcc_assert (!node
->process
);
460 /* Reset our data structures so we can analyze the function again. */
461 memset (&node
->local
, 0, sizeof (node
->local
));
462 memset (&node
->global
, 0, sizeof (node
->global
));
463 memset (&node
->rtl
, 0, sizeof (node
->rtl
));
464 node
->analyzed
= false;
465 node
->local
.redefined_extern_inline
= true;
466 node
->local
.finalized
= false;
468 cgraph_node_remove_callees (node
);
470 /* We may need to re-queue the node for assembling in case
471 we already proceeded it and ignored as not needed or got
472 a re-declaration in IMA mode. */
475 struct cgraph_node
*n
;
477 for (n
= cgraph_nodes_queue
; n
; n
= n
->next_needed
)
486 cgraph_lower_function (struct cgraph_node
*node
)
492 lower_nested_functions (node
->decl
);
493 gcc_assert (!node
->nested
);
495 tree_lowering_passes (node
->decl
);
496 node
->lowered
= true;
499 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
500 logic in effect. If NESTED is true, then our caller cannot stand to have
501 the garbage collector run at the moment. We would need to either create
502 a new GC context, or just not compile right now. */
505 cgraph_finalize_function (tree decl
, bool nested
)
507 struct cgraph_node
*node
= cgraph_node (decl
);
509 if (node
->local
.finalized
)
510 cgraph_reset_node (node
);
512 node
->pid
= cgraph_max_pid
++;
513 notice_global_symbol (decl
);
514 node
->local
.finalized
= true;
515 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
516 node
->finalized_by_frontend
= true;
517 record_cdtor_fn (node
->decl
);
519 if (cgraph_decide_is_function_needed (node
, decl
))
520 cgraph_mark_needed_node (node
);
522 /* Since we reclaim unreachable nodes at the end of every language
523 level unit, we need to be conservative about possible entry points
525 if ((TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
)))
526 cgraph_mark_reachable_node (node
);
528 /* If we've not yet emitted decl, tell the debug info about it. */
529 if (!TREE_ASM_WRITTEN (decl
))
530 (*debug_hooks
->deferred_inline_function
) (decl
);
532 /* Possibly warn about unused parameters. */
533 if (warn_unused_parameter
)
534 do_warn_unused_parameter (decl
);
540 /* C99 extern inline keywords allow changing of declaration after function
541 has been finalized. We need to re-decide if we want to mark the function as
545 cgraph_mark_if_needed (tree decl
)
547 struct cgraph_node
*node
= cgraph_node (decl
);
548 if (node
->local
.finalized
&& cgraph_decide_is_function_needed (node
, decl
))
549 cgraph_mark_needed_node (node
);
552 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
554 clone_of_p (struct cgraph_node
*node
, struct cgraph_node
*node2
)
556 while (node
!= node2
&& node2
)
557 node2
= node2
->clone_of
;
558 return node2
!= NULL
;
561 /* Verify cgraph nodes of given cgraph node. */
563 verify_cgraph_node (struct cgraph_node
*node
)
565 struct cgraph_edge
*e
;
566 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (node
->decl
);
567 struct function
*saved_cfun
= cfun
;
568 basic_block this_block
;
569 gimple_stmt_iterator gsi
;
570 bool error_found
= false;
572 if (errorcount
|| sorrycount
)
575 timevar_push (TV_CGRAPH_VERIFY
);
576 /* debug_generic_stmt needs correct cfun */
577 set_cfun (this_cfun
);
578 for (e
= node
->callees
; e
; e
= e
->next_callee
)
581 error ("aux field set for edge %s->%s",
582 identifier_to_locale (cgraph_node_name (e
->caller
)),
583 identifier_to_locale (cgraph_node_name (e
->callee
)));
588 error ("Execution count is negative");
591 if (node
->global
.inlined_to
&& node
->local
.externally_visible
)
593 error ("Externally visible inline clone");
596 if (node
->global
.inlined_to
&& node
->address_taken
)
598 error ("Inline clone with address taken");
601 if (node
->global
.inlined_to
&& node
->needed
)
603 error ("Inline clone is needed");
606 for (e
= node
->callers
; e
; e
= e
->next_caller
)
610 error ("caller edge count is negative");
613 if (e
->frequency
< 0)
615 error ("caller edge frequency is negative");
618 if (e
->frequency
> CGRAPH_FREQ_MAX
)
620 error ("caller edge frequency is too large");
623 if (gimple_has_body_p (e
->caller
->decl
)
624 && !e
->caller
->global
.inlined_to
626 != compute_call_stmt_bb_frequency (e
->caller
->decl
,
627 gimple_bb (e
->call_stmt
))))
629 error ("caller edge frequency %i does not match BB freqency %i",
631 compute_call_stmt_bb_frequency (e
->caller
->decl
,
632 gimple_bb (e
->call_stmt
)));
635 if (!e
->inline_failed
)
637 if (node
->global
.inlined_to
638 != (e
->caller
->global
.inlined_to
639 ? e
->caller
->global
.inlined_to
: e
->caller
))
641 error ("inlined_to pointer is wrong");
644 if (node
->callers
->next_caller
)
646 error ("multiple inline callers");
651 if (node
->global
.inlined_to
)
653 error ("inlined_to pointer set for noninline callers");
657 if (!node
->callers
&& node
->global
.inlined_to
)
659 error ("inlined_to pointer is set but no predecessors found");
662 if (node
->global
.inlined_to
== node
)
664 error ("inlined_to pointer refers to itself");
668 if (!cgraph_node (node
->decl
))
670 error ("node not found in cgraph_hash");
676 struct cgraph_node
*n
;
677 for (n
= node
->clone_of
->clones
; n
; n
= n
->next_sibling_clone
)
682 error ("node has wrong clone_of");
688 struct cgraph_node
*n
;
689 for (n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
690 if (n
->clone_of
!= node
)
694 error ("node has wrong clone list");
698 if ((node
->prev_sibling_clone
|| node
->next_sibling_clone
) && !node
->clone_of
)
700 error ("node is in clone list but it is not clone");
703 if (!node
->prev_sibling_clone
&& node
->clone_of
&& node
->clone_of
->clones
!= node
)
705 error ("node has wrong prev_clone pointer");
708 if (node
->prev_sibling_clone
&& node
->prev_sibling_clone
->next_sibling_clone
!= node
)
710 error ("double linked list of clones corrupted");
714 if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
715 && !TREE_ASM_WRITTEN (node
->decl
)
716 && (!DECL_EXTERNAL (node
->decl
) || node
->global
.inlined_to
)
721 /* The nodes we're interested in are never shared, so walk
722 the tree ignoring duplicates. */
723 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
724 /* Reach the trees by walking over the CFG, and note the
725 enclosing basic-blocks in the call edges. */
726 FOR_EACH_BB_FN (this_block
, this_cfun
)
727 for (gsi
= gsi_start_bb (this_block
);
731 gimple stmt
= gsi_stmt (gsi
);
733 if (is_gimple_call (stmt
) && (decl
= gimple_call_fndecl (stmt
)))
735 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
740 error ("shared call_stmt:");
741 debug_gimple_stmt (stmt
);
744 if (!clone_of_p (cgraph_node (decl
), e
->callee
)
745 && !e
->callee
->global
.inlined_to
)
747 error ("edge points to wrong declaration:");
748 debug_tree (e
->callee
->decl
);
749 fprintf (stderr
," Instead of:");
756 error ("missing callgraph edge for call stmt:");
757 debug_gimple_stmt (stmt
);
762 pointer_set_destroy (visited_nodes
);
765 /* No CFG available?! */
768 for (e
= node
->callees
; e
; e
= e
->next_callee
)
770 if (!e
->aux
&& !e
->indirect_call
)
772 error ("edge %s->%s has no corresponding call_stmt",
773 identifier_to_locale (cgraph_node_name (e
->caller
)),
774 identifier_to_locale (cgraph_node_name (e
->callee
)));
775 debug_gimple_stmt (e
->call_stmt
);
783 dump_cgraph_node (stderr
, node
);
784 internal_error ("verify_cgraph_node failed");
786 set_cfun (saved_cfun
);
787 timevar_pop (TV_CGRAPH_VERIFY
);
790 /* Verify whole cgraph structure. */
794 struct cgraph_node
*node
;
796 if (sorrycount
|| errorcount
)
799 for (node
= cgraph_nodes
; node
; node
= node
->next
)
800 verify_cgraph_node (node
);
803 /* Output all asm statements we have stored up to be output. */
806 cgraph_output_pending_asms (void)
808 struct cgraph_asm_node
*can
;
810 if (errorcount
|| sorrycount
)
813 for (can
= cgraph_asm_nodes
; can
; can
= can
->next
)
814 assemble_asm (can
->asm_str
);
815 cgraph_asm_nodes
= NULL
;
818 /* Analyze the function scheduled to be output. */
820 cgraph_analyze_function (struct cgraph_node
*node
)
822 tree save
= current_function_decl
;
823 tree decl
= node
->decl
;
825 current_function_decl
= decl
;
826 push_cfun (DECL_STRUCT_FUNCTION (decl
));
828 /* Make sure to gimplify bodies only once. During analyzing a
829 function we lower it, which will require gimplified nested
830 functions, so we can end up here with an already gimplified
832 if (!gimple_body (decl
))
833 gimplify_function_tree (decl
);
834 dump_function (TDI_generic
, decl
);
836 cgraph_lower_function (node
);
837 node
->analyzed
= true;
840 current_function_decl
= save
;
843 /* Look for externally_visible and used attributes and mark cgraph nodes
846 We cannot mark the nodes at the point the attributes are processed (in
847 handle_*_attribute) because the copy of the declarations available at that
848 point may not be canonical. For example, in:
851 void f() __attribute__((used));
853 the declaration we see in handle_used_attribute will be the second
854 declaration -- but the front end will subsequently merge that declaration
855 with the original declaration and discard the second declaration.
857 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
860 void f() __attribute__((externally_visible));
864 So, we walk the nodes at the end of the translation unit, applying the
865 attributes at that point. */
868 process_function_and_variable_attributes (struct cgraph_node
*first
,
869 struct varpool_node
*first_var
)
871 struct cgraph_node
*node
;
872 struct varpool_node
*vnode
;
874 for (node
= cgraph_nodes
; node
!= first
; node
= node
->next
)
876 tree decl
= node
->decl
;
877 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
879 mark_decl_referenced (decl
);
880 if (node
->local
.finalized
)
881 cgraph_mark_needed_node (node
);
883 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
885 if (! TREE_PUBLIC (node
->decl
))
886 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
887 "%<externally_visible%>"
888 " attribute have effect only on public objects");
889 else if (node
->local
.finalized
)
890 cgraph_mark_needed_node (node
);
893 for (vnode
= varpool_nodes
; vnode
!= first_var
; vnode
= vnode
->next
)
895 tree decl
= vnode
->decl
;
896 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
898 mark_decl_referenced (decl
);
899 vnode
->force_output
= true;
900 if (vnode
->finalized
)
901 varpool_mark_needed_node (vnode
);
903 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
905 if (! TREE_PUBLIC (vnode
->decl
))
906 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
907 "%<externally_visible%>"
908 " attribute have effect only on public objects");
909 else if (vnode
->finalized
)
910 varpool_mark_needed_node (vnode
);
915 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
916 each reachable functions) and build cgraph.
917 The function can be called multiple times after inserting new nodes
918 into beginning of queue. Just the new part of queue is re-scanned then. */
921 cgraph_analyze_functions (void)
923 /* Keep track of already processed nodes when called multiple times for
924 intermodule optimization. */
925 static struct cgraph_node
*first_analyzed
;
926 struct cgraph_node
*first_processed
= first_analyzed
;
927 static struct varpool_node
*first_analyzed_var
;
928 struct cgraph_node
*node
, *next
;
930 process_function_and_variable_attributes (first_processed
,
932 first_processed
= cgraph_nodes
;
933 first_analyzed_var
= varpool_nodes
;
934 varpool_analyze_pending_decls ();
935 if (cgraph_dump_file
)
937 fprintf (cgraph_dump_file
, "Initial entry points:");
938 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= node
->next
)
940 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
941 fprintf (cgraph_dump_file
, "\n");
943 cgraph_process_new_functions ();
945 /* Propagate reachability flag and lower representation of all reachable
946 functions. In the future, lowering will introduce new functions and
947 new entry points on the way (by template instantiation and virtual
948 method table generation for instance). */
949 while (cgraph_nodes_queue
)
951 struct cgraph_edge
*edge
;
952 tree decl
= cgraph_nodes_queue
->decl
;
954 node
= cgraph_nodes_queue
;
955 cgraph_nodes_queue
= cgraph_nodes_queue
->next_needed
;
956 node
->next_needed
= NULL
;
958 /* ??? It is possible to create extern inline function and later using
959 weak alias attribute to kill its body. See
960 gcc.c-torture/compile/20011119-1.c */
961 if (!DECL_STRUCT_FUNCTION (decl
))
963 cgraph_reset_node (node
);
968 cgraph_analyze_function (node
);
970 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
971 if (!edge
->callee
->reachable
)
972 cgraph_mark_reachable_node (edge
->callee
);
974 /* If decl is a clone of an abstract function, mark that abstract
975 function so that we don't release its body. The DECL_INITIAL() of that
976 abstract function declaration will be later needed to output debug info. */
977 if (DECL_ABSTRACT_ORIGIN (decl
))
979 struct cgraph_node
*origin_node
= cgraph_node (DECL_ABSTRACT_ORIGIN (decl
));
980 origin_node
->abstract_and_needed
= true;
983 /* We finalize local static variables during constructing callgraph
984 edges. Process their attributes too. */
985 process_function_and_variable_attributes (first_processed
,
987 first_processed
= cgraph_nodes
;
988 first_analyzed_var
= varpool_nodes
;
989 varpool_analyze_pending_decls ();
990 cgraph_process_new_functions ();
993 /* Collect entry points to the unit. */
994 if (cgraph_dump_file
)
996 fprintf (cgraph_dump_file
, "Unit entry points:");
997 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= node
->next
)
999 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1000 fprintf (cgraph_dump_file
, "\n\nInitial ");
1001 dump_cgraph (cgraph_dump_file
);
1004 if (cgraph_dump_file
)
1005 fprintf (cgraph_dump_file
, "\nReclaiming functions:");
1007 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= next
)
1009 tree decl
= node
->decl
;
1012 if (node
->local
.finalized
&& !gimple_has_body_p (decl
))
1013 cgraph_reset_node (node
);
1015 if (!node
->reachable
&& gimple_has_body_p (decl
))
1017 if (cgraph_dump_file
)
1018 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1019 cgraph_remove_node (node
);
1023 node
->next_needed
= NULL
;
1024 gcc_assert (!node
->local
.finalized
|| gimple_has_body_p (decl
));
1025 gcc_assert (node
->analyzed
== node
->local
.finalized
);
1027 if (cgraph_dump_file
)
1029 fprintf (cgraph_dump_file
, "\n\nReclaimed ");
1030 dump_cgraph (cgraph_dump_file
);
1032 first_analyzed
= cgraph_nodes
;
1037 /* Emit thunks for every node in the cgraph.
1038 FIXME: We really ought to emit thunks only for functions that are needed. */
1041 cgraph_emit_thunks (void)
1043 struct cgraph_node
*n
, *alias
;
1045 for (n
= cgraph_nodes
; n
; n
= n
->next
)
1047 /* Only emit thunks on functions defined in this TU.
1048 Note that this may emit more thunks than strictly necessary.
1049 During optimization some nodes may disappear. It would be
1050 nice to only emit thunks only for the functions that will be
1051 emitted, but we cannot know that until the inliner and other
1052 IPA passes have run (see the sequencing of the call to
1053 cgraph_mark_functions_to_output in cgraph_optimize). */
1055 && !DECL_EXTERNAL (n
->decl
))
1057 lang_hooks
.callgraph
.emit_associated_thunks (n
->decl
);
1058 for (alias
= n
->same_body
; alias
; alias
= alias
->next
)
1059 if (!DECL_EXTERNAL (alias
->decl
))
1060 lang_hooks
.callgraph
.emit_associated_thunks (alias
->decl
);
1066 /* Analyze the whole compilation unit once it is parsed completely. */
1069 cgraph_finalize_compilation_unit (void)
1071 timevar_push (TV_CGRAPH
);
1073 /* Do not skip analyzing the functions if there were errors, we
1074 miss diagnostics for following functions otherwise. */
1076 /* Emit size functions we didn't inline. */
1077 finalize_size_functions ();
1079 /* Call functions declared with the "constructor" or "destructor"
1081 cgraph_build_cdtor_fns ();
1083 /* Mark alias targets necessary and emit diagnostics. */
1084 finish_aliases_1 ();
1088 fprintf (stderr
, "\nAnalyzing compilation unit\n");
1092 /* Gimplify and lower all functions, compute reachability and
1093 remove unreachable nodes. */
1094 cgraph_analyze_functions ();
1096 /* Emit thunks for reachable nodes, if needed. */
1097 if (lang_hooks
.callgraph
.emit_associated_thunks
)
1098 cgraph_emit_thunks ();
1100 /* Mark alias targets necessary and emit diagnostics. */
1101 finish_aliases_1 ();
1103 /* Gimplify and lower thunks. */
1104 cgraph_analyze_functions ();
1106 /* Finally drive the pass manager. */
1109 timevar_pop (TV_CGRAPH
);
1113 /* Figure out what functions we want to assemble. */
1116 cgraph_mark_functions_to_output (void)
1118 struct cgraph_node
*node
;
1120 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1122 tree decl
= node
->decl
;
1123 struct cgraph_edge
*e
;
1125 gcc_assert (!node
->process
);
1127 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1128 if (e
->inline_failed
)
1131 /* We need to output all local functions that are used and not
1132 always inlined, as well as those that are reachable from
1133 outside the current compilation unit. */
1135 && !node
->global
.inlined_to
1137 || (e
&& node
->reachable
))
1138 && !TREE_ASM_WRITTEN (decl
)
1139 && !DECL_EXTERNAL (decl
))
1143 /* We should've reclaimed all functions that are not needed. */
1144 #ifdef ENABLE_CHECKING
1145 if (!node
->global
.inlined_to
1146 && gimple_has_body_p (decl
)
1147 && !DECL_EXTERNAL (decl
))
1149 dump_cgraph_node (stderr
, node
);
1150 internal_error ("failed to reclaim unneeded function");
1153 gcc_assert (node
->global
.inlined_to
1154 || !gimple_has_body_p (decl
)
1155 || DECL_EXTERNAL (decl
));
1162 /* Expand function specified by NODE. */
1165 cgraph_expand_function (struct cgraph_node
*node
)
1167 tree decl
= node
->decl
;
1169 /* We ought to not compile any inline clones. */
1170 gcc_assert (!node
->global
.inlined_to
);
1172 announce_function (decl
);
1175 gcc_assert (node
->lowered
);
1177 /* Generate RTL for the body of DECL. */
1178 tree_rest_of_compilation (decl
);
1180 /* Make sure that BE didn't give up on compiling. */
1181 gcc_assert (TREE_ASM_WRITTEN (decl
));
1182 current_function_decl
= NULL
;
1183 if (node
->same_body
)
1185 struct cgraph_node
*alias
;
1186 bool saved_alias
= node
->alias
;
1187 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
1188 assemble_alias (alias
->decl
, DECL_ASSEMBLER_NAME (decl
));
1189 node
->alias
= saved_alias
;
1191 gcc_assert (!cgraph_preserve_function_body_p (decl
));
1192 cgraph_release_function_body (node
);
1193 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1194 points to the dead function body. */
1195 cgraph_node_remove_callees (node
);
1197 cgraph_function_flags_ready
= true;
1200 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1203 cgraph_inline_p (struct cgraph_edge
*e
, cgraph_inline_failed_t
*reason
)
1205 *reason
= e
->inline_failed
;
1206 return !e
->inline_failed
;
1211 /* Expand all functions that must be output.
1213 Attempt to topologically sort the nodes so function is output when
1214 all called functions are already assembled to allow data to be
1215 propagated across the callgraph. Use a stack to get smaller distance
1216 between a function and its callees (later we may choose to use a more
1217 sophisticated algorithm for function reordering; we will likely want
1218 to use subsections to make the output functions appear in top-down
1222 cgraph_expand_all_functions (void)
1224 struct cgraph_node
*node
;
1225 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1226 int order_pos
, new_order_pos
= 0;
1229 order_pos
= cgraph_postorder (order
);
1230 gcc_assert (order_pos
== cgraph_n_nodes
);
1232 /* Garbage collector may remove inline clones we eliminate during
1233 optimization. So we must be sure to not reference them. */
1234 for (i
= 0; i
< order_pos
; i
++)
1235 if (order
[i
]->process
)
1236 order
[new_order_pos
++] = order
[i
];
1238 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1243 gcc_assert (node
->reachable
);
1245 cgraph_expand_function (node
);
1248 cgraph_process_new_functions ();
1254 /* This is used to sort the node types by the cgraph order number. */
1256 enum cgraph_order_sort_kind
1258 ORDER_UNDEFINED
= 0,
1264 struct cgraph_order_sort
1266 enum cgraph_order_sort_kind kind
;
1269 struct cgraph_node
*f
;
1270 struct varpool_node
*v
;
1271 struct cgraph_asm_node
*a
;
1275 /* Output all functions, variables, and asm statements in the order
1276 according to their order fields, which is the order in which they
1277 appeared in the file. This implements -fno-toplevel-reorder. In
1278 this mode we may output functions and variables which don't really
1279 need to be output. */
1282 cgraph_output_in_order (void)
1286 struct cgraph_order_sort
*nodes
;
1288 struct cgraph_node
*pf
;
1289 struct varpool_node
*pv
;
1290 struct cgraph_asm_node
*pa
;
1293 size
= max
* sizeof (struct cgraph_order_sort
);
1294 nodes
= (struct cgraph_order_sort
*) alloca (size
);
1295 memset (nodes
, 0, size
);
1297 varpool_analyze_pending_decls ();
1299 for (pf
= cgraph_nodes
; pf
; pf
= pf
->next
)
1304 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1305 nodes
[i
].kind
= ORDER_FUNCTION
;
1310 for (pv
= varpool_nodes_queue
; pv
; pv
= pv
->next_needed
)
1313 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1314 nodes
[i
].kind
= ORDER_VAR
;
1318 for (pa
= cgraph_asm_nodes
; pa
; pa
= pa
->next
)
1321 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1322 nodes
[i
].kind
= ORDER_ASM
;
1326 /* In toplevel reorder mode we output all statics; mark them as needed. */
1327 for (i
= 0; i
< max
; ++i
)
1329 if (nodes
[i
].kind
== ORDER_VAR
)
1331 varpool_mark_needed_node (nodes
[i
].u
.v
);
1334 varpool_empty_needed_queue ();
1336 for (i
= 0; i
< max
; ++i
)
1338 switch (nodes
[i
].kind
)
1340 case ORDER_FUNCTION
:
1341 nodes
[i
].u
.f
->process
= 0;
1342 cgraph_expand_function (nodes
[i
].u
.f
);
1346 varpool_assemble_decl (nodes
[i
].u
.v
);
1350 assemble_asm (nodes
[i
].u
.a
->asm_str
);
1353 case ORDER_UNDEFINED
:
1361 cgraph_asm_nodes
= NULL
;
1364 /* Return true when function body of DECL still needs to be kept around
1365 for later re-use. */
1367 cgraph_preserve_function_body_p (tree decl
)
1369 struct cgraph_node
*node
;
1371 gcc_assert (cgraph_global_info_ready
);
1372 /* Look if there is any clone around. */
1373 node
= cgraph_node (decl
);
1383 current_function_decl
= NULL
;
1384 gimple_register_cfg_hooks ();
1385 bitmap_obstack_initialize (NULL
);
1388 execute_ipa_pass_list (all_small_ipa_passes
);
1390 /* If pass_all_early_optimizations was not scheduled, the state of
1391 the cgraph will not be properly updated. Update it now. */
1392 if (cgraph_state
< CGRAPH_STATE_IPA_SSA
)
1393 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1397 /* Generate coverage variables and constructors. */
1400 /* Process new functions added. */
1402 current_function_decl
= NULL
;
1403 cgraph_process_new_functions ();
1405 execute_ipa_summary_passes ((struct ipa_opt_pass_d
*) all_regular_ipa_passes
);
1407 execute_ipa_summary_passes ((struct ipa_opt_pass_d
*) all_lto_gen_passes
);
1410 ipa_write_summaries ();
1413 execute_ipa_pass_list (all_regular_ipa_passes
);
1415 bitmap_obstack_release (NULL
);
1419 /* Perform simple optimizations based on callgraph. */
1422 cgraph_optimize (void)
1424 if (errorcount
|| sorrycount
)
1427 #ifdef ENABLE_CHECKING
1431 /* Frontend may output common variables after the unit has been finalized.
1432 It is safe to deal with them here as they are always zero initialized. */
1433 varpool_analyze_pending_decls ();
1435 timevar_push (TV_CGRAPHOPT
);
1436 if (pre_ipa_mem_report
)
1438 fprintf (stderr
, "Memory consumption before IPA\n");
1439 dump_memory_report (false);
1442 fprintf (stderr
, "Performing interprocedural optimizations\n");
1443 cgraph_state
= CGRAPH_STATE_IPA
;
1445 /* Don't run the IPA passes if there was any error or sorry messages. */
1446 if (errorcount
== 0 && sorrycount
== 0)
1449 /* Do nothing else if any IPA pass found errors. */
1450 if (errorcount
|| sorrycount
)
1452 timevar_pop (TV_CGRAPHOPT
);
1456 /* This pass remove bodies of extern inline functions we never inlined.
1457 Do this later so other IPA passes see what is really going on. */
1458 cgraph_remove_unreachable_nodes (false, dump_file
);
1459 cgraph_global_info_ready
= true;
1460 if (cgraph_dump_file
)
1462 fprintf (cgraph_dump_file
, "Optimized ");
1463 dump_cgraph (cgraph_dump_file
);
1464 dump_varpool (cgraph_dump_file
);
1466 if (post_ipa_mem_report
)
1468 fprintf (stderr
, "Memory consumption after IPA\n");
1469 dump_memory_report (false);
1471 timevar_pop (TV_CGRAPHOPT
);
1473 /* Output everything. */
1474 (*debug_hooks
->assembly_start
) ();
1476 fprintf (stderr
, "Assembling functions:\n");
1477 #ifdef ENABLE_CHECKING
1481 cgraph_materialize_all_clones ();
1482 cgraph_mark_functions_to_output ();
1484 cgraph_state
= CGRAPH_STATE_EXPANSION
;
1485 if (!flag_toplevel_reorder
)
1486 cgraph_output_in_order ();
1489 cgraph_output_pending_asms ();
1491 cgraph_expand_all_functions ();
1492 varpool_remove_unreferenced_decls ();
1494 varpool_assemble_pending_decls ();
1496 cgraph_process_new_functions ();
1497 cgraph_state
= CGRAPH_STATE_FINISHED
;
1499 if (cgraph_dump_file
)
1501 fprintf (cgraph_dump_file
, "\nFinal ");
1502 dump_cgraph (cgraph_dump_file
);
1504 #ifdef ENABLE_CHECKING
1506 /* Double check that all inline clones are gone and that all
1507 function bodies have been released from memory. */
1508 if (!(sorrycount
|| errorcount
))
1510 struct cgraph_node
*node
;
1511 bool error_found
= false;
1513 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1515 && (node
->global
.inlined_to
1516 || gimple_has_body_p (node
->decl
)))
1519 dump_cgraph_node (stderr
, node
);
1522 internal_error ("nodes with unreleased memory found");
1528 /* Generate and emit a static constructor or destructor. WHICH must
1529 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1530 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1531 initialization priority for this constructor or destructor. */
1534 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1536 static int counter
= 0;
1538 tree decl
, name
, resdecl
;
1540 /* The priority is encoded in the constructor or destructor name.
1541 collect2 will sort the names and arrange that they are called at
1543 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1544 name
= get_file_function_name (which_buf
);
1546 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1547 build_function_type (void_type_node
, void_list_node
));
1548 current_function_decl
= decl
;
1550 resdecl
= build_decl (input_location
,
1551 RESULT_DECL
, NULL_TREE
, void_type_node
);
1552 DECL_ARTIFICIAL (resdecl
) = 1;
1553 DECL_RESULT (decl
) = resdecl
;
1554 DECL_CONTEXT (resdecl
) = decl
;
1556 allocate_struct_function (decl
, false);
1558 TREE_STATIC (decl
) = 1;
1559 TREE_USED (decl
) = 1;
1560 DECL_ARTIFICIAL (decl
) = 1;
1561 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1562 DECL_SAVED_TREE (decl
) = body
;
1563 TREE_PUBLIC (decl
) = ! targetm
.have_ctors_dtors
;
1564 DECL_UNINLINABLE (decl
) = 1;
1566 DECL_INITIAL (decl
) = make_node (BLOCK
);
1567 TREE_USED (DECL_INITIAL (decl
)) = 1;
1569 DECL_SOURCE_LOCATION (decl
) = input_location
;
1570 cfun
->function_end_locus
= input_location
;
1575 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1576 decl_init_priority_insert (decl
, priority
);
1579 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1580 decl_fini_priority_insert (decl
, priority
);
1586 gimplify_function_tree (decl
);
1588 cgraph_add_new_function (decl
, false);
1589 cgraph_mark_needed_node (cgraph_node (decl
));
1596 cgraph_dump_file
= dump_begin (TDI_cgraph
, NULL
);
1599 /* The edges representing the callers of the NEW_VERSION node were
1600 fixed by cgraph_function_versioning (), now the call_expr in their
1601 respective tree code should be updated to call the NEW_VERSION. */
1604 update_call_expr (struct cgraph_node
*new_version
)
1606 struct cgraph_edge
*e
;
1608 gcc_assert (new_version
);
1610 /* Update the call expr on the edges to call the new version. */
1611 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
1613 struct function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->decl
);
1614 gimple_call_set_fndecl (e
->call_stmt
, new_version
->decl
);
1615 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
1620 /* Create a new cgraph node which is the new version of
1621 OLD_VERSION node. REDIRECT_CALLERS holds the callers
1622 edges which should be redirected to point to
1623 NEW_VERSION. ALL the callees edges of OLD_VERSION
1624 are cloned to the new version node. Return the new
1627 static struct cgraph_node
*
1628 cgraph_copy_node_for_versioning (struct cgraph_node
*old_version
,
1630 VEC(cgraph_edge_p
,heap
) *redirect_callers
)
1632 struct cgraph_node
*new_version
;
1633 struct cgraph_edge
*e
, *new_e
;
1634 struct cgraph_edge
*next_callee
;
1637 gcc_assert (old_version
);
1639 new_version
= cgraph_node (new_decl
);
1641 new_version
->analyzed
= true;
1642 new_version
->local
= old_version
->local
;
1643 new_version
->global
= old_version
->global
;
1644 new_version
->rtl
= new_version
->rtl
;
1645 new_version
->reachable
= true;
1646 new_version
->count
= old_version
->count
;
1648 /* Clone the old node callees. Recursive calls are
1650 for (e
= old_version
->callees
;e
; e
=e
->next_callee
)
1652 new_e
= cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
1653 e
->lto_stmt_uid
, 0, e
->frequency
,
1654 e
->loop_nest
, true);
1655 new_e
->count
= e
->count
;
1657 /* Fix recursive calls.
1658 If OLD_VERSION has a recursive call after the
1659 previous edge cloning, the new version will have an edge
1660 pointing to the old version, which is wrong;
1661 Redirect it to point to the new version. */
1662 for (e
= new_version
->callees
; e
; e
= next_callee
)
1664 next_callee
= e
->next_callee
;
1665 if (e
->callee
== old_version
)
1666 cgraph_redirect_edge_callee (e
, new_version
);
1671 for (i
= 0; VEC_iterate (cgraph_edge_p
, redirect_callers
, i
, e
); i
++)
1673 /* Redirect calls to the old version node to point to its new
1675 cgraph_redirect_edge_callee (e
, new_version
);
1681 /* Perform function versioning.
1682 Function versioning includes copying of the tree and
1683 a callgraph update (creating a new cgraph node and updating
1684 its callees and callers).
1686 REDIRECT_CALLERS varray includes the edges to be redirected
1689 TREE_MAP is a mapping of tree nodes we want to replace with
1690 new ones (according to results of prior analysis).
1691 OLD_VERSION_NODE is the node that is versioned.
1692 It returns the new version's cgraph node.
1693 ARGS_TO_SKIP lists arguments to be omitted from functions
1696 struct cgraph_node
*
1697 cgraph_function_versioning (struct cgraph_node
*old_version_node
,
1698 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
1699 VEC (ipa_replace_map_p
,gc
)* tree_map
,
1700 bitmap args_to_skip
)
1702 tree old_decl
= old_version_node
->decl
;
1703 struct cgraph_node
*new_version_node
= NULL
;
1706 if (!tree_versionable_function_p (old_decl
))
1709 /* Make a new FUNCTION_DECL tree node for the
1712 new_decl
= copy_node (old_decl
);
1714 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
);
1716 /* Create the new version's call-graph node.
1717 and update the edges of the new node. */
1719 cgraph_copy_node_for_versioning (old_version_node
, new_decl
,
1722 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1723 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
);
1725 /* Update the new version's properties.
1726 Make The new version visible only within this translation unit. Make sure
1727 that is not weak also.
1728 ??? We cannot use COMDAT linkage because there is no
1729 ABI support for this. */
1730 DECL_EXTERNAL (new_version_node
->decl
) = 0;
1731 DECL_COMDAT_GROUP (new_version_node
->decl
) = NULL_TREE
;
1732 TREE_PUBLIC (new_version_node
->decl
) = 0;
1733 DECL_COMDAT (new_version_node
->decl
) = 0;
1734 DECL_WEAK (new_version_node
->decl
) = 0;
1735 DECL_VIRTUAL_P (new_version_node
->decl
) = 0;
1736 new_version_node
->local
.externally_visible
= 0;
1737 new_version_node
->local
.local
= 1;
1738 new_version_node
->lowered
= true;
1740 /* Update the call_expr on the edges to call the new version node. */
1741 update_call_expr (new_version_node
);
1743 cgraph_call_function_insertion_hooks (new_version_node
);
1744 return new_version_node
;
1747 /* Produce separate function body for inline clones so the offline copy can be
1748 modified without affecting them. */
1749 struct cgraph_node
*
1750 save_inline_function_body (struct cgraph_node
*node
)
1752 struct cgraph_node
*first_clone
, *n
;
1754 gcc_assert (node
== cgraph_node (node
->decl
));
1756 cgraph_lower_function (node
);
1758 first_clone
= node
->clones
;
1760 first_clone
->decl
= copy_node (node
->decl
);
1761 cgraph_insert_node_to_hashtable (first_clone
);
1762 gcc_assert (first_clone
== cgraph_node (first_clone
->decl
));
1763 if (first_clone
->next_sibling_clone
)
1765 for (n
= first_clone
->next_sibling_clone
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
1766 n
->clone_of
= first_clone
;
1767 n
->clone_of
= first_clone
;
1768 n
->next_sibling_clone
= first_clone
->clones
;
1769 if (first_clone
->clones
)
1770 first_clone
->clones
->prev_sibling_clone
= n
;
1771 first_clone
->clones
= first_clone
->next_sibling_clone
;
1772 first_clone
->next_sibling_clone
->prev_sibling_clone
= NULL
;
1773 first_clone
->next_sibling_clone
= NULL
;
1774 gcc_assert (!first_clone
->prev_sibling_clone
);
1776 first_clone
->clone_of
= NULL
;
1777 node
->clones
= NULL
;
1779 if (first_clone
->clones
)
1780 for (n
= first_clone
->clones
; n
!= first_clone
;)
1782 gcc_assert (n
->decl
== node
->decl
);
1783 n
->decl
= first_clone
->decl
;
1786 else if (n
->next_sibling_clone
)
1787 n
= n
->next_sibling_clone
;
1790 while (n
!= first_clone
&& !n
->next_sibling_clone
)
1792 if (n
!= first_clone
)
1793 n
= n
->next_sibling_clone
;
1797 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1798 tree_function_versioning (node
->decl
, first_clone
->decl
, NULL
, true, NULL
);
1800 DECL_EXTERNAL (first_clone
->decl
) = 0;
1801 DECL_COMDAT_GROUP (first_clone
->decl
) = NULL_TREE
;
1802 TREE_PUBLIC (first_clone
->decl
) = 0;
1803 DECL_COMDAT (first_clone
->decl
) = 0;
1804 VEC_free (ipa_opt_pass
, heap
,
1805 first_clone
->ipa_transforms_to_apply
);
1806 first_clone
->ipa_transforms_to_apply
= NULL
;
1808 #ifdef ENABLE_CHECKING
1809 verify_cgraph_node (first_clone
);
1814 /* Given virtual clone, turn it into actual clone. */
1816 cgraph_materialize_clone (struct cgraph_node
*node
)
1818 bitmap_obstack_initialize (NULL
);
1819 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1820 tree_function_versioning (node
->clone_of
->decl
, node
->decl
,
1821 node
->clone
.tree_map
, true,
1822 node
->clone
.args_to_skip
);
1823 if (cgraph_dump_file
)
1825 dump_function_to_file (node
->clone_of
->decl
, cgraph_dump_file
, dump_flags
);
1826 dump_function_to_file (node
->decl
, cgraph_dump_file
, dump_flags
);
1829 /* Function is no longer clone. */
1830 if (node
->next_sibling_clone
)
1831 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1832 if (node
->prev_sibling_clone
)
1833 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1835 node
->clone_of
->clones
= node
->next_sibling_clone
;
1836 node
->next_sibling_clone
= NULL
;
1837 node
->prev_sibling_clone
= NULL
;
1838 if (!node
->clone_of
->analyzed
&& !node
->clone_of
->clones
)
1839 cgraph_remove_node (node
->clone_of
);
1840 node
->clone_of
= NULL
;
1841 bitmap_obstack_release (NULL
);
1844 /* Once all functions from compilation unit are in memory, produce all clones
1845 and update all calls.
1846 We might also do this on demand if we don't want to bring all functions to
1847 memory prior compilation, but current WHOPR implementation does that and it is
1848 is bit easier to keep everything right in this order. */
1850 cgraph_materialize_all_clones (void)
1852 struct cgraph_node
*node
;
1853 bool stabilized
= false;
1855 if (cgraph_dump_file
)
1856 fprintf (cgraph_dump_file
, "Materializing clones\n");
1857 #ifdef ENABLE_CHECKING
1861 /* We can also do topological order, but number of iterations should be
1862 bounded by number of IPA passes since single IPA pass is probably not
1863 going to create clones of clones it created itself. */
1867 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1869 if (node
->clone_of
&& node
->decl
!= node
->clone_of
->decl
1870 && !gimple_has_body_p (node
->decl
))
1872 if (gimple_has_body_p (node
->clone_of
->decl
))
1874 if (cgraph_dump_file
)
1876 fprintf (cgraph_dump_file
, "clonning %s to %s\n",
1877 cgraph_node_name (node
->clone_of
),
1878 cgraph_node_name (node
));
1879 if (node
->clone
.tree_map
)
1882 fprintf (cgraph_dump_file
, " replace map: ");
1883 for (i
= 0; i
< VEC_length (ipa_replace_map_p
,
1884 node
->clone
.tree_map
);
1887 struct ipa_replace_map
*replace_info
;
1888 replace_info
= VEC_index (ipa_replace_map_p
,
1889 node
->clone
.tree_map
,
1891 print_generic_expr (cgraph_dump_file
, replace_info
->old_tree
, 0);
1892 fprintf (cgraph_dump_file
, " -> ");
1893 print_generic_expr (cgraph_dump_file
, replace_info
->new_tree
, 0);
1894 fprintf (cgraph_dump_file
, "%s%s;",
1895 replace_info
->replace_p
? "(replace)":"",
1896 replace_info
->ref_p
? "(ref)":"");
1898 fprintf (cgraph_dump_file
, "\n");
1900 if (node
->clone
.args_to_skip
)
1902 fprintf (cgraph_dump_file
, " args_to_skip: ");
1903 dump_bitmap (cgraph_dump_file
, node
->clone
.args_to_skip
);
1905 if (node
->clone
.args_to_skip
)
1907 fprintf (cgraph_dump_file
, " combined_args_to_skip:");
1908 dump_bitmap (cgraph_dump_file
, node
->clone
.combined_args_to_skip
);
1911 cgraph_materialize_clone (node
);
1918 if (cgraph_dump_file
)
1919 fprintf (cgraph_dump_file
, "Updating call sites\n");
1920 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1921 if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
1922 && (!node
->clone_of
|| node
->clone_of
->decl
!= node
->decl
))
1924 struct cgraph_edge
*e
;
1926 current_function_decl
= node
->decl
;
1927 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1928 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1930 tree decl
= gimple_call_fndecl (e
->call_stmt
);
1931 /* When function gets inlined, indirect inlining might've invented
1932 new edge for orginally indirect stmt. Since we are not
1933 preserving clones in the original form, we must not update here
1934 since other inline clones don't need to contain call to the same
1935 call. Inliner will do the substitution for us later. */
1936 if (decl
&& decl
!= e
->callee
->decl
)
1939 gimple_stmt_iterator gsi
;
1941 if (e
->callee
->same_body
)
1943 struct cgraph_node
*alias
;
1945 for (alias
= e
->callee
->same_body
;
1947 alias
= alias
->next
)
1948 if (decl
== alias
->decl
)
1950 /* Don't update call from same body alias to the real
1956 if (cgraph_dump_file
)
1958 fprintf (cgraph_dump_file
, "updating call of %s in %s:",
1959 cgraph_node_name (node
),
1960 cgraph_node_name (e
->callee
));
1961 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1964 if (e
->callee
->clone
.combined_args_to_skip
)
1965 new_stmt
= gimple_call_copy_skip_args (e
->call_stmt
,
1966 e
->callee
->clone
.combined_args_to_skip
);
1968 new_stmt
= e
->call_stmt
;
1969 if (gimple_vdef (new_stmt
)
1970 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1971 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1972 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1974 gsi
= gsi_for_stmt (e
->call_stmt
);
1975 gsi_replace (&gsi
, new_stmt
, true);
1977 /* Update EH information too, just in case. */
1978 maybe_clean_or_replace_eh_stmt (e
->call_stmt
, new_stmt
);
1980 cgraph_set_call_stmt_including_clones (node
, e
->call_stmt
, new_stmt
);
1982 if (cgraph_dump_file
)
1984 fprintf (cgraph_dump_file
, " updated to:");
1985 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1990 current_function_decl
= NULL
;
1991 #ifdef ENABLE_CHECKING
1992 verify_cgraph_node (node
);
1995 #ifdef ENABLE_CHECKING
1998 cgraph_remove_unreachable_nodes (false, cgraph_dump_file
);
2001 #include "gt-cgraphunit.h"