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