1 /* Driver of optimization process
2 Copyright (C) 2003-2015 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:
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 "fold-const.h"
169 #include "stor-layout.h"
170 #include "stringpool.h"
174 #include "hard-reg-set.h"
175 #include "function.h"
176 #include "basic-block.h"
177 #include "dominance.h"
178 #include "cfgcleanup.h"
180 #include "tree-ssa-alias.h"
181 #include "internal-fn.h"
182 #include "gimple-fold.h"
183 #include "gimple-expr.h"
185 #include "gimplify.h"
186 #include "gimple-iterator.h"
187 #include "gimplify-me.h"
188 #include "gimple-ssa.h"
189 #include "tree-cfg.h"
190 #include "tree-into-ssa.h"
191 #include "tree-ssa.h"
192 #include "tree-inline.h"
193 #include "langhooks.h"
198 #include "diagnostic.h"
202 #include "alloc-pool.h"
203 #include "symbol-summary.h"
204 #include "ipa-prop.h"
205 #include "tree-iterator.h"
206 #include "tree-pass.h"
207 #include "tree-dump.h"
208 #include "gimple-pretty-print.h"
210 #include "coverage.h"
212 #include "ipa-inline.h"
213 #include "ipa-utils.h"
214 #include "lto-streamer.h"
217 #include "regset.h" /* FIXME: For reg_obstack. */
219 #include "pass_manager.h"
220 #include "tree-nested.h"
221 #include "gimplify.h"
223 #include "tree-chkp.h"
224 #include "lto-section-names.h"
226 #include "print-tree.h"
228 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
229 secondary queue used during optimization to accommodate passes that
230 may generate new functions that need to be optimized and expanded. */
231 vec
<cgraph_node
*> cgraph_new_nodes
;
233 static void expand_all_functions (void);
234 static void mark_functions_to_output (void);
235 static void handle_alias_pairs (void);
237 /* Used for vtable lookup in thunk adjusting. */
238 static GTY (()) tree vtable_entry_type
;
240 /* Determine if symbol declaration is needed. That is, visible to something
241 either outside this translation unit, something magic in the system
244 symtab_node::needed_p (void)
246 /* Double check that no one output the function into assembly file
248 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
249 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
254 if (DECL_EXTERNAL (decl
))
257 /* If the user told us it is used, then it must be so. */
261 /* ABI forced symbols are needed when they are external. */
262 if (forced_by_abi
&& TREE_PUBLIC (decl
))
265 /* Keep constructors, destructors and virtual functions. */
266 if (TREE_CODE (decl
) == FUNCTION_DECL
267 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
270 /* Externally visible variables must be output. The exception is
271 COMDAT variables that must be output only when they are needed. */
272 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
278 /* Head and terminator of the queue of nodes to be processed while building
281 static symtab_node symtab_terminator
;
282 static symtab_node
*queued_nodes
= &symtab_terminator
;
284 /* Add NODE to queue starting at QUEUED_NODES.
285 The queue is linked via AUX pointers and terminated by pointer to 1. */
288 enqueue_node (symtab_node
*node
)
292 gcc_checking_assert (queued_nodes
);
293 node
->aux
= queued_nodes
;
297 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
298 functions into callgraph in a way so they look like ordinary reachable
299 functions inserted into callgraph already at construction time. */
302 symbol_table::process_new_functions (void)
306 if (!cgraph_new_nodes
.exists ())
309 handle_alias_pairs ();
310 /* Note that this queue may grow as its being processed, as the new
311 functions may generate new ones. */
312 for (unsigned i
= 0; i
< cgraph_new_nodes
.length (); i
++)
314 cgraph_node
*node
= cgraph_new_nodes
[i
];
319 /* At construction time we just need to finalize function and move
320 it into reachable functions list. */
322 cgraph_node::finalize_function (fndecl
, false);
323 call_cgraph_insertion_hooks (node
);
329 case IPA_SSA_AFTER_INLINING
:
330 /* When IPA optimization already started, do all essential
331 transformations that has been already performed on the whole
332 cgraph but not on this function. */
334 gimple_register_cfg_hooks ();
337 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
338 if ((state
== IPA_SSA
|| state
== IPA_SSA_AFTER_INLINING
)
339 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
340 g
->get_passes ()->execute_early_local_passes ();
341 else if (inline_summaries
!= NULL
)
342 compute_inline_parameters (node
, true);
343 free_dominance_info (CDI_POST_DOMINATORS
);
344 free_dominance_info (CDI_DOMINATORS
);
346 call_cgraph_insertion_hooks (node
);
350 /* Functions created during expansion shall be compiled
353 call_cgraph_insertion_hooks (node
);
363 cgraph_new_nodes
.release ();
366 /* As an GCC extension we allow redefinition of the function. The
367 semantics when both copies of bodies differ is not well defined.
368 We replace the old body with new body so in unit at a time mode
369 we always use new body, while in normal mode we may end up with
370 old body inlined into some functions and new body expanded and
373 ??? It may make more sense to use one body for inlining and other
374 body for expanding the function but this is difficult to do. */
377 cgraph_node::reset (void)
379 /* If process is set, then we have already begun whole-unit analysis.
380 This is *not* testing for whether we've already emitted the function.
381 That case can be sort-of legitimately seen with real function redefinition
382 errors. I would argue that the front end should never present us with
383 such a case, but don't enforce that for now. */
384 gcc_assert (!process
);
386 /* Reset our data structures so we can analyze the function again. */
387 memset (&local
, 0, sizeof (local
));
388 memset (&global
, 0, sizeof (global
));
389 memset (&rtl
, 0, sizeof (rtl
));
394 cpp_implicit_alias
= false;
397 remove_all_references ();
400 /* Return true when there are references to the node. INCLUDE_SELF is
401 true if a self reference counts as a reference. */
404 symtab_node::referred_to_p (bool include_self
)
408 /* See if there are any references at all. */
409 if (iterate_referring (0, ref
))
411 /* For functions check also calls. */
412 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
413 if (cn
&& cn
->callers
)
417 for (cgraph_edge
*e
= cn
->callers
; e
; e
= e
->next_caller
)
418 if (e
->caller
!= this)
424 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
425 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
426 the garbage collector run at the moment. We would need to either create
427 a new GC context, or just not compile right now. */
430 cgraph_node::finalize_function (tree decl
, bool no_collect
)
432 cgraph_node
*node
= cgraph_node::get_create (decl
);
434 if (node
->definition
)
436 /* Nested functions should only be defined once. */
437 gcc_assert (!DECL_CONTEXT (decl
)
438 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
440 node
->local
.redefined_extern_inline
= true;
443 /* Set definition first before calling notice_global_symbol so that
444 it is available to notice_global_symbol. */
445 node
->definition
= true;
446 notice_global_symbol (decl
);
447 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
449 /* With -fkeep-inline-functions we are keeping all inline functions except
450 for extern inline ones. */
451 if (flag_keep_inline_functions
452 && DECL_DECLARED_INLINE_P (decl
)
453 && !DECL_EXTERNAL (decl
)
454 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
455 node
->force_output
= 1;
457 /* When not optimizing, also output the static functions. (see
458 PR24561), but don't do so for always_inline functions, functions
459 declared inline and nested functions. These were optimized out
460 in the original implementation and it is unclear whether we want
461 to change the behavior here. */
462 if ((!opt_for_fn (decl
, optimize
)
463 && !node
->cpp_implicit_alias
464 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
465 && !DECL_DECLARED_INLINE_P (decl
)
466 && !(DECL_CONTEXT (decl
)
467 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
468 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
469 node
->force_output
= 1;
471 /* If we've not yet emitted decl, tell the debug info about it. */
472 if (!TREE_ASM_WRITTEN (decl
))
473 (*debug_hooks
->deferred_inline_function
) (decl
);
475 /* Possibly warn about unused parameters. */
476 if (warn_unused_parameter
)
477 do_warn_unused_parameter (decl
);
482 if (symtab
->state
== CONSTRUCTION
483 && (node
->needed_p () || node
->referred_to_p ()))
487 /* Add the function FNDECL to the call graph.
488 Unlike finalize_function, this function is intended to be used
489 by middle end and allows insertion of new function at arbitrary point
490 of compilation. The function can be either in high, low or SSA form
493 The function is assumed to be reachable and have address taken (so no
494 API breaking optimizations are performed on it).
496 Main work done by this function is to enqueue the function for later
497 processing to avoid need the passes to be re-entrant. */
500 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
502 gcc::pass_manager
*passes
= g
->get_passes ();
507 struct function
*fn
= DECL_STRUCT_FUNCTION (fndecl
);
508 const char *function_type
= ((gimple_has_body_p (fndecl
))
510 ? (gimple_in_ssa_p (fn
)
514 : "to-be-gimplified");
516 "Added new %s function %s to callgraph\n",
518 fndecl_name (fndecl
));
521 switch (symtab
->state
)
524 cgraph_node::finalize_function (fndecl
, false);
527 /* Just enqueue function to be processed at nearest occurrence. */
528 node
= cgraph_node::get_create (fndecl
);
530 node
->lowered
= true;
531 cgraph_new_nodes
.safe_push (node
);
536 case IPA_SSA_AFTER_INLINING
:
538 /* Bring the function into finalized state and enqueue for later
539 analyzing and compilation. */
540 node
= cgraph_node::get_create (fndecl
);
541 node
->local
.local
= false;
542 node
->definition
= true;
543 node
->force_output
= true;
544 if (!lowered
&& symtab
->state
== EXPANSION
)
546 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
547 gimple_register_cfg_hooks ();
548 bitmap_obstack_initialize (NULL
);
549 execute_pass_list (cfun
, passes
->all_lowering_passes
);
550 passes
->execute_early_local_passes ();
551 bitmap_obstack_release (NULL
);
557 node
->lowered
= true;
558 cgraph_new_nodes
.safe_push (node
);
562 /* At the very end of compilation we have to do all the work up
564 node
= cgraph_node::create (fndecl
);
566 node
->lowered
= true;
567 node
->definition
= true;
569 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
570 gimple_register_cfg_hooks ();
571 bitmap_obstack_initialize (NULL
);
572 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
573 g
->get_passes ()->execute_early_local_passes ();
574 bitmap_obstack_release (NULL
);
583 /* Set a personality if required and we already passed EH lowering. */
585 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
586 == eh_personality_lang
))
587 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
590 /* Analyze the function scheduled to be output. */
592 cgraph_node::analyze (void)
594 tree decl
= this->decl
;
595 location_t saved_loc
= input_location
;
596 input_location
= DECL_SOURCE_LOCATION (decl
);
600 cgraph_node
*t
= cgraph_node::get (thunk
.alias
);
602 create_edge (t
, NULL
, 0, CGRAPH_FREQ_BASE
);
603 /* Target code in expand_thunk may need the thunk's target
604 to be analyzed, so recurse here. */
609 t
= t
->get_alias_target ();
613 if (!expand_thunk (false, false))
621 resolve_alias (cgraph_node::get (alias_target
));
622 else if (dispatcher_function
)
624 /* Generate the dispatcher body of multi-versioned functions. */
625 cgraph_function_version_info
*dispatcher_version_info
626 = function_version ();
627 if (dispatcher_version_info
!= NULL
628 && (dispatcher_version_info
->dispatcher_resolver
631 tree resolver
= NULL_TREE
;
632 gcc_assert (targetm
.generate_version_dispatcher_body
);
633 resolver
= targetm
.generate_version_dispatcher_body (this);
634 gcc_assert (resolver
!= NULL_TREE
);
639 push_cfun (DECL_STRUCT_FUNCTION (decl
));
641 assign_assembler_name_if_neeeded (decl
);
643 /* Make sure to gimplify bodies only once. During analyzing a
644 function we lower it, which will require gimplified nested
645 functions, so we can end up here with an already gimplified
647 if (!gimple_has_body_p (decl
))
648 gimplify_function_tree (decl
);
650 /* Lower the function. */
654 lower_nested_functions (decl
);
655 gcc_assert (!nested
);
657 gimple_register_cfg_hooks ();
658 bitmap_obstack_initialize (NULL
);
659 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
660 free_dominance_info (CDI_POST_DOMINATORS
);
661 free_dominance_info (CDI_DOMINATORS
);
663 bitmap_obstack_release (NULL
);
671 input_location
= saved_loc
;
674 /* C++ frontend produce same body aliases all over the place, even before PCH
675 gets streamed out. It relies on us linking the aliases with their function
676 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
677 first produce aliases without links, but once C++ FE is sure he won't sream
678 PCH we build the links via this function. */
681 symbol_table::process_same_body_aliases (void)
684 FOR_EACH_SYMBOL (node
)
685 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
687 (TREE_CODE (node
->alias_target
) == VAR_DECL
688 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
689 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
690 cpp_implicit_aliases_done
= true;
693 /* Process attributes common for vars and functions. */
696 process_common_attributes (symtab_node
*node
, tree decl
)
698 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
700 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
702 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
703 "%<weakref%> attribute should be accompanied with"
704 " an %<alias%> attribute");
705 DECL_WEAK (decl
) = 0;
706 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
707 DECL_ATTRIBUTES (decl
));
710 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
711 node
->no_reorder
= 1;
714 /* Look for externally_visible and used attributes and mark cgraph nodes
717 We cannot mark the nodes at the point the attributes are processed (in
718 handle_*_attribute) because the copy of the declarations available at that
719 point may not be canonical. For example, in:
722 void f() __attribute__((used));
724 the declaration we see in handle_used_attribute will be the second
725 declaration -- but the front end will subsequently merge that declaration
726 with the original declaration and discard the second declaration.
728 Furthermore, we can't mark these nodes in finalize_function because:
731 void f() __attribute__((externally_visible));
735 So, we walk the nodes at the end of the translation unit, applying the
736 attributes at that point. */
739 process_function_and_variable_attributes (cgraph_node
*first
,
740 varpool_node
*first_var
)
745 for (node
= symtab
->first_function (); node
!= first
;
746 node
= symtab
->next_function (node
))
748 tree decl
= node
->decl
;
749 if (DECL_PRESERVE_P (decl
))
750 node
->mark_force_output ();
751 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
753 if (! TREE_PUBLIC (node
->decl
))
754 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
755 "%<externally_visible%>"
756 " attribute have effect only on public objects");
758 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
759 && (node
->definition
&& !node
->alias
))
761 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
762 "%<weakref%> attribute ignored"
763 " because function is defined");
764 DECL_WEAK (decl
) = 0;
765 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
766 DECL_ATTRIBUTES (decl
));
769 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
770 && !DECL_DECLARED_INLINE_P (decl
)
771 /* redefining extern inline function makes it DECL_UNINLINABLE. */
772 && !DECL_UNINLINABLE (decl
))
773 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
774 "always_inline function might not be inlinable");
776 process_common_attributes (node
, decl
);
778 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
779 vnode
= symtab
->next_variable (vnode
))
781 tree decl
= vnode
->decl
;
782 if (DECL_EXTERNAL (decl
)
783 && DECL_INITIAL (decl
))
784 varpool_node::finalize_decl (decl
);
785 if (DECL_PRESERVE_P (decl
))
786 vnode
->force_output
= true;
787 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
789 if (! TREE_PUBLIC (vnode
->decl
))
790 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
791 "%<externally_visible%>"
792 " attribute have effect only on public objects");
794 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
796 && DECL_INITIAL (decl
))
798 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
799 "%<weakref%> attribute ignored"
800 " because variable is initialized");
801 DECL_WEAK (decl
) = 0;
802 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
803 DECL_ATTRIBUTES (decl
));
805 process_common_attributes (vnode
, decl
);
809 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
810 middle end to output the variable to asm file, if needed or externally
814 varpool_node::finalize_decl (tree decl
)
816 varpool_node
*node
= varpool_node::get_create (decl
);
818 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
820 if (node
->definition
)
822 /* Set definition first before calling notice_global_symbol so that
823 it is available to notice_global_symbol. */
824 node
->definition
= true;
825 notice_global_symbol (decl
);
826 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
827 /* Traditionally we do not eliminate static variables when not
828 optimizing and when not doing toplevel reoder. */
830 || ((!flag_toplevel_reorder
831 && !DECL_COMDAT (node
->decl
)
832 && !DECL_ARTIFICIAL (node
->decl
))))
833 node
->force_output
= true;
835 if (symtab
->state
== CONSTRUCTION
836 && (node
->needed_p () || node
->referred_to_p ()))
838 if (symtab
->state
>= IPA_SSA
)
840 /* Some frontends produce various interface variables after compilation
842 if (symtab
->state
== FINISHED
843 || (!flag_toplevel_reorder
844 && symtab
->state
== EXPANSION
))
845 node
->assemble_decl ();
847 if (DECL_INITIAL (decl
))
848 chkp_register_var_initializer (decl
);
851 /* EDGE is an polymorphic call. Mark all possible targets as reachable
852 and if there is only one target, perform trivial devirtualization.
853 REACHABLE_CALL_TARGETS collects target lists we already walked to
854 avoid udplicate work. */
857 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
863 vec
<cgraph_node
*>targets
864 = possible_polymorphic_call_targets
865 (edge
, &final
, &cache_token
);
867 if (!reachable_call_targets
->add (cache_token
))
869 if (symtab
->dump_file
)
870 dump_possible_polymorphic_call_targets
871 (symtab
->dump_file
, edge
);
873 for (i
= 0; i
< targets
.length (); i
++)
875 /* Do not bother to mark virtual methods in anonymous namespace;
876 either we will find use of virtual table defining it, or it is
878 if (targets
[i
]->definition
880 (TREE_TYPE (targets
[i
]->decl
))
882 && !type_in_anonymous_namespace_p
883 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets
[i
]->decl
))))
884 enqueue_node (targets
[i
]);
888 /* Very trivial devirtualization; when the type is
889 final or anonymous (so we know all its derivation)
890 and there is only one possible virtual call target,
891 make the edge direct. */
894 if (targets
.length () <= 1 && dbg_cnt (devirt
))
897 if (targets
.length () == 1)
900 target
= cgraph_node::create
901 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
903 if (symtab
->dump_file
)
905 fprintf (symtab
->dump_file
,
906 "Devirtualizing call: ");
907 print_gimple_stmt (symtab
->dump_file
,
911 if (dump_enabled_p ())
913 location_t locus
= gimple_location_safe (edge
->call_stmt
);
914 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
915 "devirtualizing call in %s to %s\n",
916 edge
->caller
->name (), target
->name ());
919 edge
->make_direct (target
);
920 edge
->redirect_call_stmt_to_callee ();
922 /* Call to __builtin_unreachable shouldn't be instrumented. */
923 if (!targets
.length ())
924 gimple_call_set_with_bounds (edge
->call_stmt
, false);
926 if (symtab
->dump_file
)
928 fprintf (symtab
->dump_file
,
929 "Devirtualized as: ");
930 print_gimple_stmt (symtab
->dump_file
,
939 /* Discover all functions and variables that are trivially needed, analyze
940 them as well as all functions and variables referred by them */
941 static cgraph_node
*first_analyzed
;
942 static varpool_node
*first_analyzed_var
;
944 /* FIRST_TIME is set to TRUE for the first time we are called for a
945 translation unit from finalize_compilation_unit() or false
949 analyze_functions (bool first_time
)
951 /* Keep track of already processed nodes when called multiple times for
952 intermodule optimization. */
953 cgraph_node
*first_handled
= first_analyzed
;
954 varpool_node
*first_handled_var
= first_analyzed_var
;
955 hash_set
<void *> reachable_call_targets
;
962 location_t saved_loc
= input_location
;
964 bitmap_obstack_initialize (NULL
);
965 symtab
->state
= CONSTRUCTION
;
966 input_location
= UNKNOWN_LOCATION
;
968 /* Ugly, but the fixup can not happen at a time same body alias is created;
969 C++ FE is confused about the COMDAT groups being right. */
970 if (symtab
->cpp_implicit_aliases_done
)
971 FOR_EACH_SYMBOL (node
)
972 if (node
->cpp_implicit_alias
)
973 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
974 build_type_inheritance_graph ();
976 /* Analysis adds static variables that in turn adds references to new functions.
977 So we need to iterate the process until it stabilize. */
981 process_function_and_variable_attributes (first_analyzed
,
984 /* First identify the trivially needed symbols. */
985 for (node
= symtab
->first_symbol ();
986 node
!= first_analyzed
987 && node
!= first_analyzed_var
; node
= node
->next
)
989 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
990 node
->get_comdat_group_id ();
991 if (node
->needed_p ())
994 if (!changed
&& symtab
->dump_file
)
995 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
997 if (symtab
->dump_file
)
998 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
999 if (!changed
&& symtab
->dump_file
)
1000 fprintf (symtab
->dump_file
, "\n");
1002 if (node
== first_analyzed
1003 || node
== first_analyzed_var
)
1006 symtab
->process_new_functions ();
1007 first_analyzed_var
= symtab
->first_variable ();
1008 first_analyzed
= symtab
->first_function ();
1010 if (changed
&& symtab
->dump_file
)
1011 fprintf (symtab
->dump_file
, "\n");
1013 /* Lower representation, build callgraph edges and references for all trivially
1014 needed symbols and all symbols referred by them. */
1015 while (queued_nodes
!= &symtab_terminator
)
1018 node
= queued_nodes
;
1019 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
1020 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1021 if (cnode
&& cnode
->definition
)
1024 tree decl
= cnode
->decl
;
1026 /* ??? It is possible to create extern inline function
1027 and later using weak alias attribute to kill its body.
1028 See gcc.c-torture/compile/20011119-1.c */
1029 if (!DECL_STRUCT_FUNCTION (decl
)
1031 && !cnode
->thunk
.thunk_p
1032 && !cnode
->dispatcher_function
)
1035 cnode
->local
.redefined_extern_inline
= true;
1039 if (!cnode
->analyzed
)
1042 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
1043 if (edge
->callee
->definition
1044 && (!DECL_EXTERNAL (edge
->callee
->decl
)
1045 /* When not optimizing, do not try to analyze extern
1046 inline functions. Doing so is pointless. */
1047 || opt_for_fn (edge
->callee
->decl
, optimize
)
1048 /* Weakrefs needs to be preserved. */
1049 || edge
->callee
->alias
1050 /* always_inline functions are inlined aven at -O0. */
1053 DECL_ATTRIBUTES (edge
->callee
->decl
))
1054 /* Multiversioned functions needs the dispatcher to
1055 be produced locally even for extern functions. */
1056 || edge
->callee
->function_version ()))
1057 enqueue_node (edge
->callee
);
1058 if (opt_for_fn (cnode
->decl
, optimize
)
1059 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
1063 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1065 next
= edge
->next_callee
;
1066 if (edge
->indirect_info
->polymorphic
)
1067 walk_polymorphic_call_targets (&reachable_call_targets
,
1072 /* If decl is a clone of an abstract function,
1073 mark that abstract function so that we don't release its body.
1074 The DECL_INITIAL() of that abstract function declaration
1075 will be later needed to output debug info. */
1076 if (DECL_ABSTRACT_ORIGIN (decl
))
1078 cgraph_node
*origin_node
1079 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1080 origin_node
->used_as_abstract_origin
= true;
1085 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1086 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1090 if (node
->same_comdat_group
)
1093 for (next
= node
->same_comdat_group
;
1095 next
= next
->same_comdat_group
)
1096 if (!next
->comdat_local_p ())
1097 enqueue_node (next
);
1099 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1100 if (ref
->referred
->definition
1101 && (!DECL_EXTERNAL (ref
->referred
->decl
)
1102 || ((TREE_CODE (ref
->referred
->decl
) != FUNCTION_DECL
1104 || (TREE_CODE (ref
->referred
->decl
) == FUNCTION_DECL
1105 && opt_for_fn (ref
->referred
->decl
, optimize
))
1107 || ref
->referred
->alias
)))
1108 enqueue_node (ref
->referred
);
1109 symtab
->process_new_functions ();
1112 update_type_inheritance_graph ();
1114 /* Collect entry points to the unit. */
1115 if (symtab
->dump_file
)
1117 fprintf (symtab
->dump_file
, "\n\nInitial ");
1118 symtab_node::dump_table (symtab
->dump_file
);
1124 FOR_EACH_SYMBOL (snode
)
1125 check_global_declaration (snode
->decl
);
1128 if (symtab
->dump_file
)
1129 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1131 for (node
= symtab
->first_symbol ();
1132 node
!= first_handled
1133 && node
!= first_handled_var
; node
= next
)
1136 if (!node
->aux
&& !node
->referred_to_p ())
1138 if (symtab
->dump_file
)
1139 fprintf (symtab
->dump_file
, " %s", node
->name ());
1141 /* See if the debugger can use anything before the DECL
1142 passes away. Perhaps it can notice a DECL that is now a
1143 constant and can tag the early DIE with an appropriate
1146 Otherwise, this is the last chance the debug_hooks have
1147 at looking at optimized away DECLs, since
1148 late_global_decl will subsequently be called from the
1149 contents of the now pruned symbol table. */
1150 if (!decl_function_context (node
->decl
))
1151 (*debug_hooks
->late_global_decl
) (node
->decl
);
1156 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1158 tree decl
= node
->decl
;
1160 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1162 && !cnode
->thunk
.thunk_p
)
1165 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1167 || gimple_has_body_p (decl
));
1168 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1172 for (;node
; node
= node
->next
)
1174 first_analyzed
= symtab
->first_function ();
1175 first_analyzed_var
= symtab
->first_variable ();
1176 if (symtab
->dump_file
)
1178 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1179 symtab_node::dump_table (symtab
->dump_file
);
1181 bitmap_obstack_release (NULL
);
1183 /* Initialize assembler name hash, in particular we want to trigger C++
1184 mangling and same body alias creation before we free DECL_ARGUMENTS
1187 symtab
->symtab_initialize_asm_name_hash ();
1189 input_location
= saved_loc
;
1192 /* Translate the ugly representation of aliases as alias pairs into nice
1193 representation in callgraph. We don't handle all cases yet,
1197 handle_alias_pairs (void)
1202 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1204 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1206 /* Weakrefs with target not defined in current unit are easy to handle:
1207 they behave just as external variables except we need to note the
1208 alias flag to later output the weakref pseudo op into asm file. */
1210 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1212 symtab_node
*node
= symtab_node::get (p
->decl
);
1215 node
->alias_target
= p
->target
;
1216 node
->weakref
= true;
1219 alias_pairs
->unordered_remove (i
);
1222 else if (!target_node
)
1224 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1225 symtab_node
*node
= symtab_node::get (p
->decl
);
1227 node
->alias
= false;
1228 alias_pairs
->unordered_remove (i
);
1232 if (DECL_EXTERNAL (target_node
->decl
)
1233 /* We use local aliases for C++ thunks to force the tailcall
1234 to bind locally. This is a hack - to keep it working do
1235 the following (which is not strictly correct). */
1236 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1237 || ! DECL_VIRTUAL_P (target_node
->decl
))
1238 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1240 error ("%q+D aliased to external symbol %qE",
1241 p
->decl
, p
->target
);
1244 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1245 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1247 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1248 if (src_node
&& src_node
->definition
)
1250 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1251 alias_pairs
->unordered_remove (i
);
1253 else if (TREE_CODE (p
->decl
) == VAR_DECL
1254 && target_node
&& is_a
<varpool_node
*> (target_node
))
1256 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1257 alias_pairs
->unordered_remove (i
);
1261 error ("%q+D alias in between function and variable is not supported",
1263 warning (0, "%q+D aliased declaration",
1265 alias_pairs
->unordered_remove (i
);
1268 vec_free (alias_pairs
);
1272 /* Figure out what functions we want to assemble. */
1275 mark_functions_to_output (void)
1278 #ifdef ENABLE_CHECKING
1279 bool check_same_comdat_groups
= false;
1281 FOR_EACH_FUNCTION (node
)
1282 gcc_assert (!node
->process
);
1285 FOR_EACH_FUNCTION (node
)
1287 tree decl
= node
->decl
;
1289 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1293 /* We need to output all local functions that are used and not
1294 always inlined, as well as those that are reachable from
1295 outside the current compilation unit. */
1297 && !node
->thunk
.thunk_p
1299 && !node
->global
.inlined_to
1300 && !TREE_ASM_WRITTEN (decl
)
1301 && !DECL_EXTERNAL (decl
))
1304 if (node
->same_comdat_group
)
1307 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1309 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1310 if (!next
->thunk
.thunk_p
&& !next
->alias
1311 && !next
->comdat_local_p ())
1315 else if (node
->same_comdat_group
)
1317 #ifdef ENABLE_CHECKING
1318 check_same_comdat_groups
= true;
1323 /* We should've reclaimed all functions that are not needed. */
1324 #ifdef ENABLE_CHECKING
1325 if (!node
->global
.inlined_to
1326 && gimple_has_body_p (decl
)
1327 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1328 are inside partition, we can end up not removing the body since we no longer
1329 have analyzed node pointing to it. */
1330 && !node
->in_other_partition
1333 && !DECL_EXTERNAL (decl
))
1336 internal_error ("failed to reclaim unneeded function");
1339 gcc_assert (node
->global
.inlined_to
1340 || !gimple_has_body_p (decl
)
1341 || node
->in_other_partition
1343 || DECL_ARTIFICIAL (decl
)
1344 || DECL_EXTERNAL (decl
));
1349 #ifdef ENABLE_CHECKING
1350 if (check_same_comdat_groups
)
1351 FOR_EACH_FUNCTION (node
)
1352 if (node
->same_comdat_group
&& !node
->process
)
1354 tree decl
= node
->decl
;
1355 if (!node
->global
.inlined_to
1356 && gimple_has_body_p (decl
)
1357 /* FIXME: in an ltrans unit when the offline copy is outside a
1358 partition but inline copies are inside a partition, we can
1359 end up not removing the body since we no longer have an
1360 analyzed node pointing to it. */
1361 && !node
->in_other_partition
1363 && !DECL_EXTERNAL (decl
))
1366 internal_error ("failed to reclaim unneeded function in same "
1373 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1374 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1376 Set current_function_decl and cfun to newly constructed empty function body.
1377 return basic block in the function body. */
1380 init_lowered_empty_function (tree decl
, bool in_ssa
, gcov_type count
)
1385 current_function_decl
= decl
;
1386 allocate_struct_function (decl
, false);
1387 gimple_register_cfg_hooks ();
1388 init_empty_tree_cfg ();
1392 init_tree_ssa (cfun
);
1393 init_ssa_operands (cfun
);
1394 cfun
->gimple_df
->in_ssa_p
= true;
1395 cfun
->curr_properties
|= PROP_ssa
;
1398 DECL_INITIAL (decl
) = make_node (BLOCK
);
1400 DECL_SAVED_TREE (decl
) = error_mark_node
;
1401 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1402 | PROP_cfg
| PROP_loops
);
1404 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1405 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1406 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1408 /* Create BB for body of the function and connect it properly. */
1409 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1410 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1411 EXIT_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1412 EXIT_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1413 bb
= create_basic_block (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1415 bb
->frequency
= BB_FREQ_MAX
;
1416 e
= make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1418 e
->probability
= REG_BR_PROB_BASE
;
1419 e
= make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1421 e
->probability
= REG_BR_PROB_BASE
;
1422 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1427 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1428 offset indicated by VIRTUAL_OFFSET, if that is
1429 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1430 zero for a result adjusting thunk. */
1433 thunk_adjust (gimple_stmt_iterator
* bsi
,
1434 tree ptr
, bool this_adjusting
,
1435 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1441 && fixed_offset
!= 0)
1443 stmt
= gimple_build_assign
1444 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1447 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1450 /* If there's a virtual offset, look up that value in the vtable and
1451 adjust the pointer again. */
1458 if (!vtable_entry_type
)
1460 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1461 TREE_TYPE (vfunc_type
) = integer_type_node
;
1462 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1463 layout_type (vfunc_type
);
1465 vtable_entry_type
= build_pointer_type (vfunc_type
);
1469 create_tmp_reg (build_pointer_type
1470 (build_pointer_type (vtable_entry_type
)), "vptr");
1472 /* The vptr is always at offset zero in the object. */
1473 stmt
= gimple_build_assign (vtabletmp
,
1474 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1476 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1478 /* Form the vtable address. */
1479 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1481 stmt
= gimple_build_assign (vtabletmp2
,
1482 build_simple_mem_ref (vtabletmp
));
1483 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1485 /* Find the entry with the vcall offset. */
1486 stmt
= gimple_build_assign (vtabletmp2
,
1487 fold_build_pointer_plus_loc (input_location
,
1490 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1492 /* Get the offset itself. */
1493 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1495 stmt
= gimple_build_assign (vtabletmp3
,
1496 build_simple_mem_ref (vtabletmp2
));
1497 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1499 /* Adjust the `this' pointer. */
1500 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1501 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1502 GSI_CONTINUE_LINKING
);
1506 && fixed_offset
!= 0)
1507 /* Adjust the pointer by the constant. */
1511 if (TREE_CODE (ptr
) == VAR_DECL
)
1515 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1516 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1517 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1519 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1520 ptrtmp
, fixed_offset
);
1523 /* Emit the statement and gimplify the adjustment expression. */
1524 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1525 stmt
= gimple_build_assign (ret
, ptr
);
1526 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1531 /* Expand thunk NODE to gimple if possible.
1532 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1533 no assembler is produced.
1534 When OUTPUT_ASM_THUNK is true, also produce assembler for
1535 thunks that are not lowered. */
1538 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1540 bool this_adjusting
= thunk
.this_adjusting
;
1541 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1542 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1543 tree virtual_offset
= NULL
;
1544 tree alias
= callees
->callee
->decl
;
1545 tree thunk_fndecl
= decl
;
1548 /* Instrumentation thunk is the same function with
1549 a different signature. Never need to expand it. */
1550 if (thunk
.add_pointer_bounds_args
)
1553 if (!force_gimple_thunk
&& this_adjusting
1554 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1555 virtual_value
, alias
))
1559 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1561 if (!output_asm_thunks
)
1568 get_untransformed_body ();
1569 a
= DECL_ARGUMENTS (thunk_fndecl
);
1571 current_function_decl
= thunk_fndecl
;
1573 /* Ensure thunks are emitted in their correct sections. */
1574 resolve_unique_section (thunk_fndecl
, 0,
1575 flag_function_sections
);
1577 DECL_RESULT (thunk_fndecl
)
1578 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1579 RESULT_DECL
, 0, restype
);
1580 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1581 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1583 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1585 fn_block
= make_node (BLOCK
);
1586 BLOCK_VARS (fn_block
) = a
;
1587 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1588 init_function_start (thunk_fndecl
);
1590 insn_locations_init ();
1591 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1592 prologue_location
= curr_insn_location ();
1593 assemble_start_function (thunk_fndecl
, fnname
);
1595 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1596 fixed_offset
, virtual_value
, alias
);
1598 assemble_end_function (thunk_fndecl
, fnname
);
1599 insn_locations_finalize ();
1600 init_insn_lengths ();
1601 free_after_compilation (cfun
);
1603 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1604 thunk
.thunk_p
= false;
1607 else if (stdarg_p (TREE_TYPE (thunk_fndecl
)))
1609 error ("generic thunk code fails for method %qD which uses %<...%>",
1611 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1618 basic_block bb
, then_bb
, else_bb
, return_bb
;
1619 gimple_stmt_iterator bsi
;
1629 bool alias_is_noreturn
= TREE_THIS_VOLATILE (alias
);
1632 get_untransformed_body ();
1633 a
= DECL_ARGUMENTS (thunk_fndecl
);
1635 current_function_decl
= thunk_fndecl
;
1637 /* Ensure thunks are emitted in their correct sections. */
1638 resolve_unique_section (thunk_fndecl
, 0,
1639 flag_function_sections
);
1641 DECL_IGNORED_P (thunk_fndecl
) = 1;
1642 bitmap_obstack_initialize (NULL
);
1644 if (thunk
.virtual_offset_p
)
1645 virtual_offset
= size_int (virtual_value
);
1647 /* Build the return declaration for the function. */
1648 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1649 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1651 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1652 DECL_ARTIFICIAL (resdecl
) = 1;
1653 DECL_IGNORED_P (resdecl
) = 1;
1654 DECL_RESULT (thunk_fndecl
) = resdecl
;
1655 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1658 resdecl
= DECL_RESULT (thunk_fndecl
);
1660 bb
= then_bb
= else_bb
= return_bb
1661 = init_lowered_empty_function (thunk_fndecl
, true, count
);
1663 bsi
= gsi_start_bb (bb
);
1665 /* Build call to the function being thunked. */
1666 if (!VOID_TYPE_P (restype
) && !alias_is_noreturn
)
1668 if (DECL_BY_REFERENCE (resdecl
))
1670 restmp
= gimple_fold_indirect_ref (resdecl
);
1672 restmp
= build2 (MEM_REF
,
1673 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias
))),
1675 build_int_cst (TREE_TYPE
1676 (DECL_RESULT (alias
)), 0));
1678 else if (!is_gimple_reg_type (restype
))
1680 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
)))
1684 if (TREE_CODE (restmp
) == VAR_DECL
)
1685 add_local_decl (cfun
, restmp
);
1686 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1689 restmp
= create_tmp_var (restype
, "retval");
1692 restmp
= create_tmp_reg (restype
, "retval");
1695 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1697 auto_vec
<tree
> vargs (nargs
);
1702 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1704 arg
= DECL_CHAIN (a
);
1709 for (; i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1712 if (!is_gimple_val (arg
))
1714 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1715 (TREE_TYPE (arg
)), "arg");
1716 gimple stmt
= gimple_build_assign (tmp
, arg
);
1717 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1719 vargs
.quick_push (tmp
);
1721 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1722 callees
->call_stmt
= call
;
1723 gimple_call_set_from_thunk (call
, true);
1724 gimple_call_set_with_bounds (call
, instrumentation_clone
);
1726 /* Return slot optimization is always possible and in fact requred to
1727 return values with DECL_BY_REFERENCE. */
1728 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
))
1729 && (!is_gimple_reg_type (TREE_TYPE (resdecl
))
1730 || DECL_BY_REFERENCE (resdecl
)))
1731 gimple_call_set_return_slot_opt (call
, true);
1733 if (restmp
&& !alias_is_noreturn
)
1735 gimple_call_set_lhs (call
, restmp
);
1736 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1737 TREE_TYPE (TREE_TYPE (alias
))));
1739 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1740 if (!alias_is_noreturn
)
1742 if (instrumentation_clone
1743 && !DECL_BY_REFERENCE (resdecl
)
1745 && BOUNDED_P (restmp
))
1747 resbnd
= chkp_insert_retbnd_call (NULL
, restmp
, &bsi
);
1748 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi
))),
1749 as_a
<gcall
*> (gsi_stmt (bsi
)),
1750 callees
->count
, callees
->frequency
);
1753 if (restmp
&& !this_adjusting
1754 && (fixed_offset
|| virtual_offset
))
1756 tree true_label
= NULL_TREE
;
1758 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1762 /* If the return type is a pointer, we need to
1763 protect against NULL. We know there will be an
1764 adjustment, because that's why we're emitting a
1766 then_bb
= create_basic_block (NULL
, bb
);
1767 then_bb
->count
= count
- count
/ 16;
1768 then_bb
->frequency
= BB_FREQ_MAX
- BB_FREQ_MAX
/ 16;
1769 return_bb
= create_basic_block (NULL
, then_bb
);
1770 return_bb
->count
= count
;
1771 return_bb
->frequency
= BB_FREQ_MAX
;
1772 else_bb
= create_basic_block (NULL
, else_bb
);
1773 then_bb
->count
= count
/ 16;
1774 then_bb
->frequency
= BB_FREQ_MAX
/ 16;
1775 add_bb_to_loop (then_bb
, bb
->loop_father
);
1776 add_bb_to_loop (return_bb
, bb
->loop_father
);
1777 add_bb_to_loop (else_bb
, bb
->loop_father
);
1778 remove_edge (single_succ_edge (bb
));
1779 true_label
= gimple_block_label (then_bb
);
1780 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1781 build_zero_cst (TREE_TYPE (restmp
)),
1782 NULL_TREE
, NULL_TREE
);
1783 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1784 e
= make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1785 e
->probability
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 16;
1786 e
->count
= count
- count
/ 16;
1787 e
= make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1788 e
->probability
= REG_BR_PROB_BASE
/ 16;
1789 e
->count
= count
/ 16;
1790 e
= make_edge (return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1791 e
->probability
= REG_BR_PROB_BASE
;
1793 e
= make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1794 e
->probability
= REG_BR_PROB_BASE
;
1795 e
->count
= count
- count
/ 16;
1796 e
= make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1797 e
->probability
= REG_BR_PROB_BASE
;
1798 e
->count
= count
/ 16;
1799 bsi
= gsi_last_bb (then_bb
);
1802 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1803 fixed_offset
, virtual_offset
);
1807 bsi
= gsi_last_bb (else_bb
);
1808 stmt
= gimple_build_assign (restmp
,
1809 build_zero_cst (TREE_TYPE (restmp
)));
1810 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1811 bsi
= gsi_last_bb (return_bb
);
1815 gimple_call_set_tail (call
, true);
1817 /* Build return value. */
1818 if (!DECL_BY_REFERENCE (resdecl
))
1819 ret
= gimple_build_return (restmp
);
1821 ret
= gimple_build_return (resdecl
);
1822 gimple_return_set_retbnd (ret
, resbnd
);
1824 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1828 gimple_call_set_tail (call
, true);
1829 remove_edge (single_succ_edge (bb
));
1832 cfun
->gimple_df
->in_ssa_p
= true;
1833 profile_status_for_fn (cfun
)
1834 = count
? PROFILE_READ
: PROFILE_GUESSED
;
1835 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1836 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
1837 delete_unreachable_blocks ();
1838 update_ssa (TODO_update_ssa
);
1839 #ifdef ENABLE_CHECKING
1840 verify_flow_info ();
1842 free_dominance_info (CDI_DOMINATORS
);
1844 /* Since we want to emit the thunk, we explicitly mark its name as
1846 thunk
.thunk_p
= false;
1848 bitmap_obstack_release (NULL
);
1850 current_function_decl
= NULL
;
1855 /* Assemble thunks and aliases associated to node. */
1858 cgraph_node::assemble_thunks_and_aliases (void)
1863 for (e
= callers
; e
;)
1864 if (e
->caller
->thunk
.thunk_p
1865 && !e
->caller
->thunk
.add_pointer_bounds_args
)
1867 cgraph_node
*thunk
= e
->caller
;
1870 thunk
->expand_thunk (true, false);
1871 thunk
->assemble_thunks_and_aliases ();
1876 FOR_EACH_ALIAS (this, ref
)
1878 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1879 bool saved_written
= TREE_ASM_WRITTEN (decl
);
1881 /* Force assemble_alias to really output the alias this time instead
1882 of buffering it in same alias pairs. */
1883 TREE_ASM_WRITTEN (decl
) = 1;
1884 do_assemble_alias (alias
->decl
,
1885 DECL_ASSEMBLER_NAME (decl
));
1886 alias
->assemble_thunks_and_aliases ();
1887 TREE_ASM_WRITTEN (decl
) = saved_written
;
1891 /* Expand function specified by node. */
1894 cgraph_node::expand (void)
1896 location_t saved_loc
;
1898 /* We ought to not compile any inline clones. */
1899 gcc_assert (!global
.inlined_to
);
1901 announce_function (decl
);
1903 gcc_assert (lowered
);
1904 get_untransformed_body ();
1906 /* Generate RTL for the body of DECL. */
1908 timevar_push (TV_REST_OF_COMPILATION
);
1910 gcc_assert (symtab
->global_info_ready
);
1912 /* Initialize the default bitmap obstack. */
1913 bitmap_obstack_initialize (NULL
);
1915 /* Initialize the RTL code for the function. */
1916 current_function_decl
= decl
;
1917 saved_loc
= input_location
;
1918 input_location
= DECL_SOURCE_LOCATION (decl
);
1919 init_function_start (decl
);
1921 gimple_register_cfg_hooks ();
1923 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1925 execute_all_ipa_transforms ();
1927 /* Perform all tree transforms and optimizations. */
1929 /* Signal the start of passes. */
1930 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1932 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
1934 /* Signal the end of passes. */
1935 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1937 bitmap_obstack_release (®_obstack
);
1939 /* Release the default bitmap obstack. */
1940 bitmap_obstack_release (NULL
);
1942 /* If requested, warn about function definitions where the function will
1943 return a value (usually of some struct or union type) which itself will
1944 take up a lot of stack space. */
1945 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1947 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1949 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1950 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1951 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1954 unsigned int size_as_int
1955 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1957 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1958 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
1961 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
1962 decl
, larger_than_size
);
1966 gimple_set_body (decl
, NULL
);
1967 if (DECL_STRUCT_FUNCTION (decl
) == 0
1968 && !cgraph_node::get (decl
)->origin
)
1970 /* Stop pointing to the local nodes about to be freed.
1971 But DECL_INITIAL must remain nonzero so we know this
1972 was an actual function definition.
1973 For a nested function, this is done in c_pop_function_context.
1974 If rest_of_compilation set this to 0, leave it 0. */
1975 if (DECL_INITIAL (decl
) != 0)
1976 DECL_INITIAL (decl
) = error_mark_node
;
1979 input_location
= saved_loc
;
1982 timevar_pop (TV_REST_OF_COMPILATION
);
1984 /* Make sure that BE didn't give up on compiling. */
1985 gcc_assert (TREE_ASM_WRITTEN (decl
));
1987 current_function_decl
= NULL
;
1989 /* It would make a lot more sense to output thunks before function body to get more
1990 forward and lest backwarding jumps. This however would need solving problem
1991 with comdats. See PR48668. Also aliases must come after function itself to
1992 make one pass assemblers, like one on AIX, happy. See PR 50689.
1993 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1995 assemble_thunks_and_aliases ();
1997 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1998 points to the dead function body. */
2000 remove_all_references ();
2003 /* Node comparer that is responsible for the order that corresponds
2004 to time when a function was launched for the first time. */
2007 node_cmp (const void *pa
, const void *pb
)
2009 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
2010 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
2012 /* Functions with time profile must be before these without profile. */
2013 if (!a
->tp_first_run
|| !b
->tp_first_run
)
2014 return a
->tp_first_run
- b
->tp_first_run
;
2016 return a
->tp_first_run
!= b
->tp_first_run
2017 ? b
->tp_first_run
- a
->tp_first_run
2018 : b
->order
- a
->order
;
2021 /* Expand all functions that must be output.
2023 Attempt to topologically sort the nodes so function is output when
2024 all called functions are already assembled to allow data to be
2025 propagated across the callgraph. Use a stack to get smaller distance
2026 between a function and its callees (later we may choose to use a more
2027 sophisticated algorithm for function reordering; we will likely want
2028 to use subsections to make the output functions appear in top-down
2032 expand_all_functions (void)
2035 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
2036 symtab
->cgraph_count
);
2037 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
2038 int order_pos
, new_order_pos
= 0;
2041 order_pos
= ipa_reverse_postorder (order
);
2042 gcc_assert (order_pos
== symtab
->cgraph_count
);
2044 /* Garbage collector may remove inline clones we eliminate during
2045 optimization. So we must be sure to not reference them. */
2046 for (i
= 0; i
< order_pos
; i
++)
2047 if (order
[i
]->process
)
2048 order
[new_order_pos
++] = order
[i
];
2050 if (flag_profile_reorder_functions
)
2051 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
2053 for (i
= new_order_pos
- 1; i
>= 0; i
--)
2059 expanded_func_count
++;
2060 if(node
->tp_first_run
)
2061 profiled_func_count
++;
2063 if (symtab
->dump_file
)
2064 fprintf (symtab
->dump_file
,
2065 "Time profile order in expand_all_functions:%s:%d\n",
2066 node
->asm_name (), node
->tp_first_run
);
2073 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
2074 main_input_filename
, profiled_func_count
, expanded_func_count
);
2076 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
2077 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
2078 profiled_func_count
, expanded_func_count
);
2080 symtab
->process_new_functions ();
2081 free_gimplify_stack ();
2086 /* This is used to sort the node types by the cgraph order number. */
2088 enum cgraph_order_sort_kind
2090 ORDER_UNDEFINED
= 0,
2096 struct cgraph_order_sort
2098 enum cgraph_order_sort_kind kind
;
2107 /* Output all functions, variables, and asm statements in the order
2108 according to their order fields, which is the order in which they
2109 appeared in the file. This implements -fno-toplevel-reorder. In
2110 this mode we may output functions and variables which don't really
2112 When NO_REORDER is true only do this for symbols marked no reorder. */
2115 output_in_order (bool no_reorder
)
2118 cgraph_order_sort
*nodes
;
2123 max
= symtab
->order
;
2124 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
2126 FOR_EACH_DEFINED_FUNCTION (pf
)
2128 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
2130 if (no_reorder
&& !pf
->no_reorder
)
2133 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2134 nodes
[i
].kind
= ORDER_FUNCTION
;
2139 FOR_EACH_DEFINED_VARIABLE (pv
)
2140 if (!DECL_EXTERNAL (pv
->decl
))
2142 if (no_reorder
&& !pv
->no_reorder
)
2145 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2146 nodes
[i
].kind
= ORDER_VAR
;
2150 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
2153 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2154 nodes
[i
].kind
= ORDER_ASM
;
2158 /* In toplevel reorder mode we output all statics; mark them as needed. */
2160 for (i
= 0; i
< max
; ++i
)
2161 if (nodes
[i
].kind
== ORDER_VAR
)
2162 nodes
[i
].u
.v
->finalize_named_section_flags ();
2164 for (i
= 0; i
< max
; ++i
)
2166 switch (nodes
[i
].kind
)
2168 case ORDER_FUNCTION
:
2169 nodes
[i
].u
.f
->process
= 0;
2170 nodes
[i
].u
.f
->expand ();
2174 nodes
[i
].u
.v
->assemble_decl ();
2178 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2181 case ORDER_UNDEFINED
:
2189 symtab
->clear_asm_symbols ();
2197 gcc::pass_manager
*passes
= g
->get_passes ();
2200 current_function_decl
= NULL
;
2201 gimple_register_cfg_hooks ();
2202 bitmap_obstack_initialize (NULL
);
2204 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2208 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2213 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2214 devirtualization and other changes where removal iterate. */
2215 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2217 /* If pass_all_early_optimizations was not scheduled, the state of
2218 the cgraph will not be properly updated. Update it now. */
2219 if (symtab
->state
< IPA_SSA
)
2220 symtab
->state
= IPA_SSA
;
2224 /* Generate coverage variables and constructors. */
2227 /* Process new functions added. */
2229 current_function_decl
= NULL
;
2230 symtab
->process_new_functions ();
2232 execute_ipa_summary_passes
2233 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2236 /* Some targets need to handle LTO assembler output specially. */
2237 if (flag_generate_lto
|| flag_generate_offload
)
2238 targetm
.asm_out
.lto_start ();
2242 if (g
->have_offload
)
2244 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2245 lto_stream_offload_p
= true;
2246 ipa_write_summaries ();
2247 lto_stream_offload_p
= false;
2251 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2252 lto_stream_offload_p
= false;
2253 ipa_write_summaries ();
2257 if (flag_generate_lto
|| flag_generate_offload
)
2258 targetm
.asm_out
.lto_end ();
2260 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2261 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2262 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2264 bitmap_obstack_release (NULL
);
2268 /* Return string alias is alias of. */
2271 get_alias_symbol (tree decl
)
2273 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2274 return get_identifier (TREE_STRING_POINTER
2275 (TREE_VALUE (TREE_VALUE (alias
))));
2279 /* Weakrefs may be associated to external decls and thus not output
2280 at expansion time. Emit all necessary aliases. */
2283 symbol_table::output_weakrefs (void)
2287 FOR_EACH_SYMBOL (node
)
2289 && !TREE_ASM_WRITTEN (node
->decl
)
2290 && (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
2291 || !cnode
->instrumented_version
2292 || !TREE_ASM_WRITTEN (cnode
->instrumented_version
->decl
))
2297 /* Weakrefs are special by not requiring target definition in current
2298 compilation unit. It is thus bit hard to work out what we want to
2300 When alias target is defined, we need to fetch it from symtab reference,
2301 otherwise it is pointed to by alias_target. */
2302 if (node
->alias_target
)
2303 target
= (DECL_P (node
->alias_target
)
2304 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2305 : node
->alias_target
);
2306 else if (node
->analyzed
)
2307 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2311 target
= get_alias_symbol (node
->decl
);
2313 do_assemble_alias (node
->decl
, target
);
2317 /* Perform simple optimizations based on callgraph. */
2320 symbol_table::compile (void)
2325 #ifdef ENABLE_CHECKING
2326 symtab_node::verify_symtab_nodes ();
2329 timevar_push (TV_CGRAPHOPT
);
2330 if (pre_ipa_mem_report
)
2332 fprintf (stderr
, "Memory consumption before IPA\n");
2333 dump_memory_report (false);
2336 fprintf (stderr
, "Performing interprocedural optimizations\n");
2339 /* Offloading requires LTO infrastructure. */
2340 if (!in_lto_p
&& g
->have_offload
)
2341 flag_generate_offload
= 1;
2343 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2344 if (flag_generate_lto
|| flag_generate_offload
)
2345 lto_streamer_hooks_init ();
2347 /* Don't run the IPA passes if there was any error or sorry messages. */
2351 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2353 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2355 timevar_pop (TV_CGRAPHOPT
);
2359 global_info_ready
= true;
2362 fprintf (dump_file
, "Optimized ");
2363 symtab_node:: dump_table (dump_file
);
2365 if (post_ipa_mem_report
)
2367 fprintf (stderr
, "Memory consumption after IPA\n");
2368 dump_memory_report (false);
2370 timevar_pop (TV_CGRAPHOPT
);
2372 /* Output everything. */
2373 (*debug_hooks
->assembly_start
) ();
2375 fprintf (stderr
, "Assembling functions:\n");
2376 #ifdef ENABLE_CHECKING
2377 symtab_node::verify_symtab_nodes ();
2380 materialize_all_clones ();
2381 bitmap_obstack_initialize (NULL
);
2382 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2383 bitmap_obstack_release (NULL
);
2384 mark_functions_to_output ();
2386 /* When weakref support is missing, we autmatically translate all
2387 references to NODE to references to its ultimate alias target.
2388 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2391 Set up this mapping before we output any assembler but once we are sure
2392 that all symbol renaming is done.
2394 FIXME: All this uglyness can go away if we just do renaming at gimple
2395 level by physically rewritting the IL. At the moment we can only redirect
2396 calls, so we need infrastructure for renaming references as well. */
2397 #ifndef ASM_OUTPUT_WEAKREF
2400 FOR_EACH_SYMBOL (node
)
2402 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2404 IDENTIFIER_TRANSPARENT_ALIAS
2405 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2406 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2407 = (node
->alias_target
? node
->alias_target
2408 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2414 if (!flag_toplevel_reorder
)
2415 output_in_order (false);
2418 /* Output first asm statements and anything ordered. The process
2419 flag is cleared for these nodes, so we skip them later. */
2420 output_in_order (true);
2421 expand_all_functions ();
2422 output_variables ();
2425 process_new_functions ();
2431 fprintf (dump_file
, "\nFinal ");
2432 symtab_node::dump_table (dump_file
);
2434 #ifdef ENABLE_CHECKING
2435 symtab_node::verify_symtab_nodes ();
2436 /* Double check that all inline clones are gone and that all
2437 function bodies have been released from memory. */
2441 bool error_found
= false;
2443 FOR_EACH_DEFINED_FUNCTION (node
)
2444 if (node
->global
.inlined_to
2445 || gimple_has_body_p (node
->decl
))
2451 internal_error ("nodes with unreleased memory found");
2457 /* Analyze the whole compilation unit once it is parsed completely. */
2460 symbol_table::finalize_compilation_unit (void)
2462 timevar_push (TV_CGRAPH
);
2464 /* If we're here there's no current function anymore. Some frontends
2465 are lazy in clearing these. */
2466 current_function_decl
= NULL
;
2469 /* Do not skip analyzing the functions if there were errors, we
2470 miss diagnostics for following functions otherwise. */
2472 /* Emit size functions we didn't inline. */
2473 finalize_size_functions ();
2475 /* Mark alias targets necessary and emit diagnostics. */
2476 handle_alias_pairs ();
2480 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2484 if (flag_dump_passes
)
2487 /* Gimplify and lower all functions, compute reachability and
2488 remove unreachable nodes. */
2489 analyze_functions (/*first_time=*/true);
2491 /* Mark alias targets necessary and emit diagnostics. */
2492 handle_alias_pairs ();
2494 /* Gimplify and lower thunks. */
2495 analyze_functions (/*first_time=*/false);
2497 /* Emit early debug for reachable functions, and by consequence,
2498 locally scoped symbols. */
2499 struct cgraph_node
*cnode
;
2500 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode
)
2501 (*debug_hooks
->early_global_decl
) (cnode
->decl
);
2503 /* Clean up anything that needs cleaning up after initial debug
2505 (*debug_hooks
->early_finish
) ();
2507 /* Finally drive the pass manager. */
2510 timevar_pop (TV_CGRAPH
);
2513 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2514 within the same process. For use by toplev::finalize. */
2517 cgraphunit_c_finalize (void)
2519 gcc_assert (cgraph_new_nodes
.length () == 0);
2520 cgraph_new_nodes
.truncate (0);
2522 vtable_entry_type
= NULL
;
2523 queued_nodes
= &symtab_terminator
;
2525 first_analyzed
= NULL
;
2526 first_analyzed_var
= NULL
;
2529 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2530 kind of wrapper method. */
2533 cgraph_node::create_wrapper (cgraph_node
*target
)
2535 /* Preserve DECL_RESULT so we get right by reference flag. */
2536 tree decl_result
= DECL_RESULT (decl
);
2538 /* Remove the function's body but keep arguments to be reused
2540 release_body (true);
2543 DECL_UNINLINABLE (decl
) = false;
2544 DECL_RESULT (decl
) = decl_result
;
2545 DECL_INITIAL (decl
) = NULL
;
2546 allocate_struct_function (decl
, false);
2549 /* Turn alias into thunk and expand it into GIMPLE representation. */
2552 memset (&thunk
, 0, sizeof (cgraph_thunk_info
));
2553 thunk
.thunk_p
= true;
2554 create_edge (target
, NULL
, count
, CGRAPH_FREQ_BASE
);
2556 tree arguments
= DECL_ARGUMENTS (decl
);
2560 TREE_ADDRESSABLE (arguments
) = false;
2561 arguments
= TREE_CHAIN (arguments
);
2564 expand_thunk (false, true);
2566 /* Inline summary set-up. */
2568 inline_analyze_function (this);
2571 #include "gt-cgraphunit.h"