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