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