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