Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cgraphunit.c
blob55cb0347149c4658e603751f41f80f263438381d
1 /* Driver of optimization process
2 Copyright (C) 2003-2021 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-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-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 void
384 cgraph_node::reset (void)
386 /* If process is set, then we have already begun whole-unit analysis.
387 This is *not* testing for whether we've already emitted the function.
388 That case can be sort-of legitimately seen with real function redefinition
389 errors. I would argue that the front end should never present us with
390 such a case, but don't enforce that for now. */
391 gcc_assert (!process);
393 /* Reset our data structures so we can analyze the function again. */
394 inlined_to = NULL;
395 memset (&rtl, 0, sizeof (rtl));
396 analyzed = false;
397 definition = false;
398 alias = false;
399 transparent_alias = false;
400 weakref = false;
401 cpp_implicit_alias = false;
403 remove_callees ();
404 remove_all_references ();
407 /* Return true when there are references to the node. INCLUDE_SELF is
408 true if a self reference counts as a reference. */
410 bool
411 symtab_node::referred_to_p (bool include_self)
413 ipa_ref *ref = NULL;
415 /* See if there are any references at all. */
416 if (iterate_referring (0, ref))
417 return true;
418 /* For functions check also calls. */
419 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
420 if (cn && cn->callers)
422 if (include_self)
423 return true;
424 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
425 if (e->caller != this)
426 return true;
428 return false;
431 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
432 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
433 the garbage collector run at the moment. We would need to either create
434 a new GC context, or just not compile right now. */
436 void
437 cgraph_node::finalize_function (tree decl, bool no_collect)
439 cgraph_node *node = cgraph_node::get_create (decl);
441 if (node->definition)
443 /* Nested functions should only be defined once. */
444 gcc_assert (!DECL_CONTEXT (decl)
445 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
446 node->reset ();
447 node->redefined_extern_inline = true;
450 /* Set definition first before calling notice_global_symbol so that
451 it is available to notice_global_symbol. */
452 node->definition = true;
453 notice_global_symbol (decl);
454 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
455 if (!flag_toplevel_reorder)
456 node->no_reorder = true;
458 /* With -fkeep-inline-functions we are keeping all inline functions except
459 for extern inline ones. */
460 if (flag_keep_inline_functions
461 && DECL_DECLARED_INLINE_P (decl)
462 && !DECL_EXTERNAL (decl)
463 && !DECL_DISREGARD_INLINE_LIMITS (decl))
464 node->force_output = 1;
466 /* __RTL functions were already output as soon as they were parsed (due
467 to the large amount of global state in the backend).
468 Mark such functions as "force_output" to reflect the fact that they
469 will be in the asm file when considering the symbols they reference.
470 The attempt to output them later on will bail out immediately. */
471 if (node->native_rtl_p ())
472 node->force_output = 1;
474 /* When not optimizing, also output the static functions. (see
475 PR24561), but don't do so for always_inline functions, functions
476 declared inline and nested functions. These were optimized out
477 in the original implementation and it is unclear whether we want
478 to change the behavior here. */
479 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
480 || node->no_reorder)
481 && !node->cpp_implicit_alias
482 && !DECL_DISREGARD_INLINE_LIMITS (decl)
483 && !DECL_DECLARED_INLINE_P (decl)
484 && !(DECL_CONTEXT (decl)
485 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
486 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
487 node->force_output = 1;
489 /* If we've not yet emitted decl, tell the debug info about it. */
490 if (!TREE_ASM_WRITTEN (decl))
491 (*debug_hooks->deferred_inline_function) (decl);
493 if (!no_collect)
494 ggc_collect ();
496 if (symtab->state == CONSTRUCTION
497 && (node->needed_p () || node->referred_to_p ()))
498 enqueue_node (node);
501 /* Add the function FNDECL to the call graph.
502 Unlike finalize_function, this function is intended to be used
503 by middle end and allows insertion of new function at arbitrary point
504 of compilation. The function can be either in high, low or SSA form
505 GIMPLE.
507 The function is assumed to be reachable and have address taken (so no
508 API breaking optimizations are performed on it).
510 Main work done by this function is to enqueue the function for later
511 processing to avoid need the passes to be re-entrant. */
513 void
514 cgraph_node::add_new_function (tree fndecl, bool lowered)
516 gcc::pass_manager *passes = g->get_passes ();
517 cgraph_node *node;
519 if (dump_file)
521 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
522 const char *function_type = ((gimple_has_body_p (fndecl))
523 ? (lowered
524 ? (gimple_in_ssa_p (fn)
525 ? "ssa gimple"
526 : "low gimple")
527 : "high gimple")
528 : "to-be-gimplified");
529 fprintf (dump_file,
530 "Added new %s function %s to callgraph\n",
531 function_type,
532 fndecl_name (fndecl));
535 switch (symtab->state)
537 case PARSING:
538 cgraph_node::finalize_function (fndecl, false);
539 break;
540 case CONSTRUCTION:
541 /* Just enqueue function to be processed at nearest occurrence. */
542 node = cgraph_node::get_create (fndecl);
543 if (lowered)
544 node->lowered = true;
545 cgraph_new_nodes.safe_push (node);
546 break;
548 case IPA:
549 case IPA_SSA:
550 case IPA_SSA_AFTER_INLINING:
551 case EXPANSION:
552 /* Bring the function into finalized state and enqueue for later
553 analyzing and compilation. */
554 node = cgraph_node::get_create (fndecl);
555 node->local = false;
556 node->definition = true;
557 node->force_output = true;
558 if (TREE_PUBLIC (fndecl))
559 node->externally_visible = true;
560 if (!lowered && symtab->state == EXPANSION)
562 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
563 gimple_register_cfg_hooks ();
564 bitmap_obstack_initialize (NULL);
565 execute_pass_list (cfun, passes->all_lowering_passes);
566 passes->execute_early_local_passes ();
567 bitmap_obstack_release (NULL);
568 pop_cfun ();
570 lowered = true;
572 if (lowered)
573 node->lowered = true;
574 cgraph_new_nodes.safe_push (node);
575 break;
577 case FINISHED:
578 /* At the very end of compilation we have to do all the work up
579 to expansion. */
580 node = cgraph_node::create (fndecl);
581 if (lowered)
582 node->lowered = true;
583 node->definition = true;
584 node->analyze ();
585 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
586 gimple_register_cfg_hooks ();
587 bitmap_obstack_initialize (NULL);
588 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
589 g->get_passes ()->execute_early_local_passes ();
590 bitmap_obstack_release (NULL);
591 pop_cfun ();
592 node->expand ();
593 break;
595 default:
596 gcc_unreachable ();
599 /* Set a personality if required and we already passed EH lowering. */
600 if (lowered
601 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
602 == eh_personality_lang))
603 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
606 /* Analyze the function scheduled to be output. */
607 void
608 cgraph_node::analyze (void)
610 if (native_rtl_p ())
612 analyzed = true;
613 return;
616 tree decl = this->decl;
617 location_t saved_loc = input_location;
618 input_location = DECL_SOURCE_LOCATION (decl);
620 if (thunk)
622 thunk_info *info = thunk_info::get (this);
623 cgraph_node *t = cgraph_node::get (info->alias);
625 create_edge (t, NULL, t->count);
626 callees->can_throw_external = !TREE_NOTHROW (t->decl);
627 /* Target code in expand_thunk may need the thunk's target
628 to be analyzed, so recurse here. */
629 if (!t->analyzed && t->definition)
630 t->analyze ();
631 if (t->alias)
633 t = t->get_alias_target ();
634 if (!t->analyzed && t->definition)
635 t->analyze ();
637 bool ret = expand_thunk (this, false, false);
638 thunk_info::get (this)->alias = NULL;
639 if (!ret)
640 return;
642 if (alias)
643 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
644 else if (dispatcher_function)
646 /* Generate the dispatcher body of multi-versioned functions. */
647 cgraph_function_version_info *dispatcher_version_info
648 = function_version ();
649 if (dispatcher_version_info != NULL
650 && (dispatcher_version_info->dispatcher_resolver
651 == NULL_TREE))
653 tree resolver = NULL_TREE;
654 gcc_assert (targetm.generate_version_dispatcher_body);
655 resolver = targetm.generate_version_dispatcher_body (this);
656 gcc_assert (resolver != NULL_TREE);
659 else
661 push_cfun (DECL_STRUCT_FUNCTION (decl));
663 assign_assembler_name_if_needed (decl);
665 /* Make sure to gimplify bodies only once. During analyzing a
666 function we lower it, which will require gimplified nested
667 functions, so we can end up here with an already gimplified
668 body. */
669 if (!gimple_has_body_p (decl))
670 gimplify_function_tree (decl);
672 /* Lower the function. */
673 if (!lowered)
675 if (first_nested_function (this))
676 lower_nested_functions (decl);
678 gimple_register_cfg_hooks ();
679 bitmap_obstack_initialize (NULL);
680 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
681 compact_blocks ();
682 bitmap_obstack_release (NULL);
683 lowered = true;
686 pop_cfun ();
688 analyzed = true;
690 input_location = saved_loc;
693 /* C++ frontend produce same body aliases all over the place, even before PCH
694 gets streamed out. It relies on us linking the aliases with their function
695 in order to do the fixups, but ipa-ref is not PCH safe. Consequently we
696 first produce aliases without links, but once C++ FE is sure he won't stream
697 PCH we build the links via this function. */
699 void
700 symbol_table::process_same_body_aliases (void)
702 symtab_node *node;
703 FOR_EACH_SYMBOL (node)
704 if (node->cpp_implicit_alias && !node->analyzed)
705 node->resolve_alias
706 (VAR_P (node->alias_target)
707 ? (symtab_node *)varpool_node::get_create (node->alias_target)
708 : (symtab_node *)cgraph_node::get_create (node->alias_target));
709 cpp_implicit_aliases_done = true;
712 /* Process a symver attribute. */
714 static void
715 process_symver_attribute (symtab_node *n)
717 tree value = lookup_attribute ("symver", DECL_ATTRIBUTES (n->decl));
719 for (; value != NULL; value = TREE_CHAIN (value))
721 /* Starting from bintuils 2.35 gas supports:
722 # Assign foo to bar@V1 and baz@V2.
723 .symver foo, bar@V1
724 .symver foo, baz@V2
726 const char *purpose = IDENTIFIER_POINTER (TREE_PURPOSE (value));
727 if (strcmp (purpose, "symver") != 0)
728 continue;
730 tree symver = get_identifier_with_length
731 (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (value))),
732 TREE_STRING_LENGTH (TREE_VALUE (TREE_VALUE (value))));
733 symtab_node *def = symtab_node::get_for_asmname (symver);
735 if (def)
737 error_at (DECL_SOURCE_LOCATION (n->decl),
738 "duplicate definition of a symbol version");
739 inform (DECL_SOURCE_LOCATION (def->decl),
740 "same version was previously defined here");
741 return;
743 if (!n->definition)
745 error_at (DECL_SOURCE_LOCATION (n->decl),
746 "symbol needs to be defined to have a version");
747 return;
749 if (DECL_COMMON (n->decl))
751 error_at (DECL_SOURCE_LOCATION (n->decl),
752 "common symbol cannot be versioned");
753 return;
755 if (DECL_COMDAT (n->decl))
757 error_at (DECL_SOURCE_LOCATION (n->decl),
758 "comdat symbol cannot be versioned");
759 return;
761 if (n->weakref)
763 error_at (DECL_SOURCE_LOCATION (n->decl),
764 "%<weakref%> cannot be versioned");
765 return;
767 if (!TREE_PUBLIC (n->decl))
769 error_at (DECL_SOURCE_LOCATION (n->decl),
770 "versioned symbol must be public");
771 return;
773 if (DECL_VISIBILITY (n->decl) != VISIBILITY_DEFAULT)
775 error_at (DECL_SOURCE_LOCATION (n->decl),
776 "versioned symbol must have default visibility");
777 return;
780 /* Create new symbol table entry representing the version. */
781 tree new_decl = copy_node (n->decl);
783 DECL_INITIAL (new_decl) = NULL_TREE;
784 if (TREE_CODE (new_decl) == FUNCTION_DECL)
785 DECL_STRUCT_FUNCTION (new_decl) = NULL;
786 SET_DECL_ASSEMBLER_NAME (new_decl, symver);
787 TREE_PUBLIC (new_decl) = 1;
788 DECL_ATTRIBUTES (new_decl) = NULL;
790 symtab_node *symver_node = symtab_node::get_create (new_decl);
791 symver_node->alias = true;
792 symver_node->definition = true;
793 symver_node->symver = true;
794 symver_node->create_reference (n, IPA_REF_ALIAS, NULL);
795 symver_node->analyzed = true;
799 /* Process attributes common for vars and functions. */
801 static void
802 process_common_attributes (symtab_node *node, tree decl)
804 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
806 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
808 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
809 "%<weakref%> attribute should be accompanied with"
810 " an %<alias%> attribute");
811 DECL_WEAK (decl) = 0;
812 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
813 DECL_ATTRIBUTES (decl));
816 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
817 node->no_reorder = 1;
818 process_symver_attribute (node);
821 /* Look for externally_visible and used attributes and mark cgraph nodes
822 accordingly.
824 We cannot mark the nodes at the point the attributes are processed (in
825 handle_*_attribute) because the copy of the declarations available at that
826 point may not be canonical. For example, in:
828 void f();
829 void f() __attribute__((used));
831 the declaration we see in handle_used_attribute will be the second
832 declaration -- but the front end will subsequently merge that declaration
833 with the original declaration and discard the second declaration.
835 Furthermore, we can't mark these nodes in finalize_function because:
837 void f() {}
838 void f() __attribute__((externally_visible));
840 is valid.
842 So, we walk the nodes at the end of the translation unit, applying the
843 attributes at that point. */
845 static void
846 process_function_and_variable_attributes (cgraph_node *first,
847 varpool_node *first_var)
849 cgraph_node *node;
850 varpool_node *vnode;
852 for (node = symtab->first_function (); node != first;
853 node = symtab->next_function (node))
855 tree decl = node->decl;
857 if (node->alias
858 && lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
860 tree tdecl = node->get_alias_target_tree ();
861 if (!tdecl || !DECL_P (tdecl)
862 || !lookup_attribute ("flatten", DECL_ATTRIBUTES (tdecl)))
863 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
864 "%<flatten%> attribute is ignored on aliases");
866 if (DECL_PRESERVE_P (decl))
867 node->mark_force_output ();
868 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
870 if (! TREE_PUBLIC (node->decl))
871 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
872 "%<externally_visible%>"
873 " attribute have effect only on public objects");
875 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
876 && node->definition
877 && (!node->alias || DECL_INITIAL (decl) != error_mark_node))
879 /* NODE->DEFINITION && NODE->ALIAS is nonzero for valid weakref
880 function declarations; DECL_INITIAL is non-null for invalid
881 weakref functions that are also defined. */
882 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
883 "%<weakref%> attribute ignored"
884 " because function is defined");
885 DECL_WEAK (decl) = 0;
886 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
887 DECL_ATTRIBUTES (decl));
888 DECL_ATTRIBUTES (decl) = remove_attribute ("alias",
889 DECL_ATTRIBUTES (decl));
890 node->alias = false;
891 node->weakref = false;
892 node->transparent_alias = false;
894 else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
895 && node->definition
896 && !node->alias)
897 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
898 "%<alias%> attribute ignored"
899 " because function is defined");
901 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
902 && !DECL_DECLARED_INLINE_P (decl)
903 /* redefining extern inline function makes it DECL_UNINLINABLE. */
904 && !DECL_UNINLINABLE (decl))
905 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
906 "%<always_inline%> function might not be inlinable");
908 process_common_attributes (node, decl);
910 for (vnode = symtab->first_variable (); vnode != first_var;
911 vnode = symtab->next_variable (vnode))
913 tree decl = vnode->decl;
914 if (DECL_EXTERNAL (decl)
915 && DECL_INITIAL (decl))
916 varpool_node::finalize_decl (decl);
917 if (DECL_PRESERVE_P (decl))
918 vnode->force_output = true;
919 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
921 if (! TREE_PUBLIC (vnode->decl))
922 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
923 "%<externally_visible%>"
924 " attribute have effect only on public objects");
926 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
927 && vnode->definition
928 && DECL_INITIAL (decl))
930 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
931 "%<weakref%> attribute ignored"
932 " because variable is initialized");
933 DECL_WEAK (decl) = 0;
934 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
935 DECL_ATTRIBUTES (decl));
937 process_common_attributes (vnode, decl);
941 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
942 middle end to output the variable to asm file, if needed or externally
943 visible. */
945 void
946 varpool_node::finalize_decl (tree decl)
948 varpool_node *node = varpool_node::get_create (decl);
950 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
952 if (node->definition)
953 return;
954 /* Set definition first before calling notice_global_symbol so that
955 it is available to notice_global_symbol. */
956 node->definition = true;
957 notice_global_symbol (decl);
958 if (!flag_toplevel_reorder)
959 node->no_reorder = true;
960 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
961 /* Traditionally we do not eliminate static variables when not
962 optimizing and when not doing toplevel reorder. */
963 || (node->no_reorder && !DECL_COMDAT (node->decl)
964 && !DECL_ARTIFICIAL (node->decl)))
965 node->force_output = true;
967 if (symtab->state == CONSTRUCTION
968 && (node->needed_p () || node->referred_to_p ()))
969 enqueue_node (node);
970 if (symtab->state >= IPA_SSA)
971 node->analyze ();
972 /* Some frontends produce various interface variables after compilation
973 finished. */
974 if (symtab->state == FINISHED
975 || (node->no_reorder
976 && symtab->state == EXPANSION))
977 node->assemble_decl ();
980 /* EDGE is an polymorphic call. Mark all possible targets as reachable
981 and if there is only one target, perform trivial devirtualization.
982 REACHABLE_CALL_TARGETS collects target lists we already walked to
983 avoid duplicate work. */
985 static void
986 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
987 cgraph_edge *edge)
989 unsigned int i;
990 void *cache_token;
991 bool final;
992 vec <cgraph_node *>targets
993 = possible_polymorphic_call_targets
994 (edge, &final, &cache_token);
996 if (!reachable_call_targets->add (cache_token))
998 if (symtab->dump_file)
999 dump_possible_polymorphic_call_targets
1000 (symtab->dump_file, edge);
1002 for (i = 0; i < targets.length (); i++)
1004 /* Do not bother to mark virtual methods in anonymous namespace;
1005 either we will find use of virtual table defining it, or it is
1006 unused. */
1007 if (targets[i]->definition
1008 && TREE_CODE
1009 (TREE_TYPE (targets[i]->decl))
1010 == METHOD_TYPE
1011 && !type_in_anonymous_namespace_p
1012 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
1013 enqueue_node (targets[i]);
1017 /* Very trivial devirtualization; when the type is
1018 final or anonymous (so we know all its derivation)
1019 and there is only one possible virtual call target,
1020 make the edge direct. */
1021 if (final)
1023 if (targets.length () <= 1 && dbg_cnt (devirt))
1025 cgraph_node *target;
1026 if (targets.length () == 1)
1027 target = targets[0];
1028 else
1029 target = cgraph_node::create
1030 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
1032 if (symtab->dump_file)
1034 fprintf (symtab->dump_file,
1035 "Devirtualizing call: ");
1036 print_gimple_stmt (symtab->dump_file,
1037 edge->call_stmt, 0,
1038 TDF_SLIM);
1040 if (dump_enabled_p ())
1042 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
1043 "devirtualizing call in %s to %s\n",
1044 edge->caller->dump_name (),
1045 target->dump_name ());
1048 edge = cgraph_edge::make_direct (edge, target);
1049 gimple *new_call = cgraph_edge::redirect_call_stmt_to_callee (edge);
1051 if (symtab->dump_file)
1053 fprintf (symtab->dump_file, "Devirtualized as: ");
1054 print_gimple_stmt (symtab->dump_file, new_call, 0, TDF_SLIM);
1060 /* Issue appropriate warnings for the global declaration DECL. */
1062 static void
1063 check_global_declaration (symtab_node *snode)
1065 const char *decl_file;
1066 tree decl = snode->decl;
1068 /* Warn about any function declared static but not defined. We don't
1069 warn about variables, because many programs have static variables
1070 that exist only to get some text into the object file. */
1071 if (TREE_CODE (decl) == FUNCTION_DECL
1072 && DECL_INITIAL (decl) == 0
1073 && DECL_EXTERNAL (decl)
1074 && ! DECL_ARTIFICIAL (decl)
1075 && ! TREE_PUBLIC (decl))
1077 if (warning_suppressed_p (decl, OPT_Wunused))
1079 else if (snode->referred_to_p (/*include_self=*/false))
1080 pedwarn (input_location, 0, "%q+F used but never defined", decl);
1081 else
1082 warning (OPT_Wunused_function, "%q+F declared %<static%> but never "
1083 "defined", decl);
1084 /* This symbol is effectively an "extern" declaration now. */
1085 TREE_PUBLIC (decl) = 1;
1088 /* Warn about static fns or vars defined but not used. */
1089 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
1090 || (((warn_unused_variable && ! TREE_READONLY (decl))
1091 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
1092 && (warn_unused_const_variable == 2
1093 || (main_input_filename != NULL
1094 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
1095 && filename_cmp (main_input_filename,
1096 decl_file) == 0))))
1097 && VAR_P (decl)))
1098 && ! DECL_IN_SYSTEM_HEADER (decl)
1099 && ! snode->referred_to_p (/*include_self=*/false)
1100 /* This TREE_USED check is needed in addition to referred_to_p
1101 above, because the `__unused__' attribute is not being
1102 considered for referred_to_p. */
1103 && ! TREE_USED (decl)
1104 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1105 to handle multiple external decls in different scopes. */
1106 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1107 && ! DECL_EXTERNAL (decl)
1108 && ! DECL_ARTIFICIAL (decl)
1109 && ! DECL_ABSTRACT_ORIGIN (decl)
1110 && ! TREE_PUBLIC (decl)
1111 /* A volatile variable might be used in some non-obvious way. */
1112 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1113 /* Global register variables must be declared to reserve them. */
1114 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1115 /* Global ctors and dtors are called by the runtime. */
1116 && (TREE_CODE (decl) != FUNCTION_DECL
1117 || (!DECL_STATIC_CONSTRUCTOR (decl)
1118 && !DECL_STATIC_DESTRUCTOR (decl)))
1119 /* Otherwise, ask the language. */
1120 && lang_hooks.decls.warn_unused_global (decl))
1121 warning_at (DECL_SOURCE_LOCATION (decl),
1122 (TREE_CODE (decl) == FUNCTION_DECL)
1123 ? OPT_Wunused_function
1124 : (TREE_READONLY (decl)
1125 ? OPT_Wunused_const_variable_
1126 : OPT_Wunused_variable),
1127 "%qD defined but not used", decl);
1130 /* Discover all functions and variables that are trivially needed, analyze
1131 them as well as all functions and variables referred by them */
1132 static cgraph_node *first_analyzed;
1133 static varpool_node *first_analyzed_var;
1135 /* FIRST_TIME is set to TRUE for the first time we are called for a
1136 translation unit from finalize_compilation_unit() or false
1137 otherwise. */
1139 static void
1140 analyze_functions (bool first_time)
1142 /* Keep track of already processed nodes when called multiple times for
1143 intermodule optimization. */
1144 cgraph_node *first_handled = first_analyzed;
1145 varpool_node *first_handled_var = first_analyzed_var;
1146 hash_set<void *> reachable_call_targets;
1148 symtab_node *node;
1149 symtab_node *next;
1150 int i;
1151 ipa_ref *ref;
1152 bool changed = true;
1153 location_t saved_loc = input_location;
1155 bitmap_obstack_initialize (NULL);
1156 symtab->state = CONSTRUCTION;
1157 input_location = UNKNOWN_LOCATION;
1159 thunk_info::process_early_thunks ();
1161 /* Ugly, but the fixup cannot happen at a time same body alias is created;
1162 C++ FE is confused about the COMDAT groups being right. */
1163 if (symtab->cpp_implicit_aliases_done)
1164 FOR_EACH_SYMBOL (node)
1165 if (node->cpp_implicit_alias)
1166 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1167 build_type_inheritance_graph ();
1169 if (flag_openmp && first_time)
1170 omp_discover_implicit_declare_target ();
1172 /* Analysis adds static variables that in turn adds references to new functions.
1173 So we need to iterate the process until it stabilize. */
1174 while (changed)
1176 changed = false;
1177 process_function_and_variable_attributes (first_analyzed,
1178 first_analyzed_var);
1180 /* First identify the trivially needed symbols. */
1181 for (node = symtab->first_symbol ();
1182 node != first_analyzed
1183 && node != first_analyzed_var; node = node->next)
1185 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1186 node->get_comdat_group_id ();
1187 if (node->needed_p ())
1189 enqueue_node (node);
1190 if (!changed && symtab->dump_file)
1191 fprintf (symtab->dump_file, "Trivially needed symbols:");
1192 changed = true;
1193 if (symtab->dump_file)
1194 fprintf (symtab->dump_file, " %s", node->dump_asm_name ());
1196 if (node == first_analyzed
1197 || node == first_analyzed_var)
1198 break;
1200 symtab->process_new_functions ();
1201 first_analyzed_var = symtab->first_variable ();
1202 first_analyzed = symtab->first_function ();
1204 if (changed && symtab->dump_file)
1205 fprintf (symtab->dump_file, "\n");
1207 /* Lower representation, build callgraph edges and references for all trivially
1208 needed symbols and all symbols referred by them. */
1209 while (queued_nodes != &symtab_terminator)
1211 changed = true;
1212 node = queued_nodes;
1213 queued_nodes = (symtab_node *)queued_nodes->aux;
1214 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1215 if (cnode && cnode->definition)
1217 cgraph_edge *edge;
1218 tree decl = cnode->decl;
1220 /* ??? It is possible to create extern inline function
1221 and later using weak alias attribute to kill its body.
1222 See gcc.c-torture/compile/20011119-1.c */
1223 if (!DECL_STRUCT_FUNCTION (decl)
1224 && !cnode->alias
1225 && !cnode->thunk
1226 && !cnode->dispatcher_function)
1228 cnode->reset ();
1229 cnode->redefined_extern_inline = true;
1230 continue;
1233 if (!cnode->analyzed)
1234 cnode->analyze ();
1236 for (edge = cnode->callees; edge; edge = edge->next_callee)
1237 if (edge->callee->definition
1238 && (!DECL_EXTERNAL (edge->callee->decl)
1239 /* When not optimizing, do not try to analyze extern
1240 inline functions. Doing so is pointless. */
1241 || opt_for_fn (edge->callee->decl, optimize)
1242 /* Weakrefs needs to be preserved. */
1243 || edge->callee->alias
1244 /* always_inline functions are inlined even at -O0. */
1245 || lookup_attribute
1246 ("always_inline",
1247 DECL_ATTRIBUTES (edge->callee->decl))
1248 /* Multiversioned functions needs the dispatcher to
1249 be produced locally even for extern functions. */
1250 || edge->callee->function_version ()))
1251 enqueue_node (edge->callee);
1252 if (opt_for_fn (cnode->decl, optimize)
1253 && opt_for_fn (cnode->decl, flag_devirtualize))
1255 cgraph_edge *next;
1257 for (edge = cnode->indirect_calls; edge; edge = next)
1259 next = edge->next_callee;
1260 if (edge->indirect_info->polymorphic)
1261 walk_polymorphic_call_targets (&reachable_call_targets,
1262 edge);
1266 /* If decl is a clone of an abstract function,
1267 mark that abstract function so that we don't release its body.
1268 The DECL_INITIAL() of that abstract function declaration
1269 will be later needed to output debug info. */
1270 if (DECL_ABSTRACT_ORIGIN (decl))
1272 cgraph_node *origin_node
1273 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1274 origin_node->used_as_abstract_origin = true;
1276 /* Preserve a functions function context node. It will
1277 later be needed to output debug info. */
1278 if (tree fn = decl_function_context (decl))
1280 cgraph_node *origin_node = cgraph_node::get_create (fn);
1281 enqueue_node (origin_node);
1284 else
1286 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1287 if (vnode && vnode->definition && !vnode->analyzed)
1288 vnode->analyze ();
1291 if (node->same_comdat_group)
1293 symtab_node *next;
1294 for (next = node->same_comdat_group;
1295 next != node;
1296 next = next->same_comdat_group)
1297 if (!next->comdat_local_p ())
1298 enqueue_node (next);
1300 for (i = 0; node->iterate_reference (i, ref); i++)
1301 if (ref->referred->definition
1302 && (!DECL_EXTERNAL (ref->referred->decl)
1303 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1304 && optimize)
1305 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1306 && opt_for_fn (ref->referred->decl, optimize))
1307 || node->alias
1308 || ref->referred->alias)))
1309 enqueue_node (ref->referred);
1310 symtab->process_new_functions ();
1313 update_type_inheritance_graph ();
1315 /* Collect entry points to the unit. */
1316 if (symtab->dump_file)
1318 fprintf (symtab->dump_file, "\n\nInitial ");
1319 symtab->dump (symtab->dump_file);
1322 if (first_time)
1324 symtab_node *snode;
1325 FOR_EACH_SYMBOL (snode)
1326 check_global_declaration (snode);
1329 if (symtab->dump_file)
1330 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1332 for (node = symtab->first_symbol ();
1333 node != first_handled
1334 && node != first_handled_var; node = next)
1336 next = node->next;
1337 /* For symbols declared locally we clear TREE_READONLY when emitting
1338 the constructor (if one is needed). For external declarations we can
1339 not safely assume that the type is readonly because we may be called
1340 during its construction. */
1341 if (TREE_CODE (node->decl) == VAR_DECL
1342 && TYPE_P (TREE_TYPE (node->decl))
1343 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (node->decl))
1344 && DECL_EXTERNAL (node->decl))
1345 TREE_READONLY (node->decl) = 0;
1346 if (!node->aux && !node->referred_to_p ())
1348 if (symtab->dump_file)
1349 fprintf (symtab->dump_file, " %s", node->dump_name ());
1351 /* See if the debugger can use anything before the DECL
1352 passes away. Perhaps it can notice a DECL that is now a
1353 constant and can tag the early DIE with an appropriate
1354 attribute.
1356 Otherwise, this is the last chance the debug_hooks have
1357 at looking at optimized away DECLs, since
1358 late_global_decl will subsequently be called from the
1359 contents of the now pruned symbol table. */
1360 if (VAR_P (node->decl)
1361 && !decl_function_context (node->decl))
1363 /* We are reclaiming totally unreachable code and variables
1364 so they effectively appear as readonly. Show that to
1365 the debug machinery. */
1366 TREE_READONLY (node->decl) = 1;
1367 node->definition = false;
1368 (*debug_hooks->late_global_decl) (node->decl);
1371 node->remove ();
1372 continue;
1374 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1376 tree decl = node->decl;
1378 if (cnode->definition && !gimple_has_body_p (decl)
1379 && !cnode->alias
1380 && !cnode->thunk)
1381 cnode->reset ();
1383 gcc_assert (!cnode->definition || cnode->thunk
1384 || cnode->alias
1385 || gimple_has_body_p (decl)
1386 || cnode->native_rtl_p ());
1387 gcc_assert (cnode->analyzed == cnode->definition);
1389 node->aux = NULL;
1391 for (;node; node = node->next)
1392 node->aux = NULL;
1393 first_analyzed = symtab->first_function ();
1394 first_analyzed_var = symtab->first_variable ();
1395 if (symtab->dump_file)
1397 fprintf (symtab->dump_file, "\n\nReclaimed ");
1398 symtab->dump (symtab->dump_file);
1400 bitmap_obstack_release (NULL);
1401 ggc_collect ();
1402 /* Initialize assembler name hash, in particular we want to trigger C++
1403 mangling and same body alias creation before we free DECL_ARGUMENTS
1404 used by it. */
1405 if (!seen_error ())
1406 symtab->symtab_initialize_asm_name_hash ();
1408 input_location = saved_loc;
1411 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1412 (which may be an ifunc resolver) and issue a diagnostic when they are
1413 not compatible according to language rules (plus a C++ extension for
1414 non-static member functions). */
1416 static void
1417 maybe_diag_incompatible_alias (tree alias, tree target)
1419 tree altype = TREE_TYPE (alias);
1420 tree targtype = TREE_TYPE (target);
1422 bool ifunc = cgraph_node::get (alias)->ifunc_resolver;
1423 tree funcptr = altype;
1425 if (ifunc)
1427 /* Handle attribute ifunc first. */
1428 if (TREE_CODE (altype) == METHOD_TYPE)
1430 /* Set FUNCPTR to the type of the alias target. If the type
1431 is a non-static member function of class C, construct a type
1432 of an ordinary function taking C* as the first argument,
1433 followed by the member function argument list, and use it
1434 instead to check for incompatibility. This conversion is
1435 not defined by the language but an extension provided by
1436 G++. */
1438 tree rettype = TREE_TYPE (altype);
1439 tree args = TYPE_ARG_TYPES (altype);
1440 altype = build_function_type (rettype, args);
1441 funcptr = altype;
1444 targtype = TREE_TYPE (targtype);
1446 if (POINTER_TYPE_P (targtype))
1448 targtype = TREE_TYPE (targtype);
1450 /* Only issue Wattribute-alias for conversions to void* with
1451 -Wextra. */
1452 if (VOID_TYPE_P (targtype) && !extra_warnings)
1453 return;
1455 /* Proceed to handle incompatible ifunc resolvers below. */
1457 else
1459 funcptr = build_pointer_type (funcptr);
1461 error_at (DECL_SOURCE_LOCATION (target),
1462 "%<ifunc%> resolver for %qD must return %qT",
1463 alias, funcptr);
1464 inform (DECL_SOURCE_LOCATION (alias),
1465 "resolver indirect function declared here");
1466 return;
1470 if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1471 || (prototype_p (altype)
1472 && prototype_p (targtype)
1473 && !types_compatible_p (altype, targtype))))
1475 /* Warn for incompatibilities. Avoid warning for functions
1476 without a prototype to make it possible to declare aliases
1477 without knowing the exact type, as libstdc++ does. */
1478 if (ifunc)
1480 funcptr = build_pointer_type (funcptr);
1482 auto_diagnostic_group d;
1483 if (warning_at (DECL_SOURCE_LOCATION (target),
1484 OPT_Wattribute_alias_,
1485 "%<ifunc%> resolver for %qD should return %qT",
1486 alias, funcptr))
1487 inform (DECL_SOURCE_LOCATION (alias),
1488 "resolver indirect function declared here");
1490 else
1492 auto_diagnostic_group d;
1493 if (warning_at (DECL_SOURCE_LOCATION (alias),
1494 OPT_Wattribute_alias_,
1495 "%qD alias between functions of incompatible "
1496 "types %qT and %qT", alias, altype, targtype))
1497 inform (DECL_SOURCE_LOCATION (target),
1498 "aliased declaration here");
1503 /* Translate the ugly representation of aliases as alias pairs into nice
1504 representation in callgraph. We don't handle all cases yet,
1505 unfortunately. */
1507 static void
1508 handle_alias_pairs (void)
1510 alias_pair *p;
1511 unsigned i;
1513 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1515 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1517 /* Weakrefs with target not defined in current unit are easy to handle:
1518 they behave just as external variables except we need to note the
1519 alias flag to later output the weakref pseudo op into asm file. */
1520 if (!target_node
1521 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1523 symtab_node *node = symtab_node::get (p->decl);
1524 if (node)
1526 node->alias_target = p->target;
1527 node->weakref = true;
1528 node->alias = true;
1529 node->transparent_alias = true;
1531 alias_pairs->unordered_remove (i);
1532 continue;
1534 else if (!target_node)
1536 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1537 symtab_node *node = symtab_node::get (p->decl);
1538 if (node)
1539 node->alias = false;
1540 alias_pairs->unordered_remove (i);
1541 continue;
1544 if (DECL_EXTERNAL (target_node->decl)
1545 /* We use local aliases for C++ thunks to force the tailcall
1546 to bind locally. This is a hack - to keep it working do
1547 the following (which is not strictly correct). */
1548 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1549 || ! DECL_VIRTUAL_P (target_node->decl))
1550 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1552 error ("%q+D aliased to external symbol %qE",
1553 p->decl, p->target);
1556 if (TREE_CODE (p->decl) == FUNCTION_DECL
1557 && target_node && is_a <cgraph_node *> (target_node))
1559 maybe_diag_incompatible_alias (p->decl, target_node->decl);
1561 maybe_diag_alias_attributes (p->decl, target_node->decl);
1563 cgraph_node *src_node = cgraph_node::get (p->decl);
1564 if (src_node && src_node->definition)
1565 src_node->reset ();
1566 cgraph_node::create_alias (p->decl, target_node->decl);
1567 alias_pairs->unordered_remove (i);
1569 else if (VAR_P (p->decl)
1570 && target_node && is_a <varpool_node *> (target_node))
1572 varpool_node::create_alias (p->decl, target_node->decl);
1573 alias_pairs->unordered_remove (i);
1575 else
1577 error ("%q+D alias between function and variable is not supported",
1578 p->decl);
1579 inform (DECL_SOURCE_LOCATION (target_node->decl),
1580 "aliased declaration here");
1582 alias_pairs->unordered_remove (i);
1585 vec_free (alias_pairs);
1589 /* Figure out what functions we want to assemble. */
1591 static void
1592 mark_functions_to_output (void)
1594 bool check_same_comdat_groups = false;
1595 cgraph_node *node;
1597 if (flag_checking)
1598 FOR_EACH_FUNCTION (node)
1599 gcc_assert (!node->process);
1601 FOR_EACH_FUNCTION (node)
1603 tree decl = node->decl;
1605 gcc_assert (!node->process || node->same_comdat_group);
1606 if (node->process)
1607 continue;
1609 /* We need to output all local functions that are used and not
1610 always inlined, as well as those that are reachable from
1611 outside the current compilation unit. */
1612 if (node->analyzed
1613 && !node->thunk
1614 && !node->alias
1615 && !node->inlined_to
1616 && !TREE_ASM_WRITTEN (decl)
1617 && !DECL_EXTERNAL (decl))
1619 node->process = 1;
1620 if (node->same_comdat_group)
1622 cgraph_node *next;
1623 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1624 next != node;
1625 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1626 if (!next->thunk && !next->alias
1627 && !next->comdat_local_p ())
1628 next->process = 1;
1631 else if (node->same_comdat_group)
1633 if (flag_checking)
1634 check_same_comdat_groups = true;
1636 else
1638 /* We should've reclaimed all functions that are not needed. */
1639 if (flag_checking
1640 && !node->inlined_to
1641 && gimple_has_body_p (decl)
1642 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1643 are inside partition, we can end up not removing the body since we no longer
1644 have analyzed node pointing to it. */
1645 && !node->in_other_partition
1646 && !node->alias
1647 && !node->clones
1648 && !DECL_EXTERNAL (decl))
1650 node->debug ();
1651 internal_error ("failed to reclaim unneeded function");
1653 gcc_assert (node->inlined_to
1654 || !gimple_has_body_p (decl)
1655 || node->in_other_partition
1656 || node->clones
1657 || DECL_ARTIFICIAL (decl)
1658 || DECL_EXTERNAL (decl));
1663 if (flag_checking && check_same_comdat_groups)
1664 FOR_EACH_FUNCTION (node)
1665 if (node->same_comdat_group && !node->process)
1667 tree decl = node->decl;
1668 if (!node->inlined_to
1669 && gimple_has_body_p (decl)
1670 /* FIXME: in an ltrans unit when the offline copy is outside a
1671 partition but inline copies are inside a partition, we can
1672 end up not removing the body since we no longer have an
1673 analyzed node pointing to it. */
1674 && !node->in_other_partition
1675 && !node->clones
1676 && !DECL_EXTERNAL (decl))
1678 node->debug ();
1679 internal_error ("failed to reclaim unneeded function in same "
1680 "comdat group");
1685 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1686 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1688 Set current_function_decl and cfun to newly constructed empty function body.
1689 return basic block in the function body. */
1691 basic_block
1692 init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1694 basic_block bb;
1695 edge e;
1697 current_function_decl = decl;
1698 allocate_struct_function (decl, false);
1699 gimple_register_cfg_hooks ();
1700 init_empty_tree_cfg ();
1701 init_tree_ssa (cfun);
1703 if (in_ssa)
1705 init_ssa_operands (cfun);
1706 cfun->gimple_df->in_ssa_p = true;
1707 cfun->curr_properties |= PROP_ssa;
1710 DECL_INITIAL (decl) = make_node (BLOCK);
1711 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1713 DECL_SAVED_TREE (decl) = error_mark_node;
1714 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1715 | PROP_cfg | PROP_loops);
1717 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1718 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1719 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1721 /* Create BB for body of the function and connect it properly. */
1722 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1723 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1724 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1725 bb->count = count;
1726 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1727 e->probability = profile_probability::always ();
1728 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1729 e->probability = profile_probability::always ();
1730 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1732 return bb;
1735 /* Assemble thunks and aliases associated to node. */
1737 void
1738 cgraph_node::assemble_thunks_and_aliases (void)
1740 cgraph_edge *e;
1741 ipa_ref *ref;
1743 for (e = callers; e;)
1744 if (e->caller->thunk
1745 && !e->caller->inlined_to)
1747 cgraph_node *thunk = e->caller;
1749 e = e->next_caller;
1750 expand_thunk (thunk, true, false);
1751 thunk->assemble_thunks_and_aliases ();
1753 else
1754 e = e->next_caller;
1756 FOR_EACH_ALIAS (this, ref)
1758 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1759 if (!alias->transparent_alias)
1761 bool saved_written = TREE_ASM_WRITTEN (decl);
1763 /* Force assemble_alias to really output the alias this time instead
1764 of buffering it in same alias pairs. */
1765 TREE_ASM_WRITTEN (decl) = 1;
1766 if (alias->symver)
1767 do_assemble_symver (alias->decl,
1768 DECL_ASSEMBLER_NAME (decl));
1769 else
1770 do_assemble_alias (alias->decl,
1771 DECL_ASSEMBLER_NAME (decl));
1772 alias->assemble_thunks_and_aliases ();
1773 TREE_ASM_WRITTEN (decl) = saved_written;
1778 /* Expand function specified by node. */
1780 void
1781 cgraph_node::expand (void)
1783 location_t saved_loc;
1785 /* We ought to not compile any inline clones. */
1786 gcc_assert (!inlined_to);
1788 /* __RTL functions are compiled as soon as they are parsed, so don't
1789 do it again. */
1790 if (native_rtl_p ())
1791 return;
1793 announce_function (decl);
1794 process = 0;
1795 gcc_assert (lowered);
1797 /* Initialize the default bitmap obstack. */
1798 bitmap_obstack_initialize (NULL);
1799 get_untransformed_body ();
1801 /* Generate RTL for the body of DECL. */
1803 timevar_push (TV_REST_OF_COMPILATION);
1805 gcc_assert (symtab->global_info_ready);
1807 /* Initialize the RTL code for the function. */
1808 saved_loc = input_location;
1809 input_location = DECL_SOURCE_LOCATION (decl);
1811 gcc_assert (DECL_STRUCT_FUNCTION (decl));
1812 push_cfun (DECL_STRUCT_FUNCTION (decl));
1813 init_function_start (decl);
1815 gimple_register_cfg_hooks ();
1817 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1819 update_ssa (TODO_update_ssa_only_virtuals);
1820 if (ipa_transforms_to_apply.exists ())
1821 execute_all_ipa_transforms (false);
1823 /* Perform all tree transforms and optimizations. */
1825 /* Signal the start of passes. */
1826 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1828 execute_pass_list (cfun, g->get_passes ()->all_passes);
1830 /* Signal the end of passes. */
1831 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1833 bitmap_obstack_release (&reg_obstack);
1835 /* Release the default bitmap obstack. */
1836 bitmap_obstack_release (NULL);
1838 /* If requested, warn about function definitions where the function will
1839 return a value (usually of some struct or union type) which itself will
1840 take up a lot of stack space. */
1841 if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1843 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1845 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1846 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1847 && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1848 warn_larger_than_size) > 0)
1850 unsigned int size_as_int
1851 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1853 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1854 warning (OPT_Wlarger_than_,
1855 "size of return value of %q+D is %u bytes",
1856 decl, size_as_int);
1857 else
1858 warning (OPT_Wlarger_than_,
1859 "size of return value of %q+D is larger than %wu bytes",
1860 decl, warn_larger_than_size);
1864 gimple_set_body (decl, NULL);
1865 if (DECL_STRUCT_FUNCTION (decl) == 0)
1867 /* Stop pointing to the local nodes about to be freed.
1868 But DECL_INITIAL must remain nonzero so we know this
1869 was an actual function definition. */
1870 if (DECL_INITIAL (decl) != 0)
1871 DECL_INITIAL (decl) = error_mark_node;
1874 input_location = saved_loc;
1876 ggc_collect ();
1877 timevar_pop (TV_REST_OF_COMPILATION);
1879 /* Make sure that BE didn't give up on compiling. */
1880 gcc_assert (TREE_ASM_WRITTEN (decl));
1881 if (cfun)
1882 pop_cfun ();
1884 /* It would make a lot more sense to output thunks before function body to
1885 get more forward and fewer backward jumps. This however would need
1886 solving problem with comdats. See PR48668. Also aliases must come after
1887 function itself to make one pass assemblers, like one on AIX, happy.
1888 See PR 50689.
1889 FIXME: Perhaps thunks should be move before function IFF they are not in
1890 comdat groups. */
1891 assemble_thunks_and_aliases ();
1892 release_body ();
1895 /* Node comparator that is responsible for the order that corresponds
1896 to time when a function was launched for the first time. */
1899 tp_first_run_node_cmp (const void *pa, const void *pb)
1901 const cgraph_node *a = *(const cgraph_node * const *) pa;
1902 const cgraph_node *b = *(const cgraph_node * const *) pb;
1903 unsigned int tp_first_run_a = a->tp_first_run;
1904 unsigned int tp_first_run_b = b->tp_first_run;
1906 if (!opt_for_fn (a->decl, flag_profile_reorder_functions)
1907 || a->no_reorder)
1908 tp_first_run_a = 0;
1909 if (!opt_for_fn (b->decl, flag_profile_reorder_functions)
1910 || b->no_reorder)
1911 tp_first_run_b = 0;
1913 if (tp_first_run_a == tp_first_run_b)
1914 return a->order - b->order;
1916 /* Functions with time profile must be before these without profile. */
1917 tp_first_run_a = (tp_first_run_a - 1) & INT_MAX;
1918 tp_first_run_b = (tp_first_run_b - 1) & INT_MAX;
1920 return tp_first_run_a - tp_first_run_b;
1923 /* Expand all functions that must be output.
1925 Attempt to topologically sort the nodes so function is output when
1926 all called functions are already assembled to allow data to be
1927 propagated across the callgraph. Use a stack to get smaller distance
1928 between a function and its callees (later we may choose to use a more
1929 sophisticated algorithm for function reordering; we will likely want
1930 to use subsections to make the output functions appear in top-down
1931 order). */
1933 static void
1934 expand_all_functions (void)
1936 cgraph_node *node;
1937 cgraph_node **order = XCNEWVEC (cgraph_node *,
1938 symtab->cgraph_count);
1939 cgraph_node **tp_first_run_order = XCNEWVEC (cgraph_node *,
1940 symtab->cgraph_count);
1941 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1942 int order_pos, tp_first_run_order_pos = 0, new_order_pos = 0;
1943 int i;
1945 order_pos = ipa_reverse_postorder (order);
1946 gcc_assert (order_pos == symtab->cgraph_count);
1948 /* Garbage collector may remove inline clones we eliminate during
1949 optimization. So we must be sure to not reference them. */
1950 for (i = 0; i < order_pos; i++)
1951 if (order[i]->process)
1953 if (order[i]->tp_first_run
1954 && opt_for_fn (order[i]->decl, flag_profile_reorder_functions))
1955 tp_first_run_order[tp_first_run_order_pos++] = order[i];
1956 else
1957 order[new_order_pos++] = order[i];
1960 /* First output functions with time profile in specified order. */
1961 qsort (tp_first_run_order, tp_first_run_order_pos,
1962 sizeof (cgraph_node *), tp_first_run_node_cmp);
1963 for (i = 0; i < tp_first_run_order_pos; i++)
1965 node = tp_first_run_order[i];
1967 if (node->process)
1969 expanded_func_count++;
1970 profiled_func_count++;
1972 if (symtab->dump_file)
1973 fprintf (symtab->dump_file,
1974 "Time profile order in expand_all_functions:%s:%d\n",
1975 node->dump_asm_name (), node->tp_first_run);
1976 node->process = 0;
1977 node->expand ();
1981 /* Output functions in RPO so callees get optimized before callers. This
1982 makes ipa-ra and other propagators to work.
1983 FIXME: This is far from optimal code layout. */
1984 for (i = new_order_pos - 1; i >= 0; i--)
1986 node = order[i];
1988 if (node->process)
1990 expanded_func_count++;
1991 node->process = 0;
1992 node->expand ();
1996 if (dump_file)
1997 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1998 main_input_filename, profiled_func_count, expanded_func_count);
2000 if (symtab->dump_file && tp_first_run_order_pos)
2001 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2002 profiled_func_count, expanded_func_count);
2004 symtab->process_new_functions ();
2005 free_gimplify_stack ();
2006 delete ipa_saved_clone_sources;
2007 ipa_saved_clone_sources = NULL;
2008 free (order);
2009 free (tp_first_run_order);
2012 /* This is used to sort the node types by the cgraph order number. */
2014 enum cgraph_order_sort_kind
2016 ORDER_FUNCTION,
2017 ORDER_VAR,
2018 ORDER_VAR_UNDEF,
2019 ORDER_ASM
2022 struct cgraph_order_sort
2024 /* Construct from a cgraph_node. */
2025 cgraph_order_sort (cgraph_node *node)
2026 : kind (ORDER_FUNCTION), order (node->order)
2028 u.f = node;
2031 /* Construct from a varpool_node. */
2032 cgraph_order_sort (varpool_node *node)
2033 : kind (node->definition ? ORDER_VAR : ORDER_VAR_UNDEF), order (node->order)
2035 u.v = node;
2038 /* Construct from a asm_node. */
2039 cgraph_order_sort (asm_node *node)
2040 : kind (ORDER_ASM), order (node->order)
2042 u.a = node;
2045 /* Assembly cgraph_order_sort based on its type. */
2046 void process ();
2048 enum cgraph_order_sort_kind kind;
2049 union
2051 cgraph_node *f;
2052 varpool_node *v;
2053 asm_node *a;
2054 } u;
2055 int order;
2058 /* Assembly cgraph_order_sort based on its type. */
2060 void
2061 cgraph_order_sort::process ()
2063 switch (kind)
2065 case ORDER_FUNCTION:
2066 u.f->process = 0;
2067 u.f->expand ();
2068 break;
2069 case ORDER_VAR:
2070 u.v->assemble_decl ();
2071 break;
2072 case ORDER_VAR_UNDEF:
2073 assemble_undefined_decl (u.v->decl);
2074 break;
2075 case ORDER_ASM:
2076 assemble_asm (u.a->asm_str);
2077 break;
2078 default:
2079 gcc_unreachable ();
2083 /* Compare cgraph_order_sort by order. */
2085 static int
2086 cgraph_order_cmp (const void *a_p, const void *b_p)
2088 const cgraph_order_sort *nodea = (const cgraph_order_sort *)a_p;
2089 const cgraph_order_sort *nodeb = (const cgraph_order_sort *)b_p;
2091 return nodea->order - nodeb->order;
2094 /* Output all functions, variables, and asm statements in the order
2095 according to their order fields, which is the order in which they
2096 appeared in the file. This implements -fno-toplevel-reorder. In
2097 this mode we may output functions and variables which don't really
2098 need to be output. */
2100 static void
2101 output_in_order (void)
2103 int i;
2104 cgraph_node *cnode;
2105 varpool_node *vnode;
2106 asm_node *anode;
2107 auto_vec<cgraph_order_sort> nodes;
2108 cgraph_order_sort *node;
2110 FOR_EACH_DEFINED_FUNCTION (cnode)
2111 if (cnode->process && !cnode->thunk
2112 && !cnode->alias && cnode->no_reorder)
2113 nodes.safe_push (cgraph_order_sort (cnode));
2115 /* There is a similar loop in symbol_table::output_variables.
2116 Please keep them in sync. */
2117 FOR_EACH_VARIABLE (vnode)
2118 if (vnode->no_reorder
2119 && !DECL_HARD_REGISTER (vnode->decl)
2120 && !DECL_HAS_VALUE_EXPR_P (vnode->decl))
2121 nodes.safe_push (cgraph_order_sort (vnode));
2123 for (anode = symtab->first_asm_symbol (); anode; anode = anode->next)
2124 nodes.safe_push (cgraph_order_sort (anode));
2126 /* Sort nodes by order. */
2127 nodes.qsort (cgraph_order_cmp);
2129 /* In toplevel reorder mode we output all statics; mark them as needed. */
2130 FOR_EACH_VEC_ELT (nodes, i, node)
2131 if (node->kind == ORDER_VAR)
2132 node->u.v->finalize_named_section_flags ();
2134 FOR_EACH_VEC_ELT (nodes, i, node)
2135 node->process ();
2137 symtab->clear_asm_symbols ();
2140 static void
2141 ipa_passes (void)
2143 gcc::pass_manager *passes = g->get_passes ();
2145 set_cfun (NULL);
2146 current_function_decl = NULL;
2147 gimple_register_cfg_hooks ();
2148 bitmap_obstack_initialize (NULL);
2150 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2152 if (!in_lto_p)
2154 execute_ipa_pass_list (passes->all_small_ipa_passes);
2155 if (seen_error ())
2156 return;
2159 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2160 devirtualization and other changes where removal iterate. */
2161 symtab->remove_unreachable_nodes (symtab->dump_file);
2163 /* If pass_all_early_optimizations was not scheduled, the state of
2164 the cgraph will not be properly updated. Update it now. */
2165 if (symtab->state < IPA_SSA)
2166 symtab->state = IPA_SSA;
2168 if (!in_lto_p)
2170 /* Generate coverage variables and constructors. */
2171 coverage_finish ();
2173 /* Process new functions added. */
2174 set_cfun (NULL);
2175 current_function_decl = NULL;
2176 symtab->process_new_functions ();
2178 execute_ipa_summary_passes
2179 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2182 /* Some targets need to handle LTO assembler output specially. */
2183 if (flag_generate_lto || flag_generate_offload)
2184 targetm.asm_out.lto_start ();
2186 if (!in_lto_p
2187 || flag_incremental_link == INCREMENTAL_LINK_LTO)
2189 if (!quiet_flag)
2190 fprintf (stderr, "Streaming LTO\n");
2191 if (g->have_offload)
2193 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2194 lto_stream_offload_p = true;
2195 ipa_write_summaries ();
2196 lto_stream_offload_p = false;
2198 if (flag_lto)
2200 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2201 lto_stream_offload_p = false;
2202 ipa_write_summaries ();
2206 if (flag_generate_lto || flag_generate_offload)
2207 targetm.asm_out.lto_end ();
2209 if (!flag_ltrans
2210 && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2211 || !flag_lto || flag_fat_lto_objects))
2212 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2213 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2215 bitmap_obstack_release (NULL);
2219 /* Return string alias is alias of. */
2221 static tree
2222 get_alias_symbol (tree decl)
2224 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2225 return get_identifier (TREE_STRING_POINTER
2226 (TREE_VALUE (TREE_VALUE (alias))));
2230 /* Weakrefs may be associated to external decls and thus not output
2231 at expansion time. Emit all necessary aliases. */
2233 void
2234 symbol_table::output_weakrefs (void)
2236 symtab_node *node;
2237 FOR_EACH_SYMBOL (node)
2238 if (node->alias
2239 && !TREE_ASM_WRITTEN (node->decl)
2240 && node->weakref)
2242 tree target;
2244 /* Weakrefs are special by not requiring target definition in current
2245 compilation unit. It is thus bit hard to work out what we want to
2246 alias.
2247 When alias target is defined, we need to fetch it from symtab reference,
2248 otherwise it is pointed to by alias_target. */
2249 if (node->alias_target)
2250 target = (DECL_P (node->alias_target)
2251 ? DECL_ASSEMBLER_NAME (node->alias_target)
2252 : node->alias_target);
2253 else if (node->analyzed)
2254 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2255 else
2257 gcc_unreachable ();
2258 target = get_alias_symbol (node->decl);
2260 do_assemble_alias (node->decl, target);
2264 /* Perform simple optimizations based on callgraph. */
2266 void
2267 symbol_table::compile (void)
2269 if (seen_error ())
2270 return;
2272 symtab_node::checking_verify_symtab_nodes ();
2274 timevar_push (TV_CGRAPHOPT);
2275 if (pre_ipa_mem_report)
2276 dump_memory_report ("Memory consumption before IPA");
2277 if (!quiet_flag)
2278 fprintf (stderr, "Performing interprocedural optimizations\n");
2279 state = IPA;
2281 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2282 if (flag_generate_lto || flag_generate_offload)
2283 lto_streamer_hooks_init ();
2285 /* Don't run the IPA passes if there was any error or sorry messages. */
2286 if (!seen_error ())
2288 timevar_start (TV_CGRAPH_IPA_PASSES);
2289 ipa_passes ();
2290 timevar_stop (TV_CGRAPH_IPA_PASSES);
2292 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2293 if (seen_error ()
2294 || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2295 && flag_lto && !flag_fat_lto_objects))
2297 timevar_pop (TV_CGRAPHOPT);
2298 return;
2301 global_info_ready = true;
2302 if (dump_file)
2304 fprintf (dump_file, "Optimized ");
2305 symtab->dump (dump_file);
2307 if (post_ipa_mem_report)
2308 dump_memory_report ("Memory consumption after IPA");
2309 timevar_pop (TV_CGRAPHOPT);
2311 /* Output everything. */
2312 switch_to_section (text_section);
2313 (*debug_hooks->assembly_start) ();
2314 if (!quiet_flag)
2315 fprintf (stderr, "Assembling functions:\n");
2316 symtab_node::checking_verify_symtab_nodes ();
2318 bitmap_obstack_initialize (NULL);
2319 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2320 bitmap_obstack_release (NULL);
2321 mark_functions_to_output ();
2323 /* When weakref support is missing, we automatically translate all
2324 references to NODE to references to its ultimate alias target.
2325 The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2326 TREE_CHAIN.
2328 Set up this mapping before we output any assembler but once we are sure
2329 that all symbol renaming is done.
2331 FIXME: All this ugliness can go away if we just do renaming at gimple
2332 level by physically rewriting the IL. At the moment we can only redirect
2333 calls, so we need infrastructure for renaming references as well. */
2334 #ifndef ASM_OUTPUT_WEAKREF
2335 symtab_node *node;
2337 FOR_EACH_SYMBOL (node)
2338 if (node->alias
2339 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2341 IDENTIFIER_TRANSPARENT_ALIAS
2342 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2343 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2344 = (node->alias_target ? node->alias_target
2345 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2347 #endif
2349 state = EXPANSION;
2351 /* Output first asm statements and anything ordered. The process
2352 flag is cleared for these nodes, so we skip them later. */
2353 output_in_order ();
2355 timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2356 expand_all_functions ();
2357 timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2359 output_variables ();
2361 process_new_functions ();
2362 state = FINISHED;
2363 output_weakrefs ();
2365 if (dump_file)
2367 fprintf (dump_file, "\nFinal ");
2368 symtab->dump (dump_file);
2370 if (!flag_checking)
2371 return;
2372 symtab_node::verify_symtab_nodes ();
2373 /* Double check that all inline clones are gone and that all
2374 function bodies have been released from memory. */
2375 if (!seen_error ())
2377 cgraph_node *node;
2378 bool error_found = false;
2380 FOR_EACH_DEFINED_FUNCTION (node)
2381 if (node->inlined_to
2382 || gimple_has_body_p (node->decl))
2384 error_found = true;
2385 node->debug ();
2387 if (error_found)
2388 internal_error ("nodes with unreleased memory found");
2392 /* Earlydebug dump file, flags, and number. */
2394 static int debuginfo_early_dump_nr;
2395 static FILE *debuginfo_early_dump_file;
2396 static dump_flags_t debuginfo_early_dump_flags;
2398 /* Debug dump file, flags, and number. */
2400 static int debuginfo_dump_nr;
2401 static FILE *debuginfo_dump_file;
2402 static dump_flags_t debuginfo_dump_flags;
2404 /* Register the debug and earlydebug dump files. */
2406 void
2407 debuginfo_early_init (void)
2409 gcc::dump_manager *dumps = g->get_dumps ();
2410 debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2411 "earlydebug", DK_tree,
2412 OPTGROUP_NONE,
2413 false);
2414 debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2415 "debug", DK_tree,
2416 OPTGROUP_NONE,
2417 false);
2420 /* Initialize the debug and earlydebug dump files. */
2422 void
2423 debuginfo_init (void)
2425 gcc::dump_manager *dumps = g->get_dumps ();
2426 debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2427 debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2428 debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2429 debuginfo_early_dump_flags
2430 = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2433 /* Finalize the debug and earlydebug dump files. */
2435 void
2436 debuginfo_fini (void)
2438 if (debuginfo_dump_file)
2439 dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2440 if (debuginfo_early_dump_file)
2441 dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2444 /* Set dump_file to the debug dump file. */
2446 void
2447 debuginfo_start (void)
2449 set_dump_file (debuginfo_dump_file);
2452 /* Undo setting dump_file to the debug dump file. */
2454 void
2455 debuginfo_stop (void)
2457 set_dump_file (NULL);
2460 /* Set dump_file to the earlydebug dump file. */
2462 void
2463 debuginfo_early_start (void)
2465 set_dump_file (debuginfo_early_dump_file);
2468 /* Undo setting dump_file to the earlydebug dump file. */
2470 void
2471 debuginfo_early_stop (void)
2473 set_dump_file (NULL);
2476 /* Analyze the whole compilation unit once it is parsed completely. */
2478 void
2479 symbol_table::finalize_compilation_unit (void)
2481 timevar_push (TV_CGRAPH);
2483 /* If we're here there's no current function anymore. Some frontends
2484 are lazy in clearing these. */
2485 current_function_decl = NULL;
2486 set_cfun (NULL);
2488 /* Do not skip analyzing the functions if there were errors, we
2489 miss diagnostics for following functions otherwise. */
2491 /* Emit size functions we didn't inline. */
2492 finalize_size_functions ();
2494 /* Mark alias targets necessary and emit diagnostics. */
2495 handle_alias_pairs ();
2497 if (!quiet_flag)
2499 fprintf (stderr, "\nAnalyzing compilation unit\n");
2500 fflush (stderr);
2503 if (flag_dump_passes)
2504 dump_passes ();
2506 /* Gimplify and lower all functions, compute reachability and
2507 remove unreachable nodes. */
2508 analyze_functions (/*first_time=*/true);
2510 /* Mark alias targets necessary and emit diagnostics. */
2511 handle_alias_pairs ();
2513 /* Gimplify and lower thunks. */
2514 analyze_functions (/*first_time=*/false);
2516 /* All nested functions should be lowered now. */
2517 nested_function_info::release ();
2519 /* Offloading requires LTO infrastructure. */
2520 if (!in_lto_p && g->have_offload)
2521 flag_generate_offload = 1;
2523 if (!seen_error ())
2525 /* Give the frontends the chance to emit early debug based on
2526 what is still reachable in the TU. */
2527 (*lang_hooks.finalize_early_debug) ();
2529 /* Clean up anything that needs cleaning up after initial debug
2530 generation. */
2531 debuginfo_early_start ();
2532 (*debug_hooks->early_finish) (main_input_filename);
2533 debuginfo_early_stop ();
2536 /* Finally drive the pass manager. */
2537 compile ();
2539 timevar_pop (TV_CGRAPH);
2542 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2543 within the same process. For use by toplev::finalize. */
2545 void
2546 cgraphunit_c_finalize (void)
2548 gcc_assert (cgraph_new_nodes.length () == 0);
2549 cgraph_new_nodes.truncate (0);
2551 queued_nodes = &symtab_terminator;
2553 first_analyzed = NULL;
2554 first_analyzed_var = NULL;
2557 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2558 kind of wrapper method. */
2560 void
2561 cgraph_node::create_wrapper (cgraph_node *target)
2563 /* Preserve DECL_RESULT so we get right by reference flag. */
2564 tree decl_result = DECL_RESULT (decl);
2566 /* Remove the function's body but keep arguments to be reused
2567 for thunk. */
2568 release_body (true);
2569 reset ();
2571 DECL_UNINLINABLE (decl) = false;
2572 DECL_RESULT (decl) = decl_result;
2573 DECL_INITIAL (decl) = NULL;
2574 allocate_struct_function (decl, false);
2575 set_cfun (NULL);
2577 /* Turn alias into thunk and expand it into GIMPLE representation. */
2578 definition = true;
2580 /* Create empty thunk, but be sure we did not keep former thunk around.
2581 In that case we would need to preserve the info. */
2582 gcc_checking_assert (!thunk_info::get (this));
2583 thunk_info::get_create (this);
2584 thunk = true;
2585 create_edge (target, NULL, count);
2586 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2588 tree arguments = DECL_ARGUMENTS (decl);
2590 while (arguments)
2592 TREE_ADDRESSABLE (arguments) = false;
2593 arguments = TREE_CHAIN (arguments);
2596 expand_thunk (this, false, true);
2597 thunk_info::remove (this);
2599 /* Inline summary set-up. */
2600 analyze ();
2601 inline_analyze_function (this);