1 /* Driver of optimization process
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This module implements main driver of compilation process.
23 The main scope of this file is to act as an interface in between
24 tree based frontends and the backend.
26 The front-end is supposed to use following functionality:
28 - cgraph_finalize_function
30 This function is called once front-end has parsed whole body of function
31 and it is certain that the function body nor the declaration will change.
33 (There is one exception needed for implementing GCC extern inline
36 - varpool_finalize_decl
38 This function has same behavior as the above but is used for static
43 Insert new toplevel ASM statement
45 - finalize_compilation_unit
47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
50 The symbol table is constructed starting from the trivially needed
51 symbols finalized by the frontend. Functions are lowered into
52 GIMPLE representation and callgraph/reference lists are constructed.
53 Those are used to discover other necessary functions and variables.
55 At the end the bodies of unreachable functions are removed.
57 The function can be called multiple times when multiple source level
58 compilation units are combined.
62 This passes control to the back-end. Optimizations are performed and
63 final assembler is generated. This is done in the following way. Note
64 that with link time optimization the process is split into three
65 stages (compile time, linktime analysis and parallel linktime as
70 1) Inter-procedural optimization.
73 This part is further split into:
75 a) early optimizations. These are local passes executed in
76 the topological order on the callgraph.
78 The purpose of early optimiations is to optimize away simple
79 things that may otherwise confuse IP analysis. Very simple
80 propagation across the callgraph is done i.e. to discover
81 functions without side effects and simple inlining is performed.
83 b) early small interprocedural passes.
85 Those are interprocedural passes executed only at compilation
86 time. These include, for example, transational memory lowering,
87 unreachable code removal and other simple transformations.
89 c) IP analysis stage. All interprocedural passes do their
92 Interprocedural passes differ from small interprocedural
93 passes by their ability to operate across whole program
94 at linktime. Their analysis stage is performed early to
95 both reduce linking times and linktime memory usage by
96 not having to represent whole program in memory.
98 d) LTO sreaming. When doing LTO, everything important gets
99 streamed into the object file.
101 Compile time and or linktime analysis stage (WPA):
103 At linktime units gets streamed back and symbol table is
104 merged. Function bodies are not streamed in and not
106 e) IP propagation stage. All IP passes execute their
107 IP propagation. This is done based on the earlier analysis
108 without having function bodies at hand.
109 f) Ltrans streaming. When doing WHOPR LTO, the program
110 is partitioned and streamed into multple object files.
112 Compile time and/or parallel linktime stage (ltrans)
114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
121 IP passes can produce copies of existing functoins (such
122 as versioned clones or inline clones) without actually
123 manipulating their bodies by creating virtual clones in
124 the callgraph. At this time the virtual clones are
125 turned into real functions
128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
131 4) late small IP passes
133 Simple IP passes working within single program partition.
136 (expand_all_functions)
138 At this stage functions that needs to be output into
139 assembler are identified and compiled in topological order
140 6) Output of variables and aliases
141 Now it is known what variable references was not optimized
142 out and thus all variables are output to the file.
144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
147 Finally there are functions to manipulate the callgraph from
149 - cgraph_add_new_function is used to add backend produced
150 functions introduced after the unit is finalized.
151 The functions are enqueue for later processing and inserted
152 into callgraph with cgraph_process_new_functions.
154 - cgraph_function_versioning
156 produces a copy of function into new one (a version)
157 and apply simple transformations
162 #include "coretypes.h"
167 #include "tree-flow.h"
168 #include "tree-inline.h"
169 #include "langhooks.h"
170 #include "pointer-set.h"
177 #include "diagnostic.h"
181 #include "function.h"
182 #include "ipa-prop.h"
184 #include "tree-iterator.h"
185 #include "tree-pass.h"
186 #include "tree-dump.h"
187 #include "gimple-pretty-print.h"
189 #include "coverage.h"
191 #include "ipa-inline.h"
192 #include "ipa-utils.h"
193 #include "lto-streamer.h"
196 #include "regset.h" /* FIXME: For reg_obstack. */
198 #include "pass_manager.h"
200 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
201 secondary queue used during optimization to accommodate passes that
202 may generate new functions that need to be optimized and expanded. */
203 cgraph_node_set cgraph_new_nodes
;
205 static void expand_all_functions (void);
206 static void mark_functions_to_output (void);
207 static void expand_function (struct cgraph_node
*);
208 static void analyze_function (struct cgraph_node
*);
209 static void handle_alias_pairs (void);
211 FILE *cgraph_dump_file
;
213 /* Linked list of cgraph asm nodes. */
214 struct asm_node
*asm_nodes
;
216 /* Last node in cgraph_asm_nodes. */
217 static GTY(()) struct asm_node
*asm_last_node
;
219 /* Used for vtable lookup in thunk adjusting. */
220 static GTY (()) tree vtable_entry_type
;
222 /* Determine if symbol DECL is needed. That is, visible to something
223 either outside this translation unit, something magic in the system
226 decide_is_symbol_needed (symtab_node node
)
228 tree decl
= node
->symbol
.decl
;
230 /* Double check that no one output the function into assembly file
232 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
233 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
235 if (!node
->symbol
.definition
)
238 if (DECL_EXTERNAL (decl
))
241 /* If the user told us it is used, then it must be so. */
242 if (node
->symbol
.force_output
)
245 /* ABI forced symbols are needed when they are external. */
246 if (node
->symbol
.forced_by_abi
&& TREE_PUBLIC (decl
))
249 /* Keep constructors, destructors and virtual functions. */
250 if (TREE_CODE (decl
) == FUNCTION_DECL
251 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
254 /* Externally visible variables must be output. The exception is
255 COMDAT variables that must be output only when they are needed. */
256 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
262 /* Head of the queue of nodes to be processed while building callgraph */
264 static symtab_node first
= (symtab_node
)(void *)1;
266 /* Add NODE to queue starting at FIRST.
267 The queue is linked via AUX pointers and terminated by pointer to 1. */
270 enqueue_node (symtab_node node
)
272 if (node
->symbol
.aux
)
274 gcc_checking_assert (first
);
275 node
->symbol
.aux
= first
;
279 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
280 functions into callgraph in a way so they look like ordinary reachable
281 functions inserted into callgraph already at construction time. */
284 cgraph_process_new_functions (void)
288 struct cgraph_node
*node
;
289 cgraph_node_set_iterator csi
;
291 if (!cgraph_new_nodes
)
293 handle_alias_pairs ();
294 /* Note that this queue may grow as its being processed, as the new
295 functions may generate new ones. */
296 for (csi
= csi_start (cgraph_new_nodes
); !csi_end_p (csi
); csi_next (&csi
))
298 node
= csi_node (csi
);
299 fndecl
= node
->symbol
.decl
;
300 switch (cgraph_state
)
302 case CGRAPH_STATE_CONSTRUCTION
:
303 /* At construction time we just need to finalize function and move
304 it into reachable functions list. */
306 cgraph_finalize_function (fndecl
, false);
308 cgraph_call_function_insertion_hooks (node
);
309 enqueue_node ((symtab_node
) node
);
312 case CGRAPH_STATE_IPA
:
313 case CGRAPH_STATE_IPA_SSA
:
314 /* When IPA optimization already started, do all essential
315 transformations that has been already performed on the whole
316 cgraph but not on this function. */
318 gimple_register_cfg_hooks ();
319 if (!node
->symbol
.analyzed
)
320 analyze_function (node
);
321 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
322 if (cgraph_state
== CGRAPH_STATE_IPA_SSA
323 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
324 g
->get_passes ()->execute_early_local_passes ();
325 else if (inline_summary_vec
!= NULL
)
326 compute_inline_parameters (node
, true);
327 free_dominance_info (CDI_POST_DOMINATORS
);
328 free_dominance_info (CDI_DOMINATORS
);
330 cgraph_call_function_insertion_hooks (node
);
333 case CGRAPH_STATE_EXPANSION
:
334 /* Functions created during expansion shall be compiled
337 cgraph_call_function_insertion_hooks (node
);
338 expand_function (node
);
346 free_cgraph_node_set (cgraph_new_nodes
);
347 cgraph_new_nodes
= NULL
;
351 /* As an GCC extension we allow redefinition of the function. The
352 semantics when both copies of bodies differ is not well defined.
353 We replace the old body with new body so in unit at a time mode
354 we always use new body, while in normal mode we may end up with
355 old body inlined into some functions and new body expanded and
358 ??? It may make more sense to use one body for inlining and other
359 body for expanding the function but this is difficult to do. */
362 cgraph_reset_node (struct cgraph_node
*node
)
364 /* If node->process is set, then we have already begun whole-unit analysis.
365 This is *not* testing for whether we've already emitted the function.
366 That case can be sort-of legitimately seen with real function redefinition
367 errors. I would argue that the front end should never present us with
368 such a case, but don't enforce that for now. */
369 gcc_assert (!node
->process
);
371 /* Reset our data structures so we can analyze the function again. */
372 memset (&node
->local
, 0, sizeof (node
->local
));
373 memset (&node
->global
, 0, sizeof (node
->global
));
374 memset (&node
->rtl
, 0, sizeof (node
->rtl
));
375 node
->symbol
.analyzed
= false;
376 node
->symbol
.definition
= false;
377 node
->symbol
.alias
= false;
378 node
->symbol
.weakref
= false;
379 node
->symbol
.cpp_implicit_alias
= false;
381 cgraph_node_remove_callees (node
);
382 ipa_remove_all_references (&node
->symbol
.ref_list
);
385 /* Return true when there are references to NODE. */
388 referred_to_p (symtab_node node
)
392 /* See if there are any references at all. */
393 if (ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, 0, ref
))
395 /* For functions check also calls. */
396 cgraph_node
*cn
= dyn_cast
<cgraph_node
> (node
);
397 if (cn
&& cn
->callers
)
402 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
403 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
404 the garbage collector run at the moment. We would need to either create
405 a new GC context, or just not compile right now. */
408 cgraph_finalize_function (tree decl
, bool no_collect
)
410 struct cgraph_node
*node
= cgraph_get_create_node (decl
);
412 if (node
->symbol
.definition
)
414 /* Nested functions should only be defined once. */
415 gcc_assert (!DECL_CONTEXT (decl
)
416 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
417 cgraph_reset_node (node
);
418 node
->local
.redefined_extern_inline
= true;
421 notice_global_symbol (decl
);
422 node
->symbol
.definition
= true;
423 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
425 /* With -fkeep-inline-functions we are keeping all inline functions except
426 for extern inline ones. */
427 if (flag_keep_inline_functions
428 && DECL_DECLARED_INLINE_P (decl
)
429 && !DECL_EXTERNAL (decl
)
430 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
431 node
->symbol
.force_output
= 1;
433 /* When not optimizing, also output the static functions. (see
434 PR24561), but don't do so for always_inline functions, functions
435 declared inline and nested functions. These were optimized out
436 in the original implementation and it is unclear whether we want
437 to change the behavior here. */
439 && !node
->symbol
.cpp_implicit_alias
440 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
441 && !DECL_DECLARED_INLINE_P (decl
)
442 && !(DECL_CONTEXT (decl
)
443 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
444 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
445 node
->symbol
.force_output
= 1;
447 /* If we've not yet emitted decl, tell the debug info about it. */
448 if (!TREE_ASM_WRITTEN (decl
))
449 (*debug_hooks
->deferred_inline_function
) (decl
);
451 /* Possibly warn about unused parameters. */
452 if (warn_unused_parameter
)
453 do_warn_unused_parameter (decl
);
458 if (cgraph_state
== CGRAPH_STATE_CONSTRUCTION
459 && (decide_is_symbol_needed ((symtab_node
) node
)
460 || referred_to_p ((symtab_node
)node
)))
461 enqueue_node ((symtab_node
)node
);
464 /* Add the function FNDECL to the call graph.
465 Unlike cgraph_finalize_function, this function is intended to be used
466 by middle end and allows insertion of new function at arbitrary point
467 of compilation. The function can be either in high, low or SSA form
470 The function is assumed to be reachable and have address taken (so no
471 API breaking optimizations are performed on it).
473 Main work done by this function is to enqueue the function for later
474 processing to avoid need the passes to be re-entrant. */
477 cgraph_add_new_function (tree fndecl
, bool lowered
)
479 gcc::pass_manager
*passes
= g
->get_passes ();
480 struct cgraph_node
*node
;
481 switch (cgraph_state
)
483 case CGRAPH_STATE_PARSING
:
484 cgraph_finalize_function (fndecl
, false);
486 case CGRAPH_STATE_CONSTRUCTION
:
487 /* Just enqueue function to be processed at nearest occurrence. */
488 node
= cgraph_create_node (fndecl
);
490 node
->lowered
= true;
491 if (!cgraph_new_nodes
)
492 cgraph_new_nodes
= cgraph_node_set_new ();
493 cgraph_node_set_add (cgraph_new_nodes
, node
);
496 case CGRAPH_STATE_IPA
:
497 case CGRAPH_STATE_IPA_SSA
:
498 case CGRAPH_STATE_EXPANSION
:
499 /* Bring the function into finalized state and enqueue for later
500 analyzing and compilation. */
501 node
= cgraph_get_create_node (fndecl
);
502 node
->local
.local
= false;
503 node
->symbol
.definition
= true;
504 node
->symbol
.force_output
= true;
505 if (!lowered
&& cgraph_state
== CGRAPH_STATE_EXPANSION
)
507 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
508 gimple_register_cfg_hooks ();
509 bitmap_obstack_initialize (NULL
);
510 execute_pass_list (passes
->all_lowering_passes
);
511 passes
->execute_early_local_passes ();
512 bitmap_obstack_release (NULL
);
518 node
->lowered
= true;
519 if (!cgraph_new_nodes
)
520 cgraph_new_nodes
= cgraph_node_set_new ();
521 cgraph_node_set_add (cgraph_new_nodes
, node
);
524 case CGRAPH_STATE_FINISHED
:
525 /* At the very end of compilation we have to do all the work up
527 node
= cgraph_create_node (fndecl
);
529 node
->lowered
= true;
530 node
->symbol
.definition
= true;
531 analyze_function (node
);
532 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
533 gimple_register_cfg_hooks ();
534 bitmap_obstack_initialize (NULL
);
535 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
536 g
->get_passes ()->execute_early_local_passes ();
537 bitmap_obstack_release (NULL
);
539 expand_function (node
);
546 /* Set a personality if required and we already passed EH lowering. */
548 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
549 == eh_personality_lang
))
550 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
553 /* Add a top-level asm statement to the list. */
556 add_asm_node (tree asm_str
)
558 struct asm_node
*node
;
560 node
= ggc_alloc_cleared_asm_node ();
561 node
->asm_str
= asm_str
;
562 node
->order
= symtab_order
++;
564 if (asm_nodes
== NULL
)
567 asm_last_node
->next
= node
;
568 asm_last_node
= node
;
572 /* Output all asm statements we have stored up to be output. */
575 output_asm_statements (void)
577 struct asm_node
*can
;
582 for (can
= asm_nodes
; can
; can
= can
->next
)
583 assemble_asm (can
->asm_str
);
587 /* Analyze the function scheduled to be output. */
589 analyze_function (struct cgraph_node
*node
)
591 tree decl
= node
->symbol
.decl
;
592 location_t saved_loc
= input_location
;
593 input_location
= DECL_SOURCE_LOCATION (decl
);
595 if (node
->symbol
.alias
)
597 ((symtab_node
) node
, (symtab_node
) cgraph_get_node (node
->symbol
.alias_target
));
598 else if (node
->thunk
.thunk_p
)
600 cgraph_create_edge (node
, cgraph_get_node (node
->thunk
.alias
),
601 NULL
, 0, CGRAPH_FREQ_BASE
);
602 node
->thunk
.alias
= NULL
;
604 else if (node
->dispatcher_function
)
606 /* Generate the dispatcher body of multi-versioned functions. */
607 struct cgraph_function_version_info
*dispatcher_version_info
608 = get_cgraph_node_version (node
);
609 if (dispatcher_version_info
!= NULL
610 && (dispatcher_version_info
->dispatcher_resolver
613 tree resolver
= NULL_TREE
;
614 gcc_assert (targetm
.generate_version_dispatcher_body
);
615 resolver
= targetm
.generate_version_dispatcher_body (node
);
616 gcc_assert (resolver
!= NULL_TREE
);
621 push_cfun (DECL_STRUCT_FUNCTION (decl
));
623 assign_assembler_name_if_neeeded (node
->symbol
.decl
);
625 /* Make sure to gimplify bodies only once. During analyzing a
626 function we lower it, which will require gimplified nested
627 functions, so we can end up here with an already gimplified
629 if (!gimple_has_body_p (decl
))
630 gimplify_function_tree (decl
);
631 dump_function (TDI_generic
, decl
);
633 /* Lower the function. */
637 lower_nested_functions (node
->symbol
.decl
);
638 gcc_assert (!node
->nested
);
640 gimple_register_cfg_hooks ();
641 bitmap_obstack_initialize (NULL
);
642 execute_pass_list (g
->get_passes ()->all_lowering_passes
);
643 free_dominance_info (CDI_POST_DOMINATORS
);
644 free_dominance_info (CDI_DOMINATORS
);
646 bitmap_obstack_release (NULL
);
647 node
->lowered
= true;
652 node
->symbol
.analyzed
= true;
654 input_location
= saved_loc
;
657 /* C++ frontend produce same body aliases all over the place, even before PCH
658 gets streamed out. It relies on us linking the aliases with their function
659 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
660 first produce aliases without links, but once C++ FE is sure he won't sream
661 PCH we build the links via this function. */
664 cgraph_process_same_body_aliases (void)
667 FOR_EACH_SYMBOL (node
)
668 if (node
->symbol
.cpp_implicit_alias
&& !node
->symbol
.analyzed
)
671 TREE_CODE (node
->symbol
.alias_target
) == VAR_DECL
672 ? (symtab_node
)varpool_node_for_decl (node
->symbol
.alias_target
)
673 : (symtab_node
)cgraph_get_create_node (node
->symbol
.alias_target
));
674 cpp_implicit_aliases_done
= true;
677 /* Process attributes common for vars and functions. */
680 process_common_attributes (tree decl
)
682 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
684 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
686 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
687 "%<weakref%> attribute should be accompanied with"
688 " an %<alias%> attribute");
689 DECL_WEAK (decl
) = 0;
690 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
691 DECL_ATTRIBUTES (decl
));
695 /* Look for externally_visible and used attributes and mark cgraph nodes
698 We cannot mark the nodes at the point the attributes are processed (in
699 handle_*_attribute) because the copy of the declarations available at that
700 point may not be canonical. For example, in:
703 void f() __attribute__((used));
705 the declaration we see in handle_used_attribute will be the second
706 declaration -- but the front end will subsequently merge that declaration
707 with the original declaration and discard the second declaration.
709 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
712 void f() __attribute__((externally_visible));
716 So, we walk the nodes at the end of the translation unit, applying the
717 attributes at that point. */
720 process_function_and_variable_attributes (struct cgraph_node
*first
,
721 struct varpool_node
*first_var
)
723 struct cgraph_node
*node
;
724 struct varpool_node
*vnode
;
726 for (node
= cgraph_first_function (); node
!= first
;
727 node
= cgraph_next_function (node
))
729 tree decl
= node
->symbol
.decl
;
730 if (DECL_PRESERVE_P (decl
))
731 cgraph_mark_force_output_node (node
);
732 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
734 if (! TREE_PUBLIC (node
->symbol
.decl
))
735 warning_at (DECL_SOURCE_LOCATION (node
->symbol
.decl
), OPT_Wattributes
,
736 "%<externally_visible%>"
737 " attribute have effect only on public objects");
739 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
740 && (node
->symbol
.definition
&& !node
->symbol
.alias
))
742 warning_at (DECL_SOURCE_LOCATION (node
->symbol
.decl
), OPT_Wattributes
,
743 "%<weakref%> attribute ignored"
744 " because function is defined");
745 DECL_WEAK (decl
) = 0;
746 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
747 DECL_ATTRIBUTES (decl
));
750 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
751 && !DECL_DECLARED_INLINE_P (decl
)
752 /* redefining extern inline function makes it DECL_UNINLINABLE. */
753 && !DECL_UNINLINABLE (decl
))
754 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
755 "always_inline function might not be inlinable");
757 process_common_attributes (decl
);
759 for (vnode
= varpool_first_variable (); vnode
!= first_var
;
760 vnode
= varpool_next_variable (vnode
))
762 tree decl
= vnode
->symbol
.decl
;
763 if (DECL_EXTERNAL (decl
)
764 && DECL_INITIAL (decl
))
765 varpool_finalize_decl (decl
);
766 if (DECL_PRESERVE_P (decl
))
767 vnode
->symbol
.force_output
= true;
768 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
770 if (! TREE_PUBLIC (vnode
->symbol
.decl
))
771 warning_at (DECL_SOURCE_LOCATION (vnode
->symbol
.decl
), OPT_Wattributes
,
772 "%<externally_visible%>"
773 " attribute have effect only on public objects");
775 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
776 && vnode
->symbol
.definition
777 && DECL_INITIAL (decl
))
779 warning_at (DECL_SOURCE_LOCATION (vnode
->symbol
.decl
), OPT_Wattributes
,
780 "%<weakref%> attribute ignored"
781 " because variable is initialized");
782 DECL_WEAK (decl
) = 0;
783 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
784 DECL_ATTRIBUTES (decl
));
786 process_common_attributes (decl
);
790 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
791 middle end to output the variable to asm file, if needed or externally
795 varpool_finalize_decl (tree decl
)
797 struct varpool_node
*node
= varpool_node_for_decl (decl
);
799 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
801 if (node
->symbol
.definition
)
803 notice_global_symbol (decl
);
804 node
->symbol
.definition
= true;
805 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
806 /* Traditionally we do not eliminate static variables when not
807 optimizing and when not doing toplevel reoder. */
808 || (!flag_toplevel_reorder
&& !DECL_COMDAT (node
->symbol
.decl
)
809 && !DECL_ARTIFICIAL (node
->symbol
.decl
)))
810 node
->symbol
.force_output
= true;
812 if (cgraph_state
== CGRAPH_STATE_CONSTRUCTION
813 && (decide_is_symbol_needed ((symtab_node
) node
)
814 || referred_to_p ((symtab_node
)node
)))
815 enqueue_node ((symtab_node
)node
);
816 if (cgraph_state
>= CGRAPH_STATE_IPA_SSA
)
817 varpool_analyze_node (node
);
818 /* Some frontends produce various interface variables after compilation
820 if (cgraph_state
== CGRAPH_STATE_FINISHED
)
821 varpool_assemble_decl (node
);
825 /* Discover all functions and variables that are trivially needed, analyze
826 them as well as all functions and variables referred by them */
829 analyze_functions (void)
831 /* Keep track of already processed nodes when called multiple times for
832 intermodule optimization. */
833 static struct cgraph_node
*first_analyzed
;
834 struct cgraph_node
*first_handled
= first_analyzed
;
835 static struct varpool_node
*first_analyzed_var
;
836 struct varpool_node
*first_handled_var
= first_analyzed_var
;
837 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
839 symtab_node node
, next
;
844 bitmap_obstack_initialize (NULL
);
845 cgraph_state
= CGRAPH_STATE_CONSTRUCTION
;
847 /* Ugly, but the fixup can not happen at a time same body alias is created;
848 C++ FE is confused about the COMDAT groups being right. */
849 if (cpp_implicit_aliases_done
)
850 FOR_EACH_SYMBOL (node
)
851 if (node
->symbol
.cpp_implicit_alias
)
852 fixup_same_cpp_alias_visibility (node
, symtab_alias_target (node
));
853 if (optimize
&& flag_devirtualize
)
854 build_type_inheritance_graph ();
856 /* Analysis adds static variables that in turn adds references to new functions.
857 So we need to iterate the process until it stabilize. */
861 process_function_and_variable_attributes (first_analyzed
,
864 /* First identify the trivially needed symbols. */
865 for (node
= symtab_nodes
;
866 node
!= (symtab_node
)first_analyzed
867 && node
!= (symtab_node
)first_analyzed_var
; node
= node
->symbol
.next
)
869 if (decide_is_symbol_needed (node
))
872 if (!changed
&& cgraph_dump_file
)
873 fprintf (cgraph_dump_file
, "Trivially needed symbols:");
875 if (cgraph_dump_file
)
876 fprintf (cgraph_dump_file
, " %s", symtab_node_asm_name (node
));
877 if (!changed
&& cgraph_dump_file
)
878 fprintf (cgraph_dump_file
, "\n");
880 if (node
== (symtab_node
)first_analyzed
881 || node
== (symtab_node
)first_analyzed_var
)
884 cgraph_process_new_functions ();
885 first_analyzed_var
= varpool_first_variable ();
886 first_analyzed
= cgraph_first_function ();
888 if (changed
&& dump_file
)
889 fprintf (cgraph_dump_file
, "\n");
891 /* Lower representation, build callgraph edges and references for all trivially
892 needed symbols and all symbols referred by them. */
893 while (first
!= (symtab_node
)(void *)1)
897 first
= (symtab_node
)first
->symbol
.aux
;
898 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
899 if (cnode
&& cnode
->symbol
.definition
)
901 struct cgraph_edge
*edge
;
902 tree decl
= cnode
->symbol
.decl
;
904 /* ??? It is possible to create extern inline function
905 and later using weak alias attribute to kill its body.
906 See gcc.c-torture/compile/20011119-1.c */
907 if (!DECL_STRUCT_FUNCTION (decl
)
908 && !cnode
->symbol
.alias
909 && !cnode
->thunk
.thunk_p
910 && !cnode
->dispatcher_function
)
912 cgraph_reset_node (cnode
);
913 cnode
->local
.redefined_extern_inline
= true;
917 if (!cnode
->symbol
.analyzed
)
918 analyze_function (cnode
);
920 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
921 if (edge
->callee
->symbol
.definition
)
922 enqueue_node ((symtab_node
)edge
->callee
);
923 if (optimize
&& flag_devirtualize
)
925 struct cgraph_edge
*next
;
926 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
928 next
= edge
->next_callee
;
929 if (edge
->indirect_info
->polymorphic
)
934 vec
<cgraph_node
*>targets
935 = possible_polymorphic_call_targets
936 (edge
, &final
, &cache_token
);
938 if (!pointer_set_insert (reachable_call_targets
,
941 if (cgraph_dump_file
)
942 dump_possible_polymorphic_call_targets
943 (cgraph_dump_file
, edge
);
945 for (i
= 0; i
< targets
.length(); i
++)
947 /* Do not bother to mark virtual methods in anonymous namespace;
948 either we will find use of virtual table defining it, or it is
950 if (targets
[i
]->symbol
.definition
952 (TREE_TYPE (targets
[i
]->symbol
.decl
))
954 && !type_in_anonymous_namespace_p
956 (TREE_TYPE (targets
[i
]->symbol
.decl
))))
957 enqueue_node ((symtab_node
) targets
[i
]);
961 /* Very trivial devirtualization; when the type is
962 final or anonymous (so we know all its derivation)
963 and there is only one possible virtual call target,
964 make the edge direct. */
967 gcc_assert (targets
.length());
968 if (targets
.length() == 1)
970 if (cgraph_dump_file
)
972 fprintf (cgraph_dump_file
,
973 "Devirtualizing call: ");
974 print_gimple_stmt (cgraph_dump_file
,
978 cgraph_make_edge_direct (edge
, targets
[0]);
979 cgraph_redirect_edge_call_stmt_to_callee (edge
);
980 if (cgraph_dump_file
)
982 fprintf (cgraph_dump_file
,
983 "Devirtualized as: ");
984 print_gimple_stmt (cgraph_dump_file
,
994 /* If decl is a clone of an abstract function,
995 mark that abstract function so that we don't release its body.
996 The DECL_INITIAL() of that abstract function declaration
997 will be later needed to output debug info. */
998 if (DECL_ABSTRACT_ORIGIN (decl
))
1000 struct cgraph_node
*origin_node
1001 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl
));
1002 origin_node
->used_as_abstract_origin
= true;
1007 varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
);
1008 if (vnode
&& vnode
->symbol
.definition
&& !vnode
->symbol
.analyzed
)
1009 varpool_analyze_node (vnode
);
1012 if (node
->symbol
.same_comdat_group
)
1015 for (next
= node
->symbol
.same_comdat_group
;
1017 next
= next
->symbol
.same_comdat_group
)
1018 enqueue_node (next
);
1020 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
1021 if (ref
->referred
->symbol
.definition
)
1022 enqueue_node (ref
->referred
);
1023 cgraph_process_new_functions ();
1026 if (optimize
&& flag_devirtualize
)
1027 update_type_inheritance_graph ();
1029 /* Collect entry points to the unit. */
1030 if (cgraph_dump_file
)
1032 fprintf (cgraph_dump_file
, "\n\nInitial ");
1033 dump_symtab (cgraph_dump_file
);
1036 if (cgraph_dump_file
)
1037 fprintf (cgraph_dump_file
, "\nRemoving unused symbols:");
1039 for (node
= symtab_nodes
;
1040 node
!= (symtab_node
)first_handled
1041 && node
!= (symtab_node
)first_handled_var
; node
= next
)
1043 next
= node
->symbol
.next
;
1044 if (!node
->symbol
.aux
&& !referred_to_p (node
))
1046 if (cgraph_dump_file
)
1047 fprintf (cgraph_dump_file
, " %s", symtab_node_name (node
));
1048 symtab_remove_node (node
);
1051 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
1053 tree decl
= node
->symbol
.decl
;
1055 if (cnode
->symbol
.definition
&& !gimple_has_body_p (decl
)
1056 && !cnode
->symbol
.alias
1057 && !cnode
->thunk
.thunk_p
)
1058 cgraph_reset_node (cnode
);
1060 gcc_assert (!cnode
->symbol
.definition
|| cnode
->thunk
.thunk_p
1061 || cnode
->symbol
.alias
1062 || gimple_has_body_p (decl
));
1063 gcc_assert (cnode
->symbol
.analyzed
== cnode
->symbol
.definition
);
1065 node
->symbol
.aux
= NULL
;
1067 first_analyzed
= cgraph_first_function ();
1068 first_analyzed_var
= varpool_first_variable ();
1069 if (cgraph_dump_file
)
1071 fprintf (cgraph_dump_file
, "\n\nReclaimed ");
1072 dump_symtab (cgraph_dump_file
);
1074 bitmap_obstack_release (NULL
);
1075 pointer_set_destroy (reachable_call_targets
);
1079 /* Translate the ugly representation of aliases as alias pairs into nice
1080 representation in callgraph. We don't handle all cases yet,
1084 handle_alias_pairs (void)
1089 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1091 symtab_node target_node
= symtab_node_for_asm (p
->target
);
1093 /* Weakrefs with target not defined in current unit are easy to handle:
1094 they behave just as external variables except we need to note the
1095 alias flag to later output the weakref pseudo op into asm file. */
1097 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1099 symtab_node node
= symtab_get_node (p
->decl
);
1102 node
->symbol
.alias_target
= p
->target
;
1103 node
->symbol
.weakref
= true;
1104 node
->symbol
.alias
= true;
1106 alias_pairs
->unordered_remove (i
);
1109 else if (!target_node
)
1111 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1112 symtab_node node
= symtab_get_node (p
->decl
);
1114 node
->symbol
.alias
= false;
1115 alias_pairs
->unordered_remove (i
);
1119 if (DECL_EXTERNAL (target_node
->symbol
.decl
)
1120 /* We use local aliases for C++ thunks to force the tailcall
1121 to bind locally. This is a hack - to keep it working do
1122 the following (which is not strictly correct). */
1123 && (! TREE_CODE (target_node
->symbol
.decl
) == FUNCTION_DECL
1124 || ! DECL_VIRTUAL_P (target_node
->symbol
.decl
))
1125 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1127 error ("%q+D aliased to external symbol %qE",
1128 p
->decl
, p
->target
);
1131 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1132 && target_node
&& is_a
<cgraph_node
> (target_node
))
1134 struct cgraph_node
*src_node
= cgraph_get_node (p
->decl
);
1135 if (src_node
&& src_node
->symbol
.definition
)
1136 cgraph_reset_node (src_node
);
1137 cgraph_create_function_alias (p
->decl
, target_node
->symbol
.decl
);
1138 alias_pairs
->unordered_remove (i
);
1140 else if (TREE_CODE (p
->decl
) == VAR_DECL
1141 && target_node
&& is_a
<varpool_node
> (target_node
))
1143 varpool_create_variable_alias (p
->decl
, target_node
->symbol
.decl
);
1144 alias_pairs
->unordered_remove (i
);
1148 error ("%q+D alias in between function and variable is not supported",
1150 warning (0, "%q+D aliased declaration",
1151 target_node
->symbol
.decl
);
1152 alias_pairs
->unordered_remove (i
);
1155 vec_free (alias_pairs
);
1159 /* Figure out what functions we want to assemble. */
1162 mark_functions_to_output (void)
1164 struct cgraph_node
*node
;
1165 #ifdef ENABLE_CHECKING
1166 bool check_same_comdat_groups
= false;
1168 FOR_EACH_FUNCTION (node
)
1169 gcc_assert (!node
->process
);
1172 FOR_EACH_FUNCTION (node
)
1174 tree decl
= node
->symbol
.decl
;
1176 gcc_assert (!node
->process
|| node
->symbol
.same_comdat_group
);
1180 /* We need to output all local functions that are used and not
1181 always inlined, as well as those that are reachable from
1182 outside the current compilation unit. */
1183 if (node
->symbol
.analyzed
1184 && !node
->thunk
.thunk_p
1185 && !node
->symbol
.alias
1186 && !node
->global
.inlined_to
1187 && !TREE_ASM_WRITTEN (decl
)
1188 && !DECL_EXTERNAL (decl
))
1191 if (node
->symbol
.same_comdat_group
)
1193 struct cgraph_node
*next
;
1194 for (next
= cgraph (node
->symbol
.same_comdat_group
);
1196 next
= cgraph (next
->symbol
.same_comdat_group
))
1197 if (!next
->thunk
.thunk_p
&& !next
->symbol
.alias
)
1201 else if (node
->symbol
.same_comdat_group
)
1203 #ifdef ENABLE_CHECKING
1204 check_same_comdat_groups
= true;
1209 /* We should've reclaimed all functions that are not needed. */
1210 #ifdef ENABLE_CHECKING
1211 if (!node
->global
.inlined_to
1212 && gimple_has_body_p (decl
)
1213 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1214 are inside partition, we can end up not removing the body since we no longer
1215 have analyzed node pointing to it. */
1216 && !node
->symbol
.in_other_partition
1217 && !node
->symbol
.alias
1219 && !DECL_EXTERNAL (decl
))
1221 dump_cgraph_node (stderr
, node
);
1222 internal_error ("failed to reclaim unneeded function");
1225 gcc_assert (node
->global
.inlined_to
1226 || !gimple_has_body_p (decl
)
1227 || node
->symbol
.in_other_partition
1229 || DECL_ARTIFICIAL (decl
)
1230 || DECL_EXTERNAL (decl
));
1235 #ifdef ENABLE_CHECKING
1236 if (check_same_comdat_groups
)
1237 FOR_EACH_FUNCTION (node
)
1238 if (node
->symbol
.same_comdat_group
&& !node
->process
)
1240 tree decl
= node
->symbol
.decl
;
1241 if (!node
->global
.inlined_to
1242 && gimple_has_body_p (decl
)
1243 /* FIXME: in an ltrans unit when the offline copy is outside a
1244 partition but inline copies are inside a partition, we can
1245 end up not removing the body since we no longer have an
1246 analyzed node pointing to it. */
1247 && !node
->symbol
.in_other_partition
1249 && !DECL_EXTERNAL (decl
))
1251 dump_cgraph_node (stderr
, node
);
1252 internal_error ("failed to reclaim unneeded function in same "
1259 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1260 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1262 Set current_function_decl and cfun to newly constructed empty function body.
1263 return basic block in the function body. */
1266 init_lowered_empty_function (tree decl
, bool in_ssa
)
1270 current_function_decl
= decl
;
1271 allocate_struct_function (decl
, false);
1272 gimple_register_cfg_hooks ();
1273 init_empty_tree_cfg ();
1277 init_tree_ssa (cfun
);
1278 init_ssa_operands (cfun
);
1279 cfun
->gimple_df
->in_ssa_p
= true;
1280 cfun
->curr_properties
|= PROP_ssa
;
1283 DECL_INITIAL (decl
) = make_node (BLOCK
);
1285 DECL_SAVED_TREE (decl
) = error_mark_node
;
1286 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1287 | PROP_cfg
| PROP_loops
);
1289 set_loops_for_fn (cfun
, ggc_alloc_cleared_loops ());
1290 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1291 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1293 /* Create BB for body of the function and connect it properly. */
1294 bb
= create_basic_block (NULL
, (void *) 0, ENTRY_BLOCK_PTR
);
1295 make_edge (ENTRY_BLOCK_PTR
, bb
, EDGE_FALLTHRU
);
1296 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
1297 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR
->loop_father
);
1302 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1303 offset indicated by VIRTUAL_OFFSET, if that is
1304 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1305 zero for a result adjusting thunk. */
1308 thunk_adjust (gimple_stmt_iterator
* bsi
,
1309 tree ptr
, bool this_adjusting
,
1310 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1316 && fixed_offset
!= 0)
1318 stmt
= gimple_build_assign
1319 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1322 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1325 /* If there's a virtual offset, look up that value in the vtable and
1326 adjust the pointer again. */
1333 if (!vtable_entry_type
)
1335 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1336 TREE_TYPE (vfunc_type
) = integer_type_node
;
1337 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1338 layout_type (vfunc_type
);
1340 vtable_entry_type
= build_pointer_type (vfunc_type
);
1344 create_tmp_reg (build_pointer_type
1345 (build_pointer_type (vtable_entry_type
)), "vptr");
1347 /* The vptr is always at offset zero in the object. */
1348 stmt
= gimple_build_assign (vtabletmp
,
1349 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1351 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1353 /* Form the vtable address. */
1354 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1356 stmt
= gimple_build_assign (vtabletmp2
,
1357 build_simple_mem_ref (vtabletmp
));
1358 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1360 /* Find the entry with the vcall offset. */
1361 stmt
= gimple_build_assign (vtabletmp2
,
1362 fold_build_pointer_plus_loc (input_location
,
1365 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1367 /* Get the offset itself. */
1368 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1370 stmt
= gimple_build_assign (vtabletmp3
,
1371 build_simple_mem_ref (vtabletmp2
));
1372 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1374 /* Adjust the `this' pointer. */
1375 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1376 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1377 GSI_CONTINUE_LINKING
);
1381 && fixed_offset
!= 0)
1382 /* Adjust the pointer by the constant. */
1386 if (TREE_CODE (ptr
) == VAR_DECL
)
1390 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1391 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1392 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1394 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1395 ptrtmp
, fixed_offset
);
1398 /* Emit the statement and gimplify the adjustment expression. */
1399 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1400 stmt
= gimple_build_assign (ret
, ptr
);
1401 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1406 /* Produce assembler for thunk NODE. */
1409 expand_thunk (struct cgraph_node
*node
)
1411 bool this_adjusting
= node
->thunk
.this_adjusting
;
1412 HOST_WIDE_INT fixed_offset
= node
->thunk
.fixed_offset
;
1413 HOST_WIDE_INT virtual_value
= node
->thunk
.virtual_value
;
1414 tree virtual_offset
= NULL
;
1415 tree alias
= node
->callees
->callee
->symbol
.decl
;
1416 tree thunk_fndecl
= node
->symbol
.decl
;
1417 tree a
= DECL_ARGUMENTS (thunk_fndecl
);
1419 current_function_decl
= thunk_fndecl
;
1421 /* Ensure thunks are emitted in their correct sections. */
1422 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
1425 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1426 virtual_value
, alias
))
1430 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1432 DECL_RESULT (thunk_fndecl
)
1433 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1434 RESULT_DECL
, 0, restype
);
1435 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1437 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1439 fn_block
= make_node (BLOCK
);
1440 BLOCK_VARS (fn_block
) = a
;
1441 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1442 init_function_start (thunk_fndecl
);
1444 insn_locations_init ();
1445 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1446 prologue_location
= curr_insn_location ();
1447 assemble_start_function (thunk_fndecl
, fnname
);
1449 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1450 fixed_offset
, virtual_value
, alias
);
1452 assemble_end_function (thunk_fndecl
, fnname
);
1453 insn_locations_finalize ();
1454 init_insn_lengths ();
1455 free_after_compilation (cfun
);
1457 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1458 node
->thunk
.thunk_p
= false;
1459 node
->symbol
.analyzed
= false;
1464 basic_block bb
, then_bb
, else_bb
, return_bb
;
1465 gimple_stmt_iterator bsi
;
1476 DECL_IGNORED_P (thunk_fndecl
) = 1;
1477 bitmap_obstack_initialize (NULL
);
1479 if (node
->thunk
.virtual_offset_p
)
1480 virtual_offset
= size_int (virtual_value
);
1482 /* Build the return declaration for the function. */
1483 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1484 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1486 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1487 DECL_ARTIFICIAL (resdecl
) = 1;
1488 DECL_IGNORED_P (resdecl
) = 1;
1489 DECL_RESULT (thunk_fndecl
) = resdecl
;
1492 resdecl
= DECL_RESULT (thunk_fndecl
);
1494 bb
= then_bb
= else_bb
= return_bb
= init_lowered_empty_function (thunk_fndecl
, true);
1496 bsi
= gsi_start_bb (bb
);
1498 /* Build call to the function being thunked. */
1499 if (!VOID_TYPE_P (restype
))
1501 if (DECL_BY_REFERENCE (resdecl
))
1502 restmp
= gimple_fold_indirect_ref (resdecl
);
1503 else if (!is_gimple_reg_type (restype
))
1506 add_local_decl (cfun
, restmp
);
1507 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1510 restmp
= create_tmp_reg (restype
, "retval");
1513 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1515 vargs
.create (nargs
);
1517 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1520 vargs
.quick_push (a
);
1523 for (i
= 1, arg
= DECL_CHAIN (a
); i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1524 vargs
.quick_push (arg
);
1525 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1527 gimple_call_set_from_thunk (call
, true);
1530 gimple_call_set_lhs (call
, restmp
);
1531 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1532 TREE_TYPE (TREE_TYPE (alias
))));
1534 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1535 if (!(gimple_call_flags (call
) & ECF_NORETURN
))
1537 if (restmp
&& !this_adjusting
1538 && (fixed_offset
|| virtual_offset
))
1540 tree true_label
= NULL_TREE
;
1542 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1545 /* If the return type is a pointer, we need to
1546 protect against NULL. We know there will be an
1547 adjustment, because that's why we're emitting a
1549 then_bb
= create_basic_block (NULL
, (void *) 0, bb
);
1550 return_bb
= create_basic_block (NULL
, (void *) 0, then_bb
);
1551 else_bb
= create_basic_block (NULL
, (void *) 0, else_bb
);
1552 add_bb_to_loop (then_bb
, bb
->loop_father
);
1553 add_bb_to_loop (return_bb
, bb
->loop_father
);
1554 add_bb_to_loop (else_bb
, bb
->loop_father
);
1555 remove_edge (single_succ_edge (bb
));
1556 true_label
= gimple_block_label (then_bb
);
1557 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1558 build_zero_cst (TREE_TYPE (restmp
)),
1559 NULL_TREE
, NULL_TREE
);
1560 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1561 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1562 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1563 make_edge (return_bb
, EXIT_BLOCK_PTR
, 0);
1564 make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1565 make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1566 bsi
= gsi_last_bb (then_bb
);
1569 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1570 fixed_offset
, virtual_offset
);
1574 bsi
= gsi_last_bb (else_bb
);
1575 stmt
= gimple_build_assign (restmp
,
1576 build_zero_cst (TREE_TYPE (restmp
)));
1577 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1578 bsi
= gsi_last_bb (return_bb
);
1582 gimple_call_set_tail (call
, true);
1584 /* Build return value. */
1585 ret
= gimple_build_return (restmp
);
1586 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1590 gimple_call_set_tail (call
, true);
1591 remove_edge (single_succ_edge (bb
));
1594 delete_unreachable_blocks ();
1595 update_ssa (TODO_update_ssa
);
1596 #ifdef ENABLE_CHECKING
1597 verify_flow_info ();
1600 /* Since we want to emit the thunk, we explicitly mark its name as
1602 node
->thunk
.thunk_p
= false;
1603 rebuild_cgraph_edges ();
1604 cgraph_add_new_function (thunk_fndecl
, true);
1605 bitmap_obstack_release (NULL
);
1607 current_function_decl
= NULL
;
1611 /* Assemble thunks and aliases associated to NODE. */
1614 assemble_thunks_and_aliases (struct cgraph_node
*node
)
1616 struct cgraph_edge
*e
;
1618 struct ipa_ref
*ref
;
1620 for (e
= node
->callers
; e
;)
1621 if (e
->caller
->thunk
.thunk_p
)
1623 struct cgraph_node
*thunk
= e
->caller
;
1626 assemble_thunks_and_aliases (thunk
);
1627 expand_thunk (thunk
);
1631 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
1633 if (ref
->use
== IPA_REF_ALIAS
)
1635 struct cgraph_node
*alias
= ipa_ref_referring_node (ref
);
1636 bool saved_written
= TREE_ASM_WRITTEN (node
->symbol
.decl
);
1638 /* Force assemble_alias to really output the alias this time instead
1639 of buffering it in same alias pairs. */
1640 TREE_ASM_WRITTEN (node
->symbol
.decl
) = 1;
1641 do_assemble_alias (alias
->symbol
.decl
,
1642 DECL_ASSEMBLER_NAME (node
->symbol
.decl
));
1643 assemble_thunks_and_aliases (alias
);
1644 TREE_ASM_WRITTEN (node
->symbol
.decl
) = saved_written
;
1648 /* Expand function specified by NODE. */
1651 expand_function (struct cgraph_node
*node
)
1653 tree decl
= node
->symbol
.decl
;
1654 location_t saved_loc
;
1656 /* We ought to not compile any inline clones. */
1657 gcc_assert (!node
->global
.inlined_to
);
1659 announce_function (decl
);
1661 gcc_assert (node
->lowered
);
1662 cgraph_get_body (node
);
1664 /* Generate RTL for the body of DECL. */
1666 timevar_push (TV_REST_OF_COMPILATION
);
1668 gcc_assert (cgraph_global_info_ready
);
1670 /* Initialize the default bitmap obstack. */
1671 bitmap_obstack_initialize (NULL
);
1673 /* Initialize the RTL code for the function. */
1674 current_function_decl
= decl
;
1675 saved_loc
= input_location
;
1676 input_location
= DECL_SOURCE_LOCATION (decl
);
1677 init_function_start (decl
);
1679 gimple_register_cfg_hooks ();
1681 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1683 execute_all_ipa_transforms ();
1685 /* Perform all tree transforms and optimizations. */
1687 /* Signal the start of passes. */
1688 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1690 execute_pass_list (g
->get_passes ()->all_passes
);
1692 /* Signal the end of passes. */
1693 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1695 bitmap_obstack_release (®_obstack
);
1697 /* Release the default bitmap obstack. */
1698 bitmap_obstack_release (NULL
);
1700 /* If requested, warn about function definitions where the function will
1701 return a value (usually of some struct or union type) which itself will
1702 take up a lot of stack space. */
1703 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1705 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1707 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1708 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1709 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1712 unsigned int size_as_int
1713 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1715 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1716 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
1719 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
1720 decl
, larger_than_size
);
1724 gimple_set_body (decl
, NULL
);
1725 if (DECL_STRUCT_FUNCTION (decl
) == 0
1726 && !cgraph_get_node (decl
)->origin
)
1728 /* Stop pointing to the local nodes about to be freed.
1729 But DECL_INITIAL must remain nonzero so we know this
1730 was an actual function definition.
1731 For a nested function, this is done in c_pop_function_context.
1732 If rest_of_compilation set this to 0, leave it 0. */
1733 if (DECL_INITIAL (decl
) != 0)
1734 DECL_INITIAL (decl
) = error_mark_node
;
1737 input_location
= saved_loc
;
1740 timevar_pop (TV_REST_OF_COMPILATION
);
1742 /* Make sure that BE didn't give up on compiling. */
1743 gcc_assert (TREE_ASM_WRITTEN (decl
));
1745 current_function_decl
= NULL
;
1747 /* It would make a lot more sense to output thunks before function body to get more
1748 forward and lest backwarding jumps. This however would need solving problem
1749 with comdats. See PR48668. Also aliases must come after function itself to
1750 make one pass assemblers, like one on AIX, happy. See PR 50689.
1751 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1753 assemble_thunks_and_aliases (node
);
1754 cgraph_release_function_body (node
);
1755 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1756 points to the dead function body. */
1757 cgraph_node_remove_callees (node
);
1758 ipa_remove_all_references (&node
->symbol
.ref_list
);
1762 /* Expand all functions that must be output.
1764 Attempt to topologically sort the nodes so function is output when
1765 all called functions are already assembled to allow data to be
1766 propagated across the callgraph. Use a stack to get smaller distance
1767 between a function and its callees (later we may choose to use a more
1768 sophisticated algorithm for function reordering; we will likely want
1769 to use subsections to make the output functions appear in top-down
1773 expand_all_functions (void)
1775 struct cgraph_node
*node
;
1776 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1777 int order_pos
, new_order_pos
= 0;
1780 order_pos
= ipa_reverse_postorder (order
);
1781 gcc_assert (order_pos
== cgraph_n_nodes
);
1783 /* Garbage collector may remove inline clones we eliminate during
1784 optimization. So we must be sure to not reference them. */
1785 for (i
= 0; i
< order_pos
; i
++)
1786 if (order
[i
]->process
)
1787 order
[new_order_pos
++] = order
[i
];
1789 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1795 expand_function (node
);
1798 cgraph_process_new_functions ();
1804 /* This is used to sort the node types by the cgraph order number. */
1806 enum cgraph_order_sort_kind
1808 ORDER_UNDEFINED
= 0,
1814 struct cgraph_order_sort
1816 enum cgraph_order_sort_kind kind
;
1819 struct cgraph_node
*f
;
1820 struct varpool_node
*v
;
1825 /* Output all functions, variables, and asm statements in the order
1826 according to their order fields, which is the order in which they
1827 appeared in the file. This implements -fno-toplevel-reorder. In
1828 this mode we may output functions and variables which don't really
1829 need to be output. */
1832 output_in_order (void)
1835 struct cgraph_order_sort
*nodes
;
1837 struct cgraph_node
*pf
;
1838 struct varpool_node
*pv
;
1839 struct asm_node
*pa
;
1842 nodes
= XCNEWVEC (struct cgraph_order_sort
, max
);
1844 FOR_EACH_DEFINED_FUNCTION (pf
)
1846 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->symbol
.alias
)
1848 i
= pf
->symbol
.order
;
1849 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1850 nodes
[i
].kind
= ORDER_FUNCTION
;
1855 FOR_EACH_DEFINED_VARIABLE (pv
)
1856 if (!DECL_EXTERNAL (pv
->symbol
.decl
))
1858 i
= pv
->symbol
.order
;
1859 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1860 nodes
[i
].kind
= ORDER_VAR
;
1864 for (pa
= asm_nodes
; pa
; pa
= pa
->next
)
1867 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1868 nodes
[i
].kind
= ORDER_ASM
;
1872 /* In toplevel reorder mode we output all statics; mark them as needed. */
1874 for (i
= 0; i
< max
; ++i
)
1875 if (nodes
[i
].kind
== ORDER_VAR
)
1876 varpool_finalize_named_section_flags (nodes
[i
].u
.v
);
1878 for (i
= 0; i
< max
; ++i
)
1880 switch (nodes
[i
].kind
)
1882 case ORDER_FUNCTION
:
1883 nodes
[i
].u
.f
->process
= 0;
1884 expand_function (nodes
[i
].u
.f
);
1888 varpool_assemble_decl (nodes
[i
].u
.v
);
1892 assemble_asm (nodes
[i
].u
.a
->asm_str
);
1895 case ORDER_UNDEFINED
:
1910 gcc::pass_manager
*passes
= g
->get_passes ();
1913 current_function_decl
= NULL
;
1914 gimple_register_cfg_hooks ();
1915 bitmap_obstack_initialize (NULL
);
1917 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
1921 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
1926 /* We never run removal of unreachable nodes after early passes. This is
1927 because TODO is run before the subpasses. It is important to remove
1928 the unreachable functions to save works at IPA level and to get LTO
1929 symbol tables right. */
1930 symtab_remove_unreachable_nodes (true, cgraph_dump_file
);
1932 /* If pass_all_early_optimizations was not scheduled, the state of
1933 the cgraph will not be properly updated. Update it now. */
1934 if (cgraph_state
< CGRAPH_STATE_IPA_SSA
)
1935 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1939 /* Generate coverage variables and constructors. */
1942 /* Process new functions added. */
1944 current_function_decl
= NULL
;
1945 cgraph_process_new_functions ();
1947 execute_ipa_summary_passes
1948 ((struct ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
1951 /* Some targets need to handle LTO assembler output specially. */
1952 if (flag_generate_lto
)
1953 targetm
.asm_out
.lto_start ();
1955 execute_ipa_summary_passes ((struct ipa_opt_pass_d
*)
1956 passes
->all_lto_gen_passes
);
1959 ipa_write_summaries ();
1961 if (flag_generate_lto
)
1962 targetm
.asm_out
.lto_end ();
1964 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
1965 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
1966 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
1968 bitmap_obstack_release (NULL
);
1972 /* Return string alias is alias of. */
1975 get_alias_symbol (tree decl
)
1977 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1978 return get_identifier (TREE_STRING_POINTER
1979 (TREE_VALUE (TREE_VALUE (alias
))));
1983 /* Weakrefs may be associated to external decls and thus not output
1984 at expansion time. Emit all necessary aliases. */
1987 output_weakrefs (void)
1990 FOR_EACH_SYMBOL (node
)
1991 if (node
->symbol
.alias
1992 && !TREE_ASM_WRITTEN (node
->symbol
.decl
)
1993 && node
->symbol
.weakref
)
1997 /* Weakrefs are special by not requiring target definition in current
1998 compilation unit. It is thus bit hard to work out what we want to
2000 When alias target is defined, we need to fetch it from symtab reference,
2001 otherwise it is pointed to by alias_target. */
2002 if (node
->symbol
.alias_target
)
2003 target
= (DECL_P (node
->symbol
.alias_target
)
2004 ? DECL_ASSEMBLER_NAME (node
->symbol
.alias_target
)
2005 : node
->symbol
.alias_target
);
2006 else if (node
->symbol
.analyzed
)
2007 target
= DECL_ASSEMBLER_NAME (symtab_alias_target (node
)->symbol
.decl
);
2011 target
= get_alias_symbol (node
->symbol
.decl
);
2013 do_assemble_alias (node
->symbol
.decl
, target
);
2017 /* Initialize callgraph dump file. */
2022 if (!cgraph_dump_file
)
2023 cgraph_dump_file
= dump_begin (TDI_cgraph
, NULL
);
2027 /* Perform simple optimizations based on callgraph. */
2035 #ifdef ENABLE_CHECKING
2039 timevar_push (TV_CGRAPHOPT
);
2040 if (pre_ipa_mem_report
)
2042 fprintf (stderr
, "Memory consumption before IPA\n");
2043 dump_memory_report (false);
2046 fprintf (stderr
, "Performing interprocedural optimizations\n");
2047 cgraph_state
= CGRAPH_STATE_IPA
;
2049 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2051 lto_streamer_hooks_init ();
2053 /* Don't run the IPA passes if there was any error or sorry messages. */
2057 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2059 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2061 timevar_pop (TV_CGRAPHOPT
);
2065 /* This pass remove bodies of extern inline functions we never inlined.
2066 Do this later so other IPA passes see what is really going on. */
2067 symtab_remove_unreachable_nodes (false, dump_file
);
2068 cgraph_global_info_ready
= true;
2069 if (cgraph_dump_file
)
2071 fprintf (cgraph_dump_file
, "Optimized ");
2072 dump_symtab (cgraph_dump_file
);
2074 if (post_ipa_mem_report
)
2076 fprintf (stderr
, "Memory consumption after IPA\n");
2077 dump_memory_report (false);
2079 timevar_pop (TV_CGRAPHOPT
);
2081 /* Output everything. */
2082 (*debug_hooks
->assembly_start
) ();
2084 fprintf (stderr
, "Assembling functions:\n");
2085 #ifdef ENABLE_CHECKING
2089 cgraph_materialize_all_clones ();
2090 bitmap_obstack_initialize (NULL
);
2091 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2092 symtab_remove_unreachable_nodes (true, dump_file
);
2093 #ifdef ENABLE_CHECKING
2096 bitmap_obstack_release (NULL
);
2097 mark_functions_to_output ();
2099 /* When weakref support is missing, we autmatically translate all
2100 references to NODE to references to its ultimate alias target.
2101 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2104 Set up this mapping before we output any assembler but once we are sure
2105 that all symbol renaming is done.
2107 FIXME: All this uglyness can go away if we just do renaming at gimple
2108 level by physically rewritting the IL. At the moment we can only redirect
2109 calls, so we need infrastructure for renaming references as well. */
2110 #ifndef ASM_OUTPUT_WEAKREF
2113 FOR_EACH_SYMBOL (node
)
2114 if (node
->symbol
.alias
2115 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->symbol
.decl
)))
2117 IDENTIFIER_TRANSPARENT_ALIAS
2118 (DECL_ASSEMBLER_NAME (node
->symbol
.decl
)) = 1;
2119 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->symbol
.decl
))
2120 = (node
->symbol
.alias_target
? node
->symbol
.alias_target
2121 : DECL_ASSEMBLER_NAME (symtab_alias_target (node
)->symbol
.decl
));
2125 cgraph_state
= CGRAPH_STATE_EXPANSION
;
2126 if (!flag_toplevel_reorder
)
2130 output_asm_statements ();
2132 expand_all_functions ();
2133 varpool_output_variables ();
2136 cgraph_process_new_functions ();
2137 cgraph_state
= CGRAPH_STATE_FINISHED
;
2140 if (cgraph_dump_file
)
2142 fprintf (cgraph_dump_file
, "\nFinal ");
2143 dump_symtab (cgraph_dump_file
);
2145 #ifdef ENABLE_CHECKING
2147 /* Double check that all inline clones are gone and that all
2148 function bodies have been released from memory. */
2151 struct cgraph_node
*node
;
2152 bool error_found
= false;
2154 FOR_EACH_DEFINED_FUNCTION (node
)
2155 if (node
->global
.inlined_to
2156 || gimple_has_body_p (node
->symbol
.decl
))
2159 dump_cgraph_node (stderr
, node
);
2162 internal_error ("nodes with unreleased memory found");
2168 /* Analyze the whole compilation unit once it is parsed completely. */
2171 finalize_compilation_unit (void)
2173 timevar_push (TV_CGRAPH
);
2175 /* If we're here there's no current function anymore. Some frontends
2176 are lazy in clearing these. */
2177 current_function_decl
= NULL
;
2180 /* Do not skip analyzing the functions if there were errors, we
2181 miss diagnostics for following functions otherwise. */
2183 /* Emit size functions we didn't inline. */
2184 finalize_size_functions ();
2186 /* Mark alias targets necessary and emit diagnostics. */
2187 handle_alias_pairs ();
2191 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2195 if (flag_dump_passes
)
2198 /* Gimplify and lower all functions, compute reachability and
2199 remove unreachable nodes. */
2200 analyze_functions ();
2202 /* Mark alias targets necessary and emit diagnostics. */
2203 handle_alias_pairs ();
2205 /* Gimplify and lower thunks. */
2206 analyze_functions ();
2208 /* Finally drive the pass manager. */
2211 timevar_pop (TV_CGRAPH
);
2215 #include "gt-cgraphunit.h"