re PR c++/29234 (Call to operator() of temporary object wrongly parsed)
[gcc.git] / gcc / cgraphunit.c
1 /* Driver of optimization process
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 /* This module implements main driver of compilation process.
22
23 The main scope of this file is to act as an interface in between
24 tree based frontends and the backend.
25
26 The front-end is supposed to use following functionality:
27
28 - cgraph_finalize_function
29
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.
32
33 (There is one exception needed for implementing GCC extern inline
34 function.)
35
36 - varpool_finalize_decl
37
38 This function has same behavior as the above but is used for static
39 variables.
40
41 - add_asm_node
42
43 Insert new toplevel ASM statement
44
45 - finalize_compilation_unit
46
47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
49
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.
54
55 At the end the bodies of unreachable functions are removed.
56
57 The function can be called multiple times when multiple source level
58 compilation units are combined.
59
60 - compile
61
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).
67
68 Compile time:
69
70 1) Inter-procedural optimization.
71 (ipa_passes)
72
73 This part is further split into:
74
75 a) early optimizations. These are local passes executed in
76 the topological order on the callgraph.
77
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.
82
83 b) early small interprocedural passes.
84
85 Those are interprocedural passes executed only at compilation
86 time. These include, for example, transational memory lowering,
87 unreachable code removal and other simple transformations.
88
89 c) IP analysis stage. All interprocedural passes do their
90 analysis.
91
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.
97
98 d) LTO sreaming. When doing LTO, everything important gets
99 streamed into the object file.
100
101 Compile time and or linktime analysis stage (WPA):
102
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.
111
112 Compile time and/or parallel linktime stage (ltrans)
113
114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
116 again.
117
118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
120
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
127
128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
130
131 4) late small IP passes
132
133 Simple IP passes working within single program partition.
134
135 5) Expansion
136 (expand_all_functions)
137
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.
143
144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
146
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.
153
154 - cgraph_function_versioning
155
156 produces a copy of function into new one (a version)
157 and apply simple transformations
158 */
159
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 "gimple.h"
168 #include "gimple-ssa.h"
169 #include "tree-cfg.h"
170 #include "tree-into-ssa.h"
171 #include "tree-ssa.h"
172 #include "tree-inline.h"
173 #include "langhooks.h"
174 #include "pointer-set.h"
175 #include "toplev.h"
176 #include "flags.h"
177 #include "ggc.h"
178 #include "debug.h"
179 #include "target.h"
180 #include "diagnostic.h"
181 #include "params.h"
182 #include "fibheap.h"
183 #include "intl.h"
184 #include "function.h"
185 #include "ipa-prop.h"
186 #include "tree-iterator.h"
187 #include "tree-pass.h"
188 #include "tree-dump.h"
189 #include "gimple-pretty-print.h"
190 #include "output.h"
191 #include "coverage.h"
192 #include "plugin.h"
193 #include "ipa-inline.h"
194 #include "ipa-utils.h"
195 #include "lto-streamer.h"
196 #include "except.h"
197 #include "cfgloop.h"
198 #include "regset.h" /* FIXME: For reg_obstack. */
199 #include "context.h"
200 #include "pass_manager.h"
201
202 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
203 secondary queue used during optimization to accommodate passes that
204 may generate new functions that need to be optimized and expanded. */
205 cgraph_node_set cgraph_new_nodes;
206
207 static void expand_all_functions (void);
208 static void mark_functions_to_output (void);
209 static void expand_function (struct cgraph_node *);
210 static void analyze_function (struct cgraph_node *);
211 static void handle_alias_pairs (void);
212
213 FILE *cgraph_dump_file;
214
215 /* Linked list of cgraph asm nodes. */
216 struct asm_node *asm_nodes;
217
218 /* Last node in cgraph_asm_nodes. */
219 static GTY(()) struct asm_node *asm_last_node;
220
221 /* Used for vtable lookup in thunk adjusting. */
222 static GTY (()) tree vtable_entry_type;
223
224 /* Determine if symbol DECL is needed. That is, visible to something
225 either outside this translation unit, something magic in the system
226 configury */
227 bool
228 decide_is_symbol_needed (symtab_node *node)
229 {
230 tree decl = node->decl;
231
232 /* Double check that no one output the function into assembly file
233 early. */
234 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
235 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
236
237 if (!node->definition)
238 return false;
239
240 if (DECL_EXTERNAL (decl))
241 return false;
242
243 /* If the user told us it is used, then it must be so. */
244 if (node->force_output)
245 return true;
246
247 /* ABI forced symbols are needed when they are external. */
248 if (node->forced_by_abi && TREE_PUBLIC (decl))
249 return true;
250
251 /* Keep constructors, destructors and virtual functions. */
252 if (TREE_CODE (decl) == FUNCTION_DECL
253 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
254 return true;
255
256 /* Externally visible variables must be output. The exception is
257 COMDAT variables that must be output only when they are needed. */
258 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
259 return true;
260
261 return false;
262 }
263
264 /* Head of the queue of nodes to be processed while building callgraph */
265
266 static symtab_node *first = (symtab_node *)(void *)1;
267
268 /* Add NODE to queue starting at FIRST.
269 The queue is linked via AUX pointers and terminated by pointer to 1. */
270
271 static void
272 enqueue_node (symtab_node *node)
273 {
274 if (node->aux)
275 return;
276 gcc_checking_assert (first);
277 node->aux = first;
278 first = node;
279 }
280
281 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
282 functions into callgraph in a way so they look like ordinary reachable
283 functions inserted into callgraph already at construction time. */
284
285 bool
286 cgraph_process_new_functions (void)
287 {
288 bool output = false;
289 tree fndecl;
290 struct cgraph_node *node;
291 cgraph_node_set_iterator csi;
292
293 if (!cgraph_new_nodes)
294 return false;
295 handle_alias_pairs ();
296 /* Note that this queue may grow as its being processed, as the new
297 functions may generate new ones. */
298 for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi))
299 {
300 node = csi_node (csi);
301 fndecl = node->decl;
302 switch (cgraph_state)
303 {
304 case CGRAPH_STATE_CONSTRUCTION:
305 /* At construction time we just need to finalize function and move
306 it into reachable functions list. */
307
308 cgraph_finalize_function (fndecl, false);
309 output = true;
310 cgraph_call_function_insertion_hooks (node);
311 enqueue_node (node);
312 break;
313
314 case CGRAPH_STATE_IPA:
315 case CGRAPH_STATE_IPA_SSA:
316 /* When IPA optimization already started, do all essential
317 transformations that has been already performed on the whole
318 cgraph but not on this function. */
319
320 gimple_register_cfg_hooks ();
321 if (!node->analyzed)
322 analyze_function (node);
323 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
324 if (cgraph_state == CGRAPH_STATE_IPA_SSA
325 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
326 g->get_passes ()->execute_early_local_passes ();
327 else if (inline_summary_vec != NULL)
328 compute_inline_parameters (node, true);
329 free_dominance_info (CDI_POST_DOMINATORS);
330 free_dominance_info (CDI_DOMINATORS);
331 pop_cfun ();
332 cgraph_call_function_insertion_hooks (node);
333 break;
334
335 case CGRAPH_STATE_EXPANSION:
336 /* Functions created during expansion shall be compiled
337 directly. */
338 node->process = 0;
339 cgraph_call_function_insertion_hooks (node);
340 expand_function (node);
341 break;
342
343 default:
344 gcc_unreachable ();
345 break;
346 }
347 }
348 free_cgraph_node_set (cgraph_new_nodes);
349 cgraph_new_nodes = NULL;
350 return output;
351 }
352
353 /* As an GCC extension we allow redefinition of the function. The
354 semantics when both copies of bodies differ is not well defined.
355 We replace the old body with new body so in unit at a time mode
356 we always use new body, while in normal mode we may end up with
357 old body inlined into some functions and new body expanded and
358 inlined in others.
359
360 ??? It may make more sense to use one body for inlining and other
361 body for expanding the function but this is difficult to do. */
362
363 void
364 cgraph_reset_node (struct cgraph_node *node)
365 {
366 /* If node->process is set, then we have already begun whole-unit analysis.
367 This is *not* testing for whether we've already emitted the function.
368 That case can be sort-of legitimately seen with real function redefinition
369 errors. I would argue that the front end should never present us with
370 such a case, but don't enforce that for now. */
371 gcc_assert (!node->process);
372
373 /* Reset our data structures so we can analyze the function again. */
374 memset (&node->local, 0, sizeof (node->local));
375 memset (&node->global, 0, sizeof (node->global));
376 memset (&node->rtl, 0, sizeof (node->rtl));
377 node->analyzed = false;
378 node->definition = false;
379 node->alias = false;
380 node->weakref = false;
381 node->cpp_implicit_alias = false;
382
383 cgraph_node_remove_callees (node);
384 ipa_remove_all_references (&node->ref_list);
385 }
386
387 /* Return true when there are references to NODE. */
388
389 static bool
390 referred_to_p (symtab_node *node)
391 {
392 struct ipa_ref *ref;
393
394 /* See if there are any references at all. */
395 if (ipa_ref_list_referring_iterate (&node->ref_list, 0, ref))
396 return true;
397 /* For functions check also calls. */
398 cgraph_node *cn = dyn_cast <cgraph_node> (node);
399 if (cn && cn->callers)
400 return true;
401 return false;
402 }
403
404 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
405 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
406 the garbage collector run at the moment. We would need to either create
407 a new GC context, or just not compile right now. */
408
409 void
410 cgraph_finalize_function (tree decl, bool no_collect)
411 {
412 struct cgraph_node *node = cgraph_get_create_node (decl);
413
414 if (node->definition)
415 {
416 /* Nested functions should only be defined once. */
417 gcc_assert (!DECL_CONTEXT (decl)
418 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
419 cgraph_reset_node (node);
420 node->local.redefined_extern_inline = true;
421 }
422
423 notice_global_symbol (decl);
424 node->definition = true;
425 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
426
427 /* With -fkeep-inline-functions we are keeping all inline functions except
428 for extern inline ones. */
429 if (flag_keep_inline_functions
430 && DECL_DECLARED_INLINE_P (decl)
431 && !DECL_EXTERNAL (decl)
432 && !DECL_DISREGARD_INLINE_LIMITS (decl))
433 node->force_output = 1;
434
435 /* When not optimizing, also output the static functions. (see
436 PR24561), but don't do so for always_inline functions, functions
437 declared inline and nested functions. These were optimized out
438 in the original implementation and it is unclear whether we want
439 to change the behavior here. */
440 if ((!optimize
441 && !node->cpp_implicit_alias
442 && !DECL_DISREGARD_INLINE_LIMITS (decl)
443 && !DECL_DECLARED_INLINE_P (decl)
444 && !(DECL_CONTEXT (decl)
445 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
446 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
447 node->force_output = 1;
448
449 /* If we've not yet emitted decl, tell the debug info about it. */
450 if (!TREE_ASM_WRITTEN (decl))
451 (*debug_hooks->deferred_inline_function) (decl);
452
453 /* Possibly warn about unused parameters. */
454 if (warn_unused_parameter)
455 do_warn_unused_parameter (decl);
456
457 if (!no_collect)
458 ggc_collect ();
459
460 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
461 && (decide_is_symbol_needed (node)
462 || referred_to_p (node)))
463 enqueue_node (node);
464 }
465
466 /* Add the function FNDECL to the call graph.
467 Unlike cgraph_finalize_function, this function is intended to be used
468 by middle end and allows insertion of new function at arbitrary point
469 of compilation. The function can be either in high, low or SSA form
470 GIMPLE.
471
472 The function is assumed to be reachable and have address taken (so no
473 API breaking optimizations are performed on it).
474
475 Main work done by this function is to enqueue the function for later
476 processing to avoid need the passes to be re-entrant. */
477
478 void
479 cgraph_add_new_function (tree fndecl, bool lowered)
480 {
481 gcc::pass_manager *passes = g->get_passes ();
482 struct cgraph_node *node;
483 switch (cgraph_state)
484 {
485 case CGRAPH_STATE_PARSING:
486 cgraph_finalize_function (fndecl, false);
487 break;
488 case CGRAPH_STATE_CONSTRUCTION:
489 /* Just enqueue function to be processed at nearest occurrence. */
490 node = cgraph_create_node (fndecl);
491 if (lowered)
492 node->lowered = true;
493 if (!cgraph_new_nodes)
494 cgraph_new_nodes = cgraph_node_set_new ();
495 cgraph_node_set_add (cgraph_new_nodes, node);
496 break;
497
498 case CGRAPH_STATE_IPA:
499 case CGRAPH_STATE_IPA_SSA:
500 case CGRAPH_STATE_EXPANSION:
501 /* Bring the function into finalized state and enqueue for later
502 analyzing and compilation. */
503 node = cgraph_get_create_node (fndecl);
504 node->local.local = false;
505 node->definition = true;
506 node->force_output = true;
507 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
508 {
509 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
510 gimple_register_cfg_hooks ();
511 bitmap_obstack_initialize (NULL);
512 execute_pass_list (passes->all_lowering_passes);
513 passes->execute_early_local_passes ();
514 bitmap_obstack_release (NULL);
515 pop_cfun ();
516
517 lowered = true;
518 }
519 if (lowered)
520 node->lowered = true;
521 if (!cgraph_new_nodes)
522 cgraph_new_nodes = cgraph_node_set_new ();
523 cgraph_node_set_add (cgraph_new_nodes, node);
524 break;
525
526 case CGRAPH_STATE_FINISHED:
527 /* At the very end of compilation we have to do all the work up
528 to expansion. */
529 node = cgraph_create_node (fndecl);
530 if (lowered)
531 node->lowered = true;
532 node->definition = true;
533 analyze_function (node);
534 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
535 gimple_register_cfg_hooks ();
536 bitmap_obstack_initialize (NULL);
537 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
538 g->get_passes ()->execute_early_local_passes ();
539 bitmap_obstack_release (NULL);
540 pop_cfun ();
541 expand_function (node);
542 break;
543
544 default:
545 gcc_unreachable ();
546 }
547
548 /* Set a personality if required and we already passed EH lowering. */
549 if (lowered
550 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
551 == eh_personality_lang))
552 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
553 }
554
555 /* Add a top-level asm statement to the list. */
556
557 struct asm_node *
558 add_asm_node (tree asm_str)
559 {
560 struct asm_node *node;
561
562 node = ggc_alloc_cleared_asm_node ();
563 node->asm_str = asm_str;
564 node->order = symtab_order++;
565 node->next = NULL;
566 if (asm_nodes == NULL)
567 asm_nodes = node;
568 else
569 asm_last_node->next = node;
570 asm_last_node = node;
571 return node;
572 }
573
574 /* Output all asm statements we have stored up to be output. */
575
576 static void
577 output_asm_statements (void)
578 {
579 struct asm_node *can;
580
581 if (seen_error ())
582 return;
583
584 for (can = asm_nodes; can; can = can->next)
585 assemble_asm (can->asm_str);
586 asm_nodes = NULL;
587 }
588
589 /* Analyze the function scheduled to be output. */
590 static void
591 analyze_function (struct cgraph_node *node)
592 {
593 tree decl = node->decl;
594 location_t saved_loc = input_location;
595 input_location = DECL_SOURCE_LOCATION (decl);
596
597 if (node->thunk.thunk_p)
598 {
599 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
600 NULL, 0, CGRAPH_FREQ_BASE);
601 if (!expand_thunk (node, false))
602 {
603 node->thunk.alias = NULL;
604 node->analyzed = true;
605 return;
606 }
607 node->thunk.alias = NULL;
608 }
609 if (node->alias)
610 symtab_resolve_alias
611 (node, cgraph_get_node (node->alias_target));
612 else if (node->dispatcher_function)
613 {
614 /* Generate the dispatcher body of multi-versioned functions. */
615 struct cgraph_function_version_info *dispatcher_version_info
616 = get_cgraph_node_version (node);
617 if (dispatcher_version_info != NULL
618 && (dispatcher_version_info->dispatcher_resolver
619 == NULL_TREE))
620 {
621 tree resolver = NULL_TREE;
622 gcc_assert (targetm.generate_version_dispatcher_body);
623 resolver = targetm.generate_version_dispatcher_body (node);
624 gcc_assert (resolver != NULL_TREE);
625 }
626 }
627 else
628 {
629 push_cfun (DECL_STRUCT_FUNCTION (decl));
630
631 assign_assembler_name_if_neeeded (node->decl);
632
633 /* Make sure to gimplify bodies only once. During analyzing a
634 function we lower it, which will require gimplified nested
635 functions, so we can end up here with an already gimplified
636 body. */
637 if (!gimple_has_body_p (decl))
638 gimplify_function_tree (decl);
639 dump_function (TDI_generic, decl);
640
641 /* Lower the function. */
642 if (!node->lowered)
643 {
644 if (node->nested)
645 lower_nested_functions (node->decl);
646 gcc_assert (!node->nested);
647
648 gimple_register_cfg_hooks ();
649 bitmap_obstack_initialize (NULL);
650 execute_pass_list (g->get_passes ()->all_lowering_passes);
651 free_dominance_info (CDI_POST_DOMINATORS);
652 free_dominance_info (CDI_DOMINATORS);
653 compact_blocks ();
654 bitmap_obstack_release (NULL);
655 node->lowered = true;
656 }
657
658 pop_cfun ();
659 }
660 node->analyzed = true;
661
662 input_location = saved_loc;
663 }
664
665 /* C++ frontend produce same body aliases all over the place, even before PCH
666 gets streamed out. It relies on us linking the aliases with their function
667 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
668 first produce aliases without links, but once C++ FE is sure he won't sream
669 PCH we build the links via this function. */
670
671 void
672 cgraph_process_same_body_aliases (void)
673 {
674 symtab_node *node;
675 FOR_EACH_SYMBOL (node)
676 if (node->cpp_implicit_alias && !node->analyzed)
677 symtab_resolve_alias
678 (node,
679 TREE_CODE (node->alias_target) == VAR_DECL
680 ? (symtab_node *)varpool_node_for_decl (node->alias_target)
681 : (symtab_node *)cgraph_get_create_node (node->alias_target));
682 cpp_implicit_aliases_done = true;
683 }
684
685 /* Process attributes common for vars and functions. */
686
687 static void
688 process_common_attributes (tree decl)
689 {
690 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
691
692 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
693 {
694 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
695 "%<weakref%> attribute should be accompanied with"
696 " an %<alias%> attribute");
697 DECL_WEAK (decl) = 0;
698 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
699 DECL_ATTRIBUTES (decl));
700 }
701 }
702
703 /* Look for externally_visible and used attributes and mark cgraph nodes
704 accordingly.
705
706 We cannot mark the nodes at the point the attributes are processed (in
707 handle_*_attribute) because the copy of the declarations available at that
708 point may not be canonical. For example, in:
709
710 void f();
711 void f() __attribute__((used));
712
713 the declaration we see in handle_used_attribute will be the second
714 declaration -- but the front end will subsequently merge that declaration
715 with the original declaration and discard the second declaration.
716
717 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
718
719 void f() {}
720 void f() __attribute__((externally_visible));
721
722 is valid.
723
724 So, we walk the nodes at the end of the translation unit, applying the
725 attributes at that point. */
726
727 static void
728 process_function_and_variable_attributes (struct cgraph_node *first,
729 struct varpool_node *first_var)
730 {
731 struct cgraph_node *node;
732 struct varpool_node *vnode;
733
734 for (node = cgraph_first_function (); node != first;
735 node = cgraph_next_function (node))
736 {
737 tree decl = node->decl;
738 if (DECL_PRESERVE_P (decl))
739 cgraph_mark_force_output_node (node);
740 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
741 {
742 if (! TREE_PUBLIC (node->decl))
743 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
744 "%<externally_visible%>"
745 " attribute have effect only on public objects");
746 }
747 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
748 && (node->definition && !node->alias))
749 {
750 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
751 "%<weakref%> attribute ignored"
752 " because function is defined");
753 DECL_WEAK (decl) = 0;
754 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
755 DECL_ATTRIBUTES (decl));
756 }
757
758 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
759 && !DECL_DECLARED_INLINE_P (decl)
760 /* redefining extern inline function makes it DECL_UNINLINABLE. */
761 && !DECL_UNINLINABLE (decl))
762 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
763 "always_inline function might not be inlinable");
764
765 process_common_attributes (decl);
766 }
767 for (vnode = varpool_first_variable (); vnode != first_var;
768 vnode = varpool_next_variable (vnode))
769 {
770 tree decl = vnode->decl;
771 if (DECL_EXTERNAL (decl)
772 && DECL_INITIAL (decl))
773 varpool_finalize_decl (decl);
774 if (DECL_PRESERVE_P (decl))
775 vnode->force_output = true;
776 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
777 {
778 if (! TREE_PUBLIC (vnode->decl))
779 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
780 "%<externally_visible%>"
781 " attribute have effect only on public objects");
782 }
783 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
784 && vnode->definition
785 && DECL_INITIAL (decl))
786 {
787 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
788 "%<weakref%> attribute ignored"
789 " because variable is initialized");
790 DECL_WEAK (decl) = 0;
791 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
792 DECL_ATTRIBUTES (decl));
793 }
794 process_common_attributes (decl);
795 }
796 }
797
798 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
799 middle end to output the variable to asm file, if needed or externally
800 visible. */
801
802 void
803 varpool_finalize_decl (tree decl)
804 {
805 struct varpool_node *node = varpool_node_for_decl (decl);
806
807 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
808
809 if (node->definition)
810 return;
811 notice_global_symbol (decl);
812 node->definition = true;
813 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
814 /* Traditionally we do not eliminate static variables when not
815 optimizing and when not doing toplevel reoder. */
816 || (!flag_toplevel_reorder && !DECL_COMDAT (node->decl)
817 && !DECL_ARTIFICIAL (node->decl)))
818 node->force_output = true;
819
820 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
821 && (decide_is_symbol_needed (node)
822 || referred_to_p (node)))
823 enqueue_node (node);
824 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
825 varpool_analyze_node (node);
826 /* Some frontends produce various interface variables after compilation
827 finished. */
828 if (cgraph_state == CGRAPH_STATE_FINISHED)
829 varpool_assemble_decl (node);
830 }
831
832 /* EDGE is an polymorphic call. Mark all possible targets as reachable
833 and if there is only one target, perform trivial devirtualization.
834 REACHABLE_CALL_TARGETS collects target lists we already walked to
835 avoid udplicate work. */
836
837 static void
838 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
839 struct cgraph_edge *edge)
840 {
841 unsigned int i;
842 void *cache_token;
843 bool final;
844 vec <cgraph_node *>targets
845 = possible_polymorphic_call_targets
846 (edge, &final, &cache_token);
847
848 if (!pointer_set_insert (reachable_call_targets,
849 cache_token))
850 {
851 if (cgraph_dump_file)
852 dump_possible_polymorphic_call_targets
853 (cgraph_dump_file, edge);
854
855 for (i = 0; i < targets.length (); i++)
856 {
857 /* Do not bother to mark virtual methods in anonymous namespace;
858 either we will find use of virtual table defining it, or it is
859 unused. */
860 if (targets[i]->definition
861 && TREE_CODE
862 (TREE_TYPE (targets[i]->decl))
863 == METHOD_TYPE
864 && !type_in_anonymous_namespace_p
865 (method_class_type
866 (TREE_TYPE (targets[i]->decl))))
867 enqueue_node (targets[i]);
868 }
869 }
870
871 /* Very trivial devirtualization; when the type is
872 final or anonymous (so we know all its derivation)
873 and there is only one possible virtual call target,
874 make the edge direct. */
875 if (final)
876 {
877 if (targets.length () <= 1)
878 {
879 cgraph_node *target;
880 if (targets.length () == 1)
881 target = targets[0];
882 else
883 target = cgraph_get_create_node
884 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
885
886 if (cgraph_dump_file)
887 {
888 fprintf (cgraph_dump_file,
889 "Devirtualizing call: ");
890 print_gimple_stmt (cgraph_dump_file,
891 edge->call_stmt, 0,
892 TDF_SLIM);
893 }
894 cgraph_make_edge_direct (edge, target);
895 cgraph_redirect_edge_call_stmt_to_callee (edge);
896 if (cgraph_dump_file)
897 {
898 fprintf (cgraph_dump_file,
899 "Devirtualized as: ");
900 print_gimple_stmt (cgraph_dump_file,
901 edge->call_stmt, 0,
902 TDF_SLIM);
903 }
904 }
905 }
906 }
907
908
909 /* Discover all functions and variables that are trivially needed, analyze
910 them as well as all functions and variables referred by them */
911
912 static void
913 analyze_functions (void)
914 {
915 /* Keep track of already processed nodes when called multiple times for
916 intermodule optimization. */
917 static struct cgraph_node *first_analyzed;
918 struct cgraph_node *first_handled = first_analyzed;
919 static struct varpool_node *first_analyzed_var;
920 struct varpool_node *first_handled_var = first_analyzed_var;
921 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
922
923 symtab_node *node;
924 symtab_node *next;
925 int i;
926 struct ipa_ref *ref;
927 bool changed = true;
928 location_t saved_loc = input_location;
929
930 bitmap_obstack_initialize (NULL);
931 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
932 input_location = UNKNOWN_LOCATION;
933
934 /* Ugly, but the fixup can not happen at a time same body alias is created;
935 C++ FE is confused about the COMDAT groups being right. */
936 if (cpp_implicit_aliases_done)
937 FOR_EACH_SYMBOL (node)
938 if (node->cpp_implicit_alias)
939 fixup_same_cpp_alias_visibility (node, symtab_alias_target (node));
940 if (optimize && flag_devirtualize)
941 build_type_inheritance_graph ();
942
943 /* Analysis adds static variables that in turn adds references to new functions.
944 So we need to iterate the process until it stabilize. */
945 while (changed)
946 {
947 changed = false;
948 process_function_and_variable_attributes (first_analyzed,
949 first_analyzed_var);
950
951 /* First identify the trivially needed symbols. */
952 for (node = symtab_nodes;
953 node != first_analyzed
954 && node != first_analyzed_var; node = node->next)
955 {
956 if (decide_is_symbol_needed (node))
957 {
958 enqueue_node (node);
959 if (!changed && cgraph_dump_file)
960 fprintf (cgraph_dump_file, "Trivially needed symbols:");
961 changed = true;
962 if (cgraph_dump_file)
963 fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node));
964 if (!changed && cgraph_dump_file)
965 fprintf (cgraph_dump_file, "\n");
966 }
967 if (node == first_analyzed
968 || node == first_analyzed_var)
969 break;
970 }
971 cgraph_process_new_functions ();
972 first_analyzed_var = varpool_first_variable ();
973 first_analyzed = cgraph_first_function ();
974
975 if (changed && dump_file)
976 fprintf (cgraph_dump_file, "\n");
977
978 /* Lower representation, build callgraph edges and references for all trivially
979 needed symbols and all symbols referred by them. */
980 while (first != (symtab_node *)(void *)1)
981 {
982 changed = true;
983 node = first;
984 first = (symtab_node *)first->aux;
985 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
986 if (cnode && cnode->definition)
987 {
988 struct cgraph_edge *edge;
989 tree decl = cnode->decl;
990
991 /* ??? It is possible to create extern inline function
992 and later using weak alias attribute to kill its body.
993 See gcc.c-torture/compile/20011119-1.c */
994 if (!DECL_STRUCT_FUNCTION (decl)
995 && !cnode->alias
996 && !cnode->thunk.thunk_p
997 && !cnode->dispatcher_function)
998 {
999 cgraph_reset_node (cnode);
1000 cnode->local.redefined_extern_inline = true;
1001 continue;
1002 }
1003
1004 if (!cnode->analyzed)
1005 analyze_function (cnode);
1006
1007 for (edge = cnode->callees; edge; edge = edge->next_callee)
1008 if (edge->callee->definition)
1009 enqueue_node (edge->callee);
1010 if (optimize && flag_devirtualize)
1011 {
1012 struct cgraph_edge *next;
1013
1014 for (edge = cnode->indirect_calls; edge; edge = next)
1015 {
1016 next = edge->next_callee;
1017 if (edge->indirect_info->polymorphic)
1018 walk_polymorphic_call_targets (reachable_call_targets,
1019 edge);
1020 }
1021 }
1022
1023 /* If decl is a clone of an abstract function,
1024 mark that abstract function so that we don't release its body.
1025 The DECL_INITIAL() of that abstract function declaration
1026 will be later needed to output debug info. */
1027 if (DECL_ABSTRACT_ORIGIN (decl))
1028 {
1029 struct cgraph_node *origin_node
1030 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
1031 origin_node->used_as_abstract_origin = true;
1032 }
1033 }
1034 else
1035 {
1036 varpool_node *vnode = dyn_cast <varpool_node> (node);
1037 if (vnode && vnode->definition && !vnode->analyzed)
1038 varpool_analyze_node (vnode);
1039 }
1040
1041 if (node->same_comdat_group)
1042 {
1043 symtab_node *next;
1044 for (next = node->same_comdat_group;
1045 next != node;
1046 next = next->same_comdat_group)
1047 enqueue_node (next);
1048 }
1049 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
1050 if (ref->referred->definition)
1051 enqueue_node (ref->referred);
1052 cgraph_process_new_functions ();
1053 }
1054 }
1055 if (optimize && flag_devirtualize)
1056 update_type_inheritance_graph ();
1057
1058 /* Collect entry points to the unit. */
1059 if (cgraph_dump_file)
1060 {
1061 fprintf (cgraph_dump_file, "\n\nInitial ");
1062 dump_symtab (cgraph_dump_file);
1063 }
1064
1065 if (cgraph_dump_file)
1066 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
1067
1068 for (node = symtab_nodes;
1069 node != first_handled
1070 && node != first_handled_var; node = next)
1071 {
1072 next = node->next;
1073 if (!node->aux && !referred_to_p (node))
1074 {
1075 if (cgraph_dump_file)
1076 fprintf (cgraph_dump_file, " %s", symtab_node_name (node));
1077 symtab_remove_node (node);
1078 continue;
1079 }
1080 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
1081 {
1082 tree decl = node->decl;
1083
1084 if (cnode->definition && !gimple_has_body_p (decl)
1085 && !cnode->alias
1086 && !cnode->thunk.thunk_p)
1087 cgraph_reset_node (cnode);
1088
1089 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1090 || cnode->alias
1091 || gimple_has_body_p (decl));
1092 gcc_assert (cnode->analyzed == cnode->definition);
1093 }
1094 node->aux = NULL;
1095 }
1096 for (;node; node = node->next)
1097 node->aux = NULL;
1098 first_analyzed = cgraph_first_function ();
1099 first_analyzed_var = varpool_first_variable ();
1100 if (cgraph_dump_file)
1101 {
1102 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1103 dump_symtab (cgraph_dump_file);
1104 }
1105 bitmap_obstack_release (NULL);
1106 pointer_set_destroy (reachable_call_targets);
1107 ggc_collect ();
1108 /* Initialize assembler name hash, in particular we want to trigger C++
1109 mangling and same body alias creation before we free DECL_ARGUMENTS
1110 used by it. */
1111 if (!seen_error ())
1112 symtab_initialize_asm_name_hash ();
1113
1114 input_location = saved_loc;
1115 }
1116
1117 /* Translate the ugly representation of aliases as alias pairs into nice
1118 representation in callgraph. We don't handle all cases yet,
1119 unfortunately. */
1120
1121 static void
1122 handle_alias_pairs (void)
1123 {
1124 alias_pair *p;
1125 unsigned i;
1126
1127 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1128 {
1129 symtab_node *target_node = symtab_node_for_asm (p->target);
1130
1131 /* Weakrefs with target not defined in current unit are easy to handle:
1132 they behave just as external variables except we need to note the
1133 alias flag to later output the weakref pseudo op into asm file. */
1134 if (!target_node
1135 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1136 {
1137 symtab_node *node = symtab_get_node (p->decl);
1138 if (node)
1139 {
1140 node->alias_target = p->target;
1141 node->weakref = true;
1142 node->alias = true;
1143 }
1144 alias_pairs->unordered_remove (i);
1145 continue;
1146 }
1147 else if (!target_node)
1148 {
1149 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1150 symtab_node *node = symtab_get_node (p->decl);
1151 if (node)
1152 node->alias = false;
1153 alias_pairs->unordered_remove (i);
1154 continue;
1155 }
1156
1157 if (DECL_EXTERNAL (target_node->decl)
1158 /* We use local aliases for C++ thunks to force the tailcall
1159 to bind locally. This is a hack - to keep it working do
1160 the following (which is not strictly correct). */
1161 && (! TREE_CODE (target_node->decl) == FUNCTION_DECL
1162 || ! DECL_VIRTUAL_P (target_node->decl))
1163 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1164 {
1165 error ("%q+D aliased to external symbol %qE",
1166 p->decl, p->target);
1167 }
1168
1169 if (TREE_CODE (p->decl) == FUNCTION_DECL
1170 && target_node && is_a <cgraph_node> (target_node))
1171 {
1172 struct cgraph_node *src_node = cgraph_get_node (p->decl);
1173 if (src_node && src_node->definition)
1174 cgraph_reset_node (src_node);
1175 cgraph_create_function_alias (p->decl, target_node->decl);
1176 alias_pairs->unordered_remove (i);
1177 }
1178 else if (TREE_CODE (p->decl) == VAR_DECL
1179 && target_node && is_a <varpool_node> (target_node))
1180 {
1181 varpool_create_variable_alias (p->decl, target_node->decl);
1182 alias_pairs->unordered_remove (i);
1183 }
1184 else
1185 {
1186 error ("%q+D alias in between function and variable is not supported",
1187 p->decl);
1188 warning (0, "%q+D aliased declaration",
1189 target_node->decl);
1190 alias_pairs->unordered_remove (i);
1191 }
1192 }
1193 vec_free (alias_pairs);
1194 }
1195
1196
1197 /* Figure out what functions we want to assemble. */
1198
1199 static void
1200 mark_functions_to_output (void)
1201 {
1202 struct cgraph_node *node;
1203 #ifdef ENABLE_CHECKING
1204 bool check_same_comdat_groups = false;
1205
1206 FOR_EACH_FUNCTION (node)
1207 gcc_assert (!node->process);
1208 #endif
1209
1210 FOR_EACH_FUNCTION (node)
1211 {
1212 tree decl = node->decl;
1213
1214 gcc_assert (!node->process || node->same_comdat_group);
1215 if (node->process)
1216 continue;
1217
1218 /* We need to output all local functions that are used and not
1219 always inlined, as well as those that are reachable from
1220 outside the current compilation unit. */
1221 if (node->analyzed
1222 && !node->thunk.thunk_p
1223 && !node->alias
1224 && !node->global.inlined_to
1225 && !TREE_ASM_WRITTEN (decl)
1226 && !DECL_EXTERNAL (decl))
1227 {
1228 node->process = 1;
1229 if (node->same_comdat_group)
1230 {
1231 struct cgraph_node *next;
1232 for (next = cgraph (node->same_comdat_group);
1233 next != node;
1234 next = cgraph (next->same_comdat_group))
1235 if (!next->thunk.thunk_p && !next->alias)
1236 next->process = 1;
1237 }
1238 }
1239 else if (node->same_comdat_group)
1240 {
1241 #ifdef ENABLE_CHECKING
1242 check_same_comdat_groups = true;
1243 #endif
1244 }
1245 else
1246 {
1247 /* We should've reclaimed all functions that are not needed. */
1248 #ifdef ENABLE_CHECKING
1249 if (!node->global.inlined_to
1250 && gimple_has_body_p (decl)
1251 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1252 are inside partition, we can end up not removing the body since we no longer
1253 have analyzed node pointing to it. */
1254 && !node->in_other_partition
1255 && !node->alias
1256 && !node->clones
1257 && !DECL_EXTERNAL (decl))
1258 {
1259 dump_cgraph_node (stderr, node);
1260 internal_error ("failed to reclaim unneeded function");
1261 }
1262 #endif
1263 gcc_assert (node->global.inlined_to
1264 || !gimple_has_body_p (decl)
1265 || node->in_other_partition
1266 || node->clones
1267 || DECL_ARTIFICIAL (decl)
1268 || DECL_EXTERNAL (decl));
1269
1270 }
1271
1272 }
1273 #ifdef ENABLE_CHECKING
1274 if (check_same_comdat_groups)
1275 FOR_EACH_FUNCTION (node)
1276 if (node->same_comdat_group && !node->process)
1277 {
1278 tree decl = node->decl;
1279 if (!node->global.inlined_to
1280 && gimple_has_body_p (decl)
1281 /* FIXME: in an ltrans unit when the offline copy is outside a
1282 partition but inline copies are inside a partition, we can
1283 end up not removing the body since we no longer have an
1284 analyzed node pointing to it. */
1285 && !node->in_other_partition
1286 && !node->clones
1287 && !DECL_EXTERNAL (decl))
1288 {
1289 dump_cgraph_node (stderr, node);
1290 internal_error ("failed to reclaim unneeded function in same "
1291 "comdat group");
1292 }
1293 }
1294 #endif
1295 }
1296
1297 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1298 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1299
1300 Set current_function_decl and cfun to newly constructed empty function body.
1301 return basic block in the function body. */
1302
1303 basic_block
1304 init_lowered_empty_function (tree decl, bool in_ssa)
1305 {
1306 basic_block bb;
1307
1308 current_function_decl = decl;
1309 allocate_struct_function (decl, false);
1310 gimple_register_cfg_hooks ();
1311 init_empty_tree_cfg ();
1312
1313 if (in_ssa)
1314 {
1315 init_tree_ssa (cfun);
1316 init_ssa_operands (cfun);
1317 cfun->gimple_df->in_ssa_p = true;
1318 cfun->curr_properties |= PROP_ssa;
1319 }
1320
1321 DECL_INITIAL (decl) = make_node (BLOCK);
1322
1323 DECL_SAVED_TREE (decl) = error_mark_node;
1324 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1325 | PROP_cfg | PROP_loops);
1326
1327 set_loops_for_fn (cfun, ggc_alloc_cleared_loops ());
1328 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1329 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1330
1331 /* Create BB for body of the function and connect it properly. */
1332 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
1333 make_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU);
1334 make_edge (bb, EXIT_BLOCK_PTR, 0);
1335 add_bb_to_loop (bb, ENTRY_BLOCK_PTR->loop_father);
1336
1337 return bb;
1338 }
1339
1340 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1341 offset indicated by VIRTUAL_OFFSET, if that is
1342 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1343 zero for a result adjusting thunk. */
1344
1345 static tree
1346 thunk_adjust (gimple_stmt_iterator * bsi,
1347 tree ptr, bool this_adjusting,
1348 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1349 {
1350 gimple stmt;
1351 tree ret;
1352
1353 if (this_adjusting
1354 && fixed_offset != 0)
1355 {
1356 stmt = gimple_build_assign
1357 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1358 ptr,
1359 fixed_offset));
1360 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1361 }
1362
1363 /* If there's a virtual offset, look up that value in the vtable and
1364 adjust the pointer again. */
1365 if (virtual_offset)
1366 {
1367 tree vtabletmp;
1368 tree vtabletmp2;
1369 tree vtabletmp3;
1370
1371 if (!vtable_entry_type)
1372 {
1373 tree vfunc_type = make_node (FUNCTION_TYPE);
1374 TREE_TYPE (vfunc_type) = integer_type_node;
1375 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1376 layout_type (vfunc_type);
1377
1378 vtable_entry_type = build_pointer_type (vfunc_type);
1379 }
1380
1381 vtabletmp =
1382 create_tmp_reg (build_pointer_type
1383 (build_pointer_type (vtable_entry_type)), "vptr");
1384
1385 /* The vptr is always at offset zero in the object. */
1386 stmt = gimple_build_assign (vtabletmp,
1387 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1388 ptr));
1389 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1390
1391 /* Form the vtable address. */
1392 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1393 "vtableaddr");
1394 stmt = gimple_build_assign (vtabletmp2,
1395 build_simple_mem_ref (vtabletmp));
1396 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1397
1398 /* Find the entry with the vcall offset. */
1399 stmt = gimple_build_assign (vtabletmp2,
1400 fold_build_pointer_plus_loc (input_location,
1401 vtabletmp2,
1402 virtual_offset));
1403 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1404
1405 /* Get the offset itself. */
1406 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1407 "vcalloffset");
1408 stmt = gimple_build_assign (vtabletmp3,
1409 build_simple_mem_ref (vtabletmp2));
1410 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1411
1412 /* Adjust the `this' pointer. */
1413 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1414 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1415 GSI_CONTINUE_LINKING);
1416 }
1417
1418 if (!this_adjusting
1419 && fixed_offset != 0)
1420 /* Adjust the pointer by the constant. */
1421 {
1422 tree ptrtmp;
1423
1424 if (TREE_CODE (ptr) == VAR_DECL)
1425 ptrtmp = ptr;
1426 else
1427 {
1428 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1429 stmt = gimple_build_assign (ptrtmp, ptr);
1430 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1431 }
1432 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1433 ptrtmp, fixed_offset);
1434 }
1435
1436 /* Emit the statement and gimplify the adjustment expression. */
1437 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1438 stmt = gimple_build_assign (ret, ptr);
1439 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1440
1441 return ret;
1442 }
1443
1444 /* Expand thunk NODE to gimple if possible.
1445 When OUTPUT_ASM_THUNK is true, also produce assembler for
1446 thunks that are not lowered. */
1447
1448 bool
1449 expand_thunk (struct cgraph_node *node, bool output_asm_thunks)
1450 {
1451 bool this_adjusting = node->thunk.this_adjusting;
1452 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1453 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1454 tree virtual_offset = NULL;
1455 tree alias = node->callees->callee->decl;
1456 tree thunk_fndecl = node->decl;
1457 tree a;
1458
1459
1460 if (this_adjusting
1461 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1462 virtual_value, alias))
1463 {
1464 const char *fnname;
1465 tree fn_block;
1466 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1467
1468 if (!output_asm_thunks)
1469 return false;
1470
1471 if (in_lto_p)
1472 cgraph_get_body (node);
1473 a = DECL_ARGUMENTS (thunk_fndecl);
1474
1475 current_function_decl = thunk_fndecl;
1476
1477 /* Ensure thunks are emitted in their correct sections. */
1478 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1479
1480 DECL_RESULT (thunk_fndecl)
1481 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1482 RESULT_DECL, 0, restype);
1483 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1484 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1485
1486 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1487 create one. */
1488 fn_block = make_node (BLOCK);
1489 BLOCK_VARS (fn_block) = a;
1490 DECL_INITIAL (thunk_fndecl) = fn_block;
1491 init_function_start (thunk_fndecl);
1492 cfun->is_thunk = 1;
1493 insn_locations_init ();
1494 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1495 prologue_location = curr_insn_location ();
1496 assemble_start_function (thunk_fndecl, fnname);
1497
1498 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1499 fixed_offset, virtual_value, alias);
1500
1501 assemble_end_function (thunk_fndecl, fnname);
1502 insn_locations_finalize ();
1503 init_insn_lengths ();
1504 free_after_compilation (cfun);
1505 set_cfun (NULL);
1506 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1507 node->thunk.thunk_p = false;
1508 node->analyzed = false;
1509 }
1510 else
1511 {
1512 tree restype;
1513 basic_block bb, then_bb, else_bb, return_bb;
1514 gimple_stmt_iterator bsi;
1515 int nargs = 0;
1516 tree arg;
1517 int i;
1518 tree resdecl;
1519 tree restmp = NULL;
1520 vec<tree> vargs;
1521
1522 gimple call;
1523 gimple ret;
1524
1525 if (in_lto_p)
1526 cgraph_get_body (node);
1527 a = DECL_ARGUMENTS (thunk_fndecl);
1528
1529 current_function_decl = thunk_fndecl;
1530
1531 /* Ensure thunks are emitted in their correct sections. */
1532 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1533
1534 DECL_IGNORED_P (thunk_fndecl) = 1;
1535 bitmap_obstack_initialize (NULL);
1536
1537 if (node->thunk.virtual_offset_p)
1538 virtual_offset = size_int (virtual_value);
1539
1540 /* Build the return declaration for the function. */
1541 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1542 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1543 {
1544 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1545 DECL_ARTIFICIAL (resdecl) = 1;
1546 DECL_IGNORED_P (resdecl) = 1;
1547 DECL_RESULT (thunk_fndecl) = resdecl;
1548 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1549 }
1550 else
1551 resdecl = DECL_RESULT (thunk_fndecl);
1552
1553 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
1554
1555 bsi = gsi_start_bb (bb);
1556
1557 /* Build call to the function being thunked. */
1558 if (!VOID_TYPE_P (restype))
1559 {
1560 if (DECL_BY_REFERENCE (resdecl))
1561 restmp = gimple_fold_indirect_ref (resdecl);
1562 else if (!is_gimple_reg_type (restype))
1563 {
1564 restmp = resdecl;
1565 add_local_decl (cfun, restmp);
1566 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1567 }
1568 else
1569 restmp = create_tmp_reg (restype, "retval");
1570 }
1571
1572 for (arg = a; arg; arg = DECL_CHAIN (arg))
1573 nargs++;
1574 vargs.create (nargs);
1575 if (this_adjusting)
1576 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1577 virtual_offset));
1578 else if (nargs)
1579 vargs.quick_push (a);
1580
1581 if (nargs)
1582 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1583 vargs.quick_push (arg);
1584 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1585 node->callees->call_stmt = call;
1586 vargs.release ();
1587 gimple_call_set_from_thunk (call, true);
1588 if (restmp)
1589 {
1590 gimple_call_set_lhs (call, restmp);
1591 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1592 TREE_TYPE (TREE_TYPE (alias))));
1593 }
1594 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1595 if (!(gimple_call_flags (call) & ECF_NORETURN))
1596 {
1597 if (restmp && !this_adjusting
1598 && (fixed_offset || virtual_offset))
1599 {
1600 tree true_label = NULL_TREE;
1601
1602 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1603 {
1604 gimple stmt;
1605 /* If the return type is a pointer, we need to
1606 protect against NULL. We know there will be an
1607 adjustment, because that's why we're emitting a
1608 thunk. */
1609 then_bb = create_basic_block (NULL, (void *) 0, bb);
1610 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1611 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1612 add_bb_to_loop (then_bb, bb->loop_father);
1613 add_bb_to_loop (return_bb, bb->loop_father);
1614 add_bb_to_loop (else_bb, bb->loop_father);
1615 remove_edge (single_succ_edge (bb));
1616 true_label = gimple_block_label (then_bb);
1617 stmt = gimple_build_cond (NE_EXPR, restmp,
1618 build_zero_cst (TREE_TYPE (restmp)),
1619 NULL_TREE, NULL_TREE);
1620 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1621 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1622 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1623 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1624 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1625 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1626 bsi = gsi_last_bb (then_bb);
1627 }
1628
1629 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1630 fixed_offset, virtual_offset);
1631 if (true_label)
1632 {
1633 gimple stmt;
1634 bsi = gsi_last_bb (else_bb);
1635 stmt = gimple_build_assign (restmp,
1636 build_zero_cst (TREE_TYPE (restmp)));
1637 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1638 bsi = gsi_last_bb (return_bb);
1639 }
1640 }
1641 else
1642 gimple_call_set_tail (call, true);
1643
1644 /* Build return value. */
1645 ret = gimple_build_return (restmp);
1646 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1647 }
1648 else
1649 {
1650 gimple_call_set_tail (call, true);
1651 remove_edge (single_succ_edge (bb));
1652 }
1653
1654 cfun->gimple_df->in_ssa_p = true;
1655 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1656 TREE_ASM_WRITTEN (thunk_fndecl) = false;
1657 delete_unreachable_blocks ();
1658 update_ssa (TODO_update_ssa);
1659 #ifdef ENABLE_CHECKING
1660 verify_flow_info ();
1661 #endif
1662
1663 /* Since we want to emit the thunk, we explicitly mark its name as
1664 referenced. */
1665 node->thunk.thunk_p = false;
1666 node->lowered = true;
1667 bitmap_obstack_release (NULL);
1668 }
1669 current_function_decl = NULL;
1670 set_cfun (NULL);
1671 return true;
1672 }
1673
1674 /* Assemble thunks and aliases associated to NODE. */
1675
1676 static void
1677 assemble_thunks_and_aliases (struct cgraph_node *node)
1678 {
1679 struct cgraph_edge *e;
1680 int i;
1681 struct ipa_ref *ref;
1682
1683 for (e = node->callers; e;)
1684 if (e->caller->thunk.thunk_p)
1685 {
1686 struct cgraph_node *thunk = e->caller;
1687
1688 e = e->next_caller;
1689 assemble_thunks_and_aliases (thunk);
1690 expand_thunk (thunk, true);
1691 }
1692 else
1693 e = e->next_caller;
1694 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
1695 i, ref); i++)
1696 if (ref->use == IPA_REF_ALIAS)
1697 {
1698 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1699 bool saved_written = TREE_ASM_WRITTEN (node->decl);
1700
1701 /* Force assemble_alias to really output the alias this time instead
1702 of buffering it in same alias pairs. */
1703 TREE_ASM_WRITTEN (node->decl) = 1;
1704 do_assemble_alias (alias->decl,
1705 DECL_ASSEMBLER_NAME (node->decl));
1706 assemble_thunks_and_aliases (alias);
1707 TREE_ASM_WRITTEN (node->decl) = saved_written;
1708 }
1709 }
1710
1711 /* Expand function specified by NODE. */
1712
1713 static void
1714 expand_function (struct cgraph_node *node)
1715 {
1716 tree decl = node->decl;
1717 location_t saved_loc;
1718
1719 /* We ought to not compile any inline clones. */
1720 gcc_assert (!node->global.inlined_to);
1721
1722 announce_function (decl);
1723 node->process = 0;
1724 gcc_assert (node->lowered);
1725 cgraph_get_body (node);
1726
1727 /* Generate RTL for the body of DECL. */
1728
1729 timevar_push (TV_REST_OF_COMPILATION);
1730
1731 gcc_assert (cgraph_global_info_ready);
1732
1733 /* Initialize the default bitmap obstack. */
1734 bitmap_obstack_initialize (NULL);
1735
1736 /* Initialize the RTL code for the function. */
1737 current_function_decl = decl;
1738 saved_loc = input_location;
1739 input_location = DECL_SOURCE_LOCATION (decl);
1740 init_function_start (decl);
1741
1742 gimple_register_cfg_hooks ();
1743
1744 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1745
1746 execute_all_ipa_transforms ();
1747
1748 /* Perform all tree transforms and optimizations. */
1749
1750 /* Signal the start of passes. */
1751 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1752
1753 execute_pass_list (g->get_passes ()->all_passes);
1754
1755 /* Signal the end of passes. */
1756 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1757
1758 bitmap_obstack_release (&reg_obstack);
1759
1760 /* Release the default bitmap obstack. */
1761 bitmap_obstack_release (NULL);
1762
1763 /* If requested, warn about function definitions where the function will
1764 return a value (usually of some struct or union type) which itself will
1765 take up a lot of stack space. */
1766 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
1767 {
1768 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
1769
1770 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1771 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1772 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1773 larger_than_size))
1774 {
1775 unsigned int size_as_int
1776 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1777
1778 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1779 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1780 decl, size_as_int);
1781 else
1782 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1783 decl, larger_than_size);
1784 }
1785 }
1786
1787 gimple_set_body (decl, NULL);
1788 if (DECL_STRUCT_FUNCTION (decl) == 0
1789 && !cgraph_get_node (decl)->origin)
1790 {
1791 /* Stop pointing to the local nodes about to be freed.
1792 But DECL_INITIAL must remain nonzero so we know this
1793 was an actual function definition.
1794 For a nested function, this is done in c_pop_function_context.
1795 If rest_of_compilation set this to 0, leave it 0. */
1796 if (DECL_INITIAL (decl) != 0)
1797 DECL_INITIAL (decl) = error_mark_node;
1798 }
1799
1800 input_location = saved_loc;
1801
1802 ggc_collect ();
1803 timevar_pop (TV_REST_OF_COMPILATION);
1804
1805 /* Make sure that BE didn't give up on compiling. */
1806 gcc_assert (TREE_ASM_WRITTEN (decl));
1807 set_cfun (NULL);
1808 current_function_decl = NULL;
1809
1810 /* It would make a lot more sense to output thunks before function body to get more
1811 forward and lest backwarding jumps. This however would need solving problem
1812 with comdats. See PR48668. Also aliases must come after function itself to
1813 make one pass assemblers, like one on AIX, happy. See PR 50689.
1814 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1815 groups. */
1816 assemble_thunks_and_aliases (node);
1817 cgraph_release_function_body (node);
1818 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1819 points to the dead function body. */
1820 cgraph_node_remove_callees (node);
1821 ipa_remove_all_references (&node->ref_list);
1822 }
1823
1824
1825 /* Expand all functions that must be output.
1826
1827 Attempt to topologically sort the nodes so function is output when
1828 all called functions are already assembled to allow data to be
1829 propagated across the callgraph. Use a stack to get smaller distance
1830 between a function and its callees (later we may choose to use a more
1831 sophisticated algorithm for function reordering; we will likely want
1832 to use subsections to make the output functions appear in top-down
1833 order). */
1834
1835 static void
1836 expand_all_functions (void)
1837 {
1838 struct cgraph_node *node;
1839 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1840 int order_pos, new_order_pos = 0;
1841 int i;
1842
1843 order_pos = ipa_reverse_postorder (order);
1844 gcc_assert (order_pos == cgraph_n_nodes);
1845
1846 /* Garbage collector may remove inline clones we eliminate during
1847 optimization. So we must be sure to not reference them. */
1848 for (i = 0; i < order_pos; i++)
1849 if (order[i]->process)
1850 order[new_order_pos++] = order[i];
1851
1852 for (i = new_order_pos - 1; i >= 0; i--)
1853 {
1854 node = order[i];
1855 if (node->process)
1856 {
1857 node->process = 0;
1858 expand_function (node);
1859 }
1860 }
1861 cgraph_process_new_functions ();
1862
1863 free (order);
1864
1865 }
1866
1867 /* This is used to sort the node types by the cgraph order number. */
1868
1869 enum cgraph_order_sort_kind
1870 {
1871 ORDER_UNDEFINED = 0,
1872 ORDER_FUNCTION,
1873 ORDER_VAR,
1874 ORDER_ASM
1875 };
1876
1877 struct cgraph_order_sort
1878 {
1879 enum cgraph_order_sort_kind kind;
1880 union
1881 {
1882 struct cgraph_node *f;
1883 struct varpool_node *v;
1884 struct asm_node *a;
1885 } u;
1886 };
1887
1888 /* Output all functions, variables, and asm statements in the order
1889 according to their order fields, which is the order in which they
1890 appeared in the file. This implements -fno-toplevel-reorder. In
1891 this mode we may output functions and variables which don't really
1892 need to be output. */
1893
1894 static void
1895 output_in_order (void)
1896 {
1897 int max;
1898 struct cgraph_order_sort *nodes;
1899 int i;
1900 struct cgraph_node *pf;
1901 struct varpool_node *pv;
1902 struct asm_node *pa;
1903
1904 max = symtab_order;
1905 nodes = XCNEWVEC (struct cgraph_order_sort, max);
1906
1907 FOR_EACH_DEFINED_FUNCTION (pf)
1908 {
1909 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1910 {
1911 i = pf->order;
1912 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1913 nodes[i].kind = ORDER_FUNCTION;
1914 nodes[i].u.f = pf;
1915 }
1916 }
1917
1918 FOR_EACH_DEFINED_VARIABLE (pv)
1919 if (!DECL_EXTERNAL (pv->decl))
1920 {
1921 i = pv->order;
1922 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1923 nodes[i].kind = ORDER_VAR;
1924 nodes[i].u.v = pv;
1925 }
1926
1927 for (pa = asm_nodes; pa; pa = pa->next)
1928 {
1929 i = pa->order;
1930 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1931 nodes[i].kind = ORDER_ASM;
1932 nodes[i].u.a = pa;
1933 }
1934
1935 /* In toplevel reorder mode we output all statics; mark them as needed. */
1936
1937 for (i = 0; i < max; ++i)
1938 if (nodes[i].kind == ORDER_VAR)
1939 varpool_finalize_named_section_flags (nodes[i].u.v);
1940
1941 for (i = 0; i < max; ++i)
1942 {
1943 switch (nodes[i].kind)
1944 {
1945 case ORDER_FUNCTION:
1946 nodes[i].u.f->process = 0;
1947 expand_function (nodes[i].u.f);
1948 break;
1949
1950 case ORDER_VAR:
1951 varpool_assemble_decl (nodes[i].u.v);
1952 break;
1953
1954 case ORDER_ASM:
1955 assemble_asm (nodes[i].u.a->asm_str);
1956 break;
1957
1958 case ORDER_UNDEFINED:
1959 break;
1960
1961 default:
1962 gcc_unreachable ();
1963 }
1964 }
1965
1966 asm_nodes = NULL;
1967 free (nodes);
1968 }
1969
1970 static void
1971 ipa_passes (void)
1972 {
1973 gcc::pass_manager *passes = g->get_passes ();
1974
1975 set_cfun (NULL);
1976 current_function_decl = NULL;
1977 gimple_register_cfg_hooks ();
1978 bitmap_obstack_initialize (NULL);
1979
1980 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1981
1982 if (!in_lto_p)
1983 {
1984 execute_ipa_pass_list (passes->all_small_ipa_passes);
1985 if (seen_error ())
1986 return;
1987 }
1988
1989 /* We never run removal of unreachable nodes after early passes. This is
1990 because TODO is run before the subpasses. It is important to remove
1991 the unreachable functions to save works at IPA level and to get LTO
1992 symbol tables right. */
1993 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
1994
1995 /* If pass_all_early_optimizations was not scheduled, the state of
1996 the cgraph will not be properly updated. Update it now. */
1997 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1998 cgraph_state = CGRAPH_STATE_IPA_SSA;
1999
2000 if (!in_lto_p)
2001 {
2002 /* Generate coverage variables and constructors. */
2003 coverage_finish ();
2004
2005 /* Process new functions added. */
2006 set_cfun (NULL);
2007 current_function_decl = NULL;
2008 cgraph_process_new_functions ();
2009
2010 execute_ipa_summary_passes
2011 ((struct ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2012 }
2013
2014 /* Some targets need to handle LTO assembler output specially. */
2015 if (flag_generate_lto)
2016 targetm.asm_out.lto_start ();
2017
2018 execute_ipa_summary_passes ((struct ipa_opt_pass_d *)
2019 passes->all_lto_gen_passes);
2020
2021 if (!in_lto_p)
2022 ipa_write_summaries ();
2023
2024 if (flag_generate_lto)
2025 targetm.asm_out.lto_end ();
2026
2027 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2028 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2029 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2030
2031 bitmap_obstack_release (NULL);
2032 }
2033
2034
2035 /* Return string alias is alias of. */
2036
2037 static tree
2038 get_alias_symbol (tree decl)
2039 {
2040 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2041 return get_identifier (TREE_STRING_POINTER
2042 (TREE_VALUE (TREE_VALUE (alias))));
2043 }
2044
2045
2046 /* Weakrefs may be associated to external decls and thus not output
2047 at expansion time. Emit all necessary aliases. */
2048
2049 static void
2050 output_weakrefs (void)
2051 {
2052 symtab_node *node;
2053 FOR_EACH_SYMBOL (node)
2054 if (node->alias
2055 && !TREE_ASM_WRITTEN (node->decl)
2056 && node->weakref)
2057 {
2058 tree target;
2059
2060 /* Weakrefs are special by not requiring target definition in current
2061 compilation unit. It is thus bit hard to work out what we want to
2062 alias.
2063 When alias target is defined, we need to fetch it from symtab reference,
2064 otherwise it is pointed to by alias_target. */
2065 if (node->alias_target)
2066 target = (DECL_P (node->alias_target)
2067 ? DECL_ASSEMBLER_NAME (node->alias_target)
2068 : node->alias_target);
2069 else if (node->analyzed)
2070 target = DECL_ASSEMBLER_NAME (symtab_alias_target (node)->decl);
2071 else
2072 {
2073 gcc_unreachable ();
2074 target = get_alias_symbol (node->decl);
2075 }
2076 do_assemble_alias (node->decl, target);
2077 }
2078 }
2079
2080 /* Initialize callgraph dump file. */
2081
2082 void
2083 init_cgraph (void)
2084 {
2085 if (!cgraph_dump_file)
2086 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
2087 }
2088
2089
2090 /* Perform simple optimizations based on callgraph. */
2091
2092 void
2093 compile (void)
2094 {
2095 if (seen_error ())
2096 return;
2097
2098 #ifdef ENABLE_CHECKING
2099 verify_symtab ();
2100 #endif
2101
2102 timevar_push (TV_CGRAPHOPT);
2103 if (pre_ipa_mem_report)
2104 {
2105 fprintf (stderr, "Memory consumption before IPA\n");
2106 dump_memory_report (false);
2107 }
2108 if (!quiet_flag)
2109 fprintf (stderr, "Performing interprocedural optimizations\n");
2110 cgraph_state = CGRAPH_STATE_IPA;
2111
2112 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2113 if (flag_lto)
2114 lto_streamer_hooks_init ();
2115
2116 /* Don't run the IPA passes if there was any error or sorry messages. */
2117 if (!seen_error ())
2118 ipa_passes ();
2119
2120 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2121 if (seen_error ()
2122 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2123 {
2124 timevar_pop (TV_CGRAPHOPT);
2125 return;
2126 }
2127
2128 /* This pass remove bodies of extern inline functions we never inlined.
2129 Do this later so other IPA passes see what is really going on. */
2130 symtab_remove_unreachable_nodes (false, dump_file);
2131 cgraph_global_info_ready = true;
2132 if (cgraph_dump_file)
2133 {
2134 fprintf (cgraph_dump_file, "Optimized ");
2135 dump_symtab (cgraph_dump_file);
2136 }
2137 if (post_ipa_mem_report)
2138 {
2139 fprintf (stderr, "Memory consumption after IPA\n");
2140 dump_memory_report (false);
2141 }
2142 timevar_pop (TV_CGRAPHOPT);
2143
2144 /* Output everything. */
2145 (*debug_hooks->assembly_start) ();
2146 if (!quiet_flag)
2147 fprintf (stderr, "Assembling functions:\n");
2148 #ifdef ENABLE_CHECKING
2149 verify_symtab ();
2150 #endif
2151
2152 cgraph_materialize_all_clones ();
2153 bitmap_obstack_initialize (NULL);
2154 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2155 symtab_remove_unreachable_nodes (true, dump_file);
2156 #ifdef ENABLE_CHECKING
2157 verify_symtab ();
2158 #endif
2159 bitmap_obstack_release (NULL);
2160 mark_functions_to_output ();
2161
2162 /* When weakref support is missing, we autmatically translate all
2163 references to NODE to references to its ultimate alias target.
2164 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2165 TREE_CHAIN.
2166
2167 Set up this mapping before we output any assembler but once we are sure
2168 that all symbol renaming is done.
2169
2170 FIXME: All this uglyness can go away if we just do renaming at gimple
2171 level by physically rewritting the IL. At the moment we can only redirect
2172 calls, so we need infrastructure for renaming references as well. */
2173 #ifndef ASM_OUTPUT_WEAKREF
2174 symtab_node *node;
2175
2176 FOR_EACH_SYMBOL (node)
2177 if (node->alias
2178 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2179 {
2180 IDENTIFIER_TRANSPARENT_ALIAS
2181 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2182 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2183 = (node->alias_target ? node->alias_target
2184 : DECL_ASSEMBLER_NAME (symtab_alias_target (node)->decl));
2185 }
2186 #endif
2187
2188 cgraph_state = CGRAPH_STATE_EXPANSION;
2189 if (!flag_toplevel_reorder)
2190 output_in_order ();
2191 else
2192 {
2193 output_asm_statements ();
2194
2195 expand_all_functions ();
2196 varpool_output_variables ();
2197 }
2198
2199 cgraph_process_new_functions ();
2200 cgraph_state = CGRAPH_STATE_FINISHED;
2201 output_weakrefs ();
2202
2203 if (cgraph_dump_file)
2204 {
2205 fprintf (cgraph_dump_file, "\nFinal ");
2206 dump_symtab (cgraph_dump_file);
2207 }
2208 #ifdef ENABLE_CHECKING
2209 verify_symtab ();
2210 /* Double check that all inline clones are gone and that all
2211 function bodies have been released from memory. */
2212 if (!seen_error ())
2213 {
2214 struct cgraph_node *node;
2215 bool error_found = false;
2216
2217 FOR_EACH_DEFINED_FUNCTION (node)
2218 if (node->global.inlined_to
2219 || gimple_has_body_p (node->decl))
2220 {
2221 error_found = true;
2222 dump_cgraph_node (stderr, node);
2223 }
2224 if (error_found)
2225 internal_error ("nodes with unreleased memory found");
2226 }
2227 #endif
2228 }
2229
2230
2231 /* Analyze the whole compilation unit once it is parsed completely. */
2232
2233 void
2234 finalize_compilation_unit (void)
2235 {
2236 timevar_push (TV_CGRAPH);
2237
2238 /* If we're here there's no current function anymore. Some frontends
2239 are lazy in clearing these. */
2240 current_function_decl = NULL;
2241 set_cfun (NULL);
2242
2243 /* Do not skip analyzing the functions if there were errors, we
2244 miss diagnostics for following functions otherwise. */
2245
2246 /* Emit size functions we didn't inline. */
2247 finalize_size_functions ();
2248
2249 /* Mark alias targets necessary and emit diagnostics. */
2250 handle_alias_pairs ();
2251
2252 if (!quiet_flag)
2253 {
2254 fprintf (stderr, "\nAnalyzing compilation unit\n");
2255 fflush (stderr);
2256 }
2257
2258 if (flag_dump_passes)
2259 dump_passes ();
2260
2261 /* Gimplify and lower all functions, compute reachability and
2262 remove unreachable nodes. */
2263 analyze_functions ();
2264
2265 /* Mark alias targets necessary and emit diagnostics. */
2266 handle_alias_pairs ();
2267
2268 /* Gimplify and lower thunks. */
2269 analyze_functions ();
2270
2271 /* Finally drive the pass manager. */
2272 compile ();
2273
2274 timevar_pop (TV_CGRAPH);
2275 }
2276
2277
2278 #include "gt-cgraphunit.h"