1 /* Callgraph based interprocedural optimizations.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011 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 call-graph construction and local function analysis takes
49 place here. Bodies of unreachable functions are released to
50 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"
126 #include "tree-pretty-print.h"
127 #include "gimple-pretty-print.h"
132 #include "function.h"
133 #include "ipa-prop.h"
135 #include "tree-iterator.h"
136 #include "tree-pass.h"
137 #include "tree-dump.h"
139 #include "coverage.h"
141 #include "ipa-inline.h"
142 #include "ipa-utils.h"
143 #include "lto-streamer.h"
145 static void cgraph_expand_all_functions (void);
146 static void cgraph_mark_functions_to_output (void);
147 static void cgraph_expand_function (struct cgraph_node
*);
148 static void cgraph_output_pending_asms (void);
150 FILE *cgraph_dump_file
;
152 /* Used for vtable lookup in thunk adjusting. */
153 static GTY (()) tree vtable_entry_type
;
155 /* Determine if function DECL is needed. That is, visible to something
156 either outside this translation unit, something magic in the system
160 cgraph_decide_is_function_needed (struct cgraph_node
*node
, tree decl
)
162 /* If the user told us it is used, then it must be so. */
163 if (node
->local
.externally_visible
)
166 /* ??? If the assembler name is set by hand, it is possible to assemble
167 the name later after finalizing the function and the fact is noticed
168 in assemble_name then. This is arguably a bug. */
169 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
170 && (!node
->thunk
.thunk_p
&& !node
->same_body_alias
)
171 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
174 /* With -fkeep-inline-functions we are keeping all inline functions except
175 for extern inline ones. */
176 if (flag_keep_inline_functions
177 && DECL_DECLARED_INLINE_P (decl
)
178 && !DECL_EXTERNAL (decl
)
179 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
182 /* If we decided it was needed before, but at the time we didn't have
183 the body of the function available, then it's still needed. We have
184 to go back and re-check its dependencies now. */
188 /* Externally visible functions must be output. The exception is
189 COMDAT functions that must be output only when they are needed.
191 When not optimizing, also output the static functions. (see
192 PR24561), but don't do so for always_inline functions, functions
193 declared inline and nested functions. These were optimized out
194 in the original implementation and it is unclear whether we want
195 to change the behavior here. */
196 if (((TREE_PUBLIC (decl
)
198 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
199 && !DECL_DECLARED_INLINE_P (decl
)
200 && !(DECL_CONTEXT (decl
)
201 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)))
202 && !flag_whole_program
204 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
210 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
211 functions into callgraph in a way so they look like ordinary reachable
212 functions inserted into callgraph already at construction time. */
215 cgraph_process_new_functions (void)
219 struct cgraph_node
*node
;
221 varpool_analyze_pending_decls ();
222 /* Note that this queue may grow as its being processed, as the new
223 functions may generate new ones. */
224 while (cgraph_new_nodes
)
226 node
= cgraph_new_nodes
;
228 cgraph_new_nodes
= cgraph_new_nodes
->next_needed
;
229 switch (cgraph_state
)
231 case CGRAPH_STATE_CONSTRUCTION
:
232 /* At construction time we just need to finalize function and move
233 it into reachable functions list. */
235 node
->next_needed
= NULL
;
236 cgraph_finalize_function (fndecl
, false);
237 cgraph_mark_reachable_node (node
);
239 cgraph_call_function_insertion_hooks (node
);
242 case CGRAPH_STATE_IPA
:
243 case CGRAPH_STATE_IPA_SSA
:
244 /* When IPA optimization already started, do all essential
245 transformations that has been already performed on the whole
246 cgraph but not on this function. */
248 gimple_register_cfg_hooks ();
250 cgraph_analyze_function (node
);
251 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
252 current_function_decl
= fndecl
;
253 if ((cgraph_state
== CGRAPH_STATE_IPA_SSA
254 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
255 /* When not optimizing, be sure we run early local passes anyway
258 execute_pass_list (pass_early_local_passes
.pass
.sub
);
260 compute_inline_parameters (node
, true);
261 free_dominance_info (CDI_POST_DOMINATORS
);
262 free_dominance_info (CDI_DOMINATORS
);
264 current_function_decl
= NULL
;
265 cgraph_call_function_insertion_hooks (node
);
268 case CGRAPH_STATE_EXPANSION
:
269 /* Functions created during expansion shall be compiled
272 cgraph_call_function_insertion_hooks (node
);
273 cgraph_expand_function (node
);
280 varpool_analyze_pending_decls ();
285 /* As an GCC extension we allow redefinition of the function. The
286 semantics when both copies of bodies differ is not well defined.
287 We replace the old body with new body so in unit at a time mode
288 we always use new body, while in normal mode we may end up with
289 old body inlined into some functions and new body expanded and
292 ??? It may make more sense to use one body for inlining and other
293 body for expanding the function but this is difficult to do. */
296 cgraph_reset_node (struct cgraph_node
*node
)
298 /* If node->process is set, then we have already begun whole-unit analysis.
299 This is *not* testing for whether we've already emitted the function.
300 That case can be sort-of legitimately seen with real function redefinition
301 errors. I would argue that the front end should never present us with
302 such a case, but don't enforce that for now. */
303 gcc_assert (!node
->process
);
305 /* Reset our data structures so we can analyze the function again. */
306 memset (&node
->local
, 0, sizeof (node
->local
));
307 memset (&node
->global
, 0, sizeof (node
->global
));
308 memset (&node
->rtl
, 0, sizeof (node
->rtl
));
309 node
->analyzed
= false;
310 node
->local
.finalized
= false;
312 cgraph_node_remove_callees (node
);
316 cgraph_lower_function (struct cgraph_node
*node
)
322 lower_nested_functions (node
->decl
);
323 gcc_assert (!node
->nested
);
325 tree_lowering_passes (node
->decl
);
326 node
->lowered
= true;
329 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
330 logic in effect. If NESTED is true, then our caller cannot stand to have
331 the garbage collector run at the moment. We would need to either create
332 a new GC context, or just not compile right now. */
335 cgraph_finalize_function (tree decl
, bool nested
)
337 struct cgraph_node
*node
= cgraph_get_create_node (decl
);
339 if (node
->local
.finalized
)
341 cgraph_reset_node (node
);
342 node
->local
.redefined_extern_inline
= true;
345 notice_global_symbol (decl
);
346 node
->local
.finalized
= true;
347 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
349 if (cgraph_decide_is_function_needed (node
, decl
))
350 cgraph_mark_needed_node (node
);
352 /* Since we reclaim unreachable nodes at the end of every language
353 level unit, we need to be conservative about possible entry points
355 if ((TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
356 || DECL_STATIC_CONSTRUCTOR (decl
)
357 || DECL_STATIC_DESTRUCTOR (decl
)
358 /* COMDAT virtual functions may be referenced by vtable from
359 other compilation unit. Still we want to devirtualize calls
360 to those so we need to analyze them.
361 FIXME: We should introduce may edges for this purpose and update
362 their handling in unreachable function removal and inliner too. */
363 || (DECL_VIRTUAL_P (decl
)
364 && optimize
&& (DECL_COMDAT (decl
) || DECL_EXTERNAL (decl
))))
365 cgraph_mark_reachable_node (node
);
367 /* If we've not yet emitted decl, tell the debug info about it. */
368 if (!TREE_ASM_WRITTEN (decl
))
369 (*debug_hooks
->deferred_inline_function
) (decl
);
371 /* Possibly warn about unused parameters. */
372 if (warn_unused_parameter
)
373 do_warn_unused_parameter (decl
);
379 /* C99 extern inline keywords allow changing of declaration after function
380 has been finalized. We need to re-decide if we want to mark the function as
384 cgraph_mark_if_needed (tree decl
)
386 struct cgraph_node
*node
= cgraph_get_node (decl
);
387 if (node
->local
.finalized
&& cgraph_decide_is_function_needed (node
, decl
))
388 cgraph_mark_needed_node (node
);
391 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
393 clone_of_p (struct cgraph_node
*node
, struct cgraph_node
*node2
)
395 node
= cgraph_function_or_thunk_node (node
, NULL
);
396 node2
= cgraph_function_or_thunk_node (node2
, NULL
);
397 while (node
!= node2
&& node2
)
398 node2
= node2
->clone_of
;
399 return node2
!= NULL
;
402 /* Verify edge E count and frequency. */
405 verify_edge_count_and_frequency (struct cgraph_edge
*e
)
407 bool error_found
= false;
410 error ("caller edge count is negative");
413 if (e
->frequency
< 0)
415 error ("caller edge frequency is negative");
418 if (e
->frequency
> CGRAPH_FREQ_MAX
)
420 error ("caller edge frequency is too large");
423 if (gimple_has_body_p (e
->caller
->decl
)
424 && !e
->caller
->global
.inlined_to
425 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
426 Remove this once edges are actualy removed from the function at that time. */
428 || (inline_edge_summary_vec
429 && ((VEC_length(inline_edge_summary_t
, inline_edge_summary_vec
)
430 <= (unsigned) e
->uid
)
431 || !inline_edge_summary (e
)->predicate
)))
433 != compute_call_stmt_bb_frequency (e
->caller
->decl
,
434 gimple_bb (e
->call_stmt
))))
436 error ("caller edge frequency %i does not match BB frequency %i",
438 compute_call_stmt_bb_frequency (e
->caller
->decl
,
439 gimple_bb (e
->call_stmt
)));
445 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
447 cgraph_debug_gimple_stmt (struct function
*this_cfun
, gimple stmt
)
449 /* debug_gimple_stmt needs correct cfun */
450 if (cfun
!= this_cfun
)
451 set_cfun (this_cfun
);
452 debug_gimple_stmt (stmt
);
455 /* Verify that call graph edge E corresponds to DECL from the associated
456 statement. Return true if the verification should fail. */
459 verify_edge_corresponds_to_fndecl (struct cgraph_edge
*e
, tree decl
)
461 struct cgraph_node
*node
;
463 if (!decl
|| e
->callee
->global
.inlined_to
)
465 node
= cgraph_get_node (decl
);
467 /* We do not know if a node from a different partition is an alias or what it
468 aliases and therefore cannot do the former_clone_of check reliably. */
469 if (!node
|| node
->in_other_partition
)
471 node
= cgraph_function_or_thunk_node (node
, NULL
);
473 if ((e
->callee
->former_clone_of
!= node
->decl
)
474 /* IPA-CP sometimes redirect edge to clone and then back to the former
475 function. This ping-pong has to go, eventaully. */
476 && (node
!= cgraph_function_or_thunk_node (e
->callee
, NULL
))
477 && !clone_of_p (node
, e
->callee
))
483 /* Verify cgraph nodes of given cgraph node. */
485 verify_cgraph_node (struct cgraph_node
*node
)
487 struct cgraph_edge
*e
;
488 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (node
->decl
);
489 basic_block this_block
;
490 gimple_stmt_iterator gsi
;
491 bool error_found
= false;
496 timevar_push (TV_CGRAPH_VERIFY
);
497 for (e
= node
->callees
; e
; e
= e
->next_callee
)
500 error ("aux field set for edge %s->%s",
501 identifier_to_locale (cgraph_node_name (e
->caller
)),
502 identifier_to_locale (cgraph_node_name (e
->callee
)));
507 error ("execution count is negative");
510 if (node
->global
.inlined_to
&& node
->local
.externally_visible
)
512 error ("externally visible inline clone");
515 if (node
->global
.inlined_to
&& node
->address_taken
)
517 error ("inline clone with address taken");
520 if (node
->global
.inlined_to
&& node
->needed
)
522 error ("inline clone is needed");
525 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
529 error ("aux field set for indirect edge from %s",
530 identifier_to_locale (cgraph_node_name (e
->caller
)));
533 if (!e
->indirect_unknown_callee
534 || !e
->indirect_info
)
536 error ("An indirect edge from %s is not marked as indirect or has "
537 "associated indirect_info, the corresponding statement is: ",
538 identifier_to_locale (cgraph_node_name (e
->caller
)));
539 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
543 for (e
= node
->callers
; e
; e
= e
->next_caller
)
545 if (verify_edge_count_and_frequency (e
))
547 if (!e
->inline_failed
)
549 if (node
->global
.inlined_to
550 != (e
->caller
->global
.inlined_to
551 ? e
->caller
->global
.inlined_to
: e
->caller
))
553 error ("inlined_to pointer is wrong");
556 if (node
->callers
->next_caller
)
558 error ("multiple inline callers");
563 if (node
->global
.inlined_to
)
565 error ("inlined_to pointer set for noninline callers");
569 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
570 if (verify_edge_count_and_frequency (e
))
572 if (!node
->callers
&& node
->global
.inlined_to
)
574 error ("inlined_to pointer is set but no predecessors found");
577 if (node
->global
.inlined_to
== node
)
579 error ("inlined_to pointer refers to itself");
583 if (!cgraph_get_node (node
->decl
))
585 error ("node not found in cgraph_hash");
591 struct cgraph_node
*n
;
592 for (n
= node
->clone_of
->clones
; n
; n
= n
->next_sibling_clone
)
597 error ("node has wrong clone_of");
603 struct cgraph_node
*n
;
604 for (n
= node
->clones
; n
; n
= n
->next_sibling_clone
)
605 if (n
->clone_of
!= node
)
609 error ("node has wrong clone list");
613 if ((node
->prev_sibling_clone
|| node
->next_sibling_clone
) && !node
->clone_of
)
615 error ("node is in clone list but it is not clone");
618 if (!node
->prev_sibling_clone
&& node
->clone_of
&& node
->clone_of
->clones
!= node
)
620 error ("node has wrong prev_clone pointer");
623 if (node
->prev_sibling_clone
&& node
->prev_sibling_clone
->next_sibling_clone
!= node
)
625 error ("double linked list of clones corrupted");
628 if (node
->same_comdat_group
)
630 struct cgraph_node
*n
= node
->same_comdat_group
;
632 if (!DECL_ONE_ONLY (node
->decl
))
634 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
639 error ("node is alone in a comdat group");
644 if (!n
->same_comdat_group
)
646 error ("same_comdat_group is not a circular list");
650 n
= n
->same_comdat_group
;
655 if (node
->analyzed
&& node
->alias
)
657 bool ref_found
= false;
663 error ("Alias has call edges");
666 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
); i
++)
667 if (ref
->use
!= IPA_REF_ALIAS
)
669 error ("Alias has non-alias refernece");
674 error ("Alias has more than one alias reference");
681 error ("Analyzed alias has no reference");
685 if (node
->analyzed
&& node
->thunk
.thunk_p
)
689 error ("No edge out of thunk node");
692 else if (node
->callees
->next_callee
)
694 error ("More than one edge out of thunk node");
697 if (gimple_has_body_p (node
->decl
))
699 error ("Thunk is not supposed to have body");
703 else if (node
->analyzed
&& gimple_has_body_p (node
->decl
)
704 && !TREE_ASM_WRITTEN (node
->decl
)
705 && (!DECL_EXTERNAL (node
->decl
) || node
->global
.inlined_to
)
710 /* The nodes we're interested in are never shared, so walk
711 the tree ignoring duplicates. */
712 struct pointer_set_t
*visited_nodes
= pointer_set_create ();
713 /* Reach the trees by walking over the CFG, and note the
714 enclosing basic-blocks in the call edges. */
715 FOR_EACH_BB_FN (this_block
, this_cfun
)
716 for (gsi
= gsi_start_bb (this_block
);
720 gimple stmt
= gsi_stmt (gsi
);
721 if (is_gimple_call (stmt
))
723 struct cgraph_edge
*e
= cgraph_edge (node
, stmt
);
724 tree decl
= gimple_call_fndecl (stmt
);
729 error ("shared call_stmt:");
730 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
733 if (!e
->indirect_unknown_callee
)
735 if (verify_edge_corresponds_to_fndecl (e
, decl
))
737 error ("edge points to wrong declaration:");
738 debug_tree (e
->callee
->decl
);
739 fprintf (stderr
," Instead of:");
746 error ("an indirect edge with unknown callee "
747 "corresponding to a call_stmt with "
748 "a known declaration:");
750 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
756 error ("missing callgraph edge for call stmt:");
757 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
762 pointer_set_destroy (visited_nodes
);
765 /* No CFG available?! */
768 for (e
= node
->callees
; e
; e
= e
->next_callee
)
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 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
780 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
784 error ("an indirect edge from %s has no corresponding call_stmt",
785 identifier_to_locale (cgraph_node_name (e
->caller
)));
786 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
794 dump_cgraph_node (stderr
, node
);
795 internal_error ("verify_cgraph_node failed");
797 timevar_pop (TV_CGRAPH_VERIFY
);
800 /* Verify whole cgraph structure. */
804 struct cgraph_node
*node
;
809 for (node
= cgraph_nodes
; node
; node
= node
->next
)
810 verify_cgraph_node (node
);
813 /* Output all asm statements we have stored up to be output. */
816 cgraph_output_pending_asms (void)
818 struct cgraph_asm_node
*can
;
823 for (can
= cgraph_asm_nodes
; can
; can
= can
->next
)
824 assemble_asm (can
->asm_str
);
825 cgraph_asm_nodes
= NULL
;
828 /* Analyze the function scheduled to be output. */
830 cgraph_analyze_function (struct cgraph_node
*node
)
832 tree save
= current_function_decl
;
833 tree decl
= node
->decl
;
835 if (node
->alias
&& node
->thunk
.alias
)
837 struct cgraph_node
*tgt
= cgraph_get_node (node
->thunk
.alias
);
838 if (!VEC_length (ipa_ref_t
, node
->ref_list
.references
))
839 ipa_record_reference (node
, NULL
, tgt
, NULL
, IPA_REF_ALIAS
, NULL
);
840 if (node
->same_body_alias
)
842 DECL_VIRTUAL_P (node
->decl
) = DECL_VIRTUAL_P (node
->thunk
.alias
);
843 DECL_DECLARED_INLINE_P (node
->decl
)
844 = DECL_DECLARED_INLINE_P (node
->thunk
.alias
);
845 DECL_DISREGARD_INLINE_LIMITS (node
->decl
)
846 = DECL_DISREGARD_INLINE_LIMITS (node
->thunk
.alias
);
849 /* Fixup visibility nonsences C++ frontend produce on same body aliases. */
850 if (TREE_PUBLIC (node
->decl
) && node
->same_body_alias
)
852 DECL_EXTERNAL (node
->decl
) = DECL_EXTERNAL (node
->thunk
.alias
);
853 if (DECL_ONE_ONLY (node
->thunk
.alias
))
855 DECL_COMDAT (node
->decl
) = DECL_COMDAT (node
->thunk
.alias
);
856 DECL_COMDAT_GROUP (node
->decl
) = DECL_COMDAT_GROUP (node
->thunk
.alias
);
857 if (DECL_ONE_ONLY (node
->thunk
.alias
) && !node
->same_comdat_group
)
859 struct cgraph_node
*tgt
= cgraph_get_node (node
->thunk
.alias
);
860 node
->same_comdat_group
= tgt
;
861 if (!tgt
->same_comdat_group
)
862 tgt
->same_comdat_group
= node
;
865 struct cgraph_node
*n
;
866 for (n
= tgt
->same_comdat_group
;
867 n
->same_comdat_group
!= tgt
;
868 n
= n
->same_comdat_group
)
870 n
->same_comdat_group
= node
;
875 cgraph_mark_reachable_node (cgraph_alias_aliased_node (node
));
876 if (node
->address_taken
)
877 cgraph_mark_address_taken_node (cgraph_alias_aliased_node (node
));
878 if (cgraph_decide_is_function_needed (node
, node
->decl
))
879 cgraph_mark_needed_node (node
);
881 else if (node
->thunk
.thunk_p
)
883 cgraph_create_edge (node
, cgraph_get_node (node
->thunk
.alias
),
884 NULL
, 0, CGRAPH_FREQ_BASE
);
888 current_function_decl
= decl
;
889 push_cfun (DECL_STRUCT_FUNCTION (decl
));
891 assign_assembler_name_if_neeeded (node
->decl
);
893 /* Make sure to gimplify bodies only once. During analyzing a
894 function we lower it, which will require gimplified nested
895 functions, so we can end up here with an already gimplified
897 if (!gimple_body (decl
))
898 gimplify_function_tree (decl
);
899 dump_function (TDI_generic
, decl
);
901 cgraph_lower_function (node
);
904 node
->analyzed
= true;
906 current_function_decl
= save
;
909 /* C++ frontend produce same body aliases all over the place, even before PCH
910 gets streamed out. It relies on us linking the aliases with their function
911 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
912 first produce aliases without links, but once C++ FE is sure he won't sream
913 PCH we build the links via this function. */
916 cgraph_process_same_body_aliases (void)
918 struct cgraph_node
*node
;
919 for (node
= cgraph_nodes
; node
; node
= node
->next
)
920 if (node
->same_body_alias
921 && !VEC_length (ipa_ref_t
, node
->ref_list
.references
))
923 struct cgraph_node
*tgt
= cgraph_get_node (node
->thunk
.alias
);
924 ipa_record_reference (node
, NULL
, tgt
, NULL
, IPA_REF_ALIAS
, NULL
);
926 same_body_aliases_done
= true;
929 /* Process attributes common for vars and functions. */
932 process_common_attributes (tree decl
)
934 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
936 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
938 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
939 "%<weakref%> attribute should be accompanied with"
940 " an %<alias%> attribute");
941 DECL_WEAK (decl
) = 0;
942 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
943 DECL_ATTRIBUTES (decl
));
947 /* Look for externally_visible and used attributes and mark cgraph nodes
950 We cannot mark the nodes at the point the attributes are processed (in
951 handle_*_attribute) because the copy of the declarations available at that
952 point may not be canonical. For example, in:
955 void f() __attribute__((used));
957 the declaration we see in handle_used_attribute will be the second
958 declaration -- but the front end will subsequently merge that declaration
959 with the original declaration and discard the second declaration.
961 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
964 void f() __attribute__((externally_visible));
968 So, we walk the nodes at the end of the translation unit, applying the
969 attributes at that point. */
972 process_function_and_variable_attributes (struct cgraph_node
*first
,
973 struct varpool_node
*first_var
)
975 struct cgraph_node
*node
;
976 struct varpool_node
*vnode
;
978 for (node
= cgraph_nodes
; node
!= first
; node
= node
->next
)
980 tree decl
= node
->decl
;
981 if (DECL_PRESERVE_P (decl
))
982 cgraph_mark_needed_node (node
);
983 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
984 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
))
985 && TREE_PUBLIC (node
->decl
))
987 if (node
->local
.finalized
)
988 cgraph_mark_needed_node (node
);
990 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
992 if (! TREE_PUBLIC (node
->decl
))
993 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
994 "%<externally_visible%>"
995 " attribute have effect only on public objects");
996 else if (node
->local
.finalized
)
997 cgraph_mark_needed_node (node
);
999 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
1000 && (node
->local
.finalized
&& !node
->alias
))
1002 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
1003 "%<weakref%> attribute ignored"
1004 " because function is defined");
1005 DECL_WEAK (decl
) = 0;
1006 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
1007 DECL_ATTRIBUTES (decl
));
1010 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
1011 && !DECL_DECLARED_INLINE_P (decl
)
1012 /* redefining extern inline function makes it DECL_UNINLINABLE. */
1013 && !DECL_UNINLINABLE (decl
))
1014 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
1015 "always_inline function might not be inlinable");
1017 process_common_attributes (decl
);
1019 for (vnode
= varpool_nodes
; vnode
!= first_var
; vnode
= vnode
->next
)
1021 tree decl
= vnode
->decl
;
1022 if (DECL_PRESERVE_P (decl
))
1024 vnode
->force_output
= true;
1025 if (vnode
->finalized
)
1026 varpool_mark_needed_node (vnode
);
1028 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1029 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
))
1030 && TREE_PUBLIC (vnode
->decl
))
1032 if (vnode
->finalized
)
1033 varpool_mark_needed_node (vnode
);
1035 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
1037 if (! TREE_PUBLIC (vnode
->decl
))
1038 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
1039 "%<externally_visible%>"
1040 " attribute have effect only on public objects");
1041 else if (vnode
->finalized
)
1042 varpool_mark_needed_node (vnode
);
1044 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
1046 && DECL_INITIAL (decl
))
1048 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
1049 "%<weakref%> attribute ignored"
1050 " because variable is initialized");
1051 DECL_WEAK (decl
) = 0;
1052 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
1053 DECL_ATTRIBUTES (decl
));
1055 process_common_attributes (decl
);
1059 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
1060 each reachable functions) and build cgraph.
1061 The function can be called multiple times after inserting new nodes
1062 into beginning of queue. Just the new part of queue is re-scanned then. */
1065 cgraph_analyze_functions (void)
1067 /* Keep track of already processed nodes when called multiple times for
1068 intermodule optimization. */
1069 static struct cgraph_node
*first_analyzed
;
1070 struct cgraph_node
*first_processed
= first_analyzed
;
1071 static struct varpool_node
*first_analyzed_var
;
1072 struct cgraph_node
*node
, *next
;
1074 bitmap_obstack_initialize (NULL
);
1075 process_function_and_variable_attributes (first_processed
,
1076 first_analyzed_var
);
1077 first_processed
= cgraph_nodes
;
1078 first_analyzed_var
= varpool_nodes
;
1079 varpool_analyze_pending_decls ();
1080 if (cgraph_dump_file
)
1082 fprintf (cgraph_dump_file
, "Initial entry points:");
1083 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= node
->next
)
1085 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1086 fprintf (cgraph_dump_file
, "\n");
1088 cgraph_process_new_functions ();
1090 /* Propagate reachability flag and lower representation of all reachable
1091 functions. In the future, lowering will introduce new functions and
1092 new entry points on the way (by template instantiation and virtual
1093 method table generation for instance). */
1094 while (cgraph_nodes_queue
)
1096 struct cgraph_edge
*edge
;
1097 tree decl
= cgraph_nodes_queue
->decl
;
1099 node
= cgraph_nodes_queue
;
1100 cgraph_nodes_queue
= cgraph_nodes_queue
->next_needed
;
1101 node
->next_needed
= NULL
;
1103 /* ??? It is possible to create extern inline function and later using
1104 weak alias attribute to kill its body. See
1105 gcc.c-torture/compile/20011119-1.c */
1106 if (!DECL_STRUCT_FUNCTION (decl
)
1107 && (!node
->alias
|| !node
->thunk
.alias
)
1108 && !node
->thunk
.thunk_p
)
1110 cgraph_reset_node (node
);
1111 node
->local
.redefined_extern_inline
= true;
1115 if (!node
->analyzed
)
1116 cgraph_analyze_function (node
);
1118 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1119 if (!edge
->callee
->reachable
)
1120 cgraph_mark_reachable_node (edge
->callee
);
1121 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
1122 if (!edge
->caller
->reachable
&& edge
->caller
->thunk
.thunk_p
)
1123 cgraph_mark_reachable_node (edge
->caller
);
1125 if (node
->same_comdat_group
)
1127 for (next
= node
->same_comdat_group
;
1129 next
= next
->same_comdat_group
)
1130 cgraph_mark_reachable_node (next
);
1133 /* If decl is a clone of an abstract function, mark that abstract
1134 function so that we don't release its body. The DECL_INITIAL() of that
1135 abstract function declaration will be later needed to output debug
1137 if (DECL_ABSTRACT_ORIGIN (decl
))
1139 struct cgraph_node
*origin_node
;
1140 origin_node
= cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl
));
1141 origin_node
->abstract_and_needed
= true;
1144 /* We finalize local static variables during constructing callgraph
1145 edges. Process their attributes too. */
1146 process_function_and_variable_attributes (first_processed
,
1147 first_analyzed_var
);
1148 first_processed
= cgraph_nodes
;
1149 first_analyzed_var
= varpool_nodes
;
1150 varpool_analyze_pending_decls ();
1151 cgraph_process_new_functions ();
1154 /* Collect entry points to the unit. */
1155 if (cgraph_dump_file
)
1157 fprintf (cgraph_dump_file
, "Unit entry points:");
1158 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= node
->next
)
1160 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1161 fprintf (cgraph_dump_file
, "\n\nInitial ");
1162 dump_cgraph (cgraph_dump_file
);
1163 dump_varpool (cgraph_dump_file
);
1166 if (cgraph_dump_file
)
1167 fprintf (cgraph_dump_file
, "\nReclaiming functions:");
1169 for (node
= cgraph_nodes
; node
!= first_analyzed
; node
= next
)
1171 tree decl
= node
->decl
;
1174 if (node
->local
.finalized
&& !gimple_has_body_p (decl
)
1175 && (!node
->alias
|| !node
->thunk
.alias
)
1176 && !node
->thunk
.thunk_p
)
1177 cgraph_reset_node (node
);
1179 if (!node
->reachable
1180 && (gimple_has_body_p (decl
) || node
->thunk
.thunk_p
1181 || (node
->alias
&& node
->thunk
.alias
)))
1183 if (cgraph_dump_file
)
1184 fprintf (cgraph_dump_file
, " %s", cgraph_node_name (node
));
1185 cgraph_remove_node (node
);
1189 node
->next_needed
= NULL
;
1190 gcc_assert (!node
->local
.finalized
|| node
->thunk
.thunk_p
1192 || gimple_has_body_p (decl
));
1193 gcc_assert (node
->analyzed
== node
->local
.finalized
);
1195 if (cgraph_dump_file
)
1197 fprintf (cgraph_dump_file
, "\n\nReclaimed ");
1198 dump_cgraph (cgraph_dump_file
);
1199 dump_varpool (cgraph_dump_file
);
1201 bitmap_obstack_release (NULL
);
1202 first_analyzed
= cgraph_nodes
;
1206 /* Translate the ugly representation of aliases as alias pairs into nice
1207 representation in callgraph. We don't handle all cases yet,
1211 handle_alias_pairs (void)
1215 struct cgraph_node
*target_node
;
1216 struct cgraph_node
*src_node
;
1217 struct varpool_node
*target_vnode
;
1219 for (i
= 0; VEC_iterate (alias_pair
, alias_pairs
, i
, p
);)
1221 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1222 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
))
1223 && (target_node
= cgraph_node_for_asm (p
->target
)) != NULL
)
1225 src_node
= cgraph_get_node (p
->decl
);
1226 if (src_node
&& src_node
->local
.finalized
)
1227 cgraph_reset_node (src_node
);
1228 /* Normally EXTERNAL flag is used to mark external inlines,
1229 however for aliases it seems to be allowed to use it w/o
1230 any meaning. See gcc.dg/attr-alias-3.c
1231 However for weakref we insist on EXTERNAL flag being set.
1232 See gcc.dg/attr-alias-5.c */
1233 if (DECL_EXTERNAL (p
->decl
))
1234 DECL_EXTERNAL (p
->decl
) = 0;
1235 cgraph_create_function_alias (p
->decl
, target_node
->decl
);
1236 VEC_unordered_remove (alias_pair
, alias_pairs
, i
);
1238 else if (TREE_CODE (p
->decl
) == VAR_DECL
1239 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
))
1240 && (target_vnode
= varpool_node_for_asm (p
->target
)) != NULL
)
1242 /* Normally EXTERNAL flag is used to mark external inlines,
1243 however for aliases it seems to be allowed to use it w/o
1244 any meaning. See gcc.dg/attr-alias-3.c
1245 However for weakref we insist on EXTERNAL flag being set.
1246 See gcc.dg/attr-alias-5.c */
1247 if (DECL_EXTERNAL (p
->decl
))
1248 DECL_EXTERNAL (p
->decl
) = 0;
1249 varpool_create_variable_alias (p
->decl
, target_vnode
->decl
);
1250 VEC_unordered_remove (alias_pair
, alias_pairs
, i
);
1255 fprintf (dump_file
, "Unhandled alias %s->%s\n",
1256 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
1257 IDENTIFIER_POINTER (p
->target
));
1265 /* Analyze the whole compilation unit once it is parsed completely. */
1268 cgraph_finalize_compilation_unit (void)
1270 timevar_push (TV_CGRAPH
);
1272 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1274 lto_streamer_hooks_init ();
1276 /* If we're here there's no current function anymore. Some frontends
1277 are lazy in clearing these. */
1278 current_function_decl
= NULL
;
1281 /* Do not skip analyzing the functions if there were errors, we
1282 miss diagnostics for following functions otherwise. */
1284 /* Emit size functions we didn't inline. */
1285 finalize_size_functions ();
1287 /* Mark alias targets necessary and emit diagnostics. */
1288 finish_aliases_1 ();
1289 handle_alias_pairs ();
1293 fprintf (stderr
, "\nAnalyzing compilation unit\n");
1297 if (flag_dump_passes
)
1300 /* Gimplify and lower all functions, compute reachability and
1301 remove unreachable nodes. */
1302 cgraph_analyze_functions ();
1304 /* Mark alias targets necessary and emit diagnostics. */
1305 finish_aliases_1 ();
1306 handle_alias_pairs ();
1308 /* Gimplify and lower thunks. */
1309 cgraph_analyze_functions ();
1311 /* Finally drive the pass manager. */
1314 timevar_pop (TV_CGRAPH
);
1318 /* Figure out what functions we want to assemble. */
1321 cgraph_mark_functions_to_output (void)
1323 struct cgraph_node
*node
;
1324 #ifdef ENABLE_CHECKING
1325 bool check_same_comdat_groups
= false;
1327 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1328 gcc_assert (!node
->process
);
1331 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1333 tree decl
= node
->decl
;
1334 struct cgraph_edge
*e
;
1336 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1340 for (e
= node
->callers
; e
; e
= e
->next_caller
)
1341 if (e
->inline_failed
)
1344 /* We need to output all local functions that are used and not
1345 always inlined, as well as those that are reachable from
1346 outside the current compilation unit. */
1348 && !node
->thunk
.thunk_p
1350 && !node
->global
.inlined_to
1351 && (!cgraph_only_called_directly_p (node
)
1352 || ((e
|| ipa_ref_has_aliases_p (&node
->ref_list
))
1353 && node
->reachable
))
1354 && !TREE_ASM_WRITTEN (decl
)
1355 && !DECL_EXTERNAL (decl
))
1358 if (node
->same_comdat_group
)
1360 struct cgraph_node
*next
;
1361 for (next
= node
->same_comdat_group
;
1363 next
= next
->same_comdat_group
)
1364 if (!next
->thunk
.thunk_p
&& !next
->alias
)
1368 else if (node
->same_comdat_group
)
1370 #ifdef ENABLE_CHECKING
1371 check_same_comdat_groups
= true;
1376 /* We should've reclaimed all functions that are not needed. */
1377 #ifdef ENABLE_CHECKING
1378 if (!node
->global
.inlined_to
1379 && gimple_has_body_p (decl
)
1380 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1381 are inside partition, we can end up not removing the body since we no longer
1382 have analyzed node pointing to it. */
1383 && !node
->in_other_partition
1385 && !DECL_EXTERNAL (decl
))
1387 dump_cgraph_node (stderr
, node
);
1388 internal_error ("failed to reclaim unneeded function");
1391 gcc_assert (node
->global
.inlined_to
1392 || !gimple_has_body_p (decl
)
1393 || node
->in_other_partition
1394 || DECL_EXTERNAL (decl
));
1399 #ifdef ENABLE_CHECKING
1400 if (check_same_comdat_groups
)
1401 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1402 if (node
->same_comdat_group
&& !node
->process
)
1404 tree decl
= node
->decl
;
1405 if (!node
->global
.inlined_to
1406 && gimple_has_body_p (decl
)
1407 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1408 are inside partition, we can end up not removing the body since we no longer
1409 have analyzed node pointing to it. */
1410 && !node
->in_other_partition
1411 && !DECL_EXTERNAL (decl
))
1413 dump_cgraph_node (stderr
, node
);
1414 internal_error ("failed to reclaim unneeded functionin same comdat group");
1420 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1421 in lowered gimple form.
1423 Set current_function_decl and cfun to newly constructed empty function body.
1424 return basic block in the function body. */
1427 init_lowered_empty_function (tree decl
)
1431 current_function_decl
= decl
;
1432 allocate_struct_function (decl
, false);
1433 gimple_register_cfg_hooks ();
1434 init_empty_tree_cfg ();
1435 init_tree_ssa (cfun
);
1436 init_ssa_operands ();
1437 cfun
->gimple_df
->in_ssa_p
= true;
1438 DECL_INITIAL (decl
) = make_node (BLOCK
);
1440 DECL_SAVED_TREE (decl
) = error_mark_node
;
1441 cfun
->curr_properties
|=
1442 (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_cfg
| PROP_referenced_vars
|
1443 PROP_ssa
| PROP_gimple_any
);
1445 /* Create BB for body of the function and connect it properly. */
1446 bb
= create_basic_block (NULL
, (void *) 0, ENTRY_BLOCK_PTR
);
1447 make_edge (ENTRY_BLOCK_PTR
, bb
, 0);
1448 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
1453 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1454 offset indicated by VIRTUAL_OFFSET, if that is
1455 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1456 zero for a result adjusting thunk. */
1459 thunk_adjust (gimple_stmt_iterator
* bsi
,
1460 tree ptr
, bool this_adjusting
,
1461 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1467 && fixed_offset
!= 0)
1469 stmt
= gimple_build_assign
1470 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1473 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1476 /* If there's a virtual offset, look up that value in the vtable and
1477 adjust the pointer again. */
1484 if (!vtable_entry_type
)
1486 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1487 TREE_TYPE (vfunc_type
) = integer_type_node
;
1488 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1489 layout_type (vfunc_type
);
1491 vtable_entry_type
= build_pointer_type (vfunc_type
);
1495 create_tmp_var (build_pointer_type
1496 (build_pointer_type (vtable_entry_type
)), "vptr");
1498 /* The vptr is always at offset zero in the object. */
1499 stmt
= gimple_build_assign (vtabletmp
,
1500 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1502 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1503 mark_symbols_for_renaming (stmt
);
1504 find_referenced_vars_in (stmt
);
1506 /* Form the vtable address. */
1507 vtabletmp2
= create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1509 stmt
= gimple_build_assign (vtabletmp2
,
1510 build_simple_mem_ref (vtabletmp
));
1511 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1512 mark_symbols_for_renaming (stmt
);
1513 find_referenced_vars_in (stmt
);
1515 /* Find the entry with the vcall offset. */
1516 stmt
= gimple_build_assign (vtabletmp2
,
1517 fold_build_pointer_plus_loc (input_location
,
1520 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1522 /* Get the offset itself. */
1523 vtabletmp3
= create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1525 stmt
= gimple_build_assign (vtabletmp3
,
1526 build_simple_mem_ref (vtabletmp2
));
1527 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1528 mark_symbols_for_renaming (stmt
);
1529 find_referenced_vars_in (stmt
);
1531 /* Adjust the `this' pointer. */
1532 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1533 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1534 GSI_CONTINUE_LINKING
);
1538 && fixed_offset
!= 0)
1539 /* Adjust the pointer by the constant. */
1543 if (TREE_CODE (ptr
) == VAR_DECL
)
1547 ptrtmp
= create_tmp_var (TREE_TYPE (ptr
), "ptr");
1548 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1549 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1550 mark_symbols_for_renaming (stmt
);
1551 find_referenced_vars_in (stmt
);
1553 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1554 ptrtmp
, fixed_offset
);
1557 /* Emit the statement and gimplify the adjustment expression. */
1558 ret
= create_tmp_var (TREE_TYPE (ptr
), "adjusted_this");
1559 stmt
= gimple_build_assign (ret
, ptr
);
1560 mark_symbols_for_renaming (stmt
);
1561 find_referenced_vars_in (stmt
);
1562 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1567 /* Produce assembler for thunk NODE. */
1570 assemble_thunk (struct cgraph_node
*node
)
1572 bool this_adjusting
= node
->thunk
.this_adjusting
;
1573 HOST_WIDE_INT fixed_offset
= node
->thunk
.fixed_offset
;
1574 HOST_WIDE_INT virtual_value
= node
->thunk
.virtual_value
;
1575 tree virtual_offset
= NULL
;
1576 tree alias
= node
->thunk
.alias
;
1577 tree thunk_fndecl
= node
->decl
;
1578 tree a
= DECL_ARGUMENTS (thunk_fndecl
);
1580 current_function_decl
= thunk_fndecl
;
1582 /* Ensure thunks are emitted in their correct sections. */
1583 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
1586 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1587 virtual_value
, alias
))
1591 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1593 DECL_RESULT (thunk_fndecl
)
1594 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1595 RESULT_DECL
, 0, restype
);
1596 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1598 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1600 fn_block
= make_node (BLOCK
);
1601 BLOCK_VARS (fn_block
) = a
;
1602 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1603 init_function_start (thunk_fndecl
);
1605 assemble_start_function (thunk_fndecl
, fnname
);
1607 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1608 fixed_offset
, virtual_value
, alias
);
1610 assemble_end_function (thunk_fndecl
, fnname
);
1611 init_insn_lengths ();
1612 free_after_compilation (cfun
);
1614 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1615 node
->thunk
.thunk_p
= false;
1616 node
->analyzed
= false;
1621 basic_block bb
, then_bb
, else_bb
, return_bb
;
1622 gimple_stmt_iterator bsi
;
1628 VEC(tree
, heap
) *vargs
;
1633 DECL_IGNORED_P (thunk_fndecl
) = 1;
1634 bitmap_obstack_initialize (NULL
);
1636 if (node
->thunk
.virtual_offset_p
)
1637 virtual_offset
= size_int (virtual_value
);
1639 /* Build the return declaration for the function. */
1640 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1641 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1643 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1644 DECL_ARTIFICIAL (resdecl
) = 1;
1645 DECL_IGNORED_P (resdecl
) = 1;
1646 DECL_RESULT (thunk_fndecl
) = resdecl
;
1649 resdecl
= DECL_RESULT (thunk_fndecl
);
1651 bb
= then_bb
= else_bb
= return_bb
= init_lowered_empty_function (thunk_fndecl
);
1653 bsi
= gsi_start_bb (bb
);
1655 /* Build call to the function being thunked. */
1656 if (!VOID_TYPE_P (restype
))
1658 if (!is_gimple_reg_type (restype
))
1661 add_local_decl (cfun
, restmp
);
1662 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1665 restmp
= create_tmp_var_raw (restype
, "retval");
1668 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1670 vargs
= VEC_alloc (tree
, heap
, nargs
);
1672 VEC_quick_push (tree
, vargs
,
1677 VEC_quick_push (tree
, vargs
, a
);
1678 for (i
= 1, arg
= DECL_CHAIN (a
); i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1679 VEC_quick_push (tree
, vargs
, arg
);
1680 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1681 VEC_free (tree
, heap
, vargs
);
1682 gimple_call_set_cannot_inline (call
, true);
1683 gimple_call_set_from_thunk (call
, true);
1685 gimple_call_set_lhs (call
, restmp
);
1686 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1687 mark_symbols_for_renaming (call
);
1688 find_referenced_vars_in (call
);
1691 if (restmp
&& !this_adjusting
)
1693 tree true_label
= NULL_TREE
;
1695 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1698 /* If the return type is a pointer, we need to
1699 protect against NULL. We know there will be an
1700 adjustment, because that's why we're emitting a
1702 then_bb
= create_basic_block (NULL
, (void *) 0, bb
);
1703 return_bb
= create_basic_block (NULL
, (void *) 0, then_bb
);
1704 else_bb
= create_basic_block (NULL
, (void *) 0, else_bb
);
1705 remove_edge (single_succ_edge (bb
));
1706 true_label
= gimple_block_label (then_bb
);
1707 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1708 build_zero_cst (TREE_TYPE (restmp
)),
1709 NULL_TREE
, NULL_TREE
);
1710 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1711 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1712 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1713 make_edge (return_bb
, EXIT_BLOCK_PTR
, 0);
1714 make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1715 make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1716 bsi
= gsi_last_bb (then_bb
);
1719 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1720 fixed_offset
, virtual_offset
);
1724 bsi
= gsi_last_bb (else_bb
);
1725 stmt
= gimple_build_assign (restmp
,
1726 build_zero_cst (TREE_TYPE (restmp
)));
1727 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1728 bsi
= gsi_last_bb (return_bb
);
1732 gimple_call_set_tail (call
, true);
1734 /* Build return value. */
1735 ret
= gimple_build_return (restmp
);
1736 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1738 delete_unreachable_blocks ();
1739 update_ssa (TODO_update_ssa
);
1741 /* Since we want to emit the thunk, we explicitly mark its name as
1743 node
->thunk
.thunk_p
= false;
1744 cgraph_node_remove_callees (node
);
1745 cgraph_add_new_function (thunk_fndecl
, true);
1746 bitmap_obstack_release (NULL
);
1748 current_function_decl
= NULL
;
1753 /* Assemble thunks and aliases asociated to NODE. */
1756 assemble_thunks_and_aliases (struct cgraph_node
*node
)
1758 struct cgraph_edge
*e
;
1760 struct ipa_ref
*ref
;
1762 for (e
= node
->callers
; e
;)
1763 if (e
->caller
->thunk
.thunk_p
)
1765 struct cgraph_node
*thunk
= e
->caller
;
1768 assemble_thunks_and_aliases (thunk
);
1769 assemble_thunk (thunk
);
1773 for (i
= 0; ipa_ref_list_refering_iterate (&node
->ref_list
, i
, ref
); i
++)
1774 if (ref
->use
== IPA_REF_ALIAS
)
1776 struct cgraph_node
*alias
= ipa_ref_refering_node (ref
);
1777 bool saved_written
= TREE_ASM_WRITTEN (alias
->thunk
.alias
);
1779 /* Force assemble_alias to really output the alias this time instead
1780 of buffering it in same alias pairs. */
1781 TREE_ASM_WRITTEN (alias
->thunk
.alias
) = 1;
1782 assemble_alias (alias
->decl
,
1783 DECL_ASSEMBLER_NAME (alias
->thunk
.alias
));
1784 assemble_thunks_and_aliases (alias
);
1785 TREE_ASM_WRITTEN (alias
->thunk
.alias
) = saved_written
;
1789 /* Expand function specified by NODE. */
1792 cgraph_expand_function (struct cgraph_node
*node
)
1794 tree decl
= node
->decl
;
1796 /* We ought to not compile any inline clones. */
1797 gcc_assert (!node
->global
.inlined_to
);
1799 announce_function (decl
);
1801 assemble_thunks_and_aliases (node
);
1802 gcc_assert (node
->lowered
);
1804 /* Generate RTL for the body of DECL. */
1805 tree_rest_of_compilation (decl
);
1807 /* Make sure that BE didn't give up on compiling. */
1808 gcc_assert (TREE_ASM_WRITTEN (decl
));
1809 current_function_decl
= NULL
;
1810 gcc_assert (!cgraph_preserve_function_body_p (node
));
1811 cgraph_release_function_body (node
);
1812 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1813 points to the dead function body. */
1814 cgraph_node_remove_callees (node
);
1816 cgraph_function_flags_ready
= true;
1819 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1822 cgraph_inline_p (struct cgraph_edge
*e
, cgraph_inline_failed_t
*reason
)
1824 *reason
= e
->inline_failed
;
1825 return !e
->inline_failed
;
1830 /* Expand all functions that must be output.
1832 Attempt to topologically sort the nodes so function is output when
1833 all called functions are already assembled to allow data to be
1834 propagated across the callgraph. Use a stack to get smaller distance
1835 between a function and its callees (later we may choose to use a more
1836 sophisticated algorithm for function reordering; we will likely want
1837 to use subsections to make the output functions appear in top-down
1841 cgraph_expand_all_functions (void)
1843 struct cgraph_node
*node
;
1844 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1845 int order_pos
, new_order_pos
= 0;
1848 order_pos
= ipa_reverse_postorder (order
);
1849 gcc_assert (order_pos
== cgraph_n_nodes
);
1851 /* Garbage collector may remove inline clones we eliminate during
1852 optimization. So we must be sure to not reference them. */
1853 for (i
= 0; i
< order_pos
; i
++)
1854 if (order
[i
]->process
)
1855 order
[new_order_pos
++] = order
[i
];
1857 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1862 gcc_assert (node
->reachable
);
1864 cgraph_expand_function (node
);
1867 cgraph_process_new_functions ();
1873 /* This is used to sort the node types by the cgraph order number. */
1875 enum cgraph_order_sort_kind
1877 ORDER_UNDEFINED
= 0,
1883 struct cgraph_order_sort
1885 enum cgraph_order_sort_kind kind
;
1888 struct cgraph_node
*f
;
1889 struct varpool_node
*v
;
1890 struct cgraph_asm_node
*a
;
1894 /* Output all functions, variables, and asm statements in the order
1895 according to their order fields, which is the order in which they
1896 appeared in the file. This implements -fno-toplevel-reorder. In
1897 this mode we may output functions and variables which don't really
1898 need to be output. */
1901 cgraph_output_in_order (void)
1904 struct cgraph_order_sort
*nodes
;
1906 struct cgraph_node
*pf
;
1907 struct varpool_node
*pv
;
1908 struct cgraph_asm_node
*pa
;
1911 nodes
= XCNEWVEC (struct cgraph_order_sort
, max
);
1913 varpool_analyze_pending_decls ();
1915 for (pf
= cgraph_nodes
; pf
; pf
= pf
->next
)
1917 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
1920 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1921 nodes
[i
].kind
= ORDER_FUNCTION
;
1926 for (pv
= varpool_nodes_queue
; pv
; pv
= pv
->next_needed
)
1929 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1930 nodes
[i
].kind
= ORDER_VAR
;
1934 for (pa
= cgraph_asm_nodes
; pa
; pa
= pa
->next
)
1937 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1938 nodes
[i
].kind
= ORDER_ASM
;
1942 /* In toplevel reorder mode we output all statics; mark them as needed. */
1943 for (i
= 0; i
< max
; ++i
)
1945 if (nodes
[i
].kind
== ORDER_VAR
)
1947 varpool_mark_needed_node (nodes
[i
].u
.v
);
1950 varpool_empty_needed_queue ();
1952 for (i
= 0; i
< max
; ++i
)
1953 if (nodes
[i
].kind
== ORDER_VAR
)
1954 varpool_finalize_named_section_flags (nodes
[i
].u
.v
);
1956 for (i
= 0; i
< max
; ++i
)
1958 switch (nodes
[i
].kind
)
1960 case ORDER_FUNCTION
:
1961 nodes
[i
].u
.f
->process
= 0;
1962 cgraph_expand_function (nodes
[i
].u
.f
);
1966 varpool_assemble_decl (nodes
[i
].u
.v
);
1970 assemble_asm (nodes
[i
].u
.a
->asm_str
);
1973 case ORDER_UNDEFINED
:
1981 cgraph_asm_nodes
= NULL
;
1985 /* Return true when function body of DECL still needs to be kept around
1986 for later re-use. */
1988 cgraph_preserve_function_body_p (struct cgraph_node
*node
)
1990 gcc_assert (cgraph_global_info_ready
);
1991 gcc_assert (!node
->alias
&& !node
->thunk
.thunk_p
);
1993 /* Look if there is any clone around. */
2003 current_function_decl
= NULL
;
2004 gimple_register_cfg_hooks ();
2005 bitmap_obstack_initialize (NULL
);
2007 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2011 execute_ipa_pass_list (all_small_ipa_passes
);
2016 /* We never run removal of unreachable nodes after early passes. This is
2017 because TODO is run before the subpasses. It is important to remove
2018 the unreachable functions to save works at IPA level and to get LTO
2019 symbol tables right. */
2020 cgraph_remove_unreachable_nodes (true, cgraph_dump_file
);
2022 /* If pass_all_early_optimizations was not scheduled, the state of
2023 the cgraph will not be properly updated. Update it now. */
2024 if (cgraph_state
< CGRAPH_STATE_IPA_SSA
)
2025 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
2029 /* Generate coverage variables and constructors. */
2032 /* Process new functions added. */
2034 current_function_decl
= NULL
;
2035 cgraph_process_new_functions ();
2037 execute_ipa_summary_passes
2038 ((struct ipa_opt_pass_d
*) all_regular_ipa_passes
);
2041 /* Some targets need to handle LTO assembler output specially. */
2042 if (flag_generate_lto
)
2043 targetm
.asm_out
.lto_start ();
2045 execute_ipa_summary_passes ((struct ipa_opt_pass_d
*) all_lto_gen_passes
);
2048 ipa_write_summaries ();
2050 if (flag_generate_lto
)
2051 targetm
.asm_out
.lto_end ();
2053 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2054 execute_ipa_pass_list (all_regular_ipa_passes
);
2055 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2057 bitmap_obstack_release (NULL
);
2061 /* Perform simple optimizations based on callgraph. */
2064 cgraph_optimize (void)
2069 #ifdef ENABLE_CHECKING
2073 /* Frontend may output common variables after the unit has been finalized.
2074 It is safe to deal with them here as they are always zero initialized. */
2075 varpool_analyze_pending_decls ();
2077 timevar_push (TV_CGRAPHOPT
);
2078 if (pre_ipa_mem_report
)
2080 fprintf (stderr
, "Memory consumption before IPA\n");
2081 dump_memory_report (false);
2084 fprintf (stderr
, "Performing interprocedural optimizations\n");
2085 cgraph_state
= CGRAPH_STATE_IPA
;
2087 /* Don't run the IPA passes if there was any error or sorry messages. */
2091 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2093 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2095 timevar_pop (TV_CGRAPHOPT
);
2099 /* This pass remove bodies of extern inline functions we never inlined.
2100 Do this later so other IPA passes see what is really going on. */
2101 cgraph_remove_unreachable_nodes (false, dump_file
);
2102 cgraph_global_info_ready
= true;
2103 if (cgraph_dump_file
)
2105 fprintf (cgraph_dump_file
, "Optimized ");
2106 dump_cgraph (cgraph_dump_file
);
2107 dump_varpool (cgraph_dump_file
);
2109 if (post_ipa_mem_report
)
2111 fprintf (stderr
, "Memory consumption after IPA\n");
2112 dump_memory_report (false);
2114 timevar_pop (TV_CGRAPHOPT
);
2116 /* Output everything. */
2117 (*debug_hooks
->assembly_start
) ();
2119 fprintf (stderr
, "Assembling functions:\n");
2120 #ifdef ENABLE_CHECKING
2124 cgraph_materialize_all_clones ();
2125 bitmap_obstack_initialize (NULL
);
2126 execute_ipa_pass_list (all_late_ipa_passes
);
2127 cgraph_remove_unreachable_nodes (true, dump_file
);
2128 #ifdef ENABLE_CHECKING
2131 bitmap_obstack_release (NULL
);
2132 cgraph_mark_functions_to_output ();
2134 cgraph_state
= CGRAPH_STATE_EXPANSION
;
2135 if (!flag_toplevel_reorder
)
2136 cgraph_output_in_order ();
2139 cgraph_output_pending_asms ();
2141 cgraph_expand_all_functions ();
2142 varpool_remove_unreferenced_decls ();
2144 varpool_assemble_pending_decls ();
2146 cgraph_process_new_functions ();
2147 cgraph_state
= CGRAPH_STATE_FINISHED
;
2149 if (cgraph_dump_file
)
2151 fprintf (cgraph_dump_file
, "\nFinal ");
2152 dump_cgraph (cgraph_dump_file
);
2153 dump_varpool (cgraph_dump_file
);
2155 #ifdef ENABLE_CHECKING
2157 /* Double check that all inline clones are gone and that all
2158 function bodies have been released from memory. */
2161 struct cgraph_node
*node
;
2162 bool error_found
= false;
2164 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2166 && (node
->global
.inlined_to
2167 || gimple_has_body_p (node
->decl
)))
2170 dump_cgraph_node (stderr
, node
);
2173 internal_error ("nodes with unreleased memory found");
2181 if (!cgraph_dump_file
)
2182 cgraph_dump_file
= dump_begin (TDI_cgraph
, NULL
);
2185 /* The edges representing the callers of the NEW_VERSION node were
2186 fixed by cgraph_function_versioning (), now the call_expr in their
2187 respective tree code should be updated to call the NEW_VERSION. */
2190 update_call_expr (struct cgraph_node
*new_version
)
2192 struct cgraph_edge
*e
;
2194 gcc_assert (new_version
);
2196 /* Update the call expr on the edges to call the new version. */
2197 for (e
= new_version
->callers
; e
; e
= e
->next_caller
)
2199 struct function
*inner_function
= DECL_STRUCT_FUNCTION (e
->caller
->decl
);
2200 gimple_call_set_fndecl (e
->call_stmt
, new_version
->decl
);
2201 maybe_clean_eh_stmt_fn (inner_function
, e
->call_stmt
);
2206 /* Create a new cgraph node which is the new version of
2207 OLD_VERSION node. REDIRECT_CALLERS holds the callers
2208 edges which should be redirected to point to
2209 NEW_VERSION. ALL the callees edges of OLD_VERSION
2210 are cloned to the new version node. Return the new
2213 If non-NULL BLOCK_TO_COPY determine what basic blocks
2214 was copied to prevent duplications of calls that are dead
2217 static struct cgraph_node
*
2218 cgraph_copy_node_for_versioning (struct cgraph_node
*old_version
,
2220 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
2223 struct cgraph_node
*new_version
;
2224 struct cgraph_edge
*e
;
2227 gcc_assert (old_version
);
2229 new_version
= cgraph_create_node (new_decl
);
2231 new_version
->analyzed
= true;
2232 new_version
->local
= old_version
->local
;
2233 new_version
->local
.externally_visible
= false;
2234 new_version
->local
.local
= true;
2235 new_version
->global
= old_version
->global
;
2236 new_version
->rtl
= old_version
->rtl
;
2237 new_version
->reachable
= true;
2238 new_version
->count
= old_version
->count
;
2240 for (e
= old_version
->callees
; e
; e
=e
->next_callee
)
2242 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
2243 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
2244 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
2247 for (e
= old_version
->indirect_calls
; e
; e
=e
->next_callee
)
2249 || bitmap_bit_p (bbs_to_copy
, gimple_bb (e
->call_stmt
)->index
))
2250 cgraph_clone_edge (e
, new_version
, e
->call_stmt
,
2251 e
->lto_stmt_uid
, REG_BR_PROB_BASE
,
2254 FOR_EACH_VEC_ELT (cgraph_edge_p
, redirect_callers
, i
, e
)
2256 /* Redirect calls to the old version node to point to its new
2258 cgraph_redirect_edge_callee (e
, new_version
);
2264 /* Perform function versioning.
2265 Function versioning includes copying of the tree and
2266 a callgraph update (creating a new cgraph node and updating
2267 its callees and callers).
2269 REDIRECT_CALLERS varray includes the edges to be redirected
2272 TREE_MAP is a mapping of tree nodes we want to replace with
2273 new ones (according to results of prior analysis).
2274 OLD_VERSION_NODE is the node that is versioned.
2275 It returns the new version's cgraph node.
2276 If non-NULL ARGS_TO_SKIP determine function parameters to remove
2278 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
2279 If non_NULL NEW_ENTRY determine new entry BB of the clone. */
2281 struct cgraph_node
*
2282 cgraph_function_versioning (struct cgraph_node
*old_version_node
,
2283 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
2284 VEC (ipa_replace_map_p
,gc
)* tree_map
,
2285 bitmap args_to_skip
,
2287 basic_block new_entry_block
,
2288 const char *clone_name
)
2290 tree old_decl
= old_version_node
->decl
;
2291 struct cgraph_node
*new_version_node
= NULL
;
2294 if (!tree_versionable_function_p (old_decl
))
2297 gcc_assert (old_version_node
->local
.can_change_signature
|| !args_to_skip
);
2299 /* Make a new FUNCTION_DECL tree node for the
2302 new_decl
= copy_node (old_decl
);
2304 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
);
2306 /* Generate a new name for the new version. */
2307 DECL_NAME (new_decl
) = clone_function_name (old_decl
, clone_name
);
2308 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
2309 SET_DECL_RTL (new_decl
, NULL
);
2311 /* Create the new version's call-graph node.
2312 and update the edges of the new node. */
2314 cgraph_copy_node_for_versioning (old_version_node
, new_decl
,
2315 redirect_callers
, bbs_to_copy
);
2317 /* Copy the OLD_VERSION_NODE function tree to the new version. */
2318 tree_function_versioning (old_decl
, new_decl
, tree_map
, false, args_to_skip
,
2319 bbs_to_copy
, new_entry_block
);
2321 /* Update the new version's properties.
2322 Make The new version visible only within this translation unit. Make sure
2323 that is not weak also.
2324 ??? We cannot use COMDAT linkage because there is no
2325 ABI support for this. */
2326 cgraph_make_decl_local (new_version_node
->decl
);
2327 DECL_VIRTUAL_P (new_version_node
->decl
) = 0;
2328 new_version_node
->local
.externally_visible
= 0;
2329 new_version_node
->local
.local
= 1;
2330 new_version_node
->lowered
= true;
2332 /* Update the call_expr on the edges to call the new version node. */
2333 update_call_expr (new_version_node
);
2335 cgraph_call_function_insertion_hooks (new_version_node
);
2336 return new_version_node
;
2339 /* Given virtual clone, turn it into actual clone. */
2341 cgraph_materialize_clone (struct cgraph_node
*node
)
2343 bitmap_obstack_initialize (NULL
);
2344 node
->former_clone_of
= node
->clone_of
->decl
;
2345 if (node
->clone_of
->former_clone_of
)
2346 node
->former_clone_of
= node
->clone_of
->former_clone_of
;
2347 /* Copy the OLD_VERSION_NODE function tree to the new version. */
2348 tree_function_versioning (node
->clone_of
->decl
, node
->decl
,
2349 node
->clone
.tree_map
, true,
2350 node
->clone
.args_to_skip
, NULL
, NULL
);
2351 if (cgraph_dump_file
)
2353 dump_function_to_file (node
->clone_of
->decl
, cgraph_dump_file
, dump_flags
);
2354 dump_function_to_file (node
->decl
, cgraph_dump_file
, dump_flags
);
2357 /* Function is no longer clone. */
2358 if (node
->next_sibling_clone
)
2359 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
2360 if (node
->prev_sibling_clone
)
2361 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
2363 node
->clone_of
->clones
= node
->next_sibling_clone
;
2364 node
->next_sibling_clone
= NULL
;
2365 node
->prev_sibling_clone
= NULL
;
2366 if (!node
->clone_of
->analyzed
&& !node
->clone_of
->clones
)
2368 cgraph_release_function_body (node
->clone_of
);
2369 cgraph_node_remove_callees (node
->clone_of
);
2370 ipa_remove_all_references (&node
->clone_of
->ref_list
);
2372 node
->clone_of
= NULL
;
2373 bitmap_obstack_release (NULL
);
2376 /* If necessary, change the function declaration in the call statement
2377 associated with E so that it corresponds to the edge callee. */
2380 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge
*e
)
2382 tree decl
= gimple_call_fndecl (e
->call_stmt
);
2384 gimple_stmt_iterator gsi
;
2385 #ifdef ENABLE_CHECKING
2386 struct cgraph_node
*node
;
2389 if (e
->indirect_unknown_callee
2390 || decl
== e
->callee
->decl
)
2391 return e
->call_stmt
;
2393 #ifdef ENABLE_CHECKING
2396 node
= cgraph_get_node (decl
);
2397 gcc_assert (!node
|| !node
->clone
.combined_args_to_skip
);
2401 if (cgraph_dump_file
)
2403 fprintf (cgraph_dump_file
, "updating call of %s/%i -> %s/%i: ",
2404 cgraph_node_name (e
->caller
), e
->caller
->uid
,
2405 cgraph_node_name (e
->callee
), e
->callee
->uid
);
2406 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
2407 if (e
->callee
->clone
.combined_args_to_skip
)
2409 fprintf (cgraph_dump_file
, " combined args to skip: ");
2410 dump_bitmap (cgraph_dump_file
,
2411 e
->callee
->clone
.combined_args_to_skip
);
2415 if (e
->callee
->clone
.combined_args_to_skip
)
2420 = gimple_call_copy_skip_args (e
->call_stmt
,
2421 e
->callee
->clone
.combined_args_to_skip
);
2422 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
2424 if (gimple_vdef (new_stmt
)
2425 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
2426 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
2428 gsi
= gsi_for_stmt (e
->call_stmt
);
2429 gsi_replace (&gsi
, new_stmt
, false);
2430 /* We need to defer cleaning EH info on the new statement to
2431 fixup-cfg. We may not have dominator information at this point
2432 and thus would end up with unreachable blocks and have no way
2433 to communicate that we need to run CFG cleanup then. */
2434 lp_nr
= lookup_stmt_eh_lp (e
->call_stmt
);
2437 remove_stmt_from_eh_lp (e
->call_stmt
);
2438 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
2443 new_stmt
= e
->call_stmt
;
2444 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
2445 update_stmt (new_stmt
);
2448 cgraph_set_call_stmt_including_clones (e
->caller
, e
->call_stmt
, new_stmt
);
2450 if (cgraph_dump_file
)
2452 fprintf (cgraph_dump_file
, " updated to:");
2453 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
2458 /* Once all functions from compilation unit are in memory, produce all clones
2459 and update all calls. We might also do this on demand if we don't want to
2460 bring all functions to memory prior compilation, but current WHOPR
2461 implementation does that and it is is bit easier to keep everything right in
2464 cgraph_materialize_all_clones (void)
2466 struct cgraph_node
*node
;
2467 bool stabilized
= false;
2469 if (cgraph_dump_file
)
2470 fprintf (cgraph_dump_file
, "Materializing clones\n");
2471 #ifdef ENABLE_CHECKING
2475 /* We can also do topological order, but number of iterations should be
2476 bounded by number of IPA passes since single IPA pass is probably not
2477 going to create clones of clones it created itself. */
2481 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2483 if (node
->clone_of
&& node
->decl
!= node
->clone_of
->decl
2484 && !gimple_has_body_p (node
->decl
))
2486 if (gimple_has_body_p (node
->clone_of
->decl
))
2488 if (cgraph_dump_file
)
2490 fprintf (cgraph_dump_file
, "cloning %s to %s\n",
2491 cgraph_node_name (node
->clone_of
),
2492 cgraph_node_name (node
));
2493 if (node
->clone
.tree_map
)
2496 fprintf (cgraph_dump_file
, " replace map: ");
2497 for (i
= 0; i
< VEC_length (ipa_replace_map_p
,
2498 node
->clone
.tree_map
);
2501 struct ipa_replace_map
*replace_info
;
2502 replace_info
= VEC_index (ipa_replace_map_p
,
2503 node
->clone
.tree_map
,
2505 print_generic_expr (cgraph_dump_file
, replace_info
->old_tree
, 0);
2506 fprintf (cgraph_dump_file
, " -> ");
2507 print_generic_expr (cgraph_dump_file
, replace_info
->new_tree
, 0);
2508 fprintf (cgraph_dump_file
, "%s%s;",
2509 replace_info
->replace_p
? "(replace)":"",
2510 replace_info
->ref_p
? "(ref)":"");
2512 fprintf (cgraph_dump_file
, "\n");
2514 if (node
->clone
.args_to_skip
)
2516 fprintf (cgraph_dump_file
, " args_to_skip: ");
2517 dump_bitmap (cgraph_dump_file
, node
->clone
.args_to_skip
);
2519 if (node
->clone
.args_to_skip
)
2521 fprintf (cgraph_dump_file
, " combined_args_to_skip:");
2522 dump_bitmap (cgraph_dump_file
, node
->clone
.combined_args_to_skip
);
2525 cgraph_materialize_clone (node
);
2531 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2532 if (!node
->analyzed
&& node
->callees
)
2533 cgraph_node_remove_callees (node
);
2534 if (cgraph_dump_file
)
2535 fprintf (cgraph_dump_file
, "Materialization Call site updates done.\n");
2536 #ifdef ENABLE_CHECKING
2539 cgraph_remove_unreachable_nodes (false, cgraph_dump_file
);
2542 #include "gt-cgraphunit.h"