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