Daily bump.
[official-gcc.git] / gcc / ipa.c
blob520a5bbdaec932811905d623946e854be5883f91
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 struct 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 other in the same comdat group to be also reachable. */
366 if (node->same_comdat_group)
368 symtab_node *next;
369 for (next = node->same_comdat_group;
370 next != node;
371 next = next->same_comdat_group)
372 if (!pointer_set_insert (reachable, next))
373 enqueue_node (next, &first, reachable);
375 /* Mark references as reachable. */
376 process_references (&node->ref_list, &first,
377 before_inlining_p, reachable);
380 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
382 /* Mark the callees reachable unless they are direct calls to extern
383 inline functions we decided to not inline. */
384 if (!in_boundary_p)
386 struct cgraph_edge *e;
387 /* Keep alive possible targets for devirtualization. */
388 if (optimize && flag_devirtualize)
390 struct cgraph_edge *next;
391 for (e = cnode->indirect_calls; e; e = next)
393 next = e->next_callee;
394 if (e->indirect_info->polymorphic)
395 walk_polymorphic_call_targets (reachable_call_targets,
396 e, &first, reachable,
397 before_inlining_p);
400 for (e = cnode->callees; e; e = e->next_callee)
402 if (e->callee->definition
403 && !e->callee->in_other_partition
404 && (!e->inline_failed
405 || !DECL_EXTERNAL (e->callee->decl)
406 || e->callee->alias
407 || before_inlining_p))
408 pointer_set_insert (reachable, e->callee);
409 enqueue_node (e->callee, &first, reachable);
412 /* When inline clone exists, mark body to be preserved so when removing
413 offline copy of the function we don't kill it. */
414 if (cnode->global.inlined_to)
415 pointer_set_insert (body_needed_for_clonning, cnode->decl);
417 /* For non-inline clones, force their origins to the boundary and ensure
418 that body is not removed. */
419 while (cnode->clone_of)
421 bool noninline = cnode->clone_of->decl != cnode->decl;
422 cnode = cnode->clone_of;
423 if (noninline)
425 pointer_set_insert (body_needed_for_clonning, cnode->decl);
426 enqueue_node (cnode, &first, reachable);
431 /* If any reachable function has simd clones, mark them as
432 reachable as well. */
433 if (cnode->simd_clones)
435 cgraph_node *next;
436 for (next = cnode->simd_clones;
437 next;
438 next = next->simdclone->next_clone)
439 if (in_boundary_p
440 || !pointer_set_insert (reachable, next))
441 enqueue_node (next, &first, reachable);
444 /* When we see constructor of external variable, keep referred nodes in the
445 boundary. This will also hold initializers of the external vars NODE
446 refers to. */
447 varpool_node *vnode = dyn_cast <varpool_node> (node);
448 if (vnode
449 && DECL_EXTERNAL (node->decl)
450 && !vnode->alias
451 && in_boundary_p)
453 struct ipa_ref *ref;
454 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
455 enqueue_node (ref->referred, &first, reachable);
459 /* Remove unreachable functions. */
460 for (node = cgraph_first_function (); node; node = next)
462 next = cgraph_next_function (node);
464 /* If node is not needed at all, remove it. */
465 if (!node->aux)
467 if (file)
468 fprintf (file, " %s", node->name ());
469 cgraph_remove_node (node);
470 changed = true;
472 /* If node is unreachable, remove its body. */
473 else if (!pointer_set_contains (reachable, node))
475 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
476 cgraph_release_function_body (node);
477 else if (!node->clone_of)
478 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
479 if (node->definition)
481 if (file)
482 fprintf (file, " %s", node->name ());
483 node->analyzed = false;
484 node->definition = false;
485 node->cpp_implicit_alias = false;
486 node->alias = false;
487 node->weakref = false;
488 if (!node->in_other_partition)
489 node->local.local = false;
490 cgraph_node_remove_callees (node);
491 ipa_remove_all_references (&node->ref_list);
492 changed = true;
495 else
496 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
497 || in_lto_p || DECL_RESULT (node->decl));
500 /* Inline clones might be kept around so their materializing allows further
501 cloning. If the function the clone is inlined into is removed, we need
502 to turn it into normal cone. */
503 FOR_EACH_FUNCTION (node)
505 if (node->global.inlined_to
506 && !node->callers)
508 gcc_assert (node->clones);
509 node->global.inlined_to = NULL;
510 update_inlined_to_pointer (node, node);
512 node->aux = NULL;
515 /* Remove unreachable variables. */
516 if (file)
517 fprintf (file, "\nReclaiming variables:");
518 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
520 vnext = varpool_next_variable (vnode);
521 if (!vnode->aux
522 /* For can_refer_decl_in_current_unit_p we want to track for
523 all external variables if they are defined in other partition
524 or not. */
525 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
527 if (file)
528 fprintf (file, " %s", vnode->name ());
529 varpool_remove_node (vnode);
530 changed = true;
532 else if (!pointer_set_contains (reachable, vnode))
534 tree init;
535 if (vnode->definition)
537 if (file)
538 fprintf (file, " %s", vnode->name ());
539 changed = true;
541 vnode->definition = false;
542 vnode->analyzed = false;
543 vnode->aux = NULL;
545 /* Keep body if it may be useful for constant folding. */
546 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
547 varpool_remove_initializer (vnode);
548 else
549 DECL_INITIAL (vnode->decl) = init;
550 ipa_remove_all_references (&vnode->ref_list);
552 else
553 vnode->aux = NULL;
556 pointer_set_destroy (reachable);
557 pointer_set_destroy (body_needed_for_clonning);
558 pointer_set_destroy (reachable_call_targets);
560 /* Now update address_taken flags and try to promote functions to be local. */
561 if (file)
562 fprintf (file, "\nClearing address taken flags:");
563 FOR_EACH_DEFINED_FUNCTION (node)
564 if (node->address_taken
565 && !node->used_from_other_partition)
567 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
569 if (file)
570 fprintf (file, " %s", node->name ());
571 node->address_taken = false;
572 changed = true;
573 if (cgraph_local_node_p (node))
575 node->local.local = true;
576 if (file)
577 fprintf (file, " (local)");
581 if (file)
582 fprintf (file, "\n");
584 #ifdef ENABLE_CHECKING
585 verify_symtab ();
586 #endif
588 /* If we removed something, perhaps profile could be improved. */
589 if (changed && optimize && inline_edge_summary_vec.exists ())
590 FOR_EACH_DEFINED_FUNCTION (node)
591 ipa_propagate_frequency (node);
593 timevar_pop (TV_IPA_UNREACHABLE);
594 return changed;
597 /* Discover variables that have no longer address taken or that are read only
598 and update their flags.
600 FIXME: This can not be done in between gimplify and omp_expand since
601 readonly flag plays role on what is shared and what is not. Currently we do
602 this transformation as part of whole program visibility and re-do at
603 ipa-reference pass (to take into account clonning), but it would
604 make sense to do it before early optimizations. */
606 void
607 ipa_discover_readonly_nonaddressable_vars (void)
609 struct varpool_node *vnode;
610 if (dump_file)
611 fprintf (dump_file, "Clearing variable flags:");
612 FOR_EACH_VARIABLE (vnode)
613 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
614 && (TREE_ADDRESSABLE (vnode->decl)
615 || !TREE_READONLY (vnode->decl)))
617 bool written = false;
618 bool address_taken = false;
619 int i;
620 struct ipa_ref *ref;
621 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
622 i, ref)
623 && (!written || !address_taken); i++)
624 switch (ref->use)
626 case IPA_REF_ADDR:
627 address_taken = true;
628 break;
629 case IPA_REF_LOAD:
630 break;
631 case IPA_REF_STORE:
632 written = true;
633 break;
635 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
637 if (dump_file)
638 fprintf (dump_file, " %s (addressable)", vnode->name ());
639 TREE_ADDRESSABLE (vnode->decl) = 0;
641 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
642 /* Making variable in explicit section readonly can cause section
643 type conflict.
644 See e.g. gcc.c-torture/compile/pr23237.c */
645 && DECL_SECTION_NAME (vnode->decl) == NULL)
647 if (dump_file)
648 fprintf (dump_file, " %s (read-only)", vnode->name ());
649 TREE_READONLY (vnode->decl) = 1;
652 if (dump_file)
653 fprintf (dump_file, "\n");
656 /* Return true when there is a reference to node and it is not vtable. */
657 static bool
658 address_taken_from_non_vtable_p (symtab_node *node)
660 int i;
661 struct ipa_ref *ref;
662 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
663 i, ref); i++)
664 if (ref->use == IPA_REF_ADDR)
666 struct varpool_node *node;
667 if (is_a <cgraph_node> (ref->referring))
668 return true;
669 node = ipa_ref_referring_varpool_node (ref);
670 if (!DECL_VIRTUAL_P (node->decl))
671 return true;
673 return false;
676 /* A helper for comdat_can_be_unshared_p. */
678 static bool
679 comdat_can_be_unshared_p_1 (symtab_node *node)
681 /* When address is taken, we don't know if equality comparison won't
682 break eventually. Exception are virutal functions, C++
683 constructors/destructors and vtables, where this is not possible by
684 language standard. */
685 if (!DECL_VIRTUAL_P (node->decl)
686 && (TREE_CODE (node->decl) != FUNCTION_DECL
687 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
688 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
689 && address_taken_from_non_vtable_p (node))
690 return false;
692 /* If the symbol is used in some weird way, better to not touch it. */
693 if (node->force_output)
694 return false;
696 /* Explicit instantiations needs to be output when possibly
697 used externally. */
698 if (node->forced_by_abi
699 && TREE_PUBLIC (node->decl)
700 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
701 && !flag_whole_program))
702 return false;
704 /* Non-readonly and volatile variables can not be duplicated. */
705 if (is_a <varpool_node> (node)
706 && (!TREE_READONLY (node->decl)
707 || TREE_THIS_VOLATILE (node->decl)))
708 return false;
709 return true;
712 /* COMDAT functions must be shared only if they have address taken,
713 otherwise we can produce our own private implementation with
714 -fwhole-program.
715 Return true when turning COMDAT functoin static can not lead to wrong
716 code when the resulting object links with a library defining same COMDAT.
718 Virtual functions do have their addresses taken from the vtables,
719 but in C++ there is no way to compare their addresses for equality. */
721 static bool
722 comdat_can_be_unshared_p (symtab_node *node)
724 if (!comdat_can_be_unshared_p_1 (node))
725 return false;
726 if (node->same_comdat_group)
728 symtab_node *next;
730 /* If more than one function is in the same COMDAT group, it must
731 be shared even if just one function in the comdat group has
732 address taken. */
733 for (next = node->same_comdat_group;
734 next != node; next = next->same_comdat_group)
735 if (!comdat_can_be_unshared_p_1 (next))
736 return false;
738 return true;
741 /* Return true when function NODE should be considered externally visible. */
743 static bool
744 cgraph_externally_visible_p (struct cgraph_node *node,
745 bool whole_program)
747 if (!node->definition)
748 return false;
749 if (!TREE_PUBLIC (node->decl)
750 || DECL_EXTERNAL (node->decl))
751 return false;
753 /* Do not try to localize built-in functions yet. One of problems is that we
754 end up mangling their asm for WHOPR that makes it impossible to call them
755 using the implicit built-in declarations anymore. Similarly this enables
756 us to remove them as unreachable before actual calls may appear during
757 expansion or folding. */
758 if (DECL_BUILT_IN (node->decl))
759 return true;
761 /* If linker counts on us, we must preserve the function. */
762 if (symtab_used_from_object_file_p (node))
763 return true;
764 if (DECL_PRESERVE_P (node->decl))
765 return true;
766 if (lookup_attribute ("externally_visible",
767 DECL_ATTRIBUTES (node->decl)))
768 return true;
769 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
770 && lookup_attribute ("dllexport",
771 DECL_ATTRIBUTES (node->decl)))
772 return true;
773 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
774 return false;
775 /* When doing LTO or whole program, we can bring COMDAT functoins static.
776 This improves code quality and we know we will duplicate them at most twice
777 (in the case that we are not using plugin and link with object file
778 implementing same COMDAT) */
779 if ((in_lto_p || whole_program)
780 && DECL_COMDAT (node->decl)
781 && comdat_can_be_unshared_p (node))
782 return false;
784 /* When doing link time optimizations, hidden symbols become local. */
785 if (in_lto_p
786 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
787 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
788 /* Be sure that node is defined in IR file, not in other object
789 file. In that case we don't set used_from_other_object_file. */
790 && node->definition)
792 else if (!whole_program)
793 return true;
795 if (MAIN_NAME_P (DECL_NAME (node->decl)))
796 return true;
798 return false;
801 /* Return true when variable VNODE should be considered externally visible. */
803 bool
804 varpool_externally_visible_p (struct varpool_node *vnode)
806 if (DECL_EXTERNAL (vnode->decl))
807 return true;
809 if (!TREE_PUBLIC (vnode->decl))
810 return false;
812 /* If linker counts on us, we must preserve the function. */
813 if (symtab_used_from_object_file_p (vnode))
814 return true;
816 if (DECL_HARD_REGISTER (vnode->decl))
817 return true;
818 if (DECL_PRESERVE_P (vnode->decl))
819 return true;
820 if (lookup_attribute ("externally_visible",
821 DECL_ATTRIBUTES (vnode->decl)))
822 return true;
823 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
824 && lookup_attribute ("dllexport",
825 DECL_ATTRIBUTES (vnode->decl)))
826 return true;
828 /* See if we have linker information about symbol not being used or
829 if we need to make guess based on the declaration.
831 Even if the linker clams the symbol is unused, never bring internal
832 symbols that are declared by user as used or externally visible.
833 This is needed for i.e. references from asm statements. */
834 if (symtab_used_from_object_file_p (vnode))
835 return true;
836 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
837 return false;
839 /* As a special case, the COMDAT virtual tables can be unshared.
840 In LTO mode turn vtables into static variables. The variable is readonly,
841 so this does not enable more optimization, but referring static var
842 is faster for dynamic linking. Also this match logic hidding vtables
843 from LTO symbol tables. */
844 if ((in_lto_p || flag_whole_program)
845 && DECL_COMDAT (vnode->decl)
846 && comdat_can_be_unshared_p (vnode))
847 return false;
849 /* When doing link time optimizations, hidden symbols become local. */
850 if (in_lto_p
851 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
852 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
853 /* Be sure that node is defined in IR file, not in other object
854 file. In that case we don't set used_from_other_object_file. */
855 && vnode->definition)
857 else if (!flag_whole_program)
858 return true;
860 /* Do not attempt to privatize COMDATS by default.
861 This would break linking with C++ libraries sharing
862 inline definitions.
864 FIXME: We can do so for readonly vars with no address taken and
865 possibly also for vtables since no direct pointer comparsion is done.
866 It might be interesting to do so to reduce linking overhead. */
867 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
868 return true;
869 return false;
872 /* Return true if reference to NODE can be replaced by a local alias.
873 Local aliases save dynamic linking overhead and enable more optimizations.
876 bool
877 can_replace_by_local_alias (symtab_node *node)
879 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
880 && !symtab_can_be_discarded (node));
883 /* Mark visibility of all functions.
885 A local function is one whose calls can occur only in the current
886 compilation unit and all its calls are explicit, so we can change
887 its calling convention. We simply mark all static functions whose
888 address is not taken as local.
890 We also change the TREE_PUBLIC flag of all declarations that are public
891 in language point of view but we want to overwrite this default
892 via visibilities for the backend point of view. */
894 static unsigned int
895 function_and_variable_visibility (bool whole_program)
897 struct cgraph_node *node;
898 struct varpool_node *vnode;
900 /* All aliases should be procssed at this point. */
901 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
903 FOR_EACH_FUNCTION (node)
905 int flags = flags_from_decl_or_type (node->decl);
907 /* Optimize away PURE and CONST constructors and destructors. */
908 if (optimize
909 && (flags & (ECF_CONST | ECF_PURE))
910 && !(flags & ECF_LOOPING_CONST_OR_PURE))
912 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
913 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
916 /* Frontends and alias code marks nodes as needed before parsing is finished.
917 We may end up marking as node external nodes where this flag is meaningless
918 strip it. */
919 if (DECL_EXTERNAL (node->decl) || !node->definition)
921 node->force_output = 0;
922 node->forced_by_abi = 0;
925 /* C++ FE on lack of COMDAT support create local COMDAT functions
926 (that ought to be shared but can not due to object format
927 limitations). It is necessary to keep the flag to make rest of C++ FE
928 happy. Clear the flag here to avoid confusion in middle-end. */
929 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
930 DECL_COMDAT (node->decl) = 0;
932 /* For external decls stop tracking same_comdat_group. It doesn't matter
933 what comdat group they are in when they won't be emitted in this TU. */
934 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
936 #ifdef ENABLE_CHECKING
937 symtab_node *n;
939 for (n = node->same_comdat_group;
940 n != node;
941 n = n->same_comdat_group)
942 /* If at least one of same comdat group functions is external,
943 all of them have to be, otherwise it is a front-end bug. */
944 gcc_assert (DECL_EXTERNAL (n->decl));
945 #endif
946 symtab_dissolve_same_comdat_group_list (node);
948 gcc_assert ((!DECL_WEAK (node->decl)
949 && !DECL_COMDAT (node->decl))
950 || TREE_PUBLIC (node->decl)
951 || node->weakref
952 || DECL_EXTERNAL (node->decl));
953 if (cgraph_externally_visible_p (node, whole_program))
955 gcc_assert (!node->global.inlined_to);
956 node->externally_visible = true;
958 else
960 node->externally_visible = false;
961 node->forced_by_abi = false;
963 if (!node->externally_visible
964 && node->definition && !node->weakref
965 && !DECL_EXTERNAL (node->decl))
967 gcc_assert (whole_program || in_lto_p
968 || !TREE_PUBLIC (node->decl));
969 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
970 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
971 && TREE_PUBLIC (node->decl));
972 symtab_make_decl_local (node->decl);
973 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
974 if (node->same_comdat_group)
975 /* cgraph_externally_visible_p has already checked all other nodes
976 in the group and they will all be made local. We need to
977 dissolve the group at once so that the predicate does not
978 segfault though. */
979 symtab_dissolve_same_comdat_group_list (node);
982 if (node->thunk.thunk_p
983 && TREE_PUBLIC (node->decl))
985 struct cgraph_node *decl_node = node;
987 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
989 /* Thunks have the same visibility as function they are attached to.
990 Make sure the C++ front end set this up properly. */
991 if (DECL_ONE_ONLY (decl_node->decl))
993 gcc_checking_assert (DECL_COMDAT (node->decl)
994 == DECL_COMDAT (decl_node->decl));
995 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
996 == DECL_COMDAT_GROUP (decl_node->decl));
997 gcc_checking_assert (node->same_comdat_group);
999 if (DECL_EXTERNAL (decl_node->decl))
1000 DECL_EXTERNAL (node->decl) = 1;
1003 FOR_EACH_DEFINED_FUNCTION (node)
1005 node->local.local |= cgraph_local_node_p (node);
1007 /* If we know that function can not be overwritten by a different semantics
1008 and moreover its section can not be discarded, replace all direct calls
1009 by calls to an nonoverwritable alias. This make dynamic linking
1010 cheaper and enable more optimization.
1012 TODO: We can also update virtual tables. */
1013 if (node->callers && can_replace_by_local_alias (node))
1015 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1017 if (alias && alias != node)
1019 while (node->callers)
1021 struct cgraph_edge *e = node->callers;
1023 cgraph_redirect_edge_callee (e, alias);
1024 if (gimple_has_body_p (e->caller->decl))
1026 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1027 cgraph_redirect_edge_call_stmt_to_callee (e);
1028 pop_cfun ();
1034 FOR_EACH_VARIABLE (vnode)
1036 /* weak flag makes no sense on local variables. */
1037 gcc_assert (!DECL_WEAK (vnode->decl)
1038 || vnode->weakref
1039 || TREE_PUBLIC (vnode->decl)
1040 || DECL_EXTERNAL (vnode->decl));
1041 /* In several cases declarations can not be common:
1043 - when declaration has initializer
1044 - when it is in weak
1045 - when it has specific section
1046 - when it resides in non-generic address space.
1047 - if declaration is local, it will get into .local common section
1048 so common flag is not needed. Frontends still produce these in
1049 certain cases, such as for:
1051 static int a __attribute__ ((common))
1053 Canonicalize things here and clear the redundant flag. */
1054 if (DECL_COMMON (vnode->decl)
1055 && (!(TREE_PUBLIC (vnode->decl)
1056 || DECL_EXTERNAL (vnode->decl))
1057 || (DECL_INITIAL (vnode->decl)
1058 && DECL_INITIAL (vnode->decl) != error_mark_node)
1059 || DECL_WEAK (vnode->decl)
1060 || DECL_SECTION_NAME (vnode->decl) != NULL
1061 || ! (ADDR_SPACE_GENERIC_P
1062 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1063 DECL_COMMON (vnode->decl) = 0;
1065 FOR_EACH_DEFINED_VARIABLE (vnode)
1067 if (!vnode->definition)
1068 continue;
1069 if (varpool_externally_visible_p (vnode))
1070 vnode->externally_visible = true;
1071 else
1073 vnode->externally_visible = false;
1074 vnode->forced_by_abi = false;
1076 if (!vnode->externally_visible
1077 && !vnode->weakref)
1079 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1080 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1081 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1082 && TREE_PUBLIC (vnode->decl));
1083 symtab_make_decl_local (vnode->decl);
1084 if (vnode->same_comdat_group)
1085 symtab_dissolve_same_comdat_group_list (vnode);
1086 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1090 if (dump_file)
1092 fprintf (dump_file, "\nMarking local functions:");
1093 FOR_EACH_DEFINED_FUNCTION (node)
1094 if (node->local.local)
1095 fprintf (dump_file, " %s", node->name ());
1096 fprintf (dump_file, "\n\n");
1097 fprintf (dump_file, "\nMarking externally visible functions:");
1098 FOR_EACH_DEFINED_FUNCTION (node)
1099 if (node->externally_visible)
1100 fprintf (dump_file, " %s", node->name ());
1101 fprintf (dump_file, "\n\n");
1102 fprintf (dump_file, "\nMarking externally visible variables:");
1103 FOR_EACH_DEFINED_VARIABLE (vnode)
1104 if (vnode->externally_visible)
1105 fprintf (dump_file, " %s", vnode->name ());
1106 fprintf (dump_file, "\n\n");
1108 cgraph_function_flags_ready = true;
1109 return 0;
1112 /* Local function pass handling visibilities. This happens before LTO streaming
1113 so in particular -fwhole-program should be ignored at this level. */
1115 static unsigned int
1116 local_function_and_variable_visibility (void)
1118 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1121 namespace {
1123 const pass_data pass_data_ipa_function_and_variable_visibility =
1125 SIMPLE_IPA_PASS, /* type */
1126 "visibility", /* name */
1127 OPTGROUP_NONE, /* optinfo_flags */
1128 false, /* has_gate */
1129 true, /* has_execute */
1130 TV_CGRAPHOPT, /* tv_id */
1131 0, /* properties_required */
1132 0, /* properties_provided */
1133 0, /* properties_destroyed */
1134 0, /* todo_flags_start */
1135 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1138 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1140 public:
1141 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1142 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1143 ctxt)
1146 /* opt_pass methods: */
1147 unsigned int execute () {
1148 return local_function_and_variable_visibility ();
1151 }; // class pass_ipa_function_and_variable_visibility
1153 } // anon namespace
1155 simple_ipa_opt_pass *
1156 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1158 return new pass_ipa_function_and_variable_visibility (ctxt);
1161 /* Free inline summary. */
1163 static unsigned
1164 free_inline_summary (void)
1166 inline_free_summary ();
1167 return 0;
1170 namespace {
1172 const pass_data pass_data_ipa_free_inline_summary =
1174 SIMPLE_IPA_PASS, /* type */
1175 "*free_inline_summary", /* name */
1176 OPTGROUP_NONE, /* optinfo_flags */
1177 false, /* has_gate */
1178 true, /* has_execute */
1179 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1180 0, /* properties_required */
1181 0, /* properties_provided */
1182 0, /* properties_destroyed */
1183 0, /* todo_flags_start */
1184 0, /* todo_flags_finish */
1187 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1189 public:
1190 pass_ipa_free_inline_summary (gcc::context *ctxt)
1191 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1194 /* opt_pass methods: */
1195 unsigned int execute () { return free_inline_summary (); }
1197 }; // class pass_ipa_free_inline_summary
1199 } // anon namespace
1201 simple_ipa_opt_pass *
1202 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1204 return new pass_ipa_free_inline_summary (ctxt);
1207 /* Do not re-run on ltrans stage. */
1209 static bool
1210 gate_whole_program_function_and_variable_visibility (void)
1212 return !flag_ltrans;
1215 /* Bring functionss local at LTO time with -fwhole-program. */
1217 static unsigned int
1218 whole_program_function_and_variable_visibility (void)
1220 function_and_variable_visibility (flag_whole_program);
1221 if (optimize)
1222 ipa_discover_readonly_nonaddressable_vars ();
1223 return 0;
1226 namespace {
1228 const pass_data pass_data_ipa_whole_program_visibility =
1230 IPA_PASS, /* type */
1231 "whole-program", /* name */
1232 OPTGROUP_NONE, /* optinfo_flags */
1233 true, /* has_gate */
1234 true, /* has_execute */
1235 TV_CGRAPHOPT, /* tv_id */
1236 0, /* properties_required */
1237 0, /* properties_provided */
1238 0, /* properties_destroyed */
1239 0, /* todo_flags_start */
1240 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1243 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1245 public:
1246 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1247 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1248 NULL, /* generate_summary */
1249 NULL, /* write_summary */
1250 NULL, /* read_summary */
1251 NULL, /* write_optimization_summary */
1252 NULL, /* read_optimization_summary */
1253 NULL, /* stmt_fixup */
1254 0, /* function_transform_todo_flags_start */
1255 NULL, /* function_transform */
1256 NULL) /* variable_transform */
1259 /* opt_pass methods: */
1260 bool gate () {
1261 return gate_whole_program_function_and_variable_visibility ();
1263 unsigned int execute () {
1264 return whole_program_function_and_variable_visibility ();
1267 }; // class pass_ipa_whole_program_visibility
1269 } // anon namespace
1271 ipa_opt_pass_d *
1272 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1274 return new pass_ipa_whole_program_visibility (ctxt);
1277 /* Generate and emit a static constructor or destructor. WHICH must
1278 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1279 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1280 initialization priority for this constructor or destructor.
1282 FINAL specify whether the externally visible name for collect2 should
1283 be produced. */
1285 static void
1286 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1288 static int counter = 0;
1289 char which_buf[16];
1290 tree decl, name, resdecl;
1292 /* The priority is encoded in the constructor or destructor name.
1293 collect2 will sort the names and arrange that they are called at
1294 program startup. */
1295 if (final)
1296 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1297 else
1298 /* Proudce sane name but one not recognizable by collect2, just for the
1299 case we fail to inline the function. */
1300 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1301 name = get_file_function_name (which_buf);
1303 decl = build_decl (input_location, FUNCTION_DECL, name,
1304 build_function_type_list (void_type_node, NULL_TREE));
1305 current_function_decl = decl;
1307 resdecl = build_decl (input_location,
1308 RESULT_DECL, NULL_TREE, void_type_node);
1309 DECL_ARTIFICIAL (resdecl) = 1;
1310 DECL_RESULT (decl) = resdecl;
1311 DECL_CONTEXT (resdecl) = decl;
1313 allocate_struct_function (decl, false);
1315 TREE_STATIC (decl) = 1;
1316 TREE_USED (decl) = 1;
1317 DECL_ARTIFICIAL (decl) = 1;
1318 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1319 DECL_SAVED_TREE (decl) = body;
1320 if (!targetm.have_ctors_dtors && final)
1322 TREE_PUBLIC (decl) = 1;
1323 DECL_PRESERVE_P (decl) = 1;
1325 DECL_UNINLINABLE (decl) = 1;
1327 DECL_INITIAL (decl) = make_node (BLOCK);
1328 TREE_USED (DECL_INITIAL (decl)) = 1;
1330 DECL_SOURCE_LOCATION (decl) = input_location;
1331 cfun->function_end_locus = input_location;
1333 switch (which)
1335 case 'I':
1336 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1337 decl_init_priority_insert (decl, priority);
1338 break;
1339 case 'D':
1340 DECL_STATIC_DESTRUCTOR (decl) = 1;
1341 decl_fini_priority_insert (decl, priority);
1342 break;
1343 default:
1344 gcc_unreachable ();
1347 gimplify_function_tree (decl);
1349 cgraph_add_new_function (decl, false);
1351 set_cfun (NULL);
1352 current_function_decl = NULL;
1355 /* Generate and emit a static constructor or destructor. WHICH must
1356 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1357 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1358 initialization priority for this constructor or destructor. */
1360 void
1361 cgraph_build_static_cdtor (char which, tree body, int priority)
1363 cgraph_build_static_cdtor_1 (which, body, priority, false);
1366 /* A vector of FUNCTION_DECLs declared as static constructors. */
1367 static vec<tree> static_ctors;
1368 /* A vector of FUNCTION_DECLs declared as static destructors. */
1369 static vec<tree> static_dtors;
1371 /* When target does not have ctors and dtors, we call all constructor
1372 and destructor by special initialization/destruction function
1373 recognized by collect2.
1375 When we are going to build this function, collect all constructors and
1376 destructors and turn them into normal functions. */
1378 static void
1379 record_cdtor_fn (struct cgraph_node *node)
1381 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1382 static_ctors.safe_push (node->decl);
1383 if (DECL_STATIC_DESTRUCTOR (node->decl))
1384 static_dtors.safe_push (node->decl);
1385 node = cgraph_get_node (node->decl);
1386 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1389 /* Define global constructors/destructor functions for the CDTORS, of
1390 which they are LEN. The CDTORS are sorted by initialization
1391 priority. If CTOR_P is true, these are constructors; otherwise,
1392 they are destructors. */
1394 static void
1395 build_cdtor (bool ctor_p, vec<tree> cdtors)
1397 size_t i,j;
1398 size_t len = cdtors.length ();
1400 i = 0;
1401 while (i < len)
1403 tree body;
1404 tree fn;
1405 priority_type priority;
1407 priority = 0;
1408 body = NULL_TREE;
1409 j = i;
1412 priority_type p;
1413 fn = cdtors[j];
1414 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1415 if (j == i)
1416 priority = p;
1417 else if (p != priority)
1418 break;
1419 j++;
1421 while (j < len);
1423 /* When there is only one cdtor and target supports them, do nothing. */
1424 if (j == i + 1
1425 && targetm.have_ctors_dtors)
1427 i++;
1428 continue;
1430 /* Find the next batch of constructors/destructors with the same
1431 initialization priority. */
1432 for (;i < j; i++)
1434 tree call;
1435 fn = cdtors[i];
1436 call = build_call_expr (fn, 0);
1437 if (ctor_p)
1438 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1439 else
1440 DECL_STATIC_DESTRUCTOR (fn) = 0;
1441 /* We do not want to optimize away pure/const calls here.
1442 When optimizing, these should be already removed, when not
1443 optimizing, we want user to be able to breakpoint in them. */
1444 TREE_SIDE_EFFECTS (call) = 1;
1445 append_to_statement_list (call, &body);
1447 gcc_assert (body != NULL_TREE);
1448 /* Generate a function to call all the function of like
1449 priority. */
1450 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1454 /* Comparison function for qsort. P1 and P2 are actually of type
1455 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1456 used to determine the sort order. */
1458 static int
1459 compare_ctor (const void *p1, const void *p2)
1461 tree f1;
1462 tree f2;
1463 int priority1;
1464 int priority2;
1466 f1 = *(const tree *)p1;
1467 f2 = *(const tree *)p2;
1468 priority1 = DECL_INIT_PRIORITY (f1);
1469 priority2 = DECL_INIT_PRIORITY (f2);
1471 if (priority1 < priority2)
1472 return -1;
1473 else if (priority1 > priority2)
1474 return 1;
1475 else
1476 /* Ensure a stable sort. Constructors are executed in backwarding
1477 order to make LTO initialize braries first. */
1478 return DECL_UID (f2) - DECL_UID (f1);
1481 /* Comparison function for qsort. P1 and P2 are actually of type
1482 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1483 used to determine the sort order. */
1485 static int
1486 compare_dtor (const void *p1, const void *p2)
1488 tree f1;
1489 tree f2;
1490 int priority1;
1491 int priority2;
1493 f1 = *(const tree *)p1;
1494 f2 = *(const tree *)p2;
1495 priority1 = DECL_FINI_PRIORITY (f1);
1496 priority2 = DECL_FINI_PRIORITY (f2);
1498 if (priority1 < priority2)
1499 return -1;
1500 else if (priority1 > priority2)
1501 return 1;
1502 else
1503 /* Ensure a stable sort. */
1504 return DECL_UID (f1) - DECL_UID (f2);
1507 /* Generate functions to call static constructors and destructors
1508 for targets that do not support .ctors/.dtors sections. These
1509 functions have magic names which are detected by collect2. */
1511 static void
1512 build_cdtor_fns (void)
1514 if (!static_ctors.is_empty ())
1516 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1517 static_ctors.qsort (compare_ctor);
1518 build_cdtor (/*ctor_p=*/true, static_ctors);
1521 if (!static_dtors.is_empty ())
1523 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1524 static_dtors.qsort (compare_dtor);
1525 build_cdtor (/*ctor_p=*/false, static_dtors);
1529 /* Look for constructors and destructors and produce function calling them.
1530 This is needed for targets not supporting ctors or dtors, but we perform the
1531 transformation also at linktime to merge possibly numerous
1532 constructors/destructors into single function to improve code locality and
1533 reduce size. */
1535 static unsigned int
1536 ipa_cdtor_merge (void)
1538 struct cgraph_node *node;
1539 FOR_EACH_DEFINED_FUNCTION (node)
1540 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1541 || DECL_STATIC_DESTRUCTOR (node->decl))
1542 record_cdtor_fn (node);
1543 build_cdtor_fns ();
1544 static_ctors.release ();
1545 static_dtors.release ();
1546 return 0;
1549 /* Perform the pass when we have no ctors/dtors support
1550 or at LTO time to merge multiple constructors into single
1551 function. */
1553 static bool
1554 gate_ipa_cdtor_merge (void)
1556 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1559 namespace {
1561 const pass_data pass_data_ipa_cdtor_merge =
1563 IPA_PASS, /* type */
1564 "cdtor", /* name */
1565 OPTGROUP_NONE, /* optinfo_flags */
1566 true, /* has_gate */
1567 true, /* has_execute */
1568 TV_CGRAPHOPT, /* tv_id */
1569 0, /* properties_required */
1570 0, /* properties_provided */
1571 0, /* properties_destroyed */
1572 0, /* todo_flags_start */
1573 0, /* todo_flags_finish */
1576 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1578 public:
1579 pass_ipa_cdtor_merge (gcc::context *ctxt)
1580 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1581 NULL, /* generate_summary */
1582 NULL, /* write_summary */
1583 NULL, /* read_summary */
1584 NULL, /* write_optimization_summary */
1585 NULL, /* read_optimization_summary */
1586 NULL, /* stmt_fixup */
1587 0, /* function_transform_todo_flags_start */
1588 NULL, /* function_transform */
1589 NULL) /* variable_transform */
1592 /* opt_pass methods: */
1593 bool gate () { return gate_ipa_cdtor_merge (); }
1594 unsigned int execute () { return ipa_cdtor_merge (); }
1596 }; // class pass_ipa_cdtor_merge
1598 } // anon namespace
1600 ipa_opt_pass_d *
1601 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1603 return new pass_ipa_cdtor_merge (ctxt);