* lto-partition.c (add_symbol_to_partition_1,
[official-gcc.git] / gcc / ipa.c
blobb3b630643ba7df446ba63c9772f1c05e65601885
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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "pointer-set.h"
30 #include "gimple-expr.h"
31 #include "gimplify.h"
32 #include "flags.h"
33 #include "target.h"
34 #include "tree-iterator.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
37 #include "tree-inline.h"
38 #include "profile.h"
39 #include "params.h"
41 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
43 static bool
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)
49 && node->definition
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. */
58 static bool
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. */
65 if (n->thunk.thunk_p)
66 return false;
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. */
74 static bool
75 has_addr_references_p (struct cgraph_node *node,
76 void *data ATTRIBUTE_UNUSED)
78 int i;
79 struct ipa_ref *ref;
81 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
82 i, ref); i++)
83 if (ref->use == IPA_REF_ADDR)
84 return true;
85 return false;
88 /* Look for all functions inlined to NODE and update their inlined_to pointers
89 to INLINED_TO. */
91 static void
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
109 reachable. */
111 static void
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)
117 return;
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))
121 return;
122 node->aux = *first;
123 *first = node;
126 /* Process references. */
128 static void
129 process_references (struct ipa_ref_list *list,
130 symtab_node **first,
131 bool before_inlining_p,
132 struct pointer_set_t *reachable)
134 int i;
135 struct ipa_ref *ref;
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 /* We use variable constructors during late complation for
144 constant folding. Keep references alive so partitioning
145 knows about potential references. */
146 || (TREE_CODE (node->decl) == VAR_DECL
147 && flag_wpa
148 && ctor_for_folding (node->decl)
149 != error_mark_node))))
150 pointer_set_insert (reachable, node);
151 enqueue_node (node, first, reachable);
155 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
156 all its potential targets as reachable to permit later inlining if
157 devirtualization happens. After inlining still keep their declarations
158 around, so we can devirtualize to a direct call.
160 Also try to make trivial devirutalization when no or only one target is
161 possible. */
163 static void
164 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
165 struct cgraph_edge *edge,
166 symtab_node **first,
167 pointer_set_t *reachable, bool before_inlining_p)
169 unsigned int i;
170 void *cache_token;
171 bool final;
172 vec <cgraph_node *>targets
173 = possible_polymorphic_call_targets
174 (edge, &final, &cache_token);
176 if (!pointer_set_insert (reachable_call_targets,
177 cache_token))
179 for (i = 0; i < targets.length (); i++)
181 struct cgraph_node *n = targets[i];
183 /* Do not bother to mark virtual methods in anonymous namespace;
184 either we will find use of virtual table defining it, or it is
185 unused. */
186 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
187 && type_in_anonymous_namespace_p
188 (method_class_type (TREE_TYPE (n->decl))))
189 continue;
191 /* Prior inlining, keep alive bodies of possible targets for
192 devirtualization. */
193 if (n->definition
194 && before_inlining_p)
195 pointer_set_insert (reachable, n);
197 /* Even after inlining we want to keep the possible targets in the
198 boundary, so late passes can still produce direct call even if
199 the chance for inlining is lost. */
200 enqueue_node (n, first, reachable);
204 /* Very trivial devirtualization; when the type is
205 final or anonymous (so we know all its derivation)
206 and there is only one possible virtual call target,
207 make the edge direct. */
208 if (final)
210 if (targets.length () <= 1)
212 cgraph_node *target, *node = edge->caller;
213 if (targets.length () == 1)
214 target = targets[0];
215 else
216 target = cgraph_get_create_node
217 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
219 if (dump_file)
220 fprintf (dump_file,
221 "Devirtualizing call in %s/%i to %s/%i\n",
222 edge->caller->name (),
223 edge->caller->order,
224 target->name (), target->order);
225 edge = cgraph_make_edge_direct (edge, target);
226 if (!inline_summary_vec && edge->call_stmt)
227 cgraph_redirect_edge_call_stmt_to_callee (edge);
228 else
229 inline_update_overall_summary (node);
234 /* Perform reachability analysis and reclaim all unreachable nodes.
236 The algorithm is basically mark&sweep but with some extra refinements:
238 - reachable extern inline functions needs special handling; the bodies needs
239 to stay in memory until inlining in hope that they will be inlined.
240 After inlining we release their bodies and turn them into unanalyzed
241 nodes even when they are reachable.
243 BEFORE_INLINING_P specify whether we are before or after inlining.
245 - virtual functions are kept in callgraph even if they seem unreachable in
246 hope calls to them will be devirtualized.
248 Again we remove them after inlining. In late optimization some
249 devirtualization may happen, but it is not important since we won't inline
250 the call. In theory early opts and IPA should work out all important cases.
252 - virtual clones needs bodies of their origins for later materialization;
253 this means that we want to keep the body even if the origin is unreachable
254 otherwise. To avoid origin from sitting in the callgraph and being
255 walked by IPA passes, we turn them into unanalyzed nodes with body
256 defined.
258 We maintain set of function declaration where body needs to stay in
259 body_needed_for_clonning
261 Inline clones represent special case: their declaration match the
262 declaration of origin and cgraph_remove_node already knows how to
263 reshape callgraph and preserve body when offline copy of function or
264 inline clone is being removed.
266 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
267 variables with DECL_INITIAL set. We finalize these and keep reachable
268 ones around for constant folding purposes. After inlining we however
269 stop walking their references to let everything static referneced by them
270 to be removed when it is otherwise unreachable.
272 We maintain queue of both reachable symbols (i.e. defined symbols that needs
273 to stay) and symbols that are in boundary (i.e. external symbols referenced
274 by reachable symbols or origins of clones). The queue is represented
275 as linked list by AUX pointer terminated by 1.
277 At the end we keep all reachable symbols. For symbols in boundary we always
278 turn definition into a declaration, but we may keep function body around
279 based on body_needed_for_clonning
281 All symbols that enter the queue have AUX pointer non-zero and are in the
282 boundary. Pointer set REACHABLE is used to track reachable symbols.
284 Every symbol can be visited twice - once as part of boundary and once
285 as real reachable symbol. enqueue_node needs to decide whether the
286 node needs to be re-queued for second processing. For this purpose
287 we set AUX pointer of processed symbols in the boundary to constant 2. */
289 bool
290 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
292 symtab_node *first = (symtab_node *) (void *) 1;
293 struct cgraph_node *node, *next;
294 varpool_node *vnode, *vnext;
295 bool changed = false;
296 struct pointer_set_t *reachable = pointer_set_create ();
297 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
298 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
300 timevar_push (TV_IPA_UNREACHABLE);
301 #ifdef ENABLE_CHECKING
302 verify_symtab ();
303 #endif
304 if (optimize && flag_devirtualize)
305 build_type_inheritance_graph ();
306 if (file)
307 fprintf (file, "\nReclaiming functions:");
308 #ifdef ENABLE_CHECKING
309 FOR_EACH_FUNCTION (node)
310 gcc_assert (!node->aux);
311 FOR_EACH_VARIABLE (vnode)
312 gcc_assert (!vnode->aux);
313 #endif
314 /* Mark functions whose bodies are obviously needed.
315 This is mostly when they can be referenced externally. Inline clones
316 are special since their declarations are shared with master clone and thus
317 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
318 FOR_EACH_FUNCTION (node)
320 node->used_as_abstract_origin = false;
321 if (node->definition
322 && !node->global.inlined_to
323 && !node->in_other_partition
324 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
326 gcc_assert (!node->global.inlined_to);
327 pointer_set_insert (reachable, node);
328 enqueue_node (node, &first, reachable);
330 else
331 gcc_assert (!node->aux);
334 /* Mark variables that are obviously needed. */
335 FOR_EACH_DEFINED_VARIABLE (vnode)
336 if (!varpool_can_remove_if_no_refs (vnode)
337 && !vnode->in_other_partition)
339 pointer_set_insert (reachable, vnode);
340 enqueue_node (vnode, &first, reachable);
343 /* Perform reachability analysis. */
344 while (first != (symtab_node *) (void *) 1)
346 bool in_boundary_p = !pointer_set_contains (reachable, first);
347 symtab_node *node = first;
349 first = (symtab_node *)first->aux;
351 /* If we are processing symbol in boundary, mark its AUX pointer for
352 possible later re-processing in enqueue_node. */
353 if (in_boundary_p)
354 node->aux = (void *)2;
355 else
357 if (DECL_ABSTRACT_ORIGIN (node->decl))
359 struct cgraph_node *origin_node
360 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
361 origin_node->used_as_abstract_origin = true;
362 enqueue_node (origin_node, &first, reachable);
364 /* If any symbol in a comdat group is reachable, force
365 all externally visible symbols in the same comdat
366 group to be reachable as well. Comdat-local symbols
367 can be discarded if all uses were inlined. */
368 if (node->same_comdat_group)
370 symtab_node *next;
371 for (next = node->same_comdat_group;
372 next != node;
373 next = next->same_comdat_group)
374 if (!symtab_comdat_local_p (next)
375 && !pointer_set_insert (reachable, next))
376 enqueue_node (next, &first, reachable);
378 /* Mark references as reachable. */
379 process_references (&node->ref_list, &first,
380 before_inlining_p, reachable);
383 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
385 /* Mark the callees reachable unless they are direct calls to extern
386 inline functions we decided to not inline. */
387 if (!in_boundary_p)
389 struct cgraph_edge *e;
390 /* Keep alive possible targets for devirtualization. */
391 if (optimize && flag_devirtualize)
393 struct cgraph_edge *next;
394 for (e = cnode->indirect_calls; e; e = next)
396 next = e->next_callee;
397 if (e->indirect_info->polymorphic)
398 walk_polymorphic_call_targets (reachable_call_targets,
399 e, &first, reachable,
400 before_inlining_p);
403 for (e = cnode->callees; e; e = e->next_callee)
405 if (e->callee->definition
406 && !e->callee->in_other_partition
407 && (!e->inline_failed
408 || !DECL_EXTERNAL (e->callee->decl)
409 || e->callee->alias
410 || before_inlining_p))
411 pointer_set_insert (reachable, e->callee);
412 enqueue_node (e->callee, &first, reachable);
415 /* When inline clone exists, mark body to be preserved so when removing
416 offline copy of the function we don't kill it. */
417 if (cnode->global.inlined_to)
418 pointer_set_insert (body_needed_for_clonning, cnode->decl);
420 /* For non-inline clones, force their origins to the boundary and ensure
421 that body is not removed. */
422 while (cnode->clone_of)
424 bool noninline = cnode->clone_of->decl != cnode->decl;
425 cnode = cnode->clone_of;
426 if (noninline)
428 pointer_set_insert (body_needed_for_clonning, cnode->decl);
429 enqueue_node (cnode, &first, reachable);
434 /* If any reachable function has simd clones, mark them as
435 reachable as well. */
436 if (cnode->simd_clones)
438 cgraph_node *next;
439 for (next = cnode->simd_clones;
440 next;
441 next = next->simdclone->next_clone)
442 if (in_boundary_p
443 || !pointer_set_insert (reachable, next))
444 enqueue_node (next, &first, reachable);
447 /* When we see constructor of external variable, keep referred nodes in the
448 boundary. This will also hold initializers of the external vars NODE
449 refers to. */
450 varpool_node *vnode = dyn_cast <varpool_node> (node);
451 if (vnode
452 && DECL_EXTERNAL (node->decl)
453 && !vnode->alias
454 && in_boundary_p)
456 struct ipa_ref *ref;
457 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
458 enqueue_node (ref->referred, &first, reachable);
462 /* Remove unreachable functions. */
463 for (node = cgraph_first_function (); node; node = next)
465 next = cgraph_next_function (node);
467 /* If node is not needed at all, remove it. */
468 if (!node->aux)
470 if (file)
471 fprintf (file, " %s", node->name ());
472 cgraph_remove_node (node);
473 changed = true;
475 /* If node is unreachable, remove its body. */
476 else if (!pointer_set_contains (reachable, node))
478 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
479 cgraph_release_function_body (node);
480 else if (!node->clone_of)
481 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
482 if (node->definition)
484 if (file)
485 fprintf (file, " %s", node->name ());
486 node->analyzed = false;
487 node->definition = false;
488 node->cpp_implicit_alias = false;
489 node->alias = false;
490 node->weakref = false;
491 if (!node->in_other_partition)
492 node->local.local = false;
493 cgraph_node_remove_callees (node);
494 ipa_remove_all_references (&node->ref_list);
495 changed = true;
498 else
499 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
500 || in_lto_p || DECL_RESULT (node->decl));
503 /* Inline clones might be kept around so their materializing allows further
504 cloning. If the function the clone is inlined into is removed, we need
505 to turn it into normal cone. */
506 FOR_EACH_FUNCTION (node)
508 if (node->global.inlined_to
509 && !node->callers)
511 gcc_assert (node->clones);
512 node->global.inlined_to = NULL;
513 update_inlined_to_pointer (node, node);
515 node->aux = NULL;
518 /* Remove unreachable variables. */
519 if (file)
520 fprintf (file, "\nReclaiming variables:");
521 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
523 vnext = varpool_next_variable (vnode);
524 if (!vnode->aux
525 /* For can_refer_decl_in_current_unit_p we want to track for
526 all external variables if they are defined in other partition
527 or not. */
528 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
530 if (file)
531 fprintf (file, " %s", vnode->name ());
532 varpool_remove_node (vnode);
533 changed = true;
535 else if (!pointer_set_contains (reachable, vnode))
537 tree init;
538 if (vnode->definition)
540 if (file)
541 fprintf (file, " %s", vnode->name ());
542 changed = true;
544 vnode->definition = false;
545 vnode->analyzed = false;
546 vnode->aux = NULL;
548 /* Keep body if it may be useful for constant folding. */
549 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
550 varpool_remove_initializer (vnode);
551 else
552 DECL_INITIAL (vnode->decl) = init;
553 ipa_remove_all_references (&vnode->ref_list);
555 else
556 vnode->aux = NULL;
559 pointer_set_destroy (reachable);
560 pointer_set_destroy (body_needed_for_clonning);
561 pointer_set_destroy (reachable_call_targets);
563 /* Now update address_taken flags and try to promote functions to be local. */
564 if (file)
565 fprintf (file, "\nClearing address taken flags:");
566 FOR_EACH_DEFINED_FUNCTION (node)
567 if (node->address_taken
568 && !node->used_from_other_partition)
570 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
572 if (file)
573 fprintf (file, " %s", node->name ());
574 node->address_taken = false;
575 changed = true;
576 if (cgraph_local_node_p (node))
578 node->local.local = true;
579 if (file)
580 fprintf (file, " (local)");
584 if (file)
585 fprintf (file, "\n");
587 #ifdef ENABLE_CHECKING
588 verify_symtab ();
589 #endif
591 /* If we removed something, perhaps profile could be improved. */
592 if (changed && optimize && inline_edge_summary_vec.exists ())
593 FOR_EACH_DEFINED_FUNCTION (node)
594 ipa_propagate_frequency (node);
596 timevar_pop (TV_IPA_UNREACHABLE);
597 return changed;
600 /* Discover variables that have no longer address taken or that are read only
601 and update their flags.
603 FIXME: This can not be done in between gimplify and omp_expand since
604 readonly flag plays role on what is shared and what is not. Currently we do
605 this transformation as part of whole program visibility and re-do at
606 ipa-reference pass (to take into account clonning), but it would
607 make sense to do it before early optimizations. */
609 void
610 ipa_discover_readonly_nonaddressable_vars (void)
612 varpool_node *vnode;
613 if (dump_file)
614 fprintf (dump_file, "Clearing variable flags:");
615 FOR_EACH_VARIABLE (vnode)
616 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
617 && (TREE_ADDRESSABLE (vnode->decl)
618 || !TREE_READONLY (vnode->decl)))
620 bool written = false;
621 bool address_taken = false;
622 int i;
623 struct ipa_ref *ref;
624 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
625 i, ref)
626 && (!written || !address_taken); i++)
627 switch (ref->use)
629 case IPA_REF_ADDR:
630 address_taken = true;
631 break;
632 case IPA_REF_LOAD:
633 break;
634 case IPA_REF_STORE:
635 written = true;
636 break;
638 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
640 if (dump_file)
641 fprintf (dump_file, " %s (addressable)", vnode->name ());
642 TREE_ADDRESSABLE (vnode->decl) = 0;
644 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
645 /* Making variable in explicit section readonly can cause section
646 type conflict.
647 See e.g. gcc.c-torture/compile/pr23237.c */
648 && DECL_SECTION_NAME (vnode->decl) == NULL)
650 if (dump_file)
651 fprintf (dump_file, " %s (read-only)", vnode->name ());
652 TREE_READONLY (vnode->decl) = 1;
655 if (dump_file)
656 fprintf (dump_file, "\n");
659 /* Return true when there is a reference to node and it is not vtable. */
660 static bool
661 address_taken_from_non_vtable_p (symtab_node *node)
663 int i;
664 struct ipa_ref *ref;
665 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
666 i, ref); i++)
667 if (ref->use == IPA_REF_ADDR)
669 varpool_node *node;
670 if (is_a <cgraph_node> (ref->referring))
671 return true;
672 node = ipa_ref_referring_varpool_node (ref);
673 if (!DECL_VIRTUAL_P (node->decl))
674 return true;
676 return false;
679 /* A helper for comdat_can_be_unshared_p. */
681 static bool
682 comdat_can_be_unshared_p_1 (symtab_node *node)
684 /* When address is taken, we don't know if equality comparison won't
685 break eventually. Exception are virutal functions, C++
686 constructors/destructors and vtables, where this is not possible by
687 language standard. */
688 if (!DECL_VIRTUAL_P (node->decl)
689 && (TREE_CODE (node->decl) != FUNCTION_DECL
690 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
691 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
692 && address_taken_from_non_vtable_p (node))
693 return false;
695 /* If the symbol is used in some weird way, better to not touch it. */
696 if (node->force_output)
697 return false;
699 /* Explicit instantiations needs to be output when possibly
700 used externally. */
701 if (node->forced_by_abi
702 && TREE_PUBLIC (node->decl)
703 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
704 && !flag_whole_program))
705 return false;
707 /* Non-readonly and volatile variables can not be duplicated. */
708 if (is_a <varpool_node> (node)
709 && (!TREE_READONLY (node->decl)
710 || TREE_THIS_VOLATILE (node->decl)))
711 return false;
712 return true;
715 /* COMDAT functions must be shared only if they have address taken,
716 otherwise we can produce our own private implementation with
717 -fwhole-program.
718 Return true when turning COMDAT functoin static can not lead to wrong
719 code when the resulting object links with a library defining same COMDAT.
721 Virtual functions do have their addresses taken from the vtables,
722 but in C++ there is no way to compare their addresses for equality. */
724 static bool
725 comdat_can_be_unshared_p (symtab_node *node)
727 if (!comdat_can_be_unshared_p_1 (node))
728 return false;
729 if (node->same_comdat_group)
731 symtab_node *next;
733 /* If more than one function is in the same COMDAT group, it must
734 be shared even if just one function in the comdat group has
735 address taken. */
736 for (next = node->same_comdat_group;
737 next != node; next = next->same_comdat_group)
738 if (!comdat_can_be_unshared_p_1 (next))
739 return false;
741 return true;
744 /* Return true when function NODE should be considered externally visible. */
746 static bool
747 cgraph_externally_visible_p (struct cgraph_node *node,
748 bool whole_program)
750 if (!node->definition)
751 return false;
752 if (!TREE_PUBLIC (node->decl)
753 || DECL_EXTERNAL (node->decl))
754 return false;
756 /* Do not try to localize built-in functions yet. One of problems is that we
757 end up mangling their asm for WHOPR that makes it impossible to call them
758 using the implicit built-in declarations anymore. Similarly this enables
759 us to remove them as unreachable before actual calls may appear during
760 expansion or folding. */
761 if (DECL_BUILT_IN (node->decl))
762 return true;
764 /* If linker counts on us, we must preserve the function. */
765 if (symtab_used_from_object_file_p (node))
766 return true;
767 if (DECL_PRESERVE_P (node->decl))
768 return true;
769 if (lookup_attribute ("externally_visible",
770 DECL_ATTRIBUTES (node->decl)))
771 return true;
772 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
773 && lookup_attribute ("dllexport",
774 DECL_ATTRIBUTES (node->decl)))
775 return true;
776 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
777 return false;
778 /* When doing LTO or whole program, we can bring COMDAT functoins static.
779 This improves code quality and we know we will duplicate them at most twice
780 (in the case that we are not using plugin and link with object file
781 implementing same COMDAT) */
782 if ((in_lto_p || whole_program)
783 && DECL_COMDAT (node->decl)
784 && comdat_can_be_unshared_p (node))
785 return false;
787 /* When doing link time optimizations, hidden symbols become local. */
788 if (in_lto_p
789 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
790 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
791 /* Be sure that node is defined in IR file, not in other object
792 file. In that case we don't set used_from_other_object_file. */
793 && node->definition)
795 else if (!whole_program)
796 return true;
798 if (MAIN_NAME_P (DECL_NAME (node->decl)))
799 return true;
801 return false;
804 /* Return true when variable VNODE should be considered externally visible. */
806 bool
807 varpool_externally_visible_p (varpool_node *vnode)
809 if (DECL_EXTERNAL (vnode->decl))
810 return true;
812 if (!TREE_PUBLIC (vnode->decl))
813 return false;
815 /* If linker counts on us, we must preserve the function. */
816 if (symtab_used_from_object_file_p (vnode))
817 return true;
819 if (DECL_HARD_REGISTER (vnode->decl))
820 return true;
821 if (DECL_PRESERVE_P (vnode->decl))
822 return true;
823 if (lookup_attribute ("externally_visible",
824 DECL_ATTRIBUTES (vnode->decl)))
825 return true;
826 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
827 && lookup_attribute ("dllexport",
828 DECL_ATTRIBUTES (vnode->decl)))
829 return true;
831 /* See if we have linker information about symbol not being used or
832 if we need to make guess based on the declaration.
834 Even if the linker clams the symbol is unused, never bring internal
835 symbols that are declared by user as used or externally visible.
836 This is needed for i.e. references from asm statements. */
837 if (symtab_used_from_object_file_p (vnode))
838 return true;
839 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
840 return false;
842 /* As a special case, the COMDAT virtual tables can be unshared.
843 In LTO mode turn vtables into static variables. The variable is readonly,
844 so this does not enable more optimization, but referring static var
845 is faster for dynamic linking. Also this match logic hidding vtables
846 from LTO symbol tables. */
847 if ((in_lto_p || flag_whole_program)
848 && DECL_COMDAT (vnode->decl)
849 && comdat_can_be_unshared_p (vnode))
850 return false;
852 /* When doing link time optimizations, hidden symbols become local. */
853 if (in_lto_p
854 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
855 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
856 /* Be sure that node is defined in IR file, not in other object
857 file. In that case we don't set used_from_other_object_file. */
858 && vnode->definition)
860 else if (!flag_whole_program)
861 return true;
863 /* Do not attempt to privatize COMDATS by default.
864 This would break linking with C++ libraries sharing
865 inline definitions.
867 FIXME: We can do so for readonly vars with no address taken and
868 possibly also for vtables since no direct pointer comparsion is done.
869 It might be interesting to do so to reduce linking overhead. */
870 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
871 return true;
872 return false;
875 /* Return true if reference to NODE can be replaced by a local alias.
876 Local aliases save dynamic linking overhead and enable more optimizations.
879 bool
880 can_replace_by_local_alias (symtab_node *node)
882 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
883 && !symtab_can_be_discarded (node));
886 /* Mark visibility of all functions.
888 A local function is one whose calls can occur only in the current
889 compilation unit and all its calls are explicit, so we can change
890 its calling convention. We simply mark all static functions whose
891 address is not taken as local.
893 We also change the TREE_PUBLIC flag of all declarations that are public
894 in language point of view but we want to overwrite this default
895 via visibilities for the backend point of view. */
897 static unsigned int
898 function_and_variable_visibility (bool whole_program)
900 struct cgraph_node *node;
901 varpool_node *vnode;
903 /* All aliases should be procssed at this point. */
904 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
906 FOR_EACH_FUNCTION (node)
908 int flags = flags_from_decl_or_type (node->decl);
910 /* Optimize away PURE and CONST constructors and destructors. */
911 if (optimize
912 && (flags & (ECF_CONST | ECF_PURE))
913 && !(flags & ECF_LOOPING_CONST_OR_PURE))
915 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
916 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
919 /* Frontends and alias code marks nodes as needed before parsing is finished.
920 We may end up marking as node external nodes where this flag is meaningless
921 strip it. */
922 if (DECL_EXTERNAL (node->decl) || !node->definition)
924 node->force_output = 0;
925 node->forced_by_abi = 0;
928 /* C++ FE on lack of COMDAT support create local COMDAT functions
929 (that ought to be shared but can not due to object format
930 limitations). It is necessary to keep the flag to make rest of C++ FE
931 happy. Clear the flag here to avoid confusion in middle-end. */
932 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
933 DECL_COMDAT (node->decl) = 0;
935 /* For external decls stop tracking same_comdat_group. It doesn't matter
936 what comdat group they are in when they won't be emitted in this TU. */
937 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
939 #ifdef ENABLE_CHECKING
940 symtab_node *n;
942 for (n = node->same_comdat_group;
943 n != node;
944 n = n->same_comdat_group)
945 /* If at least one of same comdat group functions is external,
946 all of them have to be, otherwise it is a front-end bug. */
947 gcc_assert (DECL_EXTERNAL (n->decl));
948 #endif
949 symtab_dissolve_same_comdat_group_list (node);
951 gcc_assert ((!DECL_WEAK (node->decl)
952 && !DECL_COMDAT (node->decl))
953 || TREE_PUBLIC (node->decl)
954 || node->weakref
955 || DECL_EXTERNAL (node->decl));
956 if (cgraph_externally_visible_p (node, whole_program))
958 gcc_assert (!node->global.inlined_to);
959 node->externally_visible = true;
961 else
963 node->externally_visible = false;
964 node->forced_by_abi = false;
966 if (!node->externally_visible
967 && node->definition && !node->weakref
968 && !DECL_EXTERNAL (node->decl))
970 gcc_assert (whole_program || in_lto_p
971 || !TREE_PUBLIC (node->decl));
972 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
973 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
974 && TREE_PUBLIC (node->decl));
975 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
976 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
977 /* cgraph_externally_visible_p has already checked all other nodes
978 in the group and they will all be made local. We need to
979 dissolve the group at once so that the predicate does not
980 segfault though. */
981 symtab_dissolve_same_comdat_group_list (node);
982 symtab_make_decl_local (node->decl);
985 if (node->thunk.thunk_p
986 && TREE_PUBLIC (node->decl))
988 struct cgraph_node *decl_node = node;
990 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
992 /* Thunks have the same visibility as function they are attached to.
993 Make sure the C++ front end set this up properly. */
994 if (DECL_ONE_ONLY (decl_node->decl))
996 gcc_checking_assert (DECL_COMDAT (node->decl)
997 == DECL_COMDAT (decl_node->decl));
998 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
999 == DECL_COMDAT_GROUP (decl_node->decl));
1000 gcc_checking_assert (node->same_comdat_group);
1002 if (DECL_EXTERNAL (decl_node->decl))
1003 DECL_EXTERNAL (node->decl) = 1;
1006 /* If whole comdat group is used only within LTO code, we can dissolve it,
1007 we handle the unification ourselves.
1008 We keep COMDAT and weak so visibility out of DSO does not change.
1009 Later we may bring the symbols static if they are not exported. */
1010 if (DECL_ONE_ONLY (node->decl)
1011 && (node->resolution == LDPR_PREVAILING_DEF_IRONLY
1012 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP))
1014 symtab_node *next = node;
1016 if (node->same_comdat_group)
1017 for (next = node->same_comdat_group;
1018 next != node;
1019 next = next->same_comdat_group)
1020 if (next->externally_visible
1021 && (next->resolution != LDPR_PREVAILING_DEF_IRONLY
1022 && next->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP))
1023 break;
1024 if (node == next)
1026 if (node->same_comdat_group)
1027 for (next = node->same_comdat_group;
1028 next != node;
1029 next = next->same_comdat_group)
1031 DECL_COMDAT_GROUP (next->decl) = NULL;
1032 DECL_WEAK (next->decl) = false;
1034 DECL_COMDAT_GROUP (node->decl) = NULL;
1035 symtab_dissolve_same_comdat_group_list (node);
1039 FOR_EACH_DEFINED_FUNCTION (node)
1041 node->local.local |= cgraph_local_node_p (node);
1043 /* If we know that function can not be overwritten by a different semantics
1044 and moreover its section can not be discarded, replace all direct calls
1045 by calls to an nonoverwritable alias. This make dynamic linking
1046 cheaper and enable more optimization.
1048 TODO: We can also update virtual tables. */
1049 if (node->callers && can_replace_by_local_alias (node))
1051 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1053 if (alias && alias != node)
1055 while (node->callers)
1057 struct cgraph_edge *e = node->callers;
1059 cgraph_redirect_edge_callee (e, alias);
1060 if (gimple_has_body_p (e->caller->decl))
1062 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1063 cgraph_redirect_edge_call_stmt_to_callee (e);
1064 pop_cfun ();
1070 FOR_EACH_VARIABLE (vnode)
1072 /* weak flag makes no sense on local variables. */
1073 gcc_assert (!DECL_WEAK (vnode->decl)
1074 || vnode->weakref
1075 || TREE_PUBLIC (vnode->decl)
1076 || DECL_EXTERNAL (vnode->decl));
1077 /* In several cases declarations can not be common:
1079 - when declaration has initializer
1080 - when it is in weak
1081 - when it has specific section
1082 - when it resides in non-generic address space.
1083 - if declaration is local, it will get into .local common section
1084 so common flag is not needed. Frontends still produce these in
1085 certain cases, such as for:
1087 static int a __attribute__ ((common))
1089 Canonicalize things here and clear the redundant flag. */
1090 if (DECL_COMMON (vnode->decl)
1091 && (!(TREE_PUBLIC (vnode->decl)
1092 || DECL_EXTERNAL (vnode->decl))
1093 || (DECL_INITIAL (vnode->decl)
1094 && DECL_INITIAL (vnode->decl) != error_mark_node)
1095 || DECL_WEAK (vnode->decl)
1096 || DECL_SECTION_NAME (vnode->decl) != NULL
1097 || ! (ADDR_SPACE_GENERIC_P
1098 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1099 DECL_COMMON (vnode->decl) = 0;
1101 FOR_EACH_DEFINED_VARIABLE (vnode)
1103 if (!vnode->definition)
1104 continue;
1105 if (varpool_externally_visible_p (vnode))
1106 vnode->externally_visible = true;
1107 else
1109 vnode->externally_visible = false;
1110 vnode->forced_by_abi = false;
1112 if (!vnode->externally_visible
1113 && !vnode->weakref)
1115 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1116 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1117 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1118 && TREE_PUBLIC (vnode->decl));
1119 symtab_make_decl_local (vnode->decl);
1120 if (vnode->same_comdat_group)
1121 symtab_dissolve_same_comdat_group_list (vnode);
1122 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1126 if (dump_file)
1128 fprintf (dump_file, "\nMarking local functions:");
1129 FOR_EACH_DEFINED_FUNCTION (node)
1130 if (node->local.local)
1131 fprintf (dump_file, " %s", node->name ());
1132 fprintf (dump_file, "\n\n");
1133 fprintf (dump_file, "\nMarking externally visible functions:");
1134 FOR_EACH_DEFINED_FUNCTION (node)
1135 if (node->externally_visible)
1136 fprintf (dump_file, " %s", node->name ());
1137 fprintf (dump_file, "\n\n");
1138 fprintf (dump_file, "\nMarking externally visible variables:");
1139 FOR_EACH_DEFINED_VARIABLE (vnode)
1140 if (vnode->externally_visible)
1141 fprintf (dump_file, " %s", vnode->name ());
1142 fprintf (dump_file, "\n\n");
1144 cgraph_function_flags_ready = true;
1145 return 0;
1148 /* Local function pass handling visibilities. This happens before LTO streaming
1149 so in particular -fwhole-program should be ignored at this level. */
1151 static unsigned int
1152 local_function_and_variable_visibility (void)
1154 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1157 namespace {
1159 const pass_data pass_data_ipa_function_and_variable_visibility =
1161 SIMPLE_IPA_PASS, /* type */
1162 "visibility", /* name */
1163 OPTGROUP_NONE, /* optinfo_flags */
1164 false, /* has_gate */
1165 true, /* has_execute */
1166 TV_CGRAPHOPT, /* tv_id */
1167 0, /* properties_required */
1168 0, /* properties_provided */
1169 0, /* properties_destroyed */
1170 0, /* todo_flags_start */
1171 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1174 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1176 public:
1177 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1178 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1179 ctxt)
1182 /* opt_pass methods: */
1183 unsigned int execute () {
1184 return local_function_and_variable_visibility ();
1187 }; // class pass_ipa_function_and_variable_visibility
1189 } // anon namespace
1191 simple_ipa_opt_pass *
1192 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1194 return new pass_ipa_function_and_variable_visibility (ctxt);
1197 /* Free inline summary. */
1199 static unsigned
1200 free_inline_summary (void)
1202 inline_free_summary ();
1203 return 0;
1206 namespace {
1208 const pass_data pass_data_ipa_free_inline_summary =
1210 SIMPLE_IPA_PASS, /* type */
1211 "*free_inline_summary", /* name */
1212 OPTGROUP_NONE, /* optinfo_flags */
1213 false, /* has_gate */
1214 true, /* has_execute */
1215 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1216 0, /* properties_required */
1217 0, /* properties_provided */
1218 0, /* properties_destroyed */
1219 0, /* todo_flags_start */
1220 0, /* todo_flags_finish */
1223 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1225 public:
1226 pass_ipa_free_inline_summary (gcc::context *ctxt)
1227 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1230 /* opt_pass methods: */
1231 unsigned int execute () { return free_inline_summary (); }
1233 }; // class pass_ipa_free_inline_summary
1235 } // anon namespace
1237 simple_ipa_opt_pass *
1238 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1240 return new pass_ipa_free_inline_summary (ctxt);
1243 /* Do not re-run on ltrans stage. */
1245 static bool
1246 gate_whole_program_function_and_variable_visibility (void)
1248 return !flag_ltrans;
1251 /* Bring functionss local at LTO time with -fwhole-program. */
1253 static unsigned int
1254 whole_program_function_and_variable_visibility (void)
1256 function_and_variable_visibility (flag_whole_program);
1257 if (optimize)
1258 ipa_discover_readonly_nonaddressable_vars ();
1259 return 0;
1262 namespace {
1264 const pass_data pass_data_ipa_whole_program_visibility =
1266 IPA_PASS, /* type */
1267 "whole-program", /* name */
1268 OPTGROUP_NONE, /* optinfo_flags */
1269 true, /* has_gate */
1270 true, /* has_execute */
1271 TV_CGRAPHOPT, /* tv_id */
1272 0, /* properties_required */
1273 0, /* properties_provided */
1274 0, /* properties_destroyed */
1275 0, /* todo_flags_start */
1276 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1279 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1281 public:
1282 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1283 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1284 NULL, /* generate_summary */
1285 NULL, /* write_summary */
1286 NULL, /* read_summary */
1287 NULL, /* write_optimization_summary */
1288 NULL, /* read_optimization_summary */
1289 NULL, /* stmt_fixup */
1290 0, /* function_transform_todo_flags_start */
1291 NULL, /* function_transform */
1292 NULL) /* variable_transform */
1295 /* opt_pass methods: */
1296 bool gate () {
1297 return gate_whole_program_function_and_variable_visibility ();
1299 unsigned int execute () {
1300 return whole_program_function_and_variable_visibility ();
1303 }; // class pass_ipa_whole_program_visibility
1305 } // anon namespace
1307 ipa_opt_pass_d *
1308 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1310 return new pass_ipa_whole_program_visibility (ctxt);
1313 /* Generate and emit a static constructor or destructor. WHICH must
1314 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1315 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1316 initialization priority for this constructor or destructor.
1318 FINAL specify whether the externally visible name for collect2 should
1319 be produced. */
1321 static void
1322 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1324 static int counter = 0;
1325 char which_buf[16];
1326 tree decl, name, resdecl;
1328 /* The priority is encoded in the constructor or destructor name.
1329 collect2 will sort the names and arrange that they are called at
1330 program startup. */
1331 if (final)
1332 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1333 else
1334 /* Proudce sane name but one not recognizable by collect2, just for the
1335 case we fail to inline the function. */
1336 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1337 name = get_file_function_name (which_buf);
1339 decl = build_decl (input_location, FUNCTION_DECL, name,
1340 build_function_type_list (void_type_node, NULL_TREE));
1341 current_function_decl = decl;
1343 resdecl = build_decl (input_location,
1344 RESULT_DECL, NULL_TREE, void_type_node);
1345 DECL_ARTIFICIAL (resdecl) = 1;
1346 DECL_RESULT (decl) = resdecl;
1347 DECL_CONTEXT (resdecl) = decl;
1349 allocate_struct_function (decl, false);
1351 TREE_STATIC (decl) = 1;
1352 TREE_USED (decl) = 1;
1353 DECL_ARTIFICIAL (decl) = 1;
1354 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1355 DECL_SAVED_TREE (decl) = body;
1356 if (!targetm.have_ctors_dtors && final)
1358 TREE_PUBLIC (decl) = 1;
1359 DECL_PRESERVE_P (decl) = 1;
1361 DECL_UNINLINABLE (decl) = 1;
1363 DECL_INITIAL (decl) = make_node (BLOCK);
1364 TREE_USED (DECL_INITIAL (decl)) = 1;
1366 DECL_SOURCE_LOCATION (decl) = input_location;
1367 cfun->function_end_locus = input_location;
1369 switch (which)
1371 case 'I':
1372 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1373 decl_init_priority_insert (decl, priority);
1374 break;
1375 case 'D':
1376 DECL_STATIC_DESTRUCTOR (decl) = 1;
1377 decl_fini_priority_insert (decl, priority);
1378 break;
1379 default:
1380 gcc_unreachable ();
1383 gimplify_function_tree (decl);
1385 cgraph_add_new_function (decl, false);
1387 set_cfun (NULL);
1388 current_function_decl = NULL;
1391 /* Generate and emit a static constructor or destructor. WHICH must
1392 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1393 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1394 initialization priority for this constructor or destructor. */
1396 void
1397 cgraph_build_static_cdtor (char which, tree body, int priority)
1399 cgraph_build_static_cdtor_1 (which, body, priority, false);
1402 /* A vector of FUNCTION_DECLs declared as static constructors. */
1403 static vec<tree> static_ctors;
1404 /* A vector of FUNCTION_DECLs declared as static destructors. */
1405 static vec<tree> static_dtors;
1407 /* When target does not have ctors and dtors, we call all constructor
1408 and destructor by special initialization/destruction function
1409 recognized by collect2.
1411 When we are going to build this function, collect all constructors and
1412 destructors and turn them into normal functions. */
1414 static void
1415 record_cdtor_fn (struct cgraph_node *node)
1417 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1418 static_ctors.safe_push (node->decl);
1419 if (DECL_STATIC_DESTRUCTOR (node->decl))
1420 static_dtors.safe_push (node->decl);
1421 node = cgraph_get_node (node->decl);
1422 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1425 /* Define global constructors/destructor functions for the CDTORS, of
1426 which they are LEN. The CDTORS are sorted by initialization
1427 priority. If CTOR_P is true, these are constructors; otherwise,
1428 they are destructors. */
1430 static void
1431 build_cdtor (bool ctor_p, vec<tree> cdtors)
1433 size_t i,j;
1434 size_t len = cdtors.length ();
1436 i = 0;
1437 while (i < len)
1439 tree body;
1440 tree fn;
1441 priority_type priority;
1443 priority = 0;
1444 body = NULL_TREE;
1445 j = i;
1448 priority_type p;
1449 fn = cdtors[j];
1450 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1451 if (j == i)
1452 priority = p;
1453 else if (p != priority)
1454 break;
1455 j++;
1457 while (j < len);
1459 /* When there is only one cdtor and target supports them, do nothing. */
1460 if (j == i + 1
1461 && targetm.have_ctors_dtors)
1463 i++;
1464 continue;
1466 /* Find the next batch of constructors/destructors with the same
1467 initialization priority. */
1468 for (;i < j; i++)
1470 tree call;
1471 fn = cdtors[i];
1472 call = build_call_expr (fn, 0);
1473 if (ctor_p)
1474 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1475 else
1476 DECL_STATIC_DESTRUCTOR (fn) = 0;
1477 /* We do not want to optimize away pure/const calls here.
1478 When optimizing, these should be already removed, when not
1479 optimizing, we want user to be able to breakpoint in them. */
1480 TREE_SIDE_EFFECTS (call) = 1;
1481 append_to_statement_list (call, &body);
1483 gcc_assert (body != NULL_TREE);
1484 /* Generate a function to call all the function of like
1485 priority. */
1486 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1490 /* Comparison function for qsort. P1 and P2 are actually of type
1491 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1492 used to determine the sort order. */
1494 static int
1495 compare_ctor (const void *p1, const void *p2)
1497 tree f1;
1498 tree f2;
1499 int priority1;
1500 int priority2;
1502 f1 = *(const tree *)p1;
1503 f2 = *(const tree *)p2;
1504 priority1 = DECL_INIT_PRIORITY (f1);
1505 priority2 = DECL_INIT_PRIORITY (f2);
1507 if (priority1 < priority2)
1508 return -1;
1509 else if (priority1 > priority2)
1510 return 1;
1511 else
1512 /* Ensure a stable sort. Constructors are executed in backwarding
1513 order to make LTO initialize braries first. */
1514 return DECL_UID (f2) - DECL_UID (f1);
1517 /* Comparison function for qsort. P1 and P2 are actually of type
1518 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1519 used to determine the sort order. */
1521 static int
1522 compare_dtor (const void *p1, const void *p2)
1524 tree f1;
1525 tree f2;
1526 int priority1;
1527 int priority2;
1529 f1 = *(const tree *)p1;
1530 f2 = *(const tree *)p2;
1531 priority1 = DECL_FINI_PRIORITY (f1);
1532 priority2 = DECL_FINI_PRIORITY (f2);
1534 if (priority1 < priority2)
1535 return -1;
1536 else if (priority1 > priority2)
1537 return 1;
1538 else
1539 /* Ensure a stable sort. */
1540 return DECL_UID (f1) - DECL_UID (f2);
1543 /* Generate functions to call static constructors and destructors
1544 for targets that do not support .ctors/.dtors sections. These
1545 functions have magic names which are detected by collect2. */
1547 static void
1548 build_cdtor_fns (void)
1550 if (!static_ctors.is_empty ())
1552 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1553 static_ctors.qsort (compare_ctor);
1554 build_cdtor (/*ctor_p=*/true, static_ctors);
1557 if (!static_dtors.is_empty ())
1559 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1560 static_dtors.qsort (compare_dtor);
1561 build_cdtor (/*ctor_p=*/false, static_dtors);
1565 /* Look for constructors and destructors and produce function calling them.
1566 This is needed for targets not supporting ctors or dtors, but we perform the
1567 transformation also at linktime to merge possibly numerous
1568 constructors/destructors into single function to improve code locality and
1569 reduce size. */
1571 static unsigned int
1572 ipa_cdtor_merge (void)
1574 struct cgraph_node *node;
1575 FOR_EACH_DEFINED_FUNCTION (node)
1576 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1577 || DECL_STATIC_DESTRUCTOR (node->decl))
1578 record_cdtor_fn (node);
1579 build_cdtor_fns ();
1580 static_ctors.release ();
1581 static_dtors.release ();
1582 return 0;
1585 /* Perform the pass when we have no ctors/dtors support
1586 or at LTO time to merge multiple constructors into single
1587 function. */
1589 static bool
1590 gate_ipa_cdtor_merge (void)
1592 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1595 namespace {
1597 const pass_data pass_data_ipa_cdtor_merge =
1599 IPA_PASS, /* type */
1600 "cdtor", /* name */
1601 OPTGROUP_NONE, /* optinfo_flags */
1602 true, /* has_gate */
1603 true, /* has_execute */
1604 TV_CGRAPHOPT, /* tv_id */
1605 0, /* properties_required */
1606 0, /* properties_provided */
1607 0, /* properties_destroyed */
1608 0, /* todo_flags_start */
1609 0, /* todo_flags_finish */
1612 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1614 public:
1615 pass_ipa_cdtor_merge (gcc::context *ctxt)
1616 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1617 NULL, /* generate_summary */
1618 NULL, /* write_summary */
1619 NULL, /* read_summary */
1620 NULL, /* write_optimization_summary */
1621 NULL, /* read_optimization_summary */
1622 NULL, /* stmt_fixup */
1623 0, /* function_transform_todo_flags_start */
1624 NULL, /* function_transform */
1625 NULL) /* variable_transform */
1628 /* opt_pass methods: */
1629 bool gate () { return gate_ipa_cdtor_merge (); }
1630 unsigned int execute () { return ipa_cdtor_merge (); }
1632 }; // class pass_ipa_cdtor_merge
1634 } // anon namespace
1636 ipa_opt_pass_d *
1637 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1639 return new pass_ipa_cdtor_merge (ctxt);