* gcc.dg/gomp/openmp-simd-1.c: Cleanup original tree dump.
[official-gcc.git] / gcc / ipa.c
blob70b1d01873fd9fa1a541ec5906a825018756ac27
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 /* When we see constructor of external variable, keep referred nodes in the
432 boundary. This will also hold initializers of the external vars NODE
433 refers to. */
434 varpool_node *vnode = dyn_cast <varpool_node> (node);
435 if (vnode
436 && DECL_EXTERNAL (node->decl)
437 && !vnode->alias
438 && in_boundary_p)
440 struct ipa_ref *ref;
441 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
442 enqueue_node (ref->referred, &first, reachable);
446 /* Remove unreachable functions. */
447 for (node = cgraph_first_function (); node; node = next)
449 next = cgraph_next_function (node);
451 /* If node is not needed at all, remove it. */
452 if (!node->aux)
454 if (file)
455 fprintf (file, " %s", node->name ());
456 cgraph_remove_node (node);
457 changed = true;
459 /* If node is unreachable, remove its body. */
460 else if (!pointer_set_contains (reachable, node))
462 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
463 cgraph_release_function_body (node);
464 else if (!node->clone_of)
465 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
466 if (node->definition)
468 if (file)
469 fprintf (file, " %s", node->name ());
470 node->analyzed = false;
471 node->definition = false;
472 node->cpp_implicit_alias = false;
473 node->alias = false;
474 node->weakref = false;
475 if (!node->in_other_partition)
476 node->local.local = false;
477 cgraph_node_remove_callees (node);
478 ipa_remove_all_references (&node->ref_list);
479 changed = true;
482 else
483 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
484 || in_lto_p || DECL_RESULT (node->decl));
487 /* Inline clones might be kept around so their materializing allows further
488 cloning. If the function the clone is inlined into is removed, we need
489 to turn it into normal cone. */
490 FOR_EACH_FUNCTION (node)
492 if (node->global.inlined_to
493 && !node->callers)
495 gcc_assert (node->clones);
496 node->global.inlined_to = NULL;
497 update_inlined_to_pointer (node, node);
499 node->aux = NULL;
502 /* Remove unreachable variables. */
503 if (file)
504 fprintf (file, "\nReclaiming variables:");
505 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
507 vnext = varpool_next_variable (vnode);
508 if (!vnode->aux
509 /* For can_refer_decl_in_current_unit_p we want to track for
510 all external variables if they are defined in other partition
511 or not. */
512 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
514 if (file)
515 fprintf (file, " %s", vnode->name ());
516 varpool_remove_node (vnode);
517 changed = true;
519 else if (!pointer_set_contains (reachable, vnode))
521 tree init;
522 if (vnode->definition)
524 if (file)
525 fprintf (file, " %s", vnode->name ());
526 changed = true;
528 vnode->definition = false;
529 vnode->analyzed = false;
530 vnode->aux = NULL;
532 /* Keep body if it may be useful for constant folding. */
533 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
534 varpool_remove_initializer (vnode);
535 else
536 DECL_INITIAL (vnode->decl) = init;
537 ipa_remove_all_references (&vnode->ref_list);
539 else
540 vnode->aux = NULL;
543 pointer_set_destroy (reachable);
544 pointer_set_destroy (body_needed_for_clonning);
545 pointer_set_destroy (reachable_call_targets);
547 /* Now update address_taken flags and try to promote functions to be local. */
548 if (file)
549 fprintf (file, "\nClearing address taken flags:");
550 FOR_EACH_DEFINED_FUNCTION (node)
551 if (node->address_taken
552 && !node->used_from_other_partition)
554 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
556 if (file)
557 fprintf (file, " %s", node->name ());
558 node->address_taken = false;
559 changed = true;
560 if (cgraph_local_node_p (node))
562 node->local.local = true;
563 if (file)
564 fprintf (file, " (local)");
568 if (file)
569 fprintf (file, "\n");
571 #ifdef ENABLE_CHECKING
572 verify_symtab ();
573 #endif
575 /* If we removed something, perhaps profile could be improved. */
576 if (changed && optimize && inline_edge_summary_vec.exists ())
577 FOR_EACH_DEFINED_FUNCTION (node)
578 ipa_propagate_frequency (node);
580 timevar_pop (TV_IPA_UNREACHABLE);
581 return changed;
584 /* Discover variables that have no longer address taken or that are read only
585 and update their flags.
587 FIXME: This can not be done in between gimplify and omp_expand since
588 readonly flag plays role on what is shared and what is not. Currently we do
589 this transformation as part of whole program visibility and re-do at
590 ipa-reference pass (to take into account clonning), but it would
591 make sense to do it before early optimizations. */
593 void
594 ipa_discover_readonly_nonaddressable_vars (void)
596 struct varpool_node *vnode;
597 if (dump_file)
598 fprintf (dump_file, "Clearing variable flags:");
599 FOR_EACH_VARIABLE (vnode)
600 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
601 && (TREE_ADDRESSABLE (vnode->decl)
602 || !TREE_READONLY (vnode->decl)))
604 bool written = false;
605 bool address_taken = false;
606 int i;
607 struct ipa_ref *ref;
608 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
609 i, ref)
610 && (!written || !address_taken); i++)
611 switch (ref->use)
613 case IPA_REF_ADDR:
614 address_taken = true;
615 break;
616 case IPA_REF_LOAD:
617 break;
618 case IPA_REF_STORE:
619 written = true;
620 break;
622 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
624 if (dump_file)
625 fprintf (dump_file, " %s (addressable)", vnode->name ());
626 TREE_ADDRESSABLE (vnode->decl) = 0;
628 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
629 /* Making variable in explicit section readonly can cause section
630 type conflict.
631 See e.g. gcc.c-torture/compile/pr23237.c */
632 && DECL_SECTION_NAME (vnode->decl) == NULL)
634 if (dump_file)
635 fprintf (dump_file, " %s (read-only)", vnode->name ());
636 TREE_READONLY (vnode->decl) = 1;
639 if (dump_file)
640 fprintf (dump_file, "\n");
643 /* Return true when there is a reference to node and it is not vtable. */
644 static bool
645 address_taken_from_non_vtable_p (symtab_node *node)
647 int i;
648 struct ipa_ref *ref;
649 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
650 i, ref); i++)
651 if (ref->use == IPA_REF_ADDR)
653 struct varpool_node *node;
654 if (is_a <cgraph_node> (ref->referring))
655 return true;
656 node = ipa_ref_referring_varpool_node (ref);
657 if (!DECL_VIRTUAL_P (node->decl))
658 return true;
660 return false;
663 /* A helper for comdat_can_be_unshared_p. */
665 static bool
666 comdat_can_be_unshared_p_1 (symtab_node *node)
668 /* When address is taken, we don't know if equality comparison won't
669 break eventually. Exception are virutal functions, C++
670 constructors/destructors and vtables, where this is not possible by
671 language standard. */
672 if (!DECL_VIRTUAL_P (node->decl)
673 && (TREE_CODE (node->decl) != FUNCTION_DECL
674 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
675 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
676 && address_taken_from_non_vtable_p (node))
677 return false;
679 /* If the symbol is used in some weird way, better to not touch it. */
680 if (node->force_output)
681 return false;
683 /* Explicit instantiations needs to be output when possibly
684 used externally. */
685 if (node->forced_by_abi
686 && TREE_PUBLIC (node->decl)
687 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
688 && !flag_whole_program))
689 return false;
691 /* Non-readonly and volatile variables can not be duplicated. */
692 if (is_a <varpool_node> (node)
693 && (!TREE_READONLY (node->decl)
694 || TREE_THIS_VOLATILE (node->decl)))
695 return false;
696 return true;
699 /* COMDAT functions must be shared only if they have address taken,
700 otherwise we can produce our own private implementation with
701 -fwhole-program.
702 Return true when turning COMDAT functoin static can not lead to wrong
703 code when the resulting object links with a library defining same COMDAT.
705 Virtual functions do have their addresses taken from the vtables,
706 but in C++ there is no way to compare their addresses for equality. */
708 static bool
709 comdat_can_be_unshared_p (symtab_node *node)
711 if (!comdat_can_be_unshared_p_1 (node))
712 return false;
713 if (node->same_comdat_group)
715 symtab_node *next;
717 /* If more than one function is in the same COMDAT group, it must
718 be shared even if just one function in the comdat group has
719 address taken. */
720 for (next = node->same_comdat_group;
721 next != node; next = next->same_comdat_group)
722 if (!comdat_can_be_unshared_p_1 (next))
723 return false;
725 return true;
728 /* Return true when function NODE should be considered externally visible. */
730 static bool
731 cgraph_externally_visible_p (struct cgraph_node *node,
732 bool whole_program)
734 if (!node->definition)
735 return false;
736 if (!TREE_PUBLIC (node->decl)
737 || DECL_EXTERNAL (node->decl))
738 return false;
740 /* Do not try to localize built-in functions yet. One of problems is that we
741 end up mangling their asm for WHOPR that makes it impossible to call them
742 using the implicit built-in declarations anymore. Similarly this enables
743 us to remove them as unreachable before actual calls may appear during
744 expansion or folding. */
745 if (DECL_BUILT_IN (node->decl))
746 return true;
748 /* If linker counts on us, we must preserve the function. */
749 if (symtab_used_from_object_file_p (node))
750 return true;
751 if (DECL_PRESERVE_P (node->decl))
752 return true;
753 if (lookup_attribute ("externally_visible",
754 DECL_ATTRIBUTES (node->decl)))
755 return true;
756 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
757 && lookup_attribute ("dllexport",
758 DECL_ATTRIBUTES (node->decl)))
759 return true;
760 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
761 return false;
762 /* When doing LTO or whole program, we can bring COMDAT functoins static.
763 This improves code quality and we know we will duplicate them at most twice
764 (in the case that we are not using plugin and link with object file
765 implementing same COMDAT) */
766 if ((in_lto_p || whole_program)
767 && DECL_COMDAT (node->decl)
768 && comdat_can_be_unshared_p (node))
769 return false;
771 /* When doing link time optimizations, hidden symbols become local. */
772 if (in_lto_p
773 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
774 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
775 /* Be sure that node is defined in IR file, not in other object
776 file. In that case we don't set used_from_other_object_file. */
777 && node->definition)
779 else if (!whole_program)
780 return true;
782 if (MAIN_NAME_P (DECL_NAME (node->decl)))
783 return true;
785 return false;
788 /* Return true when variable VNODE should be considered externally visible. */
790 bool
791 varpool_externally_visible_p (struct varpool_node *vnode)
793 if (DECL_EXTERNAL (vnode->decl))
794 return true;
796 if (!TREE_PUBLIC (vnode->decl))
797 return false;
799 /* If linker counts on us, we must preserve the function. */
800 if (symtab_used_from_object_file_p (vnode))
801 return true;
803 if (DECL_HARD_REGISTER (vnode->decl))
804 return true;
805 if (DECL_PRESERVE_P (vnode->decl))
806 return true;
807 if (lookup_attribute ("externally_visible",
808 DECL_ATTRIBUTES (vnode->decl)))
809 return true;
810 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
811 && lookup_attribute ("dllexport",
812 DECL_ATTRIBUTES (vnode->decl)))
813 return true;
815 /* See if we have linker information about symbol not being used or
816 if we need to make guess based on the declaration.
818 Even if the linker clams the symbol is unused, never bring internal
819 symbols that are declared by user as used or externally visible.
820 This is needed for i.e. references from asm statements. */
821 if (symtab_used_from_object_file_p (vnode))
822 return true;
823 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
824 return false;
826 /* As a special case, the COMDAT virtual tables can be unshared.
827 In LTO mode turn vtables into static variables. The variable is readonly,
828 so this does not enable more optimization, but referring static var
829 is faster for dynamic linking. Also this match logic hidding vtables
830 from LTO symbol tables. */
831 if ((in_lto_p || flag_whole_program)
832 && DECL_COMDAT (vnode->decl)
833 && comdat_can_be_unshared_p (vnode))
834 return false;
836 /* When doing link time optimizations, hidden symbols become local. */
837 if (in_lto_p
838 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
839 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
840 /* Be sure that node is defined in IR file, not in other object
841 file. In that case we don't set used_from_other_object_file. */
842 && vnode->definition)
844 else if (!flag_whole_program)
845 return true;
847 /* Do not attempt to privatize COMDATS by default.
848 This would break linking with C++ libraries sharing
849 inline definitions.
851 FIXME: We can do so for readonly vars with no address taken and
852 possibly also for vtables since no direct pointer comparsion is done.
853 It might be interesting to do so to reduce linking overhead. */
854 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
855 return true;
856 return false;
859 /* Return true if reference to NODE can be replaced by a local alias.
860 Local aliases save dynamic linking overhead and enable more optimizations.
863 bool
864 can_replace_by_local_alias (symtab_node *node)
866 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
867 && !symtab_can_be_discarded (node));
870 /* Mark visibility of all functions.
872 A local function is one whose calls can occur only in the current
873 compilation unit and all its calls are explicit, so we can change
874 its calling convention. We simply mark all static functions whose
875 address is not taken as local.
877 We also change the TREE_PUBLIC flag of all declarations that are public
878 in language point of view but we want to overwrite this default
879 via visibilities for the backend point of view. */
881 static unsigned int
882 function_and_variable_visibility (bool whole_program)
884 struct cgraph_node *node;
885 struct varpool_node *vnode;
887 /* All aliases should be procssed at this point. */
888 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
890 FOR_EACH_FUNCTION (node)
892 int flags = flags_from_decl_or_type (node->decl);
894 /* Optimize away PURE and CONST constructors and destructors. */
895 if (optimize
896 && (flags & (ECF_CONST | ECF_PURE))
897 && !(flags & ECF_LOOPING_CONST_OR_PURE))
899 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
900 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
903 /* Frontends and alias code marks nodes as needed before parsing is finished.
904 We may end up marking as node external nodes where this flag is meaningless
905 strip it. */
906 if (DECL_EXTERNAL (node->decl) || !node->definition)
908 node->force_output = 0;
909 node->forced_by_abi = 0;
912 /* C++ FE on lack of COMDAT support create local COMDAT functions
913 (that ought to be shared but can not due to object format
914 limitations). It is necessary to keep the flag to make rest of C++ FE
915 happy. Clear the flag here to avoid confusion in middle-end. */
916 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
917 DECL_COMDAT (node->decl) = 0;
919 /* For external decls stop tracking same_comdat_group. It doesn't matter
920 what comdat group they are in when they won't be emitted in this TU. */
921 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
923 #ifdef ENABLE_CHECKING
924 symtab_node *n;
926 for (n = node->same_comdat_group;
927 n != node;
928 n = n->same_comdat_group)
929 /* If at least one of same comdat group functions is external,
930 all of them have to be, otherwise it is a front-end bug. */
931 gcc_assert (DECL_EXTERNAL (n->decl));
932 #endif
933 symtab_dissolve_same_comdat_group_list (node);
935 gcc_assert ((!DECL_WEAK (node->decl)
936 && !DECL_COMDAT (node->decl))
937 || TREE_PUBLIC (node->decl)
938 || node->weakref
939 || DECL_EXTERNAL (node->decl));
940 if (cgraph_externally_visible_p (node, whole_program))
942 gcc_assert (!node->global.inlined_to);
943 node->externally_visible = true;
945 else
947 node->externally_visible = false;
948 node->forced_by_abi = false;
950 if (!node->externally_visible
951 && node->definition && !node->weakref
952 && !DECL_EXTERNAL (node->decl))
954 gcc_assert (whole_program || in_lto_p
955 || !TREE_PUBLIC (node->decl));
956 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
957 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
958 && TREE_PUBLIC (node->decl));
959 symtab_make_decl_local (node->decl);
960 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
961 if (node->same_comdat_group)
962 /* cgraph_externally_visible_p has already checked all other nodes
963 in the group and they will all be made local. We need to
964 dissolve the group at once so that the predicate does not
965 segfault though. */
966 symtab_dissolve_same_comdat_group_list (node);
969 if (node->thunk.thunk_p
970 && TREE_PUBLIC (node->decl))
972 struct cgraph_node *decl_node = node;
974 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
976 /* Thunks have the same visibility as function they are attached to.
977 Make sure the C++ front end set this up properly. */
978 if (DECL_ONE_ONLY (decl_node->decl))
980 gcc_checking_assert (DECL_COMDAT (node->decl)
981 == DECL_COMDAT (decl_node->decl));
982 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
983 == DECL_COMDAT_GROUP (decl_node->decl));
984 gcc_checking_assert (node->same_comdat_group);
986 if (DECL_EXTERNAL (decl_node->decl))
987 DECL_EXTERNAL (node->decl) = 1;
990 FOR_EACH_DEFINED_FUNCTION (node)
992 node->local.local |= cgraph_local_node_p (node);
994 /* If we know that function can not be overwritten by a different semantics
995 and moreover its section can not be discarded, replace all direct calls
996 by calls to an nonoverwritable alias. This make dynamic linking
997 cheaper and enable more optimization.
999 TODO: We can also update virtual tables. */
1000 if (node->callers && can_replace_by_local_alias (node))
1002 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1004 if (alias && alias != node)
1006 while (node->callers)
1008 struct cgraph_edge *e = node->callers;
1010 cgraph_redirect_edge_callee (e, alias);
1011 if (gimple_has_body_p (e->caller->decl))
1013 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1014 cgraph_redirect_edge_call_stmt_to_callee (e);
1015 pop_cfun ();
1021 FOR_EACH_VARIABLE (vnode)
1023 /* weak flag makes no sense on local variables. */
1024 gcc_assert (!DECL_WEAK (vnode->decl)
1025 || vnode->weakref
1026 || TREE_PUBLIC (vnode->decl)
1027 || DECL_EXTERNAL (vnode->decl));
1028 /* In several cases declarations can not be common:
1030 - when declaration has initializer
1031 - when it is in weak
1032 - when it has specific section
1033 - when it resides in non-generic address space.
1034 - if declaration is local, it will get into .local common section
1035 so common flag is not needed. Frontends still produce these in
1036 certain cases, such as for:
1038 static int a __attribute__ ((common))
1040 Canonicalize things here and clear the redundant flag. */
1041 if (DECL_COMMON (vnode->decl)
1042 && (!(TREE_PUBLIC (vnode->decl)
1043 || DECL_EXTERNAL (vnode->decl))
1044 || (DECL_INITIAL (vnode->decl)
1045 && DECL_INITIAL (vnode->decl) != error_mark_node)
1046 || DECL_WEAK (vnode->decl)
1047 || DECL_SECTION_NAME (vnode->decl) != NULL
1048 || ! (ADDR_SPACE_GENERIC_P
1049 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1050 DECL_COMMON (vnode->decl) = 0;
1052 FOR_EACH_DEFINED_VARIABLE (vnode)
1054 if (!vnode->definition)
1055 continue;
1056 if (varpool_externally_visible_p (vnode))
1057 vnode->externally_visible = true;
1058 else
1060 vnode->externally_visible = false;
1061 vnode->forced_by_abi = false;
1063 if (!vnode->externally_visible
1064 && !vnode->weakref)
1066 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1067 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1068 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1069 && TREE_PUBLIC (vnode->decl));
1070 symtab_make_decl_local (vnode->decl);
1071 if (vnode->same_comdat_group)
1072 symtab_dissolve_same_comdat_group_list (vnode);
1073 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1077 if (dump_file)
1079 fprintf (dump_file, "\nMarking local functions:");
1080 FOR_EACH_DEFINED_FUNCTION (node)
1081 if (node->local.local)
1082 fprintf (dump_file, " %s", node->name ());
1083 fprintf (dump_file, "\n\n");
1084 fprintf (dump_file, "\nMarking externally visible functions:");
1085 FOR_EACH_DEFINED_FUNCTION (node)
1086 if (node->externally_visible)
1087 fprintf (dump_file, " %s", node->name ());
1088 fprintf (dump_file, "\n\n");
1089 fprintf (dump_file, "\nMarking externally visible variables:");
1090 FOR_EACH_DEFINED_VARIABLE (vnode)
1091 if (vnode->externally_visible)
1092 fprintf (dump_file, " %s", vnode->name ());
1093 fprintf (dump_file, "\n\n");
1095 cgraph_function_flags_ready = true;
1096 return 0;
1099 /* Local function pass handling visibilities. This happens before LTO streaming
1100 so in particular -fwhole-program should be ignored at this level. */
1102 static unsigned int
1103 local_function_and_variable_visibility (void)
1105 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1108 namespace {
1110 const pass_data pass_data_ipa_function_and_variable_visibility =
1112 SIMPLE_IPA_PASS, /* type */
1113 "visibility", /* name */
1114 OPTGROUP_NONE, /* optinfo_flags */
1115 false, /* has_gate */
1116 true, /* has_execute */
1117 TV_CGRAPHOPT, /* tv_id */
1118 0, /* properties_required */
1119 0, /* properties_provided */
1120 0, /* properties_destroyed */
1121 0, /* todo_flags_start */
1122 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1125 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1127 public:
1128 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1129 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1130 ctxt)
1133 /* opt_pass methods: */
1134 unsigned int execute () {
1135 return local_function_and_variable_visibility ();
1138 }; // class pass_ipa_function_and_variable_visibility
1140 } // anon namespace
1142 simple_ipa_opt_pass *
1143 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1145 return new pass_ipa_function_and_variable_visibility (ctxt);
1148 /* Free inline summary. */
1150 static unsigned
1151 free_inline_summary (void)
1153 inline_free_summary ();
1154 return 0;
1157 namespace {
1159 const pass_data pass_data_ipa_free_inline_summary =
1161 SIMPLE_IPA_PASS, /* type */
1162 "*free_inline_summary", /* name */
1163 OPTGROUP_NONE, /* optinfo_flags */
1164 false, /* has_gate */
1165 true, /* has_execute */
1166 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1167 0, /* properties_required */
1168 0, /* properties_provided */
1169 0, /* properties_destroyed */
1170 0, /* todo_flags_start */
1171 0, /* todo_flags_finish */
1174 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1176 public:
1177 pass_ipa_free_inline_summary (gcc::context *ctxt)
1178 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1181 /* opt_pass methods: */
1182 unsigned int execute () { return free_inline_summary (); }
1184 }; // class pass_ipa_free_inline_summary
1186 } // anon namespace
1188 simple_ipa_opt_pass *
1189 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1191 return new pass_ipa_free_inline_summary (ctxt);
1194 /* Do not re-run on ltrans stage. */
1196 static bool
1197 gate_whole_program_function_and_variable_visibility (void)
1199 return !flag_ltrans;
1202 /* Bring functionss local at LTO time with -fwhole-program. */
1204 static unsigned int
1205 whole_program_function_and_variable_visibility (void)
1207 function_and_variable_visibility (flag_whole_program);
1208 if (optimize)
1209 ipa_discover_readonly_nonaddressable_vars ();
1210 return 0;
1213 namespace {
1215 const pass_data pass_data_ipa_whole_program_visibility =
1217 IPA_PASS, /* type */
1218 "whole-program", /* name */
1219 OPTGROUP_NONE, /* optinfo_flags */
1220 true, /* has_gate */
1221 true, /* has_execute */
1222 TV_CGRAPHOPT, /* tv_id */
1223 0, /* properties_required */
1224 0, /* properties_provided */
1225 0, /* properties_destroyed */
1226 0, /* todo_flags_start */
1227 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1230 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1232 public:
1233 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1234 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1235 NULL, /* generate_summary */
1236 NULL, /* write_summary */
1237 NULL, /* read_summary */
1238 NULL, /* write_optimization_summary */
1239 NULL, /* read_optimization_summary */
1240 NULL, /* stmt_fixup */
1241 0, /* function_transform_todo_flags_start */
1242 NULL, /* function_transform */
1243 NULL) /* variable_transform */
1246 /* opt_pass methods: */
1247 bool gate () {
1248 return gate_whole_program_function_and_variable_visibility ();
1250 unsigned int execute () {
1251 return whole_program_function_and_variable_visibility ();
1254 }; // class pass_ipa_whole_program_visibility
1256 } // anon namespace
1258 ipa_opt_pass_d *
1259 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1261 return new pass_ipa_whole_program_visibility (ctxt);
1264 /* Generate and emit a static constructor or destructor. WHICH must
1265 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
1266 (for chp static vars constructor) or 'B' (for chkp static bounds
1267 constructor). BODY is a STATEMENT_LIST containing GENERIC
1268 statements. PRIORITY is the initialization priority for this
1269 constructor or destructor.
1271 FINAL specify whether the externally visible name for collect2 should
1272 be produced. */
1274 static void
1275 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1277 static int counter = 0;
1278 char which_buf[16];
1279 tree decl, name, resdecl;
1281 /* The priority is encoded in the constructor or destructor name.
1282 collect2 will sort the names and arrange that they are called at
1283 program startup. */
1284 if (final)
1285 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1286 else
1287 /* Proudce sane name but one not recognizable by collect2, just for the
1288 case we fail to inline the function. */
1289 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1290 name = get_file_function_name (which_buf);
1292 decl = build_decl (input_location, FUNCTION_DECL, name,
1293 build_function_type_list (void_type_node, NULL_TREE));
1294 current_function_decl = decl;
1296 resdecl = build_decl (input_location,
1297 RESULT_DECL, NULL_TREE, void_type_node);
1298 DECL_ARTIFICIAL (resdecl) = 1;
1299 DECL_RESULT (decl) = resdecl;
1300 DECL_CONTEXT (resdecl) = decl;
1302 allocate_struct_function (decl, false);
1304 TREE_STATIC (decl) = 1;
1305 TREE_USED (decl) = 1;
1306 DECL_ARTIFICIAL (decl) = 1;
1307 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1308 DECL_SAVED_TREE (decl) = body;
1309 if (!targetm.have_ctors_dtors && final)
1311 TREE_PUBLIC (decl) = 1;
1312 DECL_PRESERVE_P (decl) = 1;
1314 DECL_UNINLINABLE (decl) = 1;
1316 DECL_INITIAL (decl) = make_node (BLOCK);
1317 TREE_USED (DECL_INITIAL (decl)) = 1;
1319 DECL_SOURCE_LOCATION (decl) = input_location;
1320 cfun->function_end_locus = input_location;
1322 switch (which)
1324 case 'I':
1325 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1326 decl_init_priority_insert (decl, priority);
1327 break;
1328 case 'P':
1329 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1330 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("chkp ctor"),
1331 NULL,
1332 NULL_TREE);
1333 decl_init_priority_insert (decl, priority);
1334 break;
1335 case 'B':
1336 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1337 DECL_ATTRIBUTES (decl) = tree_cons (get_identifier ("bnd_legacy"),
1338 NULL,
1339 NULL_TREE);
1340 decl_init_priority_insert (decl, priority);
1341 break;
1342 case 'D':
1343 DECL_STATIC_DESTRUCTOR (decl) = 1;
1344 decl_fini_priority_insert (decl, priority);
1345 break;
1346 default:
1347 gcc_unreachable ();
1350 gimplify_function_tree (decl);
1352 cgraph_add_new_function (decl, false);
1354 set_cfun (NULL);
1355 current_function_decl = NULL;
1358 /* Generate and emit a static constructor or destructor. WHICH must
1359 be one of 'I' (for a constructor), 'D' (for a destructor), 'P'
1360 (for chkp static vars constructor) or 'B' (for chkp static bounds
1361 constructor). BODY is a STATEMENT_LIST containing GENERIC
1362 statements. PRIORITY is the initialization priority for this
1363 constructor or destructor. */
1365 void
1366 cgraph_build_static_cdtor (char which, tree body, int priority)
1368 cgraph_build_static_cdtor_1 (which, body, priority, false);
1371 /* A vector of FUNCTION_DECLs declared as static constructors. */
1372 static vec<tree> static_ctors;
1373 /* A vector of FUNCTION_DECLs declared as static destructors. */
1374 static vec<tree> static_dtors;
1376 /* When target does not have ctors and dtors, we call all constructor
1377 and destructor by special initialization/destruction function
1378 recognized by collect2.
1380 When we are going to build this function, collect all constructors and
1381 destructors and turn them into normal functions. */
1383 static void
1384 record_cdtor_fn (struct cgraph_node *node)
1386 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1387 static_ctors.safe_push (node->decl);
1388 if (DECL_STATIC_DESTRUCTOR (node->decl))
1389 static_dtors.safe_push (node->decl);
1390 node = cgraph_get_node (node->decl);
1391 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1394 /* Define global constructors/destructor functions for the CDTORS, of
1395 which they are LEN. The CDTORS are sorted by initialization
1396 priority. If CTOR_P is true, these are constructors; otherwise,
1397 they are destructors. */
1399 static void
1400 build_cdtor (bool ctor_p, vec<tree> cdtors)
1402 size_t i,j;
1403 size_t len = cdtors.length ();
1405 i = 0;
1406 while (i < len)
1408 tree body;
1409 tree fn;
1410 priority_type priority;
1412 priority = 0;
1413 body = NULL_TREE;
1414 j = i;
1417 priority_type p;
1418 fn = cdtors[j];
1419 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1420 if (j == i)
1421 priority = p;
1422 else if (p != priority)
1423 break;
1424 j++;
1426 while (j < len);
1428 /* When there is only one cdtor and target supports them, do nothing. */
1429 if (j == i + 1
1430 && targetm.have_ctors_dtors)
1432 i++;
1433 continue;
1435 /* Find the next batch of constructors/destructors with the same
1436 initialization priority. */
1437 for (;i < j; i++)
1439 tree call;
1440 fn = cdtors[i];
1441 call = build_call_expr (fn, 0);
1442 if (ctor_p)
1443 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1444 else
1445 DECL_STATIC_DESTRUCTOR (fn) = 0;
1446 /* We do not want to optimize away pure/const calls here.
1447 When optimizing, these should be already removed, when not
1448 optimizing, we want user to be able to breakpoint in them. */
1449 TREE_SIDE_EFFECTS (call) = 1;
1450 append_to_statement_list (call, &body);
1452 gcc_assert (body != NULL_TREE);
1453 /* Generate a function to call all the function of like
1454 priority. */
1455 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1459 /* Comparison function for qsort. P1 and P2 are actually of type
1460 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1461 used to determine the sort order. */
1463 static int
1464 compare_ctor (const void *p1, const void *p2)
1466 tree f1;
1467 tree f2;
1468 int priority1;
1469 int priority2;
1471 f1 = *(const tree *)p1;
1472 f2 = *(const tree *)p2;
1473 priority1 = DECL_INIT_PRIORITY (f1);
1474 priority2 = DECL_INIT_PRIORITY (f2);
1476 if (priority1 < priority2)
1477 return -1;
1478 else if (priority1 > priority2)
1479 return 1;
1480 else
1481 /* Ensure a stable sort. Constructors are executed in backwarding
1482 order to make LTO initialize braries first. */
1483 return DECL_UID (f2) - DECL_UID (f1);
1486 /* Comparison function for qsort. P1 and P2 are actually of type
1487 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1488 used to determine the sort order. */
1490 static int
1491 compare_dtor (const void *p1, const void *p2)
1493 tree f1;
1494 tree f2;
1495 int priority1;
1496 int priority2;
1498 f1 = *(const tree *)p1;
1499 f2 = *(const tree *)p2;
1500 priority1 = DECL_FINI_PRIORITY (f1);
1501 priority2 = DECL_FINI_PRIORITY (f2);
1503 if (priority1 < priority2)
1504 return -1;
1505 else if (priority1 > priority2)
1506 return 1;
1507 else
1508 /* Ensure a stable sort. */
1509 return DECL_UID (f1) - DECL_UID (f2);
1512 /* Generate functions to call static constructors and destructors
1513 for targets that do not support .ctors/.dtors sections. These
1514 functions have magic names which are detected by collect2. */
1516 static void
1517 build_cdtor_fns (void)
1519 if (!static_ctors.is_empty ())
1521 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1522 static_ctors.qsort (compare_ctor);
1523 build_cdtor (/*ctor_p=*/true, static_ctors);
1526 if (!static_dtors.is_empty ())
1528 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1529 static_dtors.qsort (compare_dtor);
1530 build_cdtor (/*ctor_p=*/false, static_dtors);
1534 /* Look for constructors and destructors and produce function calling them.
1535 This is needed for targets not supporting ctors or dtors, but we perform the
1536 transformation also at linktime to merge possibly numerous
1537 constructors/destructors into single function to improve code locality and
1538 reduce size. */
1540 static unsigned int
1541 ipa_cdtor_merge (void)
1543 struct cgraph_node *node;
1544 FOR_EACH_DEFINED_FUNCTION (node)
1545 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1546 || DECL_STATIC_DESTRUCTOR (node->decl))
1547 record_cdtor_fn (node);
1548 build_cdtor_fns ();
1549 static_ctors.release ();
1550 static_dtors.release ();
1551 return 0;
1554 /* Perform the pass when we have no ctors/dtors support
1555 or at LTO time to merge multiple constructors into single
1556 function. */
1558 static bool
1559 gate_ipa_cdtor_merge (void)
1561 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1564 namespace {
1566 const pass_data pass_data_ipa_cdtor_merge =
1568 IPA_PASS, /* type */
1569 "cdtor", /* name */
1570 OPTGROUP_NONE, /* optinfo_flags */
1571 true, /* has_gate */
1572 true, /* has_execute */
1573 TV_CGRAPHOPT, /* tv_id */
1574 0, /* properties_required */
1575 0, /* properties_provided */
1576 0, /* properties_destroyed */
1577 0, /* todo_flags_start */
1578 0, /* todo_flags_finish */
1581 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1583 public:
1584 pass_ipa_cdtor_merge (gcc::context *ctxt)
1585 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1586 NULL, /* generate_summary */
1587 NULL, /* write_summary */
1588 NULL, /* read_summary */
1589 NULL, /* write_optimization_summary */
1590 NULL, /* read_optimization_summary */
1591 NULL, /* stmt_fixup */
1592 0, /* function_transform_todo_flags_start */
1593 NULL, /* function_transform */
1594 NULL) /* variable_transform */
1597 /* opt_pass methods: */
1598 bool gate () { return gate_ipa_cdtor_merge (); }
1599 unsigned int execute () { return ipa_cdtor_merge (); }
1601 }; // class pass_ipa_cdtor_merge
1603 } // anon namespace
1605 ipa_opt_pass_d *
1606 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1608 return new pass_ipa_cdtor_merge (ctxt);