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"
164 #include "hash-set.h"
171 #include "fold-const.h"
173 #include "stor-layout.h"
174 #include "stringpool.h"
178 #include "hard-reg-set.h"
180 #include "function.h"
181 #include "basic-block.h"
182 #include "tree-ssa-alias.h"
183 #include "internal-fn.h"
184 #include "gimple-fold.h"
185 #include "gimple-expr.h"
188 #include "gimplify.h"
189 #include "gimple-iterator.h"
190 #include "gimplify-me.h"
191 #include "gimple-ssa.h"
192 #include "tree-cfg.h"
193 #include "tree-into-ssa.h"
194 #include "tree-ssa.h"
195 #include "tree-inline.h"
196 #include "langhooks.h"
201 #include "diagnostic.h"
204 #include "hash-map.h"
205 #include "plugin-api.h"
208 #include "alloc-pool.h"
209 #include "symbol-summary.h"
210 #include "ipa-prop.h"
211 #include "tree-iterator.h"
212 #include "tree-pass.h"
213 #include "tree-dump.h"
214 #include "gimple-pretty-print.h"
216 #include "coverage.h"
218 #include "ipa-inline.h"
219 #include "ipa-utils.h"
220 #include "lto-streamer.h"
223 #include "regset.h" /* FIXME: For reg_obstack. */
225 #include "pass_manager.h"
226 #include "tree-nested.h"
227 #include "gimplify.h"
229 #include "tree-chkp.h"
230 #include "lto-section-names.h"
232 #include "print-tree.h"
234 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
235 secondary queue used during optimization to accommodate passes that
236 may generate new functions that need to be optimized and expanded. */
237 vec
<cgraph_node
*> cgraph_new_nodes
;
239 static void expand_all_functions (void);
240 static void mark_functions_to_output (void);
241 static void handle_alias_pairs (void);
243 /* Used for vtable lookup in thunk adjusting. */
244 static GTY (()) tree vtable_entry_type
;
246 /* Determine if symbol declaration is needed. That is, visible to something
247 either outside this translation unit, something magic in the system
250 symtab_node::needed_p (void)
252 /* Double check that no one output the function into assembly file
254 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
255 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
260 if (DECL_EXTERNAL (decl
))
263 /* If the user told us it is used, then it must be so. */
267 /* ABI forced symbols are needed when they are external. */
268 if (forced_by_abi
&& TREE_PUBLIC (decl
))
271 /* Keep constructors, destructors and virtual functions. */
272 if (TREE_CODE (decl
) == FUNCTION_DECL
273 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
276 /* Externally visible variables must be output. The exception is
277 COMDAT variables that must be output only when they are needed. */
278 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
284 /* Head and terminator of the queue of nodes to be processed while building
287 static symtab_node symtab_terminator
;
288 static symtab_node
*queued_nodes
= &symtab_terminator
;
290 /* Add NODE to queue starting at QUEUED_NODES.
291 The queue is linked via AUX pointers and terminated by pointer to 1. */
294 enqueue_node (symtab_node
*node
)
298 gcc_checking_assert (queued_nodes
);
299 node
->aux
= queued_nodes
;
303 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
304 functions into callgraph in a way so they look like ordinary reachable
305 functions inserted into callgraph already at construction time. */
308 symbol_table::process_new_functions (void)
312 if (!cgraph_new_nodes
.exists ())
315 handle_alias_pairs ();
316 /* Note that this queue may grow as its being processed, as the new
317 functions may generate new ones. */
318 for (unsigned i
= 0; i
< cgraph_new_nodes
.length (); i
++)
320 cgraph_node
*node
= cgraph_new_nodes
[i
];
325 /* At construction time we just need to finalize function and move
326 it into reachable functions list. */
328 cgraph_node::finalize_function (fndecl
, false);
329 call_cgraph_insertion_hooks (node
);
335 case IPA_SSA_AFTER_INLINING
:
336 /* When IPA optimization already started, do all essential
337 transformations that has been already performed on the whole
338 cgraph but not on this function. */
340 gimple_register_cfg_hooks ();
343 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
344 if ((state
== IPA_SSA
|| state
== IPA_SSA_AFTER_INLINING
)
345 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
346 g
->get_passes ()->execute_early_local_passes ();
347 else if (inline_summaries
!= NULL
)
348 compute_inline_parameters (node
, true);
349 free_dominance_info (CDI_POST_DOMINATORS
);
350 free_dominance_info (CDI_DOMINATORS
);
352 call_cgraph_insertion_hooks (node
);
356 /* Functions created during expansion shall be compiled
359 call_cgraph_insertion_hooks (node
);
369 cgraph_new_nodes
.release ();
372 /* As an GCC extension we allow redefinition of the function. The
373 semantics when both copies of bodies differ is not well defined.
374 We replace the old body with new body so in unit at a time mode
375 we always use new body, while in normal mode we may end up with
376 old body inlined into some functions and new body expanded and
379 ??? It may make more sense to use one body for inlining and other
380 body for expanding the function but this is difficult to do. */
383 cgraph_node::reset (void)
385 /* If process is set, then we have already begun whole-unit analysis.
386 This is *not* testing for whether we've already emitted the function.
387 That case can be sort-of legitimately seen with real function redefinition
388 errors. I would argue that the front end should never present us with
389 such a case, but don't enforce that for now. */
390 gcc_assert (!process
);
392 /* Reset our data structures so we can analyze the function again. */
393 memset (&local
, 0, sizeof (local
));
394 memset (&global
, 0, sizeof (global
));
395 memset (&rtl
, 0, sizeof (rtl
));
400 cpp_implicit_alias
= false;
403 remove_all_references ();
406 /* Return true when there are references to the node. INCLUDE_SELF is
407 true if a self reference counts as a reference. */
410 symtab_node::referred_to_p (bool include_self
)
414 /* See if there are any references at all. */
415 if (iterate_referring (0, ref
))
417 /* For functions check also calls. */
418 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
419 if (cn
&& cn
->callers
)
423 for (cgraph_edge
*e
= cn
->callers
; e
; e
= e
->next_caller
)
424 if (e
->caller
!= this)
430 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
431 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
432 the garbage collector run at the moment. We would need to either create
433 a new GC context, or just not compile right now. */
436 cgraph_node::finalize_function (tree decl
, bool no_collect
)
438 cgraph_node
*node
= cgraph_node::get_create (decl
);
440 if (node
->definition
)
442 /* Nested functions should only be defined once. */
443 gcc_assert (!DECL_CONTEXT (decl
)
444 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
446 node
->local
.redefined_extern_inline
= true;
449 /* Set definition first before calling notice_global_symbol so that
450 it is available to notice_global_symbol. */
451 node
->definition
= true;
452 notice_global_symbol (decl
);
453 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
455 /* With -fkeep-inline-functions we are keeping all inline functions except
456 for extern inline ones. */
457 if (flag_keep_inline_functions
458 && DECL_DECLARED_INLINE_P (decl
)
459 && !DECL_EXTERNAL (decl
)
460 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
461 node
->force_output
= 1;
463 /* When not optimizing, also output the static functions. (see
464 PR24561), but don't do so for always_inline functions, functions
465 declared inline and nested functions. These were optimized out
466 in the original implementation and it is unclear whether we want
467 to change the behavior here. */
468 if ((!opt_for_fn (decl
, optimize
)
469 && !node
->cpp_implicit_alias
470 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
471 && !DECL_DECLARED_INLINE_P (decl
)
472 && !(DECL_CONTEXT (decl
)
473 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
474 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
475 node
->force_output
= 1;
477 /* If we've not yet emitted decl, tell the debug info about it. */
478 if (!TREE_ASM_WRITTEN (decl
))
479 (*debug_hooks
->deferred_inline_function
) (decl
);
481 /* Possibly warn about unused parameters. */
482 if (warn_unused_parameter
)
483 do_warn_unused_parameter (decl
);
488 if (symtab
->state
== CONSTRUCTION
489 && (node
->needed_p () || node
->referred_to_p ()))
493 /* Add the function FNDECL to the call graph.
494 Unlike finalize_function, this function is intended to be used
495 by middle end and allows insertion of new function at arbitrary point
496 of compilation. The function can be either in high, low or SSA form
499 The function is assumed to be reachable and have address taken (so no
500 API breaking optimizations are performed on it).
502 Main work done by this function is to enqueue the function for later
503 processing to avoid need the passes to be re-entrant. */
506 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
508 gcc::pass_manager
*passes
= g
->get_passes ();
513 struct function
*fn
= DECL_STRUCT_FUNCTION (fndecl
);
514 const char *function_type
= ((gimple_has_body_p (fndecl
))
516 ? (gimple_in_ssa_p (fn
)
520 : "to-be-gimplified");
522 "Added new %s function %s to callgraph\n",
524 fndecl_name (fndecl
));
527 switch (symtab
->state
)
530 cgraph_node::finalize_function (fndecl
, false);
533 /* Just enqueue function to be processed at nearest occurrence. */
534 node
= cgraph_node::get_create (fndecl
);
536 node
->lowered
= true;
537 cgraph_new_nodes
.safe_push (node
);
542 case IPA_SSA_AFTER_INLINING
:
544 /* Bring the function into finalized state and enqueue for later
545 analyzing and compilation. */
546 node
= cgraph_node::get_create (fndecl
);
547 node
->local
.local
= false;
548 node
->definition
= true;
549 node
->force_output
= true;
550 if (!lowered
&& symtab
->state
== EXPANSION
)
552 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
553 gimple_register_cfg_hooks ();
554 bitmap_obstack_initialize (NULL
);
555 execute_pass_list (cfun
, passes
->all_lowering_passes
);
556 passes
->execute_early_local_passes ();
557 bitmap_obstack_release (NULL
);
563 node
->lowered
= true;
564 cgraph_new_nodes
.safe_push (node
);
568 /* At the very end of compilation we have to do all the work up
570 node
= cgraph_node::create (fndecl
);
572 node
->lowered
= true;
573 node
->definition
= true;
575 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
576 gimple_register_cfg_hooks ();
577 bitmap_obstack_initialize (NULL
);
578 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
579 g
->get_passes ()->execute_early_local_passes ();
580 bitmap_obstack_release (NULL
);
589 /* Set a personality if required and we already passed EH lowering. */
591 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
592 == eh_personality_lang
))
593 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
596 /* Analyze the function scheduled to be output. */
598 cgraph_node::analyze (void)
600 tree decl
= this->decl
;
601 location_t saved_loc
= input_location
;
602 input_location
= DECL_SOURCE_LOCATION (decl
);
606 cgraph_node
*t
= cgraph_node::get (thunk
.alias
);
608 create_edge (t
, NULL
, 0, CGRAPH_FREQ_BASE
);
609 /* Target code in expand_thunk may need the thunk's target
610 to be analyzed, so recurse here. */
615 t
= t
->get_alias_target ();
619 if (!expand_thunk (false, false))
627 resolve_alias (cgraph_node::get (alias_target
));
628 else if (dispatcher_function
)
630 /* Generate the dispatcher body of multi-versioned functions. */
631 cgraph_function_version_info
*dispatcher_version_info
632 = function_version ();
633 if (dispatcher_version_info
!= NULL
634 && (dispatcher_version_info
->dispatcher_resolver
637 tree resolver
= NULL_TREE
;
638 gcc_assert (targetm
.generate_version_dispatcher_body
);
639 resolver
= targetm
.generate_version_dispatcher_body (this);
640 gcc_assert (resolver
!= NULL_TREE
);
645 push_cfun (DECL_STRUCT_FUNCTION (decl
));
647 assign_assembler_name_if_neeeded (decl
);
649 /* Make sure to gimplify bodies only once. During analyzing a
650 function we lower it, which will require gimplified nested
651 functions, so we can end up here with an already gimplified
653 if (!gimple_has_body_p (decl
))
654 gimplify_function_tree (decl
);
656 /* Lower the function. */
660 lower_nested_functions (decl
);
661 gcc_assert (!nested
);
663 gimple_register_cfg_hooks ();
664 bitmap_obstack_initialize (NULL
);
665 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
666 free_dominance_info (CDI_POST_DOMINATORS
);
667 free_dominance_info (CDI_DOMINATORS
);
669 bitmap_obstack_release (NULL
);
677 input_location
= saved_loc
;
680 /* C++ frontend produce same body aliases all over the place, even before PCH
681 gets streamed out. It relies on us linking the aliases with their function
682 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
683 first produce aliases without links, but once C++ FE is sure he won't sream
684 PCH we build the links via this function. */
687 symbol_table::process_same_body_aliases (void)
690 FOR_EACH_SYMBOL (node
)
691 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
693 (TREE_CODE (node
->alias_target
) == VAR_DECL
694 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
695 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
696 cpp_implicit_aliases_done
= true;
699 /* Process attributes common for vars and functions. */
702 process_common_attributes (symtab_node
*node
, tree decl
)
704 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
706 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
708 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
709 "%<weakref%> attribute should be accompanied with"
710 " an %<alias%> attribute");
711 DECL_WEAK (decl
) = 0;
712 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
713 DECL_ATTRIBUTES (decl
));
716 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
717 node
->no_reorder
= 1;
720 /* Look for externally_visible and used attributes and mark cgraph nodes
723 We cannot mark the nodes at the point the attributes are processed (in
724 handle_*_attribute) because the copy of the declarations available at that
725 point may not be canonical. For example, in:
728 void f() __attribute__((used));
730 the declaration we see in handle_used_attribute will be the second
731 declaration -- but the front end will subsequently merge that declaration
732 with the original declaration and discard the second declaration.
734 Furthermore, we can't mark these nodes in finalize_function because:
737 void f() __attribute__((externally_visible));
741 So, we walk the nodes at the end of the translation unit, applying the
742 attributes at that point. */
745 process_function_and_variable_attributes (cgraph_node
*first
,
746 varpool_node
*first_var
)
751 for (node
= symtab
->first_function (); node
!= first
;
752 node
= symtab
->next_function (node
))
754 tree decl
= node
->decl
;
755 if (DECL_PRESERVE_P (decl
))
756 node
->mark_force_output ();
757 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
759 if (! TREE_PUBLIC (node
->decl
))
760 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
761 "%<externally_visible%>"
762 " attribute have effect only on public objects");
764 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
765 && (node
->definition
&& !node
->alias
))
767 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
768 "%<weakref%> attribute ignored"
769 " because function is defined");
770 DECL_WEAK (decl
) = 0;
771 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
772 DECL_ATTRIBUTES (decl
));
775 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
776 && !DECL_DECLARED_INLINE_P (decl
)
777 /* redefining extern inline function makes it DECL_UNINLINABLE. */
778 && !DECL_UNINLINABLE (decl
))
779 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
780 "always_inline function might not be inlinable");
782 process_common_attributes (node
, decl
);
784 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
785 vnode
= symtab
->next_variable (vnode
))
787 tree decl
= vnode
->decl
;
788 if (DECL_EXTERNAL (decl
)
789 && DECL_INITIAL (decl
))
790 varpool_node::finalize_decl (decl
);
791 if (DECL_PRESERVE_P (decl
))
792 vnode
->force_output
= true;
793 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
795 if (! TREE_PUBLIC (vnode
->decl
))
796 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
797 "%<externally_visible%>"
798 " attribute have effect only on public objects");
800 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
802 && DECL_INITIAL (decl
))
804 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
805 "%<weakref%> attribute ignored"
806 " because variable is initialized");
807 DECL_WEAK (decl
) = 0;
808 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
809 DECL_ATTRIBUTES (decl
));
811 process_common_attributes (vnode
, decl
);
815 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
816 middle end to output the variable to asm file, if needed or externally
820 varpool_node::finalize_decl (tree decl
)
822 varpool_node
*node
= varpool_node::get_create (decl
);
824 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
826 if (node
->definition
)
828 /* Set definition first before calling notice_global_symbol so that
829 it is available to notice_global_symbol. */
830 node
->definition
= true;
831 notice_global_symbol (decl
);
832 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
833 /* Traditionally we do not eliminate static variables when not
834 optimizing and when not doing toplevel reoder. */
836 || ((!flag_toplevel_reorder
837 && !DECL_COMDAT (node
->decl
)
838 && !DECL_ARTIFICIAL (node
->decl
))))
839 node
->force_output
= true;
841 if (symtab
->state
== CONSTRUCTION
842 && (node
->needed_p () || node
->referred_to_p ()))
844 if (symtab
->state
>= IPA_SSA
)
846 /* Some frontends produce various interface variables after compilation
848 if (symtab
->state
== FINISHED
849 || (!flag_toplevel_reorder
850 && symtab
->state
== EXPANSION
))
851 node
->assemble_decl ();
853 if (DECL_INITIAL (decl
))
854 chkp_register_var_initializer (decl
);
857 /* EDGE is an polymorphic call. Mark all possible targets as reachable
858 and if there is only one target, perform trivial devirtualization.
859 REACHABLE_CALL_TARGETS collects target lists we already walked to
860 avoid udplicate work. */
863 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
869 vec
<cgraph_node
*>targets
870 = possible_polymorphic_call_targets
871 (edge
, &final
, &cache_token
);
873 if (!reachable_call_targets
->add (cache_token
))
875 if (symtab
->dump_file
)
876 dump_possible_polymorphic_call_targets
877 (symtab
->dump_file
, edge
);
879 for (i
= 0; i
< targets
.length (); i
++)
881 /* Do not bother to mark virtual methods in anonymous namespace;
882 either we will find use of virtual table defining it, or it is
884 if (targets
[i
]->definition
886 (TREE_TYPE (targets
[i
]->decl
))
888 && !type_in_anonymous_namespace_p
889 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets
[i
]->decl
))))
890 enqueue_node (targets
[i
]);
894 /* Very trivial devirtualization; when the type is
895 final or anonymous (so we know all its derivation)
896 and there is only one possible virtual call target,
897 make the edge direct. */
900 if (targets
.length () <= 1 && dbg_cnt (devirt
))
903 if (targets
.length () == 1)
906 target
= cgraph_node::create
907 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
909 if (symtab
->dump_file
)
911 fprintf (symtab
->dump_file
,
912 "Devirtualizing call: ");
913 print_gimple_stmt (symtab
->dump_file
,
917 if (dump_enabled_p ())
919 location_t locus
= gimple_location_safe (edge
->call_stmt
);
920 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
921 "devirtualizing call in %s to %s\n",
922 edge
->caller
->name (), target
->name ());
925 edge
->make_direct (target
);
926 edge
->redirect_call_stmt_to_callee ();
928 /* Call to __builtin_unreachable shouldn't be instrumented. */
929 if (!targets
.length ())
930 gimple_call_set_with_bounds (edge
->call_stmt
, false);
932 if (symtab
->dump_file
)
934 fprintf (symtab
->dump_file
,
935 "Devirtualized as: ");
936 print_gimple_stmt (symtab
->dump_file
,
945 /* Discover all functions and variables that are trivially needed, analyze
946 them as well as all functions and variables referred by them */
947 static cgraph_node
*first_analyzed
;
948 static varpool_node
*first_analyzed_var
;
950 /* FIRST_TIME is set to TRUE for the first time we are called for a
951 translation unit from finalize_compilation_unit() or false
955 analyze_functions (bool first_time
)
957 /* Keep track of already processed nodes when called multiple times for
958 intermodule optimization. */
959 cgraph_node
*first_handled
= first_analyzed
;
960 varpool_node
*first_handled_var
= first_analyzed_var
;
961 hash_set
<void *> reachable_call_targets
;
968 location_t saved_loc
= input_location
;
970 bitmap_obstack_initialize (NULL
);
971 symtab
->state
= CONSTRUCTION
;
972 input_location
= UNKNOWN_LOCATION
;
974 /* Ugly, but the fixup can not happen at a time same body alias is created;
975 C++ FE is confused about the COMDAT groups being right. */
976 if (symtab
->cpp_implicit_aliases_done
)
977 FOR_EACH_SYMBOL (node
)
978 if (node
->cpp_implicit_alias
)
979 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
980 build_type_inheritance_graph ();
982 /* Analysis adds static variables that in turn adds references to new functions.
983 So we need to iterate the process until it stabilize. */
987 process_function_and_variable_attributes (first_analyzed
,
990 /* First identify the trivially needed symbols. */
991 for (node
= symtab
->first_symbol ();
992 node
!= first_analyzed
993 && node
!= first_analyzed_var
; node
= node
->next
)
995 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
996 node
->get_comdat_group_id ();
997 if (node
->needed_p ())
1000 if (!changed
&& symtab
->dump_file
)
1001 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
1003 if (symtab
->dump_file
)
1004 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
1005 if (!changed
&& symtab
->dump_file
)
1006 fprintf (symtab
->dump_file
, "\n");
1008 if (node
== first_analyzed
1009 || node
== first_analyzed_var
)
1012 symtab
->process_new_functions ();
1013 first_analyzed_var
= symtab
->first_variable ();
1014 first_analyzed
= symtab
->first_function ();
1016 if (changed
&& symtab
->dump_file
)
1017 fprintf (symtab
->dump_file
, "\n");
1019 /* Lower representation, build callgraph edges and references for all trivially
1020 needed symbols and all symbols referred by them. */
1021 while (queued_nodes
!= &symtab_terminator
)
1024 node
= queued_nodes
;
1025 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
1026 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1027 if (cnode
&& cnode
->definition
)
1030 tree decl
= cnode
->decl
;
1032 /* ??? It is possible to create extern inline function
1033 and later using weak alias attribute to kill its body.
1034 See gcc.c-torture/compile/20011119-1.c */
1035 if (!DECL_STRUCT_FUNCTION (decl
)
1037 && !cnode
->thunk
.thunk_p
1038 && !cnode
->dispatcher_function
)
1041 cnode
->local
.redefined_extern_inline
= true;
1045 if (!cnode
->analyzed
)
1048 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
1049 if (edge
->callee
->definition
1050 && (!DECL_EXTERNAL (edge
->callee
->decl
)
1051 /* When not optimizing, do not try to analyze extern
1052 inline functions. Doing so is pointless. */
1053 || opt_for_fn (edge
->callee
->decl
, optimize
)
1054 /* Weakrefs needs to be preserved. */
1055 || edge
->callee
->alias
1056 /* always_inline functions are inlined aven at -O0. */
1059 DECL_ATTRIBUTES (edge
->callee
->decl
))
1060 /* Multiversioned functions needs the dispatcher to
1061 be produced locally even for extern functions. */
1062 || edge
->callee
->function_version ()))
1063 enqueue_node (edge
->callee
);
1064 if (opt_for_fn (cnode
->decl
, optimize
)
1065 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
1069 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1071 next
= edge
->next_callee
;
1072 if (edge
->indirect_info
->polymorphic
)
1073 walk_polymorphic_call_targets (&reachable_call_targets
,
1078 /* If decl is a clone of an abstract function,
1079 mark that abstract function so that we don't release its body.
1080 The DECL_INITIAL() of that abstract function declaration
1081 will be later needed to output debug info. */
1082 if (DECL_ABSTRACT_ORIGIN (decl
))
1084 cgraph_node
*origin_node
1085 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1086 origin_node
->used_as_abstract_origin
= true;
1091 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1092 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1096 if (node
->same_comdat_group
)
1099 for (next
= node
->same_comdat_group
;
1101 next
= next
->same_comdat_group
)
1102 if (!next
->comdat_local_p ())
1103 enqueue_node (next
);
1105 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1106 if (ref
->referred
->definition
1107 && (!DECL_EXTERNAL (ref
->referred
->decl
)
1108 || ((TREE_CODE (ref
->referred
->decl
) != FUNCTION_DECL
1110 || (TREE_CODE (ref
->referred
->decl
) == FUNCTION_DECL
1111 && opt_for_fn (ref
->referred
->decl
, optimize
))
1113 || ref
->referred
->alias
)))
1114 enqueue_node (ref
->referred
);
1115 symtab
->process_new_functions ();
1118 update_type_inheritance_graph ();
1120 /* Collect entry points to the unit. */
1121 if (symtab
->dump_file
)
1123 fprintf (symtab
->dump_file
, "\n\nInitial ");
1124 symtab_node::dump_table (symtab
->dump_file
);
1130 FOR_EACH_SYMBOL (snode
)
1131 check_global_declaration (snode
->decl
);
1134 if (symtab
->dump_file
)
1135 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1137 for (node
= symtab
->first_symbol ();
1138 node
!= first_handled
1139 && node
!= first_handled_var
; node
= next
)
1142 if (!node
->aux
&& !node
->referred_to_p ())
1144 if (symtab
->dump_file
)
1145 fprintf (symtab
->dump_file
, " %s", node
->name ());
1147 /* See if the debugger can use anything before the DECL
1148 passes away. Perhaps it can notice a DECL that is now a
1149 constant and can tag the early DIE with an appropriate
1152 Otherwise, this is the last chance the debug_hooks have
1153 at looking at optimized away DECLs, since
1154 late_global_decl will subsequently be called from the
1155 contents of the now pruned symbol table. */
1156 if (!decl_function_context (node
->decl
))
1157 (*debug_hooks
->late_global_decl
) (node
->decl
);
1162 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1164 tree decl
= node
->decl
;
1166 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1168 && !cnode
->thunk
.thunk_p
)
1171 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1173 || gimple_has_body_p (decl
));
1174 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1178 for (;node
; node
= node
->next
)
1180 first_analyzed
= symtab
->first_function ();
1181 first_analyzed_var
= symtab
->first_variable ();
1182 if (symtab
->dump_file
)
1184 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1185 symtab_node::dump_table (symtab
->dump_file
);
1187 bitmap_obstack_release (NULL
);
1189 /* Initialize assembler name hash, in particular we want to trigger C++
1190 mangling and same body alias creation before we free DECL_ARGUMENTS
1193 symtab
->symtab_initialize_asm_name_hash ();
1195 input_location
= saved_loc
;
1198 /* Translate the ugly representation of aliases as alias pairs into nice
1199 representation in callgraph. We don't handle all cases yet,
1203 handle_alias_pairs (void)
1208 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1210 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1212 /* Weakrefs with target not defined in current unit are easy to handle:
1213 they behave just as external variables except we need to note the
1214 alias flag to later output the weakref pseudo op into asm file. */
1216 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1218 symtab_node
*node
= symtab_node::get (p
->decl
);
1221 node
->alias_target
= p
->target
;
1222 node
->weakref
= true;
1225 alias_pairs
->unordered_remove (i
);
1228 else if (!target_node
)
1230 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1231 symtab_node
*node
= symtab_node::get (p
->decl
);
1233 node
->alias
= false;
1234 alias_pairs
->unordered_remove (i
);
1238 if (DECL_EXTERNAL (target_node
->decl
)
1239 /* We use local aliases for C++ thunks to force the tailcall
1240 to bind locally. This is a hack - to keep it working do
1241 the following (which is not strictly correct). */
1242 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1243 || ! DECL_VIRTUAL_P (target_node
->decl
))
1244 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1246 error ("%q+D aliased to external symbol %qE",
1247 p
->decl
, p
->target
);
1250 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1251 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1253 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1254 if (src_node
&& src_node
->definition
)
1256 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1257 alias_pairs
->unordered_remove (i
);
1259 else if (TREE_CODE (p
->decl
) == VAR_DECL
1260 && target_node
&& is_a
<varpool_node
*> (target_node
))
1262 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1263 alias_pairs
->unordered_remove (i
);
1267 error ("%q+D alias in between function and variable is not supported",
1269 warning (0, "%q+D aliased declaration",
1271 alias_pairs
->unordered_remove (i
);
1274 vec_free (alias_pairs
);
1278 /* Figure out what functions we want to assemble. */
1281 mark_functions_to_output (void)
1284 #ifdef ENABLE_CHECKING
1285 bool check_same_comdat_groups
= false;
1287 FOR_EACH_FUNCTION (node
)
1288 gcc_assert (!node
->process
);
1291 FOR_EACH_FUNCTION (node
)
1293 tree decl
= node
->decl
;
1295 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1299 /* We need to output all local functions that are used and not
1300 always inlined, as well as those that are reachable from
1301 outside the current compilation unit. */
1303 && !node
->thunk
.thunk_p
1305 && !node
->global
.inlined_to
1306 && !TREE_ASM_WRITTEN (decl
)
1307 && !DECL_EXTERNAL (decl
))
1310 if (node
->same_comdat_group
)
1313 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1315 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1316 if (!next
->thunk
.thunk_p
&& !next
->alias
1317 && !next
->comdat_local_p ())
1321 else if (node
->same_comdat_group
)
1323 #ifdef ENABLE_CHECKING
1324 check_same_comdat_groups
= true;
1329 /* We should've reclaimed all functions that are not needed. */
1330 #ifdef ENABLE_CHECKING
1331 if (!node
->global
.inlined_to
1332 && gimple_has_body_p (decl
)
1333 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1334 are inside partition, we can end up not removing the body since we no longer
1335 have analyzed node pointing to it. */
1336 && !node
->in_other_partition
1339 && !DECL_EXTERNAL (decl
))
1342 internal_error ("failed to reclaim unneeded function");
1345 gcc_assert (node
->global
.inlined_to
1346 || !gimple_has_body_p (decl
)
1347 || node
->in_other_partition
1349 || DECL_ARTIFICIAL (decl
)
1350 || DECL_EXTERNAL (decl
));
1355 #ifdef ENABLE_CHECKING
1356 if (check_same_comdat_groups
)
1357 FOR_EACH_FUNCTION (node
)
1358 if (node
->same_comdat_group
&& !node
->process
)
1360 tree decl
= node
->decl
;
1361 if (!node
->global
.inlined_to
1362 && gimple_has_body_p (decl
)
1363 /* FIXME: in an ltrans unit when the offline copy is outside a
1364 partition but inline copies are inside a partition, we can
1365 end up not removing the body since we no longer have an
1366 analyzed node pointing to it. */
1367 && !node
->in_other_partition
1369 && !DECL_EXTERNAL (decl
))
1372 internal_error ("failed to reclaim unneeded function in same "
1379 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1380 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1382 Set current_function_decl and cfun to newly constructed empty function body.
1383 return basic block in the function body. */
1386 init_lowered_empty_function (tree decl
, bool in_ssa
, gcov_type count
)
1391 current_function_decl
= decl
;
1392 allocate_struct_function (decl
, false);
1393 gimple_register_cfg_hooks ();
1394 init_empty_tree_cfg ();
1398 init_tree_ssa (cfun
);
1399 init_ssa_operands (cfun
);
1400 cfun
->gimple_df
->in_ssa_p
= true;
1401 cfun
->curr_properties
|= PROP_ssa
;
1404 DECL_INITIAL (decl
) = make_node (BLOCK
);
1406 DECL_SAVED_TREE (decl
) = error_mark_node
;
1407 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1408 | PROP_cfg
| PROP_loops
);
1410 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1411 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1412 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1414 /* Create BB for body of the function and connect it properly. */
1415 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1416 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1417 EXIT_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1418 EXIT_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1419 bb
= create_basic_block (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1421 bb
->frequency
= BB_FREQ_MAX
;
1422 e
= make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1424 e
->probability
= REG_BR_PROB_BASE
;
1425 e
= make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1427 e
->probability
= REG_BR_PROB_BASE
;
1428 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1433 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1434 offset indicated by VIRTUAL_OFFSET, if that is
1435 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1436 zero for a result adjusting thunk. */
1439 thunk_adjust (gimple_stmt_iterator
* bsi
,
1440 tree ptr
, bool this_adjusting
,
1441 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1447 && fixed_offset
!= 0)
1449 stmt
= gimple_build_assign
1450 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1453 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1456 /* If there's a virtual offset, look up that value in the vtable and
1457 adjust the pointer again. */
1464 if (!vtable_entry_type
)
1466 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1467 TREE_TYPE (vfunc_type
) = integer_type_node
;
1468 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1469 layout_type (vfunc_type
);
1471 vtable_entry_type
= build_pointer_type (vfunc_type
);
1475 create_tmp_reg (build_pointer_type
1476 (build_pointer_type (vtable_entry_type
)), "vptr");
1478 /* The vptr is always at offset zero in the object. */
1479 stmt
= gimple_build_assign (vtabletmp
,
1480 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1482 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1484 /* Form the vtable address. */
1485 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1487 stmt
= gimple_build_assign (vtabletmp2
,
1488 build_simple_mem_ref (vtabletmp
));
1489 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1491 /* Find the entry with the vcall offset. */
1492 stmt
= gimple_build_assign (vtabletmp2
,
1493 fold_build_pointer_plus_loc (input_location
,
1496 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1498 /* Get the offset itself. */
1499 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1501 stmt
= gimple_build_assign (vtabletmp3
,
1502 build_simple_mem_ref (vtabletmp2
));
1503 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1505 /* Adjust the `this' pointer. */
1506 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1507 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1508 GSI_CONTINUE_LINKING
);
1512 && fixed_offset
!= 0)
1513 /* Adjust the pointer by the constant. */
1517 if (TREE_CODE (ptr
) == VAR_DECL
)
1521 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1522 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1523 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1525 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1526 ptrtmp
, fixed_offset
);
1529 /* Emit the statement and gimplify the adjustment expression. */
1530 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1531 stmt
= gimple_build_assign (ret
, ptr
);
1532 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1537 /* Expand thunk NODE to gimple if possible.
1538 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1539 no assembler is produced.
1540 When OUTPUT_ASM_THUNK is true, also produce assembler for
1541 thunks that are not lowered. */
1544 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1546 bool this_adjusting
= thunk
.this_adjusting
;
1547 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1548 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1549 tree virtual_offset
= NULL
;
1550 tree alias
= callees
->callee
->decl
;
1551 tree thunk_fndecl
= decl
;
1554 /* Instrumentation thunk is the same function with
1555 a different signature. Never need to expand it. */
1556 if (thunk
.add_pointer_bounds_args
)
1559 if (!force_gimple_thunk
&& this_adjusting
1560 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1561 virtual_value
, alias
))
1565 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1567 if (!output_asm_thunks
)
1574 get_untransformed_body ();
1575 a
= DECL_ARGUMENTS (thunk_fndecl
);
1577 current_function_decl
= thunk_fndecl
;
1579 /* Ensure thunks are emitted in their correct sections. */
1580 resolve_unique_section (thunk_fndecl
, 0,
1581 flag_function_sections
);
1583 DECL_RESULT (thunk_fndecl
)
1584 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1585 RESULT_DECL
, 0, restype
);
1586 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1587 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1589 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1591 fn_block
= make_node (BLOCK
);
1592 BLOCK_VARS (fn_block
) = a
;
1593 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1594 init_function_start (thunk_fndecl
);
1596 insn_locations_init ();
1597 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1598 prologue_location
= curr_insn_location ();
1599 assemble_start_function (thunk_fndecl
, fnname
);
1601 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1602 fixed_offset
, virtual_value
, alias
);
1604 assemble_end_function (thunk_fndecl
, fnname
);
1605 insn_locations_finalize ();
1606 init_insn_lengths ();
1607 free_after_compilation (cfun
);
1609 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1610 thunk
.thunk_p
= false;
1613 else if (stdarg_p (TREE_TYPE (thunk_fndecl
)))
1615 error ("generic thunk code fails for method %qD which uses %<...%>",
1617 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1624 basic_block bb
, then_bb
, else_bb
, return_bb
;
1625 gimple_stmt_iterator bsi
;
1635 bool alias_is_noreturn
= TREE_THIS_VOLATILE (alias
);
1638 get_untransformed_body ();
1639 a
= DECL_ARGUMENTS (thunk_fndecl
);
1641 current_function_decl
= thunk_fndecl
;
1643 /* Ensure thunks are emitted in their correct sections. */
1644 resolve_unique_section (thunk_fndecl
, 0,
1645 flag_function_sections
);
1647 DECL_IGNORED_P (thunk_fndecl
) = 1;
1648 bitmap_obstack_initialize (NULL
);
1650 if (thunk
.virtual_offset_p
)
1651 virtual_offset
= size_int (virtual_value
);
1653 /* Build the return declaration for the function. */
1654 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1655 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1657 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1658 DECL_ARTIFICIAL (resdecl
) = 1;
1659 DECL_IGNORED_P (resdecl
) = 1;
1660 DECL_RESULT (thunk_fndecl
) = resdecl
;
1661 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1664 resdecl
= DECL_RESULT (thunk_fndecl
);
1666 bb
= then_bb
= else_bb
= return_bb
1667 = init_lowered_empty_function (thunk_fndecl
, true, count
);
1669 bsi
= gsi_start_bb (bb
);
1671 /* Build call to the function being thunked. */
1672 if (!VOID_TYPE_P (restype
) && !alias_is_noreturn
)
1674 if (DECL_BY_REFERENCE (resdecl
))
1676 restmp
= gimple_fold_indirect_ref (resdecl
);
1678 restmp
= build2 (MEM_REF
,
1679 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias
))),
1681 build_int_cst (TREE_TYPE
1682 (DECL_RESULT (alias
)), 0));
1684 else if (!is_gimple_reg_type (restype
))
1686 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
)))
1690 if (TREE_CODE (restmp
) == VAR_DECL
)
1691 add_local_decl (cfun
, restmp
);
1692 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1695 restmp
= create_tmp_var (restype
, "retval");
1698 restmp
= create_tmp_reg (restype
, "retval");
1701 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1703 auto_vec
<tree
> vargs (nargs
);
1708 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1710 arg
= DECL_CHAIN (a
);
1715 for (; i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1718 if (!is_gimple_val (arg
))
1720 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1721 (TREE_TYPE (arg
)), "arg");
1722 gimple stmt
= gimple_build_assign (tmp
, arg
);
1723 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1725 vargs
.quick_push (tmp
);
1727 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1728 callees
->call_stmt
= call
;
1729 gimple_call_set_from_thunk (call
, true);
1730 gimple_call_set_with_bounds (call
, instrumentation_clone
);
1732 /* Return slot optimization is always possible and in fact requred to
1733 return values with DECL_BY_REFERENCE. */
1734 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
))
1735 && (!is_gimple_reg_type (TREE_TYPE (resdecl
))
1736 || DECL_BY_REFERENCE (resdecl
)))
1737 gimple_call_set_return_slot_opt (call
, true);
1739 if (restmp
&& !alias_is_noreturn
)
1741 gimple_call_set_lhs (call
, restmp
);
1742 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1743 TREE_TYPE (TREE_TYPE (alias
))));
1745 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1746 if (!alias_is_noreturn
)
1748 if (instrumentation_clone
1749 && !DECL_BY_REFERENCE (resdecl
)
1751 && BOUNDED_P (restmp
))
1753 resbnd
= chkp_insert_retbnd_call (NULL
, restmp
, &bsi
);
1754 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi
))),
1755 as_a
<gcall
*> (gsi_stmt (bsi
)),
1756 callees
->count
, callees
->frequency
);
1759 if (restmp
&& !this_adjusting
1760 && (fixed_offset
|| virtual_offset
))
1762 tree true_label
= NULL_TREE
;
1764 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1768 /* If the return type is a pointer, we need to
1769 protect against NULL. We know there will be an
1770 adjustment, because that's why we're emitting a
1772 then_bb
= create_basic_block (NULL
, bb
);
1773 then_bb
->count
= count
- count
/ 16;
1774 then_bb
->frequency
= BB_FREQ_MAX
- BB_FREQ_MAX
/ 16;
1775 return_bb
= create_basic_block (NULL
, then_bb
);
1776 return_bb
->count
= count
;
1777 return_bb
->frequency
= BB_FREQ_MAX
;
1778 else_bb
= create_basic_block (NULL
, else_bb
);
1779 then_bb
->count
= count
/ 16;
1780 then_bb
->frequency
= BB_FREQ_MAX
/ 16;
1781 add_bb_to_loop (then_bb
, bb
->loop_father
);
1782 add_bb_to_loop (return_bb
, bb
->loop_father
);
1783 add_bb_to_loop (else_bb
, bb
->loop_father
);
1784 remove_edge (single_succ_edge (bb
));
1785 true_label
= gimple_block_label (then_bb
);
1786 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1787 build_zero_cst (TREE_TYPE (restmp
)),
1788 NULL_TREE
, NULL_TREE
);
1789 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1790 e
= make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1791 e
->probability
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 16;
1792 e
->count
= count
- count
/ 16;
1793 e
= make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1794 e
->probability
= REG_BR_PROB_BASE
/ 16;
1795 e
->count
= count
/ 16;
1796 e
= make_edge (return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1797 e
->probability
= REG_BR_PROB_BASE
;
1799 e
= make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1800 e
->probability
= REG_BR_PROB_BASE
;
1801 e
->count
= count
- count
/ 16;
1802 e
= make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1803 e
->probability
= REG_BR_PROB_BASE
;
1804 e
->count
= count
/ 16;
1805 bsi
= gsi_last_bb (then_bb
);
1808 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1809 fixed_offset
, virtual_offset
);
1813 bsi
= gsi_last_bb (else_bb
);
1814 stmt
= gimple_build_assign (restmp
,
1815 build_zero_cst (TREE_TYPE (restmp
)));
1816 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1817 bsi
= gsi_last_bb (return_bb
);
1821 gimple_call_set_tail (call
, true);
1823 /* Build return value. */
1824 if (!DECL_BY_REFERENCE (resdecl
))
1825 ret
= gimple_build_return (restmp
);
1827 ret
= gimple_build_return (resdecl
);
1828 gimple_return_set_retbnd (ret
, resbnd
);
1830 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1834 gimple_call_set_tail (call
, true);
1835 remove_edge (single_succ_edge (bb
));
1838 cfun
->gimple_df
->in_ssa_p
= true;
1839 profile_status_for_fn (cfun
)
1840 = count
? PROFILE_READ
: PROFILE_GUESSED
;
1841 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1842 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
1843 delete_unreachable_blocks ();
1844 update_ssa (TODO_update_ssa
);
1845 #ifdef ENABLE_CHECKING
1846 verify_flow_info ();
1848 free_dominance_info (CDI_DOMINATORS
);
1850 /* Since we want to emit the thunk, we explicitly mark its name as
1852 thunk
.thunk_p
= false;
1854 bitmap_obstack_release (NULL
);
1856 current_function_decl
= NULL
;
1861 /* Assemble thunks and aliases associated to node. */
1864 cgraph_node::assemble_thunks_and_aliases (void)
1869 for (e
= callers
; e
;)
1870 if (e
->caller
->thunk
.thunk_p
1871 && !e
->caller
->thunk
.add_pointer_bounds_args
)
1873 cgraph_node
*thunk
= e
->caller
;
1876 thunk
->expand_thunk (true, false);
1877 thunk
->assemble_thunks_and_aliases ();
1882 FOR_EACH_ALIAS (this, ref
)
1884 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1885 bool saved_written
= TREE_ASM_WRITTEN (decl
);
1887 /* Force assemble_alias to really output the alias this time instead
1888 of buffering it in same alias pairs. */
1889 TREE_ASM_WRITTEN (decl
) = 1;
1890 do_assemble_alias (alias
->decl
,
1891 DECL_ASSEMBLER_NAME (decl
));
1892 alias
->assemble_thunks_and_aliases ();
1893 TREE_ASM_WRITTEN (decl
) = saved_written
;
1897 /* Expand function specified by node. */
1900 cgraph_node::expand (void)
1902 location_t saved_loc
;
1904 /* We ought to not compile any inline clones. */
1905 gcc_assert (!global
.inlined_to
);
1907 announce_function (decl
);
1909 gcc_assert (lowered
);
1910 get_untransformed_body ();
1912 /* Generate RTL for the body of DECL. */
1914 timevar_push (TV_REST_OF_COMPILATION
);
1916 gcc_assert (symtab
->global_info_ready
);
1918 /* Initialize the default bitmap obstack. */
1919 bitmap_obstack_initialize (NULL
);
1921 /* Initialize the RTL code for the function. */
1922 current_function_decl
= decl
;
1923 saved_loc
= input_location
;
1924 input_location
= DECL_SOURCE_LOCATION (decl
);
1925 init_function_start (decl
);
1927 gimple_register_cfg_hooks ();
1929 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1931 execute_all_ipa_transforms ();
1933 /* Perform all tree transforms and optimizations. */
1935 /* Signal the start of passes. */
1936 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1938 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
1940 /* Signal the end of passes. */
1941 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1943 bitmap_obstack_release (®_obstack
);
1945 /* Release the default bitmap obstack. */
1946 bitmap_obstack_release (NULL
);
1948 /* If requested, warn about function definitions where the function will
1949 return a value (usually of some struct or union type) which itself will
1950 take up a lot of stack space. */
1951 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1953 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1955 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1956 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1957 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1960 unsigned int size_as_int
1961 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1963 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1964 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
1967 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
1968 decl
, larger_than_size
);
1972 gimple_set_body (decl
, NULL
);
1973 if (DECL_STRUCT_FUNCTION (decl
) == 0
1974 && !cgraph_node::get (decl
)->origin
)
1976 /* Stop pointing to the local nodes about to be freed.
1977 But DECL_INITIAL must remain nonzero so we know this
1978 was an actual function definition.
1979 For a nested function, this is done in c_pop_function_context.
1980 If rest_of_compilation set this to 0, leave it 0. */
1981 if (DECL_INITIAL (decl
) != 0)
1982 DECL_INITIAL (decl
) = error_mark_node
;
1985 input_location
= saved_loc
;
1988 timevar_pop (TV_REST_OF_COMPILATION
);
1990 /* Make sure that BE didn't give up on compiling. */
1991 gcc_assert (TREE_ASM_WRITTEN (decl
));
1993 current_function_decl
= NULL
;
1995 /* It would make a lot more sense to output thunks before function body to get more
1996 forward and lest backwarding jumps. This however would need solving problem
1997 with comdats. See PR48668. Also aliases must come after function itself to
1998 make one pass assemblers, like one on AIX, happy. See PR 50689.
1999 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2001 assemble_thunks_and_aliases ();
2003 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2004 points to the dead function body. */
2006 remove_all_references ();
2009 /* Node comparer that is responsible for the order that corresponds
2010 to time when a function was launched for the first time. */
2013 node_cmp (const void *pa
, const void *pb
)
2015 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
2016 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
2018 /* Functions with time profile must be before these without profile. */
2019 if (!a
->tp_first_run
|| !b
->tp_first_run
)
2020 return a
->tp_first_run
- b
->tp_first_run
;
2022 return a
->tp_first_run
!= b
->tp_first_run
2023 ? b
->tp_first_run
- a
->tp_first_run
2024 : b
->order
- a
->order
;
2027 /* Expand all functions that must be output.
2029 Attempt to topologically sort the nodes so function is output when
2030 all called functions are already assembled to allow data to be
2031 propagated across the callgraph. Use a stack to get smaller distance
2032 between a function and its callees (later we may choose to use a more
2033 sophisticated algorithm for function reordering; we will likely want
2034 to use subsections to make the output functions appear in top-down
2038 expand_all_functions (void)
2041 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
2042 symtab
->cgraph_count
);
2043 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
2044 int order_pos
, new_order_pos
= 0;
2047 order_pos
= ipa_reverse_postorder (order
);
2048 gcc_assert (order_pos
== symtab
->cgraph_count
);
2050 /* Garbage collector may remove inline clones we eliminate during
2051 optimization. So we must be sure to not reference them. */
2052 for (i
= 0; i
< order_pos
; i
++)
2053 if (order
[i
]->process
)
2054 order
[new_order_pos
++] = order
[i
];
2056 if (flag_profile_reorder_functions
)
2057 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
2059 for (i
= new_order_pos
- 1; i
>= 0; i
--)
2065 expanded_func_count
++;
2066 if(node
->tp_first_run
)
2067 profiled_func_count
++;
2069 if (symtab
->dump_file
)
2070 fprintf (symtab
->dump_file
,
2071 "Time profile order in expand_all_functions:%s:%d\n",
2072 node
->asm_name (), node
->tp_first_run
);
2079 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
2080 main_input_filename
, profiled_func_count
, expanded_func_count
);
2082 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
2083 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
2084 profiled_func_count
, expanded_func_count
);
2086 symtab
->process_new_functions ();
2087 free_gimplify_stack ();
2092 /* This is used to sort the node types by the cgraph order number. */
2094 enum cgraph_order_sort_kind
2096 ORDER_UNDEFINED
= 0,
2102 struct cgraph_order_sort
2104 enum cgraph_order_sort_kind kind
;
2113 /* Output all functions, variables, and asm statements in the order
2114 according to their order fields, which is the order in which they
2115 appeared in the file. This implements -fno-toplevel-reorder. In
2116 this mode we may output functions and variables which don't really
2118 When NO_REORDER is true only do this for symbols marked no reorder. */
2121 output_in_order (bool no_reorder
)
2124 cgraph_order_sort
*nodes
;
2129 max
= symtab
->order
;
2130 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
2132 FOR_EACH_DEFINED_FUNCTION (pf
)
2134 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
2136 if (no_reorder
&& !pf
->no_reorder
)
2139 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2140 nodes
[i
].kind
= ORDER_FUNCTION
;
2145 FOR_EACH_DEFINED_VARIABLE (pv
)
2146 if (!DECL_EXTERNAL (pv
->decl
))
2148 if (no_reorder
&& !pv
->no_reorder
)
2151 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2152 nodes
[i
].kind
= ORDER_VAR
;
2156 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
2159 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2160 nodes
[i
].kind
= ORDER_ASM
;
2164 /* In toplevel reorder mode we output all statics; mark them as needed. */
2166 for (i
= 0; i
< max
; ++i
)
2167 if (nodes
[i
].kind
== ORDER_VAR
)
2168 nodes
[i
].u
.v
->finalize_named_section_flags ();
2170 for (i
= 0; i
< max
; ++i
)
2172 switch (nodes
[i
].kind
)
2174 case ORDER_FUNCTION
:
2175 nodes
[i
].u
.f
->process
= 0;
2176 nodes
[i
].u
.f
->expand ();
2180 nodes
[i
].u
.v
->assemble_decl ();
2184 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2187 case ORDER_UNDEFINED
:
2195 symtab
->clear_asm_symbols ();
2203 gcc::pass_manager
*passes
= g
->get_passes ();
2206 current_function_decl
= NULL
;
2207 gimple_register_cfg_hooks ();
2208 bitmap_obstack_initialize (NULL
);
2210 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2214 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2219 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2220 devirtualization and other changes where removal iterate. */
2221 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2223 /* If pass_all_early_optimizations was not scheduled, the state of
2224 the cgraph will not be properly updated. Update it now. */
2225 if (symtab
->state
< IPA_SSA
)
2226 symtab
->state
= IPA_SSA
;
2230 /* Generate coverage variables and constructors. */
2233 /* Process new functions added. */
2235 current_function_decl
= NULL
;
2236 symtab
->process_new_functions ();
2238 execute_ipa_summary_passes
2239 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2242 /* Some targets need to handle LTO assembler output specially. */
2243 if (flag_generate_lto
|| flag_generate_offload
)
2244 targetm
.asm_out
.lto_start ();
2248 if (g
->have_offload
)
2250 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2251 lto_stream_offload_p
= true;
2252 ipa_write_summaries ();
2253 lto_stream_offload_p
= false;
2257 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2258 lto_stream_offload_p
= false;
2259 ipa_write_summaries ();
2263 if (flag_generate_lto
|| flag_generate_offload
)
2264 targetm
.asm_out
.lto_end ();
2266 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2267 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2268 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2270 bitmap_obstack_release (NULL
);
2274 /* Return string alias is alias of. */
2277 get_alias_symbol (tree decl
)
2279 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2280 return get_identifier (TREE_STRING_POINTER
2281 (TREE_VALUE (TREE_VALUE (alias
))));
2285 /* Weakrefs may be associated to external decls and thus not output
2286 at expansion time. Emit all necessary aliases. */
2289 symbol_table::output_weakrefs (void)
2293 FOR_EACH_SYMBOL (node
)
2295 && !TREE_ASM_WRITTEN (node
->decl
)
2296 && (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
2297 || !cnode
->instrumented_version
2298 || !TREE_ASM_WRITTEN (cnode
->instrumented_version
->decl
))
2303 /* Weakrefs are special by not requiring target definition in current
2304 compilation unit. It is thus bit hard to work out what we want to
2306 When alias target is defined, we need to fetch it from symtab reference,
2307 otherwise it is pointed to by alias_target. */
2308 if (node
->alias_target
)
2309 target
= (DECL_P (node
->alias_target
)
2310 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2311 : node
->alias_target
);
2312 else if (node
->analyzed
)
2313 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2317 target
= get_alias_symbol (node
->decl
);
2319 do_assemble_alias (node
->decl
, target
);
2323 /* Perform simple optimizations based on callgraph. */
2326 symbol_table::compile (void)
2331 #ifdef ENABLE_CHECKING
2332 symtab_node::verify_symtab_nodes ();
2335 timevar_push (TV_CGRAPHOPT
);
2336 if (pre_ipa_mem_report
)
2338 fprintf (stderr
, "Memory consumption before IPA\n");
2339 dump_memory_report (false);
2342 fprintf (stderr
, "Performing interprocedural optimizations\n");
2345 /* Offloading requires LTO infrastructure. */
2346 if (!in_lto_p
&& g
->have_offload
)
2347 flag_generate_offload
= 1;
2349 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2350 if (flag_generate_lto
|| flag_generate_offload
)
2351 lto_streamer_hooks_init ();
2353 /* Don't run the IPA passes if there was any error or sorry messages. */
2357 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2359 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2361 timevar_pop (TV_CGRAPHOPT
);
2365 global_info_ready
= true;
2368 fprintf (dump_file
, "Optimized ");
2369 symtab_node:: dump_table (dump_file
);
2371 if (post_ipa_mem_report
)
2373 fprintf (stderr
, "Memory consumption after IPA\n");
2374 dump_memory_report (false);
2376 timevar_pop (TV_CGRAPHOPT
);
2378 /* Output everything. */
2379 (*debug_hooks
->assembly_start
) ();
2381 fprintf (stderr
, "Assembling functions:\n");
2382 #ifdef ENABLE_CHECKING
2383 symtab_node::verify_symtab_nodes ();
2386 materialize_all_clones ();
2387 bitmap_obstack_initialize (NULL
);
2388 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2389 bitmap_obstack_release (NULL
);
2390 mark_functions_to_output ();
2392 /* When weakref support is missing, we autmatically translate all
2393 references to NODE to references to its ultimate alias target.
2394 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2397 Set up this mapping before we output any assembler but once we are sure
2398 that all symbol renaming is done.
2400 FIXME: All this uglyness can go away if we just do renaming at gimple
2401 level by physically rewritting the IL. At the moment we can only redirect
2402 calls, so we need infrastructure for renaming references as well. */
2403 #ifndef ASM_OUTPUT_WEAKREF
2406 FOR_EACH_SYMBOL (node
)
2408 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2410 IDENTIFIER_TRANSPARENT_ALIAS
2411 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2412 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2413 = (node
->alias_target
? node
->alias_target
2414 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2420 if (!flag_toplevel_reorder
)
2421 output_in_order (false);
2424 /* Output first asm statements and anything ordered. The process
2425 flag is cleared for these nodes, so we skip them later. */
2426 output_in_order (true);
2427 expand_all_functions ();
2428 output_variables ();
2431 process_new_functions ();
2437 fprintf (dump_file
, "\nFinal ");
2438 symtab_node::dump_table (dump_file
);
2440 #ifdef ENABLE_CHECKING
2441 symtab_node::verify_symtab_nodes ();
2442 /* Double check that all inline clones are gone and that all
2443 function bodies have been released from memory. */
2447 bool error_found
= false;
2449 FOR_EACH_DEFINED_FUNCTION (node
)
2450 if (node
->global
.inlined_to
2451 || gimple_has_body_p (node
->decl
))
2457 internal_error ("nodes with unreleased memory found");
2463 /* Analyze the whole compilation unit once it is parsed completely. */
2466 symbol_table::finalize_compilation_unit (void)
2468 timevar_push (TV_CGRAPH
);
2470 /* If we're here there's no current function anymore. Some frontends
2471 are lazy in clearing these. */
2472 current_function_decl
= NULL
;
2475 /* Do not skip analyzing the functions if there were errors, we
2476 miss diagnostics for following functions otherwise. */
2478 /* Emit size functions we didn't inline. */
2479 finalize_size_functions ();
2481 /* Mark alias targets necessary and emit diagnostics. */
2482 handle_alias_pairs ();
2486 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2490 if (flag_dump_passes
)
2493 /* Gimplify and lower all functions, compute reachability and
2494 remove unreachable nodes. */
2495 analyze_functions (/*first_time=*/true);
2497 /* Mark alias targets necessary and emit diagnostics. */
2498 handle_alias_pairs ();
2500 /* Gimplify and lower thunks. */
2501 analyze_functions (/*first_time=*/false);
2503 /* Emit early debug for reachable functions, and by consequence,
2504 locally scoped symbols. */
2505 struct cgraph_node
*cnode
;
2506 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode
)
2507 (*debug_hooks
->early_global_decl
) (cnode
->decl
);
2509 /* Clean up anything that needs cleaning up after initial debug
2511 (*debug_hooks
->early_finish
) ();
2513 /* Finally drive the pass manager. */
2516 timevar_pop (TV_CGRAPH
);
2519 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2520 within the same process. For use by toplev::finalize. */
2523 cgraphunit_c_finalize (void)
2525 gcc_assert (cgraph_new_nodes
.length () == 0);
2526 cgraph_new_nodes
.truncate (0);
2528 vtable_entry_type
= NULL
;
2529 queued_nodes
= &symtab_terminator
;
2531 first_analyzed
= NULL
;
2532 first_analyzed_var
= NULL
;
2535 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2536 kind of wrapper method. */
2539 cgraph_node::create_wrapper (cgraph_node
*target
)
2541 /* Preserve DECL_RESULT so we get right by reference flag. */
2542 tree decl_result
= DECL_RESULT (decl
);
2544 /* Remove the function's body but keep arguments to be reused
2546 release_body (true);
2549 DECL_UNINLINABLE (decl
) = false;
2550 DECL_RESULT (decl
) = decl_result
;
2551 DECL_INITIAL (decl
) = NULL
;
2552 allocate_struct_function (decl
, false);
2555 /* Turn alias into thunk and expand it into GIMPLE representation. */
2558 memset (&thunk
, 0, sizeof (cgraph_thunk_info
));
2559 thunk
.thunk_p
= true;
2560 create_edge (target
, NULL
, count
, CGRAPH_FREQ_BASE
);
2562 tree arguments
= DECL_ARGUMENTS (decl
);
2566 TREE_ADDRESSABLE (arguments
) = false;
2567 arguments
= TREE_CHAIN (arguments
);
2570 expand_thunk (false, true);
2572 /* Inline summary set-up. */
2574 inline_analyze_function (this);
2577 #include "gt-cgraphunit.h"