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 exmaple, 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"
195 #include "regset.h" /* FIXME: For reg_obstack. */
197 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
198 secondary queue used during optimization to accommodate passes that
199 may generate new functions that need to be optimized and expanded. */
200 cgraph_node_set cgraph_new_nodes
;
202 static void expand_all_functions (void);
203 static void mark_functions_to_output (void);
204 static void expand_function (struct cgraph_node
*);
205 static void analyze_function (struct cgraph_node
*);
206 static void handle_alias_pairs (void);
208 FILE *cgraph_dump_file
;
210 /* Linked list of cgraph asm nodes. */
211 struct asm_node
*asm_nodes
;
213 /* Last node in cgraph_asm_nodes. */
214 static GTY(()) struct asm_node
*asm_last_node
;
216 /* Used for vtable lookup in thunk adjusting. */
217 static GTY (()) tree vtable_entry_type
;
219 /* Determine if symbol DECL is needed. That is, visible to something
220 either outside this translation unit, something magic in the system
223 decide_is_symbol_needed (symtab_node node
)
225 tree decl
= node
->symbol
.decl
;
227 /* Double check that no one output the function into assembly file
229 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
230 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
232 if (!node
->symbol
.definition
)
235 /* Devirtualization may access these. */
236 if (DECL_VIRTUAL_P (decl
) && optimize
)
239 if (DECL_EXTERNAL (decl
))
242 /* If the user told us it is used, then it must be so. */
243 if (node
->symbol
.force_output
)
246 /* ABI forced symbols are needed when they are external. */
247 if (node
->symbol
.forced_by_abi
&& TREE_PUBLIC (decl
))
250 /* Keep constructors, destructors and virtual functions. */
251 if (TREE_CODE (decl
) == FUNCTION_DECL
252 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
255 /* Externally visible variables must be output. The exception is
256 COMDAT variables that must be output only when they are needed. */
257 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
263 /* Head of the queue of nodes to be processed while building callgraph */
265 static symtab_node first
= (symtab_node
)(void *)1;
267 /* Add NODE to queue starting at FIRST.
268 The queue is linked via AUX pointers and terminated by pointer to 1. */
271 enqueue_node (symtab_node node
)
273 if (node
->symbol
.aux
)
275 gcc_checking_assert (first
);
276 node
->symbol
.aux
= first
;
280 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
281 functions into callgraph in a way so they look like ordinary reachable
282 functions inserted into callgraph already at construction time. */
285 cgraph_process_new_functions (void)
289 struct cgraph_node
*node
;
290 cgraph_node_set_iterator csi
;
292 if (!cgraph_new_nodes
)
294 handle_alias_pairs ();
295 /* Note that this queue may grow as its being processed, as the new
296 functions may generate new ones. */
297 for (csi
= csi_start (cgraph_new_nodes
); !csi_end_p (csi
); csi_next (&csi
))
299 node
= csi_node (csi
);
300 fndecl
= node
->symbol
.decl
;
301 switch (cgraph_state
)
303 case CGRAPH_STATE_CONSTRUCTION
:
304 /* At construction time we just need to finalize function and move
305 it into reachable functions list. */
307 cgraph_finalize_function (fndecl
, false);
309 cgraph_call_function_insertion_hooks (node
);
310 enqueue_node ((symtab_node
) node
);
313 case CGRAPH_STATE_IPA
:
314 case CGRAPH_STATE_IPA_SSA
:
315 /* When IPA optimization already started, do all essential
316 transformations that has been already performed on the whole
317 cgraph but not on this function. */
319 gimple_register_cfg_hooks ();
320 if (!node
->symbol
.analyzed
)
321 analyze_function (node
);
322 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
323 if ((cgraph_state
== CGRAPH_STATE_IPA_SSA
324 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
325 /* When not optimizing, be sure we run early local passes anyway
328 execute_pass_list (pass_early_local_passes
.pass
.sub
);
330 compute_inline_parameters (node
, true);
331 free_dominance_info (CDI_POST_DOMINATORS
);
332 free_dominance_info (CDI_DOMINATORS
);
334 cgraph_call_function_insertion_hooks (node
);
337 case CGRAPH_STATE_EXPANSION
:
338 /* Functions created during expansion shall be compiled
341 cgraph_call_function_insertion_hooks (node
);
342 expand_function (node
);
350 free_cgraph_node_set (cgraph_new_nodes
);
351 cgraph_new_nodes
= NULL
;
355 /* As an GCC extension we allow redefinition of the function. The
356 semantics when both copies of bodies differ is not well defined.
357 We replace the old body with new body so in unit at a time mode
358 we always use new body, while in normal mode we may end up with
359 old body inlined into some functions and new body expanded and
362 ??? It may make more sense to use one body for inlining and other
363 body for expanding the function but this is difficult to do. */
366 cgraph_reset_node (struct cgraph_node
*node
)
368 /* If node->process is set, then we have already begun whole-unit analysis.
369 This is *not* testing for whether we've already emitted the function.
370 That case can be sort-of legitimately seen with real function redefinition
371 errors. I would argue that the front end should never present us with
372 such a case, but don't enforce that for now. */
373 gcc_assert (!node
->process
);
375 /* Reset our data structures so we can analyze the function again. */
376 memset (&node
->local
, 0, sizeof (node
->local
));
377 memset (&node
->global
, 0, sizeof (node
->global
));
378 memset (&node
->rtl
, 0, sizeof (node
->rtl
));
379 node
->symbol
.analyzed
= false;
380 node
->symbol
.definition
= false;
381 node
->symbol
.alias
= false;
382 node
->symbol
.cpp_implicit_alias
= false;
384 cgraph_node_remove_callees (node
);
385 ipa_remove_all_references (&node
->symbol
.ref_list
);
388 /* Return true when there are references to NODE. */
391 referred_to_p (symtab_node node
)
395 /* See if there are any references at all. */
396 if (ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
, 0, ref
))
398 /* For functions check also calls. */
399 cgraph_node
*cn
= dyn_cast
<cgraph_node
> (node
);
400 if (cn
&& cn
->callers
)
405 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
406 logic in effect. If NESTED is true, then our caller cannot stand to have
407 the garbage collector run at the moment. We would need to either create
408 a new GC context, or just not compile right now. */
411 cgraph_finalize_function (tree decl
, bool nested
)
413 struct cgraph_node
*node
= cgraph_get_create_node (decl
);
415 if (node
->symbol
.definition
)
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 struct cgraph_node
*node
;
480 switch (cgraph_state
)
482 case CGRAPH_STATE_PARSING
:
483 cgraph_finalize_function (fndecl
, false);
485 case CGRAPH_STATE_CONSTRUCTION
:
486 /* Just enqueue function to be processed at nearest occurrence. */
487 node
= cgraph_create_node (fndecl
);
489 node
->lowered
= true;
490 if (!cgraph_new_nodes
)
491 cgraph_new_nodes
= cgraph_node_set_new ();
492 cgraph_node_set_add (cgraph_new_nodes
, node
);
495 case CGRAPH_STATE_IPA
:
496 case CGRAPH_STATE_IPA_SSA
:
497 case CGRAPH_STATE_EXPANSION
:
498 /* Bring the function into finalized state and enqueue for later
499 analyzing and compilation. */
500 node
= cgraph_get_create_node (fndecl
);
501 node
->local
.local
= false;
502 node
->symbol
.definition
= true;
503 node
->symbol
.force_output
= true;
504 if (!lowered
&& cgraph_state
== CGRAPH_STATE_EXPANSION
)
506 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
507 gimple_register_cfg_hooks ();
508 bitmap_obstack_initialize (NULL
);
509 execute_pass_list (all_lowering_passes
);
510 execute_pass_list (pass_early_local_passes
.pass
.sub
);
511 bitmap_obstack_release (NULL
);
517 node
->lowered
= true;
518 if (!cgraph_new_nodes
)
519 cgraph_new_nodes
= cgraph_node_set_new ();
520 cgraph_node_set_add (cgraph_new_nodes
, node
);
523 case CGRAPH_STATE_FINISHED
:
524 /* At the very end of compilation we have to do all the work up
526 node
= cgraph_create_node (fndecl
);
528 node
->lowered
= true;
529 node
->symbol
.definition
= true;
530 analyze_function (node
);
531 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
532 gimple_register_cfg_hooks ();
533 bitmap_obstack_initialize (NULL
);
534 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
535 execute_pass_list (pass_early_local_passes
.pass
.sub
);
536 bitmap_obstack_release (NULL
);
538 expand_function (node
);
545 /* Set a personality if required and we already passed EH lowering. */
547 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
548 == eh_personality_lang
))
549 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
552 /* Add a top-level asm statement to the list. */
555 add_asm_node (tree asm_str
)
557 struct asm_node
*node
;
559 node
= ggc_alloc_cleared_asm_node ();
560 node
->asm_str
= asm_str
;
561 node
->order
= symtab_order
++;
563 if (asm_nodes
== NULL
)
566 asm_last_node
->next
= node
;
567 asm_last_node
= node
;
571 /* Output all asm statements we have stored up to be output. */
574 output_asm_statements (void)
576 struct asm_node
*can
;
581 for (can
= asm_nodes
; can
; can
= can
->next
)
582 assemble_asm (can
->asm_str
);
586 /* Analyze the function scheduled to be output. */
588 analyze_function (struct cgraph_node
*node
)
590 tree decl
= node
->symbol
.decl
;
591 location_t saved_loc
= input_location
;
592 input_location
= DECL_SOURCE_LOCATION (decl
);
594 if (node
->symbol
.alias
)
596 ((symtab_node
) node
, (symtab_node
) cgraph_get_node (node
->symbol
.alias_target
));
597 else if (node
->thunk
.thunk_p
)
599 cgraph_create_edge (node
, cgraph_get_node (node
->thunk
.alias
),
600 NULL
, 0, CGRAPH_FREQ_BASE
);
601 node
->thunk
.alias
= NULL
;
603 else if (node
->dispatcher_function
)
605 /* Generate the dispatcher body of multi-versioned functions. */
606 struct cgraph_function_version_info
*dispatcher_version_info
607 = get_cgraph_node_version (node
);
608 if (dispatcher_version_info
!= NULL
609 && (dispatcher_version_info
->dispatcher_resolver
612 tree resolver
= NULL_TREE
;
613 gcc_assert (targetm
.generate_version_dispatcher_body
);
614 resolver
= targetm
.generate_version_dispatcher_body (node
);
615 gcc_assert (resolver
!= NULL_TREE
);
620 push_cfun (DECL_STRUCT_FUNCTION (decl
));
622 assign_assembler_name_if_neeeded (node
->symbol
.decl
);
624 /* Make sure to gimplify bodies only once. During analyzing a
625 function we lower it, which will require gimplified nested
626 functions, so we can end up here with an already gimplified
628 if (!gimple_has_body_p (decl
))
629 gimplify_function_tree (decl
);
630 dump_function (TDI_generic
, decl
);
632 /* Lower the function. */
636 lower_nested_functions (node
->symbol
.decl
);
637 gcc_assert (!node
->nested
);
639 gimple_register_cfg_hooks ();
640 bitmap_obstack_initialize (NULL
);
641 execute_pass_list (all_lowering_passes
);
642 free_dominance_info (CDI_POST_DOMINATORS
);
643 free_dominance_info (CDI_DOMINATORS
);
645 bitmap_obstack_release (NULL
);
646 node
->lowered
= true;
651 node
->symbol
.analyzed
= true;
653 input_location
= saved_loc
;
656 /* C++ frontend produce same body aliases all over the place, even before PCH
657 gets streamed out. It relies on us linking the aliases with their function
658 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
659 first produce aliases without links, but once C++ FE is sure he won't sream
660 PCH we build the links via this function. */
663 cgraph_process_same_body_aliases (void)
666 FOR_EACH_SYMBOL (node
)
667 if (node
->symbol
.cpp_implicit_alias
&& !node
->symbol
.analyzed
)
670 TREE_CODE (node
->symbol
.alias_target
) == VAR_DECL
671 ? (symtab_node
)varpool_node_for_decl (node
->symbol
.alias_target
)
672 : (symtab_node
)cgraph_get_create_node (node
->symbol
.alias_target
));
673 cpp_implicit_aliases_done
= true;
676 /* Process attributes common for vars and functions. */
679 process_common_attributes (tree decl
)
681 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
683 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
685 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
686 "%<weakref%> attribute should be accompanied with"
687 " an %<alias%> attribute");
688 DECL_WEAK (decl
) = 0;
689 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
690 DECL_ATTRIBUTES (decl
));
694 /* Look for externally_visible and used attributes and mark cgraph nodes
697 We cannot mark the nodes at the point the attributes are processed (in
698 handle_*_attribute) because the copy of the declarations available at that
699 point may not be canonical. For example, in:
702 void f() __attribute__((used));
704 the declaration we see in handle_used_attribute will be the second
705 declaration -- but the front end will subsequently merge that declaration
706 with the original declaration and discard the second declaration.
708 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
711 void f() __attribute__((externally_visible));
715 So, we walk the nodes at the end of the translation unit, applying the
716 attributes at that point. */
719 process_function_and_variable_attributes (struct cgraph_node
*first
,
720 struct varpool_node
*first_var
)
722 struct cgraph_node
*node
;
723 struct varpool_node
*vnode
;
725 for (node
= cgraph_first_function (); node
!= first
;
726 node
= cgraph_next_function (node
))
728 tree decl
= node
->symbol
.decl
;
729 if (DECL_PRESERVE_P (decl
))
730 cgraph_mark_force_output_node (node
);
731 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
733 if (! TREE_PUBLIC (node
->symbol
.decl
))
734 warning_at (DECL_SOURCE_LOCATION (node
->symbol
.decl
), OPT_Wattributes
,
735 "%<externally_visible%>"
736 " attribute have effect only on public objects");
738 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
739 && (node
->symbol
.definition
&& !node
->symbol
.alias
))
741 warning_at (DECL_SOURCE_LOCATION (node
->symbol
.decl
), OPT_Wattributes
,
742 "%<weakref%> attribute ignored"
743 " because function is defined");
744 DECL_WEAK (decl
) = 0;
745 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
746 DECL_ATTRIBUTES (decl
));
749 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
750 && !DECL_DECLARED_INLINE_P (decl
)
751 /* redefining extern inline function makes it DECL_UNINLINABLE. */
752 && !DECL_UNINLINABLE (decl
))
753 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
754 "always_inline function might not be inlinable");
756 process_common_attributes (decl
);
758 for (vnode
= varpool_first_variable (); vnode
!= first_var
;
759 vnode
= varpool_next_variable (vnode
))
761 tree decl
= vnode
->symbol
.decl
;
762 if (DECL_EXTERNAL (decl
)
763 && DECL_INITIAL (decl
)
764 && const_value_known_p (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
;
838 symtab_node node
, next
;
843 bitmap_obstack_initialize (NULL
);
844 cgraph_state
= CGRAPH_STATE_CONSTRUCTION
;
846 /* Ugly, but the fixup can not happen at a time same body alias is created;
847 C++ FE is confused about the COMDAT groups being right. */
848 if (cpp_implicit_aliases_done
)
849 FOR_EACH_SYMBOL (node
)
850 if (node
->symbol
.cpp_implicit_alias
)
851 fixup_same_cpp_alias_visibility (node
, symtab_alias_target (node
));
853 /* Analysis adds static variables that in turn adds references to new functions.
854 So we need to iterate the process until it stabilize. */
858 process_function_and_variable_attributes (first_analyzed
,
861 /* First identify the trivially needed symbols. */
862 for (node
= symtab_nodes
;
863 node
!= (symtab_node
)first_analyzed
864 && node
!= (symtab_node
)first_analyzed_var
; node
= node
->symbol
.next
)
866 if (decide_is_symbol_needed (node
))
869 if (!changed
&& cgraph_dump_file
)
870 fprintf (cgraph_dump_file
, "Trivially needed symbols:");
872 if (cgraph_dump_file
)
873 fprintf (cgraph_dump_file
, " %s", symtab_node_asm_name (node
));
875 if (node
== (symtab_node
)first_analyzed
876 || node
== (symtab_node
)first_analyzed_var
)
879 cgraph_process_new_functions ();
880 first_analyzed_var
= varpool_first_variable ();
881 first_analyzed
= cgraph_first_function ();
883 if (changed
&& dump_file
)
884 fprintf (cgraph_dump_file
, "\n");
886 /* Lower representation, build callgraph edges and references for all trivially
887 needed symbols and all symbols referred by them. */
888 while (first
!= (symtab_node
)(void *)1)
892 first
= (symtab_node
)first
->symbol
.aux
;
893 cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
);
894 if (cnode
&& cnode
->symbol
.definition
)
896 struct cgraph_edge
*edge
;
897 tree decl
= cnode
->symbol
.decl
;
899 /* ??? It is possible to create extern inline function
900 and later using weak alias attribute to kill its body.
901 See gcc.c-torture/compile/20011119-1.c */
902 if (!DECL_STRUCT_FUNCTION (decl
)
903 && !cnode
->symbol
.alias
904 && !cnode
->thunk
.thunk_p
905 && !cnode
->dispatcher_function
)
907 cgraph_reset_node (cnode
);
908 cnode
->local
.redefined_extern_inline
= true;
912 if (!cnode
->symbol
.analyzed
)
913 analyze_function (cnode
);
915 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
916 if (edge
->callee
->symbol
.definition
)
917 enqueue_node ((symtab_node
)edge
->callee
);
919 /* If decl is a clone of an abstract function,
920 mark that abstract function so that we don't release its body.
921 The DECL_INITIAL() of that abstract function declaration
922 will be later needed to output debug info. */
923 if (DECL_ABSTRACT_ORIGIN (decl
))
925 struct cgraph_node
*origin_node
926 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl
));
927 origin_node
->abstract_and_needed
= true;
932 varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
);
933 if (vnode
&& vnode
->symbol
.definition
&& !vnode
->symbol
.analyzed
)
934 varpool_analyze_node (vnode
);
937 if (node
->symbol
.same_comdat_group
)
940 for (next
= node
->symbol
.same_comdat_group
;
942 next
= next
->symbol
.same_comdat_group
)
945 for (i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
946 if (ref
->referred
->symbol
.definition
)
947 enqueue_node (ref
->referred
);
948 cgraph_process_new_functions ();
952 /* Collect entry points to the unit. */
953 if (cgraph_dump_file
)
955 fprintf (cgraph_dump_file
, "\n\nInitial ");
956 dump_symtab (cgraph_dump_file
);
959 if (cgraph_dump_file
)
960 fprintf (cgraph_dump_file
, "\nRemoving unused symbols:");
962 for (node
= symtab_nodes
;
963 node
!= (symtab_node
)first_handled
964 && node
!= (symtab_node
)first_handled_var
; node
= next
)
966 next
= node
->symbol
.next
;
967 if (!node
->symbol
.aux
&& !referred_to_p (node
))
969 if (cgraph_dump_file
)
970 fprintf (cgraph_dump_file
, " %s", symtab_node_name (node
));
971 symtab_remove_node (node
);
974 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
976 tree decl
= node
->symbol
.decl
;
978 if (cnode
->symbol
.definition
&& !gimple_has_body_p (decl
)
979 && !cnode
->symbol
.alias
980 && !cnode
->thunk
.thunk_p
)
981 cgraph_reset_node (cnode
);
983 gcc_assert (!cnode
->symbol
.definition
|| cnode
->thunk
.thunk_p
984 || cnode
->symbol
.alias
985 || gimple_has_body_p (decl
));
986 gcc_assert (cnode
->symbol
.analyzed
== cnode
->symbol
.definition
);
988 node
->symbol
.aux
= NULL
;
990 first_analyzed
= cgraph_first_function ();
991 first_analyzed_var
= varpool_first_variable ();
992 if (cgraph_dump_file
)
994 fprintf (cgraph_dump_file
, "\n\nReclaimed ");
995 dump_symtab (cgraph_dump_file
);
997 bitmap_obstack_release (NULL
);
1001 /* Translate the ugly representation of aliases as alias pairs into nice
1002 representation in callgraph. We don't handle all cases yet,
1006 handle_alias_pairs (void)
1011 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1013 symtab_node target_node
= symtab_node_for_asm (p
->target
);
1015 /* Weakrefs with target not defined in current unit are easy to handle; they
1016 behave just as external variables except we need to note the alias flag
1017 to later output the weakref pseudo op into asm file. */
1018 if (!target_node
&& lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1020 symtab_node node
= symtab_get_node (p
->decl
);
1023 node
->symbol
.alias_target
= p
->target
;
1024 node
->symbol
.alias
= true;
1026 DECL_EXTERNAL (p
->decl
) = 1;
1027 alias_pairs
->unordered_remove (i
);
1030 else if (!target_node
)
1032 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1033 alias_pairs
->unordered_remove (i
);
1037 /* Normally EXTERNAL flag is used to mark external inlines,
1038 however for aliases it seems to be allowed to use it w/o
1039 any meaning. See gcc.dg/attr-alias-3.c
1040 However for weakref we insist on EXTERNAL flag being set.
1041 See gcc.dg/attr-alias-5.c */
1042 if (DECL_EXTERNAL (p
->decl
))
1043 DECL_EXTERNAL (p
->decl
)
1044 = lookup_attribute ("weakref",
1045 DECL_ATTRIBUTES (p
->decl
)) != NULL
;
1047 if (DECL_EXTERNAL (target_node
->symbol
.decl
)
1048 /* We use local aliases for C++ thunks to force the tailcall
1049 to bind locally. This is a hack - to keep it working do
1050 the following (which is not strictly correct). */
1051 && (! TREE_CODE (target_node
->symbol
.decl
) == FUNCTION_DECL
1052 || ! DECL_VIRTUAL_P (target_node
->symbol
.decl
))
1053 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1055 error ("%q+D aliased to external symbol %qE",
1056 p
->decl
, p
->target
);
1059 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1060 && target_node
&& is_a
<cgraph_node
> (target_node
))
1062 struct cgraph_node
*src_node
= cgraph_get_node (p
->decl
);
1063 if (src_node
&& src_node
->symbol
.definition
)
1064 cgraph_reset_node (src_node
);
1065 cgraph_create_function_alias (p
->decl
, target_node
->symbol
.decl
);
1066 alias_pairs
->unordered_remove (i
);
1068 else if (TREE_CODE (p
->decl
) == VAR_DECL
1069 && target_node
&& is_a
<varpool_node
> (target_node
))
1071 varpool_create_variable_alias (p
->decl
, target_node
->symbol
.decl
);
1072 alias_pairs
->unordered_remove (i
);
1076 error ("%q+D alias in between function and variable is not supported",
1078 warning (0, "%q+D aliased declaration",
1079 target_node
->symbol
.decl
);
1080 alias_pairs
->unordered_remove (i
);
1083 vec_free (alias_pairs
);
1087 /* Figure out what functions we want to assemble. */
1090 mark_functions_to_output (void)
1092 struct cgraph_node
*node
;
1093 #ifdef ENABLE_CHECKING
1094 bool check_same_comdat_groups
= false;
1096 FOR_EACH_FUNCTION (node
)
1097 gcc_assert (!node
->process
);
1100 FOR_EACH_FUNCTION (node
)
1102 tree decl
= node
->symbol
.decl
;
1104 gcc_assert (!node
->process
|| node
->symbol
.same_comdat_group
);
1108 /* We need to output all local functions that are used and not
1109 always inlined, as well as those that are reachable from
1110 outside the current compilation unit. */
1111 if (node
->symbol
.analyzed
1112 && !node
->thunk
.thunk_p
1113 && !node
->symbol
.alias
1114 && !node
->global
.inlined_to
1115 && !TREE_ASM_WRITTEN (decl
)
1116 && !DECL_EXTERNAL (decl
))
1119 if (node
->symbol
.same_comdat_group
)
1121 struct cgraph_node
*next
;
1122 for (next
= cgraph (node
->symbol
.same_comdat_group
);
1124 next
= cgraph (next
->symbol
.same_comdat_group
))
1125 if (!next
->thunk
.thunk_p
&& !next
->symbol
.alias
)
1129 else if (node
->symbol
.same_comdat_group
)
1131 #ifdef ENABLE_CHECKING
1132 check_same_comdat_groups
= true;
1137 /* We should've reclaimed all functions that are not needed. */
1138 #ifdef ENABLE_CHECKING
1139 if (!node
->global
.inlined_to
1140 && gimple_has_body_p (decl
)
1141 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1142 are inside partition, we can end up not removing the body since we no longer
1143 have analyzed node pointing to it. */
1144 && !node
->symbol
.in_other_partition
1145 && !node
->symbol
.alias
1147 && !DECL_EXTERNAL (decl
))
1149 dump_cgraph_node (stderr
, node
);
1150 internal_error ("failed to reclaim unneeded function");
1153 gcc_assert (node
->global
.inlined_to
1154 || !gimple_has_body_p (decl
)
1155 || node
->symbol
.in_other_partition
1157 || DECL_ARTIFICIAL (decl
)
1158 || DECL_EXTERNAL (decl
));
1163 #ifdef ENABLE_CHECKING
1164 if (check_same_comdat_groups
)
1165 FOR_EACH_FUNCTION (node
)
1166 if (node
->symbol
.same_comdat_group
&& !node
->process
)
1168 tree decl
= node
->symbol
.decl
;
1169 if (!node
->global
.inlined_to
1170 && gimple_has_body_p (decl
)
1171 /* FIXME: in an ltrans unit when the offline copy is outside a
1172 partition but inline copies are inside a partition, we can
1173 end up not removing the body since we no longer have an
1174 analyzed node pointing to it. */
1175 && !node
->symbol
.in_other_partition
1177 && !DECL_EXTERNAL (decl
))
1179 dump_cgraph_node (stderr
, node
);
1180 internal_error ("failed to reclaim unneeded function in same "
1187 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1188 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1190 Set current_function_decl and cfun to newly constructed empty function body.
1191 return basic block in the function body. */
1194 init_lowered_empty_function (tree decl
, bool in_ssa
)
1198 current_function_decl
= decl
;
1199 allocate_struct_function (decl
, false);
1200 gimple_register_cfg_hooks ();
1201 init_empty_tree_cfg ();
1205 init_tree_ssa (cfun
);
1206 init_ssa_operands (cfun
);
1207 cfun
->gimple_df
->in_ssa_p
= true;
1210 DECL_INITIAL (decl
) = make_node (BLOCK
);
1212 DECL_SAVED_TREE (decl
) = error_mark_node
;
1213 cfun
->curr_properties
|=
1214 (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_cfg
| PROP_ssa
| PROP_gimple_any
);
1216 /* Create BB for body of the function and connect it properly. */
1217 bb
= create_basic_block (NULL
, (void *) 0, ENTRY_BLOCK_PTR
);
1218 make_edge (ENTRY_BLOCK_PTR
, bb
, 0);
1219 make_edge (bb
, EXIT_BLOCK_PTR
, 0);
1224 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1225 offset indicated by VIRTUAL_OFFSET, if that is
1226 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1227 zero for a result adjusting thunk. */
1230 thunk_adjust (gimple_stmt_iterator
* bsi
,
1231 tree ptr
, bool this_adjusting
,
1232 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1238 && fixed_offset
!= 0)
1240 stmt
= gimple_build_assign
1241 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1244 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1247 /* If there's a virtual offset, look up that value in the vtable and
1248 adjust the pointer again. */
1255 if (!vtable_entry_type
)
1257 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1258 TREE_TYPE (vfunc_type
) = integer_type_node
;
1259 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1260 layout_type (vfunc_type
);
1262 vtable_entry_type
= build_pointer_type (vfunc_type
);
1266 create_tmp_reg (build_pointer_type
1267 (build_pointer_type (vtable_entry_type
)), "vptr");
1269 /* The vptr is always at offset zero in the object. */
1270 stmt
= gimple_build_assign (vtabletmp
,
1271 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1273 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1275 /* Form the vtable address. */
1276 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1278 stmt
= gimple_build_assign (vtabletmp2
,
1279 build_simple_mem_ref (vtabletmp
));
1280 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1282 /* Find the entry with the vcall offset. */
1283 stmt
= gimple_build_assign (vtabletmp2
,
1284 fold_build_pointer_plus_loc (input_location
,
1287 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1289 /* Get the offset itself. */
1290 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1292 stmt
= gimple_build_assign (vtabletmp3
,
1293 build_simple_mem_ref (vtabletmp2
));
1294 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1296 /* Adjust the `this' pointer. */
1297 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1298 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1299 GSI_CONTINUE_LINKING
);
1303 && fixed_offset
!= 0)
1304 /* Adjust the pointer by the constant. */
1308 if (TREE_CODE (ptr
) == VAR_DECL
)
1312 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1313 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1314 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1316 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1317 ptrtmp
, fixed_offset
);
1320 /* Emit the statement and gimplify the adjustment expression. */
1321 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1322 stmt
= gimple_build_assign (ret
, ptr
);
1323 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1328 /* Produce assembler for thunk NODE. */
1331 assemble_thunk (struct cgraph_node
*node
)
1333 bool this_adjusting
= node
->thunk
.this_adjusting
;
1334 HOST_WIDE_INT fixed_offset
= node
->thunk
.fixed_offset
;
1335 HOST_WIDE_INT virtual_value
= node
->thunk
.virtual_value
;
1336 tree virtual_offset
= NULL
;
1337 tree alias
= node
->callees
->callee
->symbol
.decl
;
1338 tree thunk_fndecl
= node
->symbol
.decl
;
1339 tree a
= DECL_ARGUMENTS (thunk_fndecl
);
1341 current_function_decl
= thunk_fndecl
;
1343 /* Ensure thunks are emitted in their correct sections. */
1344 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
1347 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1348 virtual_value
, alias
))
1352 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1354 DECL_RESULT (thunk_fndecl
)
1355 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1356 RESULT_DECL
, 0, restype
);
1357 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1359 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1361 fn_block
= make_node (BLOCK
);
1362 BLOCK_VARS (fn_block
) = a
;
1363 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1364 init_function_start (thunk_fndecl
);
1366 insn_locations_init ();
1367 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1368 prologue_location
= curr_insn_location ();
1369 assemble_start_function (thunk_fndecl
, fnname
);
1371 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1372 fixed_offset
, virtual_value
, alias
);
1374 assemble_end_function (thunk_fndecl
, fnname
);
1375 insn_locations_finalize ();
1376 init_insn_lengths ();
1377 free_after_compilation (cfun
);
1379 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1380 node
->thunk
.thunk_p
= false;
1381 node
->symbol
.analyzed
= false;
1386 basic_block bb
, then_bb
, else_bb
, return_bb
;
1387 gimple_stmt_iterator bsi
;
1398 DECL_IGNORED_P (thunk_fndecl
) = 1;
1399 bitmap_obstack_initialize (NULL
);
1401 if (node
->thunk
.virtual_offset_p
)
1402 virtual_offset
= size_int (virtual_value
);
1404 /* Build the return declaration for the function. */
1405 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1406 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1408 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1409 DECL_ARTIFICIAL (resdecl
) = 1;
1410 DECL_IGNORED_P (resdecl
) = 1;
1411 DECL_RESULT (thunk_fndecl
) = resdecl
;
1414 resdecl
= DECL_RESULT (thunk_fndecl
);
1416 bb
= then_bb
= else_bb
= return_bb
= init_lowered_empty_function (thunk_fndecl
, true);
1418 bsi
= gsi_start_bb (bb
);
1420 /* Build call to the function being thunked. */
1421 if (!VOID_TYPE_P (restype
))
1423 if (!is_gimple_reg_type (restype
))
1426 add_local_decl (cfun
, restmp
);
1427 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1430 restmp
= create_tmp_reg (restype
, "retval");
1433 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1435 vargs
.create (nargs
);
1437 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1440 vargs
.quick_push (a
);
1441 for (i
= 1, arg
= DECL_CHAIN (a
); i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1442 vargs
.quick_push (arg
);
1443 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1445 gimple_call_set_from_thunk (call
, true);
1447 gimple_call_set_lhs (call
, restmp
);
1448 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1450 if (restmp
&& !this_adjusting
)
1452 tree true_label
= NULL_TREE
;
1454 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1457 /* If the return type is a pointer, we need to
1458 protect against NULL. We know there will be an
1459 adjustment, because that's why we're emitting a
1461 then_bb
= create_basic_block (NULL
, (void *) 0, bb
);
1462 return_bb
= create_basic_block (NULL
, (void *) 0, then_bb
);
1463 else_bb
= create_basic_block (NULL
, (void *) 0, else_bb
);
1464 remove_edge (single_succ_edge (bb
));
1465 true_label
= gimple_block_label (then_bb
);
1466 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1467 build_zero_cst (TREE_TYPE (restmp
)),
1468 NULL_TREE
, NULL_TREE
);
1469 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1470 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1471 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1472 make_edge (return_bb
, EXIT_BLOCK_PTR
, 0);
1473 make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1474 make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1475 bsi
= gsi_last_bb (then_bb
);
1478 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1479 fixed_offset
, virtual_offset
);
1483 bsi
= gsi_last_bb (else_bb
);
1484 stmt
= gimple_build_assign (restmp
,
1485 build_zero_cst (TREE_TYPE (restmp
)));
1486 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1487 bsi
= gsi_last_bb (return_bb
);
1491 gimple_call_set_tail (call
, true);
1493 /* Build return value. */
1494 ret
= gimple_build_return (restmp
);
1495 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1497 delete_unreachable_blocks ();
1498 update_ssa (TODO_update_ssa
);
1500 /* Since we want to emit the thunk, we explicitly mark its name as
1502 node
->thunk
.thunk_p
= false;
1503 cgraph_node_remove_callees (node
);
1504 cgraph_add_new_function (thunk_fndecl
, true);
1505 bitmap_obstack_release (NULL
);
1507 current_function_decl
= NULL
;
1513 /* Assemble thunks and aliases associated to NODE. */
1516 assemble_thunks_and_aliases (struct cgraph_node
*node
)
1518 struct cgraph_edge
*e
;
1520 struct ipa_ref
*ref
;
1522 for (e
= node
->callers
; e
;)
1523 if (e
->caller
->thunk
.thunk_p
)
1525 struct cgraph_node
*thunk
= e
->caller
;
1528 assemble_thunks_and_aliases (thunk
);
1529 assemble_thunk (thunk
);
1533 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
1535 if (ref
->use
== IPA_REF_ALIAS
)
1537 struct cgraph_node
*alias
= ipa_ref_referring_node (ref
);
1538 bool saved_written
= TREE_ASM_WRITTEN (node
->symbol
.decl
);
1540 /* Force assemble_alias to really output the alias this time instead
1541 of buffering it in same alias pairs. */
1542 TREE_ASM_WRITTEN (node
->symbol
.decl
) = 1;
1543 do_assemble_alias (alias
->symbol
.decl
,
1544 DECL_ASSEMBLER_NAME (node
->symbol
.decl
));
1545 assemble_thunks_and_aliases (alias
);
1546 TREE_ASM_WRITTEN (node
->symbol
.decl
) = saved_written
;
1550 /* Expand function specified by NODE. */
1553 expand_function (struct cgraph_node
*node
)
1555 tree decl
= node
->symbol
.decl
;
1556 location_t saved_loc
;
1558 /* We ought to not compile any inline clones. */
1559 gcc_assert (!node
->global
.inlined_to
);
1561 announce_function (decl
);
1563 gcc_assert (node
->lowered
);
1565 /* Generate RTL for the body of DECL. */
1567 timevar_push (TV_REST_OF_COMPILATION
);
1569 gcc_assert (cgraph_global_info_ready
);
1571 /* Initialize the default bitmap obstack. */
1572 bitmap_obstack_initialize (NULL
);
1574 /* Initialize the RTL code for the function. */
1575 current_function_decl
= decl
;
1576 saved_loc
= input_location
;
1577 input_location
= DECL_SOURCE_LOCATION (decl
);
1578 init_function_start (decl
);
1580 gimple_register_cfg_hooks ();
1582 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1584 execute_all_ipa_transforms ();
1586 /* Perform all tree transforms and optimizations. */
1588 /* Signal the start of passes. */
1589 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1591 execute_pass_list (all_passes
);
1593 /* Signal the end of passes. */
1594 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1596 bitmap_obstack_release (®_obstack
);
1598 /* Release the default bitmap obstack. */
1599 bitmap_obstack_release (NULL
);
1601 /* If requested, warn about function definitions where the function will
1602 return a value (usually of some struct or union type) which itself will
1603 take up a lot of stack space. */
1604 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1606 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1608 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1609 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1610 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1613 unsigned int size_as_int
1614 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1616 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1617 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
1620 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
1621 decl
, larger_than_size
);
1625 gimple_set_body (decl
, NULL
);
1626 if (DECL_STRUCT_FUNCTION (decl
) == 0
1627 && !cgraph_get_node (decl
)->origin
)
1629 /* Stop pointing to the local nodes about to be freed.
1630 But DECL_INITIAL must remain nonzero so we know this
1631 was an actual function definition.
1632 For a nested function, this is done in c_pop_function_context.
1633 If rest_of_compilation set this to 0, leave it 0. */
1634 if (DECL_INITIAL (decl
) != 0)
1635 DECL_INITIAL (decl
) = error_mark_node
;
1638 input_location
= saved_loc
;
1641 timevar_pop (TV_REST_OF_COMPILATION
);
1643 /* Make sure that BE didn't give up on compiling. */
1644 gcc_assert (TREE_ASM_WRITTEN (decl
));
1646 current_function_decl
= NULL
;
1648 /* It would make a lot more sense to output thunks before function body to get more
1649 forward and lest backwarding jumps. This however would need solving problem
1650 with comdats. See PR48668. Also aliases must come after function itself to
1651 make one pass assemblers, like one on AIX, happy. See PR 50689.
1652 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1654 assemble_thunks_and_aliases (node
);
1655 cgraph_release_function_body (node
);
1656 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1657 points to the dead function body. */
1658 cgraph_node_remove_callees (node
);
1662 /* Expand all functions that must be output.
1664 Attempt to topologically sort the nodes so function is output when
1665 all called functions are already assembled to allow data to be
1666 propagated across the callgraph. Use a stack to get smaller distance
1667 between a function and its callees (later we may choose to use a more
1668 sophisticated algorithm for function reordering; we will likely want
1669 to use subsections to make the output functions appear in top-down
1673 expand_all_functions (void)
1675 struct cgraph_node
*node
;
1676 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1677 int order_pos
, new_order_pos
= 0;
1680 order_pos
= ipa_reverse_postorder (order
);
1681 gcc_assert (order_pos
== cgraph_n_nodes
);
1683 /* Garbage collector may remove inline clones we eliminate during
1684 optimization. So we must be sure to not reference them. */
1685 for (i
= 0; i
< order_pos
; i
++)
1686 if (order
[i
]->process
)
1687 order
[new_order_pos
++] = order
[i
];
1689 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1695 expand_function (node
);
1698 cgraph_process_new_functions ();
1704 /* This is used to sort the node types by the cgraph order number. */
1706 enum cgraph_order_sort_kind
1708 ORDER_UNDEFINED
= 0,
1714 struct cgraph_order_sort
1716 enum cgraph_order_sort_kind kind
;
1719 struct cgraph_node
*f
;
1720 struct varpool_node
*v
;
1725 /* Output all functions, variables, and asm statements in the order
1726 according to their order fields, which is the order in which they
1727 appeared in the file. This implements -fno-toplevel-reorder. In
1728 this mode we may output functions and variables which don't really
1729 need to be output. */
1732 output_in_order (void)
1735 struct cgraph_order_sort
*nodes
;
1737 struct cgraph_node
*pf
;
1738 struct varpool_node
*pv
;
1739 struct asm_node
*pa
;
1742 nodes
= XCNEWVEC (struct cgraph_order_sort
, max
);
1744 FOR_EACH_DEFINED_FUNCTION (pf
)
1746 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->symbol
.alias
)
1748 i
= pf
->symbol
.order
;
1749 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1750 nodes
[i
].kind
= ORDER_FUNCTION
;
1755 FOR_EACH_DEFINED_VARIABLE (pv
)
1756 if (!DECL_EXTERNAL (pv
->symbol
.decl
))
1758 i
= pv
->symbol
.order
;
1759 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1760 nodes
[i
].kind
= ORDER_VAR
;
1764 for (pa
= asm_nodes
; pa
; pa
= pa
->next
)
1767 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1768 nodes
[i
].kind
= ORDER_ASM
;
1772 /* In toplevel reorder mode we output all statics; mark them as needed. */
1774 for (i
= 0; i
< max
; ++i
)
1775 if (nodes
[i
].kind
== ORDER_VAR
)
1776 varpool_finalize_named_section_flags (nodes
[i
].u
.v
);
1778 for (i
= 0; i
< max
; ++i
)
1780 switch (nodes
[i
].kind
)
1782 case ORDER_FUNCTION
:
1783 nodes
[i
].u
.f
->process
= 0;
1784 expand_function (nodes
[i
].u
.f
);
1788 varpool_assemble_decl (nodes
[i
].u
.v
);
1792 assemble_asm (nodes
[i
].u
.a
->asm_str
);
1795 case ORDER_UNDEFINED
:
1811 current_function_decl
= NULL
;
1812 gimple_register_cfg_hooks ();
1813 bitmap_obstack_initialize (NULL
);
1815 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
1819 execute_ipa_pass_list (all_small_ipa_passes
);
1824 /* We never run removal of unreachable nodes after early passes. This is
1825 because TODO is run before the subpasses. It is important to remove
1826 the unreachable functions to save works at IPA level and to get LTO
1827 symbol tables right. */
1828 symtab_remove_unreachable_nodes (true, cgraph_dump_file
);
1830 /* If pass_all_early_optimizations was not scheduled, the state of
1831 the cgraph will not be properly updated. Update it now. */
1832 if (cgraph_state
< CGRAPH_STATE_IPA_SSA
)
1833 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
1837 /* Generate coverage variables and constructors. */
1840 /* Process new functions added. */
1842 current_function_decl
= NULL
;
1843 cgraph_process_new_functions ();
1845 execute_ipa_summary_passes
1846 ((struct ipa_opt_pass_d
*) all_regular_ipa_passes
);
1849 /* Some targets need to handle LTO assembler output specially. */
1850 if (flag_generate_lto
)
1851 targetm
.asm_out
.lto_start ();
1853 execute_ipa_summary_passes ((struct ipa_opt_pass_d
*) all_lto_gen_passes
);
1856 ipa_write_summaries ();
1858 if (flag_generate_lto
)
1859 targetm
.asm_out
.lto_end ();
1861 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
1862 execute_ipa_pass_list (all_regular_ipa_passes
);
1863 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
1865 bitmap_obstack_release (NULL
);
1869 /* Return string alias is alias of. */
1872 get_alias_symbol (tree decl
)
1874 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1875 return get_identifier (TREE_STRING_POINTER
1876 (TREE_VALUE (TREE_VALUE (alias
))));
1880 /* Weakrefs may be associated to external decls and thus not output
1881 at expansion time. Emit all necessary aliases. */
1884 output_weakrefs (void)
1887 FOR_EACH_SYMBOL (node
)
1888 if (node
->symbol
.alias
&& DECL_EXTERNAL (node
->symbol
.decl
)
1889 && !TREE_ASM_WRITTEN (node
->symbol
.decl
)
1890 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->symbol
.decl
)))
1894 /* Weakrefs are special by not requiring target definition in current
1895 compilation unit. It is thus bit hard to work out what we want to
1897 When alias target is defined, we need to fetch it from symtab reference,
1898 otherwise it is pointed to by alias_target. */
1899 if (node
->symbol
.alias_target
)
1900 target
= (DECL_P (node
->symbol
.alias_target
)
1901 ? DECL_ASSEMBLER_NAME (node
->symbol
.alias_target
)
1902 : node
->symbol
.alias_target
);
1903 else if (node
->symbol
.analyzed
)
1904 target
= DECL_ASSEMBLER_NAME (symtab_alias_target (node
)->symbol
.decl
);
1908 target
= get_alias_symbol (node
->symbol
.decl
);
1910 do_assemble_alias (node
->symbol
.decl
, target
);
1914 /* Initialize callgraph dump file. */
1919 if (!cgraph_dump_file
)
1920 cgraph_dump_file
= dump_begin (TDI_cgraph
, NULL
);
1924 /* Perform simple optimizations based on callgraph. */
1932 #ifdef ENABLE_CHECKING
1936 timevar_push (TV_CGRAPHOPT
);
1937 if (pre_ipa_mem_report
)
1939 fprintf (stderr
, "Memory consumption before IPA\n");
1940 dump_memory_report (false);
1943 fprintf (stderr
, "Performing interprocedural optimizations\n");
1944 cgraph_state
= CGRAPH_STATE_IPA
;
1946 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1948 lto_streamer_hooks_init ();
1950 /* Don't run the IPA passes if there was any error or sorry messages. */
1954 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
1956 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
1958 timevar_pop (TV_CGRAPHOPT
);
1962 /* This pass remove bodies of extern inline functions we never inlined.
1963 Do this later so other IPA passes see what is really going on. */
1964 symtab_remove_unreachable_nodes (false, dump_file
);
1965 cgraph_global_info_ready
= true;
1966 if (cgraph_dump_file
)
1968 fprintf (cgraph_dump_file
, "Optimized ");
1969 dump_symtab (cgraph_dump_file
);
1971 if (post_ipa_mem_report
)
1973 fprintf (stderr
, "Memory consumption after IPA\n");
1974 dump_memory_report (false);
1976 timevar_pop (TV_CGRAPHOPT
);
1978 /* Output everything. */
1979 (*debug_hooks
->assembly_start
) ();
1981 fprintf (stderr
, "Assembling functions:\n");
1982 #ifdef ENABLE_CHECKING
1986 cgraph_materialize_all_clones ();
1987 bitmap_obstack_initialize (NULL
);
1988 execute_ipa_pass_list (all_late_ipa_passes
);
1989 symtab_remove_unreachable_nodes (true, dump_file
);
1990 #ifdef ENABLE_CHECKING
1993 bitmap_obstack_release (NULL
);
1994 mark_functions_to_output ();
1996 /* When weakref support is missing, we autmatically translate all
1997 references to NODE to references to its ultimate alias target.
1998 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2001 Set up this mapping before we output any assembler but once we are sure
2002 that all symbol renaming is done.
2004 FIXME: All this uglyness can go away if we just do renaming at gimple
2005 level by physically rewritting the IL. At the moment we can only redirect
2006 calls, so we need infrastructure for renaming references as well. */
2007 #ifndef ASM_OUTPUT_WEAKREF
2010 FOR_EACH_SYMBOL (node
)
2011 if (node
->symbol
.alias
2012 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->symbol
.decl
)))
2014 IDENTIFIER_TRANSPARENT_ALIAS
2015 (DECL_ASSEMBLER_NAME (node
->symbol
.decl
)) = 1;
2016 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->symbol
.decl
))
2017 = (node
->symbol
.alias_target
? node
->symbol
.alias_target
2018 : DECL_ASSEMBLER_NAME (symtab_alias_target (node
)->symbol
.decl
));
2022 cgraph_state
= CGRAPH_STATE_EXPANSION
;
2023 if (!flag_toplevel_reorder
)
2027 output_asm_statements ();
2029 expand_all_functions ();
2030 varpool_output_variables ();
2033 cgraph_process_new_functions ();
2034 cgraph_state
= CGRAPH_STATE_FINISHED
;
2037 if (cgraph_dump_file
)
2039 fprintf (cgraph_dump_file
, "\nFinal ");
2040 dump_symtab (cgraph_dump_file
);
2042 #ifdef ENABLE_CHECKING
2044 /* Double check that all inline clones are gone and that all
2045 function bodies have been released from memory. */
2048 struct cgraph_node
*node
;
2049 bool error_found
= false;
2051 FOR_EACH_DEFINED_FUNCTION (node
)
2052 if (node
->global
.inlined_to
2053 || gimple_has_body_p (node
->symbol
.decl
))
2056 dump_cgraph_node (stderr
, node
);
2059 internal_error ("nodes with unreleased memory found");
2065 /* Analyze the whole compilation unit once it is parsed completely. */
2068 finalize_compilation_unit (void)
2070 timevar_push (TV_CGRAPH
);
2072 /* If we're here there's no current function anymore. Some frontends
2073 are lazy in clearing these. */
2074 current_function_decl
= NULL
;
2077 /* Do not skip analyzing the functions if there were errors, we
2078 miss diagnostics for following functions otherwise. */
2080 /* Emit size functions we didn't inline. */
2081 finalize_size_functions ();
2083 /* Mark alias targets necessary and emit diagnostics. */
2084 handle_alias_pairs ();
2088 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2092 if (flag_dump_passes
)
2095 /* Gimplify and lower all functions, compute reachability and
2096 remove unreachable nodes. */
2097 analyze_functions ();
2099 /* Mark alias targets necessary and emit diagnostics. */
2100 handle_alias_pairs ();
2102 /* Gimplify and lower thunks. */
2103 analyze_functions ();
2105 /* Finally drive the pass manager. */
2108 timevar_pop (TV_CGRAPH
);
2112 #include "gt-cgraphunit.h"