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