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