1 /* Driver of optimization process
2 Copyright (C) 2003-2019 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 streaming. 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 "tree-vrp.h"
194 #include "ipa-prop.h"
195 #include "gimple-pretty-print.h"
197 #include "ipa-fnsummary.h"
198 #include "ipa-utils.h"
202 #include "pass_manager.h"
203 #include "tree-nested.h"
205 #include "lto-section-names.h"
206 #include "stringpool.h"
209 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
210 secondary queue used during optimization to accommodate passes that
211 may generate new functions that need to be optimized and expanded. */
212 vec
<cgraph_node
*> cgraph_new_nodes
;
214 static void expand_all_functions (void);
215 static void mark_functions_to_output (void);
216 static void handle_alias_pairs (void);
218 /* Used for vtable lookup in thunk adjusting. */
219 static GTY (()) tree vtable_entry_type
;
221 /* Return true if this symbol is a function from the C frontend specified
222 directly in RTL form (with "__RTL"). */
225 symtab_node::native_rtl_p () const
227 if (TREE_CODE (decl
) != FUNCTION_DECL
)
229 if (!DECL_STRUCT_FUNCTION (decl
))
231 return DECL_STRUCT_FUNCTION (decl
)->curr_properties
& PROP_rtl
;
234 /* Determine if symbol declaration is needed. That is, visible to something
235 either outside this translation unit, something magic in the system
238 symtab_node::needed_p (void)
240 /* Double check that no one output the function into assembly file
242 if (!native_rtl_p ())
244 (!DECL_ASSEMBLER_NAME_SET_P (decl
)
245 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
250 if (DECL_EXTERNAL (decl
))
253 /* If the user told us it is used, then it must be so. */
257 /* ABI forced symbols are needed when they are external. */
258 if (forced_by_abi
&& TREE_PUBLIC (decl
))
261 /* Keep constructors, destructors and virtual functions. */
262 if (TREE_CODE (decl
) == FUNCTION_DECL
263 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
266 /* Externally visible variables must be output. The exception is
267 COMDAT variables that must be output only when they are needed. */
268 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
274 /* Head and terminator of the queue of nodes to be processed while building
277 static symtab_node symtab_terminator
;
278 static symtab_node
*queued_nodes
= &symtab_terminator
;
280 /* Add NODE to queue starting at QUEUED_NODES.
281 The queue is linked via AUX pointers and terminated by pointer to 1. */
284 enqueue_node (symtab_node
*node
)
288 gcc_checking_assert (queued_nodes
);
289 node
->aux
= queued_nodes
;
293 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
294 functions into callgraph in a way so they look like ordinary reachable
295 functions inserted into callgraph already at construction time. */
298 symbol_table::process_new_functions (void)
302 if (!cgraph_new_nodes
.exists ())
305 handle_alias_pairs ();
306 /* Note that this queue may grow as its being processed, as the new
307 functions may generate new ones. */
308 for (unsigned i
= 0; i
< cgraph_new_nodes
.length (); i
++)
310 cgraph_node
*node
= cgraph_new_nodes
[i
];
315 /* At construction time we just need to finalize function and move
316 it into reachable functions list. */
318 cgraph_node::finalize_function (fndecl
, false);
319 call_cgraph_insertion_hooks (node
);
325 case IPA_SSA_AFTER_INLINING
:
326 /* When IPA optimization already started, do all essential
327 transformations that has been already performed on the whole
328 cgraph but not on this function. */
330 gimple_register_cfg_hooks ();
333 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
334 if ((state
== IPA_SSA
|| state
== IPA_SSA_AFTER_INLINING
)
335 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
337 bool summaried_computed
= ipa_fn_summaries
!= NULL
;
338 g
->get_passes ()->execute_early_local_passes ();
339 /* Early passes compure inline parameters to do inlining
340 and splitting. This is redundant for functions added late.
341 Just throw away whatever it did. */
342 if (!summaried_computed
)
343 ipa_free_fn_summary ();
345 else if (ipa_fn_summaries
!= NULL
)
346 compute_fn_summary (node
, true);
347 free_dominance_info (CDI_POST_DOMINATORS
);
348 free_dominance_info (CDI_DOMINATORS
);
350 call_cgraph_insertion_hooks (node
);
354 /* Functions created during expansion shall be compiled
357 call_cgraph_insertion_hooks (node
);
367 cgraph_new_nodes
.release ();
370 /* As an GCC extension we allow redefinition of the function. The
371 semantics when both copies of bodies differ is not well defined.
372 We replace the old body with new body so in unit at a time mode
373 we always use new body, while in normal mode we may end up with
374 old body inlined into some functions and new body expanded and
377 ??? It may make more sense to use one body for inlining and other
378 body for expanding the function but this is difficult to do. */
381 cgraph_node::reset (void)
383 /* If process is set, then we have already begun whole-unit analysis.
384 This is *not* testing for whether we've already emitted the function.
385 That case can be sort-of legitimately seen with real function redefinition
386 errors. I would argue that the front end should never present us with
387 such a case, but don't enforce that for now. */
388 gcc_assert (!process
);
390 /* Reset our data structures so we can analyze the function again. */
391 memset (&local
, 0, sizeof (local
));
392 memset (&global
, 0, sizeof (global
));
393 memset (&rtl
, 0, sizeof (rtl
));
397 transparent_alias
= false;
399 cpp_implicit_alias
= false;
402 remove_all_references ();
405 /* Return true when there are references to the node. INCLUDE_SELF is
406 true if a self reference counts as a reference. */
409 symtab_node::referred_to_p (bool include_self
)
413 /* See if there are any references at all. */
414 if (iterate_referring (0, ref
))
416 /* For functions check also calls. */
417 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
418 if (cn
&& cn
->callers
)
422 for (cgraph_edge
*e
= cn
->callers
; e
; e
= e
->next_caller
)
423 if (e
->caller
!= this)
429 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
430 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
431 the garbage collector run at the moment. We would need to either create
432 a new GC context, or just not compile right now. */
435 cgraph_node::finalize_function (tree decl
, bool no_collect
)
437 cgraph_node
*node
= cgraph_node::get_create (decl
);
439 if (node
->definition
)
441 /* Nested functions should only be defined once. */
442 gcc_assert (!DECL_CONTEXT (decl
)
443 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
445 node
->local
.redefined_extern_inline
= true;
448 /* Set definition first before calling notice_global_symbol so that
449 it is available to notice_global_symbol. */
450 node
->definition
= true;
451 notice_global_symbol (decl
);
452 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
453 if (!flag_toplevel_reorder
)
454 node
->no_reorder
= true;
456 /* With -fkeep-inline-functions we are keeping all inline functions except
457 for extern inline ones. */
458 if (flag_keep_inline_functions
459 && DECL_DECLARED_INLINE_P (decl
)
460 && !DECL_EXTERNAL (decl
)
461 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
462 node
->force_output
= 1;
464 /* __RTL functions were already output as soon as they were parsed (due
465 to the large amount of global state in the backend).
466 Mark such functions as "force_output" to reflect the fact that they
467 will be in the asm file when considering the symbols they reference.
468 The attempt to output them later on will bail out immediately. */
469 if (node
->native_rtl_p ())
470 node
->force_output
= 1;
472 /* When not optimizing, also output the static functions. (see
473 PR24561), but don't do so for always_inline functions, functions
474 declared inline and nested functions. These were optimized out
475 in the original implementation and it is unclear whether we want
476 to change the behavior here. */
477 if (((!opt_for_fn (decl
, optimize
) || flag_keep_static_functions
479 && !node
->cpp_implicit_alias
480 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
481 && !DECL_DECLARED_INLINE_P (decl
)
482 && !(DECL_CONTEXT (decl
)
483 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
484 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
485 node
->force_output
= 1;
487 /* If we've not yet emitted decl, tell the debug info about it. */
488 if (!TREE_ASM_WRITTEN (decl
))
489 (*debug_hooks
->deferred_inline_function
) (decl
);
494 if (symtab
->state
== CONSTRUCTION
495 && (node
->needed_p () || node
->referred_to_p ()))
499 /* Add the function FNDECL to the call graph.
500 Unlike finalize_function, this function is intended to be used
501 by middle end and allows insertion of new function at arbitrary point
502 of compilation. The function can be either in high, low or SSA form
505 The function is assumed to be reachable and have address taken (so no
506 API breaking optimizations are performed on it).
508 Main work done by this function is to enqueue the function for later
509 processing to avoid need the passes to be re-entrant. */
512 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
514 gcc::pass_manager
*passes
= g
->get_passes ();
519 struct function
*fn
= DECL_STRUCT_FUNCTION (fndecl
);
520 const char *function_type
= ((gimple_has_body_p (fndecl
))
522 ? (gimple_in_ssa_p (fn
)
526 : "to-be-gimplified");
528 "Added new %s function %s to callgraph\n",
530 fndecl_name (fndecl
));
533 switch (symtab
->state
)
536 cgraph_node::finalize_function (fndecl
, false);
539 /* Just enqueue function to be processed at nearest occurrence. */
540 node
= cgraph_node::get_create (fndecl
);
542 node
->lowered
= true;
543 cgraph_new_nodes
.safe_push (node
);
548 case IPA_SSA_AFTER_INLINING
:
550 /* Bring the function into finalized state and enqueue for later
551 analyzing and compilation. */
552 node
= cgraph_node::get_create (fndecl
);
553 node
->local
.local
= false;
554 node
->definition
= true;
555 node
->force_output
= true;
556 if (TREE_PUBLIC (fndecl
))
557 node
->externally_visible
= true;
558 if (!lowered
&& symtab
->state
== EXPANSION
)
560 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
561 gimple_register_cfg_hooks ();
562 bitmap_obstack_initialize (NULL
);
563 execute_pass_list (cfun
, passes
->all_lowering_passes
);
564 passes
->execute_early_local_passes ();
565 bitmap_obstack_release (NULL
);
571 node
->lowered
= true;
572 cgraph_new_nodes
.safe_push (node
);
576 /* At the very end of compilation we have to do all the work up
578 node
= cgraph_node::create (fndecl
);
580 node
->lowered
= true;
581 node
->definition
= true;
583 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
584 gimple_register_cfg_hooks ();
585 bitmap_obstack_initialize (NULL
);
586 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
587 g
->get_passes ()->execute_early_local_passes ();
588 bitmap_obstack_release (NULL
);
597 /* Set a personality if required and we already passed EH lowering. */
599 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
600 == eh_personality_lang
))
601 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
604 /* Analyze the function scheduled to be output. */
606 cgraph_node::analyze (void)
614 tree decl
= this->decl
;
615 location_t saved_loc
= input_location
;
616 input_location
= DECL_SOURCE_LOCATION (decl
);
620 cgraph_node
*t
= cgraph_node::get (thunk
.alias
);
622 create_edge (t
, NULL
, t
->count
);
623 callees
->can_throw_external
= !TREE_NOTHROW (t
->decl
);
624 /* Target code in expand_thunk may need the thunk's target
625 to be analyzed, so recurse here. */
626 if (!t
->analyzed
&& t
->definition
)
630 t
= t
->get_alias_target ();
631 if (!t
->analyzed
&& t
->definition
)
634 bool ret
= expand_thunk (false, false);
640 resolve_alias (cgraph_node::get (alias_target
), transparent_alias
);
641 else if (dispatcher_function
)
643 /* Generate the dispatcher body of multi-versioned functions. */
644 cgraph_function_version_info
*dispatcher_version_info
645 = function_version ();
646 if (dispatcher_version_info
!= NULL
647 && (dispatcher_version_info
->dispatcher_resolver
650 tree resolver
= NULL_TREE
;
651 gcc_assert (targetm
.generate_version_dispatcher_body
);
652 resolver
= targetm
.generate_version_dispatcher_body (this);
653 gcc_assert (resolver
!= NULL_TREE
);
658 push_cfun (DECL_STRUCT_FUNCTION (decl
));
660 assign_assembler_name_if_needed (decl
);
662 /* Make sure to gimplify bodies only once. During analyzing a
663 function we lower it, which will require gimplified nested
664 functions, so we can end up here with an already gimplified
666 if (!gimple_has_body_p (decl
))
667 gimplify_function_tree (decl
);
669 /* Lower the function. */
673 lower_nested_functions (decl
);
674 gcc_assert (!nested
);
676 gimple_register_cfg_hooks ();
677 bitmap_obstack_initialize (NULL
);
678 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
679 free_dominance_info (CDI_POST_DOMINATORS
);
680 free_dominance_info (CDI_DOMINATORS
);
682 bitmap_obstack_release (NULL
);
690 input_location
= saved_loc
;
693 /* C++ frontend produce same body aliases all over the place, even before PCH
694 gets streamed out. It relies on us linking the aliases with their function
695 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
696 first produce aliases without links, but once C++ FE is sure he won't sream
697 PCH we build the links via this function. */
700 symbol_table::process_same_body_aliases (void)
703 FOR_EACH_SYMBOL (node
)
704 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
706 (VAR_P (node
->alias_target
)
707 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
708 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
709 cpp_implicit_aliases_done
= true;
712 /* Process attributes common for vars and functions. */
715 process_common_attributes (symtab_node
*node
, tree decl
)
717 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
719 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
721 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
722 "%<weakref%> attribute should be accompanied with"
723 " an %<alias%> attribute");
724 DECL_WEAK (decl
) = 0;
725 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
726 DECL_ATTRIBUTES (decl
));
729 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
730 node
->no_reorder
= 1;
733 /* Look for externally_visible and used attributes and mark cgraph nodes
736 We cannot mark the nodes at the point the attributes are processed (in
737 handle_*_attribute) because the copy of the declarations available at that
738 point may not be canonical. For example, in:
741 void f() __attribute__((used));
743 the declaration we see in handle_used_attribute will be the second
744 declaration -- but the front end will subsequently merge that declaration
745 with the original declaration and discard the second declaration.
747 Furthermore, we can't mark these nodes in finalize_function because:
750 void f() __attribute__((externally_visible));
754 So, we walk the nodes at the end of the translation unit, applying the
755 attributes at that point. */
758 process_function_and_variable_attributes (cgraph_node
*first
,
759 varpool_node
*first_var
)
764 for (node
= symtab
->first_function (); node
!= first
;
765 node
= symtab
->next_function (node
))
767 tree decl
= node
->decl
;
768 if (DECL_PRESERVE_P (decl
))
769 node
->mark_force_output ();
770 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
772 if (! TREE_PUBLIC (node
->decl
))
773 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
774 "%<externally_visible%>"
775 " attribute have effect only on public objects");
777 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
778 && (node
->definition
&& !node
->alias
))
780 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
781 "%<weakref%> attribute ignored"
782 " because function is defined");
783 DECL_WEAK (decl
) = 0;
784 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
785 DECL_ATTRIBUTES (decl
));
787 else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl
))
790 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
791 "%<alias%> attribute ignored"
792 " because function is defined");
794 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
795 && !DECL_DECLARED_INLINE_P (decl
)
796 /* redefining extern inline function makes it DECL_UNINLINABLE. */
797 && !DECL_UNINLINABLE (decl
))
798 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
799 "always_inline function might not be inlinable");
801 process_common_attributes (node
, decl
);
803 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
804 vnode
= symtab
->next_variable (vnode
))
806 tree decl
= vnode
->decl
;
807 if (DECL_EXTERNAL (decl
)
808 && DECL_INITIAL (decl
))
809 varpool_node::finalize_decl (decl
);
810 if (DECL_PRESERVE_P (decl
))
811 vnode
->force_output
= true;
812 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
814 if (! TREE_PUBLIC (vnode
->decl
))
815 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
816 "%<externally_visible%>"
817 " attribute have effect only on public objects");
819 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
821 && DECL_INITIAL (decl
))
823 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
824 "%<weakref%> attribute ignored"
825 " because variable is initialized");
826 DECL_WEAK (decl
) = 0;
827 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
828 DECL_ATTRIBUTES (decl
));
830 process_common_attributes (vnode
, decl
);
834 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
835 middle end to output the variable to asm file, if needed or externally
839 varpool_node::finalize_decl (tree decl
)
841 varpool_node
*node
= varpool_node::get_create (decl
);
843 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
845 if (node
->definition
)
847 /* Set definition first before calling notice_global_symbol so that
848 it is available to notice_global_symbol. */
849 node
->definition
= true;
850 notice_global_symbol (decl
);
851 if (!flag_toplevel_reorder
)
852 node
->no_reorder
= true;
853 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
854 /* Traditionally we do not eliminate static variables when not
855 optimizing and when not doing toplevel reoder. */
856 || (node
->no_reorder
&& !DECL_COMDAT (node
->decl
)
857 && !DECL_ARTIFICIAL (node
->decl
)))
858 node
->force_output
= true;
860 if (symtab
->state
== CONSTRUCTION
861 && (node
->needed_p () || node
->referred_to_p ()))
863 if (symtab
->state
>= IPA_SSA
)
865 /* Some frontends produce various interface variables after compilation
867 if (symtab
->state
== FINISHED
869 && symtab
->state
== EXPANSION
))
870 node
->assemble_decl ();
873 /* EDGE is an polymorphic call. Mark all possible targets as reachable
874 and if there is only one target, perform trivial devirtualization.
875 REACHABLE_CALL_TARGETS collects target lists we already walked to
876 avoid udplicate work. */
879 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
885 vec
<cgraph_node
*>targets
886 = possible_polymorphic_call_targets
887 (edge
, &final
, &cache_token
);
889 if (!reachable_call_targets
->add (cache_token
))
891 if (symtab
->dump_file
)
892 dump_possible_polymorphic_call_targets
893 (symtab
->dump_file
, edge
);
895 for (i
= 0; i
< targets
.length (); i
++)
897 /* Do not bother to mark virtual methods in anonymous namespace;
898 either we will find use of virtual table defining it, or it is
900 if (targets
[i
]->definition
902 (TREE_TYPE (targets
[i
]->decl
))
904 && !type_in_anonymous_namespace_p
905 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets
[i
]->decl
))))
906 enqueue_node (targets
[i
]);
910 /* Very trivial devirtualization; when the type is
911 final or anonymous (so we know all its derivation)
912 and there is only one possible virtual call target,
913 make the edge direct. */
916 if (targets
.length () <= 1 && dbg_cnt (devirt
))
919 if (targets
.length () == 1)
922 target
= cgraph_node::create
923 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
925 if (symtab
->dump_file
)
927 fprintf (symtab
->dump_file
,
928 "Devirtualizing call: ");
929 print_gimple_stmt (symtab
->dump_file
,
933 if (dump_enabled_p ())
935 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, edge
->call_stmt
,
936 "devirtualizing call in %s to %s\n",
937 edge
->caller
->name (), target
->name ());
940 edge
->make_direct (target
);
941 edge
->redirect_call_stmt_to_callee ();
943 if (symtab
->dump_file
)
945 fprintf (symtab
->dump_file
,
946 "Devirtualized as: ");
947 print_gimple_stmt (symtab
->dump_file
,
955 /* Issue appropriate warnings for the global declaration DECL. */
958 check_global_declaration (symtab_node
*snode
)
960 const char *decl_file
;
961 tree decl
= snode
->decl
;
963 /* Warn about any function declared static but not defined. We don't
964 warn about variables, because many programs have static variables
965 that exist only to get some text into the object file. */
966 if (TREE_CODE (decl
) == FUNCTION_DECL
967 && DECL_INITIAL (decl
) == 0
968 && DECL_EXTERNAL (decl
)
969 && ! DECL_ARTIFICIAL (decl
)
970 && ! TREE_NO_WARNING (decl
)
971 && ! TREE_PUBLIC (decl
)
972 && (warn_unused_function
973 || snode
->referred_to_p (/*include_self=*/false)))
975 if (snode
->referred_to_p (/*include_self=*/false))
976 pedwarn (input_location
, 0, "%q+F used but never defined", decl
);
978 warning (OPT_Wunused_function
, "%q+F declared %<static%> but never defined", decl
);
979 /* This symbol is effectively an "extern" declaration now. */
980 TREE_PUBLIC (decl
) = 1;
983 /* Warn about static fns or vars defined but not used. */
984 if (((warn_unused_function
&& TREE_CODE (decl
) == FUNCTION_DECL
)
985 || (((warn_unused_variable
&& ! TREE_READONLY (decl
))
986 || (warn_unused_const_variable
> 0 && TREE_READONLY (decl
)
987 && (warn_unused_const_variable
== 2
988 || (main_input_filename
!= NULL
989 && (decl_file
= DECL_SOURCE_FILE (decl
)) != NULL
990 && filename_cmp (main_input_filename
,
993 && ! DECL_IN_SYSTEM_HEADER (decl
)
994 && ! snode
->referred_to_p (/*include_self=*/false)
995 /* This TREE_USED check is needed in addition to referred_to_p
996 above, because the `__unused__' attribute is not being
997 considered for referred_to_p. */
998 && ! TREE_USED (decl
)
999 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1000 to handle multiple external decls in different scopes. */
1001 && ! (DECL_NAME (decl
) && TREE_USED (DECL_NAME (decl
)))
1002 && ! DECL_EXTERNAL (decl
)
1003 && ! DECL_ARTIFICIAL (decl
)
1004 && ! DECL_ABSTRACT_ORIGIN (decl
)
1005 && ! TREE_PUBLIC (decl
)
1006 /* A volatile variable might be used in some non-obvious way. */
1007 && (! VAR_P (decl
) || ! TREE_THIS_VOLATILE (decl
))
1008 /* Global register variables must be declared to reserve them. */
1009 && ! (VAR_P (decl
) && DECL_REGISTER (decl
))
1010 /* Global ctors and dtors are called by the runtime. */
1011 && (TREE_CODE (decl
) != FUNCTION_DECL
1012 || (!DECL_STATIC_CONSTRUCTOR (decl
)
1013 && !DECL_STATIC_DESTRUCTOR (decl
)))
1014 /* Otherwise, ask the language. */
1015 && lang_hooks
.decls
.warn_unused_global (decl
))
1016 warning_at (DECL_SOURCE_LOCATION (decl
),
1017 (TREE_CODE (decl
) == FUNCTION_DECL
)
1018 ? OPT_Wunused_function
1019 : (TREE_READONLY (decl
)
1020 ? OPT_Wunused_const_variable_
1021 : OPT_Wunused_variable
),
1022 "%qD defined but not used", decl
);
1025 /* Discover all functions and variables that are trivially needed, analyze
1026 them as well as all functions and variables referred by them */
1027 static cgraph_node
*first_analyzed
;
1028 static varpool_node
*first_analyzed_var
;
1030 /* FIRST_TIME is set to TRUE for the first time we are called for a
1031 translation unit from finalize_compilation_unit() or false
1035 analyze_functions (bool first_time
)
1037 /* Keep track of already processed nodes when called multiple times for
1038 intermodule optimization. */
1039 cgraph_node
*first_handled
= first_analyzed
;
1040 varpool_node
*first_handled_var
= first_analyzed_var
;
1041 hash_set
<void *> reachable_call_targets
;
1047 bool changed
= true;
1048 location_t saved_loc
= input_location
;
1050 bitmap_obstack_initialize (NULL
);
1051 symtab
->state
= CONSTRUCTION
;
1052 input_location
= UNKNOWN_LOCATION
;
1054 /* Ugly, but the fixup cannot happen at a time same body alias is created;
1055 C++ FE is confused about the COMDAT groups being right. */
1056 if (symtab
->cpp_implicit_aliases_done
)
1057 FOR_EACH_SYMBOL (node
)
1058 if (node
->cpp_implicit_alias
)
1059 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
1060 build_type_inheritance_graph ();
1062 /* Analysis adds static variables that in turn adds references to new functions.
1063 So we need to iterate the process until it stabilize. */
1067 process_function_and_variable_attributes (first_analyzed
,
1068 first_analyzed_var
);
1070 /* First identify the trivially needed symbols. */
1071 for (node
= symtab
->first_symbol ();
1072 node
!= first_analyzed
1073 && node
!= first_analyzed_var
; node
= node
->next
)
1075 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1076 node
->get_comdat_group_id ();
1077 if (node
->needed_p ())
1079 enqueue_node (node
);
1080 if (!changed
&& symtab
->dump_file
)
1081 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
1083 if (symtab
->dump_file
)
1084 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
1085 if (!changed
&& symtab
->dump_file
)
1086 fprintf (symtab
->dump_file
, "\n");
1088 if (node
== first_analyzed
1089 || node
== first_analyzed_var
)
1092 symtab
->process_new_functions ();
1093 first_analyzed_var
= symtab
->first_variable ();
1094 first_analyzed
= symtab
->first_function ();
1096 if (changed
&& symtab
->dump_file
)
1097 fprintf (symtab
->dump_file
, "\n");
1099 /* Lower representation, build callgraph edges and references for all trivially
1100 needed symbols and all symbols referred by them. */
1101 while (queued_nodes
!= &symtab_terminator
)
1104 node
= queued_nodes
;
1105 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
1106 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1107 if (cnode
&& cnode
->definition
)
1110 tree decl
= cnode
->decl
;
1112 /* ??? It is possible to create extern inline function
1113 and later using weak alias attribute to kill its body.
1114 See gcc.c-torture/compile/20011119-1.c */
1115 if (!DECL_STRUCT_FUNCTION (decl
)
1117 && !cnode
->thunk
.thunk_p
1118 && !cnode
->dispatcher_function
)
1121 cnode
->local
.redefined_extern_inline
= true;
1125 if (!cnode
->analyzed
)
1128 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
1129 if (edge
->callee
->definition
1130 && (!DECL_EXTERNAL (edge
->callee
->decl
)
1131 /* When not optimizing, do not try to analyze extern
1132 inline functions. Doing so is pointless. */
1133 || opt_for_fn (edge
->callee
->decl
, optimize
)
1134 /* Weakrefs needs to be preserved. */
1135 || edge
->callee
->alias
1136 /* always_inline functions are inlined aven at -O0. */
1139 DECL_ATTRIBUTES (edge
->callee
->decl
))
1140 /* Multiversioned functions needs the dispatcher to
1141 be produced locally even for extern functions. */
1142 || edge
->callee
->function_version ()))
1143 enqueue_node (edge
->callee
);
1144 if (opt_for_fn (cnode
->decl
, optimize
)
1145 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
1149 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1151 next
= edge
->next_callee
;
1152 if (edge
->indirect_info
->polymorphic
)
1153 walk_polymorphic_call_targets (&reachable_call_targets
,
1158 /* If decl is a clone of an abstract function,
1159 mark that abstract function so that we don't release its body.
1160 The DECL_INITIAL() of that abstract function declaration
1161 will be later needed to output debug info. */
1162 if (DECL_ABSTRACT_ORIGIN (decl
))
1164 cgraph_node
*origin_node
1165 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1166 origin_node
->used_as_abstract_origin
= true;
1168 /* Preserve a functions function context node. It will
1169 later be needed to output debug info. */
1170 if (tree fn
= decl_function_context (decl
))
1172 cgraph_node
*origin_node
= cgraph_node::get_create (fn
);
1173 enqueue_node (origin_node
);
1178 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1179 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1183 if (node
->same_comdat_group
)
1186 for (next
= node
->same_comdat_group
;
1188 next
= next
->same_comdat_group
)
1189 if (!next
->comdat_local_p ())
1190 enqueue_node (next
);
1192 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1193 if (ref
->referred
->definition
1194 && (!DECL_EXTERNAL (ref
->referred
->decl
)
1195 || ((TREE_CODE (ref
->referred
->decl
) != FUNCTION_DECL
1197 || (TREE_CODE (ref
->referred
->decl
) == FUNCTION_DECL
1198 && opt_for_fn (ref
->referred
->decl
, optimize
))
1200 || ref
->referred
->alias
)))
1201 enqueue_node (ref
->referred
);
1202 symtab
->process_new_functions ();
1205 update_type_inheritance_graph ();
1207 /* Collect entry points to the unit. */
1208 if (symtab
->dump_file
)
1210 fprintf (symtab
->dump_file
, "\n\nInitial ");
1211 symtab
->dump (symtab
->dump_file
);
1217 FOR_EACH_SYMBOL (snode
)
1218 check_global_declaration (snode
);
1221 if (symtab
->dump_file
)
1222 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1224 for (node
= symtab
->first_symbol ();
1225 node
!= first_handled
1226 && node
!= first_handled_var
; node
= next
)
1229 if (!node
->aux
&& !node
->referred_to_p ())
1231 if (symtab
->dump_file
)
1232 fprintf (symtab
->dump_file
, " %s", node
->name ());
1234 /* See if the debugger can use anything before the DECL
1235 passes away. Perhaps it can notice a DECL that is now a
1236 constant and can tag the early DIE with an appropriate
1239 Otherwise, this is the last chance the debug_hooks have
1240 at looking at optimized away DECLs, since
1241 late_global_decl will subsequently be called from the
1242 contents of the now pruned symbol table. */
1243 if (VAR_P (node
->decl
)
1244 && !decl_function_context (node
->decl
))
1246 /* We are reclaiming totally unreachable code and variables
1247 so they effectively appear as readonly. Show that to
1248 the debug machinery. */
1249 TREE_READONLY (node
->decl
) = 1;
1250 node
->definition
= false;
1251 (*debug_hooks
->late_global_decl
) (node
->decl
);
1257 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1259 tree decl
= node
->decl
;
1261 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1263 && !cnode
->thunk
.thunk_p
)
1266 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1268 || gimple_has_body_p (decl
)
1269 || cnode
->native_rtl_p ());
1270 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1274 for (;node
; node
= node
->next
)
1276 first_analyzed
= symtab
->first_function ();
1277 first_analyzed_var
= symtab
->first_variable ();
1278 if (symtab
->dump_file
)
1280 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1281 symtab
->dump (symtab
->dump_file
);
1283 bitmap_obstack_release (NULL
);
1285 /* Initialize assembler name hash, in particular we want to trigger C++
1286 mangling and same body alias creation before we free DECL_ARGUMENTS
1289 symtab
->symtab_initialize_asm_name_hash ();
1291 input_location
= saved_loc
;
1294 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1295 (which may be an ifunc resolver) and issue a diagnostic when they are
1296 not compatible according to language rules (plus a C++ extension for
1297 non-static member functions). */
1300 maybe_diag_incompatible_alias (tree alias
, tree target
)
1302 tree altype
= TREE_TYPE (alias
);
1303 tree targtype
= TREE_TYPE (target
);
1305 bool ifunc
= cgraph_node::get (alias
)->ifunc_resolver
;
1306 tree funcptr
= altype
;
1310 /* Handle attribute ifunc first. */
1311 if (TREE_CODE (altype
) == METHOD_TYPE
)
1313 /* Set FUNCPTR to the type of the alias target. If the type
1314 is a non-static member function of class C, construct a type
1315 of an ordinary function taking C* as the first argument,
1316 followed by the member function argument list, and use it
1317 instead to check for incompatibility. This conversion is
1318 not defined by the language but an extension provided by
1321 tree rettype
= TREE_TYPE (altype
);
1322 tree args
= TYPE_ARG_TYPES (altype
);
1323 altype
= build_function_type (rettype
, args
);
1327 targtype
= TREE_TYPE (targtype
);
1329 if (POINTER_TYPE_P (targtype
))
1331 targtype
= TREE_TYPE (targtype
);
1333 /* Only issue Wattribute-alias for conversions to void* with
1335 if (VOID_TYPE_P (targtype
) && !extra_warnings
)
1338 /* Proceed to handle incompatible ifunc resolvers below. */
1342 funcptr
= build_pointer_type (funcptr
);
1344 error_at (DECL_SOURCE_LOCATION (target
),
1345 "%<ifunc%> resolver for %qD must return %qT",
1347 inform (DECL_SOURCE_LOCATION (alias
),
1348 "resolver indirect function declared here");
1353 if ((!FUNC_OR_METHOD_TYPE_P (targtype
)
1354 || (prototype_p (altype
)
1355 && prototype_p (targtype
)
1356 && !types_compatible_p (altype
, targtype
))))
1358 /* Warn for incompatibilities. Avoid warning for functions
1359 without a prototype to make it possible to declare aliases
1360 without knowing the exact type, as libstdc++ does. */
1363 funcptr
= build_pointer_type (funcptr
);
1365 auto_diagnostic_group d
;
1366 if (warning_at (DECL_SOURCE_LOCATION (target
),
1367 OPT_Wattribute_alias_
,
1368 "%<ifunc%> resolver for %qD should return %qT",
1370 inform (DECL_SOURCE_LOCATION (alias
),
1371 "resolver indirect function declared here");
1375 auto_diagnostic_group d
;
1376 if (warning_at (DECL_SOURCE_LOCATION (alias
),
1377 OPT_Wattribute_alias_
,
1378 "%qD alias between functions of incompatible "
1379 "types %qT and %qT", alias
, altype
, targtype
))
1380 inform (DECL_SOURCE_LOCATION (target
),
1381 "aliased declaration here");
1386 /* Translate the ugly representation of aliases as alias pairs into nice
1387 representation in callgraph. We don't handle all cases yet,
1391 handle_alias_pairs (void)
1396 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1398 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1400 /* Weakrefs with target not defined in current unit are easy to handle:
1401 they behave just as external variables except we need to note the
1402 alias flag to later output the weakref pseudo op into asm file. */
1404 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1406 symtab_node
*node
= symtab_node::get (p
->decl
);
1409 node
->alias_target
= p
->target
;
1410 node
->weakref
= true;
1412 node
->transparent_alias
= true;
1414 alias_pairs
->unordered_remove (i
);
1417 else if (!target_node
)
1419 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1420 symtab_node
*node
= symtab_node::get (p
->decl
);
1422 node
->alias
= false;
1423 alias_pairs
->unordered_remove (i
);
1427 if (DECL_EXTERNAL (target_node
->decl
)
1428 /* We use local aliases for C++ thunks to force the tailcall
1429 to bind locally. This is a hack - to keep it working do
1430 the following (which is not strictly correct). */
1431 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1432 || ! DECL_VIRTUAL_P (target_node
->decl
))
1433 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1435 error ("%q+D aliased to external symbol %qE",
1436 p
->decl
, p
->target
);
1439 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1440 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1442 maybe_diag_incompatible_alias (p
->decl
, target_node
->decl
);
1444 maybe_diag_alias_attributes (p
->decl
, target_node
->decl
);
1446 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1447 if (src_node
&& src_node
->definition
)
1449 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1450 alias_pairs
->unordered_remove (i
);
1452 else if (VAR_P (p
->decl
)
1453 && target_node
&& is_a
<varpool_node
*> (target_node
))
1455 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1456 alias_pairs
->unordered_remove (i
);
1460 error ("%q+D alias between function and variable is not supported",
1462 inform (DECL_SOURCE_LOCATION (target_node
->decl
),
1463 "aliased declaration here");
1465 alias_pairs
->unordered_remove (i
);
1468 vec_free (alias_pairs
);
1472 /* Figure out what functions we want to assemble. */
1475 mark_functions_to_output (void)
1477 bool check_same_comdat_groups
= false;
1481 FOR_EACH_FUNCTION (node
)
1482 gcc_assert (!node
->process
);
1484 FOR_EACH_FUNCTION (node
)
1486 tree decl
= node
->decl
;
1488 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1492 /* We need to output all local functions that are used and not
1493 always inlined, as well as those that are reachable from
1494 outside the current compilation unit. */
1496 && !node
->thunk
.thunk_p
1498 && !node
->global
.inlined_to
1499 && !TREE_ASM_WRITTEN (decl
)
1500 && !DECL_EXTERNAL (decl
))
1503 if (node
->same_comdat_group
)
1506 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1508 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1509 if (!next
->thunk
.thunk_p
&& !next
->alias
1510 && !next
->comdat_local_p ())
1514 else if (node
->same_comdat_group
)
1517 check_same_comdat_groups
= true;
1521 /* We should've reclaimed all functions that are not needed. */
1523 && !node
->global
.inlined_to
1524 && gimple_has_body_p (decl
)
1525 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1526 are inside partition, we can end up not removing the body since we no longer
1527 have analyzed node pointing to it. */
1528 && !node
->in_other_partition
1531 && !DECL_EXTERNAL (decl
))
1534 internal_error ("failed to reclaim unneeded function");
1536 gcc_assert (node
->global
.inlined_to
1537 || !gimple_has_body_p (decl
)
1538 || node
->in_other_partition
1540 || DECL_ARTIFICIAL (decl
)
1541 || DECL_EXTERNAL (decl
));
1546 if (flag_checking
&& check_same_comdat_groups
)
1547 FOR_EACH_FUNCTION (node
)
1548 if (node
->same_comdat_group
&& !node
->process
)
1550 tree decl
= node
->decl
;
1551 if (!node
->global
.inlined_to
1552 && gimple_has_body_p (decl
)
1553 /* FIXME: in an ltrans unit when the offline copy is outside a
1554 partition but inline copies are inside a partition, we can
1555 end up not removing the body since we no longer have an
1556 analyzed node pointing to it. */
1557 && !node
->in_other_partition
1559 && !DECL_EXTERNAL (decl
))
1562 internal_error ("failed to reclaim unneeded function in same "
1568 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1569 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1571 Set current_function_decl and cfun to newly constructed empty function body.
1572 return basic block in the function body. */
1575 init_lowered_empty_function (tree decl
, bool in_ssa
, profile_count count
)
1580 current_function_decl
= decl
;
1581 allocate_struct_function (decl
, false);
1582 gimple_register_cfg_hooks ();
1583 init_empty_tree_cfg ();
1584 init_tree_ssa (cfun
);
1588 init_ssa_operands (cfun
);
1589 cfun
->gimple_df
->in_ssa_p
= true;
1590 cfun
->curr_properties
|= PROP_ssa
;
1593 DECL_INITIAL (decl
) = make_node (BLOCK
);
1594 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl
)) = decl
;
1596 DECL_SAVED_TREE (decl
) = error_mark_node
;
1597 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1598 | PROP_cfg
| PROP_loops
);
1600 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1601 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1602 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1604 /* Create BB for body of the function and connect it properly. */
1605 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1606 EXIT_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1607 bb
= create_basic_block (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1609 e
= make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1610 e
->probability
= profile_probability::always ();
1611 e
= make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1612 e
->probability
= profile_probability::always ();
1613 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1618 /* Adjust PTR by the constant FIXED_OFFSET, by the vtable offset indicated by
1619 VIRTUAL_OFFSET, and by the indirect offset indicated by INDIRECT_OFFSET, if
1620 it is non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and zero
1621 for a result adjusting thunk. */
1624 thunk_adjust (gimple_stmt_iterator
* bsi
,
1625 tree ptr
, bool this_adjusting
,
1626 HOST_WIDE_INT fixed_offset
, tree virtual_offset
,
1627 HOST_WIDE_INT indirect_offset
)
1633 && fixed_offset
!= 0)
1635 stmt
= gimple_build_assign
1636 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1639 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1642 if (!vtable_entry_type
&& (virtual_offset
|| indirect_offset
!= 0))
1644 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1645 TREE_TYPE (vfunc_type
) = integer_type_node
;
1646 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1647 layout_type (vfunc_type
);
1649 vtable_entry_type
= build_pointer_type (vfunc_type
);
1652 /* If there's a virtual offset, look up that value in the vtable and
1653 adjust the pointer again. */
1661 create_tmp_reg (build_pointer_type
1662 (build_pointer_type (vtable_entry_type
)), "vptr");
1664 /* The vptr is always at offset zero in the object. */
1665 stmt
= gimple_build_assign (vtabletmp
,
1666 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1668 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1670 /* Form the vtable address. */
1671 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1673 stmt
= gimple_build_assign (vtabletmp2
,
1674 build_simple_mem_ref (vtabletmp
));
1675 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1677 /* Find the entry with the vcall offset. */
1678 stmt
= gimple_build_assign (vtabletmp2
,
1679 fold_build_pointer_plus_loc (input_location
,
1682 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1684 /* Get the offset itself. */
1685 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1687 stmt
= gimple_build_assign (vtabletmp3
,
1688 build_simple_mem_ref (vtabletmp2
));
1689 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1691 /* Adjust the `this' pointer. */
1692 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1693 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1694 GSI_CONTINUE_LINKING
);
1697 /* Likewise for an offset that is stored in the object that contains the
1699 if (indirect_offset
!= 0)
1701 tree offset_ptr
, offset_tree
;
1703 /* Get the address of the offset. */
1705 = create_tmp_reg (build_pointer_type
1706 (build_pointer_type (vtable_entry_type
)),
1708 stmt
= gimple_build_assign (offset_ptr
,
1709 build1 (NOP_EXPR
, TREE_TYPE (offset_ptr
),
1711 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1713 stmt
= gimple_build_assign
1715 fold_build_pointer_plus_hwi_loc (input_location
, offset_ptr
,
1717 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1719 /* Get the offset itself. */
1720 offset_tree
= create_tmp_reg (TREE_TYPE (TREE_TYPE (offset_ptr
)),
1722 stmt
= gimple_build_assign (offset_tree
,
1723 build_simple_mem_ref (offset_ptr
));
1724 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1726 /* Adjust the `this' pointer. */
1727 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, offset_tree
);
1728 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1729 GSI_CONTINUE_LINKING
);
1733 && fixed_offset
!= 0)
1734 /* Adjust the pointer by the constant. */
1742 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1743 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1744 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1746 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1747 ptrtmp
, fixed_offset
);
1750 /* Emit the statement and gimplify the adjustment expression. */
1751 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1752 stmt
= gimple_build_assign (ret
, ptr
);
1753 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1758 /* Expand thunk NODE to gimple if possible.
1759 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1760 no assembler is produced.
1761 When OUTPUT_ASM_THUNK is true, also produce assembler for
1762 thunks that are not lowered. */
1765 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1767 bool this_adjusting
= thunk
.this_adjusting
;
1768 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1769 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1770 HOST_WIDE_INT indirect_offset
= thunk
.indirect_offset
;
1771 tree virtual_offset
= NULL
;
1772 tree alias
= callees
->callee
->decl
;
1773 tree thunk_fndecl
= decl
;
1776 /* Instrumentation thunk is the same function with
1777 a different signature. Never need to expand it. */
1778 if (thunk
.add_pointer_bounds_args
)
1781 if (!force_gimple_thunk
1783 && indirect_offset
== 0
1784 && !DECL_EXTERNAL (alias
)
1785 && !DECL_STATIC_CHAIN (alias
)
1786 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1787 virtual_value
, alias
))
1791 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1793 if (!output_asm_thunks
)
1800 get_untransformed_body ();
1801 a
= DECL_ARGUMENTS (thunk_fndecl
);
1803 current_function_decl
= thunk_fndecl
;
1805 /* Ensure thunks are emitted in their correct sections. */
1806 resolve_unique_section (thunk_fndecl
, 0,
1807 flag_function_sections
);
1809 DECL_RESULT (thunk_fndecl
)
1810 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1811 RESULT_DECL
, 0, restype
);
1812 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1813 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1815 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1817 fn_block
= make_node (BLOCK
);
1818 BLOCK_VARS (fn_block
) = a
;
1819 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1820 BLOCK_SUPERCONTEXT (fn_block
) = thunk_fndecl
;
1821 allocate_struct_function (thunk_fndecl
, false);
1822 init_function_start (thunk_fndecl
);
1824 insn_locations_init ();
1825 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1826 prologue_location
= curr_insn_location ();
1827 assemble_start_function (thunk_fndecl
, fnname
);
1829 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1830 fixed_offset
, virtual_value
, alias
);
1832 assemble_end_function (thunk_fndecl
, fnname
);
1833 insn_locations_finalize ();
1834 init_insn_lengths ();
1835 free_after_compilation (cfun
);
1836 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1837 thunk
.thunk_p
= false;
1840 else if (stdarg_p (TREE_TYPE (thunk_fndecl
)))
1842 error ("generic thunk code fails for method %qD which uses %<...%>",
1844 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1851 basic_block bb
, then_bb
, else_bb
, return_bb
;
1852 gimple_stmt_iterator bsi
;
1861 bool alias_is_noreturn
= TREE_THIS_VOLATILE (alias
);
1863 /* We may be called from expand_thunk that releses body except for
1864 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1865 if (in_lto_p
&& !force_gimple_thunk
)
1866 get_untransformed_body ();
1868 /* We need to force DECL_IGNORED_P when the thunk is created
1869 after early debug was run. */
1870 if (force_gimple_thunk
)
1871 DECL_IGNORED_P (thunk_fndecl
) = 1;
1873 a
= DECL_ARGUMENTS (thunk_fndecl
);
1875 current_function_decl
= thunk_fndecl
;
1877 /* Ensure thunks are emitted in their correct sections. */
1878 resolve_unique_section (thunk_fndecl
, 0,
1879 flag_function_sections
);
1881 bitmap_obstack_initialize (NULL
);
1883 if (thunk
.virtual_offset_p
)
1884 virtual_offset
= size_int (virtual_value
);
1886 /* Build the return declaration for the function. */
1887 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1888 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1890 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1891 DECL_ARTIFICIAL (resdecl
) = 1;
1892 DECL_IGNORED_P (resdecl
) = 1;
1893 DECL_CONTEXT (resdecl
) = thunk_fndecl
;
1894 DECL_RESULT (thunk_fndecl
) = resdecl
;
1897 resdecl
= DECL_RESULT (thunk_fndecl
);
1899 profile_count cfg_count
= count
;
1900 if (!cfg_count
.initialized_p ())
1901 cfg_count
= profile_count::from_gcov_type (BB_FREQ_MAX
).guessed_local ();
1903 bb
= then_bb
= else_bb
= return_bb
1904 = init_lowered_empty_function (thunk_fndecl
, true, cfg_count
);
1906 bsi
= gsi_start_bb (bb
);
1908 /* Build call to the function being thunked. */
1909 if (!VOID_TYPE_P (restype
)
1910 && (!alias_is_noreturn
1911 || TREE_ADDRESSABLE (restype
)
1912 || TREE_CODE (TYPE_SIZE_UNIT (restype
)) != INTEGER_CST
))
1914 if (DECL_BY_REFERENCE (resdecl
))
1916 restmp
= gimple_fold_indirect_ref (resdecl
);
1918 restmp
= build2 (MEM_REF
,
1919 TREE_TYPE (TREE_TYPE (resdecl
)),
1921 build_int_cst (TREE_TYPE (resdecl
), 0));
1923 else if (!is_gimple_reg_type (restype
))
1925 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
)))
1931 add_local_decl (cfun
, restmp
);
1932 BLOCK_VARS (DECL_INITIAL (current_function_decl
))
1937 restmp
= create_tmp_var (restype
, "retval");
1940 restmp
= create_tmp_reg (restype
, "retval");
1943 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1945 auto_vec
<tree
> vargs (nargs
);
1950 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1951 virtual_offset
, indirect_offset
));
1952 arg
= DECL_CHAIN (a
);
1957 for (; i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1960 if (VECTOR_TYPE_P (TREE_TYPE (arg
))
1961 || TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
1962 DECL_GIMPLE_REG_P (arg
) = 1;
1964 if (!is_gimple_val (arg
))
1966 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1967 (TREE_TYPE (arg
)), "arg");
1968 gimple
*stmt
= gimple_build_assign (tmp
, arg
);
1969 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1971 vargs
.quick_push (tmp
);
1973 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1974 callees
->call_stmt
= call
;
1975 gimple_call_set_from_thunk (call
, true);
1976 if (DECL_STATIC_CHAIN (alias
))
1978 tree p
= DECL_STRUCT_FUNCTION (alias
)->static_chain_decl
;
1979 tree type
= TREE_TYPE (p
);
1980 tree decl
= build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1981 PARM_DECL
, create_tmp_var_name ("CHAIN"),
1983 DECL_ARTIFICIAL (decl
) = 1;
1984 DECL_IGNORED_P (decl
) = 1;
1985 TREE_USED (decl
) = 1;
1986 DECL_CONTEXT (decl
) = thunk_fndecl
;
1987 DECL_ARG_TYPE (decl
) = type
;
1988 TREE_READONLY (decl
) = 1;
1990 struct function
*sf
= DECL_STRUCT_FUNCTION (thunk_fndecl
);
1991 sf
->static_chain_decl
= decl
;
1993 gimple_call_set_chain (call
, decl
);
1996 /* Return slot optimization is always possible and in fact requred to
1997 return values with DECL_BY_REFERENCE. */
1998 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
))
1999 && (!is_gimple_reg_type (TREE_TYPE (resdecl
))
2000 || DECL_BY_REFERENCE (resdecl
)))
2001 gimple_call_set_return_slot_opt (call
, true);
2005 gimple_call_set_lhs (call
, restmp
);
2006 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
2007 TREE_TYPE (TREE_TYPE (alias
))));
2009 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
2010 if (!alias_is_noreturn
)
2012 if (restmp
&& !this_adjusting
2013 && (fixed_offset
|| virtual_offset
))
2015 tree true_label
= NULL_TREE
;
2017 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
2021 /* If the return type is a pointer, we need to
2022 protect against NULL. We know there will be an
2023 adjustment, because that's why we're emitting a
2025 then_bb
= create_basic_block (NULL
, bb
);
2026 then_bb
->count
= cfg_count
- cfg_count
.apply_scale (1, 16);
2027 return_bb
= create_basic_block (NULL
, then_bb
);
2028 return_bb
->count
= cfg_count
;
2029 else_bb
= create_basic_block (NULL
, else_bb
);
2030 else_bb
->count
= cfg_count
.apply_scale (1, 16);
2031 add_bb_to_loop (then_bb
, bb
->loop_father
);
2032 add_bb_to_loop (return_bb
, bb
->loop_father
);
2033 add_bb_to_loop (else_bb
, bb
->loop_father
);
2034 remove_edge (single_succ_edge (bb
));
2035 true_label
= gimple_block_label (then_bb
);
2036 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
2037 build_zero_cst (TREE_TYPE (restmp
)),
2038 NULL_TREE
, NULL_TREE
);
2039 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
2040 e
= make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
2041 e
->probability
= profile_probability::guessed_always ()
2042 .apply_scale (1, 16);
2043 e
= make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
2044 e
->probability
= profile_probability::guessed_always ()
2045 .apply_scale (1, 16);
2046 make_single_succ_edge (return_bb
,
2047 EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
2048 make_single_succ_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
2049 e
= make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
2050 e
->probability
= profile_probability::always ();
2051 bsi
= gsi_last_bb (then_bb
);
2054 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
2055 fixed_offset
, virtual_offset
,
2060 bsi
= gsi_last_bb (else_bb
);
2061 stmt
= gimple_build_assign (restmp
,
2062 build_zero_cst (TREE_TYPE (restmp
)));
2063 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
2064 bsi
= gsi_last_bb (return_bb
);
2068 gimple_call_set_tail (call
, true);
2070 /* Build return value. */
2071 if (!DECL_BY_REFERENCE (resdecl
))
2072 ret
= gimple_build_return (restmp
);
2074 ret
= gimple_build_return (resdecl
);
2076 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
2080 gimple_call_set_tail (call
, true);
2081 remove_edge (single_succ_edge (bb
));
2084 cfun
->gimple_df
->in_ssa_p
= true;
2085 update_max_bb_count ();
2086 profile_status_for_fn (cfun
)
2087 = cfg_count
.initialized_p () && cfg_count
.ipa_p ()
2088 ? PROFILE_READ
: PROFILE_GUESSED
;
2089 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
2090 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
2091 delete_unreachable_blocks ();
2092 update_ssa (TODO_update_ssa
);
2093 checking_verify_flow_info ();
2094 free_dominance_info (CDI_DOMINATORS
);
2096 /* Since we want to emit the thunk, we explicitly mark its name as
2098 thunk
.thunk_p
= false;
2100 bitmap_obstack_release (NULL
);
2102 current_function_decl
= NULL
;
2107 /* Assemble thunks and aliases associated to node. */
2110 cgraph_node::assemble_thunks_and_aliases (void)
2115 for (e
= callers
; e
;)
2116 if (e
->caller
->thunk
.thunk_p
2117 && !e
->caller
->global
.inlined_to
2118 && !e
->caller
->thunk
.add_pointer_bounds_args
)
2120 cgraph_node
*thunk
= e
->caller
;
2123 thunk
->expand_thunk (true, false);
2124 thunk
->assemble_thunks_and_aliases ();
2129 FOR_EACH_ALIAS (this, ref
)
2131 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
2132 if (!alias
->transparent_alias
)
2134 bool saved_written
= TREE_ASM_WRITTEN (decl
);
2136 /* Force assemble_alias to really output the alias this time instead
2137 of buffering it in same alias pairs. */
2138 TREE_ASM_WRITTEN (decl
) = 1;
2139 do_assemble_alias (alias
->decl
,
2140 DECL_ASSEMBLER_NAME (decl
));
2141 alias
->assemble_thunks_and_aliases ();
2142 TREE_ASM_WRITTEN (decl
) = saved_written
;
2147 /* Expand function specified by node. */
2150 cgraph_node::expand (void)
2152 location_t saved_loc
;
2154 /* We ought to not compile any inline clones. */
2155 gcc_assert (!global
.inlined_to
);
2157 /* __RTL functions are compiled as soon as they are parsed, so don't
2159 if (native_rtl_p ())
2162 announce_function (decl
);
2164 gcc_assert (lowered
);
2165 get_untransformed_body ();
2167 /* Generate RTL for the body of DECL. */
2169 timevar_push (TV_REST_OF_COMPILATION
);
2171 gcc_assert (symtab
->global_info_ready
);
2173 /* Initialize the default bitmap obstack. */
2174 bitmap_obstack_initialize (NULL
);
2176 /* Initialize the RTL code for the function. */
2177 saved_loc
= input_location
;
2178 input_location
= DECL_SOURCE_LOCATION (decl
);
2180 gcc_assert (DECL_STRUCT_FUNCTION (decl
));
2181 push_cfun (DECL_STRUCT_FUNCTION (decl
));
2182 init_function_start (decl
);
2184 gimple_register_cfg_hooks ();
2186 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
2188 execute_all_ipa_transforms ();
2190 /* Perform all tree transforms and optimizations. */
2192 /* Signal the start of passes. */
2193 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
2195 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
2197 /* Signal the end of passes. */
2198 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
2200 bitmap_obstack_release (®_obstack
);
2202 /* Release the default bitmap obstack. */
2203 bitmap_obstack_release (NULL
);
2205 /* If requested, warn about function definitions where the function will
2206 return a value (usually of some struct or union type) which itself will
2207 take up a lot of stack space. */
2208 if (!DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
2210 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
2212 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
2213 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
2214 && compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
2215 warn_larger_than_size
) > 0)
2217 unsigned int size_as_int
2218 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
2220 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
2221 warning (OPT_Wlarger_than_
,
2222 "size of return value of %q+D is %u bytes",
2225 warning (OPT_Wlarger_than_
,
2226 "size of return value of %q+D is larger than %wu bytes",
2227 decl
, warn_larger_than_size
);
2231 gimple_set_body (decl
, NULL
);
2232 if (DECL_STRUCT_FUNCTION (decl
) == 0
2233 && !cgraph_node::get (decl
)->origin
)
2235 /* Stop pointing to the local nodes about to be freed.
2236 But DECL_INITIAL must remain nonzero so we know this
2237 was an actual function definition.
2238 For a nested function, this is done in c_pop_function_context.
2239 If rest_of_compilation set this to 0, leave it 0. */
2240 if (DECL_INITIAL (decl
) != 0)
2241 DECL_INITIAL (decl
) = error_mark_node
;
2244 input_location
= saved_loc
;
2247 timevar_pop (TV_REST_OF_COMPILATION
);
2249 /* Make sure that BE didn't give up on compiling. */
2250 gcc_assert (TREE_ASM_WRITTEN (decl
));
2254 /* It would make a lot more sense to output thunks before function body to get more
2255 forward and lest backwarding jumps. This however would need solving problem
2256 with comdats. See PR48668. Also aliases must come after function itself to
2257 make one pass assemblers, like one on AIX, happy. See PR 50689.
2258 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2260 assemble_thunks_and_aliases ();
2262 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2263 points to the dead function body. */
2265 remove_all_references ();
2268 /* Node comparer that is responsible for the order that corresponds
2269 to time when a function was launched for the first time. */
2272 node_cmp (const void *pa
, const void *pb
)
2274 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
2275 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
2277 /* Functions with time profile must be before these without profile. */
2278 if (!a
->tp_first_run
|| !b
->tp_first_run
)
2279 return a
->tp_first_run
- b
->tp_first_run
;
2281 return a
->tp_first_run
!= b
->tp_first_run
2282 ? b
->tp_first_run
- a
->tp_first_run
2283 : b
->order
- a
->order
;
2286 /* Expand all functions that must be output.
2288 Attempt to topologically sort the nodes so function is output when
2289 all called functions are already assembled to allow data to be
2290 propagated across the callgraph. Use a stack to get smaller distance
2291 between a function and its callees (later we may choose to use a more
2292 sophisticated algorithm for function reordering; we will likely want
2293 to use subsections to make the output functions appear in top-down
2297 expand_all_functions (void)
2300 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
2301 symtab
->cgraph_count
);
2302 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
2303 int order_pos
, new_order_pos
= 0;
2306 order_pos
= ipa_reverse_postorder (order
);
2307 gcc_assert (order_pos
== symtab
->cgraph_count
);
2309 /* Garbage collector may remove inline clones we eliminate during
2310 optimization. So we must be sure to not reference them. */
2311 for (i
= 0; i
< order_pos
; i
++)
2312 if (order
[i
]->process
)
2313 order
[new_order_pos
++] = order
[i
];
2315 if (flag_profile_reorder_functions
)
2316 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
2318 for (i
= new_order_pos
- 1; i
>= 0; i
--)
2324 expanded_func_count
++;
2325 if(node
->tp_first_run
)
2326 profiled_func_count
++;
2328 if (symtab
->dump_file
)
2329 fprintf (symtab
->dump_file
,
2330 "Time profile order in expand_all_functions:%s:%d\n",
2331 node
->asm_name (), node
->tp_first_run
);
2338 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
2339 main_input_filename
, profiled_func_count
, expanded_func_count
);
2341 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
2342 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
2343 profiled_func_count
, expanded_func_count
);
2345 symtab
->process_new_functions ();
2346 free_gimplify_stack ();
2351 /* This is used to sort the node types by the cgraph order number. */
2353 enum cgraph_order_sort_kind
2355 ORDER_UNDEFINED
= 0,
2362 struct cgraph_order_sort
2364 enum cgraph_order_sort_kind kind
;
2373 /* Output all functions, variables, and asm statements in the order
2374 according to their order fields, which is the order in which they
2375 appeared in the file. This implements -fno-toplevel-reorder. In
2376 this mode we may output functions and variables which don't really
2377 need to be output. */
2380 output_in_order (void)
2383 cgraph_order_sort
*nodes
;
2388 max
= symtab
->order
;
2389 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
2391 FOR_EACH_DEFINED_FUNCTION (pf
)
2393 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
2395 if (!pf
->no_reorder
)
2398 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2399 nodes
[i
].kind
= ORDER_FUNCTION
;
2404 /* There is a similar loop in symbol_table::output_variables.
2405 Please keep them in sync. */
2406 FOR_EACH_VARIABLE (pv
)
2408 if (!pv
->no_reorder
)
2410 if (DECL_HARD_REGISTER (pv
->decl
)
2411 || DECL_HAS_VALUE_EXPR_P (pv
->decl
))
2414 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2415 nodes
[i
].kind
= pv
->definition
? ORDER_VAR
: ORDER_VAR_UNDEF
;
2419 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
2422 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2423 nodes
[i
].kind
= ORDER_ASM
;
2427 /* In toplevel reorder mode we output all statics; mark them as needed. */
2429 for (i
= 0; i
< max
; ++i
)
2430 if (nodes
[i
].kind
== ORDER_VAR
)
2431 nodes
[i
].u
.v
->finalize_named_section_flags ();
2433 for (i
= 0; i
< max
; ++i
)
2435 switch (nodes
[i
].kind
)
2437 case ORDER_FUNCTION
:
2438 nodes
[i
].u
.f
->process
= 0;
2439 nodes
[i
].u
.f
->expand ();
2443 nodes
[i
].u
.v
->assemble_decl ();
2446 case ORDER_VAR_UNDEF
:
2447 assemble_undefined_decl (nodes
[i
].u
.v
->decl
);
2451 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2454 case ORDER_UNDEFINED
:
2462 symtab
->clear_asm_symbols ();
2470 gcc::pass_manager
*passes
= g
->get_passes ();
2473 current_function_decl
= NULL
;
2474 gimple_register_cfg_hooks ();
2475 bitmap_obstack_initialize (NULL
);
2477 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2481 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2486 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2487 devirtualization and other changes where removal iterate. */
2488 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2490 /* If pass_all_early_optimizations was not scheduled, the state of
2491 the cgraph will not be properly updated. Update it now. */
2492 if (symtab
->state
< IPA_SSA
)
2493 symtab
->state
= IPA_SSA
;
2497 /* Generate coverage variables and constructors. */
2500 /* Process new functions added. */
2502 current_function_decl
= NULL
;
2503 symtab
->process_new_functions ();
2505 execute_ipa_summary_passes
2506 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2509 /* Some targets need to handle LTO assembler output specially. */
2510 if (flag_generate_lto
|| flag_generate_offload
)
2511 targetm
.asm_out
.lto_start ();
2514 || flag_incremental_link
== INCREMENTAL_LINK_LTO
)
2517 fprintf (stderr
, "Streaming LTO\n");
2518 if (g
->have_offload
)
2520 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2521 lto_stream_offload_p
= true;
2522 ipa_write_summaries ();
2523 lto_stream_offload_p
= false;
2527 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2528 lto_stream_offload_p
= false;
2529 ipa_write_summaries ();
2533 if (flag_generate_lto
|| flag_generate_offload
)
2534 targetm
.asm_out
.lto_end ();
2537 && ((in_lto_p
&& flag_incremental_link
!= INCREMENTAL_LINK_LTO
)
2538 || !flag_lto
|| flag_fat_lto_objects
))
2539 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2540 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2542 bitmap_obstack_release (NULL
);
2546 /* Return string alias is alias of. */
2549 get_alias_symbol (tree decl
)
2551 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2552 return get_identifier (TREE_STRING_POINTER
2553 (TREE_VALUE (TREE_VALUE (alias
))));
2557 /* Weakrefs may be associated to external decls and thus not output
2558 at expansion time. Emit all necessary aliases. */
2561 symbol_table::output_weakrefs (void)
2564 FOR_EACH_SYMBOL (node
)
2566 && !TREE_ASM_WRITTEN (node
->decl
)
2571 /* Weakrefs are special by not requiring target definition in current
2572 compilation unit. It is thus bit hard to work out what we want to
2574 When alias target is defined, we need to fetch it from symtab reference,
2575 otherwise it is pointed to by alias_target. */
2576 if (node
->alias_target
)
2577 target
= (DECL_P (node
->alias_target
)
2578 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2579 : node
->alias_target
);
2580 else if (node
->analyzed
)
2581 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2585 target
= get_alias_symbol (node
->decl
);
2587 do_assemble_alias (node
->decl
, target
);
2591 /* Perform simple optimizations based on callgraph. */
2594 symbol_table::compile (void)
2599 symtab_node::checking_verify_symtab_nodes ();
2601 timevar_push (TV_CGRAPHOPT
);
2602 if (pre_ipa_mem_report
)
2604 fprintf (stderr
, "Memory consumption before IPA\n");
2605 dump_memory_report (false);
2608 fprintf (stderr
, "Performing interprocedural optimizations\n");
2611 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2612 if (flag_generate_lto
|| flag_generate_offload
)
2613 lto_streamer_hooks_init ();
2615 /* Don't run the IPA passes if there was any error or sorry messages. */
2619 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2621 || ((!in_lto_p
|| flag_incremental_link
== INCREMENTAL_LINK_LTO
)
2622 && flag_lto
&& !flag_fat_lto_objects
))
2624 timevar_pop (TV_CGRAPHOPT
);
2628 global_info_ready
= true;
2631 fprintf (dump_file
, "Optimized ");
2632 symtab
->dump (dump_file
);
2634 if (post_ipa_mem_report
)
2636 fprintf (stderr
, "Memory consumption after IPA\n");
2637 dump_memory_report (false);
2639 timevar_pop (TV_CGRAPHOPT
);
2641 /* Output everything. */
2642 switch_to_section (text_section
);
2643 (*debug_hooks
->assembly_start
) ();
2645 fprintf (stderr
, "Assembling functions:\n");
2646 symtab_node::checking_verify_symtab_nodes ();
2648 bitmap_obstack_initialize (NULL
);
2649 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2650 bitmap_obstack_release (NULL
);
2651 mark_functions_to_output ();
2653 /* When weakref support is missing, we automatically translate all
2654 references to NODE to references to its ultimate alias target.
2655 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2658 Set up this mapping before we output any assembler but once we are sure
2659 that all symbol renaming is done.
2661 FIXME: All this uglyness can go away if we just do renaming at gimple
2662 level by physically rewritting the IL. At the moment we can only redirect
2663 calls, so we need infrastructure for renaming references as well. */
2664 #ifndef ASM_OUTPUT_WEAKREF
2667 FOR_EACH_SYMBOL (node
)
2669 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2671 IDENTIFIER_TRANSPARENT_ALIAS
2672 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2673 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2674 = (node
->alias_target
? node
->alias_target
2675 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2681 /* Output first asm statements and anything ordered. The process
2682 flag is cleared for these nodes, so we skip them later. */
2684 expand_all_functions ();
2685 output_variables ();
2687 process_new_functions ();
2693 fprintf (dump_file
, "\nFinal ");
2694 symtab
->dump (dump_file
);
2698 symtab_node::verify_symtab_nodes ();
2699 /* Double check that all inline clones are gone and that all
2700 function bodies have been released from memory. */
2704 bool error_found
= false;
2706 FOR_EACH_DEFINED_FUNCTION (node
)
2707 if (node
->global
.inlined_to
2708 || gimple_has_body_p (node
->decl
))
2714 internal_error ("nodes with unreleased memory found");
2718 /* Earlydebug dump file, flags, and number. */
2720 static int debuginfo_early_dump_nr
;
2721 static FILE *debuginfo_early_dump_file
;
2722 static dump_flags_t debuginfo_early_dump_flags
;
2724 /* Debug dump file, flags, and number. */
2726 static int debuginfo_dump_nr
;
2727 static FILE *debuginfo_dump_file
;
2728 static dump_flags_t debuginfo_dump_flags
;
2730 /* Register the debug and earlydebug dump files. */
2733 debuginfo_early_init (void)
2735 gcc::dump_manager
*dumps
= g
->get_dumps ();
2736 debuginfo_early_dump_nr
= dumps
->dump_register (".earlydebug", "earlydebug",
2737 "earlydebug", DK_tree
,
2740 debuginfo_dump_nr
= dumps
->dump_register (".debug", "debug",
2746 /* Initialize the debug and earlydebug dump files. */
2749 debuginfo_init (void)
2751 gcc::dump_manager
*dumps
= g
->get_dumps ();
2752 debuginfo_dump_file
= dump_begin (debuginfo_dump_nr
, NULL
);
2753 debuginfo_dump_flags
= dumps
->get_dump_file_info (debuginfo_dump_nr
)->pflags
;
2754 debuginfo_early_dump_file
= dump_begin (debuginfo_early_dump_nr
, NULL
);
2755 debuginfo_early_dump_flags
2756 = dumps
->get_dump_file_info (debuginfo_early_dump_nr
)->pflags
;
2759 /* Finalize the debug and earlydebug dump files. */
2762 debuginfo_fini (void)
2764 if (debuginfo_dump_file
)
2765 dump_end (debuginfo_dump_nr
, debuginfo_dump_file
);
2766 if (debuginfo_early_dump_file
)
2767 dump_end (debuginfo_early_dump_nr
, debuginfo_early_dump_file
);
2770 /* Set dump_file to the debug dump file. */
2773 debuginfo_start (void)
2775 set_dump_file (debuginfo_dump_file
);
2778 /* Undo setting dump_file to the debug dump file. */
2781 debuginfo_stop (void)
2783 set_dump_file (NULL
);
2786 /* Set dump_file to the earlydebug dump file. */
2789 debuginfo_early_start (void)
2791 set_dump_file (debuginfo_early_dump_file
);
2794 /* Undo setting dump_file to the earlydebug dump file. */
2797 debuginfo_early_stop (void)
2799 set_dump_file (NULL
);
2802 /* Analyze the whole compilation unit once it is parsed completely. */
2805 symbol_table::finalize_compilation_unit (void)
2807 timevar_push (TV_CGRAPH
);
2809 /* If we're here there's no current function anymore. Some frontends
2810 are lazy in clearing these. */
2811 current_function_decl
= NULL
;
2814 /* Do not skip analyzing the functions if there were errors, we
2815 miss diagnostics for following functions otherwise. */
2817 /* Emit size functions we didn't inline. */
2818 finalize_size_functions ();
2820 /* Mark alias targets necessary and emit diagnostics. */
2821 handle_alias_pairs ();
2825 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2829 if (flag_dump_passes
)
2832 /* Gimplify and lower all functions, compute reachability and
2833 remove unreachable nodes. */
2834 analyze_functions (/*first_time=*/true);
2836 /* Mark alias targets necessary and emit diagnostics. */
2837 handle_alias_pairs ();
2839 /* Gimplify and lower thunks. */
2840 analyze_functions (/*first_time=*/false);
2842 /* Offloading requires LTO infrastructure. */
2843 if (!in_lto_p
&& g
->have_offload
)
2844 flag_generate_offload
= 1;
2848 /* Emit early debug for reachable functions, and by consequence,
2849 locally scoped symbols. */
2850 struct cgraph_node
*cnode
;
2851 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode
)
2852 (*debug_hooks
->early_global_decl
) (cnode
->decl
);
2854 /* Clean up anything that needs cleaning up after initial debug
2856 debuginfo_early_start ();
2857 (*debug_hooks
->early_finish
) (main_input_filename
);
2858 debuginfo_early_stop ();
2861 /* Finally drive the pass manager. */
2864 timevar_pop (TV_CGRAPH
);
2867 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2868 within the same process. For use by toplev::finalize. */
2871 cgraphunit_c_finalize (void)
2873 gcc_assert (cgraph_new_nodes
.length () == 0);
2874 cgraph_new_nodes
.truncate (0);
2876 vtable_entry_type
= NULL
;
2877 queued_nodes
= &symtab_terminator
;
2879 first_analyzed
= NULL
;
2880 first_analyzed_var
= NULL
;
2883 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2884 kind of wrapper method. */
2887 cgraph_node::create_wrapper (cgraph_node
*target
)
2889 /* Preserve DECL_RESULT so we get right by reference flag. */
2890 tree decl_result
= DECL_RESULT (decl
);
2892 /* Remove the function's body but keep arguments to be reused
2894 release_body (true);
2897 DECL_UNINLINABLE (decl
) = false;
2898 DECL_RESULT (decl
) = decl_result
;
2899 DECL_INITIAL (decl
) = NULL
;
2900 allocate_struct_function (decl
, false);
2903 /* Turn alias into thunk and expand it into GIMPLE representation. */
2906 memset (&thunk
, 0, sizeof (cgraph_thunk_info
));
2907 thunk
.thunk_p
= true;
2908 create_edge (target
, NULL
, count
);
2909 callees
->can_throw_external
= !TREE_NOTHROW (target
->decl
);
2911 tree arguments
= DECL_ARGUMENTS (decl
);
2915 TREE_ADDRESSABLE (arguments
) = false;
2916 arguments
= TREE_CHAIN (arguments
);
2919 expand_thunk (false, true);
2921 /* Inline summary set-up. */
2923 inline_analyze_function (this);
2926 #include "gt-cgraphunit.h"