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