1 /* Driver of optimization process
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This module implements main driver of compilation process.
23 The main scope of this file is to act as an interface in between
24 tree based frontends and the backend.
26 The front-end is supposed to use following functionality:
30 This function is called once front-end has parsed whole body of function
31 and it is certain that the function body nor the declaration will change.
33 (There is one exception needed for implementing GCC extern inline
36 - varpool_finalize_decl
38 This function has same behavior as the above but is used for static
43 Insert new toplevel ASM statement
45 - finalize_compilation_unit
47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
50 The symbol table is constructed starting from the trivially needed
51 symbols finalized by the frontend. Functions are lowered into
52 GIMPLE representation and callgraph/reference lists are constructed.
53 Those are used to discover other necessary functions and variables.
55 At the end the bodies of unreachable functions are removed.
57 The function can be called multiple times when multiple source level
58 compilation units are combined.
62 This passes control to the back-end. Optimizations are performed and
63 final assembler is generated. This is done in the following way. Note
64 that with link time optimization the process is split into three
65 stages (compile time, linktime analysis and parallel linktime as
70 1) Inter-procedural optimization.
73 This part is further split into:
75 a) early optimizations. These are local passes executed in
76 the topological order on the callgraph.
78 The purpose of early optimiations is to optimize away simple
79 things that may otherwise confuse IP analysis. Very simple
80 propagation across the callgraph is done i.e. to discover
81 functions without side effects and simple inlining is performed.
83 b) early small interprocedural passes.
85 Those are interprocedural passes executed only at compilation
86 time. These include, for example, transational memory lowering,
87 unreachable code removal and other simple transformations.
89 c) IP analysis stage. All interprocedural passes do their
92 Interprocedural passes differ from small interprocedural
93 passes by their ability to operate across whole program
94 at linktime. Their analysis stage is performed early to
95 both reduce linking times and linktime memory usage by
96 not having to represent whole program in memory.
98 d) LTO sreaming. When doing LTO, everything important gets
99 streamed into the object file.
101 Compile time and or linktime analysis stage (WPA):
103 At linktime units gets streamed back and symbol table is
104 merged. Function bodies are not streamed in and not
106 e) IP propagation stage. All IP passes execute their
107 IP propagation. This is done based on the earlier analysis
108 without having function bodies at hand.
109 f) Ltrans streaming. When doing WHOPR LTO, the program
110 is partitioned and streamed into multple object files.
112 Compile time and/or parallel linktime stage (ltrans)
114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
121 IP passes can produce copies of existing functoins (such
122 as versioned clones or inline clones) without actually
123 manipulating their bodies by creating virtual clones in
124 the callgraph. At this time the virtual clones are
125 turned into real functions
128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
131 4) late small IP passes
133 Simple IP passes working within single program partition.
136 (expand_all_functions)
138 At this stage functions that needs to be output into
139 assembler are identified and compiled in topological order
140 6) Output of variables and aliases
141 Now it is known what variable references was not optimized
142 out and thus all variables are output to the file.
144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
147 Finally there are functions to manipulate the callgraph from
149 - cgraph_add_new_function is used to add backend produced
150 functions introduced after the unit is finalized.
151 The functions are enqueue for later processing and inserted
152 into callgraph with cgraph_process_new_functions.
154 - cgraph_function_versioning
156 produces a copy of function into new one (a version)
157 and apply simple transformations
162 #include "coretypes.h"
164 #include "hash-set.h"
165 #include "machmode.h"
167 #include "double-int.h"
171 #include "wide-int.h"
174 #include "fold-const.h"
176 #include "stor-layout.h"
177 #include "stringpool.h"
181 #include "hard-reg-set.h"
183 #include "function.h"
184 #include "basic-block.h"
185 #include "tree-ssa-alias.h"
186 #include "internal-fn.h"
187 #include "gimple-fold.h"
188 #include "gimple-expr.h"
191 #include "gimplify.h"
192 #include "gimple-iterator.h"
193 #include "gimplify-me.h"
194 #include "gimple-ssa.h"
195 #include "tree-cfg.h"
196 #include "tree-into-ssa.h"
197 #include "tree-ssa.h"
198 #include "tree-inline.h"
199 #include "langhooks.h"
204 #include "diagnostic.h"
207 #include "hash-map.h"
208 #include "plugin-api.h"
211 #include "alloc-pool.h"
212 #include "symbol-summary.h"
213 #include "ipa-prop.h"
214 #include "tree-iterator.h"
215 #include "tree-pass.h"
216 #include "tree-dump.h"
217 #include "gimple-pretty-print.h"
219 #include "coverage.h"
221 #include "ipa-inline.h"
222 #include "ipa-utils.h"
223 #include "lto-streamer.h"
226 #include "regset.h" /* FIXME: For reg_obstack. */
228 #include "pass_manager.h"
229 #include "tree-nested.h"
230 #include "gimplify.h"
232 #include "tree-chkp.h"
233 #include "lto-section-names.h"
235 #include "print-tree.h"
237 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
238 secondary queue used during optimization to accommodate passes that
239 may generate new functions that need to be optimized and expanded. */
240 vec
<cgraph_node
*> cgraph_new_nodes
;
242 static void expand_all_functions (void);
243 static void mark_functions_to_output (void);
244 static void handle_alias_pairs (void);
246 /* Used for vtable lookup in thunk adjusting. */
247 static GTY (()) tree vtable_entry_type
;
249 /* Determine if symbol declaration is needed. That is, visible to something
250 either outside this translation unit, something magic in the system
253 symtab_node::needed_p (void)
255 /* Double check that no one output the function into assembly file
257 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
258 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
263 if (DECL_EXTERNAL (decl
))
266 /* If the user told us it is used, then it must be so. */
270 /* ABI forced symbols are needed when they are external. */
271 if (forced_by_abi
&& TREE_PUBLIC (decl
))
274 /* Keep constructors, destructors and virtual functions. */
275 if (TREE_CODE (decl
) == FUNCTION_DECL
276 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
279 /* Externally visible variables must be output. The exception is
280 COMDAT variables that must be output only when they are needed. */
281 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
287 /* Head and terminator of the queue of nodes to be processed while building
290 static symtab_node symtab_terminator
;
291 static symtab_node
*queued_nodes
= &symtab_terminator
;
293 /* Add NODE to queue starting at QUEUED_NODES.
294 The queue is linked via AUX pointers and terminated by pointer to 1. */
297 enqueue_node (symtab_node
*node
)
301 gcc_checking_assert (queued_nodes
);
302 node
->aux
= queued_nodes
;
306 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
307 functions into callgraph in a way so they look like ordinary reachable
308 functions inserted into callgraph already at construction time. */
311 symbol_table::process_new_functions (void)
315 if (!cgraph_new_nodes
.exists ())
318 handle_alias_pairs ();
319 /* Note that this queue may grow as its being processed, as the new
320 functions may generate new ones. */
321 for (unsigned i
= 0; i
< cgraph_new_nodes
.length (); i
++)
323 cgraph_node
*node
= cgraph_new_nodes
[i
];
328 /* At construction time we just need to finalize function and move
329 it into reachable functions list. */
331 cgraph_node::finalize_function (fndecl
, false);
332 call_cgraph_insertion_hooks (node
);
338 case IPA_SSA_AFTER_INLINING
:
339 /* When IPA optimization already started, do all essential
340 transformations that has been already performed on the whole
341 cgraph but not on this function. */
343 gimple_register_cfg_hooks ();
346 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
347 if ((state
== IPA_SSA
|| state
== IPA_SSA_AFTER_INLINING
)
348 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
349 g
->get_passes ()->execute_early_local_passes ();
350 else if (inline_summaries
!= NULL
)
351 compute_inline_parameters (node
, true);
352 free_dominance_info (CDI_POST_DOMINATORS
);
353 free_dominance_info (CDI_DOMINATORS
);
355 call_cgraph_insertion_hooks (node
);
359 /* Functions created during expansion shall be compiled
362 call_cgraph_insertion_hooks (node
);
372 cgraph_new_nodes
.release ();
375 /* As an GCC extension we allow redefinition of the function. The
376 semantics when both copies of bodies differ is not well defined.
377 We replace the old body with new body so in unit at a time mode
378 we always use new body, while in normal mode we may end up with
379 old body inlined into some functions and new body expanded and
382 ??? It may make more sense to use one body for inlining and other
383 body for expanding the function but this is difficult to do. */
386 cgraph_node::reset (void)
388 /* If process is set, then we have already begun whole-unit analysis.
389 This is *not* testing for whether we've already emitted the function.
390 That case can be sort-of legitimately seen with real function redefinition
391 errors. I would argue that the front end should never present us with
392 such a case, but don't enforce that for now. */
393 gcc_assert (!process
);
395 /* Reset our data structures so we can analyze the function again. */
396 memset (&local
, 0, sizeof (local
));
397 memset (&global
, 0, sizeof (global
));
398 memset (&rtl
, 0, sizeof (rtl
));
403 cpp_implicit_alias
= false;
406 remove_all_references ();
409 /* Return true when there are references to the node. */
412 symtab_node::referred_to_p (void)
416 /* See if there are any references at all. */
417 if (iterate_referring (0, ref
))
419 /* For functions check also calls. */
420 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
421 if (cn
&& cn
->callers
)
426 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
427 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
428 the garbage collector run at the moment. We would need to either create
429 a new GC context, or just not compile right now. */
432 cgraph_node::finalize_function (tree decl
, bool no_collect
)
434 cgraph_node
*node
= cgraph_node::get_create (decl
);
436 if (node
->definition
)
438 /* Nested functions should only be defined once. */
439 gcc_assert (!DECL_CONTEXT (decl
)
440 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
442 node
->local
.redefined_extern_inline
= true;
445 notice_global_symbol (decl
);
446 node
->definition
= true;
447 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
449 /* With -fkeep-inline-functions we are keeping all inline functions except
450 for extern inline ones. */
451 if (flag_keep_inline_functions
452 && DECL_DECLARED_INLINE_P (decl
)
453 && !DECL_EXTERNAL (decl
)
454 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
455 node
->force_output
= 1;
457 /* When not optimizing, also output the static functions. (see
458 PR24561), but don't do so for always_inline functions, functions
459 declared inline and nested functions. These were optimized out
460 in the original implementation and it is unclear whether we want
461 to change the behavior here. */
462 if ((!opt_for_fn (decl
, optimize
)
463 && !node
->cpp_implicit_alias
464 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
465 && !DECL_DECLARED_INLINE_P (decl
)
466 && !(DECL_CONTEXT (decl
)
467 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
468 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
469 node
->force_output
= 1;
471 /* If we've not yet emitted decl, tell the debug info about it. */
472 if (!TREE_ASM_WRITTEN (decl
))
473 (*debug_hooks
->deferred_inline_function
) (decl
);
475 /* Possibly warn about unused parameters. */
476 if (warn_unused_parameter
)
477 do_warn_unused_parameter (decl
);
482 if (symtab
->state
== CONSTRUCTION
483 && (node
->needed_p () || node
->referred_to_p ()))
487 /* Add the function FNDECL to the call graph.
488 Unlike finalize_function, this function is intended to be used
489 by middle end and allows insertion of new function at arbitrary point
490 of compilation. The function can be either in high, low or SSA form
493 The function is assumed to be reachable and have address taken (so no
494 API breaking optimizations are performed on it).
496 Main work done by this function is to enqueue the function for later
497 processing to avoid need the passes to be re-entrant. */
500 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
502 gcc::pass_manager
*passes
= g
->get_passes ();
504 switch (symtab
->state
)
507 cgraph_node::finalize_function (fndecl
, false);
510 /* Just enqueue function to be processed at nearest occurrence. */
511 node
= cgraph_node::get_create (fndecl
);
513 node
->lowered
= true;
514 cgraph_new_nodes
.safe_push (node
);
519 case IPA_SSA_AFTER_INLINING
:
521 /* Bring the function into finalized state and enqueue for later
522 analyzing and compilation. */
523 node
= cgraph_node::get_create (fndecl
);
524 node
->local
.local
= false;
525 node
->definition
= true;
526 node
->force_output
= true;
527 if (!lowered
&& symtab
->state
== EXPANSION
)
529 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
530 gimple_register_cfg_hooks ();
531 bitmap_obstack_initialize (NULL
);
532 execute_pass_list (cfun
, passes
->all_lowering_passes
);
533 passes
->execute_early_local_passes ();
534 bitmap_obstack_release (NULL
);
540 node
->lowered
= true;
541 cgraph_new_nodes
.safe_push (node
);
545 /* At the very end of compilation we have to do all the work up
547 node
= cgraph_node::create (fndecl
);
549 node
->lowered
= true;
550 node
->definition
= true;
552 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
553 gimple_register_cfg_hooks ();
554 bitmap_obstack_initialize (NULL
);
555 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
556 g
->get_passes ()->execute_early_local_passes ();
557 bitmap_obstack_release (NULL
);
566 /* Set a personality if required and we already passed EH lowering. */
568 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
569 == eh_personality_lang
))
570 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
573 /* Analyze the function scheduled to be output. */
575 cgraph_node::analyze (void)
577 tree decl
= this->decl
;
578 location_t saved_loc
= input_location
;
579 input_location
= DECL_SOURCE_LOCATION (decl
);
583 cgraph_node
*t
= cgraph_node::get (thunk
.alias
);
585 create_edge (t
, NULL
, 0, CGRAPH_FREQ_BASE
);
586 /* Target code in expand_thunk may need the thunk's target
587 to be analyzed, so recurse here. */
592 t
= t
->get_alias_target ();
596 if (!expand_thunk (false, false))
604 resolve_alias (cgraph_node::get (alias_target
));
605 else if (dispatcher_function
)
607 /* Generate the dispatcher body of multi-versioned functions. */
608 cgraph_function_version_info
*dispatcher_version_info
609 = function_version ();
610 if (dispatcher_version_info
!= NULL
611 && (dispatcher_version_info
->dispatcher_resolver
614 tree resolver
= NULL_TREE
;
615 gcc_assert (targetm
.generate_version_dispatcher_body
);
616 resolver
= targetm
.generate_version_dispatcher_body (this);
617 gcc_assert (resolver
!= NULL_TREE
);
622 push_cfun (DECL_STRUCT_FUNCTION (decl
));
624 assign_assembler_name_if_neeeded (decl
);
626 /* Make sure to gimplify bodies only once. During analyzing a
627 function we lower it, which will require gimplified nested
628 functions, so we can end up here with an already gimplified
630 if (!gimple_has_body_p (decl
))
631 gimplify_function_tree (decl
);
632 dump_function (TDI_generic
, decl
);
634 /* Lower the function. */
638 lower_nested_functions (decl
);
639 gcc_assert (!nested
);
641 gimple_register_cfg_hooks ();
642 bitmap_obstack_initialize (NULL
);
643 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
644 free_dominance_info (CDI_POST_DOMINATORS
);
645 free_dominance_info (CDI_DOMINATORS
);
647 bitmap_obstack_release (NULL
);
655 input_location
= saved_loc
;
658 /* C++ frontend produce same body aliases all over the place, even before PCH
659 gets streamed out. It relies on us linking the aliases with their function
660 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
661 first produce aliases without links, but once C++ FE is sure he won't sream
662 PCH we build the links via this function. */
665 symbol_table::process_same_body_aliases (void)
668 FOR_EACH_SYMBOL (node
)
669 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
671 (TREE_CODE (node
->alias_target
) == VAR_DECL
672 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
673 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
674 cpp_implicit_aliases_done
= true;
677 /* Process attributes common for vars and functions. */
680 process_common_attributes (symtab_node
*node
, tree decl
)
682 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
684 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
686 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
687 "%<weakref%> attribute should be accompanied with"
688 " an %<alias%> attribute");
689 DECL_WEAK (decl
) = 0;
690 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
691 DECL_ATTRIBUTES (decl
));
694 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
695 node
->no_reorder
= 1;
698 /* Look for externally_visible and used attributes and mark cgraph nodes
701 We cannot mark the nodes at the point the attributes are processed (in
702 handle_*_attribute) because the copy of the declarations available at that
703 point may not be canonical. For example, in:
706 void f() __attribute__((used));
708 the declaration we see in handle_used_attribute will be the second
709 declaration -- but the front end will subsequently merge that declaration
710 with the original declaration and discard the second declaration.
712 Furthermore, we can't mark these nodes in finalize_function because:
715 void f() __attribute__((externally_visible));
719 So, we walk the nodes at the end of the translation unit, applying the
720 attributes at that point. */
723 process_function_and_variable_attributes (cgraph_node
*first
,
724 varpool_node
*first_var
)
729 for (node
= symtab
->first_function (); node
!= first
;
730 node
= symtab
->next_function (node
))
732 tree decl
= node
->decl
;
733 if (DECL_PRESERVE_P (decl
))
734 node
->mark_force_output ();
735 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
737 if (! TREE_PUBLIC (node
->decl
))
738 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
739 "%<externally_visible%>"
740 " attribute have effect only on public objects");
742 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
743 && (node
->definition
&& !node
->alias
))
745 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
746 "%<weakref%> attribute ignored"
747 " because function is defined");
748 DECL_WEAK (decl
) = 0;
749 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
750 DECL_ATTRIBUTES (decl
));
753 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
754 && !DECL_DECLARED_INLINE_P (decl
)
755 /* redefining extern inline function makes it DECL_UNINLINABLE. */
756 && !DECL_UNINLINABLE (decl
))
757 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
758 "always_inline function might not be inlinable");
760 process_common_attributes (node
, decl
);
762 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
763 vnode
= symtab
->next_variable (vnode
))
765 tree decl
= vnode
->decl
;
766 if (DECL_EXTERNAL (decl
)
767 && DECL_INITIAL (decl
))
768 varpool_node::finalize_decl (decl
);
769 if (DECL_PRESERVE_P (decl
))
770 vnode
->force_output
= true;
771 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
773 if (! TREE_PUBLIC (vnode
->decl
))
774 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
775 "%<externally_visible%>"
776 " attribute have effect only on public objects");
778 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
780 && DECL_INITIAL (decl
))
782 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
783 "%<weakref%> attribute ignored"
784 " because variable is initialized");
785 DECL_WEAK (decl
) = 0;
786 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
787 DECL_ATTRIBUTES (decl
));
789 process_common_attributes (vnode
, decl
);
793 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
794 middle end to output the variable to asm file, if needed or externally
798 varpool_node::finalize_decl (tree decl
)
800 varpool_node
*node
= varpool_node::get_create (decl
);
802 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
804 if (node
->definition
)
806 notice_global_symbol (decl
);
807 node
->definition
= true;
808 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
809 /* Traditionally we do not eliminate static variables when not
810 optimizing and when not doing toplevel reoder. */
812 || ((!flag_toplevel_reorder
813 && !DECL_COMDAT (node
->decl
)
814 && !DECL_ARTIFICIAL (node
->decl
))))
815 node
->force_output
= true;
817 if (symtab
->state
== CONSTRUCTION
818 && (node
->needed_p () || node
->referred_to_p ()))
820 if (symtab
->state
>= IPA_SSA
)
822 /* Some frontends produce various interface variables after compilation
824 if (symtab
->state
== FINISHED
825 || (!flag_toplevel_reorder
826 && symtab
->state
== EXPANSION
))
827 node
->assemble_decl ();
829 if (DECL_INITIAL (decl
))
830 chkp_register_var_initializer (decl
);
833 /* EDGE is an polymorphic call. Mark all possible targets as reachable
834 and if there is only one target, perform trivial devirtualization.
835 REACHABLE_CALL_TARGETS collects target lists we already walked to
836 avoid udplicate work. */
839 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
845 vec
<cgraph_node
*>targets
846 = possible_polymorphic_call_targets
847 (edge
, &final
, &cache_token
);
849 if (!reachable_call_targets
->add (cache_token
))
851 if (symtab
->dump_file
)
852 dump_possible_polymorphic_call_targets
853 (symtab
->dump_file
, edge
);
855 for (i
= 0; i
< targets
.length (); i
++)
857 /* Do not bother to mark virtual methods in anonymous namespace;
858 either we will find use of virtual table defining it, or it is
860 if (targets
[i
]->definition
862 (TREE_TYPE (targets
[i
]->decl
))
864 && !type_in_anonymous_namespace_p
866 (TREE_TYPE (targets
[i
]->decl
))))
867 enqueue_node (targets
[i
]);
871 /* Very trivial devirtualization; when the type is
872 final or anonymous (so we know all its derivation)
873 and there is only one possible virtual call target,
874 make the edge direct. */
877 if (targets
.length () <= 1 && dbg_cnt (devirt
))
880 if (targets
.length () == 1)
883 target
= cgraph_node::create
884 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
886 if (symtab
->dump_file
)
888 fprintf (symtab
->dump_file
,
889 "Devirtualizing call: ");
890 print_gimple_stmt (symtab
->dump_file
,
894 if (dump_enabled_p ())
896 location_t locus
= gimple_location_safe (edge
->call_stmt
);
897 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
898 "devirtualizing call in %s to %s\n",
899 edge
->caller
->name (), target
->name ());
902 edge
->make_direct (target
);
903 edge
->redirect_call_stmt_to_callee ();
905 /* Call to __builtin_unreachable shouldn't be instrumented. */
906 if (!targets
.length ())
907 gimple_call_set_with_bounds (edge
->call_stmt
, false);
909 if (symtab
->dump_file
)
911 fprintf (symtab
->dump_file
,
912 "Devirtualized as: ");
913 print_gimple_stmt (symtab
->dump_file
,
922 /* Discover all functions and variables that are trivially needed, analyze
923 them as well as all functions and variables referred by them */
924 static cgraph_node
*first_analyzed
;
925 static varpool_node
*first_analyzed_var
;
928 analyze_functions (void)
930 /* Keep track of already processed nodes when called multiple times for
931 intermodule optimization. */
932 cgraph_node
*first_handled
= first_analyzed
;
933 varpool_node
*first_handled_var
= first_analyzed_var
;
934 hash_set
<void *> reachable_call_targets
;
941 location_t saved_loc
= input_location
;
943 bitmap_obstack_initialize (NULL
);
944 symtab
->state
= CONSTRUCTION
;
945 input_location
= UNKNOWN_LOCATION
;
947 /* Ugly, but the fixup can not happen at a time same body alias is created;
948 C++ FE is confused about the COMDAT groups being right. */
949 if (symtab
->cpp_implicit_aliases_done
)
950 FOR_EACH_SYMBOL (node
)
951 if (node
->cpp_implicit_alias
)
952 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
953 build_type_inheritance_graph ();
955 /* Analysis adds static variables that in turn adds references to new functions.
956 So we need to iterate the process until it stabilize. */
960 process_function_and_variable_attributes (first_analyzed
,
963 /* First identify the trivially needed symbols. */
964 for (node
= symtab
->first_symbol ();
965 node
!= first_analyzed
966 && node
!= first_analyzed_var
; node
= node
->next
)
968 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
969 node
->get_comdat_group_id ();
970 if (node
->needed_p ())
973 if (!changed
&& symtab
->dump_file
)
974 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
976 if (symtab
->dump_file
)
977 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
978 if (!changed
&& symtab
->dump_file
)
979 fprintf (symtab
->dump_file
, "\n");
981 if (node
== first_analyzed
982 || node
== first_analyzed_var
)
985 symtab
->process_new_functions ();
986 first_analyzed_var
= symtab
->first_variable ();
987 first_analyzed
= symtab
->first_function ();
989 if (changed
&& symtab
->dump_file
)
990 fprintf (symtab
->dump_file
, "\n");
992 /* Lower representation, build callgraph edges and references for all trivially
993 needed symbols and all symbols referred by them. */
994 while (queued_nodes
!= &symtab_terminator
)
998 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
999 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
1000 if (cnode
&& cnode
->definition
)
1003 tree decl
= cnode
->decl
;
1005 /* ??? It is possible to create extern inline function
1006 and later using weak alias attribute to kill its body.
1007 See gcc.c-torture/compile/20011119-1.c */
1008 if (!DECL_STRUCT_FUNCTION (decl
)
1010 && !cnode
->thunk
.thunk_p
1011 && !cnode
->dispatcher_function
)
1014 cnode
->local
.redefined_extern_inline
= true;
1018 if (!cnode
->analyzed
)
1021 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
1022 if (edge
->callee
->definition
1023 && (!DECL_EXTERNAL (edge
->callee
->decl
)
1024 /* When not optimizing, do not try to analyze extern
1025 inline functions. Doing so is pointless. */
1026 || opt_for_fn (edge
->callee
->decl
, optimize
)
1027 /* Weakrefs needs to be preserved. */
1028 || edge
->callee
->alias
1029 /* always_inline functions are inlined aven at -O0. */
1032 DECL_ATTRIBUTES (edge
->callee
->decl
))
1033 /* Multiversioned functions needs the dispatcher to
1034 be produced locally even for extern functions. */
1035 || edge
->callee
->function_version ()))
1036 enqueue_node (edge
->callee
);
1037 if (opt_for_fn (cnode
->decl
, optimize
)
1038 && opt_for_fn (cnode
->decl
, flag_devirtualize
))
1042 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1044 next
= edge
->next_callee
;
1045 if (edge
->indirect_info
->polymorphic
)
1046 walk_polymorphic_call_targets (&reachable_call_targets
,
1051 /* If decl is a clone of an abstract function,
1052 mark that abstract function so that we don't release its body.
1053 The DECL_INITIAL() of that abstract function declaration
1054 will be later needed to output debug info. */
1055 if (DECL_ABSTRACT_ORIGIN (decl
))
1057 cgraph_node
*origin_node
1058 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1059 origin_node
->used_as_abstract_origin
= true;
1064 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1065 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1069 if (node
->same_comdat_group
)
1072 for (next
= node
->same_comdat_group
;
1074 next
= next
->same_comdat_group
)
1075 if (!next
->comdat_local_p ())
1076 enqueue_node (next
);
1078 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1079 if (ref
->referred
->definition
1080 && (!DECL_EXTERNAL (ref
->referred
->decl
)
1081 || ((TREE_CODE (ref
->referred
->decl
) != FUNCTION_DECL
1083 || (TREE_CODE (ref
->referred
->decl
) == FUNCTION_DECL
1084 && opt_for_fn (ref
->referred
->decl
, optimize
))
1086 || ref
->referred
->alias
)))
1087 enqueue_node (ref
->referred
);
1088 symtab
->process_new_functions ();
1091 update_type_inheritance_graph ();
1093 /* Collect entry points to the unit. */
1094 if (symtab
->dump_file
)
1096 fprintf (symtab
->dump_file
, "\n\nInitial ");
1097 symtab_node::dump_table (symtab
->dump_file
);
1100 if (symtab
->dump_file
)
1101 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1103 for (node
= symtab
->first_symbol ();
1104 node
!= first_handled
1105 && node
!= first_handled_var
; node
= next
)
1108 if (!node
->aux
&& !node
->referred_to_p ())
1110 if (symtab
->dump_file
)
1111 fprintf (symtab
->dump_file
, " %s", node
->name ());
1115 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1117 tree decl
= node
->decl
;
1119 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1121 && !cnode
->thunk
.thunk_p
)
1124 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1126 || gimple_has_body_p (decl
));
1127 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1131 for (;node
; node
= node
->next
)
1133 first_analyzed
= symtab
->first_function ();
1134 first_analyzed_var
= symtab
->first_variable ();
1135 if (symtab
->dump_file
)
1137 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1138 symtab_node::dump_table (symtab
->dump_file
);
1140 bitmap_obstack_release (NULL
);
1142 /* Initialize assembler name hash, in particular we want to trigger C++
1143 mangling and same body alias creation before we free DECL_ARGUMENTS
1146 symtab
->symtab_initialize_asm_name_hash ();
1148 input_location
= saved_loc
;
1151 /* Translate the ugly representation of aliases as alias pairs into nice
1152 representation in callgraph. We don't handle all cases yet,
1156 handle_alias_pairs (void)
1161 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1163 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1165 /* Weakrefs with target not defined in current unit are easy to handle:
1166 they behave just as external variables except we need to note the
1167 alias flag to later output the weakref pseudo op into asm file. */
1169 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1171 symtab_node
*node
= symtab_node::get (p
->decl
);
1174 node
->alias_target
= p
->target
;
1175 node
->weakref
= true;
1178 alias_pairs
->unordered_remove (i
);
1181 else if (!target_node
)
1183 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1184 symtab_node
*node
= symtab_node::get (p
->decl
);
1186 node
->alias
= false;
1187 alias_pairs
->unordered_remove (i
);
1191 if (DECL_EXTERNAL (target_node
->decl
)
1192 /* We use local aliases for C++ thunks to force the tailcall
1193 to bind locally. This is a hack - to keep it working do
1194 the following (which is not strictly correct). */
1195 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1196 || ! DECL_VIRTUAL_P (target_node
->decl
))
1197 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1199 error ("%q+D aliased to external symbol %qE",
1200 p
->decl
, p
->target
);
1203 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1204 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1206 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1207 if (src_node
&& src_node
->definition
)
1209 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1210 alias_pairs
->unordered_remove (i
);
1212 else if (TREE_CODE (p
->decl
) == VAR_DECL
1213 && target_node
&& is_a
<varpool_node
*> (target_node
))
1215 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1216 alias_pairs
->unordered_remove (i
);
1220 error ("%q+D alias in between function and variable is not supported",
1222 warning (0, "%q+D aliased declaration",
1224 alias_pairs
->unordered_remove (i
);
1227 vec_free (alias_pairs
);
1231 /* Figure out what functions we want to assemble. */
1234 mark_functions_to_output (void)
1237 #ifdef ENABLE_CHECKING
1238 bool check_same_comdat_groups
= false;
1240 FOR_EACH_FUNCTION (node
)
1241 gcc_assert (!node
->process
);
1244 FOR_EACH_FUNCTION (node
)
1246 tree decl
= node
->decl
;
1248 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1252 /* We need to output all local functions that are used and not
1253 always inlined, as well as those that are reachable from
1254 outside the current compilation unit. */
1256 && !node
->thunk
.thunk_p
1258 && !node
->global
.inlined_to
1259 && !TREE_ASM_WRITTEN (decl
)
1260 && !DECL_EXTERNAL (decl
))
1263 if (node
->same_comdat_group
)
1266 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1268 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1269 if (!next
->thunk
.thunk_p
&& !next
->alias
1270 && !next
->comdat_local_p ())
1274 else if (node
->same_comdat_group
)
1276 #ifdef ENABLE_CHECKING
1277 check_same_comdat_groups
= true;
1282 /* We should've reclaimed all functions that are not needed. */
1283 #ifdef ENABLE_CHECKING
1284 if (!node
->global
.inlined_to
1285 && gimple_has_body_p (decl
)
1286 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1287 are inside partition, we can end up not removing the body since we no longer
1288 have analyzed node pointing to it. */
1289 && !node
->in_other_partition
1292 && !DECL_EXTERNAL (decl
))
1295 internal_error ("failed to reclaim unneeded function");
1298 gcc_assert (node
->global
.inlined_to
1299 || !gimple_has_body_p (decl
)
1300 || node
->in_other_partition
1302 || DECL_ARTIFICIAL (decl
)
1303 || DECL_EXTERNAL (decl
));
1308 #ifdef ENABLE_CHECKING
1309 if (check_same_comdat_groups
)
1310 FOR_EACH_FUNCTION (node
)
1311 if (node
->same_comdat_group
&& !node
->process
)
1313 tree decl
= node
->decl
;
1314 if (!node
->global
.inlined_to
1315 && gimple_has_body_p (decl
)
1316 /* FIXME: in an ltrans unit when the offline copy is outside a
1317 partition but inline copies are inside a partition, we can
1318 end up not removing the body since we no longer have an
1319 analyzed node pointing to it. */
1320 && !node
->in_other_partition
1322 && !DECL_EXTERNAL (decl
))
1325 internal_error ("failed to reclaim unneeded function in same "
1332 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1333 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1335 Set current_function_decl and cfun to newly constructed empty function body.
1336 return basic block in the function body. */
1339 init_lowered_empty_function (tree decl
, bool in_ssa
, gcov_type count
)
1344 current_function_decl
= decl
;
1345 allocate_struct_function (decl
, false);
1346 gimple_register_cfg_hooks ();
1347 init_empty_tree_cfg ();
1351 init_tree_ssa (cfun
);
1352 init_ssa_operands (cfun
);
1353 cfun
->gimple_df
->in_ssa_p
= true;
1354 cfun
->curr_properties
|= PROP_ssa
;
1357 DECL_INITIAL (decl
) = make_node (BLOCK
);
1359 DECL_SAVED_TREE (decl
) = error_mark_node
;
1360 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1361 | PROP_cfg
| PROP_loops
);
1363 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1364 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1365 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1367 /* Create BB for body of the function and connect it properly. */
1368 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1369 ENTRY_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1370 EXIT_BLOCK_PTR_FOR_FN (cfun
)->count
= count
;
1371 EXIT_BLOCK_PTR_FOR_FN (cfun
)->frequency
= REG_BR_PROB_BASE
;
1372 bb
= create_basic_block (NULL
, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1374 bb
->frequency
= BB_FREQ_MAX
;
1375 e
= make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1377 e
->probability
= REG_BR_PROB_BASE
;
1378 e
= make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1380 e
->probability
= REG_BR_PROB_BASE
;
1381 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1386 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1387 offset indicated by VIRTUAL_OFFSET, if that is
1388 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1389 zero for a result adjusting thunk. */
1392 thunk_adjust (gimple_stmt_iterator
* bsi
,
1393 tree ptr
, bool this_adjusting
,
1394 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1400 && fixed_offset
!= 0)
1402 stmt
= gimple_build_assign
1403 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1406 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1409 /* If there's a virtual offset, look up that value in the vtable and
1410 adjust the pointer again. */
1417 if (!vtable_entry_type
)
1419 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1420 TREE_TYPE (vfunc_type
) = integer_type_node
;
1421 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1422 layout_type (vfunc_type
);
1424 vtable_entry_type
= build_pointer_type (vfunc_type
);
1428 create_tmp_reg (build_pointer_type
1429 (build_pointer_type (vtable_entry_type
)), "vptr");
1431 /* The vptr is always at offset zero in the object. */
1432 stmt
= gimple_build_assign (vtabletmp
,
1433 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1435 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1437 /* Form the vtable address. */
1438 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1440 stmt
= gimple_build_assign (vtabletmp2
,
1441 build_simple_mem_ref (vtabletmp
));
1442 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1444 /* Find the entry with the vcall offset. */
1445 stmt
= gimple_build_assign (vtabletmp2
,
1446 fold_build_pointer_plus_loc (input_location
,
1449 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1451 /* Get the offset itself. */
1452 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1454 stmt
= gimple_build_assign (vtabletmp3
,
1455 build_simple_mem_ref (vtabletmp2
));
1456 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1458 /* Adjust the `this' pointer. */
1459 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1460 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1461 GSI_CONTINUE_LINKING
);
1465 && fixed_offset
!= 0)
1466 /* Adjust the pointer by the constant. */
1470 if (TREE_CODE (ptr
) == VAR_DECL
)
1474 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1475 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1476 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1478 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1479 ptrtmp
, fixed_offset
);
1482 /* Emit the statement and gimplify the adjustment expression. */
1483 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1484 stmt
= gimple_build_assign (ret
, ptr
);
1485 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1490 /* Expand thunk NODE to gimple if possible.
1491 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1492 no assembler is produced.
1493 When OUTPUT_ASM_THUNK is true, also produce assembler for
1494 thunks that are not lowered. */
1497 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1499 bool this_adjusting
= thunk
.this_adjusting
;
1500 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1501 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1502 tree virtual_offset
= NULL
;
1503 tree alias
= callees
->callee
->decl
;
1504 tree thunk_fndecl
= decl
;
1508 if (!force_gimple_thunk
&& this_adjusting
1509 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1510 virtual_value
, alias
))
1514 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1516 if (!output_asm_thunks
)
1523 get_untransformed_body ();
1524 a
= DECL_ARGUMENTS (thunk_fndecl
);
1526 current_function_decl
= thunk_fndecl
;
1528 /* Ensure thunks are emitted in their correct sections. */
1529 resolve_unique_section (thunk_fndecl
, 0,
1530 flag_function_sections
);
1532 DECL_RESULT (thunk_fndecl
)
1533 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1534 RESULT_DECL
, 0, restype
);
1535 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1536 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1538 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1540 fn_block
= make_node (BLOCK
);
1541 BLOCK_VARS (fn_block
) = a
;
1542 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1543 init_function_start (thunk_fndecl
);
1545 insn_locations_init ();
1546 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1547 prologue_location
= curr_insn_location ();
1548 assemble_start_function (thunk_fndecl
, fnname
);
1550 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1551 fixed_offset
, virtual_value
, alias
);
1553 assemble_end_function (thunk_fndecl
, fnname
);
1554 insn_locations_finalize ();
1555 init_insn_lengths ();
1556 free_after_compilation (cfun
);
1558 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1559 thunk
.thunk_p
= false;
1565 basic_block bb
, then_bb
, else_bb
, return_bb
;
1566 gimple_stmt_iterator bsi
;
1577 get_untransformed_body ();
1578 a
= DECL_ARGUMENTS (thunk_fndecl
);
1580 current_function_decl
= thunk_fndecl
;
1582 /* Ensure thunks are emitted in their correct sections. */
1583 resolve_unique_section (thunk_fndecl
, 0,
1584 flag_function_sections
);
1586 DECL_IGNORED_P (thunk_fndecl
) = 1;
1587 bitmap_obstack_initialize (NULL
);
1589 if (thunk
.virtual_offset_p
)
1590 virtual_offset
= size_int (virtual_value
);
1592 /* Build the return declaration for the function. */
1593 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1594 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1596 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1597 DECL_ARTIFICIAL (resdecl
) = 1;
1598 DECL_IGNORED_P (resdecl
) = 1;
1599 DECL_RESULT (thunk_fndecl
) = resdecl
;
1600 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1603 resdecl
= DECL_RESULT (thunk_fndecl
);
1605 bb
= then_bb
= else_bb
= return_bb
1606 = init_lowered_empty_function (thunk_fndecl
, true, count
);
1608 bsi
= gsi_start_bb (bb
);
1610 /* Build call to the function being thunked. */
1611 if (!VOID_TYPE_P (restype
))
1613 if (DECL_BY_REFERENCE (resdecl
))
1615 restmp
= gimple_fold_indirect_ref (resdecl
);
1617 restmp
= build2 (MEM_REF
,
1618 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias
))),
1620 build_int_cst (TREE_TYPE
1621 (DECL_RESULT (alias
)), 0));
1623 else if (!is_gimple_reg_type (restype
))
1625 if (aggregate_value_p (resdecl
, TREE_TYPE (thunk_fndecl
)))
1629 if (TREE_CODE (restmp
) == VAR_DECL
)
1630 add_local_decl (cfun
, restmp
);
1631 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1634 restmp
= create_tmp_var (restype
, "retval");
1637 restmp
= create_tmp_reg (restype
, "retval");
1640 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1642 auto_vec
<tree
> vargs (nargs
);
1647 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1649 arg
= DECL_CHAIN (a
);
1654 for (; i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1657 if (!is_gimple_val (arg
))
1659 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1660 (TREE_TYPE (arg
)), "arg");
1661 gimple stmt
= gimple_build_assign (tmp
, arg
);
1662 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1664 vargs
.quick_push (tmp
);
1666 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1667 callees
->call_stmt
= call
;
1668 gimple_call_set_from_thunk (call
, true);
1669 gimple_call_set_with_bounds (call
, instrumentation_clone
);
1672 gimple_call_set_lhs (call
, restmp
);
1673 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1674 TREE_TYPE (TREE_TYPE (alias
))));
1676 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1677 if (!(gimple_call_flags (call
) & ECF_NORETURN
))
1679 if (restmp
&& !this_adjusting
1680 && (fixed_offset
|| virtual_offset
))
1682 tree true_label
= NULL_TREE
;
1684 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1688 /* If the return type is a pointer, we need to
1689 protect against NULL. We know there will be an
1690 adjustment, because that's why we're emitting a
1692 then_bb
= create_basic_block (NULL
, (void *) 0, bb
);
1693 then_bb
->count
= count
- count
/ 16;
1694 then_bb
->frequency
= BB_FREQ_MAX
- BB_FREQ_MAX
/ 16;
1695 return_bb
= create_basic_block (NULL
, (void *) 0, then_bb
);
1696 return_bb
->count
= count
;
1697 return_bb
->frequency
= BB_FREQ_MAX
;
1698 else_bb
= create_basic_block (NULL
, (void *) 0, else_bb
);
1699 then_bb
->count
= count
/ 16;
1700 then_bb
->frequency
= BB_FREQ_MAX
/ 16;
1701 add_bb_to_loop (then_bb
, bb
->loop_father
);
1702 add_bb_to_loop (return_bb
, bb
->loop_father
);
1703 add_bb_to_loop (else_bb
, bb
->loop_father
);
1704 remove_edge (single_succ_edge (bb
));
1705 true_label
= gimple_block_label (then_bb
);
1706 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1707 build_zero_cst (TREE_TYPE (restmp
)),
1708 NULL_TREE
, NULL_TREE
);
1709 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1710 e
= make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1711 e
->probability
= REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ 16;
1712 e
->count
= count
- count
/ 16;
1713 e
= make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1714 e
->probability
= REG_BR_PROB_BASE
/ 16;
1715 e
->count
= count
/ 16;
1716 e
= make_edge (return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1717 e
->probability
= REG_BR_PROB_BASE
;
1719 e
= make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1720 e
->probability
= REG_BR_PROB_BASE
;
1721 e
->count
= count
- count
/ 16;
1722 e
= make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1723 e
->probability
= REG_BR_PROB_BASE
;
1724 e
->count
= count
/ 16;
1725 bsi
= gsi_last_bb (then_bb
);
1728 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1729 fixed_offset
, virtual_offset
);
1733 bsi
= gsi_last_bb (else_bb
);
1734 stmt
= gimple_build_assign (restmp
,
1735 build_zero_cst (TREE_TYPE (restmp
)));
1736 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1737 bsi
= gsi_last_bb (return_bb
);
1741 gimple_call_set_tail (call
, true);
1743 /* Build return value. */
1744 if (!DECL_BY_REFERENCE (resdecl
))
1745 ret
= gimple_build_return (restmp
);
1747 ret
= gimple_build_return (resdecl
);
1749 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1753 gimple_call_set_tail (call
, true);
1754 remove_edge (single_succ_edge (bb
));
1757 cfun
->gimple_df
->in_ssa_p
= true;
1758 profile_status_for_fn (cfun
)
1759 = count
? PROFILE_READ
: PROFILE_GUESSED
;
1760 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1761 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
1762 delete_unreachable_blocks ();
1763 update_ssa (TODO_update_ssa
);
1764 #ifdef ENABLE_CHECKING
1765 verify_flow_info ();
1767 free_dominance_info (CDI_DOMINATORS
);
1769 /* Since we want to emit the thunk, we explicitly mark its name as
1771 thunk
.thunk_p
= false;
1773 bitmap_obstack_release (NULL
);
1775 current_function_decl
= NULL
;
1780 /* Assemble thunks and aliases associated to node. */
1783 cgraph_node::assemble_thunks_and_aliases (void)
1788 for (e
= callers
; e
;)
1789 if (e
->caller
->thunk
.thunk_p
1790 && !e
->caller
->thunk
.add_pointer_bounds_args
)
1792 cgraph_node
*thunk
= e
->caller
;
1795 thunk
->expand_thunk (true, false);
1796 thunk
->assemble_thunks_and_aliases ();
1801 FOR_EACH_ALIAS (this, ref
)
1803 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1804 bool saved_written
= TREE_ASM_WRITTEN (decl
);
1806 /* Force assemble_alias to really output the alias this time instead
1807 of buffering it in same alias pairs. */
1808 TREE_ASM_WRITTEN (decl
) = 1;
1809 do_assemble_alias (alias
->decl
,
1810 DECL_ASSEMBLER_NAME (decl
));
1811 alias
->assemble_thunks_and_aliases ();
1812 TREE_ASM_WRITTEN (decl
) = saved_written
;
1816 /* Expand function specified by node. */
1819 cgraph_node::expand (void)
1821 location_t saved_loc
;
1823 /* We ought to not compile any inline clones. */
1824 gcc_assert (!global
.inlined_to
);
1826 announce_function (decl
);
1828 gcc_assert (lowered
);
1829 get_untransformed_body ();
1831 /* Generate RTL for the body of DECL. */
1833 timevar_push (TV_REST_OF_COMPILATION
);
1835 gcc_assert (symtab
->global_info_ready
);
1837 /* Initialize the default bitmap obstack. */
1838 bitmap_obstack_initialize (NULL
);
1840 /* Initialize the RTL code for the function. */
1841 current_function_decl
= decl
;
1842 saved_loc
= input_location
;
1843 input_location
= DECL_SOURCE_LOCATION (decl
);
1844 init_function_start (decl
);
1846 gimple_register_cfg_hooks ();
1848 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1850 execute_all_ipa_transforms ();
1852 /* Perform all tree transforms and optimizations. */
1854 /* Signal the start of passes. */
1855 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1857 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
1859 /* Signal the end of passes. */
1860 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1862 bitmap_obstack_release (®_obstack
);
1864 /* Release the default bitmap obstack. */
1865 bitmap_obstack_release (NULL
);
1867 /* If requested, warn about function definitions where the function will
1868 return a value (usually of some struct or union type) which itself will
1869 take up a lot of stack space. */
1870 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1872 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1874 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1875 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1876 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1879 unsigned int size_as_int
1880 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1882 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1883 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
1886 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
1887 decl
, larger_than_size
);
1891 gimple_set_body (decl
, NULL
);
1892 if (DECL_STRUCT_FUNCTION (decl
) == 0
1893 && !cgraph_node::get (decl
)->origin
)
1895 /* Stop pointing to the local nodes about to be freed.
1896 But DECL_INITIAL must remain nonzero so we know this
1897 was an actual function definition.
1898 For a nested function, this is done in c_pop_function_context.
1899 If rest_of_compilation set this to 0, leave it 0. */
1900 if (DECL_INITIAL (decl
) != 0)
1901 DECL_INITIAL (decl
) = error_mark_node
;
1904 input_location
= saved_loc
;
1907 timevar_pop (TV_REST_OF_COMPILATION
);
1909 /* Make sure that BE didn't give up on compiling. */
1910 gcc_assert (TREE_ASM_WRITTEN (decl
));
1912 current_function_decl
= NULL
;
1914 /* It would make a lot more sense to output thunks before function body to get more
1915 forward and lest backwarding jumps. This however would need solving problem
1916 with comdats. See PR48668. Also aliases must come after function itself to
1917 make one pass assemblers, like one on AIX, happy. See PR 50689.
1918 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1920 assemble_thunks_and_aliases ();
1922 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1923 points to the dead function body. */
1925 remove_all_references ();
1928 /* Node comparer that is responsible for the order that corresponds
1929 to time when a function was launched for the first time. */
1932 node_cmp (const void *pa
, const void *pb
)
1934 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
1935 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
1937 /* Functions with time profile must be before these without profile. */
1938 if (!a
->tp_first_run
|| !b
->tp_first_run
)
1939 return a
->tp_first_run
- b
->tp_first_run
;
1941 return a
->tp_first_run
!= b
->tp_first_run
1942 ? b
->tp_first_run
- a
->tp_first_run
1943 : b
->order
- a
->order
;
1946 /* Expand all functions that must be output.
1948 Attempt to topologically sort the nodes so function is output when
1949 all called functions are already assembled to allow data to be
1950 propagated across the callgraph. Use a stack to get smaller distance
1951 between a function and its callees (later we may choose to use a more
1952 sophisticated algorithm for function reordering; we will likely want
1953 to use subsections to make the output functions appear in top-down
1957 expand_all_functions (void)
1960 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
1961 symtab
->cgraph_count
);
1962 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
1963 int order_pos
, new_order_pos
= 0;
1966 order_pos
= ipa_reverse_postorder (order
);
1967 gcc_assert (order_pos
== symtab
->cgraph_count
);
1969 /* Garbage collector may remove inline clones we eliminate during
1970 optimization. So we must be sure to not reference them. */
1971 for (i
= 0; i
< order_pos
; i
++)
1972 if (order
[i
]->process
)
1973 order
[new_order_pos
++] = order
[i
];
1975 if (flag_profile_reorder_functions
)
1976 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
1978 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1984 expanded_func_count
++;
1985 if(node
->tp_first_run
)
1986 profiled_func_count
++;
1988 if (symtab
->dump_file
)
1989 fprintf (symtab
->dump_file
,
1990 "Time profile order in expand_all_functions:%s:%d\n",
1991 node
->asm_name (), node
->tp_first_run
);
1998 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
1999 main_input_filename
, profiled_func_count
, expanded_func_count
);
2001 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
2002 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
2003 profiled_func_count
, expanded_func_count
);
2005 symtab
->process_new_functions ();
2006 free_gimplify_stack ();
2011 /* This is used to sort the node types by the cgraph order number. */
2013 enum cgraph_order_sort_kind
2015 ORDER_UNDEFINED
= 0,
2021 struct cgraph_order_sort
2023 enum cgraph_order_sort_kind kind
;
2032 /* Output all functions, variables, and asm statements in the order
2033 according to their order fields, which is the order in which they
2034 appeared in the file. This implements -fno-toplevel-reorder. In
2035 this mode we may output functions and variables which don't really
2037 When NO_REORDER is true only do this for symbols marked no reorder. */
2040 output_in_order (bool no_reorder
)
2043 cgraph_order_sort
*nodes
;
2048 max
= symtab
->order
;
2049 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
2051 FOR_EACH_DEFINED_FUNCTION (pf
)
2053 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
2055 if (no_reorder
&& !pf
->no_reorder
)
2058 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2059 nodes
[i
].kind
= ORDER_FUNCTION
;
2064 FOR_EACH_DEFINED_VARIABLE (pv
)
2065 if (!DECL_EXTERNAL (pv
->decl
))
2067 if (no_reorder
&& !pv
->no_reorder
)
2070 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2071 nodes
[i
].kind
= ORDER_VAR
;
2075 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
2078 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
2079 nodes
[i
].kind
= ORDER_ASM
;
2083 /* In toplevel reorder mode we output all statics; mark them as needed. */
2085 for (i
= 0; i
< max
; ++i
)
2086 if (nodes
[i
].kind
== ORDER_VAR
)
2087 nodes
[i
].u
.v
->finalize_named_section_flags ();
2089 for (i
= 0; i
< max
; ++i
)
2091 switch (nodes
[i
].kind
)
2093 case ORDER_FUNCTION
:
2094 nodes
[i
].u
.f
->process
= 0;
2095 nodes
[i
].u
.f
->expand ();
2099 nodes
[i
].u
.v
->assemble_decl ();
2103 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2106 case ORDER_UNDEFINED
:
2114 symtab
->clear_asm_symbols ();
2122 gcc::pass_manager
*passes
= g
->get_passes ();
2125 current_function_decl
= NULL
;
2126 gimple_register_cfg_hooks ();
2127 bitmap_obstack_initialize (NULL
);
2129 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2133 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2138 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2139 devirtualization and other changes where removal iterate. */
2140 symtab
->remove_unreachable_nodes (symtab
->dump_file
);
2142 /* If pass_all_early_optimizations was not scheduled, the state of
2143 the cgraph will not be properly updated. Update it now. */
2144 if (symtab
->state
< IPA_SSA
)
2145 symtab
->state
= IPA_SSA
;
2149 /* Generate coverage variables and constructors. */
2152 /* Process new functions added. */
2154 current_function_decl
= NULL
;
2155 symtab
->process_new_functions ();
2157 execute_ipa_summary_passes
2158 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2161 /* Some targets need to handle LTO assembler output specially. */
2162 if (flag_generate_lto
|| flag_generate_offload
)
2163 targetm
.asm_out
.lto_start ();
2167 if (g
->have_offload
)
2169 section_name_prefix
= OFFLOAD_SECTION_NAME_PREFIX
;
2170 lto_stream_offload_p
= true;
2171 ipa_write_summaries ();
2172 lto_stream_offload_p
= false;
2176 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2177 lto_stream_offload_p
= false;
2178 ipa_write_summaries ();
2182 if (flag_generate_lto
|| flag_generate_offload
)
2183 targetm
.asm_out
.lto_end ();
2185 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2186 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2187 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2189 bitmap_obstack_release (NULL
);
2193 /* Return string alias is alias of. */
2196 get_alias_symbol (tree decl
)
2198 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2199 return get_identifier (TREE_STRING_POINTER
2200 (TREE_VALUE (TREE_VALUE (alias
))));
2204 /* Weakrefs may be associated to external decls and thus not output
2205 at expansion time. Emit all necessary aliases. */
2208 symbol_table::output_weakrefs (void)
2212 FOR_EACH_SYMBOL (node
)
2214 && !TREE_ASM_WRITTEN (node
->decl
)
2215 && (!(cnode
= dyn_cast
<cgraph_node
*> (node
))
2216 || !cnode
->instrumented_version
2217 || !TREE_ASM_WRITTEN (cnode
->instrumented_version
->decl
))
2222 /* Weakrefs are special by not requiring target definition in current
2223 compilation unit. It is thus bit hard to work out what we want to
2225 When alias target is defined, we need to fetch it from symtab reference,
2226 otherwise it is pointed to by alias_target. */
2227 if (node
->alias_target
)
2228 target
= (DECL_P (node
->alias_target
)
2229 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2230 : node
->alias_target
);
2231 else if (node
->analyzed
)
2232 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2236 target
= get_alias_symbol (node
->decl
);
2238 do_assemble_alias (node
->decl
, target
);
2242 /* Perform simple optimizations based on callgraph. */
2245 symbol_table::compile (void)
2250 #ifdef ENABLE_CHECKING
2251 symtab_node::verify_symtab_nodes ();
2254 timevar_push (TV_CGRAPHOPT
);
2255 if (pre_ipa_mem_report
)
2257 fprintf (stderr
, "Memory consumption before IPA\n");
2258 dump_memory_report (false);
2261 fprintf (stderr
, "Performing interprocedural optimizations\n");
2264 /* Offloading requires LTO infrastructure. */
2265 if (!in_lto_p
&& g
->have_offload
)
2266 flag_generate_offload
= 1;
2268 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2269 if (flag_generate_lto
|| flag_generate_offload
)
2270 lto_streamer_hooks_init ();
2272 /* Don't run the IPA passes if there was any error or sorry messages. */
2276 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2278 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2280 timevar_pop (TV_CGRAPHOPT
);
2284 global_info_ready
= true;
2287 fprintf (dump_file
, "Optimized ");
2288 symtab_node:: dump_table (dump_file
);
2290 if (post_ipa_mem_report
)
2292 fprintf (stderr
, "Memory consumption after IPA\n");
2293 dump_memory_report (false);
2295 timevar_pop (TV_CGRAPHOPT
);
2297 /* Output everything. */
2298 (*debug_hooks
->assembly_start
) ();
2300 fprintf (stderr
, "Assembling functions:\n");
2301 #ifdef ENABLE_CHECKING
2302 symtab_node::verify_symtab_nodes ();
2305 materialize_all_clones ();
2306 bitmap_obstack_initialize (NULL
);
2307 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2308 bitmap_obstack_release (NULL
);
2309 mark_functions_to_output ();
2311 /* When weakref support is missing, we autmatically translate all
2312 references to NODE to references to its ultimate alias target.
2313 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2316 Set up this mapping before we output any assembler but once we are sure
2317 that all symbol renaming is done.
2319 FIXME: All this uglyness can go away if we just do renaming at gimple
2320 level by physically rewritting the IL. At the moment we can only redirect
2321 calls, so we need infrastructure for renaming references as well. */
2322 #ifndef ASM_OUTPUT_WEAKREF
2325 FOR_EACH_SYMBOL (node
)
2327 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2329 IDENTIFIER_TRANSPARENT_ALIAS
2330 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2331 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2332 = (node
->alias_target
? node
->alias_target
2333 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2339 if (!flag_toplevel_reorder
)
2340 output_in_order (false);
2343 /* Output first asm statements and anything ordered. The process
2344 flag is cleared for these nodes, so we skip them later. */
2345 output_in_order (true);
2346 expand_all_functions ();
2347 output_variables ();
2350 process_new_functions ();
2356 fprintf (dump_file
, "\nFinal ");
2357 symtab_node::dump_table (dump_file
);
2359 #ifdef ENABLE_CHECKING
2360 symtab_node::verify_symtab_nodes ();
2361 /* Double check that all inline clones are gone and that all
2362 function bodies have been released from memory. */
2366 bool error_found
= false;
2368 FOR_EACH_DEFINED_FUNCTION (node
)
2369 if (node
->global
.inlined_to
2370 || gimple_has_body_p (node
->decl
))
2376 internal_error ("nodes with unreleased memory found");
2382 /* Analyze the whole compilation unit once it is parsed completely. */
2385 symbol_table::finalize_compilation_unit (void)
2387 timevar_push (TV_CGRAPH
);
2389 /* If we're here there's no current function anymore. Some frontends
2390 are lazy in clearing these. */
2391 current_function_decl
= NULL
;
2394 /* Do not skip analyzing the functions if there were errors, we
2395 miss diagnostics for following functions otherwise. */
2397 /* Emit size functions we didn't inline. */
2398 finalize_size_functions ();
2400 /* Mark alias targets necessary and emit diagnostics. */
2401 handle_alias_pairs ();
2405 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2409 if (flag_dump_passes
)
2412 /* Gimplify and lower all functions, compute reachability and
2413 remove unreachable nodes. */
2414 analyze_functions ();
2416 /* Mark alias targets necessary and emit diagnostics. */
2417 handle_alias_pairs ();
2419 /* Gimplify and lower thunks. */
2420 analyze_functions ();
2422 /* Finally drive the pass manager. */
2425 timevar_pop (TV_CGRAPH
);
2428 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2429 within the same process. For use by toplev::finalize. */
2432 cgraphunit_c_finalize (void)
2434 gcc_assert (cgraph_new_nodes
.length () == 0);
2435 cgraph_new_nodes
.truncate (0);
2437 vtable_entry_type
= NULL
;
2438 queued_nodes
= &symtab_terminator
;
2440 first_analyzed
= NULL
;
2441 first_analyzed_var
= NULL
;
2444 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2445 kind of wrapper method. */
2448 cgraph_node::create_wrapper (cgraph_node
*target
)
2450 /* Preserve DECL_RESULT so we get right by reference flag. */
2451 tree decl_result
= DECL_RESULT (decl
);
2453 /* Remove the function's body but keep arguments to be reused
2455 release_body (true);
2458 DECL_RESULT (decl
) = decl_result
;
2459 DECL_INITIAL (decl
) = NULL
;
2460 allocate_struct_function (decl
, false);
2463 /* Turn alias into thunk and expand it into GIMPLE representation. */
2465 thunk
.thunk_p
= true;
2466 thunk
.this_adjusting
= false;
2467 create_edge (target
, NULL
, count
, CGRAPH_FREQ_BASE
);
2469 tree arguments
= DECL_ARGUMENTS (decl
);
2473 TREE_ADDRESSABLE (arguments
) = false;
2474 arguments
= TREE_CHAIN (arguments
);
2477 expand_thunk (false, true);
2479 /* Inline summary set-up. */
2481 inline_analyze_function (this);
2484 #include "gt-cgraphunit.h"