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