typeck.c (cp_truthvalue_conversion): Add tsubst_flags_t parameter and use it in calls...
[official-gcc.git] / gcc / cgraphunit.c
blobaa26160bf3ff14aabd80ae8a0174c6c6586dd682
1 /* Driver of optimization process
2 Copyright (C) 2003-2019 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"
209 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
210 secondary queue used during optimization to accommodate passes that
211 may generate new functions that need to be optimized and expanded. */
212 vec<cgraph_node *> cgraph_new_nodes;
214 static void expand_all_functions (void);
215 static void mark_functions_to_output (void);
216 static void handle_alias_pairs (void);
218 /* Used for vtable lookup in thunk adjusting. */
219 static GTY (()) tree vtable_entry_type;
221 /* Return true if this symbol is a function from the C frontend specified
222 directly in RTL form (with "__RTL"). */
224 bool
225 symtab_node::native_rtl_p () const
227 if (TREE_CODE (decl) != FUNCTION_DECL)
228 return false;
229 if (!DECL_STRUCT_FUNCTION (decl))
230 return false;
231 return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
234 /* Determine if symbol declaration is needed. That is, visible to something
235 either outside this translation unit, something magic in the system
236 configury */
237 bool
238 symtab_node::needed_p (void)
240 /* Double check that no one output the function into assembly file
241 early. */
242 if (!native_rtl_p ())
243 gcc_checking_assert
244 (!DECL_ASSEMBLER_NAME_SET_P (decl)
245 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
247 if (!definition)
248 return false;
250 if (DECL_EXTERNAL (decl))
251 return false;
253 /* If the user told us it is used, then it must be so. */
254 if (force_output)
255 return true;
257 /* ABI forced symbols are needed when they are external. */
258 if (forced_by_abi && TREE_PUBLIC (decl))
259 return true;
261 /* Keep constructors, destructors and virtual functions. */
262 if (TREE_CODE (decl) == FUNCTION_DECL
263 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
264 return true;
266 /* Externally visible variables must be output. The exception is
267 COMDAT variables that must be output only when they are needed. */
268 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
269 return true;
271 return false;
274 /* Head and terminator of the queue of nodes to be processed while building
275 callgraph. */
277 static symtab_node symtab_terminator;
278 static symtab_node *queued_nodes = &symtab_terminator;
280 /* Add NODE to queue starting at QUEUED_NODES.
281 The queue is linked via AUX pointers and terminated by pointer to 1. */
283 static void
284 enqueue_node (symtab_node *node)
286 if (node->aux)
287 return;
288 gcc_checking_assert (queued_nodes);
289 node->aux = queued_nodes;
290 queued_nodes = node;
293 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
294 functions into callgraph in a way so they look like ordinary reachable
295 functions inserted into callgraph already at construction time. */
297 void
298 symbol_table::process_new_functions (void)
300 tree fndecl;
302 if (!cgraph_new_nodes.exists ())
303 return;
305 handle_alias_pairs ();
306 /* Note that this queue may grow as its being processed, as the new
307 functions may generate new ones. */
308 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
310 cgraph_node *node = cgraph_new_nodes[i];
311 fndecl = node->decl;
312 switch (state)
314 case CONSTRUCTION:
315 /* At construction time we just need to finalize function and move
316 it into reachable functions list. */
318 cgraph_node::finalize_function (fndecl, false);
319 call_cgraph_insertion_hooks (node);
320 enqueue_node (node);
321 break;
323 case IPA:
324 case IPA_SSA:
325 case IPA_SSA_AFTER_INLINING:
326 /* When IPA optimization already started, do all essential
327 transformations that has been already performed on the whole
328 cgraph but not on this function. */
330 gimple_register_cfg_hooks ();
331 if (!node->analyzed)
332 node->analyze ();
333 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
334 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
335 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
337 bool summaried_computed = ipa_fn_summaries != NULL;
338 g->get_passes ()->execute_early_local_passes ();
339 /* Early passes compute inline parameters to do inlining
340 and splitting. This is redundant for functions added late.
341 Just throw away whatever it did. */
342 if (!summaried_computed)
344 ipa_free_fn_summary ();
345 ipa_free_size_summary ();
348 else if (ipa_fn_summaries != NULL)
349 compute_fn_summary (node, true);
350 free_dominance_info (CDI_POST_DOMINATORS);
351 free_dominance_info (CDI_DOMINATORS);
352 pop_cfun ();
353 call_cgraph_insertion_hooks (node);
354 break;
356 case EXPANSION:
357 /* Functions created during expansion shall be compiled
358 directly. */
359 node->process = 0;
360 call_cgraph_insertion_hooks (node);
361 node->expand ();
362 break;
364 default:
365 gcc_unreachable ();
366 break;
370 cgraph_new_nodes.release ();
373 /* As an GCC extension we allow redefinition of the function. The
374 semantics when both copies of bodies differ is not well defined.
375 We replace the old body with new body so in unit at a time mode
376 we always use new body, while in normal mode we may end up with
377 old body inlined into some functions and new body expanded and
378 inlined in others.
380 ??? It may make more sense to use one body for inlining and other
381 body for expanding the function but this is difficult to do. */
383 void
384 cgraph_node::reset (void)
386 /* If process is set, then we have already begun whole-unit analysis.
387 This is *not* testing for whether we've already emitted the function.
388 That case can be sort-of legitimately seen with real function redefinition
389 errors. I would argue that the front end should never present us with
390 such a case, but don't enforce that for now. */
391 gcc_assert (!process);
393 /* Reset our data structures so we can analyze the function again. */
394 inlined_to = NULL;
395 memset (&rtl, 0, sizeof (rtl));
396 analyzed = false;
397 definition = false;
398 alias = false;
399 transparent_alias = false;
400 weakref = false;
401 cpp_implicit_alias = false;
403 remove_callees ();
404 remove_all_references ();
407 /* Return true when there are references to the node. INCLUDE_SELF is
408 true if a self reference counts as a reference. */
410 bool
411 symtab_node::referred_to_p (bool include_self)
413 ipa_ref *ref = NULL;
415 /* See if there are any references at all. */
416 if (iterate_referring (0, ref))
417 return true;
418 /* For functions check also calls. */
419 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
420 if (cn && cn->callers)
422 if (include_self)
423 return true;
424 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
425 if (e->caller != this)
426 return true;
428 return false;
431 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
432 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
433 the garbage collector run at the moment. We would need to either create
434 a new GC context, or just not compile right now. */
436 void
437 cgraph_node::finalize_function (tree decl, bool no_collect)
439 cgraph_node *node = cgraph_node::get_create (decl);
441 if (node->definition)
443 /* Nested functions should only be defined once. */
444 gcc_assert (!DECL_CONTEXT (decl)
445 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
446 node->reset ();
447 node->redefined_extern_inline = true;
450 /* Set definition first before calling notice_global_symbol so that
451 it is available to notice_global_symbol. */
452 node->definition = true;
453 notice_global_symbol (decl);
454 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
455 if (!flag_toplevel_reorder)
456 node->no_reorder = true;
458 /* With -fkeep-inline-functions we are keeping all inline functions except
459 for extern inline ones. */
460 if (flag_keep_inline_functions
461 && DECL_DECLARED_INLINE_P (decl)
462 && !DECL_EXTERNAL (decl)
463 && !DECL_DISREGARD_INLINE_LIMITS (decl))
464 node->force_output = 1;
466 /* __RTL functions were already output as soon as they were parsed (due
467 to the large amount of global state in the backend).
468 Mark such functions as "force_output" to reflect the fact that they
469 will be in the asm file when considering the symbols they reference.
470 The attempt to output them later on will bail out immediately. */
471 if (node->native_rtl_p ())
472 node->force_output = 1;
474 /* When not optimizing, also output the static functions. (see
475 PR24561), but don't do so for always_inline functions, functions
476 declared inline and nested functions. These were optimized out
477 in the original implementation and it is unclear whether we want
478 to change the behavior here. */
479 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
480 || node->no_reorder)
481 && !node->cpp_implicit_alias
482 && !DECL_DISREGARD_INLINE_LIMITS (decl)
483 && !DECL_DECLARED_INLINE_P (decl)
484 && !(DECL_CONTEXT (decl)
485 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
486 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
487 node->force_output = 1;
489 /* If we've not yet emitted decl, tell the debug info about it. */
490 if (!TREE_ASM_WRITTEN (decl))
491 (*debug_hooks->deferred_inline_function) (decl);
493 if (!no_collect)
494 ggc_collect ();
496 if (symtab->state == CONSTRUCTION
497 && (node->needed_p () || node->referred_to_p ()))
498 enqueue_node (node);
501 /* Add the function FNDECL to the call graph.
502 Unlike finalize_function, this function is intended to be used
503 by middle end and allows insertion of new function at arbitrary point
504 of compilation. The function can be either in high, low or SSA form
505 GIMPLE.
507 The function is assumed to be reachable and have address taken (so no
508 API breaking optimizations are performed on it).
510 Main work done by this function is to enqueue the function for later
511 processing to avoid need the passes to be re-entrant. */
513 void
514 cgraph_node::add_new_function (tree fndecl, bool lowered)
516 gcc::pass_manager *passes = g->get_passes ();
517 cgraph_node *node;
519 if (dump_file)
521 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
522 const char *function_type = ((gimple_has_body_p (fndecl))
523 ? (lowered
524 ? (gimple_in_ssa_p (fn)
525 ? "ssa gimple"
526 : "low gimple")
527 : "high gimple")
528 : "to-be-gimplified");
529 fprintf (dump_file,
530 "Added new %s function %s to callgraph\n",
531 function_type,
532 fndecl_name (fndecl));
535 switch (symtab->state)
537 case PARSING:
538 cgraph_node::finalize_function (fndecl, false);
539 break;
540 case CONSTRUCTION:
541 /* Just enqueue function to be processed at nearest occurrence. */
542 node = cgraph_node::get_create (fndecl);
543 if (lowered)
544 node->lowered = true;
545 cgraph_new_nodes.safe_push (node);
546 break;
548 case IPA:
549 case IPA_SSA:
550 case IPA_SSA_AFTER_INLINING:
551 case EXPANSION:
552 /* Bring the function into finalized state and enqueue for later
553 analyzing and compilation. */
554 node = cgraph_node::get_create (fndecl);
555 node->local = false;
556 node->definition = true;
557 node->force_output = true;
558 if (TREE_PUBLIC (fndecl))
559 node->externally_visible = true;
560 if (!lowered && symtab->state == EXPANSION)
562 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
563 gimple_register_cfg_hooks ();
564 bitmap_obstack_initialize (NULL);
565 execute_pass_list (cfun, passes->all_lowering_passes);
566 passes->execute_early_local_passes ();
567 bitmap_obstack_release (NULL);
568 pop_cfun ();
570 lowered = true;
572 if (lowered)
573 node->lowered = true;
574 cgraph_new_nodes.safe_push (node);
575 break;
577 case FINISHED:
578 /* At the very end of compilation we have to do all the work up
579 to expansion. */
580 node = cgraph_node::create (fndecl);
581 if (lowered)
582 node->lowered = true;
583 node->definition = true;
584 node->analyze ();
585 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
586 gimple_register_cfg_hooks ();
587 bitmap_obstack_initialize (NULL);
588 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
589 g->get_passes ()->execute_early_local_passes ();
590 bitmap_obstack_release (NULL);
591 pop_cfun ();
592 node->expand ();
593 break;
595 default:
596 gcc_unreachable ();
599 /* Set a personality if required and we already passed EH lowering. */
600 if (lowered
601 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
602 == eh_personality_lang))
603 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
606 /* Analyze the function scheduled to be output. */
607 void
608 cgraph_node::analyze (void)
610 if (native_rtl_p ())
612 analyzed = true;
613 return;
616 tree decl = this->decl;
617 location_t saved_loc = input_location;
618 input_location = DECL_SOURCE_LOCATION (decl);
620 if (thunk.thunk_p)
622 cgraph_node *t = cgraph_node::get (thunk.alias);
624 create_edge (t, NULL, t->count);
625 callees->can_throw_external = !TREE_NOTHROW (t->decl);
626 /* Target code in expand_thunk may need the thunk's target
627 to be analyzed, so recurse here. */
628 if (!t->analyzed && t->definition)
629 t->analyze ();
630 if (t->alias)
632 t = t->get_alias_target ();
633 if (!t->analyzed && t->definition)
634 t->analyze ();
636 bool ret = expand_thunk (false, false);
637 thunk.alias = NULL;
638 if (!ret)
639 return;
641 if (alias)
642 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
643 else if (dispatcher_function)
645 /* Generate the dispatcher body of multi-versioned functions. */
646 cgraph_function_version_info *dispatcher_version_info
647 = function_version ();
648 if (dispatcher_version_info != NULL
649 && (dispatcher_version_info->dispatcher_resolver
650 == NULL_TREE))
652 tree resolver = NULL_TREE;
653 gcc_assert (targetm.generate_version_dispatcher_body);
654 resolver = targetm.generate_version_dispatcher_body (this);
655 gcc_assert (resolver != NULL_TREE);
658 else
660 push_cfun (DECL_STRUCT_FUNCTION (decl));
662 assign_assembler_name_if_needed (decl);
664 /* Make sure to gimplify bodies only once. During analyzing a
665 function we lower it, which will require gimplified nested
666 functions, so we can end up here with an already gimplified
667 body. */
668 if (!gimple_has_body_p (decl))
669 gimplify_function_tree (decl);
671 /* Lower the function. */
672 if (!lowered)
674 if (nested)
675 lower_nested_functions (decl);
676 gcc_assert (!nested);
678 gimple_register_cfg_hooks ();
679 bitmap_obstack_initialize (NULL);
680 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
681 free_dominance_info (CDI_POST_DOMINATORS);
682 free_dominance_info (CDI_DOMINATORS);
683 compact_blocks ();
684 bitmap_obstack_release (NULL);
685 lowered = true;
688 pop_cfun ();
690 analyzed = true;
692 input_location = saved_loc;
695 /* C++ frontend produce same body aliases all over the place, even before PCH
696 gets streamed out. It relies on us linking the aliases with their function
697 in order to do the fixups, but ipa-ref is not PCH safe. Consequently we
698 first produce aliases without links, but once C++ FE is sure he won't stream
699 PCH we build the links via this function. */
701 void
702 symbol_table::process_same_body_aliases (void)
704 symtab_node *node;
705 FOR_EACH_SYMBOL (node)
706 if (node->cpp_implicit_alias && !node->analyzed)
707 node->resolve_alias
708 (VAR_P (node->alias_target)
709 ? (symtab_node *)varpool_node::get_create (node->alias_target)
710 : (symtab_node *)cgraph_node::get_create (node->alias_target));
711 cpp_implicit_aliases_done = true;
714 /* Process attributes common for vars and functions. */
716 static void
717 process_common_attributes (symtab_node *node, tree decl)
719 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
721 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
723 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
724 "%<weakref%> attribute should be accompanied with"
725 " an %<alias%> attribute");
726 DECL_WEAK (decl) = 0;
727 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
728 DECL_ATTRIBUTES (decl));
731 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
732 node->no_reorder = 1;
735 /* Look for externally_visible and used attributes and mark cgraph nodes
736 accordingly.
738 We cannot mark the nodes at the point the attributes are processed (in
739 handle_*_attribute) because the copy of the declarations available at that
740 point may not be canonical. For example, in:
742 void f();
743 void f() __attribute__((used));
745 the declaration we see in handle_used_attribute will be the second
746 declaration -- but the front end will subsequently merge that declaration
747 with the original declaration and discard the second declaration.
749 Furthermore, we can't mark these nodes in finalize_function because:
751 void f() {}
752 void f() __attribute__((externally_visible));
754 is valid.
756 So, we walk the nodes at the end of the translation unit, applying the
757 attributes at that point. */
759 static void
760 process_function_and_variable_attributes (cgraph_node *first,
761 varpool_node *first_var)
763 cgraph_node *node;
764 varpool_node *vnode;
766 for (node = symtab->first_function (); node != first;
767 node = symtab->next_function (node))
769 tree decl = node->decl;
770 if (DECL_PRESERVE_P (decl))
771 node->mark_force_output ();
772 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
774 if (! TREE_PUBLIC (node->decl))
775 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
776 "%<externally_visible%>"
777 " attribute have effect only on public objects");
779 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
780 && (node->definition && !node->alias))
782 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
783 "%<weakref%> attribute ignored"
784 " because function is defined");
785 DECL_WEAK (decl) = 0;
786 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
787 DECL_ATTRIBUTES (decl));
789 else if (lookup_attribute ("alias", DECL_ATTRIBUTES (decl))
790 && node->definition
791 && !node->alias)
792 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
793 "%<alias%> attribute ignored"
794 " because function is defined");
796 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
797 && !DECL_DECLARED_INLINE_P (decl)
798 /* redefining extern inline function makes it DECL_UNINLINABLE. */
799 && !DECL_UNINLINABLE (decl))
800 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
801 "%<always_inline%> function might not be inlinable");
803 process_common_attributes (node, decl);
805 for (vnode = symtab->first_variable (); vnode != first_var;
806 vnode = symtab->next_variable (vnode))
808 tree decl = vnode->decl;
809 if (DECL_EXTERNAL (decl)
810 && DECL_INITIAL (decl))
811 varpool_node::finalize_decl (decl);
812 if (DECL_PRESERVE_P (decl))
813 vnode->force_output = true;
814 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
816 if (! TREE_PUBLIC (vnode->decl))
817 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
818 "%<externally_visible%>"
819 " attribute have effect only on public objects");
821 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
822 && vnode->definition
823 && DECL_INITIAL (decl))
825 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
826 "%<weakref%> attribute ignored"
827 " because variable is initialized");
828 DECL_WEAK (decl) = 0;
829 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
830 DECL_ATTRIBUTES (decl));
832 process_common_attributes (vnode, decl);
836 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
837 middle end to output the variable to asm file, if needed or externally
838 visible. */
840 void
841 varpool_node::finalize_decl (tree decl)
843 varpool_node *node = varpool_node::get_create (decl);
845 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
847 if (node->definition)
848 return;
849 /* Set definition first before calling notice_global_symbol so that
850 it is available to notice_global_symbol. */
851 node->definition = true;
852 notice_global_symbol (decl);
853 if (!flag_toplevel_reorder)
854 node->no_reorder = true;
855 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
856 /* Traditionally we do not eliminate static variables when not
857 optimizing and when not doing toplevel reorder. */
858 || (node->no_reorder && !DECL_COMDAT (node->decl)
859 && !DECL_ARTIFICIAL (node->decl)))
860 node->force_output = true;
862 if (symtab->state == CONSTRUCTION
863 && (node->needed_p () || node->referred_to_p ()))
864 enqueue_node (node);
865 if (symtab->state >= IPA_SSA)
866 node->analyze ();
867 /* Some frontends produce various interface variables after compilation
868 finished. */
869 if (symtab->state == FINISHED
870 || (node->no_reorder
871 && symtab->state == EXPANSION))
872 node->assemble_decl ();
875 /* EDGE is an polymorphic call. Mark all possible targets as reachable
876 and if there is only one target, perform trivial devirtualization.
877 REACHABLE_CALL_TARGETS collects target lists we already walked to
878 avoid duplicate work. */
880 static void
881 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
882 cgraph_edge *edge)
884 unsigned int i;
885 void *cache_token;
886 bool final;
887 vec <cgraph_node *>targets
888 = possible_polymorphic_call_targets
889 (edge, &final, &cache_token);
891 if (!reachable_call_targets->add (cache_token))
893 if (symtab->dump_file)
894 dump_possible_polymorphic_call_targets
895 (symtab->dump_file, edge);
897 for (i = 0; i < targets.length (); i++)
899 /* Do not bother to mark virtual methods in anonymous namespace;
900 either we will find use of virtual table defining it, or it is
901 unused. */
902 if (targets[i]->definition
903 && TREE_CODE
904 (TREE_TYPE (targets[i]->decl))
905 == METHOD_TYPE
906 && !type_in_anonymous_namespace_p
907 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
908 enqueue_node (targets[i]);
912 /* Very trivial devirtualization; when the type is
913 final or anonymous (so we know all its derivation)
914 and there is only one possible virtual call target,
915 make the edge direct. */
916 if (final)
918 if (targets.length () <= 1 && dbg_cnt (devirt))
920 cgraph_node *target;
921 if (targets.length () == 1)
922 target = targets[0];
923 else
924 target = cgraph_node::create
925 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
927 if (symtab->dump_file)
929 fprintf (symtab->dump_file,
930 "Devirtualizing call: ");
931 print_gimple_stmt (symtab->dump_file,
932 edge->call_stmt, 0,
933 TDF_SLIM);
935 if (dump_enabled_p ())
937 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, edge->call_stmt,
938 "devirtualizing call in %s to %s\n",
939 edge->caller->name (), target->name ());
942 edge->make_direct (target);
943 edge->redirect_call_stmt_to_callee ();
945 if (symtab->dump_file)
947 fprintf (symtab->dump_file,
948 "Devirtualized as: ");
949 print_gimple_stmt (symtab->dump_file,
950 edge->call_stmt, 0,
951 TDF_SLIM);
957 /* Issue appropriate warnings for the global declaration DECL. */
959 static void
960 check_global_declaration (symtab_node *snode)
962 const char *decl_file;
963 tree decl = snode->decl;
965 /* Warn about any function declared static but not defined. We don't
966 warn about variables, because many programs have static variables
967 that exist only to get some text into the object file. */
968 if (TREE_CODE (decl) == FUNCTION_DECL
969 && DECL_INITIAL (decl) == 0
970 && DECL_EXTERNAL (decl)
971 && ! DECL_ARTIFICIAL (decl)
972 && ! TREE_NO_WARNING (decl)
973 && ! TREE_PUBLIC (decl)
974 && (warn_unused_function
975 || snode->referred_to_p (/*include_self=*/false)))
977 if (snode->referred_to_p (/*include_self=*/false))
978 pedwarn (input_location, 0, "%q+F used but never defined", decl);
979 else
980 warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
981 /* This symbol is effectively an "extern" declaration now. */
982 TREE_PUBLIC (decl) = 1;
985 /* Warn about static fns or vars defined but not used. */
986 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
987 || (((warn_unused_variable && ! TREE_READONLY (decl))
988 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
989 && (warn_unused_const_variable == 2
990 || (main_input_filename != NULL
991 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
992 && filename_cmp (main_input_filename,
993 decl_file) == 0))))
994 && VAR_P (decl)))
995 && ! DECL_IN_SYSTEM_HEADER (decl)
996 && ! snode->referred_to_p (/*include_self=*/false)
997 /* This TREE_USED check is needed in addition to referred_to_p
998 above, because the `__unused__' attribute is not being
999 considered for referred_to_p. */
1000 && ! TREE_USED (decl)
1001 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1002 to handle multiple external decls in different scopes. */
1003 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1004 && ! DECL_EXTERNAL (decl)
1005 && ! DECL_ARTIFICIAL (decl)
1006 && ! DECL_ABSTRACT_ORIGIN (decl)
1007 && ! TREE_PUBLIC (decl)
1008 /* A volatile variable might be used in some non-obvious way. */
1009 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1010 /* Global register variables must be declared to reserve them. */
1011 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1012 /* Global ctors and dtors are called by the runtime. */
1013 && (TREE_CODE (decl) != FUNCTION_DECL
1014 || (!DECL_STATIC_CONSTRUCTOR (decl)
1015 && !DECL_STATIC_DESTRUCTOR (decl)))
1016 /* Otherwise, ask the language. */
1017 && lang_hooks.decls.warn_unused_global (decl))
1018 warning_at (DECL_SOURCE_LOCATION (decl),
1019 (TREE_CODE (decl) == FUNCTION_DECL)
1020 ? OPT_Wunused_function
1021 : (TREE_READONLY (decl)
1022 ? OPT_Wunused_const_variable_
1023 : OPT_Wunused_variable),
1024 "%qD defined but not used", decl);
1027 /* Discover all functions and variables that are trivially needed, analyze
1028 them as well as all functions and variables referred by them */
1029 static cgraph_node *first_analyzed;
1030 static varpool_node *first_analyzed_var;
1032 /* FIRST_TIME is set to TRUE for the first time we are called for a
1033 translation unit from finalize_compilation_unit() or false
1034 otherwise. */
1036 static void
1037 analyze_functions (bool first_time)
1039 /* Keep track of already processed nodes when called multiple times for
1040 intermodule optimization. */
1041 cgraph_node *first_handled = first_analyzed;
1042 varpool_node *first_handled_var = first_analyzed_var;
1043 hash_set<void *> reachable_call_targets;
1045 symtab_node *node;
1046 symtab_node *next;
1047 int i;
1048 ipa_ref *ref;
1049 bool changed = true;
1050 location_t saved_loc = input_location;
1052 bitmap_obstack_initialize (NULL);
1053 symtab->state = CONSTRUCTION;
1054 input_location = UNKNOWN_LOCATION;
1056 /* Ugly, but the fixup cannot happen at a time same body alias is created;
1057 C++ FE is confused about the COMDAT groups being right. */
1058 if (symtab->cpp_implicit_aliases_done)
1059 FOR_EACH_SYMBOL (node)
1060 if (node->cpp_implicit_alias)
1061 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1062 build_type_inheritance_graph ();
1064 /* Analysis adds static variables that in turn adds references to new functions.
1065 So we need to iterate the process until it stabilize. */
1066 while (changed)
1068 changed = false;
1069 process_function_and_variable_attributes (first_analyzed,
1070 first_analyzed_var);
1072 /* First identify the trivially needed symbols. */
1073 for (node = symtab->first_symbol ();
1074 node != first_analyzed
1075 && node != first_analyzed_var; node = node->next)
1077 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1078 node->get_comdat_group_id ();
1079 if (node->needed_p ())
1081 enqueue_node (node);
1082 if (!changed && symtab->dump_file)
1083 fprintf (symtab->dump_file, "Trivially needed symbols:");
1084 changed = true;
1085 if (symtab->dump_file)
1086 fprintf (symtab->dump_file, " %s", node->asm_name ());
1087 if (!changed && symtab->dump_file)
1088 fprintf (symtab->dump_file, "\n");
1090 if (node == first_analyzed
1091 || node == first_analyzed_var)
1092 break;
1094 symtab->process_new_functions ();
1095 first_analyzed_var = symtab->first_variable ();
1096 first_analyzed = symtab->first_function ();
1098 if (changed && symtab->dump_file)
1099 fprintf (symtab->dump_file, "\n");
1101 /* Lower representation, build callgraph edges and references for all trivially
1102 needed symbols and all symbols referred by them. */
1103 while (queued_nodes != &symtab_terminator)
1105 changed = true;
1106 node = queued_nodes;
1107 queued_nodes = (symtab_node *)queued_nodes->aux;
1108 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1109 if (cnode && cnode->definition)
1111 cgraph_edge *edge;
1112 tree decl = cnode->decl;
1114 /* ??? It is possible to create extern inline function
1115 and later using weak alias attribute to kill its body.
1116 See gcc.c-torture/compile/20011119-1.c */
1117 if (!DECL_STRUCT_FUNCTION (decl)
1118 && !cnode->alias
1119 && !cnode->thunk.thunk_p
1120 && !cnode->dispatcher_function)
1122 cnode->reset ();
1123 cnode->redefined_extern_inline = true;
1124 continue;
1127 if (!cnode->analyzed)
1128 cnode->analyze ();
1130 for (edge = cnode->callees; edge; edge = edge->next_callee)
1131 if (edge->callee->definition
1132 && (!DECL_EXTERNAL (edge->callee->decl)
1133 /* When not optimizing, do not try to analyze extern
1134 inline functions. Doing so is pointless. */
1135 || opt_for_fn (edge->callee->decl, optimize)
1136 /* Weakrefs needs to be preserved. */
1137 || edge->callee->alias
1138 /* always_inline functions are inlined even at -O0. */
1139 || lookup_attribute
1140 ("always_inline",
1141 DECL_ATTRIBUTES (edge->callee->decl))
1142 /* Multiversioned functions needs the dispatcher to
1143 be produced locally even for extern functions. */
1144 || edge->callee->function_version ()))
1145 enqueue_node (edge->callee);
1146 if (opt_for_fn (cnode->decl, optimize)
1147 && opt_for_fn (cnode->decl, flag_devirtualize))
1149 cgraph_edge *next;
1151 for (edge = cnode->indirect_calls; edge; edge = next)
1153 next = edge->next_callee;
1154 if (edge->indirect_info->polymorphic)
1155 walk_polymorphic_call_targets (&reachable_call_targets,
1156 edge);
1160 /* If decl is a clone of an abstract function,
1161 mark that abstract function so that we don't release its body.
1162 The DECL_INITIAL() of that abstract function declaration
1163 will be later needed to output debug info. */
1164 if (DECL_ABSTRACT_ORIGIN (decl))
1166 cgraph_node *origin_node
1167 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1168 origin_node->used_as_abstract_origin = true;
1170 /* Preserve a functions function context node. It will
1171 later be needed to output debug info. */
1172 if (tree fn = decl_function_context (decl))
1174 cgraph_node *origin_node = cgraph_node::get_create (fn);
1175 enqueue_node (origin_node);
1178 else
1180 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1181 if (vnode && vnode->definition && !vnode->analyzed)
1182 vnode->analyze ();
1185 if (node->same_comdat_group)
1187 symtab_node *next;
1188 for (next = node->same_comdat_group;
1189 next != node;
1190 next = next->same_comdat_group)
1191 if (!next->comdat_local_p ())
1192 enqueue_node (next);
1194 for (i = 0; node->iterate_reference (i, ref); i++)
1195 if (ref->referred->definition
1196 && (!DECL_EXTERNAL (ref->referred->decl)
1197 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1198 && optimize)
1199 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1200 && opt_for_fn (ref->referred->decl, optimize))
1201 || node->alias
1202 || ref->referred->alias)))
1203 enqueue_node (ref->referred);
1204 symtab->process_new_functions ();
1207 update_type_inheritance_graph ();
1209 /* Collect entry points to the unit. */
1210 if (symtab->dump_file)
1212 fprintf (symtab->dump_file, "\n\nInitial ");
1213 symtab->dump (symtab->dump_file);
1216 if (first_time)
1218 symtab_node *snode;
1219 FOR_EACH_SYMBOL (snode)
1220 check_global_declaration (snode);
1223 if (symtab->dump_file)
1224 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1226 for (node = symtab->first_symbol ();
1227 node != first_handled
1228 && node != first_handled_var; node = next)
1230 next = node->next;
1231 /* For symbols declared locally we clear TREE_READONLY when emitting
1232 the constructor (if one is needed). For external declarations we can
1233 not safely assume that the type is readonly because we may be called
1234 during its construction. */
1235 if (TREE_CODE (node->decl) == VAR_DECL
1236 && TYPE_P (TREE_TYPE (node->decl))
1237 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (node->decl))
1238 && DECL_EXTERNAL (node->decl))
1239 TREE_READONLY (node->decl) = 0;
1240 if (!node->aux && !node->referred_to_p ())
1242 if (symtab->dump_file)
1243 fprintf (symtab->dump_file, " %s", node->name ());
1245 /* See if the debugger can use anything before the DECL
1246 passes away. Perhaps it can notice a DECL that is now a
1247 constant and can tag the early DIE with an appropriate
1248 attribute.
1250 Otherwise, this is the last chance the debug_hooks have
1251 at looking at optimized away DECLs, since
1252 late_global_decl will subsequently be called from the
1253 contents of the now pruned symbol table. */
1254 if (VAR_P (node->decl)
1255 && !decl_function_context (node->decl))
1257 /* We are reclaiming totally unreachable code and variables
1258 so they effectively appear as readonly. Show that to
1259 the debug machinery. */
1260 TREE_READONLY (node->decl) = 1;
1261 node->definition = false;
1262 (*debug_hooks->late_global_decl) (node->decl);
1265 node->remove ();
1266 continue;
1268 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1270 tree decl = node->decl;
1272 if (cnode->definition && !gimple_has_body_p (decl)
1273 && !cnode->alias
1274 && !cnode->thunk.thunk_p)
1275 cnode->reset ();
1277 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1278 || cnode->alias
1279 || gimple_has_body_p (decl)
1280 || cnode->native_rtl_p ());
1281 gcc_assert (cnode->analyzed == cnode->definition);
1283 node->aux = NULL;
1285 for (;node; node = node->next)
1286 node->aux = NULL;
1287 first_analyzed = symtab->first_function ();
1288 first_analyzed_var = symtab->first_variable ();
1289 if (symtab->dump_file)
1291 fprintf (symtab->dump_file, "\n\nReclaimed ");
1292 symtab->dump (symtab->dump_file);
1294 bitmap_obstack_release (NULL);
1295 ggc_collect ();
1296 /* Initialize assembler name hash, in particular we want to trigger C++
1297 mangling and same body alias creation before we free DECL_ARGUMENTS
1298 used by it. */
1299 if (!seen_error ())
1300 symtab->symtab_initialize_asm_name_hash ();
1302 input_location = saved_loc;
1305 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1306 (which may be an ifunc resolver) and issue a diagnostic when they are
1307 not compatible according to language rules (plus a C++ extension for
1308 non-static member functions). */
1310 static void
1311 maybe_diag_incompatible_alias (tree alias, tree target)
1313 tree altype = TREE_TYPE (alias);
1314 tree targtype = TREE_TYPE (target);
1316 bool ifunc = cgraph_node::get (alias)->ifunc_resolver;
1317 tree funcptr = altype;
1319 if (ifunc)
1321 /* Handle attribute ifunc first. */
1322 if (TREE_CODE (altype) == METHOD_TYPE)
1324 /* Set FUNCPTR to the type of the alias target. If the type
1325 is a non-static member function of class C, construct a type
1326 of an ordinary function taking C* as the first argument,
1327 followed by the member function argument list, and use it
1328 instead to check for incompatibility. This conversion is
1329 not defined by the language but an extension provided by
1330 G++. */
1332 tree rettype = TREE_TYPE (altype);
1333 tree args = TYPE_ARG_TYPES (altype);
1334 altype = build_function_type (rettype, args);
1335 funcptr = altype;
1338 targtype = TREE_TYPE (targtype);
1340 if (POINTER_TYPE_P (targtype))
1342 targtype = TREE_TYPE (targtype);
1344 /* Only issue Wattribute-alias for conversions to void* with
1345 -Wextra. */
1346 if (VOID_TYPE_P (targtype) && !extra_warnings)
1347 return;
1349 /* Proceed to handle incompatible ifunc resolvers below. */
1351 else
1353 funcptr = build_pointer_type (funcptr);
1355 error_at (DECL_SOURCE_LOCATION (target),
1356 "%<ifunc%> resolver for %qD must return %qT",
1357 alias, funcptr);
1358 inform (DECL_SOURCE_LOCATION (alias),
1359 "resolver indirect function declared here");
1360 return;
1364 if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1365 || (prototype_p (altype)
1366 && prototype_p (targtype)
1367 && !types_compatible_p (altype, targtype))))
1369 /* Warn for incompatibilities. Avoid warning for functions
1370 without a prototype to make it possible to declare aliases
1371 without knowing the exact type, as libstdc++ does. */
1372 if (ifunc)
1374 funcptr = build_pointer_type (funcptr);
1376 auto_diagnostic_group d;
1377 if (warning_at (DECL_SOURCE_LOCATION (target),
1378 OPT_Wattribute_alias_,
1379 "%<ifunc%> resolver for %qD should return %qT",
1380 alias, funcptr))
1381 inform (DECL_SOURCE_LOCATION (alias),
1382 "resolver indirect function declared here");
1384 else
1386 auto_diagnostic_group d;
1387 if (warning_at (DECL_SOURCE_LOCATION (alias),
1388 OPT_Wattribute_alias_,
1389 "%qD alias between functions of incompatible "
1390 "types %qT and %qT", alias, altype, targtype))
1391 inform (DECL_SOURCE_LOCATION (target),
1392 "aliased declaration here");
1397 /* Translate the ugly representation of aliases as alias pairs into nice
1398 representation in callgraph. We don't handle all cases yet,
1399 unfortunately. */
1401 static void
1402 handle_alias_pairs (void)
1404 alias_pair *p;
1405 unsigned i;
1407 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1409 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1411 /* Weakrefs with target not defined in current unit are easy to handle:
1412 they behave just as external variables except we need to note the
1413 alias flag to later output the weakref pseudo op into asm file. */
1414 if (!target_node
1415 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1417 symtab_node *node = symtab_node::get (p->decl);
1418 if (node)
1420 node->alias_target = p->target;
1421 node->weakref = true;
1422 node->alias = true;
1423 node->transparent_alias = true;
1425 alias_pairs->unordered_remove (i);
1426 continue;
1428 else if (!target_node)
1430 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1431 symtab_node *node = symtab_node::get (p->decl);
1432 if (node)
1433 node->alias = false;
1434 alias_pairs->unordered_remove (i);
1435 continue;
1438 if (DECL_EXTERNAL (target_node->decl)
1439 /* We use local aliases for C++ thunks to force the tailcall
1440 to bind locally. This is a hack - to keep it working do
1441 the following (which is not strictly correct). */
1442 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1443 || ! DECL_VIRTUAL_P (target_node->decl))
1444 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1446 error ("%q+D aliased to external symbol %qE",
1447 p->decl, p->target);
1450 if (TREE_CODE (p->decl) == FUNCTION_DECL
1451 && target_node && is_a <cgraph_node *> (target_node))
1453 maybe_diag_incompatible_alias (p->decl, target_node->decl);
1455 maybe_diag_alias_attributes (p->decl, target_node->decl);
1457 cgraph_node *src_node = cgraph_node::get (p->decl);
1458 if (src_node && src_node->definition)
1459 src_node->reset ();
1460 cgraph_node::create_alias (p->decl, target_node->decl);
1461 alias_pairs->unordered_remove (i);
1463 else if (VAR_P (p->decl)
1464 && target_node && is_a <varpool_node *> (target_node))
1466 varpool_node::create_alias (p->decl, target_node->decl);
1467 alias_pairs->unordered_remove (i);
1469 else
1471 error ("%q+D alias between function and variable is not supported",
1472 p->decl);
1473 inform (DECL_SOURCE_LOCATION (target_node->decl),
1474 "aliased declaration here");
1476 alias_pairs->unordered_remove (i);
1479 vec_free (alias_pairs);
1483 /* Figure out what functions we want to assemble. */
1485 static void
1486 mark_functions_to_output (void)
1488 bool check_same_comdat_groups = false;
1489 cgraph_node *node;
1491 if (flag_checking)
1492 FOR_EACH_FUNCTION (node)
1493 gcc_assert (!node->process);
1495 FOR_EACH_FUNCTION (node)
1497 tree decl = node->decl;
1499 gcc_assert (!node->process || node->same_comdat_group);
1500 if (node->process)
1501 continue;
1503 /* We need to output all local functions that are used and not
1504 always inlined, as well as those that are reachable from
1505 outside the current compilation unit. */
1506 if (node->analyzed
1507 && !node->thunk.thunk_p
1508 && !node->alias
1509 && !node->inlined_to
1510 && !TREE_ASM_WRITTEN (decl)
1511 && !DECL_EXTERNAL (decl))
1513 node->process = 1;
1514 if (node->same_comdat_group)
1516 cgraph_node *next;
1517 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1518 next != node;
1519 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1520 if (!next->thunk.thunk_p && !next->alias
1521 && !next->comdat_local_p ())
1522 next->process = 1;
1525 else if (node->same_comdat_group)
1527 if (flag_checking)
1528 check_same_comdat_groups = true;
1530 else
1532 /* We should've reclaimed all functions that are not needed. */
1533 if (flag_checking
1534 && !node->inlined_to
1535 && gimple_has_body_p (decl)
1536 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1537 are inside partition, we can end up not removing the body since we no longer
1538 have analyzed node pointing to it. */
1539 && !node->in_other_partition
1540 && !node->alias
1541 && !node->clones
1542 && !DECL_EXTERNAL (decl))
1544 node->debug ();
1545 internal_error ("failed to reclaim unneeded function");
1547 gcc_assert (node->inlined_to
1548 || !gimple_has_body_p (decl)
1549 || node->in_other_partition
1550 || node->clones
1551 || DECL_ARTIFICIAL (decl)
1552 || DECL_EXTERNAL (decl));
1557 if (flag_checking && check_same_comdat_groups)
1558 FOR_EACH_FUNCTION (node)
1559 if (node->same_comdat_group && !node->process)
1561 tree decl = node->decl;
1562 if (!node->inlined_to
1563 && gimple_has_body_p (decl)
1564 /* FIXME: in an ltrans unit when the offline copy is outside a
1565 partition but inline copies are inside a partition, we can
1566 end up not removing the body since we no longer have an
1567 analyzed node pointing to it. */
1568 && !node->in_other_partition
1569 && !node->clones
1570 && !DECL_EXTERNAL (decl))
1572 node->debug ();
1573 internal_error ("failed to reclaim unneeded function in same "
1574 "comdat group");
1579 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1580 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1582 Set current_function_decl and cfun to newly constructed empty function body.
1583 return basic block in the function body. */
1585 basic_block
1586 init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1588 basic_block bb;
1589 edge e;
1591 current_function_decl = decl;
1592 allocate_struct_function (decl, false);
1593 gimple_register_cfg_hooks ();
1594 init_empty_tree_cfg ();
1595 init_tree_ssa (cfun);
1597 if (in_ssa)
1599 init_ssa_operands (cfun);
1600 cfun->gimple_df->in_ssa_p = true;
1601 cfun->curr_properties |= PROP_ssa;
1604 DECL_INITIAL (decl) = make_node (BLOCK);
1605 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1607 DECL_SAVED_TREE (decl) = error_mark_node;
1608 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1609 | PROP_cfg | PROP_loops);
1611 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1612 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1613 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1615 /* Create BB for body of the function and connect it properly. */
1616 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1617 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1618 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1619 bb->count = count;
1620 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1621 e->probability = profile_probability::always ();
1622 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1623 e->probability = profile_probability::always ();
1624 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1626 return bb;
1629 /* Adjust PTR by the constant FIXED_OFFSET, by the vtable offset indicated by
1630 VIRTUAL_OFFSET, and by the indirect offset indicated by INDIRECT_OFFSET, if
1631 it is non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and zero
1632 for a result adjusting thunk. */
1634 tree
1635 thunk_adjust (gimple_stmt_iterator * bsi,
1636 tree ptr, bool this_adjusting,
1637 HOST_WIDE_INT fixed_offset, tree virtual_offset,
1638 HOST_WIDE_INT indirect_offset)
1640 gassign *stmt;
1641 tree ret;
1643 if (this_adjusting
1644 && fixed_offset != 0)
1646 stmt = gimple_build_assign
1647 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1648 ptr,
1649 fixed_offset));
1650 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1653 if (!vtable_entry_type && (virtual_offset || indirect_offset != 0))
1655 tree vfunc_type = make_node (FUNCTION_TYPE);
1656 TREE_TYPE (vfunc_type) = integer_type_node;
1657 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1658 layout_type (vfunc_type);
1660 vtable_entry_type = build_pointer_type (vfunc_type);
1663 /* If there's a virtual offset, look up that value in the vtable and
1664 adjust the pointer again. */
1665 if (virtual_offset)
1667 tree vtabletmp;
1668 tree vtabletmp2;
1669 tree vtabletmp3;
1671 vtabletmp =
1672 create_tmp_reg (build_pointer_type
1673 (build_pointer_type (vtable_entry_type)), "vptr");
1675 /* The vptr is always at offset zero in the object. */
1676 stmt = gimple_build_assign (vtabletmp,
1677 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1678 ptr));
1679 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1681 /* Form the vtable address. */
1682 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1683 "vtableaddr");
1684 stmt = gimple_build_assign (vtabletmp2,
1685 build_simple_mem_ref (vtabletmp));
1686 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1688 /* Find the entry with the vcall offset. */
1689 stmt = gimple_build_assign (vtabletmp2,
1690 fold_build_pointer_plus_loc (input_location,
1691 vtabletmp2,
1692 virtual_offset));
1693 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1695 /* Get the offset itself. */
1696 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1697 "vcalloffset");
1698 stmt = gimple_build_assign (vtabletmp3,
1699 build_simple_mem_ref (vtabletmp2));
1700 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1702 /* Adjust the `this' pointer. */
1703 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1704 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1705 GSI_CONTINUE_LINKING);
1708 /* Likewise for an offset that is stored in the object that contains the
1709 vtable. */
1710 if (indirect_offset != 0)
1712 tree offset_ptr, offset_tree;
1714 /* Get the address of the offset. */
1715 offset_ptr
1716 = create_tmp_reg (build_pointer_type
1717 (build_pointer_type (vtable_entry_type)),
1718 "offset_ptr");
1719 stmt = gimple_build_assign (offset_ptr,
1720 build1 (NOP_EXPR, TREE_TYPE (offset_ptr),
1721 ptr));
1722 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1724 stmt = gimple_build_assign
1725 (offset_ptr,
1726 fold_build_pointer_plus_hwi_loc (input_location, offset_ptr,
1727 indirect_offset));
1728 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1730 /* Get the offset itself. */
1731 offset_tree = create_tmp_reg (TREE_TYPE (TREE_TYPE (offset_ptr)),
1732 "offset");
1733 stmt = gimple_build_assign (offset_tree,
1734 build_simple_mem_ref (offset_ptr));
1735 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1737 /* Adjust the `this' pointer. */
1738 ptr = fold_build_pointer_plus_loc (input_location, ptr, offset_tree);
1739 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1740 GSI_CONTINUE_LINKING);
1743 if (!this_adjusting
1744 && fixed_offset != 0)
1745 /* Adjust the pointer by the constant. */
1747 tree ptrtmp;
1749 if (VAR_P (ptr))
1750 ptrtmp = ptr;
1751 else
1753 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1754 stmt = gimple_build_assign (ptrtmp, ptr);
1755 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1757 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1758 ptrtmp, fixed_offset);
1761 /* Emit the statement and gimplify the adjustment expression. */
1762 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1763 stmt = gimple_build_assign (ret, ptr);
1764 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1766 return ret;
1769 /* Expand thunk NODE to gimple if possible.
1770 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1771 no assembler is produced.
1772 When OUTPUT_ASM_THUNK is true, also produce assembler for
1773 thunks that are not lowered. */
1775 bool
1776 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1778 bool this_adjusting = thunk.this_adjusting;
1779 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1780 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1781 HOST_WIDE_INT indirect_offset = thunk.indirect_offset;
1782 tree virtual_offset = NULL;
1783 tree alias = callees->callee->decl;
1784 tree thunk_fndecl = decl;
1785 tree a;
1787 if (!force_gimple_thunk
1788 && this_adjusting
1789 && indirect_offset == 0
1790 && !DECL_EXTERNAL (alias)
1791 && !DECL_STATIC_CHAIN (alias)
1792 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1793 virtual_value, alias))
1795 tree fn_block;
1796 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1798 if (!output_asm_thunks)
1800 analyzed = true;
1801 return false;
1804 if (in_lto_p)
1805 get_untransformed_body ();
1806 a = DECL_ARGUMENTS (thunk_fndecl);
1808 current_function_decl = thunk_fndecl;
1810 /* Ensure thunks are emitted in their correct sections. */
1811 resolve_unique_section (thunk_fndecl, 0,
1812 flag_function_sections);
1814 DECL_RESULT (thunk_fndecl)
1815 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1816 RESULT_DECL, 0, restype);
1817 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1819 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1820 create one. */
1821 fn_block = make_node (BLOCK);
1822 BLOCK_VARS (fn_block) = a;
1823 DECL_INITIAL (thunk_fndecl) = fn_block;
1824 BLOCK_SUPERCONTEXT (fn_block) = thunk_fndecl;
1825 allocate_struct_function (thunk_fndecl, false);
1826 init_function_start (thunk_fndecl);
1827 cfun->is_thunk = 1;
1828 insn_locations_init ();
1829 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1830 prologue_location = curr_insn_location ();
1832 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1833 fixed_offset, virtual_value, alias);
1835 insn_locations_finalize ();
1836 init_insn_lengths ();
1837 free_after_compilation (cfun);
1838 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1839 thunk.thunk_p = false;
1840 analyzed = false;
1842 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1844 error ("generic thunk code fails for method %qD which uses %<...%>",
1845 thunk_fndecl);
1846 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1847 analyzed = true;
1848 return false;
1850 else
1852 tree restype;
1853 basic_block bb, then_bb, else_bb, return_bb;
1854 gimple_stmt_iterator bsi;
1855 int nargs = 0;
1856 tree arg;
1857 int i;
1858 tree resdecl;
1859 tree restmp = NULL;
1861 gcall *call;
1862 greturn *ret;
1863 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1865 /* We may be called from expand_thunk that releases body except for
1866 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1867 if (in_lto_p && !force_gimple_thunk)
1868 get_untransformed_body ();
1870 /* We need to force DECL_IGNORED_P when the thunk is created
1871 after early debug was run. */
1872 if (force_gimple_thunk)
1873 DECL_IGNORED_P (thunk_fndecl) = 1;
1875 a = DECL_ARGUMENTS (thunk_fndecl);
1877 current_function_decl = thunk_fndecl;
1879 /* Ensure thunks are emitted in their correct sections. */
1880 resolve_unique_section (thunk_fndecl, 0,
1881 flag_function_sections);
1883 bitmap_obstack_initialize (NULL);
1885 if (thunk.virtual_offset_p)
1886 virtual_offset = size_int (virtual_value);
1888 /* Build the return declaration for the function. */
1889 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1890 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1892 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1893 DECL_ARTIFICIAL (resdecl) = 1;
1894 DECL_IGNORED_P (resdecl) = 1;
1895 DECL_CONTEXT (resdecl) = thunk_fndecl;
1896 DECL_RESULT (thunk_fndecl) = resdecl;
1898 else
1899 resdecl = DECL_RESULT (thunk_fndecl);
1901 profile_count cfg_count = count;
1902 if (!cfg_count.initialized_p ())
1903 cfg_count = profile_count::from_gcov_type (BB_FREQ_MAX).guessed_local ();
1905 bb = then_bb = else_bb = return_bb
1906 = init_lowered_empty_function (thunk_fndecl, true, cfg_count);
1908 bsi = gsi_start_bb (bb);
1910 /* Build call to the function being thunked. */
1911 if (!VOID_TYPE_P (restype)
1912 && (!alias_is_noreturn
1913 || TREE_ADDRESSABLE (restype)
1914 || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST))
1916 if (DECL_BY_REFERENCE (resdecl))
1918 restmp = gimple_fold_indirect_ref (resdecl);
1919 if (!restmp)
1920 restmp = build2 (MEM_REF,
1921 TREE_TYPE (TREE_TYPE (resdecl)),
1922 resdecl,
1923 build_int_cst (TREE_TYPE (resdecl), 0));
1925 else if (!is_gimple_reg_type (restype))
1927 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1929 restmp = resdecl;
1931 if (VAR_P (restmp))
1933 add_local_decl (cfun, restmp);
1934 BLOCK_VARS (DECL_INITIAL (current_function_decl))
1935 = restmp;
1938 else
1939 restmp = create_tmp_var (restype, "retval");
1941 else
1942 restmp = create_tmp_reg (restype, "retval");
1945 for (arg = a; arg; arg = DECL_CHAIN (arg))
1946 nargs++;
1947 auto_vec<tree> vargs (nargs);
1948 i = 0;
1949 arg = a;
1950 if (this_adjusting)
1952 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1953 virtual_offset, indirect_offset));
1954 arg = DECL_CHAIN (a);
1955 i = 1;
1958 if (nargs)
1959 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1961 tree tmp = arg;
1962 if (VECTOR_TYPE_P (TREE_TYPE (arg))
1963 || TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
1964 DECL_GIMPLE_REG_P (arg) = 1;
1966 if (!is_gimple_val (arg))
1968 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1969 (TREE_TYPE (arg)), "arg");
1970 gimple *stmt = gimple_build_assign (tmp, arg);
1971 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1973 vargs.quick_push (tmp);
1975 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1976 callees->call_stmt = call;
1977 gimple_call_set_from_thunk (call, true);
1978 if (DECL_STATIC_CHAIN (alias))
1980 tree p = DECL_STRUCT_FUNCTION (alias)->static_chain_decl;
1981 tree type = TREE_TYPE (p);
1982 tree decl = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1983 PARM_DECL, create_tmp_var_name ("CHAIN"),
1984 type);
1985 DECL_ARTIFICIAL (decl) = 1;
1986 DECL_IGNORED_P (decl) = 1;
1987 TREE_USED (decl) = 1;
1988 DECL_CONTEXT (decl) = thunk_fndecl;
1989 DECL_ARG_TYPE (decl) = type;
1990 TREE_READONLY (decl) = 1;
1992 struct function *sf = DECL_STRUCT_FUNCTION (thunk_fndecl);
1993 sf->static_chain_decl = decl;
1995 gimple_call_set_chain (call, decl);
1998 /* Return slot optimization is always possible and in fact required to
1999 return values with DECL_BY_REFERENCE. */
2000 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
2001 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
2002 || DECL_BY_REFERENCE (resdecl)))
2003 gimple_call_set_return_slot_opt (call, true);
2005 if (restmp)
2007 gimple_call_set_lhs (call, restmp);
2008 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
2009 TREE_TYPE (TREE_TYPE (alias))));
2011 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
2012 if (!alias_is_noreturn)
2014 if (restmp && !this_adjusting
2015 && (fixed_offset || virtual_offset))
2017 tree true_label = NULL_TREE;
2019 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
2021 gimple *stmt;
2022 edge e;
2023 /* If the return type is a pointer, we need to
2024 protect against NULL. We know there will be an
2025 adjustment, because that's why we're emitting a
2026 thunk. */
2027 then_bb = create_basic_block (NULL, bb);
2028 then_bb->count = cfg_count - cfg_count.apply_scale (1, 16);
2029 return_bb = create_basic_block (NULL, then_bb);
2030 return_bb->count = cfg_count;
2031 else_bb = create_basic_block (NULL, else_bb);
2032 else_bb->count = cfg_count.apply_scale (1, 16);
2033 add_bb_to_loop (then_bb, bb->loop_father);
2034 add_bb_to_loop (return_bb, bb->loop_father);
2035 add_bb_to_loop (else_bb, bb->loop_father);
2036 remove_edge (single_succ_edge (bb));
2037 true_label = gimple_block_label (then_bb);
2038 stmt = gimple_build_cond (NE_EXPR, restmp,
2039 build_zero_cst (TREE_TYPE (restmp)),
2040 NULL_TREE, NULL_TREE);
2041 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
2042 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
2043 e->probability = profile_probability::guessed_always ()
2044 .apply_scale (1, 16);
2045 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
2046 e->probability = profile_probability::guessed_always ()
2047 .apply_scale (1, 16);
2048 make_single_succ_edge (return_bb,
2049 EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
2050 make_single_succ_edge (then_bb, return_bb, EDGE_FALLTHRU);
2051 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
2052 e->probability = profile_probability::always ();
2053 bsi = gsi_last_bb (then_bb);
2056 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
2057 fixed_offset, virtual_offset,
2058 indirect_offset);
2059 if (true_label)
2061 gimple *stmt;
2062 bsi = gsi_last_bb (else_bb);
2063 stmt = gimple_build_assign (restmp,
2064 build_zero_cst (TREE_TYPE (restmp)));
2065 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
2066 bsi = gsi_last_bb (return_bb);
2069 else
2070 gimple_call_set_tail (call, true);
2072 /* Build return value. */
2073 if (!DECL_BY_REFERENCE (resdecl))
2074 ret = gimple_build_return (restmp);
2075 else
2076 ret = gimple_build_return (resdecl);
2078 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
2080 else
2082 gimple_call_set_tail (call, true);
2083 remove_edge (single_succ_edge (bb));
2086 cfun->gimple_df->in_ssa_p = true;
2087 update_max_bb_count ();
2088 profile_status_for_fn (cfun)
2089 = cfg_count.initialized_p () && cfg_count.ipa_p ()
2090 ? PROFILE_READ : PROFILE_GUESSED;
2091 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
2092 TREE_ASM_WRITTEN (thunk_fndecl) = false;
2093 delete_unreachable_blocks ();
2094 update_ssa (TODO_update_ssa);
2095 checking_verify_flow_info ();
2096 free_dominance_info (CDI_DOMINATORS);
2098 /* Since we want to emit the thunk, we explicitly mark its name as
2099 referenced. */
2100 thunk.thunk_p = false;
2101 lowered = true;
2102 bitmap_obstack_release (NULL);
2104 current_function_decl = NULL;
2105 set_cfun (NULL);
2106 return true;
2109 /* Assemble thunks and aliases associated to node. */
2111 void
2112 cgraph_node::assemble_thunks_and_aliases (void)
2114 cgraph_edge *e;
2115 ipa_ref *ref;
2117 for (e = callers; e;)
2118 if (e->caller->thunk.thunk_p
2119 && !e->caller->inlined_to)
2121 cgraph_node *thunk = e->caller;
2123 e = e->next_caller;
2124 thunk->expand_thunk (true, false);
2125 thunk->assemble_thunks_and_aliases ();
2127 else
2128 e = e->next_caller;
2130 FOR_EACH_ALIAS (this, ref)
2132 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2133 if (!alias->transparent_alias)
2135 bool saved_written = TREE_ASM_WRITTEN (decl);
2137 /* Force assemble_alias to really output the alias this time instead
2138 of buffering it in same alias pairs. */
2139 TREE_ASM_WRITTEN (decl) = 1;
2140 do_assemble_alias (alias->decl,
2141 DECL_ASSEMBLER_NAME (decl));
2142 alias->assemble_thunks_and_aliases ();
2143 TREE_ASM_WRITTEN (decl) = saved_written;
2148 /* Expand function specified by node. */
2150 void
2151 cgraph_node::expand (void)
2153 location_t saved_loc;
2155 /* We ought to not compile any inline clones. */
2156 gcc_assert (!inlined_to);
2158 /* __RTL functions are compiled as soon as they are parsed, so don't
2159 do it again. */
2160 if (native_rtl_p ())
2161 return;
2163 announce_function (decl);
2164 process = 0;
2165 gcc_assert (lowered);
2166 get_untransformed_body ();
2168 /* Generate RTL for the body of DECL. */
2170 timevar_push (TV_REST_OF_COMPILATION);
2172 gcc_assert (symtab->global_info_ready);
2174 /* Initialize the default bitmap obstack. */
2175 bitmap_obstack_initialize (NULL);
2177 /* Initialize the RTL code for the function. */
2178 saved_loc = input_location;
2179 input_location = DECL_SOURCE_LOCATION (decl);
2181 gcc_assert (DECL_STRUCT_FUNCTION (decl));
2182 push_cfun (DECL_STRUCT_FUNCTION (decl));
2183 init_function_start (decl);
2185 gimple_register_cfg_hooks ();
2187 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
2189 execute_all_ipa_transforms (false);
2191 /* Perform all tree transforms and optimizations. */
2193 /* Signal the start of passes. */
2194 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
2196 execute_pass_list (cfun, g->get_passes ()->all_passes);
2198 /* Signal the end of passes. */
2199 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
2201 bitmap_obstack_release (&reg_obstack);
2203 /* Release the default bitmap obstack. */
2204 bitmap_obstack_release (NULL);
2206 /* If requested, warn about function definitions where the function will
2207 return a value (usually of some struct or union type) which itself will
2208 take up a lot of stack space. */
2209 if (!DECL_EXTERNAL (decl) && TREE_TYPE (decl))
2211 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
2213 if (ret_type && TYPE_SIZE_UNIT (ret_type)
2214 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
2215 && compare_tree_int (TYPE_SIZE_UNIT (ret_type),
2216 warn_larger_than_size) > 0)
2218 unsigned int size_as_int
2219 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
2221 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
2222 warning (OPT_Wlarger_than_,
2223 "size of return value of %q+D is %u bytes",
2224 decl, size_as_int);
2225 else
2226 warning (OPT_Wlarger_than_,
2227 "size of return value of %q+D is larger than %wu bytes",
2228 decl, warn_larger_than_size);
2232 gimple_set_body (decl, NULL);
2233 if (DECL_STRUCT_FUNCTION (decl) == 0
2234 && !cgraph_node::get (decl)->origin)
2236 /* Stop pointing to the local nodes about to be freed.
2237 But DECL_INITIAL must remain nonzero so we know this
2238 was an actual function definition.
2239 For a nested function, this is done in c_pop_function_context.
2240 If rest_of_compilation set this to 0, leave it 0. */
2241 if (DECL_INITIAL (decl) != 0)
2242 DECL_INITIAL (decl) = error_mark_node;
2245 input_location = saved_loc;
2247 ggc_collect ();
2248 timevar_pop (TV_REST_OF_COMPILATION);
2250 /* Make sure that BE didn't give up on compiling. */
2251 gcc_assert (TREE_ASM_WRITTEN (decl));
2252 if (cfun)
2253 pop_cfun ();
2255 /* It would make a lot more sense to output thunks before function body to
2256 get more forward and fewer backward jumps. This however would need
2257 solving problem with comdats. See PR48668. Also aliases must come after
2258 function itself to make one pass assemblers, like one on AIX, happy.
2259 See PR 50689.
2260 FIXME: Perhaps thunks should be move before function IFF they are not in
2261 comdat groups. */
2262 assemble_thunks_and_aliases ();
2263 release_body ();
2264 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2265 points to the dead function body. */
2266 remove_callees ();
2267 remove_all_references ();
2270 /* Node comparator that is responsible for the order that corresponds
2271 to time when a function was launched for the first time. */
2273 static int
2274 node_cmp (const void *pa, const void *pb)
2276 const cgraph_node *a = *(const cgraph_node * const *) pa;
2277 const cgraph_node *b = *(const cgraph_node * const *) pb;
2279 /* Functions with time profile must be before these without profile. */
2280 if (!a->tp_first_run || !b->tp_first_run)
2281 return a->tp_first_run - b->tp_first_run;
2283 return a->tp_first_run != b->tp_first_run
2284 ? b->tp_first_run - a->tp_first_run
2285 : b->order - a->order;
2288 /* Expand all functions that must be output.
2290 Attempt to topologically sort the nodes so function is output when
2291 all called functions are already assembled to allow data to be
2292 propagated across the callgraph. Use a stack to get smaller distance
2293 between a function and its callees (later we may choose to use a more
2294 sophisticated algorithm for function reordering; we will likely want
2295 to use subsections to make the output functions appear in top-down
2296 order). */
2298 static void
2299 expand_all_functions (void)
2301 cgraph_node *node;
2302 cgraph_node **order = XCNEWVEC (cgraph_node *,
2303 symtab->cgraph_count);
2304 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2305 int order_pos, new_order_pos = 0;
2306 int i;
2308 order_pos = ipa_reverse_postorder (order);
2309 gcc_assert (order_pos == symtab->cgraph_count);
2311 /* Garbage collector may remove inline clones we eliminate during
2312 optimization. So we must be sure to not reference them. */
2313 for (i = 0; i < order_pos; i++)
2314 if (order[i]->process)
2315 order[new_order_pos++] = order[i];
2317 if (flag_profile_reorder_functions)
2318 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2320 for (i = new_order_pos - 1; i >= 0; i--)
2322 node = order[i];
2324 if (node->process)
2326 expanded_func_count++;
2327 if(node->tp_first_run)
2328 profiled_func_count++;
2330 if (symtab->dump_file)
2331 fprintf (symtab->dump_file,
2332 "Time profile order in expand_all_functions:%s:%d\n",
2333 node->asm_name (), node->tp_first_run);
2334 node->process = 0;
2335 node->expand ();
2339 if (dump_file)
2340 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2341 main_input_filename, profiled_func_count, expanded_func_count);
2343 if (symtab->dump_file && flag_profile_reorder_functions)
2344 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2345 profiled_func_count, expanded_func_count);
2347 symtab->process_new_functions ();
2348 free_gimplify_stack ();
2350 free (order);
2353 /* This is used to sort the node types by the cgraph order number. */
2355 enum cgraph_order_sort_kind
2357 ORDER_UNDEFINED = 0,
2358 ORDER_FUNCTION,
2359 ORDER_VAR,
2360 ORDER_VAR_UNDEF,
2361 ORDER_ASM
2364 struct cgraph_order_sort
2366 enum cgraph_order_sort_kind kind;
2367 union
2369 cgraph_node *f;
2370 varpool_node *v;
2371 asm_node *a;
2372 } u;
2375 /* Output all functions, variables, and asm statements in the order
2376 according to their order fields, which is the order in which they
2377 appeared in the file. This implements -fno-toplevel-reorder. In
2378 this mode we may output functions and variables which don't really
2379 need to be output. */
2381 static void
2382 output_in_order (void)
2384 int max;
2385 cgraph_order_sort *nodes;
2386 int i;
2387 cgraph_node *pf;
2388 varpool_node *pv;
2389 asm_node *pa;
2390 max = symtab->order;
2391 nodes = XCNEWVEC (cgraph_order_sort, max);
2393 FOR_EACH_DEFINED_FUNCTION (pf)
2395 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2397 if (!pf->no_reorder)
2398 continue;
2399 i = pf->order;
2400 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2401 nodes[i].kind = ORDER_FUNCTION;
2402 nodes[i].u.f = pf;
2406 /* There is a similar loop in symbol_table::output_variables.
2407 Please keep them in sync. */
2408 FOR_EACH_VARIABLE (pv)
2410 if (!pv->no_reorder)
2411 continue;
2412 if (DECL_HARD_REGISTER (pv->decl)
2413 || DECL_HAS_VALUE_EXPR_P (pv->decl))
2414 continue;
2415 i = pv->order;
2416 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2417 nodes[i].kind = pv->definition ? ORDER_VAR : ORDER_VAR_UNDEF;
2418 nodes[i].u.v = pv;
2421 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2423 i = pa->order;
2424 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2425 nodes[i].kind = ORDER_ASM;
2426 nodes[i].u.a = pa;
2429 /* In toplevel reorder mode we output all statics; mark them as needed. */
2431 for (i = 0; i < max; ++i)
2432 if (nodes[i].kind == ORDER_VAR)
2433 nodes[i].u.v->finalize_named_section_flags ();
2435 for (i = 0; i < max; ++i)
2437 switch (nodes[i].kind)
2439 case ORDER_FUNCTION:
2440 nodes[i].u.f->process = 0;
2441 nodes[i].u.f->expand ();
2442 break;
2444 case ORDER_VAR:
2445 nodes[i].u.v->assemble_decl ();
2446 break;
2448 case ORDER_VAR_UNDEF:
2449 assemble_undefined_decl (nodes[i].u.v->decl);
2450 break;
2452 case ORDER_ASM:
2453 assemble_asm (nodes[i].u.a->asm_str);
2454 break;
2456 case ORDER_UNDEFINED:
2457 break;
2459 default:
2460 gcc_unreachable ();
2464 symtab->clear_asm_symbols ();
2466 free (nodes);
2469 static void
2470 ipa_passes (void)
2472 gcc::pass_manager *passes = g->get_passes ();
2474 set_cfun (NULL);
2475 current_function_decl = NULL;
2476 gimple_register_cfg_hooks ();
2477 bitmap_obstack_initialize (NULL);
2479 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2481 if (!in_lto_p)
2483 execute_ipa_pass_list (passes->all_small_ipa_passes);
2484 if (seen_error ())
2485 return;
2488 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2489 devirtualization and other changes where removal iterate. */
2490 symtab->remove_unreachable_nodes (symtab->dump_file);
2492 /* If pass_all_early_optimizations was not scheduled, the state of
2493 the cgraph will not be properly updated. Update it now. */
2494 if (symtab->state < IPA_SSA)
2495 symtab->state = IPA_SSA;
2497 if (!in_lto_p)
2499 /* Generate coverage variables and constructors. */
2500 coverage_finish ();
2502 /* Process new functions added. */
2503 set_cfun (NULL);
2504 current_function_decl = NULL;
2505 symtab->process_new_functions ();
2507 execute_ipa_summary_passes
2508 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2511 /* Some targets need to handle LTO assembler output specially. */
2512 if (flag_generate_lto || flag_generate_offload)
2513 targetm.asm_out.lto_start ();
2515 if (!in_lto_p
2516 || flag_incremental_link == INCREMENTAL_LINK_LTO)
2518 if (!quiet_flag)
2519 fprintf (stderr, "Streaming LTO\n");
2520 if (g->have_offload)
2522 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2523 lto_stream_offload_p = true;
2524 ipa_write_summaries ();
2525 lto_stream_offload_p = false;
2527 if (flag_lto)
2529 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2530 lto_stream_offload_p = false;
2531 ipa_write_summaries ();
2535 if (flag_generate_lto || flag_generate_offload)
2536 targetm.asm_out.lto_end ();
2538 if (!flag_ltrans
2539 && ((in_lto_p && flag_incremental_link != INCREMENTAL_LINK_LTO)
2540 || !flag_lto || flag_fat_lto_objects))
2541 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2542 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2544 bitmap_obstack_release (NULL);
2548 /* Return string alias is alias of. */
2550 static tree
2551 get_alias_symbol (tree decl)
2553 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2554 return get_identifier (TREE_STRING_POINTER
2555 (TREE_VALUE (TREE_VALUE (alias))));
2559 /* Weakrefs may be associated to external decls and thus not output
2560 at expansion time. Emit all necessary aliases. */
2562 void
2563 symbol_table::output_weakrefs (void)
2565 symtab_node *node;
2566 FOR_EACH_SYMBOL (node)
2567 if (node->alias
2568 && !TREE_ASM_WRITTEN (node->decl)
2569 && node->weakref)
2571 tree target;
2573 /* Weakrefs are special by not requiring target definition in current
2574 compilation unit. It is thus bit hard to work out what we want to
2575 alias.
2576 When alias target is defined, we need to fetch it from symtab reference,
2577 otherwise it is pointed to by alias_target. */
2578 if (node->alias_target)
2579 target = (DECL_P (node->alias_target)
2580 ? DECL_ASSEMBLER_NAME (node->alias_target)
2581 : node->alias_target);
2582 else if (node->analyzed)
2583 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2584 else
2586 gcc_unreachable ();
2587 target = get_alias_symbol (node->decl);
2589 do_assemble_alias (node->decl, target);
2593 /* Perform simple optimizations based on callgraph. */
2595 void
2596 symbol_table::compile (void)
2598 if (seen_error ())
2599 return;
2601 symtab_node::checking_verify_symtab_nodes ();
2603 timevar_push (TV_CGRAPHOPT);
2604 if (pre_ipa_mem_report)
2605 dump_memory_report ("Memory consumption before IPA");
2606 if (!quiet_flag)
2607 fprintf (stderr, "Performing interprocedural optimizations\n");
2608 state = IPA;
2610 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2611 if (flag_generate_lto || flag_generate_offload)
2612 lto_streamer_hooks_init ();
2614 /* Don't run the IPA passes if there was any error or sorry messages. */
2615 if (!seen_error ())
2617 timevar_start (TV_CGRAPH_IPA_PASSES);
2618 ipa_passes ();
2619 timevar_stop (TV_CGRAPH_IPA_PASSES);
2621 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2622 if (seen_error ()
2623 || ((!in_lto_p || flag_incremental_link == INCREMENTAL_LINK_LTO)
2624 && flag_lto && !flag_fat_lto_objects))
2626 timevar_pop (TV_CGRAPHOPT);
2627 return;
2630 global_info_ready = true;
2631 if (dump_file)
2633 fprintf (dump_file, "Optimized ");
2634 symtab->dump (dump_file);
2636 if (post_ipa_mem_report)
2637 dump_memory_report ("Memory consumption after IPA");
2638 timevar_pop (TV_CGRAPHOPT);
2640 /* Output everything. */
2641 switch_to_section (text_section);
2642 (*debug_hooks->assembly_start) ();
2643 if (!quiet_flag)
2644 fprintf (stderr, "Assembling functions:\n");
2645 symtab_node::checking_verify_symtab_nodes ();
2647 bitmap_obstack_initialize (NULL);
2648 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2649 bitmap_obstack_release (NULL);
2650 mark_functions_to_output ();
2652 /* When weakref support is missing, we automatically translate all
2653 references to NODE to references to its ultimate alias target.
2654 The renaming mechanism uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2655 TREE_CHAIN.
2657 Set up this mapping before we output any assembler but once we are sure
2658 that all symbol renaming is done.
2660 FIXME: All this ugliness can go away if we just do renaming at gimple
2661 level by physically rewriting the IL. At the moment we can only redirect
2662 calls, so we need infrastructure for renaming references as well. */
2663 #ifndef ASM_OUTPUT_WEAKREF
2664 symtab_node *node;
2666 FOR_EACH_SYMBOL (node)
2667 if (node->alias
2668 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2670 IDENTIFIER_TRANSPARENT_ALIAS
2671 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2672 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2673 = (node->alias_target ? node->alias_target
2674 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2676 #endif
2678 state = EXPANSION;
2680 /* Output first asm statements and anything ordered. The process
2681 flag is cleared for these nodes, so we skip them later. */
2682 output_in_order ();
2684 timevar_start (TV_CGRAPH_FUNC_EXPANSION);
2685 expand_all_functions ();
2686 timevar_stop (TV_CGRAPH_FUNC_EXPANSION);
2688 output_variables ();
2690 process_new_functions ();
2691 state = FINISHED;
2692 output_weakrefs ();
2694 if (dump_file)
2696 fprintf (dump_file, "\nFinal ");
2697 symtab->dump (dump_file);
2699 if (!flag_checking)
2700 return;
2701 symtab_node::verify_symtab_nodes ();
2702 /* Double check that all inline clones are gone and that all
2703 function bodies have been released from memory. */
2704 if (!seen_error ())
2706 cgraph_node *node;
2707 bool error_found = false;
2709 FOR_EACH_DEFINED_FUNCTION (node)
2710 if (node->inlined_to
2711 || gimple_has_body_p (node->decl))
2713 error_found = true;
2714 node->debug ();
2716 if (error_found)
2717 internal_error ("nodes with unreleased memory found");
2721 /* Earlydebug dump file, flags, and number. */
2723 static int debuginfo_early_dump_nr;
2724 static FILE *debuginfo_early_dump_file;
2725 static dump_flags_t debuginfo_early_dump_flags;
2727 /* Debug dump file, flags, and number. */
2729 static int debuginfo_dump_nr;
2730 static FILE *debuginfo_dump_file;
2731 static dump_flags_t debuginfo_dump_flags;
2733 /* Register the debug and earlydebug dump files. */
2735 void
2736 debuginfo_early_init (void)
2738 gcc::dump_manager *dumps = g->get_dumps ();
2739 debuginfo_early_dump_nr = dumps->dump_register (".earlydebug", "earlydebug",
2740 "earlydebug", DK_tree,
2741 OPTGROUP_NONE,
2742 false);
2743 debuginfo_dump_nr = dumps->dump_register (".debug", "debug",
2744 "debug", DK_tree,
2745 OPTGROUP_NONE,
2746 false);
2749 /* Initialize the debug and earlydebug dump files. */
2751 void
2752 debuginfo_init (void)
2754 gcc::dump_manager *dumps = g->get_dumps ();
2755 debuginfo_dump_file = dump_begin (debuginfo_dump_nr, NULL);
2756 debuginfo_dump_flags = dumps->get_dump_file_info (debuginfo_dump_nr)->pflags;
2757 debuginfo_early_dump_file = dump_begin (debuginfo_early_dump_nr, NULL);
2758 debuginfo_early_dump_flags
2759 = dumps->get_dump_file_info (debuginfo_early_dump_nr)->pflags;
2762 /* Finalize the debug and earlydebug dump files. */
2764 void
2765 debuginfo_fini (void)
2767 if (debuginfo_dump_file)
2768 dump_end (debuginfo_dump_nr, debuginfo_dump_file);
2769 if (debuginfo_early_dump_file)
2770 dump_end (debuginfo_early_dump_nr, debuginfo_early_dump_file);
2773 /* Set dump_file to the debug dump file. */
2775 void
2776 debuginfo_start (void)
2778 set_dump_file (debuginfo_dump_file);
2781 /* Undo setting dump_file to the debug dump file. */
2783 void
2784 debuginfo_stop (void)
2786 set_dump_file (NULL);
2789 /* Set dump_file to the earlydebug dump file. */
2791 void
2792 debuginfo_early_start (void)
2794 set_dump_file (debuginfo_early_dump_file);
2797 /* Undo setting dump_file to the earlydebug dump file. */
2799 void
2800 debuginfo_early_stop (void)
2802 set_dump_file (NULL);
2805 /* Analyze the whole compilation unit once it is parsed completely. */
2807 void
2808 symbol_table::finalize_compilation_unit (void)
2810 timevar_push (TV_CGRAPH);
2812 /* If we're here there's no current function anymore. Some frontends
2813 are lazy in clearing these. */
2814 current_function_decl = NULL;
2815 set_cfun (NULL);
2817 /* Do not skip analyzing the functions if there were errors, we
2818 miss diagnostics for following functions otherwise. */
2820 /* Emit size functions we didn't inline. */
2821 finalize_size_functions ();
2823 /* Mark alias targets necessary and emit diagnostics. */
2824 handle_alias_pairs ();
2826 if (!quiet_flag)
2828 fprintf (stderr, "\nAnalyzing compilation unit\n");
2829 fflush (stderr);
2832 if (flag_dump_passes)
2833 dump_passes ();
2835 /* Gimplify and lower all functions, compute reachability and
2836 remove unreachable nodes. */
2837 analyze_functions (/*first_time=*/true);
2839 /* Mark alias targets necessary and emit diagnostics. */
2840 handle_alias_pairs ();
2842 /* Gimplify and lower thunks. */
2843 analyze_functions (/*first_time=*/false);
2845 /* Offloading requires LTO infrastructure. */
2846 if (!in_lto_p && g->have_offload)
2847 flag_generate_offload = 1;
2849 if (!seen_error ())
2851 /* Emit early debug for reachable functions, and by consequence,
2852 locally scoped symbols. */
2853 struct cgraph_node *cnode;
2854 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2855 (*debug_hooks->early_global_decl) (cnode->decl);
2857 /* Clean up anything that needs cleaning up after initial debug
2858 generation. */
2859 debuginfo_early_start ();
2860 (*debug_hooks->early_finish) (main_input_filename);
2861 debuginfo_early_stop ();
2864 /* Finally drive the pass manager. */
2865 compile ();
2867 timevar_pop (TV_CGRAPH);
2870 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2871 within the same process. For use by toplev::finalize. */
2873 void
2874 cgraphunit_c_finalize (void)
2876 gcc_assert (cgraph_new_nodes.length () == 0);
2877 cgraph_new_nodes.truncate (0);
2879 vtable_entry_type = NULL;
2880 queued_nodes = &symtab_terminator;
2882 first_analyzed = NULL;
2883 first_analyzed_var = NULL;
2886 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2887 kind of wrapper method. */
2889 void
2890 cgraph_node::create_wrapper (cgraph_node *target)
2892 /* Preserve DECL_RESULT so we get right by reference flag. */
2893 tree decl_result = DECL_RESULT (decl);
2895 /* Remove the function's body but keep arguments to be reused
2896 for thunk. */
2897 release_body (true);
2898 reset ();
2900 DECL_UNINLINABLE (decl) = false;
2901 DECL_RESULT (decl) = decl_result;
2902 DECL_INITIAL (decl) = NULL;
2903 allocate_struct_function (decl, false);
2904 set_cfun (NULL);
2906 /* Turn alias into thunk and expand it into GIMPLE representation. */
2907 definition = true;
2909 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2910 thunk.thunk_p = true;
2911 create_edge (target, NULL, count);
2912 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2914 tree arguments = DECL_ARGUMENTS (decl);
2916 while (arguments)
2918 TREE_ADDRESSABLE (arguments) = false;
2919 arguments = TREE_CHAIN (arguments);
2922 expand_thunk (false, true);
2924 /* Inline summary set-up. */
2925 analyze ();
2926 inline_analyze_function (this);
2929 #include "gt-cgraphunit.h"