2013-10-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / ipa.c
blob92343fb22c935a1dc5c84fee63e5883977f5dd51
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 "cgraph.h"
25 #include "tree-pass.h"
26 #include "gimple.h"
27 #include "ggc.h"
28 #include "flags.h"
29 #include "pointer-set.h"
30 #include "target.h"
31 #include "tree-iterator.h"
32 #include "ipa-utils.h"
33 #include "ipa-inline.h"
34 #include "tree-inline.h"
35 #include "profile.h"
36 #include "params.h"
38 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
40 static bool
41 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
43 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
44 return !(cgraph_only_called_directly_or_aliased_p (node)
45 && !ipa_ref_has_aliases_p (&node->symbol.ref_list)
46 && node->symbol.definition
47 && !DECL_EXTERNAL (node->symbol.decl)
48 && !node->symbol.externally_visible
49 && !node->symbol.used_from_other_partition
50 && !node->symbol.in_other_partition);
53 /* Return true when function can be marked local. */
55 static bool
56 cgraph_local_node_p (struct cgraph_node *node)
58 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
60 /* FIXME: thunks can be considered local, but we need prevent i386
61 from attempting to change calling convention of them. */
62 if (n->thunk.thunk_p)
63 return false;
64 return !cgraph_for_node_and_aliases (n,
65 cgraph_non_local_node_p_1, NULL, true);
69 /* Return true when NODE has ADDR reference. */
71 static bool
72 has_addr_references_p (struct cgraph_node *node,
73 void *data ATTRIBUTE_UNUSED)
75 int i;
76 struct ipa_ref *ref;
78 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
79 i, ref); i++)
80 if (ref->use == IPA_REF_ADDR)
81 return true;
82 return false;
85 /* Look for all functions inlined to NODE and update their inlined_to pointers
86 to INLINED_TO. */
88 static void
89 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
91 struct cgraph_edge *e;
92 for (e = node->callees; e; e = e->next_callee)
93 if (e->callee->global.inlined_to)
95 e->callee->global.inlined_to = inlined_to;
96 update_inlined_to_pointer (e->callee, inlined_to);
100 /* Add symtab NODE to queue starting at FIRST.
102 The queue is linked via AUX pointers and terminated by pointer to 1.
103 We enqueue nodes at two occasions: when we find them reachable or when we find
104 their bodies needed for further clonning. In the second case we mark them
105 by pointer to 2 after processing so they are re-queue when they become
106 reachable. */
108 static void
109 enqueue_node (symtab_node node, symtab_node *first,
110 struct pointer_set_t *reachable)
112 /* Node is still in queue; do nothing. */
113 if (node->symbol.aux && node->symbol.aux != (void *) 2)
114 return;
115 /* Node was already processed as unreachable, re-enqueue
116 only if it became reachable now. */
117 if (node->symbol.aux == (void *)2 && !pointer_set_contains (reachable, node))
118 return;
119 node->symbol.aux = *first;
120 *first = node;
123 /* Process references. */
125 static void
126 process_references (struct ipa_ref_list *list,
127 symtab_node *first,
128 bool before_inlining_p,
129 struct pointer_set_t *reachable)
131 int i;
132 struct ipa_ref *ref;
133 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
135 symtab_node node = ref->referred;
137 if (node->symbol.definition && !node->symbol.in_other_partition
138 && ((!DECL_EXTERNAL (node->symbol.decl) || node->symbol.alias)
139 || (before_inlining_p
140 /* We use variable constructors during late complation for
141 constant folding. Keep references alive so partitioning
142 knows about potential references. */
143 || (TREE_CODE (node->symbol.decl) == VAR_DECL
144 && flag_wpa
145 && ctor_for_folding (node->symbol.decl)
146 != error_mark_node))))
147 pointer_set_insert (reachable, node);
148 enqueue_node ((symtab_node) node, first, reachable);
152 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
153 all its potential targets as reachable to permit later inlining if
154 devirtualization happens. After inlining still keep their declarations
155 around, so we can devirtualize to a direct call.
157 Also try to make trivial devirutalization when no or only one target is
158 possible. */
160 static void
161 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
162 struct cgraph_edge *edge,
163 symtab_node *first,
164 pointer_set_t *reachable, bool before_inlining_p)
166 unsigned int i;
167 void *cache_token;
168 bool final;
169 vec <cgraph_node *>targets
170 = possible_polymorphic_call_targets
171 (edge, &final, &cache_token);
173 if (!pointer_set_insert (reachable_call_targets,
174 cache_token))
176 for (i = 0; i < targets.length (); i++)
178 struct cgraph_node *n = targets[i];
180 /* Do not bother to mark virtual methods in anonymous namespace;
181 either we will find use of virtual table defining it, or it is
182 unused. */
183 if (TREE_CODE (TREE_TYPE (n->symbol.decl)) == METHOD_TYPE
184 && type_in_anonymous_namespace_p
185 (method_class_type (TREE_TYPE (n->symbol.decl))))
186 continue;
188 /* Prior inlining, keep alive bodies of possible targets for
189 devirtualization. */
190 if (n->symbol.definition
191 && before_inlining_p)
192 pointer_set_insert (reachable, n);
194 /* Even after inlining we want to keep the possible targets in the
195 boundary, so late passes can still produce direct call even if
196 the chance for inlining is lost. */
197 enqueue_node ((symtab_node) n, first, reachable);
201 /* Very trivial devirtualization; when the type is
202 final or anonymous (so we know all its derivation)
203 and there is only one possible virtual call target,
204 make the edge direct. */
205 if (final)
207 if (targets.length () <= 1)
209 cgraph_node *target, *node = edge->caller;
210 if (targets.length () == 1)
211 target = targets[0];
212 else
213 target = cgraph_get_create_node
214 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
216 if (dump_file)
217 fprintf (dump_file,
218 "Devirtualizing call in %s/%i to %s/%i\n",
219 cgraph_node_name (edge->caller),
220 edge->caller->symbol.order,
221 cgraph_node_name (target), target->symbol.order);
222 edge = cgraph_make_edge_direct (edge, target);
223 if (!inline_summary_vec && edge->call_stmt)
224 cgraph_redirect_edge_call_stmt_to_callee (edge);
225 else
226 inline_update_overall_summary (node);
231 /* Perform reachability analysis and reclaim all unreachable nodes.
233 The algorithm is basically mark&sweep but with some extra refinements:
235 - reachable extern inline functions needs special handling; the bodies needs
236 to stay in memory until inlining in hope that they will be inlined.
237 After inlining we release their bodies and turn them into unanalyzed
238 nodes even when they are reachable.
240 BEFORE_INLINING_P specify whether we are before or after inlining.
242 - virtual functions are kept in callgraph even if they seem unreachable in
243 hope calls to them will be devirtualized.
245 Again we remove them after inlining. In late optimization some
246 devirtualization may happen, but it is not importnat since we won't inline
247 the call. In theory early opts and IPA should work out all important cases.
249 - virtual clones needs bodies of their origins for later materialization;
250 this means that we want to keep the body even if the origin is unreachable
251 otherwise. To avoid origin from sitting in the callgraph and being
252 walked by IPA passes, we turn them into unanalyzed nodes with body
253 defined.
255 We maintain set of function declaration where body needs to stay in
256 body_needed_for_clonning
258 Inline clones represent special case: their declaration match the
259 declaration of origin and cgraph_remove_node already knows how to
260 reshape callgraph and preserve body when offline copy of function or
261 inline clone is being removed.
263 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
264 variables with DECL_INITIAL set. We finalize these and keep reachable
265 ones around for constant folding purposes. After inlining we however
266 stop walking their references to let everything static referneced by them
267 to be removed when it is otherwise unreachable.
269 We maintain queue of both reachable symbols (i.e. defined symbols that needs
270 to stay) and symbols that are in boundary (i.e. external symbols referenced
271 by reachable symbols or origins of clones). The queue is represented
272 as linked list by AUX pointer terminated by 1.
274 A the end we keep all reachable symbols. For symbols in boundary we always
275 turn definition into a declaration, but we may keep function body around
276 based on body_needed_for_clonning
278 All symbols that enter the queue have AUX pointer non-zero and are in the
279 boundary. Pointer set REACHABLE is used to track reachable symbols.
281 Every symbol can be visited twice - once as part of boundary and once
282 as real reachable symbol. enqueue_node needs to decide whether the
283 node needs to be re-queued for second processing. For this purpose
284 we set AUX pointer of processed symbols in the boundary to constant 2. */
286 bool
287 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
289 symtab_node first = (symtab_node) (void *) 1;
290 struct cgraph_node *node, *next;
291 struct varpool_node *vnode, *vnext;
292 bool changed = false;
293 struct pointer_set_t *reachable = pointer_set_create ();
294 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
295 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
297 timevar_push (TV_IPA_UNREACHABLE);
298 #ifdef ENABLE_CHECKING
299 verify_symtab ();
300 #endif
301 if (optimize && flag_devirtualize)
302 build_type_inheritance_graph ();
303 if (file)
304 fprintf (file, "\nReclaiming functions:");
305 #ifdef ENABLE_CHECKING
306 FOR_EACH_FUNCTION (node)
307 gcc_assert (!node->symbol.aux);
308 FOR_EACH_VARIABLE (vnode)
309 gcc_assert (!vnode->symbol.aux);
310 #endif
311 /* Mark functions whose bodies are obviously needed.
312 This is mostly when they can be referenced externally. Inline clones
313 are special since their declarations are shared with master clone and thus
314 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
315 FOR_EACH_FUNCTION (node)
317 node->used_as_abstract_origin = false;
318 if (node->symbol.definition
319 && !node->global.inlined_to
320 && !node->symbol.in_other_partition
321 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
323 gcc_assert (!node->global.inlined_to);
324 pointer_set_insert (reachable, node);
325 enqueue_node ((symtab_node)node, &first, reachable);
327 else
328 gcc_assert (!node->symbol.aux);
331 /* Mark variables that are obviously needed. */
332 FOR_EACH_DEFINED_VARIABLE (vnode)
333 if (!varpool_can_remove_if_no_refs (vnode)
334 && !vnode->symbol.in_other_partition)
336 pointer_set_insert (reachable, vnode);
337 enqueue_node ((symtab_node)vnode, &first, reachable);
340 /* Perform reachability analysis. */
341 while (first != (symtab_node) (void *) 1)
343 bool in_boundary_p = !pointer_set_contains (reachable, first);
344 symtab_node node = first;
346 first = (symtab_node)first->symbol.aux;
348 /* If we are processing symbol in boundary, mark its AUX pointer for
349 possible later re-processing in enqueue_node. */
350 if (in_boundary_p)
351 node->symbol.aux = (void *)2;
352 else
354 if (DECL_ABSTRACT_ORIGIN (node->symbol.decl))
356 struct cgraph_node *origin_node
357 = cgraph_get_create_real_symbol_node (DECL_ABSTRACT_ORIGIN (node->symbol.decl));
358 origin_node->used_as_abstract_origin = true;
359 enqueue_node ((symtab_node) origin_node, &first, reachable);
361 /* If any symbol in a comdat group is reachable, force
362 all other in the same comdat group to be also reachable. */
363 if (node->symbol.same_comdat_group)
365 symtab_node next;
366 for (next = node->symbol.same_comdat_group;
367 next != node;
368 next = next->symbol.same_comdat_group)
369 if (!pointer_set_insert (reachable, next))
370 enqueue_node ((symtab_node) next, &first, reachable);
372 /* Mark references as reachable. */
373 process_references (&node->symbol.ref_list, &first,
374 before_inlining_p, reachable);
377 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
379 /* Mark the callees reachable unless they are direct calls to extern
380 inline functions we decided to not inline. */
381 if (!in_boundary_p)
383 struct cgraph_edge *e;
384 /* Keep alive possible targets for devirtualization. */
385 if (optimize && flag_devirtualize)
387 struct cgraph_edge *next;
388 for (e = cnode->indirect_calls; e; e = next)
390 next = e->next_callee;
391 if (e->indirect_info->polymorphic)
392 walk_polymorphic_call_targets (reachable_call_targets,
393 e, &first, reachable,
394 before_inlining_p);
397 for (e = cnode->callees; e; e = e->next_callee)
399 if (e->callee->symbol.definition
400 && !e->callee->symbol.in_other_partition
401 && (!e->inline_failed
402 || !DECL_EXTERNAL (e->callee->symbol.decl)
403 || e->callee->symbol.alias
404 || before_inlining_p))
405 pointer_set_insert (reachable, e->callee);
406 enqueue_node ((symtab_node) e->callee, &first, reachable);
409 /* When inline clone exists, mark body to be preserved so when removing
410 offline copy of the function we don't kill it. */
411 if (cnode->global.inlined_to)
412 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
414 /* For non-inline clones, force their origins to the boundary and ensure
415 that body is not removed. */
416 while (cnode->clone_of)
418 bool noninline = cnode->clone_of->symbol.decl != cnode->symbol.decl;
419 cnode = cnode->clone_of;
420 if (noninline)
422 pointer_set_insert (body_needed_for_clonning, cnode->symbol.decl);
423 enqueue_node ((symtab_node)cnode, &first, reachable);
428 /* When we see constructor of external variable, keep referred nodes in the
429 boundary. This will also hold initializers of the external vars NODE
430 refers to. */
431 varpool_node *vnode = dyn_cast <varpool_node> (node);
432 if (vnode
433 && DECL_EXTERNAL (node->symbol.decl)
434 && !vnode->symbol.alias
435 && in_boundary_p)
437 struct ipa_ref *ref;
438 for (int i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
439 enqueue_node (ref->referred, &first, reachable);
443 /* Remove unreachable functions. */
444 for (node = cgraph_first_function (); node; node = next)
446 next = cgraph_next_function (node);
448 /* If node is not needed at all, remove it. */
449 if (!node->symbol.aux)
451 if (file)
452 fprintf (file, " %s", cgraph_node_name (node));
453 cgraph_remove_node (node);
454 changed = true;
456 /* If node is unreachable, remove its body. */
457 else if (!pointer_set_contains (reachable, node))
459 if (!pointer_set_contains (body_needed_for_clonning, node->symbol.decl))
460 cgraph_release_function_body (node);
461 else if (!node->clone_of)
462 gcc_assert (in_lto_p || DECL_RESULT (node->symbol.decl));
463 if (node->symbol.definition)
465 if (file)
466 fprintf (file, " %s", cgraph_node_name (node));
467 node->symbol.analyzed = false;
468 node->symbol.definition = false;
469 node->symbol.cpp_implicit_alias = false;
470 node->symbol.alias = false;
471 node->symbol.weakref = false;
472 if (!node->symbol.in_other_partition)
473 node->local.local = false;
474 cgraph_node_remove_callees (node);
475 ipa_remove_all_references (&node->symbol.ref_list);
476 changed = true;
479 else
480 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
481 || in_lto_p || DECL_RESULT (node->symbol.decl));
484 /* Inline clones might be kept around so their materializing allows further
485 cloning. If the function the clone is inlined into is removed, we need
486 to turn it into normal cone. */
487 FOR_EACH_FUNCTION (node)
489 if (node->global.inlined_to
490 && !node->callers)
492 gcc_assert (node->clones);
493 node->global.inlined_to = NULL;
494 update_inlined_to_pointer (node, node);
496 node->symbol.aux = NULL;
499 /* Remove unreachable variables. */
500 if (file)
501 fprintf (file, "\nReclaiming variables:");
502 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
504 vnext = varpool_next_variable (vnode);
505 if (!vnode->symbol.aux
506 /* For can_refer_decl_in_current_unit_p we want to track for
507 all external variables if they are defined in other partition
508 or not. */
509 && (!flag_ltrans || !DECL_EXTERNAL (vnode->symbol.decl)))
511 if (file)
512 fprintf (file, " %s", varpool_node_name (vnode));
513 varpool_remove_node (vnode);
514 changed = true;
516 else if (!pointer_set_contains (reachable, vnode))
518 tree init;
519 if (vnode->symbol.definition)
521 if (file)
522 fprintf (file, " %s", varpool_node_name (vnode));
523 changed = true;
525 vnode->symbol.definition = false;
526 vnode->symbol.analyzed = false;
527 vnode->symbol.aux = NULL;
529 /* Keep body if it may be useful for constant folding. */
530 if ((init = ctor_for_folding (vnode->symbol.decl)) == error_mark_node)
531 varpool_remove_initializer (vnode);
532 else
533 DECL_INITIAL (vnode->symbol.decl) = init;
534 ipa_remove_all_references (&vnode->symbol.ref_list);
536 else
537 vnode->symbol.aux = NULL;
540 pointer_set_destroy (reachable);
541 pointer_set_destroy (body_needed_for_clonning);
542 pointer_set_destroy (reachable_call_targets);
544 /* Now update address_taken flags and try to promote functions to be local. */
545 if (file)
546 fprintf (file, "\nClearing address taken flags:");
547 FOR_EACH_DEFINED_FUNCTION (node)
548 if (node->symbol.address_taken
549 && !node->symbol.used_from_other_partition)
551 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
553 if (file)
554 fprintf (file, " %s", cgraph_node_name (node));
555 node->symbol.address_taken = false;
556 changed = true;
557 if (cgraph_local_node_p (node))
559 node->local.local = true;
560 if (file)
561 fprintf (file, " (local)");
565 if (file)
566 fprintf (file, "\n");
568 #ifdef ENABLE_CHECKING
569 verify_symtab ();
570 #endif
572 /* If we removed something, perhaps profile could be improved. */
573 if (changed && optimize && inline_edge_summary_vec.exists ())
574 FOR_EACH_DEFINED_FUNCTION (node)
575 ipa_propagate_frequency (node);
577 timevar_pop (TV_IPA_UNREACHABLE);
578 return changed;
581 /* Discover variables that have no longer address taken or that are read only
582 and update their flags.
584 FIXME: This can not be done in between gimplify and omp_expand since
585 readonly flag plays role on what is shared and what is not. Currently we do
586 this transformation as part of whole program visibility and re-do at
587 ipa-reference pass (to take into account clonning), but it would
588 make sense to do it before early optimizations. */
590 void
591 ipa_discover_readonly_nonaddressable_vars (void)
593 struct varpool_node *vnode;
594 if (dump_file)
595 fprintf (dump_file, "Clearing variable flags:");
596 FOR_EACH_VARIABLE (vnode)
597 if (vnode->symbol.definition && varpool_all_refs_explicit_p (vnode)
598 && (TREE_ADDRESSABLE (vnode->symbol.decl)
599 || !TREE_READONLY (vnode->symbol.decl)))
601 bool written = false;
602 bool address_taken = false;
603 int i;
604 struct ipa_ref *ref;
605 for (i = 0; ipa_ref_list_referring_iterate (&vnode->symbol.ref_list,
606 i, ref)
607 && (!written || !address_taken); i++)
608 switch (ref->use)
610 case IPA_REF_ADDR:
611 address_taken = true;
612 break;
613 case IPA_REF_LOAD:
614 break;
615 case IPA_REF_STORE:
616 written = true;
617 break;
619 if (TREE_ADDRESSABLE (vnode->symbol.decl) && !address_taken)
621 if (dump_file)
622 fprintf (dump_file, " %s (addressable)", varpool_node_name (vnode));
623 TREE_ADDRESSABLE (vnode->symbol.decl) = 0;
625 if (!TREE_READONLY (vnode->symbol.decl) && !address_taken && !written
626 /* Making variable in explicit section readonly can cause section
627 type conflict.
628 See e.g. gcc.c-torture/compile/pr23237.c */
629 && DECL_SECTION_NAME (vnode->symbol.decl) == NULL)
631 if (dump_file)
632 fprintf (dump_file, " %s (read-only)", varpool_node_name (vnode));
633 TREE_READONLY (vnode->symbol.decl) = 1;
636 if (dump_file)
637 fprintf (dump_file, "\n");
640 /* Return true when there is a reference to node and it is not vtable. */
641 static bool
642 address_taken_from_non_vtable_p (symtab_node node)
644 int i;
645 struct ipa_ref *ref;
646 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list,
647 i, ref); i++)
648 if (ref->use == IPA_REF_ADDR)
650 struct varpool_node *node;
651 if (is_a <cgraph_node> (ref->referring))
652 return true;
653 node = ipa_ref_referring_varpool_node (ref);
654 if (!DECL_VIRTUAL_P (node->symbol.decl))
655 return true;
657 return false;
660 /* A helper for comdat_can_be_unshared_p. */
662 static bool
663 comdat_can_be_unshared_p_1 (symtab_node node)
665 /* When address is taken, we don't know if equality comparison won't
666 break eventually. Exception are virutal functions, C++
667 constructors/destructors and vtables, where this is not possible by
668 language standard. */
669 if (!DECL_VIRTUAL_P (node->symbol.decl)
670 && (TREE_CODE (node->symbol.decl) != FUNCTION_DECL
671 || (!DECL_CXX_CONSTRUCTOR_P (node->symbol.decl)
672 && !DECL_CXX_DESTRUCTOR_P (node->symbol.decl)))
673 && address_taken_from_non_vtable_p (node))
674 return false;
676 /* If the symbol is used in some weird way, better to not touch it. */
677 if (node->symbol.force_output)
678 return false;
680 /* Explicit instantiations needs to be output when possibly
681 used externally. */
682 if (node->symbol.forced_by_abi
683 && TREE_PUBLIC (node->symbol.decl)
684 && (node->symbol.resolution != LDPR_PREVAILING_DEF_IRONLY
685 && !flag_whole_program))
686 return false;
688 /* Non-readonly and volatile variables can not be duplicated. */
689 if (is_a <varpool_node> (node)
690 && (!TREE_READONLY (node->symbol.decl)
691 || TREE_THIS_VOLATILE (node->symbol.decl)))
692 return false;
693 return true;
696 /* COMDAT functions must be shared only if they have address taken,
697 otherwise we can produce our own private implementation with
698 -fwhole-program.
699 Return true when turning COMDAT functoin static can not lead to wrong
700 code when the resulting object links with a library defining same COMDAT.
702 Virtual functions do have their addresses taken from the vtables,
703 but in C++ there is no way to compare their addresses for equality. */
705 static bool
706 comdat_can_be_unshared_p (symtab_node node)
708 if (!comdat_can_be_unshared_p_1 (node))
709 return false;
710 if (node->symbol.same_comdat_group)
712 symtab_node next;
714 /* If more than one function is in the same COMDAT group, it must
715 be shared even if just one function in the comdat group has
716 address taken. */
717 for (next = node->symbol.same_comdat_group;
718 next != node; next = next->symbol.same_comdat_group)
719 if (!comdat_can_be_unshared_p_1 (next))
720 return false;
722 return true;
725 /* Return true when function NODE should be considered externally visible. */
727 static bool
728 cgraph_externally_visible_p (struct cgraph_node *node,
729 bool whole_program)
731 if (!node->symbol.definition)
732 return false;
733 if (!TREE_PUBLIC (node->symbol.decl)
734 || DECL_EXTERNAL (node->symbol.decl))
735 return false;
737 /* Do not try to localize built-in functions yet. One of problems is that we
738 end up mangling their asm for WHOPR that makes it impossible to call them
739 using the implicit built-in declarations anymore. Similarly this enables
740 us to remove them as unreachable before actual calls may appear during
741 expansion or folding. */
742 if (DECL_BUILT_IN (node->symbol.decl))
743 return true;
745 /* If linker counts on us, we must preserve the function. */
746 if (symtab_used_from_object_file_p ((symtab_node) node))
747 return true;
748 if (DECL_PRESERVE_P (node->symbol.decl))
749 return true;
750 if (lookup_attribute ("externally_visible",
751 DECL_ATTRIBUTES (node->symbol.decl)))
752 return true;
753 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
754 && lookup_attribute ("dllexport",
755 DECL_ATTRIBUTES (node->symbol.decl)))
756 return true;
757 if (node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
758 return false;
759 /* When doing LTO or whole program, we can bring COMDAT functoins static.
760 This improves code quality and we know we will duplicate them at most twice
761 (in the case that we are not using plugin and link with object file
762 implementing same COMDAT) */
763 if ((in_lto_p || whole_program)
764 && DECL_COMDAT (node->symbol.decl)
765 && comdat_can_be_unshared_p ((symtab_node) node))
766 return false;
768 /* When doing link time optimizations, hidden symbols become local. */
769 if (in_lto_p
770 && (DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_HIDDEN
771 || DECL_VISIBILITY (node->symbol.decl) == VISIBILITY_INTERNAL)
772 /* Be sure that node is defined in IR file, not in other object
773 file. In that case we don't set used_from_other_object_file. */
774 && node->symbol.definition)
776 else if (!whole_program)
777 return true;
779 if (MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
780 return true;
782 return false;
785 /* Return true when variable VNODE should be considered externally visible. */
787 bool
788 varpool_externally_visible_p (struct varpool_node *vnode)
790 if (DECL_EXTERNAL (vnode->symbol.decl))
791 return true;
793 if (!TREE_PUBLIC (vnode->symbol.decl))
794 return false;
796 /* If linker counts on us, we must preserve the function. */
797 if (symtab_used_from_object_file_p ((symtab_node) vnode))
798 return true;
800 if (DECL_HARD_REGISTER (vnode->symbol.decl))
801 return true;
802 if (DECL_PRESERVE_P (vnode->symbol.decl))
803 return true;
804 if (lookup_attribute ("externally_visible",
805 DECL_ATTRIBUTES (vnode->symbol.decl)))
806 return true;
807 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
808 && lookup_attribute ("dllexport",
809 DECL_ATTRIBUTES (vnode->symbol.decl)))
810 return true;
812 /* See if we have linker information about symbol not being used or
813 if we need to make guess based on the declaration.
815 Even if the linker clams the symbol is unused, never bring internal
816 symbols that are declared by user as used or externally visible.
817 This is needed for i.e. references from asm statements. */
818 if (symtab_used_from_object_file_p ((symtab_node) vnode))
819 return true;
820 if (vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY)
821 return false;
823 /* As a special case, the COMDAT virtual tables can be unshared.
824 In LTO mode turn vtables into static variables. The variable is readonly,
825 so this does not enable more optimization, but referring static var
826 is faster for dynamic linking. Also this match logic hidding vtables
827 from LTO symbol tables. */
828 if ((in_lto_p || flag_whole_program)
829 && DECL_COMDAT (vnode->symbol.decl)
830 && comdat_can_be_unshared_p ((symtab_node) vnode))
831 return false;
833 /* When doing link time optimizations, hidden symbols become local. */
834 if (in_lto_p
835 && (DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_HIDDEN
836 || DECL_VISIBILITY (vnode->symbol.decl) == VISIBILITY_INTERNAL)
837 /* Be sure that node is defined in IR file, not in other object
838 file. In that case we don't set used_from_other_object_file. */
839 && vnode->symbol.definition)
841 else if (!flag_whole_program)
842 return true;
844 /* Do not attempt to privatize COMDATS by default.
845 This would break linking with C++ libraries sharing
846 inline definitions.
848 FIXME: We can do so for readonly vars with no address taken and
849 possibly also for vtables since no direct pointer comparsion is done.
850 It might be interesting to do so to reduce linking overhead. */
851 if (DECL_COMDAT (vnode->symbol.decl) || DECL_WEAK (vnode->symbol.decl))
852 return true;
853 return false;
856 /* Return true if reference to NODE can be replaced by a local alias.
857 Local aliases save dynamic linking overhead and enable more optimizations.
860 bool
861 can_replace_by_local_alias (symtab_node node)
863 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
864 && !symtab_can_be_discarded (node));
867 /* Mark visibility of all functions.
869 A local function is one whose calls can occur only in the current
870 compilation unit and all its calls are explicit, so we can change
871 its calling convention. We simply mark all static functions whose
872 address is not taken as local.
874 We also change the TREE_PUBLIC flag of all declarations that are public
875 in language point of view but we want to overwrite this default
876 via visibilities for the backend point of view. */
878 static unsigned int
879 function_and_variable_visibility (bool whole_program)
881 struct cgraph_node *node;
882 struct varpool_node *vnode;
884 /* All aliases should be procssed at this point. */
885 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
887 FOR_EACH_FUNCTION (node)
889 int flags = flags_from_decl_or_type (node->symbol.decl);
891 /* Optimize away PURE and CONST constructors and destructors. */
892 if (optimize
893 && (flags & (ECF_CONST | ECF_PURE))
894 && !(flags & ECF_LOOPING_CONST_OR_PURE))
896 DECL_STATIC_CONSTRUCTOR (node->symbol.decl) = 0;
897 DECL_STATIC_DESTRUCTOR (node->symbol.decl) = 0;
900 /* Frontends and alias code marks nodes as needed before parsing is finished.
901 We may end up marking as node external nodes where this flag is meaningless
902 strip it. */
903 if (DECL_EXTERNAL (node->symbol.decl) || !node->symbol.definition)
905 node->symbol.force_output = 0;
906 node->symbol.forced_by_abi = 0;
909 /* C++ FE on lack of COMDAT support create local COMDAT functions
910 (that ought to be shared but can not due to object format
911 limitations). It is necessary to keep the flag to make rest of C++ FE
912 happy. Clear the flag here to avoid confusion in middle-end. */
913 if (DECL_COMDAT (node->symbol.decl) && !TREE_PUBLIC (node->symbol.decl))
914 DECL_COMDAT (node->symbol.decl) = 0;
916 /* For external decls stop tracking same_comdat_group. It doesn't matter
917 what comdat group they are in when they won't be emitted in this TU. */
918 if (node->symbol.same_comdat_group && DECL_EXTERNAL (node->symbol.decl))
920 #ifdef ENABLE_CHECKING
921 symtab_node n;
923 for (n = node->symbol.same_comdat_group;
924 n != (symtab_node)node;
925 n = n->symbol.same_comdat_group)
926 /* If at least one of same comdat group functions is external,
927 all of them have to be, otherwise it is a front-end bug. */
928 gcc_assert (DECL_EXTERNAL (n->symbol.decl));
929 #endif
930 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
932 gcc_assert ((!DECL_WEAK (node->symbol.decl)
933 && !DECL_COMDAT (node->symbol.decl))
934 || TREE_PUBLIC (node->symbol.decl)
935 || node->symbol.weakref
936 || DECL_EXTERNAL (node->symbol.decl));
937 if (cgraph_externally_visible_p (node, whole_program))
939 gcc_assert (!node->global.inlined_to);
940 node->symbol.externally_visible = true;
942 else
944 node->symbol.externally_visible = false;
945 node->symbol.forced_by_abi = false;
947 if (!node->symbol.externally_visible
948 && node->symbol.definition && !node->symbol.weakref
949 && !DECL_EXTERNAL (node->symbol.decl))
951 gcc_assert (whole_program || in_lto_p
952 || !TREE_PUBLIC (node->symbol.decl));
953 node->symbol.unique_name = ((node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
954 || node->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
955 && TREE_PUBLIC (node->symbol.decl));
956 symtab_make_decl_local (node->symbol.decl);
957 node->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
958 if (node->symbol.same_comdat_group)
959 /* cgraph_externally_visible_p has already checked all other nodes
960 in the group and they will all be made local. We need to
961 dissolve the group at once so that the predicate does not
962 segfault though. */
963 symtab_dissolve_same_comdat_group_list ((symtab_node) node);
966 if (node->thunk.thunk_p
967 && TREE_PUBLIC (node->symbol.decl))
969 struct cgraph_node *decl_node = node;
971 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
973 /* Thunks have the same visibility as function they are attached to.
974 Make sure the C++ front end set this up properly. */
975 if (DECL_ONE_ONLY (decl_node->symbol.decl))
977 gcc_checking_assert (DECL_COMDAT (node->symbol.decl)
978 == DECL_COMDAT (decl_node->symbol.decl));
979 gcc_checking_assert (DECL_COMDAT_GROUP (node->symbol.decl)
980 == DECL_COMDAT_GROUP (decl_node->symbol.decl));
981 gcc_checking_assert (node->symbol.same_comdat_group);
983 if (DECL_EXTERNAL (decl_node->symbol.decl))
984 DECL_EXTERNAL (node->symbol.decl) = 1;
987 FOR_EACH_DEFINED_FUNCTION (node)
989 node->local.local |= cgraph_local_node_p (node);
991 /* If we know that function can not be overwritten by a different semantics
992 and moreover its section can not be discarded, replace all direct calls
993 by calls to an nonoverwritable alias. This make dynamic linking
994 cheaper and enable more optimization.
996 TODO: We can also update virtual tables. */
997 if (node->callers && can_replace_by_local_alias ((symtab_node)node))
999 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias ((symtab_node) node));
1001 if (alias && alias != node)
1003 while (node->callers)
1005 struct cgraph_edge *e = node->callers;
1007 cgraph_redirect_edge_callee (e, alias);
1008 if (gimple_has_body_p (e->caller->symbol.decl))
1010 push_cfun (DECL_STRUCT_FUNCTION (e->caller->symbol.decl));
1011 cgraph_redirect_edge_call_stmt_to_callee (e);
1012 pop_cfun ();
1018 FOR_EACH_VARIABLE (vnode)
1020 /* weak flag makes no sense on local variables. */
1021 gcc_assert (!DECL_WEAK (vnode->symbol.decl)
1022 || vnode->symbol.weakref
1023 || TREE_PUBLIC (vnode->symbol.decl)
1024 || DECL_EXTERNAL (vnode->symbol.decl));
1025 /* In several cases declarations can not be common:
1027 - when declaration has initializer
1028 - when it is in weak
1029 - when it has specific section
1030 - when it resides in non-generic address space.
1031 - if declaration is local, it will get into .local common section
1032 so common flag is not needed. Frontends still produce these in
1033 certain cases, such as for:
1035 static int a __attribute__ ((common))
1037 Canonicalize things here and clear the redundant flag. */
1038 if (DECL_COMMON (vnode->symbol.decl)
1039 && (!(TREE_PUBLIC (vnode->symbol.decl)
1040 || DECL_EXTERNAL (vnode->symbol.decl))
1041 || (DECL_INITIAL (vnode->symbol.decl)
1042 && DECL_INITIAL (vnode->symbol.decl) != error_mark_node)
1043 || DECL_WEAK (vnode->symbol.decl)
1044 || DECL_SECTION_NAME (vnode->symbol.decl) != NULL
1045 || ! (ADDR_SPACE_GENERIC_P
1046 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->symbol.decl))))))
1047 DECL_COMMON (vnode->symbol.decl) = 0;
1049 FOR_EACH_DEFINED_VARIABLE (vnode)
1051 if (!vnode->symbol.definition)
1052 continue;
1053 if (varpool_externally_visible_p (vnode))
1054 vnode->symbol.externally_visible = true;
1055 else
1057 vnode->symbol.externally_visible = false;
1058 vnode->symbol.forced_by_abi = false;
1060 if (!vnode->symbol.externally_visible
1061 && !vnode->symbol.weakref)
1063 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->symbol.decl));
1064 vnode->symbol.unique_name = ((vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY
1065 || vnode->symbol.resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1066 && TREE_PUBLIC (vnode->symbol.decl));
1067 symtab_make_decl_local (vnode->symbol.decl);
1068 if (vnode->symbol.same_comdat_group)
1069 symtab_dissolve_same_comdat_group_list ((symtab_node) vnode);
1070 vnode->symbol.resolution = LDPR_PREVAILING_DEF_IRONLY;
1074 if (dump_file)
1076 fprintf (dump_file, "\nMarking local functions:");
1077 FOR_EACH_DEFINED_FUNCTION (node)
1078 if (node->local.local)
1079 fprintf (dump_file, " %s", cgraph_node_name (node));
1080 fprintf (dump_file, "\n\n");
1081 fprintf (dump_file, "\nMarking externally visible functions:");
1082 FOR_EACH_DEFINED_FUNCTION (node)
1083 if (node->symbol.externally_visible)
1084 fprintf (dump_file, " %s", cgraph_node_name (node));
1085 fprintf (dump_file, "\n\n");
1086 fprintf (dump_file, "\nMarking externally visible variables:");
1087 FOR_EACH_DEFINED_VARIABLE (vnode)
1088 if (vnode->symbol.externally_visible)
1089 fprintf (dump_file, " %s", varpool_node_name (vnode));
1090 fprintf (dump_file, "\n\n");
1092 cgraph_function_flags_ready = true;
1093 return 0;
1096 /* Local function pass handling visibilities. This happens before LTO streaming
1097 so in particular -fwhole-program should be ignored at this level. */
1099 static unsigned int
1100 local_function_and_variable_visibility (void)
1102 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1105 namespace {
1107 const pass_data pass_data_ipa_function_and_variable_visibility =
1109 SIMPLE_IPA_PASS, /* type */
1110 "visibility", /* name */
1111 OPTGROUP_NONE, /* optinfo_flags */
1112 false, /* has_gate */
1113 true, /* has_execute */
1114 TV_CGRAPHOPT, /* tv_id */
1115 0, /* properties_required */
1116 0, /* properties_provided */
1117 0, /* properties_destroyed */
1118 0, /* todo_flags_start */
1119 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1122 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1124 public:
1125 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1126 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1127 ctxt)
1130 /* opt_pass methods: */
1131 unsigned int execute () {
1132 return local_function_and_variable_visibility ();
1135 }; // class pass_ipa_function_and_variable_visibility
1137 } // anon namespace
1139 simple_ipa_opt_pass *
1140 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1142 return new pass_ipa_function_and_variable_visibility (ctxt);
1145 /* Free inline summary. */
1147 static unsigned
1148 free_inline_summary (void)
1150 inline_free_summary ();
1151 return 0;
1154 namespace {
1156 const pass_data pass_data_ipa_free_inline_summary =
1158 SIMPLE_IPA_PASS, /* type */
1159 "*free_inline_summary", /* name */
1160 OPTGROUP_NONE, /* optinfo_flags */
1161 false, /* has_gate */
1162 true, /* has_execute */
1163 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1164 0, /* properties_required */
1165 0, /* properties_provided */
1166 0, /* properties_destroyed */
1167 0, /* todo_flags_start */
1168 0, /* todo_flags_finish */
1171 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1173 public:
1174 pass_ipa_free_inline_summary (gcc::context *ctxt)
1175 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1178 /* opt_pass methods: */
1179 unsigned int execute () { return free_inline_summary (); }
1181 }; // class pass_ipa_free_inline_summary
1183 } // anon namespace
1185 simple_ipa_opt_pass *
1186 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1188 return new pass_ipa_free_inline_summary (ctxt);
1191 /* Do not re-run on ltrans stage. */
1193 static bool
1194 gate_whole_program_function_and_variable_visibility (void)
1196 return !flag_ltrans;
1199 /* Bring functionss local at LTO time with -fwhole-program. */
1201 static unsigned int
1202 whole_program_function_and_variable_visibility (void)
1204 function_and_variable_visibility (flag_whole_program);
1205 if (optimize)
1206 ipa_discover_readonly_nonaddressable_vars ();
1207 return 0;
1210 namespace {
1212 const pass_data pass_data_ipa_whole_program_visibility =
1214 IPA_PASS, /* type */
1215 "whole-program", /* name */
1216 OPTGROUP_NONE, /* optinfo_flags */
1217 true, /* has_gate */
1218 true, /* has_execute */
1219 TV_CGRAPHOPT, /* tv_id */
1220 0, /* properties_required */
1221 0, /* properties_provided */
1222 0, /* properties_destroyed */
1223 0, /* todo_flags_start */
1224 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1227 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1229 public:
1230 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1231 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1232 NULL, /* generate_summary */
1233 NULL, /* write_summary */
1234 NULL, /* read_summary */
1235 NULL, /* write_optimization_summary */
1236 NULL, /* read_optimization_summary */
1237 NULL, /* stmt_fixup */
1238 0, /* function_transform_todo_flags_start */
1239 NULL, /* function_transform */
1240 NULL) /* variable_transform */
1243 /* opt_pass methods: */
1244 bool gate () {
1245 return gate_whole_program_function_and_variable_visibility ();
1247 unsigned int execute () {
1248 return whole_program_function_and_variable_visibility ();
1251 }; // class pass_ipa_whole_program_visibility
1253 } // anon namespace
1255 ipa_opt_pass_d *
1256 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1258 return new pass_ipa_whole_program_visibility (ctxt);
1261 /* Generate and emit a static constructor or destructor. WHICH must
1262 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1263 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1264 initialization priority for this constructor or destructor.
1266 FINAL specify whether the externally visible name for collect2 should
1267 be produced. */
1269 static void
1270 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1272 static int counter = 0;
1273 char which_buf[16];
1274 tree decl, name, resdecl;
1276 /* The priority is encoded in the constructor or destructor name.
1277 collect2 will sort the names and arrange that they are called at
1278 program startup. */
1279 if (final)
1280 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1281 else
1282 /* Proudce sane name but one not recognizable by collect2, just for the
1283 case we fail to inline the function. */
1284 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1285 name = get_file_function_name (which_buf);
1287 decl = build_decl (input_location, FUNCTION_DECL, name,
1288 build_function_type_list (void_type_node, NULL_TREE));
1289 current_function_decl = decl;
1291 resdecl = build_decl (input_location,
1292 RESULT_DECL, NULL_TREE, void_type_node);
1293 DECL_ARTIFICIAL (resdecl) = 1;
1294 DECL_RESULT (decl) = resdecl;
1295 DECL_CONTEXT (resdecl) = decl;
1297 allocate_struct_function (decl, false);
1299 TREE_STATIC (decl) = 1;
1300 TREE_USED (decl) = 1;
1301 DECL_ARTIFICIAL (decl) = 1;
1302 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1303 DECL_SAVED_TREE (decl) = body;
1304 if (!targetm.have_ctors_dtors && final)
1306 TREE_PUBLIC (decl) = 1;
1307 DECL_PRESERVE_P (decl) = 1;
1309 DECL_UNINLINABLE (decl) = 1;
1311 DECL_INITIAL (decl) = make_node (BLOCK);
1312 TREE_USED (DECL_INITIAL (decl)) = 1;
1314 DECL_SOURCE_LOCATION (decl) = input_location;
1315 cfun->function_end_locus = input_location;
1317 switch (which)
1319 case 'I':
1320 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1321 decl_init_priority_insert (decl, priority);
1322 break;
1323 case 'D':
1324 DECL_STATIC_DESTRUCTOR (decl) = 1;
1325 decl_fini_priority_insert (decl, priority);
1326 break;
1327 default:
1328 gcc_unreachable ();
1331 gimplify_function_tree (decl);
1333 cgraph_add_new_function (decl, false);
1335 set_cfun (NULL);
1336 current_function_decl = NULL;
1339 /* Generate and emit a static constructor or destructor. WHICH must
1340 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1341 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1342 initialization priority for this constructor or destructor. */
1344 void
1345 cgraph_build_static_cdtor (char which, tree body, int priority)
1347 cgraph_build_static_cdtor_1 (which, body, priority, false);
1350 /* A vector of FUNCTION_DECLs declared as static constructors. */
1351 static vec<tree> static_ctors;
1352 /* A vector of FUNCTION_DECLs declared as static destructors. */
1353 static vec<tree> static_dtors;
1355 /* When target does not have ctors and dtors, we call all constructor
1356 and destructor by special initialization/destruction function
1357 recognized by collect2.
1359 When we are going to build this function, collect all constructors and
1360 destructors and turn them into normal functions. */
1362 static void
1363 record_cdtor_fn (struct cgraph_node *node)
1365 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl))
1366 static_ctors.safe_push (node->symbol.decl);
1367 if (DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1368 static_dtors.safe_push (node->symbol.decl);
1369 node = cgraph_get_node (node->symbol.decl);
1370 DECL_DISREGARD_INLINE_LIMITS (node->symbol.decl) = 1;
1373 /* Define global constructors/destructor functions for the CDTORS, of
1374 which they are LEN. The CDTORS are sorted by initialization
1375 priority. If CTOR_P is true, these are constructors; otherwise,
1376 they are destructors. */
1378 static void
1379 build_cdtor (bool ctor_p, vec<tree> cdtors)
1381 size_t i,j;
1382 size_t len = cdtors.length ();
1384 i = 0;
1385 while (i < len)
1387 tree body;
1388 tree fn;
1389 priority_type priority;
1391 priority = 0;
1392 body = NULL_TREE;
1393 j = i;
1396 priority_type p;
1397 fn = cdtors[j];
1398 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1399 if (j == i)
1400 priority = p;
1401 else if (p != priority)
1402 break;
1403 j++;
1405 while (j < len);
1407 /* When there is only one cdtor and target supports them, do nothing. */
1408 if (j == i + 1
1409 && targetm.have_ctors_dtors)
1411 i++;
1412 continue;
1414 /* Find the next batch of constructors/destructors with the same
1415 initialization priority. */
1416 for (;i < j; i++)
1418 tree call;
1419 fn = cdtors[i];
1420 call = build_call_expr (fn, 0);
1421 if (ctor_p)
1422 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1423 else
1424 DECL_STATIC_DESTRUCTOR (fn) = 0;
1425 /* We do not want to optimize away pure/const calls here.
1426 When optimizing, these should be already removed, when not
1427 optimizing, we want user to be able to breakpoint in them. */
1428 TREE_SIDE_EFFECTS (call) = 1;
1429 append_to_statement_list (call, &body);
1431 gcc_assert (body != NULL_TREE);
1432 /* Generate a function to call all the function of like
1433 priority. */
1434 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1438 /* Comparison function for qsort. P1 and P2 are actually of type
1439 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1440 used to determine the sort order. */
1442 static int
1443 compare_ctor (const void *p1, const void *p2)
1445 tree f1;
1446 tree f2;
1447 int priority1;
1448 int priority2;
1450 f1 = *(const tree *)p1;
1451 f2 = *(const tree *)p2;
1452 priority1 = DECL_INIT_PRIORITY (f1);
1453 priority2 = DECL_INIT_PRIORITY (f2);
1455 if (priority1 < priority2)
1456 return -1;
1457 else if (priority1 > priority2)
1458 return 1;
1459 else
1460 /* Ensure a stable sort. Constructors are executed in backwarding
1461 order to make LTO initialize braries first. */
1462 return DECL_UID (f2) - DECL_UID (f1);
1465 /* Comparison function for qsort. P1 and P2 are actually of type
1466 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1467 used to determine the sort order. */
1469 static int
1470 compare_dtor (const void *p1, const void *p2)
1472 tree f1;
1473 tree f2;
1474 int priority1;
1475 int priority2;
1477 f1 = *(const tree *)p1;
1478 f2 = *(const tree *)p2;
1479 priority1 = DECL_FINI_PRIORITY (f1);
1480 priority2 = DECL_FINI_PRIORITY (f2);
1482 if (priority1 < priority2)
1483 return -1;
1484 else if (priority1 > priority2)
1485 return 1;
1486 else
1487 /* Ensure a stable sort. */
1488 return DECL_UID (f1) - DECL_UID (f2);
1491 /* Generate functions to call static constructors and destructors
1492 for targets that do not support .ctors/.dtors sections. These
1493 functions have magic names which are detected by collect2. */
1495 static void
1496 build_cdtor_fns (void)
1498 if (!static_ctors.is_empty ())
1500 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1501 static_ctors.qsort (compare_ctor);
1502 build_cdtor (/*ctor_p=*/true, static_ctors);
1505 if (!static_dtors.is_empty ())
1507 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1508 static_dtors.qsort (compare_dtor);
1509 build_cdtor (/*ctor_p=*/false, static_dtors);
1513 /* Look for constructors and destructors and produce function calling them.
1514 This is needed for targets not supporting ctors or dtors, but we perform the
1515 transformation also at linktime to merge possibly numerous
1516 constructors/destructors into single function to improve code locality and
1517 reduce size. */
1519 static unsigned int
1520 ipa_cdtor_merge (void)
1522 struct cgraph_node *node;
1523 FOR_EACH_DEFINED_FUNCTION (node)
1524 if (DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1525 || DECL_STATIC_DESTRUCTOR (node->symbol.decl))
1526 record_cdtor_fn (node);
1527 build_cdtor_fns ();
1528 static_ctors.release ();
1529 static_dtors.release ();
1530 return 0;
1533 /* Perform the pass when we have no ctors/dtors support
1534 or at LTO time to merge multiple constructors into single
1535 function. */
1537 static bool
1538 gate_ipa_cdtor_merge (void)
1540 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1543 namespace {
1545 const pass_data pass_data_ipa_cdtor_merge =
1547 IPA_PASS, /* type */
1548 "cdtor", /* name */
1549 OPTGROUP_NONE, /* optinfo_flags */
1550 true, /* has_gate */
1551 true, /* has_execute */
1552 TV_CGRAPHOPT, /* tv_id */
1553 0, /* properties_required */
1554 0, /* properties_provided */
1555 0, /* properties_destroyed */
1556 0, /* todo_flags_start */
1557 0, /* todo_flags_finish */
1560 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1562 public:
1563 pass_ipa_cdtor_merge (gcc::context *ctxt)
1564 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1565 NULL, /* generate_summary */
1566 NULL, /* write_summary */
1567 NULL, /* read_summary */
1568 NULL, /* write_optimization_summary */
1569 NULL, /* read_optimization_summary */
1570 NULL, /* stmt_fixup */
1571 0, /* function_transform_todo_flags_start */
1572 NULL, /* function_transform */
1573 NULL) /* variable_transform */
1576 /* opt_pass methods: */
1577 bool gate () { return gate_ipa_cdtor_merge (); }
1578 unsigned int execute () { return ipa_cdtor_merge (); }
1580 }; // class pass_ipa_cdtor_merge
1582 } // anon namespace
1584 ipa_opt_pass_d *
1585 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1587 return new pass_ipa_cdtor_merge (ctxt);