Re: Refactor copying decl section names
[official-gcc.git] / gcc / cgraphunit.c
blob3a9895825b18bccbc3c034862bc6948f100c59d9
1 /* Driver of optimization process
2 Copyright (C) 2003-2020 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 free_dominance_info (CDI_POST_DOMINATORS);
682 free_dominance_info (CDI_DOMINATORS);
683 compact_blocks ();
684 bitmap_obstack_release (NULL);
685 lowered = true;
688 pop_cfun ();
690 analyzed = true;
692 input_location = saved_loc;
695 /* C++ frontend produce same body aliases all over the place, even before PCH
696 gets streamed out. It relies on us linking the aliases with their function
697 in order to do the fixups, but ipa-ref is not PCH safe. Consequently we
698 first produce aliases without links, but once C++ FE is sure he won't stream
699 PCH we build the links via this function. */
701 void
702 symbol_table::process_same_body_aliases (void)
704 symtab_node *node;
705 FOR_EACH_SYMBOL (node)
706 if (node->cpp_implicit_alias && !node->analyzed)
707 node->resolve_alias
708 (VAR_P (node->alias_target)
709 ? (symtab_node *)varpool_node::get_create (node->alias_target)
710 : (symtab_node *)cgraph_node::get_create (node->alias_target));
711 cpp_implicit_aliases_done = true;
714 /* Process a symver attribute. */
716 static void
717 process_symver_attribute (symtab_node *n)
719 tree value = lookup_attribute ("symver", DECL_ATTRIBUTES (n->decl));
721 for (; value != NULL; value = TREE_CHAIN (value))
723 /* Starting from bintuils 2.35 gas supports:
724 # Assign foo to bar@V1 and baz@V2.
725 .symver foo, bar@V1
726 .symver foo, baz@V2
728 const char *purpose = IDENTIFIER_POINTER (TREE_PURPOSE (value));
729 if (strcmp (purpose, "symver") != 0)
730 continue;
732 tree symver = get_identifier_with_length
733 (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (value))),
734 TREE_STRING_LENGTH (TREE_VALUE (TREE_VALUE (value))));
735 symtab_node *def = symtab_node::get_for_asmname (symver);
737 if (def)
739 error_at (DECL_SOURCE_LOCATION (n->decl),
740 "duplicate definition of a symbol version");
741 inform (DECL_SOURCE_LOCATION (def->decl),
742 "same version was previously defined here");
743 return;
745 if (!n->definition)
747 error_at (DECL_SOURCE_LOCATION (n->decl),
748 "symbol needs to be defined to have a version");
749 return;
751 if (DECL_COMMON (n->decl))
753 error_at (DECL_SOURCE_LOCATION (n->decl),
754 "common symbol cannot be versioned");
755 return;
757 if (DECL_COMDAT (n->decl))
759 error_at (DECL_SOURCE_LOCATION (n->decl),
760 "comdat symbol cannot be versioned");
761 return;
763 if (n->weakref)
765 error_at (DECL_SOURCE_LOCATION (n->decl),
766 "%<weakref%> cannot be versioned");
767 return;
769 if (!TREE_PUBLIC (n->decl))
771 error_at (DECL_SOURCE_LOCATION (n->decl),
772 "versioned symbol must be public");
773 return;
775 if (DECL_VISIBILITY (n->decl) != VISIBILITY_DEFAULT)
777 error_at (DECL_SOURCE_LOCATION (n->decl),
778 "versioned symbol must have default visibility");
779 return;
782 /* Create new symbol table entry representing the version. */
783 tree new_decl = copy_node (n->decl);
785 DECL_INITIAL (new_decl) = NULL_TREE;
786 if (TREE_CODE (new_decl) == FUNCTION_DECL)
787 DECL_STRUCT_FUNCTION (new_decl) = NULL;
788 SET_DECL_ASSEMBLER_NAME (new_decl, symver);
789 TREE_PUBLIC (new_decl) = 1;
790 DECL_ATTRIBUTES (new_decl) = NULL;
792 symtab_node *symver_node = symtab_node::get_create (new_decl);
793 symver_node->alias = true;
794 symver_node->definition = true;
795 symver_node->symver = true;
796 symver_node->create_reference (n, IPA_REF_ALIAS, NULL);
797 symver_node->analyzed = true;
801 /* Process attributes common for vars and functions. */
803 static void
804 process_common_attributes (symtab_node *node, tree decl)
806 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
808 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
810 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
811 "%<weakref%> attribute should be accompanied with"
812 " an %<alias%> attribute");
813 DECL_WEAK (decl) = 0;
814 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
815 DECL_ATTRIBUTES (decl));
818 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
819 node->no_reorder = 1;
820 process_symver_attribute (node);
823 /* Look for externally_visible and used attributes and mark cgraph nodes
824 accordingly.
826 We cannot mark the nodes at the point the attributes are processed (in
827 handle_*_attribute) because the copy of the declarations available at that
828 point may not be canonical. For example, in:
830 void f();
831 void f() __attribute__((used));
833 the declaration we see in handle_used_attribute will be the second
834 declaration -- but the front end will subsequently merge that declaration
835 with the original declaration and discard the second declaration.
837 Furthermore, we can't mark these nodes in finalize_function because:
839 void f() {}
840 void f() __attribute__((externally_visible));
842 is valid.
844 So, we walk the nodes at the end of the translation unit, applying the
845 attributes at that point. */
847 static void
848 process_function_and_variable_attributes (cgraph_node *first,
849 varpool_node *first_var)
851 cgraph_node *node;
852 varpool_node *vnode;
854 for (node = symtab->first_function (); node != first;
855 node = symtab->next_function (node))
857 tree decl = node->decl;
859 if (node->alias
860 && lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
862 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
863 "%<flatten%> attribute is ignored on aliases");
865 if (DECL_PRESERVE_P (decl))
866 node->mark_force_output ();
867 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
869 if (! TREE_PUBLIC (node->decl))
870 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
871 "%<externally_visible%>"
872 " attribute have effect only on public objects");
874 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
875 && node->definition
876 && (!node->alias || DECL_INITIAL (decl) != error_mark_node))
878 /* NODE->DEFINITION && NODE->ALIAS is nonzero for valid weakref
879 function declarations; DECL_INITIAL is non-null for invalid
880 weakref functions that are also defined. */
881 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
882 "%<weakref%> attribute ignored"
883 " because function is defined");
884 DECL_WEAK (decl) = 0;
885 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
886 DECL_ATTRIBUTES (decl));
887 DECL_ATTRIBUTES (decl) = remove_attribute ("alias",
888 DECL_ATTRIBUTES (decl));
889 node->alias = false;
890 node->weakref = false;
891 node->transparent_alias = false;
893 else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
894 && node->definition
895 && !node->alias)
896 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
897 "%<alias%> attribute ignored"
898 " because function is defined");
900 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
901 && !DECL_DECLARED_INLINE_P (decl)
902 /* redefining extern inline function makes it DECL_UNINLINABLE. */
903 && !DECL_UNINLINABLE (decl))
904 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
905 "%<always_inline%> function might not be inlinable");
907 process_common_attributes (node, decl);
909 for (vnode = symtab->first_variable (); vnode != first_var;
910 vnode = symtab->next_variable (vnode))
912 tree decl = vnode->decl;
913 if (DECL_EXTERNAL (decl)
914 && DECL_INITIAL (decl))
915 varpool_node::finalize_decl (decl);
916 if (DECL_PRESERVE_P (decl))
917 vnode->force_output = true;
918 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
920 if (! TREE_PUBLIC (vnode->decl))
921 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
922 "%<externally_visible%>"
923 " attribute have effect only on public objects");
925 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
926 && vnode->definition
927 && DECL_INITIAL (decl))
929 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
930 "%<weakref%> attribute ignored"
931 " because variable is initialized");
932 DECL_WEAK (decl) = 0;
933 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
934 DECL_ATTRIBUTES (decl));
936 process_common_attributes (vnode, decl);
940 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
941 middle end to output the variable to asm file, if needed or externally
942 visible. */
944 void
945 varpool_node::finalize_decl (tree decl)
947 varpool_node *node = varpool_node::get_create (decl);
949 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
951 if (node->definition)
952 return;
953 /* Set definition first before calling notice_global_symbol so that
954 it is available to notice_global_symbol. */
955 node->definition = true;
956 notice_global_symbol (decl);
957 if (!flag_toplevel_reorder)
958 node->no_reorder = true;
959 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
960 /* Traditionally we do not eliminate static variables when not
961 optimizing and when not doing toplevel reorder. */
962 || (node->no_reorder && !DECL_COMDAT (node->decl)
963 && !DECL_ARTIFICIAL (node->decl)))
964 node->force_output = true;
966 if (symtab->state == CONSTRUCTION
967 && (node->needed_p () || node->referred_to_p ()))
968 enqueue_node (node);
969 if (symtab->state >= IPA_SSA)
970 node->analyze ();
971 /* Some frontends produce various interface variables after compilation
972 finished. */
973 if (symtab->state == FINISHED
974 || (node->no_reorder
975 && symtab->state == EXPANSION))
976 node->assemble_decl ();
979 /* EDGE is an polymorphic call. Mark all possible targets as reachable
980 and if there is only one target, perform trivial devirtualization.
981 REACHABLE_CALL_TARGETS collects target lists we already walked to
982 avoid duplicate work. */
984 static void
985 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
986 cgraph_edge *edge)
988 unsigned int i;
989 void *cache_token;
990 bool final;
991 vec <cgraph_node *>targets
992 = possible_polymorphic_call_targets
993 (edge, &final, &cache_token);
995 if (!reachable_call_targets->add (cache_token))
997 if (symtab->dump_file)
998 dump_possible_polymorphic_call_targets
999 (symtab->dump_file, edge);
1001 for (i = 0; i < targets.length (); i++)
1003 /* Do not bother to mark virtual methods in anonymous namespace;
1004 either we will find use of virtual table defining it, or it is
1005 unused. */
1006 if (targets[i]->definition
1007 && TREE_CODE
1008 (TREE_TYPE (targets[i]->decl))
1009 == METHOD_TYPE
1010 && !type_in_anonymous_namespace_p
1011 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
1012 enqueue_node (targets[i]);
1016 /* Very trivial devirtualization; when the type is
1017 final or anonymous (so we know all its derivation)
1018 and there is only one possible virtual call target,
1019 make the edge direct. */
1020 if (final)
1022 if (targets.length () <= 1 && dbg_cnt (devirt))
1024 cgraph_node *target;
1025 if (targets.length () == 1)
1026 target = targets[0];
1027 else
1028 target = cgraph_node::create
1029 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
1031 if (symtab->dump_file)
1033 fprintf (symtab->dump_file,
1034 "Devirtualizing call: ");
1035 print_gimple_stmt (symtab->dump_file,
1036 edge->call_stmt, 0,
1037 TDF_SLIM);
1039 if (dump_enabled_p ())
1041 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
1042 "devirtualizing call in %s to %s\n",
1043 edge->caller->dump_name (),
1044 target->dump_name ());
1047 edge = cgraph_edge::make_direct (edge, target);
1048 gimple *new_call = cgraph_edge::redirect_call_stmt_to_callee (edge);
1050 if (symtab->dump_file)
1052 fprintf (symtab->dump_file, "Devirtualized as: ");
1053 print_gimple_stmt (symtab->dump_file, new_call, 0, TDF_SLIM);
1059 /* Issue appropriate warnings for the global declaration DECL. */
1061 static void
1062 check_global_declaration (symtab_node *snode)
1064 const char *decl_file;
1065 tree decl = snode->decl;
1067 /* Warn about any function declared static but not defined. We don't
1068 warn about variables, because many programs have static variables
1069 that exist only to get some text into the object file. */
1070 if (TREE_CODE (decl) == FUNCTION_DECL
1071 && DECL_INITIAL (decl) == 0
1072 && DECL_EXTERNAL (decl)
1073 && ! DECL_ARTIFICIAL (decl)
1074 && ! TREE_PUBLIC (decl))
1076 if (TREE_NO_WARNING (decl))
1078 else if (snode->referred_to_p (/*include_self=*/false))
1079 pedwarn (input_location, 0, "%q+F used but never defined", decl);
1080 else
1081 warning (OPT_Wunused_function, "%q+F declared %<static%> but never "
1082 "defined", decl);
1083 /* This symbol is effectively an "extern" declaration now. */
1084 TREE_PUBLIC (decl) = 1;
1087 /* Warn about static fns or vars defined but not used. */
1088 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
1089 || (((warn_unused_variable && ! TREE_READONLY (decl))
1090 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
1091 && (warn_unused_const_variable == 2
1092 || (main_input_filename != NULL
1093 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
1094 && filename_cmp (main_input_filename,
1095 decl_file) == 0))))
1096 && VAR_P (decl)))
1097 && ! DECL_IN_SYSTEM_HEADER (decl)
1098 && ! snode->referred_to_p (/*include_self=*/false)
1099 /* This TREE_USED check is needed in addition to referred_to_p
1100 above, because the `__unused__' attribute is not being
1101 considered for referred_to_p. */
1102 && ! TREE_USED (decl)
1103 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1104 to handle multiple external decls in different scopes. */
1105 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1106 && ! DECL_EXTERNAL (decl)
1107 && ! DECL_ARTIFICIAL (decl)
1108 && ! DECL_ABSTRACT_ORIGIN (decl)
1109 && ! TREE_PUBLIC (decl)
1110 /* A volatile variable might be used in some non-obvious way. */
1111 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1112 /* Global register variables must be declared to reserve them. */
1113 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1114 /* Global ctors and dtors are called by the runtime. */
1115 && (TREE_CODE (decl) != FUNCTION_DECL
1116 || (!DECL_STATIC_CONSTRUCTOR (decl)
1117 && !DECL_STATIC_DESTRUCTOR (decl)))
1118 /* Otherwise, ask the language. */
1119 && lang_hooks.decls.warn_unused_global (decl))
1120 warning_at (DECL_SOURCE_LOCATION (decl),
1121 (TREE_CODE (decl) == FUNCTION_DECL)
1122 ? OPT_Wunused_function
1123 : (TREE_READONLY (decl)
1124 ? OPT_Wunused_const_variable_
1125 : OPT_Wunused_variable),
1126 "%qD defined but not used", decl);
1129 /* Discover all functions and variables that are trivially needed, analyze
1130 them as well as all functions and variables referred by them */
1131 static cgraph_node *first_analyzed;
1132 static varpool_node *first_analyzed_var;
1134 /* FIRST_TIME is set to TRUE for the first time we are called for a
1135 translation unit from finalize_compilation_unit() or false
1136 otherwise. */
1138 static void
1139 analyze_functions (bool first_time)
1141 /* Keep track of already processed nodes when called multiple times for
1142 intermodule optimization. */
1143 cgraph_node *first_handled = first_analyzed;
1144 varpool_node *first_handled_var = first_analyzed_var;
1145 hash_set<void *> reachable_call_targets;
1147 symtab_node *node;
1148 symtab_node *next;
1149 int i;
1150 ipa_ref *ref;
1151 bool changed = true;
1152 location_t saved_loc = input_location;
1154 bitmap_obstack_initialize (NULL);
1155 symtab->state = CONSTRUCTION;
1156 input_location = UNKNOWN_LOCATION;
1158 thunk_info::process_early_thunks ();
1160 /* Ugly, but the fixup cannot happen at a time same body alias is created;
1161 C++ FE is confused about the COMDAT groups being right. */
1162 if (symtab->cpp_implicit_aliases_done)
1163 FOR_EACH_SYMBOL (node)
1164 if (node->cpp_implicit_alias)
1165 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1166 build_type_inheritance_graph ();
1168 if (flag_openmp && first_time)
1169 omp_discover_implicit_declare_target ();
1171 /* Analysis adds static variables that in turn adds references to new functions.
1172 So we need to iterate the process until it stabilize. */
1173 while (changed)
1175 changed = false;
1176 process_function_and_variable_attributes (first_analyzed,
1177 first_analyzed_var);
1179 /* First identify the trivially needed symbols. */
1180 for (node = symtab->first_symbol ();
1181 node != first_analyzed
1182 && node != first_analyzed_var; node = node->next)
1184 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1185 node->get_comdat_group_id ();
1186 if (node->needed_p ())
1188 enqueue_node (node);
1189 if (!changed && symtab->dump_file)
1190 fprintf (symtab->dump_file, "Trivially needed symbols:");
1191 changed = true;
1192 if (symtab->dump_file)
1193 fprintf (symtab->dump_file, " %s", node->dump_asm_name ());
1194 if (!changed && symtab->dump_file)
1195 fprintf (symtab->dump_file, "\n");
1197 if (node == first_analyzed
1198 || node == first_analyzed_var)
1199 break;
1201 symtab->process_new_functions ();
1202 first_analyzed_var = symtab->first_variable ();
1203 first_analyzed = symtab->first_function ();
1205 if (changed && symtab->dump_file)
1206 fprintf (symtab->dump_file, "\n");
1208 /* Lower representation, build callgraph edges and references for all trivially
1209 needed symbols and all symbols referred by them. */
1210 while (queued_nodes != &symtab_terminator)
1212 changed = true;
1213 node = queued_nodes;
1214 queued_nodes = (symtab_node *)queued_nodes->aux;
1215 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1216 if (cnode && cnode->definition)
1218 cgraph_edge *edge;
1219 tree decl = cnode->decl;
1221 /* ??? It is possible to create extern inline function
1222 and later using weak alias attribute to kill its body.
1223 See gcc.c-torture/compile/20011119-1.c */
1224 if (!DECL_STRUCT_FUNCTION (decl)
1225 && !cnode->alias
1226 && !cnode->thunk
1227 && !cnode->dispatcher_function)
1229 cnode->reset ();
1230 cnode->redefined_extern_inline = true;
1231 continue;
1234 if (!cnode->analyzed)
1235 cnode->analyze ();
1237 for (edge = cnode->callees; edge; edge = edge->next_callee)
1238 if (edge->callee->definition
1239 && (!DECL_EXTERNAL (edge->callee->decl)
1240 /* When not optimizing, do not try to analyze extern
1241 inline functions. Doing so is pointless. */
1242 || opt_for_fn (edge->callee->decl, optimize)
1243 /* Weakrefs needs to be preserved. */
1244 || edge->callee->alias
1245 /* always_inline functions are inlined even at -O0. */
1246 || lookup_attribute
1247 ("always_inline",
1248 DECL_ATTRIBUTES (edge->callee->decl))
1249 /* Multiversioned functions needs the dispatcher to
1250 be produced locally even for extern functions. */
1251 || edge->callee->function_version ()))
1252 enqueue_node (edge->callee);
1253 if (opt_for_fn (cnode->decl, optimize)
1254 && opt_for_fn (cnode->decl, flag_devirtualize))
1256 cgraph_edge *next;
1258 for (edge = cnode->indirect_calls; edge; edge = next)
1260 next = edge->next_callee;
1261 if (edge->indirect_info->polymorphic)
1262 walk_polymorphic_call_targets (&reachable_call_targets,
1263 edge);
1267 /* If decl is a clone of an abstract function,
1268 mark that abstract function so that we don't release its body.
1269 The DECL_INITIAL() of that abstract function declaration
1270 will be later needed to output debug info. */
1271 if (DECL_ABSTRACT_ORIGIN (decl))
1273 cgraph_node *origin_node
1274 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1275 origin_node->used_as_abstract_origin = true;
1277 /* Preserve a functions function context node. It will
1278 later be needed to output debug info. */
1279 if (tree fn = decl_function_context (decl))
1281 cgraph_node *origin_node = cgraph_node::get_create (fn);
1282 enqueue_node (origin_node);
1285 else
1287 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1288 if (vnode && vnode->definition && !vnode->analyzed)
1289 vnode->analyze ();
1292 if (node->same_comdat_group)
1294 symtab_node *next;
1295 for (next = node->same_comdat_group;
1296 next != node;
1297 next = next->same_comdat_group)
1298 if (!next->comdat_local_p ())
1299 enqueue_node (next);
1301 for (i = 0; node->iterate_reference (i, ref); i++)
1302 if (ref->referred->definition
1303 && (!DECL_EXTERNAL (ref->referred->decl)
1304 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1305 && optimize)
1306 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1307 && opt_for_fn (ref->referred->decl, optimize))
1308 || node->alias
1309 || ref->referred->alias)))
1310 enqueue_node (ref->referred);
1311 symtab->process_new_functions ();
1314 update_type_inheritance_graph ();
1316 /* Collect entry points to the unit. */
1317 if (symtab->dump_file)
1319 fprintf (symtab->dump_file, "\n\nInitial ");
1320 symtab->dump (symtab->dump_file);
1323 if (first_time)
1325 symtab_node *snode;
1326 FOR_EACH_SYMBOL (snode)
1327 check_global_declaration (snode);
1330 if (symtab->dump_file)
1331 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1333 for (node = symtab->first_symbol ();
1334 node != first_handled
1335 && node != first_handled_var; node = next)
1337 next = node->next;
1338 /* For symbols declared locally we clear TREE_READONLY when emitting
1339 the constructor (if one is needed). For external declarations we can
1340 not safely assume that the type is readonly because we may be called
1341 during its construction. */
1342 if (TREE_CODE (node->decl) == VAR_DECL
1343 && TYPE_P (TREE_TYPE (node->decl))
1344 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (node->decl))
1345 && DECL_EXTERNAL (node->decl))
1346 TREE_READONLY (node->decl) = 0;
1347 if (!node->aux && !node->referred_to_p ())
1349 if (symtab->dump_file)
1350 fprintf (symtab->dump_file, " %s", node->dump_name ());
1352 /* See if the debugger can use anything before the DECL
1353 passes away. Perhaps it can notice a DECL that is now a
1354 constant and can tag the early DIE with an appropriate
1355 attribute.
1357 Otherwise, this is the last chance the debug_hooks have
1358 at looking at optimized away DECLs, since
1359 late_global_decl will subsequently be called from the
1360 contents of the now pruned symbol table. */
1361 if (VAR_P (node->decl)
1362 && !decl_function_context (node->decl))
1364 /* We are reclaiming totally unreachable code and variables
1365 so they effectively appear as readonly. Show that to
1366 the debug machinery. */
1367 TREE_READONLY (node->decl) = 1;
1368 node->definition = false;
1369 (*debug_hooks->late_global_decl) (node->decl);
1372 node->remove ();
1373 continue;
1375 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1377 tree decl = node->decl;
1379 if (cnode->definition && !gimple_has_body_p (decl)
1380 && !cnode->alias
1381 && !cnode->thunk)
1382 cnode->reset ();
1384 gcc_assert (!cnode->definition || cnode->thunk
1385 || cnode->alias
1386 || gimple_has_body_p (decl)
1387 || cnode->native_rtl_p ());
1388 gcc_assert (cnode->analyzed == cnode->definition);
1390 node->aux = NULL;
1392 for (;node; node = node->next)
1393 node->aux = NULL;
1394 first_analyzed = symtab->first_function ();
1395 first_analyzed_var = symtab->first_variable ();
1396 if (symtab->dump_file)
1398 fprintf (symtab->dump_file, "\n\nReclaimed ");
1399 symtab->dump (symtab->dump_file);
1401 bitmap_obstack_release (NULL);
1402 ggc_collect ();
1403 /* Initialize assembler name hash, in particular we want to trigger C++
1404 mangling and same body alias creation before we free DECL_ARGUMENTS
1405 used by it. */
1406 if (!seen_error ())
1407 symtab->symtab_initialize_asm_name_hash ();
1409 input_location = saved_loc;
1412 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1413 (which may be an ifunc resolver) and issue a diagnostic when they are
1414 not compatible according to language rules (plus a C++ extension for
1415 non-static member functions). */
1417 static void
1418 maybe_diag_incompatible_alias (tree alias, tree target)
1420 tree altype = TREE_TYPE (alias);
1421 tree targtype = TREE_TYPE (target);
1423 bool ifunc = cgraph_node::get (alias)->ifunc_resolver;
1424 tree funcptr = altype;
1426 if (ifunc)
1428 /* Handle attribute ifunc first. */
1429 if (TREE_CODE (altype) == METHOD_TYPE)
1431 /* Set FUNCPTR to the type of the alias target. If the type
1432 is a non-static member function of class C, construct a type
1433 of an ordinary function taking C* as the first argument,
1434 followed by the member function argument list, and use it
1435 instead to check for incompatibility. This conversion is
1436 not defined by the language but an extension provided by
1437 G++. */
1439 tree rettype = TREE_TYPE (altype);
1440 tree args = TYPE_ARG_TYPES (altype);
1441 altype = build_function_type (rettype, args);
1442 funcptr = altype;
1445 targtype = TREE_TYPE (targtype);
1447 if (POINTER_TYPE_P (targtype))
1449 targtype = TREE_TYPE (targtype);
1451 /* Only issue Wattribute-alias for conversions to void* with
1452 -Wextra. */
1453 if (VOID_TYPE_P (targtype) && !extra_warnings)
1454 return;
1456 /* Proceed to handle incompatible ifunc resolvers below. */
1458 else
1460 funcptr = build_pointer_type (funcptr);
1462 error_at (DECL_SOURCE_LOCATION (target),
1463 "%<ifunc%> resolver for %qD must return %qT",
1464 alias, funcptr);
1465 inform (DECL_SOURCE_LOCATION (alias),
1466 "resolver indirect function declared here");
1467 return;
1471 if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1472 || (prototype_p (altype)
1473 && prototype_p (targtype)
1474 && !types_compatible_p (altype, targtype))))
1476 /* Warn for incompatibilities. Avoid warning for functions
1477 without a prototype to make it possible to declare aliases
1478 without knowing the exact type, as libstdc++ does. */
1479 if (ifunc)
1481 funcptr = build_pointer_type (funcptr);
1483 auto_diagnostic_group d;
1484 if (warning_at (DECL_SOURCE_LOCATION (target),
1485 OPT_Wattribute_alias_,
1486 "%<ifunc%> resolver for %qD should return %qT",
1487 alias, funcptr))
1488 inform (DECL_SOURCE_LOCATION (alias),
1489 "resolver indirect function declared here");
1491 else
1493 auto_diagnostic_group d;
1494 if (warning_at (DECL_SOURCE_LOCATION (alias),
1495 OPT_Wattribute_alias_,
1496 "%qD alias between functions of incompatible "
1497 "types %qT and %qT", alias, altype, targtype))
1498 inform (DECL_SOURCE_LOCATION (target),
1499 "aliased declaration here");
1504 /* Translate the ugly representation of aliases as alias pairs into nice
1505 representation in callgraph. We don't handle all cases yet,
1506 unfortunately. */
1508 static void
1509 handle_alias_pairs (void)
1511 alias_pair *p;
1512 unsigned i;
1514 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1516 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1518 /* Weakrefs with target not defined in current unit are easy to handle:
1519 they behave just as external variables except we need to note the
1520 alias flag to later output the weakref pseudo op into asm file. */
1521 if (!target_node
1522 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1524 symtab_node *node = symtab_node::get (p->decl);
1525 if (node)
1527 node->alias_target = p->target;
1528 node->weakref = true;
1529 node->alias = true;
1530 node->transparent_alias = true;
1532 alias_pairs->unordered_remove (i);
1533 continue;
1535 else if (!target_node)
1537 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1538 symtab_node *node = symtab_node::get (p->decl);
1539 if (node)
1540 node->alias = false;
1541 alias_pairs->unordered_remove (i);
1542 continue;
1545 if (DECL_EXTERNAL (target_node->decl)
1546 /* We use local aliases for C++ thunks to force the tailcall
1547 to bind locally. This is a hack - to keep it working do
1548 the following (which is not strictly correct). */
1549 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1550 || ! DECL_VIRTUAL_P (target_node->decl))
1551 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1553 error ("%q+D aliased to external symbol %qE",
1554 p->decl, p->target);
1557 if (TREE_CODE (p->decl) == FUNCTION_DECL
1558 && target_node && is_a <cgraph_node *> (target_node))
1560 maybe_diag_incompatible_alias (p->decl, target_node->decl);
1562 maybe_diag_alias_attributes (p->decl, target_node->decl);
1564 cgraph_node *src_node = cgraph_node::get (p->decl);
1565 if (src_node && src_node->definition)
1566 src_node->reset ();
1567 cgraph_node::create_alias (p->decl, target_node->decl);
1568 alias_pairs->unordered_remove (i);
1570 else if (VAR_P (p->decl)
1571 && target_node && is_a <varpool_node *> (target_node))
1573 varpool_node::create_alias (p->decl, target_node->decl);
1574 alias_pairs->unordered_remove (i);
1576 else
1578 error ("%q+D alias between function and variable is not supported",
1579 p->decl);
1580 inform (DECL_SOURCE_LOCATION (target_node->decl),
1581 "aliased declaration here");
1583 alias_pairs->unordered_remove (i);
1586 vec_free (alias_pairs);
1590 /* Figure out what functions we want to assemble. */
1592 static void
1593 mark_functions_to_output (void)
1595 bool check_same_comdat_groups = false;
1596 cgraph_node *node;
1598 if (flag_checking)
1599 FOR_EACH_FUNCTION (node)
1600 gcc_assert (!node->process);
1602 FOR_EACH_FUNCTION (node)
1604 tree decl = node->decl;
1606 gcc_assert (!node->process || node->same_comdat_group);
1607 if (node->process)
1608 continue;
1610 /* We need to output all local functions that are used and not
1611 always inlined, as well as those that are reachable from
1612 outside the current compilation unit. */
1613 if (node->analyzed
1614 && !node->thunk
1615 && !node->alias
1616 && !node->inlined_to
1617 && !TREE_ASM_WRITTEN (decl)
1618 && !DECL_EXTERNAL (decl))
1620 node->process = 1;
1621 if (node->same_comdat_group)
1623 cgraph_node *next;
1624 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1625 next != node;
1626 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1627 if (!next->thunk && !next->alias
1628 && !next->comdat_local_p ())
1629 next->process = 1;
1632 else if (node->same_comdat_group)
1634 if (flag_checking)
1635 check_same_comdat_groups = true;
1637 else
1639 /* We should've reclaimed all functions that are not needed. */
1640 if (flag_checking
1641 && !node->inlined_to
1642 && gimple_has_body_p (decl)
1643 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1644 are inside partition, we can end up not removing the body since we no longer
1645 have analyzed node pointing to it. */
1646 && !node->in_other_partition
1647 && !node->alias
1648 && !node->clones
1649 && !DECL_EXTERNAL (decl))
1651 node->debug ();
1652 internal_error ("failed to reclaim unneeded function");
1654 gcc_assert (node->inlined_to
1655 || !gimple_has_body_p (decl)
1656 || node->in_other_partition
1657 || node->clones
1658 || DECL_ARTIFICIAL (decl)
1659 || DECL_EXTERNAL (decl));
1664 if (flag_checking && check_same_comdat_groups)
1665 FOR_EACH_FUNCTION (node)
1666 if (node->same_comdat_group && !node->process)
1668 tree decl = node->decl;
1669 if (!node->inlined_to
1670 && gimple_has_body_p (decl)
1671 /* FIXME: in an ltrans unit when the offline copy is outside a
1672 partition but inline copies are inside a partition, we can
1673 end up not removing the body since we no longer have an
1674 analyzed node pointing to it. */
1675 && !node->in_other_partition
1676 && !node->clones
1677 && !DECL_EXTERNAL (decl))
1679 node->debug ();
1680 internal_error ("failed to reclaim unneeded function in same "
1681 "comdat group");
1686 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1687 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1689 Set current_function_decl and cfun to newly constructed empty function body.
1690 return basic block in the function body. */
1692 basic_block
1693 init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1695 basic_block bb;
1696 edge e;
1698 current_function_decl = decl;
1699 allocate_struct_function (decl, false);
1700 gimple_register_cfg_hooks ();
1701 init_empty_tree_cfg ();
1702 init_tree_ssa (cfun);
1704 if (in_ssa)
1706 init_ssa_operands (cfun);
1707 cfun->gimple_df->in_ssa_p = true;
1708 cfun->curr_properties |= PROP_ssa;
1711 DECL_INITIAL (decl) = make_node (BLOCK);
1712 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1714 DECL_SAVED_TREE (decl) = error_mark_node;
1715 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1716 | PROP_cfg | PROP_loops);
1718 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1719 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1720 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1722 /* Create BB for body of the function and connect it properly. */
1723 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1724 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1725 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1726 bb->count = count;
1727 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1728 e->probability = profile_probability::always ();
1729 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1730 e->probability = profile_probability::always ();
1731 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1733 return bb;
1736 /* Assemble thunks and aliases associated to node. */
1738 void
1739 cgraph_node::assemble_thunks_and_aliases (void)
1741 cgraph_edge *e;
1742 ipa_ref *ref;
1744 for (e = callers; e;)
1745 if (e->caller->thunk
1746 && !e->caller->inlined_to)
1748 cgraph_node *thunk = e->caller;
1750 e = e->next_caller;
1751 expand_thunk (thunk, true, false);
1752 thunk->assemble_thunks_and_aliases ();
1754 else
1755 e = e->next_caller;
1757 FOR_EACH_ALIAS (this, ref)
1759 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1760 if (!alias->transparent_alias)
1762 bool saved_written = TREE_ASM_WRITTEN (decl);
1764 /* Force assemble_alias to really output the alias this time instead
1765 of buffering it in same alias pairs. */
1766 TREE_ASM_WRITTEN (decl) = 1;
1767 if (alias->symver)
1768 do_assemble_symver (alias->decl,
1769 DECL_ASSEMBLER_NAME (decl));
1770 else
1771 do_assemble_alias (alias->decl,
1772 DECL_ASSEMBLER_NAME (decl));
1773 alias->assemble_thunks_and_aliases ();
1774 TREE_ASM_WRITTEN (decl) = saved_written;
1779 /* Expand function specified by node. */
1781 void
1782 cgraph_node::expand (void)
1784 location_t saved_loc;
1786 /* We ought to not compile any inline clones. */
1787 gcc_assert (!inlined_to);
1789 /* __RTL functions are compiled as soon as they are parsed, so don't
1790 do it again. */
1791 if (native_rtl_p ())
1792 return;
1794 announce_function (decl);
1795 process = 0;
1796 gcc_assert (lowered);
1798 /* Initialize the default bitmap obstack. */
1799 bitmap_obstack_initialize (NULL);
1800 get_untransformed_body ();
1802 /* Generate RTL for the body of DECL. */
1804 timevar_push (TV_REST_OF_COMPILATION);
1806 gcc_assert (symtab->global_info_ready);
1808 /* Initialize the RTL code for the function. */
1809 saved_loc = input_location;
1810 input_location = DECL_SOURCE_LOCATION (decl);
1812 gcc_assert (DECL_STRUCT_FUNCTION (decl));
1813 push_cfun (DECL_STRUCT_FUNCTION (decl));
1814 init_function_start (decl);
1816 gimple_register_cfg_hooks ();
1818 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1820 update_ssa (TODO_update_ssa_only_virtuals);
1821 if (ipa_transforms_to_apply.exists ())
1822 execute_all_ipa_transforms (false);
1824 /* Perform all tree transforms and optimizations. */
1826 /* Signal the start of passes. */
1827 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1829 execute_pass_list (cfun, g->get_passes ()->all_passes);
1831 /* Signal the end of passes. */
1832 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1834 bitmap_obstack_release (&reg_obstack);
1836 /* Release the default bitmap obstack. */
1837 bitmap_obstack_release (NULL);
1839 /* If requested, warn about function definitions where the function will
1840 return a value (usually of some struct or union type) which itself will
1841 take up a lot of stack space. */
1842 if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1844 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1846 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1847 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1848 && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1849 warn_larger_than_size) > 0)
1851 unsigned int size_as_int
1852 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1854 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1855 warning (OPT_Wlarger_than_,
1856 "size of return value of %q+D is %u bytes",
1857 decl, size_as_int);
1858 else
1859 warning (OPT_Wlarger_than_,
1860 "size of return value of %q+D is larger than %wu bytes",
1861 decl, warn_larger_than_size);
1865 gimple_set_body (decl, NULL);
1866 if (DECL_STRUCT_FUNCTION (decl) == 0)
1868 /* Stop pointing to the local nodes about to be freed.
1869 But DECL_INITIAL must remain nonzero so we know this
1870 was an actual function definition. */
1871 if (DECL_INITIAL (decl) != 0)
1872 DECL_INITIAL (decl) = error_mark_node;
1875 input_location = saved_loc;
1877 ggc_collect ();
1878 timevar_pop (TV_REST_OF_COMPILATION);
1880 /* Make sure that BE didn't give up on compiling. */
1881 gcc_assert (TREE_ASM_WRITTEN (decl));
1882 if (cfun)
1883 pop_cfun ();
1885 /* It would make a lot more sense to output thunks before function body to
1886 get more forward and fewer backward jumps. This however would need
1887 solving problem with comdats. See PR48668. Also aliases must come after
1888 function itself to make one pass assemblers, like one on AIX, happy.
1889 See PR 50689.
1890 FIXME: Perhaps thunks should be move before function IFF they are not in
1891 comdat groups. */
1892 assemble_thunks_and_aliases ();
1893 release_body ();
1894 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1895 points to the dead function body. */
1896 remove_callees ();
1897 remove_all_references ();
1900 /* Node comparator that is responsible for the order that corresponds
1901 to time when a function was launched for the first time. */
1904 tp_first_run_node_cmp (const void *pa, const void *pb)
1906 const cgraph_node *a = *(const cgraph_node * const *) pa;
1907 const cgraph_node *b = *(const cgraph_node * const *) pb;
1908 unsigned int tp_first_run_a = a->tp_first_run;
1909 unsigned int tp_first_run_b = b->tp_first_run;
1911 if (!opt_for_fn (a->decl, flag_profile_reorder_functions)
1912 || a->no_reorder)
1913 tp_first_run_a = 0;
1914 if (!opt_for_fn (b->decl, flag_profile_reorder_functions)
1915 || b->no_reorder)
1916 tp_first_run_b = 0;
1918 if (tp_first_run_a == tp_first_run_b)
1919 return a->order - b->order;
1921 /* Functions with time profile must be before these without profile. */
1922 tp_first_run_a = (tp_first_run_a - 1) & INT_MAX;
1923 tp_first_run_b = (tp_first_run_b - 1) & INT_MAX;
1925 return tp_first_run_a - tp_first_run_b;
1928 /* Expand all functions that must be output.
1930 Attempt to topologically sort the nodes so function is output when
1931 all called functions are already assembled to allow data to be
1932 propagated across the callgraph. Use a stack to get smaller distance
1933 between a function and its callees (later we may choose to use a more
1934 sophisticated algorithm for function reordering; we will likely want
1935 to use subsections to make the output functions appear in top-down
1936 order). */
1938 static void
1939 expand_all_functions (void)
1941 cgraph_node *node;
1942 cgraph_node **order = XCNEWVEC (cgraph_node *,
1943 symtab->cgraph_count);
1944 cgraph_node **tp_first_run_order = XCNEWVEC (cgraph_node *,
1945 symtab->cgraph_count);
1946 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1947 int order_pos, tp_first_run_order_pos = 0, new_order_pos = 0;
1948 int i;
1950 order_pos = ipa_reverse_postorder (order);
1951 gcc_assert (order_pos == symtab->cgraph_count);
1953 /* Garbage collector may remove inline clones we eliminate during
1954 optimization. So we must be sure to not reference them. */
1955 for (i = 0; i < order_pos; i++)
1956 if (order[i]->process)
1958 if (order[i]->tp_first_run
1959 && opt_for_fn (order[i]->decl, flag_profile_reorder_functions))
1960 tp_first_run_order[tp_first_run_order_pos++] = order[i];
1961 else
1962 order[new_order_pos++] = order[i];
1965 /* First output functions with time profile in specified order. */
1966 qsort (tp_first_run_order, tp_first_run_order_pos,
1967 sizeof (cgraph_node *), tp_first_run_node_cmp);
1968 for (i = 0; i < tp_first_run_order_pos; i++)
1970 node = tp_first_run_order[i];
1972 if (node->process)
1974 expanded_func_count++;
1975 profiled_func_count++;
1977 if (symtab->dump_file)
1978 fprintf (symtab->dump_file,
1979 "Time profile order in expand_all_functions:%s:%d\n",
1980 node->dump_asm_name (), node->tp_first_run);
1981 node->process = 0;
1982 node->expand ();
1986 /* Output functions in RPO so callees get optimized before callers. This
1987 makes ipa-ra and other propagators to work.
1988 FIXME: This is far from optimal code layout. */
1989 for (i = new_order_pos - 1; i >= 0; i--)
1991 node = order[i];
1993 if (node->process)
1995 expanded_func_count++;
1996 node->process = 0;
1997 node->expand ();
2001 if (dump_file)
2002 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2003 main_input_filename, profiled_func_count, expanded_func_count);
2005 if (symtab->dump_file && tp_first_run_order_pos)
2006 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2007 profiled_func_count, expanded_func_count);
2009 symtab->process_new_functions ();
2010 free_gimplify_stack ();
2011 delete ipa_saved_clone_sources;
2012 ipa_saved_clone_sources = NULL;
2013 free (order);
2014 free (tp_first_run_order);
2017 /* This is used to sort the node types by the cgraph order number. */
2019 enum cgraph_order_sort_kind
2021 ORDER_FUNCTION,
2022 ORDER_VAR,
2023 ORDER_VAR_UNDEF,
2024 ORDER_ASM
2027 struct cgraph_order_sort
2029 /* Construct from a cgraph_node. */
2030 cgraph_order_sort (cgraph_node *node)
2031 : kind (ORDER_FUNCTION), order (node->order)
2033 u.f = node;
2036 /* Construct from a varpool_node. */
2037 cgraph_order_sort (varpool_node *node)
2038 : kind (node->definition ? ORDER_VAR : ORDER_VAR_UNDEF), order (node->order)
2040 u.v = node;
2043 /* Construct from a asm_node. */
2044 cgraph_order_sort (asm_node *node)
2045 : kind (ORDER_ASM), order (node->order)
2047 u.a = node;
2050 /* Assembly cgraph_order_sort based on its type. */
2051 void process ();
2053 enum cgraph_order_sort_kind kind;
2054 union
2056 cgraph_node *f;
2057 varpool_node *v;
2058 asm_node *a;
2059 } u;
2060 int order;
2063 /* Assembly cgraph_order_sort based on its type. */
2065 void
2066 cgraph_order_sort::process ()
2068 switch (kind)
2070 case ORDER_FUNCTION:
2071 u.f->process = 0;
2072 u.f->expand ();
2073 break;
2074 case ORDER_VAR:
2075 u.v->assemble_decl ();
2076 break;
2077 case ORDER_VAR_UNDEF:
2078 assemble_undefined_decl (u.v->decl);
2079 break;
2080 case ORDER_ASM:
2081 assemble_asm (u.a->asm_str);
2082 break;
2083 default:
2084 gcc_unreachable ();
2088 /* Compare cgraph_order_sort by order. */
2090 static int
2091 cgraph_order_cmp (const void *a_p, const void *b_p)
2093 const cgraph_order_sort *nodea = (const cgraph_order_sort *)a_p;
2094 const cgraph_order_sort *nodeb = (const cgraph_order_sort *)b_p;
2096 return nodea->order - nodeb->order;
2099 /* Output all functions, variables, and asm statements in the order
2100 according to their order fields, which is the order in which they
2101 appeared in the file. This implements -fno-toplevel-reorder. In
2102 this mode we may output functions and variables which don't really
2103 need to be output. */
2105 static void
2106 output_in_order (void)
2108 int i;
2109 cgraph_node *cnode;
2110 varpool_node *vnode;
2111 asm_node *anode;
2112 auto_vec<cgraph_order_sort> nodes;
2113 cgraph_order_sort *node;
2115 FOR_EACH_DEFINED_FUNCTION (cnode)
2116 if (cnode->process && !cnode->thunk
2117 && !cnode->alias && cnode->no_reorder)
2118 nodes.safe_push (cgraph_order_sort (cnode));
2120 /* There is a similar loop in symbol_table::output_variables.
2121 Please keep them in sync. */
2122 FOR_EACH_VARIABLE (vnode)
2123 if (vnode->no_reorder
2124 && !DECL_HARD_REGISTER (vnode->decl)
2125 && !DECL_HAS_VALUE_EXPR_P (vnode->decl))
2126 nodes.safe_push (cgraph_order_sort (vnode));
2128 for (anode = symtab->first_asm_symbol (); anode; anode = anode->next)
2129 nodes.safe_push (cgraph_order_sort (anode));
2131 /* Sort nodes by order. */
2132 nodes.qsort (cgraph_order_cmp);
2134 /* In toplevel reorder mode we output all statics; mark them as needed. */
2135 FOR_EACH_VEC_ELT (nodes, i, node)
2136 if (node->kind == ORDER_VAR)
2137 node->u.v->finalize_named_section_flags ();
2139 FOR_EACH_VEC_ELT (nodes, i, node)
2140 node->process ();
2142 symtab->clear_asm_symbols ();
2145 static void
2146 ipa_passes (void)
2148 gcc::pass_manager *passes = g->get_passes ();
2150 set_cfun (NULL);
2151 current_function_decl = NULL;
2152 gimple_register_cfg_hooks ();
2153 bitmap_obstack_initialize (NULL);
2155 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2157 if (!in_lto_p)
2159 execute_ipa_pass_list (passes->all_small_ipa_passes);
2160 if (seen_error ())
2161 return;
2164 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2165 devirtualization and other changes where removal iterate. */
2166 symtab->remove_unreachable_nodes (symtab->dump_file);
2168 /* If pass_all_early_optimizations was not scheduled, the state of
2169 the cgraph will not be properly updated. Update it now. */
2170 if (symtab->state < IPA_SSA)
2171 symtab->state = IPA_SSA;
2173 if (!in_lto_p)
2175 /* Generate coverage variables and constructors. */
2176 coverage_finish ();
2178 /* Process new functions added. */
2179 set_cfun (NULL);
2180 current_function_decl = NULL;
2181 symtab->process_new_functions ();
2183 execute_ipa_summary_passes
2184 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2187 /* Some targets need to handle LTO assembler output specially. */
2188 if (flag_generate_lto || flag_generate_offload)
2189 targetm.asm_out.lto_start ();
2191 if (!in_lto_p
2192 || flag_incremental_link == INCREMENTAL_LINK_LTO)
2194 if (!quiet_flag)
2195 fprintf (stderr, "Streaming LTO\n");
2196 if (g->have_offload)
2198 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2199 lto_stream_offload_p = true;
2200 ipa_write_summaries ();
2201 lto_stream_offload_p = false;
2203 if (flag_lto)
2205 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2206 lto_stream_offload_p = false;
2207 ipa_write_summaries ();
2211 if (flag_generate_lto || flag_generate_offload)
2212 targetm.asm_out.lto_end ();
2214 if (!flag_ltrans
2215 && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2216 || !flag_lto || flag_fat_lto_objects))
2217 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2218 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2220 bitmap_obstack_release (NULL);
2224 /* Return string alias is alias of. */
2226 static tree
2227 get_alias_symbol (tree decl)
2229 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2230 return get_identifier (TREE_STRING_POINTER
2231 (TREE_VALUE (TREE_VALUE (alias))));
2235 /* Weakrefs may be associated to external decls and thus not output
2236 at expansion time. Emit all necessary aliases. */
2238 void
2239 symbol_table::output_weakrefs (void)
2241 symtab_node *node;
2242 FOR_EACH_SYMBOL (node)
2243 if (node->alias
2244 && !TREE_ASM_WRITTEN (node->decl)
2245 && node->weakref)
2247 tree target;
2249 /* Weakrefs are special by not requiring target definition in current
2250 compilation unit. It is thus bit hard to work out what we want to
2251 alias.
2252 When alias target is defined, we need to fetch it from symtab reference,
2253 otherwise it is pointed to by alias_target. */
2254 if (node->alias_target)
2255 target = (DECL_P (node->alias_target)
2256 ? DECL_ASSEMBLER_NAME (node->alias_target)
2257 : node->alias_target);
2258 else if (node->analyzed)
2259 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2260 else
2262 gcc_unreachable ();
2263 target = get_alias_symbol (node->decl);
2265 do_assemble_alias (node->decl, target);
2269 /* Perform simple optimizations based on callgraph. */
2271 void
2272 symbol_table::compile (void)
2274 if (seen_error ())
2275 return;
2277 symtab_node::checking_verify_symtab_nodes ();
2279 timevar_push (TV_CGRAPHOPT);
2280 if (pre_ipa_mem_report)
2281 dump_memory_report ("Memory consumption before IPA");
2282 if (!quiet_flag)
2283 fprintf (stderr, "Performing interprocedural optimizations\n");
2284 state = IPA;
2286 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2287 if (flag_generate_lto || flag_generate_offload)
2288 lto_streamer_hooks_init ();
2290 /* Don't run the IPA passes if there was any error or sorry messages. */
2291 if (!seen_error ())
2293 timevar_start (TV_CGRAPH_IPA_PASSES);
2294 ipa_passes ();
2295 timevar_stop (TV_CGRAPH_IPA_PASSES);
2297 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2298 if (seen_error ()
2299 || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2300 && flag_lto && !flag_fat_lto_objects))
2302 timevar_pop (TV_CGRAPHOPT);
2303 return;
2306 global_info_ready = true;
2307 if (dump_file)
2309 fprintf (dump_file, "Optimized ");
2310 symtab->dump (dump_file);
2312 if (post_ipa_mem_report)
2313 dump_memory_report ("Memory consumption after IPA");
2314 timevar_pop (TV_CGRAPHOPT);
2316 /* Output everything. */
2317 switch_to_section (text_section);
2318 (*debug_hooks->assembly_start) ();
2319 if (!quiet_flag)
2320 fprintf (stderr, "Assembling functions:\n");
2321 symtab_node::checking_verify_symtab_nodes ();
2323 bitmap_obstack_initialize (NULL);
2324 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2325 bitmap_obstack_release (NULL);
2326 mark_functions_to_output ();
2328 /* When weakref support is missing, we automatically translate all
2329 references to NODE to references to its ultimate alias target.
2330 The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2331 TREE_CHAIN.
2333 Set up this mapping before we output any assembler but once we are sure
2334 that all symbol renaming is done.
2336 FIXME: All this ugliness can go away if we just do renaming at gimple
2337 level by physically rewriting the IL. At the moment we can only redirect
2338 calls, so we need infrastructure for renaming references as well. */
2339 #ifndef ASM_OUTPUT_WEAKREF
2340 symtab_node *node;
2342 FOR_EACH_SYMBOL (node)
2343 if (node->alias
2344 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2346 IDENTIFIER_TRANSPARENT_ALIAS
2347 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2348 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2349 = (node->alias_target ? node->alias_target
2350 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2352 #endif
2354 state = EXPANSION;
2356 /* Output first asm statements and anything ordered. The process
2357 flag is cleared for these nodes, so we skip them later. */
2358 output_in_order ();
2360 timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2361 expand_all_functions ();
2362 timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2364 output_variables ();
2366 process_new_functions ();
2367 state = FINISHED;
2368 output_weakrefs ();
2370 if (dump_file)
2372 fprintf (dump_file, "\nFinal ");
2373 symtab->dump (dump_file);
2375 if (!flag_checking)
2376 return;
2377 symtab_node::verify_symtab_nodes ();
2378 /* Double check that all inline clones are gone and that all
2379 function bodies have been released from memory. */
2380 if (!seen_error ())
2382 cgraph_node *node;
2383 bool error_found = false;
2385 FOR_EACH_DEFINED_FUNCTION (node)
2386 if (node->inlined_to
2387 || gimple_has_body_p (node->decl))
2389 error_found = true;
2390 node->debug ();
2392 if (error_found)
2393 internal_error ("nodes with unreleased memory found");
2397 /* Earlydebug dump file, flags, and number. */
2399 static int debuginfo_early_dump_nr;
2400 static FILE *debuginfo_early_dump_file;
2401 static dump_flags_t debuginfo_early_dump_flags;
2403 /* Debug dump file, flags, and number. */
2405 static int debuginfo_dump_nr;
2406 static FILE *debuginfo_dump_file;
2407 static dump_flags_t debuginfo_dump_flags;
2409 /* Register the debug and earlydebug dump files. */
2411 void
2412 debuginfo_early_init (void)
2414 gcc::dump_manager *dumps = g->get_dumps ();
2415 debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2416 "earlydebug", DK_tree,
2417 OPTGROUP_NONE,
2418 false);
2419 debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2420 "debug", DK_tree,
2421 OPTGROUP_NONE,
2422 false);
2425 /* Initialize the debug and earlydebug dump files. */
2427 void
2428 debuginfo_init (void)
2430 gcc::dump_manager *dumps = g->get_dumps ();
2431 debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2432 debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2433 debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2434 debuginfo_early_dump_flags
2435 = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2438 /* Finalize the debug and earlydebug dump files. */
2440 void
2441 debuginfo_fini (void)
2443 if (debuginfo_dump_file)
2444 dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2445 if (debuginfo_early_dump_file)
2446 dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2449 /* Set dump_file to the debug dump file. */
2451 void
2452 debuginfo_start (void)
2454 set_dump_file (debuginfo_dump_file);
2457 /* Undo setting dump_file to the debug dump file. */
2459 void
2460 debuginfo_stop (void)
2462 set_dump_file (NULL);
2465 /* Set dump_file to the earlydebug dump file. */
2467 void
2468 debuginfo_early_start (void)
2470 set_dump_file (debuginfo_early_dump_file);
2473 /* Undo setting dump_file to the earlydebug dump file. */
2475 void
2476 debuginfo_early_stop (void)
2478 set_dump_file (NULL);
2481 /* Analyze the whole compilation unit once it is parsed completely. */
2483 void
2484 symbol_table::finalize_compilation_unit (void)
2486 timevar_push (TV_CGRAPH);
2488 /* If we're here there's no current function anymore. Some frontends
2489 are lazy in clearing these. */
2490 current_function_decl = NULL;
2491 set_cfun (NULL);
2493 /* Do not skip analyzing the functions if there were errors, we
2494 miss diagnostics for following functions otherwise. */
2496 /* Emit size functions we didn't inline. */
2497 finalize_size_functions ();
2499 /* Mark alias targets necessary and emit diagnostics. */
2500 handle_alias_pairs ();
2502 if (!quiet_flag)
2504 fprintf (stderr, "\nAnalyzing compilation unit\n");
2505 fflush (stderr);
2508 if (flag_dump_passes)
2509 dump_passes ();
2511 /* Gimplify and lower all functions, compute reachability and
2512 remove unreachable nodes. */
2513 analyze_functions (/*first_time=*/true);
2515 /* Mark alias targets necessary and emit diagnostics. */
2516 handle_alias_pairs ();
2518 /* Gimplify and lower thunks. */
2519 analyze_functions (/*first_time=*/false);
2521 /* All nested functions should be lowered now. */
2522 nested_function_info::release ();
2524 /* Offloading requires LTO infrastructure. */
2525 if (!in_lto_p && g->have_offload)
2526 flag_generate_offload = 1;
2528 if (!seen_error ())
2530 /* Give the frontends the chance to emit early debug based on
2531 what is still reachable in the TU. */
2532 (*lang_hooks.finalize_early_debug) ();
2534 /* Clean up anything that needs cleaning up after initial debug
2535 generation. */
2536 debuginfo_early_start ();
2537 (*debug_hooks->early_finish) (main_input_filename);
2538 debuginfo_early_stop ();
2541 /* Finally drive the pass manager. */
2542 compile ();
2544 timevar_pop (TV_CGRAPH);
2547 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2548 within the same process. For use by toplev::finalize. */
2550 void
2551 cgraphunit_c_finalize (void)
2553 gcc_assert (cgraph_new_nodes.length () == 0);
2554 cgraph_new_nodes.truncate (0);
2556 queued_nodes = &symtab_terminator;
2558 first_analyzed = NULL;
2559 first_analyzed_var = NULL;
2562 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2563 kind of wrapper method. */
2565 void
2566 cgraph_node::create_wrapper (cgraph_node *target)
2568 /* Preserve DECL_RESULT so we get right by reference flag. */
2569 tree decl_result = DECL_RESULT (decl);
2571 /* Remove the function's body but keep arguments to be reused
2572 for thunk. */
2573 release_body (true);
2574 reset ();
2576 DECL_UNINLINABLE (decl) = false;
2577 DECL_RESULT (decl) = decl_result;
2578 DECL_INITIAL (decl) = NULL;
2579 allocate_struct_function (decl, false);
2580 set_cfun (NULL);
2582 /* Turn alias into thunk and expand it into GIMPLE representation. */
2583 definition = true;
2585 /* Create empty thunk, but be sure we did not keep former thunk around.
2586 In that case we would need to preserve the info. */
2587 gcc_checking_assert (!thunk_info::get (this));
2588 thunk_info::get_create (this);
2589 thunk = true;
2590 create_edge (target, NULL, count);
2591 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2593 tree arguments = DECL_ARGUMENTS (decl);
2595 while (arguments)
2597 TREE_ADDRESSABLE (arguments) = false;
2598 arguments = TREE_CHAIN (arguments);
2601 expand_thunk (this, false, true);
2602 thunk_info::remove (this);
2604 /* Inline summary set-up. */
2605 analyze ();
2606 inline_analyze_function (this);