[doc] Correct optimisation levels documentation for -fstore-merging
[official-gcc.git] / gcc / cgraphunit.c
blobcc49c798515001cb792a07d7b7fe58df062543ca
1 /* Driver of optimization process
2 Copyright (C) 2003-2017 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 /* Return true if this symbol is a function from the C frontend specified
221 directly in RTL form (with "__RTL"). */
223 bool
224 symtab_node::native_rtl_p () const
226 if (TREE_CODE (decl) != FUNCTION_DECL)
227 return false;
228 if (!DECL_STRUCT_FUNCTION (decl))
229 return false;
230 return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
233 /* Determine if symbol declaration is needed. That is, visible to something
234 either outside this translation unit, something magic in the system
235 configury */
236 bool
237 symtab_node::needed_p (void)
239 /* Double check that no one output the function into assembly file
240 early. */
241 if (!native_rtl_p ())
242 gcc_checking_assert
243 (!DECL_ASSEMBLER_NAME_SET_P (decl)
244 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
246 if (!definition)
247 return false;
249 if (DECL_EXTERNAL (decl))
250 return false;
252 /* If the user told us it is used, then it must be so. */
253 if (force_output)
254 return true;
256 /* ABI forced symbols are needed when they are external. */
257 if (forced_by_abi && TREE_PUBLIC (decl))
258 return true;
260 /* Keep constructors, destructors and virtual functions. */
261 if (TREE_CODE (decl) == FUNCTION_DECL
262 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
263 return true;
265 /* Externally visible variables must be output. The exception is
266 COMDAT variables that must be output only when they are needed. */
267 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
268 return true;
270 return false;
273 /* Head and terminator of the queue of nodes to be processed while building
274 callgraph. */
276 static symtab_node symtab_terminator;
277 static symtab_node *queued_nodes = &symtab_terminator;
279 /* Add NODE to queue starting at QUEUED_NODES.
280 The queue is linked via AUX pointers and terminated by pointer to 1. */
282 static void
283 enqueue_node (symtab_node *node)
285 if (node->aux)
286 return;
287 gcc_checking_assert (queued_nodes);
288 node->aux = queued_nodes;
289 queued_nodes = node;
292 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
293 functions into callgraph in a way so they look like ordinary reachable
294 functions inserted into callgraph already at construction time. */
296 void
297 symbol_table::process_new_functions (void)
299 tree fndecl;
301 if (!cgraph_new_nodes.exists ())
302 return;
304 handle_alias_pairs ();
305 /* Note that this queue may grow as its being processed, as the new
306 functions may generate new ones. */
307 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
309 cgraph_node *node = cgraph_new_nodes[i];
310 fndecl = node->decl;
311 switch (state)
313 case CONSTRUCTION:
314 /* At construction time we just need to finalize function and move
315 it into reachable functions list. */
317 cgraph_node::finalize_function (fndecl, false);
318 call_cgraph_insertion_hooks (node);
319 enqueue_node (node);
320 break;
322 case IPA:
323 case IPA_SSA:
324 case IPA_SSA_AFTER_INLINING:
325 /* When IPA optimization already started, do all essential
326 transformations that has been already performed on the whole
327 cgraph but not on this function. */
329 gimple_register_cfg_hooks ();
330 if (!node->analyzed)
331 node->analyze ();
332 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
333 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
334 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
335 g->get_passes ()->execute_early_local_passes ();
336 else if (inline_summaries != NULL)
337 compute_inline_parameters (node, true);
338 free_dominance_info (CDI_POST_DOMINATORS);
339 free_dominance_info (CDI_DOMINATORS);
340 pop_cfun ();
341 call_cgraph_insertion_hooks (node);
342 break;
344 case EXPANSION:
345 /* Functions created during expansion shall be compiled
346 directly. */
347 node->process = 0;
348 call_cgraph_insertion_hooks (node);
349 node->expand ();
350 break;
352 default:
353 gcc_unreachable ();
354 break;
358 cgraph_new_nodes.release ();
361 /* As an GCC extension we allow redefinition of the function. The
362 semantics when both copies of bodies differ is not well defined.
363 We replace the old body with new body so in unit at a time mode
364 we always use new body, while in normal mode we may end up with
365 old body inlined into some functions and new body expanded and
366 inlined in others.
368 ??? It may make more sense to use one body for inlining and other
369 body for expanding the function but this is difficult to do. */
371 void
372 cgraph_node::reset (void)
374 /* If process is set, then we have already begun whole-unit analysis.
375 This is *not* testing for whether we've already emitted the function.
376 That case can be sort-of legitimately seen with real function redefinition
377 errors. I would argue that the front end should never present us with
378 such a case, but don't enforce that for now. */
379 gcc_assert (!process);
381 /* Reset our data structures so we can analyze the function again. */
382 memset (&local, 0, sizeof (local));
383 memset (&global, 0, sizeof (global));
384 memset (&rtl, 0, sizeof (rtl));
385 analyzed = false;
386 definition = false;
387 alias = false;
388 transparent_alias = false;
389 weakref = false;
390 cpp_implicit_alias = false;
392 remove_callees ();
393 remove_all_references ();
396 /* Return true when there are references to the node. INCLUDE_SELF is
397 true if a self reference counts as a reference. */
399 bool
400 symtab_node::referred_to_p (bool include_self)
402 ipa_ref *ref = NULL;
404 /* See if there are any references at all. */
405 if (iterate_referring (0, ref))
406 return true;
407 /* For functions check also calls. */
408 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
409 if (cn && cn->callers)
411 if (include_self)
412 return true;
413 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
414 if (e->caller != this)
415 return true;
417 return false;
420 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
421 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
422 the garbage collector run at the moment. We would need to either create
423 a new GC context, or just not compile right now. */
425 void
426 cgraph_node::finalize_function (tree decl, bool no_collect)
428 cgraph_node *node = cgraph_node::get_create (decl);
430 if (node->definition)
432 /* Nested functions should only be defined once. */
433 gcc_assert (!DECL_CONTEXT (decl)
434 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
435 node->reset ();
436 node->local.redefined_extern_inline = true;
439 /* Set definition first before calling notice_global_symbol so that
440 it is available to notice_global_symbol. */
441 node->definition = true;
442 notice_global_symbol (decl);
443 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
445 /* With -fkeep-inline-functions we are keeping all inline functions except
446 for extern inline ones. */
447 if (flag_keep_inline_functions
448 && DECL_DECLARED_INLINE_P (decl)
449 && !DECL_EXTERNAL (decl)
450 && !DECL_DISREGARD_INLINE_LIMITS (decl))
451 node->force_output = 1;
453 /* __RTL functions were already output as soon as they were parsed (due
454 to the large amount of global state in the backend).
455 Mark such functions as "force_output" to reflect the fact that they
456 will be in the asm file when considering the symbols they reference.
457 The attempt to output them later on will bail out immediately. */
458 if (node->native_rtl_p ())
459 node->force_output = 1;
461 /* When not optimizing, also output the static functions. (see
462 PR24561), but don't do so for always_inline functions, functions
463 declared inline and nested functions. These were optimized out
464 in the original implementation and it is unclear whether we want
465 to change the behavior here. */
466 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions)
467 && !node->cpp_implicit_alias
468 && !DECL_DISREGARD_INLINE_LIMITS (decl)
469 && !DECL_DECLARED_INLINE_P (decl)
470 && !(DECL_CONTEXT (decl)
471 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
472 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
473 node->force_output = 1;
475 /* If we've not yet emitted decl, tell the debug info about it. */
476 if (!TREE_ASM_WRITTEN (decl))
477 (*debug_hooks->deferred_inline_function) (decl);
479 if (!no_collect)
480 ggc_collect ();
482 if (symtab->state == CONSTRUCTION
483 && (node->needed_p () || node->referred_to_p ()))
484 enqueue_node (node);
487 /* Add the function FNDECL to the call graph.
488 Unlike finalize_function, this function is intended to be used
489 by middle end and allows insertion of new function at arbitrary point
490 of compilation. The function can be either in high, low or SSA form
491 GIMPLE.
493 The function is assumed to be reachable and have address taken (so no
494 API breaking optimizations are performed on it).
496 Main work done by this function is to enqueue the function for later
497 processing to avoid need the passes to be re-entrant. */
499 void
500 cgraph_node::add_new_function (tree fndecl, bool lowered)
502 gcc::pass_manager *passes = g->get_passes ();
503 cgraph_node *node;
505 if (dump_file)
507 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
508 const char *function_type = ((gimple_has_body_p (fndecl))
509 ? (lowered
510 ? (gimple_in_ssa_p (fn)
511 ? "ssa gimple"
512 : "low gimple")
513 : "high gimple")
514 : "to-be-gimplified");
515 fprintf (dump_file,
516 "Added new %s function %s to callgraph\n",
517 function_type,
518 fndecl_name (fndecl));
521 switch (symtab->state)
523 case PARSING:
524 cgraph_node::finalize_function (fndecl, false);
525 break;
526 case CONSTRUCTION:
527 /* Just enqueue function to be processed at nearest occurrence. */
528 node = cgraph_node::get_create (fndecl);
529 if (lowered)
530 node->lowered = true;
531 cgraph_new_nodes.safe_push (node);
532 break;
534 case IPA:
535 case IPA_SSA:
536 case IPA_SSA_AFTER_INLINING:
537 case EXPANSION:
538 /* Bring the function into finalized state and enqueue for later
539 analyzing and compilation. */
540 node = cgraph_node::get_create (fndecl);
541 node->local.local = false;
542 node->definition = true;
543 node->force_output = true;
544 if (!lowered && symtab->state == EXPANSION)
546 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
547 gimple_register_cfg_hooks ();
548 bitmap_obstack_initialize (NULL);
549 execute_pass_list (cfun, passes->all_lowering_passes);
550 passes->execute_early_local_passes ();
551 bitmap_obstack_release (NULL);
552 pop_cfun ();
554 lowered = true;
556 if (lowered)
557 node->lowered = true;
558 cgraph_new_nodes.safe_push (node);
559 break;
561 case FINISHED:
562 /* At the very end of compilation we have to do all the work up
563 to expansion. */
564 node = cgraph_node::create (fndecl);
565 if (lowered)
566 node->lowered = true;
567 node->definition = true;
568 node->analyze ();
569 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
570 gimple_register_cfg_hooks ();
571 bitmap_obstack_initialize (NULL);
572 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
573 g->get_passes ()->execute_early_local_passes ();
574 bitmap_obstack_release (NULL);
575 pop_cfun ();
576 node->expand ();
577 break;
579 default:
580 gcc_unreachable ();
583 /* Set a personality if required and we already passed EH lowering. */
584 if (lowered
585 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
586 == eh_personality_lang))
587 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
590 /* Analyze the function scheduled to be output. */
591 void
592 cgraph_node::analyze (void)
594 if (native_rtl_p ())
596 analyzed = true;
597 return;
600 tree decl = this->decl;
601 location_t saved_loc = input_location;
602 input_location = DECL_SOURCE_LOCATION (decl);
604 if (thunk.thunk_p)
606 cgraph_node *t = cgraph_node::get (thunk.alias);
608 create_edge (t, NULL, 0, CGRAPH_FREQ_BASE);
609 callees->can_throw_external = !TREE_NOTHROW (t->decl);
610 /* Target code in expand_thunk may need the thunk's target
611 to be analyzed, so recurse here. */
612 if (!t->analyzed)
613 t->analyze ();
614 if (t->alias)
616 t = t->get_alias_target ();
617 if (!t->analyzed)
618 t->analyze ();
620 if (!expand_thunk (false, false))
622 thunk.alias = NULL;
623 return;
625 thunk.alias = NULL;
627 if (alias)
628 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
629 else if (dispatcher_function)
631 /* Generate the dispatcher body of multi-versioned functions. */
632 cgraph_function_version_info *dispatcher_version_info
633 = function_version ();
634 if (dispatcher_version_info != NULL
635 && (dispatcher_version_info->dispatcher_resolver
636 == NULL_TREE))
638 tree resolver = NULL_TREE;
639 gcc_assert (targetm.generate_version_dispatcher_body);
640 resolver = targetm.generate_version_dispatcher_body (this);
641 gcc_assert (resolver != NULL_TREE);
644 else
646 push_cfun (DECL_STRUCT_FUNCTION (decl));
648 assign_assembler_name_if_needed (decl);
650 /* Make sure to gimplify bodies only once. During analyzing a
651 function we lower it, which will require gimplified nested
652 functions, so we can end up here with an already gimplified
653 body. */
654 if (!gimple_has_body_p (decl))
655 gimplify_function_tree (decl);
657 /* Lower the function. */
658 if (!lowered)
660 if (nested)
661 lower_nested_functions (decl);
662 gcc_assert (!nested);
664 gimple_register_cfg_hooks ();
665 bitmap_obstack_initialize (NULL);
666 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
667 free_dominance_info (CDI_POST_DOMINATORS);
668 free_dominance_info (CDI_DOMINATORS);
669 compact_blocks ();
670 bitmap_obstack_release (NULL);
671 lowered = true;
674 pop_cfun ();
676 analyzed = true;
678 input_location = saved_loc;
681 /* C++ frontend produce same body aliases all over the place, even before PCH
682 gets streamed out. It relies on us linking the aliases with their function
683 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
684 first produce aliases without links, but once C++ FE is sure he won't sream
685 PCH we build the links via this function. */
687 void
688 symbol_table::process_same_body_aliases (void)
690 symtab_node *node;
691 FOR_EACH_SYMBOL (node)
692 if (node->cpp_implicit_alias && !node->analyzed)
693 node->resolve_alias
694 (VAR_P (node->alias_target)
695 ? (symtab_node *)varpool_node::get_create (node->alias_target)
696 : (symtab_node *)cgraph_node::get_create (node->alias_target));
697 cpp_implicit_aliases_done = true;
700 /* Process attributes common for vars and functions. */
702 static void
703 process_common_attributes (symtab_node *node, tree decl)
705 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
707 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
709 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
710 "%<weakref%> attribute should be accompanied with"
711 " an %<alias%> attribute");
712 DECL_WEAK (decl) = 0;
713 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
714 DECL_ATTRIBUTES (decl));
717 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
718 node->no_reorder = 1;
721 /* Look for externally_visible and used attributes and mark cgraph nodes
722 accordingly.
724 We cannot mark the nodes at the point the attributes are processed (in
725 handle_*_attribute) because the copy of the declarations available at that
726 point may not be canonical. For example, in:
728 void f();
729 void f() __attribute__((used));
731 the declaration we see in handle_used_attribute will be the second
732 declaration -- but the front end will subsequently merge that declaration
733 with the original declaration and discard the second declaration.
735 Furthermore, we can't mark these nodes in finalize_function because:
737 void f() {}
738 void f() __attribute__((externally_visible));
740 is valid.
742 So, we walk the nodes at the end of the translation unit, applying the
743 attributes at that point. */
745 static void
746 process_function_and_variable_attributes (cgraph_node *first,
747 varpool_node *first_var)
749 cgraph_node *node;
750 varpool_node *vnode;
752 for (node = symtab->first_function (); node != first;
753 node = symtab->next_function (node))
755 tree decl = node->decl;
756 if (DECL_PRESERVE_P (decl))
757 node->mark_force_output ();
758 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
760 if (! TREE_PUBLIC (node->decl))
761 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
762 "%<externally_visible%>"
763 " attribute have effect only on public objects");
765 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
766 && (node->definition && !node->alias))
768 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
769 "%<weakref%> attribute ignored"
770 " because function is defined");
771 DECL_WEAK (decl) = 0;
772 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
773 DECL_ATTRIBUTES (decl));
776 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
777 && !DECL_DECLARED_INLINE_P (decl)
778 /* redefining extern inline function makes it DECL_UNINLINABLE. */
779 && !DECL_UNINLINABLE (decl))
780 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
781 "always_inline function might not be inlinable");
783 process_common_attributes (node, decl);
785 for (vnode = symtab->first_variable (); vnode != first_var;
786 vnode = symtab->next_variable (vnode))
788 tree decl = vnode->decl;
789 if (DECL_EXTERNAL (decl)
790 && DECL_INITIAL (decl))
791 varpool_node::finalize_decl (decl);
792 if (DECL_PRESERVE_P (decl))
793 vnode->force_output = true;
794 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
796 if (! TREE_PUBLIC (vnode->decl))
797 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
798 "%<externally_visible%>"
799 " attribute have effect only on public objects");
801 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
802 && vnode->definition
803 && DECL_INITIAL (decl))
805 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
806 "%<weakref%> attribute ignored"
807 " because variable is initialized");
808 DECL_WEAK (decl) = 0;
809 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
810 DECL_ATTRIBUTES (decl));
812 process_common_attributes (vnode, decl);
816 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
817 middle end to output the variable to asm file, if needed or externally
818 visible. */
820 void
821 varpool_node::finalize_decl (tree decl)
823 varpool_node *node = varpool_node::get_create (decl);
825 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
827 if (node->definition)
828 return;
829 /* Set definition first before calling notice_global_symbol so that
830 it is available to notice_global_symbol. */
831 node->definition = true;
832 notice_global_symbol (decl);
833 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
834 /* Traditionally we do not eliminate static variables when not
835 optimizing and when not doing toplevel reoder. */
836 || node->no_reorder
837 || ((!flag_toplevel_reorder
838 && !DECL_COMDAT (node->decl)
839 && !DECL_ARTIFICIAL (node->decl))))
840 node->force_output = true;
842 if (symtab->state == CONSTRUCTION
843 && (node->needed_p () || node->referred_to_p ()))
844 enqueue_node (node);
845 if (symtab->state >= IPA_SSA)
846 node->analyze ();
847 /* Some frontends produce various interface variables after compilation
848 finished. */
849 if (symtab->state == FINISHED
850 || (!flag_toplevel_reorder
851 && symtab->state == EXPANSION))
852 node->assemble_decl ();
854 if (DECL_INITIAL (decl))
855 chkp_register_var_initializer (decl);
858 /* EDGE is an polymorphic call. Mark all possible targets as reachable
859 and if there is only one target, perform trivial devirtualization.
860 REACHABLE_CALL_TARGETS collects target lists we already walked to
861 avoid udplicate work. */
863 static void
864 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
865 cgraph_edge *edge)
867 unsigned int i;
868 void *cache_token;
869 bool final;
870 vec <cgraph_node *>targets
871 = possible_polymorphic_call_targets
872 (edge, &final, &cache_token);
874 if (!reachable_call_targets->add (cache_token))
876 if (symtab->dump_file)
877 dump_possible_polymorphic_call_targets
878 (symtab->dump_file, edge);
880 for (i = 0; i < targets.length (); i++)
882 /* Do not bother to mark virtual methods in anonymous namespace;
883 either we will find use of virtual table defining it, or it is
884 unused. */
885 if (targets[i]->definition
886 && TREE_CODE
887 (TREE_TYPE (targets[i]->decl))
888 == METHOD_TYPE
889 && !type_in_anonymous_namespace_p
890 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
891 enqueue_node (targets[i]);
895 /* Very trivial devirtualization; when the type is
896 final or anonymous (so we know all its derivation)
897 and there is only one possible virtual call target,
898 make the edge direct. */
899 if (final)
901 if (targets.length () <= 1 && dbg_cnt (devirt))
903 cgraph_node *target;
904 if (targets.length () == 1)
905 target = targets[0];
906 else
907 target = cgraph_node::create
908 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
910 if (symtab->dump_file)
912 fprintf (symtab->dump_file,
913 "Devirtualizing call: ");
914 print_gimple_stmt (symtab->dump_file,
915 edge->call_stmt, 0,
916 TDF_SLIM);
918 if (dump_enabled_p ())
920 location_t locus = gimple_location_safe (edge->call_stmt);
921 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
922 "devirtualizing call in %s to %s\n",
923 edge->caller->name (), target->name ());
926 edge->make_direct (target);
927 edge->redirect_call_stmt_to_callee ();
929 /* Call to __builtin_unreachable shouldn't be instrumented. */
930 if (!targets.length ())
931 gimple_call_set_with_bounds (edge->call_stmt, false);
933 if (symtab->dump_file)
935 fprintf (symtab->dump_file,
936 "Devirtualized as: ");
937 print_gimple_stmt (symtab->dump_file,
938 edge->call_stmt, 0,
939 TDF_SLIM);
945 /* Issue appropriate warnings for the global declaration DECL. */
947 static void
948 check_global_declaration (symtab_node *snode)
950 const char *decl_file;
951 tree decl = snode->decl;
953 /* Warn about any function declared static but not defined. We don't
954 warn about variables, because many programs have static variables
955 that exist only to get some text into the object file. */
956 if (TREE_CODE (decl) == FUNCTION_DECL
957 && DECL_INITIAL (decl) == 0
958 && DECL_EXTERNAL (decl)
959 && ! DECL_ARTIFICIAL (decl)
960 && ! TREE_NO_WARNING (decl)
961 && ! TREE_PUBLIC (decl)
962 && (warn_unused_function
963 || snode->referred_to_p (/*include_self=*/false)))
965 if (snode->referred_to_p (/*include_self=*/false))
966 pedwarn (input_location, 0, "%q+F used but never defined", decl);
967 else
968 warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
969 /* This symbol is effectively an "extern" declaration now. */
970 TREE_PUBLIC (decl) = 1;
973 /* Warn about static fns or vars defined but not used. */
974 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
975 || (((warn_unused_variable && ! TREE_READONLY (decl))
976 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
977 && (warn_unused_const_variable == 2
978 || (main_input_filename != NULL
979 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
980 && filename_cmp (main_input_filename,
981 decl_file) == 0))))
982 && VAR_P (decl)))
983 && ! DECL_IN_SYSTEM_HEADER (decl)
984 && ! snode->referred_to_p (/*include_self=*/false)
985 /* This TREE_USED check is needed in addition to referred_to_p
986 above, because the `__unused__' attribute is not being
987 considered for referred_to_p. */
988 && ! TREE_USED (decl)
989 /* The TREE_USED bit for file-scope decls is kept in the identifier,
990 to handle multiple external decls in different scopes. */
991 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
992 && ! DECL_EXTERNAL (decl)
993 && ! DECL_ARTIFICIAL (decl)
994 && ! DECL_ABSTRACT_ORIGIN (decl)
995 && ! TREE_PUBLIC (decl)
996 /* A volatile variable might be used in some non-obvious way. */
997 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
998 /* Global register variables must be declared to reserve them. */
999 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1000 /* Global ctors and dtors are called by the runtime. */
1001 && (TREE_CODE (decl) != FUNCTION_DECL
1002 || (!DECL_STATIC_CONSTRUCTOR (decl)
1003 && !DECL_STATIC_DESTRUCTOR (decl)))
1004 /* Otherwise, ask the language. */
1005 && lang_hooks.decls.warn_unused_global (decl))
1006 warning_at (DECL_SOURCE_LOCATION (decl),
1007 (TREE_CODE (decl) == FUNCTION_DECL)
1008 ? OPT_Wunused_function
1009 : (TREE_READONLY (decl)
1010 ? OPT_Wunused_const_variable_
1011 : OPT_Wunused_variable),
1012 "%qD defined but not used", decl);
1015 /* Discover all functions and variables that are trivially needed, analyze
1016 them as well as all functions and variables referred by them */
1017 static cgraph_node *first_analyzed;
1018 static varpool_node *first_analyzed_var;
1020 /* FIRST_TIME is set to TRUE for the first time we are called for a
1021 translation unit from finalize_compilation_unit() or false
1022 otherwise. */
1024 static void
1025 analyze_functions (bool first_time)
1027 /* Keep track of already processed nodes when called multiple times for
1028 intermodule optimization. */
1029 cgraph_node *first_handled = first_analyzed;
1030 varpool_node *first_handled_var = first_analyzed_var;
1031 hash_set<void *> reachable_call_targets;
1033 symtab_node *node;
1034 symtab_node *next;
1035 int i;
1036 ipa_ref *ref;
1037 bool changed = true;
1038 location_t saved_loc = input_location;
1040 bitmap_obstack_initialize (NULL);
1041 symtab->state = CONSTRUCTION;
1042 input_location = UNKNOWN_LOCATION;
1044 /* Ugly, but the fixup can not happen at a time same body alias is created;
1045 C++ FE is confused about the COMDAT groups being right. */
1046 if (symtab->cpp_implicit_aliases_done)
1047 FOR_EACH_SYMBOL (node)
1048 if (node->cpp_implicit_alias)
1049 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1050 build_type_inheritance_graph ();
1052 /* Analysis adds static variables that in turn adds references to new functions.
1053 So we need to iterate the process until it stabilize. */
1054 while (changed)
1056 changed = false;
1057 process_function_and_variable_attributes (first_analyzed,
1058 first_analyzed_var);
1060 /* First identify the trivially needed symbols. */
1061 for (node = symtab->first_symbol ();
1062 node != first_analyzed
1063 && node != first_analyzed_var; node = node->next)
1065 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1066 node->get_comdat_group_id ();
1067 if (node->needed_p ())
1069 enqueue_node (node);
1070 if (!changed && symtab->dump_file)
1071 fprintf (symtab->dump_file, "Trivially needed symbols:");
1072 changed = true;
1073 if (symtab->dump_file)
1074 fprintf (symtab->dump_file, " %s", node->asm_name ());
1075 if (!changed && symtab->dump_file)
1076 fprintf (symtab->dump_file, "\n");
1078 if (node == first_analyzed
1079 || node == first_analyzed_var)
1080 break;
1082 symtab->process_new_functions ();
1083 first_analyzed_var = symtab->first_variable ();
1084 first_analyzed = symtab->first_function ();
1086 if (changed && symtab->dump_file)
1087 fprintf (symtab->dump_file, "\n");
1089 /* Lower representation, build callgraph edges and references for all trivially
1090 needed symbols and all symbols referred by them. */
1091 while (queued_nodes != &symtab_terminator)
1093 changed = true;
1094 node = queued_nodes;
1095 queued_nodes = (symtab_node *)queued_nodes->aux;
1096 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1097 if (cnode && cnode->definition)
1099 cgraph_edge *edge;
1100 tree decl = cnode->decl;
1102 /* ??? It is possible to create extern inline function
1103 and later using weak alias attribute to kill its body.
1104 See gcc.c-torture/compile/20011119-1.c */
1105 if (!DECL_STRUCT_FUNCTION (decl)
1106 && !cnode->alias
1107 && !cnode->thunk.thunk_p
1108 && !cnode->dispatcher_function)
1110 cnode->reset ();
1111 cnode->local.redefined_extern_inline = true;
1112 continue;
1115 if (!cnode->analyzed)
1116 cnode->analyze ();
1118 for (edge = cnode->callees; edge; edge = edge->next_callee)
1119 if (edge->callee->definition
1120 && (!DECL_EXTERNAL (edge->callee->decl)
1121 /* When not optimizing, do not try to analyze extern
1122 inline functions. Doing so is pointless. */
1123 || opt_for_fn (edge->callee->decl, optimize)
1124 /* Weakrefs needs to be preserved. */
1125 || edge->callee->alias
1126 /* always_inline functions are inlined aven at -O0. */
1127 || lookup_attribute
1128 ("always_inline",
1129 DECL_ATTRIBUTES (edge->callee->decl))
1130 /* Multiversioned functions needs the dispatcher to
1131 be produced locally even for extern functions. */
1132 || edge->callee->function_version ()))
1133 enqueue_node (edge->callee);
1134 if (opt_for_fn (cnode->decl, optimize)
1135 && opt_for_fn (cnode->decl, flag_devirtualize))
1137 cgraph_edge *next;
1139 for (edge = cnode->indirect_calls; edge; edge = next)
1141 next = edge->next_callee;
1142 if (edge->indirect_info->polymorphic)
1143 walk_polymorphic_call_targets (&reachable_call_targets,
1144 edge);
1148 /* If decl is a clone of an abstract function,
1149 mark that abstract function so that we don't release its body.
1150 The DECL_INITIAL() of that abstract function declaration
1151 will be later needed to output debug info. */
1152 if (DECL_ABSTRACT_ORIGIN (decl))
1154 cgraph_node *origin_node
1155 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1156 origin_node->used_as_abstract_origin = true;
1158 /* Preserve a functions function context node. It will
1159 later be needed to output debug info. */
1160 if (tree fn = decl_function_context (decl))
1162 cgraph_node *origin_node = cgraph_node::get_create (fn);
1163 enqueue_node (origin_node);
1166 else
1168 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1169 if (vnode && vnode->definition && !vnode->analyzed)
1170 vnode->analyze ();
1173 if (node->same_comdat_group)
1175 symtab_node *next;
1176 for (next = node->same_comdat_group;
1177 next != node;
1178 next = next->same_comdat_group)
1179 if (!next->comdat_local_p ())
1180 enqueue_node (next);
1182 for (i = 0; node->iterate_reference (i, ref); i++)
1183 if (ref->referred->definition
1184 && (!DECL_EXTERNAL (ref->referred->decl)
1185 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1186 && optimize)
1187 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1188 && opt_for_fn (ref->referred->decl, optimize))
1189 || node->alias
1190 || ref->referred->alias)))
1191 enqueue_node (ref->referred);
1192 symtab->process_new_functions ();
1195 update_type_inheritance_graph ();
1197 /* Collect entry points to the unit. */
1198 if (symtab->dump_file)
1200 fprintf (symtab->dump_file, "\n\nInitial ");
1201 symtab_node::dump_table (symtab->dump_file);
1204 if (first_time)
1206 symtab_node *snode;
1207 FOR_EACH_SYMBOL (snode)
1208 check_global_declaration (snode);
1211 if (symtab->dump_file)
1212 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1214 for (node = symtab->first_symbol ();
1215 node != first_handled
1216 && node != first_handled_var; node = next)
1218 next = node->next;
1219 if (!node->aux && !node->referred_to_p ())
1221 if (symtab->dump_file)
1222 fprintf (symtab->dump_file, " %s", node->name ());
1224 /* See if the debugger can use anything before the DECL
1225 passes away. Perhaps it can notice a DECL that is now a
1226 constant and can tag the early DIE with an appropriate
1227 attribute.
1229 Otherwise, this is the last chance the debug_hooks have
1230 at looking at optimized away DECLs, since
1231 late_global_decl will subsequently be called from the
1232 contents of the now pruned symbol table. */
1233 if (VAR_P (node->decl)
1234 && !decl_function_context (node->decl))
1236 /* We are reclaiming totally unreachable code and variables
1237 so they effectively appear as readonly. Show that to
1238 the debug machinery. */
1239 TREE_READONLY (node->decl) = 1;
1240 node->definition = false;
1241 (*debug_hooks->late_global_decl) (node->decl);
1244 node->remove ();
1245 continue;
1247 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1249 tree decl = node->decl;
1251 if (cnode->definition && !gimple_has_body_p (decl)
1252 && !cnode->alias
1253 && !cnode->thunk.thunk_p)
1254 cnode->reset ();
1256 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1257 || cnode->alias
1258 || gimple_has_body_p (decl)
1259 || cnode->native_rtl_p ());
1260 gcc_assert (cnode->analyzed == cnode->definition);
1262 node->aux = NULL;
1264 for (;node; node = node->next)
1265 node->aux = NULL;
1266 first_analyzed = symtab->first_function ();
1267 first_analyzed_var = symtab->first_variable ();
1268 if (symtab->dump_file)
1270 fprintf (symtab->dump_file, "\n\nReclaimed ");
1271 symtab_node::dump_table (symtab->dump_file);
1273 bitmap_obstack_release (NULL);
1274 ggc_collect ();
1275 /* Initialize assembler name hash, in particular we want to trigger C++
1276 mangling and same body alias creation before we free DECL_ARGUMENTS
1277 used by it. */
1278 if (!seen_error ())
1279 symtab->symtab_initialize_asm_name_hash ();
1281 input_location = saved_loc;
1284 /* Translate the ugly representation of aliases as alias pairs into nice
1285 representation in callgraph. We don't handle all cases yet,
1286 unfortunately. */
1288 static void
1289 handle_alias_pairs (void)
1291 alias_pair *p;
1292 unsigned i;
1294 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1296 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1298 /* Weakrefs with target not defined in current unit are easy to handle:
1299 they behave just as external variables except we need to note the
1300 alias flag to later output the weakref pseudo op into asm file. */
1301 if (!target_node
1302 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1304 symtab_node *node = symtab_node::get (p->decl);
1305 if (node)
1307 node->alias_target = p->target;
1308 node->weakref = true;
1309 node->alias = true;
1310 node->transparent_alias = true;
1312 alias_pairs->unordered_remove (i);
1313 continue;
1315 else if (!target_node)
1317 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1318 symtab_node *node = symtab_node::get (p->decl);
1319 if (node)
1320 node->alias = false;
1321 alias_pairs->unordered_remove (i);
1322 continue;
1325 if (DECL_EXTERNAL (target_node->decl)
1326 /* We use local aliases for C++ thunks to force the tailcall
1327 to bind locally. This is a hack - to keep it working do
1328 the following (which is not strictly correct). */
1329 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1330 || ! DECL_VIRTUAL_P (target_node->decl))
1331 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1333 error ("%q+D aliased to external symbol %qE",
1334 p->decl, p->target);
1337 if (TREE_CODE (p->decl) == FUNCTION_DECL
1338 && target_node && is_a <cgraph_node *> (target_node))
1340 cgraph_node *src_node = cgraph_node::get (p->decl);
1341 if (src_node && src_node->definition)
1342 src_node->reset ();
1343 cgraph_node::create_alias (p->decl, target_node->decl);
1344 alias_pairs->unordered_remove (i);
1346 else if (VAR_P (p->decl)
1347 && target_node && is_a <varpool_node *> (target_node))
1349 varpool_node::create_alias (p->decl, target_node->decl);
1350 alias_pairs->unordered_remove (i);
1352 else
1354 error ("%q+D alias in between function and variable is not supported",
1355 p->decl);
1356 warning (0, "%q+D aliased declaration",
1357 target_node->decl);
1358 alias_pairs->unordered_remove (i);
1361 vec_free (alias_pairs);
1365 /* Figure out what functions we want to assemble. */
1367 static void
1368 mark_functions_to_output (void)
1370 bool check_same_comdat_groups = false;
1371 cgraph_node *node;
1373 if (flag_checking)
1374 FOR_EACH_FUNCTION (node)
1375 gcc_assert (!node->process);
1377 FOR_EACH_FUNCTION (node)
1379 tree decl = node->decl;
1381 gcc_assert (!node->process || node->same_comdat_group);
1382 if (node->process)
1383 continue;
1385 /* We need to output all local functions that are used and not
1386 always inlined, as well as those that are reachable from
1387 outside the current compilation unit. */
1388 if (node->analyzed
1389 && !node->thunk.thunk_p
1390 && !node->alias
1391 && !node->global.inlined_to
1392 && !TREE_ASM_WRITTEN (decl)
1393 && !DECL_EXTERNAL (decl))
1395 node->process = 1;
1396 if (node->same_comdat_group)
1398 cgraph_node *next;
1399 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1400 next != node;
1401 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1402 if (!next->thunk.thunk_p && !next->alias
1403 && !next->comdat_local_p ())
1404 next->process = 1;
1407 else if (node->same_comdat_group)
1409 if (flag_checking)
1410 check_same_comdat_groups = true;
1412 else
1414 /* We should've reclaimed all functions that are not needed. */
1415 if (flag_checking
1416 && !node->global.inlined_to
1417 && gimple_has_body_p (decl)
1418 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1419 are inside partition, we can end up not removing the body since we no longer
1420 have analyzed node pointing to it. */
1421 && !node->in_other_partition
1422 && !node->alias
1423 && !node->clones
1424 && !DECL_EXTERNAL (decl))
1426 node->debug ();
1427 internal_error ("failed to reclaim unneeded function");
1429 gcc_assert (node->global.inlined_to
1430 || !gimple_has_body_p (decl)
1431 || node->in_other_partition
1432 || node->clones
1433 || DECL_ARTIFICIAL (decl)
1434 || DECL_EXTERNAL (decl));
1439 if (flag_checking && check_same_comdat_groups)
1440 FOR_EACH_FUNCTION (node)
1441 if (node->same_comdat_group && !node->process)
1443 tree decl = node->decl;
1444 if (!node->global.inlined_to
1445 && gimple_has_body_p (decl)
1446 /* FIXME: in an ltrans unit when the offline copy is outside a
1447 partition but inline copies are inside a partition, we can
1448 end up not removing the body since we no longer have an
1449 analyzed node pointing to it. */
1450 && !node->in_other_partition
1451 && !node->clones
1452 && !DECL_EXTERNAL (decl))
1454 node->debug ();
1455 internal_error ("failed to reclaim unneeded function in same "
1456 "comdat group");
1461 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1462 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1464 Set current_function_decl and cfun to newly constructed empty function body.
1465 return basic block in the function body. */
1467 basic_block
1468 init_lowered_empty_function (tree decl, bool in_ssa, gcov_type count)
1470 basic_block bb;
1471 edge e;
1473 current_function_decl = decl;
1474 allocate_struct_function (decl, false);
1475 gimple_register_cfg_hooks ();
1476 init_empty_tree_cfg ();
1477 init_tree_ssa (cfun);
1479 if (in_ssa)
1481 init_ssa_operands (cfun);
1482 cfun->gimple_df->in_ssa_p = true;
1483 cfun->curr_properties |= PROP_ssa;
1486 DECL_INITIAL (decl) = make_node (BLOCK);
1487 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1489 DECL_SAVED_TREE (decl) = error_mark_node;
1490 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1491 | PROP_cfg | PROP_loops);
1493 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1494 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1495 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1497 /* Create BB for body of the function and connect it properly. */
1498 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1499 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1500 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1501 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = REG_BR_PROB_BASE;
1502 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1503 bb->count = count;
1504 bb->frequency = BB_FREQ_MAX;
1505 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1506 e->count = count;
1507 e->probability = REG_BR_PROB_BASE;
1508 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1509 e->count = count;
1510 e->probability = REG_BR_PROB_BASE;
1511 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1513 return bb;
1516 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1517 offset indicated by VIRTUAL_OFFSET, if that is
1518 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1519 zero for a result adjusting thunk. */
1521 tree
1522 thunk_adjust (gimple_stmt_iterator * bsi,
1523 tree ptr, bool this_adjusting,
1524 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1526 gassign *stmt;
1527 tree ret;
1529 if (this_adjusting
1530 && fixed_offset != 0)
1532 stmt = gimple_build_assign
1533 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1534 ptr,
1535 fixed_offset));
1536 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1539 /* If there's a virtual offset, look up that value in the vtable and
1540 adjust the pointer again. */
1541 if (virtual_offset)
1543 tree vtabletmp;
1544 tree vtabletmp2;
1545 tree vtabletmp3;
1547 if (!vtable_entry_type)
1549 tree vfunc_type = make_node (FUNCTION_TYPE);
1550 TREE_TYPE (vfunc_type) = integer_type_node;
1551 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1552 layout_type (vfunc_type);
1554 vtable_entry_type = build_pointer_type (vfunc_type);
1557 vtabletmp =
1558 create_tmp_reg (build_pointer_type
1559 (build_pointer_type (vtable_entry_type)), "vptr");
1561 /* The vptr is always at offset zero in the object. */
1562 stmt = gimple_build_assign (vtabletmp,
1563 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1564 ptr));
1565 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1567 /* Form the vtable address. */
1568 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1569 "vtableaddr");
1570 stmt = gimple_build_assign (vtabletmp2,
1571 build_simple_mem_ref (vtabletmp));
1572 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1574 /* Find the entry with the vcall offset. */
1575 stmt = gimple_build_assign (vtabletmp2,
1576 fold_build_pointer_plus_loc (input_location,
1577 vtabletmp2,
1578 virtual_offset));
1579 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1581 /* Get the offset itself. */
1582 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1583 "vcalloffset");
1584 stmt = gimple_build_assign (vtabletmp3,
1585 build_simple_mem_ref (vtabletmp2));
1586 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1588 /* Adjust the `this' pointer. */
1589 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1590 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1591 GSI_CONTINUE_LINKING);
1594 if (!this_adjusting
1595 && fixed_offset != 0)
1596 /* Adjust the pointer by the constant. */
1598 tree ptrtmp;
1600 if (VAR_P (ptr))
1601 ptrtmp = ptr;
1602 else
1604 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1605 stmt = gimple_build_assign (ptrtmp, ptr);
1606 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1608 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1609 ptrtmp, fixed_offset);
1612 /* Emit the statement and gimplify the adjustment expression. */
1613 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1614 stmt = gimple_build_assign (ret, ptr);
1615 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1617 return ret;
1620 /* Expand thunk NODE to gimple if possible.
1621 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1622 no assembler is produced.
1623 When OUTPUT_ASM_THUNK is true, also produce assembler for
1624 thunks that are not lowered. */
1626 bool
1627 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1629 bool this_adjusting = thunk.this_adjusting;
1630 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1631 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1632 tree virtual_offset = NULL;
1633 tree alias = callees->callee->decl;
1634 tree thunk_fndecl = decl;
1635 tree a;
1637 /* Instrumentation thunk is the same function with
1638 a different signature. Never need to expand it. */
1639 if (thunk.add_pointer_bounds_args)
1640 return false;
1642 if (!force_gimple_thunk && this_adjusting
1643 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1644 virtual_value, alias))
1646 const char *fnname;
1647 tree fn_block;
1648 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1650 if (!output_asm_thunks)
1652 analyzed = true;
1653 return false;
1656 if (in_lto_p)
1657 get_untransformed_body ();
1658 a = DECL_ARGUMENTS (thunk_fndecl);
1660 current_function_decl = thunk_fndecl;
1662 /* Ensure thunks are emitted in their correct sections. */
1663 resolve_unique_section (thunk_fndecl, 0,
1664 flag_function_sections);
1666 DECL_RESULT (thunk_fndecl)
1667 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1668 RESULT_DECL, 0, restype);
1669 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1670 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1672 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1673 create one. */
1674 fn_block = make_node (BLOCK);
1675 BLOCK_VARS (fn_block) = a;
1676 DECL_INITIAL (thunk_fndecl) = fn_block;
1677 BLOCK_SUPERCONTEXT (fn_block) = thunk_fndecl;
1678 allocate_struct_function (thunk_fndecl, false);
1679 init_function_start (thunk_fndecl);
1680 cfun->is_thunk = 1;
1681 insn_locations_init ();
1682 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1683 prologue_location = curr_insn_location ();
1684 assemble_start_function (thunk_fndecl, fnname);
1686 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1687 fixed_offset, virtual_value, alias);
1689 assemble_end_function (thunk_fndecl, fnname);
1690 insn_locations_finalize ();
1691 init_insn_lengths ();
1692 free_after_compilation (cfun);
1693 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1694 thunk.thunk_p = false;
1695 analyzed = false;
1697 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1699 error ("generic thunk code fails for method %qD which uses %<...%>",
1700 thunk_fndecl);
1701 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1702 analyzed = true;
1703 return false;
1705 else
1707 tree restype;
1708 basic_block bb, then_bb, else_bb, return_bb;
1709 gimple_stmt_iterator bsi;
1710 int nargs = 0;
1711 tree arg;
1712 int i;
1713 tree resdecl;
1714 tree restmp = NULL;
1715 tree resbnd = NULL;
1717 gcall *call;
1718 greturn *ret;
1719 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1721 /* We may be called from expand_thunk that releses body except for
1722 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1723 if (in_lto_p && !force_gimple_thunk)
1724 get_untransformed_body ();
1725 a = DECL_ARGUMENTS (thunk_fndecl);
1727 current_function_decl = thunk_fndecl;
1729 /* Ensure thunks are emitted in their correct sections. */
1730 resolve_unique_section (thunk_fndecl, 0,
1731 flag_function_sections);
1733 DECL_IGNORED_P (thunk_fndecl) = 1;
1734 bitmap_obstack_initialize (NULL);
1736 if (thunk.virtual_offset_p)
1737 virtual_offset = size_int (virtual_value);
1739 /* Build the return declaration for the function. */
1740 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1741 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1743 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1744 DECL_ARTIFICIAL (resdecl) = 1;
1745 DECL_IGNORED_P (resdecl) = 1;
1746 DECL_RESULT (thunk_fndecl) = resdecl;
1747 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1749 else
1750 resdecl = DECL_RESULT (thunk_fndecl);
1752 bb = then_bb = else_bb = return_bb
1753 = init_lowered_empty_function (thunk_fndecl, true, count);
1755 bsi = gsi_start_bb (bb);
1757 /* Build call to the function being thunked. */
1758 if (!VOID_TYPE_P (restype)
1759 && (!alias_is_noreturn
1760 || TREE_ADDRESSABLE (restype)
1761 || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST))
1763 if (DECL_BY_REFERENCE (resdecl))
1765 restmp = gimple_fold_indirect_ref (resdecl);
1766 if (!restmp)
1767 restmp = build2 (MEM_REF,
1768 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1769 resdecl,
1770 build_int_cst (TREE_TYPE
1771 (DECL_RESULT (alias)), 0));
1773 else if (!is_gimple_reg_type (restype))
1775 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1777 restmp = resdecl;
1779 if (VAR_P (restmp))
1780 add_local_decl (cfun, restmp);
1781 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1783 else
1784 restmp = create_tmp_var (restype, "retval");
1786 else
1787 restmp = create_tmp_reg (restype, "retval");
1790 for (arg = a; arg; arg = DECL_CHAIN (arg))
1791 nargs++;
1792 auto_vec<tree> vargs (nargs);
1793 i = 0;
1794 arg = a;
1795 if (this_adjusting)
1797 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1798 virtual_offset));
1799 arg = DECL_CHAIN (a);
1800 i = 1;
1803 if (nargs)
1804 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1806 tree tmp = arg;
1807 if (!is_gimple_val (arg))
1809 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1810 (TREE_TYPE (arg)), "arg");
1811 gimple *stmt = gimple_build_assign (tmp, arg);
1812 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1814 vargs.quick_push (tmp);
1816 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1817 callees->call_stmt = call;
1818 gimple_call_set_from_thunk (call, true);
1819 gimple_call_set_with_bounds (call, instrumentation_clone);
1821 /* Return slot optimization is always possible and in fact requred to
1822 return values with DECL_BY_REFERENCE. */
1823 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1824 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1825 || DECL_BY_REFERENCE (resdecl)))
1826 gimple_call_set_return_slot_opt (call, true);
1828 if (restmp)
1830 gimple_call_set_lhs (call, restmp);
1831 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1832 TREE_TYPE (TREE_TYPE (alias))));
1834 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1835 if (!alias_is_noreturn)
1837 if (instrumentation_clone
1838 && !DECL_BY_REFERENCE (resdecl)
1839 && restmp
1840 && BOUNDED_P (restmp))
1842 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1843 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1844 as_a <gcall *> (gsi_stmt (bsi)),
1845 callees->count, callees->frequency);
1848 if (restmp && !this_adjusting
1849 && (fixed_offset || virtual_offset))
1851 tree true_label = NULL_TREE;
1853 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1855 gimple *stmt;
1856 edge e;
1857 /* If the return type is a pointer, we need to
1858 protect against NULL. We know there will be an
1859 adjustment, because that's why we're emitting a
1860 thunk. */
1861 then_bb = create_basic_block (NULL, bb);
1862 then_bb->count = count - count / 16;
1863 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1864 return_bb = create_basic_block (NULL, then_bb);
1865 return_bb->count = count;
1866 return_bb->frequency = BB_FREQ_MAX;
1867 else_bb = create_basic_block (NULL, else_bb);
1868 then_bb->count = count / 16;
1869 then_bb->frequency = BB_FREQ_MAX / 16;
1870 add_bb_to_loop (then_bb, bb->loop_father);
1871 add_bb_to_loop (return_bb, bb->loop_father);
1872 add_bb_to_loop (else_bb, bb->loop_father);
1873 remove_edge (single_succ_edge (bb));
1874 true_label = gimple_block_label (then_bb);
1875 stmt = gimple_build_cond (NE_EXPR, restmp,
1876 build_zero_cst (TREE_TYPE (restmp)),
1877 NULL_TREE, NULL_TREE);
1878 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1879 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1880 e->probability = REG_BR_PROB_BASE - REG_BR_PROB_BASE / 16;
1881 e->count = count - count / 16;
1882 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1883 e->probability = REG_BR_PROB_BASE / 16;
1884 e->count = count / 16;
1885 e = make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1886 e->probability = REG_BR_PROB_BASE;
1887 e->count = count;
1888 e = make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1889 e->probability = REG_BR_PROB_BASE;
1890 e->count = count - count / 16;
1891 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1892 e->probability = REG_BR_PROB_BASE;
1893 e->count = count / 16;
1894 bsi = gsi_last_bb (then_bb);
1897 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1898 fixed_offset, virtual_offset);
1899 if (true_label)
1901 gimple *stmt;
1902 bsi = gsi_last_bb (else_bb);
1903 stmt = gimple_build_assign (restmp,
1904 build_zero_cst (TREE_TYPE (restmp)));
1905 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1906 bsi = gsi_last_bb (return_bb);
1909 else
1910 gimple_call_set_tail (call, true);
1912 /* Build return value. */
1913 if (!DECL_BY_REFERENCE (resdecl))
1914 ret = gimple_build_return (restmp);
1915 else
1916 ret = gimple_build_return (resdecl);
1917 gimple_return_set_retbnd (ret, resbnd);
1919 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1921 else
1923 gimple_call_set_tail (call, true);
1924 remove_edge (single_succ_edge (bb));
1927 cfun->gimple_df->in_ssa_p = true;
1928 profile_status_for_fn (cfun)
1929 = count ? PROFILE_READ : PROFILE_GUESSED;
1930 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1931 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1932 delete_unreachable_blocks ();
1933 update_ssa (TODO_update_ssa);
1934 checking_verify_flow_info ();
1935 free_dominance_info (CDI_DOMINATORS);
1937 /* Since we want to emit the thunk, we explicitly mark its name as
1938 referenced. */
1939 thunk.thunk_p = false;
1940 lowered = true;
1941 bitmap_obstack_release (NULL);
1943 current_function_decl = NULL;
1944 set_cfun (NULL);
1945 return true;
1948 /* Assemble thunks and aliases associated to node. */
1950 void
1951 cgraph_node::assemble_thunks_and_aliases (void)
1953 cgraph_edge *e;
1954 ipa_ref *ref;
1956 for (e = callers; e;)
1957 if (e->caller->thunk.thunk_p
1958 && !e->caller->global.inlined_to
1959 && !e->caller->thunk.add_pointer_bounds_args)
1961 cgraph_node *thunk = e->caller;
1963 e = e->next_caller;
1964 thunk->expand_thunk (true, false);
1965 thunk->assemble_thunks_and_aliases ();
1967 else
1968 e = e->next_caller;
1970 FOR_EACH_ALIAS (this, ref)
1972 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1973 if (!alias->transparent_alias)
1975 bool saved_written = TREE_ASM_WRITTEN (decl);
1977 /* Force assemble_alias to really output the alias this time instead
1978 of buffering it in same alias pairs. */
1979 TREE_ASM_WRITTEN (decl) = 1;
1980 do_assemble_alias (alias->decl,
1981 DECL_ASSEMBLER_NAME (decl));
1982 alias->assemble_thunks_and_aliases ();
1983 TREE_ASM_WRITTEN (decl) = saved_written;
1988 /* Expand function specified by node. */
1990 void
1991 cgraph_node::expand (void)
1993 location_t saved_loc;
1995 /* We ought to not compile any inline clones. */
1996 gcc_assert (!global.inlined_to);
1998 /* __RTL functions are compiled as soon as they are parsed, so don't
1999 do it again. */
2000 if (native_rtl_p ())
2001 return;
2003 announce_function (decl);
2004 process = 0;
2005 gcc_assert (lowered);
2006 get_untransformed_body ();
2008 /* Generate RTL for the body of DECL. */
2010 timevar_push (TV_REST_OF_COMPILATION);
2012 gcc_assert (symtab->global_info_ready);
2014 /* Initialize the default bitmap obstack. */
2015 bitmap_obstack_initialize (NULL);
2017 /* Initialize the RTL code for the function. */
2018 saved_loc = input_location;
2019 input_location = DECL_SOURCE_LOCATION (decl);
2021 gcc_assert (DECL_STRUCT_FUNCTION (decl));
2022 push_cfun (DECL_STRUCT_FUNCTION (decl));
2023 init_function_start (decl);
2025 gimple_register_cfg_hooks ();
2027 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
2029 execute_all_ipa_transforms ();
2031 /* Perform all tree transforms and optimizations. */
2033 /* Signal the start of passes. */
2034 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
2036 execute_pass_list (cfun, g->get_passes ()->all_passes);
2038 /* Signal the end of passes. */
2039 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
2041 bitmap_obstack_release (&reg_obstack);
2043 /* Release the default bitmap obstack. */
2044 bitmap_obstack_release (NULL);
2046 /* If requested, warn about function definitions where the function will
2047 return a value (usually of some struct or union type) which itself will
2048 take up a lot of stack space. */
2049 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
2051 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
2053 if (ret_type && TYPE_SIZE_UNIT (ret_type)
2054 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
2055 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
2056 larger_than_size))
2058 unsigned int size_as_int
2059 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
2061 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
2062 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
2063 decl, size_as_int);
2064 else
2065 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
2066 decl, larger_than_size);
2070 gimple_set_body (decl, NULL);
2071 if (DECL_STRUCT_FUNCTION (decl) == 0
2072 && !cgraph_node::get (decl)->origin)
2074 /* Stop pointing to the local nodes about to be freed.
2075 But DECL_INITIAL must remain nonzero so we know this
2076 was an actual function definition.
2077 For a nested function, this is done in c_pop_function_context.
2078 If rest_of_compilation set this to 0, leave it 0. */
2079 if (DECL_INITIAL (decl) != 0)
2080 DECL_INITIAL (decl) = error_mark_node;
2083 input_location = saved_loc;
2085 ggc_collect ();
2086 timevar_pop (TV_REST_OF_COMPILATION);
2088 /* Make sure that BE didn't give up on compiling. */
2089 gcc_assert (TREE_ASM_WRITTEN (decl));
2090 if (cfun)
2091 pop_cfun ();
2093 /* It would make a lot more sense to output thunks before function body to get more
2094 forward and lest backwarding jumps. This however would need solving problem
2095 with comdats. See PR48668. Also aliases must come after function itself to
2096 make one pass assemblers, like one on AIX, happy. See PR 50689.
2097 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2098 groups. */
2099 assemble_thunks_and_aliases ();
2100 release_body ();
2101 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2102 points to the dead function body. */
2103 remove_callees ();
2104 remove_all_references ();
2107 /* Node comparer that is responsible for the order that corresponds
2108 to time when a function was launched for the first time. */
2110 static int
2111 node_cmp (const void *pa, const void *pb)
2113 const cgraph_node *a = *(const cgraph_node * const *) pa;
2114 const cgraph_node *b = *(const cgraph_node * const *) pb;
2116 /* Functions with time profile must be before these without profile. */
2117 if (!a->tp_first_run || !b->tp_first_run)
2118 return a->tp_first_run - b->tp_first_run;
2120 return a->tp_first_run != b->tp_first_run
2121 ? b->tp_first_run - a->tp_first_run
2122 : b->order - a->order;
2125 /* Expand all functions that must be output.
2127 Attempt to topologically sort the nodes so function is output when
2128 all called functions are already assembled to allow data to be
2129 propagated across the callgraph. Use a stack to get smaller distance
2130 between a function and its callees (later we may choose to use a more
2131 sophisticated algorithm for function reordering; we will likely want
2132 to use subsections to make the output functions appear in top-down
2133 order). */
2135 static void
2136 expand_all_functions (void)
2138 cgraph_node *node;
2139 cgraph_node **order = XCNEWVEC (cgraph_node *,
2140 symtab->cgraph_count);
2141 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2142 int order_pos, new_order_pos = 0;
2143 int i;
2145 order_pos = ipa_reverse_postorder (order);
2146 gcc_assert (order_pos == symtab->cgraph_count);
2148 /* Garbage collector may remove inline clones we eliminate during
2149 optimization. So we must be sure to not reference them. */
2150 for (i = 0; i < order_pos; i++)
2151 if (order[i]->process)
2152 order[new_order_pos++] = order[i];
2154 if (flag_profile_reorder_functions)
2155 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2157 for (i = new_order_pos - 1; i >= 0; i--)
2159 node = order[i];
2161 if (node->process)
2163 expanded_func_count++;
2164 if(node->tp_first_run)
2165 profiled_func_count++;
2167 if (symtab->dump_file)
2168 fprintf (symtab->dump_file,
2169 "Time profile order in expand_all_functions:%s:%d\n",
2170 node->asm_name (), node->tp_first_run);
2171 node->process = 0;
2172 node->expand ();
2176 if (dump_file)
2177 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2178 main_input_filename, profiled_func_count, expanded_func_count);
2180 if (symtab->dump_file && flag_profile_reorder_functions)
2181 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2182 profiled_func_count, expanded_func_count);
2184 symtab->process_new_functions ();
2185 free_gimplify_stack ();
2187 free (order);
2190 /* This is used to sort the node types by the cgraph order number. */
2192 enum cgraph_order_sort_kind
2194 ORDER_UNDEFINED = 0,
2195 ORDER_FUNCTION,
2196 ORDER_VAR,
2197 ORDER_VAR_UNDEF,
2198 ORDER_ASM
2201 struct cgraph_order_sort
2203 enum cgraph_order_sort_kind kind;
2204 union
2206 cgraph_node *f;
2207 varpool_node *v;
2208 asm_node *a;
2209 } u;
2212 /* Output all functions, variables, and asm statements in the order
2213 according to their order fields, which is the order in which they
2214 appeared in the file. This implements -fno-toplevel-reorder. In
2215 this mode we may output functions and variables which don't really
2216 need to be output.
2217 When NO_REORDER is true only do this for symbols marked no reorder. */
2219 static void
2220 output_in_order (bool no_reorder)
2222 int max;
2223 cgraph_order_sort *nodes;
2224 int i;
2225 cgraph_node *pf;
2226 varpool_node *pv;
2227 asm_node *pa;
2228 max = symtab->order;
2229 nodes = XCNEWVEC (cgraph_order_sort, max);
2231 FOR_EACH_DEFINED_FUNCTION (pf)
2233 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2235 if (no_reorder && !pf->no_reorder)
2236 continue;
2237 i = pf->order;
2238 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2239 nodes[i].kind = ORDER_FUNCTION;
2240 nodes[i].u.f = pf;
2244 /* There is a similar loop in symbol_table::output_variables.
2245 Please keep them in sync. */
2246 FOR_EACH_VARIABLE (pv)
2248 if (no_reorder && !pv->no_reorder)
2249 continue;
2250 if (DECL_HARD_REGISTER (pv->decl)
2251 || DECL_HAS_VALUE_EXPR_P (pv->decl))
2252 continue;
2253 i = pv->order;
2254 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2255 nodes[i].kind = pv->definition ? ORDER_VAR : ORDER_VAR_UNDEF;
2256 nodes[i].u.v = pv;
2259 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2261 i = pa->order;
2262 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2263 nodes[i].kind = ORDER_ASM;
2264 nodes[i].u.a = pa;
2267 /* In toplevel reorder mode we output all statics; mark them as needed. */
2269 for (i = 0; i < max; ++i)
2270 if (nodes[i].kind == ORDER_VAR)
2271 nodes[i].u.v->finalize_named_section_flags ();
2273 for (i = 0; i < max; ++i)
2275 switch (nodes[i].kind)
2277 case ORDER_FUNCTION:
2278 nodes[i].u.f->process = 0;
2279 nodes[i].u.f->expand ();
2280 break;
2282 case ORDER_VAR:
2283 nodes[i].u.v->assemble_decl ();
2284 break;
2286 case ORDER_VAR_UNDEF:
2287 assemble_undefined_decl (nodes[i].u.v->decl);
2288 break;
2290 case ORDER_ASM:
2291 assemble_asm (nodes[i].u.a->asm_str);
2292 break;
2294 case ORDER_UNDEFINED:
2295 break;
2297 default:
2298 gcc_unreachable ();
2302 symtab->clear_asm_symbols ();
2304 free (nodes);
2307 static void
2308 ipa_passes (void)
2310 gcc::pass_manager *passes = g->get_passes ();
2312 set_cfun (NULL);
2313 current_function_decl = NULL;
2314 gimple_register_cfg_hooks ();
2315 bitmap_obstack_initialize (NULL);
2317 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2319 if (!in_lto_p)
2321 execute_ipa_pass_list (passes->all_small_ipa_passes);
2322 if (seen_error ())
2323 return;
2326 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2327 devirtualization and other changes where removal iterate. */
2328 symtab->remove_unreachable_nodes (symtab->dump_file);
2330 /* If pass_all_early_optimizations was not scheduled, the state of
2331 the cgraph will not be properly updated. Update it now. */
2332 if (symtab->state < IPA_SSA)
2333 symtab->state = IPA_SSA;
2335 if (!in_lto_p)
2337 /* Generate coverage variables and constructors. */
2338 coverage_finish ();
2340 /* Process new functions added. */
2341 set_cfun (NULL);
2342 current_function_decl = NULL;
2343 symtab->process_new_functions ();
2345 execute_ipa_summary_passes
2346 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2349 /* Some targets need to handle LTO assembler output specially. */
2350 if (flag_generate_lto || flag_generate_offload)
2351 targetm.asm_out.lto_start ();
2353 if (!in_lto_p)
2355 if (g->have_offload)
2357 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2358 lto_stream_offload_p = true;
2359 ipa_write_summaries ();
2360 lto_stream_offload_p = false;
2362 if (flag_lto)
2364 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2365 lto_stream_offload_p = false;
2366 ipa_write_summaries ();
2370 if (flag_generate_lto || flag_generate_offload)
2371 targetm.asm_out.lto_end ();
2373 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2374 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2375 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2377 bitmap_obstack_release (NULL);
2381 /* Return string alias is alias of. */
2383 static tree
2384 get_alias_symbol (tree decl)
2386 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2387 return get_identifier (TREE_STRING_POINTER
2388 (TREE_VALUE (TREE_VALUE (alias))));
2392 /* Weakrefs may be associated to external decls and thus not output
2393 at expansion time. Emit all necessary aliases. */
2395 void
2396 symbol_table::output_weakrefs (void)
2398 symtab_node *node;
2399 cgraph_node *cnode;
2400 FOR_EACH_SYMBOL (node)
2401 if (node->alias
2402 && !TREE_ASM_WRITTEN (node->decl)
2403 && (!(cnode = dyn_cast <cgraph_node *> (node))
2404 || !cnode->instrumented_version
2405 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2406 && node->weakref)
2408 tree target;
2410 /* Weakrefs are special by not requiring target definition in current
2411 compilation unit. It is thus bit hard to work out what we want to
2412 alias.
2413 When alias target is defined, we need to fetch it from symtab reference,
2414 otherwise it is pointed to by alias_target. */
2415 if (node->alias_target)
2416 target = (DECL_P (node->alias_target)
2417 ? DECL_ASSEMBLER_NAME (node->alias_target)
2418 : node->alias_target);
2419 else if (node->analyzed)
2420 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2421 else
2423 gcc_unreachable ();
2424 target = get_alias_symbol (node->decl);
2426 do_assemble_alias (node->decl, target);
2430 /* Perform simple optimizations based on callgraph. */
2432 void
2433 symbol_table::compile (void)
2435 if (seen_error ())
2436 return;
2438 symtab_node::checking_verify_symtab_nodes ();
2440 timevar_push (TV_CGRAPHOPT);
2441 if (pre_ipa_mem_report)
2443 fprintf (stderr, "Memory consumption before IPA\n");
2444 dump_memory_report (false);
2446 if (!quiet_flag)
2447 fprintf (stderr, "Performing interprocedural optimizations\n");
2448 state = IPA;
2450 /* Offloading requires LTO infrastructure. */
2451 if (!in_lto_p && g->have_offload)
2452 flag_generate_offload = 1;
2454 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2455 if (flag_generate_lto || flag_generate_offload)
2456 lto_streamer_hooks_init ();
2458 /* Don't run the IPA passes if there was any error or sorry messages. */
2459 if (!seen_error ())
2460 ipa_passes ();
2462 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2463 if (seen_error ()
2464 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2466 timevar_pop (TV_CGRAPHOPT);
2467 return;
2470 global_info_ready = true;
2471 if (dump_file)
2473 fprintf (dump_file, "Optimized ");
2474 symtab_node:: dump_table (dump_file);
2476 if (post_ipa_mem_report)
2478 fprintf (stderr, "Memory consumption after IPA\n");
2479 dump_memory_report (false);
2481 timevar_pop (TV_CGRAPHOPT);
2483 /* Output everything. */
2484 (*debug_hooks->assembly_start) ();
2485 if (!quiet_flag)
2486 fprintf (stderr, "Assembling functions:\n");
2487 symtab_node::checking_verify_symtab_nodes ();
2489 bitmap_obstack_initialize (NULL);
2490 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2491 bitmap_obstack_release (NULL);
2492 mark_functions_to_output ();
2494 /* When weakref support is missing, we autmatically translate all
2495 references to NODE to references to its ultimate alias target.
2496 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2497 TREE_CHAIN.
2499 Set up this mapping before we output any assembler but once we are sure
2500 that all symbol renaming is done.
2502 FIXME: All this uglyness can go away if we just do renaming at gimple
2503 level by physically rewritting the IL. At the moment we can only redirect
2504 calls, so we need infrastructure for renaming references as well. */
2505 #ifndef ASM_OUTPUT_WEAKREF
2506 symtab_node *node;
2508 FOR_EACH_SYMBOL (node)
2509 if (node->alias
2510 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2512 IDENTIFIER_TRANSPARENT_ALIAS
2513 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2514 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2515 = (node->alias_target ? node->alias_target
2516 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2518 #endif
2520 state = EXPANSION;
2522 if (!flag_toplevel_reorder)
2523 output_in_order (false);
2524 else
2526 /* Output first asm statements and anything ordered. The process
2527 flag is cleared for these nodes, so we skip them later. */
2528 output_in_order (true);
2529 expand_all_functions ();
2530 output_variables ();
2533 process_new_functions ();
2534 state = FINISHED;
2535 output_weakrefs ();
2537 if (dump_file)
2539 fprintf (dump_file, "\nFinal ");
2540 symtab_node::dump_table (dump_file);
2542 if (!flag_checking)
2543 return;
2544 symtab_node::verify_symtab_nodes ();
2545 /* Double check that all inline clones are gone and that all
2546 function bodies have been released from memory. */
2547 if (!seen_error ())
2549 cgraph_node *node;
2550 bool error_found = false;
2552 FOR_EACH_DEFINED_FUNCTION (node)
2553 if (node->global.inlined_to
2554 || gimple_has_body_p (node->decl))
2556 error_found = true;
2557 node->debug ();
2559 if (error_found)
2560 internal_error ("nodes with unreleased memory found");
2565 /* Analyze the whole compilation unit once it is parsed completely. */
2567 void
2568 symbol_table::finalize_compilation_unit (void)
2570 timevar_push (TV_CGRAPH);
2572 /* If we're here there's no current function anymore. Some frontends
2573 are lazy in clearing these. */
2574 current_function_decl = NULL;
2575 set_cfun (NULL);
2577 /* Do not skip analyzing the functions if there were errors, we
2578 miss diagnostics for following functions otherwise. */
2580 /* Emit size functions we didn't inline. */
2581 finalize_size_functions ();
2583 /* Mark alias targets necessary and emit diagnostics. */
2584 handle_alias_pairs ();
2586 if (!quiet_flag)
2588 fprintf (stderr, "\nAnalyzing compilation unit\n");
2589 fflush (stderr);
2592 if (flag_dump_passes)
2593 dump_passes ();
2595 /* Gimplify and lower all functions, compute reachability and
2596 remove unreachable nodes. */
2597 analyze_functions (/*first_time=*/true);
2599 /* Mark alias targets necessary and emit diagnostics. */
2600 handle_alias_pairs ();
2602 /* Gimplify and lower thunks. */
2603 analyze_functions (/*first_time=*/false);
2605 if (!seen_error ())
2607 /* Emit early debug for reachable functions, and by consequence,
2608 locally scoped symbols. */
2609 struct cgraph_node *cnode;
2610 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2611 (*debug_hooks->early_global_decl) (cnode->decl);
2613 /* Clean up anything that needs cleaning up after initial debug
2614 generation. */
2615 (*debug_hooks->early_finish) (main_input_filename);
2618 /* Finally drive the pass manager. */
2619 compile ();
2621 timevar_pop (TV_CGRAPH);
2624 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2625 within the same process. For use by toplev::finalize. */
2627 void
2628 cgraphunit_c_finalize (void)
2630 gcc_assert (cgraph_new_nodes.length () == 0);
2631 cgraph_new_nodes.truncate (0);
2633 vtable_entry_type = NULL;
2634 queued_nodes = &symtab_terminator;
2636 first_analyzed = NULL;
2637 first_analyzed_var = NULL;
2640 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2641 kind of wrapper method. */
2643 void
2644 cgraph_node::create_wrapper (cgraph_node *target)
2646 /* Preserve DECL_RESULT so we get right by reference flag. */
2647 tree decl_result = DECL_RESULT (decl);
2649 /* Remove the function's body but keep arguments to be reused
2650 for thunk. */
2651 release_body (true);
2652 reset ();
2654 DECL_UNINLINABLE (decl) = false;
2655 DECL_RESULT (decl) = decl_result;
2656 DECL_INITIAL (decl) = NULL;
2657 allocate_struct_function (decl, false);
2658 set_cfun (NULL);
2660 /* Turn alias into thunk and expand it into GIMPLE representation. */
2661 definition = true;
2663 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2664 thunk.thunk_p = true;
2665 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2666 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2668 tree arguments = DECL_ARGUMENTS (decl);
2670 while (arguments)
2672 TREE_ADDRESSABLE (arguments) = false;
2673 arguments = TREE_CHAIN (arguments);
2676 expand_thunk (false, true);
2678 /* Inline summary set-up. */
2679 analyze ();
2680 inline_analyze_function (this);
2683 #include "gt-cgraphunit.h"