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