aix: Fix _STDC_FORMAT_MACROS in inttypes.h [PR97044]
[official-gcc.git] / gcc / cgraphunit.c
blobbedb6e2eea1519f8f588d1a605c8162982aac175
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"
211 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
212 secondary queue used during optimization to accommodate passes that
213 may generate new functions that need to be optimized and expanded. */
214 vec<cgraph_node *> cgraph_new_nodes;
216 static void expand_all_functions (void);
217 static void mark_functions_to_output (void);
218 static void handle_alias_pairs (void);
220 /* Used for vtable lookup in thunk adjusting. */
221 static GTY (()) tree vtable_entry_type;
223 /* Return true if this symbol is a function from the C frontend specified
224 directly in RTL form (with "__RTL"). */
226 bool
227 symtab_node::native_rtl_p () const
229 if (TREE_CODE (decl) != FUNCTION_DECL)
230 return false;
231 if (!DECL_STRUCT_FUNCTION (decl))
232 return false;
233 return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
236 /* Determine if symbol declaration is needed. That is, visible to something
237 either outside this translation unit, something magic in the system
238 configury */
239 bool
240 symtab_node::needed_p (void)
242 /* Double check that no one output the function into assembly file
243 early. */
244 if (!native_rtl_p ())
245 gcc_checking_assert
246 (!DECL_ASSEMBLER_NAME_SET_P (decl)
247 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
249 if (!definition)
250 return false;
252 if (DECL_EXTERNAL (decl))
253 return false;
255 /* If the user told us it is used, then it must be so. */
256 if (force_output)
257 return true;
259 /* ABI forced symbols are needed when they are external. */
260 if (forced_by_abi && TREE_PUBLIC (decl))
261 return true;
263 /* Keep constructors, destructors and virtual functions. */
264 if (TREE_CODE (decl) == FUNCTION_DECL
265 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
266 return true;
268 /* Externally visible variables must be output. The exception is
269 COMDAT variables that must be output only when they are needed. */
270 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
271 return true;
273 return false;
276 /* Head and terminator of the queue of nodes to be processed while building
277 callgraph. */
279 static symtab_node symtab_terminator (SYMTAB_SYMBOL);
280 static symtab_node *queued_nodes = &symtab_terminator;
282 /* Add NODE to queue starting at QUEUED_NODES.
283 The queue is linked via AUX pointers and terminated by pointer to 1. */
285 static void
286 enqueue_node (symtab_node *node)
288 if (node->aux)
289 return;
290 gcc_checking_assert (queued_nodes);
291 node->aux = queued_nodes;
292 queued_nodes = node;
295 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
296 functions into callgraph in a way so they look like ordinary reachable
297 functions inserted into callgraph already at construction time. */
299 void
300 symbol_table::process_new_functions (void)
302 tree fndecl;
304 if (!cgraph_new_nodes.exists ())
305 return;
307 handle_alias_pairs ();
308 /* Note that this queue may grow as its being processed, as the new
309 functions may generate new ones. */
310 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
312 cgraph_node *node = cgraph_new_nodes[i];
313 fndecl = node->decl;
314 switch (state)
316 case CONSTRUCTION:
317 /* At construction time we just need to finalize function and move
318 it into reachable functions list. */
320 cgraph_node::finalize_function (fndecl, false);
321 call_cgraph_insertion_hooks (node);
322 enqueue_node (node);
323 break;
325 case IPA:
326 case IPA_SSA:
327 case IPA_SSA_AFTER_INLINING:
328 /* When IPA optimization already started, do all essential
329 transformations that has been already performed on the whole
330 cgraph but not on this function. */
332 gimple_register_cfg_hooks ();
333 if (!node->analyzed)
334 node->analyze ();
335 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
336 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
337 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
339 bool summaried_computed = ipa_fn_summaries != NULL;
340 g->get_passes ()->execute_early_local_passes ();
341 /* Early passes compute inline parameters to do inlining
342 and splitting. This is redundant for functions added late.
343 Just throw away whatever it did. */
344 if (!summaried_computed)
346 ipa_free_fn_summary ();
347 ipa_free_size_summary ();
350 else if (ipa_fn_summaries != NULL)
351 compute_fn_summary (node, true);
352 free_dominance_info (CDI_POST_DOMINATORS);
353 free_dominance_info (CDI_DOMINATORS);
354 pop_cfun ();
355 call_cgraph_insertion_hooks (node);
356 break;
358 case EXPANSION:
359 /* Functions created during expansion shall be compiled
360 directly. */
361 node->process = 0;
362 call_cgraph_insertion_hooks (node);
363 node->expand ();
364 break;
366 default:
367 gcc_unreachable ();
368 break;
372 cgraph_new_nodes.release ();
375 /* As an GCC extension we allow redefinition of the function. The
376 semantics when both copies of bodies differ is not well defined.
377 We replace the old body with new body so in unit at a time mode
378 we always use new body, while in normal mode we may end up with
379 old body inlined into some functions and new body expanded and
380 inlined in others.
382 ??? It may make more sense to use one body for inlining and other
383 body for expanding the function but this is difficult to do. */
385 void
386 cgraph_node::reset (void)
388 /* If process is set, then we have already begun whole-unit analysis.
389 This is *not* testing for whether we've already emitted the function.
390 That case can be sort-of legitimately seen with real function redefinition
391 errors. I would argue that the front end should never present us with
392 such a case, but don't enforce that for now. */
393 gcc_assert (!process);
395 /* Reset our data structures so we can analyze the function again. */
396 inlined_to = NULL;
397 memset (&rtl, 0, sizeof (rtl));
398 analyzed = false;
399 definition = false;
400 alias = false;
401 transparent_alias = false;
402 weakref = false;
403 cpp_implicit_alias = false;
405 remove_callees ();
406 remove_all_references ();
409 /* Return true when there are references to the node. INCLUDE_SELF is
410 true if a self reference counts as a reference. */
412 bool
413 symtab_node::referred_to_p (bool include_self)
415 ipa_ref *ref = NULL;
417 /* See if there are any references at all. */
418 if (iterate_referring (0, ref))
419 return true;
420 /* For functions check also calls. */
421 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
422 if (cn && cn->callers)
424 if (include_self)
425 return true;
426 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
427 if (e->caller != this)
428 return true;
430 return false;
433 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
434 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
435 the garbage collector run at the moment. We would need to either create
436 a new GC context, or just not compile right now. */
438 void
439 cgraph_node::finalize_function (tree decl, bool no_collect)
441 cgraph_node *node = cgraph_node::get_create (decl);
443 if (node->definition)
445 /* Nested functions should only be defined once. */
446 gcc_assert (!DECL_CONTEXT (decl)
447 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
448 node->reset ();
449 node->redefined_extern_inline = true;
452 /* Set definition first before calling notice_global_symbol so that
453 it is available to notice_global_symbol. */
454 node->definition = true;
455 notice_global_symbol (decl);
456 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
457 if (!flag_toplevel_reorder)
458 node->no_reorder = true;
460 /* With -fkeep-inline-functions we are keeping all inline functions except
461 for extern inline ones. */
462 if (flag_keep_inline_functions
463 && DECL_DECLARED_INLINE_P (decl)
464 && !DECL_EXTERNAL (decl)
465 && !DECL_DISREGARD_INLINE_LIMITS (decl))
466 node->force_output = 1;
468 /* __RTL functions were already output as soon as they were parsed (due
469 to the large amount of global state in the backend).
470 Mark such functions as "force_output" to reflect the fact that they
471 will be in the asm file when considering the symbols they reference.
472 The attempt to output them later on will bail out immediately. */
473 if (node->native_rtl_p ())
474 node->force_output = 1;
476 /* When not optimizing, also output the static functions. (see
477 PR24561), but don't do so for always_inline functions, functions
478 declared inline and nested functions. These were optimized out
479 in the original implementation and it is unclear whether we want
480 to change the behavior here. */
481 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
482 || node->no_reorder)
483 && !node->cpp_implicit_alias
484 && !DECL_DISREGARD_INLINE_LIMITS (decl)
485 && !DECL_DECLARED_INLINE_P (decl)
486 && !(DECL_CONTEXT (decl)
487 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
488 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
489 node->force_output = 1;
491 /* If we've not yet emitted decl, tell the debug info about it. */
492 if (!TREE_ASM_WRITTEN (decl))
493 (*debug_hooks->deferred_inline_function) (decl);
495 if (!no_collect)
496 ggc_collect ();
498 if (symtab->state == CONSTRUCTION
499 && (node->needed_p () || node->referred_to_p ()))
500 enqueue_node (node);
503 /* Add the function FNDECL to the call graph.
504 Unlike finalize_function, this function is intended to be used
505 by middle end and allows insertion of new function at arbitrary point
506 of compilation. The function can be either in high, low or SSA form
507 GIMPLE.
509 The function is assumed to be reachable and have address taken (so no
510 API breaking optimizations are performed on it).
512 Main work done by this function is to enqueue the function for later
513 processing to avoid need the passes to be re-entrant. */
515 void
516 cgraph_node::add_new_function (tree fndecl, bool lowered)
518 gcc::pass_manager *passes = g->get_passes ();
519 cgraph_node *node;
521 if (dump_file)
523 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
524 const char *function_type = ((gimple_has_body_p (fndecl))
525 ? (lowered
526 ? (gimple_in_ssa_p (fn)
527 ? "ssa gimple"
528 : "low gimple")
529 : "high gimple")
530 : "to-be-gimplified");
531 fprintf (dump_file,
532 "Added new %s function %s to callgraph\n",
533 function_type,
534 fndecl_name (fndecl));
537 switch (symtab->state)
539 case PARSING:
540 cgraph_node::finalize_function (fndecl, false);
541 break;
542 case CONSTRUCTION:
543 /* Just enqueue function to be processed at nearest occurrence. */
544 node = cgraph_node::get_create (fndecl);
545 if (lowered)
546 node->lowered = true;
547 cgraph_new_nodes.safe_push (node);
548 break;
550 case IPA:
551 case IPA_SSA:
552 case IPA_SSA_AFTER_INLINING:
553 case EXPANSION:
554 /* Bring the function into finalized state and enqueue for later
555 analyzing and compilation. */
556 node = cgraph_node::get_create (fndecl);
557 node->local = false;
558 node->definition = true;
559 node->force_output = true;
560 if (TREE_PUBLIC (fndecl))
561 node->externally_visible = true;
562 if (!lowered && symtab->state == EXPANSION)
564 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
565 gimple_register_cfg_hooks ();
566 bitmap_obstack_initialize (NULL);
567 execute_pass_list (cfun, passes->all_lowering_passes);
568 passes->execute_early_local_passes ();
569 bitmap_obstack_release (NULL);
570 pop_cfun ();
572 lowered = true;
574 if (lowered)
575 node->lowered = true;
576 cgraph_new_nodes.safe_push (node);
577 break;
579 case FINISHED:
580 /* At the very end of compilation we have to do all the work up
581 to expansion. */
582 node = cgraph_node::create (fndecl);
583 if (lowered)
584 node->lowered = true;
585 node->definition = true;
586 node->analyze ();
587 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
588 gimple_register_cfg_hooks ();
589 bitmap_obstack_initialize (NULL);
590 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
591 g->get_passes ()->execute_early_local_passes ();
592 bitmap_obstack_release (NULL);
593 pop_cfun ();
594 node->expand ();
595 break;
597 default:
598 gcc_unreachable ();
601 /* Set a personality if required and we already passed EH lowering. */
602 if (lowered
603 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
604 == eh_personality_lang))
605 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
608 /* Analyze the function scheduled to be output. */
609 void
610 cgraph_node::analyze (void)
612 if (native_rtl_p ())
614 analyzed = true;
615 return;
618 tree decl = this->decl;
619 location_t saved_loc = input_location;
620 input_location = DECL_SOURCE_LOCATION (decl);
622 if (thunk.thunk_p)
624 cgraph_node *t = cgraph_node::get (thunk.alias);
626 create_edge (t, NULL, t->count);
627 callees->can_throw_external = !TREE_NOTHROW (t->decl);
628 /* Target code in expand_thunk may need the thunk's target
629 to be analyzed, so recurse here. */
630 if (!t->analyzed && t->definition)
631 t->analyze ();
632 if (t->alias)
634 t = t->get_alias_target ();
635 if (!t->analyzed && t->definition)
636 t->analyze ();
638 bool ret = expand_thunk (false, false);
639 thunk.alias = NULL;
640 if (!ret)
641 return;
643 if (alias)
644 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
645 else if (dispatcher_function)
647 /* Generate the dispatcher body of multi-versioned functions. */
648 cgraph_function_version_info *dispatcher_version_info
649 = function_version ();
650 if (dispatcher_version_info != NULL
651 && (dispatcher_version_info->dispatcher_resolver
652 == NULL_TREE))
654 tree resolver = NULL_TREE;
655 gcc_assert (targetm.generate_version_dispatcher_body);
656 resolver = targetm.generate_version_dispatcher_body (this);
657 gcc_assert (resolver != NULL_TREE);
660 else
662 push_cfun (DECL_STRUCT_FUNCTION (decl));
664 assign_assembler_name_if_needed (decl);
666 /* Make sure to gimplify bodies only once. During analyzing a
667 function we lower it, which will require gimplified nested
668 functions, so we can end up here with an already gimplified
669 body. */
670 if (!gimple_has_body_p (decl))
671 gimplify_function_tree (decl);
673 /* Lower the function. */
674 if (!lowered)
676 if (nested)
677 lower_nested_functions (decl);
678 gcc_assert (!nested);
680 gimple_register_cfg_hooks ();
681 bitmap_obstack_initialize (NULL);
682 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
683 free_dominance_info (CDI_POST_DOMINATORS);
684 free_dominance_info (CDI_DOMINATORS);
685 compact_blocks ();
686 bitmap_obstack_release (NULL);
687 lowered = true;
690 pop_cfun ();
692 analyzed = true;
694 input_location = saved_loc;
697 /* C++ frontend produce same body aliases all over the place, even before PCH
698 gets streamed out. It relies on us linking the aliases with their function
699 in order to do the fixups, but ipa-ref is not PCH safe. Consequently we
700 first produce aliases without links, but once C++ FE is sure he won't stream
701 PCH we build the links via this function. */
703 void
704 symbol_table::process_same_body_aliases (void)
706 symtab_node *node;
707 FOR_EACH_SYMBOL (node)
708 if (node->cpp_implicit_alias && !node->analyzed)
709 node->resolve_alias
710 (VAR_P (node->alias_target)
711 ? (symtab_node *)varpool_node::get_create (node->alias_target)
712 : (symtab_node *)cgraph_node::get_create (node->alias_target));
713 cpp_implicit_aliases_done = true;
716 /* Process a symver attribute. */
718 static void
719 process_symver_attribute (symtab_node *n)
721 tree value = lookup_attribute ("symver", DECL_ATTRIBUTES (n->decl));
723 for (; value != NULL; value = TREE_CHAIN (value))
725 /* Starting from bintuils 2.35 gas supports:
726 # Assign foo to bar@V1 and baz@V2.
727 .symver foo, bar@V1
728 .symver foo, baz@V2
730 const char *purpose = IDENTIFIER_POINTER (TREE_PURPOSE (value));
731 if (strcmp (purpose, "symver") != 0)
732 continue;
734 tree symver = get_identifier_with_length
735 (TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (value))),
736 TREE_STRING_LENGTH (TREE_VALUE (TREE_VALUE (value))));
737 symtab_node *def = symtab_node::get_for_asmname (symver);
739 if (def)
741 error_at (DECL_SOURCE_LOCATION (n->decl),
742 "duplicate definition of a symbol version");
743 inform (DECL_SOURCE_LOCATION (def->decl),
744 "same version was previously defined here");
745 return;
747 if (!n->definition)
749 error_at (DECL_SOURCE_LOCATION (n->decl),
750 "symbol needs to be defined to have a version");
751 return;
753 if (DECL_COMMON (n->decl))
755 error_at (DECL_SOURCE_LOCATION (n->decl),
756 "common symbol cannot be versioned");
757 return;
759 if (DECL_COMDAT (n->decl))
761 error_at (DECL_SOURCE_LOCATION (n->decl),
762 "comdat symbol cannot be versioned");
763 return;
765 if (n->weakref)
767 error_at (DECL_SOURCE_LOCATION (n->decl),
768 "%<weakref%> cannot be versioned");
769 return;
771 if (!TREE_PUBLIC (n->decl))
773 error_at (DECL_SOURCE_LOCATION (n->decl),
774 "versioned symbol must be public");
775 return;
777 if (DECL_VISIBILITY (n->decl) != VISIBILITY_DEFAULT)
779 error_at (DECL_SOURCE_LOCATION (n->decl),
780 "versioned symbol must have default visibility");
781 return;
784 /* Create new symbol table entry representing the version. */
785 tree new_decl = copy_node (n->decl);
787 DECL_INITIAL (new_decl) = NULL_TREE;
788 if (TREE_CODE (new_decl) == FUNCTION_DECL)
789 DECL_STRUCT_FUNCTION (new_decl) = NULL;
790 SET_DECL_ASSEMBLER_NAME (new_decl, symver);
791 TREE_PUBLIC (new_decl) = 1;
792 DECL_ATTRIBUTES (new_decl) = NULL;
794 symtab_node *symver_node = symtab_node::get_create (new_decl);
795 symver_node->alias = true;
796 symver_node->definition = true;
797 symver_node->symver = true;
798 symver_node->create_reference (n, IPA_REF_ALIAS, NULL);
799 symver_node->analyzed = true;
803 /* Process attributes common for vars and functions. */
805 static void
806 process_common_attributes (symtab_node *node, tree decl)
808 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
810 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
812 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
813 "%<weakref%> attribute should be accompanied with"
814 " an %<alias%> attribute");
815 DECL_WEAK (decl) = 0;
816 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
817 DECL_ATTRIBUTES (decl));
820 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
821 node->no_reorder = 1;
822 process_symver_attribute (node);
825 /* Look for externally_visible and used attributes and mark cgraph nodes
826 accordingly.
828 We cannot mark the nodes at the point the attributes are processed (in
829 handle_*_attribute) because the copy of the declarations available at that
830 point may not be canonical. For example, in:
832 void f();
833 void f() __attribute__((used));
835 the declaration we see in handle_used_attribute will be the second
836 declaration -- but the front end will subsequently merge that declaration
837 with the original declaration and discard the second declaration.
839 Furthermore, we can't mark these nodes in finalize_function because:
841 void f() {}
842 void f() __attribute__((externally_visible));
844 is valid.
846 So, we walk the nodes at the end of the translation unit, applying the
847 attributes at that point. */
849 static void
850 process_function_and_variable_attributes (cgraph_node *first,
851 varpool_node *first_var)
853 cgraph_node *node;
854 varpool_node *vnode;
856 for (node = symtab->first_function (); node != first;
857 node = symtab->next_function (node))
859 tree decl = node->decl;
861 if (node->alias
862 && lookup_attribute ("flatten", DECL_ATTRIBUTES (decl)))
864 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
865 "%<flatten%> attribute is ignored on aliases");
867 if (DECL_PRESERVE_P (decl))
868 node->mark_force_output ();
869 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
871 if (! TREE_PUBLIC (node->decl))
872 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
873 "%<externally_visible%>"
874 " attribute have effect only on public objects");
876 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
877 && node->definition
878 && (!node->alias || DECL_INITIAL (decl) != error_mark_node))
880 /* NODE->DEFINITION && NODE->ALIAS is nonzero for valid weakref
881 function declarations; DECL_INITIAL is non-null for invalid
882 weakref functions that are also defined. */
883 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
884 "%<weakref%> attribute ignored"
885 " because function is defined");
886 DECL_WEAK (decl) = 0;
887 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
888 DECL_ATTRIBUTES (decl));
889 DECL_ATTRIBUTES (decl) = remove_attribute ("alias",
890 DECL_ATTRIBUTES (decl));
891 node->alias = false;
892 node->weakref = false;
893 node->transparent_alias = false;
895 else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
896 && node->definition
897 && !node->alias)
898 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
899 "%<alias%> attribute ignored"
900 " because function is defined");
902 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
903 && !DECL_DECLARED_INLINE_P (decl)
904 /* redefining extern inline function makes it DECL_UNINLINABLE. */
905 && !DECL_UNINLINABLE (decl))
906 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
907 "%<always_inline%> function might not be inlinable");
909 process_common_attributes (node, decl);
911 for (vnode = symtab->first_variable (); vnode != first_var;
912 vnode = symtab->next_variable (vnode))
914 tree decl = vnode->decl;
915 if (DECL_EXTERNAL (decl)
916 && DECL_INITIAL (decl))
917 varpool_node::finalize_decl (decl);
918 if (DECL_PRESERVE_P (decl))
919 vnode->force_output = true;
920 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
922 if (! TREE_PUBLIC (vnode->decl))
923 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
924 "%<externally_visible%>"
925 " attribute have effect only on public objects");
927 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
928 && vnode->definition
929 && DECL_INITIAL (decl))
931 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
932 "%<weakref%> attribute ignored"
933 " because variable is initialized");
934 DECL_WEAK (decl) = 0;
935 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
936 DECL_ATTRIBUTES (decl));
938 process_common_attributes (vnode, decl);
942 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
943 middle end to output the variable to asm file, if needed or externally
944 visible. */
946 void
947 varpool_node::finalize_decl (tree decl)
949 varpool_node *node = varpool_node::get_create (decl);
951 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
953 if (node->definition)
954 return;
955 /* Set definition first before calling notice_global_symbol so that
956 it is available to notice_global_symbol. */
957 node->definition = true;
958 notice_global_symbol (decl);
959 if (!flag_toplevel_reorder)
960 node->no_reorder = true;
961 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
962 /* Traditionally we do not eliminate static variables when not
963 optimizing and when not doing toplevel reorder. */
964 || (node->no_reorder && !DECL_COMDAT (node->decl)
965 && !DECL_ARTIFICIAL (node->decl)))
966 node->force_output = true;
968 if (symtab->state == CONSTRUCTION
969 && (node->needed_p () || node->referred_to_p ()))
970 enqueue_node (node);
971 if (symtab->state >= IPA_SSA)
972 node->analyze ();
973 /* Some frontends produce various interface variables after compilation
974 finished. */
975 if (symtab->state == FINISHED
976 || (node->no_reorder
977 && symtab->state == EXPANSION))
978 node->assemble_decl ();
981 /* EDGE is an polymorphic call. Mark all possible targets as reachable
982 and if there is only one target, perform trivial devirtualization.
983 REACHABLE_CALL_TARGETS collects target lists we already walked to
984 avoid duplicate work. */
986 static void
987 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
988 cgraph_edge *edge)
990 unsigned int i;
991 void *cache_token;
992 bool final;
993 vec <cgraph_node *>targets
994 = possible_polymorphic_call_targets
995 (edge, &final, &cache_token);
997 if (!reachable_call_targets->add (cache_token))
999 if (symtab->dump_file)
1000 dump_possible_polymorphic_call_targets
1001 (symtab->dump_file, edge);
1003 for (i = 0; i < targets.length (); i++)
1005 /* Do not bother to mark virtual methods in anonymous namespace;
1006 either we will find use of virtual table defining it, or it is
1007 unused. */
1008 if (targets[i]->definition
1009 && TREE_CODE
1010 (TREE_TYPE (targets[i]->decl))
1011 == METHOD_TYPE
1012 && !type_in_anonymous_namespace_p
1013 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
1014 enqueue_node (targets[i]);
1018 /* Very trivial devirtualization; when the type is
1019 final or anonymous (so we know all its derivation)
1020 and there is only one possible virtual call target,
1021 make the edge direct. */
1022 if (final)
1024 if (targets.length () <= 1 && dbg_cnt (devirt))
1026 cgraph_node *target;
1027 if (targets.length () == 1)
1028 target = targets[0];
1029 else
1030 target = cgraph_node::create
1031 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
1033 if (symtab->dump_file)
1035 fprintf (symtab->dump_file,
1036 "Devirtualizing call: ");
1037 print_gimple_stmt (symtab->dump_file,
1038 edge->call_stmt, 0,
1039 TDF_SLIM);
1041 if (dump_enabled_p ())
1043 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
1044 "devirtualizing call in %s to %s\n",
1045 edge->caller->dump_name (),
1046 target->dump_name ());
1049 edge = cgraph_edge::make_direct (edge, target);
1050 gimple *new_call = cgraph_edge::redirect_call_stmt_to_callee (edge);
1052 if (symtab->dump_file)
1054 fprintf (symtab->dump_file, "Devirtualized as: ");
1055 print_gimple_stmt (symtab->dump_file, new_call, 0, TDF_SLIM);
1061 /* Issue appropriate warnings for the global declaration DECL. */
1063 static void
1064 check_global_declaration (symtab_node *snode)
1066 const char *decl_file;
1067 tree decl = snode->decl;
1069 /* Warn about any function declared static but not defined. We don't
1070 warn about variables, because many programs have static variables
1071 that exist only to get some text into the object file. */
1072 if (TREE_CODE (decl) == FUNCTION_DECL
1073 && DECL_INITIAL (decl) == 0
1074 && DECL_EXTERNAL (decl)
1075 && ! DECL_ARTIFICIAL (decl)
1076 && ! TREE_PUBLIC (decl))
1078 if (TREE_NO_WARNING (decl))
1080 else if (snode->referred_to_p (/*include_self=*/false))
1081 pedwarn (input_location, 0, "%q+F used but never defined", decl);
1082 else
1083 warning (OPT_Wunused_function, "%q+F declared %<static%> but never "
1084 "defined", decl);
1085 /* This symbol is effectively an "extern" declaration now. */
1086 TREE_PUBLIC (decl) = 1;
1089 /* Warn about static fns or vars defined but not used. */
1090 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
1091 || (((warn_unused_variable && ! TREE_READONLY (decl))
1092 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
1093 && (warn_unused_const_variable == 2
1094 || (main_input_filename != NULL
1095 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
1096 && filename_cmp (main_input_filename,
1097 decl_file) == 0))))
1098 && VAR_P (decl)))
1099 && ! DECL_IN_SYSTEM_HEADER (decl)
1100 && ! snode->referred_to_p (/*include_self=*/false)
1101 /* This TREE_USED check is needed in addition to referred_to_p
1102 above, because the `__unused__' attribute is not being
1103 considered for referred_to_p. */
1104 && ! TREE_USED (decl)
1105 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1106 to handle multiple external decls in different scopes. */
1107 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1108 && ! DECL_EXTERNAL (decl)
1109 && ! DECL_ARTIFICIAL (decl)
1110 && ! DECL_ABSTRACT_ORIGIN (decl)
1111 && ! TREE_PUBLIC (decl)
1112 /* A volatile variable might be used in some non-obvious way. */
1113 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1114 /* Global register variables must be declared to reserve them. */
1115 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1116 /* Global ctors and dtors are called by the runtime. */
1117 && (TREE_CODE (decl) != FUNCTION_DECL
1118 || (!DECL_STATIC_CONSTRUCTOR (decl)
1119 && !DECL_STATIC_DESTRUCTOR (decl)))
1120 /* Otherwise, ask the language. */
1121 && lang_hooks.decls.warn_unused_global (decl))
1122 warning_at (DECL_SOURCE_LOCATION (decl),
1123 (TREE_CODE (decl) == FUNCTION_DECL)
1124 ? OPT_Wunused_function
1125 : (TREE_READONLY (decl)
1126 ? OPT_Wunused_const_variable_
1127 : OPT_Wunused_variable),
1128 "%qD defined but not used", decl);
1131 /* Discover all functions and variables that are trivially needed, analyze
1132 them as well as all functions and variables referred by them */
1133 static cgraph_node *first_analyzed;
1134 static varpool_node *first_analyzed_var;
1136 /* FIRST_TIME is set to TRUE for the first time we are called for a
1137 translation unit from finalize_compilation_unit() or false
1138 otherwise. */
1140 static void
1141 analyze_functions (bool first_time)
1143 /* Keep track of already processed nodes when called multiple times for
1144 intermodule optimization. */
1145 cgraph_node *first_handled = first_analyzed;
1146 varpool_node *first_handled_var = first_analyzed_var;
1147 hash_set<void *> reachable_call_targets;
1149 symtab_node *node;
1150 symtab_node *next;
1151 int i;
1152 ipa_ref *ref;
1153 bool changed = true;
1154 location_t saved_loc = input_location;
1156 bitmap_obstack_initialize (NULL);
1157 symtab->state = CONSTRUCTION;
1158 input_location = UNKNOWN_LOCATION;
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.thunk_p
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.thunk_p)
1382 cnode->reset ();
1384 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
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.thunk_p
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.thunk_p && !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 /* Adjust PTR by the constant FIXED_OFFSET, by the vtable offset indicated by
1737 VIRTUAL_OFFSET, and by the indirect offset indicated by INDIRECT_OFFSET, if
1738 it is non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and zero
1739 for a result adjusting thunk. */
1741 tree
1742 thunk_adjust (gimple_stmt_iterator * bsi,
1743 tree ptr, bool this_adjusting,
1744 HOST_WIDE_INT fixed_offset, tree virtual_offset,
1745 HOST_WIDE_INT indirect_offset)
1747 gassign *stmt;
1748 tree ret;
1750 if (this_adjusting
1751 && fixed_offset != 0)
1753 stmt = gimple_build_assign
1754 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1755 ptr,
1756 fixed_offset));
1757 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1760 if (!vtable_entry_type && (virtual_offset || indirect_offset != 0))
1762 tree vfunc_type = make_node (FUNCTION_TYPE);
1763 TREE_TYPE (vfunc_type) = integer_type_node;
1764 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1765 layout_type (vfunc_type);
1767 vtable_entry_type = build_pointer_type (vfunc_type);
1770 /* If there's a virtual offset, look up that value in the vtable and
1771 adjust the pointer again. */
1772 if (virtual_offset)
1774 tree vtabletmp;
1775 tree vtabletmp2;
1776 tree vtabletmp3;
1778 vtabletmp =
1779 create_tmp_reg (build_pointer_type
1780 (build_pointer_type (vtable_entry_type)), "vptr");
1782 /* The vptr is always at offset zero in the object. */
1783 stmt = gimple_build_assign (vtabletmp,
1784 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1785 ptr));
1786 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1788 /* Form the vtable address. */
1789 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1790 "vtableaddr");
1791 stmt = gimple_build_assign (vtabletmp2,
1792 build_simple_mem_ref (vtabletmp));
1793 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1795 /* Find the entry with the vcall offset. */
1796 stmt = gimple_build_assign (vtabletmp2,
1797 fold_build_pointer_plus_loc (input_location,
1798 vtabletmp2,
1799 virtual_offset));
1800 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1802 /* Get the offset itself. */
1803 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1804 "vcalloffset");
1805 stmt = gimple_build_assign (vtabletmp3,
1806 build_simple_mem_ref (vtabletmp2));
1807 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1809 /* Adjust the `this' pointer. */
1810 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1811 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1812 GSI_CONTINUE_LINKING);
1815 /* Likewise for an offset that is stored in the object that contains the
1816 vtable. */
1817 if (indirect_offset != 0)
1819 tree offset_ptr, offset_tree;
1821 /* Get the address of the offset. */
1822 offset_ptr
1823 = create_tmp_reg (build_pointer_type
1824 (build_pointer_type (vtable_entry_type)),
1825 "offset_ptr");
1826 stmt = gimple_build_assign (offset_ptr,
1827 build1 (NOP_EXPR, TREE_TYPE (offset_ptr),
1828 ptr));
1829 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1831 stmt = gimple_build_assign
1832 (offset_ptr,
1833 fold_build_pointer_plus_hwi_loc (input_location, offset_ptr,
1834 indirect_offset));
1835 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1837 /* Get the offset itself. */
1838 offset_tree = create_tmp_reg (TREE_TYPE (TREE_TYPE (offset_ptr)),
1839 "offset");
1840 stmt = gimple_build_assign (offset_tree,
1841 build_simple_mem_ref (offset_ptr));
1842 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1844 /* Adjust the `this' pointer. */
1845 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset_tree);
1846 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1847 GSI_CONTINUE_LINKING);
1850 if (!this_adjusting
1851 && fixed_offset != 0)
1852 /* Adjust the pointer by the constant. */
1854 tree ptrtmp;
1856 if (VAR_P (ptr))
1857 ptrtmp = ptr;
1858 else
1860 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1861 stmt = gimple_build_assign (ptrtmp, ptr);
1862 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1864 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1865 ptrtmp, fixed_offset);
1868 /* Emit the statement and gimplify the adjustment expression. */
1869 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1870 stmt = gimple_build_assign (ret, ptr);
1871 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1873 return ret;
1876 /* Expand thunk NODE to gimple if possible.
1877 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1878 no assembler is produced.
1879 When OUTPUT_ASM_THUNK is true, also produce assembler for
1880 thunks that are not lowered. */
1882 bool
1883 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1885 bool this_adjusting = thunk.this_adjusting;
1886 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1887 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1888 HOST_WIDE_INT indirect_offset = thunk.indirect_offset;
1889 tree virtual_offset = NULL;
1890 tree alias = callees->callee->decl;
1891 tree thunk_fndecl = decl;
1892 tree a;
1894 if (!force_gimple_thunk
1895 && this_adjusting
1896 && indirect_offset == 0
1897 && !DECL_EXTERNAL (alias)
1898 && !DECL_STATIC_CHAIN (alias)
1899 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1900 virtual_value, alias))
1902 tree fn_block;
1903 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1905 if (!output_asm_thunks)
1907 analyzed = true;
1908 return false;
1911 if (in_lto_p)
1912 get_untransformed_body ();
1913 a = DECL_ARGUMENTS (thunk_fndecl);
1915 current_function_decl = thunk_fndecl;
1917 /* Ensure thunks are emitted in their correct sections. */
1918 resolve_unique_section (thunk_fndecl, 0,
1919 flag_function_sections);
1921 DECL_RESULT (thunk_fndecl)
1922 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1923 RESULT_DECL, 0, restype);
1924 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1926 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1927 create one. */
1928 fn_block = make_node (BLOCK);
1929 BLOCK_VARS (fn_block) = a;
1930 DECL_INITIAL (thunk_fndecl) = fn_block;
1931 BLOCK_SUPERCONTEXT (fn_block) = thunk_fndecl;
1932 allocate_struct_function (thunk_fndecl, false);
1933 init_function_start (thunk_fndecl);
1934 cfun->is_thunk = 1;
1935 insn_locations_init ();
1936 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1937 prologue_location = curr_insn_location ();
1939 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1940 fixed_offset, virtual_value, alias);
1942 insn_locations_finalize ();
1943 init_insn_lengths ();
1944 free_after_compilation (cfun);
1945 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1946 thunk.thunk_p = false;
1947 analyzed = false;
1949 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1951 error ("generic thunk code fails for method %qD which uses %<...%>",
1952 thunk_fndecl);
1953 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1954 analyzed = true;
1955 return false;
1957 else
1959 tree restype;
1960 basic_block bb, then_bb, else_bb, return_bb;
1961 gimple_stmt_iterator bsi;
1962 int nargs = 0;
1963 tree arg;
1964 int i;
1965 tree resdecl;
1966 tree restmp = NULL;
1968 gcall *call;
1969 greturn *ret;
1970 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1972 /* We may be called from expand_thunk that releases body except for
1973 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1974 if (in_lto_p && !force_gimple_thunk)
1975 get_untransformed_body ();
1977 /* We need to force DECL_IGNORED_P when the thunk is created
1978 after early debug was run. */
1979 if (force_gimple_thunk)
1980 DECL_IGNORED_P (thunk_fndecl) = 1;
1982 a = DECL_ARGUMENTS (thunk_fndecl);
1984 current_function_decl = thunk_fndecl;
1986 /* Ensure thunks are emitted in their correct sections. */
1987 resolve_unique_section (thunk_fndecl, 0,
1988 flag_function_sections);
1990 bitmap_obstack_initialize (NULL);
1992 if (thunk.virtual_offset_p)
1993 virtual_offset = size_int (virtual_value);
1995 /* Build the return declaration for the function. */
1996 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1997 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1999 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
2000 DECL_ARTIFICIAL (resdecl) = 1;
2001 DECL_IGNORED_P (resdecl) = 1;
2002 DECL_CONTEXT (resdecl) = thunk_fndecl;
2003 DECL_RESULT (thunk_fndecl) = resdecl;
2005 else
2006 resdecl = DECL_RESULT (thunk_fndecl);
2008 profile_count cfg_count = count;
2009 if (!cfg_count.initialized_p ())
2010 cfg_count = profile_count::from_gcov_type (BB_FREQ_MAX).guessed_local ();
2012 bb = then_bb = else_bb = return_bb
2013 = init_lowered_empty_function (thunk_fndecl, true, cfg_count);
2015 bsi = gsi_start_bb (bb);
2017 /* Build call to the function being thunked. */
2018 if (!VOID_TYPE_P (restype)
2019 && (!alias_is_noreturn
2020 || TREE_ADDRESSABLE (restype)
2021 || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST))
2023 if (DECL_BY_REFERENCE (resdecl))
2025 restmp = gimple_fold_indirect_ref (resdecl);
2026 if (!restmp)
2027 restmp = build2 (MEM_REF,
2028 TREE_TYPE (TREE_TYPE (resdecl)),
2029 resdecl,
2030 build_int_cst (TREE_TYPE (resdecl), 0));
2032 else if (!is_gimple_reg_type (restype))
2034 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
2036 restmp = resdecl;
2038 if (VAR_P (restmp))
2040 add_local_decl (cfun, restmp);
2041 BLOCK_VARS (DECL_INITIAL (current_function_decl))
2042 = restmp;
2045 else
2046 restmp = create_tmp_var (restype, "retval");
2048 else
2049 restmp = create_tmp_reg (restype, "retval");
2052 for (arg = a; arg; arg = DECL_CHAIN (arg))
2053 nargs++;
2054 auto_vec<tree> vargs (nargs);
2055 i = 0;
2056 arg = a;
2057 if (this_adjusting)
2059 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
2060 virtual_offset, indirect_offset));
2061 arg = DECL_CHAIN (a);
2062 i = 1;
2065 if (nargs)
2066 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
2068 tree tmp = arg;
2069 DECL_NOT_GIMPLE_REG_P (arg) = 0;
2070 if (!is_gimple_val (arg))
2072 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
2073 (TREE_TYPE (arg)), "arg");
2074 gimple *stmt = gimple_build_assign (tmp, arg);
2075 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
2077 vargs.quick_push (tmp);
2079 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
2080 callees->call_stmt = call;
2081 gimple_call_set_from_thunk (call, true);
2082 if (DECL_STATIC_CHAIN (alias))
2084 tree p = DECL_STRUCT_FUNCTION (alias)->static_chain_decl;
2085 tree type = TREE_TYPE (p);
2086 tree decl = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
2087 PARM_DECL, create_tmp_var_name ("CHAIN"),
2088 type);
2089 DECL_ARTIFICIAL (decl) = 1;
2090 DECL_IGNORED_P (decl) = 1;
2091 TREE_USED (decl) = 1;
2092 DECL_CONTEXT (decl) = thunk_fndecl;
2093 DECL_ARG_TYPE (decl) = type;
2094 TREE_READONLY (decl) = 1;
2096 struct function *sf = DECL_STRUCT_FUNCTION (thunk_fndecl);
2097 sf->static_chain_decl = decl;
2099 gimple_call_set_chain (call, decl);
2102 /* Return slot optimization is always possible and in fact required to
2103 return values with DECL_BY_REFERENCE. */
2104 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
2105 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
2106 || DECL_BY_REFERENCE (resdecl)))
2107 gimple_call_set_return_slot_opt (call, true);
2109 if (restmp)
2111 gimple_call_set_lhs (call, restmp);
2112 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
2113 TREE_TYPE (TREE_TYPE (alias))));
2115 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
2116 if (!alias_is_noreturn)
2118 if (restmp && !this_adjusting
2119 && (fixed_offset || virtual_offset))
2121 tree true_label = NULL_TREE;
2123 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
2125 gimple *stmt;
2126 edge e;
2127 /* If the return type is a pointer, we need to
2128 protect against NULL. We know there will be an
2129 adjustment, because that's why we're emitting a
2130 thunk. */
2131 then_bb = create_basic_block (NULL, bb);
2132 then_bb->count = cfg_count - cfg_count.apply_scale (1, 16);
2133 return_bb = create_basic_block (NULL, then_bb);
2134 return_bb->count = cfg_count;
2135 else_bb = create_basic_block (NULL, else_bb);
2136 else_bb->count = cfg_count.apply_scale (1, 16);
2137 add_bb_to_loop (then_bb, bb->loop_father);
2138 add_bb_to_loop (return_bb, bb->loop_father);
2139 add_bb_to_loop (else_bb, bb->loop_father);
2140 remove_edge (single_succ_edge (bb));
2141 true_label = gimple_block_label (then_bb);
2142 stmt = gimple_build_cond (NE_EXPR, restmp,
2143 build_zero_cst (TREE_TYPE (restmp)),
2144 NULL_TREE, NULL_TREE);
2145 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
2146 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
2147 e->probability = profile_probability::guessed_always ()
2148 .apply_scale (1, 16);
2149 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
2150 e->probability = profile_probability::guessed_always ()
2151 .apply_scale (1, 16);
2152 make_single_succ_edge (return_bb,
2153 EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
2154 make_single_succ_edge (then_bb, return_bb, EDGE_FALLTHRU);
2155 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
2156 e->probability = profile_probability::always ();
2157 bsi = gsi_last_bb (then_bb);
2160 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
2161 fixed_offset, virtual_offset,
2162 indirect_offset);
2163 if (true_label)
2165 gimple *stmt;
2166 bsi = gsi_last_bb (else_bb);
2167 stmt = gimple_build_assign (restmp,
2168 build_zero_cst (TREE_TYPE (restmp)));
2169 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
2170 bsi = gsi_last_bb (return_bb);
2173 else
2175 gimple_call_set_tail (call, true);
2176 cfun->tail_call_marked = true;
2179 /* Build return value. */
2180 if (!DECL_BY_REFERENCE (resdecl))
2181 ret = gimple_build_return (restmp);
2182 else
2183 ret = gimple_build_return (resdecl);
2185 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
2187 else
2189 gimple_call_set_tail (call, true);
2190 cfun->tail_call_marked = true;
2191 remove_edge (single_succ_edge (bb));
2194 cfun->gimple_df->in_ssa_p = true;
2195 update_max_bb_count ();
2196 profile_status_for_fn (cfun)
2197 = cfg_count.initialized_p () && cfg_count.ipa_p ()
2198 ? PROFILE_READ : PROFILE_GUESSED;
2199 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
2200 TREE_ASM_WRITTEN (thunk_fndecl) = false;
2201 delete_unreachable_blocks ();
2202 update_ssa (TODO_update_ssa);
2203 checking_verify_flow_info ();
2204 free_dominance_info (CDI_DOMINATORS);
2206 /* Since we want to emit the thunk, we explicitly mark its name as
2207 referenced. */
2208 thunk.thunk_p = false;
2209 lowered = true;
2210 bitmap_obstack_release (NULL);
2212 current_function_decl = NULL;
2213 set_cfun (NULL);
2214 return true;
2217 /* Assemble thunks and aliases associated to node. */
2219 void
2220 cgraph_node::assemble_thunks_and_aliases (void)
2222 cgraph_edge *e;
2223 ipa_ref *ref;
2225 for (e = callers; e;)
2226 if (e->caller->thunk.thunk_p
2227 && !e->caller->inlined_to)
2229 cgraph_node *thunk = e->caller;
2231 e = e->next_caller;
2232 thunk->expand_thunk (true, false);
2233 thunk->assemble_thunks_and_aliases ();
2235 else
2236 e = e->next_caller;
2238 FOR_EACH_ALIAS (this, ref)
2240 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2241 if (!alias->transparent_alias)
2243 bool saved_written = TREE_ASM_WRITTEN (decl);
2245 /* Force assemble_alias to really output the alias this time instead
2246 of buffering it in same alias pairs. */
2247 TREE_ASM_WRITTEN (decl) = 1;
2248 if (alias->symver)
2249 do_assemble_symver (alias->decl,
2250 DECL_ASSEMBLER_NAME (decl));
2251 else
2252 do_assemble_alias (alias->decl,
2253 DECL_ASSEMBLER_NAME (decl));
2254 alias->assemble_thunks_and_aliases ();
2255 TREE_ASM_WRITTEN (decl) = saved_written;
2260 /* Expand function specified by node. */
2262 void
2263 cgraph_node::expand (void)
2265 location_t saved_loc;
2267 /* We ought to not compile any inline clones. */
2268 gcc_assert (!inlined_to);
2270 /* __RTL functions are compiled as soon as they are parsed, so don't
2271 do it again. */
2272 if (native_rtl_p ())
2273 return;
2275 announce_function (decl);
2276 process = 0;
2277 gcc_assert (lowered);
2278 get_untransformed_body ();
2280 /* Generate RTL for the body of DECL. */
2282 timevar_push (TV_REST_OF_COMPILATION);
2284 gcc_assert (symtab->global_info_ready);
2286 /* Initialize the default bitmap obstack. */
2287 bitmap_obstack_initialize (NULL);
2289 /* Initialize the RTL code for the function. */
2290 saved_loc = input_location;
2291 input_location = DECL_SOURCE_LOCATION (decl);
2293 gcc_assert (DECL_STRUCT_FUNCTION (decl));
2294 push_cfun (DECL_STRUCT_FUNCTION (decl));
2295 init_function_start (decl);
2297 gimple_register_cfg_hooks ();
2299 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
2301 update_ssa (TODO_update_ssa_only_virtuals);
2302 execute_all_ipa_transforms (false);
2304 /* Perform all tree transforms and optimizations. */
2306 /* Signal the start of passes. */
2307 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
2309 execute_pass_list (cfun, g->get_passes ()->all_passes);
2311 /* Signal the end of passes. */
2312 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
2314 bitmap_obstack_release (&reg_obstack);
2316 /* Release the default bitmap obstack. */
2317 bitmap_obstack_release (NULL);
2319 /* If requested, warn about function definitions where the function will
2320 return a value (usually of some struct or union type) which itself will
2321 take up a lot of stack space. */
2322 if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
2324 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
2326 if (ret_type && TYPE_SIZE_UNIT (ret_type)
2327 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
2328 && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
2329 warn_larger_than_size) > 0)
2331 unsigned int size_as_int
2332 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
2334 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
2335 warning (OPT_Wlarger_than_,
2336 "size of return value of %q+D is %u bytes",
2337 decl, size_as_int);
2338 else
2339 warning (OPT_Wlarger_than_,
2340 "size of return value of %q+D is larger than %wu bytes",
2341 decl, warn_larger_than_size);
2345 gimple_set_body (decl, NULL);
2346 if (DECL_STRUCT_FUNCTION (decl) == 0
2347 && !cgraph_node::get (decl)->origin)
2349 /* Stop pointing to the local nodes about to be freed.
2350 But DECL_INITIAL must remain nonzero so we know this
2351 was an actual function definition.
2352 For a nested function, this is done in c_pop_function_context.
2353 If rest_of_compilation set this to 0, leave it 0. */
2354 if (DECL_INITIAL (decl) != 0)
2355 DECL_INITIAL (decl) = error_mark_node;
2358 input_location = saved_loc;
2360 ggc_collect ();
2361 timevar_pop (TV_REST_OF_COMPILATION);
2363 /* Make sure that BE didn't give up on compiling. */
2364 gcc_assert (TREE_ASM_WRITTEN (decl));
2365 if (cfun)
2366 pop_cfun ();
2368 /* It would make a lot more sense to output thunks before function body to
2369 get more forward and fewer backward jumps. This however would need
2370 solving problem with comdats. See PR48668. Also aliases must come after
2371 function itself to make one pass assemblers, like one on AIX, happy.
2372 See PR 50689.
2373 FIXME: Perhaps thunks should be move before function IFF they are not in
2374 comdat groups. */
2375 assemble_thunks_and_aliases ();
2376 release_body ();
2377 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2378 points to the dead function body. */
2379 remove_callees ();
2380 remove_all_references ();
2383 /* Node comparator that is responsible for the order that corresponds
2384 to time when a function was launched for the first time. */
2387 tp_first_run_node_cmp (const void *pa, const void *pb)
2389 const cgraph_node *a = *(const cgraph_node * const *) pa;
2390 const cgraph_node *b = *(const cgraph_node * const *) pb;
2391 unsigned int tp_first_run_a = a->tp_first_run;
2392 unsigned int tp_first_run_b = b->tp_first_run;
2394 if (!opt_for_fn (a->decl, flag_profile_reorder_functions)
2395 || a->no_reorder)
2396 tp_first_run_a = 0;
2397 if (!opt_for_fn (b->decl, flag_profile_reorder_functions)
2398 || b->no_reorder)
2399 tp_first_run_b = 0;
2401 if (tp_first_run_a == tp_first_run_b)
2402 return a->order - b->order;
2404 /* Functions with time profile must be before these without profile. */
2405 tp_first_run_a = (tp_first_run_a - 1) & INT_MAX;
2406 tp_first_run_b = (tp_first_run_b - 1) & INT_MAX;
2408 return tp_first_run_a - tp_first_run_b;
2411 /* Expand all functions that must be output.
2413 Attempt to topologically sort the nodes so function is output when
2414 all called functions are already assembled to allow data to be
2415 propagated across the callgraph. Use a stack to get smaller distance
2416 between a function and its callees (later we may choose to use a more
2417 sophisticated algorithm for function reordering; we will likely want
2418 to use subsections to make the output functions appear in top-down
2419 order). */
2421 static void
2422 expand_all_functions (void)
2424 cgraph_node *node;
2425 cgraph_node **order = XCNEWVEC (cgraph_node *,
2426 symtab->cgraph_count);
2427 cgraph_node **tp_first_run_order = XCNEWVEC (cgraph_node *,
2428 symtab->cgraph_count);
2429 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2430 int order_pos, tp_first_run_order_pos = 0, new_order_pos = 0;
2431 int i;
2433 order_pos = ipa_reverse_postorder (order);
2434 gcc_assert (order_pos == symtab->cgraph_count);
2436 /* Garbage collector may remove inline clones we eliminate during
2437 optimization. So we must be sure to not reference them. */
2438 for (i = 0; i < order_pos; i++)
2439 if (order[i]->process)
2441 if (order[i]->tp_first_run
2442 && opt_for_fn (order[i]->decl, flag_profile_reorder_functions))
2443 tp_first_run_order[tp_first_run_order_pos++] = order[i];
2444 else
2445 order[new_order_pos++] = order[i];
2448 /* First output functions with time profile in specified order. */
2449 qsort (tp_first_run_order, tp_first_run_order_pos,
2450 sizeof (cgraph_node *), tp_first_run_node_cmp);
2451 for (i = 0; i < tp_first_run_order_pos; i++)
2453 node = tp_first_run_order[i];
2455 if (node->process)
2457 expanded_func_count++;
2458 profiled_func_count++;
2460 if (symtab->dump_file)
2461 fprintf (symtab->dump_file,
2462 "Time profile order in expand_all_functions:%s:%d\n",
2463 node->dump_asm_name (), node->tp_first_run);
2464 node->process = 0;
2465 node->expand ();
2469 /* Output functions in RPO so callees get optimized before callers. This
2470 makes ipa-ra and other propagators to work.
2471 FIXME: This is far from optimal code layout. */
2472 for (i = new_order_pos - 1; i >= 0; i--)
2474 node = order[i];
2476 if (node->process)
2478 expanded_func_count++;
2479 node->process = 0;
2480 node->expand ();
2484 if (dump_file)
2485 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2486 main_input_filename, profiled_func_count, expanded_func_count);
2488 if (symtab->dump_file && tp_first_run_order_pos)
2489 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2490 profiled_func_count, expanded_func_count);
2492 symtab->process_new_functions ();
2493 free_gimplify_stack ();
2494 delete ipa_saved_clone_sources;
2495 ipa_saved_clone_sources = NULL;
2496 free (order);
2499 /* This is used to sort the node types by the cgraph order number. */
2501 enum cgraph_order_sort_kind
2503 ORDER_FUNCTION,
2504 ORDER_VAR,
2505 ORDER_VAR_UNDEF,
2506 ORDER_ASM
2509 struct cgraph_order_sort
2511 /* Construct from a cgraph_node. */
2512 cgraph_order_sort (cgraph_node *node)
2513 : kind (ORDER_FUNCTION), order (node->order)
2515 u.f = node;
2518 /* Construct from a varpool_node. */
2519 cgraph_order_sort (varpool_node *node)
2520 : kind (node->definition ? ORDER_VAR : ORDER_VAR_UNDEF), order (node->order)
2522 u.v = node;
2525 /* Construct from a asm_node. */
2526 cgraph_order_sort (asm_node *node)
2527 : kind (ORDER_ASM), order (node->order)
2529 u.a = node;
2532 /* Assembly cgraph_order_sort based on its type. */
2533 void process ();
2535 enum cgraph_order_sort_kind kind;
2536 union
2538 cgraph_node *f;
2539 varpool_node *v;
2540 asm_node *a;
2541 } u;
2542 int order;
2545 /* Assembly cgraph_order_sort based on its type. */
2547 void
2548 cgraph_order_sort::process ()
2550 switch (kind)
2552 case ORDER_FUNCTION:
2553 u.f->process = 0;
2554 u.f->expand ();
2555 break;
2556 case ORDER_VAR:
2557 u.v->assemble_decl ();
2558 break;
2559 case ORDER_VAR_UNDEF:
2560 assemble_undefined_decl (u.v->decl);
2561 break;
2562 case ORDER_ASM:
2563 assemble_asm (u.a->asm_str);
2564 break;
2565 default:
2566 gcc_unreachable ();
2570 /* Compare cgraph_order_sort by order. */
2572 static int
2573 cgraph_order_cmp (const void *a_p, const void *b_p)
2575 const cgraph_order_sort *nodea = (const cgraph_order_sort *)a_p;
2576 const cgraph_order_sort *nodeb = (const cgraph_order_sort *)b_p;
2578 return nodea->order - nodeb->order;
2581 /* Output all functions, variables, and asm statements in the order
2582 according to their order fields, which is the order in which they
2583 appeared in the file. This implements -fno-toplevel-reorder. In
2584 this mode we may output functions and variables which don't really
2585 need to be output. */
2587 static void
2588 output_in_order (void)
2590 int i;
2591 cgraph_node *cnode;
2592 varpool_node *vnode;
2593 asm_node *anode;
2594 auto_vec<cgraph_order_sort> nodes;
2595 cgraph_order_sort *node;
2597 FOR_EACH_DEFINED_FUNCTION (cnode)
2598 if (cnode->process && !cnode->thunk.thunk_p
2599 && !cnode->alias && cnode->no_reorder)
2600 nodes.safe_push (cgraph_order_sort (cnode));
2602 /* There is a similar loop in symbol_table::output_variables.
2603 Please keep them in sync. */
2604 FOR_EACH_VARIABLE (vnode)
2605 if (vnode->no_reorder
2606 && !DECL_HARD_REGISTER (vnode->decl)
2607 && !DECL_HAS_VALUE_EXPR_P (vnode->decl))
2608 nodes.safe_push (cgraph_order_sort (vnode));
2610 for (anode = symtab->first_asm_symbol (); anode; anode = anode->next)
2611 nodes.safe_push (cgraph_order_sort (anode));
2613 /* Sort nodes by order. */
2614 nodes.qsort (cgraph_order_cmp);
2616 /* In toplevel reorder mode we output all statics; mark them as needed. */
2617 FOR_EACH_VEC_ELT (nodes, i, node)
2618 if (node->kind == ORDER_VAR)
2619 node->u.v->finalize_named_section_flags ();
2621 FOR_EACH_VEC_ELT (nodes, i, node)
2622 node->process ();
2624 symtab->clear_asm_symbols ();
2627 static void
2628 ipa_passes (void)
2630 gcc::pass_manager *passes = g->get_passes ();
2632 set_cfun (NULL);
2633 current_function_decl = NULL;
2634 gimple_register_cfg_hooks ();
2635 bitmap_obstack_initialize (NULL);
2637 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2639 if (!in_lto_p)
2641 execute_ipa_pass_list (passes->all_small_ipa_passes);
2642 if (seen_error ())
2643 return;
2646 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2647 devirtualization and other changes where removal iterate. */
2648 symtab->remove_unreachable_nodes (symtab->dump_file);
2650 /* If pass_all_early_optimizations was not scheduled, the state of
2651 the cgraph will not be properly updated. Update it now. */
2652 if (symtab->state < IPA_SSA)
2653 symtab->state = IPA_SSA;
2655 if (!in_lto_p)
2657 /* Generate coverage variables and constructors. */
2658 coverage_finish ();
2660 /* Process new functions added. */
2661 set_cfun (NULL);
2662 current_function_decl = NULL;
2663 symtab->process_new_functions ();
2665 execute_ipa_summary_passes
2666 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2669 /* Some targets need to handle LTO assembler output specially. */
2670 if (flag_generate_lto || flag_generate_offload)
2671 targetm.asm_out.lto_start ();
2673 if (!in_lto_p
2674 || flag_incremental_link == INCREMENTAL_LINK_LTO)
2676 if (!quiet_flag)
2677 fprintf (stderr, "Streaming LTO\n");
2678 if (g->have_offload)
2680 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2681 lto_stream_offload_p = true;
2682 ipa_write_summaries ();
2683 lto_stream_offload_p = false;
2685 if (flag_lto)
2687 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2688 lto_stream_offload_p = false;
2689 ipa_write_summaries ();
2693 if (flag_generate_lto || flag_generate_offload)
2694 targetm.asm_out.lto_end ();
2696 if (!flag_ltrans
2697 && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2698 || !flag_lto || flag_fat_lto_objects))
2699 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2700 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2702 bitmap_obstack_release (NULL);
2706 /* Return string alias is alias of. */
2708 static tree
2709 get_alias_symbol (tree decl)
2711 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2712 return get_identifier (TREE_STRING_POINTER
2713 (TREE_VALUE (TREE_VALUE (alias))));
2717 /* Weakrefs may be associated to external decls and thus not output
2718 at expansion time. Emit all necessary aliases. */
2720 void
2721 symbol_table::output_weakrefs (void)
2723 symtab_node *node;
2724 FOR_EACH_SYMBOL (node)
2725 if (node->alias
2726 && !TREE_ASM_WRITTEN (node->decl)
2727 && node->weakref)
2729 tree target;
2731 /* Weakrefs are special by not requiring target definition in current
2732 compilation unit. It is thus bit hard to work out what we want to
2733 alias.
2734 When alias target is defined, we need to fetch it from symtab reference,
2735 otherwise it is pointed to by alias_target. */
2736 if (node->alias_target)
2737 target = (DECL_P (node->alias_target)
2738 ? DECL_ASSEMBLER_NAME (node->alias_target)
2739 : node->alias_target);
2740 else if (node->analyzed)
2741 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2742 else
2744 gcc_unreachable ();
2745 target = get_alias_symbol (node->decl);
2747 do_assemble_alias (node->decl, target);
2751 /* Perform simple optimizations based on callgraph. */
2753 void
2754 symbol_table::compile (void)
2756 if (seen_error ())
2757 return;
2759 symtab_node::checking_verify_symtab_nodes ();
2761 timevar_push (TV_CGRAPHOPT);
2762 if (pre_ipa_mem_report)
2763 dump_memory_report ("Memory consumption before IPA");
2764 if (!quiet_flag)
2765 fprintf (stderr, "Performing interprocedural optimizations\n");
2766 state = IPA;
2768 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2769 if (flag_generate_lto || flag_generate_offload)
2770 lto_streamer_hooks_init ();
2772 /* Don't run the IPA passes if there was any error or sorry messages. */
2773 if (!seen_error ())
2775 timevar_start (TV_CGRAPH_IPA_PASSES);
2776 ipa_passes ();
2777 timevar_stop (TV_CGRAPH_IPA_PASSES);
2779 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2780 if (seen_error ()
2781 || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2782 && flag_lto && !flag_fat_lto_objects))
2784 timevar_pop (TV_CGRAPHOPT);
2785 return;
2788 global_info_ready = true;
2789 if (dump_file)
2791 fprintf (dump_file, "Optimized ");
2792 symtab->dump (dump_file);
2794 if (post_ipa_mem_report)
2795 dump_memory_report ("Memory consumption after IPA");
2796 timevar_pop (TV_CGRAPHOPT);
2798 /* Output everything. */
2799 switch_to_section (text_section);
2800 (*debug_hooks->assembly_start) ();
2801 if (!quiet_flag)
2802 fprintf (stderr, "Assembling functions:\n");
2803 symtab_node::checking_verify_symtab_nodes ();
2805 bitmap_obstack_initialize (NULL);
2806 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2807 bitmap_obstack_release (NULL);
2808 mark_functions_to_output ();
2810 /* When weakref support is missing, we automatically translate all
2811 references to NODE to references to its ultimate alias target.
2812 The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2813 TREE_CHAIN.
2815 Set up this mapping before we output any assembler but once we are sure
2816 that all symbol renaming is done.
2818 FIXME: All this ugliness can go away if we just do renaming at gimple
2819 level by physically rewriting the IL. At the moment we can only redirect
2820 calls, so we need infrastructure for renaming references as well. */
2821 #ifndef ASM_OUTPUT_WEAKREF
2822 symtab_node *node;
2824 FOR_EACH_SYMBOL (node)
2825 if (node->alias
2826 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2828 IDENTIFIER_TRANSPARENT_ALIAS
2829 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2830 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2831 = (node->alias_target ? node->alias_target
2832 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2834 #endif
2836 state = EXPANSION;
2838 /* Output first asm statements and anything ordered. The process
2839 flag is cleared for these nodes, so we skip them later. */
2840 output_in_order ();
2842 timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2843 expand_all_functions ();
2844 timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2846 output_variables ();
2848 process_new_functions ();
2849 state = FINISHED;
2850 output_weakrefs ();
2852 if (dump_file)
2854 fprintf (dump_file, "\nFinal ");
2855 symtab->dump (dump_file);
2857 if (!flag_checking)
2858 return;
2859 symtab_node::verify_symtab_nodes ();
2860 /* Double check that all inline clones are gone and that all
2861 function bodies have been released from memory. */
2862 if (!seen_error ())
2864 cgraph_node *node;
2865 bool error_found = false;
2867 FOR_EACH_DEFINED_FUNCTION (node)
2868 if (node->inlined_to
2869 || gimple_has_body_p (node->decl))
2871 error_found = true;
2872 node->debug ();
2874 if (error_found)
2875 internal_error ("nodes with unreleased memory found");
2879 /* Earlydebug dump file, flags, and number. */
2881 static int debuginfo_early_dump_nr;
2882 static FILE *debuginfo_early_dump_file;
2883 static dump_flags_t debuginfo_early_dump_flags;
2885 /* Debug dump file, flags, and number. */
2887 static int debuginfo_dump_nr;
2888 static FILE *debuginfo_dump_file;
2889 static dump_flags_t debuginfo_dump_flags;
2891 /* Register the debug and earlydebug dump files. */
2893 void
2894 debuginfo_early_init (void)
2896 gcc::dump_manager *dumps = g->get_dumps ();
2897 debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2898 "earlydebug", DK_tree,
2899 OPTGROUP_NONE,
2900 false);
2901 debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2902 "debug", DK_tree,
2903 OPTGROUP_NONE,
2904 false);
2907 /* Initialize the debug and earlydebug dump files. */
2909 void
2910 debuginfo_init (void)
2912 gcc::dump_manager *dumps = g->get_dumps ();
2913 debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2914 debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2915 debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2916 debuginfo_early_dump_flags
2917 = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2920 /* Finalize the debug and earlydebug dump files. */
2922 void
2923 debuginfo_fini (void)
2925 if (debuginfo_dump_file)
2926 dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2927 if (debuginfo_early_dump_file)
2928 dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2931 /* Set dump_file to the debug dump file. */
2933 void
2934 debuginfo_start (void)
2936 set_dump_file (debuginfo_dump_file);
2939 /* Undo setting dump_file to the debug dump file. */
2941 void
2942 debuginfo_stop (void)
2944 set_dump_file (NULL);
2947 /* Set dump_file to the earlydebug dump file. */
2949 void
2950 debuginfo_early_start (void)
2952 set_dump_file (debuginfo_early_dump_file);
2955 /* Undo setting dump_file to the earlydebug dump file. */
2957 void
2958 debuginfo_early_stop (void)
2960 set_dump_file (NULL);
2963 /* Analyze the whole compilation unit once it is parsed completely. */
2965 void
2966 symbol_table::finalize_compilation_unit (void)
2968 timevar_push (TV_CGRAPH);
2970 /* If we're here there's no current function anymore. Some frontends
2971 are lazy in clearing these. */
2972 current_function_decl = NULL;
2973 set_cfun (NULL);
2975 /* Do not skip analyzing the functions if there were errors, we
2976 miss diagnostics for following functions otherwise. */
2978 /* Emit size functions we didn't inline. */
2979 finalize_size_functions ();
2981 /* Mark alias targets necessary and emit diagnostics. */
2982 handle_alias_pairs ();
2984 if (!quiet_flag)
2986 fprintf (stderr, "\nAnalyzing compilation unit\n");
2987 fflush (stderr);
2990 if (flag_dump_passes)
2991 dump_passes ();
2993 /* Gimplify and lower all functions, compute reachability and
2994 remove unreachable nodes. */
2995 analyze_functions (/*first_time=*/true);
2997 /* Mark alias targets necessary and emit diagnostics. */
2998 handle_alias_pairs ();
3000 /* Gimplify and lower thunks. */
3001 analyze_functions (/*first_time=*/false);
3003 /* Offloading requires LTO infrastructure. */
3004 if (!in_lto_p && g->have_offload)
3005 flag_generate_offload = 1;
3007 if (!seen_error ())
3009 /* Give the frontends the chance to emit early debug based on
3010 what is still reachable in the TU. */
3011 (*lang_hooks.finalize_early_debug) ();
3013 /* Clean up anything that needs cleaning up after initial debug
3014 generation. */
3015 debuginfo_early_start ();
3016 (*debug_hooks->early_finish) (main_input_filename);
3017 debuginfo_early_stop ();
3020 /* Finally drive the pass manager. */
3021 compile ();
3023 timevar_pop (TV_CGRAPH);
3026 /* Reset all state within cgraphunit.c so that we can rerun the compiler
3027 within the same process. For use by toplev::finalize. */
3029 void
3030 cgraphunit_c_finalize (void)
3032 gcc_assert (cgraph_new_nodes.length () == 0);
3033 cgraph_new_nodes.truncate (0);
3035 vtable_entry_type = NULL;
3036 queued_nodes = &symtab_terminator;
3038 first_analyzed = NULL;
3039 first_analyzed_var = NULL;
3042 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
3043 kind of wrapper method. */
3045 void
3046 cgraph_node::create_wrapper (cgraph_node *target)
3048 /* Preserve DECL_RESULT so we get right by reference flag. */
3049 tree decl_result = DECL_RESULT (decl);
3051 /* Remove the function's body but keep arguments to be reused
3052 for thunk. */
3053 release_body (true);
3054 reset ();
3056 DECL_UNINLINABLE (decl) = false;
3057 DECL_RESULT (decl) = decl_result;
3058 DECL_INITIAL (decl) = NULL;
3059 allocate_struct_function (decl, false);
3060 set_cfun (NULL);
3062 /* Turn alias into thunk and expand it into GIMPLE representation. */
3063 definition = true;
3065 memset (&thunk, 0, sizeof (cgraph_thunk_info));
3066 thunk.thunk_p = true;
3067 create_edge (target, NULL, count);
3068 callees->can_throw_external = !TREE_NOTHROW (target->decl);
3070 tree arguments = DECL_ARGUMENTS (decl);
3072 while (arguments)
3074 TREE_ADDRESSABLE (arguments) = false;
3075 arguments = TREE_CHAIN (arguments);
3078 expand_thunk (false, true);
3080 /* Inline summary set-up. */
3081 analyze ();
3082 inline_analyze_function (this);
3085 #include "gt-cgraphunit.h"