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
));
369 memset (&thunk
, 0, sizeof (cgraph_thunk_info
));
373 transparent_alias
= false;
375 cpp_implicit_alias
= false;
376 instrumented_version
= NULL
;
379 remove_all_references ();
382 /* Return true when there are references to the node. INCLUDE_SELF is
383 true if a self reference counts as a reference. */
386 symtab_node::referred_to_p (bool include_self
)
390 /* See if there are any references at all. */
391 if (iterate_referring (0, ref
))
393 /* For functions check also calls. */
394 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
395 if (cn
&& cn
->callers
)
399 for (cgraph_edge
*e
= cn
->callers
; e
; e
= e
->next_caller
)
400 if (e
->caller
!= this)
406 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
407 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
408 the garbage collector run at the moment. We would need to either create
409 a new GC context, or just not compile right now. */
412 cgraph_node::finalize_function (tree decl
, bool no_collect
)
414 cgraph_node
*node
= cgraph_node::get_create (decl
);
416 if (node
->definition
)
418 /* Nested functions should only be defined once. */
419 gcc_assert (!DECL_CONTEXT (decl
)
420 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
422 node
->local
.redefined_extern_inline
= true;
425 /* Set definition first before calling notice_global_symbol so that
426 it is available to notice_global_symbol. */
427 node
->definition
= true;
428 notice_global_symbol (decl
);
429 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
431 /* With -fkeep-inline-functions we are keeping all inline functions except
432 for extern inline ones. */
433 if (flag_keep_inline_functions
434 && DECL_DECLARED_INLINE_P (decl
)
435 && !DECL_EXTERNAL (decl
)
436 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
437 node
->force_output
= 1;
439 /* When not optimizing, also output the static functions. (see
440 PR24561), but don't do so for always_inline functions, functions
441 declared inline and nested functions. These were optimized out
442 in the original implementation and it is unclear whether we want
443 to change the behavior here. */
444 if (((!opt_for_fn (decl
, optimize
) || flag_keep_static_functions
)
445 && !node
->cpp_implicit_alias
446 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
447 && !DECL_DECLARED_INLINE_P (decl
)
448 && !(DECL_CONTEXT (decl
)
449 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
450 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
451 node
->force_output
= 1;
453 /* If we've not yet emitted decl, tell the debug info about it. */
454 if (!TREE_ASM_WRITTEN (decl
))
455 (*debug_hooks
->deferred_inline_function
) (decl
);
460 if (symtab
->state
== CONSTRUCTION
461 && (node
->needed_p () || node
->referred_to_p ()))
465 /* Add the function FNDECL to the call graph.
466 Unlike finalize_function, this function is intended to be used
467 by middle end and allows insertion of new function at arbitrary point
468 of compilation. The function can be either in high, low or SSA form
471 The function is assumed to be reachable and have address taken (so no
472 API breaking optimizations are performed on it).
474 Main work done by this function is to enqueue the function for later
475 processing to avoid need the passes to be re-entrant. */
478 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
480 gcc::pass_manager
*passes
= g
->get_passes ();
485 struct function
*fn
= DECL_STRUCT_FUNCTION (fndecl
);
486 const char *function_type
= ((gimple_has_body_p (fndecl
))
488 ? (gimple_in_ssa_p (fn
)
492 : "to-be-gimplified");
494 "Added new %s function %s to callgraph\n",
496 fndecl_name (fndecl
));
499 switch (symtab
->state
)
502 cgraph_node::finalize_function (fndecl
, false);
505 /* Just enqueue function to be processed at nearest occurrence. */
506 node
= cgraph_node::get_create (fndecl
);
508 node
->lowered
= true;
509 cgraph_new_nodes
.safe_push (node
);
514 case IPA_SSA_AFTER_INLINING
:
516 /* Bring the function into finalized state and enqueue for later
517 analyzing and compilation. */
518 node
= cgraph_node::get_create (fndecl
);
519 node
->local
.local
= false;
520 node
->definition
= true;
521 node
->force_output
= true;
522 if (!lowered
&& symtab
->state
== EXPANSION
)
524 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
525 gimple_register_cfg_hooks ();
526 bitmap_obstack_initialize (NULL
);
527 execute_pass_list (cfun
, passes
->all_lowering_passes
);
528 passes
->execute_early_local_passes ();
529 bitmap_obstack_release (NULL
);
535 node
->lowered
= true;
536 cgraph_new_nodes
.safe_push (node
);
540 /* At the very end of compilation we have to do all the work up
542 node
= cgraph_node::create (fndecl
);
544 node
->lowered
= true;
545 node
->definition
= true;
547 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
548 gimple_register_cfg_hooks ();
549 bitmap_obstack_initialize (NULL
);
550 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
551 g
->get_passes ()->execute_early_local_passes ();
552 bitmap_obstack_release (NULL
);
561 /* Set a personality if required and we already passed EH lowering. */
563 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
564 == eh_personality_lang
))
565 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
568 /* Analyze the function scheduled to be output. */
570 cgraph_node::analyze (void)
572 tree decl
= this->decl
;
573 location_t saved_loc
= input_location
;
574 input_location
= DECL_SOURCE_LOCATION (decl
);
578 cgraph_node
*t
= cgraph_node::get (thunk
.alias
);
580 create_edge (t
, NULL
, 0, CGRAPH_FREQ_BASE
);
581 callees
->can_throw_external
= !TREE_NOTHROW (t
->decl
);
582 /* Target code in expand_thunk may need the thunk's target
583 to be analyzed, so recurse here. */
588 t
= t
->get_alias_target ();
592 if (!expand_thunk (false, false))
600 resolve_alias (cgraph_node::get (alias_target
), transparent_alias
);
601 else if (dispatcher_function
)
603 /* Generate the dispatcher body of multi-versioned functions. */
604 cgraph_function_version_info
*dispatcher_version_info
605 = function_version ();
606 if (dispatcher_version_info
!= NULL
607 && (dispatcher_version_info
->dispatcher_resolver
610 tree resolver
= NULL_TREE
;
611 gcc_assert (targetm
.generate_version_dispatcher_body
);
612 resolver
= targetm
.generate_version_dispatcher_body (this);
613 gcc_assert (resolver
!= NULL_TREE
);
618 push_cfun (DECL_STRUCT_FUNCTION (decl
));
620 assign_assembler_name_if_neeeded (decl
);
622 /* Make sure to gimplify bodies only once. During analyzing a
623 function we lower it, which will require gimplified nested
624 functions, so we can end up here with an already gimplified
626 if (!gimple_has_body_p (decl
))
627 gimplify_function_tree (decl
);
629 /* Lower the function. */
633 lower_nested_functions (decl
);
634 gcc_assert (!nested
);
636 gimple_register_cfg_hooks ();
637 bitmap_obstack_initialize (NULL
);
638 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
639 free_dominance_info (CDI_POST_DOMINATORS
);
640 free_dominance_info (CDI_DOMINATORS
);
642 bitmap_obstack_release (NULL
);
650 input_location
= saved_loc
;
653 /* C++ frontend produce same body aliases all over the place, even before PCH
654 gets streamed out. It relies on us linking the aliases with their function
655 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
656 first produce aliases without links, but once C++ FE is sure he won't sream
657 PCH we build the links via this function. */
660 symbol_table::process_same_body_aliases (void)
663 FOR_EACH_SYMBOL (node
)
664 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
666 (TREE_CODE (node
->alias_target
) == VAR_DECL
667 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
668 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
669 cpp_implicit_aliases_done
= true;
672 /* Process attributes common for vars and functions. */
675 process_common_attributes (symtab_node
*node
, tree decl
)
677 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
679 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
681 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
682 "%<weakref%> attribute should be accompanied with"
683 " an %<alias%> attribute");
684 DECL_WEAK (decl
) = 0;
685 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
686 DECL_ATTRIBUTES (decl
));
689 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
690 node
->no_reorder
= 1;
693 /* Look for externally_visible and used attributes and mark cgraph nodes
696 We cannot mark the nodes at the point the attributes are processed (in
697 handle_*_attribute) because the copy of the declarations available at that
698 point may not be canonical. For example, in:
701 void f() __attribute__((used));
703 the declaration we see in handle_used_attribute will be the second
704 declaration -- but the front end will subsequently merge that declaration
705 with the original declaration and discard the second declaration.
707 Furthermore, we can't mark these nodes in finalize_function because:
710 void f() __attribute__((externally_visible));
714 So, we walk the nodes at the end of the translation unit, applying the
715 attributes at that point. */
718 process_function_and_variable_attributes (cgraph_node
*first
,
719 varpool_node
*first_var
)
724 for (node
= symtab
->first_function (); node
!= first
;
725 node
= symtab
->next_function (node
))
727 tree decl
= node
->decl
;
728 if (DECL_PRESERVE_P (decl
))
729 node
->mark_force_output ();
730 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
732 if (! TREE_PUBLIC (node
->decl
))
733 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
734 "%<externally_visible%>"
735 " attribute have effect only on public objects");
737 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
738 && (node
->definition
&& !node
->alias
))
740 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
741 "%<weakref%> attribute ignored"
742 " because function is defined");
743 DECL_WEAK (decl
) = 0;
744 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
745 DECL_ATTRIBUTES (decl
));
748 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
749 && !DECL_DECLARED_INLINE_P (decl
)
750 /* redefining extern inline function makes it DECL_UNINLINABLE. */
751 && !DECL_UNINLINABLE (decl
))
752 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
753 "always_inline function might not be inlinable");
755 process_common_attributes (node
, decl
);
757 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
758 vnode
= symtab
->next_variable (vnode
))
760 tree decl
= vnode
->decl
;
761 if (DECL_EXTERNAL (decl
)
762 && DECL_INITIAL (decl
))
763 varpool_node::finalize_decl (decl
);
764 if (DECL_PRESERVE_P (decl
))
765 vnode
->force_output
= true;
766 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
768 if (! TREE_PUBLIC (vnode
->decl
))
769 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
770 "%<externally_visible%>"
771 " attribute have effect only on public objects");
773 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
775 && DECL_INITIAL (decl
))
777 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
778 "%<weakref%> attribute ignored"
779 " because variable is initialized");
780 DECL_WEAK (decl
) = 0;
781 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
782 DECL_ATTRIBUTES (decl
));
784 process_common_attributes (vnode
, decl
);
788 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
789 middle end to output the variable to asm file, if needed or externally
793 varpool_node::finalize_decl (tree decl
)
795 varpool_node
*node
= varpool_node::get_create (decl
);
797 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
799 if (node
->definition
)
801 /* Set definition first before calling notice_global_symbol so that
802 it is available to notice_global_symbol. */
803 node
->definition
= true;
804 notice_global_symbol (decl
);
805 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
806 /* Traditionally we do not eliminate static variables when not
807 optimizing and when not doing toplevel reoder. */
809 || ((!flag_toplevel_reorder
810 && !DECL_COMDAT (node
->decl
)
811 && !DECL_ARTIFICIAL (node
->decl
))))
812 node
->force_output
= true;
814 if (symtab
->state
== CONSTRUCTION
815 && (node
->needed_p () || node
->referred_to_p ()))
817 if (symtab
->state
>= IPA_SSA
)
819 /* Some frontends produce various interface variables after compilation
821 if (symtab
->state
== FINISHED
822 || (!flag_toplevel_reorder
823 && symtab
->state
== EXPANSION
))
824 node
->assemble_decl ();
826 if (DECL_INITIAL (decl
))
827 chkp_register_var_initializer (decl
);
830 /* EDGE is an polymorphic call. Mark all possible targets as reachable
831 and if there is only one target, perform trivial devirtualization.
832 REACHABLE_CALL_TARGETS collects target lists we already walked to
833 avoid udplicate work. */
836 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
842 vec
<cgraph_node
*>targets
843 = possible_polymorphic_call_targets
844 (edge
, &final
, &cache_token
);
846 if (!reachable_call_targets
->add (cache_token
))
848 if (symtab
->dump_file
)
849 dump_possible_polymorphic_call_targets
850 (symtab
->dump_file
, edge
);
852 for (i
= 0; i
< targets
.length (); i
++)
854 /* Do not bother to mark virtual methods in anonymous namespace;
855 either we will find use of virtual table defining it, or it is
857 if (targets
[i
]->definition
859 (TREE_TYPE (targets
[i
]->decl
))
861 && !type_in_anonymous_namespace_p
862 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets
[i
]->decl
))))
863 enqueue_node (targets
[i
]);
867 /* Very trivial devirtualization; when the type is
868 final or anonymous (so we know all its derivation)
869 and there is only one possible virtual call target,
870 make the edge direct. */
873 if (targets
.length () <= 1 && dbg_cnt (devirt
))
876 if (targets
.length () == 1)
879 target
= cgraph_node::create
880 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
882 if (symtab
->dump_file
)
884 fprintf (symtab
->dump_file
,
885 "Devirtualizing call: ");
886 print_gimple_stmt (symtab
->dump_file
,
890 if (dump_enabled_p ())
892 location_t locus
= gimple_location_safe (edge
->call_stmt
);
893 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
894 "devirtualizing call in %s to %s\n",
895 edge
->caller
->name (), target
->name ());
898 edge
->make_direct (target
);
899 edge
->redirect_call_stmt_to_callee ();
901 /* Call to __builtin_unreachable shouldn't be instrumented. */
902 if (!targets
.length ())
903 gimple_call_set_with_bounds (edge
->call_stmt
, false);
905 if (symtab
->dump_file
)
907 fprintf (symtab
->dump_file
,
908 "Devirtualized as: ");
909 print_gimple_stmt (symtab
->dump_file
,
917 /* Issue appropriate warnings for the global declaration DECL. */
920 check_global_declaration (symtab_node
*snode
)
922 tree decl
= snode
->decl
;
924 /* Warn about any function declared static but not defined. We don't
925 warn about variables, because many programs have static variables
926 that exist only to get some text into the object file. */
927 if (TREE_CODE (decl
) == FUNCTION_DECL
928 && DECL_INITIAL (decl
) == 0
929 && DECL_EXTERNAL (decl
)
930 && ! DECL_ARTIFICIAL (decl
)
931 && ! TREE_NO_WARNING (decl
)
932 && ! TREE_PUBLIC (decl
)
933 && (warn_unused_function
934 || snode
->referred_to_p (/*include_self=*/false)))
936 if (snode
->referred_to_p (/*include_self=*/false))
937 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
939 warning (OPT_Wunused_function
, "%q+F declared %<static%> but never defined", decl
);
940 /* This symbol is effectively an "extern" declaration now. */
941 TREE_PUBLIC (decl
) = 1;
944 /* Warn about static fns or vars defined but not used. */
945 if (((warn_unused_function
&& TREE_CODE (decl
) == FUNCTION_DECL
)
946 || (((warn_unused_variable
&& ! TREE_READONLY (decl
))
947 || (warn_unused_const_variable
&& TREE_READONLY (decl
)))
948 && TREE_CODE (decl
) == VAR_DECL
))
949 && ! DECL_IN_SYSTEM_HEADER (decl
)
950 && ! snode
->referred_to_p (/*include_self=*/false)
951 /* This TREE_USED check is needed in addition to referred_to_p
952 above, because the `__unused__' attribute is not being
953 considered for referred_to_p. */
954 && ! TREE_USED (decl
)
955 /* The TREE_USED bit for file-scope decls is kept in the identifier,
956 to handle multiple external decls in different scopes. */
957 && ! (DECL_NAME (decl
) && TREE_USED (DECL_NAME (decl
)))
958 && ! DECL_EXTERNAL (decl
)
959 && ! DECL_ARTIFICIAL (decl
)
960 && ! DECL_ABSTRACT_ORIGIN (decl
)
961 && ! TREE_PUBLIC (decl
)
962 /* A volatile variable might be used in some non-obvious way. */
963 && (! VAR_P (decl
) || ! TREE_THIS_VOLATILE (decl
))
964 /* Global register variables must be declared to reserve them. */
965 && ! (TREE_CODE (decl
) == VAR_DECL
&& DECL_REGISTER (decl
))
966 /* Global ctors and dtors are called by the runtime. */
967 && (TREE_CODE (decl
) != FUNCTION_DECL
968 || (!DECL_STATIC_CONSTRUCTOR (decl
)
969 && !DECL_STATIC_DESTRUCTOR (decl
)))
970 /* Otherwise, ask the language. */
971 && lang_hooks
.decls
.warn_unused_global (decl
))
972 warning_at (DECL_SOURCE_LOCATION (decl
),
973 (TREE_CODE (decl
) == FUNCTION_DECL
)
974 ? OPT_Wunused_function
975 : (TREE_READONLY (decl
)
976 ? OPT_Wunused_const_variable
977 : OPT_Wunused_variable
),
978 "%qD defined but not used", decl
);
981 /* Discover all functions and variables that are trivially needed, analyze
982 them as well as all functions and variables referred by them */
983 static cgraph_node
*first_analyzed
;
984 static varpool_node
*first_analyzed_var
;
986 /* FIRST_TIME is set to TRUE for the first time we are called for a
987 translation unit from finalize_compilation_unit() or false
991 analyze_functions (bool first_time
)
993 /* Keep track of already processed nodes when called multiple times for
994 intermodule optimization. */
995 cgraph_node
*first_handled
= first_analyzed
;
996 varpool_node
*first_handled_var
= first_analyzed_var
;
997 hash_set
<void *> reachable_call_targets
;
1003 bool changed
= true;
1004 location_t saved_loc
= input_location
;
1006 bitmap_obstack_initialize (NULL
);
1007 symtab
->state
= CONSTRUCTION
;
1008 input_location
= UNKNOWN_LOCATION
;
1010 /* Ugly, but the fixup can not happen at a time same body alias is created;
1011 C++ FE is confused about the COMDAT groups being right. */
1012 if (symtab
->cpp_implicit_aliases_done
)
1013 FOR_EACH_SYMBOL (node
)
1014 if (node
->cpp_implicit_alias
)
1015 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
1016 build_type_inheritance_graph ();
1018 /* Analysis adds static variables that in turn adds references to new functions.
1019 So we need to iterate the process until it stabilize. */
1023 process_function_and_variable_attributes (first_analyzed
,
1024 first_analyzed_var
);
1026 /* First identify the trivially needed symbols. */
1027 for (node
= symtab
->first_symbol ();
1028 node
!= first_analyzed
1029 && node
!= first_analyzed_var
; node
= node
->next
)
1031 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1032 node
->get_comdat_group_id ();
1033 if (node
->needed_p ())
1035 enqueue_node (node
);
1036 if (!changed
&& symtab
->dump_file
)
1037 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
1039 if (symtab
->dump_file
)
1040 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
1041 if (!changed
&& symtab
->dump_file
)
1042 fprintf (symtab
->dump_file
, "\n");
1044 if (node
== first_analyzed
1045 || node
== first_analyzed_var
)
1048 symtab
->process_new_functions ();
1049 first_analyzed_var
= symtab
->first_variable ();
1050 first_analyzed
= symtab
->first_function ();
1052 if (changed
&& symtab
->dump_file
)
1053 fprintf (symtab
->dump_file
, "\n");
1055 /* Lower representation, build callgraph edges and references for all trivially
1056 needed symbols and all symbols referred by them. */
1057 while (queued_nodes
!= &symtab_terminator
)
1060 node
= queued_nodes
;
1061 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
1062 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1063 if (cnode
&& cnode
->definition
)
1066 tree decl
= cnode
->decl
;
1068 /* ??? It is possible to create extern inline function
1069 and later using weak alias attribute to kill its body.
1070 See gcc.c-torture/compile/20011119-1.c */
1071 if (!DECL_STRUCT_FUNCTION (decl
)
1073 && !cnode
->thunk
.thunk_p
1074 && !cnode
->dispatcher_function
)
1077 cnode
->local
.redefined_extern_inline
= true;
1081 if (!cnode
->analyzed
)
1084 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
1085 if (edge
->callee
->definition
1086 && (!DECL_EXTERNAL (edge
->callee
->decl
)
1087 /* When not optimizing, do not try to analyze extern
1088 inline functions. Doing so is pointless. */
1089 || opt_for_fn (edge
->callee
->decl
, optimize
)
1090 /* Weakrefs needs to be preserved. */
1091 || edge
->callee
->alias
1092 /* always_inline functions are inlined aven at -O0. */
1095 DECL_ATTRIBUTES (edge
->callee
->decl
))
1096 /* Multiversioned functions needs the dispatcher to
1097 be produced locally even for extern functions. */
1098 || edge
->callee
->function_version ()))
1099 enqueue_node (edge
->callee
);
1100 if (opt_for_fn (cnode
->decl
, optimize
)
1101 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
1105 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1107 next
= edge
->next_callee
;
1108 if (edge
->indirect_info
->polymorphic
)
1109 walk_polymorphic_call_targets (&reachable_call_targets
,
1114 /* If decl is a clone of an abstract function,
1115 mark that abstract function so that we don't release its body.
1116 The DECL_INITIAL() of that abstract function declaration
1117 will be later needed to output debug info. */
1118 if (DECL_ABSTRACT_ORIGIN (decl
))
1120 cgraph_node
*origin_node
1121 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1122 origin_node
->used_as_abstract_origin
= true;
1127 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1128 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1132 if (node
->same_comdat_group
)
1135 for (next
= node
->same_comdat_group
;
1137 next
= next
->same_comdat_group
)
1138 if (!next
->comdat_local_p ())
1139 enqueue_node (next
);
1141 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1142 if (ref
->referred
->definition
1143 && (!DECL_EXTERNAL (ref
->referred
->decl
)
1144 || ((TREE_CODE (ref
->referred
->decl
) != FUNCTION_DECL
1146 || (TREE_CODE (ref
->referred
->decl
) == FUNCTION_DECL
1147 && opt_for_fn (ref
->referred
->decl
, optimize
))
1149 || ref
->referred
->alias
)))
1150 enqueue_node (ref
->referred
);
1151 symtab
->process_new_functions ();
1154 update_type_inheritance_graph ();
1156 /* Collect entry points to the unit. */
1157 if (symtab
->dump_file
)
1159 fprintf (symtab
->dump_file
, "\n\nInitial ");
1160 symtab_node::dump_table (symtab
->dump_file
);
1166 FOR_EACH_SYMBOL (snode
)
1167 check_global_declaration (snode
);
1170 if (symtab
->dump_file
)
1171 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1173 for (node
= symtab
->first_symbol ();
1174 node
!= first_handled
1175 && node
!= first_handled_var
; node
= next
)
1178 if (!node
->aux
&& !node
->referred_to_p ())
1180 if (symtab
->dump_file
)
1181 fprintf (symtab
->dump_file
, " %s", node
->name ());
1183 /* See if the debugger can use anything before the DECL
1184 passes away. Perhaps it can notice a DECL that is now a
1185 constant and can tag the early DIE with an appropriate
1188 Otherwise, this is the last chance the debug_hooks have
1189 at looking at optimized away DECLs, since
1190 late_global_decl will subsequently be called from the
1191 contents of the now pruned symbol table. */
1192 if (!decl_function_context (node
->decl
))
1193 (*debug_hooks
->late_global_decl
) (node
->decl
);
1198 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1200 tree decl
= node
->decl
;
1202 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1204 && !cnode
->thunk
.thunk_p
)
1207 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1209 || gimple_has_body_p (decl
));
1210 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1214 for (;node
; node
= node
->next
)
1216 first_analyzed
= symtab
->first_function ();
1217 first_analyzed_var
= symtab
->first_variable ();
1218 if (symtab
->dump_file
)
1220 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1221 symtab_node::dump_table (symtab
->dump_file
);
1223 bitmap_obstack_release (NULL
);
1225 /* Initialize assembler name hash, in particular we want to trigger C++
1226 mangling and same body alias creation before we free DECL_ARGUMENTS
1229 symtab
->symtab_initialize_asm_name_hash ();
1231 input_location
= saved_loc
;
1234 /* Translate the ugly representation of aliases as alias pairs into nice
1235 representation in callgraph. We don't handle all cases yet,
1239 handle_alias_pairs (void)
1244 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1246 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1248 /* Weakrefs with target not defined in current unit are easy to handle:
1249 they behave just as external variables except we need to note the
1250 alias flag to later output the weakref pseudo op into asm file. */
1252 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1254 symtab_node
*node
= symtab_node::get (p
->decl
);
1257 node
->alias_target
= p
->target
;
1258 node
->weakref
= true;
1260 node
->transparent_alias
= true;
1262 alias_pairs
->unordered_remove (i
);
1265 else if (!target_node
)
1267 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1268 symtab_node
*node
= symtab_node::get (p
->decl
);
1270 node
->alias
= false;
1271 alias_pairs
->unordered_remove (i
);
1275 if (DECL_EXTERNAL (target_node
->decl
)
1276 /* We use local aliases for C++ thunks to force the tailcall
1277 to bind locally. This is a hack - to keep it working do
1278 the following (which is not strictly correct). */
1279 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1280 || ! DECL_VIRTUAL_P (target_node
->decl
))
1281 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1283 error ("%q+D aliased to external symbol %qE",
1284 p
->decl
, p
->target
);
1287 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1288 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1290 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1291 if (src_node
&& src_node
->definition
)
1293 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1294 alias_pairs
->unordered_remove (i
);
1296 else if (TREE_CODE (p
->decl
) == VAR_DECL
1297 && target_node
&& is_a
<varpool_node
*> (target_node
))
1299 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1300 alias_pairs
->unordered_remove (i
);
1304 error ("%q+D alias in between function and variable is not supported",
1306 warning (0, "%q+D aliased declaration",
1308 alias_pairs
->unordered_remove (i
);
1311 vec_free (alias_pairs
);
1315 /* Figure out what functions we want to assemble. */
1318 mark_functions_to_output (void)
1320 bool check_same_comdat_groups
= false;
1324 FOR_EACH_FUNCTION (node
)
1325 gcc_assert (!node
->process
);
1327 FOR_EACH_FUNCTION (node
)
1329 tree decl
= node
->decl
;
1331 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1335 /* We need to output all local functions that are used and not
1336 always inlined, as well as those that are reachable from
1337 outside the current compilation unit. */
1339 && !node
->thunk
.thunk_p
1341 && !node
->global
.inlined_to
1342 && !TREE_ASM_WRITTEN (decl
)
1343 && !DECL_EXTERNAL (decl
))
1346 if (node
->same_comdat_group
)
1349 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1351 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1352 if (!next
->thunk
.thunk_p
&& !next
->alias
1353 && !next
->comdat_local_p ())
1357 else if (node
->same_comdat_group
)
1360 check_same_comdat_groups
= true;
1364 /* We should've reclaimed all functions that are not needed. */
1366 && !node
->global
.inlined_to
1367 && gimple_has_body_p (decl
)
1368 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1369 are inside partition, we can end up not removing the body since we no longer
1370 have analyzed node pointing to it. */
1371 && !node
->in_other_partition
1374 && !DECL_EXTERNAL (decl
))
1377 internal_error ("failed to reclaim unneeded function");
1379 gcc_assert (node
->global
.inlined_to
1380 || !gimple_has_body_p (decl
)
1381 || node
->in_other_partition
1383 || DECL_ARTIFICIAL (decl
)
1384 || DECL_EXTERNAL (decl
));
1389 if (flag_checking
&& check_same_comdat_groups
)
1390 FOR_EACH_FUNCTION (node
)
1391 if (node
->same_comdat_group
&& !node
->process
)
1393 tree decl
= node
->decl
;
1394 if (!node
->global
.inlined_to
1395 && gimple_has_body_p (decl
)
1396 /* FIXME: in an ltrans unit when the offline copy is outside a
1397 partition but inline copies are inside a partition, we can
1398 end up not removing the body since we no longer have an
1399 analyzed node pointing to it. */
1400 && !node
->in_other_partition
1402 && !DECL_EXTERNAL (decl
))
1405 internal_error ("failed to reclaim unneeded function in same "
1411 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1412 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1414 Set current_function_decl and cfun to newly constructed empty function body.
1415 return basic block in the function body. */
1418 init_lowered_empty_function (tree decl
, bool in_ssa
, gcov_type count
)
1423 current_function_decl
= decl
;
1424 allocate_struct_function (decl
, false);
1425 gimple_register_cfg_hooks ();
1426 init_empty_tree_cfg ();
1430 init_tree_ssa (cfun
);
1431 init_ssa_operands (cfun
);
1432 cfun
->gimple_df
->in_ssa_p
= true;
1433 cfun
->curr_properties
|= PROP_ssa
;
1436 DECL_INITIAL (decl
) = make_node (BLOCK
);
1438 DECL_SAVED_TREE (decl
) = error_mark_node
;
1439 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1440 | PROP_cfg
| PROP_loops
);
1442 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1443 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1444 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1446 /* Create BB for body of the function and connect it properly. */
1447 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1448 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1449 EXIT_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1450 EXIT_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1451 bb
= create_basic_block (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1453 bb
->frequency
= BB_FREQ_MAX
;
1454 e
= make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1456 e
->probability
= REG_BR_PROB_BASE
;
1457 e
= make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1459 e
->probability
= REG_BR_PROB_BASE
;
1460 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1465 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1466 offset indicated by VIRTUAL_OFFSET, if that is
1467 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1468 zero for a result adjusting thunk. */
1471 thunk_adjust (gimple_stmt_iterator
* bsi
,
1472 tree ptr
, bool this_adjusting
,
1473 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1479 && fixed_offset
!= 0)
1481 stmt
= gimple_build_assign
1482 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1485 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1488 /* If there's a virtual offset, look up that value in the vtable and
1489 adjust the pointer again. */
1496 if (!vtable_entry_type
)
1498 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1499 TREE_TYPE (vfunc_type
) = integer_type_node
;
1500 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1501 layout_type (vfunc_type
);
1503 vtable_entry_type
= build_pointer_type (vfunc_type
);
1507 create_tmp_reg (build_pointer_type
1508 (build_pointer_type (vtable_entry_type
)), "vptr");
1510 /* The vptr is always at offset zero in the object. */
1511 stmt
= gimple_build_assign (vtabletmp
,
1512 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1514 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1516 /* Form the vtable address. */
1517 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1519 stmt
= gimple_build_assign (vtabletmp2
,
1520 build_simple_mem_ref (vtabletmp
));
1521 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1523 /* Find the entry with the vcall offset. */
1524 stmt
= gimple_build_assign (vtabletmp2
,
1525 fold_build_pointer_plus_loc (input_location
,
1528 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1530 /* Get the offset itself. */
1531 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1533 stmt
= gimple_build_assign (vtabletmp3
,
1534 build_simple_mem_ref (vtabletmp2
));
1535 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1537 /* Adjust the `this' pointer. */
1538 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1539 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1540 GSI_CONTINUE_LINKING
);
1544 && fixed_offset
!= 0)
1545 /* Adjust the pointer by the constant. */
1549 if (TREE_CODE (ptr
) == VAR_DECL
)
1553 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1554 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1555 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1557 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1558 ptrtmp
, fixed_offset
);
1561 /* Emit the statement and gimplify the adjustment expression. */
1562 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1563 stmt
= gimple_build_assign (ret
, ptr
);
1564 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1569 /* Expand thunk NODE to gimple if possible.
1570 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1571 no assembler is produced.
1572 When OUTPUT_ASM_THUNK is true, also produce assembler for
1573 thunks that are not lowered. */
1576 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1578 bool this_adjusting
= thunk
.this_adjusting
;
1579 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1580 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1581 tree virtual_offset
= NULL
;
1582 tree alias
= callees
->callee
->decl
;
1583 tree thunk_fndecl
= decl
;
1586 /* Instrumentation thunk is the same function with
1587 a different signature. Never need to expand it. */
1588 if (thunk
.add_pointer_bounds_args
)
1591 if (!force_gimple_thunk
&& this_adjusting
1592 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1593 virtual_value
, alias
))
1597 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1599 if (!output_asm_thunks
)
1606 get_untransformed_body ();
1607 a
= DECL_ARGUMENTS (thunk_fndecl
);
1609 current_function_decl
= thunk_fndecl
;
1611 /* Ensure thunks are emitted in their correct sections. */
1612 resolve_unique_section (thunk_fndecl
, 0,
1613 flag_function_sections
);
1615 DECL_RESULT (thunk_fndecl
)
1616 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1617 RESULT_DECL
, 0, restype
);
1618 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1619 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1621 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1623 fn_block
= make_node (BLOCK
);
1624 BLOCK_VARS (fn_block
) = a
;
1625 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1626 allocate_struct_function (thunk_fndecl
, false);
1627 init_function_start (thunk_fndecl
);
1629 insn_locations_init ();
1630 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1631 prologue_location
= curr_insn_location ();
1632 assemble_start_function (thunk_fndecl
, fnname
);
1634 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1635 fixed_offset
, virtual_value
, alias
);
1637 assemble_end_function (thunk_fndecl
, fnname
);
1638 insn_locations_finalize ();
1639 init_insn_lengths ();
1640 free_after_compilation (cfun
);
1641 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1642 thunk
.thunk_p
= false;
1645 else if (stdarg_p (TREE_TYPE (thunk_fndecl
)))
1647 error ("generic thunk code fails for method %qD which uses %<...%>",
1649 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1656 basic_block bb
, then_bb
, else_bb
, return_bb
;
1657 gimple_stmt_iterator bsi
;
1667 bool alias_is_noreturn
= TREE_THIS_VOLATILE (alias
);
1669 /* We may be called from expand_thunk that releses body except for
1670 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1671 if (in_lto_p
&& !force_gimple_thunk
)
1672 get_untransformed_body ();
1673 a
= DECL_ARGUMENTS (thunk_fndecl
);
1675 current_function_decl
= thunk_fndecl
;
1677 /* Ensure thunks are emitted in their correct sections. */
1678 resolve_unique_section (thunk_fndecl
, 0,
1679 flag_function_sections
);
1681 DECL_IGNORED_P (thunk_fndecl
) = 1;
1682 bitmap_obstack_initialize (NULL
);
1684 if (thunk
.virtual_offset_p
)
1685 virtual_offset
= size_int (virtual_value
);
1687 /* Build the return declaration for the function. */
1688 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1689 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1691 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1692 DECL_ARTIFICIAL (resdecl
) = 1;
1693 DECL_IGNORED_P (resdecl
) = 1;
1694 DECL_RESULT (thunk_fndecl
) = resdecl
;
1695 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1698 resdecl
= DECL_RESULT (thunk_fndecl
);
1700 bb
= then_bb
= else_bb
= return_bb
1701 = init_lowered_empty_function (thunk_fndecl
, true, count
);
1703 bsi
= gsi_start_bb (bb
);
1705 /* Build call to the function being thunked. */
1706 if (!VOID_TYPE_P (restype
) && !alias_is_noreturn
)
1708 if (DECL_BY_REFERENCE (resdecl
))
1710 restmp
= gimple_fold_indirect_ref (resdecl
);
1712 restmp
= build2 (MEM_REF
,
1713 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias
))),
1715 build_int_cst (TREE_TYPE
1716 (DECL_RESULT (alias
)), 0));
1718 else if (!is_gimple_reg_type (restype
))
1720 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
)))
1724 if (TREE_CODE (restmp
) == VAR_DECL
)
1725 add_local_decl (cfun
, restmp
);
1726 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1729 restmp
= create_tmp_var (restype
, "retval");
1732 restmp
= create_tmp_reg (restype
, "retval");
1735 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1737 auto_vec
<tree
> vargs (nargs
);
1742 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1744 arg
= DECL_CHAIN (a
);
1749 for (; i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1752 if (!is_gimple_val (arg
))
1754 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1755 (TREE_TYPE (arg
)), "arg");
1756 gimple
*stmt
= gimple_build_assign (tmp
, arg
);
1757 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1759 vargs
.quick_push (tmp
);
1761 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1762 callees
->call_stmt
= call
;
1763 gimple_call_set_from_thunk (call
, true);
1764 gimple_call_set_with_bounds (call
, instrumentation_clone
);
1766 /* Return slot optimization is always possible and in fact requred to
1767 return values with DECL_BY_REFERENCE. */
1768 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
))
1769 && (!is_gimple_reg_type (TREE_TYPE (resdecl
))
1770 || DECL_BY_REFERENCE (resdecl
)))
1771 gimple_call_set_return_slot_opt (call
, true);
1773 if (restmp
&& !alias_is_noreturn
)
1775 gimple_call_set_lhs (call
, restmp
);
1776 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1777 TREE_TYPE (TREE_TYPE (alias
))));
1779 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1780 if (!alias_is_noreturn
)
1782 if (instrumentation_clone
1783 && !DECL_BY_REFERENCE (resdecl
)
1785 && BOUNDED_P (restmp
))
1787 resbnd
= chkp_insert_retbnd_call (NULL
, restmp
, &bsi
);
1788 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi
))),
1789 as_a
<gcall
*> (gsi_stmt (bsi
)),
1790 callees
->count
, callees
->frequency
);
1793 if (restmp
&& !this_adjusting
1794 && (fixed_offset
|| virtual_offset
))
1796 tree true_label
= NULL_TREE
;
1798 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1802 /* If the return type is a pointer, we need to
1803 protect against NULL. We know there will be an
1804 adjustment, because that's why we're emitting a
1806 then_bb
= create_basic_block (NULL
, bb
);
1807 then_bb
->count
= count
- count
/ 16;
1808 then_bb
->frequency
= BB_FREQ_MAX
- BB_FREQ_MAX
/ 16;
1809 return_bb
= create_basic_block (NULL
, then_bb
);
1810 return_bb
->count
= count
;
1811 return_bb
->frequency
= BB_FREQ_MAX
;
1812 else_bb
= create_basic_block (NULL
, else_bb
);
1813 then_bb
->count
= count
/ 16;
1814 then_bb
->frequency
= BB_FREQ_MAX
/ 16;
1815 add_bb_to_loop (then_bb
, bb
->loop_father
);
1816 add_bb_to_loop (return_bb
, bb
->loop_father
);
1817 add_bb_to_loop (else_bb
, bb
->loop_father
);
1818 remove_edge (single_succ_edge (bb
));
1819 true_label
= gimple_block_label (then_bb
);
1820 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1821 build_zero_cst (TREE_TYPE (restmp
)),
1822 NULL_TREE
, NULL_TREE
);
1823 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1824 e
= make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1825 e
->probability
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 16;
1826 e
->count
= count
- count
/ 16;
1827 e
= make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1828 e
->probability
= REG_BR_PROB_BASE
/ 16;
1829 e
->count
= count
/ 16;
1830 e
= make_edge (return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1831 e
->probability
= REG_BR_PROB_BASE
;
1833 e
= make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1834 e
->probability
= REG_BR_PROB_BASE
;
1835 e
->count
= count
- count
/ 16;
1836 e
= make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1837 e
->probability
= REG_BR_PROB_BASE
;
1838 e
->count
= count
/ 16;
1839 bsi
= gsi_last_bb (then_bb
);
1842 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1843 fixed_offset
, virtual_offset
);
1847 bsi
= gsi_last_bb (else_bb
);
1848 stmt
= gimple_build_assign (restmp
,
1849 build_zero_cst (TREE_TYPE (restmp
)));
1850 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1851 bsi
= gsi_last_bb (return_bb
);
1855 gimple_call_set_tail (call
, true);
1857 /* Build return value. */
1858 if (!DECL_BY_REFERENCE (resdecl
))
1859 ret
= gimple_build_return (restmp
);
1861 ret
= gimple_build_return (resdecl
);
1862 gimple_return_set_retbnd (ret
, resbnd
);
1864 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1868 gimple_call_set_tail (call
, true);
1869 remove_edge (single_succ_edge (bb
));
1872 cfun
->gimple_df
->in_ssa_p
= true;
1873 profile_status_for_fn (cfun
)
1874 = count
? PROFILE_READ
: PROFILE_GUESSED
;
1875 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1876 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
1877 delete_unreachable_blocks ();
1878 update_ssa (TODO_update_ssa
);
1879 checking_verify_flow_info ();
1880 free_dominance_info (CDI_DOMINATORS
);
1882 /* Since we want to emit the thunk, we explicitly mark its name as
1884 thunk
.thunk_p
= false;
1886 bitmap_obstack_release (NULL
);
1888 current_function_decl
= NULL
;
1893 /* Assemble thunks and aliases associated to node. */
1896 cgraph_node::assemble_thunks_and_aliases (void)
1901 for (e
= callers
; e
;)
1902 if (e
->caller
->thunk
.thunk_p
1903 && !e
->caller
->thunk
.add_pointer_bounds_args
)
1905 cgraph_node
*thunk
= e
->caller
;
1908 thunk
->expand_thunk (true, false);
1909 thunk
->assemble_thunks_and_aliases ();
1914 FOR_EACH_ALIAS (this, ref
)
1916 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1917 if (!alias
->transparent_alias
)
1919 bool saved_written
= TREE_ASM_WRITTEN (decl
);
1921 /* Force assemble_alias to really output the alias this time instead
1922 of buffering it in same alias pairs. */
1923 TREE_ASM_WRITTEN (decl
) = 1;
1924 do_assemble_alias (alias
->decl
,
1925 DECL_ASSEMBLER_NAME (decl
));
1926 alias
->assemble_thunks_and_aliases ();
1927 TREE_ASM_WRITTEN (decl
) = saved_written
;
1932 /* Expand function specified by node. */
1935 cgraph_node::expand (void)
1937 location_t saved_loc
;
1939 /* We ought to not compile any inline clones. */
1940 gcc_assert (!global
.inlined_to
);
1942 announce_function (decl
);
1944 gcc_assert (lowered
);
1945 get_untransformed_body ();
1947 /* Generate RTL for the body of DECL. */
1949 timevar_push (TV_REST_OF_COMPILATION
);
1951 gcc_assert (symtab
->global_info_ready
);
1953 /* Initialize the default bitmap obstack. */
1954 bitmap_obstack_initialize (NULL
);
1956 /* Initialize the RTL code for the function. */
1957 saved_loc
= input_location
;
1958 input_location
= DECL_SOURCE_LOCATION (decl
);
1960 gcc_assert (DECL_STRUCT_FUNCTION (decl
));
1961 push_cfun (DECL_STRUCT_FUNCTION (decl
));
1962 init_function_start (decl
);
1964 gimple_register_cfg_hooks ();
1966 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1968 execute_all_ipa_transforms ();
1970 /* Perform all tree transforms and optimizations. */
1972 /* Signal the start of passes. */
1973 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1975 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
1977 /* Signal the end of passes. */
1978 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1980 bitmap_obstack_release (®_obstack
);
1982 /* Release the default bitmap obstack. */
1983 bitmap_obstack_release (NULL
);
1985 /* If requested, warn about function definitions where the function will
1986 return a value (usually of some struct or union type) which itself will
1987 take up a lot of stack space. */
1988 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1990 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1992 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1993 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1994 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1997 unsigned int size_as_int
1998 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
2000 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
2001 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
2004 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
2005 decl
, larger_than_size
);
2009 gimple_set_body (decl
, NULL
);
2010 if (DECL_STRUCT_FUNCTION (decl
) == 0
2011 && !cgraph_node::get (decl
)->origin
)
2013 /* Stop pointing to the local nodes about to be freed.
2014 But DECL_INITIAL must remain nonzero so we know this
2015 was an actual function definition.
2016 For a nested function, this is done in c_pop_function_context.
2017 If rest_of_compilation set this to 0, leave it 0. */
2018 if (DECL_INITIAL (decl
) != 0)
2019 DECL_INITIAL (decl
) = error_mark_node
;
2022 input_location
= saved_loc
;
2025 timevar_pop (TV_REST_OF_COMPILATION
);
2027 /* Make sure that BE didn't give up on compiling. */
2028 gcc_assert (TREE_ASM_WRITTEN (decl
));
2032 /* It would make a lot more sense to output thunks before function body to get more
2033 forward and lest backwarding jumps. This however would need solving problem
2034 with comdats. See PR48668. Also aliases must come after function itself to
2035 make one pass assemblers, like one on AIX, happy. See PR 50689.
2036 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2038 assemble_thunks_and_aliases ();
2040 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2041 points to the dead function body. */
2043 remove_all_references ();
2046 /* Node comparer that is responsible for the order that corresponds
2047 to time when a function was launched for the first time. */
2050 node_cmp (const void *pa
, const void *pb
)
2052 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
2053 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
2055 /* Functions with time profile must be before these without profile. */
2056 if (!a
->tp_first_run
|| !b
->tp_first_run
)
2057 return a
->tp_first_run
- b
->tp_first_run
;
2059 return a
->tp_first_run
!= b
->tp_first_run
2060 ? b
->tp_first_run
- a
->tp_first_run
2061 : b
->order
- a
->order
;
2064 /* Expand all functions that must be output.
2066 Attempt to topologically sort the nodes so function is output when
2067 all called functions are already assembled to allow data to be
2068 propagated across the callgraph. Use a stack to get smaller distance
2069 between a function and its callees (later we may choose to use a more
2070 sophisticated algorithm for function reordering; we will likely want
2071 to use subsections to make the output functions appear in top-down
2075 expand_all_functions (void)
2078 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
2079 symtab
->cgraph_count
);
2080 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
2081 int order_pos
, new_order_pos
= 0;
2084 order_pos
= ipa_reverse_postorder (order
);
2085 gcc_assert (order_pos
== symtab
->cgraph_count
);
2087 /* Garbage collector may remove inline clones we eliminate during
2088 optimization. So we must be sure to not reference them. */
2089 for (i
= 0; i
< order_pos
; i
++)
2090 if (order
[i
]->process
)
2091 order
[new_order_pos
++] = order
[i
];
2093 if (flag_profile_reorder_functions
)
2094 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
2096 for (i
= new_order_pos
- 1; i
>= 0; i
--)
2102 expanded_func_count
++;
2103 if(node
->tp_first_run
)
2104 profiled_func_count
++;
2106 if (symtab
->dump_file
)
2107 fprintf (symtab
->dump_file
,
2108 "Time profile order in expand_all_functions:%s:%d\n",
2109 node
->asm_name (), node
->tp_first_run
);
2116 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
2117 main_input_filename
, profiled_func_count
, expanded_func_count
);
2119 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
2120 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
2121 profiled_func_count
, expanded_func_count
);
2123 symtab
->process_new_functions ();
2124 free_gimplify_stack ();
2129 /* This is used to sort the node types by the cgraph order number. */
2131 enum cgraph_order_sort_kind
2133 ORDER_UNDEFINED
= 0,
2139 struct cgraph_order_sort
2141 enum cgraph_order_sort_kind kind
;
2150 /* Output all functions, variables, and asm statements in the order
2151 according to their order fields, which is the order in which they
2152 appeared in the file. This implements -fno-toplevel-reorder. In
2153 this mode we may output functions and variables which don't really
2155 When NO_REORDER is true only do this for symbols marked no reorder. */
2158 output_in_order (bool no_reorder
)
2161 cgraph_order_sort
*nodes
;
2166 max
= symtab
->order
;
2167 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
2169 FOR_EACH_DEFINED_FUNCTION (pf
)
2171 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
2173 if (no_reorder
&& !pf
->no_reorder
)
2176 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2177 nodes
[i
].kind
= ORDER_FUNCTION
;
2182 FOR_EACH_DEFINED_VARIABLE (pv
)
2183 if (!DECL_EXTERNAL (pv
->decl
))
2185 if (no_reorder
&& !pv
->no_reorder
)
2188 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2189 nodes
[i
].kind
= ORDER_VAR
;
2193 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
2196 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2197 nodes
[i
].kind
= ORDER_ASM
;
2201 /* In toplevel reorder mode we output all statics; mark them as needed. */
2203 for (i
= 0; i
< max
; ++i
)
2204 if (nodes
[i
].kind
== ORDER_VAR
)
2205 nodes
[i
].u
.v
->finalize_named_section_flags ();
2207 for (i
= 0; i
< max
; ++i
)
2209 switch (nodes
[i
].kind
)
2211 case ORDER_FUNCTION
:
2212 nodes
[i
].u
.f
->process
= 0;
2213 nodes
[i
].u
.f
->expand ();
2217 #ifdef ACCEL_COMPILER
2218 /* Do not assemble "omp declare target link" vars. */
2219 if (DECL_HAS_VALUE_EXPR_P (nodes
[i
].u
.v
->decl
)
2220 && lookup_attribute ("omp declare target link",
2221 DECL_ATTRIBUTES (nodes
[i
].u
.v
->decl
)))
2224 nodes
[i
].u
.v
->assemble_decl ();
2228 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2231 case ORDER_UNDEFINED
:
2239 symtab
->clear_asm_symbols ();
2247 gcc::pass_manager
*passes
= g
->get_passes ();
2250 current_function_decl
= NULL
;
2251 gimple_register_cfg_hooks ();
2252 bitmap_obstack_initialize (NULL
);
2254 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2258 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2263 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2264 devirtualization and other changes where removal iterate. */
2265 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2267 /* If pass_all_early_optimizations was not scheduled, the state of
2268 the cgraph will not be properly updated. Update it now. */
2269 if (symtab
->state
< IPA_SSA
)
2270 symtab
->state
= IPA_SSA
;
2274 /* Generate coverage variables and constructors. */
2277 /* Process new functions added. */
2279 current_function_decl
= NULL
;
2280 symtab
->process_new_functions ();
2282 execute_ipa_summary_passes
2283 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2286 /* Some targets need to handle LTO assembler output specially. */
2287 if (flag_generate_lto
|| flag_generate_offload
)
2288 targetm
.asm_out
.lto_start ();
2292 if (g
->have_offload
)
2294 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2295 lto_stream_offload_p
= true;
2296 ipa_write_summaries ();
2297 lto_stream_offload_p
= false;
2301 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2302 lto_stream_offload_p
= false;
2303 ipa_write_summaries ();
2307 if (flag_generate_lto
|| flag_generate_offload
)
2308 targetm
.asm_out
.lto_end ();
2310 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2311 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2312 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2314 bitmap_obstack_release (NULL
);
2318 /* Return string alias is alias of. */
2321 get_alias_symbol (tree decl
)
2323 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2324 return get_identifier (TREE_STRING_POINTER
2325 (TREE_VALUE (TREE_VALUE (alias
))));
2329 /* Weakrefs may be associated to external decls and thus not output
2330 at expansion time. Emit all necessary aliases. */
2333 symbol_table::output_weakrefs (void)
2337 FOR_EACH_SYMBOL (node
)
2339 && !TREE_ASM_WRITTEN (node
->decl
)
2340 && (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
2341 || !cnode
->instrumented_version
2342 || !TREE_ASM_WRITTEN (cnode
->instrumented_version
->decl
))
2347 /* Weakrefs are special by not requiring target definition in current
2348 compilation unit. It is thus bit hard to work out what we want to
2350 When alias target is defined, we need to fetch it from symtab reference,
2351 otherwise it is pointed to by alias_target. */
2352 if (node
->alias_target
)
2353 target
= (DECL_P (node
->alias_target
)
2354 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2355 : node
->alias_target
);
2356 else if (node
->analyzed
)
2357 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2361 target
= get_alias_symbol (node
->decl
);
2363 do_assemble_alias (node
->decl
, target
);
2367 /* Perform simple optimizations based on callgraph. */
2370 symbol_table::compile (void)
2375 symtab_node::checking_verify_symtab_nodes ();
2377 timevar_push (TV_CGRAPHOPT
);
2378 if (pre_ipa_mem_report
)
2380 fprintf (stderr
, "Memory consumption before IPA\n");
2381 dump_memory_report (false);
2384 fprintf (stderr
, "Performing interprocedural optimizations\n");
2387 /* Offloading requires LTO infrastructure. */
2388 if (!in_lto_p
&& g
->have_offload
)
2389 flag_generate_offload
= 1;
2391 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2392 if (flag_generate_lto
|| flag_generate_offload
)
2393 lto_streamer_hooks_init ();
2395 /* Don't run the IPA passes if there was any error or sorry messages. */
2399 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2401 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2403 timevar_pop (TV_CGRAPHOPT
);
2407 global_info_ready
= true;
2410 fprintf (dump_file
, "Optimized ");
2411 symtab_node:: dump_table (dump_file
);
2413 if (post_ipa_mem_report
)
2415 fprintf (stderr
, "Memory consumption after IPA\n");
2416 dump_memory_report (false);
2418 timevar_pop (TV_CGRAPHOPT
);
2420 /* Output everything. */
2421 (*debug_hooks
->assembly_start
) ();
2423 fprintf (stderr
, "Assembling functions:\n");
2424 symtab_node::checking_verify_symtab_nodes ();
2426 materialize_all_clones ();
2427 bitmap_obstack_initialize (NULL
);
2428 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2429 bitmap_obstack_release (NULL
);
2430 mark_functions_to_output ();
2432 /* When weakref support is missing, we autmatically translate all
2433 references to NODE to references to its ultimate alias target.
2434 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2437 Set up this mapping before we output any assembler but once we are sure
2438 that all symbol renaming is done.
2440 FIXME: All this uglyness can go away if we just do renaming at gimple
2441 level by physically rewritting the IL. At the moment we can only redirect
2442 calls, so we need infrastructure for renaming references as well. */
2443 #ifndef ASM_OUTPUT_WEAKREF
2446 FOR_EACH_SYMBOL (node
)
2448 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2450 IDENTIFIER_TRANSPARENT_ALIAS
2451 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2452 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2453 = (node
->alias_target
? node
->alias_target
2454 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2460 if (!flag_toplevel_reorder
)
2461 output_in_order (false);
2464 /* Output first asm statements and anything ordered. The process
2465 flag is cleared for these nodes, so we skip them later. */
2466 output_in_order (true);
2467 expand_all_functions ();
2468 output_variables ();
2471 process_new_functions ();
2477 fprintf (dump_file
, "\nFinal ");
2478 symtab_node::dump_table (dump_file
);
2482 symtab_node::verify_symtab_nodes ();
2483 /* Double check that all inline clones are gone and that all
2484 function bodies have been released from memory. */
2488 bool error_found
= false;
2490 FOR_EACH_DEFINED_FUNCTION (node
)
2491 if (node
->global
.inlined_to
2492 || gimple_has_body_p (node
->decl
))
2498 internal_error ("nodes with unreleased memory found");
2503 /* Analyze the whole compilation unit once it is parsed completely. */
2506 symbol_table::finalize_compilation_unit (void)
2508 timevar_push (TV_CGRAPH
);
2510 /* If we're here there's no current function anymore. Some frontends
2511 are lazy in clearing these. */
2512 current_function_decl
= NULL
;
2515 /* Do not skip analyzing the functions if there were errors, we
2516 miss diagnostics for following functions otherwise. */
2518 /* Emit size functions we didn't inline. */
2519 finalize_size_functions ();
2521 /* Mark alias targets necessary and emit diagnostics. */
2522 handle_alias_pairs ();
2526 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2530 if (flag_dump_passes
)
2533 /* Gimplify and lower all functions, compute reachability and
2534 remove unreachable nodes. */
2535 analyze_functions (/*first_time=*/true);
2537 /* Mark alias targets necessary and emit diagnostics. */
2538 handle_alias_pairs ();
2540 /* Gimplify and lower thunks. */
2541 analyze_functions (/*first_time=*/false);
2545 /* Emit early debug for reachable functions, and by consequence,
2546 locally scoped symbols. */
2547 struct cgraph_node
*cnode
;
2548 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode
)
2549 (*debug_hooks
->early_global_decl
) (cnode
->decl
);
2551 /* Clean up anything that needs cleaning up after initial debug
2553 (*debug_hooks
->early_finish
) ();
2556 /* Finally drive the pass manager. */
2559 timevar_pop (TV_CGRAPH
);
2562 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2563 within the same process. For use by toplev::finalize. */
2566 cgraphunit_c_finalize (void)
2568 gcc_assert (cgraph_new_nodes
.length () == 0);
2569 cgraph_new_nodes
.truncate (0);
2571 vtable_entry_type
= NULL
;
2572 queued_nodes
= &symtab_terminator
;
2574 first_analyzed
= NULL
;
2575 first_analyzed_var
= NULL
;
2578 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2579 kind of wrapper method. */
2582 cgraph_node::create_wrapper (cgraph_node
*target
)
2584 /* Preserve DECL_RESULT so we get right by reference flag. */
2585 tree decl_result
= DECL_RESULT (decl
);
2587 /* Remove the function's body but keep arguments to be reused
2589 release_body (true);
2592 DECL_UNINLINABLE (decl
) = false;
2593 DECL_RESULT (decl
) = decl_result
;
2594 DECL_INITIAL (decl
) = NULL
;
2595 allocate_struct_function (decl
, false);
2598 /* Turn alias into thunk and expand it into GIMPLE representation. */
2601 memset (&thunk
, 0, sizeof (cgraph_thunk_info
));
2602 thunk
.thunk_p
= true;
2603 create_edge (target
, NULL
, count
, CGRAPH_FREQ_BASE
);
2604 callees
->can_throw_external
= !TREE_NOTHROW (target
->decl
);
2606 tree arguments
= DECL_ARGUMENTS (decl
);
2610 TREE_ADDRESSABLE (arguments
) = false;
2611 arguments
= TREE_CHAIN (arguments
);
2614 expand_thunk (false, true);
2616 /* Inline summary set-up. */
2618 inline_analyze_function (this);
2621 #include "gt-cgraphunit.h"