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 /* Fill array order with all nodes with output flag set in the reverse
37 cgraph_postorder (struct cgraph_node
**order
)
39 struct cgraph_node
*node
, *node2
;
42 struct cgraph_edge
*edge
, last
;
45 struct cgraph_node
**stack
=
46 XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
48 /* We have to deal with cycles nicely, so use a depth first traversal
49 output algorithm. Ignore the fact that some functions won't need
50 to be output and put them into order as well, so we get dependencies
51 right through inline functions. */
52 for (node
= cgraph_nodes
; node
; node
= node
->next
)
54 for (pass
= 0; pass
< 2; pass
++)
55 for (node
= cgraph_nodes
; node
; node
= node
->next
)
58 || (!cgraph_only_called_directly_p (node
)
59 && !node
->address_taken
)))
65 node
->aux
= node
->callers
;
68 while (node2
->aux
!= &last
)
70 edge
= (struct cgraph_edge
*) node2
->aux
;
71 if (edge
->next_caller
)
72 node2
->aux
= edge
->next_caller
;
75 /* Break possible cycles involving always-inline
76 functions by ignoring edges from always-inline
77 functions to non-always-inline functions. */
78 if (edge
->caller
->local
.disregard_inline_limits
79 && !edge
->callee
->local
.disregard_inline_limits
)
81 if (!edge
->caller
->aux
)
83 if (!edge
->caller
->callers
)
84 edge
->caller
->aux
= &last
;
86 edge
->caller
->aux
= edge
->caller
->callers
;
87 stack
[stack_size
++] = node2
;
92 if (node2
->aux
== &last
)
94 order
[order_pos
++] = node2
;
96 node2
= stack
[--stack_size
];
103 for (node
= cgraph_nodes
; node
; node
= node
->next
)
108 /* Look for all functions inlined to NODE and update their inlined_to pointers
112 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
114 struct cgraph_edge
*e
;
115 for (e
= node
->callees
; e
; e
= e
->next_callee
)
116 if (e
->callee
->global
.inlined_to
)
118 e
->callee
->global
.inlined_to
= inlined_to
;
119 update_inlined_to_pointer (e
->callee
, inlined_to
);
123 /* Add cgraph NODE to queue starting at FIRST.
125 The queue is linked via AUX pointers and terminated by pointer to 1.
126 We enqueue nodes at two occasions: when we find them reachable or when we find
127 their bodies needed for further clonning. In the second case we mark them
128 by pointer to 2 after processing so they are re-queue when they become
132 enqueue_cgraph_node (struct cgraph_node
*node
, struct cgraph_node
**first
)
134 /* Node is still in queue; do nothing. */
135 if (node
->aux
&& node
->aux
!= (void *) 2)
137 /* Node was already processed as unreachable, re-enqueue
138 only if it became reachable now. */
139 if (node
->aux
== (void *)2 && !node
->reachable
)
145 /* Add varpool NODE to queue starting at FIRST. */
148 enqueue_varpool_node (struct varpool_node
*node
, struct varpool_node
**first
)
154 /* Process references. */
157 process_references (struct ipa_ref_list
*list
,
158 struct cgraph_node
**first
,
159 struct varpool_node
**first_varpool
,
160 bool before_inlining_p
)
164 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
166 if (ref
->refered_type
== IPA_REF_CGRAPH
)
168 struct cgraph_node
*node
= ipa_ref_node (ref
);
170 && (!DECL_EXTERNAL (node
->decl
)
171 || before_inlining_p
))
173 node
->reachable
= true;
174 enqueue_cgraph_node (node
, first
);
179 struct varpool_node
*node
= ipa_ref_varpool_node (ref
);
182 varpool_mark_needed_node (node
);
183 enqueue_varpool_node (node
, first_varpool
);
189 /* Return true when function NODE can be removed from callgraph
190 if all direct calls are eliminated. */
193 varpool_can_remove_if_no_refs (struct varpool_node
*node
)
195 return (!node
->force_output
&& !node
->used_from_other_partition
196 && (DECL_COMDAT (node
->decl
) || !node
->externally_visible
));
199 /* Return true when function can be marked local. */
202 cgraph_local_node_p (struct cgraph_node
*node
)
204 return (cgraph_only_called_directly_p (node
)
206 && !DECL_EXTERNAL (node
->decl
)
207 && !node
->local
.externally_visible
208 && !node
->reachable_from_other_partition
209 && !node
->in_other_partition
);
212 /* Perform reachability analysis and reclaim all unreachable nodes.
213 If BEFORE_INLINING_P is true this function is called before inlining
214 decisions has been made. If BEFORE_INLINING_P is false this function also
215 removes unneeded bodies of extern inline functions. */
218 cgraph_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
220 struct cgraph_node
*first
= (struct cgraph_node
*) (void *) 1;
221 struct varpool_node
*first_varpool
= (struct varpool_node
*) (void *) 1;
222 struct cgraph_node
*node
, *next
;
223 struct varpool_node
*vnode
, *vnext
;
224 bool changed
= false;
226 #ifdef ENABLE_CHECKING
230 fprintf (file
, "\nReclaiming functions:");
231 #ifdef ENABLE_CHECKING
232 for (node
= cgraph_nodes
; node
; node
= node
->next
)
233 gcc_assert (!node
->aux
);
234 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
235 gcc_assert (!vnode
->aux
);
237 varpool_reset_queue ();
238 for (node
= cgraph_nodes
; node
; node
= node
->next
)
239 if (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node
)
240 && ((!DECL_EXTERNAL (node
->decl
))
241 || before_inlining_p
))
243 gcc_assert (!node
->global
.inlined_to
);
244 enqueue_cgraph_node (node
, &first
);
245 node
->reachable
= true;
249 gcc_assert (!node
->aux
);
250 node
->reachable
= false;
252 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
254 vnode
->next_needed
= NULL
;
255 vnode
->prev_needed
= NULL
;
256 if (!varpool_can_remove_if_no_refs (vnode
))
258 vnode
->needed
= false;
259 varpool_mark_needed_node (vnode
);
260 enqueue_varpool_node (vnode
, &first_varpool
);
263 vnode
->needed
= false;
266 /* Perform reachability analysis. As a special case do not consider
267 extern inline functions not inlined as live because we won't output
270 We maintain two worklist, one for cgraph nodes other for varpools and
271 are finished once both are empty. */
273 while (first
!= (struct cgraph_node
*) (void *) 1
274 || first_varpool
!= (struct varpool_node
*) (void *) 1)
276 if (first
!= (struct cgraph_node
*) (void *) 1)
278 struct cgraph_edge
*e
;
280 first
= (struct cgraph_node
*) first
->aux
;
281 if (!node
->reachable
)
282 node
->aux
= (void *)2;
284 /* If we found this node reachable, first mark on the callees
285 reachable too, unless they are direct calls to extern inline functions
286 we decided to not inline. */
289 for (e
= node
->callees
; e
; e
= e
->next_callee
)
290 if (!e
->callee
->reachable
292 && (!e
->inline_failed
|| !e
->callee
->analyzed
293 || (!DECL_EXTERNAL (e
->callee
->decl
))
294 || before_inlining_p
))
296 e
->callee
->reachable
= true;
297 enqueue_cgraph_node (e
->callee
, &first
);
299 process_references (&node
->ref_list
, &first
, &first_varpool
, before_inlining_p
);
302 /* If any function in a comdat group is reachable, force
303 all other functions in the same comdat group to be
305 if (node
->same_comdat_group
307 && !node
->global
.inlined_to
)
309 for (next
= node
->same_comdat_group
;
311 next
= next
->same_comdat_group
)
312 if (!next
->reachable
)
314 next
->reachable
= true;
315 enqueue_cgraph_node (next
, &first
);
319 /* We can freely remove inline clones even if they are cloned, however if
320 function is clone of real clone, we must keep it around in order to
321 make materialize_clones produce function body with the changes
323 while (node
->clone_of
&& !node
->clone_of
->aux
324 && !gimple_has_body_p (node
->decl
))
326 bool noninline
= node
->clone_of
->decl
!= node
->decl
;
327 node
= node
->clone_of
;
328 if (noninline
&& !node
->reachable
&& !node
->aux
)
330 enqueue_cgraph_node (node
, &first
);
335 if (first_varpool
!= (struct varpool_node
*) (void *) 1)
337 vnode
= first_varpool
;
338 first_varpool
= (struct varpool_node
*)first_varpool
->aux
;
340 process_references (&vnode
->ref_list
, &first
, &first_varpool
, before_inlining_p
);
341 /* If any function in a comdat group is reachable, force
342 all other functions in the same comdat group to be
344 if (vnode
->same_comdat_group
)
346 struct varpool_node
*next
;
347 for (next
= vnode
->same_comdat_group
;
349 next
= next
->same_comdat_group
)
352 varpool_mark_needed_node (next
);
353 enqueue_varpool_node (next
, &first_varpool
);
359 /* Remove unreachable nodes.
361 Completely unreachable functions can be fully removed from the callgraph.
362 Extern inline functions that we decided to not inline need to become unanalyzed nodes of
363 callgraph (so we still have edges to them). We remove function body then.
365 Also we need to care functions that are unreachable but we need to keep them around
366 for later clonning. In this case we also turn them to unanalyzed nodes, but
367 keep the body around. */
368 for (node
= cgraph_nodes
; node
; node
= next
)
371 if (node
->aux
&& !node
->reachable
)
373 cgraph_node_remove_callees (node
);
374 ipa_remove_all_references (&node
->ref_list
);
375 node
->analyzed
= false;
376 node
->local
.inlinable
= false;
380 node
->global
.inlined_to
= NULL
;
382 fprintf (file
, " %s", cgraph_node_name (node
));
383 if (!node
->analyzed
|| !DECL_EXTERNAL (node
->decl
) || before_inlining_p
)
384 cgraph_remove_node (node
);
387 struct cgraph_edge
*e
;
389 /* See if there is reachable caller. */
390 for (e
= node
->callers
; e
; e
= e
->next_caller
)
391 if (e
->caller
->reachable
)
394 /* If so, we need to keep node in the callgraph. */
395 if (e
|| node
->needed
)
397 struct cgraph_node
*clone
;
399 /* If there are still clones, we must keep body around.
400 Otherwise we can just remove the body but keep the clone. */
401 for (clone
= node
->clones
; clone
;
402 clone
= clone
->next_sibling_clone
)
407 cgraph_release_function_body (node
);
408 node
->analyzed
= false;
409 node
->local
.inlinable
= false;
412 gcc_assert (!clone
->in_other_partition
);
413 cgraph_node_remove_callees (node
);
414 ipa_remove_all_references (&node
->ref_list
);
415 if (node
->prev_sibling_clone
)
416 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
417 else if (node
->clone_of
)
418 node
->clone_of
->clones
= node
->next_sibling_clone
;
419 if (node
->next_sibling_clone
)
420 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
421 node
->clone_of
= NULL
;
422 node
->next_sibling_clone
= NULL
;
423 node
->prev_sibling_clone
= NULL
;
426 cgraph_remove_node (node
);
431 for (node
= cgraph_nodes
; node
; node
= node
->next
)
433 /* Inline clones might be kept around so their materializing allows further
434 cloning. If the function the clone is inlined into is removed, we need
435 to turn it into normal cone. */
436 if (node
->global
.inlined_to
439 gcc_assert (node
->clones
);
440 node
->global
.inlined_to
= NULL
;
441 update_inlined_to_pointer (node
, node
);
447 fprintf (file
, "\n");
449 /* We must release unused extern inlines or sanity checking will fail. Rest of transformations
450 are undesirable at -O0 since we do not want to remove anything. */
455 fprintf (file
, "Reclaiming variables:");
456 for (vnode
= varpool_nodes
; vnode
; vnode
= vnext
)
462 fprintf (file
, " %s", varpool_node_name (vnode
));
463 varpool_remove_node (vnode
);
468 /* Now update address_taken flags and try to promote functions to be local. */
471 fprintf (file
, "\nClearing address taken flags:");
472 for (node
= cgraph_nodes
; node
; node
= node
->next
)
473 if (node
->address_taken
474 && !node
->reachable_from_other_partition
)
479 for (i
= 0; ipa_ref_list_refering_iterate (&node
->ref_list
, i
, ref
)
482 gcc_assert (ref
->use
== IPA_REF_ADDR
);
488 fprintf (file
, " %s", cgraph_node_name (node
));
489 node
->address_taken
= false;
491 if (cgraph_local_node_p (node
))
493 node
->local
.local
= true;
495 fprintf (file
, " (local)");
500 #ifdef ENABLE_CHECKING
504 /* Reclaim alias pairs for functions that have disappeared from the
506 remove_unreachable_alias_pairs ();
511 /* Discover variables that have no longer address taken or that are read only
512 and update their flags.
514 FIXME: This can not be done in between gimplify and omp_expand since
515 readonly flag plays role on what is shared and what is not. Currently we do
516 this transformation as part of whole program visibility and re-do at
517 ipa-reference pass (to take into account clonning), but it would
518 make sense to do it before early optimizations. */
521 ipa_discover_readonly_nonaddressable_vars (void)
523 struct varpool_node
*vnode
;
525 fprintf (dump_file
, "Clearing variable flags:");
526 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
527 if (vnode
->finalized
&& varpool_all_refs_explicit_p (vnode
)
528 && (TREE_ADDRESSABLE (vnode
->decl
) || !TREE_READONLY (vnode
->decl
)))
530 bool written
= false;
531 bool address_taken
= false;
534 for (i
= 0; ipa_ref_list_refering_iterate (&vnode
->ref_list
, i
, ref
)
535 && (!written
|| !address_taken
); i
++)
539 address_taken
= true;
547 if (TREE_ADDRESSABLE (vnode
->decl
) && !address_taken
)
550 fprintf (dump_file
, " %s (addressable)", varpool_node_name (vnode
));
551 TREE_ADDRESSABLE (vnode
->decl
) = 0;
553 if (!TREE_READONLY (vnode
->decl
) && !address_taken
&& !written
554 /* Making variable in explicit section readonly can cause section
556 See e.g. gcc.c-torture/compile/pr23237.c */
557 && DECL_SECTION_NAME (vnode
->decl
) == NULL
)
560 fprintf (dump_file
, " %s (read-only)", varpool_node_name (vnode
));
561 TREE_READONLY (vnode
->decl
) = 1;
565 fprintf (dump_file
, "\n");
568 /* Return true when function NODE should be considered externally visible. */
571 cgraph_externally_visible_p (struct cgraph_node
*node
, bool whole_program
, bool aliased
)
573 if (!node
->local
.finalized
)
575 if (!DECL_COMDAT (node
->decl
)
576 && (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
)))
579 /* Do not even try to be smart about aliased nodes. Until we properly
580 represent everything by same body alias, these are just evil. */
584 /* When doing link time optimizations, hidden symbols become local. */
585 if (in_lto_p
&& DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
)
587 else if (!whole_program
)
589 /* COMDAT functions must be shared only if they have address taken,
590 otherwise we can produce our own private implementation with
592 else if (DECL_COMDAT (node
->decl
))
594 if (node
->address_taken
|| !node
->analyzed
)
596 if (node
->same_comdat_group
)
598 struct cgraph_node
*next
;
600 /* If more than one function is in the same COMDAT group, it must
601 be shared even if just one function in the comdat group has
603 for (next
= node
->same_comdat_group
;
605 next
= next
->same_comdat_group
)
606 if (next
->address_taken
|| !next
->analyzed
)
610 if (node
->local
.used_from_object_file
)
612 if (DECL_PRESERVE_P (node
->decl
))
614 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
616 if (lookup_attribute ("externally_visible", DECL_ATTRIBUTES (node
->decl
)))
621 /* Dissolve the same_comdat_group list in which NODE resides. */
624 dissolve_same_comdat_group_list (struct cgraph_node
*node
)
626 struct cgraph_node
*n
= node
, *next
;
629 next
= n
->same_comdat_group
;
630 n
->same_comdat_group
= NULL
;
636 /* Mark visibility of all functions.
638 A local function is one whose calls can occur only in the current
639 compilation unit and all its calls are explicit, so we can change
640 its calling convention. We simply mark all static functions whose
641 address is not taken as local.
643 We also change the TREE_PUBLIC flag of all declarations that are public
644 in language point of view but we want to overwrite this default
645 via visibilities for the backend point of view. */
648 function_and_variable_visibility (bool whole_program
)
650 struct cgraph_node
*node
;
651 struct varpool_node
*vnode
;
652 struct pointer_set_t
*aliased_nodes
= pointer_set_create ();
653 struct pointer_set_t
*aliased_vnodes
= pointer_set_create ();
657 /* Discover aliased nodes. */
658 for (i
= 0; VEC_iterate (alias_pair
, alias_pairs
, i
, p
); i
++)
661 fprintf (dump_file
, "Alias %s->%s",
662 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
663 IDENTIFIER_POINTER (p
->target
));
665 if ((node
= cgraph_node_for_asm (p
->target
)) != NULL
)
667 gcc_assert (node
->needed
);
668 pointer_set_insert (aliased_nodes
, node
);
670 fprintf (dump_file
, " node %s/%i",
671 cgraph_node_name (node
), node
->uid
);
673 else if ((vnode
= varpool_node_for_asm (p
->target
)) != NULL
)
675 gcc_assert (vnode
->needed
);
676 pointer_set_insert (aliased_vnodes
, vnode
);
678 fprintf (dump_file
, " varpool node %s",
679 varpool_node_name (vnode
));
682 fprintf (dump_file
, "\n");
685 for (node
= cgraph_nodes
; node
; node
= node
->next
)
687 /* C++ FE on lack of COMDAT support create local COMDAT functions
688 (that ought to be shared but can not due to object format
689 limitations). It is neccesary to keep the flag to make rest of C++ FE
690 happy. Clear the flag here to avoid confusion in middle-end. */
691 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
692 DECL_COMDAT (node
->decl
) = 0;
693 /* For external decls stop tracking same_comdat_group, it doesn't matter
694 what comdat group they are in when they won't be emitted in this TU,
695 and simplifies later passes. */
696 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
))
698 #ifdef ENABLE_CHECKING
699 struct cgraph_node
*n
;
701 for (n
= node
->same_comdat_group
;
703 n
= n
->same_comdat_group
)
704 /* If at least one of same comdat group functions is external,
705 all of them have to be, otherwise it is a front-end bug. */
706 gcc_assert (DECL_EXTERNAL (n
->decl
));
708 dissolve_same_comdat_group_list (node
);
710 gcc_assert ((!DECL_WEAK (node
->decl
) && !DECL_COMDAT (node
->decl
))
711 || TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
));
712 if (cgraph_externally_visible_p (node
, whole_program
,
713 pointer_set_contains (aliased_nodes
,
716 gcc_assert (!node
->global
.inlined_to
);
717 node
->local
.externally_visible
= true;
720 node
->local
.externally_visible
= false;
721 if (!node
->local
.externally_visible
&& node
->analyzed
722 && !DECL_EXTERNAL (node
->decl
))
724 struct cgraph_node
*alias
;
725 gcc_assert (whole_program
|| in_lto_p
|| !TREE_PUBLIC (node
->decl
));
726 cgraph_make_decl_local (node
->decl
);
727 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
728 cgraph_make_decl_local (alias
->decl
);
729 if (node
->same_comdat_group
)
730 /* cgraph_externally_visible_p has already checked all other nodes
731 in the group and they will all be made local. We need to
732 dissolve the group at once so that the predicate does not
734 dissolve_same_comdat_group_list (node
);
736 node
->local
.local
= cgraph_local_node_p (node
);
738 for (vnode
= varpool_nodes
; vnode
; vnode
= vnode
->next
)
740 /* weak flag makes no sense on local variables. */
741 gcc_assert (!DECL_WEAK (vnode
->decl
)
742 || TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
));
743 /* In several cases declarations can not be common:
745 - when declaration has initializer
747 - when it has specific section
748 - when it resides in non-generic address space.
749 - if declaration is local, it will get into .local common section
750 so common flag is not needed. Frontends still produce these in
751 certain cases, such as for:
753 static int a __attribute__ ((common))
755 Canonicalize things here and clear the redundant flag. */
756 if (DECL_COMMON (vnode
->decl
)
757 && (!(TREE_PUBLIC (vnode
->decl
) || DECL_EXTERNAL (vnode
->decl
))
758 || (DECL_INITIAL (vnode
->decl
)
759 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
760 || DECL_WEAK (vnode
->decl
)
761 || DECL_SECTION_NAME (vnode
->decl
) != NULL
762 || ! (ADDR_SPACE_GENERIC_P
763 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
764 DECL_COMMON (vnode
->decl
) = 0;
766 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
768 if (!vnode
->finalized
)
771 && (DECL_COMDAT (vnode
->decl
) || TREE_PUBLIC (vnode
->decl
))
773 /* We can privatize comdat readonly variables whose address is
774 not taken, but doing so is not going to bring us
775 optimization oppurtunities until we start reordering
777 || DECL_COMDAT (vnode
->decl
)
778 || DECL_WEAK (vnode
->decl
))
779 /* When doing linktime optimizations, all hidden symbols will
782 || DECL_VISIBILITY (vnode
->decl
) != VISIBILITY_HIDDEN
))
783 || vnode
->used_from_object_file
784 || pointer_set_contains (aliased_vnodes
, vnode
)
785 || lookup_attribute ("externally_visible",
786 DECL_ATTRIBUTES (vnode
->decl
))))
787 vnode
->externally_visible
= true;
789 vnode
->externally_visible
= false;
790 if (!vnode
->externally_visible
)
792 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->decl
));
793 cgraph_make_decl_local (vnode
->decl
);
795 gcc_assert (TREE_STATIC (vnode
->decl
));
797 pointer_set_destroy (aliased_nodes
);
798 pointer_set_destroy (aliased_vnodes
);
802 fprintf (dump_file
, "\nMarking local functions:");
803 for (node
= cgraph_nodes
; node
; node
= node
->next
)
804 if (node
->local
.local
)
805 fprintf (dump_file
, " %s", cgraph_node_name (node
));
806 fprintf (dump_file
, "\n\n");
807 fprintf (dump_file
, "\nMarking externally visible functions:");
808 for (node
= cgraph_nodes
; node
; node
= node
->next
)
809 if (node
->local
.externally_visible
)
810 fprintf (dump_file
, " %s", cgraph_node_name (node
));
811 fprintf (dump_file
, "\n\n");
812 fprintf (dump_file
, "\nMarking externally visible variables:");
813 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
814 if (vnode
->externally_visible
)
815 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
816 fprintf (dump_file
, "\n\n");
818 cgraph_function_flags_ready
= true;
822 /* Local function pass handling visibilities. This happens before LTO streaming
823 so in particular -fwhole-program should be ignored at this level. */
826 local_function_and_variable_visibility (void)
828 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
&& !flag_whopr
);
831 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility
=
835 "visibility", /* name */
837 local_function_and_variable_visibility
,/* execute */
840 0, /* static_pass_number */
841 TV_CGRAPHOPT
, /* tv_id */
842 0, /* properties_required */
843 0, /* properties_provided */
844 0, /* properties_destroyed */
845 0, /* todo_flags_start */
846 TODO_remove_functions
| TODO_dump_cgraph
847 | TODO_ggc_collect
/* todo_flags_finish */
851 /* Do not re-run on ltrans stage. */
854 gate_whole_program_function_and_variable_visibility (void)
859 /* Bring functionss local at LTO time whith -fwhole-program. */
862 whole_program_function_and_variable_visibility (void)
864 struct cgraph_node
*node
;
865 struct varpool_node
*vnode
;
867 function_and_variable_visibility (flag_whole_program
);
869 for (node
= cgraph_nodes
; node
; node
= node
->next
)
870 if ((node
->local
.externally_visible
&& !DECL_COMDAT (node
->decl
))
871 && node
->local
.finalized
)
872 cgraph_mark_needed_node (node
);
873 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
874 if (vnode
->externally_visible
&& !DECL_COMDAT (vnode
->decl
))
875 varpool_mark_needed_node (vnode
);
878 fprintf (dump_file
, "\nNeeded variables:");
879 for (vnode
= varpool_nodes_queue
; vnode
; vnode
= vnode
->next_needed
)
881 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
882 fprintf (dump_file
, "\n\n");
885 ipa_discover_readonly_nonaddressable_vars ();
889 struct ipa_opt_pass_d pass_ipa_whole_program_visibility
=
893 "whole-program", /* name */
894 gate_whole_program_function_and_variable_visibility
,/* gate */
895 whole_program_function_and_variable_visibility
,/* execute */
898 0, /* static_pass_number */
899 TV_CGRAPHOPT
, /* tv_id */
900 0, /* properties_required */
901 0, /* properties_provided */
902 0, /* properties_destroyed */
903 0, /* todo_flags_start */
904 TODO_remove_functions
| TODO_dump_cgraph
905 | TODO_ggc_collect
/* todo_flags_finish */
907 NULL
, /* generate_summary */
908 NULL
, /* write_summary */
909 NULL
, /* read_summary */
910 NULL
, /* write_optimization_summary */
911 NULL
, /* read_optimization_summary */
912 NULL
, /* stmt_fixup */
914 NULL
, /* function_transform */
915 NULL
, /* variable_transform */
918 /* Hash a cgraph node set element. */
921 hash_cgraph_node_set_element (const void *p
)
923 const_cgraph_node_set_element element
= (const_cgraph_node_set_element
) p
;
924 return htab_hash_pointer (element
->node
);
927 /* Compare two cgraph node set elements. */
930 eq_cgraph_node_set_element (const void *p1
, const void *p2
)
932 const_cgraph_node_set_element e1
= (const_cgraph_node_set_element
) p1
;
933 const_cgraph_node_set_element e2
= (const_cgraph_node_set_element
) p2
;
935 return e1
->node
== e2
->node
;
938 /* Create a new cgraph node set. */
941 cgraph_node_set_new (void)
943 cgraph_node_set new_node_set
;
945 new_node_set
= ggc_alloc_cgraph_node_set_def ();
946 new_node_set
->hashtab
= htab_create_ggc (10,
947 hash_cgraph_node_set_element
,
948 eq_cgraph_node_set_element
,
950 new_node_set
->nodes
= NULL
;
954 /* Add cgraph_node NODE to cgraph_node_set SET. */
957 cgraph_node_set_add (cgraph_node_set set
, struct cgraph_node
*node
)
960 cgraph_node_set_element element
;
961 struct cgraph_node_set_element_def dummy
;
964 slot
= htab_find_slot (set
->hashtab
, &dummy
, INSERT
);
966 if (*slot
!= HTAB_EMPTY_ENTRY
)
968 element
= (cgraph_node_set_element
) *slot
;
969 gcc_assert (node
== element
->node
970 && (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
975 /* Insert node into hash table. */
976 element
= ggc_alloc_cgraph_node_set_element_def ();
977 element
->node
= node
;
978 element
->index
= VEC_length (cgraph_node_ptr
, set
->nodes
);
981 /* Insert into node vector. */
982 VEC_safe_push (cgraph_node_ptr
, gc
, set
->nodes
, node
);
985 /* Remove cgraph_node NODE from cgraph_node_set SET. */
988 cgraph_node_set_remove (cgraph_node_set set
, struct cgraph_node
*node
)
990 void **slot
, **last_slot
;
991 cgraph_node_set_element element
, last_element
;
992 struct cgraph_node
*last_node
;
993 struct cgraph_node_set_element_def dummy
;
996 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1000 element
= (cgraph_node_set_element
) *slot
;
1001 gcc_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1004 /* Remove from vector. We do this by swapping node with the last element
1006 last_node
= VEC_pop (cgraph_node_ptr
, set
->nodes
);
1007 if (last_node
!= node
)
1009 dummy
.node
= last_node
;
1010 last_slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1011 last_element
= (cgraph_node_set_element
) *last_slot
;
1012 gcc_assert (last_element
);
1014 /* Move the last element to the original spot of NODE. */
1015 last_element
->index
= element
->index
;
1016 VEC_replace (cgraph_node_ptr
, set
->nodes
, last_element
->index
,
1020 /* Remove element from hash table. */
1021 htab_clear_slot (set
->hashtab
, slot
);
1025 /* Find NODE in SET and return an iterator to it if found. A null iterator
1026 is returned if NODE is not in SET. */
1028 cgraph_node_set_iterator
1029 cgraph_node_set_find (cgraph_node_set set
, struct cgraph_node
*node
)
1032 struct cgraph_node_set_element_def dummy
;
1033 cgraph_node_set_element element
;
1034 cgraph_node_set_iterator csi
;
1037 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1039 csi
.index
= (unsigned) ~0;
1042 element
= (cgraph_node_set_element
) *slot
;
1043 gcc_assert (VEC_index (cgraph_node_ptr
, set
->nodes
, element
->index
)
1045 csi
.index
= element
->index
;
1052 /* Dump content of SET to file F. */
1055 dump_cgraph_node_set (FILE *f
, cgraph_node_set set
)
1057 cgraph_node_set_iterator iter
;
1059 for (iter
= csi_start (set
); !csi_end_p (iter
); csi_next (&iter
))
1061 struct cgraph_node
*node
= csi_node (iter
);
1062 fprintf (f
, " %s/%i", cgraph_node_name (node
), node
->uid
);
1067 /* Dump content of SET to stderr. */
1070 debug_cgraph_node_set (cgraph_node_set set
)
1072 dump_cgraph_node_set (stderr
, set
);
1075 /* Hash a varpool node set element. */
1078 hash_varpool_node_set_element (const void *p
)
1080 const_varpool_node_set_element element
= (const_varpool_node_set_element
) p
;
1081 return htab_hash_pointer (element
->node
);
1084 /* Compare two varpool node set elements. */
1087 eq_varpool_node_set_element (const void *p1
, const void *p2
)
1089 const_varpool_node_set_element e1
= (const_varpool_node_set_element
) p1
;
1090 const_varpool_node_set_element e2
= (const_varpool_node_set_element
) p2
;
1092 return e1
->node
== e2
->node
;
1095 /* Create a new varpool node set. */
1098 varpool_node_set_new (void)
1100 varpool_node_set new_node_set
;
1102 new_node_set
= ggc_alloc_varpool_node_set_def ();
1103 new_node_set
->hashtab
= htab_create_ggc (10,
1104 hash_varpool_node_set_element
,
1105 eq_varpool_node_set_element
,
1107 new_node_set
->nodes
= NULL
;
1108 return new_node_set
;
1111 /* Add varpool_node NODE to varpool_node_set SET. */
1114 varpool_node_set_add (varpool_node_set set
, struct varpool_node
*node
)
1117 varpool_node_set_element element
;
1118 struct varpool_node_set_element_def dummy
;
1121 slot
= htab_find_slot (set
->hashtab
, &dummy
, INSERT
);
1123 if (*slot
!= HTAB_EMPTY_ENTRY
)
1125 element
= (varpool_node_set_element
) *slot
;
1126 gcc_assert (node
== element
->node
1127 && (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1132 /* Insert node into hash table. */
1133 element
= ggc_alloc_varpool_node_set_element_def ();
1134 element
->node
= node
;
1135 element
->index
= VEC_length (varpool_node_ptr
, set
->nodes
);
1138 /* Insert into node vector. */
1139 VEC_safe_push (varpool_node_ptr
, gc
, set
->nodes
, node
);
1142 /* Remove varpool_node NODE from varpool_node_set SET. */
1145 varpool_node_set_remove (varpool_node_set set
, struct varpool_node
*node
)
1147 void **slot
, **last_slot
;
1148 varpool_node_set_element element
, last_element
;
1149 struct varpool_node
*last_node
;
1150 struct varpool_node_set_element_def dummy
;
1153 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1157 element
= (varpool_node_set_element
) *slot
;
1158 gcc_assert (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1161 /* Remove from vector. We do this by swapping node with the last element
1163 last_node
= VEC_pop (varpool_node_ptr
, set
->nodes
);
1164 if (last_node
!= node
)
1166 dummy
.node
= last_node
;
1167 last_slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1168 last_element
= (varpool_node_set_element
) *last_slot
;
1169 gcc_assert (last_element
);
1171 /* Move the last element to the original spot of NODE. */
1172 last_element
->index
= element
->index
;
1173 VEC_replace (varpool_node_ptr
, set
->nodes
, last_element
->index
,
1177 /* Remove element from hash table. */
1178 htab_clear_slot (set
->hashtab
, slot
);
1182 /* Find NODE in SET and return an iterator to it if found. A null iterator
1183 is returned if NODE is not in SET. */
1185 varpool_node_set_iterator
1186 varpool_node_set_find (varpool_node_set set
, struct varpool_node
*node
)
1189 struct varpool_node_set_element_def dummy
;
1190 varpool_node_set_element element
;
1191 varpool_node_set_iterator vsi
;
1194 slot
= htab_find_slot (set
->hashtab
, &dummy
, NO_INSERT
);
1196 vsi
.index
= (unsigned) ~0;
1199 element
= (varpool_node_set_element
) *slot
;
1200 gcc_assert (VEC_index (varpool_node_ptr
, set
->nodes
, element
->index
)
1202 vsi
.index
= element
->index
;
1209 /* Dump content of SET to file F. */
1212 dump_varpool_node_set (FILE *f
, varpool_node_set set
)
1214 varpool_node_set_iterator iter
;
1216 for (iter
= vsi_start (set
); !vsi_end_p (iter
); vsi_next (&iter
))
1218 struct varpool_node
*node
= vsi_node (iter
);
1219 fprintf (f
, " %s", varpool_node_name (node
));
1224 /* Dump content of SET to stderr. */
1227 debug_varpool_node_set (varpool_node_set set
)
1229 dump_varpool_node_set (stderr
, set
);
1233 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1238 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1239 struct cgraph_edge
*e
;
1241 bool something_changed
= false;
1244 order_pos
= cgraph_postorder (order
);
1245 for (i
= order_pos
- 1; i
>= 0; i
--)
1247 if (order
[i
]->local
.local
&& cgraph_propagate_frequency (order
[i
]))
1249 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1250 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
1252 something_changed
= true;
1253 e
->callee
->aux
= (void *)1;
1256 order
[i
]->aux
= NULL
;
1259 while (something_changed
)
1261 something_changed
= false;
1262 for (i
= order_pos
- 1; i
>= 0; i
--)
1264 if (order
[i
]->aux
&& cgraph_propagate_frequency (order
[i
]))
1266 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1267 if (e
->callee
->local
.local
&& !e
->callee
->aux
)
1269 something_changed
= true;
1270 e
->callee
->aux
= (void *)1;
1273 order
[i
]->aux
= NULL
;
1281 gate_ipa_profile (void)
1283 return flag_ipa_profile
;
1286 struct ipa_opt_pass_d pass_ipa_profile
=
1290 "ipa-profile", /* name */
1291 gate_ipa_profile
, /* gate */
1292 ipa_profile
, /* execute */
1295 0, /* static_pass_number */
1296 TV_IPA_PROFILE
, /* tv_id */
1297 0, /* properties_required */
1298 0, /* properties_provided */
1299 0, /* properties_destroyed */
1300 0, /* todo_flags_start */
1301 0 /* todo_flags_finish */
1303 NULL
, /* generate_summary */
1304 NULL
, /* write_summary */
1305 NULL
, /* read_summary */
1306 NULL
, /* write_optimization_summary */
1307 NULL
, /* read_optimization_summary */
1308 NULL
, /* stmt_fixup */
1310 NULL
, /* function_transform */
1311 NULL
/* variable_transform */