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"
35 /* Fill array order with all nodes with output flag set in the reverse
39 cgraph_postorder (struct cgraph_node
**order
)
41 struct cgraph_node
*node
, *node2
;
44 struct cgraph_edge
*edge
, last
;
47 struct cgraph_node
**stack
=
48 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
50 /* We have to deal with cycles nicely, so use a depth first traversal
51 output algorithm. Ignore the fact that some functions won't need
52 to be output and put them into order as well, so we get dependencies
53 right through inline functions. */
54 for (node
= cgraph_nodes
; node
; node
= node
->next
)
56 for (pass
= 0; pass
< 2; pass
++)
57 for (node
= cgraph_nodes
; node
; node
= node
->next
)
60 || (!node
->address_taken
61 && !node
->global
.inlined_to
62 && !cgraph_only_called_directly_p (node
))))
68 node
->aux
= node
->callers
;
71 while (node2
->aux
!= &last
)
73 edge
= (struct cgraph_edge
*) node2
->aux
;
74 if (edge
->next_caller
)
75 node2
->aux
= edge
->next_caller
;
78 /* Break possible cycles involving always-inline
79 functions by ignoring edges from always-inline
80 functions to non-always-inline functions. */
81 if (edge
->caller
->local
.disregard_inline_limits
82 && !edge
->callee
->local
.disregard_inline_limits
)
84 if (!edge
->caller
->aux
)
86 if (!edge
->caller
->callers
)
87 edge
->caller
->aux
= &last
;
89 edge
->caller
->aux
= edge
->caller
->callers
;
90 stack
[stack_size
++] = node2
;
95 if (node2
->aux
== &last
)
97 order
[order_pos
++] = node2
;
99 node2
= stack
[--stack_size
];
106 for (node
= cgraph_nodes
; node
; node
= node
->next
)
111 /* Look for all functions inlined to NODE and update their inlined_to pointers
115 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
117 struct cgraph_edge
*e
;
118 for (e
= node
->callees
; e
; e
= e
->next_callee
)
119 if (e
->callee
->global
.inlined_to
)
121 e
->callee
->global
.inlined_to
= inlined_to
;
122 update_inlined_to_pointer (e
->callee
, inlined_to
);
126 /* Add cgraph NODE to queue starting at FIRST.
128 The queue is linked via AUX pointers and terminated by pointer to 1.
129 We enqueue nodes at two occasions: when we find them reachable or when we find
130 their bodies needed for further clonning. In the second case we mark them
131 by pointer to 2 after processing so they are re-queue when they become
135 enqueue_cgraph_node (struct cgraph_node
*node
, struct cgraph_node
**first
)
137 /* Node is still in queue; do nothing. */
138 if (node
->aux
&& node
->aux
!= (void *) 2)
140 /* Node was already processed as unreachable, re-enqueue
141 only if it became reachable now. */
142 if (node
->aux
== (void *)2 && !node
->reachable
)
148 /* Add varpool NODE to queue starting at FIRST. */
151 enqueue_varpool_node (struct varpool_node
*node
, struct varpool_node
**first
)
157 /* Process references. */
160 process_references (struct ipa_ref_list
*list
,
161 struct cgraph_node
**first
,
162 struct varpool_node
**first_varpool
,
163 bool before_inlining_p
)
167 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
169 if (ref
->refered_type
== IPA_REF_CGRAPH
)
171 struct cgraph_node
*node
= ipa_ref_node (ref
);
174 && (!DECL_EXTERNAL (node
->decl
)
175 || before_inlining_p
))
176 node
->reachable
= true;
177 enqueue_cgraph_node (node
, first
);
181 struct varpool_node
*node
= ipa_ref_varpool_node (ref
);
184 varpool_mark_needed_node (node
);
185 enqueue_varpool_node (node
, first_varpool
);
191 /* Return true when function can be marked local. */
194 cgraph_local_node_p (struct cgraph_node
*node
)
196 return (cgraph_only_called_directly_p (node
)
198 && !DECL_EXTERNAL (node
->decl
)
199 && !node
->local
.externally_visible
200 && !node
->reachable_from_other_partition
201 && !node
->in_other_partition
);
204 /* Perform reachability analysis and reclaim all unreachable nodes.
205 If BEFORE_INLINING_P is true this function is called before inlining
206 decisions has been made. If BEFORE_INLINING_P is false this function also
207 removes unneeded bodies of extern inline functions. */
210 cgraph_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
212 struct cgraph_node
*first
= (struct cgraph_node
*) (void *) 1;
213 struct varpool_node
*first_varpool
= (struct varpool_node
*) (void *) 1;
214 struct cgraph_node
*node
, *next
;
215 struct varpool_node
*vnode
, *vnext
;
216 bool changed
= false;
218 #ifdef ENABLE_CHECKING
222 fprintf (file
, "\nReclaiming functions:");
223 #ifdef ENABLE_CHECKING
224 for (node
= cgraph_nodes
; node
; node
= node
->next
)
225 gcc_assert (!node
->aux
);
226 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
227 gcc_assert (!vnode
->aux
);
229 varpool_reset_queue ();
230 /* Mark functions whose bodies are obviously needed.
231 This is mostly when they can be referenced externally. Inline clones
232 are special since their declarations are shared with master clone and thus
233 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
234 for (node
= cgraph_nodes
; node
; node
= node
->next
)
235 if (node
->analyzed
&& !node
->global
.inlined_to
236 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node
)
237 /* Keep around virtual functions for possible devirtualization. */
238 || (before_inlining_p
239 && DECL_VIRTUAL_P (node
->decl
)
240 && (DECL_COMDAT (node
->decl
) || DECL_EXTERNAL (node
->decl
)))
241 /* Also external functions with address taken are better to stay
242 for indirect inlining. */
243 || (before_inlining_p
244 && DECL_EXTERNAL (node
->decl
)
245 && node
->address_taken
)))
247 gcc_assert (!node
->global
.inlined_to
);
248 enqueue_cgraph_node (node
, &first
);
249 node
->reachable
= true;
253 gcc_assert (!node
->aux
);
254 node
->reachable
= false;
257 /* Mark variables that are obviously needed. */
258 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
260 vnode
->next_needed
= NULL
;
261 vnode
->prev_needed
= NULL
;
262 if ((vnode
->analyzed
|| vnode
->force_output
)
263 && !varpool_can_remove_if_no_refs (vnode
))
265 vnode
->needed
= false;
266 varpool_mark_needed_node (vnode
);
267 enqueue_varpool_node (vnode
, &first_varpool
);
270 vnode
->needed
= false;
273 /* Perform reachability analysis. As a special case do not consider
274 extern inline functions not inlined as live because we won't output
277 We maintain two worklist, one for cgraph nodes other for varpools and
278 are finished once both are empty. */
280 while (first
!= (struct cgraph_node
*) (void *) 1
281 || first_varpool
!= (struct varpool_node
*) (void *) 1)
283 if (first
!= (struct cgraph_node
*) (void *) 1)
285 struct cgraph_edge
*e
;
287 first
= (struct cgraph_node
*) first
->aux
;
288 if (!node
->reachable
)
289 node
->aux
= (void *)2;
291 /* If we found this node reachable, first mark on the callees
292 reachable too, unless they are direct calls to extern inline functions
293 we decided to not inline. */
296 for (e
= node
->callees
; e
; e
= e
->next_callee
)
298 if (!e
->callee
->reachable
300 && (!e
->inline_failed
301 || !DECL_EXTERNAL (e
->callee
->decl
)
302 || before_inlining_p
))
303 e
->callee
->reachable
= true;
304 enqueue_cgraph_node (e
->callee
, &first
);
306 process_references (&node
->ref_list
, &first
, &first_varpool
, before_inlining_p
);
309 /* If any function in a comdat group is reachable, force
310 all other functions in the same comdat group to be
312 if (node
->same_comdat_group
314 && !node
->global
.inlined_to
)
316 for (next
= node
->same_comdat_group
;
318 next
= next
->same_comdat_group
)
319 if (!next
->reachable
)
321 next
->reachable
= true;
322 enqueue_cgraph_node (next
, &first
);
326 /* We can freely remove inline clones even if they are cloned, however if
327 function is clone of real clone, we must keep it around in order to
328 make materialize_clones produce function body with the changes
330 while (node
->clone_of
&& !node
->clone_of
->aux
331 && !gimple_has_body_p (node
->decl
))
333 bool noninline
= node
->clone_of
->decl
!= node
->decl
;
334 node
= node
->clone_of
;
335 if (noninline
&& !node
->reachable
&& !node
->aux
)
337 enqueue_cgraph_node (node
, &first
);
342 if (first_varpool
!= (struct varpool_node
*) (void *) 1)
344 vnode
= first_varpool
;
345 first_varpool
= (struct varpool_node
*)first_varpool
->aux
;
347 process_references (&vnode
->ref_list
, &first
, &first_varpool
, before_inlining_p
);
348 /* If any function in a comdat group is reachable, force
349 all other functions in the same comdat group to be
351 if (vnode
->same_comdat_group
)
353 struct varpool_node
*next
;
354 for (next
= vnode
->same_comdat_group
;
356 next
= next
->same_comdat_group
)
359 varpool_mark_needed_node (next
);
360 enqueue_varpool_node (next
, &first_varpool
);
366 /* Remove unreachable nodes.
368 Completely unreachable functions can be fully removed from the callgraph.
369 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
370 callgraph (so we still have edges to them). We remove function body then.
372 Also we need to care functions that are unreachable but we need to keep them around
373 for later clonning. In this case we also turn them to unanalyzed nodes, but
374 keep the body around. */
375 for (node
= cgraph_nodes
; node
; node
= next
)
378 if (node
->aux
&& !node
->reachable
)
380 cgraph_node_remove_callees (node
);
381 ipa_remove_all_references (&node
->ref_list
);
382 node
->analyzed
= false;
383 node
->local
.inlinable
= false;
387 struct cgraph_edge
*e
;
392 node
->global
.inlined_to
= NULL
;
394 fprintf (file
, " %s", cgraph_node_name (node
));
395 /* See if there is reachable caller. */
396 for (e
= node
->callers
; e
&& !found
; e
= e
->next_caller
)
397 if (e
->caller
->reachable
)
399 for (i
= 0; (ipa_ref_list_refering_iterate (&node
->ref_list
, i
, ref
)
401 if (ref
->refering_type
== IPA_REF_CGRAPH
402 && ipa_ref_refering_node (ref
)->reachable
)
404 else if (ref
->refering_type
== IPA_REF_VARPOOL
405 && ipa_ref_refering_varpool_node (ref
)->needed
)
408 /* If so, we need to keep node in the callgraph. */
413 struct cgraph_node
*clone
;
415 /* If there are still clones, we must keep body around.
416 Otherwise we can just remove the body but keep the clone. */
417 for (clone
= node
->clones
; clone
;
418 clone
= clone
->next_sibling_clone
)
423 cgraph_release_function_body (node
);
424 node
->local
.inlinable
= false;
425 if (node
->prev_sibling_clone
)
426 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
427 else if (node
->clone_of
)
428 node
->clone_of
->clones
= node
->next_sibling_clone
;
429 if (node
->next_sibling_clone
)
430 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
432 node
->former_clone_of
= node
->clone_of
->decl
;
433 node
->clone_of
= NULL
;
434 node
->next_sibling_clone
= NULL
;
435 node
->prev_sibling_clone
= NULL
;
438 gcc_assert (!clone
->in_other_partition
);
439 node
->analyzed
= false;
441 cgraph_node_remove_callees (node
);
442 ipa_remove_all_references (&node
->ref_list
);
447 cgraph_remove_node (node
);
452 for (node
= cgraph_nodes
; node
; node
= node
->next
)
454 /* Inline clones might be kept around so their materializing allows further
455 cloning. If the function the clone is inlined into is removed, we need
456 to turn it into normal cone. */
457 if (node
->global
.inlined_to
460 gcc_assert (node
->clones
);
461 node
->global
.inlined_to
= NULL
;
462 update_inlined_to_pointer (node
, node
);
468 fprintf (file
, "\n");
470 /* We must release unused extern inlines or sanity checking will fail. Rest of transformations
471 are undesirable at -O0 since we do not want to remove anything. */
476 fprintf (file
, "Reclaiming variables:");
477 for (vnode
= varpool_nodes
; vnode
; vnode
= vnext
)
483 fprintf (file
, " %s", varpool_node_name (vnode
));
484 varpool_remove_node (vnode
);
489 /* Now update address_taken flags and try to promote functions to be local. */
492 fprintf (file
, "\nClearing address taken flags:");
493 for (node
= cgraph_nodes
; node
; node
= node
->next
)
494 if (node
->address_taken
495 && !node
->reachable_from_other_partition
)
500 for (i
= 0; ipa_ref_list_refering_iterate (&node
->ref_list
, i
, ref
)
503 gcc_assert (ref
->use
== IPA_REF_ADDR
);
509 fprintf (file
, " %s", cgraph_node_name (node
));
510 node
->address_taken
= false;
512 if (cgraph_local_node_p (node
))
514 node
->local
.local
= true;
516 fprintf (file
, " (local)");
521 #ifdef ENABLE_CHECKING
525 /* Reclaim alias pairs for functions that have disappeared from the
527 remove_unreachable_alias_pairs ();
532 /* Discover variables that have no longer address taken or that are read only
533 and update their flags.
535 FIXME: This can not be done in between gimplify and omp_expand since
536 readonly flag plays role on what is shared and what is not. Currently we do
537 this transformation as part of whole program visibility and re-do at
538 ipa-reference pass (to take into account clonning), but it would
539 make sense to do it before early optimizations. */
542 ipa_discover_readonly_nonaddressable_vars (void)
544 struct varpool_node
*vnode
;
546 fprintf (dump_file
, "Clearing variable flags:");
547 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
548 if (vnode
->finalized
&& varpool_all_refs_explicit_p (vnode
)
549 && (TREE_ADDRESSABLE (vnode
->decl
) || !TREE_READONLY (vnode
->decl
)))
551 bool written
= false;
552 bool address_taken
= false;
555 for (i
= 0; ipa_ref_list_refering_iterate (&vnode
->ref_list
, i
, ref
)
556 && (!written
|| !address_taken
); i
++)
560 address_taken
= true;
568 if (TREE_ADDRESSABLE (vnode
->decl
) && !address_taken
)
571 fprintf (dump_file
, " %s (addressable)", varpool_node_name (vnode
));
572 TREE_ADDRESSABLE (vnode
->decl
) = 0;
574 if (!TREE_READONLY (vnode
->decl
) && !address_taken
&& !written
575 /* Making variable in explicit section readonly can cause section
577 See e.g. gcc.c-torture/compile/pr23237.c */
578 && DECL_SECTION_NAME (vnode
->decl
) == NULL
)
581 fprintf (dump_file
, " %s (read-only)", varpool_node_name (vnode
));
582 TREE_READONLY (vnode
->decl
) = 1;
586 fprintf (dump_file
, "\n");
589 /* Return true when there is a reference to node and it is not vtable. */
591 cgraph_address_taken_from_non_vtable_p (struct cgraph_node
*node
)
595 for (i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
); i
++)
597 struct varpool_node
*node
;
598 if (ref
->refered_type
== IPA_REF_CGRAPH
)
600 node
= ipa_ref_varpool_node (ref
);
601 if (!DECL_VIRTUAL_P (node
->decl
))
607 /* COMDAT functions must be shared only if they have address taken,
608 otherwise we can produce our own private implementation with
610 Return true when turning COMDAT functoin static can not lead to wrong
611 code when the resulting object links with a library defining same COMDAT.
613 Virtual functions do have their addresses taken from the vtables,
614 but in C++ there is no way to compare their addresses for equality. */
617 cgraph_comdat_can_be_unshared_p (struct cgraph_node
*node
)
619 if ((cgraph_address_taken_from_non_vtable_p (node
)
620 && !DECL_VIRTUAL_P (node
->decl
))
623 if (node
->same_comdat_group
)
625 struct cgraph_node
*next
;
627 /* If more than one function is in the same COMDAT group, it must
628 be shared even if just one function in the comdat group has
630 for (next
= node
->same_comdat_group
;
631 next
!= node
; next
= next
->same_comdat_group
)
632 if (cgraph_address_taken_from_non_vtable_p (node
)
633 && !DECL_VIRTUAL_P (next
->decl
))
639 /* Return true when function NODE should be considered externally visible. */
642 cgraph_externally_visible_p (struct cgraph_node
*node
, bool whole_program
, bool aliased
)
644 struct cgraph_node
*alias
;
645 if (!node
->local
.finalized
)
647 if (!DECL_COMDAT (node
->decl
)
648 && (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
)))
651 /* Do not even try to be smart about aliased nodes. Until we properly
652 represent everything by same body alias, these are just evil. */
656 /* Do not try to localize built-in functions yet. One of problems is that we
657 end up mangling their asm for WHOPR that makes it impossible to call them
658 using the implicit built-in declarations anymore. Similarly this enables
659 us to remove them as unreachable before actual calls may appear during
660 expansion or folding. */
661 if (DECL_BUILT_IN (node
->decl
))
664 /* FIXME: We get wrong symbols with asm aliases in callgraph and LTO.
665 This is because very little of code knows that assembler name needs to
666 mangled. Avoid touching declarations with user asm name set to mask
667 some of the problems. */
668 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
)
669 && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
))[0]=='*')
672 /* If linker counts on us, we must preserve the function. */
673 if (cgraph_used_from_object_file_p (node
))
675 if (DECL_PRESERVE_P (node
->decl
))
677 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (node
->decl
)))
679 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
680 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (node
->decl
)))
682 /* When doing LTO or whole program, we can bring COMDAT functoins static.
683 This improves code quality and we know we will duplicate them at most twice
684 (in the case that we are not using plugin and link with object file
685 implementing same COMDAT) */
686 if ((in_lto_p
|| whole_program
)
687 && DECL_COMDAT (node
->decl
)
688 && cgraph_comdat_can_be_unshared_p (node
))
691 /* See if we have linker information about symbol not being used or
692 if we need to make guess based on the declaration.
694 Even if the linker clams the symbol is unused, never bring internal
695 symbols that are declared by user as used or externally visible.
696 This is needed for i.e. references from asm statements. */
697 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
698 if (alias
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
)
700 if (!alias
&& node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
703 /* When doing link time optimizations, hidden symbols become local. */
705 && (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
706 || DECL_VISIBILITY (node
->decl
) == VISIBILITY_INTERNAL
)
707 /* Be sure that node is defined in IR file, not in other object
708 file. In that case we don't set used_from_other_object_file. */
711 else if (!whole_program
)
714 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
720 /* Return true when variable VNODE should be considered externally visible. */
723 varpool_externally_visible_p (struct varpool_node
*vnode
, bool aliased
)
725 struct varpool_node
*alias
;
726 if (!DECL_COMDAT (vnode
->decl
) && !TREE_PUBLIC (vnode
->decl
))
729 /* Do not even try to be smart about aliased nodes. Until we properly
730 represent everything by same body alias, these are just evil. */
734 /* If linker counts on us, we must preserve the function. */
735 if (varpool_used_from_object_file_p (vnode
))
738 /* FIXME: We get wrong symbols with asm aliases in callgraph and LTO.
739 This is because very little of code knows that assembler name needs to
740 mangled. Avoid touching declarations with user asm name set to mask
741 some of the problems. */
742 if (DECL_ASSEMBLER_NAME_SET_P (vnode
->decl
)
743 && IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (vnode
->decl
))[0]=='*')
746 if (DECL_PRESERVE_P (vnode
->decl
))
748 if (lookup_attribute ("externally_visible",
749 DECL_ATTRIBUTES (vnode
->decl
)))
751 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
752 && lookup_attribute ("dllexport",
753 DECL_ATTRIBUTES (vnode
->decl
)))
756 /* See if we have linker information about symbol not being used or
757 if we need to make guess based on the declaration.
759 Even if the linker clams the symbol is unused, never bring internal
760 symbols that are declared by user as used or externally visible.
761 This is needed for i.e. references from asm statements. */
762 if (varpool_used_from_object_file_p (vnode
))
764 for (alias
= vnode
->extra_name
; alias
; alias
= alias
->next
)
765 if (alias
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
)
767 if (!alias
&& vnode
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
770 /* As a special case, the COMDAT virutal tables can be unshared.
771 In LTO mode turn vtables into static variables. The variable is readonly,
772 so this does not enable more optimization, but referring static var
773 is faster for dynamic linking. Also this match logic hidding vtables
774 from LTO symbol tables. */
775 if ((in_lto_p
|| flag_whole_program
)
776 && !vnode
->force_output
777 && DECL_COMDAT (vnode
->decl
) && DECL_VIRTUAL_P (vnode
->decl
))
780 /* When doing link time optimizations, hidden symbols become local. */
782 && (DECL_VISIBILITY (vnode
->decl
) == VISIBILITY_HIDDEN
783 || DECL_VISIBILITY (vnode
->decl
) == VISIBILITY_INTERNAL
)
784 /* Be sure that node is defined in IR file, not in other object
785 file. In that case we don't set used_from_other_object_file. */
788 else if (!flag_whole_program
)
791 /* Do not attempt to privatize COMDATS by default.
792 This would break linking with C++ libraries sharing
795 FIXME: We can do so for readonly vars with no address taken and
796 possibly also for vtables since no direct pointer comparsion is done.
797 It might be interesting to do so to reduce linking overhead. */
798 if (DECL_COMDAT (vnode
->decl
) || DECL_WEAK (vnode
->decl
))
803 /* Dissolve the same_comdat_group list in which NODE resides. */
806 dissolve_same_comdat_group_list (struct cgraph_node
*node
)
808 struct cgraph_node
*n
= node
, *next
;
811 next
= n
->same_comdat_group
;
812 n
->same_comdat_group
= NULL
;
818 /* Mark visibility of all functions.
820 A local function is one whose calls can occur only in the current
821 compilation unit and all its calls are explicit, so we can change
822 its calling convention. We simply mark all static functions whose
823 address is not taken as local.
825 We also change the TREE_PUBLIC flag of all declarations that are public
826 in language point of view but we want to overwrite this default
827 via visibilities for the backend point of view. */
830 function_and_variable_visibility (bool whole_program
)
832 struct cgraph_node
*node
;
833 struct varpool_node
*vnode
;
834 struct pointer_set_t
*aliased_nodes
= pointer_set_create ();
835 struct pointer_set_t
*aliased_vnodes
= pointer_set_create ();
839 /* Discover aliased nodes. */
840 FOR_EACH_VEC_ELT (alias_pair
, alias_pairs
, i
, p
)
843 fprintf (dump_file
, "Alias %s->%s",
844 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
845 IDENTIFIER_POINTER (p
->target
));
847 if ((node
= cgraph_node_for_asm (p
->target
)) != NULL
848 && !DECL_EXTERNAL (node
->decl
))
852 /* Weakrefs alias symbols from other compilation unit. In the case
853 the destination of weakref became available because of LTO, we must
854 mark it as needed. */
856 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
))
858 cgraph_mark_needed_node (node
);
859 gcc_assert (node
->needed
);
860 pointer_set_insert (aliased_nodes
, node
);
862 fprintf (dump_file
, " node %s/%i",
863 cgraph_node_name (node
), node
->uid
);
865 else if ((vnode
= varpool_node_for_asm (p
->target
)) != NULL
866 && !DECL_EXTERNAL (vnode
->decl
))
868 /* Weakrefs alias symbols from other compilation unit. In the case
869 the destination of weakref became available because of LTO, we must
870 mark it as needed. */
872 && lookup_attribute ("weakref", DECL_ATTRIBUTES (p
->decl
))
874 varpool_mark_needed_node (vnode
);
875 gcc_assert (vnode
->needed
);
876 pointer_set_insert (aliased_vnodes
, vnode
);
878 fprintf (dump_file
, " varpool node %s",
879 varpool_node_name (vnode
));
882 fprintf (dump_file
, "\n");
885 for (node
= cgraph_nodes
; node
; node
= node
->next
)
887 int flags
= flags_from_decl_or_type (node
->decl
);
889 /* Optimize away PURE and CONST constructors and destructors. */
891 && (flags
& (ECF_CONST
| ECF_PURE
))
892 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
894 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
895 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
898 /* Frontends and alias code marks nodes as needed before parsing is finished.
899 We may end up marking as node external nodes where this flag is meaningless
902 && (DECL_EXTERNAL (node
->decl
) || !node
->analyzed
))
905 /* C++ FE on lack of COMDAT support create local COMDAT functions
906 (that ought to be shared but can not due to object format
907 limitations). It is neccesary to keep the flag to make rest of C++ FE
908 happy. Clear the flag here to avoid confusion in middle-end. */
909 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
910 DECL_COMDAT (node
->decl
) = 0;
911 /* For external decls stop tracking same_comdat_group, it doesn't matter
912 what comdat group they are in when they won't be emitted in this TU,
913 and simplifies later passes. */
914 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
))
916 #ifdef ENABLE_CHECKING
917 struct cgraph_node
*n
;
919 for (n
= node
->same_comdat_group
;
921 n
= n
->same_comdat_group
)
922 /* If at least one of same comdat group functions is external,
923 all of them have to be, otherwise it is a front-end bug. */
924 gcc_assert (DECL_EXTERNAL (n
->decl
));
926 dissolve_same_comdat_group_list (node
);
928 gcc_assert ((!DECL_WEAK (node
->decl
) && !DECL_COMDAT (node
->decl
))
929 || TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
));
930 if (cgraph_externally_visible_p (node
, whole_program
,
931 pointer_set_contains (aliased_nodes
,
934 gcc_assert (!node
->global
.inlined_to
);
935 node
->local
.externally_visible
= true;
938 node
->local
.externally_visible
= false;
939 if (!node
->local
.externally_visible
&& node
->analyzed
940 && !DECL_EXTERNAL (node
->decl
))
942 struct cgraph_node
*alias
;
943 gcc_assert (whole_program
|| in_lto_p
|| !TREE_PUBLIC (node
->decl
));
944 cgraph_make_decl_local (node
->decl
);
945 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
946 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
947 cgraph_make_decl_local (alias
->decl
);
948 if (node
->same_comdat_group
)
949 /* cgraph_externally_visible_p has already checked all other nodes
950 in the group and they will all be made local. We need to
951 dissolve the group at once so that the predicate does not
953 dissolve_same_comdat_group_list (node
);
955 node
->local
.local
= cgraph_local_node_p (node
);
957 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
959 /* weak flag makes no sense on local variables. */
960 gcc_assert (!DECL_WEAK (vnode
->decl
)
961 || TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
));
962 /* In several cases declarations can not be common:
964 - when declaration has initializer
966 - when it has specific section
967 - when it resides in non-generic address space.
968 - if declaration is local, it will get into .local common section
969 so common flag is not needed. Frontends still produce these in
970 certain cases, such as for:
972 static int a __attribute__ ((common))
974 Canonicalize things here and clear the redundant flag. */
975 if (DECL_COMMON (vnode
->decl
)
976 && (!(TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
))
977 || (DECL_INITIAL (vnode
->decl
)
978 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
979 || DECL_WEAK (vnode
->decl
)
980 || DECL_SECTION_NAME (vnode
->decl
) != NULL
981 || ! (ADDR_SPACE_GENERIC_P
982 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
983 DECL_COMMON (vnode
->decl
) = 0;
985 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
987 if (!vnode
->finalized
)
990 && varpool_externally_visible_p
992 pointer_set_contains (aliased_vnodes
, vnode
)))
993 vnode
->externally_visible
= true;
995 vnode
->externally_visible
= false;
996 if (!vnode
->externally_visible
)
998 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->decl
));
999 cgraph_make_decl_local (vnode
->decl
);
1000 vnode
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
1002 gcc_assert (TREE_STATIC (vnode
->decl
));
1004 pointer_set_destroy (aliased_nodes
);
1005 pointer_set_destroy (aliased_vnodes
);
1009 fprintf (dump_file
, "\nMarking local functions:");
1010 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1011 if (node
->local
.local
)
1012 fprintf (dump_file
, " %s", cgraph_node_name (node
));
1013 fprintf (dump_file
, "\n\n");
1014 fprintf (dump_file
, "\nMarking externally visible functions:");
1015 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1016 if (node
->local
.externally_visible
)
1017 fprintf (dump_file
, " %s", cgraph_node_name (node
));
1018 fprintf (dump_file
, "\n\n");
1019 fprintf (dump_file
, "\nMarking externally visible variables:");
1020 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
1021 if (vnode
->externally_visible
)
1022 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
1023 fprintf (dump_file
, "\n\n");
1025 cgraph_function_flags_ready
= true;
1029 /* Local function pass handling visibilities. This happens before LTO streaming
1030 so in particular -fwhole-program should be ignored at this level. */
1033 local_function_and_variable_visibility (void)
1035 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
1038 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility
=
1042 "visibility", /* name */
1044 local_function_and_variable_visibility
,/* execute */
1047 0, /* static_pass_number */
1048 TV_CGRAPHOPT
, /* tv_id */
1049 0, /* properties_required */
1050 0, /* properties_provided */
1051 0, /* properties_destroyed */
1052 0, /* todo_flags_start */
1053 TODO_remove_functions
| TODO_dump_cgraph
1054 | TODO_ggc_collect
/* todo_flags_finish */
1058 /* Do not re-run on ltrans stage. */
1061 gate_whole_program_function_and_variable_visibility (void)
1063 return !flag_ltrans
;
1066 /* Bring functionss local at LTO time whith -fwhole-program. */
1069 whole_program_function_and_variable_visibility (void)
1071 struct cgraph_node
*node
;
1072 struct varpool_node
*vnode
;
1074 function_and_variable_visibility (flag_whole_program
);
1076 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1077 if ((node
->local
.externally_visible
&& !DECL_COMDAT (node
->decl
))
1078 && node
->local
.finalized
)
1079 cgraph_mark_needed_node (node
);
1080 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
1081 if (vnode
->externally_visible
&& !DECL_COMDAT (vnode
->decl
))
1082 varpool_mark_needed_node (vnode
);
1085 fprintf (dump_file
, "\nNeeded variables:");
1086 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
1088 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
1089 fprintf (dump_file
, "\n\n");
1092 ipa_discover_readonly_nonaddressable_vars ();
1096 struct ipa_opt_pass_d pass_ipa_whole_program_visibility
=
1100 "whole-program", /* name */
1101 gate_whole_program_function_and_variable_visibility
,/* gate */
1102 whole_program_function_and_variable_visibility
,/* execute */
1105 0, /* static_pass_number */
1106 TV_CGRAPHOPT
, /* tv_id */
1107 0, /* properties_required */
1108 0, /* properties_provided */
1109 0, /* properties_destroyed */
1110 0, /* todo_flags_start */
1111 TODO_remove_functions
| TODO_dump_cgraph
1112 | TODO_ggc_collect
/* todo_flags_finish */
1114 NULL
, /* generate_summary */
1115 NULL
, /* write_summary */
1116 NULL
, /* read_summary */
1117 NULL
, /* write_optimization_summary */
1118 NULL
, /* read_optimization_summary */
1119 NULL
, /* stmt_fixup */
1121 NULL
, /* function_transform */
1122 NULL
, /* variable_transform */
1125 /* Hash a cgraph node set element. */
1128 hash_cgraph_node_set_element (const void *p
)
1130 const_cgraph_node_set_element element
= (const_cgraph_node_set_element
) p
;
1131 return htab_hash_pointer (element
->node
);
1134 /* Compare two cgraph node set elements. */
1137 eq_cgraph_node_set_element (const void *p1
, const void *p2
)
1139 const_cgraph_node_set_element e1
= (const_cgraph_node_set_element
) p1
;
1140 const_cgraph_node_set_element e2
= (const_cgraph_node_set_element
) p2
;
1142 return e1
->node
== e2
->node
;
1145 /* Create a new cgraph node set. */
1148 cgraph_node_set_new (void)
1150 cgraph_node_set new_node_set
;
1152 new_node_set
= ggc_alloc_cgraph_node_set_def ();
1153 new_node_set
->hashtab
= htab_create_ggc (10,
1154 hash_cgraph_node_set_element
,
1155 eq_cgraph_node_set_element
,
1157 new_node_set
->nodes
= NULL
;
1158 return new_node_set
;
1161 /* Add cgraph_node NODE to cgraph_node_set SET. */
1164 cgraph_node_set_add (cgraph_node_set set
, struct cgraph_node
*node
)
1167 cgraph_node_set_element element
;
1168 struct cgraph_node_set_element_def dummy
;
1171 slot
= htab_find_slot (set
->hashtab
, &dummy
, INSERT
);
1173 if (*slot
!= HTAB_EMPTY_ENTRY
)
1175 element
= (cgraph_node_set_element
) *slot
;
1176 gcc_assert (node
== element
->node
1177 && (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1182 /* Insert node into hash table. */
1183 element
= ggc_alloc_cgraph_node_set_element_def ();
1184 element
->node
= node
;
1185 element
->index
= VEC_length (cgraph_node_ptr
, set
->nodes
);
1188 /* Insert into node vector. */
1189 VEC_safe_push (cgraph_node_ptr
, gc
, set
->nodes
, node
);
1192 /* Remove cgraph_node NODE from cgraph_node_set SET. */
1195 cgraph_node_set_remove (cgraph_node_set set
, struct cgraph_node
*node
)
1197 void **slot
, **last_slot
;
1198 cgraph_node_set_element element
, last_element
;
1199 struct cgraph_node
*last_node
;
1200 struct cgraph_node_set_element_def dummy
;
1203 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1207 element
= (cgraph_node_set_element
) *slot
;
1208 gcc_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1211 /* Remove from vector. We do this by swapping node with the last element
1213 last_node
= VEC_pop (cgraph_node_ptr
, set
->nodes
);
1214 if (last_node
!= node
)
1216 dummy
.node
= last_node
;
1217 last_slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1218 last_element
= (cgraph_node_set_element
) *last_slot
;
1219 gcc_assert (last_element
);
1221 /* Move the last element to the original spot of NODE. */
1222 last_element
->index
= element
->index
;
1223 VEC_replace (cgraph_node_ptr
, set
->nodes
, last_element
->index
,
1227 /* Remove element from hash table. */
1228 htab_clear_slot (set
->hashtab
, slot
);
1232 /* Find NODE in SET and return an iterator to it if found. A null iterator
1233 is returned if NODE is not in SET. */
1235 cgraph_node_set_iterator
1236 cgraph_node_set_find (cgraph_node_set set
, struct cgraph_node
*node
)
1239 struct cgraph_node_set_element_def dummy
;
1240 cgraph_node_set_element element
;
1241 cgraph_node_set_iterator csi
;
1244 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1246 csi
.index
= (unsigned) ~0;
1249 element
= (cgraph_node_set_element
) *slot
;
1250 gcc_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1252 csi
.index
= element
->index
;
1259 /* Dump content of SET to file F. */
1262 dump_cgraph_node_set (FILE *f
, cgraph_node_set set
)
1264 cgraph_node_set_iterator iter
;
1266 for (iter
= csi_start (set
); !csi_end_p (iter
); csi_next (&iter
))
1268 struct cgraph_node
*node
= csi_node (iter
);
1269 fprintf (f
, " %s/%i", cgraph_node_name (node
), node
->uid
);
1274 /* Dump content of SET to stderr. */
1277 debug_cgraph_node_set (cgraph_node_set set
)
1279 dump_cgraph_node_set (stderr
, set
);
1282 /* Hash a varpool node set element. */
1285 hash_varpool_node_set_element (const void *p
)
1287 const_varpool_node_set_element element
= (const_varpool_node_set_element
) p
;
1288 return htab_hash_pointer (element
->node
);
1291 /* Compare two varpool node set elements. */
1294 eq_varpool_node_set_element (const void *p1
, const void *p2
)
1296 const_varpool_node_set_element e1
= (const_varpool_node_set_element
) p1
;
1297 const_varpool_node_set_element e2
= (const_varpool_node_set_element
) p2
;
1299 return e1
->node
== e2
->node
;
1302 /* Create a new varpool node set. */
1305 varpool_node_set_new (void)
1307 varpool_node_set new_node_set
;
1309 new_node_set
= ggc_alloc_varpool_node_set_def ();
1310 new_node_set
->hashtab
= htab_create_ggc (10,
1311 hash_varpool_node_set_element
,
1312 eq_varpool_node_set_element
,
1314 new_node_set
->nodes
= NULL
;
1315 return new_node_set
;
1318 /* Add varpool_node NODE to varpool_node_set SET. */
1321 varpool_node_set_add (varpool_node_set set
, struct varpool_node
*node
)
1324 varpool_node_set_element element
;
1325 struct varpool_node_set_element_def dummy
;
1328 slot
= htab_find_slot (set
->hashtab
, &dummy
, INSERT
);
1330 if (*slot
!= HTAB_EMPTY_ENTRY
)
1332 element
= (varpool_node_set_element
) *slot
;
1333 gcc_assert (node
== element
->node
1334 && (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1339 /* Insert node into hash table. */
1340 element
= ggc_alloc_varpool_node_set_element_def ();
1341 element
->node
= node
;
1342 element
->index
= VEC_length (varpool_node_ptr
, set
->nodes
);
1345 /* Insert into node vector. */
1346 VEC_safe_push (varpool_node_ptr
, gc
, set
->nodes
, node
);
1349 /* Remove varpool_node NODE from varpool_node_set SET. */
1352 varpool_node_set_remove (varpool_node_set set
, struct varpool_node
*node
)
1354 void **slot
, **last_slot
;
1355 varpool_node_set_element element
, last_element
;
1356 struct varpool_node
*last_node
;
1357 struct varpool_node_set_element_def dummy
;
1360 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1364 element
= (varpool_node_set_element
) *slot
;
1365 gcc_assert (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1368 /* Remove from vector. We do this by swapping node with the last element
1370 last_node
= VEC_pop (varpool_node_ptr
, set
->nodes
);
1371 if (last_node
!= node
)
1373 dummy
.node
= last_node
;
1374 last_slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1375 last_element
= (varpool_node_set_element
) *last_slot
;
1376 gcc_assert (last_element
);
1378 /* Move the last element to the original spot of NODE. */
1379 last_element
->index
= element
->index
;
1380 VEC_replace (varpool_node_ptr
, set
->nodes
, last_element
->index
,
1384 /* Remove element from hash table. */
1385 htab_clear_slot (set
->hashtab
, slot
);
1389 /* Find NODE in SET and return an iterator to it if found. A null iterator
1390 is returned if NODE is not in SET. */
1392 varpool_node_set_iterator
1393 varpool_node_set_find (varpool_node_set set
, struct varpool_node
*node
)
1396 struct varpool_node_set_element_def dummy
;
1397 varpool_node_set_element element
;
1398 varpool_node_set_iterator vsi
;
1401 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1403 vsi
.index
= (unsigned) ~0;
1406 element
= (varpool_node_set_element
) *slot
;
1407 gcc_assert (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1409 vsi
.index
= element
->index
;
1416 /* Dump content of SET to file F. */
1419 dump_varpool_node_set (FILE *f
, varpool_node_set set
)
1421 varpool_node_set_iterator iter
;
1423 for (iter
= vsi_start (set
); !vsi_end_p (iter
); vsi_next (&iter
))
1425 struct varpool_node
*node
= vsi_node (iter
);
1426 fprintf (f
, " %s", varpool_node_name (node
));
1431 /* Dump content of SET to stderr. */
1434 debug_varpool_node_set (varpool_node_set set
)
1436 dump_varpool_node_set (stderr
, set
);
1440 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1445 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1446 struct cgraph_edge
*e
;
1448 bool something_changed
= false;
1451 order_pos
= cgraph_postorder (order
);
1452 for (i
= order_pos
- 1; i
>= 0; i
--)
1454 if (order
[i
]->local
.local
&& cgraph_propagate_frequency (order
[i
]))
1456 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1457 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
1459 something_changed
= true;
1460 e
->callee
->aux
= (void *)1;
1463 order
[i
]->aux
= NULL
;
1466 while (something_changed
)
1468 something_changed
= false;
1469 for (i
= order_pos
- 1; i
>= 0; i
--)
1471 if (order
[i
]->aux
&& cgraph_propagate_frequency (order
[i
]))
1473 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1474 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
1476 something_changed
= true;
1477 e
->callee
->aux
= (void *)1;
1480 order
[i
]->aux
= NULL
;
1488 gate_ipa_profile (void)
1490 return flag_ipa_profile
;
1493 struct ipa_opt_pass_d pass_ipa_profile
=
1497 "ipa-profile", /* name */
1498 gate_ipa_profile
, /* gate */
1499 ipa_profile
, /* execute */
1502 0, /* static_pass_number */
1503 TV_IPA_PROFILE
, /* tv_id */
1504 0, /* properties_required */
1505 0, /* properties_provided */
1506 0, /* properties_destroyed */
1507 0, /* todo_flags_start */
1508 0 /* todo_flags_finish */
1510 NULL
, /* generate_summary */
1511 NULL
, /* write_summary */
1512 NULL
, /* read_summary */
1513 NULL
, /* write_optimization_summary */
1514 NULL
, /* read_optimization_summary */
1515 NULL
, /* stmt_fixup */
1517 NULL
, /* function_transform */
1518 NULL
/* variable_transform */
1521 /* Generate and emit a static constructor or destructor. WHICH must
1522 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1523 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1524 initialization priority for this constructor or destructor.
1526 FINAL specify whether the externally visible name for collect2 should
1530 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
1532 static int counter
= 0;
1534 tree decl
, name
, resdecl
;
1536 /* The priority is encoded in the constructor or destructor name.
1537 collect2 will sort the names and arrange that they are called at
1540 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1542 /* Proudce sane name but one not recognizable by collect2, just for the
1543 case we fail to inline the function. */
1544 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
1545 name
= get_file_function_name (which_buf
);
1547 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1548 build_function_type_list (void_type_node
, NULL_TREE
));
1549 current_function_decl
= decl
;
1551 resdecl
= build_decl (input_location
,
1552 RESULT_DECL
, NULL_TREE
, void_type_node
);
1553 DECL_ARTIFICIAL (resdecl
) = 1;
1554 DECL_RESULT (decl
) = resdecl
;
1555 DECL_CONTEXT (resdecl
) = decl
;
1557 allocate_struct_function (decl
, false);
1559 TREE_STATIC (decl
) = 1;
1560 TREE_USED (decl
) = 1;
1561 DECL_ARTIFICIAL (decl
) = 1;
1562 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1563 DECL_SAVED_TREE (decl
) = body
;
1564 if (!targetm
.have_ctors_dtors
&& final
)
1566 TREE_PUBLIC (decl
) = 1;
1567 DECL_PRESERVE_P (decl
) = 1;
1569 DECL_UNINLINABLE (decl
) = 1;
1571 DECL_INITIAL (decl
) = make_node (BLOCK
);
1572 TREE_USED (DECL_INITIAL (decl
)) = 1;
1574 DECL_SOURCE_LOCATION (decl
) = input_location
;
1575 cfun
->function_end_locus
= input_location
;
1580 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1581 decl_init_priority_insert (decl
, priority
);
1584 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1585 decl_fini_priority_insert (decl
, priority
);
1591 gimplify_function_tree (decl
);
1593 cgraph_add_new_function (decl
, false);
1596 current_function_decl
= NULL
;
1599 /* Generate and emit a static constructor or destructor. WHICH must
1600 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1601 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1602 initialization priority for this constructor or destructor. */
1605 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1607 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
1610 /* A vector of FUNCTION_DECLs declared as static constructors. */
1611 static VEC(tree
, heap
) *static_ctors
;
1612 /* A vector of FUNCTION_DECLs declared as static destructors. */
1613 static VEC(tree
, heap
) *static_dtors
;
1615 /* When target does not have ctors and dtors, we call all constructor
1616 and destructor by special initialization/destruction function
1617 recognized by collect2.
1619 When we are going to build this function, collect all constructors and
1620 destructors and turn them into normal functions. */
1623 record_cdtor_fn (struct cgraph_node
*node
)
1625 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
1626 VEC_safe_push (tree
, heap
, static_ctors
, node
->decl
);
1627 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1628 VEC_safe_push (tree
, heap
, static_dtors
, node
->decl
);
1629 node
= cgraph_node (node
->decl
);
1630 node
->local
.disregard_inline_limits
= 1;
1633 /* Define global constructors/destructor functions for the CDTORS, of
1634 which they are LEN. The CDTORS are sorted by initialization
1635 priority. If CTOR_P is true, these are constructors; otherwise,
1636 they are destructors. */
1639 build_cdtor (bool ctor_p
, VEC (tree
, heap
) *cdtors
)
1642 size_t len
= VEC_length (tree
, cdtors
);
1649 priority_type priority
;
1657 fn
= VEC_index (tree
, cdtors
, j
);
1658 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1661 else if (p
!= priority
)
1667 /* When there is only one cdtor and target supports them, do nothing. */
1669 && targetm
.have_ctors_dtors
)
1674 /* Find the next batch of constructors/destructors with the same
1675 initialization priority. */
1679 fn
= VEC_index (tree
, cdtors
, i
);
1680 call
= build_call_expr (fn
, 0);
1682 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1684 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1685 /* We do not want to optimize away pure/const calls here.
1686 When optimizing, these should be already removed, when not
1687 optimizing, we want user to be able to breakpoint in them. */
1688 TREE_SIDE_EFFECTS (call
) = 1;
1689 append_to_statement_list (call
, &body
);
1691 gcc_assert (body
!= NULL_TREE
);
1692 /* Generate a function to call all the function of like
1694 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1698 /* Comparison function for qsort. P1 and P2 are actually of type
1699 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1700 used to determine the sort order. */
1703 compare_ctor (const void *p1
, const void *p2
)
1710 f1
= *(const tree
*)p1
;
1711 f2
= *(const tree
*)p2
;
1712 priority1
= DECL_INIT_PRIORITY (f1
);
1713 priority2
= DECL_INIT_PRIORITY (f2
);
1715 if (priority1
< priority2
)
1717 else if (priority1
> priority2
)
1720 /* Ensure a stable sort. Constructors are executed in backwarding
1721 order to make LTO initialize braries first. */
1722 return DECL_UID (f2
) - DECL_UID (f1
);
1725 /* Comparison function for qsort. P1 and P2 are actually of type
1726 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1727 used to determine the sort order. */
1730 compare_dtor (const void *p1
, const void *p2
)
1737 f1
= *(const tree
*)p1
;
1738 f2
= *(const tree
*)p2
;
1739 priority1
= DECL_FINI_PRIORITY (f1
);
1740 priority2
= DECL_FINI_PRIORITY (f2
);
1742 if (priority1
< priority2
)
1744 else if (priority1
> priority2
)
1747 /* Ensure a stable sort. */
1748 return DECL_UID (f1
) - DECL_UID (f2
);
1751 /* Generate functions to call static constructors and destructors
1752 for targets that do not support .ctors/.dtors sections. These
1753 functions have magic names which are detected by collect2. */
1756 build_cdtor_fns (void)
1758 if (!VEC_empty (tree
, static_ctors
))
1760 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1761 VEC_qsort (tree
, static_ctors
, compare_ctor
);
1762 build_cdtor (/*ctor_p=*/true, static_ctors
);
1765 if (!VEC_empty (tree
, static_dtors
))
1767 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1768 VEC_qsort (tree
, static_dtors
, compare_dtor
);
1769 build_cdtor (/*ctor_p=*/false, static_dtors
);
1773 /* Look for constructors and destructors and produce function calling them.
1774 This is needed for targets not supporting ctors or dtors, but we perform the
1775 transformation also at linktime to merge possibly numberous
1776 constructors/destructors into single function to improve code locality and
1780 ipa_cdtor_merge (void)
1782 struct cgraph_node
*node
;
1783 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1785 && (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1786 || DECL_STATIC_DESTRUCTOR (node
->decl
)))
1787 record_cdtor_fn (node
);
1789 VEC_free (tree
, heap
, static_ctors
);
1790 VEC_free (tree
, heap
, static_dtors
);
1794 /* Perform the pass when we have no ctors/dtors support
1795 or at LTO time to merge multiple constructors into single
1799 gate_ipa_cdtor_merge (void)
1801 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1804 struct ipa_opt_pass_d pass_ipa_cdtor_merge
=
1809 gate_ipa_cdtor_merge
, /* gate */
1810 ipa_cdtor_merge
, /* execute */
1813 0, /* static_pass_number */
1814 TV_CGRAPHOPT
, /* tv_id */
1815 0, /* properties_required */
1816 0, /* properties_provided */
1817 0, /* properties_destroyed */
1818 0, /* todo_flags_start */
1819 0 /* todo_flags_finish */
1821 NULL
, /* generate_summary */
1822 NULL
, /* write_summary */
1823 NULL
, /* read_summary */
1824 NULL
, /* write_optimization_summary */
1825 NULL
, /* read_optimization_summary */
1826 NULL
, /* stmt_fixup */
1828 NULL
, /* function_transform */
1829 NULL
/* variable_transform */