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