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_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. */
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
)))
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. */
190 build_cdtor (bool ctor_p
, tree
*cdtors
, size_t len
)
199 priority_type priority
;
203 /* Find the next batch of constructors/destructors with the same
204 initialization priority. */
209 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
212 else if (p
!= priority
)
214 append_to_statement_list (build_function_call_expr (UNKNOWN_LOCATION
,
220 gcc_assert (body
!= NULL_TREE
);
221 /* Generate a function to call all the function of like
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. */
232 compare_ctor (const void *p1
, const void *p2
)
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
)
246 else if (priority1
> priority2
)
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. */
258 compare_dtor (const void *p1
, const void *p2
)
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
)
272 else if (priority1
> priority2
)
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. */
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
),
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
),
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
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;
327 /* If the user told us it is used, then it must be so. */
328 if (node
->local
.externally_visible
)
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
)))
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
)))
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. */
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
)
364 && !flag_whole_program
)
365 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
368 /* Constructors and destructors are reachable from the runtime by
370 if (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
))
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. */
381 cgraph_process_new_functions (void)
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
;
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
);
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 ();
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
423 execute_pass_list (pass_early_local_passes
.pass
.sub
);
424 free_dominance_info (CDI_POST_DOMINATORS
);
425 free_dominance_info (CDI_DOMINATORS
);
427 current_function_decl
= NULL
;
430 case CGRAPH_STATE_EXPANSION
:
431 /* Functions created during expansion shall be compiled
434 cgraph_expand_function (node
);
441 cgraph_call_function_insertion_hooks (node
);
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
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. */
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. */
481 struct cgraph_node
*n
;
483 for (n
= cgraph_nodes_queue
; n
; n
= n
->next_needed
)
492 cgraph_lower_function (struct cgraph_node
*node
)
498 lower_nested_functions (node
->decl
);
499 gcc_assert (!node
->nested
);
501 /* Non-nested functions never need a static chain. */
502 if (!DECL_NO_STATIC_CHAIN (node
->decl
)
503 && decl_function_context (node
->decl
) == NULL
)
504 DECL_NO_STATIC_CHAIN (node
->decl
) = 1;
506 tree_lowering_passes (node
->decl
);
507 node
->lowered
= true;
510 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
511 logic in effect. If NESTED is true, then our caller cannot stand to have
512 the garbage collector run at the moment. We would need to either create
513 a new GC context, or just not compile right now. */
516 cgraph_finalize_function (tree decl
, bool nested
)
518 struct cgraph_node
*node
= cgraph_node (decl
);
520 if (node
->local
.finalized
)
521 cgraph_reset_node (node
);
523 node
->pid
= cgraph_max_pid
++;
524 notice_global_symbol (decl
);
525 node
->local
.finalized
= true;
526 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
527 node
->finalized_by_frontend
= true;
528 record_cdtor_fn (node
->decl
);
530 if (decide_is_function_needed (node
, decl
))
531 cgraph_mark_needed_node (node
);
533 /* Since we reclaim unreachable nodes at the end of every language
534 level unit, we need to be conservative about possible entry points
536 if ((TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
)))
537 cgraph_mark_reachable_node (node
);
539 /* If we've not yet emitted decl, tell the debug info about it. */
540 if (!TREE_ASM_WRITTEN (decl
))
541 (*debug_hooks
->deferred_inline_function
) (decl
);
543 /* Possibly warn about unused parameters. */
544 if (warn_unused_parameter
)
545 do_warn_unused_parameter (decl
);
551 /* C99 extern inline keywords allow changing of declaration after function
552 has been finalized. We need to re-decide if we want to mark the function as
556 cgraph_mark_if_needed (tree decl
)
558 struct cgraph_node
*node
= cgraph_node (decl
);
559 if (node
->local
.finalized
&& decide_is_function_needed (node
, decl
))
560 cgraph_mark_needed_node (node
);
563 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
565 clone_of_p (struct cgraph_node
*node
, struct cgraph_node
*node2
)
567 while (node
!= node2
&& node2
)
568 node2
= node2
->clone_of
;
569 return node2
!= NULL
;
572 /* Verify cgraph nodes of given cgraph node. */
574 verify_cgraph_node (struct cgraph_node
*node
)
576 struct cgraph_edge
*e
;
577 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (node
->decl
);
578 struct function
*saved_cfun
= cfun
;
579 basic_block this_block
;
580 gimple_stmt_iterator gsi
;
581 bool error_found
= false;
583 if (errorcount
|| sorrycount
)
586 timevar_push (TV_CGRAPH_VERIFY
);
587 /* debug_generic_stmt needs correct cfun */
588 set_cfun (this_cfun
);
589 for (e
= node
->callees
; e
; e
= e
->next_callee
)
592 error ("aux field set for edge %s->%s",
593 identifier_to_locale (cgraph_node_name (e
->caller
)),
594 identifier_to_locale (cgraph_node_name (e
->callee
)));
599 error ("Execution count is negative");
602 for (e
= node
->callers
; e
; e
= e
->next_caller
)
606 error ("caller edge count is negative");
609 if (e
->frequency
< 0)
611 error ("caller edge frequency is negative");
614 if (e
->frequency
> CGRAPH_FREQ_MAX
)
616 error ("caller edge frequency is too large");
619 if (!e
->inline_failed
)
621 if (node
->global
.inlined_to
622 != (e
->caller
->global
.inlined_to
623 ? e
->caller
->global
.inlined_to
: e
->caller
))
625 error ("inlined_to pointer is wrong");
628 if (node
->callers
->next_caller
)
630 error ("multiple inline callers");
635 if (node
->global
.inlined_to
)
637 error ("inlined_to pointer set for noninline callers");
641 if (!node
->callers
&& node
->global
.inlined_to
)
643 error ("inlined_to pointer is set but no predecessors found");
646 if (node
->global
.inlined_to
== node
)
648 error ("inlined_to pointer refers to itself");
652 if (!cgraph_node (node
->decl
))
654 error ("node not found in cgraph_hash");
660 struct cgraph_node
*n
;
661 for (n
= node
->clone_of
->clones
; n
; n
= n
->next_sibling_clone
)
666 error ("node has wrong clone_of");
672 struct cgraph_node
*n
;
673 for (n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
674 if (n
->clone_of
!= node
)
678 error ("node has wrong clone list");
682 if ((node
->prev_sibling_clone
|| node
->next_sibling_clone
) && !node
->clone_of
)
684 error ("node is in clone list but it is not clone");
687 if (!node
->prev_sibling_clone
&& node
->clone_of
&& node
->clone_of
->clones
!= node
)
689 error ("node has wrong prev_clone pointer");
692 if (node
->prev_sibling_clone
&& node
->prev_sibling_clone
->next_sibling_clone
!= node
)
694 error ("double linked list of clones corrupted");
698 if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
699 && !TREE_ASM_WRITTEN (node
->decl
)
700 && (!DECL_EXTERNAL (node
->decl
) || node
->global
.inlined_to
))
704 /* The nodes we're interested in are never shared, so walk
705 the tree ignoring duplicates. */
706 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
707 /* Reach the trees by walking over the CFG, and note the
708 enclosing basic-blocks in the call edges. */
709 FOR_EACH_BB_FN (this_block
, this_cfun
)
710 for (gsi
= gsi_start_bb (this_block
);
714 gimple stmt
= gsi_stmt (gsi
);
716 if (is_gimple_call (stmt
) && (decl
= gimple_call_fndecl (stmt
)))
718 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
723 error ("shared call_stmt:");
724 debug_gimple_stmt (stmt
);
727 if (!clone_of_p (cgraph_node (decl
), e
->callee
)
728 && !e
->callee
->global
.inlined_to
)
730 error ("edge points to wrong declaration:");
731 debug_tree (e
->callee
->decl
);
732 fprintf (stderr
," Instead of:");
739 error ("missing callgraph edge for call stmt:");
740 debug_gimple_stmt (stmt
);
745 pointer_set_destroy (visited_nodes
);
748 /* No CFG available?! */
751 for (e
= node
->callees
; e
; e
= e
->next_callee
)
753 if (!e
->aux
&& !e
->indirect_call
)
755 error ("edge %s->%s has no corresponding call_stmt",
756 identifier_to_locale (cgraph_node_name (e
->caller
)),
757 identifier_to_locale (cgraph_node_name (e
->callee
)));
758 debug_gimple_stmt (e
->call_stmt
);
766 dump_cgraph_node (stderr
, node
);
767 internal_error ("verify_cgraph_node failed");
769 set_cfun (saved_cfun
);
770 timevar_pop (TV_CGRAPH_VERIFY
);
773 /* Verify whole cgraph structure. */
777 struct cgraph_node
*node
;
779 if (sorrycount
|| errorcount
)
782 for (node
= cgraph_nodes
; node
; node
= node
->next
)
783 verify_cgraph_node (node
);
786 /* Output all asm statements we have stored up to be output. */
789 cgraph_output_pending_asms (void)
791 struct cgraph_asm_node
*can
;
793 if (errorcount
|| sorrycount
)
796 for (can
= cgraph_asm_nodes
; can
; can
= can
->next
)
797 assemble_asm (can
->asm_str
);
798 cgraph_asm_nodes
= NULL
;
801 /* Analyze the function scheduled to be output. */
803 cgraph_analyze_function (struct cgraph_node
*node
)
805 tree save
= current_function_decl
;
806 tree decl
= node
->decl
;
808 current_function_decl
= decl
;
809 push_cfun (DECL_STRUCT_FUNCTION (decl
));
811 /* Make sure to gimplify bodies only once. During analyzing a
812 function we lower it, which will require gimplified nested
813 functions, so we can end up here with an already gimplified
815 if (!gimple_body (decl
))
816 gimplify_function_tree (decl
);
817 dump_function (TDI_generic
, decl
);
819 cgraph_lower_function (node
);
820 node
->analyzed
= true;
823 current_function_decl
= save
;
826 /* Look for externally_visible and used attributes and mark cgraph nodes
829 We cannot mark the nodes at the point the attributes are processed (in
830 handle_*_attribute) because the copy of the declarations available at that
831 point may not be canonical. For example, in:
834 void f() __attribute__((used));
836 the declaration we see in handle_used_attribute will be the second
837 declaration -- but the front end will subsequently merge that declaration
838 with the original declaration and discard the second declaration.
840 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
843 void f() __attribute__((externally_visible));
847 So, we walk the nodes at the end of the translation unit, applying the
848 attributes at that point. */
851 process_function_and_variable_attributes (struct cgraph_node
*first
,
852 struct varpool_node
*first_var
)
854 struct cgraph_node
*node
;
855 struct varpool_node
*vnode
;
857 for (node
= cgraph_nodes
; node
!= first
; node
= node
->next
)
859 tree decl
= node
->decl
;
860 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
862 mark_decl_referenced (decl
);
863 if (node
->local
.finalized
)
864 cgraph_mark_needed_node (node
);
866 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
868 if (! TREE_PUBLIC (node
->decl
))
869 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
870 "%<externally_visible%>"
871 " attribute have effect only on public objects");
874 if (node
->local
.finalized
)
875 cgraph_mark_needed_node (node
);
876 node
->local
.externally_visible
= true;
880 for (vnode
= varpool_nodes
; vnode
!= first_var
; vnode
= vnode
->next
)
882 tree decl
= vnode
->decl
;
883 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
885 mark_decl_referenced (decl
);
886 if (vnode
->finalized
)
887 varpool_mark_needed_node (vnode
);
889 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
891 if (! TREE_PUBLIC (vnode
->decl
))
892 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
893 "%<externally_visible%>"
894 " attribute have effect only on public objects");
897 if (vnode
->finalized
)
898 varpool_mark_needed_node (vnode
);
899 vnode
->externally_visible
= true;
905 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
906 each reachable functions) and build cgraph.
907 The function can be called multiple times after inserting new nodes
908 into beginning of queue. Just the new part of queue is re-scanned then. */
911 cgraph_analyze_functions (void)
913 /* Keep track of already processed nodes when called multiple times for
914 intermodule optimization. */
915 static struct cgraph_node
*first_analyzed
;
916 struct cgraph_node
*first_processed
= first_analyzed
;
917 static struct varpool_node
*first_analyzed_var
;
918 struct cgraph_node
*node
, *next
;
920 process_function_and_variable_attributes (first_processed
,
922 first_processed
= cgraph_nodes
;
923 first_analyzed_var
= varpool_nodes
;
924 varpool_analyze_pending_decls ();
925 if (cgraph_dump_file
)
927 fprintf (cgraph_dump_file
, "Initial entry points:");
928 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= node
->next
)
930 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
931 fprintf (cgraph_dump_file
, "\n");
933 cgraph_process_new_functions ();
935 /* Propagate reachability flag and lower representation of all reachable
936 functions. In the future, lowering will introduce new functions and
937 new entry points on the way (by template instantiation and virtual
938 method table generation for instance). */
939 while (cgraph_nodes_queue
)
941 struct cgraph_edge
*edge
;
942 tree decl
= cgraph_nodes_queue
->decl
;
944 node
= cgraph_nodes_queue
;
945 cgraph_nodes_queue
= cgraph_nodes_queue
->next_needed
;
946 node
->next_needed
= NULL
;
948 /* ??? It is possible to create extern inline function and later using
949 weak alias attribute to kill its body. See
950 gcc.c-torture/compile/20011119-1.c */
951 if (!DECL_STRUCT_FUNCTION (decl
))
953 cgraph_reset_node (node
);
957 gcc_assert (!node
->analyzed
&& node
->reachable
);
958 cgraph_analyze_function (node
);
960 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
961 if (!edge
->callee
->reachable
)
962 cgraph_mark_reachable_node (edge
->callee
);
964 /* If decl is a clone of an abstract function, mark that abstract
965 function so that we don't release its body. The DECL_INITIAL() of that
966 abstract function declaration will be later needed to output debug info. */
967 if (DECL_ABSTRACT_ORIGIN (decl
))
969 struct cgraph_node
*origin_node
= cgraph_node (DECL_ABSTRACT_ORIGIN (decl
));
970 origin_node
->abstract_and_needed
= true;
973 /* We finalize local static variables during constructing callgraph
974 edges. Process their attributes too. */
975 process_function_and_variable_attributes (first_processed
,
977 first_processed
= cgraph_nodes
;
978 first_analyzed_var
= varpool_nodes
;
979 varpool_analyze_pending_decls ();
980 cgraph_process_new_functions ();
983 /* Collect entry points to the unit. */
984 if (cgraph_dump_file
)
986 fprintf (cgraph_dump_file
, "Unit entry points:");
987 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= node
->next
)
989 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
990 fprintf (cgraph_dump_file
, "\n\nInitial ");
991 dump_cgraph (cgraph_dump_file
);
994 if (cgraph_dump_file
)
995 fprintf (cgraph_dump_file
, "\nReclaiming functions:");
997 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= next
)
999 tree decl
= node
->decl
;
1002 if (node
->local
.finalized
&& !gimple_has_body_p (decl
))
1003 cgraph_reset_node (node
);
1005 if (!node
->reachable
&& gimple_has_body_p (decl
))
1007 if (cgraph_dump_file
)
1008 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1009 cgraph_remove_node (node
);
1013 node
->next_needed
= NULL
;
1014 gcc_assert (!node
->local
.finalized
|| gimple_has_body_p (decl
));
1015 gcc_assert (node
->analyzed
== node
->local
.finalized
);
1017 if (cgraph_dump_file
)
1019 fprintf (cgraph_dump_file
, "\n\nReclaimed ");
1020 dump_cgraph (cgraph_dump_file
);
1022 first_analyzed
= cgraph_nodes
;
1027 /* Emit thunks for every node in the cgraph.
1028 FIXME: We really ought to emit thunks only for functions that are needed. */
1031 cgraph_emit_thunks (void)
1033 struct cgraph_node
*n
;
1035 for (n
= cgraph_nodes
; n
; n
= n
->next
)
1037 /* Only emit thunks on functions defined in this TU.
1038 Note that this may emit more thunks than strictly necessary.
1039 During optimization some nodes may disappear. It would be
1040 nice to only emit thunks only for the functions that will be
1041 emitted, but we cannot know that until the inliner and other
1042 IPA passes have run (see the sequencing of the call to
1043 cgraph_mark_functions_to_output in cgraph_optimize). */
1045 && !DECL_EXTERNAL (n
->decl
))
1046 lang_hooks
.callgraph
.emit_associated_thunks (n
->decl
);
1051 /* Analyze the whole compilation unit once it is parsed completely. */
1054 cgraph_finalize_compilation_unit (void)
1056 timevar_push (TV_CGRAPH
);
1058 /* Do not skip analyzing the functions if there were errors, we
1059 miss diagnostics for following functions otherwise. */
1061 /* Emit size functions we didn't inline. */
1062 finalize_size_functions ();
1064 /* Call functions declared with the "constructor" or "destructor"
1066 cgraph_build_cdtor_fns ();
1068 /* Mark alias targets necessary and emit diagnostics. */
1069 finish_aliases_1 ();
1073 fprintf (stderr
, "\nAnalyzing compilation unit\n");
1077 /* Gimplify and lower all functions, compute reachability and
1078 remove unreachable nodes. */
1079 cgraph_analyze_functions ();
1081 /* Emit thunks for reachable nodes, if needed. */
1082 if (lang_hooks
.callgraph
.emit_associated_thunks
)
1083 cgraph_emit_thunks ();
1085 /* Mark alias targets necessary and emit diagnostics. */
1086 finish_aliases_1 ();
1088 /* Gimplify and lower thunks. */
1089 cgraph_analyze_functions ();
1091 /* Finally drive the pass manager. */
1094 timevar_pop (TV_CGRAPH
);
1098 /* Figure out what functions we want to assemble. */
1101 cgraph_mark_functions_to_output (void)
1103 struct cgraph_node
*node
;
1105 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1107 tree decl
= node
->decl
;
1108 struct cgraph_edge
*e
;
1110 gcc_assert (!node
->process
);
1112 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1113 if (e
->inline_failed
)
1116 /* We need to output all local functions that are used and not
1117 always inlined, as well as those that are reachable from
1118 outside the current compilation unit. */
1120 && !node
->global
.inlined_to
1122 || (e
&& node
->reachable
))
1123 && !TREE_ASM_WRITTEN (decl
)
1124 && !DECL_EXTERNAL (decl
))
1128 /* We should've reclaimed all functions that are not needed. */
1129 #ifdef ENABLE_CHECKING
1130 if (!node
->global
.inlined_to
1131 && gimple_has_body_p (decl
)
1132 && !DECL_EXTERNAL (decl
))
1134 dump_cgraph_node (stderr
, node
);
1135 internal_error ("failed to reclaim unneeded function");
1138 gcc_assert (node
->global
.inlined_to
1139 || !gimple_has_body_p (decl
)
1140 || DECL_EXTERNAL (decl
));
1147 /* Expand function specified by NODE. */
1150 cgraph_expand_function (struct cgraph_node
*node
)
1152 tree decl
= node
->decl
;
1154 /* We ought to not compile any inline clones. */
1155 gcc_assert (!node
->global
.inlined_to
);
1157 announce_function (decl
);
1160 gcc_assert (node
->lowered
);
1162 /* Generate RTL for the body of DECL. */
1163 tree_rest_of_compilation (decl
);
1165 /* Make sure that BE didn't give up on compiling. */
1166 gcc_assert (TREE_ASM_WRITTEN (decl
));
1167 current_function_decl
= NULL
;
1168 gcc_assert (!cgraph_preserve_function_body_p (decl
));
1169 cgraph_release_function_body (node
);
1170 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1171 points to the dead function body. */
1172 cgraph_node_remove_callees (node
);
1174 cgraph_function_flags_ready
= true;
1177 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1180 cgraph_inline_p (struct cgraph_edge
*e
, cgraph_inline_failed_t
*reason
)
1182 *reason
= e
->inline_failed
;
1183 return !e
->inline_failed
;
1188 /* Expand all functions that must be output.
1190 Attempt to topologically sort the nodes so function is output when
1191 all called functions are already assembled to allow data to be
1192 propagated across the callgraph. Use a stack to get smaller distance
1193 between a function and its callees (later we may choose to use a more
1194 sophisticated algorithm for function reordering; we will likely want
1195 to use subsections to make the output functions appear in top-down
1199 cgraph_expand_all_functions (void)
1201 struct cgraph_node
*node
;
1202 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1203 int order_pos
, new_order_pos
= 0;
1206 order_pos
= cgraph_postorder (order
);
1207 gcc_assert (order_pos
== cgraph_n_nodes
);
1209 /* Garbage collector may remove inline clones we eliminate during
1210 optimization. So we must be sure to not reference them. */
1211 for (i
= 0; i
< order_pos
; i
++)
1212 if (order
[i
]->process
)
1213 order
[new_order_pos
++] = order
[i
];
1215 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1220 gcc_assert (node
->reachable
);
1222 cgraph_expand_function (node
);
1225 cgraph_process_new_functions ();
1231 /* This is used to sort the node types by the cgraph order number. */
1233 enum cgraph_order_sort_kind
1235 ORDER_UNDEFINED
= 0,
1241 struct cgraph_order_sort
1243 enum cgraph_order_sort_kind kind
;
1246 struct cgraph_node
*f
;
1247 struct varpool_node
*v
;
1248 struct cgraph_asm_node
*a
;
1252 /* Output all functions, variables, and asm statements in the order
1253 according to their order fields, which is the order in which they
1254 appeared in the file. This implements -fno-toplevel-reorder. In
1255 this mode we may output functions and variables which don't really
1256 need to be output. */
1259 cgraph_output_in_order (void)
1263 struct cgraph_order_sort
*nodes
;
1265 struct cgraph_node
*pf
;
1266 struct varpool_node
*pv
;
1267 struct cgraph_asm_node
*pa
;
1270 size
= max
* sizeof (struct cgraph_order_sort
);
1271 nodes
= (struct cgraph_order_sort
*) alloca (size
);
1272 memset (nodes
, 0, size
);
1274 varpool_analyze_pending_decls ();
1276 for (pf
= cgraph_nodes
; pf
; pf
= pf
->next
)
1281 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1282 nodes
[i
].kind
= ORDER_FUNCTION
;
1287 for (pv
= varpool_nodes_queue
; pv
; pv
= pv
->next_needed
)
1290 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1291 nodes
[i
].kind
= ORDER_VAR
;
1295 for (pa
= cgraph_asm_nodes
; pa
; pa
= pa
->next
)
1298 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1299 nodes
[i
].kind
= ORDER_ASM
;
1303 /* In toplevel reorder mode we output all statics; mark them as needed. */
1304 for (i
= 0; i
< max
; ++i
)
1306 if (nodes
[i
].kind
== ORDER_VAR
)
1308 varpool_mark_needed_node (nodes
[i
].u
.v
);
1311 varpool_empty_needed_queue ();
1313 for (i
= 0; i
< max
; ++i
)
1315 switch (nodes
[i
].kind
)
1317 case ORDER_FUNCTION
:
1318 nodes
[i
].u
.f
->process
= 0;
1319 cgraph_expand_function (nodes
[i
].u
.f
);
1323 varpool_assemble_decl (nodes
[i
].u
.v
);
1327 assemble_asm (nodes
[i
].u
.a
->asm_str
);
1330 case ORDER_UNDEFINED
:
1338 cgraph_asm_nodes
= NULL
;
1341 /* Return true when function body of DECL still needs to be kept around
1342 for later re-use. */
1344 cgraph_preserve_function_body_p (tree decl
)
1346 struct cgraph_node
*node
;
1348 gcc_assert (cgraph_global_info_ready
);
1349 /* Look if there is any clone around. */
1350 node
= cgraph_node (decl
);
1360 current_function_decl
= NULL
;
1361 gimple_register_cfg_hooks ();
1362 bitmap_obstack_initialize (NULL
);
1363 execute_ipa_pass_list (all_ipa_passes
);
1365 /* Generate coverage variables and constructors. */
1368 /* Process new functions added. */
1370 current_function_decl
= NULL
;
1371 cgraph_process_new_functions ();
1373 bitmap_obstack_release (NULL
);
1377 /* Perform simple optimizations based on callgraph. */
1380 cgraph_optimize (void)
1382 if (errorcount
|| sorrycount
)
1385 #ifdef ENABLE_CHECKING
1389 /* Frontend may output common variables after the unit has been finalized.
1390 It is safe to deal with them here as they are always zero initialized. */
1391 varpool_analyze_pending_decls ();
1393 timevar_push (TV_CGRAPHOPT
);
1394 if (pre_ipa_mem_report
)
1396 fprintf (stderr
, "Memory consumption before IPA\n");
1397 dump_memory_report (false);
1400 fprintf (stderr
, "Performing interprocedural optimizations\n");
1401 cgraph_state
= CGRAPH_STATE_IPA
;
1403 /* Don't run the IPA passes if there was any error or sorry messages. */
1404 if (errorcount
== 0 && sorrycount
== 0)
1407 /* Do nothing else if any IPA pass found errors. */
1408 if (errorcount
|| sorrycount
)
1411 /* This pass remove bodies of extern inline functions we never inlined.
1412 Do this later so other IPA passes see what is really going on. */
1413 cgraph_remove_unreachable_nodes (false, dump_file
);
1414 cgraph_global_info_ready
= true;
1415 if (cgraph_dump_file
)
1417 fprintf (cgraph_dump_file
, "Optimized ");
1418 dump_cgraph (cgraph_dump_file
);
1419 dump_varpool (cgraph_dump_file
);
1421 if (post_ipa_mem_report
)
1423 fprintf (stderr
, "Memory consumption after IPA\n");
1424 dump_memory_report (false);
1426 timevar_pop (TV_CGRAPHOPT
);
1428 /* Output everything. */
1430 fprintf (stderr
, "Assembling functions:\n");
1431 #ifdef ENABLE_CHECKING
1435 cgraph_materialize_all_clones ();
1436 cgraph_mark_functions_to_output ();
1438 cgraph_state
= CGRAPH_STATE_EXPANSION
;
1439 if (!flag_toplevel_reorder
)
1440 cgraph_output_in_order ();
1443 cgraph_output_pending_asms ();
1445 cgraph_expand_all_functions ();
1446 varpool_remove_unreferenced_decls ();
1448 varpool_assemble_pending_decls ();
1450 cgraph_process_new_functions ();
1451 cgraph_state
= CGRAPH_STATE_FINISHED
;
1453 if (cgraph_dump_file
)
1455 fprintf (cgraph_dump_file
, "\nFinal ");
1456 dump_cgraph (cgraph_dump_file
);
1458 #ifdef ENABLE_CHECKING
1460 /* Double check that all inline clones are gone and that all
1461 function bodies have been released from memory. */
1462 if (!(sorrycount
|| errorcount
))
1464 struct cgraph_node
*node
;
1465 bool error_found
= false;
1467 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1469 && (node
->global
.inlined_to
1470 || gimple_has_body_p (node
->decl
)))
1473 dump_cgraph_node (stderr
, node
);
1476 internal_error ("nodes with unreleased memory found");
1482 /* Generate and emit a static constructor or destructor. WHICH must
1483 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1484 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1485 initialization priority for this constructor or destructor. */
1488 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1490 static int counter
= 0;
1492 tree decl
, name
, resdecl
;
1494 /* The priority is encoded in the constructor or destructor name.
1495 collect2 will sort the names and arrange that they are called at
1497 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1498 name
= get_file_function_name (which_buf
);
1500 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1501 build_function_type (void_type_node
, void_list_node
));
1502 current_function_decl
= decl
;
1504 resdecl
= build_decl (input_location
,
1505 RESULT_DECL
, NULL_TREE
, void_type_node
);
1506 DECL_ARTIFICIAL (resdecl
) = 1;
1507 DECL_RESULT (decl
) = resdecl
;
1508 DECL_CONTEXT (resdecl
) = decl
;
1510 allocate_struct_function (decl
, false);
1512 TREE_STATIC (decl
) = 1;
1513 TREE_USED (decl
) = 1;
1514 DECL_ARTIFICIAL (decl
) = 1;
1515 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1516 DECL_SAVED_TREE (decl
) = body
;
1517 TREE_PUBLIC (decl
) = ! targetm
.have_ctors_dtors
;
1518 DECL_UNINLINABLE (decl
) = 1;
1520 DECL_INITIAL (decl
) = make_node (BLOCK
);
1521 TREE_USED (DECL_INITIAL (decl
)) = 1;
1523 DECL_SOURCE_LOCATION (decl
) = input_location
;
1524 cfun
->function_end_locus
= input_location
;
1529 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1530 decl_init_priority_insert (decl
, priority
);
1533 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1534 decl_fini_priority_insert (decl
, priority
);
1540 gimplify_function_tree (decl
);
1542 cgraph_add_new_function (decl
, false);
1543 cgraph_mark_needed_node (cgraph_node (decl
));
1550 cgraph_dump_file
= dump_begin (TDI_cgraph
, NULL
);
1553 /* The edges representing the callers of the NEW_VERSION node were
1554 fixed by cgraph_function_versioning (), now the call_expr in their
1555 respective tree code should be updated to call the NEW_VERSION. */
1558 update_call_expr (struct cgraph_node
*new_version
)
1560 struct cgraph_edge
*e
;
1562 gcc_assert (new_version
);
1564 /* Update the call expr on the edges to call the new version. */
1565 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
1567 struct function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->decl
);
1568 gimple_call_set_fndecl (e
->call_stmt
, new_version
->decl
);
1569 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
1574 /* Create a new cgraph node which is the new version of
1575 OLD_VERSION node. REDIRECT_CALLERS holds the callers
1576 edges which should be redirected to point to
1577 NEW_VERSION. ALL the callees edges of OLD_VERSION
1578 are cloned to the new version node. Return the new
1581 static struct cgraph_node
*
1582 cgraph_copy_node_for_versioning (struct cgraph_node
*old_version
,
1584 VEC(cgraph_edge_p
,heap
) *redirect_callers
)
1586 struct cgraph_node
*new_version
;
1587 struct cgraph_edge
*e
, *new_e
;
1588 struct cgraph_edge
*next_callee
;
1591 gcc_assert (old_version
);
1593 new_version
= cgraph_node (new_decl
);
1595 new_version
->analyzed
= true;
1596 new_version
->local
= old_version
->local
;
1597 new_version
->global
= old_version
->global
;
1598 new_version
->rtl
= new_version
->rtl
;
1599 new_version
->reachable
= true;
1600 new_version
->count
= old_version
->count
;
1602 /* Clone the old node callees. Recursive calls are
1604 for (e
= old_version
->callees
;e
; e
=e
->next_callee
)
1606 new_e
= cgraph_clone_edge (e
, new_version
, e
->call_stmt
, 0, e
->frequency
,
1607 e
->loop_nest
, true);
1608 new_e
->count
= e
->count
;
1610 /* Fix recursive calls.
1611 If OLD_VERSION has a recursive call after the
1612 previous edge cloning, the new version will have an edge
1613 pointing to the old version, which is wrong;
1614 Redirect it to point to the new version. */
1615 for (e
= new_version
->callees
; e
; e
= next_callee
)
1617 next_callee
= e
->next_callee
;
1618 if (e
->callee
== old_version
)
1619 cgraph_redirect_edge_callee (e
, new_version
);
1624 for (i
= 0; VEC_iterate (cgraph_edge_p
, redirect_callers
, i
, e
); i
++)
1626 /* Redirect calls to the old version node to point to its new
1628 cgraph_redirect_edge_callee (e
, new_version
);
1634 /* Perform function versioning.
1635 Function versioning includes copying of the tree and
1636 a callgraph update (creating a new cgraph node and updating
1637 its callees and callers).
1639 REDIRECT_CALLERS varray includes the edges to be redirected
1642 TREE_MAP is a mapping of tree nodes we want to replace with
1643 new ones (according to results of prior analysis).
1644 OLD_VERSION_NODE is the node that is versioned.
1645 It returns the new version's cgraph node.
1646 ARGS_TO_SKIP lists arguments to be omitted from functions
1649 struct cgraph_node
*
1650 cgraph_function_versioning (struct cgraph_node
*old_version_node
,
1651 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
1652 VEC (ipa_replace_map_p
,gc
)* tree_map
,
1653 bitmap args_to_skip
)
1655 tree old_decl
= old_version_node
->decl
;
1656 struct cgraph_node
*new_version_node
= NULL
;
1659 if (!tree_versionable_function_p (old_decl
))
1662 /* Make a new FUNCTION_DECL tree node for the
1665 new_decl
= copy_node (old_decl
);
1667 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
);
1669 /* Create the new version's call-graph node.
1670 and update the edges of the new node. */
1672 cgraph_copy_node_for_versioning (old_version_node
, new_decl
,
1675 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1676 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
);
1678 /* Update the new version's properties.
1679 Make The new version visible only within this translation unit. Make sure
1680 that is not weak also.
1681 ??? We cannot use COMDAT linkage because there is no
1682 ABI support for this. */
1683 DECL_EXTERNAL (new_version_node
->decl
) = 0;
1684 DECL_COMDAT_GROUP (new_version_node
->decl
) = NULL_TREE
;
1685 TREE_PUBLIC (new_version_node
->decl
) = 0;
1686 DECL_COMDAT (new_version_node
->decl
) = 0;
1687 DECL_WEAK (new_version_node
->decl
) = 0;
1688 DECL_VIRTUAL_P (new_version_node
->decl
) = 0;
1689 new_version_node
->local
.externally_visible
= 0;
1690 new_version_node
->local
.local
= 1;
1691 new_version_node
->lowered
= true;
1693 /* Update the call_expr on the edges to call the new version node. */
1694 update_call_expr (new_version_node
);
1696 cgraph_call_function_insertion_hooks (new_version_node
);
1697 return new_version_node
;
1700 /* Produce separate function body for inline clones so the offline copy can be
1701 modified without affecting them. */
1702 struct cgraph_node
*
1703 save_inline_function_body (struct cgraph_node
*node
)
1705 struct cgraph_node
*first_clone
, *n
;
1707 gcc_assert (node
== cgraph_node (node
->decl
));
1709 cgraph_lower_function (node
);
1711 first_clone
= node
->clones
;
1713 first_clone
->decl
= copy_node (node
->decl
);
1714 cgraph_insert_node_to_hashtable (first_clone
);
1715 gcc_assert (first_clone
== cgraph_node (first_clone
->decl
));
1716 if (first_clone
->next_sibling_clone
)
1718 for (n
= first_clone
->next_sibling_clone
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
1719 n
->clone_of
= first_clone
;
1720 n
->clone_of
= first_clone
;
1721 n
->next_sibling_clone
= first_clone
->clones
;
1722 if (first_clone
->clones
)
1723 first_clone
->clones
->prev_sibling_clone
= n
;
1724 first_clone
->clones
= first_clone
->next_sibling_clone
;
1725 first_clone
->next_sibling_clone
->prev_sibling_clone
= NULL
;
1726 first_clone
->next_sibling_clone
= NULL
;
1727 gcc_assert (!first_clone
->prev_sibling_clone
);
1729 first_clone
->clone_of
= NULL
;
1730 node
->clones
= NULL
;
1732 if (first_clone
->clones
)
1733 for (n
= first_clone
->clones
; n
!= first_clone
;)
1735 gcc_assert (n
->decl
== node
->decl
);
1736 n
->decl
= first_clone
->decl
;
1739 else if (n
->next_sibling_clone
)
1740 n
= n
->next_sibling_clone
;
1743 while (n
!= first_clone
&& !n
->next_sibling_clone
)
1745 if (n
!= first_clone
)
1746 n
= n
->next_sibling_clone
;
1750 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1751 tree_function_versioning (node
->decl
, first_clone
->decl
, NULL
, true, NULL
);
1753 DECL_EXTERNAL (first_clone
->decl
) = 0;
1754 DECL_COMDAT_GROUP (first_clone
->decl
) = NULL_TREE
;
1755 TREE_PUBLIC (first_clone
->decl
) = 0;
1756 DECL_COMDAT (first_clone
->decl
) = 0;
1757 VEC_free (ipa_opt_pass
, heap
,
1758 DECL_STRUCT_FUNCTION (first_clone
->decl
)->ipa_transforms_to_apply
);
1759 DECL_STRUCT_FUNCTION (first_clone
->decl
)->ipa_transforms_to_apply
= NULL
;
1761 #ifdef ENABLE_CHECKING
1762 verify_cgraph_node (first_clone
);
1767 /* Given virtual clone, turn it into actual clone. */
1769 cgraph_materialize_clone (struct cgraph_node
*node
)
1771 bitmap_obstack_initialize (NULL
);
1772 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1773 tree_function_versioning (node
->clone_of
->decl
, node
->decl
,
1774 node
->clone
.tree_map
, true,
1775 node
->clone
.args_to_skip
);
1776 if (cgraph_dump_file
)
1778 dump_function_to_file (node
->clone_of
->decl
, cgraph_dump_file
, dump_flags
);
1779 dump_function_to_file (node
->decl
, cgraph_dump_file
, dump_flags
);
1782 /* Function is no longer clone. */
1783 if (node
->next_sibling_clone
)
1784 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1785 if (node
->prev_sibling_clone
)
1786 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1788 node
->clone_of
->clones
= node
->next_sibling_clone
;
1789 node
->next_sibling_clone
= NULL
;
1790 node
->prev_sibling_clone
= NULL
;
1791 node
->clone_of
= NULL
;
1792 bitmap_obstack_release (NULL
);
1795 /* Once all functions from compilation unit are in memory, produce all clones
1796 and update all calls.
1797 We might also do this on demand if we don't want to bring all functions to
1798 memory prior compilation, but current WHOPR implementation does that and it is
1799 is bit easier to keep everything right in this order. */
1801 cgraph_materialize_all_clones (void)
1803 struct cgraph_node
*node
;
1804 bool stabilized
= false;
1806 if (cgraph_dump_file
)
1807 fprintf (cgraph_dump_file
, "Materializing clones\n");
1808 #ifdef ENABLE_CHECKING
1812 /* We can also do topological order, but number of iterations should be
1813 bounded by number of IPA passes since single IPA pass is probably not
1814 going to create clones of clones it created itself. */
1818 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1820 if (node
->clone_of
&& node
->decl
!= node
->clone_of
->decl
1821 && !gimple_has_body_p (node
->decl
))
1823 if (gimple_has_body_p (node
->clone_of
->decl
))
1825 if (cgraph_dump_file
)
1827 fprintf (cgraph_dump_file
, "clonning %s to %s\n",
1828 cgraph_node_name (node
->clone_of
),
1829 cgraph_node_name (node
));
1830 if (node
->clone
.tree_map
)
1833 fprintf (cgraph_dump_file
, " replace map: ");
1834 for (i
= 0; i
< VEC_length (ipa_replace_map_p
,
1835 node
->clone
.tree_map
);
1838 struct ipa_replace_map
*replace_info
;
1839 replace_info
= VEC_index (ipa_replace_map_p
,
1840 node
->clone
.tree_map
,
1842 print_generic_expr (cgraph_dump_file
, replace_info
->old_tree
, 0);
1843 fprintf (cgraph_dump_file
, " -> ");
1844 print_generic_expr (cgraph_dump_file
, replace_info
->new_tree
, 0);
1845 fprintf (cgraph_dump_file
, "%s%s;",
1846 replace_info
->replace_p
? "(replace)":"",
1847 replace_info
->ref_p
? "(ref)":"");
1849 fprintf (cgraph_dump_file
, "\n");
1851 if (node
->clone
.args_to_skip
)
1853 fprintf (cgraph_dump_file
, " args_to_skip: ");
1854 dump_bitmap (cgraph_dump_file
, node
->clone
.args_to_skip
);
1856 if (node
->clone
.args_to_skip
)
1858 fprintf (cgraph_dump_file
, " combined_args_to_skip:");
1859 dump_bitmap (cgraph_dump_file
, node
->clone
.combined_args_to_skip
);
1862 cgraph_materialize_clone (node
);
1869 if (cgraph_dump_file
)
1870 fprintf (cgraph_dump_file
, "Updating call sites\n");
1871 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1872 if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
1873 && (!node
->clone_of
|| node
->clone_of
->decl
!= node
->decl
))
1875 struct cgraph_edge
*e
;
1877 current_function_decl
= node
->decl
;
1878 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1879 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1881 tree decl
= gimple_call_fndecl (e
->call_stmt
);
1882 /* When function gets inlined, indirect inlining might've invented
1883 new edge for orginally indirect stmt. Since we are not
1884 preserving clones in the original form, we must not update here
1885 since other inline clones don't need to contain call to the same
1886 call. Inliner will do the substitution for us later. */
1887 if (decl
&& decl
!= e
->callee
->decl
)
1890 gimple_stmt_iterator gsi
;
1892 if (cgraph_dump_file
)
1894 fprintf (cgraph_dump_file
, "updating call of %s in %s:",
1895 cgraph_node_name (node
),
1896 cgraph_node_name (e
->callee
));
1897 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1900 if (e
->callee
->clone
.combined_args_to_skip
)
1901 new_stmt
= gimple_call_copy_skip_args (e
->call_stmt
,
1902 e
->callee
->clone
.combined_args_to_skip
);
1904 new_stmt
= e
->call_stmt
;
1905 if (gimple_vdef (new_stmt
)
1906 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1907 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1908 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1910 gsi
= gsi_for_stmt (e
->call_stmt
);
1911 gsi_replace (&gsi
, new_stmt
, true);
1913 /* Update EH information too, just in case. */
1914 maybe_clean_or_replace_eh_stmt (e
->call_stmt
, new_stmt
);
1916 cgraph_set_call_stmt_including_clones (node
, e
->call_stmt
, new_stmt
);
1918 if (cgraph_dump_file
)
1920 fprintf (cgraph_dump_file
, " updated to:");
1921 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1926 current_function_decl
= NULL
;
1927 #ifdef ENABLE_CHECKING
1928 verify_cgraph_node (node
);
1931 #ifdef ENABLE_CHECKING
1934 cgraph_remove_unreachable_nodes (false, cgraph_dump_file
);
1937 #include "gt-cgraphunit.h"