1 /* Driver of optimization process
2 Copyright (C) 2003-2014 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"
166 #include "stor-layout.h"
167 #include "stringpool.h"
173 #include "hash-set.h"
174 #include "machmode.h"
175 #include "hard-reg-set.h"
177 #include "function.h"
178 #include "basic-block.h"
179 #include "tree-ssa-alias.h"
180 #include "internal-fn.h"
181 #include "gimple-fold.h"
182 #include "gimple-expr.h"
185 #include "gimplify.h"
186 #include "gimple-iterator.h"
187 #include "gimplify-me.h"
188 #include "gimple-ssa.h"
189 #include "tree-cfg.h"
190 #include "tree-into-ssa.h"
191 #include "tree-ssa.h"
192 #include "tree-inline.h"
193 #include "langhooks.h"
198 #include "diagnostic.h"
202 #include "hash-map.h"
203 #include "plugin-api.h"
206 #include "alloc-pool.h"
207 #include "ipa-prop.h"
208 #include "tree-iterator.h"
209 #include "tree-pass.h"
210 #include "tree-dump.h"
211 #include "gimple-pretty-print.h"
213 #include "coverage.h"
215 #include "ipa-inline.h"
216 #include "ipa-utils.h"
217 #include "lto-streamer.h"
220 #include "regset.h" /* FIXME: For reg_obstack. */
222 #include "pass_manager.h"
223 #include "tree-nested.h"
224 #include "gimplify.h"
227 #include "lto-section-names.h"
229 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
230 secondary queue used during optimization to accommodate passes that
231 may generate new functions that need to be optimized and expanded. */
232 vec
<cgraph_node
*> cgraph_new_nodes
;
234 static void expand_all_functions (void);
235 static void mark_functions_to_output (void);
236 static void handle_alias_pairs (void);
238 /* Used for vtable lookup in thunk adjusting. */
239 static GTY (()) tree vtable_entry_type
;
241 /* Determine if symbol declaration is needed. That is, visible to something
242 either outside this translation unit, something magic in the system
245 symtab_node::needed_p (void)
247 /* Double check that no one output the function into assembly file
249 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl
)
250 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)));
255 if (DECL_EXTERNAL (decl
))
258 /* If the user told us it is used, then it must be so. */
262 /* ABI forced symbols are needed when they are external. */
263 if (forced_by_abi
&& TREE_PUBLIC (decl
))
266 /* Keep constructors, destructors and virtual functions. */
267 if (TREE_CODE (decl
) == FUNCTION_DECL
268 && (DECL_STATIC_CONSTRUCTOR (decl
) || DECL_STATIC_DESTRUCTOR (decl
)))
271 /* Externally visible variables must be output. The exception is
272 COMDAT variables that must be output only when they are needed. */
273 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
))
279 /* Head and terminator of the queue of nodes to be processed while building
282 static symtab_node symtab_terminator
;
283 static symtab_node
*queued_nodes
= &symtab_terminator
;
285 /* Add NODE to queue starting at QUEUED_NODES.
286 The queue is linked via AUX pointers and terminated by pointer to 1. */
289 enqueue_node (symtab_node
*node
)
293 gcc_checking_assert (queued_nodes
);
294 node
->aux
= queued_nodes
;
298 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
299 functions into callgraph in a way so they look like ordinary reachable
300 functions inserted into callgraph already at construction time. */
303 symbol_table::process_new_functions (void)
307 if (!cgraph_new_nodes
.exists ())
310 handle_alias_pairs ();
311 /* Note that this queue may grow as its being processed, as the new
312 functions may generate new ones. */
313 for (unsigned i
= 0; i
< cgraph_new_nodes
.length (); i
++)
315 cgraph_node
*node
= cgraph_new_nodes
[i
];
320 /* At construction time we just need to finalize function and move
321 it into reachable functions list. */
323 cgraph_node::finalize_function (fndecl
, false);
324 call_cgraph_insertion_hooks (node
);
330 /* When IPA optimization already started, do all essential
331 transformations that has been already performed on the whole
332 cgraph but not on this function. */
334 gimple_register_cfg_hooks ();
337 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
339 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
340 g
->get_passes ()->execute_early_local_passes ();
341 else if (inline_summary_vec
!= NULL
)
342 compute_inline_parameters (node
, true);
343 free_dominance_info (CDI_POST_DOMINATORS
);
344 free_dominance_info (CDI_DOMINATORS
);
346 call_cgraph_insertion_hooks (node
);
350 /* Functions created during expansion shall be compiled
353 call_cgraph_insertion_hooks (node
);
363 cgraph_new_nodes
.release ();
366 /* As an GCC extension we allow redefinition of the function. The
367 semantics when both copies of bodies differ is not well defined.
368 We replace the old body with new body so in unit at a time mode
369 we always use new body, while in normal mode we may end up with
370 old body inlined into some functions and new body expanded and
373 ??? It may make more sense to use one body for inlining and other
374 body for expanding the function but this is difficult to do. */
377 cgraph_node::reset (void)
379 /* If process is set, then we have already begun whole-unit analysis.
380 This is *not* testing for whether we've already emitted the function.
381 That case can be sort-of legitimately seen with real function redefinition
382 errors. I would argue that the front end should never present us with
383 such a case, but don't enforce that for now. */
384 gcc_assert (!process
);
386 /* Reset our data structures so we can analyze the function again. */
387 memset (&local
, 0, sizeof (local
));
388 memset (&global
, 0, sizeof (global
));
389 memset (&rtl
, 0, sizeof (rtl
));
394 cpp_implicit_alias
= false;
397 remove_all_references ();
400 /* Return true when there are references to the node. */
403 symtab_node::referred_to_p (void)
407 /* See if there are any references at all. */
408 if (iterate_referring (0, ref
))
410 /* For functions check also calls. */
411 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (this);
412 if (cn
&& cn
->callers
)
417 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
418 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
419 the garbage collector run at the moment. We would need to either create
420 a new GC context, or just not compile right now. */
423 cgraph_node::finalize_function (tree decl
, bool no_collect
)
425 cgraph_node
*node
= cgraph_node::get_create (decl
);
427 if (node
->definition
)
429 /* Nested functions should only be defined once. */
430 gcc_assert (!DECL_CONTEXT (decl
)
431 || TREE_CODE (DECL_CONTEXT (decl
)) != FUNCTION_DECL
);
433 node
->local
.redefined_extern_inline
= true;
436 notice_global_symbol (decl
);
437 node
->definition
= true;
438 node
->lowered
= DECL_STRUCT_FUNCTION (decl
)->cfg
!= NULL
;
440 /* With -fkeep-inline-functions we are keeping all inline functions except
441 for extern inline ones. */
442 if (flag_keep_inline_functions
443 && DECL_DECLARED_INLINE_P (decl
)
444 && !DECL_EXTERNAL (decl
)
445 && !DECL_DISREGARD_INLINE_LIMITS (decl
))
446 node
->force_output
= 1;
448 /* When not optimizing, also output the static functions. (see
449 PR24561), but don't do so for always_inline functions, functions
450 declared inline and nested functions. These were optimized out
451 in the original implementation and it is unclear whether we want
452 to change the behavior here. */
454 && !node
->cpp_implicit_alias
455 && !DECL_DISREGARD_INLINE_LIMITS (decl
)
456 && !DECL_DECLARED_INLINE_P (decl
)
457 && !(DECL_CONTEXT (decl
)
458 && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
))
459 && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
460 node
->force_output
= 1;
462 /* If we've not yet emitted decl, tell the debug info about it. */
463 if (!TREE_ASM_WRITTEN (decl
))
464 (*debug_hooks
->deferred_inline_function
) (decl
);
466 /* Possibly warn about unused parameters. */
467 if (warn_unused_parameter
)
468 do_warn_unused_parameter (decl
);
473 if (symtab
->state
== CONSTRUCTION
474 && (node
->needed_p () || node
->referred_to_p ()))
478 /* Add the function FNDECL to the call graph.
479 Unlike finalize_function, this function is intended to be used
480 by middle end and allows insertion of new function at arbitrary point
481 of compilation. The function can be either in high, low or SSA form
484 The function is assumed to be reachable and have address taken (so no
485 API breaking optimizations are performed on it).
487 Main work done by this function is to enqueue the function for later
488 processing to avoid need the passes to be re-entrant. */
491 cgraph_node::add_new_function (tree fndecl
, bool lowered
)
493 gcc::pass_manager
*passes
= g
->get_passes ();
495 switch (symtab
->state
)
498 cgraph_node::finalize_function (fndecl
, false);
501 /* Just enqueue function to be processed at nearest occurrence. */
502 node
= cgraph_node::get_create (fndecl
);
504 node
->lowered
= true;
505 cgraph_new_nodes
.safe_push (node
);
511 /* Bring the function into finalized state and enqueue for later
512 analyzing and compilation. */
513 node
= cgraph_node::get_create (fndecl
);
514 node
->local
.local
= false;
515 node
->definition
= true;
516 node
->force_output
= true;
517 if (!lowered
&& symtab
->state
== EXPANSION
)
519 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
520 gimple_register_cfg_hooks ();
521 bitmap_obstack_initialize (NULL
);
522 execute_pass_list (cfun
, passes
->all_lowering_passes
);
523 passes
->execute_early_local_passes ();
524 bitmap_obstack_release (NULL
);
530 node
->lowered
= true;
531 cgraph_new_nodes
.safe_push (node
);
535 /* At the very end of compilation we have to do all the work up
537 node
= cgraph_node::create (fndecl
);
539 node
->lowered
= true;
540 node
->definition
= true;
542 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
543 gimple_register_cfg_hooks ();
544 bitmap_obstack_initialize (NULL
);
545 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
546 g
->get_passes ()->execute_early_local_passes ();
547 bitmap_obstack_release (NULL
);
556 /* Set a personality if required and we already passed EH lowering. */
558 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
559 == eh_personality_lang
))
560 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
563 /* Analyze the function scheduled to be output. */
565 cgraph_node::analyze (void)
567 tree decl
= this->decl
;
568 location_t saved_loc
= input_location
;
569 input_location
= DECL_SOURCE_LOCATION (decl
);
573 create_edge (cgraph_node::get (thunk
.alias
),
574 NULL
, 0, CGRAPH_FREQ_BASE
);
575 if (!expand_thunk (false, false))
583 resolve_alias (cgraph_node::get (alias_target
));
584 else if (dispatcher_function
)
586 /* Generate the dispatcher body of multi-versioned functions. */
587 cgraph_function_version_info
*dispatcher_version_info
588 = function_version ();
589 if (dispatcher_version_info
!= NULL
590 && (dispatcher_version_info
->dispatcher_resolver
593 tree resolver
= NULL_TREE
;
594 gcc_assert (targetm
.generate_version_dispatcher_body
);
595 resolver
= targetm
.generate_version_dispatcher_body (this);
596 gcc_assert (resolver
!= NULL_TREE
);
601 push_cfun (DECL_STRUCT_FUNCTION (decl
));
603 assign_assembler_name_if_neeeded (decl
);
605 /* Make sure to gimplify bodies only once. During analyzing a
606 function we lower it, which will require gimplified nested
607 functions, so we can end up here with an already gimplified
609 if (!gimple_has_body_p (decl
))
610 gimplify_function_tree (decl
);
611 dump_function (TDI_generic
, decl
);
613 /* Lower the function. */
617 lower_nested_functions (decl
);
618 gcc_assert (!nested
);
620 gimple_register_cfg_hooks ();
621 bitmap_obstack_initialize (NULL
);
622 execute_pass_list (cfun
, g
->get_passes ()->all_lowering_passes
);
623 free_dominance_info (CDI_POST_DOMINATORS
);
624 free_dominance_info (CDI_DOMINATORS
);
626 bitmap_obstack_release (NULL
);
634 input_location
= saved_loc
;
637 /* C++ frontend produce same body aliases all over the place, even before PCH
638 gets streamed out. It relies on us linking the aliases with their function
639 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
640 first produce aliases without links, but once C++ FE is sure he won't sream
641 PCH we build the links via this function. */
644 symbol_table::process_same_body_aliases (void)
647 FOR_EACH_SYMBOL (node
)
648 if (node
->cpp_implicit_alias
&& !node
->analyzed
)
650 (TREE_CODE (node
->alias_target
) == VAR_DECL
651 ? (symtab_node
*)varpool_node::get_create (node
->alias_target
)
652 : (symtab_node
*)cgraph_node::get_create (node
->alias_target
));
653 cpp_implicit_aliases_done
= true;
656 /* Process attributes common for vars and functions. */
659 process_common_attributes (symtab_node
*node
, tree decl
)
661 tree weakref
= lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
));
663 if (weakref
&& !lookup_attribute ("alias", DECL_ATTRIBUTES (decl
)))
665 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
666 "%<weakref%> attribute should be accompanied with"
667 " an %<alias%> attribute");
668 DECL_WEAK (decl
) = 0;
669 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
670 DECL_ATTRIBUTES (decl
));
673 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl
)))
674 node
->no_reorder
= 1;
677 /* Look for externally_visible and used attributes and mark cgraph nodes
680 We cannot mark the nodes at the point the attributes are processed (in
681 handle_*_attribute) because the copy of the declarations available at that
682 point may not be canonical. For example, in:
685 void f() __attribute__((used));
687 the declaration we see in handle_used_attribute will be the second
688 declaration -- but the front end will subsequently merge that declaration
689 with the original declaration and discard the second declaration.
691 Furthermore, we can't mark these nodes in finalize_function because:
694 void f() __attribute__((externally_visible));
698 So, we walk the nodes at the end of the translation unit, applying the
699 attributes at that point. */
702 process_function_and_variable_attributes (cgraph_node
*first
,
703 varpool_node
*first_var
)
708 for (node
= symtab
->first_function (); node
!= first
;
709 node
= symtab
->next_function (node
))
711 tree decl
= node
->decl
;
712 if (DECL_PRESERVE_P (decl
))
713 node
->mark_force_output ();
714 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
716 if (! TREE_PUBLIC (node
->decl
))
717 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
718 "%<externally_visible%>"
719 " attribute have effect only on public objects");
721 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
722 && (node
->definition
&& !node
->alias
))
724 warning_at (DECL_SOURCE_LOCATION (node
->decl
), OPT_Wattributes
,
725 "%<weakref%> attribute ignored"
726 " because function is defined");
727 DECL_WEAK (decl
) = 0;
728 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
729 DECL_ATTRIBUTES (decl
));
732 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
))
733 && !DECL_DECLARED_INLINE_P (decl
)
734 /* redefining extern inline function makes it DECL_UNINLINABLE. */
735 && !DECL_UNINLINABLE (decl
))
736 warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wattributes
,
737 "always_inline function might not be inlinable");
739 process_common_attributes (node
, decl
);
741 for (vnode
= symtab
->first_variable (); vnode
!= first_var
;
742 vnode
= symtab
->next_variable (vnode
))
744 tree decl
= vnode
->decl
;
745 if (DECL_EXTERNAL (decl
)
746 && DECL_INITIAL (decl
))
747 varpool_node::finalize_decl (decl
);
748 if (DECL_PRESERVE_P (decl
))
749 vnode
->force_output
= true;
750 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl
)))
752 if (! TREE_PUBLIC (vnode
->decl
))
753 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
754 "%<externally_visible%>"
755 " attribute have effect only on public objects");
757 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl
))
759 && DECL_INITIAL (decl
))
761 warning_at (DECL_SOURCE_LOCATION (vnode
->decl
), OPT_Wattributes
,
762 "%<weakref%> attribute ignored"
763 " because variable is initialized");
764 DECL_WEAK (decl
) = 0;
765 DECL_ATTRIBUTES (decl
) = remove_attribute ("weakref",
766 DECL_ATTRIBUTES (decl
));
768 process_common_attributes (vnode
, decl
);
772 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
773 middle end to output the variable to asm file, if needed or externally
777 varpool_node::finalize_decl (tree decl
)
779 varpool_node
*node
= varpool_node::get_create (decl
);
781 gcc_assert (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
));
783 if (node
->definition
)
785 notice_global_symbol (decl
);
786 node
->definition
= true;
787 if (TREE_THIS_VOLATILE (decl
) || DECL_PRESERVE_P (decl
)
788 /* Traditionally we do not eliminate static variables when not
789 optimizing and when not doing toplevel reoder. */
791 || ((!flag_toplevel_reorder
792 && !DECL_COMDAT (node
->decl
)
793 && !DECL_ARTIFICIAL (node
->decl
))))
794 node
->force_output
= true;
796 if (symtab
->state
== CONSTRUCTION
797 && (node
->needed_p () || node
->referred_to_p ()))
799 if (symtab
->state
>= IPA_SSA
)
801 /* Some frontends produce various interface variables after compilation
803 if (symtab
->state
== FINISHED
804 || (!flag_toplevel_reorder
805 && symtab
->state
== EXPANSION
))
806 node
->assemble_decl ();
809 /* EDGE is an polymorphic call. Mark all possible targets as reachable
810 and if there is only one target, perform trivial devirtualization.
811 REACHABLE_CALL_TARGETS collects target lists we already walked to
812 avoid udplicate work. */
815 walk_polymorphic_call_targets (hash_set
<void *> *reachable_call_targets
,
821 vec
<cgraph_node
*>targets
822 = possible_polymorphic_call_targets
823 (edge
, &final
, &cache_token
);
825 if (!reachable_call_targets
->add (cache_token
))
827 if (symtab
->dump_file
)
828 dump_possible_polymorphic_call_targets
829 (symtab
->dump_file
, edge
);
831 for (i
= 0; i
< targets
.length (); i
++)
833 /* Do not bother to mark virtual methods in anonymous namespace;
834 either we will find use of virtual table defining it, or it is
836 if (targets
[i
]->definition
838 (TREE_TYPE (targets
[i
]->decl
))
840 && !type_in_anonymous_namespace_p
842 (TREE_TYPE (targets
[i
]->decl
))))
843 enqueue_node (targets
[i
]);
847 /* Very trivial devirtualization; when the type is
848 final or anonymous (so we know all its derivation)
849 and there is only one possible virtual call target,
850 make the edge direct. */
853 if (targets
.length () <= 1 && dbg_cnt (devirt
))
856 if (targets
.length () == 1)
859 target
= cgraph_node::create
860 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
862 if (symtab
->dump_file
)
864 fprintf (symtab
->dump_file
,
865 "Devirtualizing call: ");
866 print_gimple_stmt (symtab
->dump_file
,
870 if (dump_enabled_p ())
872 location_t locus
= gimple_location_safe (edge
->call_stmt
);
873 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, locus
,
874 "devirtualizing call in %s to %s\n",
875 edge
->caller
->name (), target
->name ());
878 edge
->make_direct (target
);
879 edge
->redirect_call_stmt_to_callee ();
880 if (symtab
->dump_file
)
882 fprintf (symtab
->dump_file
,
883 "Devirtualized as: ");
884 print_gimple_stmt (symtab
->dump_file
,
893 /* Discover all functions and variables that are trivially needed, analyze
894 them as well as all functions and variables referred by them */
895 static cgraph_node
*first_analyzed
;
896 static varpool_node
*first_analyzed_var
;
899 analyze_functions (void)
901 /* Keep track of already processed nodes when called multiple times for
902 intermodule optimization. */
903 cgraph_node
*first_handled
= first_analyzed
;
904 varpool_node
*first_handled_var
= first_analyzed_var
;
905 hash_set
<void *> reachable_call_targets
;
912 location_t saved_loc
= input_location
;
914 bitmap_obstack_initialize (NULL
);
915 symtab
->state
= CONSTRUCTION
;
916 input_location
= UNKNOWN_LOCATION
;
918 /* Ugly, but the fixup can not happen at a time same body alias is created;
919 C++ FE is confused about the COMDAT groups being right. */
920 if (symtab
->cpp_implicit_aliases_done
)
921 FOR_EACH_SYMBOL (node
)
922 if (node
->cpp_implicit_alias
)
923 node
->fixup_same_cpp_alias_visibility (node
->get_alias_target ());
924 if (optimize
&& flag_devirtualize
)
925 build_type_inheritance_graph ();
927 /* Analysis adds static variables that in turn adds references to new functions.
928 So we need to iterate the process until it stabilize. */
932 process_function_and_variable_attributes (first_analyzed
,
935 /* First identify the trivially needed symbols. */
936 for (node
= symtab
->first_symbol ();
937 node
!= first_analyzed
938 && node
!= first_analyzed_var
; node
= node
->next
)
940 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
941 node
->get_comdat_group_id ();
942 if (node
->needed_p ())
945 if (!changed
&& symtab
->dump_file
)
946 fprintf (symtab
->dump_file
, "Trivially needed symbols:");
948 if (symtab
->dump_file
)
949 fprintf (symtab
->dump_file
, " %s", node
->asm_name ());
950 if (!changed
&& symtab
->dump_file
)
951 fprintf (symtab
->dump_file
, "\n");
953 if (node
== first_analyzed
954 || node
== first_analyzed_var
)
957 symtab
->process_new_functions ();
958 first_analyzed_var
= symtab
->first_variable ();
959 first_analyzed
= symtab
->first_function ();
961 if (changed
&& symtab
->dump_file
)
962 fprintf (symtab
->dump_file
, "\n");
964 /* Lower representation, build callgraph edges and references for all trivially
965 needed symbols and all symbols referred by them. */
966 while (queued_nodes
!= &symtab_terminator
)
970 queued_nodes
= (symtab_node
*)queued_nodes
->aux
;
971 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
);
972 if (cnode
&& cnode
->definition
)
975 tree decl
= cnode
->decl
;
977 /* ??? It is possible to create extern inline function
978 and later using weak alias attribute to kill its body.
979 See gcc.c-torture/compile/20011119-1.c */
980 if (!DECL_STRUCT_FUNCTION (decl
)
982 && !cnode
->thunk
.thunk_p
983 && !cnode
->dispatcher_function
)
986 cnode
->local
.redefined_extern_inline
= true;
990 if (!cnode
->analyzed
)
993 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
994 if (edge
->callee
->definition
)
995 enqueue_node (edge
->callee
);
996 if (optimize
&& flag_devirtualize
)
1000 for (edge
= cnode
->indirect_calls
; edge
; edge
= next
)
1002 next
= edge
->next_callee
;
1003 if (edge
->indirect_info
->polymorphic
)
1004 walk_polymorphic_call_targets (&reachable_call_targets
,
1009 /* If decl is a clone of an abstract function,
1010 mark that abstract function so that we don't release its body.
1011 The DECL_INITIAL() of that abstract function declaration
1012 will be later needed to output debug info. */
1013 if (DECL_ABSTRACT_ORIGIN (decl
))
1015 cgraph_node
*origin_node
1016 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl
));
1017 origin_node
->used_as_abstract_origin
= true;
1022 varpool_node
*vnode
= dyn_cast
<varpool_node
*> (node
);
1023 if (vnode
&& vnode
->definition
&& !vnode
->analyzed
)
1027 if (node
->same_comdat_group
)
1030 for (next
= node
->same_comdat_group
;
1032 next
= next
->same_comdat_group
)
1033 enqueue_node (next
);
1035 for (i
= 0; node
->iterate_reference (i
, ref
); i
++)
1036 if (ref
->referred
->definition
)
1037 enqueue_node (ref
->referred
);
1038 symtab
->process_new_functions ();
1041 if (optimize
&& flag_devirtualize
)
1042 update_type_inheritance_graph ();
1044 /* Collect entry points to the unit. */
1045 if (symtab
->dump_file
)
1047 fprintf (symtab
->dump_file
, "\n\nInitial ");
1048 symtab_node::dump_table (symtab
->dump_file
);
1051 if (symtab
->dump_file
)
1052 fprintf (symtab
->dump_file
, "\nRemoving unused symbols:");
1054 for (node
= symtab
->first_symbol ();
1055 node
!= first_handled
1056 && node
!= first_handled_var
; node
= next
)
1059 if (!node
->aux
&& !node
->referred_to_p ())
1061 if (symtab
->dump_file
)
1062 fprintf (symtab
->dump_file
, " %s", node
->name ());
1066 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (node
))
1068 tree decl
= node
->decl
;
1070 if (cnode
->definition
&& !gimple_has_body_p (decl
)
1072 && !cnode
->thunk
.thunk_p
)
1075 gcc_assert (!cnode
->definition
|| cnode
->thunk
.thunk_p
1077 || gimple_has_body_p (decl
));
1078 gcc_assert (cnode
->analyzed
== cnode
->definition
);
1082 for (;node
; node
= node
->next
)
1084 first_analyzed
= symtab
->first_function ();
1085 first_analyzed_var
= symtab
->first_variable ();
1086 if (symtab
->dump_file
)
1088 fprintf (symtab
->dump_file
, "\n\nReclaimed ");
1089 symtab_node::dump_table (symtab
->dump_file
);
1091 bitmap_obstack_release (NULL
);
1093 /* Initialize assembler name hash, in particular we want to trigger C++
1094 mangling and same body alias creation before we free DECL_ARGUMENTS
1097 symtab
->symtab_initialize_asm_name_hash ();
1099 input_location
= saved_loc
;
1102 /* Translate the ugly representation of aliases as alias pairs into nice
1103 representation in callgraph. We don't handle all cases yet,
1107 handle_alias_pairs (void)
1112 for (i
= 0; alias_pairs
&& alias_pairs
->iterate (i
, &p
);)
1114 symtab_node
*target_node
= symtab_node::get_for_asmname (p
->target
);
1116 /* Weakrefs with target not defined in current unit are easy to handle:
1117 they behave just as external variables except we need to note the
1118 alias flag to later output the weakref pseudo op into asm file. */
1120 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)) != NULL
)
1122 symtab_node
*node
= symtab_node::get (p
->decl
);
1125 node
->alias_target
= p
->target
;
1126 node
->weakref
= true;
1129 alias_pairs
->unordered_remove (i
);
1132 else if (!target_node
)
1134 error ("%q+D aliased to undefined symbol %qE", p
->decl
, p
->target
);
1135 symtab_node
*node
= symtab_node::get (p
->decl
);
1137 node
->alias
= false;
1138 alias_pairs
->unordered_remove (i
);
1142 if (DECL_EXTERNAL (target_node
->decl
)
1143 /* We use local aliases for C++ thunks to force the tailcall
1144 to bind locally. This is a hack - to keep it working do
1145 the following (which is not strictly correct). */
1146 && (TREE_CODE (target_node
->decl
) != FUNCTION_DECL
1147 || ! DECL_VIRTUAL_P (target_node
->decl
))
1148 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
)))
1150 error ("%q+D aliased to external symbol %qE",
1151 p
->decl
, p
->target
);
1154 if (TREE_CODE (p
->decl
) == FUNCTION_DECL
1155 && target_node
&& is_a
<cgraph_node
*> (target_node
))
1157 cgraph_node
*src_node
= cgraph_node::get (p
->decl
);
1158 if (src_node
&& src_node
->definition
)
1160 cgraph_node::create_alias (p
->decl
, target_node
->decl
);
1161 alias_pairs
->unordered_remove (i
);
1163 else if (TREE_CODE (p
->decl
) == VAR_DECL
1164 && target_node
&& is_a
<varpool_node
*> (target_node
))
1166 varpool_node::create_alias (p
->decl
, target_node
->decl
);
1167 alias_pairs
->unordered_remove (i
);
1171 error ("%q+D alias in between function and variable is not supported",
1173 warning (0, "%q+D aliased declaration",
1175 alias_pairs
->unordered_remove (i
);
1178 vec_free (alias_pairs
);
1182 /* Figure out what functions we want to assemble. */
1185 mark_functions_to_output (void)
1188 #ifdef ENABLE_CHECKING
1189 bool check_same_comdat_groups
= false;
1191 FOR_EACH_FUNCTION (node
)
1192 gcc_assert (!node
->process
);
1195 FOR_EACH_FUNCTION (node
)
1197 tree decl
= node
->decl
;
1199 gcc_assert (!node
->process
|| node
->same_comdat_group
);
1203 /* We need to output all local functions that are used and not
1204 always inlined, as well as those that are reachable from
1205 outside the current compilation unit. */
1207 && !node
->thunk
.thunk_p
1209 && !node
->global
.inlined_to
1210 && !TREE_ASM_WRITTEN (decl
)
1211 && !DECL_EXTERNAL (decl
))
1214 if (node
->same_comdat_group
)
1217 for (next
= dyn_cast
<cgraph_node
*> (node
->same_comdat_group
);
1219 next
= dyn_cast
<cgraph_node
*> (next
->same_comdat_group
))
1220 if (!next
->thunk
.thunk_p
&& !next
->alias
1221 && !next
->comdat_local_p ())
1225 else if (node
->same_comdat_group
)
1227 #ifdef ENABLE_CHECKING
1228 check_same_comdat_groups
= true;
1233 /* We should've reclaimed all functions that are not needed. */
1234 #ifdef ENABLE_CHECKING
1235 if (!node
->global
.inlined_to
1236 && gimple_has_body_p (decl
)
1237 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1238 are inside partition, we can end up not removing the body since we no longer
1239 have analyzed node pointing to it. */
1240 && !node
->in_other_partition
1243 && !DECL_EXTERNAL (decl
))
1246 internal_error ("failed to reclaim unneeded function");
1249 gcc_assert (node
->global
.inlined_to
1250 || !gimple_has_body_p (decl
)
1251 || node
->in_other_partition
1253 || DECL_ARTIFICIAL (decl
)
1254 || DECL_EXTERNAL (decl
));
1259 #ifdef ENABLE_CHECKING
1260 if (check_same_comdat_groups
)
1261 FOR_EACH_FUNCTION (node
)
1262 if (node
->same_comdat_group
&& !node
->process
)
1264 tree decl
= node
->decl
;
1265 if (!node
->global
.inlined_to
1266 && gimple_has_body_p (decl
)
1267 /* FIXME: in an ltrans unit when the offline copy is outside a
1268 partition but inline copies are inside a partition, we can
1269 end up not removing the body since we no longer have an
1270 analyzed node pointing to it. */
1271 && !node
->in_other_partition
1273 && !DECL_EXTERNAL (decl
))
1276 internal_error ("failed to reclaim unneeded function in same "
1283 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1284 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1286 Set current_function_decl and cfun to newly constructed empty function body.
1287 return basic block in the function body. */
1290 init_lowered_empty_function (tree decl
, bool in_ssa
)
1294 current_function_decl
= decl
;
1295 allocate_struct_function (decl
, false);
1296 gimple_register_cfg_hooks ();
1297 init_empty_tree_cfg ();
1301 init_tree_ssa (cfun
);
1302 init_ssa_operands (cfun
);
1303 cfun
->gimple_df
->in_ssa_p
= true;
1304 cfun
->curr_properties
|= PROP_ssa
;
1307 DECL_INITIAL (decl
) = make_node (BLOCK
);
1309 DECL_SAVED_TREE (decl
) = error_mark_node
;
1310 cfun
->curr_properties
|= (PROP_gimple_lcf
| PROP_gimple_leh
| PROP_gimple_any
1311 | PROP_cfg
| PROP_loops
);
1313 set_loops_for_fn (cfun
, ggc_cleared_alloc
<loops
> ());
1314 init_loops_structure (cfun
, loops_for_fn (cfun
), 1);
1315 loops_for_fn (cfun
)->state
|= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
1317 /* Create BB for body of the function and connect it properly. */
1318 bb
= create_basic_block (NULL
, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
1319 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, EDGE_FALLTHRU
);
1320 make_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1321 add_bb_to_loop (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->loop_father
);
1326 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1327 offset indicated by VIRTUAL_OFFSET, if that is
1328 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1329 zero for a result adjusting thunk. */
1332 thunk_adjust (gimple_stmt_iterator
* bsi
,
1333 tree ptr
, bool this_adjusting
,
1334 HOST_WIDE_INT fixed_offset
, tree virtual_offset
)
1340 && fixed_offset
!= 0)
1342 stmt
= gimple_build_assign
1343 (ptr
, fold_build_pointer_plus_hwi_loc (input_location
,
1346 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1349 /* If there's a virtual offset, look up that value in the vtable and
1350 adjust the pointer again. */
1357 if (!vtable_entry_type
)
1359 tree vfunc_type
= make_node (FUNCTION_TYPE
);
1360 TREE_TYPE (vfunc_type
) = integer_type_node
;
1361 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
1362 layout_type (vfunc_type
);
1364 vtable_entry_type
= build_pointer_type (vfunc_type
);
1368 create_tmp_reg (build_pointer_type
1369 (build_pointer_type (vtable_entry_type
)), "vptr");
1371 /* The vptr is always at offset zero in the object. */
1372 stmt
= gimple_build_assign (vtabletmp
,
1373 build1 (NOP_EXPR
, TREE_TYPE (vtabletmp
),
1375 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1377 /* Form the vtable address. */
1378 vtabletmp2
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp
)),
1380 stmt
= gimple_build_assign (vtabletmp2
,
1381 build_simple_mem_ref (vtabletmp
));
1382 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1384 /* Find the entry with the vcall offset. */
1385 stmt
= gimple_build_assign (vtabletmp2
,
1386 fold_build_pointer_plus_loc (input_location
,
1389 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1391 /* Get the offset itself. */
1392 vtabletmp3
= create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2
)),
1394 stmt
= gimple_build_assign (vtabletmp3
,
1395 build_simple_mem_ref (vtabletmp2
));
1396 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1398 /* Adjust the `this' pointer. */
1399 ptr
= fold_build_pointer_plus_loc (input_location
, ptr
, vtabletmp3
);
1400 ptr
= force_gimple_operand_gsi (bsi
, ptr
, true, NULL_TREE
, false,
1401 GSI_CONTINUE_LINKING
);
1405 && fixed_offset
!= 0)
1406 /* Adjust the pointer by the constant. */
1410 if (TREE_CODE (ptr
) == VAR_DECL
)
1414 ptrtmp
= create_tmp_reg (TREE_TYPE (ptr
), "ptr");
1415 stmt
= gimple_build_assign (ptrtmp
, ptr
);
1416 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1418 ptr
= fold_build_pointer_plus_hwi_loc (input_location
,
1419 ptrtmp
, fixed_offset
);
1422 /* Emit the statement and gimplify the adjustment expression. */
1423 ret
= create_tmp_reg (TREE_TYPE (ptr
), "adjusted_this");
1424 stmt
= gimple_build_assign (ret
, ptr
);
1425 gsi_insert_after (bsi
, stmt
, GSI_NEW_STMT
);
1430 /* Expand thunk NODE to gimple if possible.
1431 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1432 no assembler is produced.
1433 When OUTPUT_ASM_THUNK is true, also produce assembler for
1434 thunks that are not lowered. */
1437 cgraph_node::expand_thunk (bool output_asm_thunks
, bool force_gimple_thunk
)
1439 bool this_adjusting
= thunk
.this_adjusting
;
1440 HOST_WIDE_INT fixed_offset
= thunk
.fixed_offset
;
1441 HOST_WIDE_INT virtual_value
= thunk
.virtual_value
;
1442 tree virtual_offset
= NULL
;
1443 tree alias
= callees
->callee
->decl
;
1444 tree thunk_fndecl
= decl
;
1448 if (!force_gimple_thunk
&& this_adjusting
1449 && targetm
.asm_out
.can_output_mi_thunk (thunk_fndecl
, fixed_offset
,
1450 virtual_value
, alias
))
1454 tree restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1456 if (!output_asm_thunks
)
1464 a
= DECL_ARGUMENTS (thunk_fndecl
);
1466 current_function_decl
= thunk_fndecl
;
1468 /* Ensure thunks are emitted in their correct sections. */
1469 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
1471 DECL_RESULT (thunk_fndecl
)
1472 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl
),
1473 RESULT_DECL
, 0, restype
);
1474 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1475 fnname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl
));
1477 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1479 fn_block
= make_node (BLOCK
);
1480 BLOCK_VARS (fn_block
) = a
;
1481 DECL_INITIAL (thunk_fndecl
) = fn_block
;
1482 init_function_start (thunk_fndecl
);
1484 insn_locations_init ();
1485 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl
));
1486 prologue_location
= curr_insn_location ();
1487 assemble_start_function (thunk_fndecl
, fnname
);
1489 targetm
.asm_out
.output_mi_thunk (asm_out_file
, thunk_fndecl
,
1490 fixed_offset
, virtual_value
, alias
);
1492 assemble_end_function (thunk_fndecl
, fnname
);
1493 insn_locations_finalize ();
1494 init_insn_lengths ();
1495 free_after_compilation (cfun
);
1497 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
1498 thunk
.thunk_p
= false;
1504 basic_block bb
, then_bb
, else_bb
, return_bb
;
1505 gimple_stmt_iterator bsi
;
1517 a
= DECL_ARGUMENTS (thunk_fndecl
);
1519 current_function_decl
= thunk_fndecl
;
1521 /* Ensure thunks are emitted in their correct sections. */
1522 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
1524 DECL_IGNORED_P (thunk_fndecl
) = 1;
1525 bitmap_obstack_initialize (NULL
);
1527 if (thunk
.virtual_offset_p
)
1528 virtual_offset
= size_int (virtual_value
);
1530 /* Build the return declaration for the function. */
1531 restype
= TREE_TYPE (TREE_TYPE (thunk_fndecl
));
1532 if (DECL_RESULT (thunk_fndecl
) == NULL_TREE
)
1534 resdecl
= build_decl (input_location
, RESULT_DECL
, 0, restype
);
1535 DECL_ARTIFICIAL (resdecl
) = 1;
1536 DECL_IGNORED_P (resdecl
) = 1;
1537 DECL_RESULT (thunk_fndecl
) = resdecl
;
1538 DECL_CONTEXT (DECL_RESULT (thunk_fndecl
)) = thunk_fndecl
;
1541 resdecl
= DECL_RESULT (thunk_fndecl
);
1543 bb
= then_bb
= else_bb
= return_bb
= init_lowered_empty_function (thunk_fndecl
, true);
1545 bsi
= gsi_start_bb (bb
);
1547 /* Build call to the function being thunked. */
1548 if (!VOID_TYPE_P (restype
))
1550 if (DECL_BY_REFERENCE (resdecl
))
1551 restmp
= gimple_fold_indirect_ref (resdecl
);
1552 else if (!is_gimple_reg_type (restype
))
1556 if (TREE_CODE (restmp
) == VAR_DECL
)
1557 add_local_decl (cfun
, restmp
);
1558 BLOCK_VARS (DECL_INITIAL (current_function_decl
)) = restmp
;
1561 restmp
= create_tmp_reg (restype
, "retval");
1564 for (arg
= a
; arg
; arg
= DECL_CHAIN (arg
))
1566 auto_vec
<tree
> vargs (nargs
);
1568 vargs
.quick_push (thunk_adjust (&bsi
, a
, 1, fixed_offset
,
1571 vargs
.quick_push (a
);
1574 for (i
= 1, arg
= DECL_CHAIN (a
); i
< nargs
; i
++, arg
= DECL_CHAIN (arg
))
1577 if (!is_gimple_val (arg
))
1579 tmp
= create_tmp_reg (TYPE_MAIN_VARIANT
1580 (TREE_TYPE (arg
)), "arg");
1581 gimple stmt
= gimple_build_assign (tmp
, arg
);
1582 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1584 vargs
.quick_push (tmp
);
1586 call
= gimple_build_call_vec (build_fold_addr_expr_loc (0, alias
), vargs
);
1587 callees
->call_stmt
= call
;
1588 gimple_call_set_from_thunk (call
, true);
1591 gimple_call_set_lhs (call
, restmp
);
1592 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp
),
1593 TREE_TYPE (TREE_TYPE (alias
))));
1595 gsi_insert_after (&bsi
, call
, GSI_NEW_STMT
);
1596 if (!(gimple_call_flags (call
) & ECF_NORETURN
))
1598 if (restmp
&& !this_adjusting
1599 && (fixed_offset
|| virtual_offset
))
1601 tree true_label
= NULL_TREE
;
1603 if (TREE_CODE (TREE_TYPE (restmp
)) == POINTER_TYPE
)
1606 /* If the return type is a pointer, we need to
1607 protect against NULL. We know there will be an
1608 adjustment, because that's why we're emitting a
1610 then_bb
= create_basic_block (NULL
, (void *) 0, bb
);
1611 return_bb
= create_basic_block (NULL
, (void *) 0, then_bb
);
1612 else_bb
= create_basic_block (NULL
, (void *) 0, else_bb
);
1613 add_bb_to_loop (then_bb
, bb
->loop_father
);
1614 add_bb_to_loop (return_bb
, bb
->loop_father
);
1615 add_bb_to_loop (else_bb
, bb
->loop_father
);
1616 remove_edge (single_succ_edge (bb
));
1617 true_label
= gimple_block_label (then_bb
);
1618 stmt
= gimple_build_cond (NE_EXPR
, restmp
,
1619 build_zero_cst (TREE_TYPE (restmp
)),
1620 NULL_TREE
, NULL_TREE
);
1621 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1622 make_edge (bb
, then_bb
, EDGE_TRUE_VALUE
);
1623 make_edge (bb
, else_bb
, EDGE_FALSE_VALUE
);
1624 make_edge (return_bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
1625 make_edge (then_bb
, return_bb
, EDGE_FALLTHRU
);
1626 make_edge (else_bb
, return_bb
, EDGE_FALLTHRU
);
1627 bsi
= gsi_last_bb (then_bb
);
1630 restmp
= thunk_adjust (&bsi
, restmp
, /*this_adjusting=*/0,
1631 fixed_offset
, virtual_offset
);
1635 bsi
= gsi_last_bb (else_bb
);
1636 stmt
= gimple_build_assign (restmp
,
1637 build_zero_cst (TREE_TYPE (restmp
)));
1638 gsi_insert_after (&bsi
, stmt
, GSI_NEW_STMT
);
1639 bsi
= gsi_last_bb (return_bb
);
1643 gimple_call_set_tail (call
, true);
1645 /* Build return value. */
1646 ret
= gimple_build_return (restmp
);
1647 gsi_insert_after (&bsi
, ret
, GSI_NEW_STMT
);
1651 gimple_call_set_tail (call
, true);
1652 remove_edge (single_succ_edge (bb
));
1655 cfun
->gimple_df
->in_ssa_p
= true;
1656 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1657 TREE_ASM_WRITTEN (thunk_fndecl
) = false;
1658 delete_unreachable_blocks ();
1659 update_ssa (TODO_update_ssa
);
1660 #ifdef ENABLE_CHECKING
1661 verify_flow_info ();
1663 free_dominance_info (CDI_DOMINATORS
);
1665 /* Since we want to emit the thunk, we explicitly mark its name as
1667 thunk
.thunk_p
= false;
1669 bitmap_obstack_release (NULL
);
1671 current_function_decl
= NULL
;
1676 /* Assemble thunks and aliases associated to node. */
1679 cgraph_node::assemble_thunks_and_aliases (void)
1684 for (e
= callers
; e
;)
1685 if (e
->caller
->thunk
.thunk_p
)
1687 cgraph_node
*thunk
= e
->caller
;
1690 thunk
->expand_thunk (true, false);
1691 thunk
->assemble_thunks_and_aliases ();
1696 FOR_EACH_ALIAS (this, ref
)
1698 cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
1699 bool saved_written
= TREE_ASM_WRITTEN (decl
);
1701 /* Force assemble_alias to really output the alias this time instead
1702 of buffering it in same alias pairs. */
1703 TREE_ASM_WRITTEN (decl
) = 1;
1704 do_assemble_alias (alias
->decl
,
1705 DECL_ASSEMBLER_NAME (decl
));
1706 alias
->assemble_thunks_and_aliases ();
1707 TREE_ASM_WRITTEN (decl
) = saved_written
;
1711 /* Expand function specified by node. */
1714 cgraph_node::expand (void)
1716 location_t saved_loc
;
1718 /* We ought to not compile any inline clones. */
1719 gcc_assert (!global
.inlined_to
);
1721 announce_function (decl
);
1723 gcc_assert (lowered
);
1726 /* Generate RTL for the body of DECL. */
1728 timevar_push (TV_REST_OF_COMPILATION
);
1730 gcc_assert (symtab
->global_info_ready
);
1732 /* Initialize the default bitmap obstack. */
1733 bitmap_obstack_initialize (NULL
);
1735 /* Initialize the RTL code for the function. */
1736 current_function_decl
= decl
;
1737 saved_loc
= input_location
;
1738 input_location
= DECL_SOURCE_LOCATION (decl
);
1739 init_function_start (decl
);
1741 gimple_register_cfg_hooks ();
1743 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
1745 execute_all_ipa_transforms ();
1747 /* Perform all tree transforms and optimizations. */
1749 /* Signal the start of passes. */
1750 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START
, NULL
);
1752 execute_pass_list (cfun
, g
->get_passes ()->all_passes
);
1754 /* Signal the end of passes. */
1755 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END
, NULL
);
1757 bitmap_obstack_release (®_obstack
);
1759 /* Release the default bitmap obstack. */
1760 bitmap_obstack_release (NULL
);
1762 /* If requested, warn about function definitions where the function will
1763 return a value (usually of some struct or union type) which itself will
1764 take up a lot of stack space. */
1765 if (warn_larger_than
&& !DECL_EXTERNAL (decl
) && TREE_TYPE (decl
))
1767 tree ret_type
= TREE_TYPE (TREE_TYPE (decl
));
1769 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
1770 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
1771 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
1774 unsigned int size_as_int
1775 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
1777 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
1778 warning (OPT_Wlarger_than_
, "size of return value of %q+D is %u bytes",
1781 warning (OPT_Wlarger_than_
, "size of return value of %q+D is larger than %wd bytes",
1782 decl
, larger_than_size
);
1786 gimple_set_body (decl
, NULL
);
1787 if (DECL_STRUCT_FUNCTION (decl
) == 0
1788 && !cgraph_node::get (decl
)->origin
)
1790 /* Stop pointing to the local nodes about to be freed.
1791 But DECL_INITIAL must remain nonzero so we know this
1792 was an actual function definition.
1793 For a nested function, this is done in c_pop_function_context.
1794 If rest_of_compilation set this to 0, leave it 0. */
1795 if (DECL_INITIAL (decl
) != 0)
1796 DECL_INITIAL (decl
) = error_mark_node
;
1799 input_location
= saved_loc
;
1802 timevar_pop (TV_REST_OF_COMPILATION
);
1804 /* Make sure that BE didn't give up on compiling. */
1805 gcc_assert (TREE_ASM_WRITTEN (decl
));
1807 current_function_decl
= NULL
;
1809 /* It would make a lot more sense to output thunks before function body to get more
1810 forward and lest backwarding jumps. This however would need solving problem
1811 with comdats. See PR48668. Also aliases must come after function itself to
1812 make one pass assemblers, like one on AIX, happy. See PR 50689.
1813 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1815 assemble_thunks_and_aliases ();
1817 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1818 points to the dead function body. */
1820 remove_all_references ();
1823 /* Node comparer that is responsible for the order that corresponds
1824 to time when a function was launched for the first time. */
1827 node_cmp (const void *pa
, const void *pb
)
1829 const cgraph_node
*a
= *(const cgraph_node
* const *) pa
;
1830 const cgraph_node
*b
= *(const cgraph_node
* const *) pb
;
1832 /* Functions with time profile must be before these without profile. */
1833 if (!a
->tp_first_run
|| !b
->tp_first_run
)
1834 return a
->tp_first_run
- b
->tp_first_run
;
1836 return a
->tp_first_run
!= b
->tp_first_run
1837 ? b
->tp_first_run
- a
->tp_first_run
1838 : b
->order
- a
->order
;
1841 /* Expand all functions that must be output.
1843 Attempt to topologically sort the nodes so function is output when
1844 all called functions are already assembled to allow data to be
1845 propagated across the callgraph. Use a stack to get smaller distance
1846 between a function and its callees (later we may choose to use a more
1847 sophisticated algorithm for function reordering; we will likely want
1848 to use subsections to make the output functions appear in top-down
1852 expand_all_functions (void)
1855 cgraph_node
**order
= XCNEWVEC (cgraph_node
*,
1856 symtab
->cgraph_count
);
1857 unsigned int expanded_func_count
= 0, profiled_func_count
= 0;
1858 int order_pos
, new_order_pos
= 0;
1861 order_pos
= ipa_reverse_postorder (order
);
1862 gcc_assert (order_pos
== symtab
->cgraph_count
);
1864 /* Garbage collector may remove inline clones we eliminate during
1865 optimization. So we must be sure to not reference them. */
1866 for (i
= 0; i
< order_pos
; i
++)
1867 if (order
[i
]->process
)
1868 order
[new_order_pos
++] = order
[i
];
1870 if (flag_profile_reorder_functions
)
1871 qsort (order
, new_order_pos
, sizeof (cgraph_node
*), node_cmp
);
1873 for (i
= new_order_pos
- 1; i
>= 0; i
--)
1879 expanded_func_count
++;
1880 if(node
->tp_first_run
)
1881 profiled_func_count
++;
1883 if (symtab
->dump_file
)
1884 fprintf (symtab
->dump_file
,
1885 "Time profile order in expand_all_functions:%s:%d\n",
1886 node
->asm_name (), node
->tp_first_run
);
1893 fprintf (dump_file
, "Expanded functions with time profile (%s):%u/%u\n",
1894 main_input_filename
, profiled_func_count
, expanded_func_count
);
1896 if (symtab
->dump_file
&& flag_profile_reorder_functions
)
1897 fprintf (symtab
->dump_file
, "Expanded functions with time profile:%u/%u\n",
1898 profiled_func_count
, expanded_func_count
);
1900 symtab
->process_new_functions ();
1901 free_gimplify_stack ();
1906 /* This is used to sort the node types by the cgraph order number. */
1908 enum cgraph_order_sort_kind
1910 ORDER_UNDEFINED
= 0,
1916 struct cgraph_order_sort
1918 enum cgraph_order_sort_kind kind
;
1927 /* Output all functions, variables, and asm statements in the order
1928 according to their order fields, which is the order in which they
1929 appeared in the file. This implements -fno-toplevel-reorder. In
1930 this mode we may output functions and variables which don't really
1932 When NO_REORDER is true only do this for symbols marked no reorder. */
1935 output_in_order (bool no_reorder
)
1938 cgraph_order_sort
*nodes
;
1943 max
= symtab
->order
;
1944 nodes
= XCNEWVEC (cgraph_order_sort
, max
);
1946 FOR_EACH_DEFINED_FUNCTION (pf
)
1948 if (pf
->process
&& !pf
->thunk
.thunk_p
&& !pf
->alias
)
1950 if (no_reorder
&& !pf
->no_reorder
)
1953 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1954 nodes
[i
].kind
= ORDER_FUNCTION
;
1959 FOR_EACH_DEFINED_VARIABLE (pv
)
1960 if (!DECL_EXTERNAL (pv
->decl
))
1962 if (no_reorder
&& !pv
->no_reorder
)
1965 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1966 nodes
[i
].kind
= ORDER_VAR
;
1970 for (pa
= symtab
->first_asm_symbol (); pa
; pa
= pa
->next
)
1973 gcc_assert (nodes
[i
].kind
== ORDER_UNDEFINED
);
1974 nodes
[i
].kind
= ORDER_ASM
;
1978 /* In toplevel reorder mode we output all statics; mark them as needed. */
1980 for (i
= 0; i
< max
; ++i
)
1981 if (nodes
[i
].kind
== ORDER_VAR
)
1982 nodes
[i
].u
.v
->finalize_named_section_flags ();
1984 for (i
= 0; i
< max
; ++i
)
1986 switch (nodes
[i
].kind
)
1988 case ORDER_FUNCTION
:
1989 nodes
[i
].u
.f
->process
= 0;
1990 nodes
[i
].u
.f
->expand ();
1994 nodes
[i
].u
.v
->assemble_decl ();
1998 assemble_asm (nodes
[i
].u
.a
->asm_str
);
2001 case ORDER_UNDEFINED
:
2009 symtab
->clear_asm_symbols ();
2014 /* Collect all global variables with "omp declare target" attribute into
2015 OFFLOAD_VARS. It will be streamed out in ipa_write_summaries. */
2018 init_offload_var_table (void)
2020 struct varpool_node
*vnode
;
2021 FOR_EACH_DEFINED_VARIABLE (vnode
)
2023 if (!lookup_attribute ("omp declare target",
2024 DECL_ATTRIBUTES (vnode
->decl
))
2025 || TREE_CODE (vnode
->decl
) != VAR_DECL
2026 || DECL_SIZE (vnode
->decl
) == 0)
2028 vec_safe_push (offload_vars
, vnode
->decl
);
2035 gcc::pass_manager
*passes
= g
->get_passes ();
2038 current_function_decl
= NULL
;
2039 gimple_register_cfg_hooks ();
2040 bitmap_obstack_initialize (NULL
);
2042 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START
, NULL
);
2046 execute_ipa_pass_list (passes
->all_small_ipa_passes
);
2051 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2052 devirtualization and other changes where removal iterate. */
2053 symtab
->remove_unreachable_nodes (true, symtab
->dump_file
);
2055 /* If pass_all_early_optimizations was not scheduled, the state of
2056 the cgraph will not be properly updated. Update it now. */
2057 if (symtab
->state
< IPA_SSA
)
2058 symtab
->state
= IPA_SSA
;
2062 /* Generate coverage variables and constructors. */
2065 /* Process new functions added. */
2067 current_function_decl
= NULL
;
2068 symtab
->process_new_functions ();
2070 execute_ipa_summary_passes
2071 ((ipa_opt_pass_d
*) passes
->all_regular_ipa_passes
);
2074 /* Some targets need to handle LTO assembler output specially. */
2075 if (flag_generate_lto
)
2076 targetm
.asm_out
.lto_start ();
2080 init_offload_var_table ();
2082 if ((flag_openacc
|| flag_openmp
)
2083 && !(vec_safe_is_empty (offload_funcs
)
2084 && vec_safe_is_empty (offload_vars
)))
2086 section_name_prefix
= OMP_SECTION_NAME_PREFIX
;
2087 ipa_write_summaries (true);
2091 section_name_prefix
= LTO_SECTION_NAME_PREFIX
;
2092 ipa_write_summaries (false);
2096 if (flag_generate_lto
)
2097 targetm
.asm_out
.lto_end ();
2099 if (!flag_ltrans
&& (in_lto_p
|| !flag_lto
|| flag_fat_lto_objects
))
2100 execute_ipa_pass_list (passes
->all_regular_ipa_passes
);
2101 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END
, NULL
);
2103 bitmap_obstack_release (NULL
);
2107 /* Return string alias is alias of. */
2110 get_alias_symbol (tree decl
)
2112 tree alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
2113 return get_identifier (TREE_STRING_POINTER
2114 (TREE_VALUE (TREE_VALUE (alias
))));
2118 /* Weakrefs may be associated to external decls and thus not output
2119 at expansion time. Emit all necessary aliases. */
2122 symbol_table::output_weakrefs (void)
2125 FOR_EACH_SYMBOL (node
)
2127 && !TREE_ASM_WRITTEN (node
->decl
)
2132 /* Weakrefs are special by not requiring target definition in current
2133 compilation unit. It is thus bit hard to work out what we want to
2135 When alias target is defined, we need to fetch it from symtab reference,
2136 otherwise it is pointed to by alias_target. */
2137 if (node
->alias_target
)
2138 target
= (DECL_P (node
->alias_target
)
2139 ? DECL_ASSEMBLER_NAME (node
->alias_target
)
2140 : node
->alias_target
);
2141 else if (node
->analyzed
)
2142 target
= DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
);
2146 target
= get_alias_symbol (node
->decl
);
2148 do_assemble_alias (node
->decl
, target
);
2152 /* Perform simple optimizations based on callgraph. */
2155 symbol_table::compile (void)
2160 #ifdef ENABLE_CHECKING
2161 symtab_node::verify_symtab_nodes ();
2164 timevar_push (TV_CGRAPHOPT
);
2165 if (pre_ipa_mem_report
)
2167 fprintf (stderr
, "Memory consumption before IPA\n");
2168 dump_memory_report (false);
2171 fprintf (stderr
, "Performing interprocedural optimizations\n");
2174 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2175 if (flag_lto
|| flag_openacc
|| flag_openmp
)
2176 lto_streamer_hooks_init ();
2178 /* Don't run the IPA passes if there was any error or sorry messages. */
2182 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2184 || (!in_lto_p
&& flag_lto
&& !flag_fat_lto_objects
))
2186 timevar_pop (TV_CGRAPHOPT
);
2190 /* This pass remove bodies of extern inline functions we never inlined.
2191 Do this later so other IPA passes see what is really going on.
2192 FIXME: This should be run just after inlining by pasmanager. */
2193 remove_unreachable_nodes (false, dump_file
);
2194 global_info_ready
= true;
2197 fprintf (dump_file
, "Optimized ");
2198 symtab_node:: dump_table (dump_file
);
2200 if (post_ipa_mem_report
)
2202 fprintf (stderr
, "Memory consumption after IPA\n");
2203 dump_memory_report (false);
2205 timevar_pop (TV_CGRAPHOPT
);
2207 /* Output everything. */
2208 (*debug_hooks
->assembly_start
) ();
2210 fprintf (stderr
, "Assembling functions:\n");
2211 #ifdef ENABLE_CHECKING
2212 symtab_node::verify_symtab_nodes ();
2215 materialize_all_clones ();
2216 bitmap_obstack_initialize (NULL
);
2217 execute_ipa_pass_list (g
->get_passes ()->all_late_ipa_passes
);
2218 bitmap_obstack_release (NULL
);
2219 mark_functions_to_output ();
2221 /* When weakref support is missing, we autmatically translate all
2222 references to NODE to references to its ultimate alias target.
2223 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2226 Set up this mapping before we output any assembler but once we are sure
2227 that all symbol renaming is done.
2229 FIXME: All this uglyness can go away if we just do renaming at gimple
2230 level by physically rewritting the IL. At the moment we can only redirect
2231 calls, so we need infrastructure for renaming references as well. */
2232 #ifndef ASM_OUTPUT_WEAKREF
2235 FOR_EACH_SYMBOL (node
)
2237 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node
->decl
)))
2239 IDENTIFIER_TRANSPARENT_ALIAS
2240 (DECL_ASSEMBLER_NAME (node
->decl
)) = 1;
2241 TREE_CHAIN (DECL_ASSEMBLER_NAME (node
->decl
))
2242 = (node
->alias_target
? node
->alias_target
2243 : DECL_ASSEMBLER_NAME (node
->get_alias_target ()->decl
));
2249 if (!flag_toplevel_reorder
)
2250 output_in_order (false);
2253 /* Output first asm statements and anything ordered. The process
2254 flag is cleared for these nodes, so we skip them later. */
2255 output_in_order (true);
2256 expand_all_functions ();
2257 output_variables ();
2260 process_new_functions ();
2266 fprintf (dump_file
, "\nFinal ");
2267 symtab_node::dump_table (dump_file
);
2269 #ifdef ENABLE_CHECKING
2270 symtab_node::verify_symtab_nodes ();
2271 /* Double check that all inline clones are gone and that all
2272 function bodies have been released from memory. */
2276 bool error_found
= false;
2278 FOR_EACH_DEFINED_FUNCTION (node
)
2279 if (node
->global
.inlined_to
2280 || gimple_has_body_p (node
->decl
))
2286 internal_error ("nodes with unreleased memory found");
2292 /* Analyze the whole compilation unit once it is parsed completely. */
2295 symbol_table::finalize_compilation_unit (void)
2297 timevar_push (TV_CGRAPH
);
2299 /* If we're here there's no current function anymore. Some frontends
2300 are lazy in clearing these. */
2301 current_function_decl
= NULL
;
2304 /* Do not skip analyzing the functions if there were errors, we
2305 miss diagnostics for following functions otherwise. */
2307 /* Emit size functions we didn't inline. */
2308 finalize_size_functions ();
2310 /* Mark alias targets necessary and emit diagnostics. */
2311 handle_alias_pairs ();
2315 fprintf (stderr
, "\nAnalyzing compilation unit\n");
2319 if (flag_dump_passes
)
2322 /* Gimplify and lower all functions, compute reachability and
2323 remove unreachable nodes. */
2324 analyze_functions ();
2326 /* Mark alias targets necessary and emit diagnostics. */
2327 handle_alias_pairs ();
2329 /* Gimplify and lower thunks. */
2330 analyze_functions ();
2332 /* Finally drive the pass manager. */
2335 timevar_pop (TV_CGRAPH
);
2338 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2339 within the same process. For use by toplev::finalize. */
2342 cgraphunit_c_finalize (void)
2344 gcc_assert (cgraph_new_nodes
.length () == 0);
2345 cgraph_new_nodes
.truncate (0);
2347 vtable_entry_type
= NULL
;
2348 queued_nodes
= &symtab_terminator
;
2350 first_analyzed
= NULL
;
2351 first_analyzed_var
= NULL
;
2354 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2355 kind of wrapper method. */
2358 cgraph_node::create_wrapper (cgraph_node
*target
)
2360 /* Preserve DECL_RESULT so we get right by reference flag. */
2361 tree decl_result
= DECL_RESULT (decl
);
2363 /* Remove the function's body but keep arguments to be reused
2365 release_body (true);
2368 DECL_RESULT (decl
) = decl_result
;
2369 DECL_INITIAL (decl
) = NULL
;
2370 allocate_struct_function (decl
, false);
2373 /* Turn alias into thunk and expand it into GIMPLE representation. */
2375 thunk
.thunk_p
= true;
2376 thunk
.this_adjusting
= false;
2378 cgraph_edge
*e
= create_edge (target
, NULL
, 0, CGRAPH_FREQ_BASE
);
2380 expand_thunk (false, true);
2381 e
->call_stmt_cannot_inline_p
= true;
2383 /* Inline summary set-up. */
2385 inline_analyze_function (this);
2388 #include "gt-cgraphunit.h"