2013-08-02 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cgraphunit.c
blobde7bb93d2c37b2b535963935f290365e780b9a73
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 /* When not optimizing, be sure we run early local passes anyway
329 to expand OMP. */
330 || !optimize)
331 execute_pass_list (pass_early_local_passes.pass.sub);
332 else
333 compute_inline_parameters (node, true);
334 free_dominance_info (CDI_POST_DOMINATORS);
335 free_dominance_info (CDI_DOMINATORS);
336 pop_cfun ();
337 cgraph_call_function_insertion_hooks (node);
338 break;
340 case CGRAPH_STATE_EXPANSION:
341 /* Functions created during expansion shall be compiled
342 directly. */
343 node->process = 0;
344 cgraph_call_function_insertion_hooks (node);
345 expand_function (node);
346 break;
348 default:
349 gcc_unreachable ();
350 break;
353 free_cgraph_node_set (cgraph_new_nodes);
354 cgraph_new_nodes = NULL;
355 return output;
358 /* As an GCC extension we allow redefinition of the function. The
359 semantics when both copies of bodies differ is not well defined.
360 We replace the old body with new body so in unit at a time mode
361 we always use new body, while in normal mode we may end up with
362 old body inlined into some functions and new body expanded and
363 inlined in others.
365 ??? It may make more sense to use one body for inlining and other
366 body for expanding the function but this is difficult to do. */
368 void
369 cgraph_reset_node (struct cgraph_node *node)
371 /* If node->process is set, then we have already begun whole-unit analysis.
372 This is *not* testing for whether we've already emitted the function.
373 That case can be sort-of legitimately seen with real function redefinition
374 errors. I would argue that the front end should never present us with
375 such a case, but don't enforce that for now. */
376 gcc_assert (!node->process);
378 /* Reset our data structures so we can analyze the function again. */
379 memset (&node->local, 0, sizeof (node->local));
380 memset (&node->global, 0, sizeof (node->global));
381 memset (&node->rtl, 0, sizeof (node->rtl));
382 node->symbol.analyzed = false;
383 node->symbol.definition = false;
384 node->symbol.alias = false;
385 node->symbol.weakref = false;
386 node->symbol.cpp_implicit_alias = false;
388 cgraph_node_remove_callees (node);
389 ipa_remove_all_references (&node->symbol.ref_list);
392 /* Return true when there are references to NODE. */
394 static bool
395 referred_to_p (symtab_node node)
397 struct ipa_ref *ref;
399 /* See if there are any references at all. */
400 if (ipa_ref_list_referring_iterate (&node->symbol.ref_list, 0, ref))
401 return true;
402 /* For functions check also calls. */
403 cgraph_node *cn = dyn_cast <cgraph_node> (node);
404 if (cn && cn->callers)
405 return true;
406 return false;
409 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
410 logic in effect. If NESTED is true, then our caller cannot stand to have
411 the garbage collector run at the moment. We would need to either create
412 a new GC context, or just not compile right now. */
414 void
415 cgraph_finalize_function (tree decl, bool nested)
417 struct cgraph_node *node = cgraph_get_create_node (decl);
419 if (node->symbol.definition)
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 (!nested)
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 execute_pass_list (pass_early_local_passes.pass.sub);
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 execute_pass_list (pass_early_local_passes.pass.sub);
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 static void
1331 assemble_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 (!is_gimple_reg_type (restype))
1425 restmp = resdecl;
1426 add_local_decl (cfun, restmp);
1427 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1429 else
1430 restmp = create_tmp_reg (restype, "retval");
1433 for (arg = a; arg; arg = DECL_CHAIN (arg))
1434 nargs++;
1435 vargs.create (nargs);
1436 if (this_adjusting)
1437 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1438 virtual_offset));
1439 else
1440 vargs.quick_push (a);
1441 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1442 vargs.quick_push (arg);
1443 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1444 vargs.release ();
1445 gimple_call_set_from_thunk (call, true);
1446 if (restmp)
1447 gimple_call_set_lhs (call, restmp);
1448 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1450 if (restmp && !this_adjusting)
1452 tree true_label = NULL_TREE;
1454 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1456 gimple stmt;
1457 /* If the return type is a pointer, we need to
1458 protect against NULL. We know there will be an
1459 adjustment, because that's why we're emitting a
1460 thunk. */
1461 then_bb = create_basic_block (NULL, (void *) 0, bb);
1462 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1463 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1464 add_bb_to_loop (then_bb, bb->loop_father);
1465 add_bb_to_loop (return_bb, bb->loop_father);
1466 add_bb_to_loop (else_bb, bb->loop_father);
1467 remove_edge (single_succ_edge (bb));
1468 true_label = gimple_block_label (then_bb);
1469 stmt = gimple_build_cond (NE_EXPR, restmp,
1470 build_zero_cst (TREE_TYPE (restmp)),
1471 NULL_TREE, NULL_TREE);
1472 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1473 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1474 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1475 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1476 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1477 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1478 bsi = gsi_last_bb (then_bb);
1481 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1482 fixed_offset, virtual_offset);
1483 if (true_label)
1485 gimple stmt;
1486 bsi = gsi_last_bb (else_bb);
1487 stmt = gimple_build_assign (restmp,
1488 build_zero_cst (TREE_TYPE (restmp)));
1489 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1490 bsi = gsi_last_bb (return_bb);
1493 else
1494 gimple_call_set_tail (call, true);
1496 /* Build return value. */
1497 ret = gimple_build_return (restmp);
1498 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1500 delete_unreachable_blocks ();
1501 update_ssa (TODO_update_ssa);
1503 /* Since we want to emit the thunk, we explicitly mark its name as
1504 referenced. */
1505 node->thunk.thunk_p = false;
1506 cgraph_node_remove_callees (node);
1507 cgraph_add_new_function (thunk_fndecl, true);
1508 bitmap_obstack_release (NULL);
1510 current_function_decl = NULL;
1511 set_cfun (NULL);
1516 /* Assemble thunks and aliases associated to NODE. */
1518 static void
1519 assemble_thunks_and_aliases (struct cgraph_node *node)
1521 struct cgraph_edge *e;
1522 int i;
1523 struct ipa_ref *ref;
1525 for (e = node->callers; e;)
1526 if (e->caller->thunk.thunk_p)
1528 struct cgraph_node *thunk = e->caller;
1530 e = e->next_caller;
1531 assemble_thunks_and_aliases (thunk);
1532 assemble_thunk (thunk);
1534 else
1535 e = e->next_caller;
1536 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
1537 i, ref); i++)
1538 if (ref->use == IPA_REF_ALIAS)
1540 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1541 bool saved_written = TREE_ASM_WRITTEN (node->symbol.decl);
1543 /* Force assemble_alias to really output the alias this time instead
1544 of buffering it in same alias pairs. */
1545 TREE_ASM_WRITTEN (node->symbol.decl) = 1;
1546 do_assemble_alias (alias->symbol.decl,
1547 DECL_ASSEMBLER_NAME (node->symbol.decl));
1548 assemble_thunks_and_aliases (alias);
1549 TREE_ASM_WRITTEN (node->symbol.decl) = saved_written;
1553 /* Expand function specified by NODE. */
1555 static void
1556 expand_function (struct cgraph_node *node)
1558 tree decl = node->symbol.decl;
1559 location_t saved_loc;
1561 /* We ought to not compile any inline clones. */
1562 gcc_assert (!node->global.inlined_to);
1564 announce_function (decl);
1565 node->process = 0;
1566 gcc_assert (node->lowered);
1568 /* Generate RTL for the body of DECL. */
1570 timevar_push (TV_REST_OF_COMPILATION);
1572 gcc_assert (cgraph_global_info_ready);
1574 /* Initialize the default bitmap obstack. */
1575 bitmap_obstack_initialize (NULL);
1577 /* Initialize the RTL code for the function. */
1578 current_function_decl = decl;
1579 saved_loc = input_location;
1580 input_location = DECL_SOURCE_LOCATION (decl);
1581 init_function_start (decl);
1583 gimple_register_cfg_hooks ();
1585 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1587 execute_all_ipa_transforms ();
1589 /* Perform all tree transforms and optimizations. */
1591 /* Signal the start of passes. */
1592 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1594 execute_pass_list (g->get_passes ()->all_passes);
1596 /* Signal the end of passes. */
1597 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1599 bitmap_obstack_release (&reg_obstack);
1601 /* Release the default bitmap obstack. */
1602 bitmap_obstack_release (NULL);
1604 /* If requested, warn about function definitions where the function will
1605 return a value (usually of some struct or union type) which itself will
1606 take up a lot of stack space. */
1607 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1609 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1611 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1612 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1613 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1614 larger_than_size))
1616 unsigned int size_as_int
1617 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1619 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1620 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1621 decl, size_as_int);
1622 else
1623 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1624 decl, larger_than_size);
1628 gimple_set_body (decl, NULL);
1629 if (DECL_STRUCT_FUNCTION (decl) == 0
1630 && !cgraph_get_node (decl)->origin)
1632 /* Stop pointing to the local nodes about to be freed.
1633 But DECL_INITIAL must remain nonzero so we know this
1634 was an actual function definition.
1635 For a nested function, this is done in c_pop_function_context.
1636 If rest_of_compilation set this to 0, leave it 0. */
1637 if (DECL_INITIAL (decl) != 0)
1638 DECL_INITIAL (decl) = error_mark_node;
1641 input_location = saved_loc;
1643 ggc_collect ();
1644 timevar_pop (TV_REST_OF_COMPILATION);
1646 /* Make sure that BE didn't give up on compiling. */
1647 gcc_assert (TREE_ASM_WRITTEN (decl));
1648 set_cfun (NULL);
1649 current_function_decl = NULL;
1651 /* It would make a lot more sense to output thunks before function body to get more
1652 forward and lest backwarding jumps. This however would need solving problem
1653 with comdats. See PR48668. Also aliases must come after function itself to
1654 make one pass assemblers, like one on AIX, happy. See PR 50689.
1655 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1656 groups. */
1657 assemble_thunks_and_aliases (node);
1658 cgraph_release_function_body (node);
1659 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1660 points to the dead function body. */
1661 cgraph_node_remove_callees (node);
1665 /* Expand all functions that must be output.
1667 Attempt to topologically sort the nodes so function is output when
1668 all called functions are already assembled to allow data to be
1669 propagated across the callgraph. Use a stack to get smaller distance
1670 between a function and its callees (later we may choose to use a more
1671 sophisticated algorithm for function reordering; we will likely want
1672 to use subsections to make the output functions appear in top-down
1673 order). */
1675 static void
1676 expand_all_functions (void)
1678 struct cgraph_node *node;
1679 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1680 int order_pos, new_order_pos = 0;
1681 int i;
1683 order_pos = ipa_reverse_postorder (order);
1684 gcc_assert (order_pos == cgraph_n_nodes);
1686 /* Garbage collector may remove inline clones we eliminate during
1687 optimization. So we must be sure to not reference them. */
1688 for (i = 0; i < order_pos; i++)
1689 if (order[i]->process)
1690 order[new_order_pos++] = order[i];
1692 for (i = new_order_pos - 1; i >= 0; i--)
1694 node = order[i];
1695 if (node->process)
1697 node->process = 0;
1698 expand_function (node);
1701 cgraph_process_new_functions ();
1703 free (order);
1707 /* This is used to sort the node types by the cgraph order number. */
1709 enum cgraph_order_sort_kind
1711 ORDER_UNDEFINED = 0,
1712 ORDER_FUNCTION,
1713 ORDER_VAR,
1714 ORDER_ASM
1717 struct cgraph_order_sort
1719 enum cgraph_order_sort_kind kind;
1720 union
1722 struct cgraph_node *f;
1723 struct varpool_node *v;
1724 struct asm_node *a;
1725 } u;
1728 /* Output all functions, variables, and asm statements in the order
1729 according to their order fields, which is the order in which they
1730 appeared in the file. This implements -fno-toplevel-reorder. In
1731 this mode we may output functions and variables which don't really
1732 need to be output. */
1734 static void
1735 output_in_order (void)
1737 int max;
1738 struct cgraph_order_sort *nodes;
1739 int i;
1740 struct cgraph_node *pf;
1741 struct varpool_node *pv;
1742 struct asm_node *pa;
1744 max = symtab_order;
1745 nodes = XCNEWVEC (struct cgraph_order_sort, max);
1747 FOR_EACH_DEFINED_FUNCTION (pf)
1749 if (pf->process && !pf->thunk.thunk_p && !pf->symbol.alias)
1751 i = pf->symbol.order;
1752 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1753 nodes[i].kind = ORDER_FUNCTION;
1754 nodes[i].u.f = pf;
1758 FOR_EACH_DEFINED_VARIABLE (pv)
1759 if (!DECL_EXTERNAL (pv->symbol.decl))
1761 i = pv->symbol.order;
1762 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1763 nodes[i].kind = ORDER_VAR;
1764 nodes[i].u.v = pv;
1767 for (pa = asm_nodes; pa; pa = pa->next)
1769 i = pa->order;
1770 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1771 nodes[i].kind = ORDER_ASM;
1772 nodes[i].u.a = pa;
1775 /* In toplevel reorder mode we output all statics; mark them as needed. */
1777 for (i = 0; i < max; ++i)
1778 if (nodes[i].kind == ORDER_VAR)
1779 varpool_finalize_named_section_flags (nodes[i].u.v);
1781 for (i = 0; i < max; ++i)
1783 switch (nodes[i].kind)
1785 case ORDER_FUNCTION:
1786 nodes[i].u.f->process = 0;
1787 expand_function (nodes[i].u.f);
1788 break;
1790 case ORDER_VAR:
1791 varpool_assemble_decl (nodes[i].u.v);
1792 break;
1794 case ORDER_ASM:
1795 assemble_asm (nodes[i].u.a->asm_str);
1796 break;
1798 case ORDER_UNDEFINED:
1799 break;
1801 default:
1802 gcc_unreachable ();
1806 asm_nodes = NULL;
1807 free (nodes);
1810 static void
1811 ipa_passes (void)
1813 gcc::pass_manager *passes = g->get_passes ();
1815 set_cfun (NULL);
1816 current_function_decl = NULL;
1817 gimple_register_cfg_hooks ();
1818 bitmap_obstack_initialize (NULL);
1820 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1822 if (!in_lto_p)
1824 execute_ipa_pass_list (passes->all_small_ipa_passes);
1825 if (seen_error ())
1826 return;
1829 /* We never run removal of unreachable nodes after early passes. This is
1830 because TODO is run before the subpasses. It is important to remove
1831 the unreachable functions to save works at IPA level and to get LTO
1832 symbol tables right. */
1833 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
1835 /* If pass_all_early_optimizations was not scheduled, the state of
1836 the cgraph will not be properly updated. Update it now. */
1837 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1838 cgraph_state = CGRAPH_STATE_IPA_SSA;
1840 if (!in_lto_p)
1842 /* Generate coverage variables and constructors. */
1843 coverage_finish ();
1845 /* Process new functions added. */
1846 set_cfun (NULL);
1847 current_function_decl = NULL;
1848 cgraph_process_new_functions ();
1850 execute_ipa_summary_passes
1851 ((struct ipa_opt_pass_d *) passes->all_regular_ipa_passes);
1854 /* Some targets need to handle LTO assembler output specially. */
1855 if (flag_generate_lto)
1856 targetm.asm_out.lto_start ();
1858 execute_ipa_summary_passes ((struct ipa_opt_pass_d *)
1859 passes->all_lto_gen_passes);
1861 if (!in_lto_p)
1862 ipa_write_summaries ();
1864 if (flag_generate_lto)
1865 targetm.asm_out.lto_end ();
1867 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
1868 execute_ipa_pass_list (passes->all_regular_ipa_passes);
1869 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
1871 bitmap_obstack_release (NULL);
1875 /* Return string alias is alias of. */
1877 static tree
1878 get_alias_symbol (tree decl)
1880 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1881 return get_identifier (TREE_STRING_POINTER
1882 (TREE_VALUE (TREE_VALUE (alias))));
1886 /* Weakrefs may be associated to external decls and thus not output
1887 at expansion time. Emit all necessary aliases. */
1889 static void
1890 output_weakrefs (void)
1892 symtab_node node;
1893 FOR_EACH_SYMBOL (node)
1894 if (node->symbol.alias
1895 && !TREE_ASM_WRITTEN (node->symbol.decl)
1896 && node->symbol.weakref)
1898 tree target;
1900 /* Weakrefs are special by not requiring target definition in current
1901 compilation unit. It is thus bit hard to work out what we want to
1902 alias.
1903 When alias target is defined, we need to fetch it from symtab reference,
1904 otherwise it is pointed to by alias_target. */
1905 if (node->symbol.alias_target)
1906 target = (DECL_P (node->symbol.alias_target)
1907 ? DECL_ASSEMBLER_NAME (node->symbol.alias_target)
1908 : node->symbol.alias_target);
1909 else if (node->symbol.analyzed)
1910 target = DECL_ASSEMBLER_NAME (symtab_alias_target (node)->symbol.decl);
1911 else
1913 gcc_unreachable ();
1914 target = get_alias_symbol (node->symbol.decl);
1916 do_assemble_alias (node->symbol.decl, target);
1920 /* Initialize callgraph dump file. */
1922 void
1923 init_cgraph (void)
1925 if (!cgraph_dump_file)
1926 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1930 /* Perform simple optimizations based on callgraph. */
1932 void
1933 compile (void)
1935 if (seen_error ())
1936 return;
1938 #ifdef ENABLE_CHECKING
1939 verify_symtab ();
1940 #endif
1942 timevar_push (TV_CGRAPHOPT);
1943 if (pre_ipa_mem_report)
1945 fprintf (stderr, "Memory consumption before IPA\n");
1946 dump_memory_report (false);
1948 if (!quiet_flag)
1949 fprintf (stderr, "Performing interprocedural optimizations\n");
1950 cgraph_state = CGRAPH_STATE_IPA;
1952 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1953 if (flag_lto)
1954 lto_streamer_hooks_init ();
1956 /* Don't run the IPA passes if there was any error or sorry messages. */
1957 if (!seen_error ())
1958 ipa_passes ();
1960 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
1961 if (seen_error ()
1962 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
1964 timevar_pop (TV_CGRAPHOPT);
1965 return;
1968 /* This pass remove bodies of extern inline functions we never inlined.
1969 Do this later so other IPA passes see what is really going on. */
1970 symtab_remove_unreachable_nodes (false, dump_file);
1971 cgraph_global_info_ready = true;
1972 if (cgraph_dump_file)
1974 fprintf (cgraph_dump_file, "Optimized ");
1975 dump_symtab (cgraph_dump_file);
1977 if (post_ipa_mem_report)
1979 fprintf (stderr, "Memory consumption after IPA\n");
1980 dump_memory_report (false);
1982 timevar_pop (TV_CGRAPHOPT);
1984 /* Output everything. */
1985 (*debug_hooks->assembly_start) ();
1986 if (!quiet_flag)
1987 fprintf (stderr, "Assembling functions:\n");
1988 #ifdef ENABLE_CHECKING
1989 verify_symtab ();
1990 #endif
1992 cgraph_materialize_all_clones ();
1993 bitmap_obstack_initialize (NULL);
1994 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
1995 symtab_remove_unreachable_nodes (true, dump_file);
1996 #ifdef ENABLE_CHECKING
1997 verify_symtab ();
1998 #endif
1999 bitmap_obstack_release (NULL);
2000 mark_functions_to_output ();
2002 /* When weakref support is missing, we autmatically translate all
2003 references to NODE to references to its ultimate alias target.
2004 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2005 TREE_CHAIN.
2007 Set up this mapping before we output any assembler but once we are sure
2008 that all symbol renaming is done.
2010 FIXME: All this uglyness can go away if we just do renaming at gimple
2011 level by physically rewritting the IL. At the moment we can only redirect
2012 calls, so we need infrastructure for renaming references as well. */
2013 #ifndef ASM_OUTPUT_WEAKREF
2014 symtab_node node;
2016 FOR_EACH_SYMBOL (node)
2017 if (node->symbol.alias
2018 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->symbol.decl)))
2020 IDENTIFIER_TRANSPARENT_ALIAS
2021 (DECL_ASSEMBLER_NAME (node->symbol.decl)) = 1;
2022 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->symbol.decl))
2023 = (node->symbol.alias_target ? node->symbol.alias_target
2024 : DECL_ASSEMBLER_NAME (symtab_alias_target (node)->symbol.decl));
2026 #endif
2028 cgraph_state = CGRAPH_STATE_EXPANSION;
2029 if (!flag_toplevel_reorder)
2030 output_in_order ();
2031 else
2033 output_asm_statements ();
2035 expand_all_functions ();
2036 varpool_output_variables ();
2039 cgraph_process_new_functions ();
2040 cgraph_state = CGRAPH_STATE_FINISHED;
2041 output_weakrefs ();
2043 if (cgraph_dump_file)
2045 fprintf (cgraph_dump_file, "\nFinal ");
2046 dump_symtab (cgraph_dump_file);
2048 #ifdef ENABLE_CHECKING
2049 verify_symtab ();
2050 /* Double check that all inline clones are gone and that all
2051 function bodies have been released from memory. */
2052 if (!seen_error ())
2054 struct cgraph_node *node;
2055 bool error_found = false;
2057 FOR_EACH_DEFINED_FUNCTION (node)
2058 if (node->global.inlined_to
2059 || gimple_has_body_p (node->symbol.decl))
2061 error_found = true;
2062 dump_cgraph_node (stderr, node);
2064 if (error_found)
2065 internal_error ("nodes with unreleased memory found");
2067 #endif
2071 /* Analyze the whole compilation unit once it is parsed completely. */
2073 void
2074 finalize_compilation_unit (void)
2076 timevar_push (TV_CGRAPH);
2078 /* If we're here there's no current function anymore. Some frontends
2079 are lazy in clearing these. */
2080 current_function_decl = NULL;
2081 set_cfun (NULL);
2083 /* Do not skip analyzing the functions if there were errors, we
2084 miss diagnostics for following functions otherwise. */
2086 /* Emit size functions we didn't inline. */
2087 finalize_size_functions ();
2089 /* Mark alias targets necessary and emit diagnostics. */
2090 handle_alias_pairs ();
2092 if (!quiet_flag)
2094 fprintf (stderr, "\nAnalyzing compilation unit\n");
2095 fflush (stderr);
2098 if (flag_dump_passes)
2099 dump_passes ();
2101 /* Gimplify and lower all functions, compute reachability and
2102 remove unreachable nodes. */
2103 analyze_functions ();
2105 /* Mark alias targets necessary and emit diagnostics. */
2106 handle_alias_pairs ();
2108 /* Gimplify and lower thunks. */
2109 analyze_functions ();
2111 /* Finally drive the pass manager. */
2112 compile ();
2114 timevar_pop (TV_CGRAPH);
2118 #include "gt-cgraphunit.h"