* cgraph.c (cgraph_get_body): New function based on lto.c
[official-gcc.git] / gcc / cgraphunit.c
blob1472483342f1df2d8ad0437018b64b4fa6afe224
1 /* Driver of optimization process
2 Copyright (C) 2003-2013 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 - cgraph_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 "output.h"
166 #include "rtl.h"
167 #include "tree-flow.h"
168 #include "tree-inline.h"
169 #include "langhooks.h"
170 #include "pointer-set.h"
171 #include "toplev.h"
172 #include "flags.h"
173 #include "ggc.h"
174 #include "debug.h"
175 #include "target.h"
176 #include "cgraph.h"
177 #include "diagnostic.h"
178 #include "params.h"
179 #include "fibheap.h"
180 #include "intl.h"
181 #include "function.h"
182 #include "ipa-prop.h"
183 #include "gimple.h"
184 #include "tree-iterator.h"
185 #include "tree-pass.h"
186 #include "tree-dump.h"
187 #include "gimple-pretty-print.h"
188 #include "output.h"
189 #include "coverage.h"
190 #include "plugin.h"
191 #include "ipa-inline.h"
192 #include "ipa-utils.h"
193 #include "lto-streamer.h"
194 #include "except.h"
195 #include "cfgloop.h"
196 #include "regset.h" /* FIXME: For reg_obstack. */
197 #include "context.h"
198 #include "pass_manager.h"
200 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
201 secondary queue used during optimization to accommodate passes that
202 may generate new functions that need to be optimized and expanded. */
203 cgraph_node_set cgraph_new_nodes;
205 static void expand_all_functions (void);
206 static void mark_functions_to_output (void);
207 static void expand_function (struct cgraph_node *);
208 static void analyze_function (struct cgraph_node *);
209 static void handle_alias_pairs (void);
211 FILE *cgraph_dump_file;
213 /* Linked list of cgraph asm nodes. */
214 struct asm_node *asm_nodes;
216 /* Last node in cgraph_asm_nodes. */
217 static GTY(()) struct asm_node *asm_last_node;
219 /* Used for vtable lookup in thunk adjusting. */
220 static GTY (()) tree vtable_entry_type;
222 /* Determine if symbol DECL is needed. That is, visible to something
223 either outside this translation unit, something magic in the system
224 configury */
225 bool
226 decide_is_symbol_needed (symtab_node node)
228 tree decl = node->symbol.decl;
230 /* Double check that no one output the function into assembly file
231 early. */
232 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
233 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
235 if (!node->symbol.definition)
236 return false;
238 /* Devirtualization may access these. */
239 if (DECL_VIRTUAL_P (decl) && optimize)
240 return true;
242 if (DECL_EXTERNAL (decl))
243 return false;
245 /* If the user told us it is used, then it must be so. */
246 if (node->symbol.force_output)
247 return true;
249 /* ABI forced symbols are needed when they are external. */
250 if (node->symbol.forced_by_abi && TREE_PUBLIC (decl))
251 return true;
253 /* Keep constructors, destructors and virtual functions. */
254 if (TREE_CODE (decl) == FUNCTION_DECL
255 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
256 return true;
258 /* Externally visible variables must be output. The exception is
259 COMDAT variables that must be output only when they are needed. */
260 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
261 return true;
263 return false;
266 /* Head of the queue of nodes to be processed while building callgraph */
268 static symtab_node first = (symtab_node)(void *)1;
270 /* Add NODE to queue starting at FIRST.
271 The queue is linked via AUX pointers and terminated by pointer to 1. */
273 static void
274 enqueue_node (symtab_node node)
276 if (node->symbol.aux)
277 return;
278 gcc_checking_assert (first);
279 node->symbol.aux = first;
280 first = node;
283 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
284 functions into callgraph in a way so they look like ordinary reachable
285 functions inserted into callgraph already at construction time. */
287 bool
288 cgraph_process_new_functions (void)
290 bool output = false;
291 tree fndecl;
292 struct cgraph_node *node;
293 cgraph_node_set_iterator csi;
295 if (!cgraph_new_nodes)
296 return false;
297 handle_alias_pairs ();
298 /* Note that this queue may grow as its being processed, as the new
299 functions may generate new ones. */
300 for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi))
302 node = csi_node (csi);
303 fndecl = node->symbol.decl;
304 switch (cgraph_state)
306 case CGRAPH_STATE_CONSTRUCTION:
307 /* At construction time we just need to finalize function and move
308 it into reachable functions list. */
310 cgraph_finalize_function (fndecl, false);
311 output = true;
312 cgraph_call_function_insertion_hooks (node);
313 enqueue_node ((symtab_node) node);
314 break;
316 case CGRAPH_STATE_IPA:
317 case CGRAPH_STATE_IPA_SSA:
318 /* When IPA optimization already started, do all essential
319 transformations that has been already performed on the whole
320 cgraph but not on this function. */
322 gimple_register_cfg_hooks ();
323 if (!node->symbol.analyzed)
324 analyze_function (node);
325 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
326 if (cgraph_state == CGRAPH_STATE_IPA_SSA
327 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
328 g->get_passes ()->execute_early_local_passes ();
329 else if (inline_summary_vec != NULL)
330 compute_inline_parameters (node, true);
331 free_dominance_info (CDI_POST_DOMINATORS);
332 free_dominance_info (CDI_DOMINATORS);
333 pop_cfun ();
334 cgraph_call_function_insertion_hooks (node);
335 break;
337 case CGRAPH_STATE_EXPANSION:
338 /* Functions created during expansion shall be compiled
339 directly. */
340 node->process = 0;
341 cgraph_call_function_insertion_hooks (node);
342 expand_function (node);
343 break;
345 default:
346 gcc_unreachable ();
347 break;
350 free_cgraph_node_set (cgraph_new_nodes);
351 cgraph_new_nodes = NULL;
352 return output;
355 /* As an GCC extension we allow redefinition of the function. The
356 semantics when both copies of bodies differ is not well defined.
357 We replace the old body with new body so in unit at a time mode
358 we always use new body, while in normal mode we may end up with
359 old body inlined into some functions and new body expanded and
360 inlined in others.
362 ??? It may make more sense to use one body for inlining and other
363 body for expanding the function but this is difficult to do. */
365 void
366 cgraph_reset_node (struct cgraph_node *node)
368 /* If node->process is set, then we have already begun whole-unit analysis.
369 This is *not* testing for whether we've already emitted the function.
370 That case can be sort-of legitimately seen with real function redefinition
371 errors. I would argue that the front end should never present us with
372 such a case, but don't enforce that for now. */
373 gcc_assert (!node->process);
375 /* Reset our data structures so we can analyze the function again. */
376 memset (&node->local, 0, sizeof (node->local));
377 memset (&node->global, 0, sizeof (node->global));
378 memset (&node->rtl, 0, sizeof (node->rtl));
379 node->symbol.analyzed = false;
380 node->symbol.definition = false;
381 node->symbol.alias = false;
382 node->symbol.weakref = false;
383 node->symbol.cpp_implicit_alias = false;
385 cgraph_node_remove_callees (node);
386 ipa_remove_all_references (&node->symbol.ref_list);
389 /* Return true when there are references to NODE. */
391 static bool
392 referred_to_p (symtab_node node)
394 struct ipa_ref *ref;
396 /* See if there are any references at all. */
397 if (ipa_ref_list_referring_iterate (&node->symbol.ref_list, 0, ref))
398 return true;
399 /* For functions check also calls. */
400 cgraph_node *cn = dyn_cast <cgraph_node> (node);
401 if (cn && cn->callers)
402 return true;
403 return false;
406 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
407 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
408 the garbage collector run at the moment. We would need to either create
409 a new GC context, or just not compile right now. */
411 void
412 cgraph_finalize_function (tree decl, bool no_collect)
414 struct cgraph_node *node = cgraph_get_create_node (decl);
416 if (node->symbol.definition)
418 /* Nested functions should only be defined once. */
419 gcc_assert (!DECL_CONTEXT (decl)
420 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
421 cgraph_reset_node (node);
422 node->local.redefined_extern_inline = true;
425 notice_global_symbol (decl);
426 node->symbol.definition = true;
427 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
429 /* With -fkeep-inline-functions we are keeping all inline functions except
430 for extern inline ones. */
431 if (flag_keep_inline_functions
432 && DECL_DECLARED_INLINE_P (decl)
433 && !DECL_EXTERNAL (decl)
434 && !DECL_DISREGARD_INLINE_LIMITS (decl))
435 node->symbol.force_output = 1;
437 /* When not optimizing, also output the static functions. (see
438 PR24561), but don't do so for always_inline functions, functions
439 declared inline and nested functions. These were optimized out
440 in the original implementation and it is unclear whether we want
441 to change the behavior here. */
442 if ((!optimize
443 && !node->symbol.cpp_implicit_alias
444 && !DECL_DISREGARD_INLINE_LIMITS (decl)
445 && !DECL_DECLARED_INLINE_P (decl)
446 && !(DECL_CONTEXT (decl)
447 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
448 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
449 node->symbol.force_output = 1;
451 /* If we've not yet emitted decl, tell the debug info about it. */
452 if (!TREE_ASM_WRITTEN (decl))
453 (*debug_hooks->deferred_inline_function) (decl);
455 /* Possibly warn about unused parameters. */
456 if (warn_unused_parameter)
457 do_warn_unused_parameter (decl);
459 if (!no_collect)
460 ggc_collect ();
462 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
463 && (decide_is_symbol_needed ((symtab_node) node)
464 || referred_to_p ((symtab_node)node)))
465 enqueue_node ((symtab_node)node);
468 /* Add the function FNDECL to the call graph.
469 Unlike cgraph_finalize_function, this function is intended to be used
470 by middle end and allows insertion of new function at arbitrary point
471 of compilation. The function can be either in high, low or SSA form
472 GIMPLE.
474 The function is assumed to be reachable and have address taken (so no
475 API breaking optimizations are performed on it).
477 Main work done by this function is to enqueue the function for later
478 processing to avoid need the passes to be re-entrant. */
480 void
481 cgraph_add_new_function (tree fndecl, bool lowered)
483 gcc::pass_manager *passes = g->get_passes ();
484 struct cgraph_node *node;
485 switch (cgraph_state)
487 case CGRAPH_STATE_PARSING:
488 cgraph_finalize_function (fndecl, false);
489 break;
490 case CGRAPH_STATE_CONSTRUCTION:
491 /* Just enqueue function to be processed at nearest occurrence. */
492 node = cgraph_create_node (fndecl);
493 if (lowered)
494 node->lowered = true;
495 if (!cgraph_new_nodes)
496 cgraph_new_nodes = cgraph_node_set_new ();
497 cgraph_node_set_add (cgraph_new_nodes, node);
498 break;
500 case CGRAPH_STATE_IPA:
501 case CGRAPH_STATE_IPA_SSA:
502 case CGRAPH_STATE_EXPANSION:
503 /* Bring the function into finalized state and enqueue for later
504 analyzing and compilation. */
505 node = cgraph_get_create_node (fndecl);
506 node->local.local = false;
507 node->symbol.definition = true;
508 node->symbol.force_output = true;
509 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
511 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
512 gimple_register_cfg_hooks ();
513 bitmap_obstack_initialize (NULL);
514 execute_pass_list (passes->all_lowering_passes);
515 passes->execute_early_local_passes ();
516 bitmap_obstack_release (NULL);
517 pop_cfun ();
519 lowered = true;
521 if (lowered)
522 node->lowered = true;
523 if (!cgraph_new_nodes)
524 cgraph_new_nodes = cgraph_node_set_new ();
525 cgraph_node_set_add (cgraph_new_nodes, node);
526 break;
528 case CGRAPH_STATE_FINISHED:
529 /* At the very end of compilation we have to do all the work up
530 to expansion. */
531 node = cgraph_create_node (fndecl);
532 if (lowered)
533 node->lowered = true;
534 node->symbol.definition = true;
535 analyze_function (node);
536 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
537 gimple_register_cfg_hooks ();
538 bitmap_obstack_initialize (NULL);
539 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
540 g->get_passes ()->execute_early_local_passes ();
541 bitmap_obstack_release (NULL);
542 pop_cfun ();
543 expand_function (node);
544 break;
546 default:
547 gcc_unreachable ();
550 /* Set a personality if required and we already passed EH lowering. */
551 if (lowered
552 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
553 == eh_personality_lang))
554 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
557 /* Add a top-level asm statement to the list. */
559 struct asm_node *
560 add_asm_node (tree asm_str)
562 struct asm_node *node;
564 node = ggc_alloc_cleared_asm_node ();
565 node->asm_str = asm_str;
566 node->order = symtab_order++;
567 node->next = NULL;
568 if (asm_nodes == NULL)
569 asm_nodes = node;
570 else
571 asm_last_node->next = node;
572 asm_last_node = node;
573 return node;
576 /* Output all asm statements we have stored up to be output. */
578 static void
579 output_asm_statements (void)
581 struct asm_node *can;
583 if (seen_error ())
584 return;
586 for (can = asm_nodes; can; can = can->next)
587 assemble_asm (can->asm_str);
588 asm_nodes = NULL;
591 /* Analyze the function scheduled to be output. */
592 static void
593 analyze_function (struct cgraph_node *node)
595 tree decl = node->symbol.decl;
596 location_t saved_loc = input_location;
597 input_location = DECL_SOURCE_LOCATION (decl);
599 if (node->symbol.alias)
600 symtab_resolve_alias
601 ((symtab_node) node, (symtab_node) cgraph_get_node (node->symbol.alias_target));
602 else if (node->thunk.thunk_p)
604 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
605 NULL, 0, CGRAPH_FREQ_BASE);
606 node->thunk.alias = NULL;
608 else if (node->dispatcher_function)
610 /* Generate the dispatcher body of multi-versioned functions. */
611 struct cgraph_function_version_info *dispatcher_version_info
612 = get_cgraph_node_version (node);
613 if (dispatcher_version_info != NULL
614 && (dispatcher_version_info->dispatcher_resolver
615 == NULL_TREE))
617 tree resolver = NULL_TREE;
618 gcc_assert (targetm.generate_version_dispatcher_body);
619 resolver = targetm.generate_version_dispatcher_body (node);
620 gcc_assert (resolver != NULL_TREE);
623 else
625 push_cfun (DECL_STRUCT_FUNCTION (decl));
627 assign_assembler_name_if_neeeded (node->symbol.decl);
629 /* Make sure to gimplify bodies only once. During analyzing a
630 function we lower it, which will require gimplified nested
631 functions, so we can end up here with an already gimplified
632 body. */
633 if (!gimple_has_body_p (decl))
634 gimplify_function_tree (decl);
635 dump_function (TDI_generic, decl);
637 /* Lower the function. */
638 if (!node->lowered)
640 if (node->nested)
641 lower_nested_functions (node->symbol.decl);
642 gcc_assert (!node->nested);
644 gimple_register_cfg_hooks ();
645 bitmap_obstack_initialize (NULL);
646 execute_pass_list (g->get_passes ()->all_lowering_passes);
647 free_dominance_info (CDI_POST_DOMINATORS);
648 free_dominance_info (CDI_DOMINATORS);
649 compact_blocks ();
650 bitmap_obstack_release (NULL);
651 node->lowered = true;
654 pop_cfun ();
656 node->symbol.analyzed = true;
658 input_location = saved_loc;
661 /* C++ frontend produce same body aliases all over the place, even before PCH
662 gets streamed out. It relies on us linking the aliases with their function
663 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
664 first produce aliases without links, but once C++ FE is sure he won't sream
665 PCH we build the links via this function. */
667 void
668 cgraph_process_same_body_aliases (void)
670 symtab_node node;
671 FOR_EACH_SYMBOL (node)
672 if (node->symbol.cpp_implicit_alias && !node->symbol.analyzed)
673 symtab_resolve_alias
674 (node,
675 TREE_CODE (node->symbol.alias_target) == VAR_DECL
676 ? (symtab_node)varpool_node_for_decl (node->symbol.alias_target)
677 : (symtab_node)cgraph_get_create_node (node->symbol.alias_target));
678 cpp_implicit_aliases_done = true;
681 /* Process attributes common for vars and functions. */
683 static void
684 process_common_attributes (tree decl)
686 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
688 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
690 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
691 "%<weakref%> attribute should be accompanied with"
692 " an %<alias%> attribute");
693 DECL_WEAK (decl) = 0;
694 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
695 DECL_ATTRIBUTES (decl));
699 /* Look for externally_visible and used attributes and mark cgraph nodes
700 accordingly.
702 We cannot mark the nodes at the point the attributes are processed (in
703 handle_*_attribute) because the copy of the declarations available at that
704 point may not be canonical. For example, in:
706 void f();
707 void f() __attribute__((used));
709 the declaration we see in handle_used_attribute will be the second
710 declaration -- but the front end will subsequently merge that declaration
711 with the original declaration and discard the second declaration.
713 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
715 void f() {}
716 void f() __attribute__((externally_visible));
718 is valid.
720 So, we walk the nodes at the end of the translation unit, applying the
721 attributes at that point. */
723 static void
724 process_function_and_variable_attributes (struct cgraph_node *first,
725 struct varpool_node *first_var)
727 struct cgraph_node *node;
728 struct varpool_node *vnode;
730 for (node = cgraph_first_function (); node != first;
731 node = cgraph_next_function (node))
733 tree decl = node->symbol.decl;
734 if (DECL_PRESERVE_P (decl))
735 cgraph_mark_force_output_node (node);
736 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
738 if (! TREE_PUBLIC (node->symbol.decl))
739 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
740 "%<externally_visible%>"
741 " attribute have effect only on public objects");
743 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
744 && (node->symbol.definition && !node->symbol.alias))
746 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
747 "%<weakref%> attribute ignored"
748 " because function is defined");
749 DECL_WEAK (decl) = 0;
750 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
751 DECL_ATTRIBUTES (decl));
754 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
755 && !DECL_DECLARED_INLINE_P (decl)
756 /* redefining extern inline function makes it DECL_UNINLINABLE. */
757 && !DECL_UNINLINABLE (decl))
758 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
759 "always_inline function might not be inlinable");
761 process_common_attributes (decl);
763 for (vnode = varpool_first_variable (); vnode != first_var;
764 vnode = varpool_next_variable (vnode))
766 tree decl = vnode->symbol.decl;
767 if (DECL_EXTERNAL (decl)
768 && DECL_INITIAL (decl))
769 varpool_finalize_decl (decl);
770 if (DECL_PRESERVE_P (decl))
771 vnode->symbol.force_output = true;
772 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
774 if (! TREE_PUBLIC (vnode->symbol.decl))
775 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
776 "%<externally_visible%>"
777 " attribute have effect only on public objects");
779 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
780 && vnode->symbol.definition
781 && DECL_INITIAL (decl))
783 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
784 "%<weakref%> attribute ignored"
785 " because variable is initialized");
786 DECL_WEAK (decl) = 0;
787 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
788 DECL_ATTRIBUTES (decl));
790 process_common_attributes (decl);
794 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
795 middle end to output the variable to asm file, if needed or externally
796 visible. */
798 void
799 varpool_finalize_decl (tree decl)
801 struct varpool_node *node = varpool_node_for_decl (decl);
803 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
805 if (node->symbol.definition)
806 return;
807 notice_global_symbol (decl);
808 node->symbol.definition = true;
809 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
810 /* Traditionally we do not eliminate static variables when not
811 optimizing and when not doing toplevel reoder. */
812 || (!flag_toplevel_reorder && !DECL_COMDAT (node->symbol.decl)
813 && !DECL_ARTIFICIAL (node->symbol.decl)))
814 node->symbol.force_output = true;
816 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
817 && (decide_is_symbol_needed ((symtab_node) node)
818 || referred_to_p ((symtab_node)node)))
819 enqueue_node ((symtab_node)node);
820 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
821 varpool_analyze_node (node);
822 /* Some frontends produce various interface variables after compilation
823 finished. */
824 if (cgraph_state == CGRAPH_STATE_FINISHED)
825 varpool_assemble_decl (node);
829 /* Discover all functions and variables that are trivially needed, analyze
830 them as well as all functions and variables referred by them */
832 static void
833 analyze_functions (void)
835 /* Keep track of already processed nodes when called multiple times for
836 intermodule optimization. */
837 static struct cgraph_node *first_analyzed;
838 struct cgraph_node *first_handled = first_analyzed;
839 static struct varpool_node *first_analyzed_var;
840 struct varpool_node *first_handled_var = first_analyzed_var;
842 symtab_node node, next;
843 int i;
844 struct ipa_ref *ref;
845 bool changed = true;
847 bitmap_obstack_initialize (NULL);
848 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
850 /* Ugly, but the fixup can not happen at a time same body alias is created;
851 C++ FE is confused about the COMDAT groups being right. */
852 if (cpp_implicit_aliases_done)
853 FOR_EACH_SYMBOL (node)
854 if (node->symbol.cpp_implicit_alias)
855 fixup_same_cpp_alias_visibility (node, symtab_alias_target (node));
857 /* Analysis adds static variables that in turn adds references to new functions.
858 So we need to iterate the process until it stabilize. */
859 while (changed)
861 changed = false;
862 process_function_and_variable_attributes (first_analyzed,
863 first_analyzed_var);
865 /* First identify the trivially needed symbols. */
866 for (node = symtab_nodes;
867 node != (symtab_node)first_analyzed
868 && node != (symtab_node)first_analyzed_var; node = node->symbol.next)
870 if (decide_is_symbol_needed (node))
872 enqueue_node (node);
873 if (!changed && cgraph_dump_file)
874 fprintf (cgraph_dump_file, "Trivially needed symbols:");
875 changed = true;
876 if (cgraph_dump_file)
877 fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node));
879 if (node == (symtab_node)first_analyzed
880 || node == (symtab_node)first_analyzed_var)
881 break;
883 cgraph_process_new_functions ();
884 first_analyzed_var = varpool_first_variable ();
885 first_analyzed = cgraph_first_function ();
887 if (changed && dump_file)
888 fprintf (cgraph_dump_file, "\n");
890 /* Lower representation, build callgraph edges and references for all trivially
891 needed symbols and all symbols referred by them. */
892 while (first != (symtab_node)(void *)1)
894 changed = true;
895 node = first;
896 first = (symtab_node)first->symbol.aux;
897 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
898 if (cnode && cnode->symbol.definition)
900 struct cgraph_edge *edge;
901 tree decl = cnode->symbol.decl;
903 /* ??? It is possible to create extern inline function
904 and later using weak alias attribute to kill its body.
905 See gcc.c-torture/compile/20011119-1.c */
906 if (!DECL_STRUCT_FUNCTION (decl)
907 && !cnode->symbol.alias
908 && !cnode->thunk.thunk_p
909 && !cnode->dispatcher_function)
911 cgraph_reset_node (cnode);
912 cnode->local.redefined_extern_inline = true;
913 continue;
916 if (!cnode->symbol.analyzed)
917 analyze_function (cnode);
919 for (edge = cnode->callees; edge; edge = edge->next_callee)
920 if (edge->callee->symbol.definition)
921 enqueue_node ((symtab_node)edge->callee);
923 /* If decl is a clone of an abstract function,
924 mark that abstract function so that we don't release its body.
925 The DECL_INITIAL() of that abstract function declaration
926 will be later needed to output debug info. */
927 if (DECL_ABSTRACT_ORIGIN (decl))
929 struct cgraph_node *origin_node
930 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
931 origin_node->used_as_abstract_origin = true;
934 else
936 varpool_node *vnode = dyn_cast <varpool_node> (node);
937 if (vnode && vnode->symbol.definition && !vnode->symbol.analyzed)
938 varpool_analyze_node (vnode);
941 if (node->symbol.same_comdat_group)
943 symtab_node next;
944 for (next = node->symbol.same_comdat_group;
945 next != node;
946 next = next->symbol.same_comdat_group)
947 enqueue_node (next);
949 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
950 if (ref->referred->symbol.definition)
951 enqueue_node (ref->referred);
952 cgraph_process_new_functions ();
956 /* Collect entry points to the unit. */
957 if (cgraph_dump_file)
959 fprintf (cgraph_dump_file, "\n\nInitial ");
960 dump_symtab (cgraph_dump_file);
963 if (cgraph_dump_file)
964 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
966 for (node = symtab_nodes;
967 node != (symtab_node)first_handled
968 && node != (symtab_node)first_handled_var; node = next)
970 next = node->symbol.next;
971 if (!node->symbol.aux && !referred_to_p (node))
973 if (cgraph_dump_file)
974 fprintf (cgraph_dump_file, " %s", symtab_node_name (node));
975 symtab_remove_node (node);
976 continue;
978 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
980 tree decl = node->symbol.decl;
982 if (cnode->symbol.definition && !gimple_has_body_p (decl)
983 && !cnode->symbol.alias
984 && !cnode->thunk.thunk_p)
985 cgraph_reset_node (cnode);
987 gcc_assert (!cnode->symbol.definition || cnode->thunk.thunk_p
988 || cnode->symbol.alias
989 || gimple_has_body_p (decl));
990 gcc_assert (cnode->symbol.analyzed == cnode->symbol.definition);
992 node->symbol.aux = NULL;
994 first_analyzed = cgraph_first_function ();
995 first_analyzed_var = varpool_first_variable ();
996 if (cgraph_dump_file)
998 fprintf (cgraph_dump_file, "\n\nReclaimed ");
999 dump_symtab (cgraph_dump_file);
1001 bitmap_obstack_release (NULL);
1002 ggc_collect ();
1005 /* Translate the ugly representation of aliases as alias pairs into nice
1006 representation in callgraph. We don't handle all cases yet,
1007 unforutnately. */
1009 static void
1010 handle_alias_pairs (void)
1012 alias_pair *p;
1013 unsigned i;
1015 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1017 symtab_node target_node = symtab_node_for_asm (p->target);
1019 /* Weakrefs with target not defined in current unit are easy to handle; they
1020 behave just as external variables except we need to note the alias flag
1021 to later output the weakref pseudo op into asm file. */
1022 if (!target_node && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1024 symtab_node node = symtab_get_node (p->decl);
1025 if (node)
1027 node->symbol.alias_target = p->target;
1028 node->symbol.weakref = true;
1029 node->symbol.alias = true;
1031 alias_pairs->unordered_remove (i);
1032 continue;
1034 else if (!target_node)
1036 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1037 alias_pairs->unordered_remove (i);
1038 continue;
1041 if (DECL_EXTERNAL (target_node->symbol.decl)
1042 /* We use local aliases for C++ thunks to force the tailcall
1043 to bind locally. This is a hack - to keep it working do
1044 the following (which is not strictly correct). */
1045 && (! TREE_CODE (target_node->symbol.decl) == FUNCTION_DECL
1046 || ! DECL_VIRTUAL_P (target_node->symbol.decl))
1047 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1049 error ("%q+D aliased to external symbol %qE",
1050 p->decl, p->target);
1053 if (TREE_CODE (p->decl) == FUNCTION_DECL
1054 && target_node && is_a <cgraph_node> (target_node))
1056 struct cgraph_node *src_node = cgraph_get_node (p->decl);
1057 if (src_node && src_node->symbol.definition)
1058 cgraph_reset_node (src_node);
1059 cgraph_create_function_alias (p->decl, target_node->symbol.decl);
1060 alias_pairs->unordered_remove (i);
1062 else if (TREE_CODE (p->decl) == VAR_DECL
1063 && target_node && is_a <varpool_node> (target_node))
1065 varpool_create_variable_alias (p->decl, target_node->symbol.decl);
1066 alias_pairs->unordered_remove (i);
1068 else
1070 error ("%q+D alias in between function and variable is not supported",
1071 p->decl);
1072 warning (0, "%q+D aliased declaration",
1073 target_node->symbol.decl);
1074 alias_pairs->unordered_remove (i);
1077 vec_free (alias_pairs);
1081 /* Figure out what functions we want to assemble. */
1083 static void
1084 mark_functions_to_output (void)
1086 struct cgraph_node *node;
1087 #ifdef ENABLE_CHECKING
1088 bool check_same_comdat_groups = false;
1090 FOR_EACH_FUNCTION (node)
1091 gcc_assert (!node->process);
1092 #endif
1094 FOR_EACH_FUNCTION (node)
1096 tree decl = node->symbol.decl;
1098 gcc_assert (!node->process || node->symbol.same_comdat_group);
1099 if (node->process)
1100 continue;
1102 /* We need to output all local functions that are used and not
1103 always inlined, as well as those that are reachable from
1104 outside the current compilation unit. */
1105 if (node->symbol.analyzed
1106 && !node->thunk.thunk_p
1107 && !node->symbol.alias
1108 && !node->global.inlined_to
1109 && !TREE_ASM_WRITTEN (decl)
1110 && !DECL_EXTERNAL (decl))
1112 node->process = 1;
1113 if (node->symbol.same_comdat_group)
1115 struct cgraph_node *next;
1116 for (next = cgraph (node->symbol.same_comdat_group);
1117 next != node;
1118 next = cgraph (next->symbol.same_comdat_group))
1119 if (!next->thunk.thunk_p && !next->symbol.alias)
1120 next->process = 1;
1123 else if (node->symbol.same_comdat_group)
1125 #ifdef ENABLE_CHECKING
1126 check_same_comdat_groups = true;
1127 #endif
1129 else
1131 /* We should've reclaimed all functions that are not needed. */
1132 #ifdef ENABLE_CHECKING
1133 if (!node->global.inlined_to
1134 && gimple_has_body_p (decl)
1135 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1136 are inside partition, we can end up not removing the body since we no longer
1137 have analyzed node pointing to it. */
1138 && !node->symbol.in_other_partition
1139 && !node->symbol.alias
1140 && !node->clones
1141 && !DECL_EXTERNAL (decl))
1143 dump_cgraph_node (stderr, node);
1144 internal_error ("failed to reclaim unneeded function");
1146 #endif
1147 gcc_assert (node->global.inlined_to
1148 || !gimple_has_body_p (decl)
1149 || node->symbol.in_other_partition
1150 || node->clones
1151 || DECL_ARTIFICIAL (decl)
1152 || DECL_EXTERNAL (decl));
1157 #ifdef ENABLE_CHECKING
1158 if (check_same_comdat_groups)
1159 FOR_EACH_FUNCTION (node)
1160 if (node->symbol.same_comdat_group && !node->process)
1162 tree decl = node->symbol.decl;
1163 if (!node->global.inlined_to
1164 && gimple_has_body_p (decl)
1165 /* FIXME: in an ltrans unit when the offline copy is outside a
1166 partition but inline copies are inside a partition, we can
1167 end up not removing the body since we no longer have an
1168 analyzed node pointing to it. */
1169 && !node->symbol.in_other_partition
1170 && !node->clones
1171 && !DECL_EXTERNAL (decl))
1173 dump_cgraph_node (stderr, node);
1174 internal_error ("failed to reclaim unneeded function in same "
1175 "comdat group");
1178 #endif
1181 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1182 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1184 Set current_function_decl and cfun to newly constructed empty function body.
1185 return basic block in the function body. */
1187 basic_block
1188 init_lowered_empty_function (tree decl, bool in_ssa)
1190 basic_block bb;
1192 current_function_decl = decl;
1193 allocate_struct_function (decl, false);
1194 gimple_register_cfg_hooks ();
1195 init_empty_tree_cfg ();
1197 if (in_ssa)
1199 init_tree_ssa (cfun);
1200 init_ssa_operands (cfun);
1201 cfun->gimple_df->in_ssa_p = true;
1202 cfun->curr_properties |= PROP_ssa;
1205 DECL_INITIAL (decl) = make_node (BLOCK);
1207 DECL_SAVED_TREE (decl) = error_mark_node;
1208 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1209 | PROP_cfg | PROP_loops);
1211 set_loops_for_fn (cfun, ggc_alloc_cleared_loops ());
1212 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1213 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1215 /* Create BB for body of the function and connect it properly. */
1216 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
1217 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1218 make_edge (bb, EXIT_BLOCK_PTR, 0);
1219 add_bb_to_loop (bb, ENTRY_BLOCK_PTR->loop_father);
1221 return bb;
1224 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1225 offset indicated by VIRTUAL_OFFSET, if that is
1226 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1227 zero for a result adjusting thunk. */
1229 static tree
1230 thunk_adjust (gimple_stmt_iterator * bsi,
1231 tree ptr, bool this_adjusting,
1232 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1234 gimple stmt;
1235 tree ret;
1237 if (this_adjusting
1238 && fixed_offset != 0)
1240 stmt = gimple_build_assign
1241 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1242 ptr,
1243 fixed_offset));
1244 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1247 /* If there's a virtual offset, look up that value in the vtable and
1248 adjust the pointer again. */
1249 if (virtual_offset)
1251 tree vtabletmp;
1252 tree vtabletmp2;
1253 tree vtabletmp3;
1255 if (!vtable_entry_type)
1257 tree vfunc_type = make_node (FUNCTION_TYPE);
1258 TREE_TYPE (vfunc_type) = integer_type_node;
1259 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1260 layout_type (vfunc_type);
1262 vtable_entry_type = build_pointer_type (vfunc_type);
1265 vtabletmp =
1266 create_tmp_reg (build_pointer_type
1267 (build_pointer_type (vtable_entry_type)), "vptr");
1269 /* The vptr is always at offset zero in the object. */
1270 stmt = gimple_build_assign (vtabletmp,
1271 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1272 ptr));
1273 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1275 /* Form the vtable address. */
1276 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1277 "vtableaddr");
1278 stmt = gimple_build_assign (vtabletmp2,
1279 build_simple_mem_ref (vtabletmp));
1280 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1282 /* Find the entry with the vcall offset. */
1283 stmt = gimple_build_assign (vtabletmp2,
1284 fold_build_pointer_plus_loc (input_location,
1285 vtabletmp2,
1286 virtual_offset));
1287 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1289 /* Get the offset itself. */
1290 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1291 "vcalloffset");
1292 stmt = gimple_build_assign (vtabletmp3,
1293 build_simple_mem_ref (vtabletmp2));
1294 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1296 /* Adjust the `this' pointer. */
1297 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1298 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1299 GSI_CONTINUE_LINKING);
1302 if (!this_adjusting
1303 && fixed_offset != 0)
1304 /* Adjust the pointer by the constant. */
1306 tree ptrtmp;
1308 if (TREE_CODE (ptr) == VAR_DECL)
1309 ptrtmp = ptr;
1310 else
1312 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1313 stmt = gimple_build_assign (ptrtmp, ptr);
1314 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1316 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1317 ptrtmp, fixed_offset);
1320 /* Emit the statement and gimplify the adjustment expression. */
1321 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1322 stmt = gimple_build_assign (ret, ptr);
1323 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1325 return ret;
1328 /* Produce assembler for thunk NODE. */
1330 void
1331 expand_thunk (struct cgraph_node *node)
1333 bool this_adjusting = node->thunk.this_adjusting;
1334 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1335 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1336 tree virtual_offset = NULL;
1337 tree alias = node->callees->callee->symbol.decl;
1338 tree thunk_fndecl = node->symbol.decl;
1339 tree a = DECL_ARGUMENTS (thunk_fndecl);
1341 current_function_decl = thunk_fndecl;
1343 /* Ensure thunks are emitted in their correct sections. */
1344 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1346 if (this_adjusting
1347 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1348 virtual_value, alias))
1350 const char *fnname;
1351 tree fn_block;
1352 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1354 DECL_RESULT (thunk_fndecl)
1355 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1356 RESULT_DECL, 0, restype);
1357 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1359 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1360 create one. */
1361 fn_block = make_node (BLOCK);
1362 BLOCK_VARS (fn_block) = a;
1363 DECL_INITIAL (thunk_fndecl) = fn_block;
1364 init_function_start (thunk_fndecl);
1365 cfun->is_thunk = 1;
1366 insn_locations_init ();
1367 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1368 prologue_location = curr_insn_location ();
1369 assemble_start_function (thunk_fndecl, fnname);
1371 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1372 fixed_offset, virtual_value, alias);
1374 assemble_end_function (thunk_fndecl, fnname);
1375 insn_locations_finalize ();
1376 init_insn_lengths ();
1377 free_after_compilation (cfun);
1378 set_cfun (NULL);
1379 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1380 node->thunk.thunk_p = false;
1381 node->symbol.analyzed = false;
1383 else
1385 tree restype;
1386 basic_block bb, then_bb, else_bb, return_bb;
1387 gimple_stmt_iterator bsi;
1388 int nargs = 0;
1389 tree arg;
1390 int i;
1391 tree resdecl;
1392 tree restmp = NULL;
1393 vec<tree> vargs;
1395 gimple call;
1396 gimple ret;
1398 DECL_IGNORED_P (thunk_fndecl) = 1;
1399 bitmap_obstack_initialize (NULL);
1401 if (node->thunk.virtual_offset_p)
1402 virtual_offset = size_int (virtual_value);
1404 /* Build the return declaration for the function. */
1405 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1406 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1408 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1409 DECL_ARTIFICIAL (resdecl) = 1;
1410 DECL_IGNORED_P (resdecl) = 1;
1411 DECL_RESULT (thunk_fndecl) = resdecl;
1413 else
1414 resdecl = DECL_RESULT (thunk_fndecl);
1416 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1418 bsi = gsi_start_bb (bb);
1420 /* Build call to the function being thunked. */
1421 if (!VOID_TYPE_P (restype))
1423 if (DECL_BY_REFERENCE (resdecl))
1424 restmp = gimple_fold_indirect_ref (resdecl);
1425 else if (!is_gimple_reg_type (restype))
1427 restmp = resdecl;
1428 add_local_decl (cfun, restmp);
1429 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1431 else
1432 restmp = create_tmp_reg (restype, "retval");
1435 for (arg = a; arg; arg = DECL_CHAIN (arg))
1436 nargs++;
1437 vargs.create (nargs);
1438 if (this_adjusting)
1439 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1440 virtual_offset));
1441 else if (nargs)
1442 vargs.quick_push (a);
1444 if (nargs)
1445 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1446 vargs.quick_push (arg);
1447 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1448 vargs.release ();
1449 gimple_call_set_from_thunk (call, true);
1450 if (restmp)
1452 gimple_call_set_lhs (call, restmp);
1453 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1454 TREE_TYPE (TREE_TYPE (alias))));
1456 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1457 if (!(gimple_call_flags (call) & ECF_NORETURN))
1459 if (restmp && !this_adjusting
1460 && (fixed_offset || virtual_offset))
1462 tree true_label = NULL_TREE;
1464 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1466 gimple stmt;
1467 /* If the return type is a pointer, we need to
1468 protect against NULL. We know there will be an
1469 adjustment, because that's why we're emitting a
1470 thunk. */
1471 then_bb = create_basic_block (NULL, (void *) 0, bb);
1472 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1473 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1474 add_bb_to_loop (then_bb, bb->loop_father);
1475 add_bb_to_loop (return_bb, bb->loop_father);
1476 add_bb_to_loop (else_bb, bb->loop_father);
1477 remove_edge (single_succ_edge (bb));
1478 true_label = gimple_block_label (then_bb);
1479 stmt = gimple_build_cond (NE_EXPR, restmp,
1480 build_zero_cst (TREE_TYPE (restmp)),
1481 NULL_TREE, NULL_TREE);
1482 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1483 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1484 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1485 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1486 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1487 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1488 bsi = gsi_last_bb (then_bb);
1491 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1492 fixed_offset, virtual_offset);
1493 if (true_label)
1495 gimple stmt;
1496 bsi = gsi_last_bb (else_bb);
1497 stmt = gimple_build_assign (restmp,
1498 build_zero_cst (TREE_TYPE (restmp)));
1499 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1500 bsi = gsi_last_bb (return_bb);
1503 else
1504 gimple_call_set_tail (call, true);
1506 /* Build return value. */
1507 ret = gimple_build_return (restmp);
1508 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1510 else
1512 gimple_call_set_tail (call, true);
1513 remove_edge (single_succ_edge (bb));
1516 delete_unreachable_blocks ();
1517 update_ssa (TODO_update_ssa);
1518 #ifdef ENABLE_CHECKING
1519 verify_flow_info ();
1520 #endif
1522 /* Since we want to emit the thunk, we explicitly mark its name as
1523 referenced. */
1524 node->thunk.thunk_p = false;
1525 rebuild_cgraph_edges ();
1526 cgraph_add_new_function (thunk_fndecl, true);
1527 bitmap_obstack_release (NULL);
1529 current_function_decl = NULL;
1530 set_cfun (NULL);
1533 /* Assemble thunks and aliases associated to NODE. */
1535 static void
1536 assemble_thunks_and_aliases (struct cgraph_node *node)
1538 struct cgraph_edge *e;
1539 int i;
1540 struct ipa_ref *ref;
1542 for (e = node->callers; e;)
1543 if (e->caller->thunk.thunk_p)
1545 struct cgraph_node *thunk = e->caller;
1547 e = e->next_caller;
1548 assemble_thunks_and_aliases (thunk);
1549 expand_thunk (thunk);
1551 else
1552 e = e->next_caller;
1553 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
1554 i, ref); i++)
1555 if (ref->use == IPA_REF_ALIAS)
1557 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1558 bool saved_written = TREE_ASM_WRITTEN (node->symbol.decl);
1560 /* Force assemble_alias to really output the alias this time instead
1561 of buffering it in same alias pairs. */
1562 TREE_ASM_WRITTEN (node->symbol.decl) = 1;
1563 do_assemble_alias (alias->symbol.decl,
1564 DECL_ASSEMBLER_NAME (node->symbol.decl));
1565 assemble_thunks_and_aliases (alias);
1566 TREE_ASM_WRITTEN (node->symbol.decl) = saved_written;
1570 /* Expand function specified by NODE. */
1572 static void
1573 expand_function (struct cgraph_node *node)
1575 tree decl = node->symbol.decl;
1576 location_t saved_loc;
1578 /* We ought to not compile any inline clones. */
1579 gcc_assert (!node->global.inlined_to);
1581 announce_function (decl);
1582 node->process = 0;
1583 gcc_assert (node->lowered);
1584 cgraph_get_body (node);
1586 /* Generate RTL for the body of DECL. */
1588 timevar_push (TV_REST_OF_COMPILATION);
1590 gcc_assert (cgraph_global_info_ready);
1592 /* Initialize the default bitmap obstack. */
1593 bitmap_obstack_initialize (NULL);
1595 /* Initialize the RTL code for the function. */
1596 current_function_decl = decl;
1597 saved_loc = input_location;
1598 input_location = DECL_SOURCE_LOCATION (decl);
1599 init_function_start (decl);
1601 gimple_register_cfg_hooks ();
1603 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1605 execute_all_ipa_transforms ();
1607 /* Perform all tree transforms and optimizations. */
1609 /* Signal the start of passes. */
1610 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1612 execute_pass_list (g->get_passes ()->all_passes);
1614 /* Signal the end of passes. */
1615 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1617 bitmap_obstack_release (&reg_obstack);
1619 /* Release the default bitmap obstack. */
1620 bitmap_obstack_release (NULL);
1622 /* If requested, warn about function definitions where the function will
1623 return a value (usually of some struct or union type) which itself will
1624 take up a lot of stack space. */
1625 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1627 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1629 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1630 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1631 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1632 larger_than_size))
1634 unsigned int size_as_int
1635 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1637 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1638 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1639 decl, size_as_int);
1640 else
1641 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1642 decl, larger_than_size);
1646 gimple_set_body (decl, NULL);
1647 if (DECL_STRUCT_FUNCTION (decl) == 0
1648 && !cgraph_get_node (decl)->origin)
1650 /* Stop pointing to the local nodes about to be freed.
1651 But DECL_INITIAL must remain nonzero so we know this
1652 was an actual function definition.
1653 For a nested function, this is done in c_pop_function_context.
1654 If rest_of_compilation set this to 0, leave it 0. */
1655 if (DECL_INITIAL (decl) != 0)
1656 DECL_INITIAL (decl) = error_mark_node;
1659 input_location = saved_loc;
1661 ggc_collect ();
1662 timevar_pop (TV_REST_OF_COMPILATION);
1664 /* Make sure that BE didn't give up on compiling. */
1665 gcc_assert (TREE_ASM_WRITTEN (decl));
1666 set_cfun (NULL);
1667 current_function_decl = NULL;
1669 /* It would make a lot more sense to output thunks before function body to get more
1670 forward and lest backwarding jumps. This however would need solving problem
1671 with comdats. See PR48668. Also aliases must come after function itself to
1672 make one pass assemblers, like one on AIX, happy. See PR 50689.
1673 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1674 groups. */
1675 assemble_thunks_and_aliases (node);
1676 cgraph_release_function_body (node);
1677 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1678 points to the dead function body. */
1679 cgraph_node_remove_callees (node);
1683 /* Expand all functions that must be output.
1685 Attempt to topologically sort the nodes so function is output when
1686 all called functions are already assembled to allow data to be
1687 propagated across the callgraph. Use a stack to get smaller distance
1688 between a function and its callees (later we may choose to use a more
1689 sophisticated algorithm for function reordering; we will likely want
1690 to use subsections to make the output functions appear in top-down
1691 order). */
1693 static void
1694 expand_all_functions (void)
1696 struct cgraph_node *node;
1697 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1698 int order_pos, new_order_pos = 0;
1699 int i;
1701 order_pos = ipa_reverse_postorder (order);
1702 gcc_assert (order_pos == cgraph_n_nodes);
1704 /* Garbage collector may remove inline clones we eliminate during
1705 optimization. So we must be sure to not reference them. */
1706 for (i = 0; i < order_pos; i++)
1707 if (order[i]->process)
1708 order[new_order_pos++] = order[i];
1710 for (i = new_order_pos - 1; i >= 0; i--)
1712 node = order[i];
1713 if (node->process)
1715 node->process = 0;
1716 expand_function (node);
1719 cgraph_process_new_functions ();
1721 free (order);
1725 /* This is used to sort the node types by the cgraph order number. */
1727 enum cgraph_order_sort_kind
1729 ORDER_UNDEFINED = 0,
1730 ORDER_FUNCTION,
1731 ORDER_VAR,
1732 ORDER_ASM
1735 struct cgraph_order_sort
1737 enum cgraph_order_sort_kind kind;
1738 union
1740 struct cgraph_node *f;
1741 struct varpool_node *v;
1742 struct asm_node *a;
1743 } u;
1746 /* Output all functions, variables, and asm statements in the order
1747 according to their order fields, which is the order in which they
1748 appeared in the file. This implements -fno-toplevel-reorder. In
1749 this mode we may output functions and variables which don't really
1750 need to be output. */
1752 static void
1753 output_in_order (void)
1755 int max;
1756 struct cgraph_order_sort *nodes;
1757 int i;
1758 struct cgraph_node *pf;
1759 struct varpool_node *pv;
1760 struct asm_node *pa;
1762 max = symtab_order;
1763 nodes = XCNEWVEC (struct cgraph_order_sort, max);
1765 FOR_EACH_DEFINED_FUNCTION (pf)
1767 if (pf->process && !pf->thunk.thunk_p && !pf->symbol.alias)
1769 i = pf->symbol.order;
1770 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1771 nodes[i].kind = ORDER_FUNCTION;
1772 nodes[i].u.f = pf;
1776 FOR_EACH_DEFINED_VARIABLE (pv)
1777 if (!DECL_EXTERNAL (pv->symbol.decl))
1779 i = pv->symbol.order;
1780 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1781 nodes[i].kind = ORDER_VAR;
1782 nodes[i].u.v = pv;
1785 for (pa = asm_nodes; pa; pa = pa->next)
1787 i = pa->order;
1788 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1789 nodes[i].kind = ORDER_ASM;
1790 nodes[i].u.a = pa;
1793 /* In toplevel reorder mode we output all statics; mark them as needed. */
1795 for (i = 0; i < max; ++i)
1796 if (nodes[i].kind == ORDER_VAR)
1797 varpool_finalize_named_section_flags (nodes[i].u.v);
1799 for (i = 0; i < max; ++i)
1801 switch (nodes[i].kind)
1803 case ORDER_FUNCTION:
1804 nodes[i].u.f->process = 0;
1805 expand_function (nodes[i].u.f);
1806 break;
1808 case ORDER_VAR:
1809 varpool_assemble_decl (nodes[i].u.v);
1810 break;
1812 case ORDER_ASM:
1813 assemble_asm (nodes[i].u.a->asm_str);
1814 break;
1816 case ORDER_UNDEFINED:
1817 break;
1819 default:
1820 gcc_unreachable ();
1824 asm_nodes = NULL;
1825 free (nodes);
1828 static void
1829 ipa_passes (void)
1831 gcc::pass_manager *passes = g->get_passes ();
1833 set_cfun (NULL);
1834 current_function_decl = NULL;
1835 gimple_register_cfg_hooks ();
1836 bitmap_obstack_initialize (NULL);
1838 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1840 if (!in_lto_p)
1842 execute_ipa_pass_list (passes->all_small_ipa_passes);
1843 if (seen_error ())
1844 return;
1847 /* We never run removal of unreachable nodes after early passes. This is
1848 because TODO is run before the subpasses. It is important to remove
1849 the unreachable functions to save works at IPA level and to get LTO
1850 symbol tables right. */
1851 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
1853 /* If pass_all_early_optimizations was not scheduled, the state of
1854 the cgraph will not be properly updated. Update it now. */
1855 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1856 cgraph_state = CGRAPH_STATE_IPA_SSA;
1858 if (!in_lto_p)
1860 /* Generate coverage variables and constructors. */
1861 coverage_finish ();
1863 /* Process new functions added. */
1864 set_cfun (NULL);
1865 current_function_decl = NULL;
1866 cgraph_process_new_functions ();
1868 execute_ipa_summary_passes
1869 ((struct ipa_opt_pass_d *) passes->all_regular_ipa_passes);
1872 /* Some targets need to handle LTO assembler output specially. */
1873 if (flag_generate_lto)
1874 targetm.asm_out.lto_start ();
1876 execute_ipa_summary_passes ((struct ipa_opt_pass_d *)
1877 passes->all_lto_gen_passes);
1879 if (!in_lto_p)
1880 ipa_write_summaries ();
1882 if (flag_generate_lto)
1883 targetm.asm_out.lto_end ();
1885 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
1886 execute_ipa_pass_list (passes->all_regular_ipa_passes);
1887 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
1889 bitmap_obstack_release (NULL);
1893 /* Return string alias is alias of. */
1895 static tree
1896 get_alias_symbol (tree decl)
1898 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1899 return get_identifier (TREE_STRING_POINTER
1900 (TREE_VALUE (TREE_VALUE (alias))));
1904 /* Weakrefs may be associated to external decls and thus not output
1905 at expansion time. Emit all necessary aliases. */
1907 static void
1908 output_weakrefs (void)
1910 symtab_node node;
1911 FOR_EACH_SYMBOL (node)
1912 if (node->symbol.alias
1913 && !TREE_ASM_WRITTEN (node->symbol.decl)
1914 && node->symbol.weakref)
1916 tree target;
1918 /* Weakrefs are special by not requiring target definition in current
1919 compilation unit. It is thus bit hard to work out what we want to
1920 alias.
1921 When alias target is defined, we need to fetch it from symtab reference,
1922 otherwise it is pointed to by alias_target. */
1923 if (node->symbol.alias_target)
1924 target = (DECL_P (node->symbol.alias_target)
1925 ? DECL_ASSEMBLER_NAME (node->symbol.alias_target)
1926 : node->symbol.alias_target);
1927 else if (node->symbol.analyzed)
1928 target = DECL_ASSEMBLER_NAME (symtab_alias_target (node)->symbol.decl);
1929 else
1931 gcc_unreachable ();
1932 target = get_alias_symbol (node->symbol.decl);
1934 do_assemble_alias (node->symbol.decl, target);
1938 /* Initialize callgraph dump file. */
1940 void
1941 init_cgraph (void)
1943 if (!cgraph_dump_file)
1944 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1948 /* Perform simple optimizations based on callgraph. */
1950 void
1951 compile (void)
1953 if (seen_error ())
1954 return;
1956 #ifdef ENABLE_CHECKING
1957 verify_symtab ();
1958 #endif
1960 timevar_push (TV_CGRAPHOPT);
1961 if (pre_ipa_mem_report)
1963 fprintf (stderr, "Memory consumption before IPA\n");
1964 dump_memory_report (false);
1966 if (!quiet_flag)
1967 fprintf (stderr, "Performing interprocedural optimizations\n");
1968 cgraph_state = CGRAPH_STATE_IPA;
1970 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1971 if (flag_lto)
1972 lto_streamer_hooks_init ();
1974 /* Don't run the IPA passes if there was any error or sorry messages. */
1975 if (!seen_error ())
1976 ipa_passes ();
1978 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
1979 if (seen_error ()
1980 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
1982 timevar_pop (TV_CGRAPHOPT);
1983 return;
1986 /* This pass remove bodies of extern inline functions we never inlined.
1987 Do this later so other IPA passes see what is really going on. */
1988 symtab_remove_unreachable_nodes (false, dump_file);
1989 cgraph_global_info_ready = true;
1990 if (cgraph_dump_file)
1992 fprintf (cgraph_dump_file, "Optimized ");
1993 dump_symtab (cgraph_dump_file);
1995 if (post_ipa_mem_report)
1997 fprintf (stderr, "Memory consumption after IPA\n");
1998 dump_memory_report (false);
2000 timevar_pop (TV_CGRAPHOPT);
2002 /* Output everything. */
2003 (*debug_hooks->assembly_start) ();
2004 if (!quiet_flag)
2005 fprintf (stderr, "Assembling functions:\n");
2006 #ifdef ENABLE_CHECKING
2007 verify_symtab ();
2008 #endif
2010 cgraph_materialize_all_clones ();
2011 bitmap_obstack_initialize (NULL);
2012 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2013 symtab_remove_unreachable_nodes (true, dump_file);
2014 #ifdef ENABLE_CHECKING
2015 verify_symtab ();
2016 #endif
2017 bitmap_obstack_release (NULL);
2018 mark_functions_to_output ();
2020 /* When weakref support is missing, we autmatically translate all
2021 references to NODE to references to its ultimate alias target.
2022 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2023 TREE_CHAIN.
2025 Set up this mapping before we output any assembler but once we are sure
2026 that all symbol renaming is done.
2028 FIXME: All this uglyness can go away if we just do renaming at gimple
2029 level by physically rewritting the IL. At the moment we can only redirect
2030 calls, so we need infrastructure for renaming references as well. */
2031 #ifndef ASM_OUTPUT_WEAKREF
2032 symtab_node node;
2034 FOR_EACH_SYMBOL (node)
2035 if (node->symbol.alias
2036 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->symbol.decl)))
2038 IDENTIFIER_TRANSPARENT_ALIAS
2039 (DECL_ASSEMBLER_NAME (node->symbol.decl)) = 1;
2040 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->symbol.decl))
2041 = (node->symbol.alias_target ? node->symbol.alias_target
2042 : DECL_ASSEMBLER_NAME (symtab_alias_target (node)->symbol.decl));
2044 #endif
2046 cgraph_state = CGRAPH_STATE_EXPANSION;
2047 if (!flag_toplevel_reorder)
2048 output_in_order ();
2049 else
2051 output_asm_statements ();
2053 expand_all_functions ();
2054 varpool_output_variables ();
2057 cgraph_process_new_functions ();
2058 cgraph_state = CGRAPH_STATE_FINISHED;
2059 output_weakrefs ();
2061 if (cgraph_dump_file)
2063 fprintf (cgraph_dump_file, "\nFinal ");
2064 dump_symtab (cgraph_dump_file);
2066 #ifdef ENABLE_CHECKING
2067 verify_symtab ();
2068 /* Double check that all inline clones are gone and that all
2069 function bodies have been released from memory. */
2070 if (!seen_error ())
2072 struct cgraph_node *node;
2073 bool error_found = false;
2075 FOR_EACH_DEFINED_FUNCTION (node)
2076 if (node->global.inlined_to
2077 || gimple_has_body_p (node->symbol.decl))
2079 error_found = true;
2080 dump_cgraph_node (stderr, node);
2082 if (error_found)
2083 internal_error ("nodes with unreleased memory found");
2085 #endif
2089 /* Analyze the whole compilation unit once it is parsed completely. */
2091 void
2092 finalize_compilation_unit (void)
2094 timevar_push (TV_CGRAPH);
2096 /* If we're here there's no current function anymore. Some frontends
2097 are lazy in clearing these. */
2098 current_function_decl = NULL;
2099 set_cfun (NULL);
2101 /* Do not skip analyzing the functions if there were errors, we
2102 miss diagnostics for following functions otherwise. */
2104 /* Emit size functions we didn't inline. */
2105 finalize_size_functions ();
2107 /* Mark alias targets necessary and emit diagnostics. */
2108 handle_alias_pairs ();
2110 if (!quiet_flag)
2112 fprintf (stderr, "\nAnalyzing compilation unit\n");
2113 fflush (stderr);
2116 if (flag_dump_passes)
2117 dump_passes ();
2119 /* Gimplify and lower all functions, compute reachability and
2120 remove unreachable nodes. */
2121 analyze_functions ();
2123 /* Mark alias targets necessary and emit diagnostics. */
2124 handle_alias_pairs ();
2126 /* Gimplify and lower thunks. */
2127 analyze_functions ();
2129 /* Finally drive the pass manager. */
2130 compile ();
2132 timevar_pop (TV_CGRAPH);
2136 #include "gt-cgraphunit.h"