1 /* Driver of optimization process
2 Copyright (C) 2003-2016 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"
168 #include "cfghooks.h"
169 #include "regset.h" /* FIXME: For reg_obstack. */
170 #include "alloc-pool.h"
171 #include "tree-pass.h"
172 #include "stringpool.h"
173 #include "gimple-ssa.h"
175 #include "coverage.h"
176 #include "lto-streamer.h"
177 #include "fold-const.h"
179 #include "stor-layout.h"
181 #include "cfgcleanup.h"
182 #include "gimple-fold.h"
183 #include "gimplify.h"
184 #include "gimple-iterator.h"
185 #include "gimplify-me.h"
186 #include "tree-cfg.h"
187 #include "tree-into-ssa.h"
188 #include "tree-ssa.h"
189 #include "langhooks.h"
192 #include "symbol-summary.h"
193 #include "ipa-prop.h"
194 #include "gimple-pretty-print.h"
196 #include "ipa-inline.h"
197 #include "ipa-utils.h"
201 #include "pass_manager.h"
202 #include "tree-nested.h"
204 #include "tree-chkp.h"
205 #include "lto-section-names.h"
207 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
208 secondary queue used during optimization to accommodate passes that
209 may generate new functions that need to be optimized and expanded. */
210 vec
<cgraph_node
*> cgraph_new_nodes
;
212 static void expand_all_functions (void);
213 static void mark_functions_to_output (void);
214 static void handle_alias_pairs (void);
216 /* Used for vtable lookup in thunk adjusting. */
217 static GTY (()) tree vtable_entry_type
;
219 /* Determine if symbol declaration is needed. That is, visible to something
220 either outside this translation unit, something magic in the system
223 symtab_node::needed_p (void)
225 /* Double check that no one output the function into assembly file
227 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
228 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
233 if (DECL_EXTERNAL (decl
))
236 /* If the user told us it is used, then it must be so. */
240 /* ABI forced symbols are needed when they are external. */
241 if (forced_by_abi
&& TREE_PUBLIC (decl
))
244 /* Keep constructors, destructors and virtual functions. */
245 if (TREE_CODE (decl
) == FUNCTION_DECL
246 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
249 /* Externally visible variables must be output. The exception is
250 COMDAT variables that must be output only when they are needed. */
251 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
257 /* Head and terminator of the queue of nodes to be processed while building
260 static symtab_node symtab_terminator
;
261 static symtab_node
*queued_nodes
= &symtab_terminator
;
263 /* Add NODE to queue starting at QUEUED_NODES.
264 The queue is linked via AUX pointers and terminated by pointer to 1. */
267 enqueue_node (symtab_node
*node
)
271 gcc_checking_assert (queued_nodes
);
272 node
->aux
= queued_nodes
;
276 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
277 functions into callgraph in a way so they look like ordinary reachable
278 functions inserted into callgraph already at construction time. */
281 symbol_table::process_new_functions (void)
285 if (!cgraph_new_nodes
.exists ())
288 handle_alias_pairs ();
289 /* Note that this queue may grow as its being processed, as the new
290 functions may generate new ones. */
291 for (unsigned i
= 0; i
< cgraph_new_nodes
.length (); i
++)
293 cgraph_node
*node
= cgraph_new_nodes
[i
];
298 /* At construction time we just need to finalize function and move
299 it into reachable functions list. */
301 cgraph_node::finalize_function (fndecl
, false);
302 call_cgraph_insertion_hooks (node
);
308 case IPA_SSA_AFTER_INLINING
:
309 /* When IPA optimization already started, do all essential
310 transformations that has been already performed on the whole
311 cgraph but not on this function. */
313 gimple_register_cfg_hooks ();
316 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
317 if ((state
== IPA_SSA
|| state
== IPA_SSA_AFTER_INLINING
)
318 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
319 g
->get_passes ()->execute_early_local_passes ();
320 else if (inline_summaries
!= NULL
)
321 compute_inline_parameters (node
, true);
322 free_dominance_info (CDI_POST_DOMINATORS
);
323 free_dominance_info (CDI_DOMINATORS
);
325 call_cgraph_insertion_hooks (node
);
329 /* Functions created during expansion shall be compiled
332 call_cgraph_insertion_hooks (node
);
342 cgraph_new_nodes
.release ();
345 /* As an GCC extension we allow redefinition of the function. The
346 semantics when both copies of bodies differ is not well defined.
347 We replace the old body with new body so in unit at a time mode
348 we always use new body, while in normal mode we may end up with
349 old body inlined into some functions and new body expanded and
352 ??? It may make more sense to use one body for inlining and other
353 body for expanding the function but this is difficult to do. */
356 cgraph_node::reset (void)
358 /* If process is set, then we have already begun whole-unit analysis.
359 This is *not* testing for whether we've already emitted the function.
360 That case can be sort-of legitimately seen with real function redefinition
361 errors. I would argue that the front end should never present us with
362 such a case, but don't enforce that for now. */
363 gcc_assert (!process
);
365 /* Reset our data structures so we can analyze the function again. */
366 memset (&local
, 0, sizeof (local
));
367 memset (&global
, 0, sizeof (global
));
368 memset (&rtl
, 0, sizeof (rtl
));
372 transparent_alias
= false;
374 cpp_implicit_alias
= false;
377 remove_all_references ();
380 /* Return true when there are references to the node. INCLUDE_SELF is
381 true if a self reference counts as a reference. */
384 symtab_node::referred_to_p (bool include_self
)
388 /* See if there are any references at all. */
389 if (iterate_referring (0, ref
))
391 /* For functions check also calls. */
392 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
393 if (cn
&& cn
->callers
)
397 for (cgraph_edge
*e
= cn
->callers
; e
; e
= e
->next_caller
)
398 if (e
->caller
!= this)
404 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
405 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
406 the garbage collector run at the moment. We would need to either create
407 a new GC context, or just not compile right now. */
410 cgraph_node::finalize_function (tree decl
, bool no_collect
)
412 cgraph_node
*node
= cgraph_node::get_create (decl
);
414 if (node
->definition
)
416 /* Nested functions should only be defined once. */
417 gcc_assert (!DECL_CONTEXT (decl
)
418 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
420 node
->local
.redefined_extern_inline
= true;
423 /* Set definition first before calling notice_global_symbol so that
424 it is available to notice_global_symbol. */
425 node
->definition
= true;
426 notice_global_symbol (decl
);
427 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
429 /* With -fkeep-inline-functions we are keeping all inline functions except
430 for extern inline ones. */
431 if (flag_keep_inline_functions
432 && DECL_DECLARED_INLINE_P (decl
)
433 && !DECL_EXTERNAL (decl
)
434 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
435 node
->force_output
= 1;
437 /* When not optimizing, also output the static functions. (see
438 PR24561), but don't do so for always_inline functions, functions
439 declared inline and nested functions. These were optimized out
440 in the original implementation and it is unclear whether we want
441 to change the behavior here. */
442 if (((!opt_for_fn (decl
, optimize
) || flag_keep_static_functions
)
443 && !node
->cpp_implicit_alias
444 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
445 && !DECL_DECLARED_INLINE_P (decl
)
446 && !(DECL_CONTEXT (decl
)
447 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
448 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
449 node
->force_output
= 1;
451 /* If we've not yet emitted decl, tell the debug info about it. */
452 if (!TREE_ASM_WRITTEN (decl
))
453 (*debug_hooks
->deferred_inline_function
) (decl
);
458 if (symtab
->state
== CONSTRUCTION
459 && (node
->needed_p () || node
->referred_to_p ()))
463 /* Add the function FNDECL to the call graph.
464 Unlike finalize_function, this function is intended to be used
465 by middle end and allows insertion of new function at arbitrary point
466 of compilation. The function can be either in high, low or SSA form
469 The function is assumed to be reachable and have address taken (so no
470 API breaking optimizations are performed on it).
472 Main work done by this function is to enqueue the function for later
473 processing to avoid need the passes to be re-entrant. */
476 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
478 gcc::pass_manager
*passes
= g
->get_passes ();
483 struct function
*fn
= DECL_STRUCT_FUNCTION (fndecl
);
484 const char *function_type
= ((gimple_has_body_p (fndecl
))
486 ? (gimple_in_ssa_p (fn
)
490 : "to-be-gimplified");
492 "Added new %s function %s to callgraph\n",
494 fndecl_name (fndecl
));
497 switch (symtab
->state
)
500 cgraph_node::finalize_function (fndecl
, false);
503 /* Just enqueue function to be processed at nearest occurrence. */
504 node
= cgraph_node::get_create (fndecl
);
506 node
->lowered
= true;
507 cgraph_new_nodes
.safe_push (node
);
512 case IPA_SSA_AFTER_INLINING
:
514 /* Bring the function into finalized state and enqueue for later
515 analyzing and compilation. */
516 node
= cgraph_node::get_create (fndecl
);
517 node
->local
.local
= false;
518 node
->definition
= true;
519 node
->force_output
= true;
520 if (!lowered
&& symtab
->state
== EXPANSION
)
522 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
523 gimple_register_cfg_hooks ();
524 bitmap_obstack_initialize (NULL
);
525 execute_pass_list (cfun
, passes
->all_lowering_passes
);
526 passes
->execute_early_local_passes ();
527 bitmap_obstack_release (NULL
);
533 node
->lowered
= true;
534 cgraph_new_nodes
.safe_push (node
);
538 /* At the very end of compilation we have to do all the work up
540 node
= cgraph_node::create (fndecl
);
542 node
->lowered
= true;
543 node
->definition
= true;
545 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
546 gimple_register_cfg_hooks ();
547 bitmap_obstack_initialize (NULL
);
548 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
549 g
->get_passes ()->execute_early_local_passes ();
550 bitmap_obstack_release (NULL
);
559 /* Set a personality if required and we already passed EH lowering. */
561 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
562 == eh_personality_lang
))
563 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
566 /* Analyze the function scheduled to be output. */
568 cgraph_node::analyze (void)
570 tree decl
= this->decl
;
571 location_t saved_loc
= input_location
;
572 input_location
= DECL_SOURCE_LOCATION (decl
);
576 cgraph_node
*t
= cgraph_node::get (thunk
.alias
);
578 create_edge (t
, NULL
, 0, CGRAPH_FREQ_BASE
);
579 callees
->can_throw_external
= !TREE_NOTHROW (t
->decl
);
580 /* Target code in expand_thunk may need the thunk's target
581 to be analyzed, so recurse here. */
586 t
= t
->get_alias_target ();
590 if (!expand_thunk (false, false))
598 resolve_alias (cgraph_node::get (alias_target
), transparent_alias
);
599 else if (dispatcher_function
)
601 /* Generate the dispatcher body of multi-versioned functions. */
602 cgraph_function_version_info
*dispatcher_version_info
603 = function_version ();
604 if (dispatcher_version_info
!= NULL
605 && (dispatcher_version_info
->dispatcher_resolver
608 tree resolver
= NULL_TREE
;
609 gcc_assert (targetm
.generate_version_dispatcher_body
);
610 resolver
= targetm
.generate_version_dispatcher_body (this);
611 gcc_assert (resolver
!= NULL_TREE
);
616 push_cfun (DECL_STRUCT_FUNCTION (decl
));
618 assign_assembler_name_if_neeeded (decl
);
620 /* Make sure to gimplify bodies only once. During analyzing a
621 function we lower it, which will require gimplified nested
622 functions, so we can end up here with an already gimplified
624 if (!gimple_has_body_p (decl
))
625 gimplify_function_tree (decl
);
627 /* Lower the function. */
631 lower_nested_functions (decl
);
632 gcc_assert (!nested
);
634 gimple_register_cfg_hooks ();
635 bitmap_obstack_initialize (NULL
);
636 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
637 free_dominance_info (CDI_POST_DOMINATORS
);
638 free_dominance_info (CDI_DOMINATORS
);
640 bitmap_obstack_release (NULL
);
648 input_location
= saved_loc
;
651 /* C++ frontend produce same body aliases all over the place, even before PCH
652 gets streamed out. It relies on us linking the aliases with their function
653 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
654 first produce aliases without links, but once C++ FE is sure he won't sream
655 PCH we build the links via this function. */
658 symbol_table::process_same_body_aliases (void)
661 FOR_EACH_SYMBOL (node
)
662 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
664 (TREE_CODE (node
->alias_target
) == VAR_DECL
665 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
666 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
667 cpp_implicit_aliases_done
= true;
670 /* Process attributes common for vars and functions. */
673 process_common_attributes (symtab_node
*node
, tree decl
)
675 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
677 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
679 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
680 "%<weakref%> attribute should be accompanied with"
681 " an %<alias%> attribute");
682 DECL_WEAK (decl
) = 0;
683 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
684 DECL_ATTRIBUTES (decl
));
687 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
688 node
->no_reorder
= 1;
691 /* Look for externally_visible and used attributes and mark cgraph nodes
694 We cannot mark the nodes at the point the attributes are processed (in
695 handle_*_attribute) because the copy of the declarations available at that
696 point may not be canonical. For example, in:
699 void f() __attribute__((used));
701 the declaration we see in handle_used_attribute will be the second
702 declaration -- but the front end will subsequently merge that declaration
703 with the original declaration and discard the second declaration.
705 Furthermore, we can't mark these nodes in finalize_function because:
708 void f() __attribute__((externally_visible));
712 So, we walk the nodes at the end of the translation unit, applying the
713 attributes at that point. */
716 process_function_and_variable_attributes (cgraph_node
*first
,
717 varpool_node
*first_var
)
722 for (node
= symtab
->first_function (); node
!= first
;
723 node
= symtab
->next_function (node
))
725 tree decl
= node
->decl
;
726 if (DECL_PRESERVE_P (decl
))
727 node
->mark_force_output ();
728 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
730 if (! TREE_PUBLIC (node
->decl
))
731 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
732 "%<externally_visible%>"
733 " attribute have effect only on public objects");
735 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
736 && (node
->definition
&& !node
->alias
))
738 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
739 "%<weakref%> attribute ignored"
740 " because function is defined");
741 DECL_WEAK (decl
) = 0;
742 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
743 DECL_ATTRIBUTES (decl
));
746 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
747 && !DECL_DECLARED_INLINE_P (decl
)
748 /* redefining extern inline function makes it DECL_UNINLINABLE. */
749 && !DECL_UNINLINABLE (decl
))
750 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
751 "always_inline function might not be inlinable");
753 process_common_attributes (node
, decl
);
755 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
756 vnode
= symtab
->next_variable (vnode
))
758 tree decl
= vnode
->decl
;
759 if (DECL_EXTERNAL (decl
)
760 && DECL_INITIAL (decl
))
761 varpool_node::finalize_decl (decl
);
762 if (DECL_PRESERVE_P (decl
))
763 vnode
->force_output
= true;
764 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
766 if (! TREE_PUBLIC (vnode
->decl
))
767 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
768 "%<externally_visible%>"
769 " attribute have effect only on public objects");
771 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
773 && DECL_INITIAL (decl
))
775 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
776 "%<weakref%> attribute ignored"
777 " because variable is initialized");
778 DECL_WEAK (decl
) = 0;
779 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
780 DECL_ATTRIBUTES (decl
));
782 process_common_attributes (vnode
, decl
);
786 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
787 middle end to output the variable to asm file, if needed or externally
791 varpool_node::finalize_decl (tree decl
)
793 varpool_node
*node
= varpool_node::get_create (decl
);
795 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
797 if (node
->definition
)
799 /* Set definition first before calling notice_global_symbol so that
800 it is available to notice_global_symbol. */
801 node
->definition
= true;
802 notice_global_symbol (decl
);
803 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
804 /* Traditionally we do not eliminate static variables when not
805 optimizing and when not doing toplevel reoder. */
807 || ((!flag_toplevel_reorder
808 && !DECL_COMDAT (node
->decl
)
809 && !DECL_ARTIFICIAL (node
->decl
))))
810 node
->force_output
= true;
812 if (symtab
->state
== CONSTRUCTION
813 && (node
->needed_p () || node
->referred_to_p ()))
815 if (symtab
->state
>= IPA_SSA
)
817 /* Some frontends produce various interface variables after compilation
819 if (symtab
->state
== FINISHED
820 || (!flag_toplevel_reorder
821 && symtab
->state
== EXPANSION
))
822 node
->assemble_decl ();
824 if (DECL_INITIAL (decl
))
825 chkp_register_var_initializer (decl
);
828 /* EDGE is an polymorphic call. Mark all possible targets as reachable
829 and if there is only one target, perform trivial devirtualization.
830 REACHABLE_CALL_TARGETS collects target lists we already walked to
831 avoid udplicate work. */
834 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
840 vec
<cgraph_node
*>targets
841 = possible_polymorphic_call_targets
842 (edge
, &final
, &cache_token
);
844 if (!reachable_call_targets
->add (cache_token
))
846 if (symtab
->dump_file
)
847 dump_possible_polymorphic_call_targets
848 (symtab
->dump_file
, edge
);
850 for (i
= 0; i
< targets
.length (); i
++)
852 /* Do not bother to mark virtual methods in anonymous namespace;
853 either we will find use of virtual table defining it, or it is
855 if (targets
[i
]->definition
857 (TREE_TYPE (targets
[i
]->decl
))
859 && !type_in_anonymous_namespace_p
860 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets
[i
]->decl
))))
861 enqueue_node (targets
[i
]);
865 /* Very trivial devirtualization; when the type is
866 final or anonymous (so we know all its derivation)
867 and there is only one possible virtual call target,
868 make the edge direct. */
871 if (targets
.length () <= 1 && dbg_cnt (devirt
))
874 if (targets
.length () == 1)
877 target
= cgraph_node::create
878 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
880 if (symtab
->dump_file
)
882 fprintf (symtab
->dump_file
,
883 "Devirtualizing call: ");
884 print_gimple_stmt (symtab
->dump_file
,
888 if (dump_enabled_p ())
890 location_t locus
= gimple_location_safe (edge
->call_stmt
);
891 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
892 "devirtualizing call in %s to %s\n",
893 edge
->caller
->name (), target
->name ());
896 edge
->make_direct (target
);
897 edge
->redirect_call_stmt_to_callee ();
899 /* Call to __builtin_unreachable shouldn't be instrumented. */
900 if (!targets
.length ())
901 gimple_call_set_with_bounds (edge
->call_stmt
, false);
903 if (symtab
->dump_file
)
905 fprintf (symtab
->dump_file
,
906 "Devirtualized as: ");
907 print_gimple_stmt (symtab
->dump_file
,
915 /* Issue appropriate warnings for the global declaration DECL. */
918 check_global_declaration (symtab_node
*snode
)
920 tree decl
= snode
->decl
;
922 /* Warn about any function declared static but not defined. We don't
923 warn about variables, because many programs have static variables
924 that exist only to get some text into the object file. */
925 if (TREE_CODE (decl
) == FUNCTION_DECL
926 && DECL_INITIAL (decl
) == 0
927 && DECL_EXTERNAL (decl
)
928 && ! DECL_ARTIFICIAL (decl
)
929 && ! TREE_NO_WARNING (decl
)
930 && ! TREE_PUBLIC (decl
)
931 && (warn_unused_function
932 || snode
->referred_to_p (/*include_self=*/false)))
934 if (snode
->referred_to_p (/*include_self=*/false))
935 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
937 warning (OPT_Wunused_function
, "%q+F declared %<static%> but never defined", decl
);
938 /* This symbol is effectively an "extern" declaration now. */
939 TREE_PUBLIC (decl
) = 1;
942 /* Warn about static fns or vars defined but not used. */
943 if (((warn_unused_function
&& TREE_CODE (decl
) == FUNCTION_DECL
)
944 || (((warn_unused_variable
&& ! TREE_READONLY (decl
))
945 || (warn_unused_const_variable
&& TREE_READONLY (decl
)))
946 && TREE_CODE (decl
) == VAR_DECL
))
947 && ! DECL_IN_SYSTEM_HEADER (decl
)
948 && ! snode
->referred_to_p (/*include_self=*/false)
949 /* This TREE_USED check is needed in addition to referred_to_p
950 above, because the `__unused__' attribute is not being
951 considered for referred_to_p. */
952 && ! TREE_USED (decl
)
953 /* The TREE_USED bit for file-scope decls is kept in the identifier,
954 to handle multiple external decls in different scopes. */
955 && ! (DECL_NAME (decl
) && TREE_USED (DECL_NAME (decl
)))
956 && ! DECL_EXTERNAL (decl
)
957 && ! DECL_ARTIFICIAL (decl
)
958 && ! DECL_ABSTRACT_ORIGIN (decl
)
959 && ! TREE_PUBLIC (decl
)
960 /* A volatile variable might be used in some non-obvious way. */
961 && (! VAR_P (decl
) || ! TREE_THIS_VOLATILE (decl
))
962 /* Global register variables must be declared to reserve them. */
963 && ! (TREE_CODE (decl
) == VAR_DECL
&& DECL_REGISTER (decl
))
964 /* Global ctors and dtors are called by the runtime. */
965 && (TREE_CODE (decl
) != FUNCTION_DECL
966 || (!DECL_STATIC_CONSTRUCTOR (decl
)
967 && !DECL_STATIC_DESTRUCTOR (decl
)))
968 /* Otherwise, ask the language. */
969 && lang_hooks
.decls
.warn_unused_global (decl
))
970 warning_at (DECL_SOURCE_LOCATION (decl
),
971 (TREE_CODE (decl
) == FUNCTION_DECL
)
972 ? OPT_Wunused_function
973 : (TREE_READONLY (decl
)
974 ? OPT_Wunused_const_variable
975 : OPT_Wunused_variable
),
976 "%qD defined but not used", decl
);
979 /* Discover all functions and variables that are trivially needed, analyze
980 them as well as all functions and variables referred by them */
981 static cgraph_node
*first_analyzed
;
982 static varpool_node
*first_analyzed_var
;
984 /* FIRST_TIME is set to TRUE for the first time we are called for a
985 translation unit from finalize_compilation_unit() or false
989 analyze_functions (bool first_time
)
991 /* Keep track of already processed nodes when called multiple times for
992 intermodule optimization. */
993 cgraph_node
*first_handled
= first_analyzed
;
994 varpool_node
*first_handled_var
= first_analyzed_var
;
995 hash_set
<void *> reachable_call_targets
;
1001 bool changed
= true;
1002 location_t saved_loc
= input_location
;
1004 bitmap_obstack_initialize (NULL
);
1005 symtab
->state
= CONSTRUCTION
;
1006 input_location
= UNKNOWN_LOCATION
;
1008 /* Ugly, but the fixup can not happen at a time same body alias is created;
1009 C++ FE is confused about the COMDAT groups being right. */
1010 if (symtab
->cpp_implicit_aliases_done
)
1011 FOR_EACH_SYMBOL (node
)
1012 if (node
->cpp_implicit_alias
)
1013 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
1014 build_type_inheritance_graph ();
1016 /* Analysis adds static variables that in turn adds references to new functions.
1017 So we need to iterate the process until it stabilize. */
1021 process_function_and_variable_attributes (first_analyzed
,
1022 first_analyzed_var
);
1024 /* First identify the trivially needed symbols. */
1025 for (node
= symtab
->first_symbol ();
1026 node
!= first_analyzed
1027 && node
!= first_analyzed_var
; node
= node
->next
)
1029 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1030 node
->get_comdat_group_id ();
1031 if (node
->needed_p ())
1033 enqueue_node (node
);
1034 if (!changed
&& symtab
->dump_file
)
1035 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
1037 if (symtab
->dump_file
)
1038 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
1039 if (!changed
&& symtab
->dump_file
)
1040 fprintf (symtab
->dump_file
, "\n");
1042 if (node
== first_analyzed
1043 || node
== first_analyzed_var
)
1046 symtab
->process_new_functions ();
1047 first_analyzed_var
= symtab
->first_variable ();
1048 first_analyzed
= symtab
->first_function ();
1050 if (changed
&& symtab
->dump_file
)
1051 fprintf (symtab
->dump_file
, "\n");
1053 /* Lower representation, build callgraph edges and references for all trivially
1054 needed symbols and all symbols referred by them. */
1055 while (queued_nodes
!= &symtab_terminator
)
1058 node
= queued_nodes
;
1059 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
1060 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1061 if (cnode
&& cnode
->definition
)
1064 tree decl
= cnode
->decl
;
1066 /* ??? It is possible to create extern inline function
1067 and later using weak alias attribute to kill its body.
1068 See gcc.c-torture/compile/20011119-1.c */
1069 if (!DECL_STRUCT_FUNCTION (decl
)
1071 && !cnode
->thunk
.thunk_p
1072 && !cnode
->dispatcher_function
)
1075 cnode
->local
.redefined_extern_inline
= true;
1079 if (!cnode
->analyzed
)
1082 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
1083 if (edge
->callee
->definition
1084 && (!DECL_EXTERNAL (edge
->callee
->decl
)
1085 /* When not optimizing, do not try to analyze extern
1086 inline functions. Doing so is pointless. */
1087 || opt_for_fn (edge
->callee
->decl
, optimize
)
1088 /* Weakrefs needs to be preserved. */
1089 || edge
->callee
->alias
1090 /* always_inline functions are inlined aven at -O0. */
1093 DECL_ATTRIBUTES (edge
->callee
->decl
))
1094 /* Multiversioned functions needs the dispatcher to
1095 be produced locally even for extern functions. */
1096 || edge
->callee
->function_version ()))
1097 enqueue_node (edge
->callee
);
1098 if (opt_for_fn (cnode
->decl
, optimize
)
1099 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
1103 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1105 next
= edge
->next_callee
;
1106 if (edge
->indirect_info
->polymorphic
)
1107 walk_polymorphic_call_targets (&reachable_call_targets
,
1112 /* If decl is a clone of an abstract function,
1113 mark that abstract function so that we don't release its body.
1114 The DECL_INITIAL() of that abstract function declaration
1115 will be later needed to output debug info. */
1116 if (DECL_ABSTRACT_ORIGIN (decl
))
1118 cgraph_node
*origin_node
1119 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1120 origin_node
->used_as_abstract_origin
= true;
1125 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1126 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1130 if (node
->same_comdat_group
)
1133 for (next
= node
->same_comdat_group
;
1135 next
= next
->same_comdat_group
)
1136 if (!next
->comdat_local_p ())
1137 enqueue_node (next
);
1139 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1140 if (ref
->referred
->definition
1141 && (!DECL_EXTERNAL (ref
->referred
->decl
)
1142 || ((TREE_CODE (ref
->referred
->decl
) != FUNCTION_DECL
1144 || (TREE_CODE (ref
->referred
->decl
) == FUNCTION_DECL
1145 && opt_for_fn (ref
->referred
->decl
, optimize
))
1147 || ref
->referred
->alias
)))
1148 enqueue_node (ref
->referred
);
1149 symtab
->process_new_functions ();
1152 update_type_inheritance_graph ();
1154 /* Collect entry points to the unit. */
1155 if (symtab
->dump_file
)
1157 fprintf (symtab
->dump_file
, "\n\nInitial ");
1158 symtab_node::dump_table (symtab
->dump_file
);
1164 FOR_EACH_SYMBOL (snode
)
1165 check_global_declaration (snode
);
1168 if (symtab
->dump_file
)
1169 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1171 for (node
= symtab
->first_symbol ();
1172 node
!= first_handled
1173 && node
!= first_handled_var
; node
= next
)
1176 if (!node
->aux
&& !node
->referred_to_p ())
1178 if (symtab
->dump_file
)
1179 fprintf (symtab
->dump_file
, " %s", node
->name ());
1181 /* See if the debugger can use anything before the DECL
1182 passes away. Perhaps it can notice a DECL that is now a
1183 constant and can tag the early DIE with an appropriate
1186 Otherwise, this is the last chance the debug_hooks have
1187 at looking at optimized away DECLs, since
1188 late_global_decl will subsequently be called from the
1189 contents of the now pruned symbol table. */
1190 if (!decl_function_context (node
->decl
))
1191 (*debug_hooks
->late_global_decl
) (node
->decl
);
1196 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1198 tree decl
= node
->decl
;
1200 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1202 && !cnode
->thunk
.thunk_p
)
1205 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1207 || gimple_has_body_p (decl
));
1208 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1212 for (;node
; node
= node
->next
)
1214 first_analyzed
= symtab
->first_function ();
1215 first_analyzed_var
= symtab
->first_variable ();
1216 if (symtab
->dump_file
)
1218 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1219 symtab_node::dump_table (symtab
->dump_file
);
1221 bitmap_obstack_release (NULL
);
1223 /* Initialize assembler name hash, in particular we want to trigger C++
1224 mangling and same body alias creation before we free DECL_ARGUMENTS
1227 symtab
->symtab_initialize_asm_name_hash ();
1229 input_location
= saved_loc
;
1232 /* Translate the ugly representation of aliases as alias pairs into nice
1233 representation in callgraph. We don't handle all cases yet,
1237 handle_alias_pairs (void)
1242 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1244 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1246 /* Weakrefs with target not defined in current unit are easy to handle:
1247 they behave just as external variables except we need to note the
1248 alias flag to later output the weakref pseudo op into asm file. */
1250 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1252 symtab_node
*node
= symtab_node::get (p
->decl
);
1255 node
->alias_target
= p
->target
;
1256 node
->weakref
= true;
1258 node
->transparent_alias
= true;
1260 alias_pairs
->unordered_remove (i
);
1263 else if (!target_node
)
1265 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1266 symtab_node
*node
= symtab_node::get (p
->decl
);
1268 node
->alias
= false;
1269 alias_pairs
->unordered_remove (i
);
1273 if (DECL_EXTERNAL (target_node
->decl
)
1274 /* We use local aliases for C++ thunks to force the tailcall
1275 to bind locally. This is a hack - to keep it working do
1276 the following (which is not strictly correct). */
1277 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1278 || ! DECL_VIRTUAL_P (target_node
->decl
))
1279 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1281 error ("%q+D aliased to external symbol %qE",
1282 p
->decl
, p
->target
);
1285 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1286 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1288 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1289 if (src_node
&& src_node
->definition
)
1291 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1292 alias_pairs
->unordered_remove (i
);
1294 else if (TREE_CODE (p
->decl
) == VAR_DECL
1295 && target_node
&& is_a
<varpool_node
*> (target_node
))
1297 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1298 alias_pairs
->unordered_remove (i
);
1302 error ("%q+D alias in between function and variable is not supported",
1304 warning (0, "%q+D aliased declaration",
1306 alias_pairs
->unordered_remove (i
);
1309 vec_free (alias_pairs
);
1313 /* Figure out what functions we want to assemble. */
1316 mark_functions_to_output (void)
1318 bool check_same_comdat_groups
= false;
1322 FOR_EACH_FUNCTION (node
)
1323 gcc_assert (!node
->process
);
1325 FOR_EACH_FUNCTION (node
)
1327 tree decl
= node
->decl
;
1329 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1333 /* We need to output all local functions that are used and not
1334 always inlined, as well as those that are reachable from
1335 outside the current compilation unit. */
1337 && !node
->thunk
.thunk_p
1339 && !node
->global
.inlined_to
1340 && !TREE_ASM_WRITTEN (decl
)
1341 && !DECL_EXTERNAL (decl
))
1344 if (node
->same_comdat_group
)
1347 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1349 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1350 if (!next
->thunk
.thunk_p
&& !next
->alias
1351 && !next
->comdat_local_p ())
1355 else if (node
->same_comdat_group
)
1358 check_same_comdat_groups
= true;
1362 /* We should've reclaimed all functions that are not needed. */
1364 && !node
->global
.inlined_to
1365 && gimple_has_body_p (decl
)
1366 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1367 are inside partition, we can end up not removing the body since we no longer
1368 have analyzed node pointing to it. */
1369 && !node
->in_other_partition
1372 && !DECL_EXTERNAL (decl
))
1375 internal_error ("failed to reclaim unneeded function");
1377 gcc_assert (node
->global
.inlined_to
1378 || !gimple_has_body_p (decl
)
1379 || node
->in_other_partition
1381 || DECL_ARTIFICIAL (decl
)
1382 || DECL_EXTERNAL (decl
));
1387 if (flag_checking
&& check_same_comdat_groups
)
1388 FOR_EACH_FUNCTION (node
)
1389 if (node
->same_comdat_group
&& !node
->process
)
1391 tree decl
= node
->decl
;
1392 if (!node
->global
.inlined_to
1393 && gimple_has_body_p (decl
)
1394 /* FIXME: in an ltrans unit when the offline copy is outside a
1395 partition but inline copies are inside a partition, we can
1396 end up not removing the body since we no longer have an
1397 analyzed node pointing to it. */
1398 && !node
->in_other_partition
1400 && !DECL_EXTERNAL (decl
))
1403 internal_error ("failed to reclaim unneeded function in same "
1409 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1410 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1412 Set current_function_decl and cfun to newly constructed empty function body.
1413 return basic block in the function body. */
1416 init_lowered_empty_function (tree decl
, bool in_ssa
, gcov_type count
)
1421 current_function_decl
= decl
;
1422 allocate_struct_function (decl
, false);
1423 gimple_register_cfg_hooks ();
1424 init_empty_tree_cfg ();
1428 init_tree_ssa (cfun
);
1429 init_ssa_operands (cfun
);
1430 cfun
->gimple_df
->in_ssa_p
= true;
1431 cfun
->curr_properties
|= PROP_ssa
;
1434 DECL_INITIAL (decl
) = make_node (BLOCK
);
1436 DECL_SAVED_TREE (decl
) = error_mark_node
;
1437 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1438 | PROP_cfg
| PROP_loops
);
1440 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1441 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1442 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1444 /* Create BB for body of the function and connect it properly. */
1445 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1446 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1447 EXIT_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1448 EXIT_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1449 bb
= create_basic_block (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1451 bb
->frequency
= BB_FREQ_MAX
;
1452 e
= make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1454 e
->probability
= REG_BR_PROB_BASE
;
1455 e
= make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1457 e
->probability
= REG_BR_PROB_BASE
;
1458 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1463 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1464 offset indicated by VIRTUAL_OFFSET, if that is
1465 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1466 zero for a result adjusting thunk. */
1469 thunk_adjust (gimple_stmt_iterator
* bsi
,
1470 tree ptr
, bool this_adjusting
,
1471 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1477 && fixed_offset
!= 0)
1479 stmt
= gimple_build_assign
1480 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1483 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1486 /* If there's a virtual offset, look up that value in the vtable and
1487 adjust the pointer again. */
1494 if (!vtable_entry_type
)
1496 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1497 TREE_TYPE (vfunc_type
) = integer_type_node
;
1498 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1499 layout_type (vfunc_type
);
1501 vtable_entry_type
= build_pointer_type (vfunc_type
);
1505 create_tmp_reg (build_pointer_type
1506 (build_pointer_type (vtable_entry_type
)), "vptr");
1508 /* The vptr is always at offset zero in the object. */
1509 stmt
= gimple_build_assign (vtabletmp
,
1510 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1512 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1514 /* Form the vtable address. */
1515 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1517 stmt
= gimple_build_assign (vtabletmp2
,
1518 build_simple_mem_ref (vtabletmp
));
1519 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1521 /* Find the entry with the vcall offset. */
1522 stmt
= gimple_build_assign (vtabletmp2
,
1523 fold_build_pointer_plus_loc (input_location
,
1526 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1528 /* Get the offset itself. */
1529 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1531 stmt
= gimple_build_assign (vtabletmp3
,
1532 build_simple_mem_ref (vtabletmp2
));
1533 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1535 /* Adjust the `this' pointer. */
1536 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1537 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1538 GSI_CONTINUE_LINKING
);
1542 && fixed_offset
!= 0)
1543 /* Adjust the pointer by the constant. */
1547 if (TREE_CODE (ptr
) == VAR_DECL
)
1551 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1552 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1553 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1555 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1556 ptrtmp
, fixed_offset
);
1559 /* Emit the statement and gimplify the adjustment expression. */
1560 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1561 stmt
= gimple_build_assign (ret
, ptr
);
1562 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1567 /* Expand thunk NODE to gimple if possible.
1568 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1569 no assembler is produced.
1570 When OUTPUT_ASM_THUNK is true, also produce assembler for
1571 thunks that are not lowered. */
1574 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1576 bool this_adjusting
= thunk
.this_adjusting
;
1577 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1578 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1579 tree virtual_offset
= NULL
;
1580 tree alias
= callees
->callee
->decl
;
1581 tree thunk_fndecl
= decl
;
1584 /* Instrumentation thunk is the same function with
1585 a different signature. Never need to expand it. */
1586 if (thunk
.add_pointer_bounds_args
)
1589 if (!force_gimple_thunk
&& this_adjusting
1590 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1591 virtual_value
, alias
))
1595 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1597 if (!output_asm_thunks
)
1604 get_untransformed_body ();
1605 a
= DECL_ARGUMENTS (thunk_fndecl
);
1607 current_function_decl
= thunk_fndecl
;
1609 /* Ensure thunks are emitted in their correct sections. */
1610 resolve_unique_section (thunk_fndecl
, 0,
1611 flag_function_sections
);
1613 DECL_RESULT (thunk_fndecl
)
1614 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1615 RESULT_DECL
, 0, restype
);
1616 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1617 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1619 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1621 fn_block
= make_node (BLOCK
);
1622 BLOCK_VARS (fn_block
) = a
;
1623 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1624 allocate_struct_function (thunk_fndecl
, false);
1625 init_function_start (thunk_fndecl
);
1627 insn_locations_init ();
1628 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1629 prologue_location
= curr_insn_location ();
1630 assemble_start_function (thunk_fndecl
, fnname
);
1632 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1633 fixed_offset
, virtual_value
, alias
);
1635 assemble_end_function (thunk_fndecl
, fnname
);
1636 insn_locations_finalize ();
1637 init_insn_lengths ();
1638 free_after_compilation (cfun
);
1639 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1640 thunk
.thunk_p
= false;
1643 else if (stdarg_p (TREE_TYPE (thunk_fndecl
)))
1645 error ("generic thunk code fails for method %qD which uses %<...%>",
1647 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1654 basic_block bb
, then_bb
, else_bb
, return_bb
;
1655 gimple_stmt_iterator bsi
;
1665 bool alias_is_noreturn
= TREE_THIS_VOLATILE (alias
);
1668 get_untransformed_body ();
1669 a
= DECL_ARGUMENTS (thunk_fndecl
);
1671 current_function_decl
= thunk_fndecl
;
1673 /* Ensure thunks are emitted in their correct sections. */
1674 resolve_unique_section (thunk_fndecl
, 0,
1675 flag_function_sections
);
1677 DECL_IGNORED_P (thunk_fndecl
) = 1;
1678 bitmap_obstack_initialize (NULL
);
1680 if (thunk
.virtual_offset_p
)
1681 virtual_offset
= size_int (virtual_value
);
1683 /* Build the return declaration for the function. */
1684 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1685 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1687 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1688 DECL_ARTIFICIAL (resdecl
) = 1;
1689 DECL_IGNORED_P (resdecl
) = 1;
1690 DECL_RESULT (thunk_fndecl
) = resdecl
;
1691 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1694 resdecl
= DECL_RESULT (thunk_fndecl
);
1696 bb
= then_bb
= else_bb
= return_bb
1697 = init_lowered_empty_function (thunk_fndecl
, true, count
);
1699 bsi
= gsi_start_bb (bb
);
1701 /* Build call to the function being thunked. */
1702 if (!VOID_TYPE_P (restype
) && !alias_is_noreturn
)
1704 if (DECL_BY_REFERENCE (resdecl
))
1706 restmp
= gimple_fold_indirect_ref (resdecl
);
1708 restmp
= build2 (MEM_REF
,
1709 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias
))),
1711 build_int_cst (TREE_TYPE
1712 (DECL_RESULT (alias
)), 0));
1714 else if (!is_gimple_reg_type (restype
))
1716 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
)))
1720 if (TREE_CODE (restmp
) == VAR_DECL
)
1721 add_local_decl (cfun
, restmp
);
1722 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1725 restmp
= create_tmp_var (restype
, "retval");
1728 restmp
= create_tmp_reg (restype
, "retval");
1731 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1733 auto_vec
<tree
> vargs (nargs
);
1738 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1740 arg
= DECL_CHAIN (a
);
1745 for (; i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1748 if (!is_gimple_val (arg
))
1750 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1751 (TREE_TYPE (arg
)), "arg");
1752 gimple
*stmt
= gimple_build_assign (tmp
, arg
);
1753 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1755 vargs
.quick_push (tmp
);
1757 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1758 callees
->call_stmt
= call
;
1759 gimple_call_set_from_thunk (call
, true);
1760 gimple_call_set_with_bounds (call
, instrumentation_clone
);
1762 /* Return slot optimization is always possible and in fact requred to
1763 return values with DECL_BY_REFERENCE. */
1764 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
))
1765 && (!is_gimple_reg_type (TREE_TYPE (resdecl
))
1766 || DECL_BY_REFERENCE (resdecl
)))
1767 gimple_call_set_return_slot_opt (call
, true);
1769 if (restmp
&& !alias_is_noreturn
)
1771 gimple_call_set_lhs (call
, restmp
);
1772 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1773 TREE_TYPE (TREE_TYPE (alias
))));
1775 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1776 if (!alias_is_noreturn
)
1778 if (instrumentation_clone
1779 && !DECL_BY_REFERENCE (resdecl
)
1781 && BOUNDED_P (restmp
))
1783 resbnd
= chkp_insert_retbnd_call (NULL
, restmp
, &bsi
);
1784 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi
))),
1785 as_a
<gcall
*> (gsi_stmt (bsi
)),
1786 callees
->count
, callees
->frequency
);
1789 if (restmp
&& !this_adjusting
1790 && (fixed_offset
|| virtual_offset
))
1792 tree true_label
= NULL_TREE
;
1794 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1798 /* If the return type is a pointer, we need to
1799 protect against NULL. We know there will be an
1800 adjustment, because that's why we're emitting a
1802 then_bb
= create_basic_block (NULL
, bb
);
1803 then_bb
->count
= count
- count
/ 16;
1804 then_bb
->frequency
= BB_FREQ_MAX
- BB_FREQ_MAX
/ 16;
1805 return_bb
= create_basic_block (NULL
, then_bb
);
1806 return_bb
->count
= count
;
1807 return_bb
->frequency
= BB_FREQ_MAX
;
1808 else_bb
= create_basic_block (NULL
, else_bb
);
1809 then_bb
->count
= count
/ 16;
1810 then_bb
->frequency
= BB_FREQ_MAX
/ 16;
1811 add_bb_to_loop (then_bb
, bb
->loop_father
);
1812 add_bb_to_loop (return_bb
, bb
->loop_father
);
1813 add_bb_to_loop (else_bb
, bb
->loop_father
);
1814 remove_edge (single_succ_edge (bb
));
1815 true_label
= gimple_block_label (then_bb
);
1816 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1817 build_zero_cst (TREE_TYPE (restmp
)),
1818 NULL_TREE
, NULL_TREE
);
1819 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1820 e
= make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1821 e
->probability
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 16;
1822 e
->count
= count
- count
/ 16;
1823 e
= make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1824 e
->probability
= REG_BR_PROB_BASE
/ 16;
1825 e
->count
= count
/ 16;
1826 e
= make_edge (return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1827 e
->probability
= REG_BR_PROB_BASE
;
1829 e
= make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1830 e
->probability
= REG_BR_PROB_BASE
;
1831 e
->count
= count
- count
/ 16;
1832 e
= make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1833 e
->probability
= REG_BR_PROB_BASE
;
1834 e
->count
= count
/ 16;
1835 bsi
= gsi_last_bb (then_bb
);
1838 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1839 fixed_offset
, virtual_offset
);
1843 bsi
= gsi_last_bb (else_bb
);
1844 stmt
= gimple_build_assign (restmp
,
1845 build_zero_cst (TREE_TYPE (restmp
)));
1846 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1847 bsi
= gsi_last_bb (return_bb
);
1851 gimple_call_set_tail (call
, true);
1853 /* Build return value. */
1854 if (!DECL_BY_REFERENCE (resdecl
))
1855 ret
= gimple_build_return (restmp
);
1857 ret
= gimple_build_return (resdecl
);
1858 gimple_return_set_retbnd (ret
, resbnd
);
1860 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1864 gimple_call_set_tail (call
, true);
1865 remove_edge (single_succ_edge (bb
));
1868 cfun
->gimple_df
->in_ssa_p
= true;
1869 profile_status_for_fn (cfun
)
1870 = count
? PROFILE_READ
: PROFILE_GUESSED
;
1871 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1872 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
1873 delete_unreachable_blocks ();
1874 update_ssa (TODO_update_ssa
);
1875 checking_verify_flow_info ();
1876 free_dominance_info (CDI_DOMINATORS
);
1878 /* Since we want to emit the thunk, we explicitly mark its name as
1880 thunk
.thunk_p
= false;
1882 bitmap_obstack_release (NULL
);
1884 current_function_decl
= NULL
;
1889 /* Assemble thunks and aliases associated to node. */
1892 cgraph_node::assemble_thunks_and_aliases (void)
1897 for (e
= callers
; e
;)
1898 if (e
->caller
->thunk
.thunk_p
1899 && !e
->caller
->thunk
.add_pointer_bounds_args
)
1901 cgraph_node
*thunk
= e
->caller
;
1904 thunk
->expand_thunk (true, false);
1905 thunk
->assemble_thunks_and_aliases ();
1910 FOR_EACH_ALIAS (this, ref
)
1912 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1913 if (!alias
->transparent_alias
)
1915 bool saved_written
= TREE_ASM_WRITTEN (decl
);
1917 /* Force assemble_alias to really output the alias this time instead
1918 of buffering it in same alias pairs. */
1919 TREE_ASM_WRITTEN (decl
) = 1;
1920 do_assemble_alias (alias
->decl
,
1921 DECL_ASSEMBLER_NAME (decl
));
1922 alias
->assemble_thunks_and_aliases ();
1923 TREE_ASM_WRITTEN (decl
) = saved_written
;
1928 /* Expand function specified by node. */
1931 cgraph_node::expand (void)
1933 location_t saved_loc
;
1935 /* We ought to not compile any inline clones. */
1936 gcc_assert (!global
.inlined_to
);
1938 announce_function (decl
);
1940 gcc_assert (lowered
);
1941 get_untransformed_body ();
1943 /* Generate RTL for the body of DECL. */
1945 timevar_push (TV_REST_OF_COMPILATION
);
1947 gcc_assert (symtab
->global_info_ready
);
1949 /* Initialize the default bitmap obstack. */
1950 bitmap_obstack_initialize (NULL
);
1952 /* Initialize the RTL code for the function. */
1953 saved_loc
= input_location
;
1954 input_location
= DECL_SOURCE_LOCATION (decl
);
1956 gcc_assert (DECL_STRUCT_FUNCTION (decl
));
1957 push_cfun (DECL_STRUCT_FUNCTION (decl
));
1958 init_function_start (decl
);
1960 gimple_register_cfg_hooks ();
1962 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1964 execute_all_ipa_transforms ();
1966 /* Perform all tree transforms and optimizations. */
1968 /* Signal the start of passes. */
1969 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1971 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
1973 /* Signal the end of passes. */
1974 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1976 bitmap_obstack_release (®_obstack
);
1978 /* Release the default bitmap obstack. */
1979 bitmap_obstack_release (NULL
);
1981 /* If requested, warn about function definitions where the function will
1982 return a value (usually of some struct or union type) which itself will
1983 take up a lot of stack space. */
1984 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1986 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1988 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1989 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1990 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1993 unsigned int size_as_int
1994 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1996 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1997 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
2000 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
2001 decl
, larger_than_size
);
2005 gimple_set_body (decl
, NULL
);
2006 if (DECL_STRUCT_FUNCTION (decl
) == 0
2007 && !cgraph_node::get (decl
)->origin
)
2009 /* Stop pointing to the local nodes about to be freed.
2010 But DECL_INITIAL must remain nonzero so we know this
2011 was an actual function definition.
2012 For a nested function, this is done in c_pop_function_context.
2013 If rest_of_compilation set this to 0, leave it 0. */
2014 if (DECL_INITIAL (decl
) != 0)
2015 DECL_INITIAL (decl
) = error_mark_node
;
2018 input_location
= saved_loc
;
2021 timevar_pop (TV_REST_OF_COMPILATION
);
2023 /* Make sure that BE didn't give up on compiling. */
2024 gcc_assert (TREE_ASM_WRITTEN (decl
));
2028 /* It would make a lot more sense to output thunks before function body to get more
2029 forward and lest backwarding jumps. This however would need solving problem
2030 with comdats. See PR48668. Also aliases must come after function itself to
2031 make one pass assemblers, like one on AIX, happy. See PR 50689.
2032 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2034 assemble_thunks_and_aliases ();
2036 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2037 points to the dead function body. */
2039 remove_all_references ();
2042 /* Node comparer that is responsible for the order that corresponds
2043 to time when a function was launched for the first time. */
2046 node_cmp (const void *pa
, const void *pb
)
2048 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
2049 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
2051 /* Functions with time profile must be before these without profile. */
2052 if (!a
->tp_first_run
|| !b
->tp_first_run
)
2053 return a
->tp_first_run
- b
->tp_first_run
;
2055 return a
->tp_first_run
!= b
->tp_first_run
2056 ? b
->tp_first_run
- a
->tp_first_run
2057 : b
->order
- a
->order
;
2060 /* Expand all functions that must be output.
2062 Attempt to topologically sort the nodes so function is output when
2063 all called functions are already assembled to allow data to be
2064 propagated across the callgraph. Use a stack to get smaller distance
2065 between a function and its callees (later we may choose to use a more
2066 sophisticated algorithm for function reordering; we will likely want
2067 to use subsections to make the output functions appear in top-down
2071 expand_all_functions (void)
2074 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
2075 symtab
->cgraph_count
);
2076 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
2077 int order_pos
, new_order_pos
= 0;
2080 order_pos
= ipa_reverse_postorder (order
);
2081 gcc_assert (order_pos
== symtab
->cgraph_count
);
2083 /* Garbage collector may remove inline clones we eliminate during
2084 optimization. So we must be sure to not reference them. */
2085 for (i
= 0; i
< order_pos
; i
++)
2086 if (order
[i
]->process
)
2087 order
[new_order_pos
++] = order
[i
];
2089 if (flag_profile_reorder_functions
)
2090 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
2092 for (i
= new_order_pos
- 1; i
>= 0; i
--)
2098 expanded_func_count
++;
2099 if(node
->tp_first_run
)
2100 profiled_func_count
++;
2102 if (symtab
->dump_file
)
2103 fprintf (symtab
->dump_file
,
2104 "Time profile order in expand_all_functions:%s:%d\n",
2105 node
->asm_name (), node
->tp_first_run
);
2112 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
2113 main_input_filename
, profiled_func_count
, expanded_func_count
);
2115 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
2116 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
2117 profiled_func_count
, expanded_func_count
);
2119 symtab
->process_new_functions ();
2120 free_gimplify_stack ();
2125 /* This is used to sort the node types by the cgraph order number. */
2127 enum cgraph_order_sort_kind
2129 ORDER_UNDEFINED
= 0,
2135 struct cgraph_order_sort
2137 enum cgraph_order_sort_kind kind
;
2146 /* Output all functions, variables, and asm statements in the order
2147 according to their order fields, which is the order in which they
2148 appeared in the file. This implements -fno-toplevel-reorder. In
2149 this mode we may output functions and variables which don't really
2151 When NO_REORDER is true only do this for symbols marked no reorder. */
2154 output_in_order (bool no_reorder
)
2157 cgraph_order_sort
*nodes
;
2162 max
= symtab
->order
;
2163 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
2165 FOR_EACH_DEFINED_FUNCTION (pf
)
2167 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
2169 if (no_reorder
&& !pf
->no_reorder
)
2172 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2173 nodes
[i
].kind
= ORDER_FUNCTION
;
2178 FOR_EACH_DEFINED_VARIABLE (pv
)
2179 if (!DECL_EXTERNAL (pv
->decl
))
2181 if (no_reorder
&& !pv
->no_reorder
)
2184 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2185 nodes
[i
].kind
= ORDER_VAR
;
2189 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
2192 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2193 nodes
[i
].kind
= ORDER_ASM
;
2197 /* In toplevel reorder mode we output all statics; mark them as needed. */
2199 for (i
= 0; i
< max
; ++i
)
2200 if (nodes
[i
].kind
== ORDER_VAR
)
2201 nodes
[i
].u
.v
->finalize_named_section_flags ();
2203 for (i
= 0; i
< max
; ++i
)
2205 switch (nodes
[i
].kind
)
2207 case ORDER_FUNCTION
:
2208 nodes
[i
].u
.f
->process
= 0;
2209 nodes
[i
].u
.f
->expand ();
2213 #ifdef ACCEL_COMPILER
2214 /* Do not assemble "omp declare target link" vars. */
2215 if (DECL_HAS_VALUE_EXPR_P (nodes
[i
].u
.v
->decl
)
2216 && lookup_attribute ("omp declare target link",
2217 DECL_ATTRIBUTES (nodes
[i
].u
.v
->decl
)))
2220 nodes
[i
].u
.v
->assemble_decl ();
2224 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2227 case ORDER_UNDEFINED
:
2235 symtab
->clear_asm_symbols ();
2243 gcc::pass_manager
*passes
= g
->get_passes ();
2246 current_function_decl
= NULL
;
2247 gimple_register_cfg_hooks ();
2248 bitmap_obstack_initialize (NULL
);
2250 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2254 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2259 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2260 devirtualization and other changes where removal iterate. */
2261 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2263 /* If pass_all_early_optimizations was not scheduled, the state of
2264 the cgraph will not be properly updated. Update it now. */
2265 if (symtab
->state
< IPA_SSA
)
2266 symtab
->state
= IPA_SSA
;
2270 /* Generate coverage variables and constructors. */
2273 /* Process new functions added. */
2275 current_function_decl
= NULL
;
2276 symtab
->process_new_functions ();
2278 execute_ipa_summary_passes
2279 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2282 /* Some targets need to handle LTO assembler output specially. */
2283 if (flag_generate_lto
|| flag_generate_offload
)
2284 targetm
.asm_out
.lto_start ();
2288 if (g
->have_offload
)
2290 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2291 lto_stream_offload_p
= true;
2292 ipa_write_summaries ();
2293 lto_stream_offload_p
= false;
2297 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2298 lto_stream_offload_p
= false;
2299 ipa_write_summaries ();
2303 if (flag_generate_lto
|| flag_generate_offload
)
2304 targetm
.asm_out
.lto_end ();
2306 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2307 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2308 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2310 bitmap_obstack_release (NULL
);
2314 /* Return string alias is alias of. */
2317 get_alias_symbol (tree decl
)
2319 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2320 return get_identifier (TREE_STRING_POINTER
2321 (TREE_VALUE (TREE_VALUE (alias
))));
2325 /* Weakrefs may be associated to external decls and thus not output
2326 at expansion time. Emit all necessary aliases. */
2329 symbol_table::output_weakrefs (void)
2333 FOR_EACH_SYMBOL (node
)
2335 && !TREE_ASM_WRITTEN (node
->decl
)
2336 && (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
2337 || !cnode
->instrumented_version
2338 || !TREE_ASM_WRITTEN (cnode
->instrumented_version
->decl
))
2343 /* Weakrefs are special by not requiring target definition in current
2344 compilation unit. It is thus bit hard to work out what we want to
2346 When alias target is defined, we need to fetch it from symtab reference,
2347 otherwise it is pointed to by alias_target. */
2348 if (node
->alias_target
)
2349 target
= (DECL_P (node
->alias_target
)
2350 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2351 : node
->alias_target
);
2352 else if (node
->analyzed
)
2353 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2357 target
= get_alias_symbol (node
->decl
);
2359 do_assemble_alias (node
->decl
, target
);
2363 /* Perform simple optimizations based on callgraph. */
2366 symbol_table::compile (void)
2371 symtab_node::checking_verify_symtab_nodes ();
2373 timevar_push (TV_CGRAPHOPT
);
2374 if (pre_ipa_mem_report
)
2376 fprintf (stderr
, "Memory consumption before IPA\n");
2377 dump_memory_report (false);
2380 fprintf (stderr
, "Performing interprocedural optimizations\n");
2383 /* Offloading requires LTO infrastructure. */
2384 if (!in_lto_p
&& g
->have_offload
)
2385 flag_generate_offload
= 1;
2387 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2388 if (flag_generate_lto
|| flag_generate_offload
)
2389 lto_streamer_hooks_init ();
2391 /* Don't run the IPA passes if there was any error or sorry messages. */
2395 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2397 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2399 timevar_pop (TV_CGRAPHOPT
);
2403 global_info_ready
= true;
2406 fprintf (dump_file
, "Optimized ");
2407 symtab_node:: dump_table (dump_file
);
2409 if (post_ipa_mem_report
)
2411 fprintf (stderr
, "Memory consumption after IPA\n");
2412 dump_memory_report (false);
2414 timevar_pop (TV_CGRAPHOPT
);
2416 /* Output everything. */
2417 (*debug_hooks
->assembly_start
) ();
2419 fprintf (stderr
, "Assembling functions:\n");
2420 symtab_node::checking_verify_symtab_nodes ();
2422 materialize_all_clones ();
2423 bitmap_obstack_initialize (NULL
);
2424 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2425 bitmap_obstack_release (NULL
);
2426 mark_functions_to_output ();
2428 /* When weakref support is missing, we autmatically translate all
2429 references to NODE to references to its ultimate alias target.
2430 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2433 Set up this mapping before we output any assembler but once we are sure
2434 that all symbol renaming is done.
2436 FIXME: All this uglyness can go away if we just do renaming at gimple
2437 level by physically rewritting the IL. At the moment we can only redirect
2438 calls, so we need infrastructure for renaming references as well. */
2439 #ifndef ASM_OUTPUT_WEAKREF
2442 FOR_EACH_SYMBOL (node
)
2444 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2446 IDENTIFIER_TRANSPARENT_ALIAS
2447 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2448 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2449 = (node
->alias_target
? node
->alias_target
2450 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2456 if (!flag_toplevel_reorder
)
2457 output_in_order (false);
2460 /* Output first asm statements and anything ordered. The process
2461 flag is cleared for these nodes, so we skip them later. */
2462 output_in_order (true);
2463 expand_all_functions ();
2464 output_variables ();
2467 process_new_functions ();
2473 fprintf (dump_file
, "\nFinal ");
2474 symtab_node::dump_table (dump_file
);
2478 symtab_node::verify_symtab_nodes ();
2479 /* Double check that all inline clones are gone and that all
2480 function bodies have been released from memory. */
2484 bool error_found
= false;
2486 FOR_EACH_DEFINED_FUNCTION (node
)
2487 if (node
->global
.inlined_to
2488 || gimple_has_body_p (node
->decl
))
2494 internal_error ("nodes with unreleased memory found");
2499 /* Analyze the whole compilation unit once it is parsed completely. */
2502 symbol_table::finalize_compilation_unit (void)
2504 timevar_push (TV_CGRAPH
);
2506 /* If we're here there's no current function anymore. Some frontends
2507 are lazy in clearing these. */
2508 current_function_decl
= NULL
;
2511 /* Do not skip analyzing the functions if there were errors, we
2512 miss diagnostics for following functions otherwise. */
2514 /* Emit size functions we didn't inline. */
2515 finalize_size_functions ();
2517 /* Mark alias targets necessary and emit diagnostics. */
2518 handle_alias_pairs ();
2522 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2526 if (flag_dump_passes
)
2529 /* Gimplify and lower all functions, compute reachability and
2530 remove unreachable nodes. */
2531 analyze_functions (/*first_time=*/true);
2533 /* Mark alias targets necessary and emit diagnostics. */
2534 handle_alias_pairs ();
2536 /* Gimplify and lower thunks. */
2537 analyze_functions (/*first_time=*/false);
2541 /* Emit early debug for reachable functions, and by consequence,
2542 locally scoped symbols. */
2543 struct cgraph_node
*cnode
;
2544 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode
)
2545 (*debug_hooks
->early_global_decl
) (cnode
->decl
);
2547 /* Clean up anything that needs cleaning up after initial debug
2549 (*debug_hooks
->early_finish
) ();
2552 /* Finally drive the pass manager. */
2555 timevar_pop (TV_CGRAPH
);
2558 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2559 within the same process. For use by toplev::finalize. */
2562 cgraphunit_c_finalize (void)
2564 gcc_assert (cgraph_new_nodes
.length () == 0);
2565 cgraph_new_nodes
.truncate (0);
2567 vtable_entry_type
= NULL
;
2568 queued_nodes
= &symtab_terminator
;
2570 first_analyzed
= NULL
;
2571 first_analyzed_var
= NULL
;
2574 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2575 kind of wrapper method. */
2578 cgraph_node::create_wrapper (cgraph_node
*target
)
2580 /* Preserve DECL_RESULT so we get right by reference flag. */
2581 tree decl_result
= DECL_RESULT (decl
);
2583 /* Remove the function's body but keep arguments to be reused
2585 release_body (true);
2588 DECL_UNINLINABLE (decl
) = false;
2589 DECL_RESULT (decl
) = decl_result
;
2590 DECL_INITIAL (decl
) = NULL
;
2591 allocate_struct_function (decl
, false);
2594 /* Turn alias into thunk and expand it into GIMPLE representation. */
2597 memset (&thunk
, 0, sizeof (cgraph_thunk_info
));
2598 thunk
.thunk_p
= true;
2599 create_edge (target
, NULL
, count
, CGRAPH_FREQ_BASE
);
2600 callees
->can_throw_external
= !TREE_NOTHROW (target
->decl
);
2602 tree arguments
= DECL_ARGUMENTS (decl
);
2606 TREE_ADDRESSABLE (arguments
) = false;
2607 arguments
= TREE_CHAIN (arguments
);
2610 expand_thunk (false, true);
2612 /* Inline summary set-up. */
2614 inline_analyze_function (this);
2617 #include "gt-cgraphunit.h"