1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010
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 NODE can be removed from callgraph
192 if all direct calls are eliminated. */
195 varpool_can_remove_if_no_refs (struct varpool_node
*node
)
197 return (!node
->force_output
&& !node
->used_from_other_partition
198 && (DECL_COMDAT (node
->decl
) || !node
->externally_visible
));
201 /* Return true when function can be marked local. */
204 cgraph_local_node_p (struct cgraph_node
*node
)
206 return (cgraph_only_called_directly_p (node
)
208 && !DECL_EXTERNAL (node
->decl
)
209 && !node
->local
.externally_visible
210 && !node
->reachable_from_other_partition
211 && !node
->in_other_partition
);
214 /* Perform reachability analysis and reclaim all unreachable nodes.
215 If BEFORE_INLINING_P is true this function is called before inlining
216 decisions has been made. If BEFORE_INLINING_P is false this function also
217 removes unneeded bodies of extern inline functions. */
220 cgraph_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
222 struct cgraph_node
*first
= (struct cgraph_node
*) (void *) 1;
223 struct varpool_node
*first_varpool
= (struct varpool_node
*) (void *) 1;
224 struct cgraph_node
*node
, *next
;
225 struct varpool_node
*vnode
, *vnext
;
226 bool changed
= false;
228 #ifdef ENABLE_CHECKING
232 fprintf (file
, "\nReclaiming functions:");
233 #ifdef ENABLE_CHECKING
234 for (node
= cgraph_nodes
; node
; node
= node
->next
)
235 gcc_assert (!node
->aux
);
236 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
237 gcc_assert (!vnode
->aux
);
239 varpool_reset_queue ();
240 /* Mark functions whose bodies are obviously needed.
241 This is mostly when they can be referenced externally. Inline clones
242 are special since their declarations are shared with master clone and thus
243 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
244 for (node
= cgraph_nodes
; node
; node
= node
->next
)
245 if (node
->analyzed
&& !node
->global
.inlined_to
246 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node
)
247 /* Keep around virtual functions for possible devirtualization. */
248 || (before_inlining_p
249 && DECL_VIRTUAL_P (node
->decl
)
250 && (DECL_COMDAT (node
->decl
) || DECL_EXTERNAL (node
->decl
)))
251 /* Also external functions with address taken are better to stay
252 for indirect inlining. */
253 || (before_inlining_p
254 && DECL_EXTERNAL (node
->decl
)
255 && node
->address_taken
)))
257 gcc_assert (!node
->global
.inlined_to
);
258 enqueue_cgraph_node (node
, &first
);
259 node
->reachable
= true;
263 gcc_assert (!node
->aux
);
264 node
->reachable
= false;
267 /* Mark variables that are obviously needed. */
268 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
270 vnode
->next_needed
= NULL
;
271 vnode
->prev_needed
= NULL
;
272 if (!varpool_can_remove_if_no_refs (vnode
))
274 vnode
->needed
= false;
275 varpool_mark_needed_node (vnode
);
276 enqueue_varpool_node (vnode
, &first_varpool
);
279 vnode
->needed
= false;
282 /* Perform reachability analysis. As a special case do not consider
283 extern inline functions not inlined as live because we won't output
286 We maintain two worklist, one for cgraph nodes other for varpools and
287 are finished once both are empty. */
289 while (first
!= (struct cgraph_node
*) (void *) 1
290 || first_varpool
!= (struct varpool_node
*) (void *) 1)
292 if (first
!= (struct cgraph_node
*) (void *) 1)
294 struct cgraph_edge
*e
;
296 first
= (struct cgraph_node
*) first
->aux
;
297 if (!node
->reachable
)
298 node
->aux
= (void *)2;
300 /* If we found this node reachable, first mark on the callees
301 reachable too, unless they are direct calls to extern inline functions
302 we decided to not inline. */
305 for (e
= node
->callees
; e
; e
= e
->next_callee
)
307 if (!e
->callee
->reachable
309 && (!e
->inline_failed
310 || !DECL_EXTERNAL (e
->callee
->decl
)
311 || before_inlining_p
))
312 e
->callee
->reachable
= true;
313 enqueue_cgraph_node (e
->callee
, &first
);
315 process_references (&node
->ref_list
, &first
, &first_varpool
, before_inlining_p
);
318 /* If any function in a comdat group is reachable, force
319 all other functions in the same comdat group to be
321 if (node
->same_comdat_group
323 && !node
->global
.inlined_to
)
325 for (next
= node
->same_comdat_group
;
327 next
= next
->same_comdat_group
)
328 if (!next
->reachable
)
330 next
->reachable
= true;
331 enqueue_cgraph_node (next
, &first
);
335 /* We can freely remove inline clones even if they are cloned, however if
336 function is clone of real clone, we must keep it around in order to
337 make materialize_clones produce function body with the changes
339 while (node
->clone_of
&& !node
->clone_of
->aux
340 && !gimple_has_body_p (node
->decl
))
342 bool noninline
= node
->clone_of
->decl
!= node
->decl
;
343 node
= node
->clone_of
;
344 if (noninline
&& !node
->reachable
&& !node
->aux
)
346 enqueue_cgraph_node (node
, &first
);
351 if (first_varpool
!= (struct varpool_node
*) (void *) 1)
353 vnode
= first_varpool
;
354 first_varpool
= (struct varpool_node
*)first_varpool
->aux
;
356 process_references (&vnode
->ref_list
, &first
, &first_varpool
, before_inlining_p
);
357 /* If any function in a comdat group is reachable, force
358 all other functions in the same comdat group to be
360 if (vnode
->same_comdat_group
)
362 struct varpool_node
*next
;
363 for (next
= vnode
->same_comdat_group
;
365 next
= next
->same_comdat_group
)
368 varpool_mark_needed_node (next
);
369 enqueue_varpool_node (next
, &first_varpool
);
375 /* Remove unreachable nodes.
377 Completely unreachable functions can be fully removed from the callgraph.
378 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
379 callgraph (so we still have edges to them). We remove function body then.
381 Also we need to care functions that are unreachable but we need to keep them around
382 for later clonning. In this case we also turn them to unanalyzed nodes, but
383 keep the body around. */
384 for (node
= cgraph_nodes
; node
; node
= next
)
387 if (node
->aux
&& !node
->reachable
)
389 cgraph_node_remove_callees (node
);
390 ipa_remove_all_references (&node
->ref_list
);
391 node
->analyzed
= false;
392 node
->local
.inlinable
= false;
396 struct cgraph_edge
*e
;
401 node
->global
.inlined_to
= NULL
;
403 fprintf (file
, " %s", cgraph_node_name (node
));
404 /* See if there is reachable caller. */
405 for (e
= node
->callers
; e
&& !found
; e
= e
->next_caller
)
406 if (e
->caller
->reachable
)
408 for (i
= 0; (ipa_ref_list_refering_iterate (&node
->ref_list
, i
, ref
)
410 if (ref
->refering_type
== IPA_REF_CGRAPH
411 && ipa_ref_refering_node (ref
)->reachable
)
413 else if (ref
->refering_type
== IPA_REF_VARPOOL
414 && ipa_ref_refering_varpool_node (ref
)->needed
)
417 /* If so, we need to keep node in the callgraph. */
422 struct cgraph_node
*clone
;
424 /* If there are still clones, we must keep body around.
425 Otherwise we can just remove the body but keep the clone. */
426 for (clone
= node
->clones
; clone
;
427 clone
= clone
->next_sibling_clone
)
432 cgraph_release_function_body (node
);
433 node
->local
.inlinable
= false;
434 if (node
->prev_sibling_clone
)
435 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
436 else if (node
->clone_of
)
437 node
->clone_of
->clones
= node
->next_sibling_clone
;
438 if (node
->next_sibling_clone
)
439 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
440 #ifdef ENABLE_CHECKING
442 node
->former_clone_of
= node
->clone_of
->decl
;
444 node
->clone_of
= NULL
;
445 node
->next_sibling_clone
= NULL
;
446 node
->prev_sibling_clone
= NULL
;
449 gcc_assert (!clone
->in_other_partition
);
450 node
->analyzed
= false;
452 cgraph_node_remove_callees (node
);
453 ipa_remove_all_references (&node
->ref_list
);
458 cgraph_remove_node (node
);
463 for (node
= cgraph_nodes
; node
; node
= node
->next
)
465 /* Inline clones might be kept around so their materializing allows further
466 cloning. If the function the clone is inlined into is removed, we need
467 to turn it into normal cone. */
468 if (node
->global
.inlined_to
471 gcc_assert (node
->clones
);
472 node
->global
.inlined_to
= NULL
;
473 update_inlined_to_pointer (node
, node
);
479 fprintf (file
, "\n");
481 /* We must release unused extern inlines or sanity checking will fail. Rest of transformations
482 are undesirable at -O0 since we do not want to remove anything. */
487 fprintf (file
, "Reclaiming variables:");
488 for (vnode
= varpool_nodes
; vnode
; vnode
= vnext
)
494 fprintf (file
, " %s", varpool_node_name (vnode
));
495 varpool_remove_node (vnode
);
500 /* Now update address_taken flags and try to promote functions to be local. */
503 fprintf (file
, "\nClearing address taken flags:");
504 for (node
= cgraph_nodes
; node
; node
= node
->next
)
505 if (node
->address_taken
506 && !node
->reachable_from_other_partition
)
511 for (i
= 0; ipa_ref_list_refering_iterate (&node
->ref_list
, i
, ref
)
514 gcc_assert (ref
->use
== IPA_REF_ADDR
);
520 fprintf (file
, " %s", cgraph_node_name (node
));
521 node
->address_taken
= false;
523 if (cgraph_local_node_p (node
))
525 node
->local
.local
= true;
527 fprintf (file
, " (local)");
532 #ifdef ENABLE_CHECKING
536 /* Reclaim alias pairs for functions that have disappeared from the
538 remove_unreachable_alias_pairs ();
543 /* Discover variables that have no longer address taken or that are read only
544 and update their flags.
546 FIXME: This can not be done in between gimplify and omp_expand since
547 readonly flag plays role on what is shared and what is not. Currently we do
548 this transformation as part of whole program visibility and re-do at
549 ipa-reference pass (to take into account clonning), but it would
550 make sense to do it before early optimizations. */
553 ipa_discover_readonly_nonaddressable_vars (void)
555 struct varpool_node
*vnode
;
557 fprintf (dump_file
, "Clearing variable flags:");
558 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
559 if (vnode
->finalized
&& varpool_all_refs_explicit_p (vnode
)
560 && (TREE_ADDRESSABLE (vnode
->decl
) || !TREE_READONLY (vnode
->decl
)))
562 bool written
= false;
563 bool address_taken
= false;
566 for (i
= 0; ipa_ref_list_refering_iterate (&vnode
->ref_list
, i
, ref
)
567 && (!written
|| !address_taken
); i
++)
571 address_taken
= true;
579 if (TREE_ADDRESSABLE (vnode
->decl
) && !address_taken
)
582 fprintf (dump_file
, " %s (addressable)", varpool_node_name (vnode
));
583 TREE_ADDRESSABLE (vnode
->decl
) = 0;
585 if (!TREE_READONLY (vnode
->decl
) && !address_taken
&& !written
586 /* Making variable in explicit section readonly can cause section
588 See e.g. gcc.c-torture/compile/pr23237.c */
589 && DECL_SECTION_NAME (vnode
->decl
) == NULL
)
592 fprintf (dump_file
, " %s (read-only)", varpool_node_name (vnode
));
593 TREE_READONLY (vnode
->decl
) = 1;
597 fprintf (dump_file
, "\n");
600 /* Return true when function NODE should be considered externally visible. */
603 cgraph_externally_visible_p (struct cgraph_node
*node
, bool whole_program
, bool aliased
)
605 struct cgraph_node
*alias
;
606 if (!node
->local
.finalized
)
608 if (!DECL_COMDAT (node
->decl
)
609 && (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
)))
612 /* Do not even try to be smart about aliased nodes. Until we properly
613 represent everything by same body alias, these are just evil. */
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
->decl
))
622 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (node
->decl
)))
625 /* See if we have linker information about symbol not being used or
626 if we need to make guess based on the declaration.
628 Even if the linker clams the symbol is unused, never bring internal
629 symbols that are declared by user as used or externally visible.
630 This is needed for i.e. references from asm statements. */
631 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
632 if (alias
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
)
634 if (!alias
&& node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
637 /* When doing link time optimizations, hidden symbols become local. */
639 && (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
640 || DECL_VISIBILITY (node
->decl
) == VISIBILITY_INTERNAL
)
641 /* Be sure that node is defined in IR file, not in other object
642 file. In that case we don't set used_from_other_object_file. */
645 else if (!whole_program
)
647 /* COMDAT functions must be shared only if they have address taken,
648 otherwise we can produce our own private implementation with
650 else if (DECL_COMDAT (node
->decl
))
652 if (node
->address_taken
|| !node
->analyzed
)
654 if (node
->same_comdat_group
)
656 struct cgraph_node
*next
;
658 /* If more than one function is in the same COMDAT group, it must
659 be shared even if just one function in the comdat group has
661 for (next
= node
->same_comdat_group
;
663 next
= next
->same_comdat_group
)
664 if (next
->address_taken
|| !next
->analyzed
)
669 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
675 /* Return true when variable VNODE should be considered externally visible. */
678 varpool_externally_visible_p (struct varpool_node
*vnode
, bool aliased
)
680 struct varpool_node
*alias
;
681 if (!DECL_COMDAT (vnode
->decl
) && !TREE_PUBLIC (vnode
->decl
))
684 /* Do not even try to be smart about aliased nodes. Until we properly
685 represent everything by same body alias, these are just evil. */
689 /* If linker counts on us, we must preserve the function. */
690 if (varpool_used_from_object_file_p (vnode
))
693 if (DECL_PRESERVE_P (vnode
->decl
))
695 if (lookup_attribute ("externally_visible",
696 DECL_ATTRIBUTES (vnode
->decl
)))
699 /* See if we have linker information about symbol not being used or
700 if we need to make guess based on the declaration.
702 Even if the linker clams the symbol is unused, never bring internal
703 symbols that are declared by user as used or externally visible.
704 This is needed for i.e. references from asm statements. */
705 if (varpool_used_from_object_file_p (vnode
))
707 for (alias
= vnode
->extra_name
; alias
; alias
= alias
->next
)
708 if (alias
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
)
710 if (!alias
&& vnode
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
713 /* When doing link time optimizations, hidden symbols become local. */
715 && (DECL_VISIBILITY (vnode
->decl
) == VISIBILITY_HIDDEN
716 || DECL_VISIBILITY (vnode
->decl
) == VISIBILITY_INTERNAL
)
717 /* Be sure that node is defined in IR file, not in other object
718 file. In that case we don't set used_from_other_object_file. */
721 else if (!flag_whole_program
)
724 /* Do not attempt to privatize COMDATS by default.
725 This would break linking with C++ libraries sharing
728 FIXME: We can do so for readonly vars with no address taken and
729 possibly also for vtables since no direct pointer comparsion is done.
730 It might be interesting to do so to reduce linking overhead. */
731 if (DECL_COMDAT (vnode
->decl
) || DECL_WEAK (vnode
->decl
))
736 /* Dissolve the same_comdat_group list in which NODE resides. */
739 dissolve_same_comdat_group_list (struct cgraph_node
*node
)
741 struct cgraph_node
*n
= node
, *next
;
744 next
= n
->same_comdat_group
;
745 n
->same_comdat_group
= NULL
;
751 /* Mark visibility of all functions.
753 A local function is one whose calls can occur only in the current
754 compilation unit and all its calls are explicit, so we can change
755 its calling convention. We simply mark all static functions whose
756 address is not taken as local.
758 We also change the TREE_PUBLIC flag of all declarations that are public
759 in language point of view but we want to overwrite this default
760 via visibilities for the backend point of view. */
763 function_and_variable_visibility (bool whole_program
)
765 struct cgraph_node
*node
;
766 struct varpool_node
*vnode
;
767 struct pointer_set_t
*aliased_nodes
= pointer_set_create ();
768 struct pointer_set_t
*aliased_vnodes
= pointer_set_create ();
772 /* Discover aliased nodes. */
773 FOR_EACH_VEC_ELT (alias_pair
, alias_pairs
, i
, p
)
776 fprintf (dump_file
, "Alias %s->%s",
777 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
778 IDENTIFIER_POINTER (p
->target
));
780 if ((node
= cgraph_node_for_asm (p
->target
)) != NULL
)
782 gcc_assert (node
->needed
);
783 pointer_set_insert (aliased_nodes
, node
);
785 fprintf (dump_file
, " node %s/%i",
786 cgraph_node_name (node
), node
->uid
);
788 else if ((vnode
= varpool_node_for_asm (p
->target
)) != NULL
)
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 (node
= cgraph_nodes
; node
; node
= node
->next
)
802 int flags
= flags_from_decl_or_type (node
->decl
);
804 && (flags
& (ECF_CONST
| ECF_PURE
))
805 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
807 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
808 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
811 /* C++ FE on lack of COMDAT support create local COMDAT functions
812 (that ought to be shared but can not due to object format
813 limitations). It is neccesary to keep the flag to make rest of C++ FE
814 happy. Clear the flag here to avoid confusion in middle-end. */
815 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
816 DECL_COMDAT (node
->decl
) = 0;
817 /* For external decls stop tracking same_comdat_group, it doesn't matter
818 what comdat group they are in when they won't be emitted in this TU,
819 and simplifies later passes. */
820 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
))
822 #ifdef ENABLE_CHECKING
823 struct cgraph_node
*n
;
825 for (n
= node
->same_comdat_group
;
827 n
= n
->same_comdat_group
)
828 /* If at least one of same comdat group functions is external,
829 all of them have to be, otherwise it is a front-end bug. */
830 gcc_assert (DECL_EXTERNAL (n
->decl
));
832 dissolve_same_comdat_group_list (node
);
834 gcc_assert ((!DECL_WEAK (node
->decl
) && !DECL_COMDAT (node
->decl
))
835 || TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
));
836 if (cgraph_externally_visible_p (node
, whole_program
,
837 pointer_set_contains (aliased_nodes
,
840 gcc_assert (!node
->global
.inlined_to
);
841 node
->local
.externally_visible
= true;
844 node
->local
.externally_visible
= false;
845 if (!node
->local
.externally_visible
&& node
->analyzed
846 && !DECL_EXTERNAL (node
->decl
))
848 struct cgraph_node
*alias
;
849 gcc_assert (whole_program
|| in_lto_p
|| !TREE_PUBLIC (node
->decl
));
850 cgraph_make_decl_local (node
->decl
);
851 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
852 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
853 cgraph_make_decl_local (alias
->decl
);
854 if (node
->same_comdat_group
)
855 /* cgraph_externally_visible_p has already checked all other nodes
856 in the group and they will all be made local. We need to
857 dissolve the group at once so that the predicate does not
859 dissolve_same_comdat_group_list (node
);
861 node
->local
.local
= cgraph_local_node_p (node
);
863 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
865 /* weak flag makes no sense on local variables. */
866 gcc_assert (!DECL_WEAK (vnode
->decl
)
867 || TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
));
868 /* In several cases declarations can not be common:
870 - when declaration has initializer
872 - when it has specific section
873 - when it resides in non-generic address space.
874 - if declaration is local, it will get into .local common section
875 so common flag is not needed. Frontends still produce these in
876 certain cases, such as for:
878 static int a __attribute__ ((common))
880 Canonicalize things here and clear the redundant flag. */
881 if (DECL_COMMON (vnode
->decl
)
882 && (!(TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
))
883 || (DECL_INITIAL (vnode
->decl
)
884 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
885 || DECL_WEAK (vnode
->decl
)
886 || DECL_SECTION_NAME (vnode
->decl
) != NULL
887 || ! (ADDR_SPACE_GENERIC_P
888 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
889 DECL_COMMON (vnode
->decl
) = 0;
891 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
893 if (!vnode
->finalized
)
896 && varpool_externally_visible_p
898 pointer_set_contains (aliased_vnodes
, vnode
)))
899 vnode
->externally_visible
= true;
901 vnode
->externally_visible
= false;
902 if (!vnode
->externally_visible
)
904 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->decl
));
905 cgraph_make_decl_local (vnode
->decl
);
906 vnode
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
908 gcc_assert (TREE_STATIC (vnode
->decl
));
910 pointer_set_destroy (aliased_nodes
);
911 pointer_set_destroy (aliased_vnodes
);
915 fprintf (dump_file
, "\nMarking local functions:");
916 for (node
= cgraph_nodes
; node
; node
= node
->next
)
917 if (node
->local
.local
)
918 fprintf (dump_file
, " %s", cgraph_node_name (node
));
919 fprintf (dump_file
, "\n\n");
920 fprintf (dump_file
, "\nMarking externally visible functions:");
921 for (node
= cgraph_nodes
; node
; node
= node
->next
)
922 if (node
->local
.externally_visible
)
923 fprintf (dump_file
, " %s", cgraph_node_name (node
));
924 fprintf (dump_file
, "\n\n");
925 fprintf (dump_file
, "\nMarking externally visible variables:");
926 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
927 if (vnode
->externally_visible
)
928 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
929 fprintf (dump_file
, "\n\n");
931 cgraph_function_flags_ready
= true;
935 /* Local function pass handling visibilities. This happens before LTO streaming
936 so in particular -fwhole-program should be ignored at this level. */
939 local_function_and_variable_visibility (void)
941 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
&& !flag_whopr
);
944 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility
=
948 "visibility", /* name */
950 local_function_and_variable_visibility
,/* execute */
953 0, /* static_pass_number */
954 TV_CGRAPHOPT
, /* tv_id */
955 0, /* properties_required */
956 0, /* properties_provided */
957 0, /* properties_destroyed */
958 0, /* todo_flags_start */
959 TODO_remove_functions
| TODO_dump_cgraph
960 | TODO_ggc_collect
/* todo_flags_finish */
964 /* Do not re-run on ltrans stage. */
967 gate_whole_program_function_and_variable_visibility (void)
972 /* Bring functionss local at LTO time whith -fwhole-program. */
975 whole_program_function_and_variable_visibility (void)
977 struct cgraph_node
*node
;
978 struct varpool_node
*vnode
;
980 function_and_variable_visibility (flag_whole_program
);
982 for (node
= cgraph_nodes
; node
; node
= node
->next
)
983 if ((node
->local
.externally_visible
&& !DECL_COMDAT (node
->decl
))
984 && node
->local
.finalized
)
985 cgraph_mark_needed_node (node
);
986 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
987 if (vnode
->externally_visible
&& !DECL_COMDAT (vnode
->decl
))
988 varpool_mark_needed_node (vnode
);
991 fprintf (dump_file
, "\nNeeded variables:");
992 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
994 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
995 fprintf (dump_file
, "\n\n");
998 ipa_discover_readonly_nonaddressable_vars ();
1002 struct ipa_opt_pass_d pass_ipa_whole_program_visibility
=
1006 "whole-program", /* name */
1007 gate_whole_program_function_and_variable_visibility
,/* gate */
1008 whole_program_function_and_variable_visibility
,/* execute */
1011 0, /* static_pass_number */
1012 TV_CGRAPHOPT
, /* tv_id */
1013 0, /* properties_required */
1014 0, /* properties_provided */
1015 0, /* properties_destroyed */
1016 0, /* todo_flags_start */
1017 TODO_remove_functions
| TODO_dump_cgraph
1018 | TODO_ggc_collect
/* todo_flags_finish */
1020 NULL
, /* generate_summary */
1021 NULL
, /* write_summary */
1022 NULL
, /* read_summary */
1023 NULL
, /* write_optimization_summary */
1024 NULL
, /* read_optimization_summary */
1025 NULL
, /* stmt_fixup */
1027 NULL
, /* function_transform */
1028 NULL
, /* variable_transform */
1031 /* Hash a cgraph node set element. */
1034 hash_cgraph_node_set_element (const void *p
)
1036 const_cgraph_node_set_element element
= (const_cgraph_node_set_element
) p
;
1037 return htab_hash_pointer (element
->node
);
1040 /* Compare two cgraph node set elements. */
1043 eq_cgraph_node_set_element (const void *p1
, const void *p2
)
1045 const_cgraph_node_set_element e1
= (const_cgraph_node_set_element
) p1
;
1046 const_cgraph_node_set_element e2
= (const_cgraph_node_set_element
) p2
;
1048 return e1
->node
== e2
->node
;
1051 /* Create a new cgraph node set. */
1054 cgraph_node_set_new (void)
1056 cgraph_node_set new_node_set
;
1058 new_node_set
= ggc_alloc_cgraph_node_set_def ();
1059 new_node_set
->hashtab
= htab_create_ggc (10,
1060 hash_cgraph_node_set_element
,
1061 eq_cgraph_node_set_element
,
1063 new_node_set
->nodes
= NULL
;
1064 return new_node_set
;
1067 /* Add cgraph_node NODE to cgraph_node_set SET. */
1070 cgraph_node_set_add (cgraph_node_set set
, struct cgraph_node
*node
)
1073 cgraph_node_set_element element
;
1074 struct cgraph_node_set_element_def dummy
;
1077 slot
= htab_find_slot (set
->hashtab
, &dummy
, INSERT
);
1079 if (*slot
!= HTAB_EMPTY_ENTRY
)
1081 element
= (cgraph_node_set_element
) *slot
;
1082 gcc_assert (node
== element
->node
1083 && (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1088 /* Insert node into hash table. */
1089 element
= ggc_alloc_cgraph_node_set_element_def ();
1090 element
->node
= node
;
1091 element
->index
= VEC_length (cgraph_node_ptr
, set
->nodes
);
1094 /* Insert into node vector. */
1095 VEC_safe_push (cgraph_node_ptr
, gc
, set
->nodes
, node
);
1098 /* Remove cgraph_node NODE from cgraph_node_set SET. */
1101 cgraph_node_set_remove (cgraph_node_set set
, struct cgraph_node
*node
)
1103 void **slot
, **last_slot
;
1104 cgraph_node_set_element element
, last_element
;
1105 struct cgraph_node
*last_node
;
1106 struct cgraph_node_set_element_def dummy
;
1109 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1113 element
= (cgraph_node_set_element
) *slot
;
1114 gcc_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1117 /* Remove from vector. We do this by swapping node with the last element
1119 last_node
= VEC_pop (cgraph_node_ptr
, set
->nodes
);
1120 if (last_node
!= node
)
1122 dummy
.node
= last_node
;
1123 last_slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1124 last_element
= (cgraph_node_set_element
) *last_slot
;
1125 gcc_assert (last_element
);
1127 /* Move the last element to the original spot of NODE. */
1128 last_element
->index
= element
->index
;
1129 VEC_replace (cgraph_node_ptr
, set
->nodes
, last_element
->index
,
1133 /* Remove element from hash table. */
1134 htab_clear_slot (set
->hashtab
, slot
);
1138 /* Find NODE in SET and return an iterator to it if found. A null iterator
1139 is returned if NODE is not in SET. */
1141 cgraph_node_set_iterator
1142 cgraph_node_set_find (cgraph_node_set set
, struct cgraph_node
*node
)
1145 struct cgraph_node_set_element_def dummy
;
1146 cgraph_node_set_element element
;
1147 cgraph_node_set_iterator csi
;
1150 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1152 csi
.index
= (unsigned) ~0;
1155 element
= (cgraph_node_set_element
) *slot
;
1156 gcc_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1158 csi
.index
= element
->index
;
1165 /* Dump content of SET to file F. */
1168 dump_cgraph_node_set (FILE *f
, cgraph_node_set set
)
1170 cgraph_node_set_iterator iter
;
1172 for (iter
= csi_start (set
); !csi_end_p (iter
); csi_next (&iter
))
1174 struct cgraph_node
*node
= csi_node (iter
);
1175 fprintf (f
, " %s/%i", cgraph_node_name (node
), node
->uid
);
1180 /* Dump content of SET to stderr. */
1183 debug_cgraph_node_set (cgraph_node_set set
)
1185 dump_cgraph_node_set (stderr
, set
);
1188 /* Hash a varpool node set element. */
1191 hash_varpool_node_set_element (const void *p
)
1193 const_varpool_node_set_element element
= (const_varpool_node_set_element
) p
;
1194 return htab_hash_pointer (element
->node
);
1197 /* Compare two varpool node set elements. */
1200 eq_varpool_node_set_element (const void *p1
, const void *p2
)
1202 const_varpool_node_set_element e1
= (const_varpool_node_set_element
) p1
;
1203 const_varpool_node_set_element e2
= (const_varpool_node_set_element
) p2
;
1205 return e1
->node
== e2
->node
;
1208 /* Create a new varpool node set. */
1211 varpool_node_set_new (void)
1213 varpool_node_set new_node_set
;
1215 new_node_set
= ggc_alloc_varpool_node_set_def ();
1216 new_node_set
->hashtab
= htab_create_ggc (10,
1217 hash_varpool_node_set_element
,
1218 eq_varpool_node_set_element
,
1220 new_node_set
->nodes
= NULL
;
1221 return new_node_set
;
1224 /* Add varpool_node NODE to varpool_node_set SET. */
1227 varpool_node_set_add (varpool_node_set set
, struct varpool_node
*node
)
1230 varpool_node_set_element element
;
1231 struct varpool_node_set_element_def dummy
;
1234 slot
= htab_find_slot (set
->hashtab
, &dummy
, INSERT
);
1236 if (*slot
!= HTAB_EMPTY_ENTRY
)
1238 element
= (varpool_node_set_element
) *slot
;
1239 gcc_assert (node
== element
->node
1240 && (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1245 /* Insert node into hash table. */
1246 element
= ggc_alloc_varpool_node_set_element_def ();
1247 element
->node
= node
;
1248 element
->index
= VEC_length (varpool_node_ptr
, set
->nodes
);
1251 /* Insert into node vector. */
1252 VEC_safe_push (varpool_node_ptr
, gc
, set
->nodes
, node
);
1255 /* Remove varpool_node NODE from varpool_node_set SET. */
1258 varpool_node_set_remove (varpool_node_set set
, struct varpool_node
*node
)
1260 void **slot
, **last_slot
;
1261 varpool_node_set_element element
, last_element
;
1262 struct varpool_node
*last_node
;
1263 struct varpool_node_set_element_def dummy
;
1266 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1270 element
= (varpool_node_set_element
) *slot
;
1271 gcc_assert (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1274 /* Remove from vector. We do this by swapping node with the last element
1276 last_node
= VEC_pop (varpool_node_ptr
, set
->nodes
);
1277 if (last_node
!= node
)
1279 dummy
.node
= last_node
;
1280 last_slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1281 last_element
= (varpool_node_set_element
) *last_slot
;
1282 gcc_assert (last_element
);
1284 /* Move the last element to the original spot of NODE. */
1285 last_element
->index
= element
->index
;
1286 VEC_replace (varpool_node_ptr
, set
->nodes
, last_element
->index
,
1290 /* Remove element from hash table. */
1291 htab_clear_slot (set
->hashtab
, slot
);
1295 /* Find NODE in SET and return an iterator to it if found. A null iterator
1296 is returned if NODE is not in SET. */
1298 varpool_node_set_iterator
1299 varpool_node_set_find (varpool_node_set set
, struct varpool_node
*node
)
1302 struct varpool_node_set_element_def dummy
;
1303 varpool_node_set_element element
;
1304 varpool_node_set_iterator vsi
;
1307 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1309 vsi
.index
= (unsigned) ~0;
1312 element
= (varpool_node_set_element
) *slot
;
1313 gcc_assert (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1315 vsi
.index
= element
->index
;
1322 /* Dump content of SET to file F. */
1325 dump_varpool_node_set (FILE *f
, varpool_node_set set
)
1327 varpool_node_set_iterator iter
;
1329 for (iter
= vsi_start (set
); !vsi_end_p (iter
); vsi_next (&iter
))
1331 struct varpool_node
*node
= vsi_node (iter
);
1332 fprintf (f
, " %s", varpool_node_name (node
));
1337 /* Dump content of SET to stderr. */
1340 debug_varpool_node_set (varpool_node_set set
)
1342 dump_varpool_node_set (stderr
, set
);
1346 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1351 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1352 struct cgraph_edge
*e
;
1354 bool something_changed
= false;
1357 order_pos
= cgraph_postorder (order
);
1358 for (i
= order_pos
- 1; i
>= 0; i
--)
1360 if (order
[i
]->local
.local
&& cgraph_propagate_frequency (order
[i
]))
1362 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1363 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
1365 something_changed
= true;
1366 e
->callee
->aux
= (void *)1;
1369 order
[i
]->aux
= NULL
;
1372 while (something_changed
)
1374 something_changed
= false;
1375 for (i
= order_pos
- 1; i
>= 0; i
--)
1377 if (order
[i
]->aux
&& cgraph_propagate_frequency (order
[i
]))
1379 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1380 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
1382 something_changed
= true;
1383 e
->callee
->aux
= (void *)1;
1386 order
[i
]->aux
= NULL
;
1394 gate_ipa_profile (void)
1396 return flag_ipa_profile
;
1399 struct ipa_opt_pass_d pass_ipa_profile
=
1403 "ipa-profile", /* name */
1404 gate_ipa_profile
, /* gate */
1405 ipa_profile
, /* execute */
1408 0, /* static_pass_number */
1409 TV_IPA_PROFILE
, /* tv_id */
1410 0, /* properties_required */
1411 0, /* properties_provided */
1412 0, /* properties_destroyed */
1413 0, /* todo_flags_start */
1414 0 /* todo_flags_finish */
1416 NULL
, /* generate_summary */
1417 NULL
, /* write_summary */
1418 NULL
, /* read_summary */
1419 NULL
, /* write_optimization_summary */
1420 NULL
, /* read_optimization_summary */
1421 NULL
, /* stmt_fixup */
1423 NULL
, /* function_transform */
1424 NULL
/* variable_transform */
1427 /* Generate and emit a static constructor or destructor. WHICH must
1428 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1429 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1430 initialization priority for this constructor or destructor. */
1433 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1435 static int counter
= 0;
1437 tree decl
, name
, resdecl
;
1439 /* The priority is encoded in the constructor or destructor name.
1440 collect2 will sort the names and arrange that they are called at
1442 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1443 name
= get_file_function_name (which_buf
);
1445 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1446 build_function_type_list (void_type_node
, NULL_TREE
));
1447 current_function_decl
= decl
;
1449 resdecl
= build_decl (input_location
,
1450 RESULT_DECL
, NULL_TREE
, void_type_node
);
1451 DECL_ARTIFICIAL (resdecl
) = 1;
1452 DECL_RESULT (decl
) = resdecl
;
1453 DECL_CONTEXT (resdecl
) = decl
;
1455 allocate_struct_function (decl
, false);
1457 TREE_STATIC (decl
) = 1;
1458 TREE_USED (decl
) = 1;
1459 DECL_ARTIFICIAL (decl
) = 1;
1460 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1461 DECL_SAVED_TREE (decl
) = body
;
1462 if (!targetm
.have_ctors_dtors
)
1464 TREE_PUBLIC (decl
) = 1;
1465 DECL_PRESERVE_P (decl
) = 1;
1467 DECL_UNINLINABLE (decl
) = 1;
1469 DECL_INITIAL (decl
) = make_node (BLOCK
);
1470 TREE_USED (DECL_INITIAL (decl
)) = 1;
1472 DECL_SOURCE_LOCATION (decl
) = input_location
;
1473 cfun
->function_end_locus
= input_location
;
1478 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1479 decl_init_priority_insert (decl
, priority
);
1482 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1483 decl_fini_priority_insert (decl
, priority
);
1489 gimplify_function_tree (decl
);
1491 cgraph_add_new_function (decl
, false);
1494 current_function_decl
= NULL
;
1498 /* A vector of FUNCTION_DECLs declared as static constructors. */
1499 static VEC(tree
, heap
) *static_ctors
;
1500 /* A vector of FUNCTION_DECLs declared as static destructors. */
1501 static VEC(tree
, heap
) *static_dtors
;
1503 /* When target does not have ctors and dtors, we call all constructor
1504 and destructor by special initialization/destruction function
1505 recognized by collect2.
1507 When we are going to build this function, collect all constructors and
1508 destructors and turn them into normal functions. */
1511 record_cdtor_fn (struct cgraph_node
*node
)
1513 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
1514 VEC_safe_push (tree
, heap
, static_ctors
, node
->decl
);
1515 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1516 VEC_safe_push (tree
, heap
, static_dtors
, node
->decl
);
1517 node
= cgraph_node (node
->decl
);
1518 node
->local
.disregard_inline_limits
= 1;
1521 /* Define global constructors/destructor functions for the CDTORS, of
1522 which they are LEN. The CDTORS are sorted by initialization
1523 priority. If CTOR_P is true, these are constructors; otherwise,
1524 they are destructors. */
1527 build_cdtor (bool ctor_p
, VEC (tree
, heap
) *cdtors
)
1530 size_t len
= VEC_length (tree
, cdtors
);
1537 priority_type priority
;
1545 fn
= VEC_index (tree
, cdtors
, j
);
1546 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1549 else if (p
!= priority
)
1555 /* When there is only one cdtor and target supports them, do nothing. */
1557 && targetm
.have_ctors_dtors
)
1562 /* Find the next batch of constructors/destructors with the same
1563 initialization priority. */
1567 fn
= VEC_index (tree
, cdtors
, i
);
1568 call
= build_call_expr (fn
, 0);
1570 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1572 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1573 /* We do not want to optimize away pure/const calls here.
1574 When optimizing, these should be already removed, when not
1575 optimizing, we want user to be able to breakpoint in them. */
1576 TREE_SIDE_EFFECTS (call
) = 1;
1577 append_to_statement_list (call
, &body
);
1579 gcc_assert (body
!= NULL_TREE
);
1580 /* Generate a function to call all the function of like
1582 cgraph_build_static_cdtor (ctor_p
? 'I' : 'D', body
, priority
);
1586 /* Comparison function for qsort. P1 and P2 are actually of type
1587 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1588 used to determine the sort order. */
1591 compare_ctor (const void *p1
, const void *p2
)
1598 f1
= *(const tree
*)p1
;
1599 f2
= *(const tree
*)p2
;
1600 priority1
= DECL_INIT_PRIORITY (f1
);
1601 priority2
= DECL_INIT_PRIORITY (f2
);
1603 if (priority1
< priority2
)
1605 else if (priority1
> priority2
)
1608 /* Ensure a stable sort. Constructors are executed in backwarding
1609 order to make LTO initialize braries first. */
1610 return DECL_UID (f2
) - DECL_UID (f1
);
1613 /* Comparison function for qsort. P1 and P2 are actually of type
1614 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1615 used to determine the sort order. */
1618 compare_dtor (const void *p1
, const void *p2
)
1625 f1
= *(const tree
*)p1
;
1626 f2
= *(const tree
*)p2
;
1627 priority1
= DECL_FINI_PRIORITY (f1
);
1628 priority2
= DECL_FINI_PRIORITY (f2
);
1630 if (priority1
< priority2
)
1632 else if (priority1
> priority2
)
1635 /* Ensure a stable sort. */
1636 return DECL_UID (f1
) - DECL_UID (f2
);
1639 /* Generate functions to call static constructors and destructors
1640 for targets that do not support .ctors/.dtors sections. These
1641 functions have magic names which are detected by collect2. */
1644 build_cdtor_fns (void)
1646 if (!VEC_empty (tree
, static_ctors
))
1648 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1649 VEC_qsort (tree
, static_ctors
, compare_ctor
);
1650 build_cdtor (/*ctor_p=*/true, static_ctors
);
1653 if (!VEC_empty (tree
, static_dtors
))
1655 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1656 VEC_qsort (tree
, static_dtors
, compare_dtor
);
1657 build_cdtor (/*ctor_p=*/false, static_dtors
);
1661 /* Look for constructors and destructors and produce function calling them.
1662 This is needed for targets not supporting ctors or dtors, but we perform the
1663 transformation also at linktime to merge possibly numberous
1664 constructors/destructors into single function to improve code locality and
1668 ipa_cdtor_merge (void)
1670 struct cgraph_node
*node
;
1671 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1673 && (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1674 || DECL_STATIC_DESTRUCTOR (node
->decl
)))
1675 record_cdtor_fn (node
);
1677 VEC_free (tree
, heap
, static_ctors
);
1678 VEC_free (tree
, heap
, static_dtors
);
1682 /* Perform the pass when we have no ctors/dtors support
1683 or at LTO time to merge multiple constructors into single
1687 gate_ipa_cdtor_merge (void)
1689 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1692 struct ipa_opt_pass_d pass_ipa_cdtor_merge
=
1697 gate_ipa_cdtor_merge
, /* gate */
1698 ipa_cdtor_merge
, /* execute */
1701 0, /* static_pass_number */
1702 TV_CGRAPHOPT
, /* tv_id */
1703 0, /* properties_required */
1704 0, /* properties_provided */
1705 0, /* properties_destroyed */
1706 0, /* todo_flags_start */
1707 0 /* todo_flags_finish */
1709 NULL
, /* generate_summary */
1710 NULL
, /* write_summary */
1711 NULL
, /* read_summary */
1712 NULL
, /* write_optimization_summary */
1713 NULL
, /* read_optimization_summary */
1714 NULL
, /* stmt_fixup */
1716 NULL
, /* function_transform */
1717 NULL
/* variable_transform */