2013-06-18 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / gcc / cgraphunit.c
blob016a6e43fa98e1b9be695eb43b694de4f52277f2
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 exmaple, 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 "regset.h" /* FIXME: For reg_obstack. */
197 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
198 secondary queue used during optimization to accommodate passes that
199 may generate new functions that need to be optimized and expanded. */
200 cgraph_node_set cgraph_new_nodes;
202 static void expand_all_functions (void);
203 static void mark_functions_to_output (void);
204 static void expand_function (struct cgraph_node *);
205 static void analyze_function (struct cgraph_node *);
206 static void handle_alias_pairs (void);
208 FILE *cgraph_dump_file;
210 /* Linked list of cgraph asm nodes. */
211 struct asm_node *asm_nodes;
213 /* Last node in cgraph_asm_nodes. */
214 static GTY(()) struct asm_node *asm_last_node;
216 /* Used for vtable lookup in thunk adjusting. */
217 static GTY (()) tree vtable_entry_type;
219 /* Determine if symbol DECL is needed. That is, visible to something
220 either outside this translation unit, something magic in the system
221 configury */
222 bool
223 decide_is_symbol_needed (symtab_node node)
225 tree decl = node->symbol.decl;
227 /* Double check that no one output the function into assembly file
228 early. */
229 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
230 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
232 if (!node->symbol.definition)
233 return false;
235 /* Devirtualization may access these. */
236 if (DECL_VIRTUAL_P (decl) && optimize)
237 return true;
239 if (DECL_EXTERNAL (decl))
240 return false;
242 /* If the user told us it is used, then it must be so. */
243 if (node->symbol.force_output)
244 return true;
246 /* ABI forced symbols are needed when they are external. */
247 if (node->symbol.forced_by_abi && TREE_PUBLIC (decl))
248 return true;
250 /* Keep constructors, destructors and virtual functions. */
251 if (TREE_CODE (decl) == FUNCTION_DECL
252 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
253 return true;
255 /* Externally visible variables must be output. The exception is
256 COMDAT variables that must be output only when they are needed. */
257 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
258 return true;
260 return false;
263 /* Head of the queue of nodes to be processed while building callgraph */
265 static symtab_node first = (symtab_node)(void *)1;
267 /* Add NODE to queue starting at FIRST.
268 The queue is linked via AUX pointers and terminated by pointer to 1. */
270 static void
271 enqueue_node (symtab_node node)
273 if (node->symbol.aux)
274 return;
275 gcc_checking_assert (first);
276 node->symbol.aux = first;
277 first = node;
280 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
281 functions into callgraph in a way so they look like ordinary reachable
282 functions inserted into callgraph already at construction time. */
284 bool
285 cgraph_process_new_functions (void)
287 bool output = false;
288 tree fndecl;
289 struct cgraph_node *node;
290 cgraph_node_set_iterator csi;
292 if (!cgraph_new_nodes)
293 return false;
294 handle_alias_pairs ();
295 /* Note that this queue may grow as its being processed, as the new
296 functions may generate new ones. */
297 for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi))
299 node = csi_node (csi);
300 fndecl = node->symbol.decl;
301 switch (cgraph_state)
303 case CGRAPH_STATE_CONSTRUCTION:
304 /* At construction time we just need to finalize function and move
305 it into reachable functions list. */
307 cgraph_finalize_function (fndecl, false);
308 output = true;
309 cgraph_call_function_insertion_hooks (node);
310 enqueue_node ((symtab_node) node);
311 break;
313 case CGRAPH_STATE_IPA:
314 case CGRAPH_STATE_IPA_SSA:
315 /* When IPA optimization already started, do all essential
316 transformations that has been already performed on the whole
317 cgraph but not on this function. */
319 gimple_register_cfg_hooks ();
320 if (!node->symbol.analyzed)
321 analyze_function (node);
322 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
323 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
324 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
325 /* When not optimizing, be sure we run early local passes anyway
326 to expand OMP. */
327 || !optimize)
328 execute_pass_list (pass_early_local_passes.pass.sub);
329 else
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 NESTED 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 nested)
414 struct cgraph_node *node = cgraph_get_create_node (decl);
416 if (node->symbol.definition)
418 cgraph_reset_node (node);
419 node->local.redefined_extern_inline = true;
422 notice_global_symbol (decl);
423 node->symbol.definition = true;
424 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
426 /* With -fkeep-inline-functions we are keeping all inline functions except
427 for extern inline ones. */
428 if (flag_keep_inline_functions
429 && DECL_DECLARED_INLINE_P (decl)
430 && !DECL_EXTERNAL (decl)
431 && !DECL_DISREGARD_INLINE_LIMITS (decl))
432 node->symbol.force_output = 1;
434 /* When not optimizing, also output the static functions. (see
435 PR24561), but don't do so for always_inline functions, functions
436 declared inline and nested functions. These were optimized out
437 in the original implementation and it is unclear whether we want
438 to change the behavior here. */
439 if ((!optimize
440 && !node->symbol.cpp_implicit_alias
441 && !DECL_DISREGARD_INLINE_LIMITS (decl)
442 && !DECL_DECLARED_INLINE_P (decl)
443 && !(DECL_CONTEXT (decl)
444 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
445 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
446 node->symbol.force_output = 1;
448 /* If we've not yet emitted decl, tell the debug info about it. */
449 if (!TREE_ASM_WRITTEN (decl))
450 (*debug_hooks->deferred_inline_function) (decl);
452 /* Possibly warn about unused parameters. */
453 if (warn_unused_parameter)
454 do_warn_unused_parameter (decl);
456 if (!nested)
457 ggc_collect ();
459 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
460 && (decide_is_symbol_needed ((symtab_node) node)
461 || referred_to_p ((symtab_node)node)))
462 enqueue_node ((symtab_node)node);
465 /* Add the function FNDECL to the call graph.
466 Unlike cgraph_finalize_function, this function is intended to be used
467 by middle end and allows insertion of new function at arbitrary point
468 of compilation. The function can be either in high, low or SSA form
469 GIMPLE.
471 The function is assumed to be reachable and have address taken (so no
472 API breaking optimizations are performed on it).
474 Main work done by this function is to enqueue the function for later
475 processing to avoid need the passes to be re-entrant. */
477 void
478 cgraph_add_new_function (tree fndecl, bool lowered)
480 struct cgraph_node *node;
481 switch (cgraph_state)
483 case CGRAPH_STATE_PARSING:
484 cgraph_finalize_function (fndecl, false);
485 break;
486 case CGRAPH_STATE_CONSTRUCTION:
487 /* Just enqueue function to be processed at nearest occurrence. */
488 node = cgraph_create_node (fndecl);
489 if (lowered)
490 node->lowered = true;
491 if (!cgraph_new_nodes)
492 cgraph_new_nodes = cgraph_node_set_new ();
493 cgraph_node_set_add (cgraph_new_nodes, node);
494 break;
496 case CGRAPH_STATE_IPA:
497 case CGRAPH_STATE_IPA_SSA:
498 case CGRAPH_STATE_EXPANSION:
499 /* Bring the function into finalized state and enqueue for later
500 analyzing and compilation. */
501 node = cgraph_get_create_node (fndecl);
502 node->local.local = false;
503 node->symbol.definition = true;
504 node->symbol.force_output = true;
505 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
507 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
508 gimple_register_cfg_hooks ();
509 bitmap_obstack_initialize (NULL);
510 execute_pass_list (all_lowering_passes);
511 execute_pass_list (pass_early_local_passes.pass.sub);
512 bitmap_obstack_release (NULL);
513 pop_cfun ();
515 lowered = true;
517 if (lowered)
518 node->lowered = true;
519 if (!cgraph_new_nodes)
520 cgraph_new_nodes = cgraph_node_set_new ();
521 cgraph_node_set_add (cgraph_new_nodes, node);
522 break;
524 case CGRAPH_STATE_FINISHED:
525 /* At the very end of compilation we have to do all the work up
526 to expansion. */
527 node = cgraph_create_node (fndecl);
528 if (lowered)
529 node->lowered = true;
530 node->symbol.definition = true;
531 analyze_function (node);
532 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
533 gimple_register_cfg_hooks ();
534 bitmap_obstack_initialize (NULL);
535 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
536 execute_pass_list (pass_early_local_passes.pass.sub);
537 bitmap_obstack_release (NULL);
538 pop_cfun ();
539 expand_function (node);
540 break;
542 default:
543 gcc_unreachable ();
546 /* Set a personality if required and we already passed EH lowering. */
547 if (lowered
548 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
549 == eh_personality_lang))
550 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
553 /* Add a top-level asm statement to the list. */
555 struct asm_node *
556 add_asm_node (tree asm_str)
558 struct asm_node *node;
560 node = ggc_alloc_cleared_asm_node ();
561 node->asm_str = asm_str;
562 node->order = symtab_order++;
563 node->next = NULL;
564 if (asm_nodes == NULL)
565 asm_nodes = node;
566 else
567 asm_last_node->next = node;
568 asm_last_node = node;
569 return node;
572 /* Output all asm statements we have stored up to be output. */
574 static void
575 output_asm_statements (void)
577 struct asm_node *can;
579 if (seen_error ())
580 return;
582 for (can = asm_nodes; can; can = can->next)
583 assemble_asm (can->asm_str);
584 asm_nodes = NULL;
587 /* Analyze the function scheduled to be output. */
588 static void
589 analyze_function (struct cgraph_node *node)
591 tree decl = node->symbol.decl;
592 location_t saved_loc = input_location;
593 input_location = DECL_SOURCE_LOCATION (decl);
595 if (node->symbol.alias)
596 symtab_resolve_alias
597 ((symtab_node) node, (symtab_node) cgraph_get_node (node->symbol.alias_target));
598 else if (node->thunk.thunk_p)
600 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
601 NULL, 0, CGRAPH_FREQ_BASE);
602 node->thunk.alias = NULL;
604 else if (node->dispatcher_function)
606 /* Generate the dispatcher body of multi-versioned functions. */
607 struct cgraph_function_version_info *dispatcher_version_info
608 = get_cgraph_node_version (node);
609 if (dispatcher_version_info != NULL
610 && (dispatcher_version_info->dispatcher_resolver
611 == NULL_TREE))
613 tree resolver = NULL_TREE;
614 gcc_assert (targetm.generate_version_dispatcher_body);
615 resolver = targetm.generate_version_dispatcher_body (node);
616 gcc_assert (resolver != NULL_TREE);
619 else
621 push_cfun (DECL_STRUCT_FUNCTION (decl));
623 assign_assembler_name_if_neeeded (node->symbol.decl);
625 /* Make sure to gimplify bodies only once. During analyzing a
626 function we lower it, which will require gimplified nested
627 functions, so we can end up here with an already gimplified
628 body. */
629 if (!gimple_has_body_p (decl))
630 gimplify_function_tree (decl);
631 dump_function (TDI_generic, decl);
633 /* Lower the function. */
634 if (!node->lowered)
636 if (node->nested)
637 lower_nested_functions (node->symbol.decl);
638 gcc_assert (!node->nested);
640 gimple_register_cfg_hooks ();
641 bitmap_obstack_initialize (NULL);
642 execute_pass_list (all_lowering_passes);
643 free_dominance_info (CDI_POST_DOMINATORS);
644 free_dominance_info (CDI_DOMINATORS);
645 compact_blocks ();
646 bitmap_obstack_release (NULL);
647 node->lowered = true;
650 pop_cfun ();
652 node->symbol.analyzed = true;
654 input_location = saved_loc;
657 /* C++ frontend produce same body aliases all over the place, even before PCH
658 gets streamed out. It relies on us linking the aliases with their function
659 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
660 first produce aliases without links, but once C++ FE is sure he won't sream
661 PCH we build the links via this function. */
663 void
664 cgraph_process_same_body_aliases (void)
666 symtab_node node;
667 FOR_EACH_SYMBOL (node)
668 if (node->symbol.cpp_implicit_alias && !node->symbol.analyzed)
669 symtab_resolve_alias
670 (node,
671 TREE_CODE (node->symbol.alias_target) == VAR_DECL
672 ? (symtab_node)varpool_node_for_decl (node->symbol.alias_target)
673 : (symtab_node)cgraph_get_create_node (node->symbol.alias_target));
674 cpp_implicit_aliases_done = true;
677 /* Process attributes common for vars and functions. */
679 static void
680 process_common_attributes (tree decl)
682 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
684 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
686 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
687 "%<weakref%> attribute should be accompanied with"
688 " an %<alias%> attribute");
689 DECL_WEAK (decl) = 0;
690 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
691 DECL_ATTRIBUTES (decl));
695 /* Look for externally_visible and used attributes and mark cgraph nodes
696 accordingly.
698 We cannot mark the nodes at the point the attributes are processed (in
699 handle_*_attribute) because the copy of the declarations available at that
700 point may not be canonical. For example, in:
702 void f();
703 void f() __attribute__((used));
705 the declaration we see in handle_used_attribute will be the second
706 declaration -- but the front end will subsequently merge that declaration
707 with the original declaration and discard the second declaration.
709 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
711 void f() {}
712 void f() __attribute__((externally_visible));
714 is valid.
716 So, we walk the nodes at the end of the translation unit, applying the
717 attributes at that point. */
719 static void
720 process_function_and_variable_attributes (struct cgraph_node *first,
721 struct varpool_node *first_var)
723 struct cgraph_node *node;
724 struct varpool_node *vnode;
726 for (node = cgraph_first_function (); node != first;
727 node = cgraph_next_function (node))
729 tree decl = node->symbol.decl;
730 if (DECL_PRESERVE_P (decl))
731 cgraph_mark_force_output_node (node);
732 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
734 if (! TREE_PUBLIC (node->symbol.decl))
735 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
736 "%<externally_visible%>"
737 " attribute have effect only on public objects");
739 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
740 && (node->symbol.definition && !node->symbol.alias))
742 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
743 "%<weakref%> attribute ignored"
744 " because function is defined");
745 DECL_WEAK (decl) = 0;
746 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
747 DECL_ATTRIBUTES (decl));
750 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
751 && !DECL_DECLARED_INLINE_P (decl)
752 /* redefining extern inline function makes it DECL_UNINLINABLE. */
753 && !DECL_UNINLINABLE (decl))
754 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
755 "always_inline function might not be inlinable");
757 process_common_attributes (decl);
759 for (vnode = varpool_first_variable (); vnode != first_var;
760 vnode = varpool_next_variable (vnode))
762 tree decl = vnode->symbol.decl;
763 if (DECL_EXTERNAL (decl)
764 && DECL_INITIAL (decl)
765 && const_value_known_p (decl))
766 varpool_finalize_decl (decl);
767 if (DECL_PRESERVE_P (decl))
768 vnode->symbol.force_output = true;
769 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
771 if (! TREE_PUBLIC (vnode->symbol.decl))
772 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
773 "%<externally_visible%>"
774 " attribute have effect only on public objects");
776 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
777 && vnode->symbol.definition
778 && DECL_INITIAL (decl))
780 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
781 "%<weakref%> attribute ignored"
782 " because variable is initialized");
783 DECL_WEAK (decl) = 0;
784 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
785 DECL_ATTRIBUTES (decl));
787 process_common_attributes (decl);
791 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
792 middle end to output the variable to asm file, if needed or externally
793 visible. */
795 void
796 varpool_finalize_decl (tree decl)
798 struct varpool_node *node = varpool_node_for_decl (decl);
800 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
802 if (node->symbol.definition)
803 return;
804 notice_global_symbol (decl);
805 node->symbol.definition = true;
806 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
807 /* Traditionally we do not eliminate static variables when not
808 optimizing and when not doing toplevel reoder. */
809 || (!flag_toplevel_reorder && !DECL_COMDAT (node->symbol.decl)
810 && !DECL_ARTIFICIAL (node->symbol.decl)))
811 node->symbol.force_output = true;
813 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
814 && (decide_is_symbol_needed ((symtab_node) node)
815 || referred_to_p ((symtab_node)node)))
816 enqueue_node ((symtab_node)node);
817 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
818 varpool_analyze_node (node);
819 /* Some frontends produce various interface variables after compilation
820 finished. */
821 if (cgraph_state == CGRAPH_STATE_FINISHED)
822 varpool_assemble_decl (node);
826 /* Discover all functions and variables that are trivially needed, analyze
827 them as well as all functions and variables referred by them */
829 static void
830 analyze_functions (void)
832 /* Keep track of already processed nodes when called multiple times for
833 intermodule optimization. */
834 static struct cgraph_node *first_analyzed;
835 struct cgraph_node *first_handled = first_analyzed;
836 static struct varpool_node *first_analyzed_var;
837 struct varpool_node *first_handled_var = first_analyzed_var;
839 symtab_node node, next;
840 int i;
841 struct ipa_ref *ref;
842 bool changed = true;
844 bitmap_obstack_initialize (NULL);
845 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
847 /* Ugly, but the fixup can not happen at a time same body alias is created;
848 C++ FE is confused about the COMDAT groups being right. */
849 if (cpp_implicit_aliases_done)
850 FOR_EACH_SYMBOL (node)
851 if (node->symbol.cpp_implicit_alias)
852 fixup_same_cpp_alias_visibility (node, symtab_alias_target (node));
854 /* Analysis adds static variables that in turn adds references to new functions.
855 So we need to iterate the process until it stabilize. */
856 while (changed)
858 changed = false;
859 process_function_and_variable_attributes (first_analyzed,
860 first_analyzed_var);
862 /* First identify the trivially needed symbols. */
863 for (node = symtab_nodes;
864 node != (symtab_node)first_analyzed
865 && node != (symtab_node)first_analyzed_var; node = node->symbol.next)
867 if (decide_is_symbol_needed (node))
869 enqueue_node (node);
870 if (!changed && cgraph_dump_file)
871 fprintf (cgraph_dump_file, "Trivially needed symbols:");
872 changed = true;
873 if (cgraph_dump_file)
874 fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node));
876 if (node == (symtab_node)first_analyzed
877 || node == (symtab_node)first_analyzed_var)
878 break;
880 cgraph_process_new_functions ();
881 first_analyzed_var = varpool_first_variable ();
882 first_analyzed = cgraph_first_function ();
884 if (changed && dump_file)
885 fprintf (cgraph_dump_file, "\n");
887 /* Lower representation, build callgraph edges and references for all trivially
888 needed symbols and all symbols referred by them. */
889 while (first != (symtab_node)(void *)1)
891 changed = true;
892 node = first;
893 first = (symtab_node)first->symbol.aux;
894 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
895 if (cnode && cnode->symbol.definition)
897 struct cgraph_edge *edge;
898 tree decl = cnode->symbol.decl;
900 /* ??? It is possible to create extern inline function
901 and later using weak alias attribute to kill its body.
902 See gcc.c-torture/compile/20011119-1.c */
903 if (!DECL_STRUCT_FUNCTION (decl)
904 && !cnode->symbol.alias
905 && !cnode->thunk.thunk_p
906 && !cnode->dispatcher_function)
908 cgraph_reset_node (cnode);
909 cnode->local.redefined_extern_inline = true;
910 continue;
913 if (!cnode->symbol.analyzed)
914 analyze_function (cnode);
916 for (edge = cnode->callees; edge; edge = edge->next_callee)
917 if (edge->callee->symbol.definition)
918 enqueue_node ((symtab_node)edge->callee);
920 /* If decl is a clone of an abstract function,
921 mark that abstract function so that we don't release its body.
922 The DECL_INITIAL() of that abstract function declaration
923 will be later needed to output debug info. */
924 if (DECL_ABSTRACT_ORIGIN (decl))
926 struct cgraph_node *origin_node
927 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
928 origin_node->abstract_and_needed = true;
931 else
933 varpool_node *vnode = dyn_cast <varpool_node> (node);
934 if (vnode && vnode->symbol.definition && !vnode->symbol.analyzed)
935 varpool_analyze_node (vnode);
938 if (node->symbol.same_comdat_group)
940 symtab_node next;
941 for (next = node->symbol.same_comdat_group;
942 next != node;
943 next = next->symbol.same_comdat_group)
944 enqueue_node (next);
946 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
947 if (ref->referred->symbol.definition)
948 enqueue_node (ref->referred);
949 cgraph_process_new_functions ();
953 /* Collect entry points to the unit. */
954 if (cgraph_dump_file)
956 fprintf (cgraph_dump_file, "\n\nInitial ");
957 dump_symtab (cgraph_dump_file);
960 if (cgraph_dump_file)
961 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
963 for (node = symtab_nodes;
964 node != (symtab_node)first_handled
965 && node != (symtab_node)first_handled_var; node = next)
967 next = node->symbol.next;
968 if (!node->symbol.aux && !referred_to_p (node))
970 if (cgraph_dump_file)
971 fprintf (cgraph_dump_file, " %s", symtab_node_name (node));
972 symtab_remove_node (node);
973 continue;
975 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
977 tree decl = node->symbol.decl;
979 if (cnode->symbol.definition && !gimple_has_body_p (decl)
980 && !cnode->symbol.alias
981 && !cnode->thunk.thunk_p)
982 cgraph_reset_node (cnode);
984 gcc_assert (!cnode->symbol.definition || cnode->thunk.thunk_p
985 || cnode->symbol.alias
986 || gimple_has_body_p (decl));
987 gcc_assert (cnode->symbol.analyzed == cnode->symbol.definition);
989 node->symbol.aux = NULL;
991 first_analyzed = cgraph_first_function ();
992 first_analyzed_var = varpool_first_variable ();
993 if (cgraph_dump_file)
995 fprintf (cgraph_dump_file, "\n\nReclaimed ");
996 dump_symtab (cgraph_dump_file);
998 bitmap_obstack_release (NULL);
999 ggc_collect ();
1002 /* Translate the ugly representation of aliases as alias pairs into nice
1003 representation in callgraph. We don't handle all cases yet,
1004 unforutnately. */
1006 static void
1007 handle_alias_pairs (void)
1009 alias_pair *p;
1010 unsigned i;
1012 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1014 symtab_node target_node = symtab_node_for_asm (p->target);
1016 /* Weakrefs with target not defined in current unit are easy to handle; they
1017 behave just as external variables except we need to note the alias flag
1018 to later output the weakref pseudo op into asm file. */
1019 if (!target_node && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1021 symtab_node node = symtab_get_node (p->decl);
1022 if (node)
1024 node->symbol.alias_target = p->target;
1025 node->symbol.weakref = true;
1026 node->symbol.alias = true;
1028 alias_pairs->unordered_remove (i);
1029 continue;
1031 else if (!target_node)
1033 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1034 alias_pairs->unordered_remove (i);
1035 continue;
1038 if (DECL_EXTERNAL (target_node->symbol.decl)
1039 /* We use local aliases for C++ thunks to force the tailcall
1040 to bind locally. This is a hack - to keep it working do
1041 the following (which is not strictly correct). */
1042 && (! TREE_CODE (target_node->symbol.decl) == FUNCTION_DECL
1043 || ! DECL_VIRTUAL_P (target_node->symbol.decl))
1044 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1046 error ("%q+D aliased to external symbol %qE",
1047 p->decl, p->target);
1050 if (TREE_CODE (p->decl) == FUNCTION_DECL
1051 && target_node && is_a <cgraph_node> (target_node))
1053 struct cgraph_node *src_node = cgraph_get_node (p->decl);
1054 if (src_node && src_node->symbol.definition)
1055 cgraph_reset_node (src_node);
1056 cgraph_create_function_alias (p->decl, target_node->symbol.decl);
1057 alias_pairs->unordered_remove (i);
1059 else if (TREE_CODE (p->decl) == VAR_DECL
1060 && target_node && is_a <varpool_node> (target_node))
1062 varpool_create_variable_alias (p->decl, target_node->symbol.decl);
1063 alias_pairs->unordered_remove (i);
1065 else
1067 error ("%q+D alias in between function and variable is not supported",
1068 p->decl);
1069 warning (0, "%q+D aliased declaration",
1070 target_node->symbol.decl);
1071 alias_pairs->unordered_remove (i);
1074 vec_free (alias_pairs);
1078 /* Figure out what functions we want to assemble. */
1080 static void
1081 mark_functions_to_output (void)
1083 struct cgraph_node *node;
1084 #ifdef ENABLE_CHECKING
1085 bool check_same_comdat_groups = false;
1087 FOR_EACH_FUNCTION (node)
1088 gcc_assert (!node->process);
1089 #endif
1091 FOR_EACH_FUNCTION (node)
1093 tree decl = node->symbol.decl;
1095 gcc_assert (!node->process || node->symbol.same_comdat_group);
1096 if (node->process)
1097 continue;
1099 /* We need to output all local functions that are used and not
1100 always inlined, as well as those that are reachable from
1101 outside the current compilation unit. */
1102 if (node->symbol.analyzed
1103 && !node->thunk.thunk_p
1104 && !node->symbol.alias
1105 && !node->global.inlined_to
1106 && !TREE_ASM_WRITTEN (decl)
1107 && !DECL_EXTERNAL (decl))
1109 node->process = 1;
1110 if (node->symbol.same_comdat_group)
1112 struct cgraph_node *next;
1113 for (next = cgraph (node->symbol.same_comdat_group);
1114 next != node;
1115 next = cgraph (next->symbol.same_comdat_group))
1116 if (!next->thunk.thunk_p && !next->symbol.alias)
1117 next->process = 1;
1120 else if (node->symbol.same_comdat_group)
1122 #ifdef ENABLE_CHECKING
1123 check_same_comdat_groups = true;
1124 #endif
1126 else
1128 /* We should've reclaimed all functions that are not needed. */
1129 #ifdef ENABLE_CHECKING
1130 if (!node->global.inlined_to
1131 && gimple_has_body_p (decl)
1132 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1133 are inside partition, we can end up not removing the body since we no longer
1134 have analyzed node pointing to it. */
1135 && !node->symbol.in_other_partition
1136 && !node->symbol.alias
1137 && !node->clones
1138 && !DECL_EXTERNAL (decl))
1140 dump_cgraph_node (stderr, node);
1141 internal_error ("failed to reclaim unneeded function");
1143 #endif
1144 gcc_assert (node->global.inlined_to
1145 || !gimple_has_body_p (decl)
1146 || node->symbol.in_other_partition
1147 || node->clones
1148 || DECL_ARTIFICIAL (decl)
1149 || DECL_EXTERNAL (decl));
1154 #ifdef ENABLE_CHECKING
1155 if (check_same_comdat_groups)
1156 FOR_EACH_FUNCTION (node)
1157 if (node->symbol.same_comdat_group && !node->process)
1159 tree decl = node->symbol.decl;
1160 if (!node->global.inlined_to
1161 && gimple_has_body_p (decl)
1162 /* FIXME: in an ltrans unit when the offline copy is outside a
1163 partition but inline copies are inside a partition, we can
1164 end up not removing the body since we no longer have an
1165 analyzed node pointing to it. */
1166 && !node->symbol.in_other_partition
1167 && !node->clones
1168 && !DECL_EXTERNAL (decl))
1170 dump_cgraph_node (stderr, node);
1171 internal_error ("failed to reclaim unneeded function in same "
1172 "comdat group");
1175 #endif
1178 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1179 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1181 Set current_function_decl and cfun to newly constructed empty function body.
1182 return basic block in the function body. */
1184 basic_block
1185 init_lowered_empty_function (tree decl, bool in_ssa)
1187 basic_block bb;
1189 current_function_decl = decl;
1190 allocate_struct_function (decl, false);
1191 gimple_register_cfg_hooks ();
1192 init_empty_tree_cfg ();
1194 if (in_ssa)
1196 init_tree_ssa (cfun);
1197 init_ssa_operands (cfun);
1198 cfun->gimple_df->in_ssa_p = true;
1201 DECL_INITIAL (decl) = make_node (BLOCK);
1203 DECL_SAVED_TREE (decl) = error_mark_node;
1204 cfun->curr_properties |=
1205 (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_ssa | PROP_gimple_any);
1207 /* Create BB for body of the function and connect it properly. */
1208 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
1209 make_edge (ENTRY_BLOCK_PTR, bb, 0);
1210 make_edge (bb, EXIT_BLOCK_PTR, 0);
1212 return bb;
1215 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1216 offset indicated by VIRTUAL_OFFSET, if that is
1217 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1218 zero for a result adjusting thunk. */
1220 static tree
1221 thunk_adjust (gimple_stmt_iterator * bsi,
1222 tree ptr, bool this_adjusting,
1223 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1225 gimple stmt;
1226 tree ret;
1228 if (this_adjusting
1229 && fixed_offset != 0)
1231 stmt = gimple_build_assign
1232 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1233 ptr,
1234 fixed_offset));
1235 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1238 /* If there's a virtual offset, look up that value in the vtable and
1239 adjust the pointer again. */
1240 if (virtual_offset)
1242 tree vtabletmp;
1243 tree vtabletmp2;
1244 tree vtabletmp3;
1246 if (!vtable_entry_type)
1248 tree vfunc_type = make_node (FUNCTION_TYPE);
1249 TREE_TYPE (vfunc_type) = integer_type_node;
1250 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1251 layout_type (vfunc_type);
1253 vtable_entry_type = build_pointer_type (vfunc_type);
1256 vtabletmp =
1257 create_tmp_reg (build_pointer_type
1258 (build_pointer_type (vtable_entry_type)), "vptr");
1260 /* The vptr is always at offset zero in the object. */
1261 stmt = gimple_build_assign (vtabletmp,
1262 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1263 ptr));
1264 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1266 /* Form the vtable address. */
1267 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1268 "vtableaddr");
1269 stmt = gimple_build_assign (vtabletmp2,
1270 build_simple_mem_ref (vtabletmp));
1271 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1273 /* Find the entry with the vcall offset. */
1274 stmt = gimple_build_assign (vtabletmp2,
1275 fold_build_pointer_plus_loc (input_location,
1276 vtabletmp2,
1277 virtual_offset));
1278 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1280 /* Get the offset itself. */
1281 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1282 "vcalloffset");
1283 stmt = gimple_build_assign (vtabletmp3,
1284 build_simple_mem_ref (vtabletmp2));
1285 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1287 /* Adjust the `this' pointer. */
1288 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1289 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1290 GSI_CONTINUE_LINKING);
1293 if (!this_adjusting
1294 && fixed_offset != 0)
1295 /* Adjust the pointer by the constant. */
1297 tree ptrtmp;
1299 if (TREE_CODE (ptr) == VAR_DECL)
1300 ptrtmp = ptr;
1301 else
1303 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1304 stmt = gimple_build_assign (ptrtmp, ptr);
1305 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1307 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1308 ptrtmp, fixed_offset);
1311 /* Emit the statement and gimplify the adjustment expression. */
1312 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1313 stmt = gimple_build_assign (ret, ptr);
1314 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1316 return ret;
1319 /* Produce assembler for thunk NODE. */
1321 static void
1322 assemble_thunk (struct cgraph_node *node)
1324 bool this_adjusting = node->thunk.this_adjusting;
1325 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1326 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1327 tree virtual_offset = NULL;
1328 tree alias = node->callees->callee->symbol.decl;
1329 tree thunk_fndecl = node->symbol.decl;
1330 tree a = DECL_ARGUMENTS (thunk_fndecl);
1332 current_function_decl = thunk_fndecl;
1334 /* Ensure thunks are emitted in their correct sections. */
1335 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1337 if (this_adjusting
1338 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1339 virtual_value, alias))
1341 const char *fnname;
1342 tree fn_block;
1343 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1345 DECL_RESULT (thunk_fndecl)
1346 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1347 RESULT_DECL, 0, restype);
1348 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1350 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1351 create one. */
1352 fn_block = make_node (BLOCK);
1353 BLOCK_VARS (fn_block) = a;
1354 DECL_INITIAL (thunk_fndecl) = fn_block;
1355 init_function_start (thunk_fndecl);
1356 cfun->is_thunk = 1;
1357 insn_locations_init ();
1358 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1359 prologue_location = curr_insn_location ();
1360 assemble_start_function (thunk_fndecl, fnname);
1362 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1363 fixed_offset, virtual_value, alias);
1365 assemble_end_function (thunk_fndecl, fnname);
1366 insn_locations_finalize ();
1367 init_insn_lengths ();
1368 free_after_compilation (cfun);
1369 set_cfun (NULL);
1370 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1371 node->thunk.thunk_p = false;
1372 node->symbol.analyzed = false;
1374 else
1376 tree restype;
1377 basic_block bb, then_bb, else_bb, return_bb;
1378 gimple_stmt_iterator bsi;
1379 int nargs = 0;
1380 tree arg;
1381 int i;
1382 tree resdecl;
1383 tree restmp = NULL;
1384 vec<tree> vargs;
1386 gimple call;
1387 gimple ret;
1389 DECL_IGNORED_P (thunk_fndecl) = 1;
1390 bitmap_obstack_initialize (NULL);
1392 if (node->thunk.virtual_offset_p)
1393 virtual_offset = size_int (virtual_value);
1395 /* Build the return declaration for the function. */
1396 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1397 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1399 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1400 DECL_ARTIFICIAL (resdecl) = 1;
1401 DECL_IGNORED_P (resdecl) = 1;
1402 DECL_RESULT (thunk_fndecl) = resdecl;
1404 else
1405 resdecl = DECL_RESULT (thunk_fndecl);
1407 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1409 bsi = gsi_start_bb (bb);
1411 /* Build call to the function being thunked. */
1412 if (!VOID_TYPE_P (restype))
1414 if (!is_gimple_reg_type (restype))
1416 restmp = resdecl;
1417 add_local_decl (cfun, restmp);
1418 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1420 else
1421 restmp = create_tmp_reg (restype, "retval");
1424 for (arg = a; arg; arg = DECL_CHAIN (arg))
1425 nargs++;
1426 vargs.create (nargs);
1427 if (this_adjusting)
1428 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1429 virtual_offset));
1430 else
1431 vargs.quick_push (a);
1432 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1433 vargs.quick_push (arg);
1434 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1435 vargs.release ();
1436 gimple_call_set_from_thunk (call, true);
1437 if (restmp)
1438 gimple_call_set_lhs (call, restmp);
1439 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1441 if (restmp && !this_adjusting)
1443 tree true_label = NULL_TREE;
1445 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1447 gimple stmt;
1448 /* If the return type is a pointer, we need to
1449 protect against NULL. We know there will be an
1450 adjustment, because that's why we're emitting a
1451 thunk. */
1452 then_bb = create_basic_block (NULL, (void *) 0, bb);
1453 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1454 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1455 remove_edge (single_succ_edge (bb));
1456 true_label = gimple_block_label (then_bb);
1457 stmt = gimple_build_cond (NE_EXPR, restmp,
1458 build_zero_cst (TREE_TYPE (restmp)),
1459 NULL_TREE, NULL_TREE);
1460 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1461 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1462 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1463 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1464 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1465 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1466 bsi = gsi_last_bb (then_bb);
1469 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1470 fixed_offset, virtual_offset);
1471 if (true_label)
1473 gimple stmt;
1474 bsi = gsi_last_bb (else_bb);
1475 stmt = gimple_build_assign (restmp,
1476 build_zero_cst (TREE_TYPE (restmp)));
1477 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1478 bsi = gsi_last_bb (return_bb);
1481 else
1482 gimple_call_set_tail (call, true);
1484 /* Build return value. */
1485 ret = gimple_build_return (restmp);
1486 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1488 delete_unreachable_blocks ();
1489 update_ssa (TODO_update_ssa);
1491 /* Since we want to emit the thunk, we explicitly mark its name as
1492 referenced. */
1493 node->thunk.thunk_p = false;
1494 cgraph_node_remove_callees (node);
1495 cgraph_add_new_function (thunk_fndecl, true);
1496 bitmap_obstack_release (NULL);
1498 current_function_decl = NULL;
1499 set_cfun (NULL);
1504 /* Assemble thunks and aliases associated to NODE. */
1506 static void
1507 assemble_thunks_and_aliases (struct cgraph_node *node)
1509 struct cgraph_edge *e;
1510 int i;
1511 struct ipa_ref *ref;
1513 for (e = node->callers; e;)
1514 if (e->caller->thunk.thunk_p)
1516 struct cgraph_node *thunk = e->caller;
1518 e = e->next_caller;
1519 assemble_thunks_and_aliases (thunk);
1520 assemble_thunk (thunk);
1522 else
1523 e = e->next_caller;
1524 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
1525 i, ref); i++)
1526 if (ref->use == IPA_REF_ALIAS)
1528 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1529 bool saved_written = TREE_ASM_WRITTEN (node->symbol.decl);
1531 /* Force assemble_alias to really output the alias this time instead
1532 of buffering it in same alias pairs. */
1533 TREE_ASM_WRITTEN (node->symbol.decl) = 1;
1534 do_assemble_alias (alias->symbol.decl,
1535 DECL_ASSEMBLER_NAME (node->symbol.decl));
1536 assemble_thunks_and_aliases (alias);
1537 TREE_ASM_WRITTEN (node->symbol.decl) = saved_written;
1541 /* Expand function specified by NODE. */
1543 static void
1544 expand_function (struct cgraph_node *node)
1546 tree decl = node->symbol.decl;
1547 location_t saved_loc;
1549 /* We ought to not compile any inline clones. */
1550 gcc_assert (!node->global.inlined_to);
1552 announce_function (decl);
1553 node->process = 0;
1554 gcc_assert (node->lowered);
1556 /* Generate RTL for the body of DECL. */
1558 timevar_push (TV_REST_OF_COMPILATION);
1560 gcc_assert (cgraph_global_info_ready);
1562 /* Initialize the default bitmap obstack. */
1563 bitmap_obstack_initialize (NULL);
1565 /* Initialize the RTL code for the function. */
1566 current_function_decl = decl;
1567 saved_loc = input_location;
1568 input_location = DECL_SOURCE_LOCATION (decl);
1569 init_function_start (decl);
1571 gimple_register_cfg_hooks ();
1573 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1575 execute_all_ipa_transforms ();
1577 /* Perform all tree transforms and optimizations. */
1579 /* Signal the start of passes. */
1580 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1582 execute_pass_list (all_passes);
1584 /* Signal the end of passes. */
1585 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1587 bitmap_obstack_release (&reg_obstack);
1589 /* Release the default bitmap obstack. */
1590 bitmap_obstack_release (NULL);
1592 /* If requested, warn about function definitions where the function will
1593 return a value (usually of some struct or union type) which itself will
1594 take up a lot of stack space. */
1595 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1597 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1599 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1600 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1601 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1602 larger_than_size))
1604 unsigned int size_as_int
1605 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1607 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1608 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1609 decl, size_as_int);
1610 else
1611 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1612 decl, larger_than_size);
1616 gimple_set_body (decl, NULL);
1617 if (DECL_STRUCT_FUNCTION (decl) == 0
1618 && !cgraph_get_node (decl)->origin)
1620 /* Stop pointing to the local nodes about to be freed.
1621 But DECL_INITIAL must remain nonzero so we know this
1622 was an actual function definition.
1623 For a nested function, this is done in c_pop_function_context.
1624 If rest_of_compilation set this to 0, leave it 0. */
1625 if (DECL_INITIAL (decl) != 0)
1626 DECL_INITIAL (decl) = error_mark_node;
1629 input_location = saved_loc;
1631 ggc_collect ();
1632 timevar_pop (TV_REST_OF_COMPILATION);
1634 /* Make sure that BE didn't give up on compiling. */
1635 gcc_assert (TREE_ASM_WRITTEN (decl));
1636 set_cfun (NULL);
1637 current_function_decl = NULL;
1639 /* It would make a lot more sense to output thunks before function body to get more
1640 forward and lest backwarding jumps. This however would need solving problem
1641 with comdats. See PR48668. Also aliases must come after function itself to
1642 make one pass assemblers, like one on AIX, happy. See PR 50689.
1643 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1644 groups. */
1645 assemble_thunks_and_aliases (node);
1646 cgraph_release_function_body (node);
1647 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1648 points to the dead function body. */
1649 cgraph_node_remove_callees (node);
1653 /* Expand all functions that must be output.
1655 Attempt to topologically sort the nodes so function is output when
1656 all called functions are already assembled to allow data to be
1657 propagated across the callgraph. Use a stack to get smaller distance
1658 between a function and its callees (later we may choose to use a more
1659 sophisticated algorithm for function reordering; we will likely want
1660 to use subsections to make the output functions appear in top-down
1661 order). */
1663 static void
1664 expand_all_functions (void)
1666 struct cgraph_node *node;
1667 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1668 int order_pos, new_order_pos = 0;
1669 int i;
1671 order_pos = ipa_reverse_postorder (order);
1672 gcc_assert (order_pos == cgraph_n_nodes);
1674 /* Garbage collector may remove inline clones we eliminate during
1675 optimization. So we must be sure to not reference them. */
1676 for (i = 0; i < order_pos; i++)
1677 if (order[i]->process)
1678 order[new_order_pos++] = order[i];
1680 for (i = new_order_pos - 1; i >= 0; i--)
1682 node = order[i];
1683 if (node->process)
1685 node->process = 0;
1686 expand_function (node);
1689 cgraph_process_new_functions ();
1691 free (order);
1695 /* This is used to sort the node types by the cgraph order number. */
1697 enum cgraph_order_sort_kind
1699 ORDER_UNDEFINED = 0,
1700 ORDER_FUNCTION,
1701 ORDER_VAR,
1702 ORDER_ASM
1705 struct cgraph_order_sort
1707 enum cgraph_order_sort_kind kind;
1708 union
1710 struct cgraph_node *f;
1711 struct varpool_node *v;
1712 struct asm_node *a;
1713 } u;
1716 /* Output all functions, variables, and asm statements in the order
1717 according to their order fields, which is the order in which they
1718 appeared in the file. This implements -fno-toplevel-reorder. In
1719 this mode we may output functions and variables which don't really
1720 need to be output. */
1722 static void
1723 output_in_order (void)
1725 int max;
1726 struct cgraph_order_sort *nodes;
1727 int i;
1728 struct cgraph_node *pf;
1729 struct varpool_node *pv;
1730 struct asm_node *pa;
1732 max = symtab_order;
1733 nodes = XCNEWVEC (struct cgraph_order_sort, max);
1735 FOR_EACH_DEFINED_FUNCTION (pf)
1737 if (pf->process && !pf->thunk.thunk_p && !pf->symbol.alias)
1739 i = pf->symbol.order;
1740 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1741 nodes[i].kind = ORDER_FUNCTION;
1742 nodes[i].u.f = pf;
1746 FOR_EACH_DEFINED_VARIABLE (pv)
1747 if (!DECL_EXTERNAL (pv->symbol.decl))
1749 i = pv->symbol.order;
1750 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1751 nodes[i].kind = ORDER_VAR;
1752 nodes[i].u.v = pv;
1755 for (pa = asm_nodes; pa; pa = pa->next)
1757 i = pa->order;
1758 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1759 nodes[i].kind = ORDER_ASM;
1760 nodes[i].u.a = pa;
1763 /* In toplevel reorder mode we output all statics; mark them as needed. */
1765 for (i = 0; i < max; ++i)
1766 if (nodes[i].kind == ORDER_VAR)
1767 varpool_finalize_named_section_flags (nodes[i].u.v);
1769 for (i = 0; i < max; ++i)
1771 switch (nodes[i].kind)
1773 case ORDER_FUNCTION:
1774 nodes[i].u.f->process = 0;
1775 expand_function (nodes[i].u.f);
1776 break;
1778 case ORDER_VAR:
1779 varpool_assemble_decl (nodes[i].u.v);
1780 break;
1782 case ORDER_ASM:
1783 assemble_asm (nodes[i].u.a->asm_str);
1784 break;
1786 case ORDER_UNDEFINED:
1787 break;
1789 default:
1790 gcc_unreachable ();
1794 asm_nodes = NULL;
1795 free (nodes);
1798 static void
1799 ipa_passes (void)
1801 set_cfun (NULL);
1802 current_function_decl = NULL;
1803 gimple_register_cfg_hooks ();
1804 bitmap_obstack_initialize (NULL);
1806 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1808 if (!in_lto_p)
1810 execute_ipa_pass_list (all_small_ipa_passes);
1811 if (seen_error ())
1812 return;
1815 /* We never run removal of unreachable nodes after early passes. This is
1816 because TODO is run before the subpasses. It is important to remove
1817 the unreachable functions to save works at IPA level and to get LTO
1818 symbol tables right. */
1819 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
1821 /* If pass_all_early_optimizations was not scheduled, the state of
1822 the cgraph will not be properly updated. Update it now. */
1823 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1824 cgraph_state = CGRAPH_STATE_IPA_SSA;
1826 if (!in_lto_p)
1828 /* Generate coverage variables and constructors. */
1829 coverage_finish ();
1831 /* Process new functions added. */
1832 set_cfun (NULL);
1833 current_function_decl = NULL;
1834 cgraph_process_new_functions ();
1836 execute_ipa_summary_passes
1837 ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
1840 /* Some targets need to handle LTO assembler output specially. */
1841 if (flag_generate_lto)
1842 targetm.asm_out.lto_start ();
1844 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
1846 if (!in_lto_p)
1847 ipa_write_summaries ();
1849 if (flag_generate_lto)
1850 targetm.asm_out.lto_end ();
1852 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
1853 execute_ipa_pass_list (all_regular_ipa_passes);
1854 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
1856 bitmap_obstack_release (NULL);
1860 /* Return string alias is alias of. */
1862 static tree
1863 get_alias_symbol (tree decl)
1865 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1866 return get_identifier (TREE_STRING_POINTER
1867 (TREE_VALUE (TREE_VALUE (alias))));
1871 /* Weakrefs may be associated to external decls and thus not output
1872 at expansion time. Emit all necessary aliases. */
1874 static void
1875 output_weakrefs (void)
1877 symtab_node node;
1878 FOR_EACH_SYMBOL (node)
1879 if (node->symbol.alias
1880 && !TREE_ASM_WRITTEN (node->symbol.decl)
1881 && node->symbol.weakref)
1883 tree target;
1885 /* Weakrefs are special by not requiring target definition in current
1886 compilation unit. It is thus bit hard to work out what we want to
1887 alias.
1888 When alias target is defined, we need to fetch it from symtab reference,
1889 otherwise it is pointed to by alias_target. */
1890 if (node->symbol.alias_target)
1891 target = (DECL_P (node->symbol.alias_target)
1892 ? DECL_ASSEMBLER_NAME (node->symbol.alias_target)
1893 : node->symbol.alias_target);
1894 else if (node->symbol.analyzed)
1895 target = DECL_ASSEMBLER_NAME (symtab_alias_target (node)->symbol.decl);
1896 else
1898 gcc_unreachable ();
1899 target = get_alias_symbol (node->symbol.decl);
1901 do_assemble_alias (node->symbol.decl, target);
1905 /* Initialize callgraph dump file. */
1907 void
1908 init_cgraph (void)
1910 if (!cgraph_dump_file)
1911 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1915 /* Perform simple optimizations based on callgraph. */
1917 void
1918 compile (void)
1920 if (seen_error ())
1921 return;
1923 #ifdef ENABLE_CHECKING
1924 verify_symtab ();
1925 #endif
1927 timevar_push (TV_CGRAPHOPT);
1928 if (pre_ipa_mem_report)
1930 fprintf (stderr, "Memory consumption before IPA\n");
1931 dump_memory_report (false);
1933 if (!quiet_flag)
1934 fprintf (stderr, "Performing interprocedural optimizations\n");
1935 cgraph_state = CGRAPH_STATE_IPA;
1937 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1938 if (flag_lto)
1939 lto_streamer_hooks_init ();
1941 /* Don't run the IPA passes if there was any error or sorry messages. */
1942 if (!seen_error ())
1943 ipa_passes ();
1945 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
1946 if (seen_error ()
1947 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
1949 timevar_pop (TV_CGRAPHOPT);
1950 return;
1953 /* This pass remove bodies of extern inline functions we never inlined.
1954 Do this later so other IPA passes see what is really going on. */
1955 symtab_remove_unreachable_nodes (false, dump_file);
1956 cgraph_global_info_ready = true;
1957 if (cgraph_dump_file)
1959 fprintf (cgraph_dump_file, "Optimized ");
1960 dump_symtab (cgraph_dump_file);
1962 if (post_ipa_mem_report)
1964 fprintf (stderr, "Memory consumption after IPA\n");
1965 dump_memory_report (false);
1967 timevar_pop (TV_CGRAPHOPT);
1969 /* Output everything. */
1970 (*debug_hooks->assembly_start) ();
1971 if (!quiet_flag)
1972 fprintf (stderr, "Assembling functions:\n");
1973 #ifdef ENABLE_CHECKING
1974 verify_symtab ();
1975 #endif
1977 cgraph_materialize_all_clones ();
1978 bitmap_obstack_initialize (NULL);
1979 execute_ipa_pass_list (all_late_ipa_passes);
1980 symtab_remove_unreachable_nodes (true, dump_file);
1981 #ifdef ENABLE_CHECKING
1982 verify_symtab ();
1983 #endif
1984 bitmap_obstack_release (NULL);
1985 mark_functions_to_output ();
1987 /* When weakref support is missing, we autmatically translate all
1988 references to NODE to references to its ultimate alias target.
1989 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
1990 TREE_CHAIN.
1992 Set up this mapping before we output any assembler but once we are sure
1993 that all symbol renaming is done.
1995 FIXME: All this uglyness can go away if we just do renaming at gimple
1996 level by physically rewritting the IL. At the moment we can only redirect
1997 calls, so we need infrastructure for renaming references as well. */
1998 #ifndef ASM_OUTPUT_WEAKREF
1999 symtab_node node;
2001 FOR_EACH_SYMBOL (node)
2002 if (node->symbol.alias
2003 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->symbol.decl)))
2005 IDENTIFIER_TRANSPARENT_ALIAS
2006 (DECL_ASSEMBLER_NAME (node->symbol.decl)) = 1;
2007 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->symbol.decl))
2008 = (node->symbol.alias_target ? node->symbol.alias_target
2009 : DECL_ASSEMBLER_NAME (symtab_alias_target (node)->symbol.decl));
2011 #endif
2013 cgraph_state = CGRAPH_STATE_EXPANSION;
2014 if (!flag_toplevel_reorder)
2015 output_in_order ();
2016 else
2018 output_asm_statements ();
2020 expand_all_functions ();
2021 varpool_output_variables ();
2024 cgraph_process_new_functions ();
2025 cgraph_state = CGRAPH_STATE_FINISHED;
2026 output_weakrefs ();
2028 if (cgraph_dump_file)
2030 fprintf (cgraph_dump_file, "\nFinal ");
2031 dump_symtab (cgraph_dump_file);
2033 #ifdef ENABLE_CHECKING
2034 verify_symtab ();
2035 /* Double check that all inline clones are gone and that all
2036 function bodies have been released from memory. */
2037 if (!seen_error ())
2039 struct cgraph_node *node;
2040 bool error_found = false;
2042 FOR_EACH_DEFINED_FUNCTION (node)
2043 if (node->global.inlined_to
2044 || gimple_has_body_p (node->symbol.decl))
2046 error_found = true;
2047 dump_cgraph_node (stderr, node);
2049 if (error_found)
2050 internal_error ("nodes with unreleased memory found");
2052 #endif
2056 /* Analyze the whole compilation unit once it is parsed completely. */
2058 void
2059 finalize_compilation_unit (void)
2061 timevar_push (TV_CGRAPH);
2063 /* If we're here there's no current function anymore. Some frontends
2064 are lazy in clearing these. */
2065 current_function_decl = NULL;
2066 set_cfun (NULL);
2068 /* Do not skip analyzing the functions if there were errors, we
2069 miss diagnostics for following functions otherwise. */
2071 /* Emit size functions we didn't inline. */
2072 finalize_size_functions ();
2074 /* Mark alias targets necessary and emit diagnostics. */
2075 handle_alias_pairs ();
2077 if (!quiet_flag)
2079 fprintf (stderr, "\nAnalyzing compilation unit\n");
2080 fflush (stderr);
2083 if (flag_dump_passes)
2084 dump_passes ();
2086 /* Gimplify and lower all functions, compute reachability and
2087 remove unreachable nodes. */
2088 analyze_functions ();
2090 /* Mark alias targets necessary and emit diagnostics. */
2091 handle_alias_pairs ();
2093 /* Gimplify and lower thunks. */
2094 analyze_functions ();
2096 /* Finally drive the pass manager. */
2097 compile ();
2099 timevar_pop (TV_CGRAPH);
2103 #include "gt-cgraphunit.h"