]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cgraphunit.c
2009-06-12 Steven G. Kargl <kargls@comcast.net>
[thirdparty/gcc.git] / gcc / cgraphunit.c
CommitLineData
cd6bca02 1/* Callgraph based interprocedural optimizations.
cfaf579d 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
e3a37aef 3 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
6329636b 48 In the the call-graph construction and local function
b0cdf642 49 analysis takes place here. Bodies of unreachable functions are released
50 to conserve memory usage.
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"
f79b6507 126#include "timevar.h"
d7c6d889 127#include "params.h"
128#include "fibheap.h"
611e5405 129#include "intl.h"
b69eb0ff 130#include "function.h"
b5d36404 131#include "ipa-prop.h"
75a70cf9 132#include "gimple.h"
133#include "tree-iterator.h"
f1e2a033 134#include "tree-pass.h"
c1dcd13c 135#include "output.h"
9ed5b1f5 136#include "coverage.h"
d7c6d889 137
a6868229 138static void cgraph_expand_all_functions (void);
d9d9733a 139static void cgraph_mark_functions_to_output (void);
140static void cgraph_expand_function (struct cgraph_node *);
f788fff2 141static void cgraph_output_pending_asms (void);
25bb88de 142
121f3051 143static FILE *cgraph_dump_file;
144
2c56f72e 145/* A vector of FUNCTION_DECLs declared as static constructors. */
146static GTY (()) VEC(tree, gc) *static_ctors;
147/* A vector of FUNCTION_DECLs declared as static destructors. */
148static GTY (()) VEC(tree, gc) *static_dtors;
a861fe52 149
150/* When target does not have ctors and dtors, we call all constructor
310d2511 151 and destructor by special initialization/destruction function
a861fe52 152 recognized by collect2.
153
154 When we are going to build this function, collect all constructors and
155 destructors and turn them into normal functions. */
156
157static void
158record_cdtor_fn (tree fndecl)
159{
2de29097 160 struct cgraph_node *node;
161 if (targetm.have_ctors_dtors
162 || (!DECL_STATIC_CONSTRUCTOR (fndecl)
163 && !DECL_STATIC_DESTRUCTOR (fndecl)))
a861fe52 164 return;
165
166 if (DECL_STATIC_CONSTRUCTOR (fndecl))
167 {
2c56f72e 168 VEC_safe_push (tree, gc, static_ctors, fndecl);
a861fe52 169 DECL_STATIC_CONSTRUCTOR (fndecl) = 0;
a861fe52 170 }
171 if (DECL_STATIC_DESTRUCTOR (fndecl))
172 {
2c56f72e 173 VEC_safe_push (tree, gc, static_dtors, fndecl);
a861fe52 174 DECL_STATIC_DESTRUCTOR (fndecl) = 0;
a861fe52 175 }
2de29097 176 node = cgraph_node (fndecl);
177 node->local.disregard_inline_limits = 1;
178 cgraph_mark_reachable_node (node);
a861fe52 179}
180
2c56f72e 181/* Define global constructors/destructor functions for the CDTORS, of
182 which they are LEN. The CDTORS are sorted by initialization
183 priority. If CTOR_P is true, these are constructors; otherwise,
184 they are destructors. */
185
a861fe52 186static void
2c56f72e 187build_cdtor (bool ctor_p, tree *cdtors, size_t len)
a861fe52 188{
2c56f72e 189 size_t i;
a861fe52 190
2c56f72e 191 i = 0;
192 while (i < len)
193 {
194 tree body;
195 tree fn;
196 priority_type priority;
197
198 priority = 0;
199 body = NULL_TREE;
200 /* Find the next batch of constructors/destructors with the same
201 initialization priority. */
202 do
203 {
204 priority_type p;
205 fn = cdtors[i];
206 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
207 if (!body)
208 priority = p;
209 else if (p != priority)
210 break;
211 append_to_statement_list (build_function_call_expr (fn, 0),
212 &body);
213 ++i;
214 }
215 while (i < len);
216 gcc_assert (body != NULL_TREE);
217 /* Generate a function to call all the function of like
218 priority. */
219 cgraph_build_static_cdtor (ctor_p ? 'I' : 'D', body, priority);
220 }
221}
222
223/* Comparison function for qsort. P1 and P2 are actually of type
224 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
225 used to determine the sort order. */
a861fe52 226
2c56f72e 227static int
228compare_ctor (const void *p1, const void *p2)
229{
230 tree f1;
231 tree f2;
232 int priority1;
233 int priority2;
234
235 f1 = *(const tree *)p1;
236 f2 = *(const tree *)p2;
237 priority1 = DECL_INIT_PRIORITY (f1);
238 priority2 = DECL_INIT_PRIORITY (f2);
239
240 if (priority1 < priority2)
241 return -1;
242 else if (priority1 > priority2)
243 return 1;
244 else
245 /* Ensure a stable sort. */
246 return (const tree *)p1 - (const tree *)p2;
247}
248
249/* Comparison function for qsort. P1 and P2 are actually of type
250 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
251 used to determine the sort order. */
a861fe52 252
2c56f72e 253static int
254compare_dtor (const void *p1, const void *p2)
255{
256 tree f1;
257 tree f2;
258 int priority1;
259 int priority2;
260
261 f1 = *(const tree *)p1;
262 f2 = *(const tree *)p2;
263 priority1 = DECL_FINI_PRIORITY (f1);
264 priority2 = DECL_FINI_PRIORITY (f2);
265
266 if (priority1 < priority2)
267 return -1;
268 else if (priority1 > priority2)
269 return 1;
270 else
271 /* Ensure a stable sort. */
272 return (const tree *)p1 - (const tree *)p2;
a861fe52 273}
274
275/* Generate functions to call static constructors and destructors
276 for targets that do not support .ctors/.dtors sections. These
277 functions have magic names which are detected by collect2. */
278
279static void
280cgraph_build_cdtor_fns (void)
281{
2c56f72e 282 if (!VEC_empty (tree, static_ctors))
a861fe52 283 {
2c56f72e 284 gcc_assert (!targetm.have_ctors_dtors);
285 qsort (VEC_address (tree, static_ctors),
286 VEC_length (tree, static_ctors),
287 sizeof (tree),
288 compare_ctor);
289 build_cdtor (/*ctor_p=*/true,
290 VEC_address (tree, static_ctors),
291 VEC_length (tree, static_ctors));
292 VEC_truncate (tree, static_ctors, 0);
a861fe52 293 }
2c56f72e 294
295 if (!VEC_empty (tree, static_dtors))
a861fe52 296 {
2c56f72e 297 gcc_assert (!targetm.have_ctors_dtors);
298 qsort (VEC_address (tree, static_dtors),
299 VEC_length (tree, static_dtors),
300 sizeof (tree),
301 compare_dtor);
302 build_cdtor (/*ctor_p=*/false,
303 VEC_address (tree, static_dtors),
304 VEC_length (tree, static_dtors));
305 VEC_truncate (tree, static_dtors, 0);
a861fe52 306 }
307}
308
2c0b522d 309/* Determine if function DECL is needed. That is, visible to something
310 either outside this translation unit, something magic in the system
6329636b 311 configury. */
2c0b522d 312
313static bool
314decide_is_function_needed (struct cgraph_node *node, tree decl)
315{
62eec3b4 316 if (MAIN_NAME_P (DECL_NAME (decl))
317 && TREE_PUBLIC (decl))
318 {
319 node->local.externally_visible = true;
320 return true;
321 }
4ee9c684 322
3f82b628 323 /* If the user told us it is used, then it must be so. */
05806473 324 if (node->local.externally_visible)
325 return true;
326
3f82b628 327 /* ??? If the assembler name is set by hand, it is possible to assemble
328 the name later after finalizing the function and the fact is noticed
329 in assemble_name then. This is arguably a bug. */
330 if (DECL_ASSEMBLER_NAME_SET_P (decl)
331 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
332 return true;
333
55680bef 334 /* With -fkeep-inline-functions we are keeping all inline functions except
335 for extern inline ones. */
336 if (flag_keep_inline_functions
337 && DECL_DECLARED_INLINE_P (decl)
316ef6d8 338 && !DECL_EXTERNAL (decl)
339 && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)))
55680bef 340 return true;
341
2c0b522d 342 /* If we decided it was needed before, but at the time we didn't have
343 the body of the function available, then it's still needed. We have
344 to go back and re-check its dependencies now. */
345 if (node->needed)
346 return true;
347
348 /* Externally visible functions must be output. The exception is
a0c938f0 349 COMDAT functions that must be output only when they are needed.
8baa9d15 350
351 When not optimizing, also output the static functions. (see
95da6220 352 PR24561), but don't do so for always_inline functions, functions
d3d410e1 353 declared inline and nested functions. These was optimized out
354 in the original implementation and it is unclear whether we want
554f2707 355 to change the behavior here. */
bba7ddf8 356 if (((TREE_PUBLIC (decl)
d3d410e1 357 || (!optimize && !node->local.disregard_inline_limits
358 && !DECL_DECLARED_INLINE_P (decl)
359 && !node->origin))
bba7ddf8 360 && !flag_whole_program)
62eec3b4 361 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
2c0b522d 362 return true;
363
364 /* Constructors and destructors are reachable from the runtime by
365 some mechanism. */
366 if (DECL_STATIC_CONSTRUCTOR (decl) || DECL_STATIC_DESTRUCTOR (decl))
367 return true;
368
2c0b522d 369 return false;
370}
371
bdc40eb8 372/* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
523c1122 373 functions into callgraph in a way so they look like ordinary reachable
374 functions inserted into callgraph already at construction time. */
375
376bool
377cgraph_process_new_functions (void)
378{
379 bool output = false;
380 tree fndecl;
381 struct cgraph_node *node;
382
383 /* Note that this queue may grow as its being processed, as the new
384 functions may generate new ones. */
385 while (cgraph_new_nodes)
386 {
387 node = cgraph_new_nodes;
388 fndecl = node->decl;
389 cgraph_new_nodes = cgraph_new_nodes->next_needed;
390 switch (cgraph_state)
391 {
392 case CGRAPH_STATE_CONSTRUCTION:
393 /* At construction time we just need to finalize function and move
394 it into reachable functions list. */
395
396 node->next_needed = NULL;
397 cgraph_finalize_function (fndecl, false);
398 cgraph_mark_reachable_node (node);
399 output = true;
400 break;
401
402 case CGRAPH_STATE_IPA:
f517b36e 403 case CGRAPH_STATE_IPA_SSA:
523c1122 404 /* When IPA optimization already started, do all essential
405 transformations that has been already performed on the whole
406 cgraph but not on this function. */
407
75a70cf9 408 gimple_register_cfg_hooks ();
523c1122 409 if (!node->analyzed)
410 cgraph_analyze_function (node);
411 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
412 current_function_decl = fndecl;
9c1bff7a 413 compute_inline_parameters (node);
f517b36e 414 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
415 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
416 /* When not optimizing, be sure we run early local passes anyway
417 to expand OMP. */
418 || !optimize)
20099e35 419 execute_pass_list (pass_early_local_passes.pass.sub);
523c1122 420 free_dominance_info (CDI_POST_DOMINATORS);
421 free_dominance_info (CDI_DOMINATORS);
422 pop_cfun ();
423 current_function_decl = NULL;
424 break;
425
426 case CGRAPH_STATE_EXPANSION:
427 /* Functions created during expansion shall be compiled
428 directly. */
09fc9532 429 node->process = 0;
523c1122 430 cgraph_expand_function (node);
431 break;
432
433 default:
434 gcc_unreachable ();
435 break;
436 }
50828ed8 437 cgraph_call_function_insertion_hooks (node);
523c1122 438 }
439 return output;
440}
441
9b8fb23a 442/* As an GCC extension we allow redefinition of the function. The
443 semantics when both copies of bodies differ is not well defined.
444 We replace the old body with new body so in unit at a time mode
445 we always use new body, while in normal mode we may end up with
446 old body inlined into some functions and new body expanded and
447 inlined in others.
448
449 ??? It may make more sense to use one body for inlining and other
450 body for expanding the function but this is difficult to do. */
451
452static void
453cgraph_reset_node (struct cgraph_node *node)
454{
09fc9532 455 /* If node->process is set, then we have already begun whole-unit analysis.
6329636b 456 This is *not* testing for whether we've already emitted the function.
457 That case can be sort-of legitimately seen with real function redefinition
458 errors. I would argue that the front end should never present us with
459 such a case, but don't enforce that for now. */
09fc9532 460 gcc_assert (!node->process);
9b8fb23a 461
462 /* Reset our data structures so we can analyze the function again. */
463 memset (&node->local, 0, sizeof (node->local));
464 memset (&node->global, 0, sizeof (node->global));
465 memset (&node->rtl, 0, sizeof (node->rtl));
466 node->analyzed = false;
467 node->local.redefined_extern_inline = true;
468 node->local.finalized = false;
469
9b8fb23a 470 cgraph_node_remove_callees (node);
471
472 /* We may need to re-queue the node for assembling in case
46beef9a 473 we already proceeded it and ignored as not needed or got
474 a re-declaration in IMA mode. */
475 if (node->reachable)
9b8fb23a 476 {
477 struct cgraph_node *n;
478
479 for (n = cgraph_nodes_queue; n; n = n->next_needed)
480 if (n == node)
481 break;
482 if (!n)
483 node->reachable = 0;
484 }
485}
c08871a9 486
1e8e9920 487static void
488cgraph_lower_function (struct cgraph_node *node)
489{
490 if (node->lowered)
491 return;
492 tree_lowering_passes (node->decl);
493 node->lowered = true;
494}
495
28df663b 496/* DECL has been parsed. Take it, queue it, compile it at the whim of the
497 logic in effect. If NESTED is true, then our caller cannot stand to have
498 the garbage collector run at the moment. We would need to either create
499 a new GC context, or just not compile right now. */
ae01b312 500
501void
28df663b 502cgraph_finalize_function (tree decl, bool nested)
ae01b312 503{
504 struct cgraph_node *node = cgraph_node (decl);
505
c08871a9 506 if (node->local.finalized)
9b8fb23a 507 cgraph_reset_node (node);
28df663b 508
167b550b 509 node->pid = cgraph_max_pid ++;
c08871a9 510 notice_global_symbol (decl);
79bb87b4 511 node->local.finalized = true;
e27482aa 512 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
22c748a1 513 node->finalized_by_frontend = true;
a861fe52 514 record_cdtor_fn (node->decl);
9d95b2b0 515 if (node->nested)
516 lower_nested_functions (decl);
517 gcc_assert (!node->nested);
ae01b312 518
2c0b522d 519 if (decide_is_function_needed (node, decl))
520 cgraph_mark_needed_node (node);
521
ecda6e51 522 /* Since we reclaim unreachable nodes at the end of every language
3f82b628 523 level unit, we need to be conservative about possible entry points
524 there. */
62eec3b4 525 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl)))
3f82b628 526 cgraph_mark_reachable_node (node);
527
2c0b522d 528 /* If we've not yet emitted decl, tell the debug info about it. */
28df663b 529 if (!TREE_ASM_WRITTEN (decl))
2c0b522d 530 (*debug_hooks->deferred_inline_function) (decl);
4e8871a0 531
b69eb0ff 532 /* Possibly warn about unused parameters. */
533 if (warn_unused_parameter)
534 do_warn_unused_parameter (decl);
6329636b 535
536 if (!nested)
537 ggc_collect ();
ae01b312 538}
539
0da03d11 540/* C99 extern inline keywords allow changing of declaration after function
541 has been finalized. We need to re-decide if we want to mark the function as
542 needed then. */
543
544void
545cgraph_mark_if_needed (tree decl)
546{
547 struct cgraph_node *node = cgraph_node (decl);
548 if (node->local.finalized && decide_is_function_needed (node, decl))
549 cgraph_mark_needed_node (node);
550}
551
ccf4ab6b 552/* Return TRUE if NODE2 is equivalent to NODE or its clone. */
553static bool
554clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
555{
556 while (node != node2 && node2)
557 node2 = node2->clone_of;
558 return node2 != NULL;
559}
560
b0cdf642 561/* Verify cgraph nodes of given cgraph node. */
562void
563verify_cgraph_node (struct cgraph_node *node)
564{
565 struct cgraph_edge *e;
e27482aa 566 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
117ef3d7 567 struct function *saved_cfun = cfun;
e27482aa 568 basic_block this_block;
75a70cf9 569 gimple_stmt_iterator gsi;
9bfec7c2 570 bool error_found = false;
b0cdf642 571
bd09cd3e 572 if (errorcount || sorrycount)
573 return;
574
b0cdf642 575 timevar_push (TV_CGRAPH_VERIFY);
117ef3d7 576 /* debug_generic_stmt needs correct cfun */
577 set_cfun (this_cfun);
b0cdf642 578 for (e = node->callees; e; e = e->next_callee)
579 if (e->aux)
580 {
0a81f5a0 581 error ("aux field set for edge %s->%s",
abd3e6b5 582 identifier_to_locale (cgraph_node_name (e->caller)),
583 identifier_to_locale (cgraph_node_name (e->callee)));
b0cdf642 584 error_found = true;
585 }
a2cb9b3b 586 if (node->count < 0)
587 {
588 error ("Execution count is negative");
589 error_found = true;
590 }
b0cdf642 591 for (e = node->callers; e; e = e->next_caller)
592 {
a2cb9b3b 593 if (e->count < 0)
594 {
595 error ("caller edge count is negative");
596 error_found = true;
597 }
4ae20857 598 if (e->frequency < 0)
599 {
600 error ("caller edge frequency is negative");
601 error_found = true;
602 }
603 if (e->frequency > CGRAPH_FREQ_MAX)
604 {
605 error ("caller edge frequency is too large");
606 error_found = true;
607 }
b0cdf642 608 if (!e->inline_failed)
609 {
610 if (node->global.inlined_to
611 != (e->caller->global.inlined_to
612 ? e->caller->global.inlined_to : e->caller))
613 {
0a81f5a0 614 error ("inlined_to pointer is wrong");
b0cdf642 615 error_found = true;
616 }
617 if (node->callers->next_caller)
618 {
0a81f5a0 619 error ("multiple inline callers");
b0cdf642 620 error_found = true;
621 }
622 }
623 else
624 if (node->global.inlined_to)
625 {
0a81f5a0 626 error ("inlined_to pointer set for noninline callers");
b0cdf642 627 error_found = true;
628 }
629 }
630 if (!node->callers && node->global.inlined_to)
631 {
5cd75817 632 error ("inlined_to pointer is set but no predecessors found");
b0cdf642 633 error_found = true;
634 }
635 if (node->global.inlined_to == node)
636 {
0a81f5a0 637 error ("inlined_to pointer refers to itself");
b0cdf642 638 error_found = true;
639 }
640
0f6439b9 641 if (!cgraph_node (node->decl))
b0cdf642 642 {
0f6439b9 643 error ("node not found in cgraph_hash");
b0cdf642 644 error_found = true;
645 }
a0c938f0 646
ccf4ab6b 647 if (node->clone_of)
648 {
649 struct cgraph_node *n;
650 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
651 if (n == node)
652 break;
653 if (!n)
654 {
655 error ("node has wrong clone_of");
656 error_found = true;
657 }
658 }
659 if (node->clones)
660 {
661 struct cgraph_node *n;
662 for (n = node->clones; n; n = n->next_sibling_clone)
663 if (n->clone_of != node)
664 break;
665 if (n)
666 {
667 error ("node has wrong clone list");
668 error_found = true;
669 }
670 }
671 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
672 {
673 error ("node is in clone list but it is not clone");
674 error_found = true;
675 }
676 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
677 {
678 error ("node has wrong prev_clone pointer");
679 error_found = true;
680 }
681 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
682 {
683 error ("double linked list of clones corrupted");
684 error_found = true;
685 }
686
687 if (node->analyzed && gimple_has_body_p (node->decl)
75a70cf9 688 && !TREE_ASM_WRITTEN (node->decl)
b0cdf642 689 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to))
690 {
e27482aa 691 if (this_cfun->cfg)
692 {
693 /* The nodes we're interested in are never shared, so walk
694 the tree ignoring duplicates. */
e7c352d1 695 struct pointer_set_t *visited_nodes = pointer_set_create ();
e27482aa 696 /* Reach the trees by walking over the CFG, and note the
697 enclosing basic-blocks in the call edges. */
698 FOR_EACH_BB_FN (this_block, this_cfun)
75a70cf9 699 for (gsi = gsi_start_bb (this_block);
700 !gsi_end_p (gsi);
701 gsi_next (&gsi))
9bfec7c2 702 {
75a70cf9 703 gimple stmt = gsi_stmt (gsi);
9bfec7c2 704 tree decl;
75a70cf9 705 if (is_gimple_call (stmt) && (decl = gimple_call_fndecl (stmt)))
9bfec7c2 706 {
707 struct cgraph_edge *e = cgraph_edge (node, stmt);
708 if (e)
709 {
710 if (e->aux)
711 {
0a81f5a0 712 error ("shared call_stmt:");
75a70cf9 713 debug_gimple_stmt (stmt);
9bfec7c2 714 error_found = true;
715 }
ccf4ab6b 716 if (!clone_of_p (cgraph_node (decl), e->callee)
717 && !e->callee->global.inlined_to)
9bfec7c2 718 {
0a81f5a0 719 error ("edge points to wrong declaration:");
9bfec7c2 720 debug_tree (e->callee->decl);
721 fprintf (stderr," Instead of:");
722 debug_tree (decl);
723 }
724 e->aux = (void *)1;
725 }
726 else
727 {
0a81f5a0 728 error ("missing callgraph edge for call stmt:");
75a70cf9 729 debug_gimple_stmt (stmt);
9bfec7c2 730 error_found = true;
731 }
732 }
733 }
e27482aa 734 pointer_set_destroy (visited_nodes);
e27482aa 735 }
736 else
737 /* No CFG available?! */
738 gcc_unreachable ();
739
b0cdf642 740 for (e = node->callees; e; e = e->next_callee)
741 {
f8daee9b 742 if (!e->aux && !e->indirect_call)
b0cdf642 743 {
0a81f5a0 744 error ("edge %s->%s has no corresponding call_stmt",
abd3e6b5 745 identifier_to_locale (cgraph_node_name (e->caller)),
746 identifier_to_locale (cgraph_node_name (e->callee)));
75a70cf9 747 debug_gimple_stmt (e->call_stmt);
b0cdf642 748 error_found = true;
749 }
750 e->aux = 0;
751 }
752 }
753 if (error_found)
754 {
755 dump_cgraph_node (stderr, node);
0a81f5a0 756 internal_error ("verify_cgraph_node failed");
b0cdf642 757 }
117ef3d7 758 set_cfun (saved_cfun);
b0cdf642 759 timevar_pop (TV_CGRAPH_VERIFY);
760}
761
762/* Verify whole cgraph structure. */
763void
764verify_cgraph (void)
765{
766 struct cgraph_node *node;
767
8ec2a798 768 if (sorrycount || errorcount)
769 return;
770
b0cdf642 771 for (node = cgraph_nodes; node; node = node->next)
772 verify_cgraph_node (node);
773}
774
56af936e 775/* Output all asm statements we have stored up to be output. */
776
777static void
778cgraph_output_pending_asms (void)
779{
780 struct cgraph_asm_node *can;
781
782 if (errorcount || sorrycount)
783 return;
784
785 for (can = cgraph_asm_nodes; can; can = can->next)
786 assemble_asm (can->asm_str);
787 cgraph_asm_nodes = NULL;
788}
789
0785e435 790/* Analyze the function scheduled to be output. */
1e8e9920 791void
0785e435 792cgraph_analyze_function (struct cgraph_node *node)
793{
794 tree decl = node->decl;
795
ec1e35b2 796 current_function_decl = decl;
e27482aa 797 push_cfun (DECL_STRUCT_FUNCTION (decl));
798 cgraph_lower_function (node);
6e8d6e86 799 node->analyzed = true;
0785e435 800
e27482aa 801 pop_cfun ();
c08871a9 802 current_function_decl = NULL;
0785e435 803}
804
05806473 805/* Look for externally_visible and used attributes and mark cgraph nodes
806 accordingly.
807
808 We cannot mark the nodes at the point the attributes are processed (in
809 handle_*_attribute) because the copy of the declarations available at that
810 point may not be canonical. For example, in:
811
812 void f();
813 void f() __attribute__((used));
814
815 the declaration we see in handle_used_attribute will be the second
816 declaration -- but the front end will subsequently merge that declaration
817 with the original declaration and discard the second declaration.
818
819 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
820
821 void f() {}
822 void f() __attribute__((externally_visible));
823
824 is valid.
825
826 So, we walk the nodes at the end of the translation unit, applying the
827 attributes at that point. */
828
829static void
830process_function_and_variable_attributes (struct cgraph_node *first,
1d416bd7 831 struct varpool_node *first_var)
05806473 832{
833 struct cgraph_node *node;
1d416bd7 834 struct varpool_node *vnode;
05806473 835
836 for (node = cgraph_nodes; node != first; node = node->next)
837 {
838 tree decl = node->decl;
839 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
840 {
841 mark_decl_referenced (decl);
842 if (node->local.finalized)
843 cgraph_mark_needed_node (node);
844 }
845 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
846 {
ba12ea31 847 if (! TREE_PUBLIC (node->decl))
848 warning (OPT_Wattributes,
849 "%J%<externally_visible%> attribute have effect only on public objects",
850 node->decl);
851 else
852 {
853 if (node->local.finalized)
854 cgraph_mark_needed_node (node);
855 node->local.externally_visible = true;
856 }
05806473 857 }
858 }
1d416bd7 859 for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
05806473 860 {
861 tree decl = vnode->decl;
862 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
863 {
864 mark_decl_referenced (decl);
865 if (vnode->finalized)
1d416bd7 866 varpool_mark_needed_node (vnode);
05806473 867 }
868 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
869 {
ba12ea31 870 if (! TREE_PUBLIC (vnode->decl))
871 warning (OPT_Wattributes,
872 "%J%<externally_visible%> attribute have effect only on public objects",
873 vnode->decl);
874 else
875 {
876 if (vnode->finalized)
1d416bd7 877 varpool_mark_needed_node (vnode);
ba12ea31 878 vnode->externally_visible = true;
879 }
05806473 880 }
881 }
882}
883
aeeb194b 884/* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
885 each reachable functions) and build cgraph.
886 The function can be called multiple times after inserting new nodes
0d424440 887 into beginning of queue. Just the new part of queue is re-scanned then. */
ae01b312 888
aeeb194b 889static void
890cgraph_analyze_functions (void)
ae01b312 891{
c1dcd13c 892 /* Keep track of already processed nodes when called multiple times for
06b27565 893 intermodule optimization. */
c1dcd13c 894 static struct cgraph_node *first_analyzed;
c17d0de1 895 struct cgraph_node *first_processed = first_analyzed;
1d416bd7 896 static struct varpool_node *first_analyzed_var;
aeeb194b 897 struct cgraph_node *node, *next;
ae01b312 898
c17d0de1 899 process_function_and_variable_attributes (first_processed,
900 first_analyzed_var);
901 first_processed = cgraph_nodes;
1d416bd7 902 first_analyzed_var = varpool_nodes;
903 varpool_analyze_pending_decls ();
f79b6507 904 if (cgraph_dump_file)
ae01b312 905 {
e4200070 906 fprintf (cgraph_dump_file, "Initial entry points:");
c1dcd13c 907 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
1a1a827a 908 if (node->needed)
f79b6507 909 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
910 fprintf (cgraph_dump_file, "\n");
ae01b312 911 }
aeeb194b 912 cgraph_process_new_functions ();
ae01b312 913
e6d2b2d8 914 /* Propagate reachability flag and lower representation of all reachable
915 functions. In the future, lowering will introduce new functions and
916 new entry points on the way (by template instantiation and virtual
917 method table generation for instance). */
3d7bfc56 918 while (cgraph_nodes_queue)
ae01b312 919 {
0785e435 920 struct cgraph_edge *edge;
3d7bfc56 921 tree decl = cgraph_nodes_queue->decl;
922
923 node = cgraph_nodes_queue;
d87976fb 924 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
b0cdf642 925 node->next_needed = NULL;
ae01b312 926
638531ad 927 /* ??? It is possible to create extern inline function and later using
bbd5cba2 928 weak alias attribute to kill its body. See
638531ad 929 gcc.c-torture/compile/20011119-1.c */
75a70cf9 930 if (!DECL_STRUCT_FUNCTION (decl))
9b8fb23a 931 {
932 cgraph_reset_node (node);
933 continue;
934 }
638531ad 935
cc636d56 936 gcc_assert (!node->analyzed && node->reachable);
75a70cf9 937 gcc_assert (gimple_body (decl));
ae01b312 938
0785e435 939 cgraph_analyze_function (node);
2c0b522d 940
ae01b312 941 for (edge = node->callees; edge; edge = edge->next_callee)
0785e435 942 if (!edge->callee->reachable)
2c0b522d 943 cgraph_mark_reachable_node (edge->callee);
944
d544ceff 945 /* If decl is a clone of an abstract function, mark that abstract
946 function so that we don't release its body. The DECL_INITIAL() of that
947 abstract function declaration will be later needed to output debug info. */
948 if (DECL_ABSTRACT_ORIGIN (decl))
949 {
950 struct cgraph_node *origin_node = cgraph_node (DECL_ABSTRACT_ORIGIN (decl));
951 origin_node->abstract_and_needed = true;
952 }
953
c17d0de1 954 /* We finalize local static variables during constructing callgraph
955 edges. Process their attributes too. */
956 process_function_and_variable_attributes (first_processed,
957 first_analyzed_var);
958 first_processed = cgraph_nodes;
1d416bd7 959 first_analyzed_var = varpool_nodes;
960 varpool_analyze_pending_decls ();
aeeb194b 961 cgraph_process_new_functions ();
ae01b312 962 }
2c0b522d 963
aa5e06c7 964 /* Collect entry points to the unit. */
f79b6507 965 if (cgraph_dump_file)
3d7bfc56 966 {
e4200070 967 fprintf (cgraph_dump_file, "Unit entry points:");
c1dcd13c 968 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
1a1a827a 969 if (node->needed)
f79b6507 970 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
e4200070 971 fprintf (cgraph_dump_file, "\n\nInitial ");
0785e435 972 dump_cgraph (cgraph_dump_file);
3d7bfc56 973 }
e6d2b2d8 974
f79b6507 975 if (cgraph_dump_file)
976 fprintf (cgraph_dump_file, "\nReclaiming functions:");
ae01b312 977
f4ec5ce1 978 for (node = cgraph_nodes; node != first_analyzed; node = next)
ae01b312 979 {
980 tree decl = node->decl;
f4ec5ce1 981 next = node->next;
ae01b312 982
1a1a827a 983 if (node->local.finalized && !gimple_has_body_p (decl))
a0c938f0 984 cgraph_reset_node (node);
9b8fb23a 985
1a1a827a 986 if (!node->reachable && gimple_has_body_p (decl))
ae01b312 987 {
f79b6507 988 if (cgraph_dump_file)
989 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
b0cdf642 990 cgraph_remove_node (node);
9b8fb23a 991 continue;
ae01b312 992 }
bc5cab3b 993 else
994 node->next_needed = NULL;
1a1a827a 995 gcc_assert (!node->local.finalized || gimple_has_body_p (decl));
9b8fb23a 996 gcc_assert (node->analyzed == node->local.finalized);
ae01b312 997 }
f79b6507 998 if (cgraph_dump_file)
e4200070 999 {
1000 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1001 dump_cgraph (cgraph_dump_file);
1002 }
c1dcd13c 1003 first_analyzed = cgraph_nodes;
ae01b312 1004 ggc_collect ();
aeeb194b 1005}
1006
1007/* Analyze the whole compilation unit once it is parsed completely. */
1008
1009void
1010cgraph_finalize_compilation_unit (void)
1011{
1012 if (errorcount || sorrycount)
1013 return;
1014
1015 finish_aliases_1 ();
1016
aeeb194b 1017 if (!quiet_flag)
1018 {
1019 fprintf (stderr, "\nAnalyzing compilation unit\n");
1020 fflush (stderr);
1021 }
1022
1023 timevar_push (TV_CGRAPH);
1024 cgraph_analyze_functions ();
f79b6507 1025 timevar_pop (TV_CGRAPH);
ae01b312 1026}
9ed5b1f5 1027
1028
ae01b312 1029/* Figure out what functions we want to assemble. */
1030
1031static void
d9d9733a 1032cgraph_mark_functions_to_output (void)
ae01b312 1033{
1034 struct cgraph_node *node;
1035
ae01b312 1036 for (node = cgraph_nodes; node; node = node->next)
1037 {
1038 tree decl = node->decl;
d7c6d889 1039 struct cgraph_edge *e;
a0c938f0 1040
09fc9532 1041 gcc_assert (!node->process);
d7c6d889 1042
1043 for (e = node->callers; e; e = e->next_caller)
611e5405 1044 if (e->inline_failed)
d7c6d889 1045 break;
ae01b312 1046
e6d2b2d8 1047 /* We need to output all local functions that are used and not
1048 always inlined, as well as those that are reachable from
1049 outside the current compilation unit. */
1a1a827a 1050 if (node->analyzed
b0cdf642 1051 && !node->global.inlined_to
ae01b312 1052 && (node->needed
d7c6d889 1053 || (e && node->reachable))
4ee9c684 1054 && !TREE_ASM_WRITTEN (decl)
ae01b312 1055 && !DECL_EXTERNAL (decl))
09fc9532 1056 node->process = 1;
cc636d56 1057 else
9cee7c3f 1058 {
1059 /* We should've reclaimed all functions that are not needed. */
1060#ifdef ENABLE_CHECKING
75a70cf9 1061 if (!node->global.inlined_to
1a1a827a 1062 && gimple_has_body_p (decl)
9cee7c3f 1063 && !DECL_EXTERNAL (decl))
1064 {
1065 dump_cgraph_node (stderr, node);
1066 internal_error ("failed to reclaim unneeded function");
1067 }
1068#endif
75a70cf9 1069 gcc_assert (node->global.inlined_to
1a1a827a 1070 || !gimple_has_body_p (decl)
9cee7c3f 1071 || DECL_EXTERNAL (decl));
1072
1073 }
a0c938f0 1074
961e3b13 1075 }
1076}
1077
ae01b312 1078/* Expand function specified by NODE. */
e6d2b2d8 1079
ae01b312 1080static void
d9d9733a 1081cgraph_expand_function (struct cgraph_node *node)
ae01b312 1082{
1083 tree decl = node->decl;
1084
b0cdf642 1085 /* We ought to not compile any inline clones. */
cc636d56 1086 gcc_assert (!node->global.inlined_to);
b0cdf642 1087
6329636b 1088 announce_function (decl);
09fc9532 1089 node->process = 0;
961e3b13 1090
e7c352d1 1091 gcc_assert (node->lowered);
f8deefc1 1092
794da2bb 1093 /* Generate RTL for the body of DECL. */
22c748a1 1094 if (lang_hooks.callgraph.emit_associated_thunks
1095 && node->finalized_by_frontend)
84e10000 1096 lang_hooks.callgraph.emit_associated_thunks (decl);
1097 tree_rest_of_compilation (decl);
961e3b13 1098
4ee9c684 1099 /* Make sure that BE didn't give up on compiling. */
c04e3894 1100 gcc_assert (TREE_ASM_WRITTEN (decl));
ae01b312 1101 current_function_decl = NULL;
1a1a827a 1102 gcc_assert (!cgraph_preserve_function_body_p (decl));
1103 cgraph_release_function_body (node);
1104 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1105 points to the dead function body. */
1106 cgraph_node_remove_callees (node);
e1be32b8 1107
1108 cgraph_function_flags_ready = true;
ae01b312 1109}
1110
b0cdf642 1111/* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
d7c6d889 1112
1113bool
326a9581 1114cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
d7c6d889 1115{
b0cdf642 1116 *reason = e->inline_failed;
1117 return !e->inline_failed;
d7c6d889 1118}
b0cdf642 1119
acc70efa 1120
acc70efa 1121
d9d9733a 1122/* Expand all functions that must be output.
1123
d7c6d889 1124 Attempt to topologically sort the nodes so function is output when
1125 all called functions are already assembled to allow data to be
91c82c20 1126 propagated across the callgraph. Use a stack to get smaller distance
3927afe0 1127 between a function and its callees (later we may choose to use a more
d7c6d889 1128 sophisticated algorithm for function reordering; we will likely want
1129 to use subsections to make the output functions appear in top-down
1130 order). */
1131
1132static void
a6868229 1133cgraph_expand_all_functions (void)
d7c6d889 1134{
1135 struct cgraph_node *node;
4c36ffe6 1136 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
c04e3894 1137 int order_pos, new_order_pos = 0;
d7c6d889 1138 int i;
1139
d7c6d889 1140 order_pos = cgraph_postorder (order);
cc636d56 1141 gcc_assert (order_pos == cgraph_n_nodes);
d7c6d889 1142
7bd28bba 1143 /* Garbage collector may remove inline clones we eliminate during
b0cdf642 1144 optimization. So we must be sure to not reference them. */
1145 for (i = 0; i < order_pos; i++)
09fc9532 1146 if (order[i]->process)
b0cdf642 1147 order[new_order_pos++] = order[i];
1148
1149 for (i = new_order_pos - 1; i >= 0; i--)
d7c6d889 1150 {
1151 node = order[i];
09fc9532 1152 if (node->process)
d7c6d889 1153 {
cc636d56 1154 gcc_assert (node->reachable);
09fc9532 1155 node->process = 0;
d7c6d889 1156 cgraph_expand_function (node);
1157 }
1158 }
523c1122 1159 cgraph_process_new_functions ();
773c5ba7 1160
d7c6d889 1161 free (order);
773c5ba7 1162
d7c6d889 1163}
1164
56af936e 1165/* This is used to sort the node types by the cgraph order number. */
1166
0b09525f 1167enum cgraph_order_sort_kind
1168{
1169 ORDER_UNDEFINED = 0,
1170 ORDER_FUNCTION,
1171 ORDER_VAR,
1172 ORDER_ASM
1173};
1174
56af936e 1175struct cgraph_order_sort
1176{
0b09525f 1177 enum cgraph_order_sort_kind kind;
56af936e 1178 union
1179 {
1180 struct cgraph_node *f;
1d416bd7 1181 struct varpool_node *v;
56af936e 1182 struct cgraph_asm_node *a;
1183 } u;
1184};
1185
1186/* Output all functions, variables, and asm statements in the order
1187 according to their order fields, which is the order in which they
1188 appeared in the file. This implements -fno-toplevel-reorder. In
1189 this mode we may output functions and variables which don't really
1190 need to be output. */
1191
1192static void
1193cgraph_output_in_order (void)
1194{
1195 int max;
1196 size_t size;
1197 struct cgraph_order_sort *nodes;
1198 int i;
1199 struct cgraph_node *pf;
1d416bd7 1200 struct varpool_node *pv;
56af936e 1201 struct cgraph_asm_node *pa;
1202
1203 max = cgraph_order;
1204 size = max * sizeof (struct cgraph_order_sort);
1205 nodes = (struct cgraph_order_sort *) alloca (size);
1206 memset (nodes, 0, size);
1207
1d416bd7 1208 varpool_analyze_pending_decls ();
56af936e 1209
1210 for (pf = cgraph_nodes; pf; pf = pf->next)
1211 {
09fc9532 1212 if (pf->process)
56af936e 1213 {
1214 i = pf->order;
1215 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1216 nodes[i].kind = ORDER_FUNCTION;
1217 nodes[i].u.f = pf;
1218 }
1219 }
1220
1d416bd7 1221 for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
56af936e 1222 {
1223 i = pv->order;
1224 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1225 nodes[i].kind = ORDER_VAR;
1226 nodes[i].u.v = pv;
1227 }
1228
1229 for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1230 {
1231 i = pa->order;
1232 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1233 nodes[i].kind = ORDER_ASM;
1234 nodes[i].u.a = pa;
1235 }
56af936e 1236
304e5318 1237 /* In toplevel reorder mode we output all statics; mark them as needed. */
1238 for (i = 0; i < max; ++i)
1239 {
1240 if (nodes[i].kind == ORDER_VAR)
1241 {
1242 varpool_mark_needed_node (nodes[i].u.v);
1243 }
1244 }
1245 varpool_empty_needed_queue ();
1246
56af936e 1247 for (i = 0; i < max; ++i)
1248 {
1249 switch (nodes[i].kind)
1250 {
1251 case ORDER_FUNCTION:
09fc9532 1252 nodes[i].u.f->process = 0;
56af936e 1253 cgraph_expand_function (nodes[i].u.f);
1254 break;
1255
1256 case ORDER_VAR:
1d416bd7 1257 varpool_assemble_decl (nodes[i].u.v);
56af936e 1258 break;
1259
1260 case ORDER_ASM:
1261 assemble_asm (nodes[i].u.a->asm_str);
1262 break;
1263
1264 case ORDER_UNDEFINED:
1265 break;
1266
1267 default:
1268 gcc_unreachable ();
1269 }
1270 }
4b4ea2db 1271
1272 cgraph_asm_nodes = NULL;
56af936e 1273}
1274
b0cdf642 1275/* Return true when function body of DECL still needs to be kept around
1276 for later re-use. */
1277bool
1278cgraph_preserve_function_body_p (tree decl)
1279{
1280 struct cgraph_node *node;
8d8c4c8d 1281
1282 gcc_assert (cgraph_global_info_ready);
b0cdf642 1283 /* Look if there is any clone around. */
ccf4ab6b 1284 node = cgraph_node (decl);
1285 if (node->clones)
1286 return true;
b0cdf642 1287 return false;
1288}
1289
77fce4cd 1290static void
1291ipa_passes (void)
1292{
87d4aa85 1293 set_cfun (NULL);
4b14adf9 1294 current_function_decl = NULL;
75a70cf9 1295 gimple_register_cfg_hooks ();
77fce4cd 1296 bitmap_obstack_initialize (NULL);
1297 execute_ipa_pass_list (all_ipa_passes);
9ed5b1f5 1298
1299 /* Generate coverage variables and constructors. */
1300 coverage_finish ();
1301
1302 /* Process new functions added. */
1303 set_cfun (NULL);
1304 current_function_decl = NULL;
1305 cgraph_process_new_functions ();
1306
77fce4cd 1307 bitmap_obstack_release (NULL);
1308}
1309
ae01b312 1310/* Perform simple optimizations based on callgraph. */
1311
1312void
d9d9733a 1313cgraph_optimize (void)
ae01b312 1314{
cb2b5570 1315 if (errorcount || sorrycount)
1316 return;
1317
b0cdf642 1318#ifdef ENABLE_CHECKING
1319 verify_cgraph ();
1320#endif
a861fe52 1321
1322 /* Call functions declared with the "constructor" or "destructor"
1323 attribute. */
1324 cgraph_build_cdtor_fns ();
e9f08e82 1325
c1dcd13c 1326 /* Frontend may output common variables after the unit has been finalized.
1327 It is safe to deal with them here as they are always zero initialized. */
1d416bd7 1328 varpool_analyze_pending_decls ();
aeeb194b 1329 cgraph_analyze_functions ();
e9f08e82 1330
f79b6507 1331 timevar_push (TV_CGRAPHOPT);
51949610 1332 if (pre_ipa_mem_report)
1333 {
1334 fprintf (stderr, "Memory consumption before IPA\n");
1335 dump_memory_report (false);
1336 }
d7c6d889 1337 if (!quiet_flag)
cd6bca02 1338 fprintf (stderr, "Performing interprocedural optimizations\n");
523c1122 1339 cgraph_state = CGRAPH_STATE_IPA;
c04e3894 1340
be4d0974 1341 /* Don't run the IPA passes if there was any error or sorry messages. */
1342 if (errorcount == 0 && sorrycount == 0)
1343 ipa_passes ();
1344
e1be32b8 1345 /* This pass remove bodies of extern inline functions we never inlined.
1346 Do this later so other IPA passes see what is really going on. */
1347 cgraph_remove_unreachable_nodes (false, dump_file);
80a85d8a 1348 cgraph_global_info_ready = true;
f79b6507 1349 if (cgraph_dump_file)
1350 {
e4200070 1351 fprintf (cgraph_dump_file, "Optimized ");
f79b6507 1352 dump_cgraph (cgraph_dump_file);
c1dcd13c 1353 dump_varpool (cgraph_dump_file);
f79b6507 1354 }
51949610 1355 if (post_ipa_mem_report)
1356 {
defa2fa6 1357 fprintf (stderr, "Memory consumption after IPA\n");
51949610 1358 dump_memory_report (false);
1359 }
f79b6507 1360 timevar_pop (TV_CGRAPHOPT);
ae01b312 1361
d7c6d889 1362 /* Output everything. */
e4200070 1363 if (!quiet_flag)
1364 fprintf (stderr, "Assembling functions:\n");
b0cdf642 1365#ifdef ENABLE_CHECKING
1366 verify_cgraph ();
1367#endif
56af936e 1368
ccf4ab6b 1369 cgraph_materialize_all_clones ();
acc70efa 1370 cgraph_mark_functions_to_output ();
c1dcd13c 1371
523c1122 1372 cgraph_state = CGRAPH_STATE_EXPANSION;
56af936e 1373 if (!flag_toplevel_reorder)
1374 cgraph_output_in_order ();
1375 else
1376 {
1377 cgraph_output_pending_asms ();
1378
1379 cgraph_expand_all_functions ();
1d416bd7 1380 varpool_remove_unreferenced_decls ();
56af936e 1381
1d416bd7 1382 varpool_assemble_pending_decls ();
56af936e 1383 }
523c1122 1384 cgraph_process_new_functions ();
1385 cgraph_state = CGRAPH_STATE_FINISHED;
c1dcd13c 1386
f79b6507 1387 if (cgraph_dump_file)
1388 {
e4200070 1389 fprintf (cgraph_dump_file, "\nFinal ");
f79b6507 1390 dump_cgraph (cgraph_dump_file);
1391 }
b0cdf642 1392#ifdef ENABLE_CHECKING
1393 verify_cgraph ();
4ee9c684 1394 /* Double check that all inline clones are gone and that all
1395 function bodies have been released from memory. */
6329636b 1396 if (!(sorrycount || errorcount))
4ee9c684 1397 {
1398 struct cgraph_node *node;
1399 bool error_found = false;
1400
1401 for (node = cgraph_nodes; node; node = node->next)
1402 if (node->analyzed
1403 && (node->global.inlined_to
1a1a827a 1404 || gimple_has_body_p (node->decl)))
4ee9c684 1405 {
1406 error_found = true;
1407 dump_cgraph_node (stderr, node);
a0c938f0 1408 }
4ee9c684 1409 if (error_found)
c04e3894 1410 internal_error ("nodes with unreleased memory found");
4ee9c684 1411 }
b0cdf642 1412#endif
ae01b312 1413}
2c56f72e 1414/* Generate and emit a static constructor or destructor. WHICH must
1415 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1416 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
f0b5f617 1417 initialization priority for this constructor or destructor. */
b5530559 1418
1419void
c5344b58 1420cgraph_build_static_cdtor (char which, tree body, int priority)
b5530559 1421{
1422 static int counter = 0;
1423 char which_buf[16];
540edea7 1424 tree decl, name, resdecl;
b5530559 1425
2c56f72e 1426 /* The priority is encoded in the constructor or destructor name.
1427 collect2 will sort the names and arrange that they are called at
1428 program startup. */
1429 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
db85cc4f 1430 name = get_file_function_name (which_buf);
b5530559 1431
1432 decl = build_decl (FUNCTION_DECL, name,
1433 build_function_type (void_type_node, void_list_node));
1434 current_function_decl = decl;
1435
540edea7 1436 resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1437 DECL_ARTIFICIAL (resdecl) = 1;
540edea7 1438 DECL_RESULT (decl) = resdecl;
8e5b4ed6 1439 DECL_CONTEXT (resdecl) = decl;
540edea7 1440
80f2ef47 1441 allocate_struct_function (decl, false);
b5530559 1442
1443 TREE_STATIC (decl) = 1;
1444 TREE_USED (decl) = 1;
1445 DECL_ARTIFICIAL (decl) = 1;
b5530559 1446 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1447 DECL_SAVED_TREE (decl) = body;
1448 TREE_PUBLIC (decl) = ! targetm.have_ctors_dtors;
1449 DECL_UNINLINABLE (decl) = 1;
1450
1451 DECL_INITIAL (decl) = make_node (BLOCK);
1452 TREE_USED (DECL_INITIAL (decl)) = 1;
1453
1454 DECL_SOURCE_LOCATION (decl) = input_location;
1455 cfun->function_end_locus = input_location;
1456
cc636d56 1457 switch (which)
1458 {
1459 case 'I':
1460 DECL_STATIC_CONSTRUCTOR (decl) = 1;
64c2e9b0 1461 decl_init_priority_insert (decl, priority);
cc636d56 1462 break;
1463 case 'D':
1464 DECL_STATIC_DESTRUCTOR (decl) = 1;
64c2e9b0 1465 decl_fini_priority_insert (decl, priority);
cc636d56 1466 break;
1467 default:
1468 gcc_unreachable ();
1469 }
b5530559 1470
1471 gimplify_function_tree (decl);
1472
523c1122 1473 cgraph_add_new_function (decl, false);
1474 cgraph_mark_needed_node (cgraph_node (decl));
e3a37aef 1475 set_cfun (NULL);
b5530559 1476}
121f3051 1477
1478void
1479init_cgraph (void)
1480{
1481 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
1482}
b5d36404 1483
a0c938f0 1484/* The edges representing the callers of the NEW_VERSION node were
b5d36404 1485 fixed by cgraph_function_versioning (), now the call_expr in their
1486 respective tree code should be updated to call the NEW_VERSION. */
1487
1488static void
1489update_call_expr (struct cgraph_node *new_version)
1490{
1491 struct cgraph_edge *e;
1492
1493 gcc_assert (new_version);
75a70cf9 1494
1495 /* Update the call expr on the edges to call the new version. */
b5d36404 1496 for (e = new_version->callers; e; e = e->next_caller)
e03a95e7 1497 {
1498 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
1499 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
1500 /* Update EH information too, just in case. */
1501 if (!stmt_could_throw_p (e->call_stmt)
1502 && lookup_stmt_eh_region_fn (inner_function, e->call_stmt))
1503 remove_stmt_from_eh_region_fn (inner_function, e->call_stmt);
1504 }
b5d36404 1505}
1506
1507
1508/* Create a new cgraph node which is the new version of
1509 OLD_VERSION node. REDIRECT_CALLERS holds the callers
1510 edges which should be redirected to point to
1511 NEW_VERSION. ALL the callees edges of OLD_VERSION
1512 are cloned to the new version node. Return the new
1513 version node. */
1514
1515static struct cgraph_node *
1516cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
4460a647 1517 tree new_decl,
1518 VEC(cgraph_edge_p,heap) *redirect_callers)
b5d36404 1519 {
1520 struct cgraph_node *new_version;
1521 struct cgraph_edge *e, *new_e;
1522 struct cgraph_edge *next_callee;
1523 unsigned i;
1524
1525 gcc_assert (old_version);
a0c938f0 1526
b5d36404 1527 new_version = cgraph_node (new_decl);
1528
1529 new_version->analyzed = true;
1530 new_version->local = old_version->local;
1531 new_version->global = old_version->global;
1532 new_version->rtl = new_version->rtl;
1533 new_version->reachable = true;
1534 new_version->count = old_version->count;
1535
1536 /* Clone the old node callees. Recursive calls are
1537 also cloned. */
1538 for (e = old_version->callees;e; e=e->next_callee)
1539 {
4ae20857 1540 new_e = cgraph_clone_edge (e, new_version, e->call_stmt, 0, e->frequency,
1541 e->loop_nest, true);
b5d36404 1542 new_e->count = e->count;
1543 }
1544 /* Fix recursive calls.
1545 If OLD_VERSION has a recursive call after the
1546 previous edge cloning, the new version will have an edge
1547 pointing to the old version, which is wrong;
1548 Redirect it to point to the new version. */
1549 for (e = new_version->callees ; e; e = next_callee)
1550 {
1551 next_callee = e->next_callee;
1552 if (e->callee == old_version)
1553 cgraph_redirect_edge_callee (e, new_version);
a0c938f0 1554
b5d36404 1555 if (!next_callee)
1556 break;
1557 }
4460a647 1558 for (i = 0; VEC_iterate (cgraph_edge_p, redirect_callers, i, e); i++)
1559 {
1560 /* Redirect calls to the old version node to point to its new
1561 version. */
1562 cgraph_redirect_edge_callee (e, new_version);
1563 }
b5d36404 1564
1565 return new_version;
1566 }
1567
1568 /* Perform function versioning.
a0c938f0 1569 Function versioning includes copying of the tree and
b5d36404 1570 a callgraph update (creating a new cgraph node and updating
1571 its callees and callers).
1572
1573 REDIRECT_CALLERS varray includes the edges to be redirected
1574 to the new version.
1575
1576 TREE_MAP is a mapping of tree nodes we want to replace with
1577 new ones (according to results of prior analysis).
1578 OLD_VERSION_NODE is the node that is versioned.
5afe38fe 1579 It returns the new version's cgraph node.
1580 ARGS_TO_SKIP lists arguments to be omitted from functions
1581 */
b5d36404 1582
1583struct cgraph_node *
1584cgraph_function_versioning (struct cgraph_node *old_version_node,
4460a647 1585 VEC(cgraph_edge_p,heap) *redirect_callers,
ccf4ab6b 1586 VEC (ipa_replace_map_p,gc)* tree_map,
5afe38fe 1587 bitmap args_to_skip)
b5d36404 1588{
1589 tree old_decl = old_version_node->decl;
1590 struct cgraph_node *new_version_node = NULL;
1591 tree new_decl;
1592
1593 if (!tree_versionable_function_p (old_decl))
1594 return NULL;
1595
1596 /* Make a new FUNCTION_DECL tree node for the
1597 new version. */
5afe38fe 1598 if (!args_to_skip)
1599 new_decl = copy_node (old_decl);
1600 else
1601 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
b5d36404 1602
1603 /* Create the new version's call-graph node.
1604 and update the edges of the new node. */
1605 new_version_node =
1606 cgraph_copy_node_for_versioning (old_version_node, new_decl,
1607 redirect_callers);
1608
1609 /* Copy the OLD_VERSION_NODE function tree to the new version. */
5afe38fe 1610 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip);
b5d36404 1611
a0c938f0 1612 /* Update the new version's properties.
e03a95e7 1613 Make The new version visible only within this translation unit. Make sure
1614 that is not weak also.
a0c938f0 1615 ??? We cannot use COMDAT linkage because there is no
b5d36404 1616 ABI support for this. */
1617 DECL_EXTERNAL (new_version_node->decl) = 0;
1618 DECL_ONE_ONLY (new_version_node->decl) = 0;
1619 TREE_PUBLIC (new_version_node->decl) = 0;
1620 DECL_COMDAT (new_version_node->decl) = 0;
e03a95e7 1621 DECL_WEAK (new_version_node->decl) = 0;
f014e39d 1622 DECL_VIRTUAL_P (new_version_node->decl) = 0;
b5d36404 1623 new_version_node->local.externally_visible = 0;
1624 new_version_node->local.local = 1;
1625 new_version_node->lowered = true;
f014e39d 1626
e03a95e7 1627 /* Update the call_expr on the edges to call the new version node. */
1628 update_call_expr (new_version_node);
1629
50828ed8 1630 cgraph_call_function_insertion_hooks (new_version_node);
b5d36404 1631 return new_version_node;
1632}
469679ab 1633
1634/* Produce separate function body for inline clones so the offline copy can be
1635 modified without affecting them. */
1636struct cgraph_node *
1637save_inline_function_body (struct cgraph_node *node)
1638{
ccf4ab6b 1639 struct cgraph_node *first_clone, *n;
469679ab 1640
1641 gcc_assert (node == cgraph_node (node->decl));
1642
1643 cgraph_lower_function (node);
1644
ccf4ab6b 1645 first_clone = node->clones;
469679ab 1646
1647 first_clone->decl = copy_node (node->decl);
469679ab 1648 cgraph_insert_node_to_hashtable (first_clone);
1649 gcc_assert (first_clone == cgraph_node (first_clone->decl));
ccf4ab6b 1650 if (first_clone->next_sibling_clone)
1651 {
1652 for (n = first_clone->next_sibling_clone; n->next_sibling_clone; n = n->next_sibling_clone)
1653 n->clone_of = first_clone;
1654 n->clone_of = first_clone;
1655 n->next_sibling_clone = first_clone->clones;
1656 if (first_clone->clones)
1657 first_clone->clones->prev_sibling_clone = n;
1658 first_clone->clones = first_clone->next_sibling_clone;
1659 first_clone->next_sibling_clone->prev_sibling_clone = NULL;
1660 first_clone->next_sibling_clone = NULL;
1661 gcc_assert (!first_clone->prev_sibling_clone);
1662 }
1663 first_clone->clone_of = NULL;
1664 node->clones = NULL;
1665
1666 if (first_clone->clones)
1667 for (n = first_clone->clones; n != first_clone;)
1668 {
1669 gcc_assert (n->decl == node->decl);
1670 n->decl = first_clone->decl;
1671 if (n->clones)
1672 n = n->clones;
1673 else if (n->next_sibling_clone)
1674 n = n->next_sibling_clone;
1675 else
1676 {
1677 while (n != first_clone && !n->next_sibling_clone)
1678 n = n->clone_of;
1679 if (n != first_clone)
1680 n = n->next_sibling_clone;
1681 }
1682 }
469679ab 1683
1684 /* Copy the OLD_VERSION_NODE function tree to the new version. */
5afe38fe 1685 tree_function_versioning (node->decl, first_clone->decl, NULL, true, NULL);
469679ab 1686
1687 DECL_EXTERNAL (first_clone->decl) = 0;
1688 DECL_ONE_ONLY (first_clone->decl) = 0;
1689 TREE_PUBLIC (first_clone->decl) = 0;
1690 DECL_COMDAT (first_clone->decl) = 0;
7fe9b425 1691 VEC_free (ipa_opt_pass, heap,
1692 DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply);
1693 DECL_STRUCT_FUNCTION (first_clone->decl)->ipa_transforms_to_apply = NULL;
469679ab 1694
469679ab 1695#ifdef ENABLE_CHECKING
1696 verify_cgraph_node (first_clone);
1697#endif
1698 return first_clone;
1699}
a861fe52 1700
ccf4ab6b 1701/* Given virtual clone, turn it into actual clone. */
1702static void
1703cgraph_materialize_clone (struct cgraph_node *node)
1704{
1705 bitmap_obstack_initialize (NULL);
1706 /* Copy the OLD_VERSION_NODE function tree to the new version. */
1707 tree_function_versioning (node->clone_of->decl, node->decl,
1708 node->clone.tree_map, true,
1709 node->clone.args_to_skip);
1710
1711 /* Function is no longer clone. */
1712 if (node->next_sibling_clone)
1713 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
1714 if (node->prev_sibling_clone)
1715 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
1716 else
1717 node->clone_of->clones = node->next_sibling_clone;
1718 node->next_sibling_clone = NULL;
1719 node->prev_sibling_clone = NULL;
1720 node->clone_of = NULL;
1721 bitmap_obstack_release (NULL);
1722}
1723
1724/* Once all functions from compilation unit are in memory, produce all clones
1725 and update all calls.
1726 We might also do this on demand if we don't want to bring all functions to
1727 memory prior compilation, but current WHOPR implementation does that and it is
1728 is bit easier to keep everything right in this order. */
1729void
1730cgraph_materialize_all_clones (void)
1731{
1732 struct cgraph_node *node;
1733 bool stabilized = false;
1734
1735 if (cgraph_dump_file)
1736 fprintf (cgraph_dump_file, "Materializing clones\n");
1737#ifdef ENABLE_CHECKING
1738 verify_cgraph ();
1739#endif
1740
1741 /* We can also do topological order, but number of iterations should be
1742 bounded by number of IPA passes since single IPA pass is probably not
1743 going to create clones of clones it created itself. */
1744 while (!stabilized)
1745 {
1746 stabilized = true;
1747 for (node = cgraph_nodes; node; node = node->next)
1748 {
1749 if (node->clone_of && node->decl != node->clone_of->decl
1750 && !gimple_has_body_p (node->decl))
1751 {
1752 if (gimple_has_body_p (node->clone_of->decl))
1753 {
1754 if (cgraph_dump_file)
1755 fprintf (cgraph_dump_file, " clonning %s to %s",
1756 cgraph_node_name (node->clone_of),
1757 cgraph_node_name (node));
1758 cgraph_materialize_clone (node);
1759 }
1760 else
1761 stabilized = false;
1762 }
1763 }
1764 }
1765 if (cgraph_dump_file)
1766 fprintf (cgraph_dump_file, "Updating call sites\n");
1767 for (node = cgraph_nodes; node; node = node->next)
1768 if (node->analyzed && gimple_has_body_p (node->decl)
1769 && (!node->clone_of || node->clone_of->decl != node->decl))
1770 {
1771 struct cgraph_edge *e;
1772
1773 current_function_decl = node->decl;
1774 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1775 for (e = node->callees; e; e = e->next_callee)
1776 {
1777 tree decl = gimple_call_fndecl (e->call_stmt);
947781ac 1778 /* When function gets inlined, indirect inlining might've invented
1779 new edge for orginally indirect stmt. Since we are not
1780 preserving clones in the original form, we must not update here
1781 since other inline clones don't need to contain call to the same
1782 call. Inliner will do the substitution for us later. */
1783 if (decl && decl != e->callee->decl)
ccf4ab6b 1784 {
1785 gimple new_stmt;
1786 gimple_stmt_iterator gsi;
1787
1788 if (cgraph_dump_file)
1789 {
1790 fprintf (cgraph_dump_file, "updating call of %s in %s:",
1791 cgraph_node_name (node),
1792 cgraph_node_name (e->callee));
1793 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1794 }
1795
1796 if (e->callee->clone.args_to_skip)
1797 new_stmt = gimple_call_copy_skip_args (e->call_stmt,
1798 e->callee->clone.args_to_skip);
1799 else
1800 new_stmt = e->call_stmt;
1801 if (gimple_vdef (new_stmt)
1802 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
1803 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
1804 gimple_call_set_fndecl (new_stmt, e->callee->decl);
1805
1806 gsi = gsi_for_stmt (e->call_stmt);
1807 gsi_replace (&gsi, new_stmt, true);
1808
1809 /* Update EH information too, just in case. */
1810 if (!stmt_could_throw_p (new_stmt)
1811 && lookup_stmt_eh_region (new_stmt))
1812 remove_stmt_from_eh_region (new_stmt);
1813
1814 cgraph_set_call_stmt_including_clones (node, e->call_stmt, new_stmt);
1815
1816 if (cgraph_dump_file)
1817 {
1818 fprintf (cgraph_dump_file, " updated to:");
1819 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
1820 }
1821 }
1822 }
1823 pop_cfun ();
1824 current_function_decl = NULL;
1825#ifdef ENABLE_CHECKING
1826 verify_cgraph_node (node);
1827#endif
1828 }
947781ac 1829#ifdef ENABLE_CHECKING
1830 verify_cgraph ();
1831#endif
ccf4ab6b 1832 cgraph_remove_unreachable_nodes (false, cgraph_dump_file);
1833}
1834
a861fe52 1835#include "gt-cgraphunit.h"