]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphunit.c
* passes.c (init_optimization_passes): Move OMP expansion into lowering.
[thirdparty/gcc.git] / gcc / cgraphunit.c
CommitLineData
da5e1e7c 1/* Driver of optimization process
711789cc 2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
ae01b312 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
8c4c00c1 9Software Foundation; either version 3, or (at your option) any later
ae01b312 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
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
ae01b312 20
da5e1e7c 21/* This module implements main driver of compilation process.
b0cdf642 22
23 The main scope of this file is to act as an interface in between
da5e1e7c 24 tree based frontends and the backend.
b0cdf642 25
26 The front-end is supposed to use following functionality:
27
28 - cgraph_finalize_function
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
b326746d 33 (There is one exception needed for implementing GCC extern inline
34 function.)
b0cdf642 35
a2d67028 36 - varpool_finalize_decl
b0cdf642 37
7bd28bba 38 This function has same behavior as the above but is used for static
b0cdf642 39 variables.
40
cf951b1a 41 - add_asm_node
42
43 Insert new toplevel ASM statement
44
45 - finalize_compilation_unit
b0cdf642 46
b326746d 47 This function is called once (source level) compilation unit is finalized
48 and it will no longer change.
b0cdf642 49
da5e1e7c 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.
9d75589a 53 Those are used to discover other necessary functions and variables.
da5e1e7c 54
55 At the end the bodies of unreachable functions are removed.
b0cdf642 56
b326746d 57 The function can be called multiple times when multiple source level
da5e1e7c 58 compilation units are combined.
b0cdf642 59
cf951b1a 60 - compile
b0cdf642 61
da5e1e7c 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
86 time. These include, for exmaple, transational memory lowering,
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.
b0cdf642 111
da5e1e7c 112 Compile time and/or parallel linktime stage (ltrans)
b0cdf642 113
da5e1e7c 114 Each of the object files is streamed back and compiled
115 separately. Now the function bodies becomes available
116 again.
b0cdf642 117
da5e1e7c 118 2) Virtual clone materialization
119 (cgraph_materialize_clone)
b0cdf642 120
da5e1e7c 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
b0cdf642 127
da5e1e7c 128 All IP passes transform function bodies based on earlier
129 decision of the IP propagation.
b0cdf642 130
da5e1e7c 131 4) late small IP passes
b0cdf642 132
da5e1e7c 133 Simple IP passes working within single program partition.
b0cdf642 134
da5e1e7c 135 5) Expansion
cf951b1a 136 (expand_all_functions)
b0cdf642 137
da5e1e7c 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.
b0cdf642 143
da5e1e7c 144 Note that with -fno-toplevel-reorder passes 5 and 6
145 are combined together in cgraph_output_in_order.
b0cdf642 146
da5e1e7c 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.
121f3051 153
da5e1e7c 154 - cgraph_function_versioning
155
156 produces a copy of function into new one (a version)
157 and apply simple transformations
158*/
acc70efa 159
ae01b312 160#include "config.h"
161#include "system.h"
162#include "coretypes.h"
163#include "tm.h"
164#include "tree.h"
941366fd 165#include "output.h"
b5530559 166#include "rtl.h"
acc70efa 167#include "tree-flow.h"
ae01b312 168#include "tree-inline.h"
169#include "langhooks.h"
c6224531 170#include "pointer-set.h"
ae01b312 171#include "toplev.h"
172#include "flags.h"
173#include "ggc.h"
174#include "debug.h"
175#include "target.h"
176#include "cgraph.h"
80a85d8a 177#include "diagnostic.h"
d7c6d889 178#include "params.h"
179#include "fibheap.h"
611e5405 180#include "intl.h"
b69eb0ff 181#include "function.h"
b5d36404 182#include "ipa-prop.h"
75a70cf9 183#include "gimple.h"
184#include "tree-iterator.h"
f1e2a033 185#include "tree-pass.h"
bfec3452 186#include "tree-dump.h"
da5e1e7c 187#include "gimple-pretty-print.h"
c1dcd13c 188#include "output.h"
9ed5b1f5 189#include "coverage.h"
c9036234 190#include "plugin.h"
a41f2a28 191#include "ipa-inline.h"
7771d558 192#include "ipa-utils.h"
a0605d65 193#include "lto-streamer.h"
3db65b62 194#include "except.h"
941366fd 195#include "regset.h" /* FIXME: For reg_obstack. */
d7c6d889 196
ff2a5ada 197/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
198 secondary queue used during optimization to accommodate passes that
199 may generate new functions that need to be optimized and expanded. */
200cgraph_node_set cgraph_new_nodes;
201
cf951b1a 202static void expand_all_functions (void);
203static void mark_functions_to_output (void);
204static void expand_function (struct cgraph_node *);
da5e1e7c 205static void cgraph_analyze_function (struct cgraph_node *);
18a71d50 206static void handle_alias_pairs (void);
25bb88de 207
ecb08119 208FILE *cgraph_dump_file;
121f3051 209
cf951b1a 210/* Linked list of cgraph asm nodes. */
211struct asm_node *asm_nodes;
212
213/* Last node in cgraph_asm_nodes. */
214static GTY(()) struct asm_node *asm_last_node;
215
28454517 216/* Used for vtable lookup in thunk adjusting. */
217static GTY (()) tree vtable_entry_type;
218
8efa224a 219/* Determine if function DECL is trivially needed and should stay in the
220 compilation unit. This is used at the symbol table construction time
9d75589a 221 and differs from later logic removing unnecessary functions that can
8efa224a 222 take into account results of analysis, whole program info etc. */
2c0b522d 223
da5e1e7c 224static bool
7bfefa9d 225cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
2c0b522d 226{
3f82b628 227 /* If the user told us it is used, then it must be so. */
8efa224a 228 if (node->symbol.force_output)
05806473 229 return true;
230
8efa224a 231 /* Double check that no one output the function into assembly file
232 early. */
233 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
234 || (node->thunk.thunk_p || node->same_body_alias)
235 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
3f82b628 236
55680bef 237
8efa224a 238 /* Keep constructors, destructors and virtual functions. */
239 if (DECL_STATIC_CONSTRUCTOR (decl)
240 || DECL_STATIC_DESTRUCTOR (decl)
241 || (DECL_VIRTUAL_P (decl)
242 && optimize && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
243 return true;
2c0b522d 244
245 /* Externally visible functions must be output. The exception is
8efa224a 246 COMDAT functions that must be output only when they are needed. */
8baa9d15 247
8efa224a 248 if (TREE_PUBLIC (decl)
62eec3b4 249 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
2c0b522d 250 return true;
251
2c0b522d 252 return false;
253}
254
ff2a5ada 255/* Head of the queue of nodes to be processed while building callgraph */
256
257static symtab_node first = (symtab_node)(void *)1;
258
259/* Add NODE to queue starting at FIRST.
260 The queue is linked via AUX pointers and terminated by pointer to 1. */
261
262static void
263enqueue_node (symtab_node node)
264{
265 if (node->symbol.aux)
266 return;
267 gcc_checking_assert (first);
268 node->symbol.aux = first;
269 first = node;
270}
271
bdc40eb8 272/* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
523c1122 273 functions into callgraph in a way so they look like ordinary reachable
274 functions inserted into callgraph already at construction time. */
275
276bool
277cgraph_process_new_functions (void)
278{
279 bool output = false;
280 tree fndecl;
281 struct cgraph_node *node;
ff2a5ada 282 cgraph_node_set_iterator csi;
523c1122 283
ff2a5ada 284 if (!cgraph_new_nodes)
285 return false;
18a71d50 286 handle_alias_pairs ();
523c1122 287 /* Note that this queue may grow as its being processed, as the new
288 functions may generate new ones. */
ff2a5ada 289 for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi))
523c1122 290 {
ff2a5ada 291 node = csi_node (csi);
7d0d0ce1 292 fndecl = node->symbol.decl;
523c1122 293 switch (cgraph_state)
294 {
295 case CGRAPH_STATE_CONSTRUCTION:
296 /* At construction time we just need to finalize function and move
297 it into reachable functions list. */
298
523c1122 299 cgraph_finalize_function (fndecl, false);
523c1122 300 output = true;
4f7a1122 301 cgraph_call_function_insertion_hooks (node);
ff2a5ada 302 enqueue_node ((symtab_node) node);
523c1122 303 break;
304
305 case CGRAPH_STATE_IPA:
f517b36e 306 case CGRAPH_STATE_IPA_SSA:
523c1122 307 /* When IPA optimization already started, do all essential
308 transformations that has been already performed on the whole
309 cgraph but not on this function. */
310
75a70cf9 311 gimple_register_cfg_hooks ();
523c1122 312 if (!node->analyzed)
313 cgraph_analyze_function (node);
314 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
f517b36e 315 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
316 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
317 /* When not optimizing, be sure we run early local passes anyway
318 to expand OMP. */
319 || !optimize)
20099e35 320 execute_pass_list (pass_early_local_passes.pass.sub);
649597af 321 else
a41f2a28 322 compute_inline_parameters (node, true);
523c1122 323 free_dominance_info (CDI_POST_DOMINATORS);
324 free_dominance_info (CDI_DOMINATORS);
325 pop_cfun ();
4f7a1122 326 cgraph_call_function_insertion_hooks (node);
523c1122 327 break;
328
329 case CGRAPH_STATE_EXPANSION:
330 /* Functions created during expansion shall be compiled
331 directly. */
09fc9532 332 node->process = 0;
4f7a1122 333 cgraph_call_function_insertion_hooks (node);
cf951b1a 334 expand_function (node);
523c1122 335 break;
336
337 default:
338 gcc_unreachable ();
339 break;
340 }
341 }
ff2a5ada 342 free_cgraph_node_set (cgraph_new_nodes);
343 cgraph_new_nodes = NULL;
523c1122 344 return output;
345}
346
9b8fb23a 347/* As an GCC extension we allow redefinition of the function. The
348 semantics when both copies of bodies differ is not well defined.
349 We replace the old body with new body so in unit at a time mode
350 we always use new body, while in normal mode we may end up with
351 old body inlined into some functions and new body expanded and
352 inlined in others.
353
354 ??? It may make more sense to use one body for inlining and other
355 body for expanding the function but this is difficult to do. */
356
357static void
358cgraph_reset_node (struct cgraph_node *node)
359{
09fc9532 360 /* If node->process is set, then we have already begun whole-unit analysis.
6329636b 361 This is *not* testing for whether we've already emitted the function.
362 That case can be sort-of legitimately seen with real function redefinition
363 errors. I would argue that the front end should never present us with
364 such a case, but don't enforce that for now. */
09fc9532 365 gcc_assert (!node->process);
9b8fb23a 366
367 /* Reset our data structures so we can analyze the function again. */
368 memset (&node->local, 0, sizeof (node->local));
369 memset (&node->global, 0, sizeof (node->global));
370 memset (&node->rtl, 0, sizeof (node->rtl));
371 node->analyzed = false;
9b8fb23a 372 node->local.finalized = false;
373
9b8fb23a 374 cgraph_node_remove_callees (node);
9b8fb23a 375}
c08871a9 376
9a2639fc 377/* Return true when there are references to NODE. */
378
379static bool
380referred_to_p (symtab_node node)
381{
9a2639fc 382 struct ipa_ref *ref;
383
9d75589a 384 /* See if there are any references at all. */
cf951b1a 385 if (ipa_ref_list_referring_iterate (&node->symbol.ref_list, 0, ref))
9a2639fc 386 return true;
cf951b1a 387 /* For functions check also calls. */
2dc9831f 388 cgraph_node *cn = dyn_cast <cgraph_node> (node);
389 if (cn && cn->callers)
9a2639fc 390 return true;
391 return false;
392}
393
28df663b 394/* DECL has been parsed. Take it, queue it, compile it at the whim of the
395 logic in effect. If NESTED is true, then our caller cannot stand to have
396 the garbage collector run at the moment. We would need to either create
397 a new GC context, or just not compile right now. */
ae01b312 398
399void
28df663b 400cgraph_finalize_function (tree decl, bool nested)
ae01b312 401{
5a90471f 402 struct cgraph_node *node = cgraph_get_create_node (decl);
ae01b312 403
c08871a9 404 if (node->local.finalized)
443089c1 405 {
406 cgraph_reset_node (node);
407 node->local.redefined_extern_inline = true;
408 }
28df663b 409
c08871a9 410 notice_global_symbol (decl);
79bb87b4 411 node->local.finalized = true;
e27482aa 412 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
ae01b312 413
8efa224a 414 /* With -fkeep-inline-functions we are keeping all inline functions except
415 for extern inline ones. */
416 if (flag_keep_inline_functions
417 && DECL_DECLARED_INLINE_P (decl)
418 && !DECL_EXTERNAL (decl)
419 && !DECL_DISREGARD_INLINE_LIMITS (decl))
420 node->symbol.force_output = 1;
2c0b522d 421
8efa224a 422 /* When not optimizing, also output the static functions. (see
423 PR24561), but don't do so for always_inline functions, functions
424 declared inline and nested functions. These were optimized out
425 in the original implementation and it is unclear whether we want
426 to change the behavior here. */
427 if ((!optimize
428 && !node->same_body_alias
429 && !DECL_DISREGARD_INLINE_LIMITS (decl)
430 && !DECL_DECLARED_INLINE_P (decl)
431 && !(DECL_CONTEXT (decl)
432 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
433 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
434 node->symbol.force_output = 1;
435
2c0b522d 436 /* If we've not yet emitted decl, tell the debug info about it. */
28df663b 437 if (!TREE_ASM_WRITTEN (decl))
2c0b522d 438 (*debug_hooks->deferred_inline_function) (decl);
4e8871a0 439
b69eb0ff 440 /* Possibly warn about unused parameters. */
441 if (warn_unused_parameter)
442 do_warn_unused_parameter (decl);
6329636b 443
444 if (!nested)
445 ggc_collect ();
9a2639fc 446
447 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
448 && (cgraph_decide_is_function_needed (node, decl)
449 || referred_to_p ((symtab_node)node)))
450 enqueue_node ((symtab_node)node);
ae01b312 451}
452
3db65b62 453/* Add the function FNDECL to the call graph.
454 Unlike cgraph_finalize_function, this function is intended to be used
455 by middle end and allows insertion of new function at arbitrary point
456 of compilation. The function can be either in high, low or SSA form
457 GIMPLE.
458
459 The function is assumed to be reachable and have address taken (so no
460 API breaking optimizations are performed on it).
461
462 Main work done by this function is to enqueue the function for later
463 processing to avoid need the passes to be re-entrant. */
464
465void
466cgraph_add_new_function (tree fndecl, bool lowered)
467{
468 struct cgraph_node *node;
469 switch (cgraph_state)
470 {
ff2a5ada 471 case CGRAPH_STATE_PARSING:
472 cgraph_finalize_function (fndecl, false);
473 break;
3db65b62 474 case CGRAPH_STATE_CONSTRUCTION:
475 /* Just enqueue function to be processed at nearest occurrence. */
476 node = cgraph_create_node (fndecl);
3db65b62 477 if (lowered)
478 node->lowered = true;
ff2a5ada 479 if (!cgraph_new_nodes)
480 cgraph_new_nodes = cgraph_node_set_new ();
481 cgraph_node_set_add (cgraph_new_nodes, node);
3db65b62 482 break;
483
484 case CGRAPH_STATE_IPA:
485 case CGRAPH_STATE_IPA_SSA:
486 case CGRAPH_STATE_EXPANSION:
487 /* Bring the function into finalized state and enqueue for later
488 analyzing and compilation. */
489 node = cgraph_get_create_node (fndecl);
490 node->local.local = false;
491 node->local.finalized = true;
da751785 492 node->symbol.force_output = true;
3db65b62 493 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
494 {
495 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
3db65b62 496 gimple_register_cfg_hooks ();
497 bitmap_obstack_initialize (NULL);
498 execute_pass_list (all_lowering_passes);
499 execute_pass_list (pass_early_local_passes.pass.sub);
500 bitmap_obstack_release (NULL);
501 pop_cfun ();
3db65b62 502
503 lowered = true;
504 }
505 if (lowered)
506 node->lowered = true;
ff2a5ada 507 if (!cgraph_new_nodes)
508 cgraph_new_nodes = cgraph_node_set_new ();
509 cgraph_node_set_add (cgraph_new_nodes, node);
3db65b62 510 break;
511
512 case CGRAPH_STATE_FINISHED:
513 /* At the very end of compilation we have to do all the work up
514 to expansion. */
515 node = cgraph_create_node (fndecl);
516 if (lowered)
517 node->lowered = true;
518 cgraph_analyze_function (node);
519 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
3db65b62 520 gimple_register_cfg_hooks ();
521 bitmap_obstack_initialize (NULL);
522 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
523 execute_pass_list (pass_early_local_passes.pass.sub);
524 bitmap_obstack_release (NULL);
3db65b62 525 pop_cfun ();
cf951b1a 526 expand_function (node);
3db65b62 527 break;
528
529 default:
530 gcc_unreachable ();
531 }
532
533 /* Set a personality if required and we already passed EH lowering. */
534 if (lowered
535 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
536 == eh_personality_lang))
537 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
538}
539
cf951b1a 540/* Add a top-level asm statement to the list. */
541
542struct asm_node *
543add_asm_node (tree asm_str)
544{
545 struct asm_node *node;
546
547 node = ggc_alloc_cleared_asm_node ();
548 node->asm_str = asm_str;
549 node->order = symtab_order++;
550 node->next = NULL;
551 if (asm_nodes == NULL)
552 asm_nodes = node;
553 else
554 asm_last_node->next = node;
555 asm_last_node = node;
556 return node;
557}
558
56af936e 559/* Output all asm statements we have stored up to be output. */
560
561static void
cf951b1a 562output_asm_statements (void)
56af936e 563{
cf951b1a 564 struct asm_node *can;
56af936e 565
852f689e 566 if (seen_error ())
56af936e 567 return;
568
cf951b1a 569 for (can = asm_nodes; can; can = can->next)
56af936e 570 assemble_asm (can->asm_str);
cf951b1a 571 asm_nodes = NULL;
572}
573
574/* C++ FE sometimes change linkage flags after producing same body aliases. */
575void
576fixup_same_cpp_alias_visibility (symtab_node node, symtab_node target, tree alias)
577{
578 DECL_VIRTUAL_P (node->symbol.decl) = DECL_VIRTUAL_P (alias);
579 if (TREE_PUBLIC (node->symbol.decl))
580 {
581 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (alias);
582 DECL_COMDAT (node->symbol.decl) = DECL_COMDAT (alias);
583 DECL_COMDAT_GROUP (node->symbol.decl) = DECL_COMDAT_GROUP (alias);
584 if (DECL_ONE_ONLY (alias)
585 && !node->symbol.same_comdat_group)
586 symtab_add_to_same_comdat_group ((symtab_node)node, (symtab_node)target);
587 }
56af936e 588}
589
0785e435 590/* Analyze the function scheduled to be output. */
da5e1e7c 591static void
0785e435 592cgraph_analyze_function (struct cgraph_node *node)
593{
7d0d0ce1 594 tree decl = node->symbol.decl;
da5e1e7c 595 location_t saved_loc = input_location;
596 input_location = DECL_SOURCE_LOCATION (decl);
0785e435 597
c70f46b0 598 if (node->alias && node->thunk.alias)
599 {
600 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
b0898cb7 601 struct cgraph_node *n;
602
603 for (n = tgt; n && n->alias;
604 n = n->analyzed ? cgraph_alias_aliased_node (n) : NULL)
605 if (n == node)
606 {
7d0d0ce1 607 error ("function %q+D part of alias cycle", node->symbol.decl);
b0898cb7 608 node->alias = false;
da5e1e7c 609 input_location = saved_loc;
b0898cb7 610 return;
611 }
f1f41a6c 612 if (!vec_safe_length (node->symbol.ref_list.references))
04ec15fa 613 ipa_record_reference ((symtab_node)node, (symtab_node)tgt,
614 IPA_REF_ALIAS, NULL);
c70f46b0 615 if (node->same_body_alias)
616 {
7d0d0ce1 617 DECL_DECLARED_INLINE_P (node->symbol.decl)
c70f46b0 618 = DECL_DECLARED_INLINE_P (node->thunk.alias);
7d0d0ce1 619 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl)
c70f46b0 620 = DECL_DISREGARD_INLINE_LIMITS (node->thunk.alias);
cf951b1a 621 fixup_same_cpp_alias_visibility ((symtab_node) node, (symtab_node) tgt, node->thunk.alias);
c70f46b0 622 }
623
7d0d0ce1 624 if (node->symbol.address_taken)
c70f46b0 625 cgraph_mark_address_taken_node (cgraph_alias_aliased_node (node));
c70f46b0 626 }
627 else if (node->thunk.thunk_p)
91bf9d9a 628 {
629 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
630 NULL, 0, CGRAPH_FREQ_BASE);
631 }
cc8ef84f 632 else if (node->dispatcher_function)
633 {
634 /* Generate the dispatcher body of multi-versioned functions. */
635 struct cgraph_function_version_info *dispatcher_version_info
636 = get_cgraph_node_version (node);
637 if (dispatcher_version_info != NULL
638 && (dispatcher_version_info->dispatcher_resolver
639 == NULL_TREE))
640 {
641 tree resolver = NULL_TREE;
642 gcc_assert (targetm.generate_version_dispatcher_body);
643 resolver = targetm.generate_version_dispatcher_body (node);
644 gcc_assert (resolver != NULL_TREE);
645 }
646 }
91bf9d9a 647 else
648 {
91bf9d9a 649 push_cfun (DECL_STRUCT_FUNCTION (decl));
bfec3452 650
7d0d0ce1 651 assign_assembler_name_if_neeeded (node->symbol.decl);
6816d0c4 652
91bf9d9a 653 /* Make sure to gimplify bodies only once. During analyzing a
654 function we lower it, which will require gimplified nested
655 functions, so we can end up here with an already gimplified
656 body. */
e3a19533 657 if (!gimple_has_body_p (decl))
91bf9d9a 658 gimplify_function_tree (decl);
659 dump_function (TDI_generic, decl);
bfec3452 660
47199071 661 /* Lower the function. */
662 if (!node->lowered)
663 {
664 if (node->nested)
7d0d0ce1 665 lower_nested_functions (node->symbol.decl);
47199071 666 gcc_assert (!node->nested);
667
668 gimple_register_cfg_hooks ();
669 bitmap_obstack_initialize (NULL);
670 execute_pass_list (all_lowering_passes);
671 free_dominance_info (CDI_POST_DOMINATORS);
672 free_dominance_info (CDI_DOMINATORS);
673 compact_blocks ();
674 bitmap_obstack_release (NULL);
675 node->lowered = true;
676 }
677
91bf9d9a 678 pop_cfun ();
679 }
6e8d6e86 680 node->analyzed = true;
0785e435 681
da5e1e7c 682 input_location = saved_loc;
0785e435 683}
684
c70f46b0 685/* C++ frontend produce same body aliases all over the place, even before PCH
686 gets streamed out. It relies on us linking the aliases with their function
687 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
688 first produce aliases without links, but once C++ FE is sure he won't sream
689 PCH we build the links via this function. */
690
691void
692cgraph_process_same_body_aliases (void)
693{
694 struct cgraph_node *node;
7c455d87 695 FOR_EACH_FUNCTION (node)
c70f46b0 696 if (node->same_body_alias
f1f41a6c 697 && !vec_safe_length (node->symbol.ref_list.references))
c70f46b0 698 {
699 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
04ec15fa 700 ipa_record_reference ((symtab_node)node, (symtab_node)tgt,
701 IPA_REF_ALIAS, NULL);
c70f46b0 702 }
703 same_body_aliases_done = true;
704}
705
d05db70d 706/* Process attributes common for vars and functions. */
707
708static void
709process_common_attributes (tree decl)
710{
711 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
712
713 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
714 {
715 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
716 "%<weakref%> attribute should be accompanied with"
717 " an %<alias%> attribute");
718 DECL_WEAK (decl) = 0;
40b32d93 719 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
720 DECL_ATTRIBUTES (decl));
d05db70d 721 }
722}
723
05806473 724/* Look for externally_visible and used attributes and mark cgraph nodes
725 accordingly.
726
727 We cannot mark the nodes at the point the attributes are processed (in
728 handle_*_attribute) because the copy of the declarations available at that
729 point may not be canonical. For example, in:
730
731 void f();
732 void f() __attribute__((used));
733
734 the declaration we see in handle_used_attribute will be the second
735 declaration -- but the front end will subsequently merge that declaration
736 with the original declaration and discard the second declaration.
737
738 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
739
740 void f() {}
741 void f() __attribute__((externally_visible));
742
743 is valid.
744
745 So, we walk the nodes at the end of the translation unit, applying the
746 attributes at that point. */
747
748static void
749process_function_and_variable_attributes (struct cgraph_node *first,
1d416bd7 750 struct varpool_node *first_var)
05806473 751{
752 struct cgraph_node *node;
1d416bd7 753 struct varpool_node *vnode;
05806473 754
0704fb2e 755 for (node = cgraph_first_function (); node != first;
756 node = cgraph_next_function (node))
05806473 757 {
7d0d0ce1 758 tree decl = node->symbol.decl;
83a23b05 759 if (DECL_PRESERVE_P (decl))
8efa224a 760 cgraph_mark_force_output_node (node);
62433d51 761 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
05806473 762 {
7d0d0ce1 763 if (! TREE_PUBLIC (node->symbol.decl))
764 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
712d2297 765 "%<externally_visible%>"
766 " attribute have effect only on public objects");
05806473 767 }
40b32d93 768 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
c70f46b0 769 && (node->local.finalized && !node->alias))
40b32d93 770 {
7d0d0ce1 771 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
40b32d93 772 "%<weakref%> attribute ignored"
773 " because function is defined");
774 DECL_WEAK (decl) = 0;
775 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
776 DECL_ATTRIBUTES (decl));
777 }
a522e9eb 778
779 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
780 && !DECL_DECLARED_INLINE_P (decl)
781 /* redefining extern inline function makes it DECL_UNINLINABLE. */
782 && !DECL_UNINLINABLE (decl))
783 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
784 "always_inline function might not be inlinable");
785
d05db70d 786 process_common_attributes (decl);
05806473 787 }
0704fb2e 788 for (vnode = varpool_first_variable (); vnode != first_var;
789 vnode = varpool_next_variable (vnode))
05806473 790 {
7d0d0ce1 791 tree decl = vnode->symbol.decl;
aa419a52 792 if (DECL_EXTERNAL (decl)
793 && DECL_INITIAL (decl)
794 && const_value_known_p (decl))
795 varpool_finalize_decl (decl);
83a23b05 796 if (DECL_PRESERVE_P (decl))
ff2a5ada 797 vnode->symbol.force_output = true;
62433d51 798 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
05806473 799 {
7d0d0ce1 800 if (! TREE_PUBLIC (vnode->symbol.decl))
801 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
712d2297 802 "%<externally_visible%>"
803 " attribute have effect only on public objects");
05806473 804 }
40b32d93 805 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
806 && vnode->finalized
807 && DECL_INITIAL (decl))
808 {
7d0d0ce1 809 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
40b32d93 810 "%<weakref%> attribute ignored"
811 " because variable is initialized");
812 DECL_WEAK (decl) = 0;
813 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
814 DECL_ATTRIBUTES (decl));
815 }
d05db70d 816 process_common_attributes (decl);
05806473 817 }
818}
819
ff2a5ada 820/* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
821 middle end to output the variable to asm file, if needed or externally
822 visible. */
823
824void
825varpool_finalize_decl (tree decl)
826{
2dc9831f 827 struct varpool_node *node = varpool_node_for_decl (decl);
ff2a5ada 828
2d4bf241 829 gcc_assert (TREE_STATIC (decl) || DECL_EXTERNAL (decl));
ff2a5ada 830
831 if (node->finalized)
832 return;
833 notice_global_symbol (decl);
834 node->finalized = true;
835 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
836 /* Traditionally we do not eliminate static variables when not
837 optimizing and when not doing toplevel reoder. */
838 || (!flag_toplevel_reorder && !DECL_COMDAT (node->symbol.decl)
839 && !DECL_ARTIFICIAL (node->symbol.decl)))
840 node->symbol.force_output = true;
841
842 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
843 && (decide_is_variable_needed (node, decl)
844 || referred_to_p ((symtab_node)node)))
845 enqueue_node ((symtab_node)node);
846 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
847 varpool_analyze_node (node);
3d1c0354 848 /* Some frontends produce various interface variables after compilation
849 finished. */
850 if (cgraph_state == CGRAPH_STATE_FINISHED)
851 varpool_assemble_decl (node);
ff2a5ada 852}
853
2dc9831f 854
855/* Determine if a symbol NODE is finalized and needed. */
856
857inline static bool
858symbol_finalized_and_needed (symtab_node node)
859{
860 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
861 return cnode->local.finalized
862 && cgraph_decide_is_function_needed (cnode, cnode->symbol.decl);
863 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
864 return vnode->finalized
865 && !DECL_EXTERNAL (vnode->symbol.decl)
866 && decide_is_variable_needed (vnode, vnode->symbol.decl);
867 return false;
868}
869
870/* Determine if a symbol NODE is finalized. */
871
872inline static bool
873symbol_finalized (symtab_node node)
874{
875 if (cgraph_node *cnode= dyn_cast <cgraph_node> (node))
876 return cnode->local.finalized;
877 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
878 return vnode->finalized;
879 return false;
880}
881
882
ff2a5ada 883/* Discover all functions and variables that are trivially needed, analyze
884 them as well as all functions and variables referred by them */
ae01b312 885
aeeb194b 886static void
887cgraph_analyze_functions (void)
ae01b312 888{
c1dcd13c 889 /* Keep track of already processed nodes when called multiple times for
06b27565 890 intermodule optimization. */
c1dcd13c 891 static struct cgraph_node *first_analyzed;
ff2a5ada 892 struct cgraph_node *first_handled = first_analyzed;
1d416bd7 893 static struct varpool_node *first_analyzed_var;
ff2a5ada 894 struct varpool_node *first_handled_var = first_analyzed_var;
895
896 symtab_node node, next;
897 int i;
898 struct ipa_ref *ref;
899 bool changed = true;
ae01b312 900
f1c35659 901 bitmap_obstack_initialize (NULL);
ff2a5ada 902 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
ae01b312 903
ff2a5ada 904 /* Analysis adds static variables that in turn adds references to new functions.
905 So we need to iterate the process until it stabilize. */
906 while (changed)
ae01b312 907 {
ff2a5ada 908 changed = false;
909 process_function_and_variable_attributes (first_analyzed,
910 first_analyzed_var);
911
912 /* First identify the trivially needed symbols. */
913 for (node = symtab_nodes;
914 node != (symtab_node)first_analyzed
915 && node != (symtab_node)first_analyzed_var; node = node->symbol.next)
9b8fb23a 916 {
2dc9831f 917 if (symbol_finalized_and_needed (node))
ff2a5ada 918 {
919 enqueue_node (node);
920 if (!changed && cgraph_dump_file)
921 fprintf (cgraph_dump_file, "Trivially needed symbols:");
922 changed = true;
923 if (cgraph_dump_file)
924 fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node));
925 }
926 if (node == (symtab_node)first_analyzed
927 || node == (symtab_node)first_analyzed_var)
928 break;
9b8fb23a 929 }
ff2a5ada 930 cgraph_process_new_functions ();
931 first_analyzed_var = varpool_first_variable ();
932 first_analyzed = cgraph_first_function ();
638531ad 933
ff2a5ada 934 if (changed && dump_file)
935 fprintf (cgraph_dump_file, "\n");
2c0b522d 936
ff2a5ada 937 /* Lower representation, build callgraph edges and references for all trivially
938 needed symbols and all symbols referred by them. */
939 while (first != (symtab_node)(void *)1)
61c2c7b1 940 {
ff2a5ada 941 changed = true;
942 node = first;
943 first = (symtab_node)first->symbol.aux;
2dc9831f 944 cgraph_node *cnode = dyn_cast <cgraph_node> (node);
945 if (cnode && cnode->local.finalized)
ff2a5ada 946 {
947 struct cgraph_edge *edge;
2dc9831f 948 tree decl = cnode->symbol.decl;
ff2a5ada 949
2dc9831f 950 /* ??? It is possible to create extern inline function
951 and later using weak alias attribute to kill its body.
952 See gcc.c-torture/compile/20011119-1.c */
ff2a5ada 953 if (!DECL_STRUCT_FUNCTION (decl)
954 && (!cnode->alias || !cnode->thunk.alias)
cc8ef84f 955 && !cnode->thunk.thunk_p
956 && !cnode->dispatcher_function)
ff2a5ada 957 {
958 cgraph_reset_node (cnode);
959 cnode->local.redefined_extern_inline = true;
960 continue;
961 }
61c2c7b1 962
ff2a5ada 963 if (!cnode->analyzed)
964 cgraph_analyze_function (cnode);
d544ceff 965
ff2a5ada 966 for (edge = cnode->callees; edge; edge = edge->next_callee)
da751785 967 if (edge->callee->local.finalized)
2dc9831f 968 enqueue_node ((symtab_node)edge->callee);
ff2a5ada 969
2dc9831f 970 /* If decl is a clone of an abstract function,
971 mark that abstract function so that we don't release its body.
972 The DECL_INITIAL() of that abstract function declaration
973 will be later needed to output debug info. */
ff2a5ada 974 if (DECL_ABSTRACT_ORIGIN (decl))
975 {
2dc9831f 976 struct cgraph_node *origin_node
977 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
ff2a5ada 978 origin_node->abstract_and_needed = true;
979 }
ff2a5ada 980 }
2dc9831f 981 else
982 {
983 varpool_node *vnode = dyn_cast <varpool_node> (node);
984 if (vnode && vnode->finalized)
985 varpool_analyze_node (vnode);
986 }
ff2a5ada 987
988 if (node->symbol.same_comdat_group)
989 {
990 symtab_node next;
991 for (next = node->symbol.same_comdat_group;
992 next != node;
993 next = next->symbol.same_comdat_group)
994 enqueue_node (next);
995 }
996 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
2dc9831f 997 if (symbol_finalized (ref->referred))
ff2a5ada 998 enqueue_node (ref->referred);
999 cgraph_process_new_functions ();
1000 }
ae01b312 1001 }
2c0b522d 1002
aa5e06c7 1003 /* Collect entry points to the unit. */
f79b6507 1004 if (cgraph_dump_file)
3d7bfc56 1005 {
e4200070 1006 fprintf (cgraph_dump_file, "\n\nInitial ");
18841b0c 1007 dump_symtab (cgraph_dump_file);
3d7bfc56 1008 }
e6d2b2d8 1009
f79b6507 1010 if (cgraph_dump_file)
ff2a5ada 1011 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
ae01b312 1012
ff2a5ada 1013 for (node = symtab_nodes;
1014 node != (symtab_node)first_handled
1015 && node != (symtab_node)first_handled_var; node = next)
ae01b312 1016 {
ff2a5ada 1017 next = node->symbol.next;
1018 if (!node->symbol.aux && !referred_to_p (node))
ae01b312 1019 {
f79b6507 1020 if (cgraph_dump_file)
ff2a5ada 1021 fprintf (cgraph_dump_file, " %s", symtab_node_name (node));
1022 symtab_remove_node (node);
9b8fb23a 1023 continue;
ae01b312 1024 }
2dc9831f 1025 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
ff2a5ada 1026 {
1027 tree decl = node->symbol.decl;
ff2a5ada 1028
1029 if (cnode->local.finalized && !gimple_has_body_p (decl)
1030 && (!cnode->alias || !cnode->thunk.alias)
1031 && !cnode->thunk.thunk_p)
1032 cgraph_reset_node (cnode);
1033
1034 gcc_assert (!cnode->local.finalized || cnode->thunk.thunk_p
1035 || cnode->alias
1036 || gimple_has_body_p (decl));
1037 gcc_assert (cnode->analyzed == cnode->local.finalized);
1038 }
1039 node->symbol.aux = NULL;
ae01b312 1040 }
ff2a5ada 1041 first_analyzed = cgraph_first_function ();
1042 first_analyzed_var = varpool_first_variable ();
f79b6507 1043 if (cgraph_dump_file)
e4200070 1044 {
1045 fprintf (cgraph_dump_file, "\n\nReclaimed ");
18841b0c 1046 dump_symtab (cgraph_dump_file);
e4200070 1047 }
f1c35659 1048 bitmap_obstack_release (NULL);
ae01b312 1049 ggc_collect ();
aeeb194b 1050}
1051
3a849bc1 1052/* Translate the ugly representation of aliases as alias pairs into nice
1053 representation in callgraph. We don't handle all cases yet,
1054 unforutnately. */
1055
1056static void
1057handle_alias_pairs (void)
1058{
1059 alias_pair *p;
1060 unsigned i;
3a849bc1 1061
f1f41a6c 1062 for (i = 0; alias_pairs && alias_pairs->iterate (i, &p);)
3a849bc1 1063 {
48c84ee3 1064 symtab_node target_node = symtab_node_for_asm (p->target);
1065
badeded8 1066 /* Weakrefs with target not defined in current unit are easy to handle; they
1067 behave just as external variables except we need to note the alias flag
1068 to later output the weakref pseudo op into asm file. */
48c84ee3 1069 if (!target_node && lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL)
badeded8 1070 {
1071 if (TREE_CODE (p->decl) == FUNCTION_DECL)
fc8456b4 1072 {
1073 struct cgraph_node *anode = cgraph_get_create_node (p->decl);
1074 anode->alias = true;
1075 anode->thunk.alias = p->target;
1076 }
badeded8 1077 else
fc8456b4 1078 {
1079 struct varpool_node *anode = varpool_get_node (p->decl);
1080 anode->alias = true;
1081 anode->alias_of = p->target;
1082 }
badeded8 1083 DECL_EXTERNAL (p->decl) = 1;
f1f41a6c 1084 alias_pairs->unordered_remove (i);
48c84ee3 1085 continue;
badeded8 1086 }
48c84ee3 1087 else if (!target_node)
3a849bc1 1088 {
48c84ee3 1089 error ("%q+D aliased to undefined symbol %qE", p->decl, p->target);
f1f41a6c 1090 alias_pairs->unordered_remove (i);
48c84ee3 1091 continue;
1092 }
1093
1094 /* Normally EXTERNAL flag is used to mark external inlines,
1095 however for aliases it seems to be allowed to use it w/o
1096 any meaning. See gcc.dg/attr-alias-3.c
1097 However for weakref we insist on EXTERNAL flag being set.
1098 See gcc.dg/attr-alias-5.c */
1099 if (DECL_EXTERNAL (p->decl))
1100 DECL_EXTERNAL (p->decl)
1101 = lookup_attribute ("weakref",
1102 DECL_ATTRIBUTES (p->decl)) != NULL;
3a849bc1 1103
afea39ad 1104 if (DECL_EXTERNAL (target_node->symbol.decl)
1105 /* We use local aliases for C++ thunks to force the tailcall
1106 to bind locally. This is a hack - to keep it working do
1107 the following (which is not strictly correct). */
1108 && (! TREE_CODE (target_node->symbol.decl) == FUNCTION_DECL
1109 || ! DECL_VIRTUAL_P (target_node->symbol.decl))
1110 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
1111 {
1112 error ("%q+D aliased to external symbol %qE",
1113 p->decl, p->target);
1114 }
1115
48c84ee3 1116 if (TREE_CODE (p->decl) == FUNCTION_DECL
2dc9831f 1117 && target_node && is_a <cgraph_node> (target_node))
48c84ee3 1118 {
1119 struct cgraph_node *src_node = cgraph_get_node (p->decl);
1120 if (src_node && src_node->local.finalized)
1121 cgraph_reset_node (src_node);
1122 cgraph_create_function_alias (p->decl, target_node->symbol.decl);
f1f41a6c 1123 alias_pairs->unordered_remove (i);
48c84ee3 1124 }
1125 else if (TREE_CODE (p->decl) == VAR_DECL
2dc9831f 1126 && target_node && is_a <varpool_node> (target_node))
48c84ee3 1127 {
1128 varpool_create_variable_alias (p->decl, target_node->symbol.decl);
f1f41a6c 1129 alias_pairs->unordered_remove (i);
48c84ee3 1130 }
1131 else
1132 {
1133 error ("%q+D alias in between function and variable is not supported",
1134 p->decl);
1135 warning (0, "%q+D aliased declaration",
1136 target_node->symbol.decl);
f1f41a6c 1137 alias_pairs->unordered_remove (i);
3a849bc1 1138 }
1139 }
f1f41a6c 1140 vec_free (alias_pairs);
3a849bc1 1141}
1142
8f69fd82 1143
ae01b312 1144/* Figure out what functions we want to assemble. */
1145
1146static void
cf951b1a 1147mark_functions_to_output (void)
ae01b312 1148{
1149 struct cgraph_node *node;
61c2c7b1 1150#ifdef ENABLE_CHECKING
1151 bool check_same_comdat_groups = false;
1152
7c455d87 1153 FOR_EACH_FUNCTION (node)
61c2c7b1 1154 gcc_assert (!node->process);
1155#endif
ae01b312 1156
7c455d87 1157 FOR_EACH_FUNCTION (node)
ae01b312 1158 {
7d0d0ce1 1159 tree decl = node->symbol.decl;
a0c938f0 1160
7d0d0ce1 1161 gcc_assert (!node->process || node->symbol.same_comdat_group);
61c2c7b1 1162 if (node->process)
1163 continue;
d7c6d889 1164
e6d2b2d8 1165 /* We need to output all local functions that are used and not
1166 always inlined, as well as those that are reachable from
1167 outside the current compilation unit. */
1a1a827a 1168 if (node->analyzed
91bf9d9a 1169 && !node->thunk.thunk_p
c70f46b0 1170 && !node->alias
b0cdf642 1171 && !node->global.inlined_to
4ee9c684 1172 && !TREE_ASM_WRITTEN (decl)
ae01b312 1173 && !DECL_EXTERNAL (decl))
61c2c7b1 1174 {
1175 node->process = 1;
7d0d0ce1 1176 if (node->symbol.same_comdat_group)
61c2c7b1 1177 {
1178 struct cgraph_node *next;
7d0d0ce1 1179 for (next = cgraph (node->symbol.same_comdat_group);
61c2c7b1 1180 next != node;
7d0d0ce1 1181 next = cgraph (next->symbol.same_comdat_group))
c70f46b0 1182 if (!next->thunk.thunk_p && !next->alias)
91bf9d9a 1183 next->process = 1;
61c2c7b1 1184 }
1185 }
7d0d0ce1 1186 else if (node->symbol.same_comdat_group)
61c2c7b1 1187 {
1188#ifdef ENABLE_CHECKING
1189 check_same_comdat_groups = true;
1190#endif
1191 }
cc636d56 1192 else
9cee7c3f 1193 {
1194 /* We should've reclaimed all functions that are not needed. */
1195#ifdef ENABLE_CHECKING
75a70cf9 1196 if (!node->global.inlined_to
1a1a827a 1197 && gimple_has_body_p (decl)
08843223 1198 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1199 are inside partition, we can end up not removing the body since we no longer
1200 have analyzed node pointing to it. */
7d0d0ce1 1201 && !node->symbol.in_other_partition
c70f46b0 1202 && !node->alias
2d4bf241 1203 && !node->clones
9cee7c3f 1204 && !DECL_EXTERNAL (decl))
1205 {
1206 dump_cgraph_node (stderr, node);
1207 internal_error ("failed to reclaim unneeded function");
1208 }
1209#endif
75a70cf9 1210 gcc_assert (node->global.inlined_to
1a1a827a 1211 || !gimple_has_body_p (decl)
7d0d0ce1 1212 || node->symbol.in_other_partition
aa419a52 1213 || node->clones
1214 || DECL_ARTIFICIAL (decl)
9cee7c3f 1215 || DECL_EXTERNAL (decl));
1216
1217 }
a0c938f0 1218
961e3b13 1219 }
61c2c7b1 1220#ifdef ENABLE_CHECKING
1221 if (check_same_comdat_groups)
7c455d87 1222 FOR_EACH_FUNCTION (node)
7d0d0ce1 1223 if (node->symbol.same_comdat_group && !node->process)
61c2c7b1 1224 {
7d0d0ce1 1225 tree decl = node->symbol.decl;
61c2c7b1 1226 if (!node->global.inlined_to
1227 && gimple_has_body_p (decl)
6d36105a 1228 /* FIXME: in an ltrans unit when the offline copy is outside a
1229 partition but inline copies are inside a partition, we can
1230 end up not removing the body since we no longer have an
1231 analyzed node pointing to it. */
7d0d0ce1 1232 && !node->symbol.in_other_partition
afea39ad 1233 && !node->clones
61c2c7b1 1234 && !DECL_EXTERNAL (decl))
1235 {
1236 dump_cgraph_node (stderr, node);
6d36105a 1237 internal_error ("failed to reclaim unneeded function in same "
1238 "comdat group");
61c2c7b1 1239 }
1240 }
1241#endif
961e3b13 1242}
1243
28454517 1244/* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
cc8ef84f 1245 in lowered gimple form. IN_SSA is true if the gimple is in SSA.
28454517 1246
1247 Set current_function_decl and cfun to newly constructed empty function body.
1248 return basic block in the function body. */
1249
cc8ef84f 1250basic_block
1251init_lowered_empty_function (tree decl, bool in_ssa)
28454517 1252{
1253 basic_block bb;
1254
1255 current_function_decl = decl;
1256 allocate_struct_function (decl, false);
1257 gimple_register_cfg_hooks ();
1258 init_empty_tree_cfg ();
cc8ef84f 1259
1260 if (in_ssa)
1261 {
1262 init_tree_ssa (cfun);
1263 init_ssa_operands (cfun);
1264 cfun->gimple_df->in_ssa_p = true;
1265 }
1266
28454517 1267 DECL_INITIAL (decl) = make_node (BLOCK);
1268
1269 DECL_SAVED_TREE (decl) = error_mark_node;
1270 cfun->curr_properties |=
b03e5397 1271 (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_ssa | PROP_gimple_any);
28454517 1272
1273 /* Create BB for body of the function and connect it properly. */
1274 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
167ef6d9 1275 make_edge (ENTRY_BLOCK_PTR, bb, 0);
1276 make_edge (bb, EXIT_BLOCK_PTR, 0);
28454517 1277
1278 return bb;
1279}
1280
1281/* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1282 offset indicated by VIRTUAL_OFFSET, if that is
1283 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1284 zero for a result adjusting thunk. */
1285
1286static tree
1287thunk_adjust (gimple_stmt_iterator * bsi,
1288 tree ptr, bool this_adjusting,
1289 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1290{
1291 gimple stmt;
1292 tree ret;
1293
55d6cb23 1294 if (this_adjusting
1295 && fixed_offset != 0)
28454517 1296 {
2cc66f2a 1297 stmt = gimple_build_assign
1298 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1299 ptr,
1300 fixed_offset));
28454517 1301 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1302 }
1303
1304 /* If there's a virtual offset, look up that value in the vtable and
1305 adjust the pointer again. */
1306 if (virtual_offset)
1307 {
1308 tree vtabletmp;
1309 tree vtabletmp2;
1310 tree vtabletmp3;
28454517 1311
1312 if (!vtable_entry_type)
1313 {
1314 tree vfunc_type = make_node (FUNCTION_TYPE);
1315 TREE_TYPE (vfunc_type) = integer_type_node;
1316 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1317 layout_type (vfunc_type);
1318
1319 vtable_entry_type = build_pointer_type (vfunc_type);
1320 }
1321
1322 vtabletmp =
072f7ab1 1323 create_tmp_reg (build_pointer_type
37ffa8fe 1324 (build_pointer_type (vtable_entry_type)), "vptr");
28454517 1325
1326 /* The vptr is always at offset zero in the object. */
1327 stmt = gimple_build_assign (vtabletmp,
1328 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1329 ptr));
1330 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
28454517 1331
1332 /* Form the vtable address. */
072f7ab1 1333 vtabletmp2 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp)),
37ffa8fe 1334 "vtableaddr");
28454517 1335 stmt = gimple_build_assign (vtabletmp2,
182cf5a9 1336 build_simple_mem_ref (vtabletmp));
28454517 1337 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
28454517 1338
1339 /* Find the entry with the vcall offset. */
1340 stmt = gimple_build_assign (vtabletmp2,
2cc66f2a 1341 fold_build_pointer_plus_loc (input_location,
1342 vtabletmp2,
1343 virtual_offset));
28454517 1344 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1345
1346 /* Get the offset itself. */
072f7ab1 1347 vtabletmp3 = create_tmp_reg (TREE_TYPE (TREE_TYPE (vtabletmp2)),
37ffa8fe 1348 "vcalloffset");
28454517 1349 stmt = gimple_build_assign (vtabletmp3,
182cf5a9 1350 build_simple_mem_ref (vtabletmp2));
28454517 1351 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
28454517 1352
28454517 1353 /* Adjust the `this' pointer. */
a0553bff 1354 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1355 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1356 GSI_CONTINUE_LINKING);
28454517 1357 }
1358
55d6cb23 1359 if (!this_adjusting
1360 && fixed_offset != 0)
28454517 1361 /* Adjust the pointer by the constant. */
1362 {
1363 tree ptrtmp;
1364
1365 if (TREE_CODE (ptr) == VAR_DECL)
1366 ptrtmp = ptr;
1367 else
1368 {
072f7ab1 1369 ptrtmp = create_tmp_reg (TREE_TYPE (ptr), "ptr");
28454517 1370 stmt = gimple_build_assign (ptrtmp, ptr);
1371 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
28454517 1372 }
2cc66f2a 1373 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1374 ptrtmp, fixed_offset);
28454517 1375 }
1376
1377 /* Emit the statement and gimplify the adjustment expression. */
072f7ab1 1378 ret = create_tmp_reg (TREE_TYPE (ptr), "adjusted_this");
28454517 1379 stmt = gimple_build_assign (ret, ptr);
28454517 1380 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1381
1382 return ret;
1383}
1384
1385/* Produce assembler for thunk NODE. */
1386
1387static void
1388assemble_thunk (struct cgraph_node *node)
1389{
1390 bool this_adjusting = node->thunk.this_adjusting;
1391 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1392 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1393 tree virtual_offset = NULL;
1394 tree alias = node->thunk.alias;
7d0d0ce1 1395 tree thunk_fndecl = node->symbol.decl;
28454517 1396 tree a = DECL_ARGUMENTS (thunk_fndecl);
1397
1398 current_function_decl = thunk_fndecl;
1399
aed6e608 1400 /* Ensure thunks are emitted in their correct sections. */
1401 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1402
28454517 1403 if (this_adjusting
1404 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1405 virtual_value, alias))
1406 {
1407 const char *fnname;
1408 tree fn_block;
28b2c6a7 1409 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
28454517 1410
1411 DECL_RESULT (thunk_fndecl)
1412 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
28b2c6a7 1413 RESULT_DECL, 0, restype);
22ea3b47 1414 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
28454517 1415
1416 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1417 create one. */
1418 fn_block = make_node (BLOCK);
1419 BLOCK_VARS (fn_block) = a;
1420 DECL_INITIAL (thunk_fndecl) = fn_block;
1421 init_function_start (thunk_fndecl);
1422 cfun->is_thunk = 1;
32c7c682 1423 insn_locations_init ();
1424 set_curr_insn_location (DECL_SOURCE_LOCATION (thunk_fndecl));
1425 prologue_location = curr_insn_location ();
28454517 1426 assemble_start_function (thunk_fndecl, fnname);
1427
1428 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1429 fixed_offset, virtual_value, alias);
1430
1431 assemble_end_function (thunk_fndecl, fnname);
32c7c682 1432 insn_locations_finalize ();
28454517 1433 init_insn_lengths ();
1434 free_after_compilation (cfun);
1435 set_cfun (NULL);
1436 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
91bf9d9a 1437 node->thunk.thunk_p = false;
1438 node->analyzed = false;
28454517 1439 }
1440 else
1441 {
1442 tree restype;
1443 basic_block bb, then_bb, else_bb, return_bb;
1444 gimple_stmt_iterator bsi;
1445 int nargs = 0;
1446 tree arg;
1447 int i;
1448 tree resdecl;
1449 tree restmp = NULL;
f1f41a6c 1450 vec<tree> vargs;
28454517 1451
1452 gimple call;
1453 gimple ret;
1454
1455 DECL_IGNORED_P (thunk_fndecl) = 1;
1456 bitmap_obstack_initialize (NULL);
1457
1458 if (node->thunk.virtual_offset_p)
1459 virtual_offset = size_int (virtual_value);
1460
1461 /* Build the return declaration for the function. */
1462 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1463 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1464 {
1465 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1466 DECL_ARTIFICIAL (resdecl) = 1;
1467 DECL_IGNORED_P (resdecl) = 1;
1468 DECL_RESULT (thunk_fndecl) = resdecl;
1469 }
1470 else
1471 resdecl = DECL_RESULT (thunk_fndecl);
1472
cc8ef84f 1473 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl, true);
28454517 1474
1475 bsi = gsi_start_bb (bb);
1476
1477 /* Build call to the function being thunked. */
1478 if (!VOID_TYPE_P (restype))
1479 {
1480 if (!is_gimple_reg_type (restype))
1481 {
1482 restmp = resdecl;
2ab2ce89 1483 add_local_decl (cfun, restmp);
28454517 1484 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1485 }
1486 else
072f7ab1 1487 restmp = create_tmp_reg (restype, "retval");
28454517 1488 }
1489
1767a056 1490 for (arg = a; arg; arg = DECL_CHAIN (arg))
28454517 1491 nargs++;
f1f41a6c 1492 vargs.create (nargs);
28454517 1493 if (this_adjusting)
f1f41a6c 1494 vargs.quick_push (thunk_adjust (&bsi, a, 1, fixed_offset,
1495 virtual_offset));
28454517 1496 else
f1f41a6c 1497 vargs.quick_push (a);
1767a056 1498 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
f1f41a6c 1499 vargs.quick_push (arg);
28454517 1500 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
f1f41a6c 1501 vargs.release ();
28454517 1502 gimple_call_set_from_thunk (call, true);
1503 if (restmp)
1504 gimple_call_set_lhs (call, restmp);
1505 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
28454517 1506
1507 if (restmp && !this_adjusting)
1508 {
57ab8ec3 1509 tree true_label = NULL_TREE;
28454517 1510
1511 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1512 {
1513 gimple stmt;
1514 /* If the return type is a pointer, we need to
1515 protect against NULL. We know there will be an
1516 adjustment, because that's why we're emitting a
1517 thunk. */
1518 then_bb = create_basic_block (NULL, (void *) 0, bb);
1519 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1520 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1521 remove_edge (single_succ_edge (bb));
1522 true_label = gimple_block_label (then_bb);
28454517 1523 stmt = gimple_build_cond (NE_EXPR, restmp,
385f3f36 1524 build_zero_cst (TREE_TYPE (restmp)),
28454517 1525 NULL_TREE, NULL_TREE);
1526 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1527 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1528 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1529 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1530 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1531 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1532 bsi = gsi_last_bb (then_bb);
1533 }
1534
1535 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1536 fixed_offset, virtual_offset);
1537 if (true_label)
1538 {
1539 gimple stmt;
1540 bsi = gsi_last_bb (else_bb);
385f3f36 1541 stmt = gimple_build_assign (restmp,
1542 build_zero_cst (TREE_TYPE (restmp)));
28454517 1543 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1544 bsi = gsi_last_bb (return_bb);
1545 }
1546 }
1547 else
1548 gimple_call_set_tail (call, true);
1549
1550 /* Build return value. */
1551 ret = gimple_build_return (restmp);
1552 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1553
1554 delete_unreachable_blocks ();
1555 update_ssa (TODO_update_ssa);
1556
28454517 1557 /* Since we want to emit the thunk, we explicitly mark its name as
1558 referenced. */
91bf9d9a 1559 node->thunk.thunk_p = false;
1560 cgraph_node_remove_callees (node);
28454517 1561 cgraph_add_new_function (thunk_fndecl, true);
1562 bitmap_obstack_release (NULL);
1563 }
1564 current_function_decl = NULL;
9078126c 1565 set_cfun (NULL);
28454517 1566}
1567
91bf9d9a 1568
c70f46b0 1569
9d75589a 1570/* Assemble thunks and aliases associated to NODE. */
91bf9d9a 1571
1572static void
c70f46b0 1573assemble_thunks_and_aliases (struct cgraph_node *node)
91bf9d9a 1574{
1575 struct cgraph_edge *e;
c70f46b0 1576 int i;
1577 struct ipa_ref *ref;
1578
91bf9d9a 1579 for (e = node->callers; e;)
1580 if (e->caller->thunk.thunk_p)
1581 {
1582 struct cgraph_node *thunk = e->caller;
1583
1584 e = e->next_caller;
c70f46b0 1585 assemble_thunks_and_aliases (thunk);
91bf9d9a 1586 assemble_thunk (thunk);
1587 }
1588 else
1589 e = e->next_caller;
04ec15fa 1590 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
7d0d0ce1 1591 i, ref); i++)
c70f46b0 1592 if (ref->use == IPA_REF_ALIAS)
1593 {
04ec15fa 1594 struct cgraph_node *alias = ipa_ref_referring_node (ref);
968b8c52 1595 bool saved_written = TREE_ASM_WRITTEN (alias->thunk.alias);
1596
1597 /* Force assemble_alias to really output the alias this time instead
1598 of buffering it in same alias pairs. */
1599 TREE_ASM_WRITTEN (alias->thunk.alias) = 1;
afea39ad 1600 do_assemble_alias (alias->symbol.decl,
1601 DECL_ASSEMBLER_NAME (alias->thunk.alias));
c70f46b0 1602 assemble_thunks_and_aliases (alias);
968b8c52 1603 TREE_ASM_WRITTEN (alias->thunk.alias) = saved_written;
c70f46b0 1604 }
91bf9d9a 1605}
1606
da5e1e7c 1607/* Expand function specified by NODE. */
941366fd 1608
3db65b62 1609static void
cf951b1a 1610expand_function (struct cgraph_node *node)
941366fd 1611{
da5e1e7c 1612 tree decl = node->symbol.decl;
941366fd 1613 location_t saved_loc;
1614
da5e1e7c 1615 /* We ought to not compile any inline clones. */
1616 gcc_assert (!node->global.inlined_to);
1617
1618 announce_function (decl);
1619 node->process = 0;
1620 gcc_assert (node->lowered);
1621
1622 /* Generate RTL for the body of DECL. */
1623
941366fd 1624 timevar_push (TV_REST_OF_COMPILATION);
1625
1626 gcc_assert (cgraph_global_info_ready);
1627
1628 /* Initialize the default bitmap obstack. */
1629 bitmap_obstack_initialize (NULL);
1630
1631 /* Initialize the RTL code for the function. */
da5e1e7c 1632 current_function_decl = decl;
941366fd 1633 saved_loc = input_location;
da5e1e7c 1634 input_location = DECL_SOURCE_LOCATION (decl);
1635 init_function_start (decl);
941366fd 1636
1637 gimple_register_cfg_hooks ();
1638
1639 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1640
1641 execute_all_ipa_transforms ();
1642
1643 /* Perform all tree transforms and optimizations. */
1644
1645 /* Signal the start of passes. */
1646 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1647
1648 execute_pass_list (all_passes);
1649
1650 /* Signal the end of passes. */
1651 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1652
1653 bitmap_obstack_release (&reg_obstack);
1654
1655 /* Release the default bitmap obstack. */
1656 bitmap_obstack_release (NULL);
1657
941366fd 1658 /* If requested, warn about function definitions where the function will
1659 return a value (usually of some struct or union type) which itself will
1660 take up a lot of stack space. */
da5e1e7c 1661 if (warn_larger_than && !DECL_EXTERNAL (decl) && TREE_TYPE (decl))
941366fd 1662 {
da5e1e7c 1663 tree ret_type = TREE_TYPE (TREE_TYPE (decl));
941366fd 1664
1665 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1666 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1667 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1668 larger_than_size))
1669 {
1670 unsigned int size_as_int
1671 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1672
1673 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1674 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
da5e1e7c 1675 decl, size_as_int);
941366fd 1676 else
1677 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
da5e1e7c 1678 decl, larger_than_size);
941366fd 1679 }
1680 }
1681
da5e1e7c 1682 gimple_set_body (decl, NULL);
1683 if (DECL_STRUCT_FUNCTION (decl) == 0
1684 && !cgraph_get_node (decl)->origin)
941366fd 1685 {
1686 /* Stop pointing to the local nodes about to be freed.
1687 But DECL_INITIAL must remain nonzero so we know this
1688 was an actual function definition.
1689 For a nested function, this is done in c_pop_function_context.
1690 If rest_of_compilation set this to 0, leave it 0. */
da5e1e7c 1691 if (DECL_INITIAL (decl) != 0)
1692 DECL_INITIAL (decl) = error_mark_node;
941366fd 1693 }
1694
1695 input_location = saved_loc;
1696
1697 ggc_collect ();
1698 timevar_pop (TV_REST_OF_COMPILATION);
f7777314 1699
1700 /* Make sure that BE didn't give up on compiling. */
1701 gcc_assert (TREE_ASM_WRITTEN (decl));
9078126c 1702 set_cfun (NULL);
f7777314 1703 current_function_decl = NULL;
f76f7453 1704
1705 /* It would make a lot more sense to output thunks before function body to get more
cf951b1a 1706 forward and lest backwarding jumps. This however would need solving problem
f76f7453 1707 with comdats. See PR48668. Also aliases must come after function itself to
cf951b1a 1708 make one pass assemblers, like one on AIX, happy. See PR 50689.
f76f7453 1709 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
1710 groups. */
1711 assemble_thunks_and_aliases (node);
1a1a827a 1712 cgraph_release_function_body (node);
1713 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1714 points to the dead function body. */
1715 cgraph_node_remove_callees (node);
ae01b312 1716}
1717
acc70efa 1718
d9d9733a 1719/* Expand all functions that must be output.
1720
d7c6d889 1721 Attempt to topologically sort the nodes so function is output when
1722 all called functions are already assembled to allow data to be
91c82c20 1723 propagated across the callgraph. Use a stack to get smaller distance
3927afe0 1724 between a function and its callees (later we may choose to use a more
d7c6d889 1725 sophisticated algorithm for function reordering; we will likely want
1726 to use subsections to make the output functions appear in top-down
1727 order). */
1728
1729static void
cf951b1a 1730expand_all_functions (void)
d7c6d889 1731{
1732 struct cgraph_node *node;
4c36ffe6 1733 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
c04e3894 1734 int order_pos, new_order_pos = 0;
d7c6d889 1735 int i;
1736
7771d558 1737 order_pos = ipa_reverse_postorder (order);
cc636d56 1738 gcc_assert (order_pos == cgraph_n_nodes);
d7c6d889 1739
7bd28bba 1740 /* Garbage collector may remove inline clones we eliminate during
b0cdf642 1741 optimization. So we must be sure to not reference them. */
1742 for (i = 0; i < order_pos; i++)
09fc9532 1743 if (order[i]->process)
b0cdf642 1744 order[new_order_pos++] = order[i];
1745
1746 for (i = new_order_pos - 1; i >= 0; i--)
d7c6d889 1747 {
1748 node = order[i];
09fc9532 1749 if (node->process)
d7c6d889 1750 {
09fc9532 1751 node->process = 0;
cf951b1a 1752 expand_function (node);
d7c6d889 1753 }
1754 }
523c1122 1755 cgraph_process_new_functions ();
773c5ba7 1756
d7c6d889 1757 free (order);
773c5ba7 1758
d7c6d889 1759}
1760
56af936e 1761/* This is used to sort the node types by the cgraph order number. */
1762
0b09525f 1763enum cgraph_order_sort_kind
1764{
1765 ORDER_UNDEFINED = 0,
1766 ORDER_FUNCTION,
1767 ORDER_VAR,
1768 ORDER_ASM
1769};
1770
56af936e 1771struct cgraph_order_sort
1772{
0b09525f 1773 enum cgraph_order_sort_kind kind;
56af936e 1774 union
1775 {
1776 struct cgraph_node *f;
1d416bd7 1777 struct varpool_node *v;
cf951b1a 1778 struct asm_node *a;
56af936e 1779 } u;
1780};
1781
1782/* Output all functions, variables, and asm statements in the order
1783 according to their order fields, which is the order in which they
1784 appeared in the file. This implements -fno-toplevel-reorder. In
1785 this mode we may output functions and variables which don't really
1786 need to be output. */
1787
1788static void
cf951b1a 1789output_in_order (void)
56af936e 1790{
1791 int max;
56af936e 1792 struct cgraph_order_sort *nodes;
1793 int i;
1794 struct cgraph_node *pf;
1d416bd7 1795 struct varpool_node *pv;
cf951b1a 1796 struct asm_node *pa;
56af936e 1797
0704fb2e 1798 max = symtab_order;
3e1cde87 1799 nodes = XCNEWVEC (struct cgraph_order_sort, max);
56af936e 1800
7c455d87 1801 FOR_EACH_DEFINED_FUNCTION (pf)
56af936e 1802 {
c70f46b0 1803 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
56af936e 1804 {
7d0d0ce1 1805 i = pf->symbol.order;
56af936e 1806 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1807 nodes[i].kind = ORDER_FUNCTION;
1808 nodes[i].u.f = pf;
1809 }
1810 }
1811
7c455d87 1812 FOR_EACH_DEFINED_VARIABLE (pv)
aa419a52 1813 if (!DECL_EXTERNAL (pv->symbol.decl))
1814 {
1815 i = pv->symbol.order;
1816 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1817 nodes[i].kind = ORDER_VAR;
1818 nodes[i].u.v = pv;
1819 }
56af936e 1820
cf951b1a 1821 for (pa = asm_nodes; pa; pa = pa->next)
56af936e 1822 {
1823 i = pa->order;
1824 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1825 nodes[i].kind = ORDER_ASM;
1826 nodes[i].u.a = pa;
1827 }
56af936e 1828
304e5318 1829 /* In toplevel reorder mode we output all statics; mark them as needed. */
304e5318 1830
91da0f1c 1831 for (i = 0; i < max; ++i)
1832 if (nodes[i].kind == ORDER_VAR)
1833 varpool_finalize_named_section_flags (nodes[i].u.v);
1834
56af936e 1835 for (i = 0; i < max; ++i)
1836 {
1837 switch (nodes[i].kind)
1838 {
1839 case ORDER_FUNCTION:
09fc9532 1840 nodes[i].u.f->process = 0;
cf951b1a 1841 expand_function (nodes[i].u.f);
56af936e 1842 break;
1843
1844 case ORDER_VAR:
1d416bd7 1845 varpool_assemble_decl (nodes[i].u.v);
56af936e 1846 break;
1847
1848 case ORDER_ASM:
1849 assemble_asm (nodes[i].u.a->asm_str);
1850 break;
1851
1852 case ORDER_UNDEFINED:
1853 break;
1854
1855 default:
1856 gcc_unreachable ();
1857 }
1858 }
4b4ea2db 1859
cf951b1a 1860 asm_nodes = NULL;
3e1cde87 1861 free (nodes);
56af936e 1862}
1863
77fce4cd 1864static void
1865ipa_passes (void)
1866{
87d4aa85 1867 set_cfun (NULL);
4b14adf9 1868 current_function_decl = NULL;
75a70cf9 1869 gimple_register_cfg_hooks ();
77fce4cd 1870 bitmap_obstack_initialize (NULL);
59dd4830 1871
c9036234 1872 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1873
59dd4830 1874 if (!in_lto_p)
7b2e8956 1875 {
1876 execute_ipa_pass_list (all_small_ipa_passes);
1877 if (seen_error ())
1878 return;
1879 }
9ed5b1f5 1880
941125aa 1881 /* We never run removal of unreachable nodes after early passes. This is
1882 because TODO is run before the subpasses. It is important to remove
1883 the unreachable functions to save works at IPA level and to get LTO
1884 symbol tables right. */
91f0ab48 1885 symtab_remove_unreachable_nodes (true, cgraph_dump_file);
941125aa 1886
7bfefa9d 1887 /* If pass_all_early_optimizations was not scheduled, the state of
1888 the cgraph will not be properly updated. Update it now. */
1889 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1890 cgraph_state = CGRAPH_STATE_IPA_SSA;
9ed5b1f5 1891
7bfefa9d 1892 if (!in_lto_p)
1893 {
1894 /* Generate coverage variables and constructors. */
1895 coverage_finish ();
1896
1897 /* Process new functions added. */
1898 set_cfun (NULL);
1899 current_function_decl = NULL;
1900 cgraph_process_new_functions ();
7bfefa9d 1901
c9036234 1902 execute_ipa_summary_passes
1903 ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
8867b500 1904 }
23433d72 1905
1906 /* Some targets need to handle LTO assembler output specially. */
1907 if (flag_generate_lto)
1908 targetm.asm_out.lto_start ();
1909
7bfefa9d 1910 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
1911
1912 if (!in_lto_p)
1913 ipa_write_summaries ();
1914
23433d72 1915 if (flag_generate_lto)
1916 targetm.asm_out.lto_end ();
1917
b33542ab 1918 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
8867b500 1919 execute_ipa_pass_list (all_regular_ipa_passes);
c9036234 1920 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
9ed5b1f5 1921
77fce4cd 1922 bitmap_obstack_release (NULL);
1923}
1924
badeded8 1925
1926/* Return string alias is alias of. */
1927
1928static tree
1929get_alias_symbol (tree decl)
1930{
1931 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1932 return get_identifier (TREE_STRING_POINTER
1933 (TREE_VALUE (TREE_VALUE (alias))));
1934}
1935
1936
5e712541 1937/* Weakrefs may be associated to external decls and thus not output
9d75589a 1938 at expansion time. Emit all necessary aliases. */
5e712541 1939
5139ff04 1940static void
5e712541 1941output_weakrefs (void)
1942{
1943 struct cgraph_node *node;
1944 struct varpool_node *vnode;
7c455d87 1945 FOR_EACH_FUNCTION (node)
7d0d0ce1 1946 if (node->alias && DECL_EXTERNAL (node->symbol.decl)
1947 && !TREE_ASM_WRITTEN (node->symbol.decl)
1948 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->symbol.decl)))
afea39ad 1949 do_assemble_alias (node->symbol.decl,
fc8456b4 1950 node->thunk.alias && DECL_P (node->thunk.alias) ? DECL_ASSEMBLER_NAME (node->thunk.alias)
afea39ad 1951 : get_alias_symbol (node->symbol.decl));
7c455d87 1952 FOR_EACH_VARIABLE (vnode)
7d0d0ce1 1953 if (vnode->alias && DECL_EXTERNAL (vnode->symbol.decl)
1954 && !TREE_ASM_WRITTEN (vnode->symbol.decl)
1955 && lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->symbol.decl)))
afea39ad 1956 do_assemble_alias (vnode->symbol.decl,
fc8456b4 1957 vnode->alias_of && DECL_P (vnode->alias_of) ? DECL_ASSEMBLER_NAME (vnode->alias_of)
afea39ad 1958 : get_alias_symbol (vnode->symbol.decl));
5e712541 1959}
1960
da5e1e7c 1961/* Initialize callgraph dump file. */
34e5cced 1962
121f3051 1963void
1964init_cgraph (void)
1965{
01ec0a6c 1966 if (!cgraph_dump_file)
1967 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
121f3051 1968}
b5d36404 1969
d2bb3f9d 1970
1971/* Perform simple optimizations based on callgraph. */
1972
1973void
cf951b1a 1974compile (void)
d2bb3f9d 1975{
1976 if (seen_error ())
1977 return;
1978
1979#ifdef ENABLE_CHECKING
3e7775f6 1980 verify_symtab ();
d2bb3f9d 1981#endif
1982
d2bb3f9d 1983 timevar_push (TV_CGRAPHOPT);
1984 if (pre_ipa_mem_report)
1985 {
1986 fprintf (stderr, "Memory consumption before IPA\n");
1987 dump_memory_report (false);
1988 }
1989 if (!quiet_flag)
1990 fprintf (stderr, "Performing interprocedural optimizations\n");
1991 cgraph_state = CGRAPH_STATE_IPA;
1992
cf951b1a 1993 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1994 if (flag_lto)
1995 lto_streamer_hooks_init ();
1996
d2bb3f9d 1997 /* Don't run the IPA passes if there was any error or sorry messages. */
1998 if (!seen_error ())
1999 ipa_passes ();
2000
2001 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2002 if (seen_error ()
2003 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2004 {
2005 timevar_pop (TV_CGRAPHOPT);
2006 return;
2007 }
2008
2009 /* This pass remove bodies of extern inline functions we never inlined.
2010 Do this later so other IPA passes see what is really going on. */
91f0ab48 2011 symtab_remove_unreachable_nodes (false, dump_file);
d2bb3f9d 2012 cgraph_global_info_ready = true;
2013 if (cgraph_dump_file)
2014 {
2015 fprintf (cgraph_dump_file, "Optimized ");
18841b0c 2016 dump_symtab (cgraph_dump_file);
d2bb3f9d 2017 }
2018 if (post_ipa_mem_report)
2019 {
2020 fprintf (stderr, "Memory consumption after IPA\n");
2021 dump_memory_report (false);
2022 }
2023 timevar_pop (TV_CGRAPHOPT);
2024
2025 /* Output everything. */
2026 (*debug_hooks->assembly_start) ();
2027 if (!quiet_flag)
2028 fprintf (stderr, "Assembling functions:\n");
2029#ifdef ENABLE_CHECKING
3e7775f6 2030 verify_symtab ();
d2bb3f9d 2031#endif
2032
2033 cgraph_materialize_all_clones ();
2034 bitmap_obstack_initialize (NULL);
2035 execute_ipa_pass_list (all_late_ipa_passes);
91f0ab48 2036 symtab_remove_unreachable_nodes (true, dump_file);
d2bb3f9d 2037#ifdef ENABLE_CHECKING
3e7775f6 2038 verify_symtab ();
d2bb3f9d 2039#endif
2040 bitmap_obstack_release (NULL);
cf951b1a 2041 mark_functions_to_output ();
d2bb3f9d 2042
2043 cgraph_state = CGRAPH_STATE_EXPANSION;
2044 if (!flag_toplevel_reorder)
cf951b1a 2045 output_in_order ();
d2bb3f9d 2046 else
2047 {
cf951b1a 2048 output_asm_statements ();
d2bb3f9d 2049
cf951b1a 2050 expand_all_functions ();
2051 varpool_output_variables ();
d2bb3f9d 2052 }
2053
2054 cgraph_process_new_functions ();
2055 cgraph_state = CGRAPH_STATE_FINISHED;
afea39ad 2056 output_weakrefs ();
d2bb3f9d 2057
2058 if (cgraph_dump_file)
2059 {
2060 fprintf (cgraph_dump_file, "\nFinal ");
18841b0c 2061 dump_symtab (cgraph_dump_file);
d2bb3f9d 2062 }
2063#ifdef ENABLE_CHECKING
3e7775f6 2064 verify_symtab ();
d2bb3f9d 2065 /* Double check that all inline clones are gone and that all
2066 function bodies have been released from memory. */
2067 if (!seen_error ())
2068 {
2069 struct cgraph_node *node;
2070 bool error_found = false;
2071
7c455d87 2072 FOR_EACH_DEFINED_FUNCTION (node)
2073 if (node->global.inlined_to
2074 || gimple_has_body_p (node->symbol.decl))
d2bb3f9d 2075 {
2076 error_found = true;
2077 dump_cgraph_node (stderr, node);
2078 }
2079 if (error_found)
2080 internal_error ("nodes with unreleased memory found");
2081 }
2082#endif
2083}
2084
2085
2086/* Analyze the whole compilation unit once it is parsed completely. */
2087
2088void
cf951b1a 2089finalize_compilation_unit (void)
d2bb3f9d 2090{
2091 timevar_push (TV_CGRAPH);
2092
d2bb3f9d 2093 /* If we're here there's no current function anymore. Some frontends
2094 are lazy in clearing these. */
2095 current_function_decl = NULL;
2096 set_cfun (NULL);
2097
2098 /* Do not skip analyzing the functions if there were errors, we
2099 miss diagnostics for following functions otherwise. */
2100
2101 /* Emit size functions we didn't inline. */
2102 finalize_size_functions ();
2103
2104 /* Mark alias targets necessary and emit diagnostics. */
d2bb3f9d 2105 handle_alias_pairs ();
2106
2107 if (!quiet_flag)
2108 {
2109 fprintf (stderr, "\nAnalyzing compilation unit\n");
2110 fflush (stderr);
2111 }
2112
2113 if (flag_dump_passes)
2114 dump_passes ();
2115
2116 /* Gimplify and lower all functions, compute reachability and
2117 remove unreachable nodes. */
2118 cgraph_analyze_functions ();
2119
2120 /* Mark alias targets necessary and emit diagnostics. */
d2bb3f9d 2121 handle_alias_pairs ();
2122
2123 /* Gimplify and lower thunks. */
2124 cgraph_analyze_functions ();
2125
2126 /* Finally drive the pass manager. */
cf951b1a 2127 compile ();
d2bb3f9d 2128
2129 timevar_pop (TV_CGRAPH);
2130}
2131
2132
a861fe52 2133#include "gt-cgraphunit.h"