]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphunit.c
Update config.sub to 2012-04-18 version from official repo.
[thirdparty/gcc.git] / gcc / cgraphunit.c
CommitLineData
cd6bca02 1/* Callgraph based interprocedural optimizations.
aed6e608 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
5c8ad8f5 3 2011, 2012 Free Software Foundation, Inc.
ae01b312 4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
ae01b312 11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
ae01b312 21
b0cdf642 22/* This module implements main driver of compilation process as well as
cd6bca02 23 few basic interprocedural optimizers.
b0cdf642 24
25 The main scope of this file is to act as an interface in between
26 tree based frontends and the backend (and middle end)
27
28 The front-end is supposed to use following functionality:
29
30 - cgraph_finalize_function
31
32 This function is called once front-end has parsed whole body of function
33 and it is certain that the function body nor the declaration will change.
34
b326746d 35 (There is one exception needed for implementing GCC extern inline
36 function.)
b0cdf642 37
1d416bd7 38 - varpool_finalize_variable
b0cdf642 39
7bd28bba 40 This function has same behavior as the above but is used for static
b0cdf642 41 variables.
42
43 - cgraph_finalize_compilation_unit
44
b326746d 45 This function is called once (source level) compilation unit is finalized
46 and it will no longer change.
b0cdf642 47
851d9296 48 In the call-graph construction and local function analysis takes
49 place here. Bodies of unreachable functions are released to
50 conserve memory usage.
b0cdf642 51
b326746d 52 The function can be called multiple times when multiple source level
53 compilation units are combined (such as in C frontend)
b0cdf642 54
55 - cgraph_optimize
56
57 In this unit-at-a-time compilation the intra procedural analysis takes
58 place here. In particular the static functions whose address is never
59 taken are marked as local. Backend can then use this information to
60 modify calling conventions, do better inlining or similar optimizations.
61
b0cdf642 62 - cgraph_mark_needed_node
1d416bd7 63 - varpool_mark_needed_node
b0cdf642 64
b326746d 65 When function or variable is referenced by some hidden way the call-graph
66 data structure must be updated accordingly by this function.
67 There should be little need to call this function and all the references
68 should be made explicit to cgraph code. At present these functions are
ccd2f3d1 69 used by C++ frontend to explicitly mark the keyed methods.
b0cdf642 70
71 - analyze_expr callback
72
73 This function is responsible for lowering tree nodes not understood by
74 generic code into understandable ones or alternatively marking
75 callgraph and varpool nodes referenced by the as needed.
76
77 ??? On the tree-ssa genericizing should take place here and we will avoid
78 need for these hooks (replacing them by genericizing hook)
79
6329636b 80 Analyzing of all functions is deferred
b0cdf642 81 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
82
83 In cgraph_finalize_compilation_unit the reachable functions are
84 analyzed. During analysis the call-graph edges from reachable
85 functions are constructed and their destinations are marked as
86 reachable. References to functions and variables are discovered too
87 and variables found to be needed output to the assembly file. Via
88 mark_referenced call in assemble_variable functions referenced by
89 static variables are noticed too.
90
ca67a72b 91 The intra-procedural information is produced and its existence
b0cdf642 92 indicated by global_info_ready. Once this flag is set it is impossible
93 to change function from !reachable to reachable and thus
94 assemble_variable no longer call mark_referenced.
95
96 Finally the call-graph is topologically sorted and all reachable functions
97 that has not been completely inlined or are not external are output.
98
99 ??? It is possible that reference to function or variable is optimized
100 out. We can not deal with this nicely because topological order is not
101 suitable for it. For tree-ssa we may consider another pass doing
102 optimization and re-discovering reachable functions.
103
104 ??? Reorganize code so variables are output very last and only if they
105 really has been referenced by produced code, so we catch more cases
6329636b 106 where reference has been optimized out. */
121f3051 107
acc70efa 108
ae01b312 109#include "config.h"
110#include "system.h"
111#include "coretypes.h"
112#include "tm.h"
113#include "tree.h"
941366fd 114#include "output.h"
b5530559 115#include "rtl.h"
acc70efa 116#include "tree-flow.h"
ae01b312 117#include "tree-inline.h"
118#include "langhooks.h"
c6224531 119#include "pointer-set.h"
ae01b312 120#include "toplev.h"
121#include "flags.h"
122#include "ggc.h"
123#include "debug.h"
124#include "target.h"
125#include "cgraph.h"
80a85d8a 126#include "diagnostic.h"
ce084dfc 127#include "tree-pretty-print.h"
128#include "gimple-pretty-print.h"
f79b6507 129#include "timevar.h"
d7c6d889 130#include "params.h"
131#include "fibheap.h"
611e5405 132#include "intl.h"
b69eb0ff 133#include "function.h"
b5d36404 134#include "ipa-prop.h"
75a70cf9 135#include "gimple.h"
136#include "tree-iterator.h"
f1e2a033 137#include "tree-pass.h"
bfec3452 138#include "tree-dump.h"
c1dcd13c 139#include "output.h"
9ed5b1f5 140#include "coverage.h"
c9036234 141#include "plugin.h"
a41f2a28 142#include "ipa-inline.h"
7771d558 143#include "ipa-utils.h"
a0605d65 144#include "lto-streamer.h"
3db65b62 145#include "except.h"
941366fd 146#include "regset.h" /* FIXME: For reg_obstack. */
d7c6d889 147
ff2a5ada 148/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
149 secondary queue used during optimization to accommodate passes that
150 may generate new functions that need to be optimized and expanded. */
151cgraph_node_set cgraph_new_nodes;
152
a6868229 153static void cgraph_expand_all_functions (void);
d9d9733a 154static void cgraph_mark_functions_to_output (void);
155static void cgraph_expand_function (struct cgraph_node *);
f788fff2 156static void cgraph_output_pending_asms (void);
3db65b62 157static void tree_rest_of_compilation (struct cgraph_node *);
25bb88de 158
ecb08119 159FILE *cgraph_dump_file;
121f3051 160
28454517 161/* Used for vtable lookup in thunk adjusting. */
162static GTY (()) tree vtable_entry_type;
163
8efa224a 164/* Determine if function DECL is trivially needed and should stay in the
165 compilation unit. This is used at the symbol table construction time
166 and differs from later logic removing unnecesary functions that can
167 take into account results of analysis, whole program info etc. */
2c0b522d 168
7bfefa9d 169bool
170cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
2c0b522d 171{
3f82b628 172 /* If the user told us it is used, then it must be so. */
8efa224a 173 if (node->symbol.force_output)
05806473 174 return true;
175
8efa224a 176 /* Double check that no one output the function into assembly file
177 early. */
178 gcc_checking_assert (!DECL_ASSEMBLER_NAME_SET_P (decl)
179 || (node->thunk.thunk_p || node->same_body_alias)
180 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)));
3f82b628 181
55680bef 182
8efa224a 183 /* Keep constructors, destructors and virtual functions. */
184 if (DECL_STATIC_CONSTRUCTOR (decl)
185 || DECL_STATIC_DESTRUCTOR (decl)
186 || (DECL_VIRTUAL_P (decl)
187 && optimize && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
188 return true;
2c0b522d 189
190 /* Externally visible functions must be output. The exception is
8efa224a 191 COMDAT functions that must be output only when they are needed. */
8baa9d15 192
8efa224a 193 if (TREE_PUBLIC (decl)
62eec3b4 194 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
2c0b522d 195 return true;
196
2c0b522d 197 return false;
198}
199
ff2a5ada 200/* Head of the queue of nodes to be processed while building callgraph */
201
202static symtab_node first = (symtab_node)(void *)1;
203
204/* Add NODE to queue starting at FIRST.
205 The queue is linked via AUX pointers and terminated by pointer to 1. */
206
207static void
208enqueue_node (symtab_node node)
209{
210 if (node->symbol.aux)
211 return;
212 gcc_checking_assert (first);
213 node->symbol.aux = first;
214 first = node;
215}
216
bdc40eb8 217/* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
523c1122 218 functions into callgraph in a way so they look like ordinary reachable
219 functions inserted into callgraph already at construction time. */
220
221bool
222cgraph_process_new_functions (void)
223{
224 bool output = false;
225 tree fndecl;
226 struct cgraph_node *node;
ff2a5ada 227 cgraph_node_set_iterator csi;
523c1122 228
ff2a5ada 229 if (!cgraph_new_nodes)
230 return false;
523c1122 231 /* Note that this queue may grow as its being processed, as the new
232 functions may generate new ones. */
ff2a5ada 233 for (csi = csi_start (cgraph_new_nodes); !csi_end_p (csi); csi_next (&csi))
523c1122 234 {
ff2a5ada 235 node = csi_node (csi);
7d0d0ce1 236 fndecl = node->symbol.decl;
523c1122 237 switch (cgraph_state)
238 {
239 case CGRAPH_STATE_CONSTRUCTION:
240 /* At construction time we just need to finalize function and move
241 it into reachable functions list. */
242
523c1122 243 cgraph_finalize_function (fndecl, false);
523c1122 244 output = true;
4f7a1122 245 cgraph_call_function_insertion_hooks (node);
ff2a5ada 246 enqueue_node ((symtab_node) node);
523c1122 247 break;
248
249 case CGRAPH_STATE_IPA:
f517b36e 250 case CGRAPH_STATE_IPA_SSA:
523c1122 251 /* When IPA optimization already started, do all essential
252 transformations that has been already performed on the whole
253 cgraph but not on this function. */
254
75a70cf9 255 gimple_register_cfg_hooks ();
523c1122 256 if (!node->analyzed)
257 cgraph_analyze_function (node);
258 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
259 current_function_decl = fndecl;
f517b36e 260 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
261 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
262 /* When not optimizing, be sure we run early local passes anyway
263 to expand OMP. */
264 || !optimize)
20099e35 265 execute_pass_list (pass_early_local_passes.pass.sub);
649597af 266 else
a41f2a28 267 compute_inline_parameters (node, true);
523c1122 268 free_dominance_info (CDI_POST_DOMINATORS);
269 free_dominance_info (CDI_DOMINATORS);
270 pop_cfun ();
271 current_function_decl = NULL;
4f7a1122 272 cgraph_call_function_insertion_hooks (node);
523c1122 273 break;
274
275 case CGRAPH_STATE_EXPANSION:
276 /* Functions created during expansion shall be compiled
277 directly. */
09fc9532 278 node->process = 0;
4f7a1122 279 cgraph_call_function_insertion_hooks (node);
523c1122 280 cgraph_expand_function (node);
281 break;
282
283 default:
284 gcc_unreachable ();
285 break;
286 }
287 }
ff2a5ada 288 free_cgraph_node_set (cgraph_new_nodes);
289 cgraph_new_nodes = NULL;
523c1122 290 return output;
291}
292
9b8fb23a 293/* As an GCC extension we allow redefinition of the function. The
294 semantics when both copies of bodies differ is not well defined.
295 We replace the old body with new body so in unit at a time mode
296 we always use new body, while in normal mode we may end up with
297 old body inlined into some functions and new body expanded and
298 inlined in others.
299
300 ??? It may make more sense to use one body for inlining and other
301 body for expanding the function but this is difficult to do. */
302
303static void
304cgraph_reset_node (struct cgraph_node *node)
305{
09fc9532 306 /* If node->process is set, then we have already begun whole-unit analysis.
6329636b 307 This is *not* testing for whether we've already emitted the function.
308 That case can be sort-of legitimately seen with real function redefinition
309 errors. I would argue that the front end should never present us with
310 such a case, but don't enforce that for now. */
09fc9532 311 gcc_assert (!node->process);
9b8fb23a 312
313 /* Reset our data structures so we can analyze the function again. */
314 memset (&node->local, 0, sizeof (node->local));
315 memset (&node->global, 0, sizeof (node->global));
316 memset (&node->rtl, 0, sizeof (node->rtl));
317 node->analyzed = false;
9b8fb23a 318 node->local.finalized = false;
319
9b8fb23a 320 cgraph_node_remove_callees (node);
9b8fb23a 321}
c08871a9 322
9a2639fc 323/* Return true when there are references to NODE. */
324
325static bool
326referred_to_p (symtab_node node)
327{
328 int i;
329 struct ipa_ref *ref;
330
331 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref);
332 i++)
333 return true;
334 if (symtab_function_p (node) && cgraph (node)->callers)
335 return true;
336 return false;
337}
338
28df663b 339/* DECL has been parsed. Take it, queue it, compile it at the whim of the
340 logic in effect. If NESTED is true, then our caller cannot stand to have
341 the garbage collector run at the moment. We would need to either create
342 a new GC context, or just not compile right now. */
ae01b312 343
344void
28df663b 345cgraph_finalize_function (tree decl, bool nested)
ae01b312 346{
5a90471f 347 struct cgraph_node *node = cgraph_get_create_node (decl);
ae01b312 348
c08871a9 349 if (node->local.finalized)
443089c1 350 {
351 cgraph_reset_node (node);
352 node->local.redefined_extern_inline = true;
353 }
28df663b 354
c08871a9 355 notice_global_symbol (decl);
79bb87b4 356 node->local.finalized = true;
e27482aa 357 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
ae01b312 358
8efa224a 359 /* With -fkeep-inline-functions we are keeping all inline functions except
360 for extern inline ones. */
361 if (flag_keep_inline_functions
362 && DECL_DECLARED_INLINE_P (decl)
363 && !DECL_EXTERNAL (decl)
364 && !DECL_DISREGARD_INLINE_LIMITS (decl))
365 node->symbol.force_output = 1;
2c0b522d 366
8efa224a 367 /* When not optimizing, also output the static functions. (see
368 PR24561), but don't do so for always_inline functions, functions
369 declared inline and nested functions. These were optimized out
370 in the original implementation and it is unclear whether we want
371 to change the behavior here. */
372 if ((!optimize
373 && !node->same_body_alias
374 && !DECL_DISREGARD_INLINE_LIMITS (decl)
375 && !DECL_DECLARED_INLINE_P (decl)
376 && !(DECL_CONTEXT (decl)
377 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL))
378 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
379 node->symbol.force_output = 1;
380
2c0b522d 381 /* If we've not yet emitted decl, tell the debug info about it. */
28df663b 382 if (!TREE_ASM_WRITTEN (decl))
2c0b522d 383 (*debug_hooks->deferred_inline_function) (decl);
4e8871a0 384
b69eb0ff 385 /* Possibly warn about unused parameters. */
386 if (warn_unused_parameter)
387 do_warn_unused_parameter (decl);
6329636b 388
389 if (!nested)
390 ggc_collect ();
9a2639fc 391
392 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
393 && (cgraph_decide_is_function_needed (node, decl)
394 || referred_to_p ((symtab_node)node)))
395 enqueue_node ((symtab_node)node);
ae01b312 396}
397
3db65b62 398/* Add the function FNDECL to the call graph.
399 Unlike cgraph_finalize_function, this function is intended to be used
400 by middle end and allows insertion of new function at arbitrary point
401 of compilation. The function can be either in high, low or SSA form
402 GIMPLE.
403
404 The function is assumed to be reachable and have address taken (so no
405 API breaking optimizations are performed on it).
406
407 Main work done by this function is to enqueue the function for later
408 processing to avoid need the passes to be re-entrant. */
409
410void
411cgraph_add_new_function (tree fndecl, bool lowered)
412{
413 struct cgraph_node *node;
414 switch (cgraph_state)
415 {
ff2a5ada 416 case CGRAPH_STATE_PARSING:
417 cgraph_finalize_function (fndecl, false);
418 break;
3db65b62 419 case CGRAPH_STATE_CONSTRUCTION:
420 /* Just enqueue function to be processed at nearest occurrence. */
421 node = cgraph_create_node (fndecl);
3db65b62 422 if (lowered)
423 node->lowered = true;
ff2a5ada 424 if (!cgraph_new_nodes)
425 cgraph_new_nodes = cgraph_node_set_new ();
426 cgraph_node_set_add (cgraph_new_nodes, node);
3db65b62 427 break;
428
429 case CGRAPH_STATE_IPA:
430 case CGRAPH_STATE_IPA_SSA:
431 case CGRAPH_STATE_EXPANSION:
432 /* Bring the function into finalized state and enqueue for later
433 analyzing and compilation. */
434 node = cgraph_get_create_node (fndecl);
435 node->local.local = false;
436 node->local.finalized = true;
da751785 437 node->symbol.force_output = true;
3db65b62 438 if (!lowered && cgraph_state == CGRAPH_STATE_EXPANSION)
439 {
440 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
441 current_function_decl = fndecl;
442 gimple_register_cfg_hooks ();
443 bitmap_obstack_initialize (NULL);
444 execute_pass_list (all_lowering_passes);
445 execute_pass_list (pass_early_local_passes.pass.sub);
446 bitmap_obstack_release (NULL);
447 pop_cfun ();
448 current_function_decl = NULL;
449
450 lowered = true;
451 }
452 if (lowered)
453 node->lowered = true;
ff2a5ada 454 if (!cgraph_new_nodes)
455 cgraph_new_nodes = cgraph_node_set_new ();
456 cgraph_node_set_add (cgraph_new_nodes, node);
3db65b62 457 break;
458
459 case CGRAPH_STATE_FINISHED:
460 /* At the very end of compilation we have to do all the work up
461 to expansion. */
462 node = cgraph_create_node (fndecl);
463 if (lowered)
464 node->lowered = true;
465 cgraph_analyze_function (node);
466 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
467 current_function_decl = fndecl;
468 gimple_register_cfg_hooks ();
469 bitmap_obstack_initialize (NULL);
470 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
471 execute_pass_list (pass_early_local_passes.pass.sub);
472 bitmap_obstack_release (NULL);
473 tree_rest_of_compilation (node);
474 pop_cfun ();
475 current_function_decl = NULL;
476 break;
477
478 default:
479 gcc_unreachable ();
480 }
481
482 /* Set a personality if required and we already passed EH lowering. */
483 if (lowered
484 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl))
485 == eh_personality_lang))
486 DECL_FUNCTION_PERSONALITY (fndecl) = lang_hooks.eh_personality ();
487}
488
ccf4ab6b 489/* Return TRUE if NODE2 is equivalent to NODE or its clone. */
490static bool
491clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
492{
c70f46b0 493 node = cgraph_function_or_thunk_node (node, NULL);
494 node2 = cgraph_function_or_thunk_node (node2, NULL);
ccf4ab6b 495 while (node != node2 && node2)
496 node2 = node2->clone_of;
497 return node2 != NULL;
498}
499
1a036a3b 500/* Verify edge E count and frequency. */
501
502static bool
503verify_edge_count_and_frequency (struct cgraph_edge *e)
504{
505 bool error_found = false;
506 if (e->count < 0)
507 {
508 error ("caller edge count is negative");
509 error_found = true;
510 }
511 if (e->frequency < 0)
512 {
513 error ("caller edge frequency is negative");
514 error_found = true;
515 }
516 if (e->frequency > CGRAPH_FREQ_MAX)
517 {
518 error ("caller edge frequency is too large");
519 error_found = true;
520 }
7d0d0ce1 521 if (gimple_has_body_p (e->caller->symbol.decl)
1a036a3b 522 && !e->caller->global.inlined_to
8bae3ea4 523 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
524 Remove this once edges are actualy removed from the function at that time. */
525 && (e->frequency
526 || (inline_edge_summary_vec
9ed50dd9 527 && ((VEC_length(inline_edge_summary_t, inline_edge_summary_vec)
528 <= (unsigned) e->uid)
529 || !inline_edge_summary (e)->predicate)))
1a036a3b 530 && (e->frequency
7d0d0ce1 531 != compute_call_stmt_bb_frequency (e->caller->symbol.decl,
1a036a3b 532 gimple_bb (e->call_stmt))))
533 {
0a10fd82 534 error ("caller edge frequency %i does not match BB frequency %i",
1a036a3b 535 e->frequency,
7d0d0ce1 536 compute_call_stmt_bb_frequency (e->caller->symbol.decl,
1a036a3b 537 gimple_bb (e->call_stmt)));
538 error_found = true;
539 }
540 return error_found;
541}
542
7b29dd2f 543/* Switch to THIS_CFUN if needed and print STMT to stderr. */
544static void
545cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
546{
547 /* debug_gimple_stmt needs correct cfun */
548 if (cfun != this_cfun)
549 set_cfun (this_cfun);
550 debug_gimple_stmt (stmt);
551}
552
2f9d66d3 553/* Verify that call graph edge E corresponds to DECL from the associated
554 statement. Return true if the verification should fail. */
555
556static bool
557verify_edge_corresponds_to_fndecl (struct cgraph_edge *e, tree decl)
558{
54e8af13 559 struct cgraph_node *node;
560
561 if (!decl || e->callee->global.inlined_to)
562 return false;
563 node = cgraph_get_node (decl);
564
565 /* We do not know if a node from a different partition is an alias or what it
566 aliases and therefore cannot do the former_clone_of check reliably. */
7d0d0ce1 567 if (!node || node->symbol.in_other_partition)
54e8af13 568 return false;
569 node = cgraph_function_or_thunk_node (node, NULL);
570
7d0d0ce1 571 if ((e->callee->former_clone_of != node->symbol.decl
cdf67cee 572 && (!node->same_body_alias
573 || e->callee->former_clone_of != node->thunk.alias))
2f9d66d3 574 /* IPA-CP sometimes redirect edge to clone and then back to the former
cdf67cee 575 function. This ping-pong has to go, eventually. */
54e8af13 576 && (node != cgraph_function_or_thunk_node (e->callee, NULL))
cdf67cee 577 && !clone_of_p (node, e->callee)
578 /* If decl is a same body alias of some other decl, allow e->callee to be
579 a clone of a clone of that other decl too. */
580 && (!node->same_body_alias
581 || !clone_of_p (cgraph_get_node (node->thunk.alias), e->callee)))
2f9d66d3 582 return true;
583 else
584 return false;
585}
586
b0cdf642 587/* Verify cgraph nodes of given cgraph node. */
4b987fac 588DEBUG_FUNCTION void
b0cdf642 589verify_cgraph_node (struct cgraph_node *node)
590{
591 struct cgraph_edge *e;
7d0d0ce1 592 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->symbol.decl);
e27482aa 593 basic_block this_block;
75a70cf9 594 gimple_stmt_iterator gsi;
9bfec7c2 595 bool error_found = false;
b0cdf642 596
852f689e 597 if (seen_error ())
bd09cd3e 598 return;
599
b0cdf642 600 timevar_push (TV_CGRAPH_VERIFY);
3e7775f6 601 error_found |= verify_symtab_base ((symtab_node) node);
b0cdf642 602 for (e = node->callees; e; e = e->next_callee)
603 if (e->aux)
604 {
0a81f5a0 605 error ("aux field set for edge %s->%s",
abd3e6b5 606 identifier_to_locale (cgraph_node_name (e->caller)),
607 identifier_to_locale (cgraph_node_name (e->callee)));
b0cdf642 608 error_found = true;
609 }
a2cb9b3b 610 if (node->count < 0)
611 {
bf776685 612 error ("execution count is negative");
a2cb9b3b 613 error_found = true;
614 }
7d0d0ce1 615 if (node->global.inlined_to && node->symbol.externally_visible)
59dd4830 616 {
bf776685 617 error ("externally visible inline clone");
59dd4830 618 error_found = true;
619 }
7d0d0ce1 620 if (node->global.inlined_to && node->symbol.address_taken)
59dd4830 621 {
bf776685 622 error ("inline clone with address taken");
59dd4830 623 error_found = true;
624 }
8efa224a 625 if (node->global.inlined_to && node->symbol.force_output)
59dd4830 626 {
8efa224a 627 error ("inline clone is forced to output");
59dd4830 628 error_found = true;
629 }
799c8711 630 for (e = node->indirect_calls; e; e = e->next_callee)
631 {
632 if (e->aux)
633 {
634 error ("aux field set for indirect edge from %s",
635 identifier_to_locale (cgraph_node_name (e->caller)));
636 error_found = true;
637 }
638 if (!e->indirect_unknown_callee
639 || !e->indirect_info)
640 {
641 error ("An indirect edge from %s is not marked as indirect or has "
642 "associated indirect_info, the corresponding statement is: ",
643 identifier_to_locale (cgraph_node_name (e->caller)));
7b29dd2f 644 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
799c8711 645 error_found = true;
646 }
647 }
b0cdf642 648 for (e = node->callers; e; e = e->next_caller)
649 {
1a036a3b 650 if (verify_edge_count_and_frequency (e))
651 error_found = true;
b0cdf642 652 if (!e->inline_failed)
653 {
654 if (node->global.inlined_to
655 != (e->caller->global.inlined_to
656 ? e->caller->global.inlined_to : e->caller))
657 {
0a81f5a0 658 error ("inlined_to pointer is wrong");
b0cdf642 659 error_found = true;
660 }
661 if (node->callers->next_caller)
662 {
0a81f5a0 663 error ("multiple inline callers");
b0cdf642 664 error_found = true;
665 }
666 }
667 else
668 if (node->global.inlined_to)
669 {
0a81f5a0 670 error ("inlined_to pointer set for noninline callers");
b0cdf642 671 error_found = true;
672 }
673 }
1a036a3b 674 for (e = node->indirect_calls; e; e = e->next_callee)
675 if (verify_edge_count_and_frequency (e))
676 error_found = true;
b0cdf642 677 if (!node->callers && node->global.inlined_to)
678 {
5cd75817 679 error ("inlined_to pointer is set but no predecessors found");
b0cdf642 680 error_found = true;
681 }
682 if (node->global.inlined_to == node)
683 {
0a81f5a0 684 error ("inlined_to pointer refers to itself");
b0cdf642 685 error_found = true;
686 }
687
ccf4ab6b 688 if (node->clone_of)
689 {
690 struct cgraph_node *n;
691 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
692 if (n == node)
693 break;
694 if (!n)
695 {
696 error ("node has wrong clone_of");
697 error_found = true;
698 }
699 }
700 if (node->clones)
701 {
702 struct cgraph_node *n;
703 for (n = node->clones; n; n = n->next_sibling_clone)
704 if (n->clone_of != node)
705 break;
706 if (n)
707 {
708 error ("node has wrong clone list");
709 error_found = true;
710 }
711 }
712 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
713 {
714 error ("node is in clone list but it is not clone");
715 error_found = true;
716 }
717 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
718 {
719 error ("node has wrong prev_clone pointer");
720 error_found = true;
721 }
722 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
723 {
724 error ("double linked list of clones corrupted");
725 error_found = true;
726 }
727
c70f46b0 728 if (node->analyzed && node->alias)
729 {
730 bool ref_found = false;
731 int i;
732 struct ipa_ref *ref;
733
734 if (node->callees)
735 {
736 error ("Alias has call edges");
737 error_found = true;
738 }
7d0d0ce1 739 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list,
740 i, ref); i++)
c70f46b0 741 if (ref->use != IPA_REF_ALIAS)
742 {
70f89d12 743 error ("Alias has non-alias reference");
c70f46b0 744 error_found = true;
745 }
746 else if (ref_found)
747 {
748 error ("Alias has more than one alias reference");
749 error_found = true;
750 }
751 else
752 ref_found = true;
753 if (!ref_found)
754 {
755 error ("Analyzed alias has no reference");
756 error_found = true;
757 }
758 }
91bf9d9a 759 if (node->analyzed && node->thunk.thunk_p)
760 {
761 if (!node->callees)
762 {
763 error ("No edge out of thunk node");
764 error_found = true;
765 }
766 else if (node->callees->next_callee)
767 {
768 error ("More than one edge out of thunk node");
769 error_found = true;
770 }
7d0d0ce1 771 if (gimple_has_body_p (node->symbol.decl))
91bf9d9a 772 {
773 error ("Thunk is not supposed to have body");
774 error_found = true;
775 }
776 }
7d0d0ce1 777 else if (node->analyzed && gimple_has_body_p (node->symbol.decl)
778 && !TREE_ASM_WRITTEN (node->symbol.decl)
779 && (!DECL_EXTERNAL (node->symbol.decl) || node->global.inlined_to)
91bf9d9a 780 && !flag_wpa)
b0cdf642 781 {
e27482aa 782 if (this_cfun->cfg)
783 {
784 /* The nodes we're interested in are never shared, so walk
785 the tree ignoring duplicates. */
e7c352d1 786 struct pointer_set_t *visited_nodes = pointer_set_create ();
e27482aa 787 /* Reach the trees by walking over the CFG, and note the
788 enclosing basic-blocks in the call edges. */
789 FOR_EACH_BB_FN (this_block, this_cfun)
75a70cf9 790 for (gsi = gsi_start_bb (this_block);
791 !gsi_end_p (gsi);
792 gsi_next (&gsi))
9bfec7c2 793 {
75a70cf9 794 gimple stmt = gsi_stmt (gsi);
799c8711 795 if (is_gimple_call (stmt))
9bfec7c2 796 {
797 struct cgraph_edge *e = cgraph_edge (node, stmt);
799c8711 798 tree decl = gimple_call_fndecl (stmt);
9bfec7c2 799 if (e)
800 {
801 if (e->aux)
802 {
0a81f5a0 803 error ("shared call_stmt:");
7b29dd2f 804 cgraph_debug_gimple_stmt (this_cfun, stmt);
9bfec7c2 805 error_found = true;
806 }
799c8711 807 if (!e->indirect_unknown_callee)
28454517 808 {
2f9d66d3 809 if (verify_edge_corresponds_to_fndecl (e, decl))
799c8711 810 {
811 error ("edge points to wrong declaration:");
7d0d0ce1 812 debug_tree (e->callee->symbol.decl);
799c8711 813 fprintf (stderr," Instead of:");
814 debug_tree (decl);
815 error_found = true;
816 }
28454517 817 }
799c8711 818 else if (decl)
9bfec7c2 819 {
799c8711 820 error ("an indirect edge with unknown callee "
821 "corresponding to a call_stmt with "
822 "a known declaration:");
ee3f5fc0 823 error_found = true;
7b29dd2f 824 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
9bfec7c2 825 }
826 e->aux = (void *)1;
827 }
799c8711 828 else if (decl)
9bfec7c2 829 {
0a81f5a0 830 error ("missing callgraph edge for call stmt:");
7b29dd2f 831 cgraph_debug_gimple_stmt (this_cfun, stmt);
9bfec7c2 832 error_found = true;
833 }
834 }
835 }
e27482aa 836 pointer_set_destroy (visited_nodes);
e27482aa 837 }
838 else
839 /* No CFG available?! */
840 gcc_unreachable ();
841
b0cdf642 842 for (e = node->callees; e; e = e->next_callee)
843 {
799c8711 844 if (!e->aux)
b0cdf642 845 {
0a81f5a0 846 error ("edge %s->%s has no corresponding call_stmt",
abd3e6b5 847 identifier_to_locale (cgraph_node_name (e->caller)),
848 identifier_to_locale (cgraph_node_name (e->callee)));
7b29dd2f 849 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
b0cdf642 850 error_found = true;
851 }
852 e->aux = 0;
853 }
799c8711 854 for (e = node->indirect_calls; e; e = e->next_callee)
855 {
856 if (!e->aux)
857 {
858 error ("an indirect edge from %s has no corresponding call_stmt",
859 identifier_to_locale (cgraph_node_name (e->caller)));
7b29dd2f 860 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
799c8711 861 error_found = true;
862 }
863 e->aux = 0;
864 }
b0cdf642 865 }
866 if (error_found)
867 {
868 dump_cgraph_node (stderr, node);
0a81f5a0 869 internal_error ("verify_cgraph_node failed");
b0cdf642 870 }
871 timevar_pop (TV_CGRAPH_VERIFY);
872}
873
874/* Verify whole cgraph structure. */
4b987fac 875DEBUG_FUNCTION void
b0cdf642 876verify_cgraph (void)
877{
878 struct cgraph_node *node;
879
852f689e 880 if (seen_error ())
8ec2a798 881 return;
882
7c455d87 883 FOR_EACH_FUNCTION (node)
b0cdf642 884 verify_cgraph_node (node);
885}
886
56af936e 887/* Output all asm statements we have stored up to be output. */
888
889static void
890cgraph_output_pending_asms (void)
891{
892 struct cgraph_asm_node *can;
893
852f689e 894 if (seen_error ())
56af936e 895 return;
896
897 for (can = cgraph_asm_nodes; can; can = can->next)
898 assemble_asm (can->asm_str);
899 cgraph_asm_nodes = NULL;
900}
901
0785e435 902/* Analyze the function scheduled to be output. */
222bc9b9 903void
0785e435 904cgraph_analyze_function (struct cgraph_node *node)
905{
bfec3452 906 tree save = current_function_decl;
7d0d0ce1 907 tree decl = node->symbol.decl;
0785e435 908
c70f46b0 909 if (node->alias && node->thunk.alias)
910 {
911 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
b0898cb7 912 struct cgraph_node *n;
913
914 for (n = tgt; n && n->alias;
915 n = n->analyzed ? cgraph_alias_aliased_node (n) : NULL)
916 if (n == node)
917 {
7d0d0ce1 918 error ("function %q+D part of alias cycle", node->symbol.decl);
b0898cb7 919 node->alias = false;
920 return;
921 }
7d0d0ce1 922 if (!VEC_length (ipa_ref_t, node->symbol.ref_list.references))
04ec15fa 923 ipa_record_reference ((symtab_node)node, (symtab_node)tgt,
924 IPA_REF_ALIAS, NULL);
c70f46b0 925 if (node->same_body_alias)
926 {
7d0d0ce1 927 DECL_VIRTUAL_P (node->symbol.decl) = DECL_VIRTUAL_P (node->thunk.alias);
928 DECL_DECLARED_INLINE_P (node->symbol.decl)
c70f46b0 929 = DECL_DECLARED_INLINE_P (node->thunk.alias);
7d0d0ce1 930 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl)
c70f46b0 931 = DECL_DISREGARD_INLINE_LIMITS (node->thunk.alias);
932 }
933
934 /* Fixup visibility nonsences C++ frontend produce on same body aliases. */
7d0d0ce1 935 if (TREE_PUBLIC (node->symbol.decl) && node->same_body_alias)
c70f46b0 936 {
7d0d0ce1 937 DECL_EXTERNAL (node->symbol.decl) = DECL_EXTERNAL (node->thunk.alias);
89bf5ca9 938 if (DECL_ONE_ONLY (node->thunk.alias))
c70f46b0 939 {
7d0d0ce1 940 DECL_COMDAT (node->symbol.decl) = DECL_COMDAT (node->thunk.alias);
941 DECL_COMDAT_GROUP (node->symbol.decl) = DECL_COMDAT_GROUP (node->thunk.alias);
942 if (DECL_ONE_ONLY (node->thunk.alias) && !node->symbol.same_comdat_group)
c70f46b0 943 {
944 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
7d0d0ce1 945 node->symbol.same_comdat_group = (symtab_node)tgt;
946 if (!tgt->symbol.same_comdat_group)
947 tgt->symbol.same_comdat_group = (symtab_node)node;
c70f46b0 948 else
949 {
7d0d0ce1 950 symtab_node n;
951 for (n = tgt->symbol.same_comdat_group;
952 n->symbol.same_comdat_group != (symtab_node)tgt;
953 n = n->symbol.same_comdat_group)
c70f46b0 954 ;
7d0d0ce1 955 n->symbol.same_comdat_group = (symtab_node)node;
c70f46b0 956 }
957 }
958 }
959 }
7d0d0ce1 960 if (node->symbol.address_taken)
c70f46b0 961 cgraph_mark_address_taken_node (cgraph_alias_aliased_node (node));
c70f46b0 962 }
963 else if (node->thunk.thunk_p)
91bf9d9a 964 {
965 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
966 NULL, 0, CGRAPH_FREQ_BASE);
967 }
968 else
969 {
970 current_function_decl = decl;
971 push_cfun (DECL_STRUCT_FUNCTION (decl));
bfec3452 972
7d0d0ce1 973 assign_assembler_name_if_neeeded (node->symbol.decl);
6816d0c4 974
91bf9d9a 975 /* Make sure to gimplify bodies only once. During analyzing a
976 function we lower it, which will require gimplified nested
977 functions, so we can end up here with an already gimplified
978 body. */
979 if (!gimple_body (decl))
980 gimplify_function_tree (decl);
981 dump_function (TDI_generic, decl);
bfec3452 982
47199071 983 /* Lower the function. */
984 if (!node->lowered)
985 {
986 if (node->nested)
7d0d0ce1 987 lower_nested_functions (node->symbol.decl);
47199071 988 gcc_assert (!node->nested);
989
990 gimple_register_cfg_hooks ();
991 bitmap_obstack_initialize (NULL);
992 execute_pass_list (all_lowering_passes);
993 free_dominance_info (CDI_POST_DOMINATORS);
994 free_dominance_info (CDI_DOMINATORS);
995 compact_blocks ();
996 bitmap_obstack_release (NULL);
997 node->lowered = true;
998 }
999
91bf9d9a 1000 pop_cfun ();
1001 }
6e8d6e86 1002 node->analyzed = true;
0785e435 1003
bfec3452 1004 current_function_decl = save;
0785e435 1005}
1006
c70f46b0 1007/* C++ frontend produce same body aliases all over the place, even before PCH
1008 gets streamed out. It relies on us linking the aliases with their function
1009 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
1010 first produce aliases without links, but once C++ FE is sure he won't sream
1011 PCH we build the links via this function. */
1012
1013void
1014cgraph_process_same_body_aliases (void)
1015{
1016 struct cgraph_node *node;
7c455d87 1017 FOR_EACH_FUNCTION (node)
c70f46b0 1018 if (node->same_body_alias
7d0d0ce1 1019 && !VEC_length (ipa_ref_t, node->symbol.ref_list.references))
c70f46b0 1020 {
1021 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
04ec15fa 1022 ipa_record_reference ((symtab_node)node, (symtab_node)tgt,
1023 IPA_REF_ALIAS, NULL);
c70f46b0 1024 }
1025 same_body_aliases_done = true;
1026}
1027
d05db70d 1028/* Process attributes common for vars and functions. */
1029
1030static void
1031process_common_attributes (tree decl)
1032{
1033 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
1034
1035 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
1036 {
1037 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
1038 "%<weakref%> attribute should be accompanied with"
1039 " an %<alias%> attribute");
1040 DECL_WEAK (decl) = 0;
40b32d93 1041 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1042 DECL_ATTRIBUTES (decl));
d05db70d 1043 }
1044}
1045
05806473 1046/* Look for externally_visible and used attributes and mark cgraph nodes
1047 accordingly.
1048
1049 We cannot mark the nodes at the point the attributes are processed (in
1050 handle_*_attribute) because the copy of the declarations available at that
1051 point may not be canonical. For example, in:
1052
1053 void f();
1054 void f() __attribute__((used));
1055
1056 the declaration we see in handle_used_attribute will be the second
1057 declaration -- but the front end will subsequently merge that declaration
1058 with the original declaration and discard the second declaration.
1059
1060 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
1061
1062 void f() {}
1063 void f() __attribute__((externally_visible));
1064
1065 is valid.
1066
1067 So, we walk the nodes at the end of the translation unit, applying the
1068 attributes at that point. */
1069
1070static void
1071process_function_and_variable_attributes (struct cgraph_node *first,
1d416bd7 1072 struct varpool_node *first_var)
05806473 1073{
1074 struct cgraph_node *node;
1d416bd7 1075 struct varpool_node *vnode;
05806473 1076
0704fb2e 1077 for (node = cgraph_first_function (); node != first;
1078 node = cgraph_next_function (node))
05806473 1079 {
7d0d0ce1 1080 tree decl = node->symbol.decl;
83a23b05 1081 if (DECL_PRESERVE_P (decl))
8efa224a 1082 cgraph_mark_force_output_node (node);
62433d51 1083 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
05806473 1084 {
7d0d0ce1 1085 if (! TREE_PUBLIC (node->symbol.decl))
1086 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
712d2297 1087 "%<externally_visible%>"
1088 " attribute have effect only on public objects");
05806473 1089 }
40b32d93 1090 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
c70f46b0 1091 && (node->local.finalized && !node->alias))
40b32d93 1092 {
7d0d0ce1 1093 warning_at (DECL_SOURCE_LOCATION (node->symbol.decl), OPT_Wattributes,
40b32d93 1094 "%<weakref%> attribute ignored"
1095 " because function is defined");
1096 DECL_WEAK (decl) = 0;
1097 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1098 DECL_ATTRIBUTES (decl));
1099 }
a522e9eb 1100
1101 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl))
1102 && !DECL_DECLARED_INLINE_P (decl)
1103 /* redefining extern inline function makes it DECL_UNINLINABLE. */
1104 && !DECL_UNINLINABLE (decl))
1105 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
1106 "always_inline function might not be inlinable");
1107
d05db70d 1108 process_common_attributes (decl);
05806473 1109 }
0704fb2e 1110 for (vnode = varpool_first_variable (); vnode != first_var;
1111 vnode = varpool_next_variable (vnode))
05806473 1112 {
7d0d0ce1 1113 tree decl = vnode->symbol.decl;
83a23b05 1114 if (DECL_PRESERVE_P (decl))
ff2a5ada 1115 vnode->symbol.force_output = true;
62433d51 1116 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
05806473 1117 {
7d0d0ce1 1118 if (! TREE_PUBLIC (vnode->symbol.decl))
1119 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
712d2297 1120 "%<externally_visible%>"
1121 " attribute have effect only on public objects");
05806473 1122 }
40b32d93 1123 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1124 && vnode->finalized
1125 && DECL_INITIAL (decl))
1126 {
7d0d0ce1 1127 warning_at (DECL_SOURCE_LOCATION (vnode->symbol.decl), OPT_Wattributes,
40b32d93 1128 "%<weakref%> attribute ignored"
1129 " because variable is initialized");
1130 DECL_WEAK (decl) = 0;
1131 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1132 DECL_ATTRIBUTES (decl));
1133 }
d05db70d 1134 process_common_attributes (decl);
05806473 1135 }
1136}
1137
ff2a5ada 1138/* Mark DECL as finalized. By finalizing the declaration, frontend instruct the
1139 middle end to output the variable to asm file, if needed or externally
1140 visible. */
1141
1142void
1143varpool_finalize_decl (tree decl)
1144{
1145 struct varpool_node *node = varpool_node (decl);
1146
1147 gcc_assert (TREE_STATIC (decl));
1148
1149 if (node->finalized)
1150 return;
1151 notice_global_symbol (decl);
1152 node->finalized = true;
1153 if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)
1154 /* Traditionally we do not eliminate static variables when not
1155 optimizing and when not doing toplevel reoder. */
1156 || (!flag_toplevel_reorder && !DECL_COMDAT (node->symbol.decl)
1157 && !DECL_ARTIFICIAL (node->symbol.decl)))
1158 node->symbol.force_output = true;
1159
1160 if (cgraph_state == CGRAPH_STATE_CONSTRUCTION
1161 && (decide_is_variable_needed (node, decl)
1162 || referred_to_p ((symtab_node)node)))
1163 enqueue_node ((symtab_node)node);
1164 if (cgraph_state >= CGRAPH_STATE_IPA_SSA)
1165 varpool_analyze_node (node);
1166}
1167
1168/* Discover all functions and variables that are trivially needed, analyze
1169 them as well as all functions and variables referred by them */
ae01b312 1170
aeeb194b 1171static void
1172cgraph_analyze_functions (void)
ae01b312 1173{
c1dcd13c 1174 /* Keep track of already processed nodes when called multiple times for
06b27565 1175 intermodule optimization. */
c1dcd13c 1176 static struct cgraph_node *first_analyzed;
ff2a5ada 1177 struct cgraph_node *first_handled = first_analyzed;
1d416bd7 1178 static struct varpool_node *first_analyzed_var;
ff2a5ada 1179 struct varpool_node *first_handled_var = first_analyzed_var;
1180
1181 symtab_node node, next;
1182 int i;
1183 struct ipa_ref *ref;
1184 bool changed = true;
ae01b312 1185
f1c35659 1186 bitmap_obstack_initialize (NULL);
ff2a5ada 1187 cgraph_state = CGRAPH_STATE_CONSTRUCTION;
ae01b312 1188
ff2a5ada 1189 /* Analysis adds static variables that in turn adds references to new functions.
1190 So we need to iterate the process until it stabilize. */
1191 while (changed)
ae01b312 1192 {
ff2a5ada 1193 changed = false;
1194 process_function_and_variable_attributes (first_analyzed,
1195 first_analyzed_var);
1196
1197 /* First identify the trivially needed symbols. */
1198 for (node = symtab_nodes;
1199 node != (symtab_node)first_analyzed
1200 && node != (symtab_node)first_analyzed_var; node = node->symbol.next)
9b8fb23a 1201 {
ff2a5ada 1202 if ((symtab_function_p (node)
1203 && cgraph (node)->local.finalized
1204 && cgraph_decide_is_function_needed (cgraph (node), node->symbol.decl))
1205 || (symtab_variable_p (node)
1206 && varpool (node)->finalized
1207 && !DECL_EXTERNAL (node->symbol.decl)
1208 && decide_is_variable_needed (varpool (node), node->symbol.decl)))
1209 {
1210 enqueue_node (node);
1211 if (!changed && cgraph_dump_file)
1212 fprintf (cgraph_dump_file, "Trivially needed symbols:");
1213 changed = true;
1214 if (cgraph_dump_file)
1215 fprintf (cgraph_dump_file, " %s", symtab_node_asm_name (node));
1216 }
1217 if (node == (symtab_node)first_analyzed
1218 || node == (symtab_node)first_analyzed_var)
1219 break;
9b8fb23a 1220 }
ff2a5ada 1221 cgraph_process_new_functions ();
1222 first_analyzed_var = varpool_first_variable ();
1223 first_analyzed = cgraph_first_function ();
638531ad 1224
ff2a5ada 1225 if (changed && dump_file)
1226 fprintf (cgraph_dump_file, "\n");
2c0b522d 1227
ff2a5ada 1228 /* Lower representation, build callgraph edges and references for all trivially
1229 needed symbols and all symbols referred by them. */
1230 while (first != (symtab_node)(void *)1)
61c2c7b1 1231 {
ff2a5ada 1232 changed = true;
1233 node = first;
1234 first = (symtab_node)first->symbol.aux;
1235 if (symtab_function_p (node) && cgraph (node)->local.finalized)
1236 {
1237 struct cgraph_edge *edge;
1238 struct cgraph_node *cnode;
1239 tree decl;
1240
1241 cnode = cgraph (node);
1242 decl = cnode->symbol.decl;
1243
1244 /* ??? It is possible to create extern inline function and later using
1245 weak alias attribute to kill its body. See
1246 gcc.c-torture/compile/20011119-1.c */
1247 if (!DECL_STRUCT_FUNCTION (decl)
1248 && (!cnode->alias || !cnode->thunk.alias)
1249 && !cnode->thunk.thunk_p)
1250 {
1251 cgraph_reset_node (cnode);
1252 cnode->local.redefined_extern_inline = true;
1253 continue;
1254 }
61c2c7b1 1255
ff2a5ada 1256 if (!cnode->analyzed)
1257 cgraph_analyze_function (cnode);
d544ceff 1258
ff2a5ada 1259 for (edge = cnode->callees; edge; edge = edge->next_callee)
da751785 1260 if (edge->callee->local.finalized)
1261 enqueue_node ((symtab_node)edge->callee);
ff2a5ada 1262
1263 /* If decl is a clone of an abstract function, mark that abstract
1264 function so that we don't release its body. The DECL_INITIAL() of that
1265 abstract function declaration will be later needed to output debug
1266 info. */
1267 if (DECL_ABSTRACT_ORIGIN (decl))
1268 {
1269 struct cgraph_node *origin_node;
1270 origin_node = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
1271 origin_node->abstract_and_needed = true;
1272 }
1273
1274 }
1275 else if (symtab_variable_p (node)
1276 && varpool (node)->finalized)
da751785 1277 varpool_analyze_node (varpool (node));
ff2a5ada 1278
1279 if (node->symbol.same_comdat_group)
1280 {
1281 symtab_node next;
1282 for (next = node->symbol.same_comdat_group;
1283 next != node;
1284 next = next->symbol.same_comdat_group)
1285 enqueue_node (next);
1286 }
1287 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
1288 if ((symtab_function_p (ref->referred) && cgraph (ref->referred)->local.finalized)
1289 || (symtab_variable_p (ref->referred) && varpool (ref->referred)->finalized))
1290 enqueue_node (ref->referred);
1291 cgraph_process_new_functions ();
1292 }
ae01b312 1293 }
2c0b522d 1294
aa5e06c7 1295 /* Collect entry points to the unit. */
f79b6507 1296 if (cgraph_dump_file)
3d7bfc56 1297 {
e4200070 1298 fprintf (cgraph_dump_file, "\n\nInitial ");
18841b0c 1299 dump_symtab (cgraph_dump_file);
3d7bfc56 1300 }
e6d2b2d8 1301
f79b6507 1302 if (cgraph_dump_file)
ff2a5ada 1303 fprintf (cgraph_dump_file, "\nRemoving unused symbols:");
ae01b312 1304
ff2a5ada 1305 for (node = symtab_nodes;
1306 node != (symtab_node)first_handled
1307 && node != (symtab_node)first_handled_var; node = next)
ae01b312 1308 {
ff2a5ada 1309 next = node->symbol.next;
1310 if (!node->symbol.aux && !referred_to_p (node))
ae01b312 1311 {
f79b6507 1312 if (cgraph_dump_file)
ff2a5ada 1313 fprintf (cgraph_dump_file, " %s", symtab_node_name (node));
1314 symtab_remove_node (node);
9b8fb23a 1315 continue;
ae01b312 1316 }
ff2a5ada 1317 if (symtab_function_p (node))
1318 {
1319 tree decl = node->symbol.decl;
1320 struct cgraph_node *cnode = cgraph (node);
1321
1322 if (cnode->local.finalized && !gimple_has_body_p (decl)
1323 && (!cnode->alias || !cnode->thunk.alias)
1324 && !cnode->thunk.thunk_p)
1325 cgraph_reset_node (cnode);
1326
1327 gcc_assert (!cnode->local.finalized || cnode->thunk.thunk_p
1328 || cnode->alias
1329 || gimple_has_body_p (decl));
1330 gcc_assert (cnode->analyzed == cnode->local.finalized);
1331 }
1332 node->symbol.aux = NULL;
ae01b312 1333 }
ff2a5ada 1334 first_analyzed = cgraph_first_function ();
1335 first_analyzed_var = varpool_first_variable ();
f79b6507 1336 if (cgraph_dump_file)
e4200070 1337 {
1338 fprintf (cgraph_dump_file, "\n\nReclaimed ");
18841b0c 1339 dump_symtab (cgraph_dump_file);
e4200070 1340 }
f1c35659 1341 bitmap_obstack_release (NULL);
ae01b312 1342 ggc_collect ();
aeeb194b 1343}
1344
3a849bc1 1345/* Translate the ugly representation of aliases as alias pairs into nice
1346 representation in callgraph. We don't handle all cases yet,
1347 unforutnately. */
1348
1349static void
1350handle_alias_pairs (void)
1351{
1352 alias_pair *p;
1353 unsigned i;
1354 struct cgraph_node *target_node;
1355 struct cgraph_node *src_node;
e0eaac80 1356 struct varpool_node *target_vnode;
3a849bc1 1357
1358 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p);)
1359 {
1360 if (TREE_CODE (p->decl) == FUNCTION_DECL
3a849bc1 1361 && (target_node = cgraph_node_for_asm (p->target)) != NULL)
1362 {
1363 src_node = cgraph_get_node (p->decl);
1364 if (src_node && src_node->local.finalized)
1365 cgraph_reset_node (src_node);
1366 /* Normally EXTERNAL flag is used to mark external inlines,
1367 however for aliases it seems to be allowed to use it w/o
1368 any meaning. See gcc.dg/attr-alias-3.c
1369 However for weakref we insist on EXTERNAL flag being set.
1370 See gcc.dg/attr-alias-5.c */
1371 if (DECL_EXTERNAL (p->decl))
7d0d0ce1 1372 DECL_EXTERNAL (p->decl)
1373 = lookup_attribute ("weakref",
1374 DECL_ATTRIBUTES (p->decl)) != NULL;
1375 cgraph_create_function_alias (p->decl, target_node->symbol.decl);
3a849bc1 1376 VEC_unordered_remove (alias_pair, alias_pairs, i);
1377 }
e0eaac80 1378 else if (TREE_CODE (p->decl) == VAR_DECL
e0eaac80 1379 && (target_vnode = varpool_node_for_asm (p->target)) != NULL)
1380 {
1381 /* Normally EXTERNAL flag is used to mark external inlines,
1382 however for aliases it seems to be allowed to use it w/o
1383 any meaning. See gcc.dg/attr-alias-3.c
1384 However for weakref we insist on EXTERNAL flag being set.
1385 See gcc.dg/attr-alias-5.c */
1386 if (DECL_EXTERNAL (p->decl))
7d0d0ce1 1387 DECL_EXTERNAL (p->decl)
1388 = lookup_attribute ("weakref",
1389 DECL_ATTRIBUTES (p->decl)) != NULL;
1390 varpool_create_variable_alias (p->decl, target_vnode->symbol.decl);
e0eaac80 1391 VEC_unordered_remove (alias_pair, alias_pairs, i);
1392 }
badeded8 1393 /* Weakrefs with target not defined in current unit are easy to handle; they
1394 behave just as external variables except we need to note the alias flag
1395 to later output the weakref pseudo op into asm file. */
1396 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)) != NULL
1397 && (TREE_CODE (p->decl) == FUNCTION_DECL
1398 ? (varpool_node_for_asm (p->target) == NULL)
1399 : (cgraph_node_for_asm (p->target) == NULL)))
1400 {
1401 if (TREE_CODE (p->decl) == FUNCTION_DECL)
1402 cgraph_get_create_node (p->decl)->alias = true;
1403 else
1404 varpool_get_node (p->decl)->alias = true;
1405 DECL_EXTERNAL (p->decl) = 1;
1406 VEC_unordered_remove (alias_pair, alias_pairs, i);
1407 }
3a849bc1 1408 else
1409 {
1410 if (dump_file)
1411 fprintf (dump_file, "Unhandled alias %s->%s\n",
1412 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p->decl)),
1413 IDENTIFIER_POINTER (p->target));
1414
1415 i++;
1416 }
1417 }
1418}
1419
8f69fd82 1420
ae01b312 1421/* Figure out what functions we want to assemble. */
1422
1423static void
d9d9733a 1424cgraph_mark_functions_to_output (void)
ae01b312 1425{
1426 struct cgraph_node *node;
61c2c7b1 1427#ifdef ENABLE_CHECKING
1428 bool check_same_comdat_groups = false;
1429
7c455d87 1430 FOR_EACH_FUNCTION (node)
61c2c7b1 1431 gcc_assert (!node->process);
1432#endif
ae01b312 1433
7c455d87 1434 FOR_EACH_FUNCTION (node)
ae01b312 1435 {
7d0d0ce1 1436 tree decl = node->symbol.decl;
d7c6d889 1437 struct cgraph_edge *e;
a0c938f0 1438
7d0d0ce1 1439 gcc_assert (!node->process || node->symbol.same_comdat_group);
61c2c7b1 1440 if (node->process)
1441 continue;
d7c6d889 1442
1443 for (e = node->callers; e; e = e->next_caller)
611e5405 1444 if (e->inline_failed)
d7c6d889 1445 break;
ae01b312 1446
e6d2b2d8 1447 /* We need to output all local functions that are used and not
1448 always inlined, as well as those that are reachable from
1449 outside the current compilation unit. */
1a1a827a 1450 if (node->analyzed
91bf9d9a 1451 && !node->thunk.thunk_p
c70f46b0 1452 && !node->alias
b0cdf642 1453 && !node->global.inlined_to
4ee9c684 1454 && !TREE_ASM_WRITTEN (decl)
ae01b312 1455 && !DECL_EXTERNAL (decl))
61c2c7b1 1456 {
1457 node->process = 1;
7d0d0ce1 1458 if (node->symbol.same_comdat_group)
61c2c7b1 1459 {
1460 struct cgraph_node *next;
7d0d0ce1 1461 for (next = cgraph (node->symbol.same_comdat_group);
61c2c7b1 1462 next != node;
7d0d0ce1 1463 next = cgraph (next->symbol.same_comdat_group))
c70f46b0 1464 if (!next->thunk.thunk_p && !next->alias)
91bf9d9a 1465 next->process = 1;
61c2c7b1 1466 }
1467 }
7d0d0ce1 1468 else if (node->symbol.same_comdat_group)
61c2c7b1 1469 {
1470#ifdef ENABLE_CHECKING
1471 check_same_comdat_groups = true;
1472#endif
1473 }
cc636d56 1474 else
9cee7c3f 1475 {
1476 /* We should've reclaimed all functions that are not needed. */
1477#ifdef ENABLE_CHECKING
75a70cf9 1478 if (!node->global.inlined_to
1a1a827a 1479 && gimple_has_body_p (decl)
08843223 1480 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1481 are inside partition, we can end up not removing the body since we no longer
1482 have analyzed node pointing to it. */
7d0d0ce1 1483 && !node->symbol.in_other_partition
c70f46b0 1484 && !node->alias
9cee7c3f 1485 && !DECL_EXTERNAL (decl))
1486 {
1487 dump_cgraph_node (stderr, node);
1488 internal_error ("failed to reclaim unneeded function");
1489 }
1490#endif
75a70cf9 1491 gcc_assert (node->global.inlined_to
1a1a827a 1492 || !gimple_has_body_p (decl)
7d0d0ce1 1493 || node->symbol.in_other_partition
9cee7c3f 1494 || DECL_EXTERNAL (decl));
1495
1496 }
a0c938f0 1497
961e3b13 1498 }
61c2c7b1 1499#ifdef ENABLE_CHECKING
1500 if (check_same_comdat_groups)
7c455d87 1501 FOR_EACH_FUNCTION (node)
7d0d0ce1 1502 if (node->symbol.same_comdat_group && !node->process)
61c2c7b1 1503 {
7d0d0ce1 1504 tree decl = node->symbol.decl;
61c2c7b1 1505 if (!node->global.inlined_to
1506 && gimple_has_body_p (decl)
6d36105a 1507 /* FIXME: in an ltrans unit when the offline copy is outside a
1508 partition but inline copies are inside a partition, we can
1509 end up not removing the body since we no longer have an
1510 analyzed node pointing to it. */
7d0d0ce1 1511 && !node->symbol.in_other_partition
61c2c7b1 1512 && !DECL_EXTERNAL (decl))
1513 {
1514 dump_cgraph_node (stderr, node);
6d36105a 1515 internal_error ("failed to reclaim unneeded function in same "
1516 "comdat group");
61c2c7b1 1517 }
1518 }
1519#endif
961e3b13 1520}
1521
28454517 1522/* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1523 in lowered gimple form.
1524
1525 Set current_function_decl and cfun to newly constructed empty function body.
1526 return basic block in the function body. */
1527
1528static basic_block
1529init_lowered_empty_function (tree decl)
1530{
1531 basic_block bb;
1532
1533 current_function_decl = decl;
1534 allocate_struct_function (decl, false);
1535 gimple_register_cfg_hooks ();
1536 init_empty_tree_cfg ();
1537 init_tree_ssa (cfun);
1538 init_ssa_operands ();
1539 cfun->gimple_df->in_ssa_p = true;
1540 DECL_INITIAL (decl) = make_node (BLOCK);
1541
1542 DECL_SAVED_TREE (decl) = error_mark_node;
1543 cfun->curr_properties |=
1544 (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_referenced_vars |
657e3a56 1545 PROP_ssa | PROP_gimple_any);
28454517 1546
1547 /* Create BB for body of the function and connect it properly. */
1548 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
167ef6d9 1549 make_edge (ENTRY_BLOCK_PTR, bb, 0);
1550 make_edge (bb, EXIT_BLOCK_PTR, 0);
28454517 1551
1552 return bb;
1553}
1554
1555/* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1556 offset indicated by VIRTUAL_OFFSET, if that is
1557 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1558 zero for a result adjusting thunk. */
1559
1560static tree
1561thunk_adjust (gimple_stmt_iterator * bsi,
1562 tree ptr, bool this_adjusting,
1563 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1564{
1565 gimple stmt;
1566 tree ret;
1567
55d6cb23 1568 if (this_adjusting
1569 && fixed_offset != 0)
28454517 1570 {
2cc66f2a 1571 stmt = gimple_build_assign
1572 (ptr, fold_build_pointer_plus_hwi_loc (input_location,
1573 ptr,
1574 fixed_offset));
28454517 1575 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1576 }
1577
1578 /* If there's a virtual offset, look up that value in the vtable and
1579 adjust the pointer again. */
1580 if (virtual_offset)
1581 {
1582 tree vtabletmp;
1583 tree vtabletmp2;
1584 tree vtabletmp3;
28454517 1585
1586 if (!vtable_entry_type)
1587 {
1588 tree vfunc_type = make_node (FUNCTION_TYPE);
1589 TREE_TYPE (vfunc_type) = integer_type_node;
1590 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1591 layout_type (vfunc_type);
1592
1593 vtable_entry_type = build_pointer_type (vfunc_type);
1594 }
1595
1596 vtabletmp =
1597 create_tmp_var (build_pointer_type
1598 (build_pointer_type (vtable_entry_type)), "vptr");
1599
1600 /* The vptr is always at offset zero in the object. */
1601 stmt = gimple_build_assign (vtabletmp,
1602 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1603 ptr));
1604 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1605 mark_symbols_for_renaming (stmt);
1606 find_referenced_vars_in (stmt);
1607
1608 /* Form the vtable address. */
1609 vtabletmp2 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp)),
1610 "vtableaddr");
1611 stmt = gimple_build_assign (vtabletmp2,
182cf5a9 1612 build_simple_mem_ref (vtabletmp));
28454517 1613 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1614 mark_symbols_for_renaming (stmt);
1615 find_referenced_vars_in (stmt);
1616
1617 /* Find the entry with the vcall offset. */
1618 stmt = gimple_build_assign (vtabletmp2,
2cc66f2a 1619 fold_build_pointer_plus_loc (input_location,
1620 vtabletmp2,
1621 virtual_offset));
28454517 1622 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1623
1624 /* Get the offset itself. */
1625 vtabletmp3 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1626 "vcalloffset");
1627 stmt = gimple_build_assign (vtabletmp3,
182cf5a9 1628 build_simple_mem_ref (vtabletmp2));
28454517 1629 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1630 mark_symbols_for_renaming (stmt);
1631 find_referenced_vars_in (stmt);
1632
28454517 1633 /* Adjust the `this' pointer. */
a0553bff 1634 ptr = fold_build_pointer_plus_loc (input_location, ptr, vtabletmp3);
1635 ptr = force_gimple_operand_gsi (bsi, ptr, true, NULL_TREE, false,
1636 GSI_CONTINUE_LINKING);
28454517 1637 }
1638
55d6cb23 1639 if (!this_adjusting
1640 && fixed_offset != 0)
28454517 1641 /* Adjust the pointer by the constant. */
1642 {
1643 tree ptrtmp;
1644
1645 if (TREE_CODE (ptr) == VAR_DECL)
1646 ptrtmp = ptr;
1647 else
1648 {
1649 ptrtmp = create_tmp_var (TREE_TYPE (ptr), "ptr");
1650 stmt = gimple_build_assign (ptrtmp, ptr);
1651 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1652 mark_symbols_for_renaming (stmt);
1653 find_referenced_vars_in (stmt);
1654 }
2cc66f2a 1655 ptr = fold_build_pointer_plus_hwi_loc (input_location,
1656 ptrtmp, fixed_offset);
28454517 1657 }
1658
1659 /* Emit the statement and gimplify the adjustment expression. */
1660 ret = create_tmp_var (TREE_TYPE (ptr), "adjusted_this");
1661 stmt = gimple_build_assign (ret, ptr);
1662 mark_symbols_for_renaming (stmt);
1663 find_referenced_vars_in (stmt);
1664 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1665
1666 return ret;
1667}
1668
1669/* Produce assembler for thunk NODE. */
1670
1671static void
1672assemble_thunk (struct cgraph_node *node)
1673{
1674 bool this_adjusting = node->thunk.this_adjusting;
1675 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1676 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1677 tree virtual_offset = NULL;
1678 tree alias = node->thunk.alias;
7d0d0ce1 1679 tree thunk_fndecl = node->symbol.decl;
28454517 1680 tree a = DECL_ARGUMENTS (thunk_fndecl);
1681
1682 current_function_decl = thunk_fndecl;
1683
aed6e608 1684 /* Ensure thunks are emitted in their correct sections. */
1685 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1686
28454517 1687 if (this_adjusting
1688 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1689 virtual_value, alias))
1690 {
1691 const char *fnname;
1692 tree fn_block;
28b2c6a7 1693 tree restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
28454517 1694
1695 DECL_RESULT (thunk_fndecl)
1696 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
28b2c6a7 1697 RESULT_DECL, 0, restype);
22ea3b47 1698 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
28454517 1699
1700 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1701 create one. */
1702 fn_block = make_node (BLOCK);
1703 BLOCK_VARS (fn_block) = a;
1704 DECL_INITIAL (thunk_fndecl) = fn_block;
1705 init_function_start (thunk_fndecl);
1706 cfun->is_thunk = 1;
1707 assemble_start_function (thunk_fndecl, fnname);
1708
1709 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1710 fixed_offset, virtual_value, alias);
1711
1712 assemble_end_function (thunk_fndecl, fnname);
1713 init_insn_lengths ();
1714 free_after_compilation (cfun);
1715 set_cfun (NULL);
1716 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
91bf9d9a 1717 node->thunk.thunk_p = false;
1718 node->analyzed = false;
28454517 1719 }
1720 else
1721 {
1722 tree restype;
1723 basic_block bb, then_bb, else_bb, return_bb;
1724 gimple_stmt_iterator bsi;
1725 int nargs = 0;
1726 tree arg;
1727 int i;
1728 tree resdecl;
1729 tree restmp = NULL;
1730 VEC(tree, heap) *vargs;
1731
1732 gimple call;
1733 gimple ret;
1734
1735 DECL_IGNORED_P (thunk_fndecl) = 1;
1736 bitmap_obstack_initialize (NULL);
1737
1738 if (node->thunk.virtual_offset_p)
1739 virtual_offset = size_int (virtual_value);
1740
1741 /* Build the return declaration for the function. */
1742 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1743 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1744 {
1745 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1746 DECL_ARTIFICIAL (resdecl) = 1;
1747 DECL_IGNORED_P (resdecl) = 1;
1748 DECL_RESULT (thunk_fndecl) = resdecl;
1749 }
1750 else
1751 resdecl = DECL_RESULT (thunk_fndecl);
1752
1753 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl);
1754
1755 bsi = gsi_start_bb (bb);
1756
1757 /* Build call to the function being thunked. */
1758 if (!VOID_TYPE_P (restype))
1759 {
1760 if (!is_gimple_reg_type (restype))
1761 {
1762 restmp = resdecl;
2ab2ce89 1763 add_local_decl (cfun, restmp);
28454517 1764 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1765 }
1766 else
1767 restmp = create_tmp_var_raw (restype, "retval");
1768 }
1769
1767a056 1770 for (arg = a; arg; arg = DECL_CHAIN (arg))
28454517 1771 nargs++;
1772 vargs = VEC_alloc (tree, heap, nargs);
1773 if (this_adjusting)
1774 VEC_quick_push (tree, vargs,
1775 thunk_adjust (&bsi,
1776 a, 1, fixed_offset,
1777 virtual_offset));
1778 else
1779 VEC_quick_push (tree, vargs, a);
1767a056 1780 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
28454517 1781 VEC_quick_push (tree, vargs, arg);
1782 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1783 VEC_free (tree, heap, vargs);
28454517 1784 gimple_call_set_from_thunk (call, true);
1785 if (restmp)
1786 gimple_call_set_lhs (call, restmp);
1787 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1788 mark_symbols_for_renaming (call);
1789 find_referenced_vars_in (call);
1790 update_stmt (call);
1791
1792 if (restmp && !this_adjusting)
1793 {
57ab8ec3 1794 tree true_label = NULL_TREE;
28454517 1795
1796 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1797 {
1798 gimple stmt;
1799 /* If the return type is a pointer, we need to
1800 protect against NULL. We know there will be an
1801 adjustment, because that's why we're emitting a
1802 thunk. */
1803 then_bb = create_basic_block (NULL, (void *) 0, bb);
1804 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1805 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1806 remove_edge (single_succ_edge (bb));
1807 true_label = gimple_block_label (then_bb);
28454517 1808 stmt = gimple_build_cond (NE_EXPR, restmp,
385f3f36 1809 build_zero_cst (TREE_TYPE (restmp)),
28454517 1810 NULL_TREE, NULL_TREE);
1811 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1812 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1813 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1814 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1815 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1816 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1817 bsi = gsi_last_bb (then_bb);
1818 }
1819
1820 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1821 fixed_offset, virtual_offset);
1822 if (true_label)
1823 {
1824 gimple stmt;
1825 bsi = gsi_last_bb (else_bb);
385f3f36 1826 stmt = gimple_build_assign (restmp,
1827 build_zero_cst (TREE_TYPE (restmp)));
28454517 1828 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1829 bsi = gsi_last_bb (return_bb);
1830 }
1831 }
1832 else
1833 gimple_call_set_tail (call, true);
1834
1835 /* Build return value. */
1836 ret = gimple_build_return (restmp);
1837 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1838
1839 delete_unreachable_blocks ();
1840 update_ssa (TODO_update_ssa);
1841
28454517 1842 /* Since we want to emit the thunk, we explicitly mark its name as
1843 referenced. */
91bf9d9a 1844 node->thunk.thunk_p = false;
1845 cgraph_node_remove_callees (node);
28454517 1846 cgraph_add_new_function (thunk_fndecl, true);
1847 bitmap_obstack_release (NULL);
1848 }
1849 current_function_decl = NULL;
1850}
1851
91bf9d9a 1852
c70f46b0 1853
1854/* Assemble thunks and aliases asociated to NODE. */
91bf9d9a 1855
1856static void
c70f46b0 1857assemble_thunks_and_aliases (struct cgraph_node *node)
91bf9d9a 1858{
1859 struct cgraph_edge *e;
c70f46b0 1860 int i;
1861 struct ipa_ref *ref;
1862
91bf9d9a 1863 for (e = node->callers; e;)
1864 if (e->caller->thunk.thunk_p)
1865 {
1866 struct cgraph_node *thunk = e->caller;
1867
1868 e = e->next_caller;
c70f46b0 1869 assemble_thunks_and_aliases (thunk);
91bf9d9a 1870 assemble_thunk (thunk);
1871 }
1872 else
1873 e = e->next_caller;
04ec15fa 1874 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
7d0d0ce1 1875 i, ref); i++)
c70f46b0 1876 if (ref->use == IPA_REF_ALIAS)
1877 {
04ec15fa 1878 struct cgraph_node *alias = ipa_ref_referring_node (ref);
968b8c52 1879 bool saved_written = TREE_ASM_WRITTEN (alias->thunk.alias);
1880
1881 /* Force assemble_alias to really output the alias this time instead
1882 of buffering it in same alias pairs. */
1883 TREE_ASM_WRITTEN (alias->thunk.alias) = 1;
7d0d0ce1 1884 assemble_alias (alias->symbol.decl,
c70f46b0 1885 DECL_ASSEMBLER_NAME (alias->thunk.alias));
1886 assemble_thunks_and_aliases (alias);
968b8c52 1887 TREE_ASM_WRITTEN (alias->thunk.alias) = saved_written;
c70f46b0 1888 }
91bf9d9a 1889}
1890
3db65b62 1891/* Perform IPA transforms and all further optimizations and compilation
1892 for FNDECL. */
941366fd 1893
3db65b62 1894static void
941366fd 1895tree_rest_of_compilation (struct cgraph_node *node)
1896{
7d0d0ce1 1897 tree fndecl = node->symbol.decl;
941366fd 1898 location_t saved_loc;
1899
1900 timevar_push (TV_REST_OF_COMPILATION);
1901
1902 gcc_assert (cgraph_global_info_ready);
1903
1904 /* Initialize the default bitmap obstack. */
1905 bitmap_obstack_initialize (NULL);
1906
1907 /* Initialize the RTL code for the function. */
1908 current_function_decl = fndecl;
1909 saved_loc = input_location;
1910 input_location = DECL_SOURCE_LOCATION (fndecl);
1911 init_function_start (fndecl);
1912
1913 gimple_register_cfg_hooks ();
1914
1915 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
1916
1917 execute_all_ipa_transforms ();
1918
1919 /* Perform all tree transforms and optimizations. */
1920
1921 /* Signal the start of passes. */
1922 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
1923
1924 execute_pass_list (all_passes);
1925
1926 /* Signal the end of passes. */
1927 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
1928
1929 bitmap_obstack_release (&reg_obstack);
1930
1931 /* Release the default bitmap obstack. */
1932 bitmap_obstack_release (NULL);
1933
1934 set_cfun (NULL);
1935
1936 /* If requested, warn about function definitions where the function will
1937 return a value (usually of some struct or union type) which itself will
1938 take up a lot of stack space. */
1939 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
1940 {
1941 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
1942
1943 if (ret_type && TYPE_SIZE_UNIT (ret_type)
1944 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
1945 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
1946 larger_than_size))
1947 {
1948 unsigned int size_as_int
1949 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
1950
1951 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
1952 warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
1953 fndecl, size_as_int);
1954 else
1955 warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
1956 fndecl, larger_than_size);
1957 }
1958 }
1959
1960 gimple_set_body (fndecl, NULL);
1961 if (DECL_STRUCT_FUNCTION (fndecl) == 0
1962 && !cgraph_get_node (fndecl)->origin)
1963 {
1964 /* Stop pointing to the local nodes about to be freed.
1965 But DECL_INITIAL must remain nonzero so we know this
1966 was an actual function definition.
1967 For a nested function, this is done in c_pop_function_context.
1968 If rest_of_compilation set this to 0, leave it 0. */
1969 if (DECL_INITIAL (fndecl) != 0)
1970 DECL_INITIAL (fndecl) = error_mark_node;
1971 }
1972
1973 input_location = saved_loc;
1974
1975 ggc_collect ();
1976 timevar_pop (TV_REST_OF_COMPILATION);
1977}
1978
ae01b312 1979/* Expand function specified by NODE. */
e6d2b2d8 1980
ae01b312 1981static void
d9d9733a 1982cgraph_expand_function (struct cgraph_node *node)
ae01b312 1983{
7d0d0ce1 1984 tree decl = node->symbol.decl;
ae01b312 1985
b0cdf642 1986 /* We ought to not compile any inline clones. */
cc636d56 1987 gcc_assert (!node->global.inlined_to);
b0cdf642 1988
6329636b 1989 announce_function (decl);
09fc9532 1990 node->process = 0;
f7777314 1991 gcc_assert (node->lowered);
1992
1993 /* Generate RTL for the body of DECL. */
941366fd 1994 tree_rest_of_compilation (node);
f7777314 1995
1996 /* Make sure that BE didn't give up on compiling. */
1997 gcc_assert (TREE_ASM_WRITTEN (decl));
1998 current_function_decl = NULL;
cc91b414 1999 gcc_assert (!cgraph_preserve_function_body_p (node));
f76f7453 2000
2001 /* It would make a lot more sense to output thunks before function body to get more
2002 forward and lest backwarding jumps. This is however would need solving problem
2003 with comdats. See PR48668. Also aliases must come after function itself to
2004 make one pass assemblers, like one on AIX happy. See PR 50689.
2005 FIXME: Perhaps thunks should be move before function IFF they are not in comdat
2006 groups. */
2007 assemble_thunks_and_aliases (node);
1a1a827a 2008 cgraph_release_function_body (node);
2009 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
2010 points to the dead function body. */
2011 cgraph_node_remove_callees (node);
ae01b312 2012}
2013
b0cdf642 2014/* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
d7c6d889 2015
2016bool
326a9581 2017cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
d7c6d889 2018{
b0cdf642 2019 *reason = e->inline_failed;
2020 return !e->inline_failed;
d7c6d889 2021}
b0cdf642 2022
acc70efa 2023
acc70efa 2024
d9d9733a 2025/* Expand all functions that must be output.
2026
d7c6d889 2027 Attempt to topologically sort the nodes so function is output when
2028 all called functions are already assembled to allow data to be
91c82c20 2029 propagated across the callgraph. Use a stack to get smaller distance
3927afe0 2030 between a function and its callees (later we may choose to use a more
d7c6d889 2031 sophisticated algorithm for function reordering; we will likely want
2032 to use subsections to make the output functions appear in top-down
2033 order). */
2034
2035static void
a6868229 2036cgraph_expand_all_functions (void)
d7c6d889 2037{
2038 struct cgraph_node *node;
4c36ffe6 2039 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
c04e3894 2040 int order_pos, new_order_pos = 0;
d7c6d889 2041 int i;
2042
7771d558 2043 order_pos = ipa_reverse_postorder (order);
cc636d56 2044 gcc_assert (order_pos == cgraph_n_nodes);
d7c6d889 2045
7bd28bba 2046 /* Garbage collector may remove inline clones we eliminate during
b0cdf642 2047 optimization. So we must be sure to not reference them. */
2048 for (i = 0; i < order_pos; i++)
09fc9532 2049 if (order[i]->process)
b0cdf642 2050 order[new_order_pos++] = order[i];
2051
2052 for (i = new_order_pos - 1; i >= 0; i--)
d7c6d889 2053 {
2054 node = order[i];
09fc9532 2055 if (node->process)
d7c6d889 2056 {
09fc9532 2057 node->process = 0;
d7c6d889 2058 cgraph_expand_function (node);
2059 }
2060 }
523c1122 2061 cgraph_process_new_functions ();
773c5ba7 2062
d7c6d889 2063 free (order);
773c5ba7 2064
d7c6d889 2065}
2066
56af936e 2067/* This is used to sort the node types by the cgraph order number. */
2068
0b09525f 2069enum cgraph_order_sort_kind
2070{
2071 ORDER_UNDEFINED = 0,
2072 ORDER_FUNCTION,
2073 ORDER_VAR,
2074 ORDER_ASM
2075};
2076
56af936e 2077struct cgraph_order_sort
2078{
0b09525f 2079 enum cgraph_order_sort_kind kind;
56af936e 2080 union
2081 {
2082 struct cgraph_node *f;
1d416bd7 2083 struct varpool_node *v;
56af936e 2084 struct cgraph_asm_node *a;
2085 } u;
2086};
2087
2088/* Output all functions, variables, and asm statements in the order
2089 according to their order fields, which is the order in which they
2090 appeared in the file. This implements -fno-toplevel-reorder. In
2091 this mode we may output functions and variables which don't really
2092 need to be output. */
2093
2094static void
2095cgraph_output_in_order (void)
2096{
2097 int max;
56af936e 2098 struct cgraph_order_sort *nodes;
2099 int i;
2100 struct cgraph_node *pf;
1d416bd7 2101 struct varpool_node *pv;
56af936e 2102 struct cgraph_asm_node *pa;
2103
0704fb2e 2104 max = symtab_order;
3e1cde87 2105 nodes = XCNEWVEC (struct cgraph_order_sort, max);
56af936e 2106
7c455d87 2107 FOR_EACH_DEFINED_FUNCTION (pf)
56af936e 2108 {
c70f46b0 2109 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
56af936e 2110 {
7d0d0ce1 2111 i = pf->symbol.order;
56af936e 2112 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2113 nodes[i].kind = ORDER_FUNCTION;
2114 nodes[i].u.f = pf;
2115 }
2116 }
2117
7c455d87 2118 FOR_EACH_DEFINED_VARIABLE (pv)
56af936e 2119 {
7d0d0ce1 2120 i = pv->symbol.order;
56af936e 2121 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2122 nodes[i].kind = ORDER_VAR;
2123 nodes[i].u.v = pv;
2124 }
2125
2126 for (pa = cgraph_asm_nodes; pa; pa = pa->next)
2127 {
2128 i = pa->order;
2129 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
2130 nodes[i].kind = ORDER_ASM;
2131 nodes[i].u.a = pa;
2132 }
56af936e 2133
304e5318 2134 /* In toplevel reorder mode we output all statics; mark them as needed. */
304e5318 2135
91da0f1c 2136 for (i = 0; i < max; ++i)
2137 if (nodes[i].kind == ORDER_VAR)
2138 varpool_finalize_named_section_flags (nodes[i].u.v);
2139
56af936e 2140 for (i = 0; i < max; ++i)
2141 {
2142 switch (nodes[i].kind)
2143 {
2144 case ORDER_FUNCTION:
09fc9532 2145 nodes[i].u.f->process = 0;
56af936e 2146 cgraph_expand_function (nodes[i].u.f);
2147 break;
2148
2149 case ORDER_VAR:
1d416bd7 2150 varpool_assemble_decl (nodes[i].u.v);
56af936e 2151 break;
2152
2153 case ORDER_ASM:
2154 assemble_asm (nodes[i].u.a->asm_str);
2155 break;
2156
2157 case ORDER_UNDEFINED:
2158 break;
2159
2160 default:
2161 gcc_unreachable ();
2162 }
2163 }
4b4ea2db 2164
2165 cgraph_asm_nodes = NULL;
3e1cde87 2166 free (nodes);
56af936e 2167}
2168
b0cdf642 2169/* Return true when function body of DECL still needs to be kept around
2170 for later re-use. */
2171bool
cc91b414 2172cgraph_preserve_function_body_p (struct cgraph_node *node)
b0cdf642 2173{
8d8c4c8d 2174 gcc_assert (cgraph_global_info_ready);
c70f46b0 2175 gcc_assert (!node->alias && !node->thunk.thunk_p);
cc91b414 2176
b0cdf642 2177 /* Look if there is any clone around. */
ccf4ab6b 2178 if (node->clones)
2179 return true;
b0cdf642 2180 return false;
2181}
2182
77fce4cd 2183static void
2184ipa_passes (void)
2185{
87d4aa85 2186 set_cfun (NULL);
4b14adf9 2187 current_function_decl = NULL;
75a70cf9 2188 gimple_register_cfg_hooks ();
77fce4cd 2189 bitmap_obstack_initialize (NULL);
59dd4830 2190
c9036234 2191 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
2192
59dd4830 2193 if (!in_lto_p)
7b2e8956 2194 {
2195 execute_ipa_pass_list (all_small_ipa_passes);
2196 if (seen_error ())
2197 return;
2198 }
9ed5b1f5 2199
941125aa 2200 /* We never run removal of unreachable nodes after early passes. This is
2201 because TODO is run before the subpasses. It is important to remove
2202 the unreachable functions to save works at IPA level and to get LTO
2203 symbol tables right. */
2204 cgraph_remove_unreachable_nodes (true, cgraph_dump_file);
2205
7bfefa9d 2206 /* If pass_all_early_optimizations was not scheduled, the state of
2207 the cgraph will not be properly updated. Update it now. */
2208 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
2209 cgraph_state = CGRAPH_STATE_IPA_SSA;
9ed5b1f5 2210
7bfefa9d 2211 if (!in_lto_p)
2212 {
2213 /* Generate coverage variables and constructors. */
2214 coverage_finish ();
2215
2216 /* Process new functions added. */
2217 set_cfun (NULL);
2218 current_function_decl = NULL;
2219 cgraph_process_new_functions ();
7bfefa9d 2220
c9036234 2221 execute_ipa_summary_passes
2222 ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
8867b500 2223 }
23433d72 2224
2225 /* Some targets need to handle LTO assembler output specially. */
2226 if (flag_generate_lto)
2227 targetm.asm_out.lto_start ();
2228
7bfefa9d 2229 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
2230
2231 if (!in_lto_p)
2232 ipa_write_summaries ();
2233
23433d72 2234 if (flag_generate_lto)
2235 targetm.asm_out.lto_end ();
2236
b33542ab 2237 if (!flag_ltrans && (in_lto_p || !flag_lto || flag_fat_lto_objects))
8867b500 2238 execute_ipa_pass_list (all_regular_ipa_passes);
c9036234 2239 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
9ed5b1f5 2240
77fce4cd 2241 bitmap_obstack_release (NULL);
2242}
2243
badeded8 2244
2245/* Return string alias is alias of. */
2246
2247static tree
2248get_alias_symbol (tree decl)
2249{
2250 tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
2251 return get_identifier (TREE_STRING_POINTER
2252 (TREE_VALUE (TREE_VALUE (alias))));
2253}
2254
2255
5e712541 2256/* Weakrefs may be associated to external decls and thus not output
2257 at expansion time. Emit all neccesary aliases. */
2258
5139ff04 2259static void
5e712541 2260output_weakrefs (void)
2261{
2262 struct cgraph_node *node;
2263 struct varpool_node *vnode;
7c455d87 2264 FOR_EACH_FUNCTION (node)
7d0d0ce1 2265 if (node->alias && DECL_EXTERNAL (node->symbol.decl)
2266 && !TREE_ASM_WRITTEN (node->symbol.decl)
2267 && lookup_attribute ("weakref", DECL_ATTRIBUTES (node->symbol.decl)))
2268 assemble_alias (node->symbol.decl,
badeded8 2269 node->thunk.alias ? DECL_ASSEMBLER_NAME (node->thunk.alias)
7d0d0ce1 2270 : get_alias_symbol (node->symbol.decl));
7c455d87 2271 FOR_EACH_VARIABLE (vnode)
7d0d0ce1 2272 if (vnode->alias && DECL_EXTERNAL (vnode->symbol.decl)
2273 && !TREE_ASM_WRITTEN (vnode->symbol.decl)
2274 && lookup_attribute ("weakref", DECL_ATTRIBUTES (vnode->symbol.decl)))
2275 assemble_alias (vnode->symbol.decl,
badeded8 2276 vnode->alias_of ? DECL_ASSEMBLER_NAME (vnode->alias_of)
7d0d0ce1 2277 : get_alias_symbol (vnode->symbol.decl));
5e712541 2278}
2279
34e5cced 2280
34e5cced 2281
121f3051 2282void
2283init_cgraph (void)
2284{
01ec0a6c 2285 if (!cgraph_dump_file)
2286 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
121f3051 2287}
b5d36404 2288
a0c938f0 2289/* The edges representing the callers of the NEW_VERSION node were
b5d36404 2290 fixed by cgraph_function_versioning (), now the call_expr in their
2291 respective tree code should be updated to call the NEW_VERSION. */
2292
2293static void
2294update_call_expr (struct cgraph_node *new_version)
2295{
2296 struct cgraph_edge *e;
2297
2298 gcc_assert (new_version);
75a70cf9 2299
2300 /* Update the call expr on the edges to call the new version. */
b5d36404 2301 for (e = new_version->callers; e; e = e->next_caller)
e03a95e7 2302 {
7d0d0ce1 2303 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->symbol.decl);
2304 gimple_call_set_fndecl (e->call_stmt, new_version->symbol.decl);
e38def9c 2305 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
e03a95e7 2306 }
b5d36404 2307}
2308
2309
2310/* Create a new cgraph node which is the new version of
2311 OLD_VERSION node. REDIRECT_CALLERS holds the callers
2312 edges which should be redirected to point to
2313 NEW_VERSION. ALL the callees edges of OLD_VERSION
2314 are cloned to the new version node. Return the new
b06ab5fa 2315 version node.
2316
2317 If non-NULL BLOCK_TO_COPY determine what basic blocks
2318 was copied to prevent duplications of calls that are dead
2319 in the clone. */
b5d36404 2320
4c0315d0 2321struct cgraph_node *
b5d36404 2322cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
4460a647 2323 tree new_decl,
b06ab5fa 2324 VEC(cgraph_edge_p,heap) *redirect_callers,
2325 bitmap bbs_to_copy)
2326 {
b5d36404 2327 struct cgraph_node *new_version;
32936803 2328 struct cgraph_edge *e;
b5d36404 2329 unsigned i;
2330
2331 gcc_assert (old_version);
a0c938f0 2332
5a90471f 2333 new_version = cgraph_create_node (new_decl);
b5d36404 2334
4c0315d0 2335 new_version->analyzed = old_version->analyzed;
b5d36404 2336 new_version->local = old_version->local;
7d0d0ce1 2337 new_version->symbol.externally_visible = false;
7c455d87 2338 new_version->local.local = old_version->analyzed;
b5d36404 2339 new_version->global = old_version->global;
a93f1c3b 2340 new_version->rtl = old_version->rtl;
b5d36404 2341 new_version->count = old_version->count;
2342
a70a5e2c 2343 for (e = old_version->callees; e; e=e->next_callee)
b06ab5fa 2344 if (!bbs_to_copy
2345 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
2346 cgraph_clone_edge (e, new_version, e->call_stmt,
2347 e->lto_stmt_uid, REG_BR_PROB_BASE,
2348 CGRAPH_FREQ_BASE,
0835ad03 2349 true);
a70a5e2c 2350 for (e = old_version->indirect_calls; e; e=e->next_callee)
b06ab5fa 2351 if (!bbs_to_copy
2352 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
2353 cgraph_clone_edge (e, new_version, e->call_stmt,
2354 e->lto_stmt_uid, REG_BR_PROB_BASE,
2355 CGRAPH_FREQ_BASE,
0835ad03 2356 true);
48148244 2357 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
4460a647 2358 {
2359 /* Redirect calls to the old version node to point to its new
2360 version. */
2361 cgraph_redirect_edge_callee (e, new_version);
2362 }
b5d36404 2363
ad687a96 2364 cgraph_call_node_duplication_hooks (old_version, new_version);
2365
b5d36404 2366 return new_version;
2367 }
2368
2369 /* Perform function versioning.
a0c938f0 2370 Function versioning includes copying of the tree and
b5d36404 2371 a callgraph update (creating a new cgraph node and updating
2372 its callees and callers).
2373
2374 REDIRECT_CALLERS varray includes the edges to be redirected
2375 to the new version.
2376
2377 TREE_MAP is a mapping of tree nodes we want to replace with
2378 new ones (according to results of prior analysis).
2379 OLD_VERSION_NODE is the node that is versioned.
7a3ec978 2380
b06ab5fa 2381 If non-NULL ARGS_TO_SKIP determine function parameters to remove
2382 from new version.
7a3ec978 2383 If SKIP_RETURN is true, the new version will return void.
b06ab5fa 2384 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
7a3ec978 2385 If non_NULL NEW_ENTRY determine new entry BB of the clone.
2386
2387 Return the new version's cgraph node. */
b5d36404 2388
2389struct cgraph_node *
2390cgraph_function_versioning (struct cgraph_node *old_version_node,
4460a647 2391 VEC(cgraph_edge_p,heap) *redirect_callers,
ccf4ab6b 2392 VEC (ipa_replace_map_p,gc)* tree_map,
a70a5e2c 2393 bitmap args_to_skip,
7a3ec978 2394 bool skip_return,
b06ab5fa 2395 bitmap bbs_to_copy,
2396 basic_block new_entry_block,
a70a5e2c 2397 const char *clone_name)
b5d36404 2398{
7d0d0ce1 2399 tree old_decl = old_version_node->symbol.decl;
b5d36404 2400 struct cgraph_node *new_version_node = NULL;
2401 tree new_decl;
2402
2403 if (!tree_versionable_function_p (old_decl))
2404 return NULL;
2405
3c97c75d 2406 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
2407
7a3ec978 2408 /* Make a new FUNCTION_DECL tree node for the new version. */
2409 if (!args_to_skip && !skip_return)
5afe38fe 2410 new_decl = copy_node (old_decl);
2411 else
7a3ec978 2412 new_decl
2413 = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
b5d36404 2414
df0b8dfb 2415 /* Generate a new name for the new version. */
2416 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
2417 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2418 SET_DECL_RTL (new_decl, NULL);
2419
e54aa8a4 2420 /* When the old decl was a con-/destructor make sure the clone isn't. */
2421 DECL_STATIC_CONSTRUCTOR(new_decl) = 0;
2422 DECL_STATIC_DESTRUCTOR(new_decl) = 0;
2423
b5d36404 2424 /* Create the new version's call-graph node.
2425 and update the edges of the new node. */
2426 new_version_node =
2427 cgraph_copy_node_for_versioning (old_version_node, new_decl,
b06ab5fa 2428 redirect_callers, bbs_to_copy);
b5d36404 2429
2430 /* Copy the OLD_VERSION_NODE function tree to the new version. */
b06ab5fa 2431 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
7a3ec978 2432 skip_return, bbs_to_copy, new_entry_block);
b5d36404 2433
a0c938f0 2434 /* Update the new version's properties.
e03a95e7 2435 Make The new version visible only within this translation unit. Make sure
2436 that is not weak also.
a0c938f0 2437 ??? We cannot use COMDAT linkage because there is no
b5d36404 2438 ABI support for this. */
7d0d0ce1 2439 cgraph_make_decl_local (new_version_node->symbol.decl);
2440 DECL_VIRTUAL_P (new_version_node->symbol.decl) = 0;
2441 new_version_node->symbol.externally_visible = 0;
b5d36404 2442 new_version_node->local.local = 1;
2443 new_version_node->lowered = true;
f014e39d 2444
e03a95e7 2445 /* Update the call_expr on the edges to call the new version node. */
2446 update_call_expr (new_version_node);
48e1416a 2447
50828ed8 2448 cgraph_call_function_insertion_hooks (new_version_node);
b5d36404 2449 return new_version_node;
2450}
469679ab 2451
ccf4ab6b 2452/* Given virtual clone, turn it into actual clone. */
2453static void
2454cgraph_materialize_clone (struct cgraph_node *node)
2455{
2456 bitmap_obstack_initialize (NULL);
7d0d0ce1 2457 node->former_clone_of = node->clone_of->symbol.decl;
e748b31d 2458 if (node->clone_of->former_clone_of)
2459 node->former_clone_of = node->clone_of->former_clone_of;
ccf4ab6b 2460 /* Copy the OLD_VERSION_NODE function tree to the new version. */
7d0d0ce1 2461 tree_function_versioning (node->clone_of->symbol.decl, node->symbol.decl,
ccf4ab6b 2462 node->clone.tree_map, true,
7a3ec978 2463 node->clone.args_to_skip, false,
2464 NULL, NULL);
e20422ea 2465 if (cgraph_dump_file)
2466 {
7d0d0ce1 2467 dump_function_to_file (node->clone_of->symbol.decl, cgraph_dump_file, dump_flags);
2468 dump_function_to_file (node->symbol.decl, cgraph_dump_file, dump_flags);
e20422ea 2469 }
ccf4ab6b 2470
2471 /* Function is no longer clone. */
2472 if (node->next_sibling_clone)
2473 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
2474 if (node->prev_sibling_clone)
2475 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
2476 else
2477 node->clone_of->clones = node->next_sibling_clone;
2478 node->next_sibling_clone = NULL;
2479 node->prev_sibling_clone = NULL;
6d1cc52c 2480 if (!node->clone_of->analyzed && !node->clone_of->clones)
7d6a1ec8 2481 {
2482 cgraph_release_function_body (node->clone_of);
2483 cgraph_node_remove_callees (node->clone_of);
7d0d0ce1 2484 ipa_remove_all_references (&node->clone_of->symbol.ref_list);
7d6a1ec8 2485 }
ccf4ab6b 2486 node->clone_of = NULL;
2487 bitmap_obstack_release (NULL);
2488}
2489
c596d830 2490/* If necessary, change the function declaration in the call statement
2491 associated with E so that it corresponds to the edge callee. */
2492
2493gimple
2494cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
2495{
2496 tree decl = gimple_call_fndecl (e->call_stmt);
2497 gimple new_stmt;
3fd0ca33 2498 gimple_stmt_iterator gsi;
1f449108 2499#ifdef ENABLE_CHECKING
2500 struct cgraph_node *node;
2501#endif
c596d830 2502
1caef38b 2503 if (e->indirect_unknown_callee
7d0d0ce1 2504 || decl == e->callee->symbol.decl)
c596d830 2505 return e->call_stmt;
2506
1f449108 2507#ifdef ENABLE_CHECKING
1caef38b 2508 if (decl)
2509 {
2510 node = cgraph_get_node (decl);
2511 gcc_assert (!node || !node->clone.combined_args_to_skip);
2512 }
1f449108 2513#endif
e748b31d 2514
c596d830 2515 if (cgraph_dump_file)
2516 {
2517 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
2518 cgraph_node_name (e->caller), e->caller->uid,
2519 cgraph_node_name (e->callee), e->callee->uid);
2520 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
e748b31d 2521 if (e->callee->clone.combined_args_to_skip)
91aba934 2522 {
2523 fprintf (cgraph_dump_file, " combined args to skip: ");
2524 dump_bitmap (cgraph_dump_file,
2525 e->callee->clone.combined_args_to_skip);
e748b31d 2526 }
c596d830 2527 }
2528
2529 if (e->callee->clone.combined_args_to_skip)
91aba934 2530 {
092cd838 2531 int lp_nr;
91aba934 2532
2533 new_stmt
2534 = gimple_call_copy_skip_args (e->call_stmt,
2535 e->callee->clone.combined_args_to_skip);
7d0d0ce1 2536 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
91aba934 2537
2538 if (gimple_vdef (new_stmt)
2539 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
2540 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
2541
d4e80e2b 2542 gsi = gsi_for_stmt (e->call_stmt);
9126b675 2543 gsi_replace (&gsi, new_stmt, false);
092cd838 2544 /* We need to defer cleaning EH info on the new statement to
2545 fixup-cfg. We may not have dominator information at this point
2546 and thus would end up with unreachable blocks and have no way
2547 to communicate that we need to run CFG cleanup then. */
2548 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
2549 if (lp_nr != 0)
2550 {
2551 remove_stmt_from_eh_lp (e->call_stmt);
2552 add_stmt_to_eh_lp (new_stmt, lp_nr);
2553 }
91aba934 2554 }
c596d830 2555 else
75c7f5a5 2556 {
2557 new_stmt = e->call_stmt;
7d0d0ce1 2558 gimple_call_set_fndecl (new_stmt, e->callee->symbol.decl);
75c7f5a5 2559 update_stmt (new_stmt);
2560 }
c596d830 2561
c596d830 2562 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
2563
2564 if (cgraph_dump_file)
2565 {
2566 fprintf (cgraph_dump_file, " updated to:");
2567 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
2568 }
2569 return new_stmt;
2570}
2571
ccf4ab6b 2572/* Once all functions from compilation unit are in memory, produce all clones
c596d830 2573 and update all calls. We might also do this on demand if we don't want to
2574 bring all functions to memory prior compilation, but current WHOPR
2575 implementation does that and it is is bit easier to keep everything right in
2576 this order. */
d2bb3f9d 2577static void
ccf4ab6b 2578cgraph_materialize_all_clones (void)
2579{
2580 struct cgraph_node *node;
2581 bool stabilized = false;
2582
2583 if (cgraph_dump_file)
2584 fprintf (cgraph_dump_file, "Materializing clones\n");
2585#ifdef ENABLE_CHECKING
2586 verify_cgraph ();
2587#endif
2588
2589 /* We can also do topological order, but number of iterations should be
2590 bounded by number of IPA passes since single IPA pass is probably not
2591 going to create clones of clones it created itself. */
2592 while (!stabilized)
2593 {
2594 stabilized = true;
7c455d87 2595 FOR_EACH_FUNCTION (node)
ccf4ab6b 2596 {
7d0d0ce1 2597 if (node->clone_of && node->symbol.decl != node->clone_of->symbol.decl
2598 && !gimple_has_body_p (node->symbol.decl))
ccf4ab6b 2599 {
7d0d0ce1 2600 if (gimple_has_body_p (node->clone_of->symbol.decl))
ccf4ab6b 2601 {
2602 if (cgraph_dump_file)
e20422ea 2603 {
0a10fd82 2604 fprintf (cgraph_dump_file, "cloning %s to %s\n",
e20422ea 2605 cgraph_node_name (node->clone_of),
2606 cgraph_node_name (node));
2607 if (node->clone.tree_map)
2608 {
2609 unsigned int i;
2610 fprintf (cgraph_dump_file, " replace map: ");
2611 for (i = 0; i < VEC_length (ipa_replace_map_p,
2612 node->clone.tree_map);
2613 i++)
2614 {
2615 struct ipa_replace_map *replace_info;
2616 replace_info = VEC_index (ipa_replace_map_p,
2617 node->clone.tree_map,
2618 i);
2619 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
2620 fprintf (cgraph_dump_file, " -> ");
2621 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
2622 fprintf (cgraph_dump_file, "%s%s;",
2623 replace_info->replace_p ? "(replace)":"",
2624 replace_info->ref_p ? "(ref)":"");
2625 }
2626 fprintf (cgraph_dump_file, "\n");
2627 }
2628 if (node->clone.args_to_skip)
2629 {
2630 fprintf (cgraph_dump_file, " args_to_skip: ");
2631 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
2632 }
2633 if (node->clone.args_to_skip)
2634 {
2635 fprintf (cgraph_dump_file, " combined_args_to_skip:");
2636 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
2637 }
2638 }
ccf4ab6b 2639 cgraph_materialize_clone (node);
a510bd8d 2640 stabilized = false;
ccf4ab6b 2641 }
ccf4ab6b 2642 }
2643 }
2644 }
7c455d87 2645 FOR_EACH_FUNCTION (node)
ee3f5fc0 2646 if (!node->analyzed && node->callees)
2647 cgraph_node_remove_callees (node);
c596d830 2648 if (cgraph_dump_file)
2649 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
947781ac 2650#ifdef ENABLE_CHECKING
2651 verify_cgraph ();
2652#endif
ccf4ab6b 2653 cgraph_remove_unreachable_nodes (false, cgraph_dump_file);
2654}
2655
d2bb3f9d 2656
2657/* Perform simple optimizations based on callgraph. */
2658
2659void
2660cgraph_optimize (void)
2661{
2662 if (seen_error ())
2663 return;
2664
2665#ifdef ENABLE_CHECKING
3e7775f6 2666 verify_symtab ();
d2bb3f9d 2667#endif
2668
d2bb3f9d 2669 timevar_push (TV_CGRAPHOPT);
2670 if (pre_ipa_mem_report)
2671 {
2672 fprintf (stderr, "Memory consumption before IPA\n");
2673 dump_memory_report (false);
2674 }
2675 if (!quiet_flag)
2676 fprintf (stderr, "Performing interprocedural optimizations\n");
2677 cgraph_state = CGRAPH_STATE_IPA;
2678
2679 /* Don't run the IPA passes if there was any error or sorry messages. */
2680 if (!seen_error ())
2681 ipa_passes ();
2682
2683 /* Do nothing else if any IPA pass found errors or if we are just streaming LTO. */
2684 if (seen_error ()
2685 || (!in_lto_p && flag_lto && !flag_fat_lto_objects))
2686 {
2687 timevar_pop (TV_CGRAPHOPT);
2688 return;
2689 }
2690
2691 /* This pass remove bodies of extern inline functions we never inlined.
2692 Do this later so other IPA passes see what is really going on. */
2693 cgraph_remove_unreachable_nodes (false, dump_file);
2694 cgraph_global_info_ready = true;
2695 if (cgraph_dump_file)
2696 {
2697 fprintf (cgraph_dump_file, "Optimized ");
18841b0c 2698 dump_symtab (cgraph_dump_file);
d2bb3f9d 2699 }
2700 if (post_ipa_mem_report)
2701 {
2702 fprintf (stderr, "Memory consumption after IPA\n");
2703 dump_memory_report (false);
2704 }
2705 timevar_pop (TV_CGRAPHOPT);
2706
2707 /* Output everything. */
2708 (*debug_hooks->assembly_start) ();
2709 if (!quiet_flag)
2710 fprintf (stderr, "Assembling functions:\n");
2711#ifdef ENABLE_CHECKING
3e7775f6 2712 verify_symtab ();
d2bb3f9d 2713#endif
2714
2715 cgraph_materialize_all_clones ();
2716 bitmap_obstack_initialize (NULL);
2717 execute_ipa_pass_list (all_late_ipa_passes);
2718 cgraph_remove_unreachable_nodes (true, dump_file);
2719#ifdef ENABLE_CHECKING
3e7775f6 2720 verify_symtab ();
d2bb3f9d 2721#endif
2722 bitmap_obstack_release (NULL);
2723 cgraph_mark_functions_to_output ();
2724 output_weakrefs ();
2725
2726 cgraph_state = CGRAPH_STATE_EXPANSION;
2727 if (!flag_toplevel_reorder)
2728 cgraph_output_in_order ();
2729 else
2730 {
2731 cgraph_output_pending_asms ();
2732
2733 cgraph_expand_all_functions ();
2734 varpool_remove_unreferenced_decls ();
2735
2736 varpool_assemble_pending_decls ();
2737 }
2738
2739 cgraph_process_new_functions ();
2740 cgraph_state = CGRAPH_STATE_FINISHED;
2741
2742 if (cgraph_dump_file)
2743 {
2744 fprintf (cgraph_dump_file, "\nFinal ");
18841b0c 2745 dump_symtab (cgraph_dump_file);
d2bb3f9d 2746 }
2747#ifdef ENABLE_CHECKING
3e7775f6 2748 verify_symtab ();
d2bb3f9d 2749 /* Double check that all inline clones are gone and that all
2750 function bodies have been released from memory. */
2751 if (!seen_error ())
2752 {
2753 struct cgraph_node *node;
2754 bool error_found = false;
2755
7c455d87 2756 FOR_EACH_DEFINED_FUNCTION (node)
2757 if (node->global.inlined_to
2758 || gimple_has_body_p (node->symbol.decl))
d2bb3f9d 2759 {
2760 error_found = true;
2761 dump_cgraph_node (stderr, node);
2762 }
2763 if (error_found)
2764 internal_error ("nodes with unreleased memory found");
2765 }
2766#endif
2767}
2768
2769
2770/* Analyze the whole compilation unit once it is parsed completely. */
2771
2772void
2773cgraph_finalize_compilation_unit (void)
2774{
2775 timevar_push (TV_CGRAPH);
2776
2777 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
2778 if (flag_lto)
2779 lto_streamer_hooks_init ();
2780
2781 /* If we're here there's no current function anymore. Some frontends
2782 are lazy in clearing these. */
2783 current_function_decl = NULL;
2784 set_cfun (NULL);
2785
2786 /* Do not skip analyzing the functions if there were errors, we
2787 miss diagnostics for following functions otherwise. */
2788
2789 /* Emit size functions we didn't inline. */
2790 finalize_size_functions ();
2791
2792 /* Mark alias targets necessary and emit diagnostics. */
2793 finish_aliases_1 ();
2794 handle_alias_pairs ();
2795
2796 if (!quiet_flag)
2797 {
2798 fprintf (stderr, "\nAnalyzing compilation unit\n");
2799 fflush (stderr);
2800 }
2801
2802 if (flag_dump_passes)
2803 dump_passes ();
2804
2805 /* Gimplify and lower all functions, compute reachability and
2806 remove unreachable nodes. */
2807 cgraph_analyze_functions ();
2808
2809 /* Mark alias targets necessary and emit diagnostics. */
2810 finish_aliases_1 ();
2811 handle_alias_pairs ();
2812
2813 /* Gimplify and lower thunks. */
2814 cgraph_analyze_functions ();
2815
2816 /* Finally drive the pass manager. */
2817 cgraph_optimize ();
2818
2819 timevar_pop (TV_CGRAPH);
2820}
2821
2822
a861fe52 2823#include "gt-cgraphunit.h"