xfail gnat.dg/trampoline3.adb scan-assembler-not check on hppa*-*-*
[official-gcc.git] / gcc / cgraphunit.cc
blob5c405258ec3080eb79353f9f6dad25480f8005b3
1 /* Driver of optimization process
2 Copyright (C) 2003-2024 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 optimizations 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, transactional 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 streaming. When doing LTO, everything important gets
99 streamed into the object file.
101 Compile time and or linktime analysis stage (WPA):
103 At linktime units gets streamed back and symbol table is
104 merged. Function bodies are not streamed in and not
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 multiple 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 functions (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-iterator.h"
183 #include "gimple-fold.h"
184 #include "gimplify.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-fnsummary.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 "lto-section-names.h"
206 #include "stringpool.h"
207 #include "attribs.h"
208 #include "ipa-inline.h"
209 #include "omp-offload.h"
210 #include "symtab-thunks.h"
212 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
213 secondary queue used during optimization to accommodate passes that
214 may generate new functions that need to be optimized and expanded. */
215 vec<cgraph_node *> cgraph_new_nodes;
217 static void expand_all_functions (void);
218 static void mark_functions_to_output (void);
219 static void handle_alias_pairs (void);
221 /* Return true if this symbol is a function from the C frontend specified
222 directly in RTL form (with "__RTL"). */
224 bool
225 symtab_node::native_rtl_p () const
227 if (TREE_CODE (decl) != FUNCTION_DECL)
228 return false;
229 if (!DECL_STRUCT_FUNCTION (decl))
230 return false;
231 return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
234 /* Determine if symbol declaration is needed. That is, visible to something
235 either outside this translation unit, something magic in the system
236 configury */
237 bool
238 symtab_node::needed_p (void)
240 /* Double check that no one output the function into assembly file
241 early. */
242 if (!native_rtl_p ())
243 gcc_checking_assert
244 (!DECL_ASSEMBLER_NAME_SET_P (decl)
245 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
247 if (!definition)
248 return false;
250 if (DECL_EXTERNAL (decl))
251 return false;
253 /* If the user told us it is used, then it must be so. */
254 if (force_output)
255 return true;
257 /* ABI forced symbols are needed when they are external. */
258 if (forced_by_abi && TREE_PUBLIC (decl))
259 return true;
261 /* Keep constructors, destructors and virtual functions. */
262 if (TREE_CODE (decl) == FUNCTION_DECL
263 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
264 return true;
266 /* Externally visible variables must be output. The exception is
267 COMDAT variables that must be output only when they are needed. */
268 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
269 return true;
271 return false;
274 /* Head and terminator of the queue of nodes to be processed while building
275 callgraph. */
277 static symtab_node symtab_terminator (SYMTAB_SYMBOL);
278 static symtab_node *queued_nodes = &symtab_terminator;
280 /* Add NODE to queue starting at QUEUED_NODES.
281 The queue is linked via AUX pointers and terminated by pointer to 1. */
283 static void
284 enqueue_node (symtab_node *node)
286 if (node->aux)
287 return;
288 gcc_checking_assert (queued_nodes);
289 node->aux = queued_nodes;
290 queued_nodes = node;
293 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
294 functions into callgraph in a way so they look like ordinary reachable
295 functions inserted into callgraph already at construction time. */
297 void
298 symbol_table::process_new_functions (void)
300 tree fndecl;
302 if (!cgraph_new_nodes.exists ())
303 return;
305 handle_alias_pairs ();
306 /* Note that this queue may grow as its being processed, as the new
307 functions may generate new ones. */
308 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
310 cgraph_node *node = cgraph_new_nodes[i];
311 fndecl = node->decl;
312 switch (state)
314 case CONSTRUCTION:
315 /* At construction time we just need to finalize function and move
316 it into reachable functions list. */
318 cgraph_node::finalize_function (fndecl, false);
319 call_cgraph_insertion_hooks (node);
320 enqueue_node (node);
321 break;
323 case IPA:
324 case IPA_SSA:
325 case IPA_SSA_AFTER_INLINING:
326 /* When IPA optimization already started, do all essential
327 transformations that has been already performed on the whole
328 cgraph but not on this function. */
330 gimple_register_cfg_hooks ();
331 if (!node->analyzed)
332 node->analyze ();
333 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
334 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
335 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
337 bool summaried_computed = ipa_fn_summaries != NULL;
338 g->get_passes ()->execute_early_local_passes ();
339 /* Early passes compute inline parameters to do inlining
340 and splitting. This is redundant for functions added late.
341 Just throw away whatever it did. */
342 if (!summaried_computed)
344 ipa_free_fn_summary ();
345 ipa_free_size_summary ();
348 else if (ipa_fn_summaries != NULL)
349 compute_fn_summary (node, true);
350 free_dominance_info (CDI_POST_DOMINATORS);
351 free_dominance_info (CDI_DOMINATORS);
352 pop_cfun ();
353 call_cgraph_insertion_hooks (node);
354 break;
356 case EXPANSION:
357 /* Functions created during expansion shall be compiled
358 directly. */
359 node->process = 0;
360 call_cgraph_insertion_hooks (node);
361 node->expand ();
362 break;
364 default:
365 gcc_unreachable ();
366 break;
370 cgraph_new_nodes.release ();
373 /* As an GCC extension we allow redefinition of the function. The
374 semantics when both copies of bodies differ is not well defined.
375 We replace the old body with new body so in unit at a time mode
376 we always use new body, while in normal mode we may end up with
377 old body inlined into some functions and new body expanded and
378 inlined in others.
380 ??? It may make more sense to use one body for inlining and other
381 body for expanding the function but this is difficult to do.
383 This is also used to cancel C++ mangling aliases, which can be for
384 functions or variables. */
386 void
387 symtab_node::reset (bool preserve_comdat_group)
389 /* Reset our data structures so we can analyze the function again. */
390 analyzed = false;
391 definition = false;
392 alias = false;
393 transparent_alias = false;
394 weakref = false;
395 cpp_implicit_alias = false;
397 remove_all_references ();
398 if (!preserve_comdat_group)
399 remove_from_same_comdat_group ();
401 if (cgraph_node *cn = dyn_cast <cgraph_node *> (this))
403 /* If process is set, then we have already begun whole-unit analysis.
404 This is *not* testing for whether we've already emitted the function.
405 That case can be sort-of legitimately seen with real function
406 redefinition errors. I would argue that the front end should never
407 present us with such a case, but don't enforce that for now. */
408 gcc_assert (!cn->process);
410 memset (&cn->rtl, 0, sizeof (cn->rtl));
411 cn->inlined_to = NULL;
412 cn->remove_callees ();
416 /* Return true when there are references to the node. INCLUDE_SELF is
417 true if a self reference counts as a reference. */
419 bool
420 symtab_node::referred_to_p (bool include_self)
422 ipa_ref *ref = NULL;
424 /* See if there are any references at all. */
425 if (iterate_referring (0, ref))
426 return true;
427 /* For functions check also calls. */
428 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
429 if (cn && cn->callers)
431 if (include_self)
432 return true;
433 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
434 if (e->caller != this)
435 return true;
437 return false;
440 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
441 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
442 the garbage collector run at the moment. We would need to either create
443 a new GC context, or just not compile right now. */
445 void
446 cgraph_node::finalize_function (tree decl, bool no_collect)
448 cgraph_node *node = cgraph_node::get_create (decl);
450 if (node->definition)
452 /* Nested functions should only be defined once. */
453 gcc_assert (!DECL_CONTEXT (decl)
454 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
455 node->reset ();
456 node->redefined_extern_inline = true;
459 /* Set definition first before calling notice_global_symbol so that
460 it is available to notice_global_symbol. */
461 node->definition = true;
462 notice_global_symbol (decl);
463 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
464 node->semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
465 if (!flag_toplevel_reorder)
466 node->no_reorder = true;
468 /* With -fkeep-inline-functions we are keeping all inline functions except
469 for extern inline ones. */
470 if (flag_keep_inline_functions
471 && DECL_DECLARED_INLINE_P (decl)
472 && !DECL_EXTERNAL (decl)
473 && !DECL_DISREGARD_INLINE_LIMITS (decl))
474 node->force_output = 1;
476 /* __RTL functions were already output as soon as they were parsed (due
477 to the large amount of global state in the backend).
478 Mark such functions as "force_output" to reflect the fact that they
479 will be in the asm file when considering the symbols they reference.
480 The attempt to output them later on will bail out immediately. */
481 if (node->native_rtl_p ())
482 node->force_output = 1;
484 /* When not optimizing, also output the static functions. (see
485 PR24561), but don't do so for always_inline functions, functions
486 declared inline and nested functions. These were optimized out
487 in the original implementation and it is unclear whether we want
488 to change the behavior here. */
489 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
490 || node->no_reorder)
491 && !node->cpp_implicit_alias
492 && !DECL_DISREGARD_INLINE_LIMITS (decl)
493 && !DECL_DECLARED_INLINE_P (decl)
494 && !(DECL_CONTEXT (decl)
495 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
496 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
497 node->force_output = 1;
499 /* If we've not yet emitted decl, tell the debug info about it. */
500 if (!TREE_ASM_WRITTEN (decl))
501 (*debug_hooks->deferred_inline_function) (decl);
503 if (!no_collect)
504 ggc_collect ();
506 if (symtab->state == CONSTRUCTION
507 && (node->needed_p () || node->referred_to_p ()))
508 enqueue_node (node);
511 /* Add the function FNDECL to the call graph.
512 Unlike finalize_function, this function is intended to be used
513 by middle end and allows insertion of new function at arbitrary point
514 of compilation. The function can be either in high, low or SSA form
515 GIMPLE.
517 The function is assumed to be reachable and have address taken (so no
518 API breaking optimizations are performed on it).
520 Main work done by this function is to enqueue the function for later
521 processing to avoid need the passes to be re-entrant. */
523 void
524 cgraph_node::add_new_function (tree fndecl, bool lowered)
526 gcc::pass_manager *passes = g->get_passes ();
527 cgraph_node *node;
529 if (dump_file)
531 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
532 const char *function_type = ((gimple_has_body_p (fndecl))
533 ? (lowered
534 ? (gimple_in_ssa_p (fn)
535 ? "ssa gimple"
536 : "low gimple")
537 : "high gimple")
538 : "to-be-gimplified");
539 fprintf (dump_file,
540 "Added new %s function %s to callgraph\n",
541 function_type,
542 fndecl_name (fndecl));
545 switch (symtab->state)
547 case PARSING:
548 cgraph_node::finalize_function (fndecl, false);
549 break;
550 case CONSTRUCTION:
551 /* Just enqueue function to be processed at nearest occurrence. */
552 node = cgraph_node::get_create (fndecl);
553 if (lowered)
554 node->lowered = true;
555 cgraph_new_nodes.safe_push (node);
556 break;
558 case IPA:
559 case IPA_SSA:
560 case IPA_SSA_AFTER_INLINING:
561 case EXPANSION:
562 /* Bring the function into finalized state and enqueue for later
563 analyzing and compilation. */
564 node = cgraph_node::get_create (fndecl);
565 node->local = false;
566 node->definition = true;
567 node->semantic_interposition = opt_for_fn (fndecl,
568 flag_semantic_interposition);
569 node->force_output = true;
570 if (TREE_PUBLIC (fndecl))
571 node->externally_visible = true;
572 if (!lowered && symtab->state == EXPANSION)
574 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
575 gimple_register_cfg_hooks ();
576 bitmap_obstack_initialize (NULL);
577 execute_pass_list (cfun, passes->all_lowering_passes);
578 passes->execute_early_local_passes ();
579 bitmap_obstack_release (NULL);
580 pop_cfun ();
582 lowered = true;
584 if (lowered)
585 node->lowered = true;
586 cgraph_new_nodes.safe_push (node);
587 break;
589 case FINISHED:
590 /* At the very end of compilation we have to do all the work up
591 to expansion. */
592 node = cgraph_node::create (fndecl);
593 if (lowered)
594 node->lowered = true;
595 node->definition = true;
596 node->semantic_interposition = opt_for_fn (fndecl,
597 flag_semantic_interposition);
598 node->analyze ();
599 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
600 gimple_register_cfg_hooks ();
601 bitmap_obstack_initialize (NULL);
602 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
603 g->get_passes ()->execute_early_local_passes ();
604 bitmap_obstack_release (NULL);
605 pop_cfun ();
606 node->expand ();
607 break;
609 default:
610 gcc_unreachable ();
613 /* Set a personality if required and we already passed EH lowering. */
614 if (lowered
615 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
616 == eh_personality_lang))
617 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
620 /* Analyze the function scheduled to be output. */
621 void
622 cgraph_node::analyze (void)
624 if (native_rtl_p ())
626 analyzed = true;
627 return;
630 tree decl = this->decl;
631 location_t saved_loc = input_location;
632 input_location = DECL_SOURCE_LOCATION (decl);
633 semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
635 if (thunk)
637 thunk_info *info = thunk_info::get (this);
638 cgraph_node *t = cgraph_node::get (info->alias);
640 create_edge (t, NULL, t->count);
641 callees->can_throw_external = !TREE_NOTHROW (t->decl);
642 /* Target code in expand_thunk may need the thunk's target
643 to be analyzed, so recurse here. */
644 if (!t->analyzed && t->definition)
645 t->analyze ();
646 if (t->alias)
648 t = t->get_alias_target ();
649 if (!t->analyzed && t->definition)
650 t->analyze ();
652 bool ret = expand_thunk (this, false, false);
653 thunk_info::get (this)->alias = NULL;
654 if (!ret)
655 return;
657 if (alias)
658 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
659 else if (dispatcher_function)
661 /* Generate the dispatcher body of multi-versioned functions. */
662 cgraph_function_version_info *dispatcher_version_info
663 = function_version ();
664 if (dispatcher_version_info != NULL
665 && (dispatcher_version_info->dispatcher_resolver
666 == NULL_TREE))
668 tree resolver = NULL_TREE;
669 gcc_assert (targetm.generate_version_dispatcher_body);
670 resolver = targetm.generate_version_dispatcher_body (this);
671 gcc_assert (resolver != NULL_TREE);
674 else
676 push_cfun (DECL_STRUCT_FUNCTION (decl));
678 assign_assembler_name_if_needed (decl);
680 /* Make sure to gimplify bodies only once. During analyzing a
681 function we lower it, which will require gimplified nested
682 functions, so we can end up here with an already gimplified
683 body. */
684 if (!gimple_has_body_p (decl))
685 gimplify_function_tree (decl);
687 /* Lower the function. */
688 if (!lowered)
690 if (first_nested_function (this))
691 lower_nested_functions (decl);
693 gimple_register_cfg_hooks ();
694 bitmap_obstack_initialize (NULL);
695 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
696 compact_blocks ();
697 bitmap_obstack_release (NULL);
698 lowered = true;
701 pop_cfun ();
703 analyzed = true;
705 input_location = saved_loc;
708 /* C++ frontend produce same body aliases all over the place, even before PCH
709 gets streamed out. It relies on us linking the aliases with their function
710 in order to do the fixups, but ipa-ref is not PCH safe. Consequently we
711 first produce aliases without links, but once C++ FE is sure he won't stream
712 PCH we build the links via this function. */
714 void
715 symbol_table::process_same_body_aliases (void)
717 symtab_node *node;
718 FOR_EACH_SYMBOL (node)
719 if (node->cpp_implicit_alias && !node->analyzed)
720 node->resolve_alias
721 (VAR_P (node->alias_target)
722 ? (symtab_node *)varpool_node::get_create (node->alias_target)
723 : (symtab_node *)cgraph_node::get_create (node->alias_target));
724 cpp_implicit_aliases_done = true;
727 /* Process a symver attribute. */
729 static void
730 process_symver_attribute (symtab_node *n)
732 tree value = lookup_attribute ("symver", DECL_ATTRIBUTES (n->decl));
734 for (; value != NULL; value = TREE_CHAIN (value))
736 /* Starting from bintuils 2.35 gas supports:
737 # Assign foo to bar@V1 and baz@V2.
738 .symver foo, bar@V1
739 .symver foo, baz@V2
741 const char *purpose = IDENTIFIER_POINTER (TREE_PURPOSE (value));
742 if (strcmp (purpose, "symver") != 0)
743 continue;
745 tree symver = get_identifier_with_length
746 (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (value))),
747 TREE_STRING_LENGTH (TREE_VALUE (TREE_VALUE (value))));
748 symtab_node *def = symtab_node::get_for_asmname (symver);
750 if (def)
752 error_at (DECL_SOURCE_LOCATION (n->decl),
753 "duplicate definition of a symbol version");
754 inform (DECL_SOURCE_LOCATION (def->decl),
755 "same version was previously defined here");
756 return;
758 if (!n->definition)
760 error_at (DECL_SOURCE_LOCATION (n->decl),
761 "symbol needs to be defined to have a version");
762 return;
764 if (DECL_COMMON (n->decl))
766 error_at (DECL_SOURCE_LOCATION (n->decl),
767 "common symbol cannot be versioned");
768 return;
770 if (DECL_COMDAT (n->decl))
772 error_at (DECL_SOURCE_LOCATION (n->decl),
773 "comdat symbol cannot be versioned");
774 return;
776 if (n->weakref)
778 error_at (DECL_SOURCE_LOCATION (n->decl),
779 "%<weakref%> cannot be versioned");
780 return;
782 if (!TREE_PUBLIC (n->decl))
784 error_at (DECL_SOURCE_LOCATION (n->decl),
785 "versioned symbol must be public");
786 return;
788 if (DECL_VISIBILITY (n->decl) != VISIBILITY_DEFAULT)
790 error_at (DECL_SOURCE_LOCATION (n->decl),
791 "versioned symbol must have default visibility");
792 return;
795 /* Create new symbol table entry representing the version. */
796 tree new_decl = copy_node (n->decl);
798 DECL_INITIAL (new_decl) = NULL_TREE;
799 if (TREE_CODE (new_decl) == FUNCTION_DECL)
800 DECL_STRUCT_FUNCTION (new_decl) = NULL;
801 SET_DECL_ASSEMBLER_NAME (new_decl, symver);
802 TREE_PUBLIC (new_decl) = 1;
803 DECL_ATTRIBUTES (new_decl) = NULL;
805 symtab_node *symver_node = symtab_node::get_create (new_decl);
806 symver_node->alias = true;
807 symver_node->definition = true;
808 symver_node->symver = true;
809 symver_node->create_reference (n, IPA_REF_ALIAS, NULL);
810 symver_node->analyzed = true;
814 /* Process attributes common for vars and functions. */
816 static void
817 process_common_attributes (symtab_node *node, tree decl)
819 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
821 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
823 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
824 "%<weakref%> attribute should be accompanied with"
825 " an %<alias%> attribute");
826 DECL_WEAK (decl) = 0;
827 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
828 DECL_ATTRIBUTES (decl));
831 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
832 node->no_reorder = 1;
833 process_symver_attribute (node);
836 /* Look for externally_visible and used attributes and mark cgraph nodes
837 accordingly.
839 We cannot mark the nodes at the point the attributes are processed (in
840 handle_*_attribute) because the copy of the declarations available at that
841 point may not be canonical. For example, in:
843 void f();
844 void f() __attribute__((used));
846 the declaration we see in handle_used_attribute will be the second
847 declaration -- but the front end will subsequently merge that declaration
848 with the original declaration and discard the second declaration.
850 Furthermore, we can't mark these nodes in finalize_function because:
852 void f() {}
853 void f() __attribute__((externally_visible));
855 is valid.
857 So, we walk the nodes at the end of the translation unit, applying the
858 attributes at that point. */
860 static void
861 process_function_and_variable_attributes (cgraph_node *first,
862 varpool_node *first_var)
864 cgraph_node *node;
865 varpool_node *vnode;
867 for (node = symtab->first_function (); node != first;
868 node = symtab->next_function (node))
870 tree decl = node->decl;
872 if (node->alias
873 && lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
875 tree tdecl = node->get_alias_target_tree ();
876 if (!tdecl || !DECL_P (tdecl)
877 || !lookup_attribute ("flatten", DECL_ATTRIBUTES (tdecl)))
878 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
879 "%<flatten%> attribute is ignored on aliases");
881 if (DECL_PRESERVE_P (decl))
882 node->mark_force_output ();
883 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
885 if (! TREE_PUBLIC (node->decl))
886 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
887 "%<externally_visible%>"
888 " attribute have effect only on public objects");
890 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
891 && node->definition
892 && (!node->alias || DECL_INITIAL (decl) != error_mark_node))
894 /* NODE->DEFINITION && NODE->ALIAS is nonzero for valid weakref
895 function declarations; DECL_INITIAL is non-null for invalid
896 weakref functions that are also defined. */
897 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
898 "%<weakref%> attribute ignored"
899 " because function is defined");
900 DECL_WEAK (decl) = 0;
901 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
902 DECL_ATTRIBUTES (decl));
903 DECL_ATTRIBUTES (decl) = remove_attribute ("alias",
904 DECL_ATTRIBUTES (decl));
905 node->alias = false;
906 node->weakref = false;
907 node->transparent_alias = false;
909 else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
910 && node->definition
911 && !node->alias)
912 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
913 "%<alias%> attribute ignored"
914 " because function is defined");
916 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
917 && !DECL_DECLARED_INLINE_P (decl)
918 /* redefining extern inline function makes it DECL_UNINLINABLE. */
919 && !DECL_UNINLINABLE (decl))
920 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
921 "%<always_inline%> function might not be inlinable"
922 " unless also declared %<inline%>");
924 process_common_attributes (node, decl);
926 for (vnode = symtab->first_variable (); vnode != first_var;
927 vnode = symtab->next_variable (vnode))
929 tree decl = vnode->decl;
930 if (DECL_EXTERNAL (decl)
931 && DECL_INITIAL (decl))
932 varpool_node::finalize_decl (decl);
933 if (DECL_PRESERVE_P (decl))
934 vnode->force_output = true;
935 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
937 if (! TREE_PUBLIC (vnode->decl))
938 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
939 "%<externally_visible%>"
940 " attribute have effect only on public objects");
942 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
943 && vnode->definition
944 && DECL_INITIAL (decl))
946 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
947 "%<weakref%> attribute ignored"
948 " because variable is initialized");
949 DECL_WEAK (decl) = 0;
950 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
951 DECL_ATTRIBUTES (decl));
953 process_common_attributes (vnode, decl);
957 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
958 middle end to output the variable to asm file, if needed or externally
959 visible. */
961 void
962 varpool_node::finalize_decl (tree decl)
964 varpool_node *node = varpool_node::get_create (decl);
966 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
968 if (node->definition)
969 return;
970 /* Set definition first before calling notice_global_symbol so that
971 it is available to notice_global_symbol. */
972 node->definition = true;
973 node->semantic_interposition = flag_semantic_interposition;
974 notice_global_symbol (decl);
975 if (!flag_toplevel_reorder)
976 node->no_reorder = true;
977 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
978 /* Traditionally we do not eliminate static variables when not
979 optimizing and when not doing toplevel reorder. */
980 || (node->no_reorder && !DECL_COMDAT (node->decl)
981 && !DECL_ARTIFICIAL (node->decl)))
982 node->force_output = true;
984 if (symtab->state == CONSTRUCTION
985 && (node->needed_p () || node->referred_to_p ()))
986 enqueue_node (node);
987 if (symtab->state >= IPA_SSA)
988 node->analyze ();
989 /* Some frontends produce various interface variables after compilation
990 finished. */
991 if (symtab->state == FINISHED
992 || (node->no_reorder
993 && symtab->state == EXPANSION))
994 node->assemble_decl ();
997 /* EDGE is an polymorphic call. Mark all possible targets as reachable
998 and if there is only one target, perform trivial devirtualization.
999 REACHABLE_CALL_TARGETS collects target lists we already walked to
1000 avoid duplicate work. */
1002 static void
1003 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
1004 cgraph_edge *edge)
1006 unsigned int i;
1007 void *cache_token;
1008 bool final;
1009 vec <cgraph_node *>targets
1010 = possible_polymorphic_call_targets
1011 (edge, &final, &cache_token);
1013 if (cache_token != NULL && !reachable_call_targets->add (cache_token))
1015 if (symtab->dump_file)
1016 dump_possible_polymorphic_call_targets
1017 (symtab->dump_file, edge);
1019 for (i = 0; i < targets.length (); i++)
1021 /* Do not bother to mark virtual methods in anonymous namespace;
1022 either we will find use of virtual table defining it, or it is
1023 unused. */
1024 if (targets[i]->definition
1025 && TREE_CODE
1026 (TREE_TYPE (targets[i]->decl))
1027 == METHOD_TYPE
1028 && !type_in_anonymous_namespace_p
1029 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
1030 enqueue_node (targets[i]);
1034 /* Very trivial devirtualization; when the type is
1035 final or anonymous (so we know all its derivation)
1036 and there is only one possible virtual call target,
1037 make the edge direct. */
1038 if (final)
1040 if (targets.length () <= 1 && dbg_cnt (devirt))
1042 cgraph_node *target;
1043 if (targets.length () == 1)
1044 target = targets[0];
1045 else
1046 target = cgraph_node::create (builtin_decl_unreachable ());
1048 if (symtab->dump_file)
1050 fprintf (symtab->dump_file,
1051 "Devirtualizing call: ");
1052 print_gimple_stmt (symtab->dump_file,
1053 edge->call_stmt, 0,
1054 TDF_SLIM);
1056 if (dump_enabled_p ())
1058 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
1059 "devirtualizing call in %s to %s\n",
1060 edge->caller->dump_name (),
1061 target->dump_name ());
1064 edge = cgraph_edge::make_direct (edge, target);
1065 gimple *new_call = cgraph_edge::redirect_call_stmt_to_callee (edge);
1067 if (symtab->dump_file)
1069 fprintf (symtab->dump_file, "Devirtualized as: ");
1070 print_gimple_stmt (symtab->dump_file, new_call, 0, TDF_SLIM);
1076 /* Issue appropriate warnings for the global declaration DECL. */
1078 static void
1079 check_global_declaration (symtab_node *snode)
1081 const char *decl_file;
1082 tree decl = snode->decl;
1084 /* Warn about any function declared static but not defined. We don't
1085 warn about variables, because many programs have static variables
1086 that exist only to get some text into the object file. */
1087 if (TREE_CODE (decl) == FUNCTION_DECL
1088 && DECL_INITIAL (decl) == 0
1089 && DECL_EXTERNAL (decl)
1090 && ! DECL_ARTIFICIAL (decl)
1091 && ! TREE_PUBLIC (decl))
1093 if (warning_suppressed_p (decl, OPT_Wunused))
1095 else if (snode->referred_to_p (/*include_self=*/false))
1096 pedwarn (input_location, 0, "%q+F used but never defined", decl);
1097 else
1098 warning (OPT_Wunused_function, "%q+F declared %<static%> but never "
1099 "defined", decl);
1102 /* Warn about static fns or vars defined but not used. */
1103 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
1104 || (((warn_unused_variable && ! TREE_READONLY (decl))
1105 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
1106 && (warn_unused_const_variable == 2
1107 || (main_input_filename != NULL
1108 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
1109 && filename_cmp (main_input_filename,
1110 decl_file) == 0))))
1111 && VAR_P (decl)))
1112 && ! DECL_IN_SYSTEM_HEADER (decl)
1113 && ! snode->referred_to_p (/*include_self=*/false)
1114 /* This TREE_USED check is needed in addition to referred_to_p
1115 above, because the `__unused__' attribute is not being
1116 considered for referred_to_p. */
1117 && ! TREE_USED (decl)
1118 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1119 to handle multiple external decls in different scopes. */
1120 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1121 && ! DECL_EXTERNAL (decl)
1122 && ! DECL_ARTIFICIAL (decl)
1123 && ! DECL_ABSTRACT_ORIGIN (decl)
1124 && ! TREE_PUBLIC (decl)
1125 /* A volatile variable might be used in some non-obvious way. */
1126 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1127 /* Global register variables must be declared to reserve them. */
1128 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1129 /* Global ctors and dtors are called by the runtime. */
1130 && (TREE_CODE (decl) != FUNCTION_DECL
1131 || (!DECL_STATIC_CONSTRUCTOR (decl)
1132 && !DECL_STATIC_DESTRUCTOR (decl)))
1133 && (! VAR_P (decl) || !warning_suppressed_p (decl, OPT_Wunused_variable))
1134 /* Otherwise, ask the language. */
1135 && lang_hooks.decls.warn_unused_global (decl))
1136 warning_at (DECL_SOURCE_LOCATION (decl),
1137 (TREE_CODE (decl) == FUNCTION_DECL)
1138 ? OPT_Wunused_function
1139 : (TREE_READONLY (decl)
1140 ? OPT_Wunused_const_variable_
1141 : OPT_Wunused_variable),
1142 "%qD defined but not used", decl);
1145 /* Discover all functions and variables that are trivially needed, analyze
1146 them as well as all functions and variables referred by them */
1147 static cgraph_node *first_analyzed;
1148 static varpool_node *first_analyzed_var;
1150 /* FIRST_TIME is set to TRUE for the first time we are called for a
1151 translation unit from finalize_compilation_unit() or false
1152 otherwise. */
1154 static void
1155 analyze_functions (bool first_time)
1157 /* Keep track of already processed nodes when called multiple times for
1158 intermodule optimization. */
1159 cgraph_node *first_handled = first_analyzed;
1160 varpool_node *first_handled_var = first_analyzed_var;
1161 hash_set<void *> reachable_call_targets;
1163 symtab_node *node;
1164 symtab_node *next;
1165 int i;
1166 ipa_ref *ref;
1167 bool changed = true;
1168 location_t saved_loc = input_location;
1170 bitmap_obstack_initialize (NULL);
1171 symtab->state = CONSTRUCTION;
1172 input_location = UNKNOWN_LOCATION;
1174 thunk_info::process_early_thunks ();
1176 /* Ugly, but the fixup cannot happen at a time same body alias is created;
1177 C++ FE is confused about the COMDAT groups being right. */
1178 if (symtab->cpp_implicit_aliases_done)
1179 FOR_EACH_SYMBOL (node)
1180 if (node->cpp_implicit_alias)
1181 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1182 build_type_inheritance_graph ();
1184 if (flag_openmp && first_time)
1185 omp_discover_implicit_declare_target ();
1187 /* Analysis adds static variables that in turn adds references to new functions.
1188 So we need to iterate the process until it stabilize. */
1189 while (changed)
1191 changed = false;
1192 process_function_and_variable_attributes (first_analyzed,
1193 first_analyzed_var);
1195 /* First identify the trivially needed symbols. */
1196 for (node = symtab->first_symbol ();
1197 node != first_analyzed
1198 && node != first_analyzed_var; node = node->next)
1200 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1201 node->get_comdat_group_id ();
1202 if (node->needed_p ())
1204 enqueue_node (node);
1205 if (!changed && symtab->dump_file)
1206 fprintf (symtab->dump_file, "Trivially needed symbols:");
1207 changed = true;
1208 if (symtab->dump_file)
1209 fprintf (symtab->dump_file, " %s", node->dump_asm_name ());
1211 if (node == first_analyzed
1212 || node == first_analyzed_var)
1213 break;
1215 symtab->process_new_functions ();
1216 first_analyzed_var = symtab->first_variable ();
1217 first_analyzed = symtab->first_function ();
1219 if (changed && symtab->dump_file)
1220 fprintf (symtab->dump_file, "\n");
1222 /* Lower representation, build callgraph edges and references for all trivially
1223 needed symbols and all symbols referred by them. */
1224 while (queued_nodes != &symtab_terminator)
1226 changed = true;
1227 node = queued_nodes;
1228 queued_nodes = (symtab_node *)queued_nodes->aux;
1229 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1230 if (cnode && cnode->definition)
1232 cgraph_edge *edge;
1233 tree decl = cnode->decl;
1235 /* ??? It is possible to create extern inline function
1236 and later using weak alias attribute to kill its body.
1237 See gcc.c-torture/compile/20011119-1.c */
1238 if (!DECL_STRUCT_FUNCTION (decl)
1239 && !cnode->alias
1240 && !cnode->thunk
1241 && !cnode->dispatcher_function)
1243 cnode->reset ();
1244 cnode->redefined_extern_inline = true;
1245 continue;
1248 if (!cnode->analyzed)
1249 cnode->analyze ();
1251 for (edge = cnode->callees; edge; edge = edge->next_callee)
1252 if (edge->callee->definition
1253 && (!DECL_EXTERNAL (edge->callee->decl)
1254 /* When not optimizing, do not try to analyze extern
1255 inline functions. Doing so is pointless. */
1256 || opt_for_fn (edge->callee->decl, optimize)
1257 /* Weakrefs needs to be preserved. */
1258 || edge->callee->alias
1259 /* always_inline functions are inlined even at -O0. */
1260 || lookup_attribute
1261 ("always_inline",
1262 DECL_ATTRIBUTES (edge->callee->decl))
1263 /* Multiversioned functions needs the dispatcher to
1264 be produced locally even for extern functions. */
1265 || edge->callee->function_version ()))
1266 enqueue_node (edge->callee);
1267 if (opt_for_fn (cnode->decl, optimize)
1268 && opt_for_fn (cnode->decl, flag_devirtualize))
1270 cgraph_edge *next;
1272 for (edge = cnode->indirect_calls; edge; edge = next)
1274 next = edge->next_callee;
1275 if (edge->indirect_info->polymorphic)
1276 walk_polymorphic_call_targets (&reachable_call_targets,
1277 edge);
1281 /* If decl is a clone of an abstract function,
1282 mark that abstract function so that we don't release its body.
1283 The DECL_INITIAL() of that abstract function declaration
1284 will be later needed to output debug info. */
1285 if (DECL_ABSTRACT_ORIGIN (decl))
1287 cgraph_node *origin_node
1288 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1289 origin_node->used_as_abstract_origin = true;
1291 /* Preserve a functions function context node. It will
1292 later be needed to output debug info. */
1293 if (tree fn = decl_function_context (decl))
1295 cgraph_node *origin_node = cgraph_node::get_create (fn);
1296 enqueue_node (origin_node);
1299 else
1301 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1302 if (vnode && vnode->definition && !vnode->analyzed)
1303 vnode->analyze ();
1306 if (node->same_comdat_group)
1308 symtab_node *next;
1309 for (next = node->same_comdat_group;
1310 next != node;
1311 next = next->same_comdat_group)
1312 if (!next->comdat_local_p ())
1313 enqueue_node (next);
1315 for (i = 0; node->iterate_reference (i, ref); i++)
1316 if (ref->referred->definition
1317 && (!DECL_EXTERNAL (ref->referred->decl)
1318 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1319 && optimize)
1320 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1321 && opt_for_fn (ref->referred->decl, optimize))
1322 || node->alias
1323 || ref->referred->alias)))
1324 enqueue_node (ref->referred);
1325 symtab->process_new_functions ();
1328 update_type_inheritance_graph ();
1330 /* Collect entry points to the unit. */
1331 if (symtab->dump_file)
1333 fprintf (symtab->dump_file, "\n\nInitial ");
1334 symtab->dump (symtab->dump_file);
1337 if (first_time)
1339 symtab_node *snode;
1340 FOR_EACH_SYMBOL (snode)
1341 check_global_declaration (snode);
1344 if (symtab->dump_file)
1345 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1347 for (node = symtab->first_symbol ();
1348 node != first_handled
1349 && node != first_handled_var; node = next)
1351 next = node->next;
1352 /* For symbols declared locally we clear TREE_READONLY when emitting
1353 the constructor (if one is needed). For external declarations we can
1354 not safely assume that the type is readonly because we may be called
1355 during its construction. */
1356 if (TREE_CODE (node->decl) == VAR_DECL
1357 && TYPE_P (TREE_TYPE (node->decl))
1358 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (node->decl))
1359 && DECL_EXTERNAL (node->decl))
1360 TREE_READONLY (node->decl) = 0;
1361 if (!node->aux && !node->referred_to_p ())
1363 if (symtab->dump_file)
1364 fprintf (symtab->dump_file, " %s", node->dump_name ());
1366 /* See if the debugger can use anything before the DECL
1367 passes away. Perhaps it can notice a DECL that is now a
1368 constant and can tag the early DIE with an appropriate
1369 attribute.
1371 Otherwise, this is the last chance the debug_hooks have
1372 at looking at optimized away DECLs, since
1373 late_global_decl will subsequently be called from the
1374 contents of the now pruned symbol table. */
1375 if (VAR_P (node->decl)
1376 && !decl_function_context (node->decl))
1378 /* We are reclaiming totally unreachable code and variables
1379 so they effectively appear as readonly. Show that to
1380 the debug machinery. */
1381 TREE_READONLY (node->decl) = 1;
1382 node->definition = false;
1383 (*debug_hooks->late_global_decl) (node->decl);
1386 node->remove ();
1387 continue;
1389 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1391 tree decl = node->decl;
1393 if (cnode->definition && !gimple_has_body_p (decl)
1394 && !cnode->alias
1395 && !cnode->thunk)
1396 cnode->reset ();
1398 gcc_assert (!cnode->definition || cnode->thunk
1399 || cnode->alias
1400 || gimple_has_body_p (decl)
1401 || cnode->native_rtl_p ());
1402 gcc_assert (cnode->analyzed == cnode->definition);
1404 node->aux = NULL;
1406 for (;node; node = node->next)
1407 node->aux = NULL;
1408 first_analyzed = symtab->first_function ();
1409 first_analyzed_var = symtab->first_variable ();
1410 if (symtab->dump_file)
1412 fprintf (symtab->dump_file, "\n\nReclaimed ");
1413 symtab->dump (symtab->dump_file);
1415 bitmap_obstack_release (NULL);
1416 ggc_collect ();
1417 /* Initialize assembler name hash, in particular we want to trigger C++
1418 mangling and same body alias creation before we free DECL_ARGUMENTS
1419 used by it. */
1420 if (!seen_error ())
1421 symtab->symtab_initialize_asm_name_hash ();
1423 input_location = saved_loc;
1426 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1427 (which may be an ifunc resolver) and issue a diagnostic when they are
1428 not compatible according to language rules (plus a C++ extension for
1429 non-static member functions). */
1431 static void
1432 maybe_diag_incompatible_alias (tree alias, tree target)
1434 tree altype = TREE_TYPE (alias);
1435 tree targtype = TREE_TYPE (target);
1437 bool ifunc = cgraph_node::get (alias)->ifunc_resolver;
1438 tree funcptr = altype;
1440 if (ifunc)
1442 /* Handle attribute ifunc first. */
1443 if (TREE_CODE (altype) == METHOD_TYPE)
1445 /* Set FUNCPTR to the type of the alias target. If the type
1446 is a non-static member function of class C, construct a type
1447 of an ordinary function taking C* as the first argument,
1448 followed by the member function argument list, and use it
1449 instead to check for incompatibility. This conversion is
1450 not defined by the language but an extension provided by
1451 G++. */
1453 tree rettype = TREE_TYPE (altype);
1454 tree args = TYPE_ARG_TYPES (altype);
1455 altype = build_function_type (rettype, args);
1456 funcptr = altype;
1459 targtype = TREE_TYPE (targtype);
1461 if (POINTER_TYPE_P (targtype))
1463 targtype = TREE_TYPE (targtype);
1465 /* Only issue Wattribute-alias for conversions to void* with
1466 -Wextra. */
1467 if (VOID_TYPE_P (targtype) && !extra_warnings)
1468 return;
1470 /* Proceed to handle incompatible ifunc resolvers below. */
1472 else
1474 funcptr = build_pointer_type (funcptr);
1476 error_at (DECL_SOURCE_LOCATION (target),
1477 "%<ifunc%> resolver for %qD must return %qT",
1478 alias, funcptr);
1479 inform (DECL_SOURCE_LOCATION (alias),
1480 "resolver indirect function declared here");
1481 return;
1485 if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1486 || (prototype_p (altype)
1487 && prototype_p (targtype)
1488 && !types_compatible_p (altype, targtype))))
1490 /* Warn for incompatibilities. Avoid warning for functions
1491 without a prototype to make it possible to declare aliases
1492 without knowing the exact type, as libstdc++ does. */
1493 if (ifunc)
1495 funcptr = build_pointer_type (funcptr);
1497 auto_diagnostic_group d;
1498 if (warning_at (DECL_SOURCE_LOCATION (target),
1499 OPT_Wattribute_alias_,
1500 "%<ifunc%> resolver for %qD should return %qT",
1501 alias, funcptr))
1502 inform (DECL_SOURCE_LOCATION (alias),
1503 "resolver indirect function declared here");
1505 else
1507 auto_diagnostic_group d;
1508 if (warning_at (DECL_SOURCE_LOCATION (alias),
1509 OPT_Wattribute_alias_,
1510 "%qD alias between functions of incompatible "
1511 "types %qT and %qT", alias, altype, targtype))
1512 inform (DECL_SOURCE_LOCATION (target),
1513 "aliased declaration here");
1518 /* Translate the ugly representation of aliases as alias pairs into nice
1519 representation in callgraph. We don't handle all cases yet,
1520 unfortunately. */
1522 static void
1523 handle_alias_pairs (void)
1525 alias_pair *p;
1526 unsigned i;
1528 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1530 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1532 /* Weakrefs with target not defined in current unit are easy to handle:
1533 they behave just as external variables except we need to note the
1534 alias flag to later output the weakref pseudo op into asm file. */
1535 if (!target_node
1536 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1538 symtab_node *node = symtab_node::get (p->decl);
1539 if (node)
1541 node->alias_target = p->target;
1542 node->weakref = true;
1543 node->alias = true;
1544 node->transparent_alias = true;
1546 alias_pairs->unordered_remove (i);
1547 continue;
1549 else if (!target_node)
1551 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1552 symtab_node *node = symtab_node::get (p->decl);
1553 if (node)
1554 node->alias = false;
1555 alias_pairs->unordered_remove (i);
1556 continue;
1559 if (DECL_EXTERNAL (target_node->decl)
1560 /* We use local aliases for C++ thunks to force the tailcall
1561 to bind locally. This is a hack - to keep it working do
1562 the following (which is not strictly correct). */
1563 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1564 || ! DECL_VIRTUAL_P (target_node->decl))
1565 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1567 error ("%q+D aliased to external symbol %qE",
1568 p->decl, p->target);
1571 if (TREE_CODE (p->decl) == FUNCTION_DECL
1572 && target_node && is_a <cgraph_node *> (target_node))
1574 maybe_diag_incompatible_alias (p->decl, target_node->decl);
1576 maybe_diag_alias_attributes (p->decl, target_node->decl);
1578 cgraph_node *src_node = cgraph_node::get (p->decl);
1579 if (src_node && src_node->definition)
1580 src_node->reset ();
1581 cgraph_node::create_alias (p->decl, target_node->decl);
1582 alias_pairs->unordered_remove (i);
1584 else if (VAR_P (p->decl)
1585 && target_node && is_a <varpool_node *> (target_node))
1587 varpool_node::create_alias (p->decl, target_node->decl);
1588 alias_pairs->unordered_remove (i);
1590 else
1592 error ("%q+D alias between function and variable is not supported",
1593 p->decl);
1594 inform (DECL_SOURCE_LOCATION (target_node->decl),
1595 "aliased declaration here");
1597 alias_pairs->unordered_remove (i);
1600 vec_free (alias_pairs);
1604 /* Figure out what functions we want to assemble. */
1606 static void
1607 mark_functions_to_output (void)
1609 bool check_same_comdat_groups = false;
1610 cgraph_node *node;
1612 if (flag_checking)
1613 FOR_EACH_FUNCTION (node)
1614 gcc_assert (!node->process);
1616 FOR_EACH_FUNCTION (node)
1618 tree decl = node->decl;
1620 gcc_assert (!node->process || node->same_comdat_group);
1621 if (node->process)
1622 continue;
1624 /* We need to output all local functions that are used and not
1625 always inlined, as well as those that are reachable from
1626 outside the current compilation unit. */
1627 if (node->analyzed
1628 && !node->thunk
1629 && !node->alias
1630 && !node->inlined_to
1631 && !TREE_ASM_WRITTEN (decl)
1632 && !DECL_EXTERNAL (decl))
1634 node->process = 1;
1635 if (node->same_comdat_group)
1637 cgraph_node *next;
1638 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1639 next != node;
1640 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1641 if (!next->thunk && !next->alias
1642 && !next->comdat_local_p ())
1643 next->process = 1;
1646 else if (node->same_comdat_group)
1648 if (flag_checking)
1649 check_same_comdat_groups = true;
1651 else
1653 /* We should've reclaimed all functions that are not needed. */
1654 if (flag_checking
1655 && !node->inlined_to
1656 && gimple_has_body_p (decl)
1657 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1658 are inside partition, we can end up not removing the body since we no longer
1659 have analyzed node pointing to it. */
1660 && !node->in_other_partition
1661 && !node->alias
1662 && !node->clones
1663 && !DECL_EXTERNAL (decl))
1665 node->debug ();
1666 internal_error ("failed to reclaim unneeded function");
1668 gcc_assert (node->inlined_to
1669 || !gimple_has_body_p (decl)
1670 || node->in_other_partition
1671 || node->clones
1672 || DECL_ARTIFICIAL (decl)
1673 || DECL_EXTERNAL (decl));
1678 if (flag_checking && check_same_comdat_groups)
1679 FOR_EACH_FUNCTION (node)
1680 if (node->same_comdat_group && !node->process)
1682 tree decl = node->decl;
1683 if (!node->inlined_to
1684 && gimple_has_body_p (decl)
1685 /* FIXME: in an ltrans unit when the offline copy is outside a
1686 partition but inline copies are inside a partition, we can
1687 end up not removing the body since we no longer have an
1688 analyzed node pointing to it. */
1689 && !node->in_other_partition
1690 && !node->clones
1691 && !DECL_EXTERNAL (decl))
1693 node->debug ();
1694 internal_error ("failed to reclaim unneeded function in same "
1695 "comdat group");
1700 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1701 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1703 Set current_function_decl and cfun to newly constructed empty function body.
1704 return basic block in the function body. */
1706 basic_block
1707 init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1709 basic_block bb;
1710 edge e;
1712 current_function_decl = decl;
1713 allocate_struct_function (decl, false);
1714 gimple_register_cfg_hooks ();
1715 init_empty_tree_cfg ();
1716 init_tree_ssa (cfun);
1718 if (in_ssa)
1720 init_ssa_operands (cfun);
1721 cfun->gimple_df->in_ssa_p = true;
1722 cfun->curr_properties |= PROP_ssa;
1725 DECL_INITIAL (decl) = make_node (BLOCK);
1726 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1728 DECL_SAVED_TREE (decl) = error_mark_node;
1729 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1730 | PROP_cfg | PROP_loops);
1732 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1733 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1734 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1736 /* Create BB for body of the function and connect it properly. */
1737 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1738 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1739 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1740 bb->count = count;
1741 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1742 e->probability = profile_probability::always ();
1743 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1744 e->probability = profile_probability::always ();
1745 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1747 return bb;
1750 /* Assemble thunks and aliases associated to node. */
1752 void
1753 cgraph_node::assemble_thunks_and_aliases (void)
1755 cgraph_edge *e;
1756 ipa_ref *ref;
1758 for (e = callers; e;)
1759 if (e->caller->thunk
1760 && !e->caller->inlined_to)
1762 cgraph_node *thunk = e->caller;
1764 e = e->next_caller;
1765 expand_thunk (thunk, !rtl_dump_and_exit, false);
1766 thunk->assemble_thunks_and_aliases ();
1768 else
1769 e = e->next_caller;
1771 FOR_EACH_ALIAS (this, ref)
1773 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1774 if (!alias->transparent_alias)
1776 bool saved_written = TREE_ASM_WRITTEN (decl);
1778 /* Force assemble_alias to really output the alias this time instead
1779 of buffering it in same alias pairs. */
1780 TREE_ASM_WRITTEN (decl) = 1;
1781 if (alias->symver)
1782 do_assemble_symver (alias->decl,
1783 DECL_ASSEMBLER_NAME (decl));
1784 else
1785 do_assemble_alias (alias->decl,
1786 DECL_ASSEMBLER_NAME (decl));
1787 alias->assemble_thunks_and_aliases ();
1788 TREE_ASM_WRITTEN (decl) = saved_written;
1793 /* Expand function specified by node. */
1795 void
1796 cgraph_node::expand (void)
1798 location_t saved_loc;
1800 /* We ought to not compile any inline clones. */
1801 gcc_assert (!inlined_to);
1803 /* __RTL functions are compiled as soon as they are parsed, so don't
1804 do it again. */
1805 if (native_rtl_p ())
1806 return;
1808 announce_function (decl);
1809 process = 0;
1810 gcc_assert (lowered);
1812 /* Initialize the default bitmap obstack. */
1813 bitmap_obstack_initialize (NULL);
1814 get_untransformed_body ();
1816 /* Generate RTL for the body of DECL. */
1818 timevar_push (TV_REST_OF_COMPILATION);
1820 gcc_assert (symtab->global_info_ready);
1822 /* Initialize the RTL code for the function. */
1823 saved_loc = input_location;
1824 input_location = DECL_SOURCE_LOCATION (decl);
1826 gcc_assert (DECL_STRUCT_FUNCTION (decl));
1827 push_cfun (DECL_STRUCT_FUNCTION (decl));
1828 init_function_start (decl);
1830 gimple_register_cfg_hooks ();
1832 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1834 update_ssa (TODO_update_ssa_only_virtuals);
1835 if (ipa_transforms_to_apply.exists ())
1836 execute_all_ipa_transforms (false);
1838 /* Perform all tree transforms and optimizations. */
1840 /* Signal the start of passes. */
1841 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1843 execute_pass_list (cfun, g->get_passes ()->all_passes);
1845 /* Signal the end of passes. */
1846 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1848 bitmap_obstack_release (&reg_obstack);
1850 /* Release the default bitmap obstack. */
1851 bitmap_obstack_release (NULL);
1853 /* If requested, warn about function definitions where the function will
1854 return a value (usually of some struct or union type) which itself will
1855 take up a lot of stack space. */
1856 if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1858 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1860 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1861 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1862 && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1863 warn_larger_than_size) > 0)
1865 unsigned int size_as_int
1866 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1868 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1869 warning (OPT_Wlarger_than_,
1870 "size of return value of %q+D is %u bytes",
1871 decl, size_as_int);
1872 else
1873 warning (OPT_Wlarger_than_,
1874 "size of return value of %q+D is larger than %wu bytes",
1875 decl, warn_larger_than_size);
1879 gimple_set_body (decl, NULL);
1880 if (DECL_STRUCT_FUNCTION (decl) == 0)
1882 /* Stop pointing to the local nodes about to be freed.
1883 But DECL_INITIAL must remain nonzero so we know this
1884 was an actual function definition. */
1885 if (DECL_INITIAL (decl) != 0)
1886 DECL_INITIAL (decl) = error_mark_node;
1889 input_location = saved_loc;
1891 ggc_collect ();
1892 timevar_pop (TV_REST_OF_COMPILATION);
1894 if (DECL_STRUCT_FUNCTION (decl)
1895 && DECL_STRUCT_FUNCTION (decl)->assume_function)
1897 /* Assume functions aren't expanded into RTL, on the other side
1898 we don't want to release their body. */
1899 if (cfun)
1900 pop_cfun ();
1901 return;
1904 /* Make sure that BE didn't give up on compiling. */
1905 gcc_assert (TREE_ASM_WRITTEN (decl));
1906 if (cfun)
1907 pop_cfun ();
1909 /* It would make a lot more sense to output thunks before function body to
1910 get more forward and fewer backward jumps. This however would need
1911 solving problem with comdats. See PR48668. Also aliases must come after
1912 function itself to make one pass assemblers, like one on AIX, happy.
1913 See PR 50689.
1914 FIXME: Perhaps thunks should be move before function IFF they are not in
1915 comdat groups. */
1916 assemble_thunks_and_aliases ();
1917 release_body ();
1920 /* Node comparator that is responsible for the order that corresponds
1921 to time when a function was launched for the first time. */
1924 tp_first_run_node_cmp (const void *pa, const void *pb)
1926 const cgraph_node *a = *(const cgraph_node * const *) pa;
1927 const cgraph_node *b = *(const cgraph_node * const *) pb;
1928 unsigned int tp_first_run_a = a->tp_first_run;
1929 unsigned int tp_first_run_b = b->tp_first_run;
1931 if (!opt_for_fn (a->decl, flag_profile_reorder_functions)
1932 || a->no_reorder)
1933 tp_first_run_a = 0;
1934 if (!opt_for_fn (b->decl, flag_profile_reorder_functions)
1935 || b->no_reorder)
1936 tp_first_run_b = 0;
1938 if (tp_first_run_a == tp_first_run_b)
1939 return a->order - b->order;
1941 /* Functions with time profile must be before these without profile. */
1942 tp_first_run_a = (tp_first_run_a - 1) & INT_MAX;
1943 tp_first_run_b = (tp_first_run_b - 1) & INT_MAX;
1945 return tp_first_run_a - tp_first_run_b;
1948 /* Expand all functions that must be output.
1950 Attempt to topologically sort the nodes so function is output when
1951 all called functions are already assembled to allow data to be
1952 propagated across the callgraph. Use a stack to get smaller distance
1953 between a function and its callees (later we may choose to use a more
1954 sophisticated algorithm for function reordering; we will likely want
1955 to use subsections to make the output functions appear in top-down
1956 order). */
1958 static void
1959 expand_all_functions (void)
1961 cgraph_node *node;
1962 cgraph_node **order = XCNEWVEC (cgraph_node *,
1963 symtab->cgraph_count);
1964 cgraph_node **tp_first_run_order = XCNEWVEC (cgraph_node *,
1965 symtab->cgraph_count);
1966 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1967 int order_pos, tp_first_run_order_pos = 0, new_order_pos = 0;
1968 int i;
1970 order_pos = ipa_reverse_postorder (order);
1971 gcc_assert (order_pos == symtab->cgraph_count);
1973 /* Garbage collector may remove inline clones we eliminate during
1974 optimization. So we must be sure to not reference them. */
1975 for (i = 0; i < order_pos; i++)
1976 if (order[i]->process)
1978 if (order[i]->tp_first_run
1979 && opt_for_fn (order[i]->decl, flag_profile_reorder_functions))
1980 tp_first_run_order[tp_first_run_order_pos++] = order[i];
1981 else
1982 order[new_order_pos++] = order[i];
1985 /* First output functions with time profile in specified order. */
1986 qsort (tp_first_run_order, tp_first_run_order_pos,
1987 sizeof (cgraph_node *), tp_first_run_node_cmp);
1988 for (i = 0; i < tp_first_run_order_pos; i++)
1990 node = tp_first_run_order[i];
1992 if (node->process)
1994 expanded_func_count++;
1995 profiled_func_count++;
1997 if (symtab->dump_file)
1998 fprintf (symtab->dump_file,
1999 "Time profile order in expand_all_functions:%s:%d\n",
2000 node->dump_asm_name (), node->tp_first_run);
2001 node->process = 0;
2002 node->expand ();
2006 /* Output functions in RPO so callees get optimized before callers. This
2007 makes ipa-ra and other propagators to work.
2008 FIXME: This is far from optimal code layout.
2009 Make multiple passes over the list to defer processing of gc
2010 candidates until all potential uses are seen. */
2011 int gc_candidates = 0;
2012 int prev_gc_candidates = 0;
2014 while (1)
2016 for (i = new_order_pos - 1; i >= 0; i--)
2018 node = order[i];
2020 if (node->gc_candidate)
2021 gc_candidates++;
2022 else if (node->process)
2024 expanded_func_count++;
2025 node->process = 0;
2026 node->expand ();
2029 if (!gc_candidates || gc_candidates == prev_gc_candidates)
2030 break;
2031 prev_gc_candidates = gc_candidates;
2032 gc_candidates = 0;
2035 /* Free any unused gc_candidate functions. */
2036 if (gc_candidates)
2037 for (i = new_order_pos - 1; i >= 0; i--)
2039 node = order[i];
2040 if (node->gc_candidate)
2042 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
2043 if (symtab->dump_file)
2044 fprintf (symtab->dump_file,
2045 "Deleting unused function %s\n",
2046 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
2047 node->process = false;
2048 free_dominance_info (fn, CDI_DOMINATORS);
2049 free_dominance_info (fn, CDI_POST_DOMINATORS);
2050 node->release_body (false);
2054 if (dump_file)
2055 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2056 main_input_filename, profiled_func_count, expanded_func_count);
2058 if (symtab->dump_file && tp_first_run_order_pos)
2059 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2060 profiled_func_count, expanded_func_count);
2062 symtab->process_new_functions ();
2063 free_gimplify_stack ();
2064 delete ipa_saved_clone_sources;
2065 ipa_saved_clone_sources = NULL;
2066 free (order);
2067 free (tp_first_run_order);
2070 /* This is used to sort the node types by the cgraph order number. */
2072 enum cgraph_order_sort_kind
2074 ORDER_FUNCTION,
2075 ORDER_VAR,
2076 ORDER_VAR_UNDEF,
2077 ORDER_ASM
2080 struct cgraph_order_sort
2082 /* Construct from a cgraph_node. */
2083 cgraph_order_sort (cgraph_node *node)
2084 : kind (ORDER_FUNCTION), order (node->order)
2086 u.f = node;
2089 /* Construct from a varpool_node. */
2090 cgraph_order_sort (varpool_node *node)
2091 : kind (node->definition ? ORDER_VAR : ORDER_VAR_UNDEF), order (node->order)
2093 u.v = node;
2096 /* Construct from a asm_node. */
2097 cgraph_order_sort (asm_node *node)
2098 : kind (ORDER_ASM), order (node->order)
2100 u.a = node;
2103 /* Assembly cgraph_order_sort based on its type. */
2104 void process ();
2106 enum cgraph_order_sort_kind kind;
2107 union
2109 cgraph_node *f;
2110 varpool_node *v;
2111 asm_node *a;
2112 } u;
2113 int order;
2116 /* Assembly cgraph_order_sort based on its type. */
2118 void
2119 cgraph_order_sort::process ()
2121 switch (kind)
2123 case ORDER_FUNCTION:
2124 u.f->process = 0;
2125 u.f->expand ();
2126 break;
2127 case ORDER_VAR:
2128 u.v->assemble_decl ();
2129 break;
2130 case ORDER_VAR_UNDEF:
2131 assemble_undefined_decl (u.v->decl);
2132 break;
2133 case ORDER_ASM:
2134 assemble_asm (u.a->asm_str);
2135 break;
2136 default:
2137 gcc_unreachable ();
2141 /* Compare cgraph_order_sort by order. */
2143 static int
2144 cgraph_order_cmp (const void *a_p, const void *b_p)
2146 const cgraph_order_sort *nodea = (const cgraph_order_sort *)a_p;
2147 const cgraph_order_sort *nodeb = (const cgraph_order_sort *)b_p;
2149 return nodea->order - nodeb->order;
2152 /* Output all functions, variables, and asm statements in the order
2153 according to their order fields, which is the order in which they
2154 appeared in the file. This implements -fno-toplevel-reorder. In
2155 this mode we may output functions and variables which don't really
2156 need to be output. */
2158 static void
2159 output_in_order (void)
2161 int i;
2162 cgraph_node *cnode;
2163 varpool_node *vnode;
2164 asm_node *anode;
2165 auto_vec<cgraph_order_sort> nodes;
2166 cgraph_order_sort *node;
2168 FOR_EACH_DEFINED_FUNCTION (cnode)
2169 if (cnode->process && !cnode->thunk
2170 && !cnode->alias && cnode->no_reorder)
2171 nodes.safe_push (cgraph_order_sort (cnode));
2173 /* There is a similar loop in symbol_table::output_variables.
2174 Please keep them in sync. */
2175 FOR_EACH_VARIABLE (vnode)
2176 if (vnode->no_reorder
2177 && !DECL_HARD_REGISTER (vnode->decl)
2178 && !DECL_HAS_VALUE_EXPR_P (vnode->decl))
2179 nodes.safe_push (cgraph_order_sort (vnode));
2181 for (anode = symtab->first_asm_symbol (); anode; anode = anode->next)
2182 nodes.safe_push (cgraph_order_sort (anode));
2184 /* Sort nodes by order. */
2185 nodes.qsort (cgraph_order_cmp);
2187 /* In toplevel reorder mode we output all statics; mark them as needed. */
2188 FOR_EACH_VEC_ELT (nodes, i, node)
2189 if (node->kind == ORDER_VAR)
2190 node->u.v->finalize_named_section_flags ();
2192 FOR_EACH_VEC_ELT (nodes, i, node)
2193 node->process ();
2195 symtab->clear_asm_symbols ();
2198 static void
2199 ipa_passes (void)
2201 gcc::pass_manager *passes = g->get_passes ();
2203 set_cfun (NULL);
2204 current_function_decl = NULL;
2205 gimple_register_cfg_hooks ();
2206 bitmap_obstack_initialize (NULL);
2208 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2210 if (!in_lto_p)
2212 execute_ipa_pass_list (passes->all_small_ipa_passes);
2213 if (seen_error ())
2214 return;
2217 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2218 devirtualization and other changes where removal iterate. */
2219 symtab->remove_unreachable_nodes (symtab->dump_file);
2221 /* If pass_all_early_optimizations was not scheduled, the state of
2222 the cgraph will not be properly updated. Update it now. */
2223 if (symtab->state < IPA_SSA)
2224 symtab->state = IPA_SSA;
2226 if (!in_lto_p)
2228 /* Generate coverage variables and constructors. */
2229 coverage_finish ();
2231 /* Process new functions added. */
2232 set_cfun (NULL);
2233 current_function_decl = NULL;
2234 symtab->process_new_functions ();
2236 execute_ipa_summary_passes
2237 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2240 /* Some targets need to handle LTO assembler output specially. */
2241 if (flag_generate_lto || flag_generate_offload)
2242 targetm.asm_out.lto_start ();
2244 if (!in_lto_p
2245 || flag_incremental_link == INCREMENTAL_LINK_LTO)
2247 if (!quiet_flag)
2248 fprintf (stderr, "Streaming LTO\n");
2249 if (g->have_offload)
2251 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2252 lto_stream_offload_p = true;
2253 ipa_write_summaries ();
2254 lto_stream_offload_p = false;
2256 if (flag_lto)
2258 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2259 lto_stream_offload_p = false;
2260 ipa_write_summaries ();
2264 if (flag_generate_lto || flag_generate_offload)
2265 targetm.asm_out.lto_end ();
2267 if (!flag_ltrans
2268 && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2269 || !flag_lto || flag_fat_lto_objects))
2270 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2271 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2273 bitmap_obstack_release (NULL);
2277 /* Weakrefs may be associated to external decls and thus not output
2278 at expansion time. Emit all necessary aliases. */
2280 void
2281 symbol_table::output_weakrefs (void)
2283 symtab_node *node;
2284 FOR_EACH_SYMBOL (node)
2285 if (node->alias
2286 && !TREE_ASM_WRITTEN (node->decl)
2287 && node->weakref)
2289 tree target;
2291 /* Weakrefs are special by not requiring target definition in current
2292 compilation unit. It is thus bit hard to work out what we want to
2293 alias.
2294 When alias target is defined, we need to fetch it from symtab reference,
2295 otherwise it is pointed to by alias_target. */
2296 if (node->alias_target)
2297 target = (DECL_P (node->alias_target)
2298 ? DECL_ASSEMBLER_NAME (node->alias_target)
2299 : node->alias_target);
2300 else if (node->analyzed)
2301 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2302 else
2303 gcc_unreachable ();
2304 do_assemble_alias (node->decl, target);
2308 /* Perform simple optimizations based on callgraph. */
2310 void
2311 symbol_table::compile (void)
2313 if (seen_error ())
2314 return;
2316 symtab_node::checking_verify_symtab_nodes ();
2318 timevar_push (TV_CGRAPHOPT);
2319 if (pre_ipa_mem_report)
2320 dump_memory_report ("Memory consumption before IPA");
2321 if (!quiet_flag)
2322 fprintf (stderr, "Performing interprocedural optimizations\n");
2323 state = IPA;
2325 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2326 if (flag_generate_lto || flag_generate_offload)
2327 lto_streamer_hooks_init ();
2329 /* Don't run the IPA passes if there was any error or sorry messages. */
2330 if (!seen_error ())
2332 timevar_start (TV_CGRAPH_IPA_PASSES);
2333 ipa_passes ();
2334 timevar_stop (TV_CGRAPH_IPA_PASSES);
2336 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2337 if (seen_error ()
2338 || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2339 && flag_lto && !flag_fat_lto_objects))
2341 timevar_pop (TV_CGRAPHOPT);
2342 return;
2345 global_info_ready = true;
2346 if (dump_file)
2348 fprintf (dump_file, "Optimized ");
2349 symtab->dump (dump_file);
2351 if (post_ipa_mem_report)
2352 dump_memory_report ("Memory consumption after IPA");
2353 timevar_pop (TV_CGRAPHOPT);
2355 /* Output everything. */
2356 switch_to_section (text_section);
2357 (*debug_hooks->assembly_start) ();
2358 if (!quiet_flag)
2359 fprintf (stderr, "Assembling functions:\n");
2360 symtab_node::checking_verify_symtab_nodes ();
2362 bitmap_obstack_initialize (NULL);
2363 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2364 bitmap_obstack_release (NULL);
2365 mark_functions_to_output ();
2367 /* When weakref support is missing, we automatically translate all
2368 references to NODE to references to its ultimate alias target.
2369 The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2370 TREE_CHAIN.
2372 Set up this mapping before we output any assembler but once we are sure
2373 that all symbol renaming is done.
2375 FIXME: All this ugliness can go away if we just do renaming at gimple
2376 level by physically rewriting the IL. At the moment we can only redirect
2377 calls, so we need infrastructure for renaming references as well. */
2378 #ifndef ASM_OUTPUT_WEAKREF
2379 symtab_node *node;
2381 FOR_EACH_SYMBOL (node)
2382 if (node->alias
2383 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2385 IDENTIFIER_TRANSPARENT_ALIAS
2386 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2387 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2388 = (node->alias_target ? node->alias_target
2389 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2391 #endif
2393 state = EXPANSION;
2395 /* Output first asm statements and anything ordered. The process
2396 flag is cleared for these nodes, so we skip them later. */
2397 output_in_order ();
2399 timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2400 expand_all_functions ();
2401 timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2403 output_variables ();
2405 process_new_functions ();
2406 state = FINISHED;
2407 output_weakrefs ();
2409 if (dump_file)
2411 fprintf (dump_file, "\nFinal ");
2412 symtab->dump (dump_file);
2414 if (!flag_checking)
2415 return;
2416 symtab_node::verify_symtab_nodes ();
2417 /* Double check that all inline clones are gone and that all
2418 function bodies have been released from memory. */
2419 if (!seen_error ())
2421 cgraph_node *node;
2422 bool error_found = false;
2424 FOR_EACH_DEFINED_FUNCTION (node)
2425 if (node->inlined_to
2426 || gimple_has_body_p (node->decl))
2428 if (DECL_STRUCT_FUNCTION (node->decl)
2429 && (DECL_STRUCT_FUNCTION (node->decl)->curr_properties
2430 & PROP_assumptions_done) != 0)
2431 continue;
2432 error_found = true;
2433 node->debug ();
2435 if (error_found)
2436 internal_error ("nodes with unreleased memory found");
2440 /* Earlydebug dump file, flags, and number. */
2442 static int debuginfo_early_dump_nr;
2443 static FILE *debuginfo_early_dump_file;
2444 static dump_flags_t debuginfo_early_dump_flags;
2446 /* Debug dump file, flags, and number. */
2448 static int debuginfo_dump_nr;
2449 static FILE *debuginfo_dump_file;
2450 static dump_flags_t debuginfo_dump_flags;
2452 /* Register the debug and earlydebug dump files. */
2454 void
2455 debuginfo_early_init (void)
2457 gcc::dump_manager *dumps = g->get_dumps ();
2458 debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2459 "earlydebug", DK_tree,
2460 OPTGROUP_NONE,
2461 false);
2462 debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2463 "debug", DK_tree,
2464 OPTGROUP_NONE,
2465 false);
2468 /* Initialize the debug and earlydebug dump files. */
2470 void
2471 debuginfo_init (void)
2473 gcc::dump_manager *dumps = g->get_dumps ();
2474 debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2475 debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2476 debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2477 debuginfo_early_dump_flags
2478 = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2481 /* Finalize the debug and earlydebug dump files. */
2483 void
2484 debuginfo_fini (void)
2486 if (debuginfo_dump_file)
2487 dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2488 if (debuginfo_early_dump_file)
2489 dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2492 /* Set dump_file to the debug dump file. */
2494 void
2495 debuginfo_start (void)
2497 set_dump_file (debuginfo_dump_file);
2500 /* Undo setting dump_file to the debug dump file. */
2502 void
2503 debuginfo_stop (void)
2505 set_dump_file (NULL);
2508 /* Set dump_file to the earlydebug dump file. */
2510 void
2511 debuginfo_early_start (void)
2513 set_dump_file (debuginfo_early_dump_file);
2516 /* Undo setting dump_file to the earlydebug dump file. */
2518 void
2519 debuginfo_early_stop (void)
2521 set_dump_file (NULL);
2524 /* Analyze the whole compilation unit once it is parsed completely. */
2526 void
2527 symbol_table::finalize_compilation_unit (void)
2529 timevar_push (TV_CGRAPH);
2531 /* If we're here there's no current function anymore. Some frontends
2532 are lazy in clearing these. */
2533 current_function_decl = NULL;
2534 set_cfun (NULL);
2536 /* Do not skip analyzing the functions if there were errors, we
2537 miss diagnostics for following functions otherwise. */
2539 /* Emit size functions we didn't inline. */
2540 finalize_size_functions ();
2542 /* Mark alias targets necessary and emit diagnostics. */
2543 handle_alias_pairs ();
2545 if (!quiet_flag)
2547 fprintf (stderr, "\nAnalyzing compilation unit\n");
2548 fflush (stderr);
2551 if (flag_dump_passes)
2552 dump_passes ();
2554 /* Gimplify and lower all functions, compute reachability and
2555 remove unreachable nodes. */
2556 analyze_functions (/*first_time=*/true);
2558 /* Mark alias targets necessary and emit diagnostics. */
2559 handle_alias_pairs ();
2561 /* Gimplify and lower thunks. */
2562 analyze_functions (/*first_time=*/false);
2564 /* All nested functions should be lowered now. */
2565 nested_function_info::release ();
2567 /* Offloading requires LTO infrastructure. */
2568 if (!in_lto_p && g->have_offload)
2569 flag_generate_offload = 1;
2571 if (!seen_error ())
2573 /* Give the frontends the chance to emit early debug based on
2574 what is still reachable in the TU. */
2575 (*lang_hooks.finalize_early_debug) ();
2577 /* Clean up anything that needs cleaning up after initial debug
2578 generation. */
2579 debuginfo_early_start ();
2580 (*debug_hooks->early_finish) (main_input_filename);
2581 debuginfo_early_stop ();
2584 /* Finally drive the pass manager. */
2585 compile ();
2587 timevar_pop (TV_CGRAPH);
2590 /* Reset all state within cgraphunit.cc so that we can rerun the compiler
2591 within the same process. For use by toplev::finalize. */
2593 void
2594 cgraphunit_cc_finalize (void)
2596 gcc_assert (cgraph_new_nodes.length () == 0);
2597 cgraph_new_nodes.truncate (0);
2599 queued_nodes = &symtab_terminator;
2601 first_analyzed = NULL;
2602 first_analyzed_var = NULL;
2605 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2606 kind of wrapper method. */
2608 void
2609 cgraph_node::create_wrapper (cgraph_node *target)
2611 /* Preserve DECL_RESULT so we get right by reference flag. */
2612 tree decl_result = DECL_RESULT (decl);
2614 /* Remove the function's body but keep arguments to be reused
2615 for thunk. */
2616 release_body (true);
2617 reset ();
2619 DECL_UNINLINABLE (decl) = false;
2620 DECL_RESULT (decl) = decl_result;
2621 DECL_INITIAL (decl) = NULL;
2622 allocate_struct_function (decl, false);
2623 set_cfun (NULL);
2625 /* Turn alias into thunk and expand it into GIMPLE representation. */
2626 definition = true;
2627 semantic_interposition = opt_for_fn (decl, flag_semantic_interposition);
2629 /* Create empty thunk, but be sure we did not keep former thunk around.
2630 In that case we would need to preserve the info. */
2631 gcc_checking_assert (!thunk_info::get (this));
2632 thunk_info::get_create (this);
2633 thunk = true;
2634 create_edge (target, NULL, count);
2635 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2637 tree arguments = DECL_ARGUMENTS (decl);
2639 while (arguments)
2641 TREE_ADDRESSABLE (arguments) = false;
2642 arguments = TREE_CHAIN (arguments);
2645 expand_thunk (this, false, true);
2646 thunk_info::remove (this);
2648 /* Inline summary set-up. */
2649 analyze ();
2650 inline_analyze_function (this);