2013-04-03 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / cgraphunit.c
blob7fa8055878c8588bbbda1109d8d17cf284339b30
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 cgraph_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 function DECL is trivially needed and should stay in the
220 compilation unit. This is used at the symbol table construction time
221 and differs from later logic removing unnecessary functions that can
222 take into account results of analysis, whole program info etc. */
224 static bool
225 cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
227 /* If the user told us it is used, then it must be so. */
228 if (node->symbol.force_output)
229 return true;
231 /* Double check that no one output the function into assembly file
232 early. */
233 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
234 || (node->thunk.thunk_p || node->same_body_alias)
235 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
238 /* Keep constructors, destructors and virtual functions. */
239 if (DECL_STATIC_CONSTRUCTOR (decl)
240 || DECL_STATIC_DESTRUCTOR (decl)
241 || (DECL_VIRTUAL_P (decl)
242 && optimize && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
243 return true;
245 /* Externally visible functions must be output. The exception is
246 COMDAT functions that must be output only when they are needed. */
248 if (TREE_PUBLIC (decl)
249 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
250 return true;
252 return false;
255 /* Head of the queue of nodes to be processed while building callgraph */
257 static symtab_node first = (symtab_node)(void *)1;
259 /* Add NODE to queue starting at FIRST.
260 The queue is linked via AUX pointers and terminated by pointer to 1. */
262 static void
263 enqueue_node (symtab_node node)
265 if (node->symbol.aux)
266 return;
267 gcc_checking_assert (first);
268 node->symbol.aux = first;
269 first = node;
272 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
273 functions into callgraph in a way so they look like ordinary reachable
274 functions inserted into callgraph already at construction time. */
276 bool
277 cgraph_process_new_functions (void)
279 bool output = false;
280 tree fndecl;
281 struct cgraph_node *node;
282 cgraph_node_set_iterator csi;
284 if (!cgraph_new_nodes)
285 return false;
286 handle_alias_pairs ();
287 /* Note that this queue may grow as its being processed, as the new
288 functions may generate new ones. */
289 for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi))
291 node = csi_node (csi);
292 fndecl = node->symbol.decl;
293 switch (cgraph_state)
295 case CGRAPH_STATE_CONSTRUCTION:
296 /* At construction time we just need to finalize function and move
297 it into reachable functions list. */
299 cgraph_finalize_function (fndecl, false);
300 output = true;
301 cgraph_call_function_insertion_hooks (node);
302 enqueue_node ((symtab_node) node);
303 break;
305 case CGRAPH_STATE_IPA:
306 case CGRAPH_STATE_IPA_SSA:
307 /* When IPA optimization already started, do all essential
308 transformations that has been already performed on the whole
309 cgraph but not on this function. */
311 gimple_register_cfg_hooks ();
312 if (!node->analyzed)
313 cgraph_analyze_function (node);
314 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
315 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
316 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
317 /* When not optimizing, be sure we run early local passes anyway
318 to expand OMP. */
319 || !optimize)
320 execute_pass_list (pass_early_local_passes.pass.sub);
321 else
322 compute_inline_parameters (node, true);
323 free_dominance_info (CDI_POST_DOMINATORS);
324 free_dominance_info (CDI_DOMINATORS);
325 pop_cfun ();
326 cgraph_call_function_insertion_hooks (node);
327 break;
329 case CGRAPH_STATE_EXPANSION:
330 /* Functions created during expansion shall be compiled
331 directly. */
332 node->process = 0;
333 cgraph_call_function_insertion_hooks (node);
334 expand_function (node);
335 break;
337 default:
338 gcc_unreachable ();
339 break;
342 free_cgraph_node_set (cgraph_new_nodes);
343 cgraph_new_nodes = NULL;
344 return output;
347 /* As an GCC extension we allow redefinition of the function. The
348 semantics when both copies of bodies differ is not well defined.
349 We replace the old body with new body so in unit at a time mode
350 we always use new body, while in normal mode we may end up with
351 old body inlined into some functions and new body expanded and
352 inlined in others.
354 ??? It may make more sense to use one body for inlining and other
355 body for expanding the function but this is difficult to do. */
357 static void
358 cgraph_reset_node (struct cgraph_node *node)
360 /* If node->process is set, then we have already begun whole-unit analysis.
361 This is *not* testing for whether we've already emitted the function.
362 That case can be sort-of legitimately seen with real function redefinition
363 errors. I would argue that the front end should never present us with
364 such a case, but don't enforce that for now. */
365 gcc_assert (!node->process);
367 /* Reset our data structures so we can analyze the function again. */
368 memset (&node->local, 0, sizeof (node->local));
369 memset (&node->global, 0, sizeof (node->global));
370 memset (&node->rtl, 0, sizeof (node->rtl));
371 node->analyzed = false;
372 node->local.finalized = false;
374 cgraph_node_remove_callees (node);
377 /* Return true when there are references to NODE. */
379 static bool
380 referred_to_p (symtab_node node)
382 struct ipa_ref *ref;
384 /* See if there are any references at all. */
385 if (ipa_ref_list_referring_iterate (&node->symbol.ref_list, 0, ref))
386 return true;
387 /* For functions check also calls. */
388 cgraph_node *cn = dyn_cast <cgraph_node> (node);
389 if (cn && cn->callers)
390 return true;
391 return false;
394 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
395 logic in effect. If NESTED is true, then our caller cannot stand to have
396 the garbage collector run at the moment. We would need to either create
397 a new GC context, or just not compile right now. */
399 void
400 cgraph_finalize_function (tree decl, bool nested)
402 struct cgraph_node *node = cgraph_get_create_node (decl);
404 if (node->local.finalized)
406 cgraph_reset_node (node);
407 node->local.redefined_extern_inline = true;
410 notice_global_symbol (decl);
411 node->local.finalized = true;
412 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
414 /* With -fkeep-inline-functions we are keeping all inline functions except
415 for extern inline ones. */
416 if (flag_keep_inline_functions
417 && DECL_DECLARED_INLINE_P (decl)
418 && !DECL_EXTERNAL (decl)
419 && !DECL_DISREGARD_INLINE_LIMITS (decl))
420 node->symbol.force_output = 1;
422 /* When not optimizing, also output the static functions. (see
423 PR24561), but don't do so for always_inline functions, functions
424 declared inline and nested functions. These were optimized out
425 in the original implementation and it is unclear whether we want
426 to change the behavior here. */
427 if ((!optimize
428 && !node->same_body_alias
429 && !DECL_DISREGARD_INLINE_LIMITS (decl)
430 && !DECL_DECLARED_INLINE_P (decl)
431 && !(DECL_CONTEXT (decl)
432 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
433 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
434 node->symbol.force_output = 1;
436 /* If we've not yet emitted decl, tell the debug info about it. */
437 if (!TREE_ASM_WRITTEN (decl))
438 (*debug_hooks->deferred_inline_function) (decl);
440 /* Possibly warn about unused parameters. */
441 if (warn_unused_parameter)
442 do_warn_unused_parameter (decl);
444 if (!nested)
445 ggc_collect ();
447 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
448 && (cgraph_decide_is_function_needed (node, decl)
449 || referred_to_p ((symtab_node)node)))
450 enqueue_node ((symtab_node)node);
453 /* Add the function FNDECL to the call graph.
454 Unlike cgraph_finalize_function, this function is intended to be used
455 by middle end and allows insertion of new function at arbitrary point
456 of compilation. The function can be either in high, low or SSA form
457 GIMPLE.
459 The function is assumed to be reachable and have address taken (so no
460 API breaking optimizations are performed on it).
462 Main work done by this function is to enqueue the function for later
463 processing to avoid need the passes to be re-entrant. */
465 void
466 cgraph_add_new_function (tree fndecl, bool lowered)
468 struct cgraph_node *node;
469 switch (cgraph_state)
471 case CGRAPH_STATE_PARSING:
472 cgraph_finalize_function (fndecl, false);
473 break;
474 case CGRAPH_STATE_CONSTRUCTION:
475 /* Just enqueue function to be processed at nearest occurrence. */
476 node = cgraph_create_node (fndecl);
477 if (lowered)
478 node->lowered = true;
479 if (!cgraph_new_nodes)
480 cgraph_new_nodes = cgraph_node_set_new ();
481 cgraph_node_set_add (cgraph_new_nodes, node);
482 break;
484 case CGRAPH_STATE_IPA:
485 case CGRAPH_STATE_IPA_SSA:
486 case CGRAPH_STATE_EXPANSION:
487 /* Bring the function into finalized state and enqueue for later
488 analyzing and compilation. */
489 node = cgraph_get_create_node (fndecl);
490 node->local.local = false;
491 node->local.finalized = true;
492 node->symbol.force_output = true;
493 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
495 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
496 gimple_register_cfg_hooks ();
497 bitmap_obstack_initialize (NULL);
498 execute_pass_list (all_lowering_passes);
499 execute_pass_list (pass_early_local_passes.pass.sub);
500 bitmap_obstack_release (NULL);
501 pop_cfun ();
503 lowered = true;
505 if (lowered)
506 node->lowered = true;
507 if (!cgraph_new_nodes)
508 cgraph_new_nodes = cgraph_node_set_new ();
509 cgraph_node_set_add (cgraph_new_nodes, node);
510 break;
512 case CGRAPH_STATE_FINISHED:
513 /* At the very end of compilation we have to do all the work up
514 to expansion. */
515 node = cgraph_create_node (fndecl);
516 if (lowered)
517 node->lowered = true;
518 cgraph_analyze_function (node);
519 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
520 gimple_register_cfg_hooks ();
521 bitmap_obstack_initialize (NULL);
522 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
523 execute_pass_list (pass_early_local_passes.pass.sub);
524 bitmap_obstack_release (NULL);
525 pop_cfun ();
526 expand_function (node);
527 break;
529 default:
530 gcc_unreachable ();
533 /* Set a personality if required and we already passed EH lowering. */
534 if (lowered
535 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
536 == eh_personality_lang))
537 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
540 /* Add a top-level asm statement to the list. */
542 struct asm_node *
543 add_asm_node (tree asm_str)
545 struct asm_node *node;
547 node = ggc_alloc_cleared_asm_node ();
548 node->asm_str = asm_str;
549 node->order = symtab_order++;
550 node->next = NULL;
551 if (asm_nodes == NULL)
552 asm_nodes = node;
553 else
554 asm_last_node->next = node;
555 asm_last_node = node;
556 return node;
559 /* Output all asm statements we have stored up to be output. */
561 static void
562 output_asm_statements (void)
564 struct asm_node *can;
566 if (seen_error ())
567 return;
569 for (can = asm_nodes; can; can = can->next)
570 assemble_asm (can->asm_str);
571 asm_nodes = NULL;
574 /* C++ FE sometimes change linkage flags after producing same body aliases. */
575 void
576 fixup_same_cpp_alias_visibility (symtab_node node, symtab_node target, tree alias)
578 DECL_VIRTUAL_P (node->symbol.decl) = DECL_VIRTUAL_P (alias);
579 if (TREE_PUBLIC (node->symbol.decl))
581 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (alias);
582 DECL_COMDAT (node->symbol.decl) = DECL_COMDAT (alias);
583 DECL_COMDAT_GROUP (node->symbol.decl) = DECL_COMDAT_GROUP (alias);
584 if (DECL_ONE_ONLY (alias)
585 && !node->symbol.same_comdat_group)
586 symtab_add_to_same_comdat_group ((symtab_node)node, (symtab_node)target);
590 /* Analyze the function scheduled to be output. */
591 static void
592 cgraph_analyze_function (struct cgraph_node *node)
594 tree decl = node->symbol.decl;
595 location_t saved_loc = input_location;
596 input_location = DECL_SOURCE_LOCATION (decl);
598 if (node->alias && node->thunk.alias)
600 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
601 struct cgraph_node *n;
603 for (n = tgt; n && n->alias;
604 n = n->analyzed ? cgraph_alias_aliased_node (n) : NULL)
605 if (n == node)
607 error ("function %q+D part of alias cycle", node->symbol.decl);
608 node->alias = false;
609 input_location = saved_loc;
610 return;
612 if (!vec_safe_length (node->symbol.ref_list.references))
613 ipa_record_reference ((symtab_node)node, (symtab_node)tgt,
614 IPA_REF_ALIAS, NULL);
615 if (node->same_body_alias)
617 DECL_DECLARED_INLINE_P (node->symbol.decl)
618 = DECL_DECLARED_INLINE_P (node->thunk.alias);
619 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl)
620 = DECL_DISREGARD_INLINE_LIMITS (node->thunk.alias);
621 fixup_same_cpp_alias_visibility ((symtab_node) node, (symtab_node) tgt, node->thunk.alias);
624 if (node->symbol.address_taken)
625 cgraph_mark_address_taken_node (cgraph_alias_aliased_node (node));
627 else if (node->thunk.thunk_p)
629 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
630 NULL, 0, CGRAPH_FREQ_BASE);
632 else if (node->dispatcher_function)
634 /* Generate the dispatcher body of multi-versioned functions. */
635 struct cgraph_function_version_info *dispatcher_version_info
636 = get_cgraph_node_version (node);
637 if (dispatcher_version_info != NULL
638 && (dispatcher_version_info->dispatcher_resolver
639 == NULL_TREE))
641 tree resolver = NULL_TREE;
642 gcc_assert (targetm.generate_version_dispatcher_body);
643 resolver = targetm.generate_version_dispatcher_body (node);
644 gcc_assert (resolver != NULL_TREE);
647 else
649 push_cfun (DECL_STRUCT_FUNCTION (decl));
651 assign_assembler_name_if_neeeded (node->symbol.decl);
653 /* Make sure to gimplify bodies only once. During analyzing a
654 function we lower it, which will require gimplified nested
655 functions, so we can end up here with an already gimplified
656 body. */
657 if (!gimple_has_body_p (decl))
658 gimplify_function_tree (decl);
659 dump_function (TDI_generic, decl);
661 /* Lower the function. */
662 if (!node->lowered)
664 if (node->nested)
665 lower_nested_functions (node->symbol.decl);
666 gcc_assert (!node->nested);
668 gimple_register_cfg_hooks ();
669 bitmap_obstack_initialize (NULL);
670 execute_pass_list (all_lowering_passes);
671 free_dominance_info (CDI_POST_DOMINATORS);
672 free_dominance_info (CDI_DOMINATORS);
673 compact_blocks ();
674 bitmap_obstack_release (NULL);
675 node->lowered = true;
678 pop_cfun ();
680 node->analyzed = true;
682 input_location = saved_loc;
685 /* C++ frontend produce same body aliases all over the place, even before PCH
686 gets streamed out. It relies on us linking the aliases with their function
687 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
688 first produce aliases without links, but once C++ FE is sure he won't sream
689 PCH we build the links via this function. */
691 void
692 cgraph_process_same_body_aliases (void)
694 struct cgraph_node *node;
695 FOR_EACH_FUNCTION (node)
696 if (node->same_body_alias
697 && !vec_safe_length (node->symbol.ref_list.references))
699 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
700 ipa_record_reference ((symtab_node)node, (symtab_node)tgt,
701 IPA_REF_ALIAS, NULL);
703 same_body_aliases_done = true;
706 /* Process attributes common for vars and functions. */
708 static void
709 process_common_attributes (tree decl)
711 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
713 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
715 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
716 "%<weakref%> attribute should be accompanied with"
717 " an %<alias%> attribute");
718 DECL_WEAK (decl) = 0;
719 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
720 DECL_ATTRIBUTES (decl));
724 /* Look for externally_visible and used attributes and mark cgraph nodes
725 accordingly.
727 We cannot mark the nodes at the point the attributes are processed (in
728 handle_*_attribute) because the copy of the declarations available at that
729 point may not be canonical. For example, in:
731 void f();
732 void f() __attribute__((used));
734 the declaration we see in handle_used_attribute will be the second
735 declaration -- but the front end will subsequently merge that declaration
736 with the original declaration and discard the second declaration.
738 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
740 void f() {}
741 void f() __attribute__((externally_visible));
743 is valid.
745 So, we walk the nodes at the end of the translation unit, applying the
746 attributes at that point. */
748 static void
749 process_function_and_variable_attributes (struct cgraph_node *first,
750 struct varpool_node *first_var)
752 struct cgraph_node *node;
753 struct varpool_node *vnode;
755 for (node = cgraph_first_function (); node != first;
756 node = cgraph_next_function (node))
758 tree decl = node->symbol.decl;
759 if (DECL_PRESERVE_P (decl))
760 cgraph_mark_force_output_node (node);
761 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
763 if (! TREE_PUBLIC (node->symbol.decl))
764 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
765 "%<externally_visible%>"
766 " attribute have effect only on public objects");
768 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
769 && (node->local.finalized && !node->alias))
771 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
772 "%<weakref%> attribute ignored"
773 " because function is defined");
774 DECL_WEAK (decl) = 0;
775 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
776 DECL_ATTRIBUTES (decl));
779 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
780 && !DECL_DECLARED_INLINE_P (decl)
781 /* redefining extern inline function makes it DECL_UNINLINABLE. */
782 && !DECL_UNINLINABLE (decl))
783 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
784 "always_inline function might not be inlinable");
786 process_common_attributes (decl);
788 for (vnode = varpool_first_variable (); vnode != first_var;
789 vnode = varpool_next_variable (vnode))
791 tree decl = vnode->symbol.decl;
792 if (DECL_EXTERNAL (decl)
793 && DECL_INITIAL (decl)
794 && const_value_known_p (decl))
795 varpool_finalize_decl (decl);
796 if (DECL_PRESERVE_P (decl))
797 vnode->symbol.force_output = true;
798 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
800 if (! TREE_PUBLIC (vnode->symbol.decl))
801 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
802 "%<externally_visible%>"
803 " attribute have effect only on public objects");
805 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
806 && vnode->finalized
807 && DECL_INITIAL (decl))
809 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
810 "%<weakref%> attribute ignored"
811 " because variable is initialized");
812 DECL_WEAK (decl) = 0;
813 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
814 DECL_ATTRIBUTES (decl));
816 process_common_attributes (decl);
820 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
821 middle end to output the variable to asm file, if needed or externally
822 visible. */
824 void
825 varpool_finalize_decl (tree decl)
827 struct varpool_node *node = varpool_node_for_decl (decl);
829 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
831 if (node->finalized)
832 return;
833 notice_global_symbol (decl);
834 node->finalized = true;
835 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
836 /* Traditionally we do not eliminate static variables when not
837 optimizing and when not doing toplevel reoder. */
838 || (!flag_toplevel_reorder && !DECL_COMDAT (node->symbol.decl)
839 && !DECL_ARTIFICIAL (node->symbol.decl)))
840 node->symbol.force_output = true;
842 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
843 && (decide_is_variable_needed (node, decl)
844 || referred_to_p ((symtab_node)node)))
845 enqueue_node ((symtab_node)node);
846 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
847 varpool_analyze_node (node);
848 /* Some frontends produce various interface variables after compilation
849 finished. */
850 if (cgraph_state == CGRAPH_STATE_FINISHED)
851 varpool_assemble_decl (node);
855 /* Determine if a symbol NODE is finalized and needed. */
857 inline static bool
858 symbol_finalized_and_needed (symtab_node node)
860 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
861 return cnode->local.finalized
862 && cgraph_decide_is_function_needed (cnode, cnode->symbol.decl);
863 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
864 return vnode->finalized
865 && !DECL_EXTERNAL (vnode->symbol.decl)
866 && decide_is_variable_needed (vnode, vnode->symbol.decl);
867 return false;
870 /* Determine if a symbol NODE is finalized. */
872 inline static bool
873 symbol_finalized (symtab_node node)
875 if (cgraph_node *cnode= dyn_cast <cgraph_node> (node))
876 return cnode->local.finalized;
877 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
878 return vnode->finalized;
879 return false;
883 /* Discover all functions and variables that are trivially needed, analyze
884 them as well as all functions and variables referred by them */
886 static void
887 cgraph_analyze_functions (void)
889 /* Keep track of already processed nodes when called multiple times for
890 intermodule optimization. */
891 static struct cgraph_node *first_analyzed;
892 struct cgraph_node *first_handled = first_analyzed;
893 static struct varpool_node *first_analyzed_var;
894 struct varpool_node *first_handled_var = first_analyzed_var;
896 symtab_node node, next;
897 int i;
898 struct ipa_ref *ref;
899 bool changed = true;
901 bitmap_obstack_initialize (NULL);
902 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
904 /* Analysis adds static variables that in turn adds references to new functions.
905 So we need to iterate the process until it stabilize. */
906 while (changed)
908 changed = false;
909 process_function_and_variable_attributes (first_analyzed,
910 first_analyzed_var);
912 /* First identify the trivially needed symbols. */
913 for (node = symtab_nodes;
914 node != (symtab_node)first_analyzed
915 && node != (symtab_node)first_analyzed_var; node = node->symbol.next)
917 if (symbol_finalized_and_needed (node))
919 enqueue_node (node);
920 if (!changed && cgraph_dump_file)
921 fprintf (cgraph_dump_file, "Trivially needed symbols:");
922 changed = true;
923 if (cgraph_dump_file)
924 fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node));
926 if (node == (symtab_node)first_analyzed
927 || node == (symtab_node)first_analyzed_var)
928 break;
930 cgraph_process_new_functions ();
931 first_analyzed_var = varpool_first_variable ();
932 first_analyzed = cgraph_first_function ();
934 if (changed && dump_file)
935 fprintf (cgraph_dump_file, "\n");
937 /* Lower representation, build callgraph edges and references for all trivially
938 needed symbols and all symbols referred by them. */
939 while (first != (symtab_node)(void *)1)
941 changed = true;
942 node = first;
943 first = (symtab_node)first->symbol.aux;
944 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
945 if (cnode && cnode->local.finalized)
947 struct cgraph_edge *edge;
948 tree decl = cnode->symbol.decl;
950 /* ??? It is possible to create extern inline function
951 and later using weak alias attribute to kill its body.
952 See gcc.c-torture/compile/20011119-1.c */
953 if (!DECL_STRUCT_FUNCTION (decl)
954 && (!cnode->alias || !cnode->thunk.alias)
955 && !cnode->thunk.thunk_p
956 && !cnode->dispatcher_function)
958 cgraph_reset_node (cnode);
959 cnode->local.redefined_extern_inline = true;
960 continue;
963 if (!cnode->analyzed)
964 cgraph_analyze_function (cnode);
966 for (edge = cnode->callees; edge; edge = edge->next_callee)
967 if (edge->callee->local.finalized)
968 enqueue_node ((symtab_node)edge->callee);
970 /* If decl is a clone of an abstract function,
971 mark that abstract function so that we don't release its body.
972 The DECL_INITIAL() of that abstract function declaration
973 will be later needed to output debug info. */
974 if (DECL_ABSTRACT_ORIGIN (decl))
976 struct cgraph_node *origin_node
977 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
978 origin_node->abstract_and_needed = true;
981 else
983 varpool_node *vnode = dyn_cast <varpool_node> (node);
984 if (vnode && vnode->finalized)
985 varpool_analyze_node (vnode);
988 if (node->symbol.same_comdat_group)
990 symtab_node next;
991 for (next = node->symbol.same_comdat_group;
992 next != node;
993 next = next->symbol.same_comdat_group)
994 enqueue_node (next);
996 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
997 if (symbol_finalized (ref->referred))
998 enqueue_node (ref->referred);
999 cgraph_process_new_functions ();
1003 /* Collect entry points to the unit. */
1004 if (cgraph_dump_file)
1006 fprintf (cgraph_dump_file, "\n\nInitial ");
1007 dump_symtab (cgraph_dump_file);
1010 if (cgraph_dump_file)
1011 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
1013 for (node = symtab_nodes;
1014 node != (symtab_node)first_handled
1015 && node != (symtab_node)first_handled_var; node = next)
1017 next = node->symbol.next;
1018 if (!node->symbol.aux && !referred_to_p (node))
1020 if (cgraph_dump_file)
1021 fprintf (cgraph_dump_file, " %s", symtab_node_name (node));
1022 symtab_remove_node (node);
1023 continue;
1025 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1027 tree decl = node->symbol.decl;
1029 if (cnode->local.finalized && !gimple_has_body_p (decl)
1030 && (!cnode->alias || !cnode->thunk.alias)
1031 && !cnode->thunk.thunk_p)
1032 cgraph_reset_node (cnode);
1034 gcc_assert (!cnode->local.finalized || cnode->thunk.thunk_p
1035 || cnode->alias
1036 || gimple_has_body_p (decl));
1037 gcc_assert (cnode->analyzed == cnode->local.finalized);
1039 node->symbol.aux = NULL;
1041 first_analyzed = cgraph_first_function ();
1042 first_analyzed_var = varpool_first_variable ();
1043 if (cgraph_dump_file)
1045 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1046 dump_symtab (cgraph_dump_file);
1048 bitmap_obstack_release (NULL);
1049 ggc_collect ();
1052 /* Translate the ugly representation of aliases as alias pairs into nice
1053 representation in callgraph. We don't handle all cases yet,
1054 unforutnately. */
1056 static void
1057 handle_alias_pairs (void)
1059 alias_pair *p;
1060 unsigned i;
1062 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1064 symtab_node target_node = symtab_node_for_asm (p->target);
1066 /* Weakrefs with target not defined in current unit are easy to handle; they
1067 behave just as external variables except we need to note the alias flag
1068 to later output the weakref pseudo op into asm file. */
1069 if (!target_node && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1071 if (TREE_CODE (p->decl) == FUNCTION_DECL)
1072 cgraph_get_create_node (p->decl)->alias = true;
1073 else
1074 varpool_get_node (p->decl)->alias = true;
1075 DECL_EXTERNAL (p->decl) = 1;
1076 alias_pairs->unordered_remove (i);
1077 continue;
1079 else if (!target_node)
1081 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1082 alias_pairs->unordered_remove (i);
1083 continue;
1086 /* Normally EXTERNAL flag is used to mark external inlines,
1087 however for aliases it seems to be allowed to use it w/o
1088 any meaning. See gcc.dg/attr-alias-3.c
1089 However for weakref we insist on EXTERNAL flag being set.
1090 See gcc.dg/attr-alias-5.c */
1091 if (DECL_EXTERNAL (p->decl))
1092 DECL_EXTERNAL (p->decl)
1093 = lookup_attribute ("weakref",
1094 DECL_ATTRIBUTES (p->decl)) != NULL;
1096 if (DECL_EXTERNAL (target_node->symbol.decl)
1097 /* We use local aliases for C++ thunks to force the tailcall
1098 to bind locally. This is a hack - to keep it working do
1099 the following (which is not strictly correct). */
1100 && (! TREE_CODE (target_node->symbol.decl) == FUNCTION_DECL
1101 || ! DECL_VIRTUAL_P (target_node->symbol.decl))
1102 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1104 error ("%q+D aliased to external symbol %qE",
1105 p->decl, p->target);
1108 if (TREE_CODE (p->decl) == FUNCTION_DECL
1109 && target_node && is_a <cgraph_node> (target_node))
1111 struct cgraph_node *src_node = cgraph_get_node (p->decl);
1112 if (src_node && src_node->local.finalized)
1113 cgraph_reset_node (src_node);
1114 cgraph_create_function_alias (p->decl, target_node->symbol.decl);
1115 alias_pairs->unordered_remove (i);
1117 else if (TREE_CODE (p->decl) == VAR_DECL
1118 && target_node && is_a <varpool_node> (target_node))
1120 varpool_create_variable_alias (p->decl, target_node->symbol.decl);
1121 alias_pairs->unordered_remove (i);
1123 else
1125 error ("%q+D alias in between function and variable is not supported",
1126 p->decl);
1127 warning (0, "%q+D aliased declaration",
1128 target_node->symbol.decl);
1129 alias_pairs->unordered_remove (i);
1132 vec_free (alias_pairs);
1136 /* Figure out what functions we want to assemble. */
1138 static void
1139 mark_functions_to_output (void)
1141 struct cgraph_node *node;
1142 #ifdef ENABLE_CHECKING
1143 bool check_same_comdat_groups = false;
1145 FOR_EACH_FUNCTION (node)
1146 gcc_assert (!node->process);
1147 #endif
1149 FOR_EACH_FUNCTION (node)
1151 tree decl = node->symbol.decl;
1153 gcc_assert (!node->process || node->symbol.same_comdat_group);
1154 if (node->process)
1155 continue;
1157 /* We need to output all local functions that are used and not
1158 always inlined, as well as those that are reachable from
1159 outside the current compilation unit. */
1160 if (node->analyzed
1161 && !node->thunk.thunk_p
1162 && !node->alias
1163 && !node->global.inlined_to
1164 && !TREE_ASM_WRITTEN (decl)
1165 && !DECL_EXTERNAL (decl))
1167 node->process = 1;
1168 if (node->symbol.same_comdat_group)
1170 struct cgraph_node *next;
1171 for (next = cgraph (node->symbol.same_comdat_group);
1172 next != node;
1173 next = cgraph (next->symbol.same_comdat_group))
1174 if (!next->thunk.thunk_p && !next->alias)
1175 next->process = 1;
1178 else if (node->symbol.same_comdat_group)
1180 #ifdef ENABLE_CHECKING
1181 check_same_comdat_groups = true;
1182 #endif
1184 else
1186 /* We should've reclaimed all functions that are not needed. */
1187 #ifdef ENABLE_CHECKING
1188 if (!node->global.inlined_to
1189 && gimple_has_body_p (decl)
1190 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1191 are inside partition, we can end up not removing the body since we no longer
1192 have analyzed node pointing to it. */
1193 && !node->symbol.in_other_partition
1194 && !node->alias
1195 && !node->clones
1196 && !DECL_EXTERNAL (decl))
1198 dump_cgraph_node (stderr, node);
1199 internal_error ("failed to reclaim unneeded function");
1201 #endif
1202 gcc_assert (node->global.inlined_to
1203 || !gimple_has_body_p (decl)
1204 || node->symbol.in_other_partition
1205 || node->clones
1206 || DECL_ARTIFICIAL (decl)
1207 || DECL_EXTERNAL (decl));
1212 #ifdef ENABLE_CHECKING
1213 if (check_same_comdat_groups)
1214 FOR_EACH_FUNCTION (node)
1215 if (node->symbol.same_comdat_group && !node->process)
1217 tree decl = node->symbol.decl;
1218 if (!node->global.inlined_to
1219 && gimple_has_body_p (decl)
1220 /* FIXME: in an ltrans unit when the offline copy is outside a
1221 partition but inline copies are inside a partition, we can
1222 end up not removing the body since we no longer have an
1223 analyzed node pointing to it. */
1224 && !node->symbol.in_other_partition
1225 && !node->clones
1226 && !DECL_EXTERNAL (decl))
1228 dump_cgraph_node (stderr, node);
1229 internal_error ("failed to reclaim unneeded function in same "
1230 "comdat group");
1233 #endif
1236 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1237 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1239 Set current_function_decl and cfun to newly constructed empty function body.
1240 return basic block in the function body. */
1242 basic_block
1243 init_lowered_empty_function (tree decl, bool in_ssa)
1245 basic_block bb;
1247 current_function_decl = decl;
1248 allocate_struct_function (decl, false);
1249 gimple_register_cfg_hooks ();
1250 init_empty_tree_cfg ();
1252 if (in_ssa)
1254 init_tree_ssa (cfun);
1255 init_ssa_operands (cfun);
1256 cfun->gimple_df->in_ssa_p = true;
1259 DECL_INITIAL (decl) = make_node (BLOCK);
1261 DECL_SAVED_TREE (decl) = error_mark_node;
1262 cfun->curr_properties |=
1263 (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_ssa | PROP_gimple_any);
1265 /* Create BB for body of the function and connect it properly. */
1266 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
1267 make_edge (ENTRY_BLOCK_PTR, bb, 0);
1268 make_edge (bb, EXIT_BLOCK_PTR, 0);
1270 return bb;
1273 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1274 offset indicated by VIRTUAL_OFFSET, if that is
1275 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1276 zero for a result adjusting thunk. */
1278 static tree
1279 thunk_adjust (gimple_stmt_iterator * bsi,
1280 tree ptr, bool this_adjusting,
1281 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1283 gimple stmt;
1284 tree ret;
1286 if (this_adjusting
1287 && fixed_offset != 0)
1289 stmt = gimple_build_assign
1290 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1291 ptr,
1292 fixed_offset));
1293 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1296 /* If there's a virtual offset, look up that value in the vtable and
1297 adjust the pointer again. */
1298 if (virtual_offset)
1300 tree vtabletmp;
1301 tree vtabletmp2;
1302 tree vtabletmp3;
1304 if (!vtable_entry_type)
1306 tree vfunc_type = make_node (FUNCTION_TYPE);
1307 TREE_TYPE (vfunc_type) = integer_type_node;
1308 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1309 layout_type (vfunc_type);
1311 vtable_entry_type = build_pointer_type (vfunc_type);
1314 vtabletmp =
1315 create_tmp_reg (build_pointer_type
1316 (build_pointer_type (vtable_entry_type)), "vptr");
1318 /* The vptr is always at offset zero in the object. */
1319 stmt = gimple_build_assign (vtabletmp,
1320 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1321 ptr));
1322 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1324 /* Form the vtable address. */
1325 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1326 "vtableaddr");
1327 stmt = gimple_build_assign (vtabletmp2,
1328 build_simple_mem_ref (vtabletmp));
1329 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1331 /* Find the entry with the vcall offset. */
1332 stmt = gimple_build_assign (vtabletmp2,
1333 fold_build_pointer_plus_loc (input_location,
1334 vtabletmp2,
1335 virtual_offset));
1336 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1338 /* Get the offset itself. */
1339 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1340 "vcalloffset");
1341 stmt = gimple_build_assign (vtabletmp3,
1342 build_simple_mem_ref (vtabletmp2));
1343 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1345 /* Adjust the `this' pointer. */
1346 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1347 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1348 GSI_CONTINUE_LINKING);
1351 if (!this_adjusting
1352 && fixed_offset != 0)
1353 /* Adjust the pointer by the constant. */
1355 tree ptrtmp;
1357 if (TREE_CODE (ptr) == VAR_DECL)
1358 ptrtmp = ptr;
1359 else
1361 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1362 stmt = gimple_build_assign (ptrtmp, ptr);
1363 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1365 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1366 ptrtmp, fixed_offset);
1369 /* Emit the statement and gimplify the adjustment expression. */
1370 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1371 stmt = gimple_build_assign (ret, ptr);
1372 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1374 return ret;
1377 /* Produce assembler for thunk NODE. */
1379 static void
1380 assemble_thunk (struct cgraph_node *node)
1382 bool this_adjusting = node->thunk.this_adjusting;
1383 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1384 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1385 tree virtual_offset = NULL;
1386 tree alias = node->thunk.alias;
1387 tree thunk_fndecl = node->symbol.decl;
1388 tree a = DECL_ARGUMENTS (thunk_fndecl);
1390 current_function_decl = thunk_fndecl;
1392 /* Ensure thunks are emitted in their correct sections. */
1393 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1395 if (this_adjusting
1396 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1397 virtual_value, alias))
1399 const char *fnname;
1400 tree fn_block;
1401 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1403 DECL_RESULT (thunk_fndecl)
1404 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1405 RESULT_DECL, 0, restype);
1406 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1408 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1409 create one. */
1410 fn_block = make_node (BLOCK);
1411 BLOCK_VARS (fn_block) = a;
1412 DECL_INITIAL (thunk_fndecl) = fn_block;
1413 init_function_start (thunk_fndecl);
1414 cfun->is_thunk = 1;
1415 insn_locations_init ();
1416 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1417 prologue_location = curr_insn_location ();
1418 assemble_start_function (thunk_fndecl, fnname);
1420 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1421 fixed_offset, virtual_value, alias);
1423 assemble_end_function (thunk_fndecl, fnname);
1424 insn_locations_finalize ();
1425 init_insn_lengths ();
1426 free_after_compilation (cfun);
1427 set_cfun (NULL);
1428 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1429 node->thunk.thunk_p = false;
1430 node->analyzed = false;
1432 else
1434 tree restype;
1435 basic_block bb, then_bb, else_bb, return_bb;
1436 gimple_stmt_iterator bsi;
1437 int nargs = 0;
1438 tree arg;
1439 int i;
1440 tree resdecl;
1441 tree restmp = NULL;
1442 vec<tree> vargs;
1444 gimple call;
1445 gimple ret;
1447 DECL_IGNORED_P (thunk_fndecl) = 1;
1448 bitmap_obstack_initialize (NULL);
1450 if (node->thunk.virtual_offset_p)
1451 virtual_offset = size_int (virtual_value);
1453 /* Build the return declaration for the function. */
1454 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1455 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1457 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1458 DECL_ARTIFICIAL (resdecl) = 1;
1459 DECL_IGNORED_P (resdecl) = 1;
1460 DECL_RESULT (thunk_fndecl) = resdecl;
1462 else
1463 resdecl = DECL_RESULT (thunk_fndecl);
1465 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1467 bsi = gsi_start_bb (bb);
1469 /* Build call to the function being thunked. */
1470 if (!VOID_TYPE_P (restype))
1472 if (!is_gimple_reg_type (restype))
1474 restmp = resdecl;
1475 add_local_decl (cfun, restmp);
1476 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1478 else
1479 restmp = create_tmp_reg (restype, "retval");
1482 for (arg = a; arg; arg = DECL_CHAIN (arg))
1483 nargs++;
1484 vargs.create (nargs);
1485 if (this_adjusting)
1486 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1487 virtual_offset));
1488 else
1489 vargs.quick_push (a);
1490 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1491 vargs.quick_push (arg);
1492 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1493 vargs.release ();
1494 gimple_call_set_from_thunk (call, true);
1495 if (restmp)
1496 gimple_call_set_lhs (call, restmp);
1497 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1499 if (restmp && !this_adjusting)
1501 tree true_label = NULL_TREE;
1503 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1505 gimple stmt;
1506 /* If the return type is a pointer, we need to
1507 protect against NULL. We know there will be an
1508 adjustment, because that's why we're emitting a
1509 thunk. */
1510 then_bb = create_basic_block (NULL, (void *) 0, bb);
1511 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1512 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1513 remove_edge (single_succ_edge (bb));
1514 true_label = gimple_block_label (then_bb);
1515 stmt = gimple_build_cond (NE_EXPR, restmp,
1516 build_zero_cst (TREE_TYPE (restmp)),
1517 NULL_TREE, NULL_TREE);
1518 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1519 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1520 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1521 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1522 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1523 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1524 bsi = gsi_last_bb (then_bb);
1527 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1528 fixed_offset, virtual_offset);
1529 if (true_label)
1531 gimple stmt;
1532 bsi = gsi_last_bb (else_bb);
1533 stmt = gimple_build_assign (restmp,
1534 build_zero_cst (TREE_TYPE (restmp)));
1535 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1536 bsi = gsi_last_bb (return_bb);
1539 else
1540 gimple_call_set_tail (call, true);
1542 /* Build return value. */
1543 ret = gimple_build_return (restmp);
1544 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1546 delete_unreachable_blocks ();
1547 update_ssa (TODO_update_ssa);
1549 /* Since we want to emit the thunk, we explicitly mark its name as
1550 referenced. */
1551 node->thunk.thunk_p = false;
1552 cgraph_node_remove_callees (node);
1553 cgraph_add_new_function (thunk_fndecl, true);
1554 bitmap_obstack_release (NULL);
1556 current_function_decl = NULL;
1557 set_cfun (NULL);
1562 /* Assemble thunks and aliases associated to NODE. */
1564 static void
1565 assemble_thunks_and_aliases (struct cgraph_node *node)
1567 struct cgraph_edge *e;
1568 int i;
1569 struct ipa_ref *ref;
1571 for (e = node->callers; e;)
1572 if (e->caller->thunk.thunk_p)
1574 struct cgraph_node *thunk = e->caller;
1576 e = e->next_caller;
1577 assemble_thunks_and_aliases (thunk);
1578 assemble_thunk (thunk);
1580 else
1581 e = e->next_caller;
1582 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
1583 i, ref); i++)
1584 if (ref->use == IPA_REF_ALIAS)
1586 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1587 bool saved_written = TREE_ASM_WRITTEN (alias->thunk.alias);
1589 /* Force assemble_alias to really output the alias this time instead
1590 of buffering it in same alias pairs. */
1591 TREE_ASM_WRITTEN (alias->thunk.alias) = 1;
1592 do_assemble_alias (alias->symbol.decl,
1593 DECL_ASSEMBLER_NAME (alias->thunk.alias));
1594 assemble_thunks_and_aliases (alias);
1595 TREE_ASM_WRITTEN (alias->thunk.alias) = saved_written;
1599 /* Expand function specified by NODE. */
1601 static void
1602 expand_function (struct cgraph_node *node)
1604 tree decl = node->symbol.decl;
1605 location_t saved_loc;
1607 /* We ought to not compile any inline clones. */
1608 gcc_assert (!node->global.inlined_to);
1610 announce_function (decl);
1611 node->process = 0;
1612 gcc_assert (node->lowered);
1614 /* Generate RTL for the body of DECL. */
1616 timevar_push (TV_REST_OF_COMPILATION);
1618 gcc_assert (cgraph_global_info_ready);
1620 /* Initialize the default bitmap obstack. */
1621 bitmap_obstack_initialize (NULL);
1623 /* Initialize the RTL code for the function. */
1624 current_function_decl = decl;
1625 saved_loc = input_location;
1626 input_location = DECL_SOURCE_LOCATION (decl);
1627 init_function_start (decl);
1629 gimple_register_cfg_hooks ();
1631 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1633 execute_all_ipa_transforms ();
1635 /* Perform all tree transforms and optimizations. */
1637 /* Signal the start of passes. */
1638 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1640 execute_pass_list (all_passes);
1642 /* Signal the end of passes. */
1643 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1645 bitmap_obstack_release (&reg_obstack);
1647 /* Release the default bitmap obstack. */
1648 bitmap_obstack_release (NULL);
1650 /* If requested, warn about function definitions where the function will
1651 return a value (usually of some struct or union type) which itself will
1652 take up a lot of stack space. */
1653 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1655 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1657 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1658 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1659 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1660 larger_than_size))
1662 unsigned int size_as_int
1663 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1665 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1666 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1667 decl, size_as_int);
1668 else
1669 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1670 decl, larger_than_size);
1674 gimple_set_body (decl, NULL);
1675 if (DECL_STRUCT_FUNCTION (decl) == 0
1676 && !cgraph_get_node (decl)->origin)
1678 /* Stop pointing to the local nodes about to be freed.
1679 But DECL_INITIAL must remain nonzero so we know this
1680 was an actual function definition.
1681 For a nested function, this is done in c_pop_function_context.
1682 If rest_of_compilation set this to 0, leave it 0. */
1683 if (DECL_INITIAL (decl) != 0)
1684 DECL_INITIAL (decl) = error_mark_node;
1687 input_location = saved_loc;
1689 ggc_collect ();
1690 timevar_pop (TV_REST_OF_COMPILATION);
1692 /* Make sure that BE didn't give up on compiling. */
1693 gcc_assert (TREE_ASM_WRITTEN (decl));
1694 set_cfun (NULL);
1695 current_function_decl = NULL;
1697 /* It would make a lot more sense to output thunks before function body to get more
1698 forward and lest backwarding jumps. This however would need solving problem
1699 with comdats. See PR48668. Also aliases must come after function itself to
1700 make one pass assemblers, like one on AIX, happy. See PR 50689.
1701 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1702 groups. */
1703 assemble_thunks_and_aliases (node);
1704 cgraph_release_function_body (node);
1705 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1706 points to the dead function body. */
1707 cgraph_node_remove_callees (node);
1711 /* Expand all functions that must be output.
1713 Attempt to topologically sort the nodes so function is output when
1714 all called functions are already assembled to allow data to be
1715 propagated across the callgraph. Use a stack to get smaller distance
1716 between a function and its callees (later we may choose to use a more
1717 sophisticated algorithm for function reordering; we will likely want
1718 to use subsections to make the output functions appear in top-down
1719 order). */
1721 static void
1722 expand_all_functions (void)
1724 struct cgraph_node *node;
1725 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1726 int order_pos, new_order_pos = 0;
1727 int i;
1729 order_pos = ipa_reverse_postorder (order);
1730 gcc_assert (order_pos == cgraph_n_nodes);
1732 /* Garbage collector may remove inline clones we eliminate during
1733 optimization. So we must be sure to not reference them. */
1734 for (i = 0; i < order_pos; i++)
1735 if (order[i]->process)
1736 order[new_order_pos++] = order[i];
1738 for (i = new_order_pos - 1; i >= 0; i--)
1740 node = order[i];
1741 if (node->process)
1743 node->process = 0;
1744 expand_function (node);
1747 cgraph_process_new_functions ();
1749 free (order);
1753 /* This is used to sort the node types by the cgraph order number. */
1755 enum cgraph_order_sort_kind
1757 ORDER_UNDEFINED = 0,
1758 ORDER_FUNCTION,
1759 ORDER_VAR,
1760 ORDER_ASM
1763 struct cgraph_order_sort
1765 enum cgraph_order_sort_kind kind;
1766 union
1768 struct cgraph_node *f;
1769 struct varpool_node *v;
1770 struct asm_node *a;
1771 } u;
1774 /* Output all functions, variables, and asm statements in the order
1775 according to their order fields, which is the order in which they
1776 appeared in the file. This implements -fno-toplevel-reorder. In
1777 this mode we may output functions and variables which don't really
1778 need to be output. */
1780 static void
1781 output_in_order (void)
1783 int max;
1784 struct cgraph_order_sort *nodes;
1785 int i;
1786 struct cgraph_node *pf;
1787 struct varpool_node *pv;
1788 struct asm_node *pa;
1790 max = symtab_order;
1791 nodes = XCNEWVEC (struct cgraph_order_sort, max);
1793 FOR_EACH_DEFINED_FUNCTION (pf)
1795 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1797 i = pf->symbol.order;
1798 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1799 nodes[i].kind = ORDER_FUNCTION;
1800 nodes[i].u.f = pf;
1804 FOR_EACH_DEFINED_VARIABLE (pv)
1805 if (!DECL_EXTERNAL (pv->symbol.decl))
1807 i = pv->symbol.order;
1808 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1809 nodes[i].kind = ORDER_VAR;
1810 nodes[i].u.v = pv;
1813 for (pa = asm_nodes; pa; pa = pa->next)
1815 i = pa->order;
1816 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1817 nodes[i].kind = ORDER_ASM;
1818 nodes[i].u.a = pa;
1821 /* In toplevel reorder mode we output all statics; mark them as needed. */
1823 for (i = 0; i < max; ++i)
1824 if (nodes[i].kind == ORDER_VAR)
1825 varpool_finalize_named_section_flags (nodes[i].u.v);
1827 for (i = 0; i < max; ++i)
1829 switch (nodes[i].kind)
1831 case ORDER_FUNCTION:
1832 nodes[i].u.f->process = 0;
1833 expand_function (nodes[i].u.f);
1834 break;
1836 case ORDER_VAR:
1837 varpool_assemble_decl (nodes[i].u.v);
1838 break;
1840 case ORDER_ASM:
1841 assemble_asm (nodes[i].u.a->asm_str);
1842 break;
1844 case ORDER_UNDEFINED:
1845 break;
1847 default:
1848 gcc_unreachable ();
1852 asm_nodes = NULL;
1853 free (nodes);
1856 static void
1857 ipa_passes (void)
1859 set_cfun (NULL);
1860 current_function_decl = NULL;
1861 gimple_register_cfg_hooks ();
1862 bitmap_obstack_initialize (NULL);
1864 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1866 if (!in_lto_p)
1868 execute_ipa_pass_list (all_small_ipa_passes);
1869 if (seen_error ())
1870 return;
1873 /* We never run removal of unreachable nodes after early passes. This is
1874 because TODO is run before the subpasses. It is important to remove
1875 the unreachable functions to save works at IPA level and to get LTO
1876 symbol tables right. */
1877 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
1879 /* If pass_all_early_optimizations was not scheduled, the state of
1880 the cgraph will not be properly updated. Update it now. */
1881 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1882 cgraph_state = CGRAPH_STATE_IPA_SSA;
1884 if (!in_lto_p)
1886 /* Generate coverage variables and constructors. */
1887 coverage_finish ();
1889 /* Process new functions added. */
1890 set_cfun (NULL);
1891 current_function_decl = NULL;
1892 cgraph_process_new_functions ();
1894 execute_ipa_summary_passes
1895 ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
1898 /* Some targets need to handle LTO assembler output specially. */
1899 if (flag_generate_lto)
1900 targetm.asm_out.lto_start ();
1902 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
1904 if (!in_lto_p)
1905 ipa_write_summaries ();
1907 if (flag_generate_lto)
1908 targetm.asm_out.lto_end ();
1910 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
1911 execute_ipa_pass_list (all_regular_ipa_passes);
1912 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
1914 bitmap_obstack_release (NULL);
1918 /* Return string alias is alias of. */
1920 static tree
1921 get_alias_symbol (tree decl)
1923 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1924 return get_identifier (TREE_STRING_POINTER
1925 (TREE_VALUE (TREE_VALUE (alias))));
1929 /* Weakrefs may be associated to external decls and thus not output
1930 at expansion time. Emit all necessary aliases. */
1932 static void
1933 output_weakrefs (void)
1935 struct cgraph_node *node;
1936 struct varpool_node *vnode;
1937 FOR_EACH_FUNCTION (node)
1938 if (node->alias && DECL_EXTERNAL (node->symbol.decl)
1939 && !TREE_ASM_WRITTEN (node->symbol.decl)
1940 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->symbol.decl)))
1941 do_assemble_alias (node->symbol.decl,
1942 node->thunk.alias ? DECL_ASSEMBLER_NAME (node->thunk.alias)
1943 : get_alias_symbol (node->symbol.decl));
1944 FOR_EACH_VARIABLE (vnode)
1945 if (vnode->alias && DECL_EXTERNAL (vnode->symbol.decl)
1946 && !TREE_ASM_WRITTEN (vnode->symbol.decl)
1947 && lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->symbol.decl)))
1948 do_assemble_alias (vnode->symbol.decl,
1949 vnode->alias_of ? DECL_ASSEMBLER_NAME (vnode->alias_of)
1950 : get_alias_symbol (vnode->symbol.decl));
1953 /* Initialize callgraph dump file. */
1955 void
1956 init_cgraph (void)
1958 if (!cgraph_dump_file)
1959 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1963 /* Perform simple optimizations based on callgraph. */
1965 void
1966 compile (void)
1968 if (seen_error ())
1969 return;
1971 #ifdef ENABLE_CHECKING
1972 verify_symtab ();
1973 #endif
1975 timevar_push (TV_CGRAPHOPT);
1976 if (pre_ipa_mem_report)
1978 fprintf (stderr, "Memory consumption before IPA\n");
1979 dump_memory_report (false);
1981 if (!quiet_flag)
1982 fprintf (stderr, "Performing interprocedural optimizations\n");
1983 cgraph_state = CGRAPH_STATE_IPA;
1985 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1986 if (flag_lto)
1987 lto_streamer_hooks_init ();
1989 /* Don't run the IPA passes if there was any error or sorry messages. */
1990 if (!seen_error ())
1991 ipa_passes ();
1993 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
1994 if (seen_error ()
1995 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
1997 timevar_pop (TV_CGRAPHOPT);
1998 return;
2001 /* This pass remove bodies of extern inline functions we never inlined.
2002 Do this later so other IPA passes see what is really going on. */
2003 symtab_remove_unreachable_nodes (false, dump_file);
2004 cgraph_global_info_ready = true;
2005 if (cgraph_dump_file)
2007 fprintf (cgraph_dump_file, "Optimized ");
2008 dump_symtab (cgraph_dump_file);
2010 if (post_ipa_mem_report)
2012 fprintf (stderr, "Memory consumption after IPA\n");
2013 dump_memory_report (false);
2015 timevar_pop (TV_CGRAPHOPT);
2017 /* Output everything. */
2018 (*debug_hooks->assembly_start) ();
2019 if (!quiet_flag)
2020 fprintf (stderr, "Assembling functions:\n");
2021 #ifdef ENABLE_CHECKING
2022 verify_symtab ();
2023 #endif
2025 cgraph_materialize_all_clones ();
2026 bitmap_obstack_initialize (NULL);
2027 execute_ipa_pass_list (all_late_ipa_passes);
2028 symtab_remove_unreachable_nodes (true, dump_file);
2029 #ifdef ENABLE_CHECKING
2030 verify_symtab ();
2031 #endif
2032 bitmap_obstack_release (NULL);
2033 mark_functions_to_output ();
2035 cgraph_state = CGRAPH_STATE_EXPANSION;
2036 if (!flag_toplevel_reorder)
2037 output_in_order ();
2038 else
2040 output_asm_statements ();
2042 expand_all_functions ();
2043 varpool_output_variables ();
2046 cgraph_process_new_functions ();
2047 cgraph_state = CGRAPH_STATE_FINISHED;
2048 output_weakrefs ();
2050 if (cgraph_dump_file)
2052 fprintf (cgraph_dump_file, "\nFinal ");
2053 dump_symtab (cgraph_dump_file);
2055 #ifdef ENABLE_CHECKING
2056 verify_symtab ();
2057 /* Double check that all inline clones are gone and that all
2058 function bodies have been released from memory. */
2059 if (!seen_error ())
2061 struct cgraph_node *node;
2062 bool error_found = false;
2064 FOR_EACH_DEFINED_FUNCTION (node)
2065 if (node->global.inlined_to
2066 || gimple_has_body_p (node->symbol.decl))
2068 error_found = true;
2069 dump_cgraph_node (stderr, node);
2071 if (error_found)
2072 internal_error ("nodes with unreleased memory found");
2074 #endif
2078 /* Analyze the whole compilation unit once it is parsed completely. */
2080 void
2081 finalize_compilation_unit (void)
2083 timevar_push (TV_CGRAPH);
2085 /* If we're here there's no current function anymore. Some frontends
2086 are lazy in clearing these. */
2087 current_function_decl = NULL;
2088 set_cfun (NULL);
2090 /* Do not skip analyzing the functions if there were errors, we
2091 miss diagnostics for following functions otherwise. */
2093 /* Emit size functions we didn't inline. */
2094 finalize_size_functions ();
2096 /* Mark alias targets necessary and emit diagnostics. */
2097 handle_alias_pairs ();
2099 if (!quiet_flag)
2101 fprintf (stderr, "\nAnalyzing compilation unit\n");
2102 fflush (stderr);
2105 if (flag_dump_passes)
2106 dump_passes ();
2108 /* Gimplify and lower all functions, compute reachability and
2109 remove unreachable nodes. */
2110 cgraph_analyze_functions ();
2112 /* Mark alias targets necessary and emit diagnostics. */
2113 handle_alias_pairs ();
2115 /* Gimplify and lower thunks. */
2116 cgraph_analyze_functions ();
2118 /* Finally drive the pass manager. */
2119 compile ();
2121 timevar_pop (TV_CGRAPH);
2125 #include "gt-cgraphunit.h"