]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphunit.c
optc-save-gen.awk: flag_fp_contract_mode is no longer speical.
[thirdparty/gcc.git] / gcc / cgraphunit.c
CommitLineData
9c8305f8 1/* Driver of optimization process
5624e564 2 Copyright (C) 2003-2015 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
78 The purpose of early optimiations is to optimize away simple
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
688010ba 86 time. These include, for example, transational 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
98 d) LTO sreaming. When doing LTO, everything important gets
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
110 is partitioned and streamed into multple 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
9c8305f8
JH
121 IP passes can produce copies of existing functoins (such
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"
163#include "tm.h"
40e23961
MC
164#include "hash-set.h"
165#include "machmode.h"
166#include "vec.h"
167#include "double-int.h"
168#include "input.h"
169#include "alias.h"
170#include "symtab.h"
171#include "wide-int.h"
172#include "inchash.h"
1c4a429a 173#include "tree.h"
40e23961 174#include "fold-const.h"
d8a2d370
DN
175#include "varasm.h"
176#include "stor-layout.h"
177#include "stringpool.h"
0878843f 178#include "output.h"
c9b9aa64 179#include "rtl.h"
60393bbc 180#include "predict.h"
60393bbc
AM
181#include "hard-reg-set.h"
182#include "input.h"
183#include "function.h"
2fb9a547
AM
184#include "basic-block.h"
185#include "tree-ssa-alias.h"
186#include "internal-fn.h"
187#include "gimple-fold.h"
188#include "gimple-expr.h"
189#include "is-a.h"
18f429e2 190#include "gimple.h"
45b0be94 191#include "gimplify.h"
5be5c238 192#include "gimple-iterator.h"
18f429e2 193#include "gimplify-me.h"
442b4905
AM
194#include "gimple-ssa.h"
195#include "tree-cfg.h"
196#include "tree-into-ssa.h"
7a300452 197#include "tree-ssa.h"
1c4a429a
JH
198#include "tree-inline.h"
199#include "langhooks.h"
1c4a429a
JH
200#include "toplev.h"
201#include "flags.h"
1c4a429a
JH
202#include "debug.h"
203#include "target.h"
dafc5b82 204#include "diagnostic.h"
b58b1157 205#include "params.h"
dc0bfe6a 206#include "intl.h"
c582198b
AM
207#include "hash-map.h"
208#include "plugin-api.h"
209#include "ipa-ref.h"
210#include "cgraph.h"
211#include "alloc-pool.h"
dd912cb8 212#include "symbol-summary.h"
57fb5341 213#include "ipa-prop.h"
726a989a 214#include "tree-iterator.h"
b4861090 215#include "tree-pass.h"
a406865a 216#include "tree-dump.h"
9c8305f8 217#include "gimple-pretty-print.h"
cd9c7bd2 218#include "output.h"
3baf459d 219#include "coverage.h"
090fa0ab 220#include "plugin.h"
632b4f8e 221#include "ipa-inline.h"
af8bca3c 222#include "ipa-utils.h"
47c79d56 223#include "lto-streamer.h"
452aa9c5 224#include "except.h"
31ee20ba 225#include "cfgloop.h"
0878843f 226#include "regset.h" /* FIXME: For reg_obstack. */
315f8c0e
DM
227#include "context.h"
228#include "pass_manager.h"
1fe37220 229#include "tree-nested.h"
45852dcc 230#include "gimplify.h"
2b5f0895 231#include "dbgcnt.h"
d5e254e1 232#include "tree-chkp.h"
1f6be682 233#include "lto-section-names.h"
ec6fe917 234#include "omp-low.h"
1bf7c324 235#include "print-tree.h"
b58b1157 236
66058468
JH
237/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
238 secondary queue used during optimization to accommodate passes that
239 may generate new functions that need to be optimized and expanded. */
31acf1bb 240vec<cgraph_node *> cgraph_new_nodes;
66058468 241
65d630d4
JH
242static void expand_all_functions (void);
243static void mark_functions_to_output (void);
877ab5e9 244static void handle_alias_pairs (void);
7dff32e6 245
6744a6ab
JH
246/* Used for vtable lookup in thunk adjusting. */
247static GTY (()) tree vtable_entry_type;
248
3dafb85c 249/* Determine if symbol declaration is needed. That is, visible to something
edb983b2
JH
250 either outside this translation unit, something magic in the system
251 configury */
252bool
3dafb85c 253symtab_node::needed_p (void)
8dafba3c 254{
ead84f73
JH
255 /* Double check that no one output the function into assembly file
256 early. */
257 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
40a7fe1e 258 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
e7d6beb0 259
3dafb85c 260 if (!definition)
edb983b2 261 return false;
a1d31187 262
edb983b2
JH
263 if (DECL_EXTERNAL (decl))
264 return false;
04f77d0f 265
edb983b2 266 /* If the user told us it is used, then it must be so. */
3dafb85c 267 if (force_output)
edb983b2
JH
268 return true;
269
270 /* ABI forced symbols are needed when they are external. */
3dafb85c 271 if (forced_by_abi && TREE_PUBLIC (decl))
edb983b2
JH
272 return true;
273
1bf7c324 274 /* Keep constructors, destructors and virtual functions. */
edb983b2
JH
275 if (TREE_CODE (decl) == FUNCTION_DECL
276 && (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl)))
277 return true;
278
279 /* Externally visible variables must be output. The exception is
280 COMDAT variables that must be output only when they are needed. */
281 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl))
8dafba3c
RH
282 return true;
283
8dafba3c
RH
284 return false;
285}
286
b4d05578
BS
287/* Head and terminator of the queue of nodes to be processed while building
288 callgraph. */
66058468 289
b4d05578
BS
290static symtab_node symtab_terminator;
291static symtab_node *queued_nodes = &symtab_terminator;
66058468 292
b4d05578 293/* Add NODE to queue starting at QUEUED_NODES.
66058468
JH
294 The queue is linked via AUX pointers and terminated by pointer to 1. */
295
296static void
5e20cdc9 297enqueue_node (symtab_node *node)
66058468 298{
67348ccc 299 if (node->aux)
66058468 300 return;
b4d05578
BS
301 gcc_checking_assert (queued_nodes);
302 node->aux = queued_nodes;
303 queued_nodes = node;
66058468
JH
304}
305
d60ab196 306/* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
f45e0ad1
JH
307 functions into callgraph in a way so they look like ordinary reachable
308 functions inserted into callgraph already at construction time. */
309
b4d05578 310void
3dafb85c 311symbol_table::process_new_functions (void)
f45e0ad1 312{
f45e0ad1 313 tree fndecl;
f45e0ad1 314
31acf1bb 315 if (!cgraph_new_nodes.exists ())
b4d05578 316 return;
31acf1bb 317
877ab5e9 318 handle_alias_pairs ();
f45e0ad1
JH
319 /* Note that this queue may grow as its being processed, as the new
320 functions may generate new ones. */
31acf1bb 321 for (unsigned i = 0; i < cgraph_new_nodes.length (); i++)
f45e0ad1 322 {
31acf1bb 323 cgraph_node *node = cgraph_new_nodes[i];
67348ccc 324 fndecl = node->decl;
3dafb85c 325 switch (state)
f45e0ad1 326 {
3dafb85c 327 case CONSTRUCTION:
f45e0ad1
JH
328 /* At construction time we just need to finalize function and move
329 it into reachable functions list. */
330
3dafb85c
ML
331 cgraph_node::finalize_function (fndecl, false);
332 call_cgraph_insertion_hooks (node);
67348ccc 333 enqueue_node (node);
f45e0ad1
JH
334 break;
335
3dafb85c
ML
336 case IPA:
337 case IPA_SSA:
17e0fc92 338 case IPA_SSA_AFTER_INLINING:
f45e0ad1
JH
339 /* When IPA optimization already started, do all essential
340 transformations that has been already performed on the whole
341 cgraph but not on this function. */
342
726a989a 343 gimple_register_cfg_hooks ();
67348ccc 344 if (!node->analyzed)
d52f5295 345 node->analyze ();
f45e0ad1 346 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
17e0fc92 347 if ((state == IPA_SSA || state == IPA_SSA_AFTER_INLINING)
7a388ee4 348 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
f7695dbf 349 g->get_passes ()->execute_early_local_passes ();
9a1e784a 350 else if (inline_summaries != NULL)
632b4f8e 351 compute_inline_parameters (node, true);
f45e0ad1
JH
352 free_dominance_info (CDI_POST_DOMINATORS);
353 free_dominance_info (CDI_DOMINATORS);
354 pop_cfun ();
82afdc6f 355 call_cgraph_insertion_hooks (node);
f45e0ad1
JH
356 break;
357
3dafb85c 358 case EXPANSION:
f45e0ad1
JH
359 /* Functions created during expansion shall be compiled
360 directly. */
257eb6e3 361 node->process = 0;
3dafb85c
ML
362 call_cgraph_insertion_hooks (node);
363 node->expand ();
f45e0ad1
JH
364 break;
365
366 default:
367 gcc_unreachable ();
368 break;
369 }
370 }
31acf1bb
ML
371
372 cgraph_new_nodes.release ();
f45e0ad1
JH
373}
374
d71cc23f
JH
375/* As an GCC extension we allow redefinition of the function. The
376 semantics when both copies of bodies differ is not well defined.
377 We replace the old body with new body so in unit at a time mode
378 we always use new body, while in normal mode we may end up with
379 old body inlined into some functions and new body expanded and
380 inlined in others.
381
382 ??? It may make more sense to use one body for inlining and other
383 body for expanding the function but this is difficult to do. */
384
e70670cf 385void
d52f5295 386cgraph_node::reset (void)
d71cc23f 387{
d52f5295 388 /* If process is set, then we have already begun whole-unit analysis.
7e8b322a
JH
389 This is *not* testing for whether we've already emitted the function.
390 That case can be sort-of legitimately seen with real function redefinition
391 errors. I would argue that the front end should never present us with
392 such a case, but don't enforce that for now. */
d52f5295 393 gcc_assert (!process);
d71cc23f
JH
394
395 /* Reset our data structures so we can analyze the function again. */
d52f5295
ML
396 memset (&local, 0, sizeof (local));
397 memset (&global, 0, sizeof (global));
398 memset (&rtl, 0, sizeof (rtl));
399 analyzed = false;
400 definition = false;
401 alias = false;
402 weakref = false;
403 cpp_implicit_alias = false;
404
405 remove_callees ();
406 remove_all_references ();
d71cc23f 407}
d853a20e 408
3dafb85c 409/* Return true when there are references to the node. */
838ff415 410
3dafb85c
ML
411bool
412symtab_node::referred_to_p (void)
838ff415 413{
3dafb85c 414 ipa_ref *ref = NULL;
838ff415 415
073a8998 416 /* See if there are any references at all. */
3dafb85c 417 if (iterate_referring (0, ref))
838ff415 418 return true;
65d630d4 419 /* For functions check also calls. */
3dafb85c 420 cgraph_node *cn = dyn_cast <cgraph_node *> (this);
5d59b5e1 421 if (cn && cn->callers)
838ff415
JH
422 return true;
423 return false;
424}
425
6b00c969 426/* DECL has been parsed. Take it, queue it, compile it at the whim of the
15682f24 427 logic in effect. If NO_COLLECT is true, then our caller cannot stand to have
6b00c969
RH
428 the garbage collector run at the moment. We would need to either create
429 a new GC context, or just not compile right now. */
1c4a429a
JH
430
431void
3dafb85c 432cgraph_node::finalize_function (tree decl, bool no_collect)
1c4a429a 433{
3dafb85c 434 cgraph_node *node = cgraph_node::get_create (decl);
1c4a429a 435
67348ccc 436 if (node->definition)
b125ad45 437 {
15682f24
MJ
438 /* Nested functions should only be defined once. */
439 gcc_assert (!DECL_CONTEXT (decl)
440 || TREE_CODE (DECL_CONTEXT (decl)) != FUNCTION_DECL);
d52f5295 441 node->reset ();
b125ad45
JH
442 node->local.redefined_extern_inline = true;
443 }
6b00c969 444
d853a20e 445 notice_global_symbol (decl);
67348ccc 446 node->definition = true;
e21aff8a 447 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
1c4a429a 448
ead84f73
JH
449 /* With -fkeep-inline-functions we are keeping all inline functions except
450 for extern inline ones. */
451 if (flag_keep_inline_functions
452 && DECL_DECLARED_INLINE_P (decl)
453 && !DECL_EXTERNAL (decl)
454 && !DECL_DISREGARD_INLINE_LIMITS (decl))
67348ccc 455 node->force_output = 1;
8dafba3c 456
ead84f73
JH
457 /* When not optimizing, also output the static functions. (see
458 PR24561), but don't do so for always_inline functions, functions
459 declared inline and nested functions. These were optimized out
460 in the original implementation and it is unclear whether we want
461 to change the behavior here. */
2bf86c84 462 if ((!opt_for_fn (decl, optimize)
67348ccc 463 && !node->cpp_implicit_alias
ead84f73
JH
464 && !DECL_DISREGARD_INLINE_LIMITS (decl)
465 && !DECL_DECLARED_INLINE_P (decl)
466 && !(DECL_CONTEXT (decl)
467 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
468 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
67348ccc 469 node->force_output = 1;
ead84f73 470
8dafba3c 471 /* If we've not yet emitted decl, tell the debug info about it. */
6b00c969 472 if (!TREE_ASM_WRITTEN (decl))
8dafba3c 473 (*debug_hooks->deferred_inline_function) (decl);
d173e685 474
902edd36
JH
475 /* Possibly warn about unused parameters. */
476 if (warn_unused_parameter)
477 do_warn_unused_parameter (decl);
7e8b322a 478
15682f24 479 if (!no_collect)
7e8b322a 480 ggc_collect ();
838ff415 481
3dafb85c
ML
482 if (symtab->state == CONSTRUCTION
483 && (node->needed_p () || node->referred_to_p ()))
67348ccc 484 enqueue_node (node);
1c4a429a
JH
485}
486
452aa9c5 487/* Add the function FNDECL to the call graph.
3dafb85c 488 Unlike finalize_function, this function is intended to be used
452aa9c5
RG
489 by middle end and allows insertion of new function at arbitrary point
490 of compilation. The function can be either in high, low or SSA form
491 GIMPLE.
492
493 The function is assumed to be reachable and have address taken (so no
494 API breaking optimizations are performed on it).
495
496 Main work done by this function is to enqueue the function for later
497 processing to avoid need the passes to be re-entrant. */
498
499void
d52f5295 500cgraph_node::add_new_function (tree fndecl, bool lowered)
452aa9c5 501{
315f8c0e 502 gcc::pass_manager *passes = g->get_passes ();
3dafb85c
ML
503 cgraph_node *node;
504 switch (symtab->state)
452aa9c5 505 {
3dafb85c
ML
506 case PARSING:
507 cgraph_node::finalize_function (fndecl, false);
66058468 508 break;
3dafb85c 509 case CONSTRUCTION:
452aa9c5 510 /* Just enqueue function to be processed at nearest occurrence. */
d52f5295 511 node = cgraph_node::get_create (fndecl);
452aa9c5
RG
512 if (lowered)
513 node->lowered = true;
31acf1bb 514 cgraph_new_nodes.safe_push (node);
452aa9c5
RG
515 break;
516
3dafb85c
ML
517 case IPA:
518 case IPA_SSA:
17e0fc92 519 case IPA_SSA_AFTER_INLINING:
3dafb85c 520 case EXPANSION:
452aa9c5
RG
521 /* Bring the function into finalized state and enqueue for later
522 analyzing and compilation. */
d52f5295 523 node = cgraph_node::get_create (fndecl);
452aa9c5 524 node->local.local = false;
67348ccc
DM
525 node->definition = true;
526 node->force_output = true;
3dafb85c 527 if (!lowered && symtab->state == EXPANSION)
452aa9c5
RG
528 {
529 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
452aa9c5
RG
530 gimple_register_cfg_hooks ();
531 bitmap_obstack_initialize (NULL);
2cbf2d95 532 execute_pass_list (cfun, passes->all_lowering_passes);
f7695dbf 533 passes->execute_early_local_passes ();
452aa9c5
RG
534 bitmap_obstack_release (NULL);
535 pop_cfun ();
452aa9c5
RG
536
537 lowered = true;
538 }
539 if (lowered)
540 node->lowered = true;
31acf1bb 541 cgraph_new_nodes.safe_push (node);
452aa9c5
RG
542 break;
543
3dafb85c 544 case FINISHED:
452aa9c5
RG
545 /* At the very end of compilation we have to do all the work up
546 to expansion. */
d52f5295 547 node = cgraph_node::create (fndecl);
452aa9c5
RG
548 if (lowered)
549 node->lowered = true;
67348ccc 550 node->definition = true;
d52f5295 551 node->analyze ();
452aa9c5 552 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
452aa9c5
RG
553 gimple_register_cfg_hooks ();
554 bitmap_obstack_initialize (NULL);
555 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
f7695dbf 556 g->get_passes ()->execute_early_local_passes ();
452aa9c5 557 bitmap_obstack_release (NULL);
452aa9c5 558 pop_cfun ();
3dafb85c 559 node->expand ();
452aa9c5
RG
560 break;
561
562 default:
563 gcc_unreachable ();
564 }
565
566 /* Set a personality if required and we already passed EH lowering. */
567 if (lowered
568 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
569 == eh_personality_lang))
570 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
571}
572
e767b5be 573/* Analyze the function scheduled to be output. */
d52f5295
ML
574void
575cgraph_node::analyze (void)
e767b5be 576{
d52f5295 577 tree decl = this->decl;
9c8305f8
JH
578 location_t saved_loc = input_location;
579 input_location = DECL_SOURCE_LOCATION (decl);
e767b5be 580
d52f5295 581 if (thunk.thunk_p)
c47d0034 582 {
d52f5295
ML
583 create_edge (cgraph_node::get (thunk.alias),
584 NULL, 0, CGRAPH_FREQ_BASE);
585 if (!expand_thunk (false, false))
afdec9bd 586 {
d52f5295 587 thunk.alias = NULL;
afdec9bd
JH
588 return;
589 }
d52f5295 590 thunk.alias = NULL;
c47d0034 591 }
d52f5295
ML
592 if (alias)
593 resolve_alias (cgraph_node::get (alias_target));
594 else if (dispatcher_function)
3649b9b7
ST
595 {
596 /* Generate the dispatcher body of multi-versioned functions. */
3dafb85c 597 cgraph_function_version_info *dispatcher_version_info
d52f5295 598 = function_version ();
3649b9b7
ST
599 if (dispatcher_version_info != NULL
600 && (dispatcher_version_info->dispatcher_resolver
601 == NULL_TREE))
602 {
603 tree resolver = NULL_TREE;
604 gcc_assert (targetm.generate_version_dispatcher_body);
d52f5295 605 resolver = targetm.generate_version_dispatcher_body (this);
3649b9b7
ST
606 gcc_assert (resolver != NULL_TREE);
607 }
608 }
c47d0034
JH
609 else
610 {
c47d0034 611 push_cfun (DECL_STRUCT_FUNCTION (decl));
a406865a 612
d52f5295 613 assign_assembler_name_if_neeeded (decl);
0e0a1359 614
c47d0034
JH
615 /* Make sure to gimplify bodies only once. During analyzing a
616 function we lower it, which will require gimplified nested
617 functions, so we can end up here with an already gimplified
618 body. */
355a7673 619 if (!gimple_has_body_p (decl))
c47d0034
JH
620 gimplify_function_tree (decl);
621 dump_function (TDI_generic, decl);
a406865a 622
26eb69c6 623 /* Lower the function. */
d52f5295 624 if (!lowered)
26eb69c6 625 {
d52f5295
ML
626 if (nested)
627 lower_nested_functions (decl);
628 gcc_assert (!nested);
26eb69c6
RG
629
630 gimple_register_cfg_hooks ();
631 bitmap_obstack_initialize (NULL);
2cbf2d95 632 execute_pass_list (cfun, g->get_passes ()->all_lowering_passes);
26eb69c6
RG
633 free_dominance_info (CDI_POST_DOMINATORS);
634 free_dominance_info (CDI_DOMINATORS);
635 compact_blocks ();
636 bitmap_obstack_release (NULL);
d52f5295 637 lowered = true;
26eb69c6
RG
638 }
639
c47d0034
JH
640 pop_cfun ();
641 }
d52f5295 642 analyzed = true;
e767b5be 643
9c8305f8 644 input_location = saved_loc;
e767b5be
JH
645}
646
39e2db00
JH
647/* C++ frontend produce same body aliases all over the place, even before PCH
648 gets streamed out. It relies on us linking the aliases with their function
649 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
650 first produce aliases without links, but once C++ FE is sure he won't sream
651 PCH we build the links via this function. */
652
653void
3dafb85c 654symbol_table::process_same_body_aliases (void)
39e2db00 655{
5e20cdc9 656 symtab_node *node;
40a7fe1e 657 FOR_EACH_SYMBOL (node)
67348ccc 658 if (node->cpp_implicit_alias && !node->analyzed)
d52f5295
ML
659 node->resolve_alias
660 (TREE_CODE (node->alias_target) == VAR_DECL
9041d2e6 661 ? (symtab_node *)varpool_node::get_create (node->alias_target)
d52f5295 662 : (symtab_node *)cgraph_node::get_create (node->alias_target));
40a7fe1e 663 cpp_implicit_aliases_done = true;
39e2db00
JH
664}
665
768e3c60
RG
666/* Process attributes common for vars and functions. */
667
668static void
7861b648 669process_common_attributes (symtab_node *node, tree decl)
768e3c60
RG
670{
671 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
672
673 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
674 {
675 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
676 "%<weakref%> attribute should be accompanied with"
677 " an %<alias%> attribute");
678 DECL_WEAK (decl) = 0;
779d4b91
JH
679 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
680 DECL_ATTRIBUTES (decl));
768e3c60 681 }
7861b648
AK
682
683 if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
684 node->no_reorder = 1;
768e3c60
RG
685}
686
386b46cf
JH
687/* Look for externally_visible and used attributes and mark cgraph nodes
688 accordingly.
689
690 We cannot mark the nodes at the point the attributes are processed (in
691 handle_*_attribute) because the copy of the declarations available at that
692 point may not be canonical. For example, in:
693
694 void f();
695 void f() __attribute__((used));
696
697 the declaration we see in handle_used_attribute will be the second
698 declaration -- but the front end will subsequently merge that declaration
699 with the original declaration and discard the second declaration.
700
3dafb85c 701 Furthermore, we can't mark these nodes in finalize_function because:
386b46cf
JH
702
703 void f() {}
704 void f() __attribute__((externally_visible));
705
706 is valid.
707
708 So, we walk the nodes at the end of the translation unit, applying the
709 attributes at that point. */
710
711static void
3dafb85c 712process_function_and_variable_attributes (cgraph_node *first,
2c8326a5 713 varpool_node *first_var)
386b46cf 714{
3dafb85c 715 cgraph_node *node;
2c8326a5 716 varpool_node *vnode;
386b46cf 717
3dafb85c
ML
718 for (node = symtab->first_function (); node != first;
719 node = symtab->next_function (node))
386b46cf 720 {
67348ccc 721 tree decl = node->decl;
b42186f1 722 if (DECL_PRESERVE_P (decl))
d52f5295 723 node->mark_force_output ();
9d602c59 724 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
386b46cf 725 {
67348ccc
DM
726 if (! TREE_PUBLIC (node->decl))
727 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
c5d75364
MLI
728 "%<externally_visible%>"
729 " attribute have effect only on public objects");
386b46cf 730 }
779d4b91 731 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
67348ccc 732 && (node->definition && !node->alias))
779d4b91 733 {
67348ccc 734 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
779d4b91
JH
735 "%<weakref%> attribute ignored"
736 " because function is defined");
737 DECL_WEAK (decl) = 0;
738 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
739 DECL_ATTRIBUTES (decl));
740 }
c9fc06dc
CB
741
742 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
743 && !DECL_DECLARED_INLINE_P (decl)
744 /* redefining extern inline function makes it DECL_UNINLINABLE. */
745 && !DECL_UNINLINABLE (decl))
746 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
747 "always_inline function might not be inlinable");
748
7861b648 749 process_common_attributes (node, decl);
386b46cf 750 }
3dafb85c
ML
751 for (vnode = symtab->first_variable (); vnode != first_var;
752 vnode = symtab->next_variable (vnode))
386b46cf 753 {
67348ccc 754 tree decl = vnode->decl;
6649df51 755 if (DECL_EXTERNAL (decl)
6a6dac52 756 && DECL_INITIAL (decl))
9041d2e6 757 varpool_node::finalize_decl (decl);
b42186f1 758 if (DECL_PRESERVE_P (decl))
67348ccc 759 vnode->force_output = true;
9d602c59 760 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
386b46cf 761 {
67348ccc
DM
762 if (! TREE_PUBLIC (vnode->decl))
763 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
c5d75364
MLI
764 "%<externally_visible%>"
765 " attribute have effect only on public objects");
386b46cf 766 }
779d4b91 767 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
67348ccc 768 && vnode->definition
779d4b91
JH
769 && DECL_INITIAL (decl))
770 {
67348ccc 771 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
779d4b91
JH
772 "%<weakref%> attribute ignored"
773 " because variable is initialized");
774 DECL_WEAK (decl) = 0;
775 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
776 DECL_ATTRIBUTES (decl));
777 }
7861b648 778 process_common_attributes (vnode, decl);
386b46cf
JH
779 }
780}
781
66058468
JH
782/* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
783 middle end to output the variable to asm file, if needed or externally
784 visible. */
785
786void
9041d2e6 787varpool_node::finalize_decl (tree decl)
66058468 788{
9041d2e6 789 varpool_node *node = varpool_node::get_create (decl);
66058468 790
387df871 791 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
66058468 792
67348ccc 793 if (node->definition)
66058468
JH
794 return;
795 notice_global_symbol (decl);
67348ccc 796 node->definition = true;
66058468
JH
797 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
798 /* Traditionally we do not eliminate static variables when not
799 optimizing and when not doing toplevel reoder. */
7861b648
AK
800 || node->no_reorder
801 || ((!flag_toplevel_reorder
802 && !DECL_COMDAT (node->decl)
803 && !DECL_ARTIFICIAL (node->decl))))
67348ccc 804 node->force_output = true;
66058468 805
3dafb85c
ML
806 if (symtab->state == CONSTRUCTION
807 && (node->needed_p () || node->referred_to_p ()))
67348ccc 808 enqueue_node (node);
3dafb85c 809 if (symtab->state >= IPA_SSA)
9041d2e6 810 node->analyze ();
0d6bf48c
JH
811 /* Some frontends produce various interface variables after compilation
812 finished. */
3dafb85c
ML
813 if (symtab->state == FINISHED
814 || (!flag_toplevel_reorder
815 && symtab->state == EXPANSION))
9041d2e6 816 node->assemble_decl ();
d5e254e1
IE
817
818 if (DECL_INITIAL (decl))
819 chkp_register_var_initializer (decl);
66058468
JH
820}
821
e18412fc
JH
822/* EDGE is an polymorphic call. Mark all possible targets as reachable
823 and if there is only one target, perform trivial devirtualization.
824 REACHABLE_CALL_TARGETS collects target lists we already walked to
825 avoid udplicate work. */
826
827static void
6e2830c3 828walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
3dafb85c 829 cgraph_edge *edge)
e18412fc
JH
830{
831 unsigned int i;
832 void *cache_token;
833 bool final;
834 vec <cgraph_node *>targets
835 = possible_polymorphic_call_targets
836 (edge, &final, &cache_token);
837
6e2830c3 838 if (!reachable_call_targets->add (cache_token))
e18412fc 839 {
3dafb85c 840 if (symtab->dump_file)
e18412fc 841 dump_possible_polymorphic_call_targets
3dafb85c 842 (symtab->dump_file, edge);
e18412fc 843
c3284718 844 for (i = 0; i < targets.length (); i++)
e18412fc
JH
845 {
846 /* Do not bother to mark virtual methods in anonymous namespace;
847 either we will find use of virtual table defining it, or it is
848 unused. */
67348ccc 849 if (targets[i]->definition
e18412fc 850 && TREE_CODE
67348ccc 851 (TREE_TYPE (targets[i]->decl))
e18412fc
JH
852 == METHOD_TYPE
853 && !type_in_anonymous_namespace_p
854 (method_class_type
67348ccc
DM
855 (TREE_TYPE (targets[i]->decl))))
856 enqueue_node (targets[i]);
e18412fc
JH
857 }
858 }
859
860 /* Very trivial devirtualization; when the type is
861 final or anonymous (so we know all its derivation)
862 and there is only one possible virtual call target,
863 make the edge direct. */
864 if (final)
865 {
2b5f0895 866 if (targets.length () <= 1 && dbg_cnt (devirt))
e18412fc 867 {
3462aa02
JH
868 cgraph_node *target;
869 if (targets.length () == 1)
870 target = targets[0];
871 else
d52f5295
ML
872 target = cgraph_node::create
873 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
3462aa02 874
3dafb85c 875 if (symtab->dump_file)
e18412fc 876 {
3dafb85c 877 fprintf (symtab->dump_file,
e18412fc 878 "Devirtualizing call: ");
3dafb85c 879 print_gimple_stmt (symtab->dump_file,
e18412fc
JH
880 edge->call_stmt, 0,
881 TDF_SLIM);
882 }
2b5f0895
XDL
883 if (dump_enabled_p ())
884 {
807b7d62 885 location_t locus = gimple_location_safe (edge->call_stmt);
2b5f0895
XDL
886 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
887 "devirtualizing call in %s to %s\n",
888 edge->caller->name (), target->name ());
889 }
890
3dafb85c
ML
891 edge->make_direct (target);
892 edge->redirect_call_stmt_to_callee ();
d5e254e1
IE
893
894 /* Call to __builtin_unreachable shouldn't be instrumented. */
895 if (!targets.length ())
896 gimple_call_set_with_bounds (edge->call_stmt, false);
897
3dafb85c 898 if (symtab->dump_file)
e18412fc 899 {
3dafb85c 900 fprintf (symtab->dump_file,
e18412fc 901 "Devirtualized as: ");
3dafb85c 902 print_gimple_stmt (symtab->dump_file,
e18412fc
JH
903 edge->call_stmt, 0,
904 TDF_SLIM);
905 }
906 }
907 }
908}
909
5d59b5e1 910
66058468
JH
911/* Discover all functions and variables that are trivially needed, analyze
912 them as well as all functions and variables referred by them */
3edf64aa
DM
913static cgraph_node *first_analyzed;
914static varpool_node *first_analyzed_var;
1c4a429a 915
151e6f24 916static void
e70670cf 917analyze_functions (void)
1c4a429a 918{
cd9c7bd2 919 /* Keep track of already processed nodes when called multiple times for
aabcd309 920 intermodule optimization. */
3dafb85c 921 cgraph_node *first_handled = first_analyzed;
2c8326a5 922 varpool_node *first_handled_var = first_analyzed_var;
6e2830c3 923 hash_set<void *> reachable_call_targets;
66058468 924
5e20cdc9
DM
925 symtab_node *node;
926 symtab_node *next;
66058468 927 int i;
3dafb85c 928 ipa_ref *ref;
66058468 929 bool changed = true;
4f90d3e0 930 location_t saved_loc = input_location;
1c4a429a 931
1389294c 932 bitmap_obstack_initialize (NULL);
3dafb85c 933 symtab->state = CONSTRUCTION;
4f90d3e0 934 input_location = UNKNOWN_LOCATION;
1c4a429a 935
40a7fe1e
JH
936 /* Ugly, but the fixup can not happen at a time same body alias is created;
937 C++ FE is confused about the COMDAT groups being right. */
3dafb85c 938 if (symtab->cpp_implicit_aliases_done)
40a7fe1e 939 FOR_EACH_SYMBOL (node)
67348ccc 940 if (node->cpp_implicit_alias)
d52f5295 941 node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
2bf86c84 942 build_type_inheritance_graph ();
40a7fe1e 943
66058468
JH
944 /* Analysis adds static variables that in turn adds references to new functions.
945 So we need to iterate the process until it stabilize. */
946 while (changed)
1c4a429a 947 {
66058468
JH
948 changed = false;
949 process_function_and_variable_attributes (first_analyzed,
950 first_analyzed_var);
951
952 /* First identify the trivially needed symbols. */
3dafb85c 953 for (node = symtab->first_symbol ();
67348ccc
DM
954 node != first_analyzed
955 && node != first_analyzed_var; node = node->next)
d71cc23f 956 {
d67ff7b7
JM
957 /* Convert COMDAT group designators to IDENTIFIER_NODEs. */
958 node->get_comdat_group_id ();
3dafb85c 959 if (node->needed_p ())
66058468
JH
960 {
961 enqueue_node (node);
3dafb85c
ML
962 if (!changed && symtab->dump_file)
963 fprintf (symtab->dump_file, "Trivially needed symbols:");
66058468 964 changed = true;
3dafb85c
ML
965 if (symtab->dump_file)
966 fprintf (symtab->dump_file, " %s", node->asm_name ());
967 if (!changed && symtab->dump_file)
968 fprintf (symtab->dump_file, "\n");
66058468 969 }
67348ccc
DM
970 if (node == first_analyzed
971 || node == first_analyzed_var)
66058468 972 break;
d71cc23f 973 }
3dafb85c
ML
974 symtab->process_new_functions ();
975 first_analyzed_var = symtab->first_variable ();
976 first_analyzed = symtab->first_function ();
cd4dea62 977
3dafb85c
ML
978 if (changed && symtab->dump_file)
979 fprintf (symtab->dump_file, "\n");
8dafba3c 980
66058468
JH
981 /* Lower representation, build callgraph edges and references for all trivially
982 needed symbols and all symbols referred by them. */
b4d05578 983 while (queued_nodes != &symtab_terminator)
b66887e4 984 {
66058468 985 changed = true;
b4d05578
BS
986 node = queued_nodes;
987 queued_nodes = (symtab_node *)queued_nodes->aux;
7de90a6c 988 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
67348ccc 989 if (cnode && cnode->definition)
66058468 990 {
3dafb85c 991 cgraph_edge *edge;
67348ccc 992 tree decl = cnode->decl;
66058468 993
5d59b5e1
LC
994 /* ??? It is possible to create extern inline function
995 and later using weak alias attribute to kill its body.
996 See gcc.c-torture/compile/20011119-1.c */
66058468 997 if (!DECL_STRUCT_FUNCTION (decl)
67348ccc 998 && !cnode->alias
3649b9b7
ST
999 && !cnode->thunk.thunk_p
1000 && !cnode->dispatcher_function)
66058468 1001 {
d52f5295 1002 cnode->reset ();
66058468
JH
1003 cnode->local.redefined_extern_inline = true;
1004 continue;
1005 }
b66887e4 1006
67348ccc 1007 if (!cnode->analyzed)
d52f5295 1008 cnode->analyze ();
6b20f353 1009
66058468 1010 for (edge = cnode->callees; edge; edge = edge->next_callee)
1bf7c324
JH
1011 if (edge->callee->definition
1012 && (!DECL_EXTERNAL (edge->callee->decl)
1013 /* When not optimizing, do not try to analyze extern
1014 inline functions. Doing so is pointless. */
1015 || opt_for_fn (edge->callee->decl, optimize)
1016 /* Weakrefs needs to be preserved. */
1017 || edge->callee->alias
1018 /* always_inline functions are inlined aven at -O0. */
1019 || lookup_attribute
1020 ("always_inline",
1021 DECL_ATTRIBUTES (edge->callee->decl))
1022 /* Multiversioned functions needs the dispatcher to
1023 be produced locally even for extern functions. */
1024 || edge->callee->function_version ()))
67348ccc 1025 enqueue_node (edge->callee);
2bf86c84
JH
1026 if (opt_for_fn (cnode->decl, optimize)
1027 && opt_for_fn (cnode->decl, flag_devirtualize))
eefe9a99 1028 {
3dafb85c 1029 cgraph_edge *next;
e18412fc
JH
1030
1031 for (edge = cnode->indirect_calls; edge; edge = next)
c4be6568
JH
1032 {
1033 next = edge->next_callee;
1034 if (edge->indirect_info->polymorphic)
6e2830c3 1035 walk_polymorphic_call_targets (&reachable_call_targets,
e18412fc 1036 edge);
c4be6568 1037 }
eefe9a99 1038 }
66058468 1039
5d59b5e1
LC
1040 /* If decl is a clone of an abstract function,
1041 mark that abstract function so that we don't release its body.
1042 The DECL_INITIAL() of that abstract function declaration
1043 will be later needed to output debug info. */
66058468
JH
1044 if (DECL_ABSTRACT_ORIGIN (decl))
1045 {
3dafb85c 1046 cgraph_node *origin_node
c84495c0 1047 = cgraph_node::get_create (DECL_ABSTRACT_ORIGIN (decl));
c0c123ef 1048 origin_node->used_as_abstract_origin = true;
66058468 1049 }
66058468 1050 }
5d59b5e1
LC
1051 else
1052 {
7de90a6c 1053 varpool_node *vnode = dyn_cast <varpool_node *> (node);
67348ccc 1054 if (vnode && vnode->definition && !vnode->analyzed)
9041d2e6 1055 vnode->analyze ();
5d59b5e1 1056 }
66058468 1057
67348ccc 1058 if (node->same_comdat_group)
66058468 1059 {
5e20cdc9 1060 symtab_node *next;
67348ccc 1061 for (next = node->same_comdat_group;
66058468 1062 next != node;
67348ccc 1063 next = next->same_comdat_group)
1bf7c324
JH
1064 if (!next->comdat_local_p ())
1065 enqueue_node (next);
66058468 1066 }
d122681a 1067 for (i = 0; node->iterate_reference (i, ref); i++)
1bf7c324
JH
1068 if (ref->referred->definition
1069 && (!DECL_EXTERNAL (ref->referred->decl)
1070 || ((TREE_CODE (ref->referred->decl) != FUNCTION_DECL
1071 && optimize)
1072 || (TREE_CODE (ref->referred->decl) == FUNCTION_DECL
1073 && opt_for_fn (ref->referred->decl, optimize))
38c1b72f 1074 || node->alias
1bf7c324 1075 || ref->referred->alias)))
66058468 1076 enqueue_node (ref->referred);
3dafb85c 1077 symtab->process_new_functions ();
66058468 1078 }
1c4a429a 1079 }
2bf86c84 1080 update_type_inheritance_graph ();
8dafba3c 1081
564738df 1082 /* Collect entry points to the unit. */
3dafb85c 1083 if (symtab->dump_file)
1668aabc 1084 {
3dafb85c
ML
1085 fprintf (symtab->dump_file, "\n\nInitial ");
1086 symtab_node::dump_table (symtab->dump_file);
1668aabc 1087 }
7660e67e 1088
3dafb85c
ML
1089 if (symtab->dump_file)
1090 fprintf (symtab->dump_file, "\nRemoving unused symbols:");
1c4a429a 1091
3dafb85c 1092 for (node = symtab->first_symbol ();
67348ccc
DM
1093 node != first_handled
1094 && node != first_handled_var; node = next)
1c4a429a 1095 {
67348ccc 1096 next = node->next;
3dafb85c 1097 if (!node->aux && !node->referred_to_p ())
1c4a429a 1098 {
3dafb85c
ML
1099 if (symtab->dump_file)
1100 fprintf (symtab->dump_file, " %s", node->name ());
d52f5295 1101 node->remove ();
d71cc23f 1102 continue;
1c4a429a 1103 }
7de90a6c 1104 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
66058468 1105 {
67348ccc 1106 tree decl = node->decl;
66058468 1107
67348ccc
DM
1108 if (cnode->definition && !gimple_has_body_p (decl)
1109 && !cnode->alias
66058468 1110 && !cnode->thunk.thunk_p)
d52f5295 1111 cnode->reset ();
66058468 1112
67348ccc
DM
1113 gcc_assert (!cnode->definition || cnode->thunk.thunk_p
1114 || cnode->alias
66058468 1115 || gimple_has_body_p (decl));
67348ccc 1116 gcc_assert (cnode->analyzed == cnode->definition);
66058468 1117 }
67348ccc 1118 node->aux = NULL;
1c4a429a 1119 }
67348ccc
DM
1120 for (;node; node = node->next)
1121 node->aux = NULL;
3dafb85c
ML
1122 first_analyzed = symtab->first_function ();
1123 first_analyzed_var = symtab->first_variable ();
1124 if (symtab->dump_file)
7d82fe7c 1125 {
3dafb85c
ML
1126 fprintf (symtab->dump_file, "\n\nReclaimed ");
1127 symtab_node::dump_table (symtab->dump_file);
7d82fe7c 1128 }
1389294c 1129 bitmap_obstack_release (NULL);
1c4a429a 1130 ggc_collect ();
d352b245
JH
1131 /* Initialize assembler name hash, in particular we want to trigger C++
1132 mangling and same body alias creation before we free DECL_ARGUMENTS
1133 used by it. */
1134 if (!seen_error ())
3dafb85c 1135 symtab->symtab_initialize_asm_name_hash ();
4f90d3e0
JH
1136
1137 input_location = saved_loc;
151e6f24
JH
1138}
1139
85ce9375
JH
1140/* Translate the ugly representation of aliases as alias pairs into nice
1141 representation in callgraph. We don't handle all cases yet,
4f219961 1142 unfortunately. */
85ce9375
JH
1143
1144static void
1145handle_alias_pairs (void)
1146{
1147 alias_pair *p;
1148 unsigned i;
85ce9375 1149
9771b263 1150 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
85ce9375 1151 {
3dafb85c 1152 symtab_node *target_node = symtab_node::get_for_asmname (p->target);
38e55ac9 1153
4f219961
EB
1154 /* Weakrefs with target not defined in current unit are easy to handle:
1155 they behave just as external variables except we need to note the
1156 alias flag to later output the weakref pseudo op into asm file. */
1157 if (!target_node
1158 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
25e2c40d 1159 {
d52f5295 1160 symtab_node *node = symtab_node::get (p->decl);
40a7fe1e 1161 if (node)
e01c7cca 1162 {
67348ccc
DM
1163 node->alias_target = p->target;
1164 node->weakref = true;
1165 node->alias = true;
e01c7cca 1166 }
9771b263 1167 alias_pairs->unordered_remove (i);
38e55ac9 1168 continue;
25e2c40d 1169 }
38e55ac9 1170 else if (!target_node)
85ce9375 1171 {
38e55ac9 1172 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
d52f5295 1173 symtab_node *node = symtab_node::get (p->decl);
4f219961 1174 if (node)
67348ccc 1175 node->alias = false;
9771b263 1176 alias_pairs->unordered_remove (i);
38e55ac9
JH
1177 continue;
1178 }
1179
67348ccc 1180 if (DECL_EXTERNAL (target_node->decl)
07250f0e
JH
1181 /* We use local aliases for C++ thunks to force the tailcall
1182 to bind locally. This is a hack - to keep it working do
1183 the following (which is not strictly correct). */
abbb94e6 1184 && (TREE_CODE (target_node->decl) != FUNCTION_DECL
67348ccc 1185 || ! DECL_VIRTUAL_P (target_node->decl))
07250f0e
JH
1186 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1187 {
1188 error ("%q+D aliased to external symbol %qE",
1189 p->decl, p->target);
1190 }
1191
38e55ac9 1192 if (TREE_CODE (p->decl) == FUNCTION_DECL
7de90a6c 1193 && target_node && is_a <cgraph_node *> (target_node))
38e55ac9 1194 {
3dafb85c 1195 cgraph_node *src_node = cgraph_node::get (p->decl);
67348ccc 1196 if (src_node && src_node->definition)
d52f5295
ML
1197 src_node->reset ();
1198 cgraph_node::create_alias (p->decl, target_node->decl);
9771b263 1199 alias_pairs->unordered_remove (i);
38e55ac9
JH
1200 }
1201 else if (TREE_CODE (p->decl) == VAR_DECL
7de90a6c 1202 && target_node && is_a <varpool_node *> (target_node))
38e55ac9 1203 {
9041d2e6 1204 varpool_node::create_alias (p->decl, target_node->decl);
9771b263 1205 alias_pairs->unordered_remove (i);
38e55ac9
JH
1206 }
1207 else
1208 {
1209 error ("%q+D alias in between function and variable is not supported",
1210 p->decl);
1211 warning (0, "%q+D aliased declaration",
67348ccc 1212 target_node->decl);
9771b263 1213 alias_pairs->unordered_remove (i);
85ce9375
JH
1214 }
1215 }
9771b263 1216 vec_free (alias_pairs);
85ce9375
JH
1217}
1218
5f1a9ebb 1219
1c4a429a
JH
1220/* Figure out what functions we want to assemble. */
1221
1222static void
65d630d4 1223mark_functions_to_output (void)
1c4a429a 1224{
3dafb85c 1225 cgraph_node *node;
b66887e4
JJ
1226#ifdef ENABLE_CHECKING
1227 bool check_same_comdat_groups = false;
1228
65c70e6b 1229 FOR_EACH_FUNCTION (node)
b66887e4
JJ
1230 gcc_assert (!node->process);
1231#endif
1c4a429a 1232
65c70e6b 1233 FOR_EACH_FUNCTION (node)
1c4a429a 1234 {
67348ccc 1235 tree decl = node->decl;
c22cacf3 1236
67348ccc 1237 gcc_assert (!node->process || node->same_comdat_group);
b66887e4
JJ
1238 if (node->process)
1239 continue;
b58b1157 1240
7660e67e
SB
1241 /* We need to output all local functions that are used and not
1242 always inlined, as well as those that are reachable from
1243 outside the current compilation unit. */
67348ccc 1244 if (node->analyzed
c47d0034 1245 && !node->thunk.thunk_p
67348ccc 1246 && !node->alias
18c6ada9 1247 && !node->global.inlined_to
6de9cd9a 1248 && !TREE_ASM_WRITTEN (decl)
1c4a429a 1249 && !DECL_EXTERNAL (decl))
b66887e4
JJ
1250 {
1251 node->process = 1;
67348ccc 1252 if (node->same_comdat_group)
b66887e4 1253 {
3dafb85c 1254 cgraph_node *next;
d52f5295 1255 for (next = dyn_cast<cgraph_node *> (node->same_comdat_group);
b66887e4 1256 next != node;
d52f5295 1257 next = dyn_cast<cgraph_node *> (next->same_comdat_group))
1f26ac87 1258 if (!next->thunk.thunk_p && !next->alias
d52f5295 1259 && !next->comdat_local_p ())
c47d0034 1260 next->process = 1;
b66887e4
JJ
1261 }
1262 }
67348ccc 1263 else if (node->same_comdat_group)
b66887e4
JJ
1264 {
1265#ifdef ENABLE_CHECKING
1266 check_same_comdat_groups = true;
1267#endif
1268 }
341c100f 1269 else
1a2caa7a
NS
1270 {
1271 /* We should've reclaimed all functions that are not needed. */
1272#ifdef ENABLE_CHECKING
726a989a 1273 if (!node->global.inlined_to
39ecc018 1274 && gimple_has_body_p (decl)
a837268b
JH
1275 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1276 are inside partition, we can end up not removing the body since we no longer
1277 have analyzed node pointing to it. */
67348ccc
DM
1278 && !node->in_other_partition
1279 && !node->alias
387df871 1280 && !node->clones
1a2caa7a
NS
1281 && !DECL_EXTERNAL (decl))
1282 {
d52f5295 1283 node->debug ();
1a2caa7a
NS
1284 internal_error ("failed to reclaim unneeded function");
1285 }
1286#endif
726a989a 1287 gcc_assert (node->global.inlined_to
39ecc018 1288 || !gimple_has_body_p (decl)
67348ccc 1289 || node->in_other_partition
6649df51
JH
1290 || node->clones
1291 || DECL_ARTIFICIAL (decl)
1a2caa7a
NS
1292 || DECL_EXTERNAL (decl));
1293
1294 }
c22cacf3 1295
18d13f34 1296 }
b66887e4
JJ
1297#ifdef ENABLE_CHECKING
1298 if (check_same_comdat_groups)
65c70e6b 1299 FOR_EACH_FUNCTION (node)
67348ccc 1300 if (node->same_comdat_group && !node->process)
b66887e4 1301 {
67348ccc 1302 tree decl = node->decl;
b66887e4
JJ
1303 if (!node->global.inlined_to
1304 && gimple_has_body_p (decl)
7cbf224d
RG
1305 /* FIXME: in an ltrans unit when the offline copy is outside a
1306 partition but inline copies are inside a partition, we can
1307 end up not removing the body since we no longer have an
1308 analyzed node pointing to it. */
67348ccc 1309 && !node->in_other_partition
07250f0e 1310 && !node->clones
b66887e4
JJ
1311 && !DECL_EXTERNAL (decl))
1312 {
d52f5295 1313 node->debug ();
7cbf224d
RG
1314 internal_error ("failed to reclaim unneeded function in same "
1315 "comdat group");
b66887e4
JJ
1316 }
1317 }
1318#endif
18d13f34
JH
1319}
1320
6744a6ab 1321/* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
3649b9b7 1322 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
6744a6ab
JH
1323
1324 Set current_function_decl and cfun to newly constructed empty function body.
1325 return basic block in the function body. */
1326
3649b9b7
ST
1327basic_block
1328init_lowered_empty_function (tree decl, bool in_ssa)
6744a6ab
JH
1329{
1330 basic_block bb;
1331
1332 current_function_decl = decl;
1333 allocate_struct_function (decl, false);
1334 gimple_register_cfg_hooks ();
1335 init_empty_tree_cfg ();
3649b9b7
ST
1336
1337 if (in_ssa)
1338 {
1339 init_tree_ssa (cfun);
1340 init_ssa_operands (cfun);
1341 cfun->gimple_df->in_ssa_p = true;
31ee20ba 1342 cfun->curr_properties |= PROP_ssa;
3649b9b7
ST
1343 }
1344
6744a6ab
JH
1345 DECL_INITIAL (decl) = make_node (BLOCK);
1346
1347 DECL_SAVED_TREE (decl) = error_mark_node;
31ee20ba
RB
1348 cfun->curr_properties |= (PROP_gimple_lcf | PROP_gimple_leh | PROP_gimple_any
1349 | PROP_cfg | PROP_loops);
1350
766090c2 1351 set_loops_for_fn (cfun, ggc_cleared_alloc<loops> ());
31ee20ba
RB
1352 init_loops_structure (cfun, loops_for_fn (cfun), 1);
1353 loops_for_fn (cfun)->state |= LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
6744a6ab
JH
1354
1355 /* Create BB for body of the function and connect it properly. */
fefa31b5
DM
1356 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR_FOR_FN (cfun));
1357 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FALLTHRU);
1358 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
1359 add_bb_to_loop (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
6744a6ab
JH
1360
1361 return bb;
1362}
1363
1364/* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1365 offset indicated by VIRTUAL_OFFSET, if that is
1366 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1367 zero for a result adjusting thunk. */
1368
1369static tree
1370thunk_adjust (gimple_stmt_iterator * bsi,
1371 tree ptr, bool this_adjusting,
1372 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1373{
538dd0b7 1374 gassign *stmt;
6744a6ab
JH
1375 tree ret;
1376
313333a6
RG
1377 if (this_adjusting
1378 && fixed_offset != 0)
6744a6ab 1379 {
5d49b6a7
RG
1380 stmt = gimple_build_assign
1381 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1382 ptr,
1383 fixed_offset));
6744a6ab
JH
1384 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1385 }
1386
1387 /* If there's a virtual offset, look up that value in the vtable and
1388 adjust the pointer again. */
1389 if (virtual_offset)
1390 {
1391 tree vtabletmp;
1392 tree vtabletmp2;
1393 tree vtabletmp3;
6744a6ab
JH
1394
1395 if (!vtable_entry_type)
1396 {
1397 tree vfunc_type = make_node (FUNCTION_TYPE);
1398 TREE_TYPE (vfunc_type) = integer_type_node;
1399 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1400 layout_type (vfunc_type);
1401
1402 vtable_entry_type = build_pointer_type (vfunc_type);
1403 }
1404
1405 vtabletmp =
7cc434a3 1406 create_tmp_reg (build_pointer_type
7d80ca1f 1407 (build_pointer_type (vtable_entry_type)), "vptr");
6744a6ab
JH
1408
1409 /* The vptr is always at offset zero in the object. */
1410 stmt = gimple_build_assign (vtabletmp,
1411 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1412 ptr));
1413 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
6744a6ab
JH
1414
1415 /* Form the vtable address. */
7cc434a3 1416 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
7d80ca1f 1417 "vtableaddr");
6744a6ab 1418 stmt = gimple_build_assign (vtabletmp2,
70f34814 1419 build_simple_mem_ref (vtabletmp));
6744a6ab 1420 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
6744a6ab
JH
1421
1422 /* Find the entry with the vcall offset. */
1423 stmt = gimple_build_assign (vtabletmp2,
5d49b6a7
RG
1424 fold_build_pointer_plus_loc (input_location,
1425 vtabletmp2,
1426 virtual_offset));
6744a6ab
JH
1427 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1428
1429 /* Get the offset itself. */
7cc434a3 1430 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
7d80ca1f 1431 "vcalloffset");
6744a6ab 1432 stmt = gimple_build_assign (vtabletmp3,
70f34814 1433 build_simple_mem_ref (vtabletmp2));
6744a6ab 1434 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
6744a6ab 1435
6744a6ab 1436 /* Adjust the `this' pointer. */
0d82a1c8
RG
1437 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1438 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1439 GSI_CONTINUE_LINKING);
6744a6ab
JH
1440 }
1441
313333a6
RG
1442 if (!this_adjusting
1443 && fixed_offset != 0)
6744a6ab
JH
1444 /* Adjust the pointer by the constant. */
1445 {
1446 tree ptrtmp;
1447
1448 if (TREE_CODE (ptr) == VAR_DECL)
1449 ptrtmp = ptr;
1450 else
1451 {
7cc434a3 1452 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
6744a6ab
JH
1453 stmt = gimple_build_assign (ptrtmp, ptr);
1454 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
6744a6ab 1455 }
5d49b6a7
RG
1456 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1457 ptrtmp, fixed_offset);
6744a6ab
JH
1458 }
1459
1460 /* Emit the statement and gimplify the adjustment expression. */
7cc434a3 1461 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
6744a6ab 1462 stmt = gimple_build_assign (ret, ptr);
6744a6ab
JH
1463 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1464
1465 return ret;
1466}
1467
afdec9bd 1468/* Expand thunk NODE to gimple if possible.
d211e471
ML
1469 When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
1470 no assembler is produced.
afdec9bd
JH
1471 When OUTPUT_ASM_THUNK is true, also produce assembler for
1472 thunks that are not lowered. */
6744a6ab 1473
afdec9bd 1474bool
d52f5295 1475cgraph_node::expand_thunk (bool output_asm_thunks, bool force_gimple_thunk)
6744a6ab 1476{
d52f5295
ML
1477 bool this_adjusting = thunk.this_adjusting;
1478 HOST_WIDE_INT fixed_offset = thunk.fixed_offset;
1479 HOST_WIDE_INT virtual_value = thunk.virtual_value;
6744a6ab 1480 tree virtual_offset = NULL;
d52f5295
ML
1481 tree alias = callees->callee->decl;
1482 tree thunk_fndecl = decl;
bcb650cb
JH
1483 tree a;
1484
d06865bf 1485
d211e471 1486 if (!force_gimple_thunk && this_adjusting
6744a6ab
JH
1487 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1488 virtual_value, alias))
1489 {
1490 const char *fnname;
1491 tree fn_block;
4399cf59 1492 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
afdec9bd
JH
1493
1494 if (!output_asm_thunks)
77b7d74b
MJ
1495 {
1496 analyzed = true;
1497 return false;
1498 }
afdec9bd
JH
1499
1500 if (in_lto_p)
70486010 1501 get_untransformed_body ();
afdec9bd 1502 a = DECL_ARGUMENTS (thunk_fndecl);
6744a6ab 1503
afdec9bd
JH
1504 current_function_decl = thunk_fndecl;
1505
1506 /* Ensure thunks are emitted in their correct sections. */
1507 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1508
6744a6ab
JH
1509 DECL_RESULT (thunk_fndecl)
1510 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
4399cf59 1511 RESULT_DECL, 0, restype);
afdec9bd 1512 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
15488554 1513 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
6744a6ab
JH
1514
1515 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1516 create one. */
1517 fn_block = make_node (BLOCK);
1518 BLOCK_VARS (fn_block) = a;
1519 DECL_INITIAL (thunk_fndecl) = fn_block;
1520 init_function_start (thunk_fndecl);
1521 cfun->is_thunk = 1;
68a55980
JJ
1522 insn_locations_init ();
1523 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1524 prologue_location = curr_insn_location ();
6744a6ab
JH
1525 assemble_start_function (thunk_fndecl, fnname);
1526
1527 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1528 fixed_offset, virtual_value, alias);
1529
1530 assemble_end_function (thunk_fndecl, fnname);
68a55980 1531 insn_locations_finalize ();
6744a6ab
JH
1532 init_insn_lengths ();
1533 free_after_compilation (cfun);
1534 set_cfun (NULL);
1535 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
d52f5295
ML
1536 thunk.thunk_p = false;
1537 analyzed = false;
6744a6ab
JH
1538 }
1539 else
1540 {
1541 tree restype;
1542 basic_block bb, then_bb, else_bb, return_bb;
1543 gimple_stmt_iterator bsi;
1544 int nargs = 0;
1545 tree arg;
1546 int i;
1547 tree resdecl;
1548 tree restmp = NULL;
6744a6ab 1549
538dd0b7
DM
1550 gcall *call;
1551 greturn *ret;
6744a6ab 1552
afdec9bd 1553 if (in_lto_p)
70486010 1554 get_untransformed_body ();
afdec9bd 1555 a = DECL_ARGUMENTS (thunk_fndecl);
b84d4347 1556
afdec9bd
JH
1557 current_function_decl = thunk_fndecl;
1558
1559 /* Ensure thunks are emitted in their correct sections. */
1560 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1561
6744a6ab
JH
1562 DECL_IGNORED_P (thunk_fndecl) = 1;
1563 bitmap_obstack_initialize (NULL);
1564
d52f5295 1565 if (thunk.virtual_offset_p)
6744a6ab
JH
1566 virtual_offset = size_int (virtual_value);
1567
1568 /* Build the return declaration for the function. */
1569 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1570 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1571 {
1572 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1573 DECL_ARTIFICIAL (resdecl) = 1;
1574 DECL_IGNORED_P (resdecl) = 1;
1575 DECL_RESULT (thunk_fndecl) = resdecl;
afdec9bd 1576 DECL_CONTEXT (DECL_RESULT (thunk_fndecl)) = thunk_fndecl;
6744a6ab
JH
1577 }
1578 else
1579 resdecl = DECL_RESULT (thunk_fndecl);
1580
3649b9b7 1581 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
6744a6ab
JH
1582
1583 bsi = gsi_start_bb (bb);
1584
1585 /* Build call to the function being thunked. */
1586 if (!VOID_TYPE_P (restype))
1587 {
bc0ec027 1588 if (DECL_BY_REFERENCE (resdecl))
8c14c817
ML
1589 {
1590 restmp = gimple_fold_indirect_ref (resdecl);
1591 if (!restmp)
1592 restmp = build2 (MEM_REF,
1593 TREE_TYPE (TREE_TYPE (DECL_RESULT (alias))),
1594 resdecl,
1595 build_int_cst (TREE_TYPE
1596 (DECL_RESULT (alias)), 0));
1597 }
bc0ec027 1598 else if (!is_gimple_reg_type (restype))
6744a6ab
JH
1599 {
1600 restmp = resdecl;
5cf18d25
ML
1601
1602 if (TREE_CODE (restmp) == VAR_DECL)
1603 add_local_decl (cfun, restmp);
6744a6ab
JH
1604 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1605 }
1606 else
7cc434a3 1607 restmp = create_tmp_reg (restype, "retval");
6744a6ab
JH
1608 }
1609
910ad8de 1610 for (arg = a; arg; arg = DECL_CHAIN (arg))
6744a6ab 1611 nargs++;
ef062b13 1612 auto_vec<tree> vargs (nargs);
4eaf52aa
JJ
1613 i = 0;
1614 arg = a;
6744a6ab 1615 if (this_adjusting)
4eaf52aa
JJ
1616 {
1617 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1618 virtual_offset));
1619 arg = DECL_CHAIN (a);
1620 i = 1;
1621 }
bc0ec027
JH
1622
1623 if (nargs)
4eaf52aa 1624 for (; i < nargs; i++, arg = DECL_CHAIN (arg))
fc044323
JM
1625 {
1626 tree tmp = arg;
1627 if (!is_gimple_val (arg))
1628 {
1629 tmp = create_tmp_reg (TYPE_MAIN_VARIANT
1630 (TREE_TYPE (arg)), "arg");
1631 gimple stmt = gimple_build_assign (tmp, arg);
1632 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1633 }
1634 vargs.quick_push (tmp);
1635 }
6744a6ab 1636 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
d52f5295 1637 callees->call_stmt = call;
6744a6ab 1638 gimple_call_set_from_thunk (call, true);
d5e254e1 1639 gimple_call_set_with_bounds (call, instrumentation_clone);
6744a6ab 1640 if (restmp)
bc0ec027
JH
1641 {
1642 gimple_call_set_lhs (call, restmp);
1643 gcc_assert (useless_type_conversion_p (TREE_TYPE (restmp),
1644 TREE_TYPE (TREE_TYPE (alias))));
1645 }
6744a6ab 1646 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
bc0ec027
JH
1647 if (!(gimple_call_flags (call) & ECF_NORETURN))
1648 {
1649 if (restmp && !this_adjusting
1650 && (fixed_offset || virtual_offset))
1651 {
1652 tree true_label = NULL_TREE;
6744a6ab 1653
bc0ec027
JH
1654 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1655 {
1656 gimple stmt;
1657 /* If the return type is a pointer, we need to
1658 protect against NULL. We know there will be an
1659 adjustment, because that's why we're emitting a
1660 thunk. */
1661 then_bb = create_basic_block (NULL, (void *) 0, bb);
1662 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1663 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1664 add_bb_to_loop (then_bb, bb->loop_father);
1665 add_bb_to_loop (return_bb, bb->loop_father);
1666 add_bb_to_loop (else_bb, bb->loop_father);
1667 remove_edge (single_succ_edge (bb));
1668 true_label = gimple_block_label (then_bb);
1669 stmt = gimple_build_cond (NE_EXPR, restmp,
1670 build_zero_cst (TREE_TYPE (restmp)),
1671 NULL_TREE, NULL_TREE);
1672 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1673 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1674 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
fefa31b5 1675 make_edge (return_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
bc0ec027
JH
1676 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1677 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1678 bsi = gsi_last_bb (then_bb);
1679 }
6744a6ab 1680
bc0ec027
JH
1681 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1682 fixed_offset, virtual_offset);
1683 if (true_label)
1684 {
1685 gimple stmt;
1686 bsi = gsi_last_bb (else_bb);
1687 stmt = gimple_build_assign (restmp,
1688 build_zero_cst (TREE_TYPE (restmp)));
1689 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1690 bsi = gsi_last_bb (return_bb);
1691 }
6744a6ab 1692 }
bc0ec027
JH
1693 else
1694 gimple_call_set_tail (call, true);
6744a6ab 1695
bc0ec027 1696 /* Build return value. */
8c14c817
ML
1697 if (!DECL_BY_REFERENCE (resdecl))
1698 ret = gimple_build_return (restmp);
1699 else
1700 ret = gimple_build_return (resdecl);
1701
bc0ec027 1702 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
6744a6ab
JH
1703 }
1704 else
bc0ec027
JH
1705 {
1706 gimple_call_set_tail (call, true);
1707 remove_edge (single_succ_edge (bb));
1708 }
6744a6ab 1709
afdec9bd
JH
1710 cfun->gimple_df->in_ssa_p = true;
1711 /* FIXME: C++ FE should stop setting TREE_ASM_WRITTEN on thunks. */
1712 TREE_ASM_WRITTEN (thunk_fndecl) = false;
6744a6ab
JH
1713 delete_unreachable_blocks ();
1714 update_ssa (TODO_update_ssa);
bc0ec027
JH
1715#ifdef ENABLE_CHECKING
1716 verify_flow_info ();
1717#endif
253eab4f 1718 free_dominance_info (CDI_DOMINATORS);
6744a6ab 1719
6744a6ab
JH
1720 /* Since we want to emit the thunk, we explicitly mark its name as
1721 referenced. */
d52f5295
ML
1722 thunk.thunk_p = false;
1723 lowered = true;
6744a6ab
JH
1724 bitmap_obstack_release (NULL);
1725 }
1726 current_function_decl = NULL;
af16bc76 1727 set_cfun (NULL);
afdec9bd 1728 return true;
6744a6ab
JH
1729}
1730
3dafb85c 1731/* Assemble thunks and aliases associated to node. */
c47d0034 1732
3dafb85c
ML
1733void
1734cgraph_node::assemble_thunks_and_aliases (void)
c47d0034 1735{
3dafb85c
ML
1736 cgraph_edge *e;
1737 ipa_ref *ref;
39e2db00 1738
3dafb85c 1739 for (e = callers; e;)
d5e254e1
IE
1740 if (e->caller->thunk.thunk_p
1741 && !e->caller->thunk.add_pointer_bounds_args)
c47d0034 1742 {
3dafb85c 1743 cgraph_node *thunk = e->caller;
c47d0034
JH
1744
1745 e = e->next_caller;
d52f5295 1746 thunk->expand_thunk (true, false);
3dafb85c 1747 thunk->assemble_thunks_and_aliases ();
c47d0034
JH
1748 }
1749 else
1750 e = e->next_caller;
e55637b7 1751
3dafb85c 1752 FOR_EACH_ALIAS (this, ref)
e55637b7 1753 {
3dafb85c
ML
1754 cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
1755 bool saved_written = TREE_ASM_WRITTEN (decl);
e55637b7
ML
1756
1757 /* Force assemble_alias to really output the alias this time instead
1758 of buffering it in same alias pairs. */
3dafb85c 1759 TREE_ASM_WRITTEN (decl) = 1;
e55637b7 1760 do_assemble_alias (alias->decl,
3dafb85c
ML
1761 DECL_ASSEMBLER_NAME (decl));
1762 alias->assemble_thunks_and_aliases ();
1763 TREE_ASM_WRITTEN (decl) = saved_written;
e55637b7 1764 }
c47d0034
JH
1765}
1766
3dafb85c 1767/* Expand function specified by node. */
0878843f 1768
3dafb85c
ML
1769void
1770cgraph_node::expand (void)
0878843f 1771{
0878843f
RG
1772 location_t saved_loc;
1773
9c8305f8 1774 /* We ought to not compile any inline clones. */
3dafb85c 1775 gcc_assert (!global.inlined_to);
9c8305f8
JH
1776
1777 announce_function (decl);
3dafb85c
ML
1778 process = 0;
1779 gcc_assert (lowered);
70486010 1780 get_untransformed_body ();
9c8305f8
JH
1781
1782 /* Generate RTL for the body of DECL. */
1783
0878843f
RG
1784 timevar_push (TV_REST_OF_COMPILATION);
1785
3dafb85c 1786 gcc_assert (symtab->global_info_ready);
0878843f
RG
1787
1788 /* Initialize the default bitmap obstack. */
1789 bitmap_obstack_initialize (NULL);
1790
1791 /* Initialize the RTL code for the function. */
9c8305f8 1792 current_function_decl = decl;
0878843f 1793 saved_loc = input_location;
9c8305f8
JH
1794 input_location = DECL_SOURCE_LOCATION (decl);
1795 init_function_start (decl);
0878843f
RG
1796
1797 gimple_register_cfg_hooks ();
1798
1799 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1800
1801 execute_all_ipa_transforms ();
1802
1803 /* Perform all tree transforms and optimizations. */
1804
1805 /* Signal the start of passes. */
1806 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1807
2cbf2d95 1808 execute_pass_list (cfun, g->get_passes ()->all_passes);
0878843f
RG
1809
1810 /* Signal the end of passes. */
1811 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1812
1813 bitmap_obstack_release (&reg_obstack);
1814
1815 /* Release the default bitmap obstack. */
1816 bitmap_obstack_release (NULL);
1817
0878843f
RG
1818 /* If requested, warn about function definitions where the function will
1819 return a value (usually of some struct or union type) which itself will
1820 take up a lot of stack space. */
9c8305f8 1821 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
0878843f 1822 {
9c8305f8 1823 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
0878843f
RG
1824
1825 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1826 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1827 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1828 larger_than_size))
1829 {
1830 unsigned int size_as_int
1831 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1832
1833 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1834 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
9c8305f8 1835 decl, size_as_int);
0878843f
RG
1836 else
1837 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
9c8305f8 1838 decl, larger_than_size);
0878843f
RG
1839 }
1840 }
1841
9c8305f8
JH
1842 gimple_set_body (decl, NULL);
1843 if (DECL_STRUCT_FUNCTION (decl) == 0
d52f5295 1844 && !cgraph_node::get (decl)->origin)
0878843f
RG
1845 {
1846 /* Stop pointing to the local nodes about to be freed.
1847 But DECL_INITIAL must remain nonzero so we know this
1848 was an actual function definition.
1849 For a nested function, this is done in c_pop_function_context.
1850 If rest_of_compilation set this to 0, leave it 0. */
9c8305f8
JH
1851 if (DECL_INITIAL (decl) != 0)
1852 DECL_INITIAL (decl) = error_mark_node;
0878843f
RG
1853 }
1854
1855 input_location = saved_loc;
1856
1857 ggc_collect ();
1858 timevar_pop (TV_REST_OF_COMPILATION);
5806d9ac
JH
1859
1860 /* Make sure that BE didn't give up on compiling. */
1861 gcc_assert (TREE_ASM_WRITTEN (decl));
af16bc76 1862 set_cfun (NULL);
5806d9ac 1863 current_function_decl = NULL;
1bb7e8f8
JH
1864
1865 /* It would make a lot more sense to output thunks before function body to get more
65d630d4 1866 forward and lest backwarding jumps. This however would need solving problem
1bb7e8f8 1867 with comdats. See PR48668. Also aliases must come after function itself to
65d630d4 1868 make one pass assemblers, like one on AIX, happy. See PR 50689.
1bb7e8f8
JH
1869 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1870 groups. */
3dafb85c
ML
1871 assemble_thunks_and_aliases ();
1872 release_body ();
39ecc018
JH
1873 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1874 points to the dead function body. */
3dafb85c
ML
1875 remove_callees ();
1876 remove_all_references ();
1c4a429a
JH
1877}
1878
9cec31f4
ML
1879/* Node comparer that is responsible for the order that corresponds
1880 to time when a function was launched for the first time. */
1881
1882static int
1883node_cmp (const void *pa, const void *pb)
1884{
3dafb85c
ML
1885 const cgraph_node *a = *(const cgraph_node * const *) pa;
1886 const cgraph_node *b = *(const cgraph_node * const *) pb;
9cec31f4
ML
1887
1888 /* Functions with time profile must be before these without profile. */
1889 if (!a->tp_first_run || !b->tp_first_run)
1890 return a->tp_first_run - b->tp_first_run;
1891
1892 return a->tp_first_run != b->tp_first_run
1893 ? b->tp_first_run - a->tp_first_run
1894 : b->order - a->order;
1895}
6674a6ce 1896
db0e878d
AJ
1897/* Expand all functions that must be output.
1898
b58b1157
JH
1899 Attempt to topologically sort the nodes so function is output when
1900 all called functions are already assembled to allow data to be
a98ebe2e 1901 propagated across the callgraph. Use a stack to get smaller distance
d1a6adeb 1902 between a function and its callees (later we may choose to use a more
b58b1157
JH
1903 sophisticated algorithm for function reordering; we will likely want
1904 to use subsections to make the output functions appear in top-down
1905 order). */
1906
1907static void
65d630d4 1908expand_all_functions (void)
b58b1157 1909{
3dafb85c
ML
1910 cgraph_node *node;
1911 cgraph_node **order = XCNEWVEC (cgraph_node *,
1912 symtab->cgraph_count);
9cec31f4 1913 unsigned int expanded_func_count = 0, profiled_func_count = 0;
f30cfcb1 1914 int order_pos, new_order_pos = 0;
b58b1157
JH
1915 int i;
1916
af8bca3c 1917 order_pos = ipa_reverse_postorder (order);
3dafb85c 1918 gcc_assert (order_pos == symtab->cgraph_count);
b58b1157 1919
1ae58c30 1920 /* Garbage collector may remove inline clones we eliminate during
18c6ada9
JH
1921 optimization. So we must be sure to not reference them. */
1922 for (i = 0; i < order_pos; i++)
257eb6e3 1923 if (order[i]->process)
18c6ada9
JH
1924 order[new_order_pos++] = order[i];
1925
9cec31f4 1926 if (flag_profile_reorder_functions)
3dafb85c 1927 qsort (order, new_order_pos, sizeof (cgraph_node *), node_cmp);
9cec31f4 1928
18c6ada9 1929 for (i = new_order_pos - 1; i >= 0; i--)
b58b1157
JH
1930 {
1931 node = order[i];
9cec31f4 1932
257eb6e3 1933 if (node->process)
b58b1157 1934 {
9cec31f4
ML
1935 expanded_func_count++;
1936 if(node->tp_first_run)
1937 profiled_func_count++;
1938
3dafb85c
ML
1939 if (symtab->dump_file)
1940 fprintf (symtab->dump_file,
1941 "Time profile order in expand_all_functions:%s:%d\n",
1942 node->asm_name (), node->tp_first_run);
257eb6e3 1943 node->process = 0;
3dafb85c 1944 node->expand ();
b58b1157
JH
1945 }
1946 }
9cec31f4
ML
1947
1948 if (dump_file)
1949 fprintf (dump_file, "Expanded functions with time profile (%s):%u/%u\n",
1950 main_input_filename, profiled_func_count, expanded_func_count);
1951
3dafb85c
ML
1952 if (symtab->dump_file && flag_profile_reorder_functions)
1953 fprintf (symtab->dump_file, "Expanded functions with time profile:%u/%u\n",
9cec31f4
ML
1954 profiled_func_count, expanded_func_count);
1955
3dafb85c 1956 symtab->process_new_functions ();
45852dcc 1957 free_gimplify_stack ();
50674e96 1958
b58b1157
JH
1959 free (order);
1960}
1961
474eccc6
ILT
1962/* This is used to sort the node types by the cgraph order number. */
1963
24b97832
ILT
1964enum cgraph_order_sort_kind
1965{
1966 ORDER_UNDEFINED = 0,
1967 ORDER_FUNCTION,
1968 ORDER_VAR,
1969 ORDER_ASM
1970};
1971
474eccc6
ILT
1972struct cgraph_order_sort
1973{
24b97832 1974 enum cgraph_order_sort_kind kind;
474eccc6
ILT
1975 union
1976 {
3dafb85c 1977 cgraph_node *f;
2c8326a5 1978 varpool_node *v;
3dafb85c 1979 asm_node *a;
474eccc6
ILT
1980 } u;
1981};
1982
1983/* Output all functions, variables, and asm statements in the order
1984 according to their order fields, which is the order in which they
1985 appeared in the file. This implements -fno-toplevel-reorder. In
1986 this mode we may output functions and variables which don't really
7861b648
AK
1987 need to be output.
1988 When NO_REORDER is true only do this for symbols marked no reorder. */
474eccc6
ILT
1989
1990static void
7861b648 1991output_in_order (bool no_reorder)
474eccc6
ILT
1992{
1993 int max;
3dafb85c 1994 cgraph_order_sort *nodes;
474eccc6 1995 int i;
3dafb85c 1996 cgraph_node *pf;
2c8326a5 1997 varpool_node *pv;
3dafb85c
ML
1998 asm_node *pa;
1999 max = symtab->order;
2000 nodes = XCNEWVEC (cgraph_order_sort, max);
474eccc6 2001
65c70e6b 2002 FOR_EACH_DEFINED_FUNCTION (pf)
474eccc6 2003 {
67348ccc 2004 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
474eccc6 2005 {
7861b648
AK
2006 if (no_reorder && !pf->no_reorder)
2007 continue;
67348ccc 2008 i = pf->order;
474eccc6
ILT
2009 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2010 nodes[i].kind = ORDER_FUNCTION;
2011 nodes[i].u.f = pf;
2012 }
2013 }
2014
65c70e6b 2015 FOR_EACH_DEFINED_VARIABLE (pv)
67348ccc 2016 if (!DECL_EXTERNAL (pv->decl))
6649df51 2017 {
7861b648
AK
2018 if (no_reorder && !pv->no_reorder)
2019 continue;
67348ccc 2020 i = pv->order;
6649df51
JH
2021 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2022 nodes[i].kind = ORDER_VAR;
2023 nodes[i].u.v = pv;
2024 }
474eccc6 2025
3dafb85c 2026 for (pa = symtab->first_asm_symbol (); pa; pa = pa->next)
474eccc6
ILT
2027 {
2028 i = pa->order;
2029 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2030 nodes[i].kind = ORDER_ASM;
2031 nodes[i].u.a = pa;
2032 }
474eccc6 2033
7386e3ee 2034 /* In toplevel reorder mode we output all statics; mark them as needed. */
7386e3ee 2035
7fece979
JJ
2036 for (i = 0; i < max; ++i)
2037 if (nodes[i].kind == ORDER_VAR)
9041d2e6 2038 nodes[i].u.v->finalize_named_section_flags ();
7fece979 2039
474eccc6
ILT
2040 for (i = 0; i < max; ++i)
2041 {
2042 switch (nodes[i].kind)
2043 {
2044 case ORDER_FUNCTION:
257eb6e3 2045 nodes[i].u.f->process = 0;
3dafb85c 2046 nodes[i].u.f->expand ();
474eccc6
ILT
2047 break;
2048
2049 case ORDER_VAR:
9041d2e6 2050 nodes[i].u.v->assemble_decl ();
474eccc6
ILT
2051 break;
2052
2053 case ORDER_ASM:
2054 assemble_asm (nodes[i].u.a->asm_str);
2055 break;
2056
2057 case ORDER_UNDEFINED:
2058 break;
2059
2060 default:
2061 gcc_unreachable ();
2062 }
2063 }
e7b9eb2c 2064
3dafb85c
ML
2065 symtab->clear_asm_symbols ();
2066
33283dad 2067 free (nodes);
474eccc6
ILT
2068}
2069
ef330312
PB
2070static void
2071ipa_passes (void)
2072{
315f8c0e
DM
2073 gcc::pass_manager *passes = g->get_passes ();
2074
db2960f4 2075 set_cfun (NULL);
04b201a2 2076 current_function_decl = NULL;
726a989a 2077 gimple_register_cfg_hooks ();
ef330312 2078 bitmap_obstack_initialize (NULL);
b20996ff 2079
090fa0ab
GF
2080 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2081
b20996ff 2082 if (!in_lto_p)
0430f80c 2083 {
315f8c0e 2084 execute_ipa_pass_list (passes->all_small_ipa_passes);
0430f80c
RG
2085 if (seen_error ())
2086 return;
2087 }
3baf459d 2088
8605403e
JH
2089 /* This extra symtab_remove_unreachable_nodes pass tends to catch some
2090 devirtualization and other changes where removal iterate. */
17e0fc92 2091 symtab->remove_unreachable_nodes (symtab->dump_file);
467a8db0 2092
d7f09764
DN
2093 /* If pass_all_early_optimizations was not scheduled, the state of
2094 the cgraph will not be properly updated. Update it now. */
3dafb85c
ML
2095 if (symtab->state < IPA_SSA)
2096 symtab->state = IPA_SSA;
3baf459d 2097
d7f09764
DN
2098 if (!in_lto_p)
2099 {
2100 /* Generate coverage variables and constructors. */
2101 coverage_finish ();
2102
2103 /* Process new functions added. */
2104 set_cfun (NULL);
2105 current_function_decl = NULL;
3dafb85c 2106 symtab->process_new_functions ();
d7f09764 2107
090fa0ab 2108 execute_ipa_summary_passes
6a5ac314 2109 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
fb3f88cc 2110 }
c082f9f3
SB
2111
2112 /* Some targets need to handle LTO assembler output specially. */
f0d78df9 2113 if (flag_generate_lto || flag_generate_offload)
c082f9f3
SB
2114 targetm.asm_out.lto_start ();
2115
d7f09764 2116 if (!in_lto_p)
1f6be682
IV
2117 {
2118 if (g->have_offload)
2119 {
2120 section_name_prefix = OFFLOAD_SECTION_NAME_PREFIX;
1b34e6e2 2121 lto_stream_offload_p = true;
837bac8c 2122 ipa_write_summaries ();
1b34e6e2 2123 lto_stream_offload_p = false;
1f6be682
IV
2124 }
2125 if (flag_lto)
2126 {
2127 section_name_prefix = LTO_SECTION_NAME_PREFIX;
1b34e6e2 2128 lto_stream_offload_p = false;
837bac8c 2129 ipa_write_summaries ();
1f6be682
IV
2130 }
2131 }
d7f09764 2132
f0d78df9 2133 if (flag_generate_lto || flag_generate_offload)
c082f9f3
SB
2134 targetm.asm_out.lto_end ();
2135
cc8547a7 2136 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
315f8c0e 2137 execute_ipa_pass_list (passes->all_regular_ipa_passes);
090fa0ab 2138 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
3baf459d 2139
ef330312
PB
2140 bitmap_obstack_release (NULL);
2141}
2142
25e2c40d
JH
2143
2144/* Return string alias is alias of. */
2145
2146static tree
2147get_alias_symbol (tree decl)
2148{
2149 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2150 return get_identifier (TREE_STRING_POINTER
2151 (TREE_VALUE (TREE_VALUE (alias))));
2152}
2153
2154
c9552bff 2155/* Weakrefs may be associated to external decls and thus not output
073a8998 2156 at expansion time. Emit all necessary aliases. */
c9552bff 2157
3dafb85c
ML
2158void
2159symbol_table::output_weakrefs (void)
c9552bff 2160{
5e20cdc9 2161 symtab_node *node;
d5e254e1 2162 cgraph_node *cnode;
40a7fe1e 2163 FOR_EACH_SYMBOL (node)
67348ccc
DM
2164 if (node->alias
2165 && !TREE_ASM_WRITTEN (node->decl)
d5e254e1
IE
2166 && (!(cnode = dyn_cast <cgraph_node *> (node))
2167 || !cnode->instrumented_version
2168 || !TREE_ASM_WRITTEN (cnode->instrumented_version->decl))
67348ccc 2169 && node->weakref)
40a7fe1e
JH
2170 {
2171 tree target;
2172
2173 /* Weakrefs are special by not requiring target definition in current
2174 compilation unit. It is thus bit hard to work out what we want to
2175 alias.
2176 When alias target is defined, we need to fetch it from symtab reference,
2177 otherwise it is pointed to by alias_target. */
67348ccc
DM
2178 if (node->alias_target)
2179 target = (DECL_P (node->alias_target)
2180 ? DECL_ASSEMBLER_NAME (node->alias_target)
2181 : node->alias_target);
2182 else if (node->analyzed)
d52f5295 2183 target = DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl);
40a7fe1e
JH
2184 else
2185 {
2186 gcc_unreachable ();
67348ccc 2187 target = get_alias_symbol (node->decl);
40a7fe1e 2188 }
67348ccc 2189 do_assemble_alias (node->decl, target);
40a7fe1e 2190 }
c9552bff
JH
2191}
2192
711417cd
RG
2193/* Perform simple optimizations based on callgraph. */
2194
2195void
3dafb85c 2196symbol_table::compile (void)
711417cd
RG
2197{
2198 if (seen_error ())
2199 return;
2200
2201#ifdef ENABLE_CHECKING
d52f5295 2202 symtab_node::verify_symtab_nodes ();
711417cd
RG
2203#endif
2204
711417cd
RG
2205 timevar_push (TV_CGRAPHOPT);
2206 if (pre_ipa_mem_report)
2207 {
2208 fprintf (stderr, "Memory consumption before IPA\n");
2209 dump_memory_report (false);
2210 }
2211 if (!quiet_flag)
2212 fprintf (stderr, "Performing interprocedural optimizations\n");
3dafb85c 2213 state = IPA;
711417cd 2214
1f6be682
IV
2215 /* Offloading requires LTO infrastructure. */
2216 if (!in_lto_p && g->have_offload)
f0d78df9 2217 flag_generate_offload = 1;
1f6be682 2218
65d630d4 2219 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
f0d78df9 2220 if (flag_generate_lto || flag_generate_offload)
65d630d4
JH
2221 lto_streamer_hooks_init ();
2222
711417cd
RG
2223 /* Don't run the IPA passes if there was any error or sorry messages. */
2224 if (!seen_error ())
2225 ipa_passes ();
2226
2227 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2228 if (seen_error ()
2229 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2230 {
2231 timevar_pop (TV_CGRAPHOPT);
2232 return;
2233 }
2234
3dafb85c
ML
2235 global_info_ready = true;
2236 if (dump_file)
711417cd 2237 {
3dafb85c
ML
2238 fprintf (dump_file, "Optimized ");
2239 symtab_node:: dump_table (dump_file);
711417cd
RG
2240 }
2241 if (post_ipa_mem_report)
2242 {
2243 fprintf (stderr, "Memory consumption after IPA\n");
2244 dump_memory_report (false);
2245 }
2246 timevar_pop (TV_CGRAPHOPT);
2247
2248 /* Output everything. */
2249 (*debug_hooks->assembly_start) ();
2250 if (!quiet_flag)
2251 fprintf (stderr, "Assembling functions:\n");
2252#ifdef ENABLE_CHECKING
d52f5295 2253 symtab_node::verify_symtab_nodes ();
711417cd
RG
2254#endif
2255
3dafb85c 2256 materialize_all_clones ();
711417cd 2257 bitmap_obstack_initialize (NULL);
315f8c0e 2258 execute_ipa_pass_list (g->get_passes ()->all_late_ipa_passes);
711417cd 2259 bitmap_obstack_release (NULL);
65d630d4 2260 mark_functions_to_output ();
711417cd 2261
38e55e5c
JH
2262 /* When weakref support is missing, we autmatically translate all
2263 references to NODE to references to its ultimate alias target.
2264 The renaming mechanizm uses flag IDENTIFIER_TRANSPARENT_ALIAS and
2265 TREE_CHAIN.
2266
2267 Set up this mapping before we output any assembler but once we are sure
2268 that all symbol renaming is done.
2269
2270 FIXME: All this uglyness can go away if we just do renaming at gimple
2271 level by physically rewritting the IL. At the moment we can only redirect
2272 calls, so we need infrastructure for renaming references as well. */
2273#ifndef ASM_OUTPUT_WEAKREF
5e20cdc9 2274 symtab_node *node;
38e55e5c
JH
2275
2276 FOR_EACH_SYMBOL (node)
67348ccc
DM
2277 if (node->alias
2278 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->decl)))
38e55e5c
JH
2279 {
2280 IDENTIFIER_TRANSPARENT_ALIAS
67348ccc
DM
2281 (DECL_ASSEMBLER_NAME (node->decl)) = 1;
2282 TREE_CHAIN (DECL_ASSEMBLER_NAME (node->decl))
2283 = (node->alias_target ? node->alias_target
dacd445e 2284 : DECL_ASSEMBLER_NAME (node->get_alias_target ()->decl));
38e55e5c
JH
2285 }
2286#endif
2287
3dafb85c 2288 state = EXPANSION;
9cec31f4 2289
711417cd 2290 if (!flag_toplevel_reorder)
7861b648 2291 output_in_order (false);
711417cd
RG
2292 else
2293 {
7861b648
AK
2294 /* Output first asm statements and anything ordered. The process
2295 flag is cleared for these nodes, so we skip them later. */
2296 output_in_order (true);
65d630d4 2297 expand_all_functions ();
3dafb85c 2298 output_variables ();
711417cd
RG
2299 }
2300
3dafb85c
ML
2301 process_new_functions ();
2302 state = FINISHED;
07250f0e 2303 output_weakrefs ();
711417cd 2304
3dafb85c 2305 if (dump_file)
711417cd 2306 {
3dafb85c
ML
2307 fprintf (dump_file, "\nFinal ");
2308 symtab_node::dump_table (dump_file);
711417cd
RG
2309 }
2310#ifdef ENABLE_CHECKING
d52f5295 2311 symtab_node::verify_symtab_nodes ();
711417cd
RG
2312 /* Double check that all inline clones are gone and that all
2313 function bodies have been released from memory. */
2314 if (!seen_error ())
2315 {
3dafb85c 2316 cgraph_node *node;
711417cd
RG
2317 bool error_found = false;
2318
65c70e6b
JH
2319 FOR_EACH_DEFINED_FUNCTION (node)
2320 if (node->global.inlined_to
67348ccc 2321 || gimple_has_body_p (node->decl))
711417cd
RG
2322 {
2323 error_found = true;
d52f5295 2324 node->debug ();
711417cd
RG
2325 }
2326 if (error_found)
2327 internal_error ("nodes with unreleased memory found");
2328 }
2329#endif
2330}
2331
2332
2333/* Analyze the whole compilation unit once it is parsed completely. */
2334
2335void
3dafb85c 2336symbol_table::finalize_compilation_unit (void)
711417cd
RG
2337{
2338 timevar_push (TV_CGRAPH);
2339
711417cd
RG
2340 /* If we're here there's no current function anymore. Some frontends
2341 are lazy in clearing these. */
2342 current_function_decl = NULL;
2343 set_cfun (NULL);
2344
2345 /* Do not skip analyzing the functions if there were errors, we
2346 miss diagnostics for following functions otherwise. */
2347
2348 /* Emit size functions we didn't inline. */
2349 finalize_size_functions ();
2350
2351 /* Mark alias targets necessary and emit diagnostics. */
711417cd
RG
2352 handle_alias_pairs ();
2353
2354 if (!quiet_flag)
2355 {
2356 fprintf (stderr, "\nAnalyzing compilation unit\n");
2357 fflush (stderr);
2358 }
2359
2360 if (flag_dump_passes)
2361 dump_passes ();
2362
2363 /* Gimplify and lower all functions, compute reachability and
2364 remove unreachable nodes. */
e70670cf 2365 analyze_functions ();
711417cd
RG
2366
2367 /* Mark alias targets necessary and emit diagnostics. */
711417cd
RG
2368 handle_alias_pairs ();
2369
2370 /* Gimplify and lower thunks. */
e70670cf 2371 analyze_functions ();
711417cd
RG
2372
2373 /* Finally drive the pass manager. */
65d630d4 2374 compile ();
711417cd
RG
2375
2376 timevar_pop (TV_CGRAPH);
2377}
2378
3edf64aa
DM
2379/* Reset all state within cgraphunit.c so that we can rerun the compiler
2380 within the same process. For use by toplev::finalize. */
2381
2382void
2383cgraphunit_c_finalize (void)
2384{
2385 gcc_assert (cgraph_new_nodes.length () == 0);
2386 cgraph_new_nodes.truncate (0);
2387
2388 vtable_entry_type = NULL;
2389 queued_nodes = &symtab_terminator;
2390
2391 first_analyzed = NULL;
2392 first_analyzed_var = NULL;
2393}
2394
d52f5295 2395/* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
8be2dc8c
ML
2396 kind of wrapper method. */
2397
2398void
3dafb85c 2399cgraph_node::create_wrapper (cgraph_node *target)
8be2dc8c 2400{
3f9f4ae7
ML
2401 /* Preserve DECL_RESULT so we get right by reference flag. */
2402 tree decl_result = DECL_RESULT (decl);
8be2dc8c 2403
3f9f4ae7
ML
2404 /* Remove the function's body but keep arguments to be reused
2405 for thunk. */
2406 release_body (true);
2407 reset ();
8be2dc8c 2408
3f9f4ae7
ML
2409 DECL_RESULT (decl) = decl_result;
2410 DECL_INITIAL (decl) = NULL;
2411 allocate_struct_function (decl, false);
2412 set_cfun (NULL);
8be2dc8c 2413
3f9f4ae7
ML
2414 /* Turn alias into thunk and expand it into GIMPLE representation. */
2415 definition = true;
2416 thunk.thunk_p = true;
2417 thunk.this_adjusting = false;
8be2dc8c 2418
3f9f4ae7 2419 cgraph_edge *e = create_edge (target, NULL, 0, CGRAPH_FREQ_BASE);
8be2dc8c 2420
3f9f4ae7 2421 tree arguments = DECL_ARGUMENTS (decl);
d862b343 2422
3f9f4ae7
ML
2423 while (arguments)
2424 {
2425 TREE_ADDRESSABLE (arguments) = false;
2426 arguments = TREE_CHAIN (arguments);
2427 }
d862b343 2428
3f9f4ae7
ML
2429 expand_thunk (false, true);
2430 e->call_stmt_cannot_inline_p = true;
8be2dc8c 2431
3f9f4ae7
ML
2432 /* Inline summary set-up. */
2433 analyze ();
2434 inline_analyze_function (this);
8be2dc8c 2435}
711417cd 2436
7be82279 2437#include "gt-cgraphunit.h"