rs6000.c (rs6000_cannot_force_const_mem): Match CONST high part large-toc address.
[official-gcc.git] / gcc / cgraphunit.c
blob6683d2a5df375f62cec0792d94bdd90ca3ce11ed
1 /* Callgraph based interprocedural optimizations.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
3 2011 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module implements main driver of compilation process as well as
23 few basic interprocedural optimizers.
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)
28 The front-end is supposed to use following functionality:
30 - cgraph_finalize_function
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.
35 (There is one exception needed for implementing GCC extern inline
36 function.)
38 - varpool_finalize_variable
40 This function has same behavior as the above but is used for static
41 variables.
43 - cgraph_finalize_compilation_unit
45 This function is called once (source level) compilation unit is finalized
46 and it will no longer change.
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.
52 The function can be called multiple times when multiple source level
53 compilation units are combined (such as in C frontend)
55 - cgraph_optimize
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.
62 - cgraph_mark_needed_node
63 - varpool_mark_needed_node
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
69 used by C++ frontend to explicitly mark the keyed methods.
71 - analyze_expr callback
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.
77 ??? On the tree-ssa genericizing should take place here and we will avoid
78 need for these hooks (replacing them by genericizing hook)
80 Analyzing of all functions is deferred
81 to cgraph_finalize_compilation_unit and expansion into cgraph_optimize.
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.
91 The intra-procedural information is produced and its existence
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.
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.
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.
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
106 where reference has been optimized out. */
109 #include "config.h"
110 #include "system.h"
111 #include "coretypes.h"
112 #include "tm.h"
113 #include "tree.h"
114 #include "rtl.h"
115 #include "tree-flow.h"
116 #include "tree-inline.h"
117 #include "langhooks.h"
118 #include "pointer-set.h"
119 #include "toplev.h"
120 #include "flags.h"
121 #include "ggc.h"
122 #include "debug.h"
123 #include "target.h"
124 #include "cgraph.h"
125 #include "diagnostic.h"
126 #include "tree-pretty-print.h"
127 #include "gimple-pretty-print.h"
128 #include "timevar.h"
129 #include "params.h"
130 #include "fibheap.h"
131 #include "intl.h"
132 #include "function.h"
133 #include "ipa-prop.h"
134 #include "gimple.h"
135 #include "tree-iterator.h"
136 #include "tree-pass.h"
137 #include "tree-dump.h"
138 #include "output.h"
139 #include "coverage.h"
140 #include "plugin.h"
141 #include "ipa-inline.h"
142 #include "ipa-utils.h"
143 #include "lto-streamer.h"
145 static void cgraph_expand_all_functions (void);
146 static void cgraph_mark_functions_to_output (void);
147 static void cgraph_expand_function (struct cgraph_node *);
148 static void cgraph_output_pending_asms (void);
150 FILE *cgraph_dump_file;
152 /* Used for vtable lookup in thunk adjusting. */
153 static GTY (()) tree vtable_entry_type;
155 /* Determine if function DECL is needed. That is, visible to something
156 either outside this translation unit, something magic in the system
157 configury. */
159 bool
160 cgraph_decide_is_function_needed (struct cgraph_node *node, tree decl)
162 /* If the user told us it is used, then it must be so. */
163 if (node->local.externally_visible)
164 return true;
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)
170 && (!node->thunk.thunk_p && !node->same_body_alias)
171 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
172 return true;
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)
178 && !DECL_EXTERNAL (decl)
179 && !DECL_DISREGARD_INLINE_LIMITS (decl))
180 return true;
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;
188 /* Externally visible functions must be output. The exception is
189 COMDAT functions that must be output only when they are needed.
191 When not optimizing, also output the static functions. (see
192 PR24561), but don't do so for always_inline functions, functions
193 declared inline and nested functions. These were optimized out
194 in the original implementation and it is unclear whether we want
195 to change the behavior here. */
196 if (((TREE_PUBLIC (decl)
197 || (!optimize
198 && !DECL_DISREGARD_INLINE_LIMITS (decl)
199 && !DECL_DECLARED_INLINE_P (decl)
200 && !(DECL_CONTEXT (decl)
201 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)))
202 && !flag_whole_program
203 && !flag_lto)
204 && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
205 return true;
207 return false;
210 /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
211 functions into callgraph in a way so they look like ordinary reachable
212 functions inserted into callgraph already at construction time. */
214 bool
215 cgraph_process_new_functions (void)
217 bool output = false;
218 tree fndecl;
219 struct cgraph_node *node;
221 varpool_analyze_pending_decls ();
222 /* Note that this queue may grow as its being processed, as the new
223 functions may generate new ones. */
224 while (cgraph_new_nodes)
226 node = cgraph_new_nodes;
227 fndecl = node->decl;
228 cgraph_new_nodes = cgraph_new_nodes->next_needed;
229 switch (cgraph_state)
231 case CGRAPH_STATE_CONSTRUCTION:
232 /* At construction time we just need to finalize function and move
233 it into reachable functions list. */
235 node->next_needed = NULL;
236 cgraph_finalize_function (fndecl, false);
237 cgraph_mark_reachable_node (node);
238 output = true;
239 cgraph_call_function_insertion_hooks (node);
240 break;
242 case CGRAPH_STATE_IPA:
243 case CGRAPH_STATE_IPA_SSA:
244 /* When IPA optimization already started, do all essential
245 transformations that has been already performed on the whole
246 cgraph but not on this function. */
248 gimple_register_cfg_hooks ();
249 if (!node->analyzed)
250 cgraph_analyze_function (node);
251 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
252 current_function_decl = fndecl;
253 if ((cgraph_state == CGRAPH_STATE_IPA_SSA
254 && !gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl)))
255 /* When not optimizing, be sure we run early local passes anyway
256 to expand OMP. */
257 || !optimize)
258 execute_pass_list (pass_early_local_passes.pass.sub);
259 else
260 compute_inline_parameters (node, true);
261 free_dominance_info (CDI_POST_DOMINATORS);
262 free_dominance_info (CDI_DOMINATORS);
263 pop_cfun ();
264 current_function_decl = NULL;
265 cgraph_call_function_insertion_hooks (node);
266 break;
268 case CGRAPH_STATE_EXPANSION:
269 /* Functions created during expansion shall be compiled
270 directly. */
271 node->process = 0;
272 cgraph_call_function_insertion_hooks (node);
273 cgraph_expand_function (node);
274 break;
276 default:
277 gcc_unreachable ();
278 break;
280 varpool_analyze_pending_decls ();
282 return output;
285 /* As an GCC extension we allow redefinition of the function. The
286 semantics when both copies of bodies differ is not well defined.
287 We replace the old body with new body so in unit at a time mode
288 we always use new body, while in normal mode we may end up with
289 old body inlined into some functions and new body expanded and
290 inlined in others.
292 ??? It may make more sense to use one body for inlining and other
293 body for expanding the function but this is difficult to do. */
295 static void
296 cgraph_reset_node (struct cgraph_node *node)
298 /* If node->process is set, then we have already begun whole-unit analysis.
299 This is *not* testing for whether we've already emitted the function.
300 That case can be sort-of legitimately seen with real function redefinition
301 errors. I would argue that the front end should never present us with
302 such a case, but don't enforce that for now. */
303 gcc_assert (!node->process);
305 /* Reset our data structures so we can analyze the function again. */
306 memset (&node->local, 0, sizeof (node->local));
307 memset (&node->global, 0, sizeof (node->global));
308 memset (&node->rtl, 0, sizeof (node->rtl));
309 node->analyzed = false;
310 node->local.finalized = false;
312 cgraph_node_remove_callees (node);
315 static void
316 cgraph_lower_function (struct cgraph_node *node)
318 if (node->lowered)
319 return;
321 if (node->nested)
322 lower_nested_functions (node->decl);
323 gcc_assert (!node->nested);
325 tree_lowering_passes (node->decl);
326 node->lowered = true;
329 /* DECL has been parsed. Take it, queue it, compile it at the whim of the
330 logic in effect. If NESTED is true, then our caller cannot stand to have
331 the garbage collector run at the moment. We would need to either create
332 a new GC context, or just not compile right now. */
334 void
335 cgraph_finalize_function (tree decl, bool nested)
337 struct cgraph_node *node = cgraph_get_create_node (decl);
339 if (node->local.finalized)
341 cgraph_reset_node (node);
342 node->local.redefined_extern_inline = true;
345 notice_global_symbol (decl);
346 node->local.finalized = true;
347 node->lowered = DECL_STRUCT_FUNCTION (decl)->cfg != NULL;
349 if (cgraph_decide_is_function_needed (node, decl))
350 cgraph_mark_needed_node (node);
352 /* Since we reclaim unreachable nodes at the end of every language
353 level unit, we need to be conservative about possible entry points
354 there. */
355 if ((TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_EXTERNAL (decl))
356 || DECL_STATIC_CONSTRUCTOR (decl)
357 || DECL_STATIC_DESTRUCTOR (decl)
358 /* COMDAT virtual functions may be referenced by vtable from
359 other compilation unit. Still we want to devirtualize calls
360 to those so we need to analyze them.
361 FIXME: We should introduce may edges for this purpose and update
362 their handling in unreachable function removal and inliner too. */
363 || (DECL_VIRTUAL_P (decl)
364 && optimize && (DECL_COMDAT (decl) || DECL_EXTERNAL (decl))))
365 cgraph_mark_reachable_node (node);
367 /* If we've not yet emitted decl, tell the debug info about it. */
368 if (!TREE_ASM_WRITTEN (decl))
369 (*debug_hooks->deferred_inline_function) (decl);
371 /* Possibly warn about unused parameters. */
372 if (warn_unused_parameter)
373 do_warn_unused_parameter (decl);
375 if (!nested)
376 ggc_collect ();
379 /* C99 extern inline keywords allow changing of declaration after function
380 has been finalized. We need to re-decide if we want to mark the function as
381 needed then. */
383 void
384 cgraph_mark_if_needed (tree decl)
386 struct cgraph_node *node = cgraph_get_node (decl);
387 if (node->local.finalized && cgraph_decide_is_function_needed (node, decl))
388 cgraph_mark_needed_node (node);
391 /* Return TRUE if NODE2 is equivalent to NODE or its clone. */
392 static bool
393 clone_of_p (struct cgraph_node *node, struct cgraph_node *node2)
395 node = cgraph_function_or_thunk_node (node, NULL);
396 node2 = cgraph_function_or_thunk_node (node2, NULL);
397 while (node != node2 && node2)
398 node2 = node2->clone_of;
399 return node2 != NULL;
402 /* Verify edge E count and frequency. */
404 static bool
405 verify_edge_count_and_frequency (struct cgraph_edge *e)
407 bool error_found = false;
408 if (e->count < 0)
410 error ("caller edge count is negative");
411 error_found = true;
413 if (e->frequency < 0)
415 error ("caller edge frequency is negative");
416 error_found = true;
418 if (e->frequency > CGRAPH_FREQ_MAX)
420 error ("caller edge frequency is too large");
421 error_found = true;
423 if (gimple_has_body_p (e->caller->decl)
424 && !e->caller->global.inlined_to
425 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
426 Remove this once edges are actualy removed from the function at that time. */
427 && (e->frequency
428 || (inline_edge_summary_vec
429 && !inline_edge_summary (e)->predicate))
430 && (e->frequency
431 != compute_call_stmt_bb_frequency (e->caller->decl,
432 gimple_bb (e->call_stmt))))
434 error ("caller edge frequency %i does not match BB frequency %i",
435 e->frequency,
436 compute_call_stmt_bb_frequency (e->caller->decl,
437 gimple_bb (e->call_stmt)));
438 error_found = true;
440 return error_found;
443 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
444 static void
445 cgraph_debug_gimple_stmt (struct function *this_cfun, gimple stmt)
447 /* debug_gimple_stmt needs correct cfun */
448 if (cfun != this_cfun)
449 set_cfun (this_cfun);
450 debug_gimple_stmt (stmt);
453 /* Verify cgraph nodes of given cgraph node. */
454 DEBUG_FUNCTION void
455 verify_cgraph_node (struct cgraph_node *node)
457 struct cgraph_edge *e;
458 struct function *this_cfun = DECL_STRUCT_FUNCTION (node->decl);
459 basic_block this_block;
460 gimple_stmt_iterator gsi;
461 bool error_found = false;
463 if (seen_error ())
464 return;
466 timevar_push (TV_CGRAPH_VERIFY);
467 for (e = node->callees; e; e = e->next_callee)
468 if (e->aux)
470 error ("aux field set for edge %s->%s",
471 identifier_to_locale (cgraph_node_name (e->caller)),
472 identifier_to_locale (cgraph_node_name (e->callee)));
473 error_found = true;
475 if (node->count < 0)
477 error ("execution count is negative");
478 error_found = true;
480 if (node->global.inlined_to && node->local.externally_visible)
482 error ("externally visible inline clone");
483 error_found = true;
485 if (node->global.inlined_to && node->address_taken)
487 error ("inline clone with address taken");
488 error_found = true;
490 if (node->global.inlined_to && node->needed)
492 error ("inline clone is needed");
493 error_found = true;
495 for (e = node->indirect_calls; e; e = e->next_callee)
497 if (e->aux)
499 error ("aux field set for indirect edge from %s",
500 identifier_to_locale (cgraph_node_name (e->caller)));
501 error_found = true;
503 if (!e->indirect_unknown_callee
504 || !e->indirect_info)
506 error ("An indirect edge from %s is not marked as indirect or has "
507 "associated indirect_info, the corresponding statement is: ",
508 identifier_to_locale (cgraph_node_name (e->caller)));
509 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
510 error_found = true;
513 for (e = node->callers; e; e = e->next_caller)
515 if (verify_edge_count_and_frequency (e))
516 error_found = true;
517 if (!e->inline_failed)
519 if (node->global.inlined_to
520 != (e->caller->global.inlined_to
521 ? e->caller->global.inlined_to : e->caller))
523 error ("inlined_to pointer is wrong");
524 error_found = true;
526 if (node->callers->next_caller)
528 error ("multiple inline callers");
529 error_found = true;
532 else
533 if (node->global.inlined_to)
535 error ("inlined_to pointer set for noninline callers");
536 error_found = true;
539 for (e = node->indirect_calls; e; e = e->next_callee)
540 if (verify_edge_count_and_frequency (e))
541 error_found = true;
542 if (!node->callers && node->global.inlined_to)
544 error ("inlined_to pointer is set but no predecessors found");
545 error_found = true;
547 if (node->global.inlined_to == node)
549 error ("inlined_to pointer refers to itself");
550 error_found = true;
553 if (!cgraph_get_node (node->decl))
555 error ("node not found in cgraph_hash");
556 error_found = true;
559 if (node->clone_of)
561 struct cgraph_node *n;
562 for (n = node->clone_of->clones; n; n = n->next_sibling_clone)
563 if (n == node)
564 break;
565 if (!n)
567 error ("node has wrong clone_of");
568 error_found = true;
571 if (node->clones)
573 struct cgraph_node *n;
574 for (n = node->clones; n; n = n->next_sibling_clone)
575 if (n->clone_of != node)
576 break;
577 if (n)
579 error ("node has wrong clone list");
580 error_found = true;
583 if ((node->prev_sibling_clone || node->next_sibling_clone) && !node->clone_of)
585 error ("node is in clone list but it is not clone");
586 error_found = true;
588 if (!node->prev_sibling_clone && node->clone_of && node->clone_of->clones != node)
590 error ("node has wrong prev_clone pointer");
591 error_found = true;
593 if (node->prev_sibling_clone && node->prev_sibling_clone->next_sibling_clone != node)
595 error ("double linked list of clones corrupted");
596 error_found = true;
598 if (node->same_comdat_group)
600 struct cgraph_node *n = node->same_comdat_group;
602 if (!DECL_ONE_ONLY (node->decl))
604 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
605 error_found = true;
607 if (n == node)
609 error ("node is alone in a comdat group");
610 error_found = true;
614 if (!n->same_comdat_group)
616 error ("same_comdat_group is not a circular list");
617 error_found = true;
618 break;
620 n = n->same_comdat_group;
622 while (n != node);
625 if (node->analyzed && node->alias)
627 bool ref_found = false;
628 int i;
629 struct ipa_ref *ref;
631 if (node->callees)
633 error ("Alias has call edges");
634 error_found = true;
636 for (i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
637 if (ref->use != IPA_REF_ALIAS)
639 error ("Alias has non-alias refernece");
640 error_found = true;
642 else if (ref_found)
644 error ("Alias has more than one alias reference");
645 error_found = true;
647 else
648 ref_found = true;
649 if (!ref_found)
651 error ("Analyzed alias has no reference");
652 error_found = true;
655 if (node->analyzed && node->thunk.thunk_p)
657 if (!node->callees)
659 error ("No edge out of thunk node");
660 error_found = true;
662 else if (node->callees->next_callee)
664 error ("More than one edge out of thunk node");
665 error_found = true;
667 if (gimple_has_body_p (node->decl))
669 error ("Thunk is not supposed to have body");
670 error_found = true;
673 else if (node->analyzed && gimple_has_body_p (node->decl)
674 && !TREE_ASM_WRITTEN (node->decl)
675 && (!DECL_EXTERNAL (node->decl) || node->global.inlined_to)
676 && !flag_wpa)
678 if (this_cfun->cfg)
680 /* The nodes we're interested in are never shared, so walk
681 the tree ignoring duplicates. */
682 struct pointer_set_t *visited_nodes = pointer_set_create ();
683 /* Reach the trees by walking over the CFG, and note the
684 enclosing basic-blocks in the call edges. */
685 FOR_EACH_BB_FN (this_block, this_cfun)
686 for (gsi = gsi_start_bb (this_block);
687 !gsi_end_p (gsi);
688 gsi_next (&gsi))
690 gimple stmt = gsi_stmt (gsi);
691 if (is_gimple_call (stmt))
693 struct cgraph_edge *e = cgraph_edge (node, stmt);
694 tree decl = gimple_call_fndecl (stmt);
695 if (e)
697 if (e->aux)
699 error ("shared call_stmt:");
700 cgraph_debug_gimple_stmt (this_cfun, stmt);
701 error_found = true;
703 if (!e->indirect_unknown_callee)
705 if (!e->callee->global.inlined_to
706 && decl
707 && cgraph_get_node (decl)
708 && (e->callee->former_clone_of
709 != cgraph_get_node (decl)->decl)
710 /* IPA-CP sometimes redirect edge to clone and then back to the former
711 function. This ping-pong has to go, eventaully. */
712 && (cgraph_function_or_thunk_node (cgraph_get_node (decl), NULL)
713 != cgraph_function_or_thunk_node (e->callee, NULL))
714 && !clone_of_p (cgraph_get_node (decl),
715 e->callee))
717 error ("edge points to wrong declaration:");
718 debug_tree (e->callee->decl);
719 fprintf (stderr," Instead of:");
720 debug_tree (decl);
721 error_found = true;
724 else if (decl)
726 error ("an indirect edge with unknown callee "
727 "corresponding to a call_stmt with "
728 "a known declaration:");
729 error_found = true;
730 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
732 e->aux = (void *)1;
734 else if (decl)
736 error ("missing callgraph edge for call stmt:");
737 cgraph_debug_gimple_stmt (this_cfun, stmt);
738 error_found = true;
742 pointer_set_destroy (visited_nodes);
744 else
745 /* No CFG available?! */
746 gcc_unreachable ();
748 for (e = node->callees; e; e = e->next_callee)
750 if (!e->aux)
752 error ("edge %s->%s has no corresponding call_stmt",
753 identifier_to_locale (cgraph_node_name (e->caller)),
754 identifier_to_locale (cgraph_node_name (e->callee)));
755 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
756 error_found = true;
758 e->aux = 0;
760 for (e = node->indirect_calls; e; e = e->next_callee)
762 if (!e->aux)
764 error ("an indirect edge from %s has no corresponding call_stmt",
765 identifier_to_locale (cgraph_node_name (e->caller)));
766 cgraph_debug_gimple_stmt (this_cfun, e->call_stmt);
767 error_found = true;
769 e->aux = 0;
772 if (error_found)
774 dump_cgraph_node (stderr, node);
775 internal_error ("verify_cgraph_node failed");
777 timevar_pop (TV_CGRAPH_VERIFY);
780 /* Verify whole cgraph structure. */
781 DEBUG_FUNCTION void
782 verify_cgraph (void)
784 struct cgraph_node *node;
786 if (seen_error ())
787 return;
789 for (node = cgraph_nodes; node; node = node->next)
790 verify_cgraph_node (node);
793 /* Output all asm statements we have stored up to be output. */
795 static void
796 cgraph_output_pending_asms (void)
798 struct cgraph_asm_node *can;
800 if (seen_error ())
801 return;
803 for (can = cgraph_asm_nodes; can; can = can->next)
804 assemble_asm (can->asm_str);
805 cgraph_asm_nodes = NULL;
808 /* Analyze the function scheduled to be output. */
809 void
810 cgraph_analyze_function (struct cgraph_node *node)
812 tree save = current_function_decl;
813 tree decl = node->decl;
815 if (node->alias && node->thunk.alias)
817 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
818 if (!VEC_length (ipa_ref_t, node->ref_list.references))
819 ipa_record_reference (node, NULL, tgt, NULL, IPA_REF_ALIAS, NULL);
820 if (node->same_body_alias)
822 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (node->thunk.alias);
823 DECL_DECLARED_INLINE_P (node->decl)
824 = DECL_DECLARED_INLINE_P (node->thunk.alias);
825 DECL_DISREGARD_INLINE_LIMITS (node->decl)
826 = DECL_DISREGARD_INLINE_LIMITS (node->thunk.alias);
829 /* Fixup visibility nonsences C++ frontend produce on same body aliases. */
830 if (TREE_PUBLIC (node->decl) && node->same_body_alias)
832 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (node->thunk.alias);
833 if (DECL_ONE_ONLY (node->thunk.alias))
835 DECL_COMDAT (node->decl) = DECL_COMDAT (node->thunk.alias);
836 DECL_COMDAT_GROUP (node->decl) = DECL_COMDAT_GROUP (node->thunk.alias);
837 if (DECL_ONE_ONLY (node->thunk.alias) && !node->same_comdat_group)
839 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
840 node->same_comdat_group = tgt;
841 if (!tgt->same_comdat_group)
842 tgt->same_comdat_group = node;
843 else
845 struct cgraph_node *n;
846 for (n = tgt->same_comdat_group;
847 n->same_comdat_group != tgt;
848 n = n->same_comdat_group)
850 n->same_comdat_group = node;
855 cgraph_mark_reachable_node (cgraph_alias_aliased_node (node));
856 if (node->address_taken)
857 cgraph_mark_address_taken_node (cgraph_alias_aliased_node (node));
858 if (cgraph_decide_is_function_needed (node, node->decl))
859 cgraph_mark_needed_node (node);
861 else if (node->thunk.thunk_p)
863 cgraph_create_edge (node, cgraph_get_node (node->thunk.alias),
864 NULL, 0, CGRAPH_FREQ_BASE);
866 else
868 current_function_decl = decl;
869 push_cfun (DECL_STRUCT_FUNCTION (decl));
871 assign_assembler_name_if_neeeded (node->decl);
873 /* Make sure to gimplify bodies only once. During analyzing a
874 function we lower it, which will require gimplified nested
875 functions, so we can end up here with an already gimplified
876 body. */
877 if (!gimple_body (decl))
878 gimplify_function_tree (decl);
879 dump_function (TDI_generic, decl);
881 cgraph_lower_function (node);
882 pop_cfun ();
884 node->analyzed = true;
886 current_function_decl = save;
889 /* C++ frontend produce same body aliases all over the place, even before PCH
890 gets streamed out. It relies on us linking the aliases with their function
891 in order to do the fixups, but ipa-ref is not PCH safe. Consequentely we
892 first produce aliases without links, but once C++ FE is sure he won't sream
893 PCH we build the links via this function. */
895 void
896 cgraph_process_same_body_aliases (void)
898 struct cgraph_node *node;
899 for (node = cgraph_nodes; node; node = node->next)
900 if (node->same_body_alias
901 && !VEC_length (ipa_ref_t, node->ref_list.references))
903 struct cgraph_node *tgt = cgraph_get_node (node->thunk.alias);
904 ipa_record_reference (node, NULL, tgt, NULL, IPA_REF_ALIAS, NULL);
906 same_body_aliases_done = true;
909 /* Process attributes common for vars and functions. */
911 static void
912 process_common_attributes (tree decl)
914 tree weakref = lookup_attribute ("weakref", DECL_ATTRIBUTES (decl));
916 if (weakref && !lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
918 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes,
919 "%<weakref%> attribute should be accompanied with"
920 " an %<alias%> attribute");
921 DECL_WEAK (decl) = 0;
922 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
923 DECL_ATTRIBUTES (decl));
927 /* Look for externally_visible and used attributes and mark cgraph nodes
928 accordingly.
930 We cannot mark the nodes at the point the attributes are processed (in
931 handle_*_attribute) because the copy of the declarations available at that
932 point may not be canonical. For example, in:
934 void f();
935 void f() __attribute__((used));
937 the declaration we see in handle_used_attribute will be the second
938 declaration -- but the front end will subsequently merge that declaration
939 with the original declaration and discard the second declaration.
941 Furthermore, we can't mark these nodes in cgraph_finalize_function because:
943 void f() {}
944 void f() __attribute__((externally_visible));
946 is valid.
948 So, we walk the nodes at the end of the translation unit, applying the
949 attributes at that point. */
951 static void
952 process_function_and_variable_attributes (struct cgraph_node *first,
953 struct varpool_node *first_var)
955 struct cgraph_node *node;
956 struct varpool_node *vnode;
958 for (node = cgraph_nodes; node != first; node = node->next)
960 tree decl = node->decl;
961 if (DECL_PRESERVE_P (decl))
962 cgraph_mark_needed_node (node);
963 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
964 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))
965 && TREE_PUBLIC (node->decl))
967 if (node->local.finalized)
968 cgraph_mark_needed_node (node);
970 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
972 if (! TREE_PUBLIC (node->decl))
973 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
974 "%<externally_visible%>"
975 " attribute have effect only on public objects");
976 else if (node->local.finalized)
977 cgraph_mark_needed_node (node);
979 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
980 && (node->local.finalized && !node->alias))
982 warning_at (DECL_SOURCE_LOCATION (node->decl), OPT_Wattributes,
983 "%<weakref%> attribute ignored"
984 " because function is defined");
985 DECL_WEAK (decl) = 0;
986 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
987 DECL_ATTRIBUTES (decl));
989 process_common_attributes (decl);
991 for (vnode = varpool_nodes; vnode != first_var; vnode = vnode->next)
993 tree decl = vnode->decl;
994 if (DECL_PRESERVE_P (decl))
996 vnode->force_output = true;
997 if (vnode->finalized)
998 varpool_mark_needed_node (vnode);
1000 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
1001 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))
1002 && TREE_PUBLIC (vnode->decl))
1004 if (vnode->finalized)
1005 varpool_mark_needed_node (vnode);
1007 else if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (decl)))
1009 if (! TREE_PUBLIC (vnode->decl))
1010 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
1011 "%<externally_visible%>"
1012 " attribute have effect only on public objects");
1013 else if (vnode->finalized)
1014 varpool_mark_needed_node (vnode);
1016 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1017 && vnode->finalized
1018 && DECL_INITIAL (decl))
1020 warning_at (DECL_SOURCE_LOCATION (vnode->decl), OPT_Wattributes,
1021 "%<weakref%> attribute ignored"
1022 " because variable is initialized");
1023 DECL_WEAK (decl) = 0;
1024 DECL_ATTRIBUTES (decl) = remove_attribute ("weakref",
1025 DECL_ATTRIBUTES (decl));
1027 process_common_attributes (decl);
1031 /* Process CGRAPH_NODES_NEEDED queue, analyze each function (and transitively
1032 each reachable functions) and build cgraph.
1033 The function can be called multiple times after inserting new nodes
1034 into beginning of queue. Just the new part of queue is re-scanned then. */
1036 static void
1037 cgraph_analyze_functions (void)
1039 /* Keep track of already processed nodes when called multiple times for
1040 intermodule optimization. */
1041 static struct cgraph_node *first_analyzed;
1042 struct cgraph_node *first_processed = first_analyzed;
1043 static struct varpool_node *first_analyzed_var;
1044 struct cgraph_node *node, *next;
1046 bitmap_obstack_initialize (NULL);
1047 process_function_and_variable_attributes (first_processed,
1048 first_analyzed_var);
1049 first_processed = cgraph_nodes;
1050 first_analyzed_var = varpool_nodes;
1051 varpool_analyze_pending_decls ();
1052 if (cgraph_dump_file)
1054 fprintf (cgraph_dump_file, "Initial entry points:");
1055 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
1056 if (node->needed)
1057 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1058 fprintf (cgraph_dump_file, "\n");
1060 cgraph_process_new_functions ();
1062 /* Propagate reachability flag and lower representation of all reachable
1063 functions. In the future, lowering will introduce new functions and
1064 new entry points on the way (by template instantiation and virtual
1065 method table generation for instance). */
1066 while (cgraph_nodes_queue)
1068 struct cgraph_edge *edge;
1069 tree decl = cgraph_nodes_queue->decl;
1071 node = cgraph_nodes_queue;
1072 cgraph_nodes_queue = cgraph_nodes_queue->next_needed;
1073 node->next_needed = NULL;
1075 /* ??? It is possible to create extern inline function and later using
1076 weak alias attribute to kill its body. See
1077 gcc.c-torture/compile/20011119-1.c */
1078 if (!DECL_STRUCT_FUNCTION (decl)
1079 && (!node->alias || !node->thunk.alias)
1080 && !node->thunk.thunk_p)
1082 cgraph_reset_node (node);
1083 node->local.redefined_extern_inline = true;
1084 continue;
1087 if (!node->analyzed)
1088 cgraph_analyze_function (node);
1090 for (edge = node->callees; edge; edge = edge->next_callee)
1091 if (!edge->callee->reachable)
1092 cgraph_mark_reachable_node (edge->callee);
1093 for (edge = node->callers; edge; edge = edge->next_caller)
1094 if (!edge->caller->reachable && edge->caller->thunk.thunk_p)
1095 cgraph_mark_reachable_node (edge->caller);
1097 if (node->same_comdat_group)
1099 for (next = node->same_comdat_group;
1100 next != node;
1101 next = next->same_comdat_group)
1102 cgraph_mark_reachable_node (next);
1105 /* If decl is a clone of an abstract function, mark that abstract
1106 function so that we don't release its body. The DECL_INITIAL() of that
1107 abstract function declaration will be later needed to output debug
1108 info. */
1109 if (DECL_ABSTRACT_ORIGIN (decl))
1111 struct cgraph_node *origin_node;
1112 origin_node = cgraph_get_node (DECL_ABSTRACT_ORIGIN (decl));
1113 origin_node->abstract_and_needed = true;
1116 /* We finalize local static variables during constructing callgraph
1117 edges. Process their attributes too. */
1118 process_function_and_variable_attributes (first_processed,
1119 first_analyzed_var);
1120 first_processed = cgraph_nodes;
1121 first_analyzed_var = varpool_nodes;
1122 varpool_analyze_pending_decls ();
1123 cgraph_process_new_functions ();
1126 /* Collect entry points to the unit. */
1127 if (cgraph_dump_file)
1129 fprintf (cgraph_dump_file, "Unit entry points:");
1130 for (node = cgraph_nodes; node != first_analyzed; node = node->next)
1131 if (node->needed)
1132 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1133 fprintf (cgraph_dump_file, "\n\nInitial ");
1134 dump_cgraph (cgraph_dump_file);
1135 dump_varpool (cgraph_dump_file);
1138 if (cgraph_dump_file)
1139 fprintf (cgraph_dump_file, "\nReclaiming functions:");
1141 for (node = cgraph_nodes; node != first_analyzed; node = next)
1143 tree decl = node->decl;
1144 next = node->next;
1146 if (node->local.finalized && !gimple_has_body_p (decl)
1147 && (!node->alias || !node->thunk.alias)
1148 && !node->thunk.thunk_p)
1149 cgraph_reset_node (node);
1151 if (!node->reachable
1152 && (gimple_has_body_p (decl) || node->thunk.thunk_p
1153 || (node->alias && node->thunk.alias)))
1155 if (cgraph_dump_file)
1156 fprintf (cgraph_dump_file, " %s", cgraph_node_name (node));
1157 cgraph_remove_node (node);
1158 continue;
1160 else
1161 node->next_needed = NULL;
1162 gcc_assert (!node->local.finalized || node->thunk.thunk_p
1163 || node->alias
1164 || gimple_has_body_p (decl));
1165 gcc_assert (node->analyzed == node->local.finalized);
1167 if (cgraph_dump_file)
1169 fprintf (cgraph_dump_file, "\n\nReclaimed ");
1170 dump_cgraph (cgraph_dump_file);
1171 dump_varpool (cgraph_dump_file);
1173 bitmap_obstack_release (NULL);
1174 first_analyzed = cgraph_nodes;
1175 ggc_collect ();
1178 /* Translate the ugly representation of aliases as alias pairs into nice
1179 representation in callgraph. We don't handle all cases yet,
1180 unforutnately. */
1182 static void
1183 handle_alias_pairs (void)
1185 alias_pair *p;
1186 unsigned i;
1187 struct cgraph_node *target_node;
1188 struct cgraph_node *src_node;
1189 struct varpool_node *target_vnode;
1191 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p);)
1193 if (TREE_CODE (p->decl) == FUNCTION_DECL
1194 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))
1195 && (target_node = cgraph_node_for_asm (p->target)) != NULL)
1197 src_node = cgraph_get_node (p->decl);
1198 if (src_node && src_node->local.finalized)
1199 cgraph_reset_node (src_node);
1200 /* Normally EXTERNAL flag is used to mark external inlines,
1201 however for aliases it seems to be allowed to use it w/o
1202 any meaning. See gcc.dg/attr-alias-3.c
1203 However for weakref we insist on EXTERNAL flag being set.
1204 See gcc.dg/attr-alias-5.c */
1205 if (DECL_EXTERNAL (p->decl))
1206 DECL_EXTERNAL (p->decl) = 0;
1207 cgraph_create_function_alias (p->decl, target_node->decl);
1208 VEC_unordered_remove (alias_pair, alias_pairs, i);
1210 else if (TREE_CODE (p->decl) == VAR_DECL
1211 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl))
1212 && (target_vnode = varpool_node_for_asm (p->target)) != NULL)
1214 /* Normally EXTERNAL flag is used to mark external inlines,
1215 however for aliases it seems to be allowed to use it w/o
1216 any meaning. See gcc.dg/attr-alias-3.c
1217 However for weakref we insist on EXTERNAL flag being set.
1218 See gcc.dg/attr-alias-5.c */
1219 if (DECL_EXTERNAL (p->decl))
1220 DECL_EXTERNAL (p->decl) = 0;
1221 varpool_create_variable_alias (p->decl, target_vnode->decl);
1222 VEC_unordered_remove (alias_pair, alias_pairs, i);
1224 else
1226 if (dump_file)
1227 fprintf (dump_file, "Unhandled alias %s->%s\n",
1228 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p->decl)),
1229 IDENTIFIER_POINTER (p->target));
1231 i++;
1237 /* Analyze the whole compilation unit once it is parsed completely. */
1239 void
1240 cgraph_finalize_compilation_unit (void)
1242 timevar_push (TV_CGRAPH);
1244 /* If LTO is enabled, initialize the streamer hooks needed by GIMPLE. */
1245 if (flag_lto)
1246 lto_streamer_hooks_init ();
1248 /* If we're here there's no current function anymore. Some frontends
1249 are lazy in clearing these. */
1250 current_function_decl = NULL;
1251 set_cfun (NULL);
1253 /* Do not skip analyzing the functions if there were errors, we
1254 miss diagnostics for following functions otherwise. */
1256 /* Emit size functions we didn't inline. */
1257 finalize_size_functions ();
1259 /* Mark alias targets necessary and emit diagnostics. */
1260 finish_aliases_1 ();
1261 handle_alias_pairs ();
1263 if (!quiet_flag)
1265 fprintf (stderr, "\nAnalyzing compilation unit\n");
1266 fflush (stderr);
1269 if (flag_dump_passes)
1270 dump_passes ();
1272 /* Gimplify and lower all functions, compute reachability and
1273 remove unreachable nodes. */
1274 cgraph_analyze_functions ();
1276 /* Mark alias targets necessary and emit diagnostics. */
1277 finish_aliases_1 ();
1278 handle_alias_pairs ();
1280 /* Gimplify and lower thunks. */
1281 cgraph_analyze_functions ();
1283 /* Finally drive the pass manager. */
1284 cgraph_optimize ();
1286 timevar_pop (TV_CGRAPH);
1290 /* Figure out what functions we want to assemble. */
1292 static void
1293 cgraph_mark_functions_to_output (void)
1295 struct cgraph_node *node;
1296 #ifdef ENABLE_CHECKING
1297 bool check_same_comdat_groups = false;
1299 for (node = cgraph_nodes; node; node = node->next)
1300 gcc_assert (!node->process);
1301 #endif
1303 for (node = cgraph_nodes; node; node = node->next)
1305 tree decl = node->decl;
1306 struct cgraph_edge *e;
1308 gcc_assert (!node->process || node->same_comdat_group);
1309 if (node->process)
1310 continue;
1312 for (e = node->callers; e; e = e->next_caller)
1313 if (e->inline_failed)
1314 break;
1316 /* We need to output all local functions that are used and not
1317 always inlined, as well as those that are reachable from
1318 outside the current compilation unit. */
1319 if (node->analyzed
1320 && !node->thunk.thunk_p
1321 && !node->alias
1322 && !node->global.inlined_to
1323 && (!cgraph_only_called_directly_p (node)
1324 || ((e || ipa_ref_has_aliases_p (&node->ref_list))
1325 && node->reachable))
1326 && !TREE_ASM_WRITTEN (decl)
1327 && !DECL_EXTERNAL (decl))
1329 node->process = 1;
1330 if (node->same_comdat_group)
1332 struct cgraph_node *next;
1333 for (next = node->same_comdat_group;
1334 next != node;
1335 next = next->same_comdat_group)
1336 if (!next->thunk.thunk_p && !next->alias)
1337 next->process = 1;
1340 else if (node->same_comdat_group)
1342 #ifdef ENABLE_CHECKING
1343 check_same_comdat_groups = true;
1344 #endif
1346 else
1348 /* We should've reclaimed all functions that are not needed. */
1349 #ifdef ENABLE_CHECKING
1350 if (!node->global.inlined_to
1351 && gimple_has_body_p (decl)
1352 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1353 are inside partition, we can end up not removing the body since we no longer
1354 have analyzed node pointing to it. */
1355 && !node->in_other_partition
1356 && !node->alias
1357 && !DECL_EXTERNAL (decl))
1359 dump_cgraph_node (stderr, node);
1360 internal_error ("failed to reclaim unneeded function");
1362 #endif
1363 gcc_assert (node->global.inlined_to
1364 || !gimple_has_body_p (decl)
1365 || node->in_other_partition
1366 || DECL_EXTERNAL (decl));
1371 #ifdef ENABLE_CHECKING
1372 if (check_same_comdat_groups)
1373 for (node = cgraph_nodes; node; node = node->next)
1374 if (node->same_comdat_group && !node->process)
1376 tree decl = node->decl;
1377 if (!node->global.inlined_to
1378 && gimple_has_body_p (decl)
1379 /* FIXME: in ltrans unit when offline copy is outside partition but inline copies
1380 are inside partition, we can end up not removing the body since we no longer
1381 have analyzed node pointing to it. */
1382 && !node->in_other_partition
1383 && !DECL_EXTERNAL (decl))
1385 dump_cgraph_node (stderr, node);
1386 internal_error ("failed to reclaim unneeded functionin same comdat group");
1389 #endif
1392 /* DECL is FUNCTION_DECL. Initialize datastructures so DECL is a function
1393 in lowered gimple form.
1395 Set current_function_decl and cfun to newly constructed empty function body.
1396 return basic block in the function body. */
1398 static basic_block
1399 init_lowered_empty_function (tree decl)
1401 basic_block bb;
1403 current_function_decl = decl;
1404 allocate_struct_function (decl, false);
1405 gimple_register_cfg_hooks ();
1406 init_empty_tree_cfg ();
1407 init_tree_ssa (cfun);
1408 init_ssa_operands ();
1409 cfun->gimple_df->in_ssa_p = true;
1410 DECL_INITIAL (decl) = make_node (BLOCK);
1412 DECL_SAVED_TREE (decl) = error_mark_node;
1413 cfun->curr_properties |=
1414 (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_referenced_vars |
1415 PROP_ssa);
1417 /* Create BB for body of the function and connect it properly. */
1418 bb = create_basic_block (NULL, (void *) 0, ENTRY_BLOCK_PTR);
1419 make_edge (ENTRY_BLOCK_PTR, bb, 0);
1420 make_edge (bb, EXIT_BLOCK_PTR, 0);
1422 return bb;
1425 /* Adjust PTR by the constant FIXED_OFFSET, and by the vtable
1426 offset indicated by VIRTUAL_OFFSET, if that is
1427 non-null. THIS_ADJUSTING is nonzero for a this adjusting thunk and
1428 zero for a result adjusting thunk. */
1430 static tree
1431 thunk_adjust (gimple_stmt_iterator * bsi,
1432 tree ptr, bool this_adjusting,
1433 HOST_WIDE_INT fixed_offset, tree virtual_offset)
1435 gimple stmt;
1436 tree ret;
1438 if (this_adjusting
1439 && fixed_offset != 0)
1441 stmt = gimple_build_assign (ptr,
1442 fold_build2_loc (input_location,
1443 POINTER_PLUS_EXPR,
1444 TREE_TYPE (ptr), ptr,
1445 size_int (fixed_offset)));
1446 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1449 /* If there's a virtual offset, look up that value in the vtable and
1450 adjust the pointer again. */
1451 if (virtual_offset)
1453 tree vtabletmp;
1454 tree vtabletmp2;
1455 tree vtabletmp3;
1456 tree offsettmp;
1458 if (!vtable_entry_type)
1460 tree vfunc_type = make_node (FUNCTION_TYPE);
1461 TREE_TYPE (vfunc_type) = integer_type_node;
1462 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
1463 layout_type (vfunc_type);
1465 vtable_entry_type = build_pointer_type (vfunc_type);
1468 vtabletmp =
1469 create_tmp_var (build_pointer_type
1470 (build_pointer_type (vtable_entry_type)), "vptr");
1472 /* The vptr is always at offset zero in the object. */
1473 stmt = gimple_build_assign (vtabletmp,
1474 build1 (NOP_EXPR, TREE_TYPE (vtabletmp),
1475 ptr));
1476 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1477 mark_symbols_for_renaming (stmt);
1478 find_referenced_vars_in (stmt);
1480 /* Form the vtable address. */
1481 vtabletmp2 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp)),
1482 "vtableaddr");
1483 stmt = gimple_build_assign (vtabletmp2,
1484 build_simple_mem_ref (vtabletmp));
1485 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1486 mark_symbols_for_renaming (stmt);
1487 find_referenced_vars_in (stmt);
1489 /* Find the entry with the vcall offset. */
1490 stmt = gimple_build_assign (vtabletmp2,
1491 fold_build2_loc (input_location,
1492 POINTER_PLUS_EXPR,
1493 TREE_TYPE (vtabletmp2),
1494 vtabletmp2,
1495 fold_convert (sizetype,
1496 virtual_offset)));
1497 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1499 /* Get the offset itself. */
1500 vtabletmp3 = create_tmp_var (TREE_TYPE (TREE_TYPE (vtabletmp2)),
1501 "vcalloffset");
1502 stmt = gimple_build_assign (vtabletmp3,
1503 build_simple_mem_ref (vtabletmp2));
1504 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1505 mark_symbols_for_renaming (stmt);
1506 find_referenced_vars_in (stmt);
1508 /* Cast to sizetype. */
1509 offsettmp = create_tmp_var (sizetype, "offset");
1510 stmt = gimple_build_assign (offsettmp, fold_convert (sizetype, vtabletmp3));
1511 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1512 mark_symbols_for_renaming (stmt);
1513 find_referenced_vars_in (stmt);
1515 /* Adjust the `this' pointer. */
1516 ptr = fold_build2_loc (input_location,
1517 POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr,
1518 offsettmp);
1521 if (!this_adjusting
1522 && fixed_offset != 0)
1523 /* Adjust the pointer by the constant. */
1525 tree ptrtmp;
1527 if (TREE_CODE (ptr) == VAR_DECL)
1528 ptrtmp = ptr;
1529 else
1531 ptrtmp = create_tmp_var (TREE_TYPE (ptr), "ptr");
1532 stmt = gimple_build_assign (ptrtmp, ptr);
1533 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1534 mark_symbols_for_renaming (stmt);
1535 find_referenced_vars_in (stmt);
1537 ptr = fold_build2_loc (input_location,
1538 POINTER_PLUS_EXPR, TREE_TYPE (ptrtmp), ptrtmp,
1539 size_int (fixed_offset));
1542 /* Emit the statement and gimplify the adjustment expression. */
1543 ret = create_tmp_var (TREE_TYPE (ptr), "adjusted_this");
1544 stmt = gimple_build_assign (ret, ptr);
1545 mark_symbols_for_renaming (stmt);
1546 find_referenced_vars_in (stmt);
1547 gsi_insert_after (bsi, stmt, GSI_NEW_STMT);
1549 return ret;
1552 /* Produce assembler for thunk NODE. */
1554 static void
1555 assemble_thunk (struct cgraph_node *node)
1557 bool this_adjusting = node->thunk.this_adjusting;
1558 HOST_WIDE_INT fixed_offset = node->thunk.fixed_offset;
1559 HOST_WIDE_INT virtual_value = node->thunk.virtual_value;
1560 tree virtual_offset = NULL;
1561 tree alias = node->thunk.alias;
1562 tree thunk_fndecl = node->decl;
1563 tree a = DECL_ARGUMENTS (thunk_fndecl);
1565 current_function_decl = thunk_fndecl;
1567 /* Ensure thunks are emitted in their correct sections. */
1568 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
1570 if (this_adjusting
1571 && targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
1572 virtual_value, alias))
1574 const char *fnname;
1575 tree fn_block;
1577 DECL_RESULT (thunk_fndecl)
1578 = build_decl (DECL_SOURCE_LOCATION (thunk_fndecl),
1579 RESULT_DECL, 0, integer_type_node);
1580 fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
1582 /* The back end expects DECL_INITIAL to contain a BLOCK, so we
1583 create one. */
1584 fn_block = make_node (BLOCK);
1585 BLOCK_VARS (fn_block) = a;
1586 DECL_INITIAL (thunk_fndecl) = fn_block;
1587 init_function_start (thunk_fndecl);
1588 cfun->is_thunk = 1;
1589 assemble_start_function (thunk_fndecl, fnname);
1591 targetm.asm_out.output_mi_thunk (asm_out_file, thunk_fndecl,
1592 fixed_offset, virtual_value, alias);
1594 assemble_end_function (thunk_fndecl, fnname);
1595 init_insn_lengths ();
1596 free_after_compilation (cfun);
1597 set_cfun (NULL);
1598 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1599 node->thunk.thunk_p = false;
1600 node->analyzed = false;
1602 else
1604 tree restype;
1605 basic_block bb, then_bb, else_bb, return_bb;
1606 gimple_stmt_iterator bsi;
1607 int nargs = 0;
1608 tree arg;
1609 int i;
1610 tree resdecl;
1611 tree restmp = NULL;
1612 VEC(tree, heap) *vargs;
1614 gimple call;
1615 gimple ret;
1617 DECL_IGNORED_P (thunk_fndecl) = 1;
1618 bitmap_obstack_initialize (NULL);
1620 if (node->thunk.virtual_offset_p)
1621 virtual_offset = size_int (virtual_value);
1623 /* Build the return declaration for the function. */
1624 restype = TREE_TYPE (TREE_TYPE (thunk_fndecl));
1625 if (DECL_RESULT (thunk_fndecl) == NULL_TREE)
1627 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
1628 DECL_ARTIFICIAL (resdecl) = 1;
1629 DECL_IGNORED_P (resdecl) = 1;
1630 DECL_RESULT (thunk_fndecl) = resdecl;
1632 else
1633 resdecl = DECL_RESULT (thunk_fndecl);
1635 bb = then_bb = else_bb = return_bb = init_lowered_empty_function (thunk_fndecl);
1637 bsi = gsi_start_bb (bb);
1639 /* Build call to the function being thunked. */
1640 if (!VOID_TYPE_P (restype))
1642 if (!is_gimple_reg_type (restype))
1644 restmp = resdecl;
1645 add_local_decl (cfun, restmp);
1646 BLOCK_VARS (DECL_INITIAL (current_function_decl)) = restmp;
1648 else
1649 restmp = create_tmp_var_raw (restype, "retval");
1652 for (arg = a; arg; arg = DECL_CHAIN (arg))
1653 nargs++;
1654 vargs = VEC_alloc (tree, heap, nargs);
1655 if (this_adjusting)
1656 VEC_quick_push (tree, vargs,
1657 thunk_adjust (&bsi,
1658 a, 1, fixed_offset,
1659 virtual_offset));
1660 else
1661 VEC_quick_push (tree, vargs, a);
1662 for (i = 1, arg = DECL_CHAIN (a); i < nargs; i++, arg = DECL_CHAIN (arg))
1663 VEC_quick_push (tree, vargs, arg);
1664 call = gimple_build_call_vec (build_fold_addr_expr_loc (0, alias), vargs);
1665 VEC_free (tree, heap, vargs);
1666 gimple_call_set_cannot_inline (call, true);
1667 gimple_call_set_from_thunk (call, true);
1668 if (restmp)
1669 gimple_call_set_lhs (call, restmp);
1670 gsi_insert_after (&bsi, call, GSI_NEW_STMT);
1671 mark_symbols_for_renaming (call);
1672 find_referenced_vars_in (call);
1673 update_stmt (call);
1675 if (restmp && !this_adjusting)
1677 tree true_label = NULL_TREE;
1679 if (TREE_CODE (TREE_TYPE (restmp)) == POINTER_TYPE)
1681 gimple stmt;
1682 /* If the return type is a pointer, we need to
1683 protect against NULL. We know there will be an
1684 adjustment, because that's why we're emitting a
1685 thunk. */
1686 then_bb = create_basic_block (NULL, (void *) 0, bb);
1687 return_bb = create_basic_block (NULL, (void *) 0, then_bb);
1688 else_bb = create_basic_block (NULL, (void *) 0, else_bb);
1689 remove_edge (single_succ_edge (bb));
1690 true_label = gimple_block_label (then_bb);
1691 stmt = gimple_build_cond (NE_EXPR, restmp,
1692 build_zero_cst (TREE_TYPE (restmp)),
1693 NULL_TREE, NULL_TREE);
1694 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1695 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
1696 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
1697 make_edge (return_bb, EXIT_BLOCK_PTR, 0);
1698 make_edge (then_bb, return_bb, EDGE_FALLTHRU);
1699 make_edge (else_bb, return_bb, EDGE_FALLTHRU);
1700 bsi = gsi_last_bb (then_bb);
1703 restmp = thunk_adjust (&bsi, restmp, /*this_adjusting=*/0,
1704 fixed_offset, virtual_offset);
1705 if (true_label)
1707 gimple stmt;
1708 bsi = gsi_last_bb (else_bb);
1709 stmt = gimple_build_assign (restmp,
1710 build_zero_cst (TREE_TYPE (restmp)));
1711 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
1712 bsi = gsi_last_bb (return_bb);
1715 else
1716 gimple_call_set_tail (call, true);
1718 /* Build return value. */
1719 ret = gimple_build_return (restmp);
1720 gsi_insert_after (&bsi, ret, GSI_NEW_STMT);
1722 delete_unreachable_blocks ();
1723 update_ssa (TODO_update_ssa);
1725 /* Since we want to emit the thunk, we explicitly mark its name as
1726 referenced. */
1727 node->thunk.thunk_p = false;
1728 cgraph_node_remove_callees (node);
1729 cgraph_add_new_function (thunk_fndecl, true);
1730 bitmap_obstack_release (NULL);
1732 current_function_decl = NULL;
1737 /* Assemble thunks and aliases asociated to NODE. */
1739 static void
1740 assemble_thunks_and_aliases (struct cgraph_node *node)
1742 struct cgraph_edge *e;
1743 int i;
1744 struct ipa_ref *ref;
1746 for (e = node->callers; e;)
1747 if (e->caller->thunk.thunk_p)
1749 struct cgraph_node *thunk = e->caller;
1751 e = e->next_caller;
1752 assemble_thunks_and_aliases (thunk);
1753 assemble_thunk (thunk);
1755 else
1756 e = e->next_caller;
1757 for (i = 0; ipa_ref_list_refering_iterate (&node->ref_list, i, ref); i++)
1758 if (ref->use == IPA_REF_ALIAS)
1760 struct cgraph_node *alias = ipa_ref_refering_node (ref);
1761 assemble_alias (alias->decl,
1762 DECL_ASSEMBLER_NAME (alias->thunk.alias));
1763 assemble_thunks_and_aliases (alias);
1767 /* Expand function specified by NODE. */
1769 static void
1770 cgraph_expand_function (struct cgraph_node *node)
1772 tree decl = node->decl;
1774 /* We ought to not compile any inline clones. */
1775 gcc_assert (!node->global.inlined_to);
1777 announce_function (decl);
1778 node->process = 0;
1779 assemble_thunks_and_aliases (node);
1780 gcc_assert (node->lowered);
1782 /* Generate RTL for the body of DECL. */
1783 tree_rest_of_compilation (decl);
1785 /* Make sure that BE didn't give up on compiling. */
1786 gcc_assert (TREE_ASM_WRITTEN (decl));
1787 current_function_decl = NULL;
1788 gcc_assert (!cgraph_preserve_function_body_p (node));
1789 cgraph_release_function_body (node);
1790 /* Eliminate all call edges. This is important so the GIMPLE_CALL no longer
1791 points to the dead function body. */
1792 cgraph_node_remove_callees (node);
1794 cgraph_function_flags_ready = true;
1797 /* Return true when CALLER_DECL should be inlined into CALLEE_DECL. */
1799 bool
1800 cgraph_inline_p (struct cgraph_edge *e, cgraph_inline_failed_t *reason)
1802 *reason = e->inline_failed;
1803 return !e->inline_failed;
1808 /* Expand all functions that must be output.
1810 Attempt to topologically sort the nodes so function is output when
1811 all called functions are already assembled to allow data to be
1812 propagated across the callgraph. Use a stack to get smaller distance
1813 between a function and its callees (later we may choose to use a more
1814 sophisticated algorithm for function reordering; we will likely want
1815 to use subsections to make the output functions appear in top-down
1816 order). */
1818 static void
1819 cgraph_expand_all_functions (void)
1821 struct cgraph_node *node;
1822 struct cgraph_node **order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1823 int order_pos, new_order_pos = 0;
1824 int i;
1826 order_pos = ipa_reverse_postorder (order);
1827 gcc_assert (order_pos == cgraph_n_nodes);
1829 /* Garbage collector may remove inline clones we eliminate during
1830 optimization. So we must be sure to not reference them. */
1831 for (i = 0; i < order_pos; i++)
1832 if (order[i]->process)
1833 order[new_order_pos++] = order[i];
1835 for (i = new_order_pos - 1; i >= 0; i--)
1837 node = order[i];
1838 if (node->process)
1840 gcc_assert (node->reachable);
1841 node->process = 0;
1842 cgraph_expand_function (node);
1845 cgraph_process_new_functions ();
1847 free (order);
1851 /* This is used to sort the node types by the cgraph order number. */
1853 enum cgraph_order_sort_kind
1855 ORDER_UNDEFINED = 0,
1856 ORDER_FUNCTION,
1857 ORDER_VAR,
1858 ORDER_ASM
1861 struct cgraph_order_sort
1863 enum cgraph_order_sort_kind kind;
1864 union
1866 struct cgraph_node *f;
1867 struct varpool_node *v;
1868 struct cgraph_asm_node *a;
1869 } u;
1872 /* Output all functions, variables, and asm statements in the order
1873 according to their order fields, which is the order in which they
1874 appeared in the file. This implements -fno-toplevel-reorder. In
1875 this mode we may output functions and variables which don't really
1876 need to be output. */
1878 static void
1879 cgraph_output_in_order (void)
1881 int max;
1882 struct cgraph_order_sort *nodes;
1883 int i;
1884 struct cgraph_node *pf;
1885 struct varpool_node *pv;
1886 struct cgraph_asm_node *pa;
1888 max = cgraph_order;
1889 nodes = XCNEWVEC (struct cgraph_order_sort, max);
1891 varpool_analyze_pending_decls ();
1893 for (pf = cgraph_nodes; pf; pf = pf->next)
1895 if (pf->process && !pf->thunk.thunk_p && !pf->alias)
1897 i = pf->order;
1898 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1899 nodes[i].kind = ORDER_FUNCTION;
1900 nodes[i].u.f = pf;
1904 for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1906 i = pv->order;
1907 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1908 nodes[i].kind = ORDER_VAR;
1909 nodes[i].u.v = pv;
1912 for (pa = cgraph_asm_nodes; pa; pa = pa->next)
1914 i = pa->order;
1915 gcc_assert (nodes[i].kind == ORDER_UNDEFINED);
1916 nodes[i].kind = ORDER_ASM;
1917 nodes[i].u.a = pa;
1920 /* In toplevel reorder mode we output all statics; mark them as needed. */
1921 for (i = 0; i < max; ++i)
1923 if (nodes[i].kind == ORDER_VAR)
1925 varpool_mark_needed_node (nodes[i].u.v);
1928 varpool_empty_needed_queue ();
1930 for (i = 0; i < max; ++i)
1931 if (nodes[i].kind == ORDER_VAR)
1932 varpool_finalize_named_section_flags (nodes[i].u.v);
1934 for (i = 0; i < max; ++i)
1936 switch (nodes[i].kind)
1938 case ORDER_FUNCTION:
1939 nodes[i].u.f->process = 0;
1940 cgraph_expand_function (nodes[i].u.f);
1941 break;
1943 case ORDER_VAR:
1944 varpool_assemble_decl (nodes[i].u.v);
1945 break;
1947 case ORDER_ASM:
1948 assemble_asm (nodes[i].u.a->asm_str);
1949 break;
1951 case ORDER_UNDEFINED:
1952 break;
1954 default:
1955 gcc_unreachable ();
1959 cgraph_asm_nodes = NULL;
1960 free (nodes);
1963 /* Return true when function body of DECL still needs to be kept around
1964 for later re-use. */
1965 bool
1966 cgraph_preserve_function_body_p (struct cgraph_node *node)
1968 gcc_assert (cgraph_global_info_ready);
1969 gcc_assert (!node->alias && !node->thunk.thunk_p);
1971 /* Look if there is any clone around. */
1972 if (node->clones)
1973 return true;
1974 return false;
1977 static void
1978 ipa_passes (void)
1980 set_cfun (NULL);
1981 current_function_decl = NULL;
1982 gimple_register_cfg_hooks ();
1983 bitmap_obstack_initialize (NULL);
1985 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_START, NULL);
1987 if (!in_lto_p)
1989 execute_ipa_pass_list (all_small_ipa_passes);
1990 if (seen_error ())
1991 return;
1994 /* If pass_all_early_optimizations was not scheduled, the state of
1995 the cgraph will not be properly updated. Update it now. */
1996 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
1997 cgraph_state = CGRAPH_STATE_IPA_SSA;
1999 if (!in_lto_p)
2001 /* Generate coverage variables and constructors. */
2002 coverage_finish ();
2004 /* Process new functions added. */
2005 set_cfun (NULL);
2006 current_function_decl = NULL;
2007 cgraph_process_new_functions ();
2009 execute_ipa_summary_passes
2010 ((struct ipa_opt_pass_d *) all_regular_ipa_passes);
2013 /* Some targets need to handle LTO assembler output specially. */
2014 if (flag_generate_lto)
2015 targetm.asm_out.lto_start ();
2017 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) all_lto_gen_passes);
2019 if (!in_lto_p)
2020 ipa_write_summaries ();
2022 if (flag_generate_lto)
2023 targetm.asm_out.lto_end ();
2025 if (!flag_ltrans)
2026 execute_ipa_pass_list (all_regular_ipa_passes);
2027 invoke_plugin_callbacks (PLUGIN_ALL_IPA_PASSES_END, NULL);
2029 bitmap_obstack_release (NULL);
2033 /* Perform simple optimizations based on callgraph. */
2035 void
2036 cgraph_optimize (void)
2038 if (seen_error ())
2039 return;
2041 #ifdef ENABLE_CHECKING
2042 verify_cgraph ();
2043 #endif
2045 /* Frontend may output common variables after the unit has been finalized.
2046 It is safe to deal with them here as they are always zero initialized. */
2047 varpool_analyze_pending_decls ();
2049 timevar_push (TV_CGRAPHOPT);
2050 if (pre_ipa_mem_report)
2052 fprintf (stderr, "Memory consumption before IPA\n");
2053 dump_memory_report (false);
2055 if (!quiet_flag)
2056 fprintf (stderr, "Performing interprocedural optimizations\n");
2057 cgraph_state = CGRAPH_STATE_IPA;
2059 /* Don't run the IPA passes if there was any error or sorry messages. */
2060 if (!seen_error ())
2061 ipa_passes ();
2063 /* Do nothing else if any IPA pass found errors. */
2064 if (seen_error ())
2066 timevar_pop (TV_CGRAPHOPT);
2067 return;
2070 /* This pass remove bodies of extern inline functions we never inlined.
2071 Do this later so other IPA passes see what is really going on. */
2072 cgraph_remove_unreachable_nodes (false, dump_file);
2073 cgraph_global_info_ready = true;
2074 if (cgraph_dump_file)
2076 fprintf (cgraph_dump_file, "Optimized ");
2077 dump_cgraph (cgraph_dump_file);
2078 dump_varpool (cgraph_dump_file);
2080 if (post_ipa_mem_report)
2082 fprintf (stderr, "Memory consumption after IPA\n");
2083 dump_memory_report (false);
2085 timevar_pop (TV_CGRAPHOPT);
2087 /* Output everything. */
2088 (*debug_hooks->assembly_start) ();
2089 if (!quiet_flag)
2090 fprintf (stderr, "Assembling functions:\n");
2091 #ifdef ENABLE_CHECKING
2092 verify_cgraph ();
2093 #endif
2095 cgraph_materialize_all_clones ();
2096 cgraph_mark_functions_to_output ();
2098 cgraph_state = CGRAPH_STATE_EXPANSION;
2099 if (!flag_toplevel_reorder)
2100 cgraph_output_in_order ();
2101 else
2103 cgraph_output_pending_asms ();
2105 cgraph_expand_all_functions ();
2106 varpool_remove_unreferenced_decls ();
2108 varpool_assemble_pending_decls ();
2110 cgraph_process_new_functions ();
2111 cgraph_state = CGRAPH_STATE_FINISHED;
2113 if (cgraph_dump_file)
2115 fprintf (cgraph_dump_file, "\nFinal ");
2116 dump_cgraph (cgraph_dump_file);
2117 dump_varpool (cgraph_dump_file);
2119 #ifdef ENABLE_CHECKING
2120 verify_cgraph ();
2121 /* Double check that all inline clones are gone and that all
2122 function bodies have been released from memory. */
2123 if (!seen_error ())
2125 struct cgraph_node *node;
2126 bool error_found = false;
2128 for (node = cgraph_nodes; node; node = node->next)
2129 if (node->analyzed
2130 && (node->global.inlined_to
2131 || gimple_has_body_p (node->decl)))
2133 error_found = true;
2134 dump_cgraph_node (stderr, node);
2136 if (error_found)
2137 internal_error ("nodes with unreleased memory found");
2139 #endif
2142 void
2143 init_cgraph (void)
2145 if (!cgraph_dump_file)
2146 cgraph_dump_file = dump_begin (TDI_cgraph, NULL);
2149 /* The edges representing the callers of the NEW_VERSION node were
2150 fixed by cgraph_function_versioning (), now the call_expr in their
2151 respective tree code should be updated to call the NEW_VERSION. */
2153 static void
2154 update_call_expr (struct cgraph_node *new_version)
2156 struct cgraph_edge *e;
2158 gcc_assert (new_version);
2160 /* Update the call expr on the edges to call the new version. */
2161 for (e = new_version->callers; e; e = e->next_caller)
2163 struct function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
2164 gimple_call_set_fndecl (e->call_stmt, new_version->decl);
2165 maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
2170 /* Create a new cgraph node which is the new version of
2171 OLD_VERSION node. REDIRECT_CALLERS holds the callers
2172 edges which should be redirected to point to
2173 NEW_VERSION. ALL the callees edges of OLD_VERSION
2174 are cloned to the new version node. Return the new
2175 version node.
2177 If non-NULL BLOCK_TO_COPY determine what basic blocks
2178 was copied to prevent duplications of calls that are dead
2179 in the clone. */
2181 static struct cgraph_node *
2182 cgraph_copy_node_for_versioning (struct cgraph_node *old_version,
2183 tree new_decl,
2184 VEC(cgraph_edge_p,heap) *redirect_callers,
2185 bitmap bbs_to_copy)
2187 struct cgraph_node *new_version;
2188 struct cgraph_edge *e;
2189 unsigned i;
2191 gcc_assert (old_version);
2193 new_version = cgraph_create_node (new_decl);
2195 new_version->analyzed = true;
2196 new_version->local = old_version->local;
2197 new_version->local.externally_visible = false;
2198 new_version->local.local = true;
2199 new_version->global = old_version->global;
2200 new_version->rtl = old_version->rtl;
2201 new_version->reachable = true;
2202 new_version->count = old_version->count;
2204 for (e = old_version->callees; e; e=e->next_callee)
2205 if (!bbs_to_copy
2206 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
2207 cgraph_clone_edge (e, new_version, e->call_stmt,
2208 e->lto_stmt_uid, REG_BR_PROB_BASE,
2209 CGRAPH_FREQ_BASE,
2210 true);
2211 for (e = old_version->indirect_calls; e; e=e->next_callee)
2212 if (!bbs_to_copy
2213 || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
2214 cgraph_clone_edge (e, new_version, e->call_stmt,
2215 e->lto_stmt_uid, REG_BR_PROB_BASE,
2216 CGRAPH_FREQ_BASE,
2217 true);
2218 FOR_EACH_VEC_ELT (cgraph_edge_p, redirect_callers, i, e)
2220 /* Redirect calls to the old version node to point to its new
2221 version. */
2222 cgraph_redirect_edge_callee (e, new_version);
2225 return new_version;
2228 /* Perform function versioning.
2229 Function versioning includes copying of the tree and
2230 a callgraph update (creating a new cgraph node and updating
2231 its callees and callers).
2233 REDIRECT_CALLERS varray includes the edges to be redirected
2234 to the new version.
2236 TREE_MAP is a mapping of tree nodes we want to replace with
2237 new ones (according to results of prior analysis).
2238 OLD_VERSION_NODE is the node that is versioned.
2239 It returns the new version's cgraph node.
2240 If non-NULL ARGS_TO_SKIP determine function parameters to remove
2241 from new version.
2242 If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
2243 If non_NULL NEW_ENTRY determine new entry BB of the clone. */
2245 struct cgraph_node *
2246 cgraph_function_versioning (struct cgraph_node *old_version_node,
2247 VEC(cgraph_edge_p,heap) *redirect_callers,
2248 VEC (ipa_replace_map_p,gc)* tree_map,
2249 bitmap args_to_skip,
2250 bitmap bbs_to_copy,
2251 basic_block new_entry_block,
2252 const char *clone_name)
2254 tree old_decl = old_version_node->decl;
2255 struct cgraph_node *new_version_node = NULL;
2256 tree new_decl;
2258 if (!tree_versionable_function_p (old_decl))
2259 return NULL;
2261 gcc_assert (old_version_node->local.can_change_signature || !args_to_skip);
2263 /* Make a new FUNCTION_DECL tree node for the
2264 new version. */
2265 if (!args_to_skip)
2266 new_decl = copy_node (old_decl);
2267 else
2268 new_decl = build_function_decl_skip_args (old_decl, args_to_skip);
2270 /* Generate a new name for the new version. */
2271 DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
2272 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
2273 SET_DECL_RTL (new_decl, NULL);
2275 /* Create the new version's call-graph node.
2276 and update the edges of the new node. */
2277 new_version_node =
2278 cgraph_copy_node_for_versioning (old_version_node, new_decl,
2279 redirect_callers, bbs_to_copy);
2281 /* Copy the OLD_VERSION_NODE function tree to the new version. */
2282 tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
2283 bbs_to_copy, new_entry_block);
2285 /* Update the new version's properties.
2286 Make The new version visible only within this translation unit. Make sure
2287 that is not weak also.
2288 ??? We cannot use COMDAT linkage because there is no
2289 ABI support for this. */
2290 cgraph_make_decl_local (new_version_node->decl);
2291 DECL_VIRTUAL_P (new_version_node->decl) = 0;
2292 new_version_node->local.externally_visible = 0;
2293 new_version_node->local.local = 1;
2294 new_version_node->lowered = true;
2296 /* Update the call_expr on the edges to call the new version node. */
2297 update_call_expr (new_version_node);
2299 cgraph_call_function_insertion_hooks (new_version_node);
2300 return new_version_node;
2303 /* Given virtual clone, turn it into actual clone. */
2304 static void
2305 cgraph_materialize_clone (struct cgraph_node *node)
2307 bitmap_obstack_initialize (NULL);
2308 node->former_clone_of = node->clone_of->decl;
2309 if (node->clone_of->former_clone_of)
2310 node->former_clone_of = node->clone_of->former_clone_of;
2311 /* Copy the OLD_VERSION_NODE function tree to the new version. */
2312 tree_function_versioning (node->clone_of->decl, node->decl,
2313 node->clone.tree_map, true,
2314 node->clone.args_to_skip, NULL, NULL);
2315 if (cgraph_dump_file)
2317 dump_function_to_file (node->clone_of->decl, cgraph_dump_file, dump_flags);
2318 dump_function_to_file (node->decl, cgraph_dump_file, dump_flags);
2321 /* Function is no longer clone. */
2322 if (node->next_sibling_clone)
2323 node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
2324 if (node->prev_sibling_clone)
2325 node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
2326 else
2327 node->clone_of->clones = node->next_sibling_clone;
2328 node->next_sibling_clone = NULL;
2329 node->prev_sibling_clone = NULL;
2330 if (!node->clone_of->analyzed && !node->clone_of->clones)
2332 cgraph_release_function_body (node->clone_of);
2333 cgraph_node_remove_callees (node->clone_of);
2334 ipa_remove_all_references (&node->clone_of->ref_list);
2336 node->clone_of = NULL;
2337 bitmap_obstack_release (NULL);
2340 /* If necessary, change the function declaration in the call statement
2341 associated with E so that it corresponds to the edge callee. */
2343 gimple
2344 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *e)
2346 tree decl = gimple_call_fndecl (e->call_stmt);
2347 gimple new_stmt;
2348 gimple_stmt_iterator gsi;
2349 bool gsi_computed = false;
2350 #ifdef ENABLE_CHECKING
2351 struct cgraph_node *node;
2352 #endif
2354 if (e->indirect_unknown_callee
2355 || decl == e->callee->decl
2356 /* Don't update call from same body alias to the real function. */
2357 || (decl && cgraph_get_node (decl) == cgraph_get_node (e->callee->decl)))
2358 return e->call_stmt;
2360 #ifdef ENABLE_CHECKING
2361 if (decl)
2363 node = cgraph_get_node (decl);
2364 gcc_assert (!node || !node->clone.combined_args_to_skip);
2366 #endif
2368 if (cgraph_dump_file)
2370 fprintf (cgraph_dump_file, "updating call of %s/%i -> %s/%i: ",
2371 cgraph_node_name (e->caller), e->caller->uid,
2372 cgraph_node_name (e->callee), e->callee->uid);
2373 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
2374 if (e->callee->clone.combined_args_to_skip)
2376 fprintf (cgraph_dump_file, " combined args to skip: ");
2377 dump_bitmap (cgraph_dump_file,
2378 e->callee->clone.combined_args_to_skip);
2382 if (e->indirect_info &&
2383 e->indirect_info->thunk_delta != 0
2384 && (!e->callee->clone.combined_args_to_skip
2385 || !bitmap_bit_p (e->callee->clone.combined_args_to_skip, 0)))
2387 if (cgraph_dump_file)
2388 fprintf (cgraph_dump_file, " Thunk delta is "
2389 HOST_WIDE_INT_PRINT_DEC "\n", e->indirect_info->thunk_delta);
2390 gsi = gsi_for_stmt (e->call_stmt);
2391 gsi_computed = true;
2392 gimple_adjust_this_by_delta (&gsi,
2393 build_int_cst (sizetype,
2394 e->indirect_info->thunk_delta));
2395 e->indirect_info->thunk_delta = 0;
2398 if (e->callee->clone.combined_args_to_skip)
2400 int lp_nr;
2402 new_stmt
2403 = gimple_call_copy_skip_args (e->call_stmt,
2404 e->callee->clone.combined_args_to_skip);
2405 gimple_call_set_fndecl (new_stmt, e->callee->decl);
2407 if (gimple_vdef (new_stmt)
2408 && TREE_CODE (gimple_vdef (new_stmt)) == SSA_NAME)
2409 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
2411 if (!gsi_computed)
2412 gsi = gsi_for_stmt (e->call_stmt);
2413 gsi_replace (&gsi, new_stmt, false);
2414 /* We need to defer cleaning EH info on the new statement to
2415 fixup-cfg. We may not have dominator information at this point
2416 and thus would end up with unreachable blocks and have no way
2417 to communicate that we need to run CFG cleanup then. */
2418 lp_nr = lookup_stmt_eh_lp (e->call_stmt);
2419 if (lp_nr != 0)
2421 remove_stmt_from_eh_lp (e->call_stmt);
2422 add_stmt_to_eh_lp (new_stmt, lp_nr);
2425 else
2427 new_stmt = e->call_stmt;
2428 gimple_call_set_fndecl (new_stmt, e->callee->decl);
2429 update_stmt (new_stmt);
2432 cgraph_set_call_stmt_including_clones (e->caller, e->call_stmt, new_stmt);
2434 if (cgraph_dump_file)
2436 fprintf (cgraph_dump_file, " updated to:");
2437 print_gimple_stmt (cgraph_dump_file, e->call_stmt, 0, dump_flags);
2439 return new_stmt;
2442 /* Once all functions from compilation unit are in memory, produce all clones
2443 and update all calls. We might also do this on demand if we don't want to
2444 bring all functions to memory prior compilation, but current WHOPR
2445 implementation does that and it is is bit easier to keep everything right in
2446 this order. */
2447 void
2448 cgraph_materialize_all_clones (void)
2450 struct cgraph_node *node;
2451 bool stabilized = false;
2453 if (cgraph_dump_file)
2454 fprintf (cgraph_dump_file, "Materializing clones\n");
2455 #ifdef ENABLE_CHECKING
2456 verify_cgraph ();
2457 #endif
2459 /* We can also do topological order, but number of iterations should be
2460 bounded by number of IPA passes since single IPA pass is probably not
2461 going to create clones of clones it created itself. */
2462 while (!stabilized)
2464 stabilized = true;
2465 for (node = cgraph_nodes; node; node = node->next)
2467 if (node->clone_of && node->decl != node->clone_of->decl
2468 && !gimple_has_body_p (node->decl))
2470 if (gimple_has_body_p (node->clone_of->decl))
2472 if (cgraph_dump_file)
2474 fprintf (cgraph_dump_file, "cloning %s to %s\n",
2475 cgraph_node_name (node->clone_of),
2476 cgraph_node_name (node));
2477 if (node->clone.tree_map)
2479 unsigned int i;
2480 fprintf (cgraph_dump_file, " replace map: ");
2481 for (i = 0; i < VEC_length (ipa_replace_map_p,
2482 node->clone.tree_map);
2483 i++)
2485 struct ipa_replace_map *replace_info;
2486 replace_info = VEC_index (ipa_replace_map_p,
2487 node->clone.tree_map,
2489 print_generic_expr (cgraph_dump_file, replace_info->old_tree, 0);
2490 fprintf (cgraph_dump_file, " -> ");
2491 print_generic_expr (cgraph_dump_file, replace_info->new_tree, 0);
2492 fprintf (cgraph_dump_file, "%s%s;",
2493 replace_info->replace_p ? "(replace)":"",
2494 replace_info->ref_p ? "(ref)":"");
2496 fprintf (cgraph_dump_file, "\n");
2498 if (node->clone.args_to_skip)
2500 fprintf (cgraph_dump_file, " args_to_skip: ");
2501 dump_bitmap (cgraph_dump_file, node->clone.args_to_skip);
2503 if (node->clone.args_to_skip)
2505 fprintf (cgraph_dump_file, " combined_args_to_skip:");
2506 dump_bitmap (cgraph_dump_file, node->clone.combined_args_to_skip);
2509 cgraph_materialize_clone (node);
2510 stabilized = false;
2515 for (node = cgraph_nodes; node; node = node->next)
2516 if (!node->analyzed && node->callees)
2517 cgraph_node_remove_callees (node);
2518 if (cgraph_dump_file)
2519 fprintf (cgraph_dump_file, "Materialization Call site updates done.\n");
2520 #ifdef ENABLE_CHECKING
2521 verify_cgraph ();
2522 #endif
2523 cgraph_remove_unreachable_nodes (false, cgraph_dump_file);
2526 #include "gt-cgraphunit.h"