1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "tree-pass.h"
29 #include "pointer-set.h"
31 #include "tree-iterator.h"
32 #include "ipa-utils.h"
33 #include "pointer-set.h"
34 #include "ipa-inline.h"
36 /* Look for all functions inlined to NODE and update their inlined_to pointers
40 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
42 struct cgraph_edge
*e
;
43 for (e
= node
->callees
; e
; e
= e
->next_callee
)
44 if (e
->callee
->global
.inlined_to
)
46 e
->callee
->global
.inlined_to
= inlined_to
;
47 update_inlined_to_pointer (e
->callee
, inlined_to
);
51 /* Add symtab NODE to queue starting at FIRST.
53 The queue is linked via AUX pointers and terminated by pointer to 1.
54 We enqueue nodes at two occasions: when we find them reachable or when we find
55 their bodies needed for further clonning. In the second case we mark them
56 by pointer to 2 after processing so they are re-queue when they become
60 enqueue_node (symtab_node node
, symtab_node
*first
,
61 struct pointer_set_t
*reachable
)
63 /* Node is still in queue; do nothing. */
64 if (node
->symbol
.aux
&& node
->symbol
.aux
!= (void *) 2)
66 /* Node was already processed as unreachable, re-enqueue
67 only if it became reachable now. */
68 if (node
->symbol
.aux
== (void *)2 && !pointer_set_contains (reachable
, node
))
70 node
->symbol
.aux
= *first
;
74 /* Process references. */
77 process_references (struct ipa_ref_list
*list
,
79 bool before_inlining_p
,
80 struct pointer_set_t
*reachable
)
84 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
86 if (is_a
<cgraph_node
> (ref
->referred
))
88 struct cgraph_node
*node
= ipa_ref_node (ref
);
91 && (!DECL_EXTERNAL (node
->symbol
.decl
)
93 || before_inlining_p
))
94 pointer_set_insert (reachable
, node
);
95 enqueue_node ((symtab_node
) node
, first
, reachable
);
99 struct varpool_node
*node
= ipa_ref_varpool_node (ref
);
102 && (!DECL_EXTERNAL (node
->symbol
.decl
)
104 || before_inlining_p
))
105 pointer_set_insert (reachable
, node
);
106 enqueue_node ((symtab_node
) node
, first
, reachable
);
112 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
115 cgraph_non_local_node_p_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
117 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
118 return !(cgraph_only_called_directly_or_aliased_p (node
)
119 && !ipa_ref_has_aliases_p (&node
->symbol
.ref_list
)
121 && !DECL_EXTERNAL (node
->symbol
.decl
)
122 && !node
->symbol
.externally_visible
123 && !node
->symbol
.used_from_other_partition
124 && !node
->symbol
.in_other_partition
);
127 /* Return true when function can be marked local. */
130 cgraph_local_node_p (struct cgraph_node
*node
)
132 struct cgraph_node
*n
= cgraph_function_or_thunk_node (node
, NULL
);
134 /* FIXME: thunks can be considered local, but we need prevent i386
135 from attempting to change calling convention of them. */
136 if (n
->thunk
.thunk_p
)
138 return !cgraph_for_node_and_aliases (n
,
139 cgraph_non_local_node_p_1
, NULL
, true);
143 /* Return true when NODE has ADDR reference. */
146 has_addr_references_p (struct cgraph_node
*node
,
147 void *data ATTRIBUTE_UNUSED
)
152 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
154 if (ref
->use
== IPA_REF_ADDR
)
159 /* Perform reachability analysis and reclaim all unreachable nodes.
161 The algorithm is basically mark&sweep but with some extra refinements:
163 - reachable extern inline functions needs special handling; the bodies needs
164 to stay in memory until inlining in hope that they will be inlined.
165 After inlining we release their bodies and turn them into unanalyzed
166 nodes even when they are reachable.
168 BEFORE_INLINING_P specify whether we are before or after inlining.
170 - virtual functions are kept in callgraph even if they seem unreachable in
171 hope calls to them will be devirtualized.
173 Again we remove them after inlining. In late optimization some
174 devirtualization may happen, but it is not importnat since we won't inline
175 the call. In theory early opts and IPA should work out all important cases.
177 - virtual clones needs bodies of their origins for later materialization;
178 this means that we want to keep the body even if the origin is unreachable
179 otherwise. To avoid origin from sitting in the callgraph and being
180 walked by IPA passes, we turn them into unanalyzed nodes with body
183 We maintain set of function declaration where body needs to stay in
184 body_needed_for_clonning
186 Inline clones represent special case: their declaration match the
187 declaration of origin and cgraph_remove_node already knows how to
188 reshape callgraph and preserve body when offline copy of function or
189 inline clone is being removed.
191 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
192 variables with DECL_INITIAL set. We finalize these and keep reachable
193 ones around for constant folding purposes. After inlining we however
194 stop walking their references to let everything static referneced by them
195 to be removed when it is otherwise unreachable.
197 We maintain queue of both reachable symbols (i.e. defined symbols that needs
198 to stay) and symbols that are in boundary (i.e. external symbols referenced
199 by reachable symbols or origins of clones). The queue is represented
200 as linked list by AUX pointer terminated by 1.
202 A the end we keep all reachable symbols. For symbols in boundary we always
203 turn definition into a declaration, but we may keep function body around
204 based on body_needed_for_clonning
206 All symbols that enter the queue have AUX pointer non-zero and are in the
207 boundary. Pointer set REACHABLE is used to track reachable symbols.
209 Every symbol can be visited twice - once as part of boundary and once
210 as real reachable symbol. enqueue_node needs to decide whether the
211 node needs to be re-queued for second processing. For this purpose
212 we set AUX pointer of processed symbols in the boundary to constant 2. */
215 symtab_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
217 symtab_node first
= (symtab_node
) (void *) 1;
218 struct cgraph_node
*node
, *next
;
219 struct varpool_node
*vnode
, *vnext
;
220 bool changed
= false;
221 struct pointer_set_t
*reachable
= pointer_set_create ();
222 struct pointer_set_t
*body_needed_for_clonning
= pointer_set_create ();
224 #ifdef ENABLE_CHECKING
228 fprintf (file
, "\nReclaiming functions:");
229 #ifdef ENABLE_CHECKING
230 FOR_EACH_FUNCTION (node
)
231 gcc_assert (!node
->symbol
.aux
);
232 FOR_EACH_VARIABLE (vnode
)
233 gcc_assert (!vnode
->symbol
.aux
);
235 /* Mark functions whose bodies are obviously needed.
236 This is mostly when they can be referenced externally. Inline clones
237 are special since their declarations are shared with master clone and thus
238 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
239 FOR_EACH_DEFINED_FUNCTION (node
)
240 if (!node
->global
.inlined_to
241 && (!cgraph_can_remove_if_no_direct_calls_and_refs_p (node
)
242 /* Keep around virtual functions for possible devirtualization. */
243 || (before_inlining_p
244 && DECL_VIRTUAL_P (node
->symbol
.decl
)
245 && (DECL_COMDAT (node
->symbol
.decl
) || DECL_EXTERNAL (node
->symbol
.decl
)))))
247 gcc_assert (!node
->global
.inlined_to
);
248 pointer_set_insert (reachable
, node
);
249 enqueue_node ((symtab_node
)node
, &first
, reachable
);
252 gcc_assert (!node
->symbol
.aux
);
254 /* Mark variables that are obviously needed. */
255 FOR_EACH_DEFINED_VARIABLE (vnode
)
256 if (!varpool_can_remove_if_no_refs (vnode
))
258 pointer_set_insert (reachable
, vnode
);
259 enqueue_node ((symtab_node
)vnode
, &first
, reachable
);
262 /* Perform reachability analysis. */
263 while (first
!= (symtab_node
) (void *) 1)
265 bool in_boundary_p
= !pointer_set_contains (reachable
, first
);
266 symtab_node node
= first
;
268 first
= (symtab_node
)first
->symbol
.aux
;
270 /* If we are processing symbol in boundary, mark its AUX pointer for
271 possible later re-processing in enqueue_node. */
273 node
->symbol
.aux
= (void *)2;
276 /* If any symbol in a comdat group is reachable, force
277 all other in the same comdat group to be also reachable. */
278 if (node
->symbol
.same_comdat_group
)
281 for (next
= node
->symbol
.same_comdat_group
;
283 next
= next
->symbol
.same_comdat_group
)
284 if (!pointer_set_insert (reachable
, next
))
285 enqueue_node ((symtab_node
) next
, &first
, reachable
);
287 /* Mark references as reachable. */
288 process_references (&node
->symbol
.ref_list
, &first
,
289 before_inlining_p
, reachable
);
292 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
294 /* Mark the callees reachable unless they are direct calls to extern
295 inline functions we decided to not inline. */
298 struct cgraph_edge
*e
;
299 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
301 if (e
->callee
->analyzed
302 && (!e
->inline_failed
303 || !DECL_EXTERNAL (e
->callee
->symbol
.decl
)
305 || before_inlining_p
))
306 pointer_set_insert (reachable
, e
->callee
);
307 enqueue_node ((symtab_node
) e
->callee
, &first
, reachable
);
310 /* When inline clone exists, mark body to be preserved so when removing
311 offline copy of the function we don't kill it. */
312 if (!cnode
->alias
&& cnode
->global
.inlined_to
)
313 pointer_set_insert (body_needed_for_clonning
, cnode
->symbol
.decl
);
316 /* For non-inline clones, force their origins to the boundary and ensure
317 that body is not removed. */
318 while (cnode
->clone_of
319 && !gimple_has_body_p (cnode
->symbol
.decl
))
321 bool noninline
= cnode
->clone_of
->symbol
.decl
!= cnode
->symbol
.decl
;
322 cnode
= cnode
->clone_of
;
325 pointer_set_insert (body_needed_for_clonning
, cnode
->symbol
.decl
);
326 enqueue_node ((symtab_node
)cnode
, &first
, reachable
);
331 /* When we see constructor of external variable, keep referred nodes in the
332 boundary. This will also hold initializers of the external vars NODE
334 varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
);
336 && DECL_EXTERNAL (node
->symbol
.decl
)
341 for (int i
= 0; ipa_ref_list_reference_iterate (&node
->symbol
.ref_list
, i
, ref
); i
++)
342 enqueue_node (ref
->referred
, &first
, reachable
);
346 /* Remove unreachable functions. */
347 for (node
= cgraph_first_function (); node
; node
= next
)
349 next
= cgraph_next_function (node
);
350 if (!node
->symbol
.aux
)
353 fprintf (file
, " %s", cgraph_node_name (node
));
354 cgraph_remove_node (node
);
357 else if (!pointer_set_contains (reachable
, node
))
362 fprintf (file
, " %s", cgraph_node_name (node
));
363 cgraph_node_remove_callees (node
);
364 ipa_remove_all_references (&node
->symbol
.ref_list
);
367 if (!pointer_set_contains (body_needed_for_clonning
, node
->symbol
.decl
)
368 && (node
->local
.finalized
|| !DECL_ARTIFICIAL (node
->symbol
.decl
)))
369 cgraph_release_function_body (node
);
370 node
->analyzed
= false;
374 /* Inline clones might be kept around so their materializing allows further
375 cloning. If the function the clone is inlined into is removed, we need
376 to turn it into normal cone. */
377 FOR_EACH_FUNCTION (node
)
379 if (node
->global
.inlined_to
382 gcc_assert (node
->clones
);
383 node
->global
.inlined_to
= NULL
;
384 update_inlined_to_pointer (node
, node
);
386 node
->symbol
.aux
= NULL
;
389 /* Remove unreachable variables. */
391 fprintf (file
, "\nReclaiming variables:");
392 for (vnode
= varpool_first_variable (); vnode
; vnode
= vnext
)
394 vnext
= varpool_next_variable (vnode
);
395 if (!vnode
->symbol
.aux
)
398 fprintf (file
, " %s", varpool_node_name (vnode
));
399 varpool_remove_node (vnode
);
402 else if (!pointer_set_contains (reachable
, vnode
))
407 fprintf (file
, " %s", varpool_node_name (vnode
));
410 vnode
->analyzed
= false;
411 vnode
->symbol
.aux
= NULL
;
414 vnode
->symbol
.aux
= NULL
;
417 pointer_set_destroy (reachable
);
418 pointer_set_destroy (body_needed_for_clonning
);
420 /* Now update address_taken flags and try to promote functions to be local. */
422 fprintf (file
, "\nClearing address taken flags:");
423 FOR_EACH_DEFINED_FUNCTION (node
)
424 if (node
->symbol
.address_taken
425 && !node
->symbol
.used_from_other_partition
)
427 if (!cgraph_for_node_and_aliases (node
, has_addr_references_p
, NULL
, true))
430 fprintf (file
, " %s", cgraph_node_name (node
));
431 node
->symbol
.address_taken
= false;
433 if (cgraph_local_node_p (node
))
435 node
->local
.local
= true;
437 fprintf (file
, " (local)");
442 fprintf (file
, "\n");
444 #ifdef ENABLE_CHECKING
448 /* If we removed something, perhaps profile could be improved. */
449 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
450 FOR_EACH_DEFINED_FUNCTION (node
)
451 cgraph_propagate_frequency (node
);
456 /* Discover variables that have no longer address taken or that are read only
457 and update their flags.
459 FIXME: This can not be done in between gimplify and omp_expand since
460 readonly flag plays role on what is shared and what is not. Currently we do
461 this transformation as part of whole program visibility and re-do at
462 ipa-reference pass (to take into account clonning), but it would
463 make sense to do it before early optimizations. */
466 ipa_discover_readonly_nonaddressable_vars (void)
468 struct varpool_node
*vnode
;
470 fprintf (dump_file
, "Clearing variable flags:");
471 FOR_EACH_VARIABLE (vnode
)
472 if (vnode
->finalized
&& varpool_all_refs_explicit_p (vnode
)
473 && (TREE_ADDRESSABLE (vnode
->symbol
.decl
)
474 || !TREE_READONLY (vnode
->symbol
.decl
)))
476 bool written
= false;
477 bool address_taken
= false;
480 for (i
= 0; ipa_ref_list_referring_iterate (&vnode
->symbol
.ref_list
,
482 && (!written
|| !address_taken
); i
++)
486 address_taken
= true;
494 if (TREE_ADDRESSABLE (vnode
->symbol
.decl
) && !address_taken
)
497 fprintf (dump_file
, " %s (addressable)", varpool_node_name (vnode
));
498 TREE_ADDRESSABLE (vnode
->symbol
.decl
) = 0;
500 if (!TREE_READONLY (vnode
->symbol
.decl
) && !address_taken
&& !written
501 /* Making variable in explicit section readonly can cause section
503 See e.g. gcc.c-torture/compile/pr23237.c */
504 && DECL_SECTION_NAME (vnode
->symbol
.decl
) == NULL
)
507 fprintf (dump_file
, " %s (read-only)", varpool_node_name (vnode
));
508 TREE_READONLY (vnode
->symbol
.decl
) = 1;
512 fprintf (dump_file
, "\n");
515 /* Return true when there is a reference to node and it is not vtable. */
517 cgraph_address_taken_from_non_vtable_p (struct cgraph_node
*node
)
521 for (i
= 0; ipa_ref_list_referring_iterate (&node
->symbol
.ref_list
,
523 if (ref
->use
== IPA_REF_ADDR
)
525 struct varpool_node
*node
;
526 if (is_a
<cgraph_node
> (ref
->referring
))
528 node
= ipa_ref_referring_varpool_node (ref
);
529 if (!DECL_VIRTUAL_P (node
->symbol
.decl
))
535 /* COMDAT functions must be shared only if they have address taken,
536 otherwise we can produce our own private implementation with
538 Return true when turning COMDAT functoin static can not lead to wrong
539 code when the resulting object links with a library defining same COMDAT.
541 Virtual functions do have their addresses taken from the vtables,
542 but in C++ there is no way to compare their addresses for equality. */
545 cgraph_comdat_can_be_unshared_p (struct cgraph_node
*node
)
547 if ((cgraph_address_taken_from_non_vtable_p (node
)
548 && !DECL_VIRTUAL_P (node
->symbol
.decl
))
551 if (node
->symbol
.same_comdat_group
)
553 struct cgraph_node
*next
;
555 /* If more than one function is in the same COMDAT group, it must
556 be shared even if just one function in the comdat group has
558 for (next
= cgraph (node
->symbol
.same_comdat_group
);
559 next
!= node
; next
= cgraph (next
->symbol
.same_comdat_group
))
560 if (cgraph_address_taken_from_non_vtable_p (next
)
561 && !DECL_VIRTUAL_P (next
->symbol
.decl
))
567 /* Return true when function NODE should be considered externally visible. */
570 cgraph_externally_visible_p (struct cgraph_node
*node
,
571 bool whole_program
, bool aliased
)
573 if (!node
->local
.finalized
)
575 if (!DECL_COMDAT (node
->symbol
.decl
)
576 && (!TREE_PUBLIC (node
->symbol
.decl
)
577 || DECL_EXTERNAL (node
->symbol
.decl
)))
580 /* Do not even try to be smart about aliased nodes. Until we properly
581 represent everything by same body alias, these are just evil. */
585 /* Do not try to localize built-in functions yet. One of problems is that we
586 end up mangling their asm for WHOPR that makes it impossible to call them
587 using the implicit built-in declarations anymore. Similarly this enables
588 us to remove them as unreachable before actual calls may appear during
589 expansion or folding. */
590 if (DECL_BUILT_IN (node
->symbol
.decl
))
593 /* If linker counts on us, we must preserve the function. */
594 if (symtab_used_from_object_file_p ((symtab_node
) node
))
596 if (DECL_PRESERVE_P (node
->symbol
.decl
))
598 if (lookup_attribute ("externally_visible",
599 DECL_ATTRIBUTES (node
->symbol
.decl
)))
601 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
602 && lookup_attribute ("dllexport",
603 DECL_ATTRIBUTES (node
->symbol
.decl
)))
605 if (node
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
)
607 /* When doing LTO or whole program, we can bring COMDAT functoins static.
608 This improves code quality and we know we will duplicate them at most twice
609 (in the case that we are not using plugin and link with object file
610 implementing same COMDAT) */
611 if ((in_lto_p
|| whole_program
)
612 && DECL_COMDAT (node
->symbol
.decl
)
613 && cgraph_comdat_can_be_unshared_p (node
))
616 /* When doing link time optimizations, hidden symbols become local. */
618 && (DECL_VISIBILITY (node
->symbol
.decl
) == VISIBILITY_HIDDEN
619 || DECL_VISIBILITY (node
->symbol
.decl
) == VISIBILITY_INTERNAL
)
620 /* Be sure that node is defined in IR file, not in other object
621 file. In that case we don't set used_from_other_object_file. */
624 else if (!whole_program
)
627 if (MAIN_NAME_P (DECL_NAME (node
->symbol
.decl
)))
633 /* Return true when variable VNODE should be considered externally visible. */
636 varpool_externally_visible_p (struct varpool_node
*vnode
, bool aliased
)
638 /* Do not touch weakrefs; while they are not externally visible,
639 dropping their DECL_EXTERNAL flags confuse most
640 of code handling them. */
641 if (vnode
->alias
&& DECL_EXTERNAL (vnode
->symbol
.decl
))
644 if (DECL_EXTERNAL (vnode
->symbol
.decl
))
647 if (!DECL_COMDAT (vnode
->symbol
.decl
) && !TREE_PUBLIC (vnode
->symbol
.decl
))
650 /* Do not even try to be smart about aliased nodes. Until we properly
651 represent everything by same body alias, these are just evil. */
655 /* If linker counts on us, we must preserve the function. */
656 if (symtab_used_from_object_file_p ((symtab_node
) vnode
))
659 if (DECL_HARD_REGISTER (vnode
->symbol
.decl
))
661 if (DECL_PRESERVE_P (vnode
->symbol
.decl
))
663 if (lookup_attribute ("externally_visible",
664 DECL_ATTRIBUTES (vnode
->symbol
.decl
)))
666 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
667 && lookup_attribute ("dllexport",
668 DECL_ATTRIBUTES (vnode
->symbol
.decl
)))
671 /* See if we have linker information about symbol not being used or
672 if we need to make guess based on the declaration.
674 Even if the linker clams the symbol is unused, never bring internal
675 symbols that are declared by user as used or externally visible.
676 This is needed for i.e. references from asm statements. */
677 if (symtab_used_from_object_file_p ((symtab_node
) vnode
))
679 if (vnode
->symbol
.resolution
== LDPR_PREVAILING_DEF_IRONLY
)
682 /* As a special case, the COMDAT virtual tables can be unshared.
683 In LTO mode turn vtables into static variables. The variable is readonly,
684 so this does not enable more optimization, but referring static var
685 is faster for dynamic linking. Also this match logic hidding vtables
686 from LTO symbol tables. */
687 if ((in_lto_p
|| flag_whole_program
)
688 && !vnode
->symbol
.force_output
689 && DECL_COMDAT (vnode
->symbol
.decl
) && DECL_VIRTUAL_P (vnode
->symbol
.decl
))
692 /* When doing link time optimizations, hidden symbols become local. */
694 && (DECL_VISIBILITY (vnode
->symbol
.decl
) == VISIBILITY_HIDDEN
695 || DECL_VISIBILITY (vnode
->symbol
.decl
) == VISIBILITY_INTERNAL
)
696 /* Be sure that node is defined in IR file, not in other object
697 file. In that case we don't set used_from_other_object_file. */
700 else if (!flag_whole_program
)
703 /* Do not attempt to privatize COMDATS by default.
704 This would break linking with C++ libraries sharing
707 FIXME: We can do so for readonly vars with no address taken and
708 possibly also for vtables since no direct pointer comparsion is done.
709 It might be interesting to do so to reduce linking overhead. */
710 if (DECL_COMDAT (vnode
->symbol
.decl
) || DECL_WEAK (vnode
->symbol
.decl
))
715 /* Mark visibility of all functions.
717 A local function is one whose calls can occur only in the current
718 compilation unit and all its calls are explicit, so we can change
719 its calling convention. We simply mark all static functions whose
720 address is not taken as local.
722 We also change the TREE_PUBLIC flag of all declarations that are public
723 in language point of view but we want to overwrite this default
724 via visibilities for the backend point of view. */
727 function_and_variable_visibility (bool whole_program
)
729 struct cgraph_node
*node
;
730 struct varpool_node
*vnode
;
731 struct pointer_set_t
*aliased_nodes
= pointer_set_create ();
732 struct pointer_set_t
*aliased_vnodes
= pointer_set_create ();
736 /* Discover aliased nodes. */
737 FOR_EACH_VEC_SAFE_ELT (alias_pairs
, i
, p
)
740 fprintf (dump_file
, "Alias %s->%s",
741 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (p
->decl
)),
742 IDENTIFIER_POINTER (p
->target
));
744 if ((node
= cgraph_node_for_asm (p
->target
)) != NULL
745 && !DECL_EXTERNAL (node
->symbol
.decl
))
749 cgraph_mark_force_output_node (node
);
750 pointer_set_insert (aliased_nodes
, node
);
752 fprintf (dump_file
, " node %s/%i",
753 cgraph_node_name (node
), node
->uid
);
755 else if ((vnode
= varpool_node_for_asm (p
->target
)) != NULL
756 && !DECL_EXTERNAL (vnode
->symbol
.decl
))
758 vnode
->symbol
.force_output
= 1;
759 pointer_set_insert (aliased_vnodes
, vnode
);
761 fprintf (dump_file
, " varpool node %s",
762 varpool_node_name (vnode
));
765 fprintf (dump_file
, "\n");
768 FOR_EACH_FUNCTION (node
)
770 int flags
= flags_from_decl_or_type (node
->symbol
.decl
);
772 /* Optimize away PURE and CONST constructors and destructors. */
774 && (flags
& (ECF_CONST
| ECF_PURE
))
775 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
777 DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
) = 0;
778 DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
) = 0;
781 /* Frontends and alias code marks nodes as needed before parsing is finished.
782 We may end up marking as node external nodes where this flag is meaningless
784 if (node
->symbol
.force_output
785 && (DECL_EXTERNAL (node
->symbol
.decl
) || !node
->analyzed
))
786 node
->symbol
.force_output
= 0;
788 /* C++ FE on lack of COMDAT support create local COMDAT functions
789 (that ought to be shared but can not due to object format
790 limitations). It is necessary to keep the flag to make rest of C++ FE
791 happy. Clear the flag here to avoid confusion in middle-end. */
792 if (DECL_COMDAT (node
->symbol
.decl
) && !TREE_PUBLIC (node
->symbol
.decl
))
793 DECL_COMDAT (node
->symbol
.decl
) = 0;
794 /* For external decls stop tracking same_comdat_group, it doesn't matter
795 what comdat group they are in when they won't be emitted in this TU,
796 and simplifies later passes. */
797 if (node
->symbol
.same_comdat_group
&& DECL_EXTERNAL (node
->symbol
.decl
))
799 #ifdef ENABLE_CHECKING
802 for (n
= node
->symbol
.same_comdat_group
;
803 n
!= (symtab_node
)node
;
804 n
= n
->symbol
.same_comdat_group
)
805 /* If at least one of same comdat group functions is external,
806 all of them have to be, otherwise it is a front-end bug. */
807 gcc_assert (DECL_EXTERNAL (n
->symbol
.decl
));
809 symtab_dissolve_same_comdat_group_list ((symtab_node
) node
);
811 gcc_assert ((!DECL_WEAK (node
->symbol
.decl
)
812 && !DECL_COMDAT (node
->symbol
.decl
))
813 || TREE_PUBLIC (node
->symbol
.decl
)
814 || DECL_EXTERNAL (node
->symbol
.decl
));
815 if (cgraph_externally_visible_p (node
, whole_program
,
816 pointer_set_contains (aliased_nodes
,
819 gcc_assert (!node
->global
.inlined_to
);
820 node
->symbol
.externally_visible
= true;
823 node
->symbol
.externally_visible
= false;
824 if (!node
->symbol
.externally_visible
&& node
->analyzed
825 && !DECL_EXTERNAL (node
->symbol
.decl
))
827 gcc_assert (whole_program
|| in_lto_p
828 || !TREE_PUBLIC (node
->symbol
.decl
));
829 symtab_make_decl_local (node
->symbol
.decl
);
830 node
->symbol
.resolution
= LDPR_PREVAILING_DEF_IRONLY
;
831 if (node
->symbol
.same_comdat_group
)
832 /* cgraph_externally_visible_p has already checked all other nodes
833 in the group and they will all be made local. We need to
834 dissolve the group at once so that the predicate does not
836 symtab_dissolve_same_comdat_group_list ((symtab_node
) node
);
839 if (node
->thunk
.thunk_p
840 && TREE_PUBLIC (node
->symbol
.decl
))
842 struct cgraph_node
*decl_node
= node
;
844 decl_node
= cgraph_function_node (decl_node
->callees
->callee
, NULL
);
846 /* Thunks have the same visibility as function they are attached to.
847 Make sure the C++ front end set this up properly. */
848 if (DECL_ONE_ONLY (decl_node
->symbol
.decl
))
850 gcc_checking_assert (DECL_COMDAT (node
->symbol
.decl
)
851 == DECL_COMDAT (decl_node
->symbol
.decl
));
852 gcc_checking_assert (DECL_COMDAT_GROUP (node
->symbol
.decl
)
853 == DECL_COMDAT_GROUP (decl_node
->symbol
.decl
));
854 gcc_checking_assert (node
->symbol
.same_comdat_group
);
856 if (DECL_EXTERNAL (decl_node
->symbol
.decl
))
857 DECL_EXTERNAL (node
->symbol
.decl
) = 1;
860 FOR_EACH_DEFINED_FUNCTION (node
)
861 node
->local
.local
= cgraph_local_node_p (node
);
862 FOR_EACH_VARIABLE (vnode
)
864 /* weak flag makes no sense on local variables. */
865 gcc_assert (!DECL_WEAK (vnode
->symbol
.decl
)
866 || TREE_PUBLIC (vnode
->symbol
.decl
)
867 || DECL_EXTERNAL (vnode
->symbol
.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
->symbol
.decl
)
882 && (!(TREE_PUBLIC (vnode
->symbol
.decl
)
883 || DECL_EXTERNAL (vnode
->symbol
.decl
))
884 || (DECL_INITIAL (vnode
->symbol
.decl
)
885 && DECL_INITIAL (vnode
->symbol
.decl
) != error_mark_node
)
886 || DECL_WEAK (vnode
->symbol
.decl
)
887 || DECL_SECTION_NAME (vnode
->symbol
.decl
) != NULL
888 || ! (ADDR_SPACE_GENERIC_P
889 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->symbol
.decl
))))))
890 DECL_COMMON (vnode
->symbol
.decl
) = 0;
892 FOR_EACH_DEFINED_VARIABLE (vnode
)
894 if (!vnode
->finalized
)
896 if (varpool_externally_visible_p
898 pointer_set_contains (aliased_vnodes
, vnode
)))
899 vnode
->symbol
.externally_visible
= true;
901 vnode
->symbol
.externally_visible
= false;
902 if (!vnode
->symbol
.externally_visible
)
904 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->symbol
.decl
));
905 symtab_make_decl_local (vnode
->symbol
.decl
);
906 if (vnode
->symbol
.same_comdat_group
)
907 symtab_dissolve_same_comdat_group_list ((symtab_node
) vnode
);
908 vnode
->symbol
.resolution
= LDPR_PREVAILING_DEF_IRONLY
;
911 pointer_set_destroy (aliased_nodes
);
912 pointer_set_destroy (aliased_vnodes
);
916 fprintf (dump_file
, "\nMarking local functions:");
917 FOR_EACH_DEFINED_FUNCTION (node
)
918 if (node
->local
.local
)
919 fprintf (dump_file
, " %s", cgraph_node_name (node
));
920 fprintf (dump_file
, "\n\n");
921 fprintf (dump_file
, "\nMarking externally visible functions:");
922 FOR_EACH_DEFINED_FUNCTION (node
)
923 if (node
->symbol
.externally_visible
)
924 fprintf (dump_file
, " %s", cgraph_node_name (node
));
925 fprintf (dump_file
, "\n\n");
926 fprintf (dump_file
, "\nMarking externally visible variables:");
927 FOR_EACH_DEFINED_VARIABLE (vnode
)
928 if (vnode
->symbol
.externally_visible
)
929 fprintf (dump_file
, " %s", varpool_node_name (vnode
));
930 fprintf (dump_file
, "\n\n");
932 cgraph_function_flags_ready
= true;
936 /* Local function pass handling visibilities. This happens before LTO streaming
937 so in particular -fwhole-program should be ignored at this level. */
940 local_function_and_variable_visibility (void)
942 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
945 struct simple_ipa_opt_pass pass_ipa_function_and_variable_visibility
=
949 "visibility", /* name */
950 OPTGROUP_NONE
, /* optinfo_flags */
952 local_function_and_variable_visibility
,/* execute */
955 0, /* static_pass_number */
956 TV_CGRAPHOPT
, /* tv_id */
957 0, /* properties_required */
958 0, /* properties_provided */
959 0, /* properties_destroyed */
960 0, /* todo_flags_start */
961 TODO_remove_functions
| TODO_dump_symtab
962 | TODO_ggc_collect
/* todo_flags_finish */
966 /* Free inline summary. */
969 free_inline_summary (void)
971 inline_free_summary ();
975 struct simple_ipa_opt_pass pass_ipa_free_inline_summary
=
979 "*free_inline_summary", /* name */
980 OPTGROUP_NONE
, /* optinfo_flags */
982 free_inline_summary
, /* execute */
985 0, /* static_pass_number */
986 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
987 0, /* properties_required */
988 0, /* properties_provided */
989 0, /* properties_destroyed */
990 0, /* todo_flags_start */
991 TODO_ggc_collect
/* todo_flags_finish */
995 /* Do not re-run on ltrans stage. */
998 gate_whole_program_function_and_variable_visibility (void)
1000 return !flag_ltrans
;
1003 /* Bring functionss local at LTO time with -fwhole-program. */
1006 whole_program_function_and_variable_visibility (void)
1008 function_and_variable_visibility (flag_whole_program
);
1010 ipa_discover_readonly_nonaddressable_vars ();
1014 struct ipa_opt_pass_d pass_ipa_whole_program_visibility
=
1018 "whole-program", /* name */
1019 OPTGROUP_NONE
, /* optinfo_flags */
1020 gate_whole_program_function_and_variable_visibility
,/* gate */
1021 whole_program_function_and_variable_visibility
,/* execute */
1024 0, /* static_pass_number */
1025 TV_CGRAPHOPT
, /* tv_id */
1026 0, /* properties_required */
1027 0, /* properties_provided */
1028 0, /* properties_destroyed */
1029 0, /* todo_flags_start */
1030 TODO_remove_functions
| TODO_dump_symtab
1031 | TODO_ggc_collect
/* todo_flags_finish */
1033 NULL
, /* generate_summary */
1034 NULL
, /* write_summary */
1035 NULL
, /* read_summary */
1036 NULL
, /* write_optimization_summary */
1037 NULL
, /* read_optimization_summary */
1038 NULL
, /* stmt_fixup */
1040 NULL
, /* function_transform */
1041 NULL
, /* variable_transform */
1045 /* Simple ipa profile pass propagating frequencies across the callgraph. */
1050 struct cgraph_node
**order
= XCNEWVEC (struct cgraph_node
*, cgraph_n_nodes
);
1051 struct cgraph_edge
*e
;
1053 bool something_changed
= false;
1056 order_pos
= ipa_reverse_postorder (order
);
1057 for (i
= order_pos
- 1; i
>= 0; i
--)
1059 if (order
[i
]->local
.local
&& cgraph_propagate_frequency (order
[i
]))
1061 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1062 if (e
->callee
->local
.local
&& !e
->callee
->symbol
.aux
)
1064 something_changed
= true;
1065 e
->callee
->symbol
.aux
= (void *)1;
1068 order
[i
]->symbol
.aux
= NULL
;
1071 while (something_changed
)
1073 something_changed
= false;
1074 for (i
= order_pos
- 1; i
>= 0; i
--)
1076 if (order
[i
]->symbol
.aux
&& cgraph_propagate_frequency (order
[i
]))
1078 for (e
= order
[i
]->callees
; e
; e
= e
->next_callee
)
1079 if (e
->callee
->local
.local
&& !e
->callee
->symbol
.aux
)
1081 something_changed
= true;
1082 e
->callee
->symbol
.aux
= (void *)1;
1085 order
[i
]->symbol
.aux
= NULL
;
1093 gate_ipa_profile (void)
1095 return flag_ipa_profile
;
1098 struct ipa_opt_pass_d pass_ipa_profile
=
1102 "profile_estimate", /* name */
1103 OPTGROUP_NONE
, /* optinfo_flags */
1104 gate_ipa_profile
, /* gate */
1105 ipa_profile
, /* execute */
1108 0, /* static_pass_number */
1109 TV_IPA_PROFILE
, /* tv_id */
1110 0, /* properties_required */
1111 0, /* properties_provided */
1112 0, /* properties_destroyed */
1113 0, /* todo_flags_start */
1114 0 /* todo_flags_finish */
1116 NULL
, /* generate_summary */
1117 NULL
, /* write_summary */
1118 NULL
, /* read_summary */
1119 NULL
, /* write_optimization_summary */
1120 NULL
, /* read_optimization_summary */
1121 NULL
, /* stmt_fixup */
1123 NULL
, /* function_transform */
1124 NULL
/* variable_transform */
1127 /* Generate and emit a static constructor or destructor. WHICH must
1128 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1129 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1130 initialization priority for this constructor or destructor.
1132 FINAL specify whether the externally visible name for collect2 should
1136 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
1138 static int counter
= 0;
1140 tree decl
, name
, resdecl
;
1142 /* The priority is encoded in the constructor or destructor name.
1143 collect2 will sort the names and arrange that they are called at
1146 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1148 /* Proudce sane name but one not recognizable by collect2, just for the
1149 case we fail to inline the function. */
1150 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
1151 name
= get_file_function_name (which_buf
);
1153 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1154 build_function_type_list (void_type_node
, NULL_TREE
));
1155 current_function_decl
= decl
;
1157 resdecl
= build_decl (input_location
,
1158 RESULT_DECL
, NULL_TREE
, void_type_node
);
1159 DECL_ARTIFICIAL (resdecl
) = 1;
1160 DECL_RESULT (decl
) = resdecl
;
1161 DECL_CONTEXT (resdecl
) = decl
;
1163 allocate_struct_function (decl
, false);
1165 TREE_STATIC (decl
) = 1;
1166 TREE_USED (decl
) = 1;
1167 DECL_ARTIFICIAL (decl
) = 1;
1168 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1169 DECL_SAVED_TREE (decl
) = body
;
1170 if (!targetm
.have_ctors_dtors
&& final
)
1172 TREE_PUBLIC (decl
) = 1;
1173 DECL_PRESERVE_P (decl
) = 1;
1175 DECL_UNINLINABLE (decl
) = 1;
1177 DECL_INITIAL (decl
) = make_node (BLOCK
);
1178 TREE_USED (DECL_INITIAL (decl
)) = 1;
1180 DECL_SOURCE_LOCATION (decl
) = input_location
;
1181 cfun
->function_end_locus
= input_location
;
1186 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1187 decl_init_priority_insert (decl
, priority
);
1190 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1191 decl_fini_priority_insert (decl
, priority
);
1197 gimplify_function_tree (decl
);
1199 cgraph_add_new_function (decl
, false);
1202 current_function_decl
= NULL
;
1205 /* Generate and emit a static constructor or destructor. WHICH must
1206 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1207 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1208 initialization priority for this constructor or destructor. */
1211 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1213 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
1216 /* A vector of FUNCTION_DECLs declared as static constructors. */
1217 static vec
<tree
> static_ctors
;
1218 /* A vector of FUNCTION_DECLs declared as static destructors. */
1219 static vec
<tree
> static_dtors
;
1221 /* When target does not have ctors and dtors, we call all constructor
1222 and destructor by special initialization/destruction function
1223 recognized by collect2.
1225 When we are going to build this function, collect all constructors and
1226 destructors and turn them into normal functions. */
1229 record_cdtor_fn (struct cgraph_node
*node
)
1231 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
))
1232 static_ctors
.safe_push (node
->symbol
.decl
);
1233 if (DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
1234 static_dtors
.safe_push (node
->symbol
.decl
);
1235 node
= cgraph_get_node (node
->symbol
.decl
);
1236 DECL_DISREGARD_INLINE_LIMITS (node
->symbol
.decl
) = 1;
1239 /* Define global constructors/destructor functions for the CDTORS, of
1240 which they are LEN. The CDTORS are sorted by initialization
1241 priority. If CTOR_P is true, these are constructors; otherwise,
1242 they are destructors. */
1245 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
1248 size_t len
= cdtors
.length ();
1255 priority_type priority
;
1264 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1267 else if (p
!= priority
)
1273 /* When there is only one cdtor and target supports them, do nothing. */
1275 && targetm
.have_ctors_dtors
)
1280 /* Find the next batch of constructors/destructors with the same
1281 initialization priority. */
1286 call
= build_call_expr (fn
, 0);
1288 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1290 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1291 /* We do not want to optimize away pure/const calls here.
1292 When optimizing, these should be already removed, when not
1293 optimizing, we want user to be able to breakpoint in them. */
1294 TREE_SIDE_EFFECTS (call
) = 1;
1295 append_to_statement_list (call
, &body
);
1297 gcc_assert (body
!= NULL_TREE
);
1298 /* Generate a function to call all the function of like
1300 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1304 /* Comparison function for qsort. P1 and P2 are actually of type
1305 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1306 used to determine the sort order. */
1309 compare_ctor (const void *p1
, const void *p2
)
1316 f1
= *(const tree
*)p1
;
1317 f2
= *(const tree
*)p2
;
1318 priority1
= DECL_INIT_PRIORITY (f1
);
1319 priority2
= DECL_INIT_PRIORITY (f2
);
1321 if (priority1
< priority2
)
1323 else if (priority1
> priority2
)
1326 /* Ensure a stable sort. Constructors are executed in backwarding
1327 order to make LTO initialize braries first. */
1328 return DECL_UID (f2
) - DECL_UID (f1
);
1331 /* Comparison function for qsort. P1 and P2 are actually of type
1332 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1333 used to determine the sort order. */
1336 compare_dtor (const void *p1
, const void *p2
)
1343 f1
= *(const tree
*)p1
;
1344 f2
= *(const tree
*)p2
;
1345 priority1
= DECL_FINI_PRIORITY (f1
);
1346 priority2
= DECL_FINI_PRIORITY (f2
);
1348 if (priority1
< priority2
)
1350 else if (priority1
> priority2
)
1353 /* Ensure a stable sort. */
1354 return DECL_UID (f1
) - DECL_UID (f2
);
1357 /* Generate functions to call static constructors and destructors
1358 for targets that do not support .ctors/.dtors sections. These
1359 functions have magic names which are detected by collect2. */
1362 build_cdtor_fns (void)
1364 if (!static_ctors
.is_empty ())
1366 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1367 static_ctors
.qsort (compare_ctor
);
1368 build_cdtor (/*ctor_p=*/true, static_ctors
);
1371 if (!static_dtors
.is_empty ())
1373 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1374 static_dtors
.qsort (compare_dtor
);
1375 build_cdtor (/*ctor_p=*/false, static_dtors
);
1379 /* Look for constructors and destructors and produce function calling them.
1380 This is needed for targets not supporting ctors or dtors, but we perform the
1381 transformation also at linktime to merge possibly numerous
1382 constructors/destructors into single function to improve code locality and
1386 ipa_cdtor_merge (void)
1388 struct cgraph_node
*node
;
1389 FOR_EACH_DEFINED_FUNCTION (node
)
1390 if (DECL_STATIC_CONSTRUCTOR (node
->symbol
.decl
)
1391 || DECL_STATIC_DESTRUCTOR (node
->symbol
.decl
))
1392 record_cdtor_fn (node
);
1394 static_ctors
.release ();
1395 static_dtors
.release ();
1399 /* Perform the pass when we have no ctors/dtors support
1400 or at LTO time to merge multiple constructors into single
1404 gate_ipa_cdtor_merge (void)
1406 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1409 struct ipa_opt_pass_d pass_ipa_cdtor_merge
=
1414 OPTGROUP_NONE
, /* optinfo_flags */
1415 gate_ipa_cdtor_merge
, /* gate */
1416 ipa_cdtor_merge
, /* execute */
1419 0, /* static_pass_number */
1420 TV_CGRAPHOPT
, /* tv_id */
1421 0, /* properties_required */
1422 0, /* properties_provided */
1423 0, /* properties_destroyed */
1424 0, /* todo_flags_start */
1425 0 /* todo_flags_finish */
1427 NULL
, /* generate_summary */
1428 NULL
, /* write_summary */
1429 NULL
, /* read_summary */
1430 NULL
, /* write_optimization_summary */
1431 NULL
, /* read_optimization_summary */
1432 NULL
, /* stmt_fixup */
1434 NULL
, /* function_transform */
1435 NULL
/* variable_transform */