* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / cgraphunit.c
bloba1295d1f4fec92315c58a6e528a8747f155742bb
1 /* Driver of optimization process
2 Copyright (C) 2003-2014 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 optimiations 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, transational 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 sreaming. 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 multple 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 functoins (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 "tm.h"
164 #include "tree.h"
165 #include "varasm.h"
166 #include "stor-layout.h"
167 #include "stringpool.h"
168 #include "output.h"
169 #include "rtl.h"
170 #include "predict.h"
171 #include "vec.h"
172 #include "hashtab.h"
173 #include "hash-set.h"
174 #include "machmode.h"
175 #include "hard-reg-set.h"
176 #include "input.h"
177 #include "function.h"
178 #include "basic-block.h"
179 #include "tree-ssa-alias.h"
180 #include "internal-fn.h"
181 #include "gimple-fold.h"
182 #include "gimple-expr.h"
183 #include "is-a.h"
184 #include "gimple.h"
185 #include "gimplify.h"
186 #include "gimple-iterator.h"
187 #include "gimplify-me.h"
188 #include "gimple-ssa.h"
189 #include "tree-cfg.h"
190 #include "tree-into-ssa.h"
191 #include "tree-ssa.h"
192 #include "tree-inline.h"
193 #include "langhooks.h"
194 #include "toplev.h"
195 #include "flags.h"
196 #include "debug.h"
197 #include "target.h"
198 #include "diagnostic.h"
199 #include "params.h"
200 #include "intl.h"
201 #include "hash-map.h"
202 #include "plugin-api.h"
203 #include "ipa-ref.h"
204 #include "cgraph.h"
205 #include "alloc-pool.h"
206 #include "symbol-summary.h"
207 #include "ipa-prop.h"
208 #include "tree-iterator.h"
209 #include "tree-pass.h"
210 #include "tree-dump.h"
211 #include "gimple-pretty-print.h"
212 #include "output.h"
213 #include "coverage.h"
214 #include "plugin.h"
215 #include "ipa-inline.h"
216 #include "ipa-utils.h"
217 #include "lto-streamer.h"
218 #include "except.h"
219 #include "cfgloop.h"
220 #include "regset.h" /* FIXME: For reg_obstack. */
221 #include "context.h"
222 #include "pass_manager.h"
223 #include "tree-nested.h"
224 #include "gimplify.h"
225 #include "dbgcnt.h"
226 #include "tree-chkp.h"
227 #include "lto-section-names.h"
228 #include "omp-low.h"
229 #include "print-tree.h"
231 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
232 secondary queue used during optimization to accommodate passes that
233 may generate new functions that need to be optimized and expanded. */
234 vec<cgraph_node *> cgraph_new_nodes;
236 static void expand_all_functions (void);
237 static void mark_functions_to_output (void);
238 static void handle_alias_pairs (void);
240 /* Used for vtable lookup in thunk adjusting. */
241 static GTY (()) tree vtable_entry_type;
243 /* Determine if symbol declaration is needed. That is, visible to something
244 either outside this translation unit, something magic in the system
245 configury */
246 bool
247 symtab_node::needed_p (void)
249 /* Double check that no one output the function into assembly file
250 early. */
251 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
252 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
254 if (!definition)
255 return false;
257 if (DECL_EXTERNAL (decl))
258 return false;
260 /* If the user told us it is used, then it must be so. */
261 if (force_output)
262 return true;
264 /* ABI forced symbols are needed when they are external. */
265 if (forced_by_abi && TREE_PUBLIC (decl))
266 return true;
268 /* Keep constructors, destructors and virtual functions. */
269 if (TREE_CODE (decl) == FUNCTION_DECL
270 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
271 return true;
273 /* Externally visible variables must be output. The exception is
274 COMDAT variables that must be output only when they are needed. */
275 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
276 return true;
278 return false;
281 /* Head and terminator of the queue of nodes to be processed while building
282 callgraph. */
284 static symtab_node symtab_terminator;
285 static symtab_node *queued_nodes = &symtab_terminator;
287 /* Add NODE to queue starting at QUEUED_NODES.
288 The queue is linked via AUX pointers and terminated by pointer to 1. */
290 static void
291 enqueue_node (symtab_node *node)
293 if (node->aux)
294 return;
295 gcc_checking_assert (queued_nodes);
296 node->aux = queued_nodes;
297 queued_nodes = node;
300 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
301 functions into callgraph in a way so they look like ordinary reachable
302 functions inserted into callgraph already at construction time. */
304 void
305 symbol_table::process_new_functions (void)
307 tree fndecl;
309 if (!cgraph_new_nodes.exists ())
310 return;
312 handle_alias_pairs ();
313 /* Note that this queue may grow as its being processed, as the new
314 functions may generate new ones. */
315 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
317 cgraph_node *node = cgraph_new_nodes[i];
318 fndecl = node->decl;
319 switch (state)
321 case CONSTRUCTION:
322 /* At construction time we just need to finalize function and move
323 it into reachable functions list. */
325 cgraph_node::finalize_function (fndecl, false);
326 call_cgraph_insertion_hooks (node);
327 enqueue_node (node);
328 break;
330 case IPA:
331 case IPA_SSA:
332 case IPA_SSA_AFTER_INLINING:
333 /* When IPA optimization already started, do all essential
334 transformations that has been already performed on the whole
335 cgraph but not on this function. */
337 gimple_register_cfg_hooks ();
338 if (!node->analyzed)
339 node->analyze ();
340 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
341 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
342 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
343 g->get_passes ()->execute_early_local_passes ();
344 else if (inline_summaries != NULL)
345 compute_inline_parameters (node, true);
346 free_dominance_info (CDI_POST_DOMINATORS);
347 free_dominance_info (CDI_DOMINATORS);
348 pop_cfun ();
349 call_cgraph_insertion_hooks (node);
350 break;
352 case EXPANSION:
353 /* Functions created during expansion shall be compiled
354 directly. */
355 node->process = 0;
356 call_cgraph_insertion_hooks (node);
357 node->expand ();
358 break;
360 default:
361 gcc_unreachable ();
362 break;
366 cgraph_new_nodes.release ();
369 /* As an GCC extension we allow redefinition of the function. The
370 semantics when both copies of bodies differ is not well defined.
371 We replace the old body with new body so in unit at a time mode
372 we always use new body, while in normal mode we may end up with
373 old body inlined into some functions and new body expanded and
374 inlined in others.
376 ??? It may make more sense to use one body for inlining and other
377 body for expanding the function but this is difficult to do. */
379 void
380 cgraph_node::reset (void)
382 /* If process is set, then we have already begun whole-unit analysis.
383 This is *not* testing for whether we've already emitted the function.
384 That case can be sort-of legitimately seen with real function redefinition
385 errors. I would argue that the front end should never present us with
386 such a case, but don't enforce that for now. */
387 gcc_assert (!process);
389 /* Reset our data structures so we can analyze the function again. */
390 memset (&local, 0, sizeof (local));
391 memset (&global, 0, sizeof (global));
392 memset (&rtl, 0, sizeof (rtl));
393 analyzed = false;
394 definition = false;
395 alias = false;
396 weakref = false;
397 cpp_implicit_alias = false;
399 remove_callees ();
400 remove_all_references ();
403 /* Return true when there are references to the node. */
405 bool
406 symtab_node::referred_to_p (void)
408 ipa_ref *ref = NULL;
410 /* See if there are any references at all. */
411 if (iterate_referring (0, ref))
412 return true;
413 /* For functions check also calls. */
414 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
415 if (cn && cn->callers)
416 return true;
417 return false;
420 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
421 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
422 the garbage collector run at the moment. We would need to either create
423 a new GC context, or just not compile right now. */
425 void
426 cgraph_node::finalize_function (tree decl, bool no_collect)
428 cgraph_node *node = cgraph_node::get_create (decl);
430 if (node->definition)
432 /* Nested functions should only be defined once. */
433 gcc_assert (!DECL_CONTEXT (decl)
434 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
435 node->reset ();
436 node->local.redefined_extern_inline = true;
439 notice_global_symbol (decl);
440 node->definition = true;
441 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
443 /* With -fkeep-inline-functions we are keeping all inline functions except
444 for extern inline ones. */
445 if (flag_keep_inline_functions
446 && DECL_DECLARED_INLINE_P (decl)
447 && !DECL_EXTERNAL (decl)
448 && !DECL_DISREGARD_INLINE_LIMITS (decl))
449 node->force_output = 1;
451 /* When not optimizing, also output the static functions. (see
452 PR24561), but don't do so for always_inline functions, functions
453 declared inline and nested functions. These were optimized out
454 in the original implementation and it is unclear whether we want
455 to change the behavior here. */
456 if ((!opt_for_fn (decl, optimize)
457 && !node->cpp_implicit_alias
458 && !DECL_DISREGARD_INLINE_LIMITS (decl)
459 && !DECL_DECLARED_INLINE_P (decl)
460 && !(DECL_CONTEXT (decl)
461 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
462 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
463 node->force_output = 1;
465 /* If we've not yet emitted decl, tell the debug info about it. */
466 if (!TREE_ASM_WRITTEN (decl))
467 (*debug_hooks->deferred_inline_function) (decl);
469 /* Possibly warn about unused parameters. */
470 if (warn_unused_parameter)
471 do_warn_unused_parameter (decl);
473 if (!no_collect)
474 ggc_collect ();
476 if (symtab->state == CONSTRUCTION
477 && (node->needed_p () || node->referred_to_p ()))
478 enqueue_node (node);
481 /* Add the function FNDECL to the call graph.
482 Unlike finalize_function, this function is intended to be used
483 by middle end and allows insertion of new function at arbitrary point
484 of compilation. The function can be either in high, low or SSA form
485 GIMPLE.
487 The function is assumed to be reachable and have address taken (so no
488 API breaking optimizations are performed on it).
490 Main work done by this function is to enqueue the function for later
491 processing to avoid need the passes to be re-entrant. */
493 void
494 cgraph_node::add_new_function (tree fndecl, bool lowered)
496 gcc::pass_manager *passes = g->get_passes ();
497 cgraph_node *node;
498 switch (symtab->state)
500 case PARSING:
501 cgraph_node::finalize_function (fndecl, false);
502 break;
503 case CONSTRUCTION:
504 /* Just enqueue function to be processed at nearest occurrence. */
505 node = cgraph_node::get_create (fndecl);
506 if (lowered)
507 node->lowered = true;
508 cgraph_new_nodes.safe_push (node);
509 break;
511 case IPA:
512 case IPA_SSA:
513 case IPA_SSA_AFTER_INLINING:
514 case EXPANSION:
515 /* Bring the function into finalized state and enqueue for later
516 analyzing and compilation. */
517 node = cgraph_node::get_create (fndecl);
518 node->local.local = false;
519 node->definition = true;
520 node->force_output = true;
521 if (!lowered && symtab->state == EXPANSION)
523 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
524 gimple_register_cfg_hooks ();
525 bitmap_obstack_initialize (NULL);
526 execute_pass_list (cfun, passes->all_lowering_passes);
527 passes->execute_early_local_passes ();
528 bitmap_obstack_release (NULL);
529 pop_cfun ();
531 lowered = true;
533 if (lowered)
534 node->lowered = true;
535 cgraph_new_nodes.safe_push (node);
536 break;
538 case FINISHED:
539 /* At the very end of compilation we have to do all the work up
540 to expansion. */
541 node = cgraph_node::create (fndecl);
542 if (lowered)
543 node->lowered = true;
544 node->definition = true;
545 node->analyze ();
546 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
547 gimple_register_cfg_hooks ();
548 bitmap_obstack_initialize (NULL);
549 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
550 g->get_passes ()->execute_early_local_passes ();
551 bitmap_obstack_release (NULL);
552 pop_cfun ();
553 node->expand ();
554 break;
556 default:
557 gcc_unreachable ();
560 /* Set a personality if required and we already passed EH lowering. */
561 if (lowered
562 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
563 == eh_personality_lang))
564 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
567 /* Analyze the function scheduled to be output. */
568 void
569 cgraph_node::analyze (void)
571 tree decl = this->decl;
572 location_t saved_loc = input_location;
573 input_location = DECL_SOURCE_LOCATION (decl);
575 if (thunk.thunk_p)
577 create_edge (cgraph_node::get (thunk.alias),
578 NULL, 0, CGRAPH_FREQ_BASE);
579 if (!expand_thunk (false, false))
581 thunk.alias = NULL;
582 return;
584 thunk.alias = NULL;
586 if (alias)
587 resolve_alias (cgraph_node::get (alias_target));
588 else if (dispatcher_function)
590 /* Generate the dispatcher body of multi-versioned functions. */
591 cgraph_function_version_info *dispatcher_version_info
592 = function_version ();
593 if (dispatcher_version_info != NULL
594 && (dispatcher_version_info->dispatcher_resolver
595 == NULL_TREE))
597 tree resolver = NULL_TREE;
598 gcc_assert (targetm.generate_version_dispatcher_body);
599 resolver = targetm.generate_version_dispatcher_body (this);
600 gcc_assert (resolver != NULL_TREE);
603 else
605 push_cfun (DECL_STRUCT_FUNCTION (decl));
607 assign_assembler_name_if_neeeded (decl);
609 /* Make sure to gimplify bodies only once. During analyzing a
610 function we lower it, which will require gimplified nested
611 functions, so we can end up here with an already gimplified
612 body. */
613 if (!gimple_has_body_p (decl))
614 gimplify_function_tree (decl);
615 dump_function (TDI_generic, decl);
617 /* Lower the function. */
618 if (!lowered)
620 if (nested)
621 lower_nested_functions (decl);
622 gcc_assert (!nested);
624 gimple_register_cfg_hooks ();
625 bitmap_obstack_initialize (NULL);
626 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
627 free_dominance_info (CDI_POST_DOMINATORS);
628 free_dominance_info (CDI_DOMINATORS);
629 compact_blocks ();
630 bitmap_obstack_release (NULL);
631 lowered = true;
634 pop_cfun ();
636 analyzed = true;
638 input_location = saved_loc;
641 /* C++ frontend produce same body aliases all over the place, even before PCH
642 gets streamed out. It relies on us linking the aliases with their function
643 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
644 first produce aliases without links, but once C++ FE is sure he won't sream
645 PCH we build the links via this function. */
647 void
648 symbol_table::process_same_body_aliases (void)
650 symtab_node *node;
651 FOR_EACH_SYMBOL (node)
652 if (node->cpp_implicit_alias && !node->analyzed)
653 node->resolve_alias
654 (TREE_CODE (node->alias_target) == VAR_DECL
655 ? (symtab_node *)varpool_node::get_create (node->alias_target)
656 : (symtab_node *)cgraph_node::get_create (node->alias_target));
657 cpp_implicit_aliases_done = true;
660 /* Process attributes common for vars and functions. */
662 static void
663 process_common_attributes (symtab_node *node, tree decl)
665 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
667 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
669 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
670 "%<weakref%> attribute should be accompanied with"
671 " an %<alias%> attribute");
672 DECL_WEAK (decl) = 0;
673 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
674 DECL_ATTRIBUTES (decl));
677 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
678 node->no_reorder = 1;
681 /* Look for externally_visible and used attributes and mark cgraph nodes
682 accordingly.
684 We cannot mark the nodes at the point the attributes are processed (in
685 handle_*_attribute) because the copy of the declarations available at that
686 point may not be canonical. For example, in:
688 void f();
689 void f() __attribute__((used));
691 the declaration we see in handle_used_attribute will be the second
692 declaration -- but the front end will subsequently merge that declaration
693 with the original declaration and discard the second declaration.
695 Furthermore, we can't mark these nodes in finalize_function because:
697 void f() {}
698 void f() __attribute__((externally_visible));
700 is valid.
702 So, we walk the nodes at the end of the translation unit, applying the
703 attributes at that point. */
705 static void
706 process_function_and_variable_attributes (cgraph_node *first,
707 varpool_node *first_var)
709 cgraph_node *node;
710 varpool_node *vnode;
712 for (node = symtab->first_function (); node != first;
713 node = symtab->next_function (node))
715 tree decl = node->decl;
716 if (DECL_PRESERVE_P (decl))
717 node->mark_force_output ();
718 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
720 if (! TREE_PUBLIC (node->decl))
721 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
722 "%<externally_visible%>"
723 " attribute have effect only on public objects");
725 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
726 && (node->definition && !node->alias))
728 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
729 "%<weakref%> attribute ignored"
730 " because function is defined");
731 DECL_WEAK (decl) = 0;
732 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
733 DECL_ATTRIBUTES (decl));
736 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
737 && !DECL_DECLARED_INLINE_P (decl)
738 /* redefining extern inline function makes it DECL_UNINLINABLE. */
739 && !DECL_UNINLINABLE (decl))
740 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
741 "always_inline function might not be inlinable");
743 process_common_attributes (node, decl);
745 for (vnode = symtab->first_variable (); vnode != first_var;
746 vnode = symtab->next_variable (vnode))
748 tree decl = vnode->decl;
749 if (DECL_EXTERNAL (decl)
750 && DECL_INITIAL (decl))
751 varpool_node::finalize_decl (decl);
752 if (DECL_PRESERVE_P (decl))
753 vnode->force_output = true;
754 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
756 if (! TREE_PUBLIC (vnode->decl))
757 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
758 "%<externally_visible%>"
759 " attribute have effect only on public objects");
761 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
762 && vnode->definition
763 && DECL_INITIAL (decl))
765 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
766 "%<weakref%> attribute ignored"
767 " because variable is initialized");
768 DECL_WEAK (decl) = 0;
769 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
770 DECL_ATTRIBUTES (decl));
772 process_common_attributes (vnode, decl);
776 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
777 middle end to output the variable to asm file, if needed or externally
778 visible. */
780 void
781 varpool_node::finalize_decl (tree decl)
783 varpool_node *node = varpool_node::get_create (decl);
785 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
787 if (node->definition)
788 return;
789 notice_global_symbol (decl);
790 node->definition = true;
791 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
792 /* Traditionally we do not eliminate static variables when not
793 optimizing and when not doing toplevel reoder. */
794 || node->no_reorder
795 || ((!flag_toplevel_reorder
796 && !DECL_COMDAT (node->decl)
797 && !DECL_ARTIFICIAL (node->decl))))
798 node->force_output = true;
800 if (symtab->state == CONSTRUCTION
801 && (node->needed_p () || node->referred_to_p ()))
802 enqueue_node (node);
803 if (symtab->state >= IPA_SSA)
804 node->analyze ();
805 /* Some frontends produce various interface variables after compilation
806 finished. */
807 if (symtab->state == FINISHED
808 || (!flag_toplevel_reorder
809 && symtab->state == EXPANSION))
810 node->assemble_decl ();
812 if (DECL_INITIAL (decl))
813 chkp_register_var_initializer (decl);
816 /* EDGE is an polymorphic call. Mark all possible targets as reachable
817 and if there is only one target, perform trivial devirtualization.
818 REACHABLE_CALL_TARGETS collects target lists we already walked to
819 avoid udplicate work. */
821 static void
822 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
823 cgraph_edge *edge)
825 unsigned int i;
826 void *cache_token;
827 bool final;
828 vec <cgraph_node *>targets
829 = possible_polymorphic_call_targets
830 (edge, &final, &cache_token);
832 if (!reachable_call_targets->add (cache_token))
834 if (symtab->dump_file)
835 dump_possible_polymorphic_call_targets
836 (symtab->dump_file, edge);
838 for (i = 0; i < targets.length (); i++)
840 /* Do not bother to mark virtual methods in anonymous namespace;
841 either we will find use of virtual table defining it, or it is
842 unused. */
843 if (targets[i]->definition
844 && TREE_CODE
845 (TREE_TYPE (targets[i]->decl))
846 == METHOD_TYPE
847 && !type_in_anonymous_namespace_p
848 (method_class_type
849 (TREE_TYPE (targets[i]->decl))))
850 enqueue_node (targets[i]);
854 /* Very trivial devirtualization; when the type is
855 final or anonymous (so we know all its derivation)
856 and there is only one possible virtual call target,
857 make the edge direct. */
858 if (final)
860 if (targets.length () <= 1 && dbg_cnt (devirt))
862 cgraph_node *target;
863 if (targets.length () == 1)
864 target = targets[0];
865 else
866 target = cgraph_node::create
867 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
869 if (symtab->dump_file)
871 fprintf (symtab->dump_file,
872 "Devirtualizing call: ");
873 print_gimple_stmt (symtab->dump_file,
874 edge->call_stmt, 0,
875 TDF_SLIM);
877 if (dump_enabled_p ())
879 location_t locus = gimple_location_safe (edge->call_stmt);
880 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
881 "devirtualizing call in %s to %s\n",
882 edge->caller->name (), target->name ());
885 edge->make_direct (target);
886 edge->redirect_call_stmt_to_callee ();
888 /* Call to __builtin_unreachable shouldn't be instrumented. */
889 if (!targets.length ())
890 gimple_call_set_with_bounds (edge->call_stmt, false);
892 if (symtab->dump_file)
894 fprintf (symtab->dump_file,
895 "Devirtualized as: ");
896 print_gimple_stmt (symtab->dump_file,
897 edge->call_stmt, 0,
898 TDF_SLIM);
905 /* Discover all functions and variables that are trivially needed, analyze
906 them as well as all functions and variables referred by them */
907 static cgraph_node *first_analyzed;
908 static varpool_node *first_analyzed_var;
910 static void
911 analyze_functions (void)
913 /* Keep track of already processed nodes when called multiple times for
914 intermodule optimization. */
915 cgraph_node *first_handled = first_analyzed;
916 varpool_node *first_handled_var = first_analyzed_var;
917 hash_set<void *> reachable_call_targets;
919 symtab_node *node;
920 symtab_node *next;
921 int i;
922 ipa_ref *ref;
923 bool changed = true;
924 location_t saved_loc = input_location;
926 bitmap_obstack_initialize (NULL);
927 symtab->state = CONSTRUCTION;
928 input_location = UNKNOWN_LOCATION;
930 /* Ugly, but the fixup can not happen at a time same body alias is created;
931 C++ FE is confused about the COMDAT groups being right. */
932 if (symtab->cpp_implicit_aliases_done)
933 FOR_EACH_SYMBOL (node)
934 if (node->cpp_implicit_alias)
935 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
936 build_type_inheritance_graph ();
938 /* Analysis adds static variables that in turn adds references to new functions.
939 So we need to iterate the process until it stabilize. */
940 while (changed)
942 changed = false;
943 process_function_and_variable_attributes (first_analyzed,
944 first_analyzed_var);
946 /* First identify the trivially needed symbols. */
947 for (node = symtab->first_symbol ();
948 node != first_analyzed
949 && node != first_analyzed_var; node = node->next)
951 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
952 node->get_comdat_group_id ();
953 if (node->needed_p ())
955 enqueue_node (node);
956 if (!changed && symtab->dump_file)
957 fprintf (symtab->dump_file, "Trivially needed symbols:");
958 changed = true;
959 if (symtab->dump_file)
960 fprintf (symtab->dump_file, " %s", node->asm_name ());
961 if (!changed && symtab->dump_file)
962 fprintf (symtab->dump_file, "\n");
964 if (node == first_analyzed
965 || node == first_analyzed_var)
966 break;
968 symtab->process_new_functions ();
969 first_analyzed_var = symtab->first_variable ();
970 first_analyzed = symtab->first_function ();
972 if (changed && symtab->dump_file)
973 fprintf (symtab->dump_file, "\n");
975 /* Lower representation, build callgraph edges and references for all trivially
976 needed symbols and all symbols referred by them. */
977 while (queued_nodes != &symtab_terminator)
979 changed = true;
980 node = queued_nodes;
981 queued_nodes = (symtab_node *)queued_nodes->aux;
982 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
983 if (cnode && cnode->definition)
985 cgraph_edge *edge;
986 tree decl = cnode->decl;
988 /* ??? It is possible to create extern inline function
989 and later using weak alias attribute to kill its body.
990 See gcc.c-torture/compile/20011119-1.c */
991 if (!DECL_STRUCT_FUNCTION (decl)
992 && !cnode->alias
993 && !cnode->thunk.thunk_p
994 && !cnode->dispatcher_function)
996 cnode->reset ();
997 cnode->local.redefined_extern_inline = true;
998 continue;
1001 if (!cnode->analyzed)
1002 cnode->analyze ();
1004 for (edge = cnode->callees; edge; edge = edge->next_callee)
1005 if (edge->callee->definition
1006 && (!DECL_EXTERNAL (edge->callee->decl)
1007 /* When not optimizing, do not try to analyze extern
1008 inline functions. Doing so is pointless. */
1009 || opt_for_fn (edge->callee->decl, optimize)
1010 /* Weakrefs needs to be preserved. */
1011 || edge->callee->alias
1012 /* always_inline functions are inlined aven at -O0. */
1013 || lookup_attribute
1014 ("always_inline",
1015 DECL_ATTRIBUTES (edge->callee->decl))
1016 /* Multiversioned functions needs the dispatcher to
1017 be produced locally even for extern functions. */
1018 || edge->callee->function_version ()))
1019 enqueue_node (edge->callee);
1020 if (opt_for_fn (cnode->decl, optimize)
1021 && opt_for_fn (cnode->decl, flag_devirtualize))
1023 cgraph_edge *next;
1025 for (edge = cnode->indirect_calls; edge; edge = next)
1027 next = edge->next_callee;
1028 if (edge->indirect_info->polymorphic)
1029 walk_polymorphic_call_targets (&reachable_call_targets,
1030 edge);
1034 /* If decl is a clone of an abstract function,
1035 mark that abstract function so that we don't release its body.
1036 The DECL_INITIAL() of that abstract function declaration
1037 will be later needed to output debug info. */
1038 if (DECL_ABSTRACT_ORIGIN (decl))
1040 cgraph_node *origin_node
1041 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1042 origin_node->used_as_abstract_origin = true;
1045 else
1047 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1048 if (vnode && vnode->definition && !vnode->analyzed)
1049 vnode->analyze ();
1052 if (node->same_comdat_group)
1054 symtab_node *next;
1055 for (next = node->same_comdat_group;
1056 next != node;
1057 next = next->same_comdat_group)
1058 if (!next->comdat_local_p ())
1059 enqueue_node (next);
1061 for (i = 0; node->iterate_reference (i, ref); i++)
1062 if (ref->referred->definition
1063 && (!DECL_EXTERNAL (ref->referred->decl)
1064 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1065 && optimize)
1066 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1067 && opt_for_fn (ref->referred->decl, optimize))
1068 || node->alias
1069 || ref->referred->alias)))
1070 enqueue_node (ref->referred);
1071 symtab->process_new_functions ();
1074 update_type_inheritance_graph ();
1076 /* Collect entry points to the unit. */
1077 if (symtab->dump_file)
1079 fprintf (symtab->dump_file, "\n\nInitial ");
1080 symtab_node::dump_table (symtab->dump_file);
1083 if (symtab->dump_file)
1084 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1086 for (node = symtab->first_symbol ();
1087 node != first_handled
1088 && node != first_handled_var; node = next)
1090 next = node->next;
1091 if (!node->aux && !node->referred_to_p ())
1093 if (symtab->dump_file)
1094 fprintf (symtab->dump_file, " %s", node->name ());
1095 node->remove ();
1096 continue;
1098 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1100 tree decl = node->decl;
1102 if (cnode->definition && !gimple_has_body_p (decl)
1103 && !cnode->alias
1104 && !cnode->thunk.thunk_p)
1105 cnode->reset ();
1107 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1108 || cnode->alias
1109 || gimple_has_body_p (decl));
1110 gcc_assert (cnode->analyzed == cnode->definition);
1112 node->aux = NULL;
1114 for (;node; node = node->next)
1115 node->aux = NULL;
1116 first_analyzed = symtab->first_function ();
1117 first_analyzed_var = symtab->first_variable ();
1118 if (symtab->dump_file)
1120 fprintf (symtab->dump_file, "\n\nReclaimed ");
1121 symtab_node::dump_table (symtab->dump_file);
1123 bitmap_obstack_release (NULL);
1124 ggc_collect ();
1125 /* Initialize assembler name hash, in particular we want to trigger C++
1126 mangling and same body alias creation before we free DECL_ARGUMENTS
1127 used by it. */
1128 if (!seen_error ())
1129 symtab->symtab_initialize_asm_name_hash ();
1131 input_location = saved_loc;
1134 /* Translate the ugly representation of aliases as alias pairs into nice
1135 representation in callgraph. We don't handle all cases yet,
1136 unfortunately. */
1138 static void
1139 handle_alias_pairs (void)
1141 alias_pair *p;
1142 unsigned i;
1144 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1146 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1148 /* Weakrefs with target not defined in current unit are easy to handle:
1149 they behave just as external variables except we need to note the
1150 alias flag to later output the weakref pseudo op into asm file. */
1151 if (!target_node
1152 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1154 symtab_node *node = symtab_node::get (p->decl);
1155 if (node)
1157 node->alias_target = p->target;
1158 node->weakref = true;
1159 node->alias = true;
1161 alias_pairs->unordered_remove (i);
1162 continue;
1164 else if (!target_node)
1166 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1167 symtab_node *node = symtab_node::get (p->decl);
1168 if (node)
1169 node->alias = false;
1170 alias_pairs->unordered_remove (i);
1171 continue;
1174 if (DECL_EXTERNAL (target_node->decl)
1175 /* We use local aliases for C++ thunks to force the tailcall
1176 to bind locally. This is a hack - to keep it working do
1177 the following (which is not strictly correct). */
1178 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1179 || ! DECL_VIRTUAL_P (target_node->decl))
1180 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1182 error ("%q+D aliased to external symbol %qE",
1183 p->decl, p->target);
1186 if (TREE_CODE (p->decl) == FUNCTION_DECL
1187 && target_node && is_a <cgraph_node *> (target_node))
1189 cgraph_node *src_node = cgraph_node::get (p->decl);
1190 if (src_node && src_node->definition)
1191 src_node->reset ();
1192 cgraph_node::create_alias (p->decl, target_node->decl);
1193 alias_pairs->unordered_remove (i);
1195 else if (TREE_CODE (p->decl) == VAR_DECL
1196 && target_node && is_a <varpool_node *> (target_node))
1198 varpool_node::create_alias (p->decl, target_node->decl);
1199 alias_pairs->unordered_remove (i);
1201 else
1203 error ("%q+D alias in between function and variable is not supported",
1204 p->decl);
1205 warning (0, "%q+D aliased declaration",
1206 target_node->decl);
1207 alias_pairs->unordered_remove (i);
1210 vec_free (alias_pairs);
1214 /* Figure out what functions we want to assemble. */
1216 static void
1217 mark_functions_to_output (void)
1219 cgraph_node *node;
1220 #ifdef ENABLE_CHECKING
1221 bool check_same_comdat_groups = false;
1223 FOR_EACH_FUNCTION (node)
1224 gcc_assert (!node->process);
1225 #endif
1227 FOR_EACH_FUNCTION (node)
1229 tree decl = node->decl;
1231 gcc_assert (!node->process || node->same_comdat_group);
1232 if (node->process)
1233 continue;
1235 /* We need to output all local functions that are used and not
1236 always inlined, as well as those that are reachable from
1237 outside the current compilation unit. */
1238 if (node->analyzed
1239 && !node->thunk.thunk_p
1240 && !node->alias
1241 && !node->global.inlined_to
1242 && !TREE_ASM_WRITTEN (decl)
1243 && !DECL_EXTERNAL (decl))
1245 node->process = 1;
1246 if (node->same_comdat_group)
1248 cgraph_node *next;
1249 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1250 next != node;
1251 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1252 if (!next->thunk.thunk_p && !next->alias
1253 && !next->comdat_local_p ())
1254 next->process = 1;
1257 else if (node->same_comdat_group)
1259 #ifdef ENABLE_CHECKING
1260 check_same_comdat_groups = true;
1261 #endif
1263 else
1265 /* We should've reclaimed all functions that are not needed. */
1266 #ifdef ENABLE_CHECKING
1267 if (!node->global.inlined_to
1268 && gimple_has_body_p (decl)
1269 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1270 are inside partition, we can end up not removing the body since we no longer
1271 have analyzed node pointing to it. */
1272 && !node->in_other_partition
1273 && !node->alias
1274 && !node->clones
1275 && !DECL_EXTERNAL (decl))
1277 node->debug ();
1278 internal_error ("failed to reclaim unneeded function");
1280 #endif
1281 gcc_assert (node->global.inlined_to
1282 || !gimple_has_body_p (decl)
1283 || node->in_other_partition
1284 || node->clones
1285 || DECL_ARTIFICIAL (decl)
1286 || DECL_EXTERNAL (decl));
1291 #ifdef ENABLE_CHECKING
1292 if (check_same_comdat_groups)
1293 FOR_EACH_FUNCTION (node)
1294 if (node->same_comdat_group && !node->process)
1296 tree decl = node->decl;
1297 if (!node->global.inlined_to
1298 && gimple_has_body_p (decl)
1299 /* FIXME: in an ltrans unit when the offline copy is outside a
1300 partition but inline copies are inside a partition, we can
1301 end up not removing the body since we no longer have an
1302 analyzed node pointing to it. */
1303 && !node->in_other_partition
1304 && !node->clones
1305 && !DECL_EXTERNAL (decl))
1307 node->debug ();
1308 internal_error ("failed to reclaim unneeded function in same "
1309 "comdat group");
1312 #endif
1315 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1316 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1318 Set current_function_decl and cfun to newly constructed empty function body.
1319 return basic block in the function body. */
1321 basic_block
1322 init_lowered_empty_function (tree decl, bool in_ssa)
1324 basic_block bb;
1326 current_function_decl = decl;
1327 allocate_struct_function (decl, false);
1328 gimple_register_cfg_hooks ();
1329 init_empty_tree_cfg ();
1331 if (in_ssa)
1333 init_tree_ssa (cfun);
1334 init_ssa_operands (cfun);
1335 cfun->gimple_df->in_ssa_p = true;
1336 cfun->curr_properties |= PROP_ssa;
1339 DECL_INITIAL (decl) = make_node (BLOCK);
1341 DECL_SAVED_TREE (decl) = error_mark_node;
1342 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1343 | PROP_cfg | PROP_loops);
1345 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1346 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1347 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1349 /* Create BB for body of the function and connect it properly. */
1350 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1351 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1352 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1353 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1355 return bb;
1358 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1359 offset indicated by VIRTUAL_OFFSET, if that is
1360 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1361 zero for a result adjusting thunk. */
1363 static tree
1364 thunk_adjust (gimple_stmt_iterator * bsi,
1365 tree ptr, bool this_adjusting,
1366 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1368 gassign *stmt;
1369 tree ret;
1371 if (this_adjusting
1372 && fixed_offset != 0)
1374 stmt = gimple_build_assign
1375 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1376 ptr,
1377 fixed_offset));
1378 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1381 /* If there's a virtual offset, look up that value in the vtable and
1382 adjust the pointer again. */
1383 if (virtual_offset)
1385 tree vtabletmp;
1386 tree vtabletmp2;
1387 tree vtabletmp3;
1389 if (!vtable_entry_type)
1391 tree vfunc_type = make_node (FUNCTION_TYPE);
1392 TREE_TYPE (vfunc_type) = integer_type_node;
1393 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1394 layout_type (vfunc_type);
1396 vtable_entry_type = build_pointer_type (vfunc_type);
1399 vtabletmp =
1400 create_tmp_reg (build_pointer_type
1401 (build_pointer_type (vtable_entry_type)), "vptr");
1403 /* The vptr is always at offset zero in the object. */
1404 stmt = gimple_build_assign (vtabletmp,
1405 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1406 ptr));
1407 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1409 /* Form the vtable address. */
1410 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1411 "vtableaddr");
1412 stmt = gimple_build_assign (vtabletmp2,
1413 build_simple_mem_ref (vtabletmp));
1414 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1416 /* Find the entry with the vcall offset. */
1417 stmt = gimple_build_assign (vtabletmp2,
1418 fold_build_pointer_plus_loc (input_location,
1419 vtabletmp2,
1420 virtual_offset));
1421 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1423 /* Get the offset itself. */
1424 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1425 "vcalloffset");
1426 stmt = gimple_build_assign (vtabletmp3,
1427 build_simple_mem_ref (vtabletmp2));
1428 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1430 /* Adjust the `this' pointer. */
1431 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1432 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1433 GSI_CONTINUE_LINKING);
1436 if (!this_adjusting
1437 && fixed_offset != 0)
1438 /* Adjust the pointer by the constant. */
1440 tree ptrtmp;
1442 if (TREE_CODE (ptr) == VAR_DECL)
1443 ptrtmp = ptr;
1444 else
1446 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1447 stmt = gimple_build_assign (ptrtmp, ptr);
1448 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1450 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1451 ptrtmp, fixed_offset);
1454 /* Emit the statement and gimplify the adjustment expression. */
1455 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1456 stmt = gimple_build_assign (ret, ptr);
1457 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1459 return ret;
1462 /* Expand thunk NODE to gimple if possible.
1463 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1464 no assembler is produced.
1465 When OUTPUT_ASM_THUNK is true, also produce assembler for
1466 thunks that are not lowered. */
1468 bool
1469 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1471 bool this_adjusting = thunk.this_adjusting;
1472 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1473 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1474 tree virtual_offset = NULL;
1475 tree alias = callees->callee->decl;
1476 tree thunk_fndecl = decl;
1477 tree a;
1480 if (!force_gimple_thunk && this_adjusting
1481 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1482 virtual_value, alias))
1484 const char *fnname;
1485 tree fn_block;
1486 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1488 if (!output_asm_thunks)
1490 analyzed = true;
1491 return false;
1494 if (in_lto_p)
1495 get_untransformed_body ();
1496 a = DECL_ARGUMENTS (thunk_fndecl);
1498 current_function_decl = thunk_fndecl;
1500 /* Ensure thunks are emitted in their correct sections. */
1501 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1503 DECL_RESULT (thunk_fndecl)
1504 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1505 RESULT_DECL, 0, restype);
1506 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1507 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1509 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1510 create one. */
1511 fn_block = make_node (BLOCK);
1512 BLOCK_VARS (fn_block) = a;
1513 DECL_INITIAL (thunk_fndecl) = fn_block;
1514 init_function_start (thunk_fndecl);
1515 cfun->is_thunk = 1;
1516 insn_locations_init ();
1517 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1518 prologue_location = curr_insn_location ();
1519 assemble_start_function (thunk_fndecl, fnname);
1521 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1522 fixed_offset, virtual_value, alias);
1524 assemble_end_function (thunk_fndecl, fnname);
1525 insn_locations_finalize ();
1526 init_insn_lengths ();
1527 free_after_compilation (cfun);
1528 set_cfun (NULL);
1529 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1530 thunk.thunk_p = false;
1531 analyzed = false;
1533 else
1535 tree restype;
1536 basic_block bb, then_bb, else_bb, return_bb;
1537 gimple_stmt_iterator bsi;
1538 int nargs = 0;
1539 tree arg;
1540 int i;
1541 tree resdecl;
1542 tree restmp = NULL;
1544 gcall *call;
1545 greturn *ret;
1547 if (in_lto_p)
1548 get_untransformed_body ();
1549 a = DECL_ARGUMENTS (thunk_fndecl);
1551 current_function_decl = thunk_fndecl;
1553 /* Ensure thunks are emitted in their correct sections. */
1554 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1556 DECL_IGNORED_P (thunk_fndecl) = 1;
1557 bitmap_obstack_initialize (NULL);
1559 if (thunk.virtual_offset_p)
1560 virtual_offset = size_int (virtual_value);
1562 /* Build the return declaration for the function. */
1563 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1564 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1566 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1567 DECL_ARTIFICIAL (resdecl) = 1;
1568 DECL_IGNORED_P (resdecl) = 1;
1569 DECL_RESULT (thunk_fndecl) = resdecl;
1570 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1572 else
1573 resdecl = DECL_RESULT (thunk_fndecl);
1575 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1577 bsi = gsi_start_bb (bb);
1579 /* Build call to the function being thunked. */
1580 if (!VOID_TYPE_P (restype))
1582 if (DECL_BY_REFERENCE (resdecl))
1584 restmp = gimple_fold_indirect_ref (resdecl);
1585 if (!restmp)
1586 restmp = build2 (MEM_REF,
1587 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1588 resdecl,
1589 build_int_cst (TREE_TYPE
1590 (DECL_RESULT (alias)), 0));
1592 else if (!is_gimple_reg_type (restype))
1594 restmp = resdecl;
1596 if (TREE_CODE (restmp) == VAR_DECL)
1597 add_local_decl (cfun, restmp);
1598 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1600 else
1601 restmp = create_tmp_reg (restype, "retval");
1604 for (arg = a; arg; arg = DECL_CHAIN (arg))
1605 nargs++;
1606 auto_vec<tree> vargs (nargs);
1607 if (this_adjusting)
1608 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1609 virtual_offset));
1610 else if (nargs)
1611 vargs.quick_push (a);
1613 if (nargs)
1614 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1616 tree tmp = arg;
1617 if (!is_gimple_val (arg))
1619 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1620 (TREE_TYPE (arg)), "arg");
1621 gimple stmt = gimple_build_assign (tmp, arg);
1622 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1624 vargs.quick_push (tmp);
1626 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1627 callees->call_stmt = call;
1628 gimple_call_set_from_thunk (call, true);
1629 gimple_call_set_with_bounds (call, instrumentation_clone);
1630 if (restmp)
1632 gimple_call_set_lhs (call, restmp);
1633 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1634 TREE_TYPE (TREE_TYPE (alias))));
1636 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1637 if (!(gimple_call_flags (call) & ECF_NORETURN))
1639 if (restmp && !this_adjusting
1640 && (fixed_offset || virtual_offset))
1642 tree true_label = NULL_TREE;
1644 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1646 gimple stmt;
1647 /* If the return type is a pointer, we need to
1648 protect against NULL. We know there will be an
1649 adjustment, because that's why we're emitting a
1650 thunk. */
1651 then_bb = create_basic_block (NULL, (void *) 0, bb);
1652 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1653 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1654 add_bb_to_loop (then_bb, bb->loop_father);
1655 add_bb_to_loop (return_bb, bb->loop_father);
1656 add_bb_to_loop (else_bb, bb->loop_father);
1657 remove_edge (single_succ_edge (bb));
1658 true_label = gimple_block_label (then_bb);
1659 stmt = gimple_build_cond (NE_EXPR, restmp,
1660 build_zero_cst (TREE_TYPE (restmp)),
1661 NULL_TREE, NULL_TREE);
1662 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1663 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1664 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1665 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1666 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1667 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1668 bsi = gsi_last_bb (then_bb);
1671 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1672 fixed_offset, virtual_offset);
1673 if (true_label)
1675 gimple stmt;
1676 bsi = gsi_last_bb (else_bb);
1677 stmt = gimple_build_assign (restmp,
1678 build_zero_cst (TREE_TYPE (restmp)));
1679 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1680 bsi = gsi_last_bb (return_bb);
1683 else
1684 gimple_call_set_tail (call, true);
1686 /* Build return value. */
1687 if (!DECL_BY_REFERENCE (resdecl))
1688 ret = gimple_build_return (restmp);
1689 else
1690 ret = gimple_build_return (resdecl);
1692 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1694 else
1696 gimple_call_set_tail (call, true);
1697 remove_edge (single_succ_edge (bb));
1700 cfun->gimple_df->in_ssa_p = true;
1701 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1702 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1703 delete_unreachable_blocks ();
1704 update_ssa (TODO_update_ssa);
1705 #ifdef ENABLE_CHECKING
1706 verify_flow_info ();
1707 #endif
1708 free_dominance_info (CDI_DOMINATORS);
1710 /* Since we want to emit the thunk, we explicitly mark its name as
1711 referenced. */
1712 thunk.thunk_p = false;
1713 lowered = true;
1714 bitmap_obstack_release (NULL);
1716 current_function_decl = NULL;
1717 set_cfun (NULL);
1718 return true;
1721 /* Assemble thunks and aliases associated to node. */
1723 void
1724 cgraph_node::assemble_thunks_and_aliases (void)
1726 cgraph_edge *e;
1727 ipa_ref *ref;
1729 for (e = callers; e;)
1730 if (e->caller->thunk.thunk_p
1731 && !e->caller->thunk.add_pointer_bounds_args)
1733 cgraph_node *thunk = e->caller;
1735 e = e->next_caller;
1736 thunk->expand_thunk (true, false);
1737 thunk->assemble_thunks_and_aliases ();
1739 else
1740 e = e->next_caller;
1742 FOR_EACH_ALIAS (this, ref)
1744 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1745 bool saved_written = TREE_ASM_WRITTEN (decl);
1747 /* Force assemble_alias to really output the alias this time instead
1748 of buffering it in same alias pairs. */
1749 TREE_ASM_WRITTEN (decl) = 1;
1750 do_assemble_alias (alias->decl,
1751 DECL_ASSEMBLER_NAME (decl));
1752 alias->assemble_thunks_and_aliases ();
1753 TREE_ASM_WRITTEN (decl) = saved_written;
1757 /* Expand function specified by node. */
1759 void
1760 cgraph_node::expand (void)
1762 location_t saved_loc;
1764 /* We ought to not compile any inline clones. */
1765 gcc_assert (!global.inlined_to);
1767 announce_function (decl);
1768 process = 0;
1769 gcc_assert (lowered);
1770 get_untransformed_body ();
1772 /* Generate RTL for the body of DECL. */
1774 timevar_push (TV_REST_OF_COMPILATION);
1776 gcc_assert (symtab->global_info_ready);
1778 /* Initialize the default bitmap obstack. */
1779 bitmap_obstack_initialize (NULL);
1781 /* Initialize the RTL code for the function. */
1782 current_function_decl = decl;
1783 saved_loc = input_location;
1784 input_location = DECL_SOURCE_LOCATION (decl);
1785 init_function_start (decl);
1787 gimple_register_cfg_hooks ();
1789 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1791 execute_all_ipa_transforms ();
1793 /* Perform all tree transforms and optimizations. */
1795 /* Signal the start of passes. */
1796 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1798 execute_pass_list (cfun, g->get_passes ()->all_passes);
1800 /* Signal the end of passes. */
1801 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1803 bitmap_obstack_release (&reg_obstack);
1805 /* Release the default bitmap obstack. */
1806 bitmap_obstack_release (NULL);
1808 /* If requested, warn about function definitions where the function will
1809 return a value (usually of some struct or union type) which itself will
1810 take up a lot of stack space. */
1811 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1813 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1815 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1816 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1817 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1818 larger_than_size))
1820 unsigned int size_as_int
1821 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1823 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1824 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1825 decl, size_as_int);
1826 else
1827 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1828 decl, larger_than_size);
1832 gimple_set_body (decl, NULL);
1833 if (DECL_STRUCT_FUNCTION (decl) == 0
1834 && !cgraph_node::get (decl)->origin)
1836 /* Stop pointing to the local nodes about to be freed.
1837 But DECL_INITIAL must remain nonzero so we know this
1838 was an actual function definition.
1839 For a nested function, this is done in c_pop_function_context.
1840 If rest_of_compilation set this to 0, leave it 0. */
1841 if (DECL_INITIAL (decl) != 0)
1842 DECL_INITIAL (decl) = error_mark_node;
1845 input_location = saved_loc;
1847 ggc_collect ();
1848 timevar_pop (TV_REST_OF_COMPILATION);
1850 /* Make sure that BE didn't give up on compiling. */
1851 gcc_assert (TREE_ASM_WRITTEN (decl));
1852 set_cfun (NULL);
1853 current_function_decl = NULL;
1855 /* It would make a lot more sense to output thunks before function body to get more
1856 forward and lest backwarding jumps. This however would need solving problem
1857 with comdats. See PR48668. Also aliases must come after function itself to
1858 make one pass assemblers, like one on AIX, happy. See PR 50689.
1859 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1860 groups. */
1861 assemble_thunks_and_aliases ();
1862 release_body ();
1863 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1864 points to the dead function body. */
1865 remove_callees ();
1866 remove_all_references ();
1869 /* Node comparer that is responsible for the order that corresponds
1870 to time when a function was launched for the first time. */
1872 static int
1873 node_cmp (const void *pa, const void *pb)
1875 const cgraph_node *a = *(const cgraph_node * const *) pa;
1876 const cgraph_node *b = *(const cgraph_node * const *) pb;
1878 /* Functions with time profile must be before these without profile. */
1879 if (!a->tp_first_run || !b->tp_first_run)
1880 return a->tp_first_run - b->tp_first_run;
1882 return a->tp_first_run != b->tp_first_run
1883 ? b->tp_first_run - a->tp_first_run
1884 : b->order - a->order;
1887 /* Expand all functions that must be output.
1889 Attempt to topologically sort the nodes so function is output when
1890 all called functions are already assembled to allow data to be
1891 propagated across the callgraph. Use a stack to get smaller distance
1892 between a function and its callees (later we may choose to use a more
1893 sophisticated algorithm for function reordering; we will likely want
1894 to use subsections to make the output functions appear in top-down
1895 order). */
1897 static void
1898 expand_all_functions (void)
1900 cgraph_node *node;
1901 cgraph_node **order = XCNEWVEC (cgraph_node *,
1902 symtab->cgraph_count);
1903 unsigned int expanded_func_count = 0, profiled_func_count = 0;
1904 int order_pos, new_order_pos = 0;
1905 int i;
1907 order_pos = ipa_reverse_postorder (order);
1908 gcc_assert (order_pos == symtab->cgraph_count);
1910 /* Garbage collector may remove inline clones we eliminate during
1911 optimization. So we must be sure to not reference them. */
1912 for (i = 0; i < order_pos; i++)
1913 if (order[i]->process)
1914 order[new_order_pos++] = order[i];
1916 if (flag_profile_reorder_functions)
1917 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
1919 for (i = new_order_pos - 1; i >= 0; i--)
1921 node = order[i];
1923 if (node->process)
1925 expanded_func_count++;
1926 if(node->tp_first_run)
1927 profiled_func_count++;
1929 if (symtab->dump_file)
1930 fprintf (symtab->dump_file,
1931 "Time profile order in expand_all_functions:%s:%d\n",
1932 node->asm_name (), node->tp_first_run);
1933 node->process = 0;
1934 node->expand ();
1938 if (dump_file)
1939 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1940 main_input_filename, profiled_func_count, expanded_func_count);
1942 if (symtab->dump_file && flag_profile_reorder_functions)
1943 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
1944 profiled_func_count, expanded_func_count);
1946 symtab->process_new_functions ();
1947 free_gimplify_stack ();
1949 free (order);
1952 /* This is used to sort the node types by the cgraph order number. */
1954 enum cgraph_order_sort_kind
1956 ORDER_UNDEFINED = 0,
1957 ORDER_FUNCTION,
1958 ORDER_VAR,
1959 ORDER_ASM
1962 struct cgraph_order_sort
1964 enum cgraph_order_sort_kind kind;
1965 union
1967 cgraph_node *f;
1968 varpool_node *v;
1969 asm_node *a;
1970 } u;
1973 /* Output all functions, variables, and asm statements in the order
1974 according to their order fields, which is the order in which they
1975 appeared in the file. This implements -fno-toplevel-reorder. In
1976 this mode we may output functions and variables which don't really
1977 need to be output.
1978 When NO_REORDER is true only do this for symbols marked no reorder. */
1980 static void
1981 output_in_order (bool no_reorder)
1983 int max;
1984 cgraph_order_sort *nodes;
1985 int i;
1986 cgraph_node *pf;
1987 varpool_node *pv;
1988 asm_node *pa;
1989 max = symtab->order;
1990 nodes = XCNEWVEC (cgraph_order_sort, max);
1992 FOR_EACH_DEFINED_FUNCTION (pf)
1994 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1996 if (no_reorder && !pf->no_reorder)
1997 continue;
1998 i = pf->order;
1999 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2000 nodes[i].kind = ORDER_FUNCTION;
2001 nodes[i].u.f = pf;
2005 FOR_EACH_DEFINED_VARIABLE (pv)
2006 if (!DECL_EXTERNAL (pv->decl))
2008 if (no_reorder && !pv->no_reorder)
2009 continue;
2010 i = pv->order;
2011 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2012 nodes[i].kind = ORDER_VAR;
2013 nodes[i].u.v = pv;
2016 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2018 i = pa->order;
2019 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2020 nodes[i].kind = ORDER_ASM;
2021 nodes[i].u.a = pa;
2024 /* In toplevel reorder mode we output all statics; mark them as needed. */
2026 for (i = 0; i < max; ++i)
2027 if (nodes[i].kind == ORDER_VAR)
2028 nodes[i].u.v->finalize_named_section_flags ();
2030 for (i = 0; i < max; ++i)
2032 switch (nodes[i].kind)
2034 case ORDER_FUNCTION:
2035 nodes[i].u.f->process = 0;
2036 nodes[i].u.f->expand ();
2037 break;
2039 case ORDER_VAR:
2040 nodes[i].u.v->assemble_decl ();
2041 break;
2043 case ORDER_ASM:
2044 assemble_asm (nodes[i].u.a->asm_str);
2045 break;
2047 case ORDER_UNDEFINED:
2048 break;
2050 default:
2051 gcc_unreachable ();
2055 symtab->clear_asm_symbols ();
2057 free (nodes);
2060 static void
2061 ipa_passes (void)
2063 gcc::pass_manager *passes = g->get_passes ();
2065 set_cfun (NULL);
2066 current_function_decl = NULL;
2067 gimple_register_cfg_hooks ();
2068 bitmap_obstack_initialize (NULL);
2070 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2072 if (!in_lto_p)
2074 execute_ipa_pass_list (passes->all_small_ipa_passes);
2075 if (seen_error ())
2076 return;
2079 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2080 devirtualization and other changes where removal iterate. */
2081 symtab->remove_unreachable_nodes (symtab->dump_file);
2083 /* If pass_all_early_optimizations was not scheduled, the state of
2084 the cgraph will not be properly updated. Update it now. */
2085 if (symtab->state < IPA_SSA)
2086 symtab->state = IPA_SSA;
2088 if (!in_lto_p)
2090 /* Generate coverage variables and constructors. */
2091 coverage_finish ();
2093 /* Process new functions added. */
2094 set_cfun (NULL);
2095 current_function_decl = NULL;
2096 symtab->process_new_functions ();
2098 execute_ipa_summary_passes
2099 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2102 /* Some targets need to handle LTO assembler output specially. */
2103 if (flag_generate_lto || flag_generate_offload)
2104 targetm.asm_out.lto_start ();
2106 if (!in_lto_p)
2108 if (g->have_offload)
2110 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2111 ipa_write_summaries (true);
2113 if (flag_lto)
2115 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2116 ipa_write_summaries (false);
2120 if (flag_generate_lto || flag_generate_offload)
2121 targetm.asm_out.lto_end ();
2123 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2124 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2125 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2127 bitmap_obstack_release (NULL);
2131 /* Return string alias is alias of. */
2133 static tree
2134 get_alias_symbol (tree decl)
2136 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2137 return get_identifier (TREE_STRING_POINTER
2138 (TREE_VALUE (TREE_VALUE (alias))));
2142 /* Weakrefs may be associated to external decls and thus not output
2143 at expansion time. Emit all necessary aliases. */
2145 void
2146 symbol_table::output_weakrefs (void)
2148 symtab_node *node;
2149 cgraph_node *cnode;
2150 FOR_EACH_SYMBOL (node)
2151 if (node->alias
2152 && !TREE_ASM_WRITTEN (node->decl)
2153 && (!(cnode = dyn_cast <cgraph_node *> (node))
2154 || !cnode->instrumented_version
2155 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2156 && node->weakref)
2158 tree target;
2160 /* Weakrefs are special by not requiring target definition in current
2161 compilation unit. It is thus bit hard to work out what we want to
2162 alias.
2163 When alias target is defined, we need to fetch it from symtab reference,
2164 otherwise it is pointed to by alias_target. */
2165 if (node->alias_target)
2166 target = (DECL_P (node->alias_target)
2167 ? DECL_ASSEMBLER_NAME (node->alias_target)
2168 : node->alias_target);
2169 else if (node->analyzed)
2170 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2171 else
2173 gcc_unreachable ();
2174 target = get_alias_symbol (node->decl);
2176 do_assemble_alias (node->decl, target);
2180 /* Perform simple optimizations based on callgraph. */
2182 void
2183 symbol_table::compile (void)
2185 if (seen_error ())
2186 return;
2188 #ifdef ENABLE_CHECKING
2189 symtab_node::verify_symtab_nodes ();
2190 #endif
2192 timevar_push (TV_CGRAPHOPT);
2193 if (pre_ipa_mem_report)
2195 fprintf (stderr, "Memory consumption before IPA\n");
2196 dump_memory_report (false);
2198 if (!quiet_flag)
2199 fprintf (stderr, "Performing interprocedural optimizations\n");
2200 state = IPA;
2202 /* Offloading requires LTO infrastructure. */
2203 if (!in_lto_p && g->have_offload)
2204 flag_generate_offload = 1;
2206 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2207 if (flag_generate_lto || flag_generate_offload)
2208 lto_streamer_hooks_init ();
2210 /* Don't run the IPA passes if there was any error or sorry messages. */
2211 if (!seen_error ())
2212 ipa_passes ();
2214 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2215 if (seen_error ()
2216 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2218 timevar_pop (TV_CGRAPHOPT);
2219 return;
2222 global_info_ready = true;
2223 if (dump_file)
2225 fprintf (dump_file, "Optimized ");
2226 symtab_node:: dump_table (dump_file);
2228 if (post_ipa_mem_report)
2230 fprintf (stderr, "Memory consumption after IPA\n");
2231 dump_memory_report (false);
2233 timevar_pop (TV_CGRAPHOPT);
2235 /* Output everything. */
2236 (*debug_hooks->assembly_start) ();
2237 if (!quiet_flag)
2238 fprintf (stderr, "Assembling functions:\n");
2239 #ifdef ENABLE_CHECKING
2240 symtab_node::verify_symtab_nodes ();
2241 #endif
2243 materialize_all_clones ();
2244 bitmap_obstack_initialize (NULL);
2245 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2246 bitmap_obstack_release (NULL);
2247 mark_functions_to_output ();
2249 /* When weakref support is missing, we autmatically translate all
2250 references to NODE to references to its ultimate alias target.
2251 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2252 TREE_CHAIN.
2254 Set up this mapping before we output any assembler but once we are sure
2255 that all symbol renaming is done.
2257 FIXME: All this uglyness can go away if we just do renaming at gimple
2258 level by physically rewritting the IL. At the moment we can only redirect
2259 calls, so we need infrastructure for renaming references as well. */
2260 #ifndef ASM_OUTPUT_WEAKREF
2261 symtab_node *node;
2263 FOR_EACH_SYMBOL (node)
2264 if (node->alias
2265 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2267 IDENTIFIER_TRANSPARENT_ALIAS
2268 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2269 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2270 = (node->alias_target ? node->alias_target
2271 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2273 #endif
2275 state = EXPANSION;
2277 if (!flag_toplevel_reorder)
2278 output_in_order (false);
2279 else
2281 /* Output first asm statements and anything ordered. The process
2282 flag is cleared for these nodes, so we skip them later. */
2283 output_in_order (true);
2284 expand_all_functions ();
2285 output_variables ();
2288 process_new_functions ();
2289 state = FINISHED;
2290 output_weakrefs ();
2292 if (dump_file)
2294 fprintf (dump_file, "\nFinal ");
2295 symtab_node::dump_table (dump_file);
2297 #ifdef ENABLE_CHECKING
2298 symtab_node::verify_symtab_nodes ();
2299 /* Double check that all inline clones are gone and that all
2300 function bodies have been released from memory. */
2301 if (!seen_error ())
2303 cgraph_node *node;
2304 bool error_found = false;
2306 FOR_EACH_DEFINED_FUNCTION (node)
2307 if (node->global.inlined_to
2308 || gimple_has_body_p (node->decl))
2310 error_found = true;
2311 node->debug ();
2313 if (error_found)
2314 internal_error ("nodes with unreleased memory found");
2316 #endif
2320 /* Analyze the whole compilation unit once it is parsed completely. */
2322 void
2323 symbol_table::finalize_compilation_unit (void)
2325 timevar_push (TV_CGRAPH);
2327 /* If we're here there's no current function anymore. Some frontends
2328 are lazy in clearing these. */
2329 current_function_decl = NULL;
2330 set_cfun (NULL);
2332 /* Do not skip analyzing the functions if there were errors, we
2333 miss diagnostics for following functions otherwise. */
2335 /* Emit size functions we didn't inline. */
2336 finalize_size_functions ();
2338 /* Mark alias targets necessary and emit diagnostics. */
2339 handle_alias_pairs ();
2341 if (!quiet_flag)
2343 fprintf (stderr, "\nAnalyzing compilation unit\n");
2344 fflush (stderr);
2347 if (flag_dump_passes)
2348 dump_passes ();
2350 /* Gimplify and lower all functions, compute reachability and
2351 remove unreachable nodes. */
2352 analyze_functions ();
2354 /* Mark alias targets necessary and emit diagnostics. */
2355 handle_alias_pairs ();
2357 /* Gimplify and lower thunks. */
2358 analyze_functions ();
2360 /* Finally drive the pass manager. */
2361 compile ();
2363 timevar_pop (TV_CGRAPH);
2366 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2367 within the same process. For use by toplev::finalize. */
2369 void
2370 cgraphunit_c_finalize (void)
2372 gcc_assert (cgraph_new_nodes.length () == 0);
2373 cgraph_new_nodes.truncate (0);
2375 vtable_entry_type = NULL;
2376 queued_nodes = &symtab_terminator;
2378 first_analyzed = NULL;
2379 first_analyzed_var = NULL;
2382 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2383 kind of wrapper method. */
2385 void
2386 cgraph_node::create_wrapper (cgraph_node *target)
2388 /* Preserve DECL_RESULT so we get right by reference flag. */
2389 tree decl_result = DECL_RESULT (decl);
2391 /* Remove the function's body but keep arguments to be reused
2392 for thunk. */
2393 release_body (true);
2394 reset ();
2396 DECL_RESULT (decl) = decl_result;
2397 DECL_INITIAL (decl) = NULL;
2398 allocate_struct_function (decl, false);
2399 set_cfun (NULL);
2401 /* Turn alias into thunk and expand it into GIMPLE representation. */
2402 definition = true;
2403 thunk.thunk_p = true;
2404 thunk.this_adjusting = false;
2406 cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
2408 tree arguments = DECL_ARGUMENTS (decl);
2410 while (arguments)
2412 TREE_ADDRESSABLE (arguments) = false;
2413 arguments = TREE_CHAIN (arguments);
2416 expand_thunk (false, true);
2417 e->call_stmt_cannot_inline_p = true;
2419 /* Inline summary set-up. */
2420 analyze ();
2421 inline_analyze_function (this);
2424 #include "gt-cgraphunit.h"