]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cgraphunit.c
PR c/82301 - Updated test case g++.dg/ext/attr-ifunc-1.C (and others) in r253041...
[thirdparty/gcc.git] / gcc / cgraphunit.c
1 /* Driver of optimization process
2 Copyright (C) 2003-2017 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 - 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 "backend.h"
164 #include "target.h"
165 #include "rtl.h"
166 #include "tree.h"
167 #include "gimple.h"
168 #include "cfghooks.h"
169 #include "regset.h" /* FIXME: For reg_obstack. */
170 #include "alloc-pool.h"
171 #include "tree-pass.h"
172 #include "stringpool.h"
173 #include "gimple-ssa.h"
174 #include "cgraph.h"
175 #include "coverage.h"
176 #include "lto-streamer.h"
177 #include "fold-const.h"
178 #include "varasm.h"
179 #include "stor-layout.h"
180 #include "output.h"
181 #include "cfgcleanup.h"
182 #include "gimple-fold.h"
183 #include "gimplify.h"
184 #include "gimple-iterator.h"
185 #include "gimplify-me.h"
186 #include "tree-cfg.h"
187 #include "tree-into-ssa.h"
188 #include "tree-ssa.h"
189 #include "langhooks.h"
190 #include "toplev.h"
191 #include "debug.h"
192 #include "symbol-summary.h"
193 #include "tree-vrp.h"
194 #include "ipa-prop.h"
195 #include "gimple-pretty-print.h"
196 #include "plugin.h"
197 #include "ipa-fnsummary.h"
198 #include "ipa-utils.h"
199 #include "except.h"
200 #include "cfgloop.h"
201 #include "context.h"
202 #include "pass_manager.h"
203 #include "tree-nested.h"
204 #include "dbgcnt.h"
205 #include "tree-chkp.h"
206 #include "lto-section-names.h"
207 #include "stringpool.h"
208 #include "attribs.h"
209
210 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
211 secondary queue used during optimization to accommodate passes that
212 may generate new functions that need to be optimized and expanded. */
213 vec<cgraph_node *> cgraph_new_nodes;
214
215 static void expand_all_functions (void);
216 static void mark_functions_to_output (void);
217 static void handle_alias_pairs (void);
218
219 /* Used for vtable lookup in thunk adjusting. */
220 static GTY (()) tree vtable_entry_type;
221
222 /* Return true if this symbol is a function from the C frontend specified
223 directly in RTL form (with "__RTL"). */
224
225 bool
226 symtab_node::native_rtl_p () const
227 {
228 if (TREE_CODE (decl) != FUNCTION_DECL)
229 return false;
230 if (!DECL_STRUCT_FUNCTION (decl))
231 return false;
232 return DECL_STRUCT_FUNCTION (decl)->curr_properties & PROP_rtl;
233 }
234
235 /* Determine if symbol declaration is needed. That is, visible to something
236 either outside this translation unit, something magic in the system
237 configury */
238 bool
239 symtab_node::needed_p (void)
240 {
241 /* Double check that no one output the function into assembly file
242 early. */
243 if (!native_rtl_p ())
244 gcc_checking_assert
245 (!DECL_ASSEMBLER_NAME_SET_P (decl)
246 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
247
248 if (!definition)
249 return false;
250
251 if (DECL_EXTERNAL (decl))
252 return false;
253
254 /* If the user told us it is used, then it must be so. */
255 if (force_output)
256 return true;
257
258 /* ABI forced symbols are needed when they are external. */
259 if (forced_by_abi && TREE_PUBLIC (decl))
260 return true;
261
262 /* Keep constructors, destructors and virtual functions. */
263 if (TREE_CODE (decl) == FUNCTION_DECL
264 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
265 return true;
266
267 /* Externally visible variables must be output. The exception is
268 COMDAT variables that must be output only when they are needed. */
269 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
270 return true;
271
272 return false;
273 }
274
275 /* Head and terminator of the queue of nodes to be processed while building
276 callgraph. */
277
278 static symtab_node symtab_terminator;
279 static symtab_node *queued_nodes = &symtab_terminator;
280
281 /* Add NODE to queue starting at QUEUED_NODES.
282 The queue is linked via AUX pointers and terminated by pointer to 1. */
283
284 static void
285 enqueue_node (symtab_node *node)
286 {
287 if (node->aux)
288 return;
289 gcc_checking_assert (queued_nodes);
290 node->aux = queued_nodes;
291 queued_nodes = node;
292 }
293
294 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
295 functions into callgraph in a way so they look like ordinary reachable
296 functions inserted into callgraph already at construction time. */
297
298 void
299 symbol_table::process_new_functions (void)
300 {
301 tree fndecl;
302
303 if (!cgraph_new_nodes.exists ())
304 return;
305
306 handle_alias_pairs ();
307 /* Note that this queue may grow as its being processed, as the new
308 functions may generate new ones. */
309 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
310 {
311 cgraph_node *node = cgraph_new_nodes[i];
312 fndecl = node->decl;
313 switch (state)
314 {
315 case CONSTRUCTION:
316 /* At construction time we just need to finalize function and move
317 it into reachable functions list. */
318
319 cgraph_node::finalize_function (fndecl, false);
320 call_cgraph_insertion_hooks (node);
321 enqueue_node (node);
322 break;
323
324 case IPA:
325 case IPA_SSA:
326 case IPA_SSA_AFTER_INLINING:
327 /* When IPA optimization already started, do all essential
328 transformations that has been already performed on the whole
329 cgraph but not on this function. */
330
331 gimple_register_cfg_hooks ();
332 if (!node->analyzed)
333 node->analyze ();
334 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
335 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
336 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
337 {
338 bool summaried_computed = ipa_fn_summaries != NULL;
339 g->get_passes ()->execute_early_local_passes ();
340 /* Early passes compure inline parameters to do inlining
341 and splitting. This is redundant for functions added late.
342 Just throw away whatever it did. */
343 if (!summaried_computed)
344 ipa_free_fn_summary ();
345 }
346 else if (ipa_fn_summaries != NULL)
347 compute_fn_summary (node, true);
348 free_dominance_info (CDI_POST_DOMINATORS);
349 free_dominance_info (CDI_DOMINATORS);
350 pop_cfun ();
351 call_cgraph_insertion_hooks (node);
352 break;
353
354 case EXPANSION:
355 /* Functions created during expansion shall be compiled
356 directly. */
357 node->process = 0;
358 call_cgraph_insertion_hooks (node);
359 node->expand ();
360 break;
361
362 default:
363 gcc_unreachable ();
364 break;
365 }
366 }
367
368 cgraph_new_nodes.release ();
369 }
370
371 /* As an GCC extension we allow redefinition of the function. The
372 semantics when both copies of bodies differ is not well defined.
373 We replace the old body with new body so in unit at a time mode
374 we always use new body, while in normal mode we may end up with
375 old body inlined into some functions and new body expanded and
376 inlined in others.
377
378 ??? It may make more sense to use one body for inlining and other
379 body for expanding the function but this is difficult to do. */
380
381 void
382 cgraph_node::reset (void)
383 {
384 /* If process is set, then we have already begun whole-unit analysis.
385 This is *not* testing for whether we've already emitted the function.
386 That case can be sort-of legitimately seen with real function redefinition
387 errors. I would argue that the front end should never present us with
388 such a case, but don't enforce that for now. */
389 gcc_assert (!process);
390
391 /* Reset our data structures so we can analyze the function again. */
392 memset (&local, 0, sizeof (local));
393 memset (&global, 0, sizeof (global));
394 memset (&rtl, 0, sizeof (rtl));
395 analyzed = false;
396 definition = false;
397 alias = false;
398 transparent_alias = false;
399 weakref = false;
400 cpp_implicit_alias = false;
401
402 remove_callees ();
403 remove_all_references ();
404 }
405
406 /* Return true when there are references to the node. INCLUDE_SELF is
407 true if a self reference counts as a reference. */
408
409 bool
410 symtab_node::referred_to_p (bool include_self)
411 {
412 ipa_ref *ref = NULL;
413
414 /* See if there are any references at all. */
415 if (iterate_referring (0, ref))
416 return true;
417 /* For functions check also calls. */
418 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
419 if (cn && cn->callers)
420 {
421 if (include_self)
422 return true;
423 for (cgraph_edge *e = cn->callers; e; e = e->next_caller)
424 if (e->caller != this)
425 return true;
426 }
427 return false;
428 }
429
430 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
431 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
432 the garbage collector run at the moment. We would need to either create
433 a new GC context, or just not compile right now. */
434
435 void
436 cgraph_node::finalize_function (tree decl, bool no_collect)
437 {
438 cgraph_node *node = cgraph_node::get_create (decl);
439
440 if (node->definition)
441 {
442 /* Nested functions should only be defined once. */
443 gcc_assert (!DECL_CONTEXT (decl)
444 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
445 node->reset ();
446 node->local.redefined_extern_inline = true;
447 }
448
449 /* Set definition first before calling notice_global_symbol so that
450 it is available to notice_global_symbol. */
451 node->definition = true;
452 notice_global_symbol (decl);
453 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
454 if (!flag_toplevel_reorder)
455 node->no_reorder = true;
456
457 /* With -fkeep-inline-functions we are keeping all inline functions except
458 for extern inline ones. */
459 if (flag_keep_inline_functions
460 && DECL_DECLARED_INLINE_P (decl)
461 && !DECL_EXTERNAL (decl)
462 && !DECL_DISREGARD_INLINE_LIMITS (decl))
463 node->force_output = 1;
464
465 /* __RTL functions were already output as soon as they were parsed (due
466 to the large amount of global state in the backend).
467 Mark such functions as "force_output" to reflect the fact that they
468 will be in the asm file when considering the symbols they reference.
469 The attempt to output them later on will bail out immediately. */
470 if (node->native_rtl_p ())
471 node->force_output = 1;
472
473 /* When not optimizing, also output the static functions. (see
474 PR24561), but don't do so for always_inline functions, functions
475 declared inline and nested functions. These were optimized out
476 in the original implementation and it is unclear whether we want
477 to change the behavior here. */
478 if (((!opt_for_fn (decl, optimize) || flag_keep_static_functions
479 || node->no_reorder)
480 && !node->cpp_implicit_alias
481 && !DECL_DISREGARD_INLINE_LIMITS (decl)
482 && !DECL_DECLARED_INLINE_P (decl)
483 && !(DECL_CONTEXT (decl)
484 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
485 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
486 node->force_output = 1;
487
488 /* If we've not yet emitted decl, tell the debug info about it. */
489 if (!TREE_ASM_WRITTEN (decl))
490 (*debug_hooks->deferred_inline_function) (decl);
491
492 if (!no_collect)
493 ggc_collect ();
494
495 if (symtab->state == CONSTRUCTION
496 && (node->needed_p () || node->referred_to_p ()))
497 enqueue_node (node);
498 }
499
500 /* Add the function FNDECL to the call graph.
501 Unlike finalize_function, this function is intended to be used
502 by middle end and allows insertion of new function at arbitrary point
503 of compilation. The function can be either in high, low or SSA form
504 GIMPLE.
505
506 The function is assumed to be reachable and have address taken (so no
507 API breaking optimizations are performed on it).
508
509 Main work done by this function is to enqueue the function for later
510 processing to avoid need the passes to be re-entrant. */
511
512 void
513 cgraph_node::add_new_function (tree fndecl, bool lowered)
514 {
515 gcc::pass_manager *passes = g->get_passes ();
516 cgraph_node *node;
517
518 if (dump_file)
519 {
520 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
521 const char *function_type = ((gimple_has_body_p (fndecl))
522 ? (lowered
523 ? (gimple_in_ssa_p (fn)
524 ? "ssa gimple"
525 : "low gimple")
526 : "high gimple")
527 : "to-be-gimplified");
528 fprintf (dump_file,
529 "Added new %s function %s to callgraph\n",
530 function_type,
531 fndecl_name (fndecl));
532 }
533
534 switch (symtab->state)
535 {
536 case PARSING:
537 cgraph_node::finalize_function (fndecl, false);
538 break;
539 case CONSTRUCTION:
540 /* Just enqueue function to be processed at nearest occurrence. */
541 node = cgraph_node::get_create (fndecl);
542 if (lowered)
543 node->lowered = true;
544 cgraph_new_nodes.safe_push (node);
545 break;
546
547 case IPA:
548 case IPA_SSA:
549 case IPA_SSA_AFTER_INLINING:
550 case EXPANSION:
551 /* Bring the function into finalized state and enqueue for later
552 analyzing and compilation. */
553 node = cgraph_node::get_create (fndecl);
554 node->local.local = false;
555 node->definition = true;
556 node->force_output = true;
557 if (TREE_PUBLIC (fndecl))
558 node->externally_visible = true;
559 if (!lowered && symtab->state == EXPANSION)
560 {
561 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
562 gimple_register_cfg_hooks ();
563 bitmap_obstack_initialize (NULL);
564 execute_pass_list (cfun, passes->all_lowering_passes);
565 passes->execute_early_local_passes ();
566 bitmap_obstack_release (NULL);
567 pop_cfun ();
568
569 lowered = true;
570 }
571 if (lowered)
572 node->lowered = true;
573 cgraph_new_nodes.safe_push (node);
574 break;
575
576 case FINISHED:
577 /* At the very end of compilation we have to do all the work up
578 to expansion. */
579 node = cgraph_node::create (fndecl);
580 if (lowered)
581 node->lowered = true;
582 node->definition = true;
583 node->analyze ();
584 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
585 gimple_register_cfg_hooks ();
586 bitmap_obstack_initialize (NULL);
587 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
588 g->get_passes ()->execute_early_local_passes ();
589 bitmap_obstack_release (NULL);
590 pop_cfun ();
591 node->expand ();
592 break;
593
594 default:
595 gcc_unreachable ();
596 }
597
598 /* Set a personality if required and we already passed EH lowering. */
599 if (lowered
600 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
601 == eh_personality_lang))
602 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
603 }
604
605 /* Analyze the function scheduled to be output. */
606 void
607 cgraph_node::analyze (void)
608 {
609 if (native_rtl_p ())
610 {
611 analyzed = true;
612 return;
613 }
614
615 tree decl = this->decl;
616 location_t saved_loc = input_location;
617 input_location = DECL_SOURCE_LOCATION (decl);
618
619 if (thunk.thunk_p)
620 {
621 cgraph_node *t = cgraph_node::get (thunk.alias);
622
623 create_edge (t, NULL, t->count, CGRAPH_FREQ_BASE);
624 callees->can_throw_external = !TREE_NOTHROW (t->decl);
625 /* Target code in expand_thunk may need the thunk's target
626 to be analyzed, so recurse here. */
627 if (!t->analyzed)
628 t->analyze ();
629 if (t->alias)
630 {
631 t = t->get_alias_target ();
632 if (!t->analyzed)
633 t->analyze ();
634 }
635 if (!expand_thunk (false, false))
636 {
637 thunk.alias = NULL;
638 return;
639 }
640 thunk.alias = NULL;
641 }
642 if (alias)
643 resolve_alias (cgraph_node::get (alias_target), transparent_alias);
644 else if (dispatcher_function)
645 {
646 /* Generate the dispatcher body of multi-versioned functions. */
647 cgraph_function_version_info *dispatcher_version_info
648 = function_version ();
649 if (dispatcher_version_info != NULL
650 && (dispatcher_version_info->dispatcher_resolver
651 == NULL_TREE))
652 {
653 tree resolver = NULL_TREE;
654 gcc_assert (targetm.generate_version_dispatcher_body);
655 resolver = targetm.generate_version_dispatcher_body (this);
656 gcc_assert (resolver != NULL_TREE);
657 }
658 }
659 else
660 {
661 push_cfun (DECL_STRUCT_FUNCTION (decl));
662
663 assign_assembler_name_if_needed (decl);
664
665 /* Make sure to gimplify bodies only once. During analyzing a
666 function we lower it, which will require gimplified nested
667 functions, so we can end up here with an already gimplified
668 body. */
669 if (!gimple_has_body_p (decl))
670 gimplify_function_tree (decl);
671
672 /* Lower the function. */
673 if (!lowered)
674 {
675 if (nested)
676 lower_nested_functions (decl);
677 gcc_assert (!nested);
678
679 gimple_register_cfg_hooks ();
680 bitmap_obstack_initialize (NULL);
681 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
682 free_dominance_info (CDI_POST_DOMINATORS);
683 free_dominance_info (CDI_DOMINATORS);
684 compact_blocks ();
685 bitmap_obstack_release (NULL);
686 lowered = true;
687 }
688
689 pop_cfun ();
690 }
691 analyzed = true;
692
693 input_location = saved_loc;
694 }
695
696 /* C++ frontend produce same body aliases all over the place, even before PCH
697 gets streamed out. It relies on us linking the aliases with their function
698 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
699 first produce aliases without links, but once C++ FE is sure he won't sream
700 PCH we build the links via this function. */
701
702 void
703 symbol_table::process_same_body_aliases (void)
704 {
705 symtab_node *node;
706 FOR_EACH_SYMBOL (node)
707 if (node->cpp_implicit_alias && !node->analyzed)
708 node->resolve_alias
709 (VAR_P (node->alias_target)
710 ? (symtab_node *)varpool_node::get_create (node->alias_target)
711 : (symtab_node *)cgraph_node::get_create (node->alias_target));
712 cpp_implicit_aliases_done = true;
713 }
714
715 /* Process attributes common for vars and functions. */
716
717 static void
718 process_common_attributes (symtab_node *node, tree decl)
719 {
720 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
721
722 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
723 {
724 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
725 "%<weakref%> attribute should be accompanied with"
726 " an %<alias%> attribute");
727 DECL_WEAK (decl) = 0;
728 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
729 DECL_ATTRIBUTES (decl));
730 }
731
732 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
733 node->no_reorder = 1;
734 }
735
736 /* Look for externally_visible and used attributes and mark cgraph nodes
737 accordingly.
738
739 We cannot mark the nodes at the point the attributes are processed (in
740 handle_*_attribute) because the copy of the declarations available at that
741 point may not be canonical. For example, in:
742
743 void f();
744 void f() __attribute__((used));
745
746 the declaration we see in handle_used_attribute will be the second
747 declaration -- but the front end will subsequently merge that declaration
748 with the original declaration and discard the second declaration.
749
750 Furthermore, we can't mark these nodes in finalize_function because:
751
752 void f() {}
753 void f() __attribute__((externally_visible));
754
755 is valid.
756
757 So, we walk the nodes at the end of the translation unit, applying the
758 attributes at that point. */
759
760 static void
761 process_function_and_variable_attributes (cgraph_node *first,
762 varpool_node *first_var)
763 {
764 cgraph_node *node;
765 varpool_node *vnode;
766
767 for (node = symtab->first_function (); node != first;
768 node = symtab->next_function (node))
769 {
770 tree decl = node->decl;
771 if (DECL_PRESERVE_P (decl))
772 node->mark_force_output ();
773 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
774 {
775 if (! TREE_PUBLIC (node->decl))
776 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
777 "%<externally_visible%>"
778 " attribute have effect only on public objects");
779 }
780 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
781 && (node->definition && !node->alias))
782 {
783 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
784 "%<weakref%> attribute ignored"
785 " because function is defined");
786 DECL_WEAK (decl) = 0;
787 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
788 DECL_ATTRIBUTES (decl));
789 }
790
791 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
792 && !DECL_DECLARED_INLINE_P (decl)
793 /* redefining extern inline function makes it DECL_UNINLINABLE. */
794 && !DECL_UNINLINABLE (decl))
795 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
796 "always_inline function might not be inlinable");
797
798 process_common_attributes (node, decl);
799 }
800 for (vnode = symtab->first_variable (); vnode != first_var;
801 vnode = symtab->next_variable (vnode))
802 {
803 tree decl = vnode->decl;
804 if (DECL_EXTERNAL (decl)
805 && DECL_INITIAL (decl))
806 varpool_node::finalize_decl (decl);
807 if (DECL_PRESERVE_P (decl))
808 vnode->force_output = true;
809 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
810 {
811 if (! TREE_PUBLIC (vnode->decl))
812 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
813 "%<externally_visible%>"
814 " attribute have effect only on public objects");
815 }
816 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
817 && vnode->definition
818 && DECL_INITIAL (decl))
819 {
820 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
821 "%<weakref%> attribute ignored"
822 " because variable is initialized");
823 DECL_WEAK (decl) = 0;
824 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
825 DECL_ATTRIBUTES (decl));
826 }
827 process_common_attributes (vnode, decl);
828 }
829 }
830
831 /* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
832 middle end to output the variable to asm file, if needed or externally
833 visible. */
834
835 void
836 varpool_node::finalize_decl (tree decl)
837 {
838 varpool_node *node = varpool_node::get_create (decl);
839
840 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
841
842 if (node->definition)
843 return;
844 /* Set definition first before calling notice_global_symbol so that
845 it is available to notice_global_symbol. */
846 node->definition = true;
847 notice_global_symbol (decl);
848 if (!flag_toplevel_reorder)
849 node->no_reorder = true;
850 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
851 /* Traditionally we do not eliminate static variables when not
852 optimizing and when not doing toplevel reoder. */
853 || (node->no_reorder && !DECL_COMDAT (node->decl)
854 && !DECL_ARTIFICIAL (node->decl)))
855 node->force_output = true;
856
857 if (symtab->state == CONSTRUCTION
858 && (node->needed_p () || node->referred_to_p ()))
859 enqueue_node (node);
860 if (symtab->state >= IPA_SSA)
861 node->analyze ();
862 /* Some frontends produce various interface variables after compilation
863 finished. */
864 if (symtab->state == FINISHED
865 || (node->no_reorder
866 && symtab->state == EXPANSION))
867 node->assemble_decl ();
868
869 if (DECL_INITIAL (decl))
870 chkp_register_var_initializer (decl);
871 }
872
873 /* EDGE is an polymorphic call. Mark all possible targets as reachable
874 and if there is only one target, perform trivial devirtualization.
875 REACHABLE_CALL_TARGETS collects target lists we already walked to
876 avoid udplicate work. */
877
878 static void
879 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
880 cgraph_edge *edge)
881 {
882 unsigned int i;
883 void *cache_token;
884 bool final;
885 vec <cgraph_node *>targets
886 = possible_polymorphic_call_targets
887 (edge, &final, &cache_token);
888
889 if (!reachable_call_targets->add (cache_token))
890 {
891 if (symtab->dump_file)
892 dump_possible_polymorphic_call_targets
893 (symtab->dump_file, edge);
894
895 for (i = 0; i < targets.length (); i++)
896 {
897 /* Do not bother to mark virtual methods in anonymous namespace;
898 either we will find use of virtual table defining it, or it is
899 unused. */
900 if (targets[i]->definition
901 && TREE_CODE
902 (TREE_TYPE (targets[i]->decl))
903 == METHOD_TYPE
904 && !type_in_anonymous_namespace_p
905 (TYPE_METHOD_BASETYPE (TREE_TYPE (targets[i]->decl))))
906 enqueue_node (targets[i]);
907 }
908 }
909
910 /* Very trivial devirtualization; when the type is
911 final or anonymous (so we know all its derivation)
912 and there is only one possible virtual call target,
913 make the edge direct. */
914 if (final)
915 {
916 if (targets.length () <= 1 && dbg_cnt (devirt))
917 {
918 cgraph_node *target;
919 if (targets.length () == 1)
920 target = targets[0];
921 else
922 target = cgraph_node::create
923 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
924
925 if (symtab->dump_file)
926 {
927 fprintf (symtab->dump_file,
928 "Devirtualizing call: ");
929 print_gimple_stmt (symtab->dump_file,
930 edge->call_stmt, 0,
931 TDF_SLIM);
932 }
933 if (dump_enabled_p ())
934 {
935 location_t locus = gimple_location_safe (edge->call_stmt);
936 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
937 "devirtualizing call in %s to %s\n",
938 edge->caller->name (), target->name ());
939 }
940
941 edge->make_direct (target);
942 edge->redirect_call_stmt_to_callee ();
943
944 /* Call to __builtin_unreachable shouldn't be instrumented. */
945 if (!targets.length ())
946 gimple_call_set_with_bounds (edge->call_stmt, false);
947
948 if (symtab->dump_file)
949 {
950 fprintf (symtab->dump_file,
951 "Devirtualized as: ");
952 print_gimple_stmt (symtab->dump_file,
953 edge->call_stmt, 0,
954 TDF_SLIM);
955 }
956 }
957 }
958 }
959
960 /* Issue appropriate warnings for the global declaration DECL. */
961
962 static void
963 check_global_declaration (symtab_node *snode)
964 {
965 const char *decl_file;
966 tree decl = snode->decl;
967
968 /* Warn about any function declared static but not defined. We don't
969 warn about variables, because many programs have static variables
970 that exist only to get some text into the object file. */
971 if (TREE_CODE (decl) == FUNCTION_DECL
972 && DECL_INITIAL (decl) == 0
973 && DECL_EXTERNAL (decl)
974 && ! DECL_ARTIFICIAL (decl)
975 && ! TREE_NO_WARNING (decl)
976 && ! TREE_PUBLIC (decl)
977 && (warn_unused_function
978 || snode->referred_to_p (/*include_self=*/false)))
979 {
980 if (snode->referred_to_p (/*include_self=*/false))
981 pedwarn (input_location, 0, "%q+F used but never defined", decl);
982 else
983 warning (OPT_Wunused_function, "%q+F declared %<static%> but never defined", decl);
984 /* This symbol is effectively an "extern" declaration now. */
985 TREE_PUBLIC (decl) = 1;
986 }
987
988 /* Warn about static fns or vars defined but not used. */
989 if (((warn_unused_function && TREE_CODE (decl) == FUNCTION_DECL)
990 || (((warn_unused_variable && ! TREE_READONLY (decl))
991 || (warn_unused_const_variable > 0 && TREE_READONLY (decl)
992 && (warn_unused_const_variable == 2
993 || (main_input_filename != NULL
994 && (decl_file = DECL_SOURCE_FILE (decl)) != NULL
995 && filename_cmp (main_input_filename,
996 decl_file) == 0))))
997 && VAR_P (decl)))
998 && ! DECL_IN_SYSTEM_HEADER (decl)
999 && ! snode->referred_to_p (/*include_self=*/false)
1000 /* This TREE_USED check is needed in addition to referred_to_p
1001 above, because the `__unused__' attribute is not being
1002 considered for referred_to_p. */
1003 && ! TREE_USED (decl)
1004 /* The TREE_USED bit for file-scope decls is kept in the identifier,
1005 to handle multiple external decls in different scopes. */
1006 && ! (DECL_NAME (decl) && TREE_USED (DECL_NAME (decl)))
1007 && ! DECL_EXTERNAL (decl)
1008 && ! DECL_ARTIFICIAL (decl)
1009 && ! DECL_ABSTRACT_ORIGIN (decl)
1010 && ! TREE_PUBLIC (decl)
1011 /* A volatile variable might be used in some non-obvious way. */
1012 && (! VAR_P (decl) || ! TREE_THIS_VOLATILE (decl))
1013 /* Global register variables must be declared to reserve them. */
1014 && ! (VAR_P (decl) && DECL_REGISTER (decl))
1015 /* Global ctors and dtors are called by the runtime. */
1016 && (TREE_CODE (decl) != FUNCTION_DECL
1017 || (!DECL_STATIC_CONSTRUCTOR (decl)
1018 && !DECL_STATIC_DESTRUCTOR (decl)))
1019 /* Otherwise, ask the language. */
1020 && lang_hooks.decls.warn_unused_global (decl))
1021 warning_at (DECL_SOURCE_LOCATION (decl),
1022 (TREE_CODE (decl) == FUNCTION_DECL)
1023 ? OPT_Wunused_function
1024 : (TREE_READONLY (decl)
1025 ? OPT_Wunused_const_variable_
1026 : OPT_Wunused_variable),
1027 "%qD defined but not used", decl);
1028 }
1029
1030 /* Discover all functions and variables that are trivially needed, analyze
1031 them as well as all functions and variables referred by them */
1032 static cgraph_node *first_analyzed;
1033 static varpool_node *first_analyzed_var;
1034
1035 /* FIRST_TIME is set to TRUE for the first time we are called for a
1036 translation unit from finalize_compilation_unit() or false
1037 otherwise. */
1038
1039 static void
1040 analyze_functions (bool first_time)
1041 {
1042 /* Keep track of already processed nodes when called multiple times for
1043 intermodule optimization. */
1044 cgraph_node *first_handled = first_analyzed;
1045 varpool_node *first_handled_var = first_analyzed_var;
1046 hash_set<void *> reachable_call_targets;
1047
1048 symtab_node *node;
1049 symtab_node *next;
1050 int i;
1051 ipa_ref *ref;
1052 bool changed = true;
1053 location_t saved_loc = input_location;
1054
1055 bitmap_obstack_initialize (NULL);
1056 symtab->state = CONSTRUCTION;
1057 input_location = UNKNOWN_LOCATION;
1058
1059 /* Ugly, but the fixup can not happen at a time same body alias is created;
1060 C++ FE is confused about the COMDAT groups being right. */
1061 if (symtab->cpp_implicit_aliases_done)
1062 FOR_EACH_SYMBOL (node)
1063 if (node->cpp_implicit_alias)
1064 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
1065 build_type_inheritance_graph ();
1066
1067 /* Analysis adds static variables that in turn adds references to new functions.
1068 So we need to iterate the process until it stabilize. */
1069 while (changed)
1070 {
1071 changed = false;
1072 process_function_and_variable_attributes (first_analyzed,
1073 first_analyzed_var);
1074
1075 /* First identify the trivially needed symbols. */
1076 for (node = symtab->first_symbol ();
1077 node != first_analyzed
1078 && node != first_analyzed_var; node = node->next)
1079 {
1080 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
1081 node->get_comdat_group_id ();
1082 if (node->needed_p ())
1083 {
1084 enqueue_node (node);
1085 if (!changed && symtab->dump_file)
1086 fprintf (symtab->dump_file, "Trivially needed symbols:");
1087 changed = true;
1088 if (symtab->dump_file)
1089 fprintf (symtab->dump_file, " %s", node->asm_name ());
1090 if (!changed && symtab->dump_file)
1091 fprintf (symtab->dump_file, "\n");
1092 }
1093 if (node == first_analyzed
1094 || node == first_analyzed_var)
1095 break;
1096 }
1097 symtab->process_new_functions ();
1098 first_analyzed_var = symtab->first_variable ();
1099 first_analyzed = symtab->first_function ();
1100
1101 if (changed && symtab->dump_file)
1102 fprintf (symtab->dump_file, "\n");
1103
1104 /* Lower representation, build callgraph edges and references for all trivially
1105 needed symbols and all symbols referred by them. */
1106 while (queued_nodes != &symtab_terminator)
1107 {
1108 changed = true;
1109 node = queued_nodes;
1110 queued_nodes = (symtab_node *)queued_nodes->aux;
1111 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1112 if (cnode && cnode->definition)
1113 {
1114 cgraph_edge *edge;
1115 tree decl = cnode->decl;
1116
1117 /* ??? It is possible to create extern inline function
1118 and later using weak alias attribute to kill its body.
1119 See gcc.c-torture/compile/20011119-1.c */
1120 if (!DECL_STRUCT_FUNCTION (decl)
1121 && !cnode->alias
1122 && !cnode->thunk.thunk_p
1123 && !cnode->dispatcher_function)
1124 {
1125 cnode->reset ();
1126 cnode->local.redefined_extern_inline = true;
1127 continue;
1128 }
1129
1130 if (!cnode->analyzed)
1131 cnode->analyze ();
1132
1133 for (edge = cnode->callees; edge; edge = edge->next_callee)
1134 if (edge->callee->definition
1135 && (!DECL_EXTERNAL (edge->callee->decl)
1136 /* When not optimizing, do not try to analyze extern
1137 inline functions. Doing so is pointless. */
1138 || opt_for_fn (edge->callee->decl, optimize)
1139 /* Weakrefs needs to be preserved. */
1140 || edge->callee->alias
1141 /* always_inline functions are inlined aven at -O0. */
1142 || lookup_attribute
1143 ("always_inline",
1144 DECL_ATTRIBUTES (edge->callee->decl))
1145 /* Multiversioned functions needs the dispatcher to
1146 be produced locally even for extern functions. */
1147 || edge->callee->function_version ()))
1148 enqueue_node (edge->callee);
1149 if (opt_for_fn (cnode->decl, optimize)
1150 && opt_for_fn (cnode->decl, flag_devirtualize))
1151 {
1152 cgraph_edge *next;
1153
1154 for (edge = cnode->indirect_calls; edge; edge = next)
1155 {
1156 next = edge->next_callee;
1157 if (edge->indirect_info->polymorphic)
1158 walk_polymorphic_call_targets (&reachable_call_targets,
1159 edge);
1160 }
1161 }
1162
1163 /* If decl is a clone of an abstract function,
1164 mark that abstract function so that we don't release its body.
1165 The DECL_INITIAL() of that abstract function declaration
1166 will be later needed to output debug info. */
1167 if (DECL_ABSTRACT_ORIGIN (decl))
1168 {
1169 cgraph_node *origin_node
1170 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
1171 origin_node->used_as_abstract_origin = true;
1172 }
1173 /* Preserve a functions function context node. It will
1174 later be needed to output debug info. */
1175 if (tree fn = decl_function_context (decl))
1176 {
1177 cgraph_node *origin_node = cgraph_node::get_create (fn);
1178 enqueue_node (origin_node);
1179 }
1180 }
1181 else
1182 {
1183 varpool_node *vnode = dyn_cast <varpool_node *> (node);
1184 if (vnode && vnode->definition && !vnode->analyzed)
1185 vnode->analyze ();
1186 }
1187
1188 if (node->same_comdat_group)
1189 {
1190 symtab_node *next;
1191 for (next = node->same_comdat_group;
1192 next != node;
1193 next = next->same_comdat_group)
1194 if (!next->comdat_local_p ())
1195 enqueue_node (next);
1196 }
1197 for (i = 0; node->iterate_reference (i, ref); i++)
1198 if (ref->referred->definition
1199 && (!DECL_EXTERNAL (ref->referred->decl)
1200 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1201 && optimize)
1202 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1203 && opt_for_fn (ref->referred->decl, optimize))
1204 || node->alias
1205 || ref->referred->alias)))
1206 enqueue_node (ref->referred);
1207 symtab->process_new_functions ();
1208 }
1209 }
1210 update_type_inheritance_graph ();
1211
1212 /* Collect entry points to the unit. */
1213 if (symtab->dump_file)
1214 {
1215 fprintf (symtab->dump_file, "\n\nInitial ");
1216 symtab->dump (symtab->dump_file);
1217 }
1218
1219 if (first_time)
1220 {
1221 symtab_node *snode;
1222 FOR_EACH_SYMBOL (snode)
1223 check_global_declaration (snode);
1224 }
1225
1226 if (symtab->dump_file)
1227 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1228
1229 for (node = symtab->first_symbol ();
1230 node != first_handled
1231 && node != first_handled_var; node = next)
1232 {
1233 next = node->next;
1234 if (!node->aux && !node->referred_to_p ())
1235 {
1236 if (symtab->dump_file)
1237 fprintf (symtab->dump_file, " %s", node->name ());
1238
1239 /* See if the debugger can use anything before the DECL
1240 passes away. Perhaps it can notice a DECL that is now a
1241 constant and can tag the early DIE with an appropriate
1242 attribute.
1243
1244 Otherwise, this is the last chance the debug_hooks have
1245 at looking at optimized away DECLs, since
1246 late_global_decl will subsequently be called from the
1247 contents of the now pruned symbol table. */
1248 if (VAR_P (node->decl)
1249 && !decl_function_context (node->decl))
1250 {
1251 /* We are reclaiming totally unreachable code and variables
1252 so they effectively appear as readonly. Show that to
1253 the debug machinery. */
1254 TREE_READONLY (node->decl) = 1;
1255 node->definition = false;
1256 (*debug_hooks->late_global_decl) (node->decl);
1257 }
1258
1259 node->remove ();
1260 continue;
1261 }
1262 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
1263 {
1264 tree decl = node->decl;
1265
1266 if (cnode->definition && !gimple_has_body_p (decl)
1267 && !cnode->alias
1268 && !cnode->thunk.thunk_p)
1269 cnode->reset ();
1270
1271 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1272 || cnode->alias
1273 || gimple_has_body_p (decl)
1274 || cnode->native_rtl_p ());
1275 gcc_assert (cnode->analyzed == cnode->definition);
1276 }
1277 node->aux = NULL;
1278 }
1279 for (;node; node = node->next)
1280 node->aux = NULL;
1281 first_analyzed = symtab->first_function ();
1282 first_analyzed_var = symtab->first_variable ();
1283 if (symtab->dump_file)
1284 {
1285 fprintf (symtab->dump_file, "\n\nReclaimed ");
1286 symtab->dump (symtab->dump_file);
1287 }
1288 bitmap_obstack_release (NULL);
1289 ggc_collect ();
1290 /* Initialize assembler name hash, in particular we want to trigger C++
1291 mangling and same body alias creation before we free DECL_ARGUMENTS
1292 used by it. */
1293 if (!seen_error ())
1294 symtab->symtab_initialize_asm_name_hash ();
1295
1296 input_location = saved_loc;
1297 }
1298
1299 /* Check declaration of the type of ALIAS for compatibility with its TARGET
1300 (which may be an ifunc resolver) and issue a diagnostic when they are
1301 not compatible according to language rules (plus a C++ extension for
1302 non-static member functions). */
1303
1304 static void
1305 maybe_diag_incompatible_alias (tree alias, tree target)
1306 {
1307 tree altype = TREE_TYPE (alias);
1308 tree targtype = TREE_TYPE (target);
1309
1310 bool ifunc = lookup_attribute ("ifunc", DECL_ATTRIBUTES (alias));
1311 tree funcptr = altype;
1312
1313 if (ifunc)
1314 {
1315 /* Handle attribute ifunc first. */
1316 if (TREE_CODE (altype) == METHOD_TYPE)
1317 {
1318 /* Set FUNCPTR to the type of the alias target. If the type
1319 is a non-static member function of class C, construct a type
1320 of an ordinary function taking C* as the first argument,
1321 followed by the member function argument list, and use it
1322 instead to check for incompatibility. This conversion is
1323 not defined by the language but an extension provided by
1324 G++. */
1325
1326 tree rettype = TREE_TYPE (altype);
1327 tree args = TYPE_ARG_TYPES (altype);
1328 altype = build_function_type (rettype, args);
1329 funcptr = altype;
1330 }
1331
1332 targtype = TREE_TYPE (targtype);
1333
1334 if (POINTER_TYPE_P (targtype))
1335 {
1336 targtype = TREE_TYPE (targtype);
1337
1338 /* Only issue Wattribute-alias for conversions to void* with
1339 -Wextra. */
1340 if (VOID_TYPE_P (targtype) && !extra_warnings)
1341 return;
1342
1343 /* Proceed to handle incompatible ifunc resolvers below. */
1344 }
1345 else
1346 {
1347 funcptr = build_pointer_type (funcptr);
1348
1349 error_at (DECL_SOURCE_LOCATION (target),
1350 "%<ifunc%> resolver for %qD must return %qT",
1351 alias, funcptr);
1352 inform (DECL_SOURCE_LOCATION (alias),
1353 "resolver indirect function declared here");
1354 return;
1355 }
1356 }
1357
1358 if ((!FUNC_OR_METHOD_TYPE_P (targtype)
1359 || (prototype_p (altype)
1360 && prototype_p (targtype)
1361 && !types_compatible_p (altype, targtype))))
1362 {
1363 /* Warn for incompatibilities. Avoid warning for functions
1364 without a prototype to make it possible to declare aliases
1365 without knowing the exact type, as libstdc++ does. */
1366 if (ifunc)
1367 {
1368 funcptr = build_pointer_type (funcptr);
1369
1370 if (warning_at (DECL_SOURCE_LOCATION (target),
1371 OPT_Wattribute_alias,
1372 "%<ifunc%> resolver for %qD should return %qT",
1373 alias, funcptr))
1374 inform (DECL_SOURCE_LOCATION (alias),
1375 "resolver indirect function declared here");
1376 }
1377 else if (warning_at (DECL_SOURCE_LOCATION (alias),
1378 OPT_Wattribute_alias,
1379 "%qD alias between functions of incompatible "
1380 "types %qT and %qT", alias, altype, targtype))
1381 inform (DECL_SOURCE_LOCATION (target),
1382 "aliased declaration here");
1383 }
1384 }
1385
1386 /* Translate the ugly representation of aliases as alias pairs into nice
1387 representation in callgraph. We don't handle all cases yet,
1388 unfortunately. */
1389
1390 static void
1391 handle_alias_pairs (void)
1392 {
1393 alias_pair *p;
1394 unsigned i;
1395
1396 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
1397 {
1398 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
1399
1400 /* Weakrefs with target not defined in current unit are easy to handle:
1401 they behave just as external variables except we need to note the
1402 alias flag to later output the weakref pseudo op into asm file. */
1403 if (!target_node
1404 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
1405 {
1406 symtab_node *node = symtab_node::get (p->decl);
1407 if (node)
1408 {
1409 node->alias_target = p->target;
1410 node->weakref = true;
1411 node->alias = true;
1412 node->transparent_alias = true;
1413 }
1414 alias_pairs->unordered_remove (i);
1415 continue;
1416 }
1417 else if (!target_node)
1418 {
1419 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
1420 symtab_node *node = symtab_node::get (p->decl);
1421 if (node)
1422 node->alias = false;
1423 alias_pairs->unordered_remove (i);
1424 continue;
1425 }
1426
1427 if (DECL_EXTERNAL (target_node->decl)
1428 /* We use local aliases for C++ thunks to force the tailcall
1429 to bind locally. This is a hack - to keep it working do
1430 the following (which is not strictly correct). */
1431 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
1432 || ! DECL_VIRTUAL_P (target_node->decl))
1433 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1434 {
1435 error ("%q+D aliased to external symbol %qE",
1436 p->decl, p->target);
1437 }
1438
1439 if (TREE_CODE (p->decl) == FUNCTION_DECL
1440 && target_node && is_a <cgraph_node *> (target_node))
1441 {
1442 maybe_diag_incompatible_alias (p->decl, target_node->decl);
1443
1444 cgraph_node *src_node = cgraph_node::get (p->decl);
1445 if (src_node && src_node->definition)
1446 src_node->reset ();
1447 cgraph_node::create_alias (p->decl, target_node->decl);
1448 alias_pairs->unordered_remove (i);
1449 }
1450 else if (VAR_P (p->decl)
1451 && target_node && is_a <varpool_node *> (target_node))
1452 {
1453 varpool_node::create_alias (p->decl, target_node->decl);
1454 alias_pairs->unordered_remove (i);
1455 }
1456 else
1457 {
1458 error ("%q+D alias between function and variable is not supported",
1459 p->decl);
1460 inform (DECL_SOURCE_LOCATION (target_node->decl),
1461 "aliased declaration here");
1462
1463 alias_pairs->unordered_remove (i);
1464 }
1465 }
1466 vec_free (alias_pairs);
1467 }
1468
1469
1470 /* Figure out what functions we want to assemble. */
1471
1472 static void
1473 mark_functions_to_output (void)
1474 {
1475 bool check_same_comdat_groups = false;
1476 cgraph_node *node;
1477
1478 if (flag_checking)
1479 FOR_EACH_FUNCTION (node)
1480 gcc_assert (!node->process);
1481
1482 FOR_EACH_FUNCTION (node)
1483 {
1484 tree decl = node->decl;
1485
1486 gcc_assert (!node->process || node->same_comdat_group);
1487 if (node->process)
1488 continue;
1489
1490 /* We need to output all local functions that are used and not
1491 always inlined, as well as those that are reachable from
1492 outside the current compilation unit. */
1493 if (node->analyzed
1494 && !node->thunk.thunk_p
1495 && !node->alias
1496 && !node->global.inlined_to
1497 && !TREE_ASM_WRITTEN (decl)
1498 && !DECL_EXTERNAL (decl))
1499 {
1500 node->process = 1;
1501 if (node->same_comdat_group)
1502 {
1503 cgraph_node *next;
1504 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
1505 next != node;
1506 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1507 if (!next->thunk.thunk_p && !next->alias
1508 && !next->comdat_local_p ())
1509 next->process = 1;
1510 }
1511 }
1512 else if (node->same_comdat_group)
1513 {
1514 if (flag_checking)
1515 check_same_comdat_groups = true;
1516 }
1517 else
1518 {
1519 /* We should've reclaimed all functions that are not needed. */
1520 if (flag_checking
1521 && !node->global.inlined_to
1522 && gimple_has_body_p (decl)
1523 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1524 are inside partition, we can end up not removing the body since we no longer
1525 have analyzed node pointing to it. */
1526 && !node->in_other_partition
1527 && !node->alias
1528 && !node->clones
1529 && !DECL_EXTERNAL (decl))
1530 {
1531 node->debug ();
1532 internal_error ("failed to reclaim unneeded function");
1533 }
1534 gcc_assert (node->global.inlined_to
1535 || !gimple_has_body_p (decl)
1536 || node->in_other_partition
1537 || node->clones
1538 || DECL_ARTIFICIAL (decl)
1539 || DECL_EXTERNAL (decl));
1540
1541 }
1542
1543 }
1544 if (flag_checking && check_same_comdat_groups)
1545 FOR_EACH_FUNCTION (node)
1546 if (node->same_comdat_group && !node->process)
1547 {
1548 tree decl = node->decl;
1549 if (!node->global.inlined_to
1550 && gimple_has_body_p (decl)
1551 /* FIXME: in an ltrans unit when the offline copy is outside a
1552 partition but inline copies are inside a partition, we can
1553 end up not removing the body since we no longer have an
1554 analyzed node pointing to it. */
1555 && !node->in_other_partition
1556 && !node->clones
1557 && !DECL_EXTERNAL (decl))
1558 {
1559 node->debug ();
1560 internal_error ("failed to reclaim unneeded function in same "
1561 "comdat group");
1562 }
1563 }
1564 }
1565
1566 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1567 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
1568
1569 Set current_function_decl and cfun to newly constructed empty function body.
1570 return basic block in the function body. */
1571
1572 basic_block
1573 init_lowered_empty_function (tree decl, bool in_ssa, profile_count count)
1574 {
1575 basic_block bb;
1576 edge e;
1577
1578 current_function_decl = decl;
1579 allocate_struct_function (decl, false);
1580 gimple_register_cfg_hooks ();
1581 init_empty_tree_cfg ();
1582 init_tree_ssa (cfun);
1583
1584 if (in_ssa)
1585 {
1586 init_ssa_operands (cfun);
1587 cfun->gimple_df->in_ssa_p = true;
1588 cfun->curr_properties |= PROP_ssa;
1589 }
1590
1591 DECL_INITIAL (decl) = make_node (BLOCK);
1592 BLOCK_SUPERCONTEXT (DECL_INITIAL (decl)) = decl;
1593
1594 DECL_SAVED_TREE (decl) = error_mark_node;
1595 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1596 | PROP_cfg | PROP_loops);
1597
1598 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
1599 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1600 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
1601
1602 /* Create BB for body of the function and connect it properly. */
1603 ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = count;
1604 ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = BB_FREQ_MAX;
1605 EXIT_BLOCK_PTR_FOR_FN (cfun)->count = count;
1606 EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency = BB_FREQ_MAX;
1607 bb = create_basic_block (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1608 bb->count = count;
1609 bb->frequency = BB_FREQ_MAX;
1610 e = make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1611 e->count = count;
1612 e->probability = profile_probability::always ();
1613 e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1614 e->count = count;
1615 e->probability = profile_probability::always ();
1616 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
1617
1618 return bb;
1619 }
1620
1621 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1622 offset indicated by VIRTUAL_OFFSET, if that is
1623 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1624 zero for a result adjusting thunk. */
1625
1626 tree
1627 thunk_adjust (gimple_stmt_iterator * bsi,
1628 tree ptr, bool this_adjusting,
1629 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1630 {
1631 gassign *stmt;
1632 tree ret;
1633
1634 if (this_adjusting
1635 && fixed_offset != 0)
1636 {
1637 stmt = gimple_build_assign
1638 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1639 ptr,
1640 fixed_offset));
1641 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1642 }
1643
1644 /* If there's a virtual offset, look up that value in the vtable and
1645 adjust the pointer again. */
1646 if (virtual_offset)
1647 {
1648 tree vtabletmp;
1649 tree vtabletmp2;
1650 tree vtabletmp3;
1651
1652 if (!vtable_entry_type)
1653 {
1654 tree vfunc_type = make_node (FUNCTION_TYPE);
1655 TREE_TYPE (vfunc_type) = integer_type_node;
1656 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1657 layout_type (vfunc_type);
1658
1659 vtable_entry_type = build_pointer_type (vfunc_type);
1660 }
1661
1662 vtabletmp =
1663 create_tmp_reg (build_pointer_type
1664 (build_pointer_type (vtable_entry_type)), "vptr");
1665
1666 /* The vptr is always at offset zero in the object. */
1667 stmt = gimple_build_assign (vtabletmp,
1668 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1669 ptr));
1670 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1671
1672 /* Form the vtable address. */
1673 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
1674 "vtableaddr");
1675 stmt = gimple_build_assign (vtabletmp2,
1676 build_simple_mem_ref (vtabletmp));
1677 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1678
1679 /* Find the entry with the vcall offset. */
1680 stmt = gimple_build_assign (vtabletmp2,
1681 fold_build_pointer_plus_loc (input_location,
1682 vtabletmp2,
1683 virtual_offset));
1684 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1685
1686 /* Get the offset itself. */
1687 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1688 "vcalloffset");
1689 stmt = gimple_build_assign (vtabletmp3,
1690 build_simple_mem_ref (vtabletmp2));
1691 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1692
1693 /* Adjust the `this' pointer. */
1694 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1695 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1696 GSI_CONTINUE_LINKING);
1697 }
1698
1699 if (!this_adjusting
1700 && fixed_offset != 0)
1701 /* Adjust the pointer by the constant. */
1702 {
1703 tree ptrtmp;
1704
1705 if (VAR_P (ptr))
1706 ptrtmp = ptr;
1707 else
1708 {
1709 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
1710 stmt = gimple_build_assign (ptrtmp, ptr);
1711 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1712 }
1713 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1714 ptrtmp, fixed_offset);
1715 }
1716
1717 /* Emit the statement and gimplify the adjustment expression. */
1718 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
1719 stmt = gimple_build_assign (ret, ptr);
1720 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1721
1722 return ret;
1723 }
1724
1725 /* Expand thunk NODE to gimple if possible.
1726 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1727 no assembler is produced.
1728 When OUTPUT_ASM_THUNK is true, also produce assembler for
1729 thunks that are not lowered. */
1730
1731 bool
1732 cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
1733 {
1734 bool this_adjusting = thunk.this_adjusting;
1735 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1736 HOST_WIDE_INT virtual_value = thunk.virtual_value;
1737 tree virtual_offset = NULL;
1738 tree alias = callees->callee->decl;
1739 tree thunk_fndecl = decl;
1740 tree a;
1741
1742 /* Instrumentation thunk is the same function with
1743 a different signature. Never need to expand it. */
1744 if (thunk.add_pointer_bounds_args)
1745 return false;
1746
1747 if (!force_gimple_thunk && this_adjusting
1748 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1749 virtual_value, alias))
1750 {
1751 const char *fnname;
1752 tree fn_block;
1753 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1754
1755 if (!output_asm_thunks)
1756 {
1757 analyzed = true;
1758 return false;
1759 }
1760
1761 if (in_lto_p)
1762 get_untransformed_body ();
1763 a = DECL_ARGUMENTS (thunk_fndecl);
1764
1765 current_function_decl = thunk_fndecl;
1766
1767 /* Ensure thunks are emitted in their correct sections. */
1768 resolve_unique_section (thunk_fndecl, 0,
1769 flag_function_sections);
1770
1771 DECL_RESULT (thunk_fndecl)
1772 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1773 RESULT_DECL, 0, restype);
1774 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1775 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1776
1777 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1778 create one. */
1779 fn_block = make_node (BLOCK);
1780 BLOCK_VARS (fn_block) = a;
1781 DECL_INITIAL (thunk_fndecl) = fn_block;
1782 BLOCK_SUPERCONTEXT (fn_block) = thunk_fndecl;
1783 allocate_struct_function (thunk_fndecl, false);
1784 init_function_start (thunk_fndecl);
1785 cfun->is_thunk = 1;
1786 insn_locations_init ();
1787 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1788 prologue_location = curr_insn_location ();
1789 assemble_start_function (thunk_fndecl, fnname);
1790
1791 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1792 fixed_offset, virtual_value, alias);
1793
1794 assemble_end_function (thunk_fndecl, fnname);
1795 insn_locations_finalize ();
1796 init_insn_lengths ();
1797 free_after_compilation (cfun);
1798 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1799 thunk.thunk_p = false;
1800 analyzed = false;
1801 }
1802 else if (stdarg_p (TREE_TYPE (thunk_fndecl)))
1803 {
1804 error ("generic thunk code fails for method %qD which uses %<...%>",
1805 thunk_fndecl);
1806 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1807 analyzed = true;
1808 return false;
1809 }
1810 else
1811 {
1812 tree restype;
1813 basic_block bb, then_bb, else_bb, return_bb;
1814 gimple_stmt_iterator bsi;
1815 int nargs = 0;
1816 tree arg;
1817 int i;
1818 tree resdecl;
1819 tree restmp = NULL;
1820 tree resbnd = NULL;
1821
1822 gcall *call;
1823 greturn *ret;
1824 bool alias_is_noreturn = TREE_THIS_VOLATILE (alias);
1825
1826 /* We may be called from expand_thunk that releses body except for
1827 DECL_ARGUMENTS. In this case force_gimple_thunk is true. */
1828 if (in_lto_p && !force_gimple_thunk)
1829 get_untransformed_body ();
1830 a = DECL_ARGUMENTS (thunk_fndecl);
1831
1832 current_function_decl = thunk_fndecl;
1833
1834 /* Ensure thunks are emitted in their correct sections. */
1835 resolve_unique_section (thunk_fndecl, 0,
1836 flag_function_sections);
1837
1838 DECL_IGNORED_P (thunk_fndecl) = 1;
1839 bitmap_obstack_initialize (NULL);
1840
1841 if (thunk.virtual_offset_p)
1842 virtual_offset = size_int (virtual_value);
1843
1844 /* Build the return declaration for the function. */
1845 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1846 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1847 {
1848 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1849 DECL_ARTIFICIAL (resdecl) = 1;
1850 DECL_IGNORED_P (resdecl) = 1;
1851 DECL_RESULT (thunk_fndecl) = resdecl;
1852 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
1853 }
1854 else
1855 resdecl = DECL_RESULT (thunk_fndecl);
1856
1857 bb = then_bb = else_bb = return_bb
1858 = init_lowered_empty_function (thunk_fndecl, true, count);
1859
1860 bsi = gsi_start_bb (bb);
1861
1862 /* Build call to the function being thunked. */
1863 if (!VOID_TYPE_P (restype)
1864 && (!alias_is_noreturn
1865 || TREE_ADDRESSABLE (restype)
1866 || TREE_CODE (TYPE_SIZE_UNIT (restype)) != INTEGER_CST))
1867 {
1868 if (DECL_BY_REFERENCE (resdecl))
1869 {
1870 restmp = gimple_fold_indirect_ref (resdecl);
1871 if (!restmp)
1872 restmp = build2 (MEM_REF,
1873 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1874 resdecl,
1875 build_int_cst (TREE_TYPE
1876 (DECL_RESULT (alias)), 0));
1877 }
1878 else if (!is_gimple_reg_type (restype))
1879 {
1880 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl)))
1881 {
1882 restmp = resdecl;
1883
1884 if (VAR_P (restmp))
1885 add_local_decl (cfun, restmp);
1886 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1887 }
1888 else
1889 restmp = create_tmp_var (restype, "retval");
1890 }
1891 else
1892 restmp = create_tmp_reg (restype, "retval");
1893 }
1894
1895 for (arg = a; arg; arg = DECL_CHAIN (arg))
1896 nargs++;
1897 auto_vec<tree> vargs (nargs);
1898 i = 0;
1899 arg = a;
1900 if (this_adjusting)
1901 {
1902 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1903 virtual_offset));
1904 arg = DECL_CHAIN (a);
1905 i = 1;
1906 }
1907
1908 if (nargs)
1909 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
1910 {
1911 tree tmp = arg;
1912 if (VECTOR_TYPE_P (TREE_TYPE (arg))
1913 || TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
1914 DECL_GIMPLE_REG_P (arg) = 1;
1915
1916 if (!is_gimple_val (arg))
1917 {
1918 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1919 (TREE_TYPE (arg)), "arg");
1920 gimple *stmt = gimple_build_assign (tmp, arg);
1921 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1922 }
1923 vargs.quick_push (tmp);
1924 }
1925 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1926 callees->call_stmt = call;
1927 gimple_call_set_from_thunk (call, true);
1928 gimple_call_set_with_bounds (call, instrumentation_clone);
1929
1930 /* Return slot optimization is always possible and in fact requred to
1931 return values with DECL_BY_REFERENCE. */
1932 if (aggregate_value_p (resdecl, TREE_TYPE (thunk_fndecl))
1933 && (!is_gimple_reg_type (TREE_TYPE (resdecl))
1934 || DECL_BY_REFERENCE (resdecl)))
1935 gimple_call_set_return_slot_opt (call, true);
1936
1937 if (restmp)
1938 {
1939 gimple_call_set_lhs (call, restmp);
1940 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1941 TREE_TYPE (TREE_TYPE (alias))));
1942 }
1943 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1944 if (!alias_is_noreturn)
1945 {
1946 if (instrumentation_clone
1947 && !DECL_BY_REFERENCE (resdecl)
1948 && restmp
1949 && BOUNDED_P (restmp))
1950 {
1951 resbnd = chkp_insert_retbnd_call (NULL, restmp, &bsi);
1952 create_edge (get_create (gimple_call_fndecl (gsi_stmt (bsi))),
1953 as_a <gcall *> (gsi_stmt (bsi)),
1954 callees->count, callees->frequency);
1955 }
1956
1957 if (restmp && !this_adjusting
1958 && (fixed_offset || virtual_offset))
1959 {
1960 tree true_label = NULL_TREE;
1961
1962 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1963 {
1964 gimple *stmt;
1965 edge e;
1966 /* If the return type is a pointer, we need to
1967 protect against NULL. We know there will be an
1968 adjustment, because that's why we're emitting a
1969 thunk. */
1970 then_bb = create_basic_block (NULL, bb);
1971 then_bb->count = count - count.apply_scale (1, 16);
1972 then_bb->frequency = BB_FREQ_MAX - BB_FREQ_MAX / 16;
1973 return_bb = create_basic_block (NULL, then_bb);
1974 return_bb->count = count;
1975 return_bb->frequency = BB_FREQ_MAX;
1976 else_bb = create_basic_block (NULL, else_bb);
1977 then_bb->count = count.apply_scale (1, 16);
1978 then_bb->frequency = BB_FREQ_MAX / 16;
1979 add_bb_to_loop (then_bb, bb->loop_father);
1980 add_bb_to_loop (return_bb, bb->loop_father);
1981 add_bb_to_loop (else_bb, bb->loop_father);
1982 remove_edge (single_succ_edge (bb));
1983 true_label = gimple_block_label (then_bb);
1984 stmt = gimple_build_cond (NE_EXPR, restmp,
1985 build_zero_cst (TREE_TYPE (restmp)),
1986 NULL_TREE, NULL_TREE);
1987 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1988 e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1989 e->probability = profile_probability::guessed_always ()
1990 .apply_scale (1, 16);
1991 e->count = count - count.apply_scale (1, 16);
1992 e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1993 e->probability = profile_probability::guessed_always ()
1994 .apply_scale (1, 16);
1995 e->count = count.apply_scale (1, 16);
1996 make_single_succ_edge (return_bb,
1997 EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1998 make_single_succ_edge (then_bb, return_bb, EDGE_FALLTHRU);
1999 e = make_edge (else_bb, return_bb, EDGE_FALLTHRU);
2000 e->probability = profile_probability::always ();
2001 e->count = count.apply_scale (1, 16);
2002 bsi = gsi_last_bb (then_bb);
2003 }
2004
2005 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
2006 fixed_offset, virtual_offset);
2007 if (true_label)
2008 {
2009 gimple *stmt;
2010 bsi = gsi_last_bb (else_bb);
2011 stmt = gimple_build_assign (restmp,
2012 build_zero_cst (TREE_TYPE (restmp)));
2013 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
2014 bsi = gsi_last_bb (return_bb);
2015 }
2016 }
2017 else
2018 gimple_call_set_tail (call, true);
2019
2020 /* Build return value. */
2021 if (!DECL_BY_REFERENCE (resdecl))
2022 ret = gimple_build_return (restmp);
2023 else
2024 ret = gimple_build_return (resdecl);
2025 gimple_return_set_retbnd (ret, resbnd);
2026
2027 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
2028 }
2029 else
2030 {
2031 gimple_call_set_tail (call, true);
2032 remove_edge (single_succ_edge (bb));
2033 }
2034
2035 cfun->gimple_df->in_ssa_p = true;
2036 profile_status_for_fn (cfun)
2037 = count.initialized_p () ? PROFILE_READ : PROFILE_GUESSED;
2038 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
2039 TREE_ASM_WRITTEN (thunk_fndecl) = false;
2040 delete_unreachable_blocks ();
2041 update_ssa (TODO_update_ssa);
2042 checking_verify_flow_info ();
2043 free_dominance_info (CDI_DOMINATORS);
2044
2045 /* Since we want to emit the thunk, we explicitly mark its name as
2046 referenced. */
2047 thunk.thunk_p = false;
2048 lowered = true;
2049 bitmap_obstack_release (NULL);
2050 }
2051 current_function_decl = NULL;
2052 set_cfun (NULL);
2053 return true;
2054 }
2055
2056 /* Assemble thunks and aliases associated to node. */
2057
2058 void
2059 cgraph_node::assemble_thunks_and_aliases (void)
2060 {
2061 cgraph_edge *e;
2062 ipa_ref *ref;
2063
2064 for (e = callers; e;)
2065 if (e->caller->thunk.thunk_p
2066 && !e->caller->global.inlined_to
2067 && !e->caller->thunk.add_pointer_bounds_args)
2068 {
2069 cgraph_node *thunk = e->caller;
2070
2071 e = e->next_caller;
2072 thunk->expand_thunk (true, false);
2073 thunk->assemble_thunks_and_aliases ();
2074 }
2075 else
2076 e = e->next_caller;
2077
2078 FOR_EACH_ALIAS (this, ref)
2079 {
2080 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
2081 if (!alias->transparent_alias)
2082 {
2083 bool saved_written = TREE_ASM_WRITTEN (decl);
2084
2085 /* Force assemble_alias to really output the alias this time instead
2086 of buffering it in same alias pairs. */
2087 TREE_ASM_WRITTEN (decl) = 1;
2088 do_assemble_alias (alias->decl,
2089 DECL_ASSEMBLER_NAME (decl));
2090 alias->assemble_thunks_and_aliases ();
2091 TREE_ASM_WRITTEN (decl) = saved_written;
2092 }
2093 }
2094 }
2095
2096 /* Expand function specified by node. */
2097
2098 void
2099 cgraph_node::expand (void)
2100 {
2101 location_t saved_loc;
2102
2103 /* We ought to not compile any inline clones. */
2104 gcc_assert (!global.inlined_to);
2105
2106 /* __RTL functions are compiled as soon as they are parsed, so don't
2107 do it again. */
2108 if (native_rtl_p ())
2109 return;
2110
2111 announce_function (decl);
2112 process = 0;
2113 gcc_assert (lowered);
2114 get_untransformed_body ();
2115
2116 /* Generate RTL for the body of DECL. */
2117
2118 timevar_push (TV_REST_OF_COMPILATION);
2119
2120 gcc_assert (symtab->global_info_ready);
2121
2122 /* Initialize the default bitmap obstack. */
2123 bitmap_obstack_initialize (NULL);
2124
2125 /* Initialize the RTL code for the function. */
2126 saved_loc = input_location;
2127 input_location = DECL_SOURCE_LOCATION (decl);
2128
2129 gcc_assert (DECL_STRUCT_FUNCTION (decl));
2130 push_cfun (DECL_STRUCT_FUNCTION (decl));
2131 init_function_start (decl);
2132
2133 gimple_register_cfg_hooks ();
2134
2135 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
2136
2137 execute_all_ipa_transforms ();
2138
2139 /* Perform all tree transforms and optimizations. */
2140
2141 /* Signal the start of passes. */
2142 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
2143
2144 execute_pass_list (cfun, g->get_passes ()->all_passes);
2145
2146 /* Signal the end of passes. */
2147 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
2148
2149 bitmap_obstack_release (&reg_obstack);
2150
2151 /* Release the default bitmap obstack. */
2152 bitmap_obstack_release (NULL);
2153
2154 /* If requested, warn about function definitions where the function will
2155 return a value (usually of some struct or union type) which itself will
2156 take up a lot of stack space. */
2157 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
2158 {
2159 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
2160
2161 if (ret_type && TYPE_SIZE_UNIT (ret_type)
2162 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
2163 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
2164 larger_than_size))
2165 {
2166 unsigned int size_as_int
2167 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
2168
2169 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
2170 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
2171 decl, size_as_int);
2172 else
2173 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
2174 decl, larger_than_size);
2175 }
2176 }
2177
2178 gimple_set_body (decl, NULL);
2179 if (DECL_STRUCT_FUNCTION (decl) == 0
2180 && !cgraph_node::get (decl)->origin)
2181 {
2182 /* Stop pointing to the local nodes about to be freed.
2183 But DECL_INITIAL must remain nonzero so we know this
2184 was an actual function definition.
2185 For a nested function, this is done in c_pop_function_context.
2186 If rest_of_compilation set this to 0, leave it 0. */
2187 if (DECL_INITIAL (decl) != 0)
2188 DECL_INITIAL (decl) = error_mark_node;
2189 }
2190
2191 input_location = saved_loc;
2192
2193 ggc_collect ();
2194 timevar_pop (TV_REST_OF_COMPILATION);
2195
2196 /* Make sure that BE didn't give up on compiling. */
2197 gcc_assert (TREE_ASM_WRITTEN (decl));
2198 if (cfun)
2199 pop_cfun ();
2200
2201 /* It would make a lot more sense to output thunks before function body to get more
2202 forward and lest backwarding jumps. This however would need solving problem
2203 with comdats. See PR48668. Also aliases must come after function itself to
2204 make one pass assemblers, like one on AIX, happy. See PR 50689.
2205 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2206 groups. */
2207 assemble_thunks_and_aliases ();
2208 release_body ();
2209 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2210 points to the dead function body. */
2211 remove_callees ();
2212 remove_all_references ();
2213 }
2214
2215 /* Node comparer that is responsible for the order that corresponds
2216 to time when a function was launched for the first time. */
2217
2218 static int
2219 node_cmp (const void *pa, const void *pb)
2220 {
2221 const cgraph_node *a = *(const cgraph_node * const *) pa;
2222 const cgraph_node *b = *(const cgraph_node * const *) pb;
2223
2224 /* Functions with time profile must be before these without profile. */
2225 if (!a->tp_first_run || !b->tp_first_run)
2226 return a->tp_first_run - b->tp_first_run;
2227
2228 return a->tp_first_run != b->tp_first_run
2229 ? b->tp_first_run - a->tp_first_run
2230 : b->order - a->order;
2231 }
2232
2233 /* Expand all functions that must be output.
2234
2235 Attempt to topologically sort the nodes so function is output when
2236 all called functions are already assembled to allow data to be
2237 propagated across the callgraph. Use a stack to get smaller distance
2238 between a function and its callees (later we may choose to use a more
2239 sophisticated algorithm for function reordering; we will likely want
2240 to use subsections to make the output functions appear in top-down
2241 order). */
2242
2243 static void
2244 expand_all_functions (void)
2245 {
2246 cgraph_node *node;
2247 cgraph_node **order = XCNEWVEC (cgraph_node *,
2248 symtab->cgraph_count);
2249 unsigned int expanded_func_count = 0, profiled_func_count = 0;
2250 int order_pos, new_order_pos = 0;
2251 int i;
2252
2253 order_pos = ipa_reverse_postorder (order);
2254 gcc_assert (order_pos == symtab->cgraph_count);
2255
2256 /* Garbage collector may remove inline clones we eliminate during
2257 optimization. So we must be sure to not reference them. */
2258 for (i = 0; i < order_pos; i++)
2259 if (order[i]->process)
2260 order[new_order_pos++] = order[i];
2261
2262 if (flag_profile_reorder_functions)
2263 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
2264
2265 for (i = new_order_pos - 1; i >= 0; i--)
2266 {
2267 node = order[i];
2268
2269 if (node->process)
2270 {
2271 expanded_func_count++;
2272 if(node->tp_first_run)
2273 profiled_func_count++;
2274
2275 if (symtab->dump_file)
2276 fprintf (symtab->dump_file,
2277 "Time profile order in expand_all_functions:%s:%d\n",
2278 node->asm_name (), node->tp_first_run);
2279 node->process = 0;
2280 node->expand ();
2281 }
2282 }
2283
2284 if (dump_file)
2285 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
2286 main_input_filename, profiled_func_count, expanded_func_count);
2287
2288 if (symtab->dump_file && flag_profile_reorder_functions)
2289 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
2290 profiled_func_count, expanded_func_count);
2291
2292 symtab->process_new_functions ();
2293 free_gimplify_stack ();
2294
2295 free (order);
2296 }
2297
2298 /* This is used to sort the node types by the cgraph order number. */
2299
2300 enum cgraph_order_sort_kind
2301 {
2302 ORDER_UNDEFINED = 0,
2303 ORDER_FUNCTION,
2304 ORDER_VAR,
2305 ORDER_VAR_UNDEF,
2306 ORDER_ASM
2307 };
2308
2309 struct cgraph_order_sort
2310 {
2311 enum cgraph_order_sort_kind kind;
2312 union
2313 {
2314 cgraph_node *f;
2315 varpool_node *v;
2316 asm_node *a;
2317 } u;
2318 };
2319
2320 /* Output all functions, variables, and asm statements in the order
2321 according to their order fields, which is the order in which they
2322 appeared in the file. This implements -fno-toplevel-reorder. In
2323 this mode we may output functions and variables which don't really
2324 need to be output. */
2325
2326 static void
2327 output_in_order (void)
2328 {
2329 int max;
2330 cgraph_order_sort *nodes;
2331 int i;
2332 cgraph_node *pf;
2333 varpool_node *pv;
2334 asm_node *pa;
2335 max = symtab->order;
2336 nodes = XCNEWVEC (cgraph_order_sort, max);
2337
2338 FOR_EACH_DEFINED_FUNCTION (pf)
2339 {
2340 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
2341 {
2342 if (!pf->no_reorder)
2343 continue;
2344 i = pf->order;
2345 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2346 nodes[i].kind = ORDER_FUNCTION;
2347 nodes[i].u.f = pf;
2348 }
2349 }
2350
2351 /* There is a similar loop in symbol_table::output_variables.
2352 Please keep them in sync. */
2353 FOR_EACH_VARIABLE (pv)
2354 {
2355 if (!pv->no_reorder)
2356 continue;
2357 if (DECL_HARD_REGISTER (pv->decl)
2358 || DECL_HAS_VALUE_EXPR_P (pv->decl))
2359 continue;
2360 i = pv->order;
2361 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2362 nodes[i].kind = pv->definition ? ORDER_VAR : ORDER_VAR_UNDEF;
2363 nodes[i].u.v = pv;
2364 }
2365
2366 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
2367 {
2368 i = pa->order;
2369 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2370 nodes[i].kind = ORDER_ASM;
2371 nodes[i].u.a = pa;
2372 }
2373
2374 /* In toplevel reorder mode we output all statics; mark them as needed. */
2375
2376 for (i = 0; i < max; ++i)
2377 if (nodes[i].kind == ORDER_VAR)
2378 nodes[i].u.v->finalize_named_section_flags ();
2379
2380 for (i = 0; i < max; ++i)
2381 {
2382 switch (nodes[i].kind)
2383 {
2384 case ORDER_FUNCTION:
2385 nodes[i].u.f->process = 0;
2386 nodes[i].u.f->expand ();
2387 break;
2388
2389 case ORDER_VAR:
2390 nodes[i].u.v->assemble_decl ();
2391 break;
2392
2393 case ORDER_VAR_UNDEF:
2394 assemble_undefined_decl (nodes[i].u.v->decl);
2395 break;
2396
2397 case ORDER_ASM:
2398 assemble_asm (nodes[i].u.a->asm_str);
2399 break;
2400
2401 case ORDER_UNDEFINED:
2402 break;
2403
2404 default:
2405 gcc_unreachable ();
2406 }
2407 }
2408
2409 symtab->clear_asm_symbols ();
2410
2411 free (nodes);
2412 }
2413
2414 static void
2415 ipa_passes (void)
2416 {
2417 gcc::pass_manager *passes = g->get_passes ();
2418
2419 set_cfun (NULL);
2420 current_function_decl = NULL;
2421 gimple_register_cfg_hooks ();
2422 bitmap_obstack_initialize (NULL);
2423
2424 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2425
2426 if (!in_lto_p)
2427 {
2428 execute_ipa_pass_list (passes->all_small_ipa_passes);
2429 if (seen_error ())
2430 return;
2431 }
2432
2433 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2434 devirtualization and other changes where removal iterate. */
2435 symtab->remove_unreachable_nodes (symtab->dump_file);
2436
2437 /* If pass_all_early_optimizations was not scheduled, the state of
2438 the cgraph will not be properly updated. Update it now. */
2439 if (symtab->state < IPA_SSA)
2440 symtab->state = IPA_SSA;
2441
2442 if (!in_lto_p)
2443 {
2444 /* Generate coverage variables and constructors. */
2445 coverage_finish ();
2446
2447 /* Process new functions added. */
2448 set_cfun (NULL);
2449 current_function_decl = NULL;
2450 symtab->process_new_functions ();
2451
2452 execute_ipa_summary_passes
2453 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2454 }
2455
2456 /* Some targets need to handle LTO assembler output specially. */
2457 if (flag_generate_lto || flag_generate_offload)
2458 targetm.asm_out.lto_start ();
2459
2460 if (!in_lto_p)
2461 {
2462 if (g->have_offload)
2463 {
2464 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
2465 lto_stream_offload_p = true;
2466 ipa_write_summaries ();
2467 lto_stream_offload_p = false;
2468 }
2469 if (flag_lto)
2470 {
2471 section_name_prefix = LTO_SECTION_NAME_PREFIX;
2472 lto_stream_offload_p = false;
2473 ipa_write_summaries ();
2474 }
2475 }
2476
2477 if (flag_generate_lto || flag_generate_offload)
2478 targetm.asm_out.lto_end ();
2479
2480 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
2481 execute_ipa_pass_list (passes->all_regular_ipa_passes);
2482 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2483
2484 bitmap_obstack_release (NULL);
2485 }
2486
2487
2488 /* Return string alias is alias of. */
2489
2490 static tree
2491 get_alias_symbol (tree decl)
2492 {
2493 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2494 return get_identifier (TREE_STRING_POINTER
2495 (TREE_VALUE (TREE_VALUE (alias))));
2496 }
2497
2498
2499 /* Weakrefs may be associated to external decls and thus not output
2500 at expansion time. Emit all necessary aliases. */
2501
2502 void
2503 symbol_table::output_weakrefs (void)
2504 {
2505 symtab_node *node;
2506 cgraph_node *cnode;
2507 FOR_EACH_SYMBOL (node)
2508 if (node->alias
2509 && !TREE_ASM_WRITTEN (node->decl)
2510 && (!(cnode = dyn_cast <cgraph_node *> (node))
2511 || !cnode->instrumented_version
2512 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
2513 && node->weakref)
2514 {
2515 tree target;
2516
2517 /* Weakrefs are special by not requiring target definition in current
2518 compilation unit. It is thus bit hard to work out what we want to
2519 alias.
2520 When alias target is defined, we need to fetch it from symtab reference,
2521 otherwise it is pointed to by alias_target. */
2522 if (node->alias_target)
2523 target = (DECL_P (node->alias_target)
2524 ? DECL_ASSEMBLER_NAME (node->alias_target)
2525 : node->alias_target);
2526 else if (node->analyzed)
2527 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
2528 else
2529 {
2530 gcc_unreachable ();
2531 target = get_alias_symbol (node->decl);
2532 }
2533 do_assemble_alias (node->decl, target);
2534 }
2535 }
2536
2537 /* Perform simple optimizations based on callgraph. */
2538
2539 void
2540 symbol_table::compile (void)
2541 {
2542 if (seen_error ())
2543 return;
2544
2545 symtab_node::checking_verify_symtab_nodes ();
2546
2547 timevar_push (TV_CGRAPHOPT);
2548 if (pre_ipa_mem_report)
2549 {
2550 fprintf (stderr, "Memory consumption before IPA\n");
2551 dump_memory_report (false);
2552 }
2553 if (!quiet_flag)
2554 fprintf (stderr, "Performing interprocedural optimizations\n");
2555 state = IPA;
2556
2557 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2558 if (flag_generate_lto || flag_generate_offload)
2559 lto_streamer_hooks_init ();
2560
2561 /* Don't run the IPA passes if there was any error or sorry messages. */
2562 if (!seen_error ())
2563 ipa_passes ();
2564
2565 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2566 if (seen_error ()
2567 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2568 {
2569 timevar_pop (TV_CGRAPHOPT);
2570 return;
2571 }
2572
2573 global_info_ready = true;
2574 if (dump_file)
2575 {
2576 fprintf (dump_file, "Optimized ");
2577 symtab->dump (dump_file);
2578 }
2579 if (post_ipa_mem_report)
2580 {
2581 fprintf (stderr, "Memory consumption after IPA\n");
2582 dump_memory_report (false);
2583 }
2584 timevar_pop (TV_CGRAPHOPT);
2585
2586 /* Output everything. */
2587 (*debug_hooks->assembly_start) ();
2588 if (!quiet_flag)
2589 fprintf (stderr, "Assembling functions:\n");
2590 symtab_node::checking_verify_symtab_nodes ();
2591
2592 bitmap_obstack_initialize (NULL);
2593 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
2594 bitmap_obstack_release (NULL);
2595 mark_functions_to_output ();
2596
2597 /* When weakref support is missing, we automatically translate all
2598 references to NODE to references to its ultimate alias target.
2599 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2600 TREE_CHAIN.
2601
2602 Set up this mapping before we output any assembler but once we are sure
2603 that all symbol renaming is done.
2604
2605 FIXME: All this uglyness can go away if we just do renaming at gimple
2606 level by physically rewritting the IL. At the moment we can only redirect
2607 calls, so we need infrastructure for renaming references as well. */
2608 #ifndef ASM_OUTPUT_WEAKREF
2609 symtab_node *node;
2610
2611 FOR_EACH_SYMBOL (node)
2612 if (node->alias
2613 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
2614 {
2615 IDENTIFIER_TRANSPARENT_ALIAS
2616 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2617 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2618 = (node->alias_target ? node->alias_target
2619 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
2620 }
2621 #endif
2622
2623 state = EXPANSION;
2624
2625 /* Output first asm statements and anything ordered. The process
2626 flag is cleared for these nodes, so we skip them later. */
2627 output_in_order ();
2628 expand_all_functions ();
2629 output_variables ();
2630
2631 process_new_functions ();
2632 state = FINISHED;
2633 output_weakrefs ();
2634
2635 if (dump_file)
2636 {
2637 fprintf (dump_file, "\nFinal ");
2638 symtab->dump (dump_file);
2639 }
2640 if (!flag_checking)
2641 return;
2642 symtab_node::verify_symtab_nodes ();
2643 /* Double check that all inline clones are gone and that all
2644 function bodies have been released from memory. */
2645 if (!seen_error ())
2646 {
2647 cgraph_node *node;
2648 bool error_found = false;
2649
2650 FOR_EACH_DEFINED_FUNCTION (node)
2651 if (node->global.inlined_to
2652 || gimple_has_body_p (node->decl))
2653 {
2654 error_found = true;
2655 node->debug ();
2656 }
2657 if (error_found)
2658 internal_error ("nodes with unreleased memory found");
2659 }
2660 }
2661
2662
2663 /* Analyze the whole compilation unit once it is parsed completely. */
2664
2665 void
2666 symbol_table::finalize_compilation_unit (void)
2667 {
2668 timevar_push (TV_CGRAPH);
2669
2670 /* If we're here there's no current function anymore. Some frontends
2671 are lazy in clearing these. */
2672 current_function_decl = NULL;
2673 set_cfun (NULL);
2674
2675 /* Do not skip analyzing the functions if there were errors, we
2676 miss diagnostics for following functions otherwise. */
2677
2678 /* Emit size functions we didn't inline. */
2679 finalize_size_functions ();
2680
2681 /* Mark alias targets necessary and emit diagnostics. */
2682 handle_alias_pairs ();
2683
2684 if (!quiet_flag)
2685 {
2686 fprintf (stderr, "\nAnalyzing compilation unit\n");
2687 fflush (stderr);
2688 }
2689
2690 if (flag_dump_passes)
2691 dump_passes ();
2692
2693 /* Gimplify and lower all functions, compute reachability and
2694 remove unreachable nodes. */
2695 analyze_functions (/*first_time=*/true);
2696
2697 /* Mark alias targets necessary and emit diagnostics. */
2698 handle_alias_pairs ();
2699
2700 /* Gimplify and lower thunks. */
2701 analyze_functions (/*first_time=*/false);
2702
2703 /* Offloading requires LTO infrastructure. */
2704 if (!in_lto_p && g->have_offload)
2705 flag_generate_offload = 1;
2706
2707 if (!seen_error ())
2708 {
2709 /* Emit early debug for reachable functions, and by consequence,
2710 locally scoped symbols. */
2711 struct cgraph_node *cnode;
2712 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cnode)
2713 (*debug_hooks->early_global_decl) (cnode->decl);
2714
2715 /* Clean up anything that needs cleaning up after initial debug
2716 generation. */
2717 (*debug_hooks->early_finish) (main_input_filename);
2718 }
2719
2720 /* Finally drive the pass manager. */
2721 compile ();
2722
2723 timevar_pop (TV_CGRAPH);
2724 }
2725
2726 /* Reset all state within cgraphunit.c so that we can rerun the compiler
2727 within the same process. For use by toplev::finalize. */
2728
2729 void
2730 cgraphunit_c_finalize (void)
2731 {
2732 gcc_assert (cgraph_new_nodes.length () == 0);
2733 cgraph_new_nodes.truncate (0);
2734
2735 vtable_entry_type = NULL;
2736 queued_nodes = &symtab_terminator;
2737
2738 first_analyzed = NULL;
2739 first_analyzed_var = NULL;
2740 }
2741
2742 /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
2743 kind of wrapper method. */
2744
2745 void
2746 cgraph_node::create_wrapper (cgraph_node *target)
2747 {
2748 /* Preserve DECL_RESULT so we get right by reference flag. */
2749 tree decl_result = DECL_RESULT (decl);
2750
2751 /* Remove the function's body but keep arguments to be reused
2752 for thunk. */
2753 release_body (true);
2754 reset ();
2755
2756 DECL_UNINLINABLE (decl) = false;
2757 DECL_RESULT (decl) = decl_result;
2758 DECL_INITIAL (decl) = NULL;
2759 allocate_struct_function (decl, false);
2760 set_cfun (NULL);
2761
2762 /* Turn alias into thunk and expand it into GIMPLE representation. */
2763 definition = true;
2764
2765 memset (&thunk, 0, sizeof (cgraph_thunk_info));
2766 thunk.thunk_p = true;
2767 create_edge (target, NULL, count, CGRAPH_FREQ_BASE);
2768 callees->can_throw_external = !TREE_NOTHROW (target->decl);
2769
2770 tree arguments = DECL_ARGUMENTS (decl);
2771
2772 while (arguments)
2773 {
2774 TREE_ADDRESSABLE (arguments) = false;
2775 arguments = TREE_CHAIN (arguments);
2776 }
2777
2778 expand_thunk (false, true);
2779
2780 /* Inline summary set-up. */
2781 analyze ();
2782 inline_analyze_function (this);
2783 }
2784
2785 #include "gt-cgraphunit.h"