* config/visium/visium.c (visium_gimplify_va_arg): Emit a big-endian
[official-gcc.git] / gcc / cgraphunit.c
blob4fa518d27c63ded2dcd348d0825951f09f2dea2d
1 /* Driver of optimization process
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
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
15 for more details.
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:
28 - finalize_function
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
34 function.)
36 - varpool_finalize_decl
38 This function has same behavior as the above but is used for static
39 variables.
41 - add_asm_node
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.
60 - compile
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
66 indicated bellow).
68 Compile time:
70 1) Inter-procedural optimization.
71 (ipa_passes)
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
90 analysis.
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
105 available.
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
116 again.
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
126 3) IP transformation
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.
135 5) Expansion
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
148 backend.
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
160 #include "config.h"
161 #include "system.h"
162 #include "coretypes.h"
163 #include "backend.h"
164 #include "target.h"
165 #include "rtl.h"
166 #include "tree.h"
167 #include "gimple.h"
168 #include "cfghooks.h"
169 #include "regset.h" /* FIXME: For reg_obstack. */
170 #include "alloc-pool.h"
171 #include "tree-pass.h"
172 #include "stringpool.h"
173 #include "gimple-ssa.h"
174 #include "cgraph.h"
175 #include "coverage.h"
176 #include "lto-streamer.h"
177 #include "fold-const.h"
178 #include "varasm.h"
179 #include "stor-layout.h"
180 #include "output.h"
181 #include "cfgcleanup.h"
182 #include "gimple-fold.h"
183 #include "gimplify.h"
184 #include "gimple-iterator.h"
185 #include "gimplify-me.h"
186 #include "tree-cfg.h"
187 #include "tree-into-ssa.h"
188 #include "tree-ssa.h"
189 #include "langhooks.h"
190 #include "toplev.h"
191 #include "debug.h"
192 #include "symbol-summary.h"
193 #include "tree-vrp.h"
194 #include "ipa-prop.h"
195 #include "gimple-pretty-print.h"
196 #include "plugin.h"
197 #include "ipa-inline.h"
198 #include "ipa-utils.h"
199 #include "except.h"
200 #include "cfgloop.h"
201 #include "context.h"
202 #include "pass_manager.h"
203 #include "tree-nested.h"
204 #include "dbgcnt.h"
205 #include "tree-chkp.h"
206 #include "lto-section-names.h"
208 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
209 secondary queue used during optimization to accommodate passes that
210 may generate new functions that need to be optimized and expanded. */
211 vec<cgraph_node *> cgraph_new_nodes;
213 static void expand_all_functions (void);
214 static void mark_functions_to_output (void);
215 static void handle_alias_pairs (void);
217 /* Used for vtable lookup in thunk adjusting. */
218 static GTY (()) tree vtable_entry_type;
220 /* Determine if symbol declaration is needed. That is, visible to something
221 either outside this translation unit, something magic in the system
222 configury */
223 bool
224 symtab_node::needed_p (void)
226 /* Double check that no one output the function into assembly file
227 early. */
228 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
229 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
231 if (!definition)
232 return false;
234 if (DECL_EXTERNAL (decl))
235 return false;
237 /* If the user told us it is used, then it must be so. */
238 if (force_output)
239 return true;
241 /* ABI forced symbols are needed when they are external. */
242 if (forced_by_abi && TREE_PUBLIC (decl))
243 return true;
245 /* Keep constructors, destructors and virtual functions. */
246 if (TREE_CODE (decl) == FUNCTION_DECL
247 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
248 return true;
250 /* Externally visible variables must be output. The exception is
251 COMDAT variables that must be output only when they are needed. */
252 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
253 return true;
255 return false;
258 /* Head and terminator of the queue of nodes to be processed while building
259 callgraph. */
261 static symtab_node symtab_terminator;
262 static symtab_node *queued_nodes = &symtab_terminator;
264 /* Add NODE to queue starting at QUEUED_NODES.
265 The queue is linked via AUX pointers and terminated by pointer to 1. */
267 static void
268 enqueue_node (symtab_node *node)
270 if (node->aux)
271 return;
272 gcc_checking_assert (queued_nodes);
273 node->aux = queued_nodes;
274 queued_nodes = node;
277 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
278 functions into callgraph in a way so they look like ordinary reachable
279 functions inserted into callgraph already at construction time. */
281 void
282 symbol_table::process_new_functions (void)
284 tree fndecl;
286 if (!cgraph_new_nodes.exists ())
287 return;
289 handle_alias_pairs ();
290 /* Note that this queue may grow as its being processed, as the new
291 functions may generate new ones. */
292 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
294 cgraph_node *node = cgraph_new_nodes[i];
295 fndecl = node->decl;
296 switch (state)
298 case CONSTRUCTION:
299 /* At construction time we just need to finalize function and move
300 it into reachable functions list. */
302 cgraph_node::finalize_function (fndecl, false);
303 call_cgraph_insertion_hooks (node);
304 enqueue_node (node);
305 break;
307 case IPA:
308 case IPA_SSA:
309 case IPA_SSA_AFTER_INLINING:
310 /* When IPA optimization already started, do all essential
311 transformations that has been already performed on the whole
312 cgraph but not on this function. */
314 gimple_register_cfg_hooks ();
315 if (!node->analyzed)
316 node->analyze ();
317 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
318 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
319 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
320 g->get_passes ()->execute_early_local_passes ();
321 else if (inline_summaries != NULL)
322 compute_inline_parameters (node, true);
323 free_dominance_info (CDI_POST_DOMINATORS);
324 free_dominance_info (CDI_DOMINATORS);
325 pop_cfun ();
326 call_cgraph_insertion_hooks (node);
327 break;
329 case EXPANSION:
330 /* Functions created during expansion shall be compiled
331 directly. */
332 node->process = 0;
333 call_cgraph_insertion_hooks (node);
334 node->expand ();
335 break;
337 default:
338 gcc_unreachable ();
339 break;
343 cgraph_new_nodes.release ();
346 /* As an GCC extension we allow redefinition of the function. The
347 semantics when both copies of bodies differ is not well defined.
348 We replace the old body with new body so in unit at a time mode
349 we always use new body, while in normal mode we may end up with
350 old body inlined into some functions and new body expanded and
351 inlined in others.
353 ??? It may make more sense to use one body for inlining and other
354 body for expanding the function but this is difficult to do. */
356 void
357 cgraph_node::reset (void)
359 /* If process is set, then we have already begun whole-unit analysis.
360 This is *not* testing for whether we've already emitted the function.
361 That case can be sort-of legitimately seen with real function redefinition
362 errors. I would argue that the front end should never present us with
363 such a case, but don't enforce that for now. */
364 gcc_assert (!process);
366 /* Reset our data structures so we can analyze the function again. */
367 memset (&local, 0, sizeof (local));
368 memset (&global, 0, sizeof (global));
369 memset (&rtl, 0, sizeof (rtl));
370 analyzed = false;
371 definition = false;
372 alias = false;
373 transparent_alias = false;
374 weakref = false;
375 cpp_implicit_alias = false;
377 remove_callees ();
378 remove_all_references ();
381 /* Return true when there are references to the node. INCLUDE_SELF is
382 true if a self reference counts as a reference. */
384 bool
385 symtab_node::referred_to_p (bool include_self)
387 ipa_ref *ref = NULL;
389 /* See if there are any references at all. */
390 if (iterate_referring (0, ref))
391 return true;
392 /* For functions check also calls. */
393 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
394 if (cn && cn->callers)
396 if (include_self)
397 return true;
398 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
399 if (e->caller != this)
400 return true;
402 return false;
405 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
406 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
407 the garbage collector run at the moment. We would need to either create
408 a new GC context, or just not compile right now. */
410 void
411 cgraph_node::finalize_function (tree decl, bool no_collect)
413 cgraph_node *node = cgraph_node::get_create (decl);
415 if (node->definition)
417 /* Nested functions should only be defined once. */
418 gcc_assert (!DECL_CONTEXT (decl)
419 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
420 node->reset ();
421 node->local.redefined_extern_inline = true;
424 /* Set definition first before calling notice_global_symbol so that
425 it is available to notice_global_symbol. */
426 node->definition = true;
427 notice_global_symbol (decl);
428 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
430 /* With -fkeep-inline-functions we are keeping all inline functions except
431 for extern inline ones. */
432 if (flag_keep_inline_functions
433 && DECL_DECLARED_INLINE_P (decl)
434 && !DECL_EXTERNAL (decl)
435 && !DECL_DISREGARD_INLINE_LIMITS (decl))
436 node->force_output = 1;
438 /* When not optimizing, also output the static functions. (see
439 PR24561), but don't do so for always_inline functions, functions
440 declared inline and nested functions. These were optimized out
441 in the original implementation and it is unclear whether we want
442 to change the behavior here. */
443 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions)
444 && !node->cpp_implicit_alias
445 && !DECL_DISREGARD_INLINE_LIMITS (decl)
446 && !DECL_DECLARED_INLINE_P (decl)
447 && !(DECL_CONTEXT (decl)
448 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
449 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
450 node->force_output = 1;
452 /* If we've not yet emitted decl, tell the debug info about it. */
453 if (!TREE_ASM_WRITTEN (decl))
454 (*debug_hooks->deferred_inline_function) (decl);
456 if (!no_collect)
457 ggc_collect ();
459 if (symtab->state == CONSTRUCTION
460 && (node->needed_p () || node->referred_to_p ()))
461 enqueue_node (node);
464 /* Add the function FNDECL to the call graph.
465 Unlike finalize_function, this function is intended to be used
466 by middle end and allows insertion of new function at arbitrary point
467 of compilation. The function can be either in high, low or SSA form
468 GIMPLE.
470 The function is assumed to be reachable and have address taken (so no
471 API breaking optimizations are performed on it).
473 Main work done by this function is to enqueue the function for later
474 processing to avoid need the passes to be re-entrant. */
476 void
477 cgraph_node::add_new_function (tree fndecl, bool lowered)
479 gcc::pass_manager *passes = g->get_passes ();
480 cgraph_node *node;
482 if (dump_file)
484 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
485 const char *function_type = ((gimple_has_body_p (fndecl))
486 ? (lowered
487 ? (gimple_in_ssa_p (fn)
488 ? "ssa gimple"
489 : "low gimple")
490 : "high gimple")
491 : "to-be-gimplified");
492 fprintf (dump_file,
493 "Added new %s function %s to callgraph\n",
494 function_type,
495 fndecl_name (fndecl));
498 switch (symtab->state)
500 case PARSING:
501 cgraph_node::finalize_function (fndecl, false);
502 break;
503 case CONSTRUCTION:
504 /* Just enqueue function to be processed at nearest occurrence. */
505 node = cgraph_node::get_create (fndecl);
506 if (lowered)
507 node->lowered = true;
508 cgraph_new_nodes.safe_push (node);
509 break;
511 case IPA:
512 case IPA_SSA:
513 case IPA_SSA_AFTER_INLINING:
514 case EXPANSION:
515 /* Bring the function into finalized state and enqueue for later
516 analyzing and compilation. */
517 node = cgraph_node::get_create (fndecl);
518 node->local.local = false;
519 node->definition = true;
520 node->force_output = true;
521 if (!lowered && symtab->state == EXPANSION)
523 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
524 gimple_register_cfg_hooks ();
525 bitmap_obstack_initialize (NULL);
526 execute_pass_list (cfun, passes->all_lowering_passes);
527 passes->execute_early_local_passes ();
528 bitmap_obstack_release (NULL);
529 pop_cfun ();
531 lowered = true;
533 if (lowered)
534 node->lowered = true;
535 cgraph_new_nodes.safe_push (node);
536 break;
538 case FINISHED:
539 /* At the very end of compilation we have to do all the work up
540 to expansion. */
541 node = cgraph_node::create (fndecl);
542 if (lowered)
543 node->lowered = true;
544 node->definition = true;
545 node->analyze ();
546 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
547 gimple_register_cfg_hooks ();
548 bitmap_obstack_initialize (NULL);
549 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
550 g->get_passes ()->execute_early_local_passes ();
551 bitmap_obstack_release (NULL);
552 pop_cfun ();
553 node->expand ();
554 break;
556 default:
557 gcc_unreachable ();
560 /* Set a personality if required and we already passed EH lowering. */
561 if (lowered
562 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
563 == eh_personality_lang))
564 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
567 /* Analyze the function scheduled to be output. */
568 void
569 cgraph_node::analyze (void)
571 tree decl = this->decl;
572 location_t saved_loc = input_location;
573 input_location = DECL_SOURCE_LOCATION (decl);
575 if (thunk.thunk_p)
577 cgraph_node *t = cgraph_node::get (thunk.alias);
579 create_edge (t, NULL, 0, CGRAPH_FREQ_BASE);
580 callees->can_throw_external = !TREE_NOTHROW (t->decl);
581 /* Target code in expand_thunk may need the thunk's target
582 to be analyzed, so recurse here. */
583 if (!t->analyzed)
584 t->analyze ();
585 if (t->alias)
587 t = t->get_alias_target ();
588 if (!t->analyzed)
589 t->analyze ();
591 if (!expand_thunk (false, false))
593 thunk.alias = NULL;
594 return;
596 thunk.alias = NULL;
598 if (alias)
599 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
600 else if (dispatcher_function)
602 /* Generate the dispatcher body of multi-versioned functions. */
603 cgraph_function_version_info *dispatcher_version_info
604 = function_version ();
605 if (dispatcher_version_info != NULL
606 && (dispatcher_version_info->dispatcher_resolver
607 == NULL_TREE))
609 tree resolver = NULL_TREE;
610 gcc_assert (targetm.generate_version_dispatcher_body);
611 resolver = targetm.generate_version_dispatcher_body (this);
612 gcc_assert (resolver != NULL_TREE);
615 else
617 push_cfun (DECL_STRUCT_FUNCTION (decl));
619 assign_assembler_name_if_neeeded (decl);
621 /* Make sure to gimplify bodies only once. During analyzing a
622 function we lower it, which will require gimplified nested
623 functions, so we can end up here with an already gimplified
624 body. */
625 if (!gimple_has_body_p (decl))
626 gimplify_function_tree (decl);
628 /* Lower the function. */
629 if (!lowered)
631 if (nested)
632 lower_nested_functions (decl);
633 gcc_assert (!nested);
635 gimple_register_cfg_hooks ();
636 bitmap_obstack_initialize (NULL);
637 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
638 free_dominance_info (CDI_POST_DOMINATORS);
639 free_dominance_info (CDI_DOMINATORS);
640 compact_blocks ();
641 bitmap_obstack_release (NULL);
642 lowered = true;
645 pop_cfun ();
647 analyzed = true;
649 input_location = saved_loc;
652 /* C++ frontend produce same body aliases all over the place, even before PCH
653 gets streamed out. It relies on us linking the aliases with their function
654 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
655 first produce aliases without links, but once C++ FE is sure he won't sream
656 PCH we build the links via this function. */
658 void
659 symbol_table::process_same_body_aliases (void)
661 symtab_node *node;
662 FOR_EACH_SYMBOL (node)
663 if (node->cpp_implicit_alias && !node->analyzed)
664 node->resolve_alias
665 (VAR_P (node->alias_target)
666 ? (symtab_node *)varpool_node::get_create (node->alias_target)
667 : (symtab_node *)cgraph_node::get_create (node->alias_target));
668 cpp_implicit_aliases_done = true;
671 /* Process attributes common for vars and functions. */
673 static void
674 process_common_attributes (symtab_node *node, tree decl)
676 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
678 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
680 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
681 "%<weakref%> attribute should be accompanied with"
682 " an %<alias%> attribute");
683 DECL_WEAK (decl) = 0;
684 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
685 DECL_ATTRIBUTES (decl));
688 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
689 node->no_reorder = 1;
692 /* Look for externally_visible and used attributes and mark cgraph nodes
693 accordingly.
695 We cannot mark the nodes at the point the attributes are processed (in
696 handle_*_attribute) because the copy of the declarations available at that
697 point may not be canonical. For example, in:
699 void f();
700 void f() __attribute__((used));
702 the declaration we see in handle_used_attribute will be the second
703 declaration -- but the front end will subsequently merge that declaration
704 with the original declaration and discard the second declaration.
706 Furthermore, we can't mark these nodes in finalize_function because:
708 void f() {}
709 void f() __attribute__((externally_visible));
711 is valid.
713 So, we walk the nodes at the end of the translation unit, applying the
714 attributes at that point. */
716 static void
717 process_function_and_variable_attributes (cgraph_node *first,
718 varpool_node *first_var)
720 cgraph_node *node;
721 varpool_node *vnode;
723 for (node = symtab->first_function (); node != first;
724 node = symtab->next_function (node))
726 tree decl = node->decl;
727 if (DECL_PRESERVE_P (decl))
728 node->mark_force_output ();
729 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
731 if (! TREE_PUBLIC (node->decl))
732 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
733 "%<externally_visible%>"
734 " attribute have effect only on public objects");
736 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
737 && (node->definition && !node->alias))
739 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
740 "%<weakref%> attribute ignored"
741 " because function is defined");
742 DECL_WEAK (decl) = 0;
743 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
744 DECL_ATTRIBUTES (decl));
747 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
748 && !DECL_DECLARED_INLINE_P (decl)
749 /* redefining extern inline function makes it DECL_UNINLINABLE. */
750 && !DECL_UNINLINABLE (decl))
751 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
752 "always_inline function might not be inlinable");
754 process_common_attributes (node, decl);
756 for (vnode = symtab->first_variable (); vnode != first_var;
757 vnode = symtab->next_variable (vnode))
759 tree decl = vnode->decl;
760 if (DECL_EXTERNAL (decl)
761 && DECL_INITIAL (decl))
762 varpool_node::finalize_decl (decl);
763 if (DECL_PRESERVE_P (decl))
764 vnode->force_output = true;
765 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
767 if (! TREE_PUBLIC (vnode->decl))
768 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
769 "%<externally_visible%>"
770 " attribute have effect only on public objects");
772 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
773 && vnode->definition
774 && DECL_INITIAL (decl))
776 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
777 "%<weakref%> attribute ignored"
778 " because variable is initialized");
779 DECL_WEAK (decl) = 0;
780 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
781 DECL_ATTRIBUTES (decl));
783 process_common_attributes (vnode, decl);
787 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
788 middle end to output the variable to asm file, if needed or externally
789 visible. */
791 void
792 varpool_node::finalize_decl (tree decl)
794 varpool_node *node = varpool_node::get_create (decl);
796 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
798 if (node->definition)
799 return;
800 /* Set definition first before calling notice_global_symbol so that
801 it is available to notice_global_symbol. */
802 node->definition = true;
803 notice_global_symbol (decl);
804 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
805 /* Traditionally we do not eliminate static variables when not
806 optimizing and when not doing toplevel reoder. */
807 || node->no_reorder
808 || ((!flag_toplevel_reorder
809 && !DECL_COMDAT (node->decl)
810 && !DECL_ARTIFICIAL (node->decl))))
811 node->force_output = true;
813 if (symtab->state == CONSTRUCTION
814 && (node->needed_p () || node->referred_to_p ()))
815 enqueue_node (node);
816 if (symtab->state >= IPA_SSA)
817 node->analyze ();
818 /* Some frontends produce various interface variables after compilation
819 finished. */
820 if (symtab->state == FINISHED
821 || (!flag_toplevel_reorder
822 && symtab->state == EXPANSION))
823 node->assemble_decl ();
825 if (DECL_INITIAL (decl))
826 chkp_register_var_initializer (decl);
829 /* EDGE is an polymorphic call. Mark all possible targets as reachable
830 and if there is only one target, perform trivial devirtualization.
831 REACHABLE_CALL_TARGETS collects target lists we already walked to
832 avoid udplicate work. */
834 static void
835 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
836 cgraph_edge *edge)
838 unsigned int i;
839 void *cache_token;
840 bool final;
841 vec <cgraph_node *>targets
842 = possible_polymorphic_call_targets
843 (edge, &final, &cache_token);
845 if (!reachable_call_targets->add (cache_token))
847 if (symtab->dump_file)
848 dump_possible_polymorphic_call_targets
849 (symtab->dump_file, edge);
851 for (i = 0; i < targets.length (); i++)
853 /* Do not bother to mark virtual methods in anonymous namespace;
854 either we will find use of virtual table defining it, or it is
855 unused. */
856 if (targets[i]->definition
857 && TREE_CODE
858 (TREE_TYPE (targets[i]->decl))
859 == METHOD_TYPE
860 && !type_in_anonymous_namespace_p
861 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
862 enqueue_node (targets[i]);
866 /* Very trivial devirtualization; when the type is
867 final or anonymous (so we know all its derivation)
868 and there is only one possible virtual call target,
869 make the edge direct. */
870 if (final)
872 if (targets.length () <= 1 && dbg_cnt (devirt))
874 cgraph_node *target;
875 if (targets.length () == 1)
876 target = targets[0];
877 else
878 target = cgraph_node::create
879 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
881 if (symtab->dump_file)
883 fprintf (symtab->dump_file,
884 "Devirtualizing call: ");
885 print_gimple_stmt (symtab->dump_file,
886 edge->call_stmt, 0,
887 TDF_SLIM);
889 if (dump_enabled_p ())
891 location_t locus = gimple_location_safe (edge->call_stmt);
892 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
893 "devirtualizing call in %s to %s\n",
894 edge->caller->name (), target->name ());
897 edge->make_direct (target);
898 edge->redirect_call_stmt_to_callee ();
900 /* Call to __builtin_unreachable shouldn't be instrumented. */
901 if (!targets.length ())
902 gimple_call_set_with_bounds (edge->call_stmt, false);
904 if (symtab->dump_file)
906 fprintf (symtab->dump_file,
907 "Devirtualized as: ");
908 print_gimple_stmt (symtab->dump_file,
909 edge->call_stmt, 0,
910 TDF_SLIM);
916 /* Issue appropriate warnings for the global declaration DECL. */
918 static void
919 check_global_declaration (symtab_node *snode)
921 const char *decl_file;
922 tree decl = snode->decl;
924 /* Warn about any function declared static but not defined. We don't
925 warn about variables, because many programs have static variables
926 that exist only to get some text into the object file. */
927 if (TREE_CODE (decl) == FUNCTION_DECL
928 && DECL_INITIAL (decl) == 0
929 && DECL_EXTERNAL (decl)
930 && ! DECL_ARTIFICIAL (decl)
931 && ! TREE_NO_WARNING (decl)
932 && ! TREE_PUBLIC (decl)
933 && (warn_unused_function
934 || snode->referred_to_p (/*include_self=*/false)))
936 if (snode->referred_to_p (/*include_self=*/false))
937 pedwarn (input_location, 0, "%q+F used but never defined", decl);
938 else
939 warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
940 /* This symbol is effectively an "extern" declaration now. */
941 TREE_PUBLIC (decl) = 1;
944 /* Warn about static fns or vars defined but not used. */
945 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
946 || (((warn_unused_variable && ! TREE_READONLY (decl))
947 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
948 && (warn_unused_const_variable == 2
949 || (main_input_filename != NULL
950 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
951 && filename_cmp (main_input_filename,
952 decl_file) == 0))))
953 && VAR_P (decl)))
954 && ! DECL_IN_SYSTEM_HEADER (decl)
955 && ! snode->referred_to_p (/*include_self=*/false)
956 /* This TREE_USED check is needed in addition to referred_to_p
957 above, because the `__unused__' attribute is not being
958 considered for referred_to_p. */
959 && ! TREE_USED (decl)
960 /* The TREE_USED bit for file-scope decls is kept in the identifier,
961 to handle multiple external decls in different scopes. */
962 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
963 && ! DECL_EXTERNAL (decl)
964 && ! DECL_ARTIFICIAL (decl)
965 && ! DECL_ABSTRACT_ORIGIN (decl)
966 && ! TREE_PUBLIC (decl)
967 /* A volatile variable might be used in some non-obvious way. */
968 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
969 /* Global register variables must be declared to reserve them. */
970 && ! (VAR_P (decl) && DECL_REGISTER (decl))
971 /* Global ctors and dtors are called by the runtime. */
972 && (TREE_CODE (decl) != FUNCTION_DECL
973 || (!DECL_STATIC_CONSTRUCTOR (decl)
974 && !DECL_STATIC_DESTRUCTOR (decl)))
975 /* Otherwise, ask the language. */
976 && lang_hooks.decls.warn_unused_global (decl))
977 warning_at (DECL_SOURCE_LOCATION (decl),
978 (TREE_CODE (decl) == FUNCTION_DECL)
979 ? OPT_Wunused_function
980 : (TREE_READONLY (decl)
981 ? OPT_Wunused_const_variable_
982 : OPT_Wunused_variable),
983 "%qD defined but not used", decl);
986 /* Discover all functions and variables that are trivially needed, analyze
987 them as well as all functions and variables referred by them */
988 static cgraph_node *first_analyzed;
989 static varpool_node *first_analyzed_var;
991 /* FIRST_TIME is set to TRUE for the first time we are called for a
992 translation unit from finalize_compilation_unit() or false
993 otherwise. */
995 static void
996 analyze_functions (bool first_time)
998 /* Keep track of already processed nodes when called multiple times for
999 intermodule optimization. */
1000 cgraph_node *first_handled = first_analyzed;
1001 varpool_node *first_handled_var = first_analyzed_var;
1002 hash_set<void *> reachable_call_targets;
1004 symtab_node *node;
1005 symtab_node *next;
1006 int i;
1007 ipa_ref *ref;
1008 bool changed = true;
1009 location_t saved_loc = input_location;
1011 bitmap_obstack_initialize (NULL);
1012 symtab->state = CONSTRUCTION;
1013 input_location = UNKNOWN_LOCATION;
1015 /* Ugly, but the fixup can not happen at a time same body alias is created;
1016 C++ FE is confused about the COMDAT groups being right. */
1017 if (symtab->cpp_implicit_aliases_done)
1018 FOR_EACH_SYMBOL (node)
1019 if (node->cpp_implicit_alias)
1020 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1021 build_type_inheritance_graph ();
1023 /* Analysis adds static variables that in turn adds references to new functions.
1024 So we need to iterate the process until it stabilize. */
1025 while (changed)
1027 changed = false;
1028 process_function_and_variable_attributes (first_analyzed,
1029 first_analyzed_var);
1031 /* First identify the trivially needed symbols. */
1032 for (node = symtab->first_symbol ();
1033 node != first_analyzed
1034 && node != first_analyzed_var; node = node->next)
1036 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1037 node->get_comdat_group_id ();
1038 if (node->needed_p ())
1040 enqueue_node (node);
1041 if (!changed && symtab->dump_file)
1042 fprintf (symtab->dump_file, "Trivially needed symbols:");
1043 changed = true;
1044 if (symtab->dump_file)
1045 fprintf (symtab->dump_file, " %s", node->asm_name ());
1046 if (!changed && symtab->dump_file)
1047 fprintf (symtab->dump_file, "\n");
1049 if (node == first_analyzed
1050 || node == first_analyzed_var)
1051 break;
1053 symtab->process_new_functions ();
1054 first_analyzed_var = symtab->first_variable ();
1055 first_analyzed = symtab->first_function ();
1057 if (changed && symtab->dump_file)
1058 fprintf (symtab->dump_file, "\n");
1060 /* Lower representation, build callgraph edges and references for all trivially
1061 needed symbols and all symbols referred by them. */
1062 while (queued_nodes != &symtab_terminator)
1064 changed = true;
1065 node = queued_nodes;
1066 queued_nodes = (symtab_node *)queued_nodes->aux;
1067 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1068 if (cnode && cnode->definition)
1070 cgraph_edge *edge;
1071 tree decl = cnode->decl;
1073 /* ??? It is possible to create extern inline function
1074 and later using weak alias attribute to kill its body.
1075 See gcc.c-torture/compile/20011119-1.c */
1076 if (!DECL_STRUCT_FUNCTION (decl)
1077 && !cnode->alias
1078 && !cnode->thunk.thunk_p
1079 && !cnode->dispatcher_function)
1081 cnode->reset ();
1082 cnode->local.redefined_extern_inline = true;
1083 continue;
1086 if (!cnode->analyzed)
1087 cnode->analyze ();
1089 for (edge = cnode->callees; edge; edge = edge->next_callee)
1090 if (edge->callee->definition
1091 && (!DECL_EXTERNAL (edge->callee->decl)
1092 /* When not optimizing, do not try to analyze extern
1093 inline functions. Doing so is pointless. */
1094 || opt_for_fn (edge->callee->decl, optimize)
1095 /* Weakrefs needs to be preserved. */
1096 || edge->callee->alias
1097 /* always_inline functions are inlined aven at -O0. */
1098 || lookup_attribute
1099 ("always_inline",
1100 DECL_ATTRIBUTES (edge->callee->decl))
1101 /* Multiversioned functions needs the dispatcher to
1102 be produced locally even for extern functions. */
1103 || edge->callee->function_version ()))
1104 enqueue_node (edge->callee);
1105 if (opt_for_fn (cnode->decl, optimize)
1106 && opt_for_fn (cnode->decl, flag_devirtualize))
1108 cgraph_edge *next;
1110 for (edge = cnode->indirect_calls; edge; edge = next)
1112 next = edge->next_callee;
1113 if (edge->indirect_info->polymorphic)
1114 walk_polymorphic_call_targets (&reachable_call_targets,
1115 edge);
1119 /* If decl is a clone of an abstract function,
1120 mark that abstract function so that we don't release its body.
1121 The DECL_INITIAL() of that abstract function declaration
1122 will be later needed to output debug info. */
1123 if (DECL_ABSTRACT_ORIGIN (decl))
1125 cgraph_node *origin_node
1126 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1127 origin_node->used_as_abstract_origin = true;
1129 /* Preserve a functions function context node. It will
1130 later be needed to output debug info. */
1131 if (tree fn = decl_function_context (decl))
1133 cgraph_node *origin_node = cgraph_node::get_create (fn);
1134 enqueue_node (origin_node);
1137 else
1139 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1140 if (vnode && vnode->definition && !vnode->analyzed)
1141 vnode->analyze ();
1144 if (node->same_comdat_group)
1146 symtab_node *next;
1147 for (next = node->same_comdat_group;
1148 next != node;
1149 next = next->same_comdat_group)
1150 if (!next->comdat_local_p ())
1151 enqueue_node (next);
1153 for (i = 0; node->iterate_reference (i, ref); i++)
1154 if (ref->referred->definition
1155 && (!DECL_EXTERNAL (ref->referred->decl)
1156 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1157 && optimize)
1158 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1159 && opt_for_fn (ref->referred->decl, optimize))
1160 || node->alias
1161 || ref->referred->alias)))
1162 enqueue_node (ref->referred);
1163 symtab->process_new_functions ();
1166 update_type_inheritance_graph ();
1168 /* Collect entry points to the unit. */
1169 if (symtab->dump_file)
1171 fprintf (symtab->dump_file, "\n\nInitial ");
1172 symtab_node::dump_table (symtab->dump_file);
1175 if (first_time)
1177 symtab_node *snode;
1178 FOR_EACH_SYMBOL (snode)
1179 check_global_declaration (snode);
1182 if (symtab->dump_file)
1183 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1185 for (node = symtab->first_symbol ();
1186 node != first_handled
1187 && node != first_handled_var; node = next)
1189 next = node->next;
1190 if (!node->aux && !node->referred_to_p ())
1192 if (symtab->dump_file)
1193 fprintf (symtab->dump_file, " %s", node->name ());
1195 /* See if the debugger can use anything before the DECL
1196 passes away. Perhaps it can notice a DECL that is now a
1197 constant and can tag the early DIE with an appropriate
1198 attribute.
1200 Otherwise, this is the last chance the debug_hooks have
1201 at looking at optimized away DECLs, since
1202 late_global_decl will subsequently be called from the
1203 contents of the now pruned symbol table. */
1204 if (VAR_P (node->decl)
1205 && !decl_function_context (node->decl))
1207 /* We are reclaiming totally unreachable code and variables
1208 so they effectively appear as readonly. Show that to
1209 the debug machinery. */
1210 TREE_READONLY (node->decl) = 1;
1211 (*debug_hooks->late_global_decl) (node->decl);
1214 node->remove ();
1215 continue;
1217 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1219 tree decl = node->decl;
1221 if (cnode->definition && !gimple_has_body_p (decl)
1222 && !cnode->alias
1223 && !cnode->thunk.thunk_p)
1224 cnode->reset ();
1226 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1227 || cnode->alias
1228 || gimple_has_body_p (decl));
1229 gcc_assert (cnode->analyzed == cnode->definition);
1231 node->aux = NULL;
1233 for (;node; node = node->next)
1234 node->aux = NULL;
1235 first_analyzed = symtab->first_function ();
1236 first_analyzed_var = symtab->first_variable ();
1237 if (symtab->dump_file)
1239 fprintf (symtab->dump_file, "\n\nReclaimed ");
1240 symtab_node::dump_table (symtab->dump_file);
1242 bitmap_obstack_release (NULL);
1243 ggc_collect ();
1244 /* Initialize assembler name hash, in particular we want to trigger C++
1245 mangling and same body alias creation before we free DECL_ARGUMENTS
1246 used by it. */
1247 if (!seen_error ())
1248 symtab->symtab_initialize_asm_name_hash ();
1250 input_location = saved_loc;
1253 /* Translate the ugly representation of aliases as alias pairs into nice
1254 representation in callgraph. We don't handle all cases yet,
1255 unfortunately. */
1257 static void
1258 handle_alias_pairs (void)
1260 alias_pair *p;
1261 unsigned i;
1263 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1265 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1267 /* Weakrefs with target not defined in current unit are easy to handle:
1268 they behave just as external variables except we need to note the
1269 alias flag to later output the weakref pseudo op into asm file. */
1270 if (!target_node
1271 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1273 symtab_node *node = symtab_node::get (p->decl);
1274 if (node)
1276 node->alias_target = p->target;
1277 node->weakref = true;
1278 node->alias = true;
1279 node->transparent_alias = true;
1281 alias_pairs->unordered_remove (i);
1282 continue;
1284 else if (!target_node)
1286 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1287 symtab_node *node = symtab_node::get (p->decl);
1288 if (node)
1289 node->alias = false;
1290 alias_pairs->unordered_remove (i);
1291 continue;
1294 if (DECL_EXTERNAL (target_node->decl)
1295 /* We use local aliases for C++ thunks to force the tailcall
1296 to bind locally. This is a hack - to keep it working do
1297 the following (which is not strictly correct). */
1298 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1299 || ! DECL_VIRTUAL_P (target_node->decl))
1300 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1302 error ("%q+D aliased to external symbol %qE",
1303 p->decl, p->target);
1306 if (TREE_CODE (p->decl) == FUNCTION_DECL
1307 && target_node && is_a <cgraph_node *> (target_node))
1309 cgraph_node *src_node = cgraph_node::get (p->decl);
1310 if (src_node && src_node->definition)
1311 src_node->reset ();
1312 cgraph_node::create_alias (p->decl, target_node->decl);
1313 alias_pairs->unordered_remove (i);
1315 else if (VAR_P (p->decl)
1316 && target_node && is_a <varpool_node *> (target_node))
1318 varpool_node::create_alias (p->decl, target_node->decl);
1319 alias_pairs->unordered_remove (i);
1321 else
1323 error ("%q+D alias in between function and variable is not supported",
1324 p->decl);
1325 warning (0, "%q+D aliased declaration",
1326 target_node->decl);
1327 alias_pairs->unordered_remove (i);
1330 vec_free (alias_pairs);
1334 /* Figure out what functions we want to assemble. */
1336 static void
1337 mark_functions_to_output (void)
1339 bool check_same_comdat_groups = false;
1340 cgraph_node *node;
1342 if (flag_checking)
1343 FOR_EACH_FUNCTION (node)
1344 gcc_assert (!node->process);
1346 FOR_EACH_FUNCTION (node)
1348 tree decl = node->decl;
1350 gcc_assert (!node->process || node->same_comdat_group);
1351 if (node->process)
1352 continue;
1354 /* We need to output all local functions that are used and not
1355 always inlined, as well as those that are reachable from
1356 outside the current compilation unit. */
1357 if (node->analyzed
1358 && !node->thunk.thunk_p
1359 && !node->alias
1360 && !node->global.inlined_to
1361 && !TREE_ASM_WRITTEN (decl)
1362 && !DECL_EXTERNAL (decl))
1364 node->process = 1;
1365 if (node->same_comdat_group)
1367 cgraph_node *next;
1368 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1369 next != node;
1370 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1371 if (!next->thunk.thunk_p && !next->alias
1372 && !next->comdat_local_p ())
1373 next->process = 1;
1376 else if (node->same_comdat_group)
1378 if (flag_checking)
1379 check_same_comdat_groups = true;
1381 else
1383 /* We should've reclaimed all functions that are not needed. */
1384 if (flag_checking
1385 && !node->global.inlined_to
1386 && gimple_has_body_p (decl)
1387 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1388 are inside partition, we can end up not removing the body since we no longer
1389 have analyzed node pointing to it. */
1390 && !node->in_other_partition
1391 && !node->alias
1392 && !node->clones
1393 && !DECL_EXTERNAL (decl))
1395 node->debug ();
1396 internal_error ("failed to reclaim unneeded function");
1398 gcc_assert (node->global.inlined_to
1399 || !gimple_has_body_p (decl)
1400 || node->in_other_partition
1401 || node->clones
1402 || DECL_ARTIFICIAL (decl)
1403 || DECL_EXTERNAL (decl));
1408 if (flag_checking && check_same_comdat_groups)
1409 FOR_EACH_FUNCTION (node)
1410 if (node->same_comdat_group && !node->process)
1412 tree decl = node->decl;
1413 if (!node->global.inlined_to
1414 && gimple_has_body_p (decl)
1415 /* FIXME: in an ltrans unit when the offline copy is outside a
1416 partition but inline copies are inside a partition, we can
1417 end up not removing the body since we no longer have an
1418 analyzed node pointing to it. */
1419 && !node->in_other_partition
1420 && !node->clones
1421 && !DECL_EXTERNAL (decl))
1423 node->debug ();
1424 internal_error ("failed to reclaim unneeded function in same "
1425 "comdat group");
1430 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1431 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1433 Set current_function_decl and cfun to newly constructed empty function body.
1434 return basic block in the function body. */
1436 basic_block
1437 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1439 basic_block bb;
1440 edge e;
1442 current_function_decl = decl;
1443 allocate_struct_function (decl, false);
1444 gimple_register_cfg_hooks ();
1445 init_empty_tree_cfg ();
1446 init_tree_ssa (cfun);
1448 if (in_ssa)
1450 init_ssa_operands (cfun);
1451 cfun->gimple_df->in_ssa_p = true;
1452 cfun->curr_properties |= PROP_ssa;
1455 DECL_INITIAL (decl) = make_node (BLOCK);
1456 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1458 DECL_SAVED_TREE (decl) = error_mark_node;
1459 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1460 | PROP_cfg | PROP_loops);
1462 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1463 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1464 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1466 /* Create BB for body of the function and connect it properly. */
1467 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1468 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1469 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1470 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1471 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1472 bb->count = count;
1473 bb->frequency = BB_FREQ_MAX;
1474 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1475 e->count = count;
1476 e->probability = REG_BR_PROB_BASE;
1477 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1478 e->count = count;
1479 e->probability = REG_BR_PROB_BASE;
1480 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1482 return bb;
1485 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1486 offset indicated by VIRTUAL_OFFSET, if that is
1487 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1488 zero for a result adjusting thunk. */
1490 tree
1491 thunk_adjust (gimple_stmt_iterator * bsi,
1492 tree ptr, bool this_adjusting,
1493 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1495 gassign *stmt;
1496 tree ret;
1498 if (this_adjusting
1499 && fixed_offset != 0)
1501 stmt = gimple_build_assign
1502 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1503 ptr,
1504 fixed_offset));
1505 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1508 /* If there's a virtual offset, look up that value in the vtable and
1509 adjust the pointer again. */
1510 if (virtual_offset)
1512 tree vtabletmp;
1513 tree vtabletmp2;
1514 tree vtabletmp3;
1516 if (!vtable_entry_type)
1518 tree vfunc_type = make_node (FUNCTION_TYPE);
1519 TREE_TYPE (vfunc_type) = integer_type_node;
1520 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1521 layout_type (vfunc_type);
1523 vtable_entry_type = build_pointer_type (vfunc_type);
1526 vtabletmp =
1527 create_tmp_reg (build_pointer_type
1528 (build_pointer_type (vtable_entry_type)), "vptr");
1530 /* The vptr is always at offset zero in the object. */
1531 stmt = gimple_build_assign (vtabletmp,
1532 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1533 ptr));
1534 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1536 /* Form the vtable address. */
1537 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1538 "vtableaddr");
1539 stmt = gimple_build_assign (vtabletmp2,
1540 build_simple_mem_ref (vtabletmp));
1541 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1543 /* Find the entry with the vcall offset. */
1544 stmt = gimple_build_assign (vtabletmp2,
1545 fold_build_pointer_plus_loc (input_location,
1546 vtabletmp2,
1547 virtual_offset));
1548 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1550 /* Get the offset itself. */
1551 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1552 "vcalloffset");
1553 stmt = gimple_build_assign (vtabletmp3,
1554 build_simple_mem_ref (vtabletmp2));
1555 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1557 /* Adjust the `this' pointer. */
1558 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1559 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1560 GSI_CONTINUE_LINKING);
1563 if (!this_adjusting
1564 && fixed_offset != 0)
1565 /* Adjust the pointer by the constant. */
1567 tree ptrtmp;
1569 if (VAR_P (ptr))
1570 ptrtmp = ptr;
1571 else
1573 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1574 stmt = gimple_build_assign (ptrtmp, ptr);
1575 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1577 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1578 ptrtmp, fixed_offset);
1581 /* Emit the statement and gimplify the adjustment expression. */
1582 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1583 stmt = gimple_build_assign (ret, ptr);
1584 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1586 return ret;
1589 /* Expand thunk NODE to gimple if possible.
1590 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1591 no assembler is produced.
1592 When OUTPUT_ASM_THUNK is true, also produce assembler for
1593 thunks that are not lowered. */
1595 bool
1596 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1598 bool this_adjusting = thunk.this_adjusting;
1599 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1600 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1601 tree virtual_offset = NULL;
1602 tree alias = callees->callee->decl;
1603 tree thunk_fndecl = decl;
1604 tree a;
1606 /* Instrumentation thunk is the same function with
1607 a different signature. Never need to expand it. */
1608 if (thunk.add_pointer_bounds_args)
1609 return false;
1611 if (!force_gimple_thunk && this_adjusting
1612 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1613 virtual_value, alias))
1615 const char *fnname;
1616 tree fn_block;
1617 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1619 if (!output_asm_thunks)
1621 analyzed = true;
1622 return false;
1625 if (in_lto_p)
1626 get_untransformed_body ();
1627 a = DECL_ARGUMENTS (thunk_fndecl);
1629 current_function_decl = thunk_fndecl;
1631 /* Ensure thunks are emitted in their correct sections. */
1632 resolve_unique_section (thunk_fndecl, 0,
1633 flag_function_sections);
1635 DECL_RESULT (thunk_fndecl)
1636 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1637 RESULT_DECL, 0, restype);
1638 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1639 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1641 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1642 create one. */
1643 fn_block = make_node (BLOCK);
1644 BLOCK_VARS (fn_block) = a;
1645 DECL_INITIAL (thunk_fndecl) = fn_block;
1646 BLOCK_SUPERCONTEXT (fn_block) = thunk_fndecl;
1647 allocate_struct_function (thunk_fndecl, false);
1648 init_function_start (thunk_fndecl);
1649 cfun->is_thunk = 1;
1650 insn_locations_init ();
1651 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1652 prologue_location = curr_insn_location ();
1653 assemble_start_function (thunk_fndecl, fnname);
1655 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1656 fixed_offset, virtual_value, alias);
1658 assemble_end_function (thunk_fndecl, fnname);
1659 insn_locations_finalize ();
1660 init_insn_lengths ();
1661 free_after_compilation (cfun);
1662 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1663 thunk.thunk_p = false;
1664 analyzed = false;
1666 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1668 error ("generic thunk code fails for method %qD which uses %<...%>",
1669 thunk_fndecl);
1670 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1671 analyzed = true;
1672 return false;
1674 else
1676 tree restype;
1677 basic_block bb, then_bb, else_bb, return_bb;
1678 gimple_stmt_iterator bsi;
1679 int nargs = 0;
1680 tree arg;
1681 int i;
1682 tree resdecl;
1683 tree restmp = NULL;
1684 tree resbnd = NULL;
1686 gcall *call;
1687 greturn *ret;
1688 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1690 /* We may be called from expand_thunk that releses body except for
1691 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1692 if (in_lto_p && !force_gimple_thunk)
1693 get_untransformed_body ();
1694 a = DECL_ARGUMENTS (thunk_fndecl);
1696 current_function_decl = thunk_fndecl;
1698 /* Ensure thunks are emitted in their correct sections. */
1699 resolve_unique_section (thunk_fndecl, 0,
1700 flag_function_sections);
1702 DECL_IGNORED_P (thunk_fndecl) = 1;
1703 bitmap_obstack_initialize (NULL);
1705 if (thunk.virtual_offset_p)
1706 virtual_offset = size_int (virtual_value);
1708 /* Build the return declaration for the function. */
1709 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1710 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1712 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1713 DECL_ARTIFICIAL (resdecl) = 1;
1714 DECL_IGNORED_P (resdecl) = 1;
1715 DECL_RESULT (thunk_fndecl) = resdecl;
1716 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1718 else
1719 resdecl = DECL_RESULT (thunk_fndecl);
1721 bb = then_bb = else_bb = return_bb
1722 = init_lowered_empty_function (thunk_fndecl, true, count);
1724 bsi = gsi_start_bb (bb);
1726 /* Build call to the function being thunked. */
1727 if (!VOID_TYPE_P (restype)
1728 && (!alias_is_noreturn
1729 || TREE_ADDRESSABLE (restype)
1730 || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST))
1732 if (DECL_BY_REFERENCE (resdecl))
1734 restmp = gimple_fold_indirect_ref (resdecl);
1735 if (!restmp)
1736 restmp = build2 (MEM_REF,
1737 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1738 resdecl,
1739 build_int_cst (TREE_TYPE
1740 (DECL_RESULT (alias)), 0));
1742 else if (!is_gimple_reg_type (restype))
1744 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1746 restmp = resdecl;
1748 if (VAR_P (restmp))
1749 add_local_decl (cfun, restmp);
1750 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1752 else
1753 restmp = create_tmp_var (restype, "retval");
1755 else
1756 restmp = create_tmp_reg (restype, "retval");
1759 for (arg = a; arg; arg = DECL_CHAIN (arg))
1760 nargs++;
1761 auto_vec<tree> vargs (nargs);
1762 i = 0;
1763 arg = a;
1764 if (this_adjusting)
1766 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1767 virtual_offset));
1768 arg = DECL_CHAIN (a);
1769 i = 1;
1772 if (nargs)
1773 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1775 tree tmp = arg;
1776 if (!is_gimple_val (arg))
1778 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1779 (TREE_TYPE (arg)), "arg");
1780 gimple *stmt = gimple_build_assign (tmp, arg);
1781 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1783 vargs.quick_push (tmp);
1785 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1786 callees->call_stmt = call;
1787 gimple_call_set_from_thunk (call, true);
1788 gimple_call_set_with_bounds (call, instrumentation_clone);
1790 /* Return slot optimization is always possible and in fact requred to
1791 return values with DECL_BY_REFERENCE. */
1792 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1793 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1794 || DECL_BY_REFERENCE (resdecl)))
1795 gimple_call_set_return_slot_opt (call, true);
1797 if (restmp)
1799 gimple_call_set_lhs (call, restmp);
1800 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1801 TREE_TYPE (TREE_TYPE (alias))));
1803 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1804 if (!alias_is_noreturn)
1806 if (instrumentation_clone
1807 && !DECL_BY_REFERENCE (resdecl)
1808 && restmp
1809 && BOUNDED_P (restmp))
1811 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1812 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1813 as_a <gcall *> (gsi_stmt (bsi)),
1814 callees->count, callees->frequency);
1817 if (restmp && !this_adjusting
1818 && (fixed_offset || virtual_offset))
1820 tree true_label = NULL_TREE;
1822 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1824 gimple *stmt;
1825 edge e;
1826 /* If the return type is a pointer, we need to
1827 protect against NULL. We know there will be an
1828 adjustment, because that's why we're emitting a
1829 thunk. */
1830 then_bb = create_basic_block (NULL, bb);
1831 then_bb->count = count - count / 16;
1832 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1833 return_bb = create_basic_block (NULL, then_bb);
1834 return_bb->count = count;
1835 return_bb->frequency = BB_FREQ_MAX;
1836 else_bb = create_basic_block (NULL, else_bb);
1837 then_bb->count = count / 16;
1838 then_bb->frequency = BB_FREQ_MAX / 16;
1839 add_bb_to_loop (then_bb, bb->loop_father);
1840 add_bb_to_loop (return_bb, bb->loop_father);
1841 add_bb_to_loop (else_bb, bb->loop_father);
1842 remove_edge (single_succ_edge (bb));
1843 true_label = gimple_block_label (then_bb);
1844 stmt = gimple_build_cond (NE_EXPR, restmp,
1845 build_zero_cst (TREE_TYPE (restmp)),
1846 NULL_TREE, NULL_TREE);
1847 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1848 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1849 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1850 e->count = count - count / 16;
1851 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1852 e->probability = REG_BR_PROB_BASE / 16;
1853 e->count = count / 16;
1854 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1855 e->probability = REG_BR_PROB_BASE;
1856 e->count = count;
1857 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1858 e->probability = REG_BR_PROB_BASE;
1859 e->count = count - count / 16;
1860 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1861 e->probability = REG_BR_PROB_BASE;
1862 e->count = count / 16;
1863 bsi = gsi_last_bb (then_bb);
1866 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1867 fixed_offset, virtual_offset);
1868 if (true_label)
1870 gimple *stmt;
1871 bsi = gsi_last_bb (else_bb);
1872 stmt = gimple_build_assign (restmp,
1873 build_zero_cst (TREE_TYPE (restmp)));
1874 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1875 bsi = gsi_last_bb (return_bb);
1878 else
1879 gimple_call_set_tail (call, true);
1881 /* Build return value. */
1882 if (!DECL_BY_REFERENCE (resdecl))
1883 ret = gimple_build_return (restmp);
1884 else
1885 ret = gimple_build_return (resdecl);
1886 gimple_return_set_retbnd (ret, resbnd);
1888 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1890 else
1892 gimple_call_set_tail (call, true);
1893 remove_edge (single_succ_edge (bb));
1896 cfun->gimple_df->in_ssa_p = true;
1897 profile_status_for_fn (cfun)
1898 = count ? PROFILE_READ : PROFILE_GUESSED;
1899 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1900 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1901 delete_unreachable_blocks ();
1902 update_ssa (TODO_update_ssa);
1903 checking_verify_flow_info ();
1904 free_dominance_info (CDI_DOMINATORS);
1906 /* Since we want to emit the thunk, we explicitly mark its name as
1907 referenced. */
1908 thunk.thunk_p = false;
1909 lowered = true;
1910 bitmap_obstack_release (NULL);
1912 current_function_decl = NULL;
1913 set_cfun (NULL);
1914 return true;
1917 /* Assemble thunks and aliases associated to node. */
1919 void
1920 cgraph_node::assemble_thunks_and_aliases (void)
1922 cgraph_edge *e;
1923 ipa_ref *ref;
1925 for (e = callers; e;)
1926 if (e->caller->thunk.thunk_p
1927 && !e->caller->global.inlined_to
1928 && !e->caller->thunk.add_pointer_bounds_args)
1930 cgraph_node *thunk = e->caller;
1932 e = e->next_caller;
1933 thunk->expand_thunk (true, false);
1934 thunk->assemble_thunks_and_aliases ();
1936 else
1937 e = e->next_caller;
1939 FOR_EACH_ALIAS (this, ref)
1941 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1942 if (!alias->transparent_alias)
1944 bool saved_written = TREE_ASM_WRITTEN (decl);
1946 /* Force assemble_alias to really output the alias this time instead
1947 of buffering it in same alias pairs. */
1948 TREE_ASM_WRITTEN (decl) = 1;
1949 do_assemble_alias (alias->decl,
1950 DECL_ASSEMBLER_NAME (decl));
1951 alias->assemble_thunks_and_aliases ();
1952 TREE_ASM_WRITTEN (decl) = saved_written;
1957 /* Expand function specified by node. */
1959 void
1960 cgraph_node::expand (void)
1962 location_t saved_loc;
1964 /* We ought to not compile any inline clones. */
1965 gcc_assert (!global.inlined_to);
1967 announce_function (decl);
1968 process = 0;
1969 gcc_assert (lowered);
1970 get_untransformed_body ();
1972 /* Generate RTL for the body of DECL. */
1974 timevar_push (TV_REST_OF_COMPILATION);
1976 gcc_assert (symtab->global_info_ready);
1978 /* Initialize the default bitmap obstack. */
1979 bitmap_obstack_initialize (NULL);
1981 /* Initialize the RTL code for the function. */
1982 saved_loc = input_location;
1983 input_location = DECL_SOURCE_LOCATION (decl);
1985 gcc_assert (DECL_STRUCT_FUNCTION (decl));
1986 push_cfun (DECL_STRUCT_FUNCTION (decl));
1987 init_function_start (decl);
1989 gimple_register_cfg_hooks ();
1991 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1993 execute_all_ipa_transforms ();
1995 /* Perform all tree transforms and optimizations. */
1997 /* Signal the start of passes. */
1998 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
2000 execute_pass_list (cfun, g->get_passes ()->all_passes);
2002 /* Signal the end of passes. */
2003 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
2005 bitmap_obstack_release (&reg_obstack);
2007 /* Release the default bitmap obstack. */
2008 bitmap_obstack_release (NULL);
2010 /* If requested, warn about function definitions where the function will
2011 return a value (usually of some struct or union type) which itself will
2012 take up a lot of stack space. */
2013 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
2015 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
2017 if (ret_type && TYPE_SIZE_UNIT (ret_type)
2018 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
2019 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
2020 larger_than_size))
2022 unsigned int size_as_int
2023 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
2025 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
2026 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
2027 decl, size_as_int);
2028 else
2029 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
2030 decl, larger_than_size);
2034 gimple_set_body (decl, NULL);
2035 if (DECL_STRUCT_FUNCTION (decl) == 0
2036 && !cgraph_node::get (decl)->origin)
2038 /* Stop pointing to the local nodes about to be freed.
2039 But DECL_INITIAL must remain nonzero so we know this
2040 was an actual function definition.
2041 For a nested function, this is done in c_pop_function_context.
2042 If rest_of_compilation set this to 0, leave it 0. */
2043 if (DECL_INITIAL (decl) != 0)
2044 DECL_INITIAL (decl) = error_mark_node;
2047 input_location = saved_loc;
2049 ggc_collect ();
2050 timevar_pop (TV_REST_OF_COMPILATION);
2052 /* Make sure that BE didn't give up on compiling. */
2053 gcc_assert (TREE_ASM_WRITTEN (decl));
2054 if (cfun)
2055 pop_cfun ();
2057 /* It would make a lot more sense to output thunks before function body to get more
2058 forward and lest backwarding jumps. This however would need solving problem
2059 with comdats. See PR48668. Also aliases must come after function itself to
2060 make one pass assemblers, like one on AIX, happy. See PR 50689.
2061 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2062 groups. */
2063 assemble_thunks_and_aliases ();
2064 release_body ();
2065 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2066 points to the dead function body. */
2067 remove_callees ();
2068 remove_all_references ();
2071 /* Node comparer that is responsible for the order that corresponds
2072 to time when a function was launched for the first time. */
2074 static int
2075 node_cmp (const void *pa, const void *pb)
2077 const cgraph_node *a = *(const cgraph_node * const *) pa;
2078 const cgraph_node *b = *(const cgraph_node * const *) pb;
2080 /* Functions with time profile must be before these without profile. */
2081 if (!a->tp_first_run || !b->tp_first_run)
2082 return a->tp_first_run - b->tp_first_run;
2084 return a->tp_first_run != b->tp_first_run
2085 ? b->tp_first_run - a->tp_first_run
2086 : b->order - a->order;
2089 /* Expand all functions that must be output.
2091 Attempt to topologically sort the nodes so function is output when
2092 all called functions are already assembled to allow data to be
2093 propagated across the callgraph. Use a stack to get smaller distance
2094 between a function and its callees (later we may choose to use a more
2095 sophisticated algorithm for function reordering; we will likely want
2096 to use subsections to make the output functions appear in top-down
2097 order). */
2099 static void
2100 expand_all_functions (void)
2102 cgraph_node *node;
2103 cgraph_node **order = XCNEWVEC (cgraph_node *,
2104 symtab->cgraph_count);
2105 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2106 int order_pos, new_order_pos = 0;
2107 int i;
2109 order_pos = ipa_reverse_postorder (order);
2110 gcc_assert (order_pos == symtab->cgraph_count);
2112 /* Garbage collector may remove inline clones we eliminate during
2113 optimization. So we must be sure to not reference them. */
2114 for (i = 0; i < order_pos; i++)
2115 if (order[i]->process)
2116 order[new_order_pos++] = order[i];
2118 if (flag_profile_reorder_functions)
2119 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2121 for (i = new_order_pos - 1; i >= 0; i--)
2123 node = order[i];
2125 if (node->process)
2127 expanded_func_count++;
2128 if(node->tp_first_run)
2129 profiled_func_count++;
2131 if (symtab->dump_file)
2132 fprintf (symtab->dump_file,
2133 "Time profile order in expand_all_functions:%s:%d\n",
2134 node->asm_name (), node->tp_first_run);
2135 node->process = 0;
2136 node->expand ();
2140 if (dump_file)
2141 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2142 main_input_filename, profiled_func_count, expanded_func_count);
2144 if (symtab->dump_file && flag_profile_reorder_functions)
2145 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2146 profiled_func_count, expanded_func_count);
2148 symtab->process_new_functions ();
2149 free_gimplify_stack ();
2151 free (order);
2154 /* This is used to sort the node types by the cgraph order number. */
2156 enum cgraph_order_sort_kind
2158 ORDER_UNDEFINED = 0,
2159 ORDER_FUNCTION,
2160 ORDER_VAR,
2161 ORDER_VAR_UNDEF,
2162 ORDER_ASM
2165 struct cgraph_order_sort
2167 enum cgraph_order_sort_kind kind;
2168 union
2170 cgraph_node *f;
2171 varpool_node *v;
2172 asm_node *a;
2173 } u;
2176 /* Output all functions, variables, and asm statements in the order
2177 according to their order fields, which is the order in which they
2178 appeared in the file. This implements -fno-toplevel-reorder. In
2179 this mode we may output functions and variables which don't really
2180 need to be output.
2181 When NO_REORDER is true only do this for symbols marked no reorder. */
2183 static void
2184 output_in_order (bool no_reorder)
2186 int max;
2187 cgraph_order_sort *nodes;
2188 int i;
2189 cgraph_node *pf;
2190 varpool_node *pv;
2191 asm_node *pa;
2192 max = symtab->order;
2193 nodes = XCNEWVEC (cgraph_order_sort, max);
2195 FOR_EACH_DEFINED_FUNCTION (pf)
2197 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2199 if (no_reorder && !pf->no_reorder)
2200 continue;
2201 i = pf->order;
2202 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2203 nodes[i].kind = ORDER_FUNCTION;
2204 nodes[i].u.f = pf;
2208 /* There is a similar loop in symbol_table::output_variables.
2209 Please keep them in sync. */
2210 FOR_EACH_VARIABLE (pv)
2212 if (no_reorder && !pv->no_reorder)
2213 continue;
2214 if (DECL_HARD_REGISTER (pv->decl)
2215 || DECL_HAS_VALUE_EXPR_P (pv->decl))
2216 continue;
2217 i = pv->order;
2218 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2219 nodes[i].kind = pv->definition ? ORDER_VAR : ORDER_VAR_UNDEF;
2220 nodes[i].u.v = pv;
2223 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2225 i = pa->order;
2226 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2227 nodes[i].kind = ORDER_ASM;
2228 nodes[i].u.a = pa;
2231 /* In toplevel reorder mode we output all statics; mark them as needed. */
2233 for (i = 0; i < max; ++i)
2234 if (nodes[i].kind == ORDER_VAR)
2235 nodes[i].u.v->finalize_named_section_flags ();
2237 for (i = 0; i < max; ++i)
2239 switch (nodes[i].kind)
2241 case ORDER_FUNCTION:
2242 nodes[i].u.f->process = 0;
2243 nodes[i].u.f->expand ();
2244 break;
2246 case ORDER_VAR:
2247 nodes[i].u.v->assemble_decl ();
2248 break;
2250 case ORDER_VAR_UNDEF:
2251 assemble_undefined_decl (nodes[i].u.v->decl);
2252 break;
2254 case ORDER_ASM:
2255 assemble_asm (nodes[i].u.a->asm_str);
2256 break;
2258 case ORDER_UNDEFINED:
2259 break;
2261 default:
2262 gcc_unreachable ();
2266 symtab->clear_asm_symbols ();
2268 free (nodes);
2271 static void
2272 ipa_passes (void)
2274 gcc::pass_manager *passes = g->get_passes ();
2276 set_cfun (NULL);
2277 current_function_decl = NULL;
2278 gimple_register_cfg_hooks ();
2279 bitmap_obstack_initialize (NULL);
2281 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2283 if (!in_lto_p)
2285 execute_ipa_pass_list (passes->all_small_ipa_passes);
2286 if (seen_error ())
2287 return;
2290 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2291 devirtualization and other changes where removal iterate. */
2292 symtab->remove_unreachable_nodes (symtab->dump_file);
2294 /* If pass_all_early_optimizations was not scheduled, the state of
2295 the cgraph will not be properly updated. Update it now. */
2296 if (symtab->state < IPA_SSA)
2297 symtab->state = IPA_SSA;
2299 if (!in_lto_p)
2301 /* Generate coverage variables and constructors. */
2302 coverage_finish ();
2304 /* Process new functions added. */
2305 set_cfun (NULL);
2306 current_function_decl = NULL;
2307 symtab->process_new_functions ();
2309 execute_ipa_summary_passes
2310 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2313 /* Some targets need to handle LTO assembler output specially. */
2314 if (flag_generate_lto || flag_generate_offload)
2315 targetm.asm_out.lto_start ();
2317 if (!in_lto_p)
2319 if (g->have_offload)
2321 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2322 lto_stream_offload_p = true;
2323 ipa_write_summaries ();
2324 lto_stream_offload_p = false;
2326 if (flag_lto)
2328 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2329 lto_stream_offload_p = false;
2330 ipa_write_summaries ();
2334 if (flag_generate_lto || flag_generate_offload)
2335 targetm.asm_out.lto_end ();
2337 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2338 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2339 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2341 bitmap_obstack_release (NULL);
2345 /* Return string alias is alias of. */
2347 static tree
2348 get_alias_symbol (tree decl)
2350 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2351 return get_identifier (TREE_STRING_POINTER
2352 (TREE_VALUE (TREE_VALUE (alias))));
2356 /* Weakrefs may be associated to external decls and thus not output
2357 at expansion time. Emit all necessary aliases. */
2359 void
2360 symbol_table::output_weakrefs (void)
2362 symtab_node *node;
2363 cgraph_node *cnode;
2364 FOR_EACH_SYMBOL (node)
2365 if (node->alias
2366 && !TREE_ASM_WRITTEN (node->decl)
2367 && (!(cnode = dyn_cast <cgraph_node *> (node))
2368 || !cnode->instrumented_version
2369 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2370 && node->weakref)
2372 tree target;
2374 /* Weakrefs are special by not requiring target definition in current
2375 compilation unit. It is thus bit hard to work out what we want to
2376 alias.
2377 When alias target is defined, we need to fetch it from symtab reference,
2378 otherwise it is pointed to by alias_target. */
2379 if (node->alias_target)
2380 target = (DECL_P (node->alias_target)
2381 ? DECL_ASSEMBLER_NAME (node->alias_target)
2382 : node->alias_target);
2383 else if (node->analyzed)
2384 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2385 else
2387 gcc_unreachable ();
2388 target = get_alias_symbol (node->decl);
2390 do_assemble_alias (node->decl, target);
2394 /* Perform simple optimizations based on callgraph. */
2396 void
2397 symbol_table::compile (void)
2399 if (seen_error ())
2400 return;
2402 symtab_node::checking_verify_symtab_nodes ();
2404 timevar_push (TV_CGRAPHOPT);
2405 if (pre_ipa_mem_report)
2407 fprintf (stderr, "Memory consumption before IPA\n");
2408 dump_memory_report (false);
2410 if (!quiet_flag)
2411 fprintf (stderr, "Performing interprocedural optimizations\n");
2412 state = IPA;
2414 /* Offloading requires LTO infrastructure. */
2415 if (!in_lto_p && g->have_offload)
2416 flag_generate_offload = 1;
2418 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2419 if (flag_generate_lto || flag_generate_offload)
2420 lto_streamer_hooks_init ();
2422 /* Don't run the IPA passes if there was any error or sorry messages. */
2423 if (!seen_error ())
2424 ipa_passes ();
2426 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2427 if (seen_error ()
2428 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2430 timevar_pop (TV_CGRAPHOPT);
2431 return;
2434 global_info_ready = true;
2435 if (dump_file)
2437 fprintf (dump_file, "Optimized ");
2438 symtab_node:: dump_table (dump_file);
2440 if (post_ipa_mem_report)
2442 fprintf (stderr, "Memory consumption after IPA\n");
2443 dump_memory_report (false);
2445 timevar_pop (TV_CGRAPHOPT);
2447 /* Output everything. */
2448 (*debug_hooks->assembly_start) ();
2449 if (!quiet_flag)
2450 fprintf (stderr, "Assembling functions:\n");
2451 symtab_node::checking_verify_symtab_nodes ();
2453 bitmap_obstack_initialize (NULL);
2454 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2455 bitmap_obstack_release (NULL);
2456 mark_functions_to_output ();
2458 /* When weakref support is missing, we autmatically translate all
2459 references to NODE to references to its ultimate alias target.
2460 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2461 TREE_CHAIN.
2463 Set up this mapping before we output any assembler but once we are sure
2464 that all symbol renaming is done.
2466 FIXME: All this uglyness can go away if we just do renaming at gimple
2467 level by physically rewritting the IL. At the moment we can only redirect
2468 calls, so we need infrastructure for renaming references as well. */
2469 #ifndef ASM_OUTPUT_WEAKREF
2470 symtab_node *node;
2472 FOR_EACH_SYMBOL (node)
2473 if (node->alias
2474 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2476 IDENTIFIER_TRANSPARENT_ALIAS
2477 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2478 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2479 = (node->alias_target ? node->alias_target
2480 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2482 #endif
2484 state = EXPANSION;
2486 if (!flag_toplevel_reorder)
2487 output_in_order (false);
2488 else
2490 /* Output first asm statements and anything ordered. The process
2491 flag is cleared for these nodes, so we skip them later. */
2492 output_in_order (true);
2493 expand_all_functions ();
2494 output_variables ();
2497 process_new_functions ();
2498 state = FINISHED;
2499 output_weakrefs ();
2501 if (dump_file)
2503 fprintf (dump_file, "\nFinal ");
2504 symtab_node::dump_table (dump_file);
2506 if (!flag_checking)
2507 return;
2508 symtab_node::verify_symtab_nodes ();
2509 /* Double check that all inline clones are gone and that all
2510 function bodies have been released from memory. */
2511 if (!seen_error ())
2513 cgraph_node *node;
2514 bool error_found = false;
2516 FOR_EACH_DEFINED_FUNCTION (node)
2517 if (node->global.inlined_to
2518 || gimple_has_body_p (node->decl))
2520 error_found = true;
2521 node->debug ();
2523 if (error_found)
2524 internal_error ("nodes with unreleased memory found");
2529 /* Analyze the whole compilation unit once it is parsed completely. */
2531 void
2532 symbol_table::finalize_compilation_unit (void)
2534 timevar_push (TV_CGRAPH);
2536 /* If we're here there's no current function anymore. Some frontends
2537 are lazy in clearing these. */
2538 current_function_decl = NULL;
2539 set_cfun (NULL);
2541 /* Do not skip analyzing the functions if there were errors, we
2542 miss diagnostics for following functions otherwise. */
2544 /* Emit size functions we didn't inline. */
2545 finalize_size_functions ();
2547 /* Mark alias targets necessary and emit diagnostics. */
2548 handle_alias_pairs ();
2550 if (!quiet_flag)
2552 fprintf (stderr, "\nAnalyzing compilation unit\n");
2553 fflush (stderr);
2556 if (flag_dump_passes)
2557 dump_passes ();
2559 /* Gimplify and lower all functions, compute reachability and
2560 remove unreachable nodes. */
2561 analyze_functions (/*first_time=*/true);
2563 /* Mark alias targets necessary and emit diagnostics. */
2564 handle_alias_pairs ();
2566 /* Gimplify and lower thunks. */
2567 analyze_functions (/*first_time=*/false);
2569 if (!seen_error ())
2571 /* Emit early debug for reachable functions, and by consequence,
2572 locally scoped symbols. */
2573 struct cgraph_node *cnode;
2574 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2575 (*debug_hooks->early_global_decl) (cnode->decl);
2577 /* Clean up anything that needs cleaning up after initial debug
2578 generation. */
2579 (*debug_hooks->early_finish) (main_input_filename);
2582 /* Finally drive the pass manager. */
2583 compile ();
2585 timevar_pop (TV_CGRAPH);
2588 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2589 within the same process. For use by toplev::finalize. */
2591 void
2592 cgraphunit_c_finalize (void)
2594 gcc_assert (cgraph_new_nodes.length () == 0);
2595 cgraph_new_nodes.truncate (0);
2597 vtable_entry_type = NULL;
2598 queued_nodes = &symtab_terminator;
2600 first_analyzed = NULL;
2601 first_analyzed_var = NULL;
2604 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2605 kind of wrapper method. */
2607 void
2608 cgraph_node::create_wrapper (cgraph_node *target)
2610 /* Preserve DECL_RESULT so we get right by reference flag. */
2611 tree decl_result = DECL_RESULT (decl);
2613 /* Remove the function's body but keep arguments to be reused
2614 for thunk. */
2615 release_body (true);
2616 reset ();
2618 DECL_UNINLINABLE (decl) = false;
2619 DECL_RESULT (decl) = decl_result;
2620 DECL_INITIAL (decl) = NULL;
2621 allocate_struct_function (decl, false);
2622 set_cfun (NULL);
2624 /* Turn alias into thunk and expand it into GIMPLE representation. */
2625 definition = true;
2627 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2628 thunk.thunk_p = true;
2629 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2630 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2632 tree arguments = DECL_ARGUMENTS (decl);
2634 while (arguments)
2636 TREE_ADDRESSABLE (arguments) = false;
2637 arguments = TREE_CHAIN (arguments);
2640 expand_thunk (false, true);
2642 /* Inline summary set-up. */
2643 analyze ();
2644 inline_analyze_function (this);
2647 #include "gt-cgraphunit.h"