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"
36 /* Look for all functions inlined to NODE and update their inlined_to pointers
40 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
42 struct cgraph_edge
*e
;
43 for (e
= node
->callees
; e
; e
= e
->next_callee
)
44 if (e
->callee
->global
.inlined_to
)
46 e
->callee
->global
.inlined_to
= inlined_to
;
47 update_inlined_to_pointer (e
->callee
, inlined_to
);
51 /* Add cgraph NODE to queue starting at FIRST.
53 The queue is linked via AUX pointers and terminated by pointer to 1.
54 We enqueue nodes at two occasions: when we find them reachable or when we find
55 their bodies needed for further clonning. In the second case we mark them
56 by pointer to 2 after processing so they are re-queue when they become
60 enqueue_cgraph_node (struct cgraph_node
*node
, struct cgraph_node
**first
)
62 /* Node is still in queue; do nothing. */
63 if (node
->symbol
.aux
&& node
->symbol
.aux
!= (void *) 2)
65 /* Node was already processed as unreachable, re-enqueue
66 only if it became reachable now. */
67 if (node
->symbol
.aux
== (void *)2 && !node
->reachable
)
69 node
->symbol
.aux
= *first
;
73 /* Add varpool NODE to queue starting at FIRST. */
76 enqueue_varpool_node (struct varpool_node
*node
, struct varpool_node
**first
)
78 node
->symbol
.aux
= *first
;
82 /* Process references. */
85 process_references (struct ipa_ref_list
*list
,
86 struct cgraph_node
**first
,
87 struct varpool_node
**first_varpool
,
88 bool before_inlining_p
)
92 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
94 if (ref
->refered_type
== IPA_REF_CGRAPH
)
96 struct cgraph_node
*node
= ipa_ref_node (ref
);
99 && (!DECL_EXTERNAL (node
->symbol
.decl
)
100 || before_inlining_p
))
101 node
->reachable
= true;
102 enqueue_cgraph_node (node
, first
);
106 struct varpool_node
*node
= ipa_ref_varpool_node (ref
);
109 varpool_mark_needed_node (node
);
110 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_refering_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;
178 #ifdef ENABLE_CHECKING
182 fprintf (file
, "\nReclaiming functions:");
183 #ifdef ENABLE_CHECKING
184 FOR_EACH_FUNCTION (node
)
185 gcc_assert (!node
->symbol
.aux
);
186 FOR_EACH_VARIABLE (vnode
)
187 gcc_assert (!vnode
->symbol
.aux
);
189 varpool_reset_queue ();
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
);
204 node
->reachable
= true;
208 gcc_assert (!node
->symbol
.aux
);
209 node
->reachable
= false;
212 /* Mark variables that are obviously needed. */
213 FOR_EACH_VARIABLE (vnode
)
215 vnode
->next_needed
= NULL
;
216 vnode
->prev_needed
= NULL
;
217 if ((vnode
->analyzed
|| vnode
->force_output
)
218 && !varpool_can_remove_if_no_refs (vnode
))
220 vnode
->needed
= false;
221 varpool_mark_needed_node (vnode
);
222 enqueue_varpool_node (vnode
, &first_varpool
);
225 vnode
->needed
= false;
228 /* Perform reachability analysis. As a special case do not consider
229 extern inline functions not inlined as live because we won't output
232 We maintain two worklist, one for cgraph nodes other for varpools and
233 are finished once both are empty. */
235 while (first
!= (struct cgraph_node
*) (void *) 1
236 || first_varpool
!= (struct varpool_node
*) (void *) 1)
238 if (first
!= (struct cgraph_node
*) (void *) 1)
240 struct cgraph_edge
*e
;
242 first
= (struct cgraph_node
*) first
->symbol
.aux
;
243 if (!node
->reachable
)
244 node
->symbol
.aux
= (void *)2;
246 /* If we found this node reachable, first mark on the callees
247 reachable too, unless they are direct calls to extern inline functions
248 we decided to not inline. */
251 for (e
= node
->callees
; e
; e
= e
->next_callee
)
253 if (!e
->callee
->reachable
255 && (!e
->inline_failed
256 || !DECL_EXTERNAL (e
->callee
->symbol
.decl
)
257 || before_inlining_p
))
258 e
->callee
->reachable
= true;
259 enqueue_cgraph_node (e
->callee
, &first
);
261 process_references (&node
->symbol
.ref_list
, &first
,
262 &first_varpool
, before_inlining_p
);
265 /* If any function in a comdat group is reachable, force
266 all other functions in the same comdat group to be
268 if (node
->symbol
.same_comdat_group
270 && !node
->global
.inlined_to
)
272 for (next
= cgraph (node
->symbol
.same_comdat_group
);
274 next
= cgraph (next
->symbol
.same_comdat_group
))
275 if (!next
->reachable
)
277 next
->reachable
= true;
278 enqueue_cgraph_node (next
, &first
);
282 /* We can freely remove inline clones even if they are cloned, however if
283 function is clone of real clone, we must keep it around in order to
284 make materialize_clones produce function body with the changes
286 while (node
->clone_of
&& !node
->clone_of
->symbol
.aux
287 && !gimple_has_body_p (node
->symbol
.decl
))
289 bool noninline
= node
->clone_of
->symbol
.decl
!= node
->symbol
.decl
;
290 node
= node
->clone_of
;
291 if (noninline
&& !node
->reachable
&& !node
->symbol
.aux
)
293 enqueue_cgraph_node (node
, &first
);
298 if (first_varpool
!= (struct varpool_node
*) (void *) 1)
300 vnode
= first_varpool
;
301 first_varpool
= (struct varpool_node
*)first_varpool
->symbol
.aux
;
302 vnode
->symbol
.aux
= NULL
;
303 process_references (&vnode
->symbol
.ref_list
, &first
,
304 &first_varpool
, before_inlining_p
);
305 /* If any function in a comdat group is reachable, force
306 all other functions in the same comdat group to be
308 if (vnode
->symbol
.same_comdat_group
)
310 struct varpool_node
*next
;
311 for (next
= varpool (vnode
->symbol
.same_comdat_group
);
313 next
= varpool (next
->symbol
.same_comdat_group
))
316 varpool_mark_needed_node (next
);
317 enqueue_varpool_node (next
, &first_varpool
);
323 /* Remove unreachable nodes.
325 Completely unreachable functions can be fully removed from the callgraph.
326 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
327 callgraph (so we still have edges to them). We remove function body then.
329 Also we need to care functions that are unreachable but we need to keep them around
330 for later clonning. In this case we also turn them to unanalyzed nodes, but
331 keep the body around. */
332 for (node
= cgraph_nodes
; node
; node
= next
)
335 if (node
->symbol
.aux
&& !node
->reachable
)
337 cgraph_node_remove_callees (node
);
338 ipa_remove_all_references (&node
->symbol
.ref_list
);
339 node
->analyzed
= false;
341 if (!node
->symbol
.aux
)
343 struct cgraph_edge
*e
;
348 node
->global
.inlined_to
= NULL
;
350 fprintf (file
, " %s", cgraph_node_name (node
));
351 /* See if there is reachable caller. */
352 for (e
= node
->callers
; e
&& !found
; e
= e
->next_caller
)
353 if (e
->caller
->reachable
)
355 for (i
= 0; (ipa_ref_list_refering_iterate (&node
->symbol
.ref_list
,
358 if (ref
->refering_type
== IPA_REF_CGRAPH
359 && ipa_ref_refering_node (ref
)->reachable
)
361 else if (ref
->refering_type
== IPA_REF_VARPOOL
362 && ipa_ref_refering_varpool_node (ref
)->needed
)
365 /* If so, we need to keep node in the callgraph. */
370 struct cgraph_node
*clone
;
372 /* If there are still clones, we must keep body around.
373 Otherwise we can just remove the body but keep the clone. */
374 for (clone
= node
->clones
; clone
;
375 clone
= clone
->next_sibling_clone
)
376 if (clone
->symbol
.aux
)
380 cgraph_release_function_body (node
);
381 if (node
->prev_sibling_clone
)
382 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
383 else if (node
->clone_of
)
384 node
->clone_of
->clones
= node
->next_sibling_clone
;
385 if (node
->next_sibling_clone
)
386 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
388 node
->former_clone_of
= node
->clone_of
->symbol
.decl
;
389 node
->clone_of
= NULL
;
390 node
->next_sibling_clone
= NULL
;
391 node
->prev_sibling_clone
= NULL
;
394 gcc_assert (!clone
->symbol
.in_other_partition
);
395 node
->analyzed
= false;
397 cgraph_node_remove_callees (node
);
398 ipa_remove_all_references (&node
->symbol
.ref_list
);
403 cgraph_remove_node (node
);
408 FOR_EACH_FUNCTION (node
)
410 /* Inline clones might be kept around so their materializing allows further
411 cloning. If the function the clone is inlined into is removed, we need
412 to turn it into normal cone. */
413 if (node
->global
.inlined_to
416 gcc_assert (node
->clones
);
417 node
->global
.inlined_to
= NULL
;
418 update_inlined_to_pointer (node
, node
);
420 node
->symbol
.aux
= NULL
;
424 fprintf (file
, "\n");
427 fprintf (file
, "Reclaiming variables:");
428 for (vnode
= varpool_nodes
; vnode
; vnode
= vnext
)
434 fprintf (file
, " %s", varpool_node_name (vnode
));
435 varpool_remove_node (vnode
);
440 /* Now update address_taken flags and try to promote functions to be local. */
443 fprintf (file
, "\nClearing address taken flags:");
444 FOR_EACH_DEFINED_FUNCTION (node
)
445 if (node
->symbol
.address_taken
446 && !node
->symbol
.used_from_other_partition
)
448 if (!cgraph_for_node_and_aliases (node
, has_addr_references_p
, NULL
, true))
451 fprintf (file
, " %s", cgraph_node_name (node
));
452 node
->symbol
.address_taken
= false;
454 if (cgraph_local_node_p (node
))
456 node
->local
.local
= true;
458 fprintf (file
, " (local)");
463 fprintf (file
, "\n");
465 /* Rest of transformations are undesirable at -O0. */
469 #ifdef ENABLE_CHECKING
473 /* Reclaim alias pairs for functions that have disappeared from the
475 remove_unreachable_alias_pairs ();
480 /* Discover variables that have no longer address taken or that are read only
481 and update their flags.
483 FIXME: This can not be done in between gimplify and omp_expand since
484 readonly flag plays role on what is shared and what is not. Currently we do
485 this transformation as part of whole program visibility and re-do at
486 ipa-reference pass (to take into account clonning), but it would
487 make sense to do it before early optimizations. */
490 ipa_discover_readonly_nonaddressable_vars (void)
492 struct varpool_node
*vnode
;
494 fprintf (dump_file
, "Clearing variable flags:");
495 FOR_EACH_VARIABLE (vnode
)
496 if (vnode
->finalized
&& varpool_all_refs_explicit_p (vnode
)
497 && (TREE_ADDRESSABLE (vnode
->symbol
.decl
)
498 || !TREE_READONLY (vnode
->symbol
.decl
)))
500 bool written
= false;
501 bool address_taken
= false;
504 for (i
= 0; ipa_ref_list_refering_iterate (&vnode
->symbol
.ref_list
,
506 && (!written
|| !address_taken
); i
++)
510 address_taken
= true;
518 if (TREE_ADDRESSABLE (vnode
->symbol
.decl
) && !address_taken
)
521 fprintf (dump_file
, " %s (addressable)", varpool_node_name (vnode
));
522 TREE_ADDRESSABLE (vnode
->symbol
.decl
) = 0;
524 if (!TREE_READONLY (vnode
->symbol
.decl
) && !address_taken
&& !written
525 /* Making variable in explicit section readonly can cause section
527 See e.g. gcc.c-torture/compile/pr23237.c */
528 && DECL_SECTION_NAME (vnode
->symbol
.decl
) == NULL
)
531 fprintf (dump_file
, " %s (read-only)", varpool_node_name (vnode
));
532 TREE_READONLY (vnode
->symbol
.decl
) = 1;
536 fprintf (dump_file
, "\n");
539 /* Return true when there is a reference to node and it is not vtable. */
541 cgraph_address_taken_from_non_vtable_p (struct cgraph_node
*node
)
545 for (i
= 0; ipa_ref_list_refering_iterate (&node
->symbol
.ref_list
,
547 if (ref
->use
== IPA_REF_ADDR
)
549 struct varpool_node
*node
;
550 if (ref
->refering_type
== IPA_REF_CGRAPH
)
552 node
= ipa_ref_refering_varpool_node (ref
);
553 if (!DECL_VIRTUAL_P (node
->symbol
.decl
))
559 /* COMDAT functions must be shared only if they have address taken,
560 otherwise we can produce our own private implementation with
562 Return true when turning COMDAT functoin static can not lead to wrong
563 code when the resulting object links with a library defining same COMDAT.
565 Virtual functions do have their addresses taken from the vtables,
566 but in C++ there is no way to compare their addresses for equality. */
569 cgraph_comdat_can_be_unshared_p (struct cgraph_node
*node
)
571 if ((cgraph_address_taken_from_non_vtable_p (node
)
572 && !DECL_VIRTUAL_P (node
->symbol
.decl
))
575 if (node
->symbol
.same_comdat_group
)
577 struct cgraph_node
*next
;
579 /* If more than one function is in the same COMDAT group, it must
580 be shared even if just one function in the comdat group has
582 for (next
= cgraph (node
->symbol
.same_comdat_group
);
583 next
!= node
; next
= cgraph (next
->symbol
.same_comdat_group
))
584 if (cgraph_address_taken_from_non_vtable_p (next
)
585 && !DECL_VIRTUAL_P (next
->symbol
.decl
))
591 /* Return true when function NODE should be considered externally visible. */
594 cgraph_externally_visible_p (struct cgraph_node
*node
,
595 bool whole_program
, bool aliased
)
597 if (!node
->local
.finalized
)
599 if (!DECL_COMDAT (node
->symbol
.decl
)
600 && (!TREE_PUBLIC (node
->symbol
.decl
)
601 || DECL_EXTERNAL (node
->symbol
.decl
)))
604 /* Do not even try to be smart about aliased nodes. Until we properly
605 represent everything by same body alias, these are just evil. */
609 /* Do not try to localize built-in functions yet. One of problems is that we
610 end up mangling their asm for WHOPR that makes it impossible to call them
611 using the implicit built-in declarations anymore. Similarly this enables
612 us to remove them as unreachable before actual calls may appear during
613 expansion or folding. */
614 if (DECL_BUILT_IN (node
->symbol
.decl
))
617 /* If linker counts on us, we must preserve the function. */
618 if (cgraph_used_from_object_file_p (node
))
620 if (DECL_PRESERVE_P (node
->symbol
.decl
))
622 if (lookup_attribute ("externally_visible",
623 DECL_ATTRIBUTES (node
->symbol
.decl
)))
625 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
626 && lookup_attribute ("dllexport",
627 DECL_ATTRIBUTES (node
->symbol
.decl
)))
629 if (node
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
)
631 /* When doing LTO or whole program, we can bring COMDAT functoins static.
632 This improves code quality and we know we will duplicate them at most twice
633 (in the case that we are not using plugin and link with object file
634 implementing same COMDAT) */
635 if ((in_lto_p
|| whole_program
)
636 && DECL_COMDAT (node
->symbol
.decl
)
637 && cgraph_comdat_can_be_unshared_p (node
))
640 /* When doing link time optimizations, hidden symbols become local. */
642 && (DECL_VISIBILITY (node
->symbol
.decl
) == VISIBILITY_HIDDEN
643 || DECL_VISIBILITY (node
->symbol
.decl
) == VISIBILITY_INTERNAL
)
644 /* Be sure that node is defined in IR file, not in other object
645 file. In that case we don't set used_from_other_object_file. */
648 else if (!whole_program
)
651 if (MAIN_NAME_P (DECL_NAME (node
->symbol
.decl
)))
657 /* Return true when variable VNODE should be considered externally visible. */
660 varpool_externally_visible_p (struct varpool_node
*vnode
, bool aliased
)
662 if (!DECL_COMDAT (vnode
->symbol
.decl
) && !TREE_PUBLIC (vnode
->symbol
.decl
))
665 /* Do not even try to be smart about aliased nodes. Until we properly
666 represent everything by same body alias, these are just evil. */
670 /* If linker counts on us, we must preserve the function. */
671 if (varpool_used_from_object_file_p (vnode
))
674 if (DECL_HARD_REGISTER (vnode
->symbol
.decl
))
676 if (DECL_PRESERVE_P (vnode
->symbol
.decl
))
678 if (lookup_attribute ("externally_visible",
679 DECL_ATTRIBUTES (vnode
->symbol
.decl
)))
681 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
682 && lookup_attribute ("dllexport",
683 DECL_ATTRIBUTES (vnode
->symbol
.decl
)))
686 /* See if we have linker information about symbol not being used or
687 if we need to make guess based on the declaration.
689 Even if the linker clams the symbol is unused, never bring internal
690 symbols that are declared by user as used or externally visible.
691 This is needed for i.e. references from asm statements. */
692 if (varpool_used_from_object_file_p (vnode
))
694 if (vnode
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
)
697 /* As a special case, the COMDAT virutal tables can be unshared.
698 In LTO mode turn vtables into static variables. The variable is readonly,
699 so this does not enable more optimization, but referring static var
700 is faster for dynamic linking. Also this match logic hidding vtables
701 from LTO symbol tables. */
702 if ((in_lto_p
|| flag_whole_program
)
703 && !vnode
->force_output
704 && DECL_COMDAT (vnode
->symbol
.decl
) && DECL_VIRTUAL_P (vnode
->symbol
.decl
))
707 /* When doing link time optimizations, hidden symbols become local. */
709 && (DECL_VISIBILITY (vnode
->symbol
.decl
) == VISIBILITY_HIDDEN
710 || DECL_VISIBILITY (vnode
->symbol
.decl
) == VISIBILITY_INTERNAL
)
711 /* Be sure that node is defined in IR file, not in other object
712 file. In that case we don't set used_from_other_object_file. */
715 else if (!flag_whole_program
)
718 /* Do not attempt to privatize COMDATS by default.
719 This would break linking with C++ libraries sharing
722 FIXME: We can do so for readonly vars with no address taken and
723 possibly also for vtables since no direct pointer comparsion is done.
724 It might be interesting to do so to reduce linking overhead. */
725 if (DECL_COMDAT (vnode
->symbol
.decl
) || DECL_WEAK (vnode
->symbol
.decl
))
730 /* Dissolve the same_comdat_group list in which NODE resides. */
733 dissolve_same_comdat_group_list (struct cgraph_node
*node
)
735 struct cgraph_node
*n
= node
, *next
;
738 next
= cgraph (n
->symbol
.same_comdat_group
);
739 n
->symbol
.same_comdat_group
= NULL
;
745 /* Mark visibility of all functions.
747 A local function is one whose calls can occur only in the current
748 compilation unit and all its calls are explicit, so we can change
749 its calling convention. We simply mark all static functions whose
750 address is not taken as local.
752 We also change the TREE_PUBLIC flag of all declarations that are public
753 in language point of view but we want to overwrite this default
754 via visibilities for the backend point of view. */
757 function_and_variable_visibility (bool whole_program
)
759 struct cgraph_node
*node
;
760 struct varpool_node
*vnode
;
761 struct pointer_set_t
*aliased_nodes
= pointer_set_create ();
762 struct pointer_set_t
*aliased_vnodes
= pointer_set_create ();
766 /* Discover aliased nodes. */
767 FOR_EACH_VEC_ELT (alias_pair
, alias_pairs
, i
, p
)
770 fprintf (dump_file
, "Alias %s->%s",
771 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
772 IDENTIFIER_POINTER (p
->target
));
774 if ((node
= cgraph_node_for_asm (p
->target
)) != NULL
775 && !DECL_EXTERNAL (node
->symbol
.decl
))
779 cgraph_mark_needed_node (node
);
780 gcc_assert (node
->needed
);
781 pointer_set_insert (aliased_nodes
, node
);
783 fprintf (dump_file
, " node %s/%i",
784 cgraph_node_name (node
), node
->uid
);
786 else if ((vnode
= varpool_node_for_asm (p
->target
)) != NULL
787 && !DECL_EXTERNAL (vnode
->symbol
.decl
))
789 varpool_mark_needed_node (vnode
);
790 gcc_assert (vnode
->needed
);
791 pointer_set_insert (aliased_vnodes
, vnode
);
793 fprintf (dump_file
, " varpool node %s",
794 varpool_node_name (vnode
));
797 fprintf (dump_file
, "\n");
800 FOR_EACH_FUNCTION (node
)
802 int flags
= flags_from_decl_or_type (node
->symbol
.decl
);
804 /* Optimize away PURE and CONST constructors and destructors. */
806 && (flags
& (ECF_CONST
| ECF_PURE
))
807 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
809 DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
) = 0;
810 DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
) = 0;
813 /* Frontends and alias code marks nodes as needed before parsing is finished.
814 We may end up marking as node external nodes where this flag is meaningless
817 && (DECL_EXTERNAL (node
->symbol
.decl
) || !node
->analyzed
))
820 /* C++ FE on lack of COMDAT support create local COMDAT functions
821 (that ought to be shared but can not due to object format
822 limitations). It is neccesary to keep the flag to make rest of C++ FE
823 happy. Clear the flag here to avoid confusion in middle-end. */
824 if (DECL_COMDAT (node
->symbol
.decl
) && !TREE_PUBLIC (node
->symbol
.decl
))
825 DECL_COMDAT (node
->symbol
.decl
) = 0;
826 /* For external decls stop tracking same_comdat_group, it doesn't matter
827 what comdat group they are in when they won't be emitted in this TU,
828 and simplifies later passes. */
829 if (node
->symbol
.same_comdat_group
&& DECL_EXTERNAL (node
->symbol
.decl
))
831 #ifdef ENABLE_CHECKING
834 for (n
= node
->symbol
.same_comdat_group
;
835 n
!= (symtab_node
)node
;
836 n
= n
->symbol
.same_comdat_group
)
837 /* If at least one of same comdat group functions is external,
838 all of them have to be, otherwise it is a front-end bug. */
839 gcc_assert (DECL_EXTERNAL (n
->symbol
.decl
));
841 dissolve_same_comdat_group_list (node
);
843 gcc_assert ((!DECL_WEAK (node
->symbol
.decl
)
844 && !DECL_COMDAT (node
->symbol
.decl
))
845 || TREE_PUBLIC (node
->symbol
.decl
)
846 || DECL_EXTERNAL (node
->symbol
.decl
));
847 if (cgraph_externally_visible_p (node
, whole_program
,
848 pointer_set_contains (aliased_nodes
,
851 gcc_assert (!node
->global
.inlined_to
);
852 node
->symbol
.externally_visible
= true;
855 node
->symbol
.externally_visible
= false;
856 if (!node
->symbol
.externally_visible
&& node
->analyzed
857 && !DECL_EXTERNAL (node
->symbol
.decl
))
859 gcc_assert (whole_program
|| in_lto_p
860 || !TREE_PUBLIC (node
->symbol
.decl
));
861 cgraph_make_decl_local (node
->symbol
.decl
);
862 node
->symbol
.resolution
= LDPR_PREVAILING_DEF_IRONLY
;
863 if (node
->symbol
.same_comdat_group
)
864 /* cgraph_externally_visible_p has already checked all other nodes
865 in the group and they will all be made local. We need to
866 dissolve the group at once so that the predicate does not
868 dissolve_same_comdat_group_list (node
);
871 if (node
->thunk
.thunk_p
872 && TREE_PUBLIC (node
->symbol
.decl
))
874 struct cgraph_node
*decl_node
= node
;
876 decl_node
= cgraph_function_node (decl_node
->callees
->callee
, NULL
);
878 /* Thunks have the same visibility as function they are attached to.
879 Make sure the C++ front end set this up properly. */
880 if (DECL_ONE_ONLY (decl_node
->symbol
.decl
))
882 gcc_checking_assert (DECL_COMDAT (node
->symbol
.decl
)
883 == DECL_COMDAT (decl_node
->symbol
.decl
));
884 gcc_checking_assert (DECL_COMDAT_GROUP (node
->symbol
.decl
)
885 == DECL_COMDAT_GROUP (decl_node
->symbol
.decl
));
886 gcc_checking_assert (node
->symbol
.same_comdat_group
);
888 if (DECL_EXTERNAL (decl_node
->symbol
.decl
))
889 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
892 FOR_EACH_DEFINED_FUNCTION (node
)
893 node
->local
.local
= cgraph_local_node_p (node
);
894 FOR_EACH_VARIABLE (vnode
)
896 /* weak flag makes no sense on local variables. */
897 gcc_assert (!DECL_WEAK (vnode
->symbol
.decl
)
898 || TREE_PUBLIC (vnode
->symbol
.decl
)
899 || DECL_EXTERNAL (vnode
->symbol
.decl
));
900 /* In several cases declarations can not be common:
902 - when declaration has initializer
904 - when it has specific section
905 - when it resides in non-generic address space.
906 - if declaration is local, it will get into .local common section
907 so common flag is not needed. Frontends still produce these in
908 certain cases, such as for:
910 static int a __attribute__ ((common))
912 Canonicalize things here and clear the redundant flag. */
913 if (DECL_COMMON (vnode
->symbol
.decl
)
914 && (!(TREE_PUBLIC (vnode
->symbol
.decl
)
915 || DECL_EXTERNAL (vnode
->symbol
.decl
))
916 || (DECL_INITIAL (vnode
->symbol
.decl
)
917 && DECL_INITIAL (vnode
->symbol
.decl
) != error_mark_node
)
918 || DECL_WEAK (vnode
->symbol
.decl
)
919 || DECL_SECTION_NAME (vnode
->symbol
.decl
) != NULL
920 || ! (ADDR_SPACE_GENERIC_P
921 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->symbol
.decl
))))))
922 DECL_COMMON (vnode
->symbol
.decl
) = 0;
924 FOR_EACH_DEFINED_VARIABLE (vnode
)
926 if (!vnode
->finalized
)
929 && varpool_externally_visible_p
931 pointer_set_contains (aliased_vnodes
, vnode
)))
932 vnode
->symbol
.externally_visible
= true;
934 vnode
->symbol
.externally_visible
= false;
935 if (!vnode
->symbol
.externally_visible
)
937 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->symbol
.decl
));
938 cgraph_make_decl_local (vnode
->symbol
.decl
);
939 vnode
->symbol
.resolution
= LDPR_PREVAILING_DEF_IRONLY
;
941 gcc_assert (TREE_STATIC (vnode
->symbol
.decl
));
943 pointer_set_destroy (aliased_nodes
);
944 pointer_set_destroy (aliased_vnodes
);
948 fprintf (dump_file
, "\nMarking local functions:");
949 FOR_EACH_DEFINED_FUNCTION (node
)
950 if (node
->local
.local
)
951 fprintf (dump_file
, " %s", cgraph_node_name (node
));
952 fprintf (dump_file
, "\n\n");
953 fprintf (dump_file
, "\nMarking externally visible functions:");
954 FOR_EACH_DEFINED_FUNCTION (node
)
955 if (node
->symbol
.externally_visible
)
956 fprintf (dump_file
, " %s", cgraph_node_name (node
));
957 fprintf (dump_file
, "\n\n");
958 fprintf (dump_file
, "\nMarking externally visible variables:");
959 FOR_EACH_DEFINED_VARIABLE (vnode
)
960 if (vnode
->symbol
.externally_visible
)
961 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
962 fprintf (dump_file
, "\n\n");
964 cgraph_function_flags_ready
= true;
968 /* Local function pass handling visibilities. This happens before LTO streaming
969 so in particular -fwhole-program should be ignored at this level. */
972 local_function_and_variable_visibility (void)
974 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
977 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility
=
981 "visibility", /* name */
983 local_function_and_variable_visibility
,/* execute */
986 0, /* static_pass_number */
987 TV_CGRAPHOPT
, /* tv_id */
988 0, /* properties_required */
989 0, /* properties_provided */
990 0, /* properties_destroyed */
991 0, /* todo_flags_start */
992 TODO_remove_functions
| TODO_dump_cgraph
993 | TODO_ggc_collect
/* todo_flags_finish */
997 /* Do not re-run on ltrans stage. */
1000 gate_whole_program_function_and_variable_visibility (void)
1002 return !flag_ltrans
;
1005 /* Bring functionss local at LTO time whith -fwhole-program. */
1008 whole_program_function_and_variable_visibility (void)
1010 struct cgraph_node
*node
;
1011 struct varpool_node
*vnode
;
1013 function_and_variable_visibility (flag_whole_program
);
1015 FOR_EACH_DEFINED_FUNCTION (node
)
1016 if ((node
->symbol
.externally_visible
&& !DECL_COMDAT (node
->symbol
.decl
))
1017 && node
->local
.finalized
)
1018 cgraph_mark_needed_node (node
);
1019 FOR_EACH_DEFINED_VARIABLE (vnode
)
1020 if (vnode
->symbol
.externally_visible
&& !DECL_COMDAT (vnode
->symbol
.decl
))
1021 varpool_mark_needed_node (vnode
);
1024 fprintf (dump_file
, "\nNeeded variables:");
1025 FOR_EACH_DEFINED_VARIABLE (vnode
)
1027 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
1028 fprintf (dump_file
, "\n\n");
1031 ipa_discover_readonly_nonaddressable_vars ();
1035 struct ipa_opt_pass_d pass_ipa_whole_program_visibility
=
1039 "whole-program", /* name */
1040 gate_whole_program_function_and_variable_visibility
,/* gate */
1041 whole_program_function_and_variable_visibility
,/* execute */
1044 0, /* static_pass_number */
1045 TV_CGRAPHOPT
, /* tv_id */
1046 0, /* properties_required */
1047 0, /* properties_provided */
1048 0, /* properties_destroyed */
1049 0, /* todo_flags_start */
1050 TODO_remove_functions
| TODO_dump_cgraph
1051 | TODO_ggc_collect
/* todo_flags_finish */
1053 NULL
, /* generate_summary */
1054 NULL
, /* write_summary */
1055 NULL
, /* read_summary */
1056 NULL
, /* write_optimization_summary */
1057 NULL
, /* read_optimization_summary */
1058 NULL
, /* stmt_fixup */
1060 NULL
, /* function_transform */
1061 NULL
, /* variable_transform */
1065 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1070 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1071 struct cgraph_edge
*e
;
1073 bool something_changed
= false;
1076 order_pos
= ipa_reverse_postorder (order
);
1077 for (i
= order_pos
- 1; i
>= 0; i
--)
1079 if (order
[i
]->local
.local
&& cgraph_propagate_frequency (order
[i
]))
1081 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1082 if (e
->callee
->local
.local
&& !e
->callee
->symbol
.aux
)
1084 something_changed
= true;
1085 e
->callee
->symbol
.aux
= (void *)1;
1088 order
[i
]->symbol
.aux
= NULL
;
1091 while (something_changed
)
1093 something_changed
= false;
1094 for (i
= order_pos
- 1; i
>= 0; i
--)
1096 if (order
[i
]->symbol
.aux
&& cgraph_propagate_frequency (order
[i
]))
1098 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1099 if (e
->callee
->local
.local
&& !e
->callee
->symbol
.aux
)
1101 something_changed
= true;
1102 e
->callee
->symbol
.aux
= (void *)1;
1105 order
[i
]->symbol
.aux
= NULL
;
1113 gate_ipa_profile (void)
1115 return flag_ipa_profile
;
1118 struct ipa_opt_pass_d pass_ipa_profile
=
1122 "profile_estimate", /* name */
1123 gate_ipa_profile
, /* gate */
1124 ipa_profile
, /* execute */
1127 0, /* static_pass_number */
1128 TV_IPA_PROFILE
, /* tv_id */
1129 0, /* properties_required */
1130 0, /* properties_provided */
1131 0, /* properties_destroyed */
1132 0, /* todo_flags_start */
1133 0 /* todo_flags_finish */
1135 NULL
, /* generate_summary */
1136 NULL
, /* write_summary */
1137 NULL
, /* read_summary */
1138 NULL
, /* write_optimization_summary */
1139 NULL
, /* read_optimization_summary */
1140 NULL
, /* stmt_fixup */
1142 NULL
, /* function_transform */
1143 NULL
/* variable_transform */
1146 /* Generate and emit a static constructor or destructor. WHICH must
1147 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1148 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1149 initialization priority for this constructor or destructor.
1151 FINAL specify whether the externally visible name for collect2 should
1155 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
1157 static int counter
= 0;
1159 tree decl
, name
, resdecl
;
1161 /* The priority is encoded in the constructor or destructor name.
1162 collect2 will sort the names and arrange that they are called at
1165 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1167 /* Proudce sane name but one not recognizable by collect2, just for the
1168 case we fail to inline the function. */
1169 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
1170 name
= get_file_function_name (which_buf
);
1172 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1173 build_function_type_list (void_type_node
, NULL_TREE
));
1174 current_function_decl
= decl
;
1176 resdecl
= build_decl (input_location
,
1177 RESULT_DECL
, NULL_TREE
, void_type_node
);
1178 DECL_ARTIFICIAL (resdecl
) = 1;
1179 DECL_RESULT (decl
) = resdecl
;
1180 DECL_CONTEXT (resdecl
) = decl
;
1182 allocate_struct_function (decl
, false);
1184 TREE_STATIC (decl
) = 1;
1185 TREE_USED (decl
) = 1;
1186 DECL_ARTIFICIAL (decl
) = 1;
1187 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1188 DECL_SAVED_TREE (decl
) = body
;
1189 if (!targetm
.have_ctors_dtors
&& final
)
1191 TREE_PUBLIC (decl
) = 1;
1192 DECL_PRESERVE_P (decl
) = 1;
1194 DECL_UNINLINABLE (decl
) = 1;
1196 DECL_INITIAL (decl
) = make_node (BLOCK
);
1197 TREE_USED (DECL_INITIAL (decl
)) = 1;
1199 DECL_SOURCE_LOCATION (decl
) = input_location
;
1200 cfun
->function_end_locus
= input_location
;
1205 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1206 decl_init_priority_insert (decl
, priority
);
1209 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1210 decl_fini_priority_insert (decl
, priority
);
1216 gimplify_function_tree (decl
);
1218 cgraph_add_new_function (decl
, false);
1221 current_function_decl
= NULL
;
1224 /* Generate and emit a static constructor or destructor. WHICH must
1225 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1226 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1227 initialization priority for this constructor or destructor. */
1230 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1232 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
1235 /* A vector of FUNCTION_DECLs declared as static constructors. */
1236 static VEC(tree
, heap
) *static_ctors
;
1237 /* A vector of FUNCTION_DECLs declared as static destructors. */
1238 static VEC(tree
, heap
) *static_dtors
;
1240 /* When target does not have ctors and dtors, we call all constructor
1241 and destructor by special initialization/destruction function
1242 recognized by collect2.
1244 When we are going to build this function, collect all constructors and
1245 destructors and turn them into normal functions. */
1248 record_cdtor_fn (struct cgraph_node
*node
)
1250 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
))
1251 VEC_safe_push (tree
, heap
, static_ctors
, node
->symbol
.decl
);
1252 if (DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
1253 VEC_safe_push (tree
, heap
, static_dtors
, node
->symbol
.decl
);
1254 node
= cgraph_get_node (node
->symbol
.decl
);
1255 DECL_DISREGARD_INLINE_LIMITS (node
->symbol
.decl
) = 1;
1258 /* Define global constructors/destructor functions for the CDTORS, of
1259 which they are LEN. The CDTORS are sorted by initialization
1260 priority. If CTOR_P is true, these are constructors; otherwise,
1261 they are destructors. */
1264 build_cdtor (bool ctor_p
, VEC (tree
, heap
) *cdtors
)
1267 size_t len
= VEC_length (tree
, cdtors
);
1274 priority_type priority
;
1282 fn
= VEC_index (tree
, cdtors
, j
);
1283 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1286 else if (p
!= priority
)
1292 /* When there is only one cdtor and target supports them, do nothing. */
1294 && targetm
.have_ctors_dtors
)
1299 /* Find the next batch of constructors/destructors with the same
1300 initialization priority. */
1304 fn
= VEC_index (tree
, cdtors
, i
);
1305 call
= build_call_expr (fn
, 0);
1307 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1309 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1310 /* We do not want to optimize away pure/const calls here.
1311 When optimizing, these should be already removed, when not
1312 optimizing, we want user to be able to breakpoint in them. */
1313 TREE_SIDE_EFFECTS (call
) = 1;
1314 append_to_statement_list (call
, &body
);
1316 gcc_assert (body
!= NULL_TREE
);
1317 /* Generate a function to call all the function of like
1319 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1323 /* Comparison function for qsort. P1 and P2 are actually of type
1324 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1325 used to determine the sort order. */
1328 compare_ctor (const void *p1
, const void *p2
)
1335 f1
= *(const tree
*)p1
;
1336 f2
= *(const tree
*)p2
;
1337 priority1
= DECL_INIT_PRIORITY (f1
);
1338 priority2
= DECL_INIT_PRIORITY (f2
);
1340 if (priority1
< priority2
)
1342 else if (priority1
> priority2
)
1345 /* Ensure a stable sort. Constructors are executed in backwarding
1346 order to make LTO initialize braries first. */
1347 return DECL_UID (f2
) - DECL_UID (f1
);
1350 /* Comparison function for qsort. P1 and P2 are actually of type
1351 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1352 used to determine the sort order. */
1355 compare_dtor (const void *p1
, const void *p2
)
1362 f1
= *(const tree
*)p1
;
1363 f2
= *(const tree
*)p2
;
1364 priority1
= DECL_FINI_PRIORITY (f1
);
1365 priority2
= DECL_FINI_PRIORITY (f2
);
1367 if (priority1
< priority2
)
1369 else if (priority1
> priority2
)
1372 /* Ensure a stable sort. */
1373 return DECL_UID (f1
) - DECL_UID (f2
);
1376 /* Generate functions to call static constructors and destructors
1377 for targets that do not support .ctors/.dtors sections. These
1378 functions have magic names which are detected by collect2. */
1381 build_cdtor_fns (void)
1383 if (!VEC_empty (tree
, static_ctors
))
1385 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1386 VEC_qsort (tree
, static_ctors
, compare_ctor
);
1387 build_cdtor (/*ctor_p=*/true, static_ctors
);
1390 if (!VEC_empty (tree
, static_dtors
))
1392 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1393 VEC_qsort (tree
, static_dtors
, compare_dtor
);
1394 build_cdtor (/*ctor_p=*/false, static_dtors
);
1398 /* Look for constructors and destructors and produce function calling them.
1399 This is needed for targets not supporting ctors or dtors, but we perform the
1400 transformation also at linktime to merge possibly numberous
1401 constructors/destructors into single function to improve code locality and
1405 ipa_cdtor_merge (void)
1407 struct cgraph_node
*node
;
1408 FOR_EACH_DEFINED_FUNCTION (node
)
1409 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
)
1410 || DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
1411 record_cdtor_fn (node
);
1413 VEC_free (tree
, heap
, static_ctors
);
1414 VEC_free (tree
, heap
, static_dtors
);
1418 /* Perform the pass when we have no ctors/dtors support
1419 or at LTO time to merge multiple constructors into single
1423 gate_ipa_cdtor_merge (void)
1425 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1428 struct ipa_opt_pass_d pass_ipa_cdtor_merge
=
1433 gate_ipa_cdtor_merge
, /* gate */
1434 ipa_cdtor_merge
, /* execute */
1437 0, /* static_pass_number */
1438 TV_CGRAPHOPT
, /* tv_id */
1439 0, /* properties_required */
1440 0, /* properties_provided */
1441 0, /* properties_destroyed */
1442 0, /* todo_flags_start */
1443 0 /* todo_flags_finish */
1445 NULL
, /* generate_summary */
1446 NULL
, /* write_summary */
1447 NULL
, /* read_summary */
1448 NULL
, /* write_optimization_summary */
1449 NULL
, /* read_optimization_summary */
1450 NULL
, /* stmt_fixup */
1452 NULL
, /* function_transform */
1453 NULL
/* variable_transform */