1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2014 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"
26 #include "stringpool.h"
28 #include "tree-pass.h"
29 #include "pointer-set.h"
30 #include "gimple-expr.h"
34 #include "tree-iterator.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
37 #include "tree-inline.h"
41 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
44 cgraph_non_local_node_p_1 (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
46 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
47 return !(cgraph_only_called_directly_or_aliased_p (node
)
48 && !ipa_ref_has_aliases_p (&node
->ref_list
)
50 && !DECL_EXTERNAL (node
->decl
)
51 && !node
->externally_visible
52 && !node
->used_from_other_partition
53 && !node
->in_other_partition
);
56 /* Return true when function can be marked local. */
59 cgraph_local_node_p (struct cgraph_node
*node
)
61 struct cgraph_node
*n
= cgraph_function_or_thunk_node (node
, NULL
);
63 /* FIXME: thunks can be considered local, but we need prevent i386
64 from attempting to change calling convention of them. */
67 return !cgraph_for_node_and_aliases (n
,
68 cgraph_non_local_node_p_1
, NULL
, true);
72 /* Return true when NODE has ADDR reference. */
75 has_addr_references_p (struct cgraph_node
*node
,
76 void *data ATTRIBUTE_UNUSED
)
81 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
,
83 if (ref
->use
== IPA_REF_ADDR
)
88 /* Look for all functions inlined to NODE and update their inlined_to pointers
92 update_inlined_to_pointer (struct cgraph_node
*node
, struct cgraph_node
*inlined_to
)
94 struct cgraph_edge
*e
;
95 for (e
= node
->callees
; e
; e
= e
->next_callee
)
96 if (e
->callee
->global
.inlined_to
)
98 e
->callee
->global
.inlined_to
= inlined_to
;
99 update_inlined_to_pointer (e
->callee
, inlined_to
);
103 /* Add symtab NODE to queue starting at FIRST.
105 The queue is linked via AUX pointers and terminated by pointer to 1.
106 We enqueue nodes at two occasions: when we find them reachable or when we find
107 their bodies needed for further clonning. In the second case we mark them
108 by pointer to 2 after processing so they are re-queue when they become
112 enqueue_node (symtab_node
*node
, symtab_node
**first
,
113 struct pointer_set_t
*reachable
)
115 /* Node is still in queue; do nothing. */
116 if (node
->aux
&& node
->aux
!= (void *) 2)
118 /* Node was already processed as unreachable, re-enqueue
119 only if it became reachable now. */
120 if (node
->aux
== (void *)2 && !pointer_set_contains (reachable
, node
))
126 /* Process references. */
129 process_references (struct ipa_ref_list
*list
,
131 bool before_inlining_p
,
132 struct pointer_set_t
*reachable
)
136 for (i
= 0; ipa_ref_list_reference_iterate (list
, i
, ref
); i
++)
138 symtab_node
*node
= ref
->referred
;
140 if (node
->definition
&& !node
->in_other_partition
141 && ((!DECL_EXTERNAL (node
->decl
) || node
->alias
)
142 || (((before_inlining_p
143 && (cgraph_state
< CGRAPH_STATE_IPA_SSA
144 || !lookup_attribute ("always_inline",
145 DECL_ATTRIBUTES (node
->decl
)))))
146 /* We use variable constructors during late complation for
147 constant folding. Keep references alive so partitioning
148 knows about potential references. */
149 || (TREE_CODE (node
->decl
) == VAR_DECL
151 && ctor_for_folding (node
->decl
)
152 != error_mark_node
))))
153 pointer_set_insert (reachable
, node
);
154 enqueue_node (node
, first
, reachable
);
158 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
159 all its potential targets as reachable to permit later inlining if
160 devirtualization happens. After inlining still keep their declarations
161 around, so we can devirtualize to a direct call.
163 Also try to make trivial devirutalization when no or only one target is
167 walk_polymorphic_call_targets (pointer_set_t
*reachable_call_targets
,
168 struct cgraph_edge
*edge
,
170 pointer_set_t
*reachable
, bool before_inlining_p
)
175 vec
<cgraph_node
*>targets
176 = possible_polymorphic_call_targets
177 (edge
, &final
, &cache_token
);
179 if (!pointer_set_insert (reachable_call_targets
,
182 for (i
= 0; i
< targets
.length (); i
++)
184 struct cgraph_node
*n
= targets
[i
];
186 /* Do not bother to mark virtual methods in anonymous namespace;
187 either we will find use of virtual table defining it, or it is
189 if (TREE_CODE (TREE_TYPE (n
->decl
)) == METHOD_TYPE
190 && type_in_anonymous_namespace_p
191 (method_class_type (TREE_TYPE (n
->decl
))))
194 /* Prior inlining, keep alive bodies of possible targets for
197 && (before_inlining_p
198 && (cgraph_state
< CGRAPH_STATE_IPA_SSA
199 || !lookup_attribute ("always_inline",
200 DECL_ATTRIBUTES (n
->decl
)))))
201 pointer_set_insert (reachable
, n
);
203 /* Even after inlining we want to keep the possible targets in the
204 boundary, so late passes can still produce direct call even if
205 the chance for inlining is lost. */
206 enqueue_node (n
, first
, reachable
);
210 /* Very trivial devirtualization; when the type is
211 final or anonymous (so we know all its derivation)
212 and there is only one possible virtual call target,
213 make the edge direct. */
216 if (targets
.length () <= 1)
218 cgraph_node
*target
, *node
= edge
->caller
;
219 if (targets
.length () == 1)
222 target
= cgraph_get_create_node
223 (builtin_decl_implicit (BUILT_IN_UNREACHABLE
));
227 "Devirtualizing call in %s/%i to %s/%i\n",
228 edge
->caller
->name (),
230 target
->name (), target
->order
);
231 edge
= cgraph_make_edge_direct (edge
, target
);
232 if (inline_summary_vec
)
233 inline_update_overall_summary (node
);
234 else if (edge
->call_stmt
)
235 cgraph_redirect_edge_call_stmt_to_callee (edge
);
240 /* Perform reachability analysis and reclaim all unreachable nodes.
242 The algorithm is basically mark&sweep but with some extra refinements:
244 - reachable extern inline functions needs special handling; the bodies needs
245 to stay in memory until inlining in hope that they will be inlined.
246 After inlining we release their bodies and turn them into unanalyzed
247 nodes even when they are reachable.
249 BEFORE_INLINING_P specify whether we are before or after inlining.
251 - virtual functions are kept in callgraph even if they seem unreachable in
252 hope calls to them will be devirtualized.
254 Again we remove them after inlining. In late optimization some
255 devirtualization may happen, but it is not important since we won't inline
256 the call. In theory early opts and IPA should work out all important cases.
258 - virtual clones needs bodies of their origins for later materialization;
259 this means that we want to keep the body even if the origin is unreachable
260 otherwise. To avoid origin from sitting in the callgraph and being
261 walked by IPA passes, we turn them into unanalyzed nodes with body
264 We maintain set of function declaration where body needs to stay in
265 body_needed_for_clonning
267 Inline clones represent special case: their declaration match the
268 declaration of origin and cgraph_remove_node already knows how to
269 reshape callgraph and preserve body when offline copy of function or
270 inline clone is being removed.
272 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
273 variables with DECL_INITIAL set. We finalize these and keep reachable
274 ones around for constant folding purposes. After inlining we however
275 stop walking their references to let everything static referneced by them
276 to be removed when it is otherwise unreachable.
278 We maintain queue of both reachable symbols (i.e. defined symbols that needs
279 to stay) and symbols that are in boundary (i.e. external symbols referenced
280 by reachable symbols or origins of clones). The queue is represented
281 as linked list by AUX pointer terminated by 1.
283 At the end we keep all reachable symbols. For symbols in boundary we always
284 turn definition into a declaration, but we may keep function body around
285 based on body_needed_for_clonning
287 All symbols that enter the queue have AUX pointer non-zero and are in the
288 boundary. Pointer set REACHABLE is used to track reachable symbols.
290 Every symbol can be visited twice - once as part of boundary and once
291 as real reachable symbol. enqueue_node needs to decide whether the
292 node needs to be re-queued for second processing. For this purpose
293 we set AUX pointer of processed symbols in the boundary to constant 2. */
296 symtab_remove_unreachable_nodes (bool before_inlining_p
, FILE *file
)
298 symtab_node
*first
= (symtab_node
*) (void *) 1;
299 struct cgraph_node
*node
, *next
;
300 varpool_node
*vnode
, *vnext
;
301 bool changed
= false;
302 struct pointer_set_t
*reachable
= pointer_set_create ();
303 struct pointer_set_t
*body_needed_for_clonning
= pointer_set_create ();
304 struct pointer_set_t
*reachable_call_targets
= pointer_set_create ();
306 timevar_push (TV_IPA_UNREACHABLE
);
307 #ifdef ENABLE_CHECKING
310 if (optimize
&& flag_devirtualize
)
311 build_type_inheritance_graph ();
313 fprintf (file
, "\nReclaiming functions:");
314 #ifdef ENABLE_CHECKING
315 FOR_EACH_FUNCTION (node
)
316 gcc_assert (!node
->aux
);
317 FOR_EACH_VARIABLE (vnode
)
318 gcc_assert (!vnode
->aux
);
320 /* Mark functions whose bodies are obviously needed.
321 This is mostly when they can be referenced externally. Inline clones
322 are special since their declarations are shared with master clone and thus
323 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
324 FOR_EACH_FUNCTION (node
)
326 node
->used_as_abstract_origin
= false;
328 && !node
->global
.inlined_to
329 && !node
->in_other_partition
330 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node
))
332 gcc_assert (!node
->global
.inlined_to
);
333 pointer_set_insert (reachable
, node
);
334 enqueue_node (node
, &first
, reachable
);
337 gcc_assert (!node
->aux
);
340 /* Mark variables that are obviously needed. */
341 FOR_EACH_DEFINED_VARIABLE (vnode
)
342 if (!varpool_can_remove_if_no_refs (vnode
)
343 && !vnode
->in_other_partition
)
345 pointer_set_insert (reachable
, vnode
);
346 enqueue_node (vnode
, &first
, reachable
);
349 /* Perform reachability analysis. */
350 while (first
!= (symtab_node
*) (void *) 1)
352 bool in_boundary_p
= !pointer_set_contains (reachable
, first
);
353 symtab_node
*node
= first
;
355 first
= (symtab_node
*)first
->aux
;
357 /* If we are processing symbol in boundary, mark its AUX pointer for
358 possible later re-processing in enqueue_node. */
360 node
->aux
= (void *)2;
363 if (TREE_CODE (node
->decl
) == FUNCTION_DECL
364 && DECL_ABSTRACT_ORIGIN (node
->decl
))
366 struct cgraph_node
*origin_node
367 = cgraph_get_node (DECL_ABSTRACT_ORIGIN (node
->decl
));
368 if (origin_node
&& !origin_node
->used_as_abstract_origin
)
370 origin_node
->used_as_abstract_origin
= true;
371 gcc_assert (!origin_node
->prev_sibling_clone
);
372 gcc_assert (!origin_node
->next_sibling_clone
);
373 for (cgraph_node
*n
= origin_node
->clones
; n
;
374 n
= n
->next_sibling_clone
)
375 if (n
->decl
== DECL_ABSTRACT_ORIGIN (node
->decl
))
376 n
->used_as_abstract_origin
= true;
379 /* If any symbol in a comdat group is reachable, force
380 all externally visible symbols in the same comdat
381 group to be reachable as well. Comdat-local symbols
382 can be discarded if all uses were inlined. */
383 if (node
->same_comdat_group
)
386 for (next
= node
->same_comdat_group
;
388 next
= next
->same_comdat_group
)
389 if (!symtab_comdat_local_p (next
)
390 && !pointer_set_insert (reachable
, next
))
391 enqueue_node (next
, &first
, reachable
);
393 /* Mark references as reachable. */
394 process_references (&node
->ref_list
, &first
,
395 before_inlining_p
, reachable
);
398 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
> (node
))
400 /* Mark the callees reachable unless they are direct calls to extern
401 inline functions we decided to not inline. */
404 struct cgraph_edge
*e
;
405 /* Keep alive possible targets for devirtualization. */
406 if (optimize
&& flag_devirtualize
)
408 struct cgraph_edge
*next
;
409 for (e
= cnode
->indirect_calls
; e
; e
= next
)
411 next
= e
->next_callee
;
412 if (e
->indirect_info
->polymorphic
)
413 walk_polymorphic_call_targets (reachable_call_targets
,
414 e
, &first
, reachable
,
418 for (e
= cnode
->callees
; e
; e
= e
->next_callee
)
420 if (e
->callee
->definition
421 && !e
->callee
->in_other_partition
422 && (!e
->inline_failed
423 || !DECL_EXTERNAL (e
->callee
->decl
)
425 || before_inlining_p
))
427 /* Be sure that we will not optimize out alias target
429 if (DECL_EXTERNAL (e
->callee
->decl
)
431 && before_inlining_p
)
433 pointer_set_insert (reachable
,
434 cgraph_function_node (e
->callee
));
436 pointer_set_insert (reachable
, e
->callee
);
438 enqueue_node (e
->callee
, &first
, reachable
);
441 /* When inline clone exists, mark body to be preserved so when removing
442 offline copy of the function we don't kill it. */
443 if (cnode
->global
.inlined_to
)
444 pointer_set_insert (body_needed_for_clonning
, cnode
->decl
);
446 /* For non-inline clones, force their origins to the boundary and ensure
447 that body is not removed. */
448 while (cnode
->clone_of
)
450 bool noninline
= cnode
->clone_of
->decl
!= cnode
->decl
;
451 cnode
= cnode
->clone_of
;
454 pointer_set_insert (body_needed_for_clonning
, cnode
->decl
);
455 enqueue_node (cnode
, &first
, reachable
);
460 /* If any reachable function has simd clones, mark them as
461 reachable as well. */
462 if (cnode
->simd_clones
)
465 for (next
= cnode
->simd_clones
;
467 next
= next
->simdclone
->next_clone
)
469 || !pointer_set_insert (reachable
, next
))
470 enqueue_node (next
, &first
, reachable
);
473 /* When we see constructor of external variable, keep referred nodes in the
474 boundary. This will also hold initializers of the external vars NODE
476 varpool_node
*vnode
= dyn_cast
<varpool_node
> (node
);
478 && DECL_EXTERNAL (node
->decl
)
483 for (int i
= 0; ipa_ref_list_reference_iterate (&node
->ref_list
, i
, ref
); i
++)
484 enqueue_node (ref
->referred
, &first
, reachable
);
488 /* Remove unreachable functions. */
489 for (node
= cgraph_first_function (); node
; node
= next
)
491 next
= cgraph_next_function (node
);
493 /* If node is not needed at all, remove it. */
497 fprintf (file
, " %s/%i", node
->name (), node
->order
);
498 cgraph_remove_node (node
);
501 /* If node is unreachable, remove its body. */
502 else if (!pointer_set_contains (reachable
, node
))
504 if (!pointer_set_contains (body_needed_for_clonning
, node
->decl
))
505 cgraph_release_function_body (node
);
506 else if (!node
->clone_of
)
507 gcc_assert (in_lto_p
|| DECL_RESULT (node
->decl
));
508 if (node
->definition
)
511 fprintf (file
, " %s/%i", node
->name (), node
->order
);
512 node
->body_removed
= true;
513 node
->analyzed
= false;
514 node
->definition
= false;
515 node
->cpp_implicit_alias
= false;
517 node
->thunk
.thunk_p
= false;
518 node
->weakref
= false;
519 /* After early inlining we drop always_inline attributes on
520 bodies of functions that are still referenced (have their
522 DECL_ATTRIBUTES (node
->decl
)
523 = remove_attribute ("always_inline",
524 DECL_ATTRIBUTES (node
->decl
));
525 if (!node
->in_other_partition
)
526 node
->local
.local
= false;
527 cgraph_node_remove_callees (node
);
528 symtab_remove_from_same_comdat_group (node
);
529 ipa_remove_all_references (&node
->ref_list
);
534 gcc_assert (node
->clone_of
|| !cgraph_function_with_gimple_body_p (node
)
535 || in_lto_p
|| DECL_RESULT (node
->decl
));
538 /* Inline clones might be kept around so their materializing allows further
539 cloning. If the function the clone is inlined into is removed, we need
540 to turn it into normal cone. */
541 FOR_EACH_FUNCTION (node
)
543 if (node
->global
.inlined_to
546 gcc_assert (node
->clones
);
547 node
->global
.inlined_to
= NULL
;
548 update_inlined_to_pointer (node
, node
);
553 /* Remove unreachable variables. */
555 fprintf (file
, "\nReclaiming variables:");
556 for (vnode
= varpool_first_variable (); vnode
; vnode
= vnext
)
558 vnext
= varpool_next_variable (vnode
);
560 /* For can_refer_decl_in_current_unit_p we want to track for
561 all external variables if they are defined in other partition
563 && (!flag_ltrans
|| !DECL_EXTERNAL (vnode
->decl
)))
566 fprintf (file
, " %s/%i", vnode
->name (), vnode
->order
);
567 varpool_remove_node (vnode
);
570 else if (!pointer_set_contains (reachable
, vnode
))
573 if (vnode
->definition
)
576 fprintf (file
, " %s", vnode
->name ());
579 vnode
->body_removed
= true;
580 vnode
->definition
= false;
581 vnode
->analyzed
= false;
584 symtab_remove_from_same_comdat_group (vnode
);
586 /* Keep body if it may be useful for constant folding. */
587 if ((init
= ctor_for_folding (vnode
->decl
)) == error_mark_node
)
588 varpool_remove_initializer (vnode
);
590 DECL_INITIAL (vnode
->decl
) = init
;
591 ipa_remove_all_references (&vnode
->ref_list
);
597 pointer_set_destroy (reachable
);
598 pointer_set_destroy (body_needed_for_clonning
);
599 pointer_set_destroy (reachable_call_targets
);
601 /* Now update address_taken flags and try to promote functions to be local. */
603 fprintf (file
, "\nClearing address taken flags:");
604 FOR_EACH_DEFINED_FUNCTION (node
)
605 if (node
->address_taken
606 && !node
->used_from_other_partition
)
608 if (!cgraph_for_node_and_aliases (node
, has_addr_references_p
, NULL
, true))
611 fprintf (file
, " %s", node
->name ());
612 node
->address_taken
= false;
614 if (cgraph_local_node_p (node
))
616 node
->local
.local
= true;
618 fprintf (file
, " (local)");
623 fprintf (file
, "\n");
625 #ifdef ENABLE_CHECKING
629 /* If we removed something, perhaps profile could be improved. */
630 if (changed
&& optimize
&& inline_edge_summary_vec
.exists ())
631 FOR_EACH_DEFINED_FUNCTION (node
)
632 ipa_propagate_frequency (node
);
634 timevar_pop (TV_IPA_UNREACHABLE
);
638 /* Discover variables that have no longer address taken or that are read only
639 and update their flags.
641 FIXME: This can not be done in between gimplify and omp_expand since
642 readonly flag plays role on what is shared and what is not. Currently we do
643 this transformation as part of whole program visibility and re-do at
644 ipa-reference pass (to take into account clonning), but it would
645 make sense to do it before early optimizations. */
648 ipa_discover_readonly_nonaddressable_vars (void)
652 fprintf (dump_file
, "Clearing variable flags:");
653 FOR_EACH_VARIABLE (vnode
)
654 if (vnode
->definition
&& varpool_all_refs_explicit_p (vnode
)
655 && (TREE_ADDRESSABLE (vnode
->decl
)
656 || !TREE_READONLY (vnode
->decl
)))
658 bool written
= false;
659 bool address_taken
= false;
662 for (i
= 0; ipa_ref_list_referring_iterate (&vnode
->ref_list
,
664 && (!written
|| !address_taken
); i
++)
668 address_taken
= true;
676 if (TREE_ADDRESSABLE (vnode
->decl
) && !address_taken
)
679 fprintf (dump_file
, " %s (addressable)", vnode
->name ());
680 TREE_ADDRESSABLE (vnode
->decl
) = 0;
682 if (!TREE_READONLY (vnode
->decl
) && !address_taken
&& !written
683 /* Making variable in explicit section readonly can cause section
685 See e.g. gcc.c-torture/compile/pr23237.c */
686 && DECL_SECTION_NAME (vnode
->decl
) == NULL
)
689 fprintf (dump_file
, " %s (read-only)", vnode
->name ());
690 TREE_READONLY (vnode
->decl
) = 1;
694 fprintf (dump_file
, "\n");
697 /* Return true when there is a reference to node and it is not vtable. */
699 address_taken_from_non_vtable_p (symtab_node
*node
)
703 for (i
= 0; ipa_ref_list_referring_iterate (&node
->ref_list
,
705 if (ref
->use
== IPA_REF_ADDR
)
708 if (is_a
<cgraph_node
> (ref
->referring
))
710 node
= ipa_ref_referring_varpool_node (ref
);
711 if (!DECL_VIRTUAL_P (node
->decl
))
717 /* A helper for comdat_can_be_unshared_p. */
720 comdat_can_be_unshared_p_1 (symtab_node
*node
)
722 if (!node
->externally_visible
)
724 /* When address is taken, we don't know if equality comparison won't
725 break eventually. Exception are virutal functions, C++
726 constructors/destructors and vtables, where this is not possible by
727 language standard. */
728 if (!DECL_VIRTUAL_P (node
->decl
)
729 && (TREE_CODE (node
->decl
) != FUNCTION_DECL
730 || (!DECL_CXX_CONSTRUCTOR_P (node
->decl
)
731 && !DECL_CXX_DESTRUCTOR_P (node
->decl
)))
732 && address_taken_from_non_vtable_p (node
))
735 /* If the symbol is used in some weird way, better to not touch it. */
736 if (node
->force_output
)
739 /* Explicit instantiations needs to be output when possibly
741 if (node
->forced_by_abi
742 && TREE_PUBLIC (node
->decl
)
743 && (node
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
744 && !flag_whole_program
))
747 /* Non-readonly and volatile variables can not be duplicated. */
748 if (is_a
<varpool_node
> (node
)
749 && (!TREE_READONLY (node
->decl
)
750 || TREE_THIS_VOLATILE (node
->decl
)))
755 /* COMDAT functions must be shared only if they have address taken,
756 otherwise we can produce our own private implementation with
758 Return true when turning COMDAT functoin static can not lead to wrong
759 code when the resulting object links with a library defining same COMDAT.
761 Virtual functions do have their addresses taken from the vtables,
762 but in C++ there is no way to compare their addresses for equality. */
765 comdat_can_be_unshared_p (symtab_node
*node
)
767 if (!comdat_can_be_unshared_p_1 (node
))
769 if (node
->same_comdat_group
)
773 /* If more than one function is in the same COMDAT group, it must
774 be shared even if just one function in the comdat group has
776 for (next
= node
->same_comdat_group
;
777 next
!= node
; next
= next
->same_comdat_group
)
778 if (!comdat_can_be_unshared_p_1 (next
))
784 /* Return true when function NODE should be considered externally visible. */
787 cgraph_externally_visible_p (struct cgraph_node
*node
,
790 if (!node
->definition
)
792 if (!TREE_PUBLIC (node
->decl
)
793 || DECL_EXTERNAL (node
->decl
))
796 /* Do not try to localize built-in functions yet. One of problems is that we
797 end up mangling their asm for WHOPR that makes it impossible to call them
798 using the implicit built-in declarations anymore. Similarly this enables
799 us to remove them as unreachable before actual calls may appear during
800 expansion or folding. */
801 if (DECL_BUILT_IN (node
->decl
))
804 /* If linker counts on us, we must preserve the function. */
805 if (symtab_used_from_object_file_p (node
))
807 if (DECL_PRESERVE_P (node
->decl
))
809 if (lookup_attribute ("externally_visible",
810 DECL_ATTRIBUTES (node
->decl
)))
812 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
813 && lookup_attribute ("dllexport",
814 DECL_ATTRIBUTES (node
->decl
)))
816 if (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
818 /* When doing LTO or whole program, we can bring COMDAT functoins static.
819 This improves code quality and we know we will duplicate them at most twice
820 (in the case that we are not using plugin and link with object file
821 implementing same COMDAT) */
822 if ((in_lto_p
|| whole_program
)
823 && DECL_COMDAT (node
->decl
)
824 && comdat_can_be_unshared_p (node
))
827 /* When doing link time optimizations, hidden symbols become local. */
829 && (DECL_VISIBILITY (node
->decl
) == VISIBILITY_HIDDEN
830 || DECL_VISIBILITY (node
->decl
) == VISIBILITY_INTERNAL
)
831 /* Be sure that node is defined in IR file, not in other object
832 file. In that case we don't set used_from_other_object_file. */
835 else if (!whole_program
)
838 if (MAIN_NAME_P (DECL_NAME (node
->decl
)))
844 /* Return true when variable VNODE should be considered externally visible. */
847 varpool_externally_visible_p (varpool_node
*vnode
)
849 if (DECL_EXTERNAL (vnode
->decl
))
852 if (!TREE_PUBLIC (vnode
->decl
))
855 /* If linker counts on us, we must preserve the function. */
856 if (symtab_used_from_object_file_p (vnode
))
859 if (DECL_HARD_REGISTER (vnode
->decl
))
861 if (DECL_PRESERVE_P (vnode
->decl
))
863 if (lookup_attribute ("externally_visible",
864 DECL_ATTRIBUTES (vnode
->decl
)))
866 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
867 && lookup_attribute ("dllexport",
868 DECL_ATTRIBUTES (vnode
->decl
)))
871 /* See if we have linker information about symbol not being used or
872 if we need to make guess based on the declaration.
874 Even if the linker clams the symbol is unused, never bring internal
875 symbols that are declared by user as used or externally visible.
876 This is needed for i.e. references from asm statements. */
877 if (symtab_used_from_object_file_p (vnode
))
879 if (vnode
->resolution
== LDPR_PREVAILING_DEF_IRONLY
)
882 /* As a special case, the COMDAT virtual tables can be unshared.
883 In LTO mode turn vtables into static variables. The variable is readonly,
884 so this does not enable more optimization, but referring static var
885 is faster for dynamic linking. Also this match logic hidding vtables
886 from LTO symbol tables. */
887 if ((in_lto_p
|| flag_whole_program
)
888 && DECL_COMDAT (vnode
->decl
)
889 && comdat_can_be_unshared_p (vnode
))
892 /* When doing link time optimizations, hidden symbols become local. */
894 && (DECL_VISIBILITY (vnode
->decl
) == VISIBILITY_HIDDEN
895 || DECL_VISIBILITY (vnode
->decl
) == VISIBILITY_INTERNAL
)
896 /* Be sure that node is defined in IR file, not in other object
897 file. In that case we don't set used_from_other_object_file. */
898 && vnode
->definition
)
900 else if (!flag_whole_program
)
903 /* Do not attempt to privatize COMDATS by default.
904 This would break linking with C++ libraries sharing
907 FIXME: We can do so for readonly vars with no address taken and
908 possibly also for vtables since no direct pointer comparsion is done.
909 It might be interesting to do so to reduce linking overhead. */
910 if (DECL_COMDAT (vnode
->decl
) || DECL_WEAK (vnode
->decl
))
915 /* Return true if reference to NODE can be replaced by a local alias.
916 Local aliases save dynamic linking overhead and enable more optimizations.
920 can_replace_by_local_alias (symtab_node
*node
)
922 return (symtab_node_availability (node
) > AVAIL_OVERWRITABLE
923 && !symtab_can_be_discarded (node
));
926 /* Mark visibility of all functions.
928 A local function is one whose calls can occur only in the current
929 compilation unit and all its calls are explicit, so we can change
930 its calling convention. We simply mark all static functions whose
931 address is not taken as local.
933 We also change the TREE_PUBLIC flag of all declarations that are public
934 in language point of view but we want to overwrite this default
935 via visibilities for the backend point of view. */
938 function_and_variable_visibility (bool whole_program
)
940 struct cgraph_node
*node
;
943 /* All aliases should be procssed at this point. */
944 gcc_checking_assert (!alias_pairs
|| !alias_pairs
->length ());
946 FOR_EACH_FUNCTION (node
)
948 int flags
= flags_from_decl_or_type (node
->decl
);
950 /* Optimize away PURE and CONST constructors and destructors. */
952 && (flags
& (ECF_CONST
| ECF_PURE
))
953 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
955 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
956 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
959 /* Frontends and alias code marks nodes as needed before parsing is finished.
960 We may end up marking as node external nodes where this flag is meaningless
962 if (DECL_EXTERNAL (node
->decl
) || !node
->definition
)
964 node
->force_output
= 0;
965 node
->forced_by_abi
= 0;
968 /* C++ FE on lack of COMDAT support create local COMDAT functions
969 (that ought to be shared but can not due to object format
970 limitations). It is necessary to keep the flag to make rest of C++ FE
971 happy. Clear the flag here to avoid confusion in middle-end. */
972 if (DECL_COMDAT (node
->decl
) && !TREE_PUBLIC (node
->decl
))
973 DECL_COMDAT (node
->decl
) = 0;
975 /* For external decls stop tracking same_comdat_group. It doesn't matter
976 what comdat group they are in when they won't be emitted in this TU. */
977 if (node
->same_comdat_group
&& DECL_EXTERNAL (node
->decl
))
979 #ifdef ENABLE_CHECKING
982 for (n
= node
->same_comdat_group
;
984 n
= n
->same_comdat_group
)
985 /* If at least one of same comdat group functions is external,
986 all of them have to be, otherwise it is a front-end bug. */
987 gcc_assert (DECL_EXTERNAL (n
->decl
));
989 symtab_dissolve_same_comdat_group_list (node
);
991 gcc_assert ((!DECL_WEAK (node
->decl
)
992 && !DECL_COMDAT (node
->decl
))
993 || TREE_PUBLIC (node
->decl
)
995 || DECL_EXTERNAL (node
->decl
));
996 if (cgraph_externally_visible_p (node
, whole_program
))
998 gcc_assert (!node
->global
.inlined_to
);
999 node
->externally_visible
= true;
1003 node
->externally_visible
= false;
1004 node
->forced_by_abi
= false;
1006 if (!node
->externally_visible
1007 && node
->definition
&& !node
->weakref
1008 && !DECL_EXTERNAL (node
->decl
))
1010 gcc_assert (whole_program
|| in_lto_p
1011 || !TREE_PUBLIC (node
->decl
));
1012 node
->unique_name
= ((node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
1013 || node
->unique_name
1014 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
1015 && TREE_PUBLIC (node
->decl
));
1016 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
1017 if (node
->same_comdat_group
&& TREE_PUBLIC (node
->decl
))
1019 symtab_node
*next
= node
;
1021 /* Set all members of comdat group local. */
1022 if (node
->same_comdat_group
)
1023 for (next
= node
->same_comdat_group
;
1025 next
= next
->same_comdat_group
)
1027 symtab_make_decl_local (next
->decl
);
1028 next
->unique_name
= ((next
->resolution
== LDPR_PREVAILING_DEF_IRONLY
1029 || next
->unique_name
1030 || next
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
1031 && TREE_PUBLIC (next
->decl
));
1033 /* cgraph_externally_visible_p has already checked all other nodes
1034 in the group and they will all be made local. We need to
1035 dissolve the group at once so that the predicate does not
1037 symtab_dissolve_same_comdat_group_list (node
);
1039 symtab_make_decl_local (node
->decl
);
1042 if (node
->thunk
.thunk_p
1043 && TREE_PUBLIC (node
->decl
))
1045 struct cgraph_node
*decl_node
= node
;
1047 decl_node
= cgraph_function_node (decl_node
->callees
->callee
, NULL
);
1049 /* Thunks have the same visibility as function they are attached to.
1050 Make sure the C++ front end set this up properly. */
1051 if (DECL_ONE_ONLY (decl_node
->decl
))
1053 gcc_checking_assert (DECL_COMDAT (node
->decl
)
1054 == DECL_COMDAT (decl_node
->decl
));
1055 gcc_checking_assert (DECL_COMDAT_GROUP (node
->decl
)
1056 == DECL_COMDAT_GROUP (decl_node
->decl
));
1057 gcc_checking_assert (node
->same_comdat_group
);
1059 node
->forced_by_abi
= decl_node
->forced_by_abi
;
1060 if (DECL_EXTERNAL (decl_node
->decl
))
1061 DECL_EXTERNAL (node
->decl
) = 1;
1064 /* If whole comdat group is used only within LTO code, we can dissolve it,
1065 we handle the unification ourselves.
1066 We keep COMDAT and weak so visibility out of DSO does not change.
1067 Later we may bring the symbols static if they are not exported. */
1068 if (DECL_ONE_ONLY (node
->decl
)
1069 && (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
1070 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
))
1072 symtab_node
*next
= node
;
1074 if (node
->same_comdat_group
)
1075 for (next
= node
->same_comdat_group
;
1077 next
= next
->same_comdat_group
)
1078 if (next
->externally_visible
1079 && (next
->resolution
!= LDPR_PREVAILING_DEF_IRONLY
1080 && next
->resolution
!= LDPR_PREVAILING_DEF_IRONLY_EXP
))
1084 if (node
->same_comdat_group
)
1085 for (next
= node
->same_comdat_group
;
1087 next
= next
->same_comdat_group
)
1089 DECL_COMDAT_GROUP (next
->decl
) = NULL
;
1090 DECL_WEAK (next
->decl
) = false;
1092 DECL_COMDAT_GROUP (node
->decl
) = NULL
;
1093 symtab_dissolve_same_comdat_group_list (node
);
1097 FOR_EACH_DEFINED_FUNCTION (node
)
1099 node
->local
.local
|= cgraph_local_node_p (node
);
1101 /* If we know that function can not be overwritten by a different semantics
1102 and moreover its section can not be discarded, replace all direct calls
1103 by calls to an nonoverwritable alias. This make dynamic linking
1104 cheaper and enable more optimization.
1106 TODO: We can also update virtual tables. */
1107 if (node
->callers
&& can_replace_by_local_alias (node
))
1109 struct cgraph_node
*alias
= cgraph (symtab_nonoverwritable_alias (node
));
1111 if (alias
&& alias
!= node
)
1113 while (node
->callers
)
1115 struct cgraph_edge
*e
= node
->callers
;
1117 cgraph_redirect_edge_callee (e
, alias
);
1118 if (gimple_has_body_p (e
->caller
->decl
))
1120 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
1121 cgraph_redirect_edge_call_stmt_to_callee (e
);
1128 FOR_EACH_VARIABLE (vnode
)
1130 /* weak flag makes no sense on local variables. */
1131 gcc_assert (!DECL_WEAK (vnode
->decl
)
1133 || TREE_PUBLIC (vnode
->decl
)
1134 || DECL_EXTERNAL (vnode
->decl
));
1135 /* In several cases declarations can not be common:
1137 - when declaration has initializer
1138 - when it is in weak
1139 - when it has specific section
1140 - when it resides in non-generic address space.
1141 - if declaration is local, it will get into .local common section
1142 so common flag is not needed. Frontends still produce these in
1143 certain cases, such as for:
1145 static int a __attribute__ ((common))
1147 Canonicalize things here and clear the redundant flag. */
1148 if (DECL_COMMON (vnode
->decl
)
1149 && (!(TREE_PUBLIC (vnode
->decl
)
1150 || DECL_EXTERNAL (vnode
->decl
))
1151 || (DECL_INITIAL (vnode
->decl
)
1152 && DECL_INITIAL (vnode
->decl
) != error_mark_node
)
1153 || DECL_WEAK (vnode
->decl
)
1154 || DECL_SECTION_NAME (vnode
->decl
) != NULL
1155 || ! (ADDR_SPACE_GENERIC_P
1156 (TYPE_ADDR_SPACE (TREE_TYPE (vnode
->decl
))))))
1157 DECL_COMMON (vnode
->decl
) = 0;
1159 FOR_EACH_DEFINED_VARIABLE (vnode
)
1161 if (!vnode
->definition
)
1163 if (varpool_externally_visible_p (vnode
))
1164 vnode
->externally_visible
= true;
1167 vnode
->externally_visible
= false;
1168 vnode
->forced_by_abi
= false;
1170 if (!vnode
->externally_visible
1173 gcc_assert (in_lto_p
|| whole_program
|| !TREE_PUBLIC (vnode
->decl
));
1174 vnode
->unique_name
= ((vnode
->resolution
== LDPR_PREVAILING_DEF_IRONLY
1175 || vnode
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
)
1176 && TREE_PUBLIC (vnode
->decl
));
1177 symtab_make_decl_local (vnode
->decl
);
1178 if (vnode
->same_comdat_group
)
1179 symtab_dissolve_same_comdat_group_list (vnode
);
1180 vnode
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
1186 fprintf (dump_file
, "\nMarking local functions:");
1187 FOR_EACH_DEFINED_FUNCTION (node
)
1188 if (node
->local
.local
)
1189 fprintf (dump_file
, " %s", node
->name ());
1190 fprintf (dump_file
, "\n\n");
1191 fprintf (dump_file
, "\nMarking externally visible functions:");
1192 FOR_EACH_DEFINED_FUNCTION (node
)
1193 if (node
->externally_visible
)
1194 fprintf (dump_file
, " %s", node
->name ());
1195 fprintf (dump_file
, "\n\n");
1196 fprintf (dump_file
, "\nMarking externally visible variables:");
1197 FOR_EACH_DEFINED_VARIABLE (vnode
)
1198 if (vnode
->externally_visible
)
1199 fprintf (dump_file
, " %s", vnode
->name ());
1200 fprintf (dump_file
, "\n\n");
1202 cgraph_function_flags_ready
= true;
1206 /* Local function pass handling visibilities. This happens before LTO streaming
1207 so in particular -fwhole-program should be ignored at this level. */
1210 local_function_and_variable_visibility (void)
1212 return function_and_variable_visibility (flag_whole_program
&& !flag_lto
);
1217 const pass_data pass_data_ipa_function_and_variable_visibility
=
1219 SIMPLE_IPA_PASS
, /* type */
1220 "visibility", /* name */
1221 OPTGROUP_NONE
, /* optinfo_flags */
1222 false, /* has_gate */
1223 true, /* has_execute */
1224 TV_CGRAPHOPT
, /* tv_id */
1225 0, /* properties_required */
1226 0, /* properties_provided */
1227 0, /* properties_destroyed */
1228 0, /* todo_flags_start */
1229 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
1232 class pass_ipa_function_and_variable_visibility
: public simple_ipa_opt_pass
1235 pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
1236 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility
,
1240 /* opt_pass methods: */
1241 unsigned int execute () {
1242 return local_function_and_variable_visibility ();
1245 }; // class pass_ipa_function_and_variable_visibility
1249 simple_ipa_opt_pass
*
1250 make_pass_ipa_function_and_variable_visibility (gcc::context
*ctxt
)
1252 return new pass_ipa_function_and_variable_visibility (ctxt
);
1255 /* Free inline summary. */
1258 free_inline_summary (void)
1260 inline_free_summary ();
1266 const pass_data pass_data_ipa_free_inline_summary
=
1268 SIMPLE_IPA_PASS
, /* type */
1269 "*free_inline_summary", /* name */
1270 OPTGROUP_NONE
, /* optinfo_flags */
1271 false, /* has_gate */
1272 true, /* has_execute */
1273 TV_IPA_FREE_INLINE_SUMMARY
, /* tv_id */
1274 0, /* properties_required */
1275 0, /* properties_provided */
1276 0, /* properties_destroyed */
1277 0, /* todo_flags_start */
1278 0, /* todo_flags_finish */
1281 class pass_ipa_free_inline_summary
: public simple_ipa_opt_pass
1284 pass_ipa_free_inline_summary (gcc::context
*ctxt
)
1285 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary
, ctxt
)
1288 /* opt_pass methods: */
1289 unsigned int execute () { return free_inline_summary (); }
1291 }; // class pass_ipa_free_inline_summary
1295 simple_ipa_opt_pass
*
1296 make_pass_ipa_free_inline_summary (gcc::context
*ctxt
)
1298 return new pass_ipa_free_inline_summary (ctxt
);
1301 /* Do not re-run on ltrans stage. */
1304 gate_whole_program_function_and_variable_visibility (void)
1306 return !flag_ltrans
;
1309 /* Bring functionss local at LTO time with -fwhole-program. */
1312 whole_program_function_and_variable_visibility (void)
1314 function_and_variable_visibility (flag_whole_program
);
1316 ipa_discover_readonly_nonaddressable_vars ();
1322 const pass_data pass_data_ipa_whole_program_visibility
=
1324 IPA_PASS
, /* type */
1325 "whole-program", /* name */
1326 OPTGROUP_NONE
, /* optinfo_flags */
1327 true, /* has_gate */
1328 true, /* has_execute */
1329 TV_CGRAPHOPT
, /* tv_id */
1330 0, /* properties_required */
1331 0, /* properties_provided */
1332 0, /* properties_destroyed */
1333 0, /* todo_flags_start */
1334 ( TODO_remove_functions
| TODO_dump_symtab
), /* todo_flags_finish */
1337 class pass_ipa_whole_program_visibility
: public ipa_opt_pass_d
1340 pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
1341 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility
, ctxt
,
1342 NULL
, /* generate_summary */
1343 NULL
, /* write_summary */
1344 NULL
, /* read_summary */
1345 NULL
, /* write_optimization_summary */
1346 NULL
, /* read_optimization_summary */
1347 NULL
, /* stmt_fixup */
1348 0, /* function_transform_todo_flags_start */
1349 NULL
, /* function_transform */
1350 NULL
) /* variable_transform */
1353 /* opt_pass methods: */
1355 return gate_whole_program_function_and_variable_visibility ();
1357 unsigned int execute () {
1358 return whole_program_function_and_variable_visibility ();
1361 }; // class pass_ipa_whole_program_visibility
1366 make_pass_ipa_whole_program_visibility (gcc::context
*ctxt
)
1368 return new pass_ipa_whole_program_visibility (ctxt
);
1371 /* Generate and emit a static constructor or destructor. WHICH must
1372 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1373 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1374 initialization priority for this constructor or destructor.
1376 FINAL specify whether the externally visible name for collect2 should
1380 cgraph_build_static_cdtor_1 (char which
, tree body
, int priority
, bool final
)
1382 static int counter
= 0;
1384 tree decl
, name
, resdecl
;
1386 /* The priority is encoded in the constructor or destructor name.
1387 collect2 will sort the names and arrange that they are called at
1390 sprintf (which_buf
, "%c_%.5d_%d", which
, priority
, counter
++);
1392 /* Proudce sane name but one not recognizable by collect2, just for the
1393 case we fail to inline the function. */
1394 sprintf (which_buf
, "sub_%c_%.5d_%d", which
, priority
, counter
++);
1395 name
= get_file_function_name (which_buf
);
1397 decl
= build_decl (input_location
, FUNCTION_DECL
, name
,
1398 build_function_type_list (void_type_node
, NULL_TREE
));
1399 current_function_decl
= decl
;
1401 resdecl
= build_decl (input_location
,
1402 RESULT_DECL
, NULL_TREE
, void_type_node
);
1403 DECL_ARTIFICIAL (resdecl
) = 1;
1404 DECL_RESULT (decl
) = resdecl
;
1405 DECL_CONTEXT (resdecl
) = decl
;
1407 allocate_struct_function (decl
, false);
1409 TREE_STATIC (decl
) = 1;
1410 TREE_USED (decl
) = 1;
1411 DECL_ARTIFICIAL (decl
) = 1;
1412 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1413 DECL_SAVED_TREE (decl
) = body
;
1414 if (!targetm
.have_ctors_dtors
&& final
)
1416 TREE_PUBLIC (decl
) = 1;
1417 DECL_PRESERVE_P (decl
) = 1;
1419 DECL_UNINLINABLE (decl
) = 1;
1421 DECL_INITIAL (decl
) = make_node (BLOCK
);
1422 TREE_USED (DECL_INITIAL (decl
)) = 1;
1424 DECL_SOURCE_LOCATION (decl
) = input_location
;
1425 cfun
->function_end_locus
= input_location
;
1430 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1431 decl_init_priority_insert (decl
, priority
);
1434 DECL_STATIC_DESTRUCTOR (decl
) = 1;
1435 decl_fini_priority_insert (decl
, priority
);
1441 gimplify_function_tree (decl
);
1443 cgraph_add_new_function (decl
, false);
1446 current_function_decl
= NULL
;
1449 /* Generate and emit a static constructor or destructor. WHICH must
1450 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1451 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1452 initialization priority for this constructor or destructor. */
1455 cgraph_build_static_cdtor (char which
, tree body
, int priority
)
1457 cgraph_build_static_cdtor_1 (which
, body
, priority
, false);
1460 /* A vector of FUNCTION_DECLs declared as static constructors. */
1461 static vec
<tree
> static_ctors
;
1462 /* A vector of FUNCTION_DECLs declared as static destructors. */
1463 static vec
<tree
> static_dtors
;
1465 /* When target does not have ctors and dtors, we call all constructor
1466 and destructor by special initialization/destruction function
1467 recognized by collect2.
1469 When we are going to build this function, collect all constructors and
1470 destructors and turn them into normal functions. */
1473 record_cdtor_fn (struct cgraph_node
*node
)
1475 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
1476 static_ctors
.safe_push (node
->decl
);
1477 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
1478 static_dtors
.safe_push (node
->decl
);
1479 node
= cgraph_get_node (node
->decl
);
1480 DECL_DISREGARD_INLINE_LIMITS (node
->decl
) = 1;
1483 /* Define global constructors/destructor functions for the CDTORS, of
1484 which they are LEN. The CDTORS are sorted by initialization
1485 priority. If CTOR_P is true, these are constructors; otherwise,
1486 they are destructors. */
1489 build_cdtor (bool ctor_p
, vec
<tree
> cdtors
)
1492 size_t len
= cdtors
.length ();
1499 priority_type priority
;
1508 p
= ctor_p
? DECL_INIT_PRIORITY (fn
) : DECL_FINI_PRIORITY (fn
);
1511 else if (p
!= priority
)
1517 /* When there is only one cdtor and target supports them, do nothing. */
1519 && targetm
.have_ctors_dtors
)
1524 /* Find the next batch of constructors/destructors with the same
1525 initialization priority. */
1530 call
= build_call_expr (fn
, 0);
1532 DECL_STATIC_CONSTRUCTOR (fn
) = 0;
1534 DECL_STATIC_DESTRUCTOR (fn
) = 0;
1535 /* We do not want to optimize away pure/const calls here.
1536 When optimizing, these should be already removed, when not
1537 optimizing, we want user to be able to breakpoint in them. */
1538 TREE_SIDE_EFFECTS (call
) = 1;
1539 append_to_statement_list (call
, &body
);
1541 gcc_assert (body
!= NULL_TREE
);
1542 /* Generate a function to call all the function of like
1544 cgraph_build_static_cdtor_1 (ctor_p
? 'I' : 'D', body
, priority
, true);
1548 /* Comparison function for qsort. P1 and P2 are actually of type
1549 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1550 used to determine the sort order. */
1553 compare_ctor (const void *p1
, const void *p2
)
1560 f1
= *(const tree
*)p1
;
1561 f2
= *(const tree
*)p2
;
1562 priority1
= DECL_INIT_PRIORITY (f1
);
1563 priority2
= DECL_INIT_PRIORITY (f2
);
1565 if (priority1
< priority2
)
1567 else if (priority1
> priority2
)
1570 /* Ensure a stable sort. Constructors are executed in backwarding
1571 order to make LTO initialize braries first. */
1572 return DECL_UID (f2
) - DECL_UID (f1
);
1575 /* Comparison function for qsort. P1 and P2 are actually of type
1576 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1577 used to determine the sort order. */
1580 compare_dtor (const void *p1
, const void *p2
)
1587 f1
= *(const tree
*)p1
;
1588 f2
= *(const tree
*)p2
;
1589 priority1
= DECL_FINI_PRIORITY (f1
);
1590 priority2
= DECL_FINI_PRIORITY (f2
);
1592 if (priority1
< priority2
)
1594 else if (priority1
> priority2
)
1597 /* Ensure a stable sort. */
1598 return DECL_UID (f1
) - DECL_UID (f2
);
1601 /* Generate functions to call static constructors and destructors
1602 for targets that do not support .ctors/.dtors sections. These
1603 functions have magic names which are detected by collect2. */
1606 build_cdtor_fns (void)
1608 if (!static_ctors
.is_empty ())
1610 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1611 static_ctors
.qsort (compare_ctor
);
1612 build_cdtor (/*ctor_p=*/true, static_ctors
);
1615 if (!static_dtors
.is_empty ())
1617 gcc_assert (!targetm
.have_ctors_dtors
|| in_lto_p
);
1618 static_dtors
.qsort (compare_dtor
);
1619 build_cdtor (/*ctor_p=*/false, static_dtors
);
1623 /* Look for constructors and destructors and produce function calling them.
1624 This is needed for targets not supporting ctors or dtors, but we perform the
1625 transformation also at linktime to merge possibly numerous
1626 constructors/destructors into single function to improve code locality and
1630 ipa_cdtor_merge (void)
1632 struct cgraph_node
*node
;
1633 FOR_EACH_DEFINED_FUNCTION (node
)
1634 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
1635 || DECL_STATIC_DESTRUCTOR (node
->decl
))
1636 record_cdtor_fn (node
);
1638 static_ctors
.release ();
1639 static_dtors
.release ();
1643 /* Perform the pass when we have no ctors/dtors support
1644 or at LTO time to merge multiple constructors into single
1648 gate_ipa_cdtor_merge (void)
1650 return !targetm
.have_ctors_dtors
|| (optimize
&& in_lto_p
);
1655 const pass_data pass_data_ipa_cdtor_merge
=
1657 IPA_PASS
, /* type */
1659 OPTGROUP_NONE
, /* optinfo_flags */
1660 true, /* has_gate */
1661 true, /* has_execute */
1662 TV_CGRAPHOPT
, /* tv_id */
1663 0, /* properties_required */
1664 0, /* properties_provided */
1665 0, /* properties_destroyed */
1666 0, /* todo_flags_start */
1667 0, /* todo_flags_finish */
1670 class pass_ipa_cdtor_merge
: public ipa_opt_pass_d
1673 pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1674 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge
, ctxt
,
1675 NULL
, /* generate_summary */
1676 NULL
, /* write_summary */
1677 NULL
, /* read_summary */
1678 NULL
, /* write_optimization_summary */
1679 NULL
, /* read_optimization_summary */
1680 NULL
, /* stmt_fixup */
1681 0, /* function_transform_todo_flags_start */
1682 NULL
, /* function_transform */
1683 NULL
) /* variable_transform */
1686 /* opt_pass methods: */
1687 bool gate () { return gate_ipa_cdtor_merge (); }
1688 unsigned int execute () { return ipa_cdtor_merge (); }
1690 }; // class pass_ipa_cdtor_merge
1695 make_pass_ipa_cdtor_merge (gcc::context
*ctxt
)
1697 return new pass_ipa_cdtor_merge (ctxt
);