1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "tree-pass.h"
31 #include "pointer-set.h"
33 #include "tree-iterator.h"
34 #include "ipa-utils.h"
35 #include "pointer-set.h"
37 /* Look for all functions inlined to NODE and update their inlined_to pointers
41 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
43 struct cgraph_edge
*e
;
44 for (e
= node
->callees
; e
; e
= e
->next_callee
)
45 if (e
->callee
->global
.inlined_to
)
47 e
->callee
->global
.inlined_to
= inlined_to
;
48 update_inlined_to_pointer (e
->callee
, inlined_to
);
52 /* Add cgraph NODE to queue starting at FIRST.
54 The queue is linked via AUX pointers and terminated by pointer to 1.
55 We enqueue nodes at two occasions: when we find them reachable or when we find
56 their bodies needed for further clonning. In the second case we mark them
57 by pointer to 2 after processing so they are re-queue when they become
61 enqueue_cgraph_node (struct cgraph_node
*node
, struct cgraph_node
**first
,
62 struct pointer_set_t
*reachable
)
64 /* Node is still in queue; do nothing. */
65 if (node
->symbol
.aux
&& node
->symbol
.aux
!= (void *) 2)
67 /* Node was already processed as unreachable, re-enqueue
68 only if it became reachable now. */
69 if (node
->symbol
.aux
== (void *)2 && !pointer_set_contains (reachable
, node
))
71 node
->symbol
.aux
= *first
;
75 /* Add varpool NODE to queue starting at FIRST. */
78 enqueue_varpool_node (struct varpool_node
*node
, struct varpool_node
**first
)
80 node
->symbol
.aux
= *first
;
84 /* Process references. */
87 process_references (struct ipa_ref_list
*list
,
88 struct cgraph_node
**first
,
89 struct varpool_node
**first_varpool
,
90 bool before_inlining_p
,
91 struct pointer_set_t
*reachable
)
95 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
97 if (symtab_function_p (ref
->referred
))
99 struct cgraph_node
*node
= ipa_ref_node (ref
);
101 && (!DECL_EXTERNAL (node
->symbol
.decl
)
103 || before_inlining_p
))
104 pointer_set_insert (reachable
, node
);
105 enqueue_cgraph_node (node
, first
, reachable
);
109 struct varpool_node
*node
= ipa_ref_varpool_node (ref
);
110 if (!pointer_set_insert (reachable
, node
))
111 enqueue_varpool_node (node
, first_varpool
);
117 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
120 cgraph_non_local_node_p_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
122 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
123 return !(cgraph_only_called_directly_or_aliased_p (node
)
124 && !ipa_ref_has_aliases_p (&node
->symbol
.ref_list
)
126 && !DECL_EXTERNAL (node
->symbol
.decl
)
127 && !node
->symbol
.externally_visible
128 && !node
->symbol
.used_from_other_partition
129 && !node
->symbol
.in_other_partition
);
132 /* Return true when function can be marked local. */
135 cgraph_local_node_p (struct cgraph_node
*node
)
137 struct cgraph_node
*n
= cgraph_function_or_thunk_node (node
, NULL
);
139 /* FIXME: thunks can be considered local, but we need prevent i386
140 from attempting to change calling convention of them. */
141 if (n
->thunk
.thunk_p
)
143 return !cgraph_for_node_and_aliases (n
,
144 cgraph_non_local_node_p_1
, NULL
, true);
148 /* Return true when NODE has ADDR reference. */
151 has_addr_references_p (struct cgraph_node
*node
,
152 void *data ATTRIBUTE_UNUSED
)
157 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
159 if (ref
->use
== IPA_REF_ADDR
)
164 /* Perform reachability analysis and reclaim all unreachable nodes.
165 If BEFORE_INLINING_P is true this function is called before inlining
166 decisions has been made. If BEFORE_INLINING_P is false this function also
167 removes unneeded bodies of extern inline functions. */
170 cgraph_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
172 struct cgraph_node
*first
= (struct cgraph_node
*) (void *) 1;
173 struct varpool_node
*first_varpool
= (struct varpool_node
*) (void *) 1;
174 struct cgraph_node
*node
, *next
;
175 struct varpool_node
*vnode
, *vnext
;
176 bool changed
= false;
177 struct pointer_set_t
*reachable
= pointer_set_create ();
179 #ifdef ENABLE_CHECKING
183 fprintf (file
, "\nReclaiming functions:");
184 #ifdef ENABLE_CHECKING
185 FOR_EACH_FUNCTION (node
)
186 gcc_assert (!node
->symbol
.aux
);
187 FOR_EACH_VARIABLE (vnode
)
188 gcc_assert (!vnode
->symbol
.aux
);
190 /* Mark functions whose bodies are obviously needed.
191 This is mostly when they can be referenced externally. Inline clones
192 are special since their declarations are shared with master clone and thus
193 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
194 FOR_EACH_FUNCTION (node
)
195 if (node
->analyzed
&& !node
->global
.inlined_to
196 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node
)
197 /* Keep around virtual functions for possible devirtualization. */
198 || (before_inlining_p
199 && DECL_VIRTUAL_P (node
->symbol
.decl
)
200 && (DECL_COMDAT (node
->symbol
.decl
) || DECL_EXTERNAL (node
->symbol
.decl
)))))
202 gcc_assert (!node
->global
.inlined_to
);
203 enqueue_cgraph_node (node
, &first
, reachable
);
204 pointer_set_insert (reachable
, node
);
207 gcc_assert (!node
->symbol
.aux
);
209 /* Mark variables that are obviously needed. */
210 FOR_EACH_VARIABLE (vnode
)
212 if ((vnode
->analyzed
|| vnode
->symbol
.force_output
)
213 && !varpool_can_remove_if_no_refs (vnode
))
215 pointer_set_insert (reachable
, vnode
);
216 enqueue_varpool_node (vnode
, &first_varpool
);
220 /* Perform reachability analysis. As a special case do not consider
221 extern inline functions not inlined as live because we won't output
224 We maintain two worklist, one for cgraph nodes other for varpools and
225 are finished once both are empty. */
227 while (first
!= (struct cgraph_node
*) (void *) 1
228 || first_varpool
!= (struct varpool_node
*) (void *) 1)
230 if (first
!= (struct cgraph_node
*) (void *) 1)
232 struct cgraph_edge
*e
;
234 first
= (struct cgraph_node
*) first
->symbol
.aux
;
235 if (!pointer_set_contains (reachable
, node
))
236 node
->symbol
.aux
= (void *)2;
237 /* If we found this node reachable, first mark on the callees
238 reachable too, unless they are direct calls to extern inline functions
239 we decided to not inline. */
242 for (e
= node
->callees
; e
; e
= e
->next_callee
)
245 && (!e
->inline_failed
246 || !DECL_EXTERNAL (e
->callee
->symbol
.decl
)
248 || before_inlining_p
))
249 pointer_set_insert (reachable
, e
->callee
);
250 enqueue_cgraph_node (e
->callee
, &first
, reachable
);
252 process_references (&node
->symbol
.ref_list
, &first
,
253 &first_varpool
, before_inlining_p
,
256 /* If any function in a comdat group is reachable, force
257 all other functions in the same comdat group to be
259 if (node
->symbol
.same_comdat_group
260 && !node
->global
.inlined_to
)
262 for (next
= cgraph (node
->symbol
.same_comdat_group
);
264 next
= cgraph (next
->symbol
.same_comdat_group
))
265 if (!pointer_set_insert (reachable
, next
))
266 enqueue_cgraph_node (next
, &first
, reachable
);
270 /* We can freely remove inline clones even if they are cloned, however if
271 function is clone of real clone, we must keep it around in order to
272 make materialize_clones produce function body with the changes
274 while (node
->clone_of
&& !node
->clone_of
->symbol
.aux
275 && !gimple_has_body_p (node
->symbol
.decl
))
277 bool noninline
= node
->clone_of
->symbol
.decl
!= node
->symbol
.decl
;
278 node
= node
->clone_of
;
279 if (noninline
&& !pointer_set_insert (reachable
, node
) && !node
->symbol
.aux
)
281 enqueue_cgraph_node (node
, &first
, reachable
);
286 if (first_varpool
!= (struct varpool_node
*) (void *) 1)
288 vnode
= first_varpool
;
289 first_varpool
= (struct varpool_node
*)first_varpool
->symbol
.aux
;
290 vnode
->symbol
.aux
= NULL
;
291 process_references (&vnode
->symbol
.ref_list
, &first
,
292 &first_varpool
, before_inlining_p
,
294 /* If any function in a comdat group is reachable, force
295 all other functions in the same comdat group to be
297 if (vnode
->symbol
.same_comdat_group
)
299 struct varpool_node
*next
;
300 for (next
= varpool (vnode
->symbol
.same_comdat_group
);
302 next
= varpool (next
->symbol
.same_comdat_group
))
303 if (!pointer_set_insert (reachable
, next
))
304 enqueue_varpool_node (next
, &first_varpool
);
309 /* Remove unreachable nodes.
311 Completely unreachable functions can be fully removed from the callgraph.
312 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
313 callgraph (so we still have edges to them). We remove function body then.
315 Also we need to care functions that are unreachable but we need to keep them around
316 for later clonning. In this case we also turn them to unanalyzed nodes, but
317 keep the body around. */
318 for (node
= cgraph_first_function (); node
; node
= next
)
320 next
= cgraph_next_function (node
);
321 if (node
->symbol
.aux
&& !pointer_set_contains (reachable
, node
))
323 cgraph_node_remove_callees (node
);
324 ipa_remove_all_references (&node
->symbol
.ref_list
);
325 node
->analyzed
= false;
327 if (!node
->symbol
.aux
)
329 struct cgraph_edge
*e
;
334 node
->global
.inlined_to
= NULL
;
336 fprintf (file
, " %s", cgraph_node_name (node
));
337 /* See if there is reachable caller. */
338 for (e
= node
->callers
; e
&& !found
; e
= e
->next_caller
)
339 if (pointer_set_contains (reachable
, e
->caller
))
341 for (i
= 0; (ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
344 if (pointer_set_contains (reachable
, ref
->referring
))
347 /* If so, we need to keep node in the callgraph. */
352 struct cgraph_node
*clone
;
354 /* If there are still clones, we must keep body around.
355 Otherwise we can just remove the body but keep the clone. */
356 for (clone
= node
->clones
; clone
;
357 clone
= clone
->next_sibling_clone
)
358 if (clone
->symbol
.aux
)
362 cgraph_release_function_body (node
);
363 if (node
->prev_sibling_clone
)
364 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
365 else if (node
->clone_of
)
366 node
->clone_of
->clones
= node
->next_sibling_clone
;
367 if (node
->next_sibling_clone
)
368 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
370 node
->former_clone_of
= node
->clone_of
->symbol
.decl
;
371 node
->clone_of
= NULL
;
372 node
->next_sibling_clone
= NULL
;
373 node
->prev_sibling_clone
= NULL
;
376 gcc_assert (!clone
->symbol
.in_other_partition
);
377 node
->analyzed
= false;
379 cgraph_node_remove_callees (node
);
380 ipa_remove_all_references (&node
->symbol
.ref_list
);
385 cgraph_remove_node (node
);
390 FOR_EACH_FUNCTION (node
)
392 /* Inline clones might be kept around so their materializing allows further
393 cloning. If the function the clone is inlined into is removed, we need
394 to turn it into normal cone. */
395 if (node
->global
.inlined_to
398 gcc_assert (node
->clones
);
399 node
->global
.inlined_to
= NULL
;
400 update_inlined_to_pointer (node
, node
);
402 node
->symbol
.aux
= NULL
;
406 fprintf (file
, "\n");
409 fprintf (file
, "Reclaiming variables:");
410 for (vnode
= varpool_first_variable (); vnode
; vnode
= vnext
)
412 vnext
= varpool_next_variable (vnode
);
413 if (!pointer_set_contains (reachable
, vnode
))
416 fprintf (file
, " %s", varpool_node_name (vnode
));
417 varpool_remove_node (vnode
);
422 /* Now update address_taken flags and try to promote functions to be local. */
425 fprintf (file
, "\nClearing address taken flags:");
426 FOR_EACH_DEFINED_FUNCTION (node
)
427 if (node
->symbol
.address_taken
428 && !node
->symbol
.used_from_other_partition
)
430 if (!cgraph_for_node_and_aliases (node
, has_addr_references_p
, NULL
, true))
433 fprintf (file
, " %s", cgraph_node_name (node
));
434 node
->symbol
.address_taken
= false;
436 if (cgraph_local_node_p (node
))
438 node
->local
.local
= true;
440 fprintf (file
, " (local)");
445 fprintf (file
, "\n");
447 /* Rest of transformations are undesirable at -O0. */
451 #ifdef ENABLE_CHECKING
455 /* Reclaim alias pairs for functions that have disappeared from the
457 remove_unreachable_alias_pairs ();
458 pointer_set_destroy (reachable
);
463 /* Discover variables that have no longer address taken or that are read only
464 and update their flags.
466 FIXME: This can not be done in between gimplify and omp_expand since
467 readonly flag plays role on what is shared and what is not. Currently we do
468 this transformation as part of whole program visibility and re-do at
469 ipa-reference pass (to take into account clonning), but it would
470 make sense to do it before early optimizations. */
473 ipa_discover_readonly_nonaddressable_vars (void)
475 struct varpool_node
*vnode
;
477 fprintf (dump_file
, "Clearing variable flags:");
478 FOR_EACH_VARIABLE (vnode
)
479 if (vnode
->finalized
&& varpool_all_refs_explicit_p (vnode
)
480 && (TREE_ADDRESSABLE (vnode
->symbol
.decl
)
481 || !TREE_READONLY (vnode
->symbol
.decl
)))
483 bool written
= false;
484 bool address_taken
= false;
487 for (i
= 0; ipa_ref_list_referring_iterate (&vnode
->symbol
.ref_list
,
489 && (!written
|| !address_taken
); i
++)
493 address_taken
= true;
501 if (TREE_ADDRESSABLE (vnode
->symbol
.decl
) && !address_taken
)
504 fprintf (dump_file
, " %s (addressable)", varpool_node_name (vnode
));
505 TREE_ADDRESSABLE (vnode
->symbol
.decl
) = 0;
507 if (!TREE_READONLY (vnode
->symbol
.decl
) && !address_taken
&& !written
508 /* Making variable in explicit section readonly can cause section
510 See e.g. gcc.c-torture/compile/pr23237.c */
511 && DECL_SECTION_NAME (vnode
->symbol
.decl
) == NULL
)
514 fprintf (dump_file
, " %s (read-only)", varpool_node_name (vnode
));
515 TREE_READONLY (vnode
->symbol
.decl
) = 1;
519 fprintf (dump_file
, "\n");
522 /* Return true when there is a reference to node and it is not vtable. */
524 cgraph_address_taken_from_non_vtable_p (struct cgraph_node
*node
)
528 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
530 if (ref
->use
== IPA_REF_ADDR
)
532 struct varpool_node
*node
;
533 if (symtab_function_p (ref
->referring
))
535 node
= ipa_ref_referring_varpool_node (ref
);
536 if (!DECL_VIRTUAL_P (node
->symbol
.decl
))
542 /* COMDAT functions must be shared only if they have address taken,
543 otherwise we can produce our own private implementation with
545 Return true when turning COMDAT functoin static can not lead to wrong
546 code when the resulting object links with a library defining same COMDAT.
548 Virtual functions do have their addresses taken from the vtables,
549 but in C++ there is no way to compare their addresses for equality. */
552 cgraph_comdat_can_be_unshared_p (struct cgraph_node
*node
)
554 if ((cgraph_address_taken_from_non_vtable_p (node
)
555 && !DECL_VIRTUAL_P (node
->symbol
.decl
))
558 if (node
->symbol
.same_comdat_group
)
560 struct cgraph_node
*next
;
562 /* If more than one function is in the same COMDAT group, it must
563 be shared even if just one function in the comdat group has
565 for (next
= cgraph (node
->symbol
.same_comdat_group
);
566 next
!= node
; next
= cgraph (next
->symbol
.same_comdat_group
))
567 if (cgraph_address_taken_from_non_vtable_p (next
)
568 && !DECL_VIRTUAL_P (next
->symbol
.decl
))
574 /* Return true when function NODE should be considered externally visible. */
577 cgraph_externally_visible_p (struct cgraph_node
*node
,
578 bool whole_program
, bool aliased
)
580 if (!node
->local
.finalized
)
582 if (!DECL_COMDAT (node
->symbol
.decl
)
583 && (!TREE_PUBLIC (node
->symbol
.decl
)
584 || DECL_EXTERNAL (node
->symbol
.decl
)))
587 /* Do not even try to be smart about aliased nodes. Until we properly
588 represent everything by same body alias, these are just evil. */
592 /* Do not try to localize built-in functions yet. One of problems is that we
593 end up mangling their asm for WHOPR that makes it impossible to call them
594 using the implicit built-in declarations anymore. Similarly this enables
595 us to remove them as unreachable before actual calls may appear during
596 expansion or folding. */
597 if (DECL_BUILT_IN (node
->symbol
.decl
))
600 /* If linker counts on us, we must preserve the function. */
601 if (cgraph_used_from_object_file_p (node
))
603 if (DECL_PRESERVE_P (node
->symbol
.decl
))
605 if (lookup_attribute ("externally_visible",
606 DECL_ATTRIBUTES (node
->symbol
.decl
)))
608 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
609 && lookup_attribute ("dllexport",
610 DECL_ATTRIBUTES (node
->symbol
.decl
)))
612 if (node
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
)
614 /* When doing LTO or whole program, we can bring COMDAT functoins static.
615 This improves code quality and we know we will duplicate them at most twice
616 (in the case that we are not using plugin and link with object file
617 implementing same COMDAT) */
618 if ((in_lto_p
|| whole_program
)
619 && DECL_COMDAT (node
->symbol
.decl
)
620 && cgraph_comdat_can_be_unshared_p (node
))
623 /* When doing link time optimizations, hidden symbols become local. */
625 && (DECL_VISIBILITY (node
->symbol
.decl
) == VISIBILITY_HIDDEN
626 || DECL_VISIBILITY (node
->symbol
.decl
) == VISIBILITY_INTERNAL
)
627 /* Be sure that node is defined in IR file, not in other object
628 file. In that case we don't set used_from_other_object_file. */
631 else if (!whole_program
)
634 if (MAIN_NAME_P (DECL_NAME (node
->symbol
.decl
)))
640 /* Return true when variable VNODE should be considered externally visible. */
643 varpool_externally_visible_p (struct varpool_node
*vnode
, bool aliased
)
645 /* Do not touch weakrefs; while they are not externally visible,
646 dropping their DECL_EXTERNAL flags confuse most
647 of code handling them. */
648 if (vnode
->alias
&& DECL_EXTERNAL (vnode
->symbol
.decl
))
651 if (!DECL_COMDAT (vnode
->symbol
.decl
) && !TREE_PUBLIC (vnode
->symbol
.decl
))
654 /* Do not even try to be smart about aliased nodes. Until we properly
655 represent everything by same body alias, these are just evil. */
659 /* If linker counts on us, we must preserve the function. */
660 if (varpool_used_from_object_file_p (vnode
))
663 if (DECL_HARD_REGISTER (vnode
->symbol
.decl
))
665 if (DECL_PRESERVE_P (vnode
->symbol
.decl
))
667 if (lookup_attribute ("externally_visible",
668 DECL_ATTRIBUTES (vnode
->symbol
.decl
)))
670 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
671 && lookup_attribute ("dllexport",
672 DECL_ATTRIBUTES (vnode
->symbol
.decl
)))
675 /* See if we have linker information about symbol not being used or
676 if we need to make guess based on the declaration.
678 Even if the linker clams the symbol is unused, never bring internal
679 symbols that are declared by user as used or externally visible.
680 This is needed for i.e. references from asm statements. */
681 if (varpool_used_from_object_file_p (vnode
))
683 if (vnode
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
)
686 /* As a special case, the COMDAT virutal tables can be unshared.
687 In LTO mode turn vtables into static variables. The variable is readonly,
688 so this does not enable more optimization, but referring static var
689 is faster for dynamic linking. Also this match logic hidding vtables
690 from LTO symbol tables. */
691 if ((in_lto_p
|| flag_whole_program
)
692 && !vnode
->symbol
.force_output
693 && DECL_COMDAT (vnode
->symbol
.decl
) && DECL_VIRTUAL_P (vnode
->symbol
.decl
))
696 /* When doing link time optimizations, hidden symbols become local. */
698 && (DECL_VISIBILITY (vnode
->symbol
.decl
) == VISIBILITY_HIDDEN
699 || DECL_VISIBILITY (vnode
->symbol
.decl
) == VISIBILITY_INTERNAL
)
700 /* Be sure that node is defined in IR file, not in other object
701 file. In that case we don't set used_from_other_object_file. */
704 else if (!flag_whole_program
)
707 /* Do not attempt to privatize COMDATS by default.
708 This would break linking with C++ libraries sharing
711 FIXME: We can do so for readonly vars with no address taken and
712 possibly also for vtables since no direct pointer comparsion is done.
713 It might be interesting to do so to reduce linking overhead. */
714 if (DECL_COMDAT (vnode
->symbol
.decl
) || DECL_WEAK (vnode
->symbol
.decl
))
719 /* Dissolve the same_comdat_group list in which NODE resides. */
722 dissolve_same_comdat_group_list (symtab_node node
)
724 symtab_node n
= node
, next
;
727 next
= n
->symbol
.same_comdat_group
;
728 n
->symbol
.same_comdat_group
= NULL
;
734 /* Mark visibility of all functions.
736 A local function is one whose calls can occur only in the current
737 compilation unit and all its calls are explicit, so we can change
738 its calling convention. We simply mark all static functions whose
739 address is not taken as local.
741 We also change the TREE_PUBLIC flag of all declarations that are public
742 in language point of view but we want to overwrite this default
743 via visibilities for the backend point of view. */
746 function_and_variable_visibility (bool whole_program
)
748 struct cgraph_node
*node
;
749 struct varpool_node
*vnode
;
750 struct pointer_set_t
*aliased_nodes
= pointer_set_create ();
751 struct pointer_set_t
*aliased_vnodes
= pointer_set_create ();
755 /* Discover aliased nodes. */
756 FOR_EACH_VEC_ELT (alias_pair
, alias_pairs
, i
, p
)
759 fprintf (dump_file
, "Alias %s->%s",
760 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
761 IDENTIFIER_POINTER (p
->target
));
763 if ((node
= cgraph_node_for_asm (p
->target
)) != NULL
764 && !DECL_EXTERNAL (node
->symbol
.decl
))
768 cgraph_mark_force_output_node (node
);
769 pointer_set_insert (aliased_nodes
, node
);
771 fprintf (dump_file
, " node %s/%i",
772 cgraph_node_name (node
), node
->uid
);
774 else if ((vnode
= varpool_node_for_asm (p
->target
)) != NULL
775 && !DECL_EXTERNAL (vnode
->symbol
.decl
))
777 vnode
->symbol
.force_output
= 1;
778 pointer_set_insert (aliased_vnodes
, vnode
);
780 fprintf (dump_file
, " varpool node %s",
781 varpool_node_name (vnode
));
784 fprintf (dump_file
, "\n");
787 FOR_EACH_FUNCTION (node
)
789 int flags
= flags_from_decl_or_type (node
->symbol
.decl
);
791 /* Optimize away PURE and CONST constructors and destructors. */
793 && (flags
& (ECF_CONST
| ECF_PURE
))
794 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
796 DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
) = 0;
797 DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
) = 0;
800 /* Frontends and alias code marks nodes as needed before parsing is finished.
801 We may end up marking as node external nodes where this flag is meaningless
803 if (node
->symbol
.force_output
804 && (DECL_EXTERNAL (node
->symbol
.decl
) || !node
->analyzed
))
805 node
->symbol
.force_output
= 0;
807 /* C++ FE on lack of COMDAT support create local COMDAT functions
808 (that ought to be shared but can not due to object format
809 limitations). It is neccesary to keep the flag to make rest of C++ FE
810 happy. Clear the flag here to avoid confusion in middle-end. */
811 if (DECL_COMDAT (node
->symbol
.decl
) && !TREE_PUBLIC (node
->symbol
.decl
))
812 DECL_COMDAT (node
->symbol
.decl
) = 0;
813 /* For external decls stop tracking same_comdat_group, it doesn't matter
814 what comdat group they are in when they won't be emitted in this TU,
815 and simplifies later passes. */
816 if (node
->symbol
.same_comdat_group
&& DECL_EXTERNAL (node
->symbol
.decl
))
818 #ifdef ENABLE_CHECKING
821 for (n
= node
->symbol
.same_comdat_group
;
822 n
!= (symtab_node
)node
;
823 n
= n
->symbol
.same_comdat_group
)
824 /* If at least one of same comdat group functions is external,
825 all of them have to be, otherwise it is a front-end bug. */
826 gcc_assert (DECL_EXTERNAL (n
->symbol
.decl
));
828 dissolve_same_comdat_group_list ((symtab_node
) node
);
830 gcc_assert ((!DECL_WEAK (node
->symbol
.decl
)
831 && !DECL_COMDAT (node
->symbol
.decl
))
832 || TREE_PUBLIC (node
->symbol
.decl
)
833 || DECL_EXTERNAL (node
->symbol
.decl
));
834 if (cgraph_externally_visible_p (node
, whole_program
,
835 pointer_set_contains (aliased_nodes
,
838 gcc_assert (!node
->global
.inlined_to
);
839 node
->symbol
.externally_visible
= true;
842 node
->symbol
.externally_visible
= false;
843 if (!node
->symbol
.externally_visible
&& node
->analyzed
844 && !DECL_EXTERNAL (node
->symbol
.decl
))
846 gcc_assert (whole_program
|| in_lto_p
847 || !TREE_PUBLIC (node
->symbol
.decl
));
848 cgraph_make_decl_local (node
->symbol
.decl
);
849 node
->symbol
.resolution
= LDPR_PREVAILING_DEF_IRONLY
;
850 if (node
->symbol
.same_comdat_group
)
851 /* cgraph_externally_visible_p has already checked all other nodes
852 in the group and they will all be made local. We need to
853 dissolve the group at once so that the predicate does not
855 dissolve_same_comdat_group_list ((symtab_node
) node
);
858 if (node
->thunk
.thunk_p
859 && TREE_PUBLIC (node
->symbol
.decl
))
861 struct cgraph_node
*decl_node
= node
;
863 decl_node
= cgraph_function_node (decl_node
->callees
->callee
, NULL
);
865 /* Thunks have the same visibility as function they are attached to.
866 Make sure the C++ front end set this up properly. */
867 if (DECL_ONE_ONLY (decl_node
->symbol
.decl
))
869 gcc_checking_assert (DECL_COMDAT (node
->symbol
.decl
)
870 == DECL_COMDAT (decl_node
->symbol
.decl
));
871 gcc_checking_assert (DECL_COMDAT_GROUP (node
->symbol
.decl
)
872 == DECL_COMDAT_GROUP (decl_node
->symbol
.decl
));
873 gcc_checking_assert (node
->symbol
.same_comdat_group
);
875 if (DECL_EXTERNAL (decl_node
->symbol
.decl
))
876 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
879 FOR_EACH_DEFINED_FUNCTION (node
)
880 node
->local
.local
= cgraph_local_node_p (node
);
881 FOR_EACH_VARIABLE (vnode
)
883 /* weak flag makes no sense on local variables. */
884 gcc_assert (!DECL_WEAK (vnode
->symbol
.decl
)
885 || TREE_PUBLIC (vnode
->symbol
.decl
)
886 || DECL_EXTERNAL (vnode
->symbol
.decl
));
887 /* In several cases declarations can not be common:
889 - when declaration has initializer
891 - when it has specific section
892 - when it resides in non-generic address space.
893 - if declaration is local, it will get into .local common section
894 so common flag is not needed. Frontends still produce these in
895 certain cases, such as for:
897 static int a __attribute__ ((common))
899 Canonicalize things here and clear the redundant flag. */
900 if (DECL_COMMON (vnode
->symbol
.decl
)
901 && (!(TREE_PUBLIC (vnode
->symbol
.decl
)
902 || DECL_EXTERNAL (vnode
->symbol
.decl
))
903 || (DECL_INITIAL (vnode
->symbol
.decl
)
904 && DECL_INITIAL (vnode
->symbol
.decl
) != error_mark_node
)
905 || DECL_WEAK (vnode
->symbol
.decl
)
906 || DECL_SECTION_NAME (vnode
->symbol
.decl
) != NULL
907 || ! (ADDR_SPACE_GENERIC_P
908 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->symbol
.decl
))))))
909 DECL_COMMON (vnode
->symbol
.decl
) = 0;
911 FOR_EACH_DEFINED_VARIABLE (vnode
)
913 if (!vnode
->finalized
)
915 if (varpool_externally_visible_p
917 pointer_set_contains (aliased_vnodes
, vnode
)))
918 vnode
->symbol
.externally_visible
= true;
920 vnode
->symbol
.externally_visible
= false;
921 if (!vnode
->symbol
.externally_visible
)
923 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->symbol
.decl
));
924 cgraph_make_decl_local (vnode
->symbol
.decl
);
925 if (vnode
->symbol
.same_comdat_group
)
926 dissolve_same_comdat_group_list ((symtab_node
) vnode
);
927 vnode
->symbol
.resolution
= LDPR_PREVAILING_DEF_IRONLY
;
929 gcc_assert (TREE_STATIC (vnode
->symbol
.decl
));
931 pointer_set_destroy (aliased_nodes
);
932 pointer_set_destroy (aliased_vnodes
);
936 fprintf (dump_file
, "\nMarking local functions:");
937 FOR_EACH_DEFINED_FUNCTION (node
)
938 if (node
->local
.local
)
939 fprintf (dump_file
, " %s", cgraph_node_name (node
));
940 fprintf (dump_file
, "\n\n");
941 fprintf (dump_file
, "\nMarking externally visible functions:");
942 FOR_EACH_DEFINED_FUNCTION (node
)
943 if (node
->symbol
.externally_visible
)
944 fprintf (dump_file
, " %s", cgraph_node_name (node
));
945 fprintf (dump_file
, "\n\n");
946 fprintf (dump_file
, "\nMarking externally visible variables:");
947 FOR_EACH_DEFINED_VARIABLE (vnode
)
948 if (vnode
->symbol
.externally_visible
)
949 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
950 fprintf (dump_file
, "\n\n");
952 cgraph_function_flags_ready
= true;
956 /* Local function pass handling visibilities. This happens before LTO streaming
957 so in particular -fwhole-program should be ignored at this level. */
960 local_function_and_variable_visibility (void)
962 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
965 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility
=
969 "visibility", /* name */
971 local_function_and_variable_visibility
,/* execute */
974 0, /* static_pass_number */
975 TV_CGRAPHOPT
, /* tv_id */
976 0, /* properties_required */
977 0, /* properties_provided */
978 0, /* properties_destroyed */
979 0, /* todo_flags_start */
980 TODO_remove_functions
| TODO_dump_symtab
981 | TODO_ggc_collect
/* todo_flags_finish */
985 /* Do not re-run on ltrans stage. */
988 gate_whole_program_function_and_variable_visibility (void)
993 /* Bring functionss local at LTO time whith -fwhole-program. */
996 whole_program_function_and_variable_visibility (void)
998 function_and_variable_visibility (flag_whole_program
);
1000 ipa_discover_readonly_nonaddressable_vars ();
1004 struct ipa_opt_pass_d pass_ipa_whole_program_visibility
=
1008 "whole-program", /* name */
1009 gate_whole_program_function_and_variable_visibility
,/* gate */
1010 whole_program_function_and_variable_visibility
,/* execute */
1013 0, /* static_pass_number */
1014 TV_CGRAPHOPT
, /* tv_id */
1015 0, /* properties_required */
1016 0, /* properties_provided */
1017 0, /* properties_destroyed */
1018 0, /* todo_flags_start */
1019 TODO_remove_functions
| TODO_dump_symtab
1020 | TODO_ggc_collect
/* todo_flags_finish */
1022 NULL
, /* generate_summary */
1023 NULL
, /* write_summary */
1024 NULL
, /* read_summary */
1025 NULL
, /* write_optimization_summary */
1026 NULL
, /* read_optimization_summary */
1027 NULL
, /* stmt_fixup */
1029 NULL
, /* function_transform */
1030 NULL
, /* variable_transform */
1034 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1039 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1040 struct cgraph_edge
*e
;
1042 bool something_changed
= false;
1045 order_pos
= ipa_reverse_postorder (order
);
1046 for (i
= order_pos
- 1; i
>= 0; i
--)
1048 if (order
[i
]->local
.local
&& cgraph_propagate_frequency (order
[i
]))
1050 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1051 if (e
->callee
->local
.local
&& !e
->callee
->symbol
.aux
)
1053 something_changed
= true;
1054 e
->callee
->symbol
.aux
= (void *)1;
1057 order
[i
]->symbol
.aux
= NULL
;
1060 while (something_changed
)
1062 something_changed
= false;
1063 for (i
= order_pos
- 1; i
>= 0; i
--)
1065 if (order
[i
]->symbol
.aux
&& cgraph_propagate_frequency (order
[i
]))
1067 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1068 if (e
->callee
->local
.local
&& !e
->callee
->symbol
.aux
)
1070 something_changed
= true;
1071 e
->callee
->symbol
.aux
= (void *)1;
1074 order
[i
]->symbol
.aux
= NULL
;
1082 gate_ipa_profile (void)
1084 return flag_ipa_profile
;
1087 struct ipa_opt_pass_d pass_ipa_profile
=
1091 "profile_estimate", /* name */
1092 gate_ipa_profile
, /* gate */
1093 ipa_profile
, /* execute */
1096 0, /* static_pass_number */
1097 TV_IPA_PROFILE
, /* tv_id */
1098 0, /* properties_required */
1099 0, /* properties_provided */
1100 0, /* properties_destroyed */
1101 0, /* todo_flags_start */
1102 0 /* todo_flags_finish */
1104 NULL
, /* generate_summary */
1105 NULL
, /* write_summary */
1106 NULL
, /* read_summary */
1107 NULL
, /* write_optimization_summary */
1108 NULL
, /* read_optimization_summary */
1109 NULL
, /* stmt_fixup */
1111 NULL
, /* function_transform */
1112 NULL
/* variable_transform */
1115 /* Generate and emit a static constructor or destructor. WHICH must
1116 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1117 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1118 initialization priority for this constructor or destructor.
1120 FINAL specify whether the externally visible name for collect2 should
1124 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
1126 static int counter
= 0;
1128 tree decl
, name
, resdecl
;
1130 /* The priority is encoded in the constructor or destructor name.
1131 collect2 will sort the names and arrange that they are called at
1134 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1136 /* Proudce sane name but one not recognizable by collect2, just for the
1137 case we fail to inline the function. */
1138 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
1139 name
= get_file_function_name (which_buf
);
1141 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1142 build_function_type_list (void_type_node
, NULL_TREE
));
1143 current_function_decl
= decl
;
1145 resdecl
= build_decl (input_location
,
1146 RESULT_DECL
, NULL_TREE
, void_type_node
);
1147 DECL_ARTIFICIAL (resdecl
) = 1;
1148 DECL_RESULT (decl
) = resdecl
;
1149 DECL_CONTEXT (resdecl
) = decl
;
1151 allocate_struct_function (decl
, false);
1153 TREE_STATIC (decl
) = 1;
1154 TREE_USED (decl
) = 1;
1155 DECL_ARTIFICIAL (decl
) = 1;
1156 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1157 DECL_SAVED_TREE (decl
) = body
;
1158 if (!targetm
.have_ctors_dtors
&& final
)
1160 TREE_PUBLIC (decl
) = 1;
1161 DECL_PRESERVE_P (decl
) = 1;
1163 DECL_UNINLINABLE (decl
) = 1;
1165 DECL_INITIAL (decl
) = make_node (BLOCK
);
1166 TREE_USED (DECL_INITIAL (decl
)) = 1;
1168 DECL_SOURCE_LOCATION (decl
) = input_location
;
1169 cfun
->function_end_locus
= input_location
;
1174 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1175 decl_init_priority_insert (decl
, priority
);
1178 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1179 decl_fini_priority_insert (decl
, priority
);
1185 gimplify_function_tree (decl
);
1187 cgraph_add_new_function (decl
, false);
1190 current_function_decl
= NULL
;
1193 /* Generate and emit a static constructor or destructor. WHICH must
1194 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1195 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1196 initialization priority for this constructor or destructor. */
1199 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1201 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
1204 /* A vector of FUNCTION_DECLs declared as static constructors. */
1205 static VEC(tree
, heap
) *static_ctors
;
1206 /* A vector of FUNCTION_DECLs declared as static destructors. */
1207 static VEC(tree
, heap
) *static_dtors
;
1209 /* When target does not have ctors and dtors, we call all constructor
1210 and destructor by special initialization/destruction function
1211 recognized by collect2.
1213 When we are going to build this function, collect all constructors and
1214 destructors and turn them into normal functions. */
1217 record_cdtor_fn (struct cgraph_node
*node
)
1219 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
))
1220 VEC_safe_push (tree
, heap
, static_ctors
, node
->symbol
.decl
);
1221 if (DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
1222 VEC_safe_push (tree
, heap
, static_dtors
, node
->symbol
.decl
);
1223 node
= cgraph_get_node (node
->symbol
.decl
);
1224 DECL_DISREGARD_INLINE_LIMITS (node
->symbol
.decl
) = 1;
1227 /* Define global constructors/destructor functions for the CDTORS, of
1228 which they are LEN. The CDTORS are sorted by initialization
1229 priority. If CTOR_P is true, these are constructors; otherwise,
1230 they are destructors. */
1233 build_cdtor (bool ctor_p
, VEC (tree
, heap
) *cdtors
)
1236 size_t len
= VEC_length (tree
, cdtors
);
1243 priority_type priority
;
1251 fn
= VEC_index (tree
, cdtors
, j
);
1252 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1255 else if (p
!= priority
)
1261 /* When there is only one cdtor and target supports them, do nothing. */
1263 && targetm
.have_ctors_dtors
)
1268 /* Find the next batch of constructors/destructors with the same
1269 initialization priority. */
1273 fn
= VEC_index (tree
, cdtors
, i
);
1274 call
= build_call_expr (fn
, 0);
1276 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1278 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1279 /* We do not want to optimize away pure/const calls here.
1280 When optimizing, these should be already removed, when not
1281 optimizing, we want user to be able to breakpoint in them. */
1282 TREE_SIDE_EFFECTS (call
) = 1;
1283 append_to_statement_list (call
, &body
);
1285 gcc_assert (body
!= NULL_TREE
);
1286 /* Generate a function to call all the function of like
1288 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1292 /* Comparison function for qsort. P1 and P2 are actually of type
1293 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1294 used to determine the sort order. */
1297 compare_ctor (const void *p1
, const void *p2
)
1304 f1
= *(const tree
*)p1
;
1305 f2
= *(const tree
*)p2
;
1306 priority1
= DECL_INIT_PRIORITY (f1
);
1307 priority2
= DECL_INIT_PRIORITY (f2
);
1309 if (priority1
< priority2
)
1311 else if (priority1
> priority2
)
1314 /* Ensure a stable sort. Constructors are executed in backwarding
1315 order to make LTO initialize braries first. */
1316 return DECL_UID (f2
) - DECL_UID (f1
);
1319 /* Comparison function for qsort. P1 and P2 are actually of type
1320 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1321 used to determine the sort order. */
1324 compare_dtor (const void *p1
, const void *p2
)
1331 f1
= *(const tree
*)p1
;
1332 f2
= *(const tree
*)p2
;
1333 priority1
= DECL_FINI_PRIORITY (f1
);
1334 priority2
= DECL_FINI_PRIORITY (f2
);
1336 if (priority1
< priority2
)
1338 else if (priority1
> priority2
)
1341 /* Ensure a stable sort. */
1342 return DECL_UID (f1
) - DECL_UID (f2
);
1345 /* Generate functions to call static constructors and destructors
1346 for targets that do not support .ctors/.dtors sections. These
1347 functions have magic names which are detected by collect2. */
1350 build_cdtor_fns (void)
1352 if (!VEC_empty (tree
, static_ctors
))
1354 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1355 VEC_qsort (tree
, static_ctors
, compare_ctor
);
1356 build_cdtor (/*ctor_p=*/true, static_ctors
);
1359 if (!VEC_empty (tree
, static_dtors
))
1361 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1362 VEC_qsort (tree
, static_dtors
, compare_dtor
);
1363 build_cdtor (/*ctor_p=*/false, static_dtors
);
1367 /* Look for constructors and destructors and produce function calling them.
1368 This is needed for targets not supporting ctors or dtors, but we perform the
1369 transformation also at linktime to merge possibly numberous
1370 constructors/destructors into single function to improve code locality and
1374 ipa_cdtor_merge (void)
1376 struct cgraph_node
*node
;
1377 FOR_EACH_DEFINED_FUNCTION (node
)
1378 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
)
1379 || DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
1380 record_cdtor_fn (node
);
1382 VEC_free (tree
, heap
, static_ctors
);
1383 VEC_free (tree
, heap
, static_dtors
);
1387 /* Perform the pass when we have no ctors/dtors support
1388 or at LTO time to merge multiple constructors into single
1392 gate_ipa_cdtor_merge (void)
1394 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1397 struct ipa_opt_pass_d pass_ipa_cdtor_merge
=
1402 gate_ipa_cdtor_merge
, /* gate */
1403 ipa_cdtor_merge
, /* execute */
1406 0, /* static_pass_number */
1407 TV_CGRAPHOPT
, /* tv_id */
1408 0, /* properties_required */
1409 0, /* properties_provided */
1410 0, /* properties_destroyed */
1411 0, /* todo_flags_start */
1412 0 /* todo_flags_finish */
1414 NULL
, /* generate_summary */
1415 NULL
, /* write_summary */
1416 NULL
, /* read_summary */
1417 NULL
, /* write_optimization_summary */
1418 NULL
, /* read_optimization_summary */
1419 NULL
, /* stmt_fixup */
1421 NULL
, /* function_transform */
1422 NULL
/* variable_transform */