2014-04-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa.c
blob26e9b03b9bdbd0029fb97987251d98645eb90b9d
1 /* Basic IPA optimizations and utilities.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "calls.h"
26 #include "stringpool.h"
27 #include "cgraph.h"
28 #include "tree-pass.h"
29 #include "pointer-set.h"
30 #include "gimple-expr.h"
31 #include "gimplify.h"
32 #include "flags.h"
33 #include "target.h"
34 #include "tree-iterator.h"
35 #include "ipa-utils.h"
36 #include "ipa-inline.h"
37 #include "tree-inline.h"
38 #include "profile.h"
39 #include "params.h"
41 /* Return true when NODE can not be local. Worker for cgraph_local_node_p. */
43 static bool
44 cgraph_non_local_node_p_1 (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
46 /* FIXME: Aliases can be local, but i386 gets thunks wrong then. */
47 return !(cgraph_only_called_directly_or_aliased_p (node)
48 && !ipa_ref_has_aliases_p (&node->ref_list)
49 && node->definition
50 && !DECL_EXTERNAL (node->decl)
51 && !node->externally_visible
52 && !node->used_from_other_partition
53 && !node->in_other_partition);
56 /* Return true when function can be marked local. */
58 static bool
59 cgraph_local_node_p (struct cgraph_node *node)
61 struct cgraph_node *n = cgraph_function_or_thunk_node (node, NULL);
63 /* FIXME: thunks can be considered local, but we need prevent i386
64 from attempting to change calling convention of them. */
65 if (n->thunk.thunk_p)
66 return false;
67 return !cgraph_for_node_and_aliases (n,
68 cgraph_non_local_node_p_1, NULL, true);
72 /* Return true when NODE has ADDR reference. */
74 static bool
75 has_addr_references_p (struct cgraph_node *node,
76 void *data ATTRIBUTE_UNUSED)
78 int i;
79 struct ipa_ref *ref;
81 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
82 i, ref); i++)
83 if (ref->use == IPA_REF_ADDR)
84 return true;
85 return false;
88 /* Look for all functions inlined to NODE and update their inlined_to pointers
89 to INLINED_TO. */
91 static void
92 update_inlined_to_pointer (struct cgraph_node *node, struct cgraph_node *inlined_to)
94 struct cgraph_edge *e;
95 for (e = node->callees; e; e = e->next_callee)
96 if (e->callee->global.inlined_to)
98 e->callee->global.inlined_to = inlined_to;
99 update_inlined_to_pointer (e->callee, inlined_to);
103 /* Add symtab NODE to queue starting at FIRST.
105 The queue is linked via AUX pointers and terminated by pointer to 1.
106 We enqueue nodes at two occasions: when we find them reachable or when we find
107 their bodies needed for further clonning. In the second case we mark them
108 by pointer to 2 after processing so they are re-queue when they become
109 reachable. */
111 static void
112 enqueue_node (symtab_node *node, symtab_node **first,
113 struct pointer_set_t *reachable)
115 /* Node is still in queue; do nothing. */
116 if (node->aux && node->aux != (void *) 2)
117 return;
118 /* Node was already processed as unreachable, re-enqueue
119 only if it became reachable now. */
120 if (node->aux == (void *)2 && !pointer_set_contains (reachable, node))
121 return;
122 node->aux = *first;
123 *first = node;
126 /* Process references. */
128 static void
129 process_references (struct ipa_ref_list *list,
130 symtab_node **first,
131 bool before_inlining_p,
132 struct pointer_set_t *reachable)
134 int i;
135 struct ipa_ref *ref;
136 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
138 symtab_node *node = ref->referred;
140 if (node->definition && !node->in_other_partition
141 && ((!DECL_EXTERNAL (node->decl) || node->alias)
142 || (((before_inlining_p
143 && (cgraph_state < CGRAPH_STATE_IPA_SSA
144 || !lookup_attribute ("always_inline",
145 DECL_ATTRIBUTES (node->decl)))))
146 /* We use variable constructors during late complation for
147 constant folding. Keep references alive so partitioning
148 knows about potential references. */
149 || (TREE_CODE (node->decl) == VAR_DECL
150 && flag_wpa
151 && ctor_for_folding (node->decl)
152 != error_mark_node))))
153 pointer_set_insert (reachable, node);
154 enqueue_node (node, first, reachable);
158 /* EDGE is an polymorphic call. If BEFORE_INLINING_P is set, mark
159 all its potential targets as reachable to permit later inlining if
160 devirtualization happens. After inlining still keep their declarations
161 around, so we can devirtualize to a direct call.
163 Also try to make trivial devirutalization when no or only one target is
164 possible. */
166 static void
167 walk_polymorphic_call_targets (pointer_set_t *reachable_call_targets,
168 struct cgraph_edge *edge,
169 symtab_node **first,
170 pointer_set_t *reachable, bool before_inlining_p)
172 unsigned int i;
173 void *cache_token;
174 bool final;
175 vec <cgraph_node *>targets
176 = possible_polymorphic_call_targets
177 (edge, &final, &cache_token);
179 if (!pointer_set_insert (reachable_call_targets,
180 cache_token))
182 for (i = 0; i < targets.length (); i++)
184 struct cgraph_node *n = targets[i];
186 /* Do not bother to mark virtual methods in anonymous namespace;
187 either we will find use of virtual table defining it, or it is
188 unused. */
189 if (TREE_CODE (TREE_TYPE (n->decl)) == METHOD_TYPE
190 && type_in_anonymous_namespace_p
191 (method_class_type (TREE_TYPE (n->decl))))
192 continue;
194 /* Prior inlining, keep alive bodies of possible targets for
195 devirtualization. */
196 if (n->definition
197 && (before_inlining_p
198 && (cgraph_state < CGRAPH_STATE_IPA_SSA
199 || !lookup_attribute ("always_inline",
200 DECL_ATTRIBUTES (n->decl)))))
201 pointer_set_insert (reachable, n);
203 /* Even after inlining we want to keep the possible targets in the
204 boundary, so late passes can still produce direct call even if
205 the chance for inlining is lost. */
206 enqueue_node (n, first, reachable);
210 /* Very trivial devirtualization; when the type is
211 final or anonymous (so we know all its derivation)
212 and there is only one possible virtual call target,
213 make the edge direct. */
214 if (final)
216 if (targets.length () <= 1)
218 cgraph_node *target, *node = edge->caller;
219 if (targets.length () == 1)
220 target = targets[0];
221 else
222 target = cgraph_get_create_node
223 (builtin_decl_implicit (BUILT_IN_UNREACHABLE));
225 if (dump_file)
226 fprintf (dump_file,
227 "Devirtualizing call in %s/%i to %s/%i\n",
228 edge->caller->name (),
229 edge->caller->order,
230 target->name (), target->order);
231 edge = cgraph_make_edge_direct (edge, target);
232 if (inline_summary_vec)
233 inline_update_overall_summary (node);
234 else if (edge->call_stmt)
235 cgraph_redirect_edge_call_stmt_to_callee (edge);
240 /* Perform reachability analysis and reclaim all unreachable nodes.
242 The algorithm is basically mark&sweep but with some extra refinements:
244 - reachable extern inline functions needs special handling; the bodies needs
245 to stay in memory until inlining in hope that they will be inlined.
246 After inlining we release their bodies and turn them into unanalyzed
247 nodes even when they are reachable.
249 BEFORE_INLINING_P specify whether we are before or after inlining.
251 - virtual functions are kept in callgraph even if they seem unreachable in
252 hope calls to them will be devirtualized.
254 Again we remove them after inlining. In late optimization some
255 devirtualization may happen, but it is not important since we won't inline
256 the call. In theory early opts and IPA should work out all important cases.
258 - virtual clones needs bodies of their origins for later materialization;
259 this means that we want to keep the body even if the origin is unreachable
260 otherwise. To avoid origin from sitting in the callgraph and being
261 walked by IPA passes, we turn them into unanalyzed nodes with body
262 defined.
264 We maintain set of function declaration where body needs to stay in
265 body_needed_for_clonning
267 Inline clones represent special case: their declaration match the
268 declaration of origin and cgraph_remove_node already knows how to
269 reshape callgraph and preserve body when offline copy of function or
270 inline clone is being removed.
272 - C++ virtual tables keyed to other unit are represented as DECL_EXTERNAL
273 variables with DECL_INITIAL set. We finalize these and keep reachable
274 ones around for constant folding purposes. After inlining we however
275 stop walking their references to let everything static referneced by them
276 to be removed when it is otherwise unreachable.
278 We maintain queue of both reachable symbols (i.e. defined symbols that needs
279 to stay) and symbols that are in boundary (i.e. external symbols referenced
280 by reachable symbols or origins of clones). The queue is represented
281 as linked list by AUX pointer terminated by 1.
283 At the end we keep all reachable symbols. For symbols in boundary we always
284 turn definition into a declaration, but we may keep function body around
285 based on body_needed_for_clonning
287 All symbols that enter the queue have AUX pointer non-zero and are in the
288 boundary. Pointer set REACHABLE is used to track reachable symbols.
290 Every symbol can be visited twice - once as part of boundary and once
291 as real reachable symbol. enqueue_node needs to decide whether the
292 node needs to be re-queued for second processing. For this purpose
293 we set AUX pointer of processed symbols in the boundary to constant 2. */
295 bool
296 symtab_remove_unreachable_nodes (bool before_inlining_p, FILE *file)
298 symtab_node *first = (symtab_node *) (void *) 1;
299 struct cgraph_node *node, *next;
300 varpool_node *vnode, *vnext;
301 bool changed = false;
302 struct pointer_set_t *reachable = pointer_set_create ();
303 struct pointer_set_t *body_needed_for_clonning = pointer_set_create ();
304 struct pointer_set_t *reachable_call_targets = pointer_set_create ();
306 timevar_push (TV_IPA_UNREACHABLE);
307 #ifdef ENABLE_CHECKING
308 verify_symtab ();
309 #endif
310 if (optimize && flag_devirtualize)
311 build_type_inheritance_graph ();
312 if (file)
313 fprintf (file, "\nReclaiming functions:");
314 #ifdef ENABLE_CHECKING
315 FOR_EACH_FUNCTION (node)
316 gcc_assert (!node->aux);
317 FOR_EACH_VARIABLE (vnode)
318 gcc_assert (!vnode->aux);
319 #endif
320 /* Mark functions whose bodies are obviously needed.
321 This is mostly when they can be referenced externally. Inline clones
322 are special since their declarations are shared with master clone and thus
323 cgraph_can_remove_if_no_direct_calls_and_refs_p should not be called on them. */
324 FOR_EACH_FUNCTION (node)
326 node->used_as_abstract_origin = false;
327 if (node->definition
328 && !node->global.inlined_to
329 && !node->in_other_partition
330 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
332 gcc_assert (!node->global.inlined_to);
333 pointer_set_insert (reachable, node);
334 enqueue_node (node, &first, reachable);
336 else
337 gcc_assert (!node->aux);
340 /* Mark variables that are obviously needed. */
341 FOR_EACH_DEFINED_VARIABLE (vnode)
342 if (!varpool_can_remove_if_no_refs (vnode)
343 && !vnode->in_other_partition)
345 pointer_set_insert (reachable, vnode);
346 enqueue_node (vnode, &first, reachable);
349 /* Perform reachability analysis. */
350 while (first != (symtab_node *) (void *) 1)
352 bool in_boundary_p = !pointer_set_contains (reachable, first);
353 symtab_node *node = first;
355 first = (symtab_node *)first->aux;
357 /* If we are processing symbol in boundary, mark its AUX pointer for
358 possible later re-processing in enqueue_node. */
359 if (in_boundary_p)
360 node->aux = (void *)2;
361 else
363 if (TREE_CODE (node->decl) == FUNCTION_DECL
364 && DECL_ABSTRACT_ORIGIN (node->decl))
366 struct cgraph_node *origin_node
367 = cgraph_get_create_node (DECL_ABSTRACT_ORIGIN (node->decl));
368 origin_node->used_as_abstract_origin = true;
369 enqueue_node (origin_node, &first, reachable);
371 /* If any symbol in a comdat group is reachable, force
372 all externally visible symbols in the same comdat
373 group to be reachable as well. Comdat-local symbols
374 can be discarded if all uses were inlined. */
375 if (node->same_comdat_group)
377 symtab_node *next;
378 for (next = node->same_comdat_group;
379 next != node;
380 next = next->same_comdat_group)
381 if (!symtab_comdat_local_p (next)
382 && !pointer_set_insert (reachable, next))
383 enqueue_node (next, &first, reachable);
385 /* Mark references as reachable. */
386 process_references (&node->ref_list, &first,
387 before_inlining_p, reachable);
390 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
392 /* Mark the callees reachable unless they are direct calls to extern
393 inline functions we decided to not inline. */
394 if (!in_boundary_p)
396 struct cgraph_edge *e;
397 /* Keep alive possible targets for devirtualization. */
398 if (optimize && flag_devirtualize)
400 struct cgraph_edge *next;
401 for (e = cnode->indirect_calls; e; e = next)
403 next = e->next_callee;
404 if (e->indirect_info->polymorphic)
405 walk_polymorphic_call_targets (reachable_call_targets,
406 e, &first, reachable,
407 before_inlining_p);
410 for (e = cnode->callees; e; e = e->next_callee)
412 if (e->callee->definition
413 && !e->callee->in_other_partition
414 && (!e->inline_failed
415 || !DECL_EXTERNAL (e->callee->decl)
416 || e->callee->alias
417 || before_inlining_p))
418 pointer_set_insert (reachable, e->callee);
419 enqueue_node (e->callee, &first, reachable);
422 /* When inline clone exists, mark body to be preserved so when removing
423 offline copy of the function we don't kill it. */
424 if (cnode->global.inlined_to)
425 pointer_set_insert (body_needed_for_clonning, cnode->decl);
427 /* For non-inline clones, force their origins to the boundary and ensure
428 that body is not removed. */
429 while (cnode->clone_of)
431 bool noninline = cnode->clone_of->decl != cnode->decl;
432 cnode = cnode->clone_of;
433 if (noninline)
435 pointer_set_insert (body_needed_for_clonning, cnode->decl);
436 enqueue_node (cnode, &first, reachable);
441 /* If any reachable function has simd clones, mark them as
442 reachable as well. */
443 if (cnode->simd_clones)
445 cgraph_node *next;
446 for (next = cnode->simd_clones;
447 next;
448 next = next->simdclone->next_clone)
449 if (in_boundary_p
450 || !pointer_set_insert (reachable, next))
451 enqueue_node (next, &first, reachable);
454 /* When we see constructor of external variable, keep referred nodes in the
455 boundary. This will also hold initializers of the external vars NODE
456 refers to. */
457 varpool_node *vnode = dyn_cast <varpool_node> (node);
458 if (vnode
459 && DECL_EXTERNAL (node->decl)
460 && !vnode->alias
461 && in_boundary_p)
463 struct ipa_ref *ref;
464 for (int i = 0; ipa_ref_list_reference_iterate (&node->ref_list, i, ref); i++)
465 enqueue_node (ref->referred, &first, reachable);
469 /* Remove unreachable functions. */
470 for (node = cgraph_first_function (); node; node = next)
472 next = cgraph_next_function (node);
474 /* If node is not needed at all, remove it. */
475 if (!node->aux)
477 if (file)
478 fprintf (file, " %s/%i", node->name (), node->order);
479 cgraph_remove_node (node);
480 changed = true;
482 /* If node is unreachable, remove its body. */
483 else if (!pointer_set_contains (reachable, node))
485 if (!pointer_set_contains (body_needed_for_clonning, node->decl))
486 cgraph_release_function_body (node);
487 else if (!node->clone_of)
488 gcc_assert (in_lto_p || DECL_RESULT (node->decl));
489 if (node->definition)
491 if (file)
492 fprintf (file, " %s/%i", node->name (), node->order);
493 node->body_removed = true;
494 node->analyzed = false;
495 node->definition = false;
496 node->cpp_implicit_alias = false;
497 node->alias = false;
498 node->thunk.thunk_p = false;
499 node->weakref = false;
500 /* After early inlining we drop always_inline attributes on
501 bodies of functions that are still referenced (have their
502 address taken). */
503 DECL_ATTRIBUTES (node->decl)
504 = remove_attribute ("always_inline",
505 DECL_ATTRIBUTES (node->decl));
506 if (!node->in_other_partition)
507 node->local.local = false;
508 cgraph_node_remove_callees (node);
509 ipa_remove_all_references (&node->ref_list);
510 changed = true;
513 else
514 gcc_assert (node->clone_of || !cgraph_function_with_gimple_body_p (node)
515 || in_lto_p || DECL_RESULT (node->decl));
518 /* Inline clones might be kept around so their materializing allows further
519 cloning. If the function the clone is inlined into is removed, we need
520 to turn it into normal cone. */
521 FOR_EACH_FUNCTION (node)
523 if (node->global.inlined_to
524 && !node->callers)
526 gcc_assert (node->clones);
527 node->global.inlined_to = NULL;
528 update_inlined_to_pointer (node, node);
530 node->aux = NULL;
533 /* Remove unreachable variables. */
534 if (file)
535 fprintf (file, "\nReclaiming variables:");
536 for (vnode = varpool_first_variable (); vnode; vnode = vnext)
538 vnext = varpool_next_variable (vnode);
539 if (!vnode->aux
540 /* For can_refer_decl_in_current_unit_p we want to track for
541 all external variables if they are defined in other partition
542 or not. */
543 && (!flag_ltrans || !DECL_EXTERNAL (vnode->decl)))
545 if (file)
546 fprintf (file, " %s/%i", vnode->name (), vnode->order);
547 varpool_remove_node (vnode);
548 changed = true;
550 else if (!pointer_set_contains (reachable, vnode))
552 tree init;
553 if (vnode->definition)
555 if (file)
556 fprintf (file, " %s", vnode->name ());
557 changed = true;
559 vnode->body_removed = true;
560 vnode->definition = false;
561 vnode->analyzed = false;
562 vnode->aux = NULL;
564 /* Keep body if it may be useful for constant folding. */
565 if ((init = ctor_for_folding (vnode->decl)) == error_mark_node)
566 varpool_remove_initializer (vnode);
567 else
568 DECL_INITIAL (vnode->decl) = init;
569 ipa_remove_all_references (&vnode->ref_list);
571 else
572 vnode->aux = NULL;
575 pointer_set_destroy (reachable);
576 pointer_set_destroy (body_needed_for_clonning);
577 pointer_set_destroy (reachable_call_targets);
579 /* Now update address_taken flags and try to promote functions to be local. */
580 if (file)
581 fprintf (file, "\nClearing address taken flags:");
582 FOR_EACH_DEFINED_FUNCTION (node)
583 if (node->address_taken
584 && !node->used_from_other_partition)
586 if (!cgraph_for_node_and_aliases (node, has_addr_references_p, NULL, true))
588 if (file)
589 fprintf (file, " %s", node->name ());
590 node->address_taken = false;
591 changed = true;
592 if (cgraph_local_node_p (node))
594 node->local.local = true;
595 if (file)
596 fprintf (file, " (local)");
600 if (file)
601 fprintf (file, "\n");
603 #ifdef ENABLE_CHECKING
604 verify_symtab ();
605 #endif
607 /* If we removed something, perhaps profile could be improved. */
608 if (changed && optimize && inline_edge_summary_vec.exists ())
609 FOR_EACH_DEFINED_FUNCTION (node)
610 ipa_propagate_frequency (node);
612 timevar_pop (TV_IPA_UNREACHABLE);
613 return changed;
616 /* Discover variables that have no longer address taken or that are read only
617 and update their flags.
619 FIXME: This can not be done in between gimplify and omp_expand since
620 readonly flag plays role on what is shared and what is not. Currently we do
621 this transformation as part of whole program visibility and re-do at
622 ipa-reference pass (to take into account clonning), but it would
623 make sense to do it before early optimizations. */
625 void
626 ipa_discover_readonly_nonaddressable_vars (void)
628 varpool_node *vnode;
629 if (dump_file)
630 fprintf (dump_file, "Clearing variable flags:");
631 FOR_EACH_VARIABLE (vnode)
632 if (vnode->definition && varpool_all_refs_explicit_p (vnode)
633 && (TREE_ADDRESSABLE (vnode->decl)
634 || !TREE_READONLY (vnode->decl)))
636 bool written = false;
637 bool address_taken = false;
638 int i;
639 struct ipa_ref *ref;
640 for (i = 0; ipa_ref_list_referring_iterate (&vnode->ref_list,
641 i, ref)
642 && (!written || !address_taken); i++)
643 switch (ref->use)
645 case IPA_REF_ADDR:
646 address_taken = true;
647 break;
648 case IPA_REF_LOAD:
649 break;
650 case IPA_REF_STORE:
651 written = true;
652 break;
654 if (TREE_ADDRESSABLE (vnode->decl) && !address_taken)
656 if (dump_file)
657 fprintf (dump_file, " %s (addressable)", vnode->name ());
658 TREE_ADDRESSABLE (vnode->decl) = 0;
660 if (!TREE_READONLY (vnode->decl) && !address_taken && !written
661 /* Making variable in explicit section readonly can cause section
662 type conflict.
663 See e.g. gcc.c-torture/compile/pr23237.c */
664 && DECL_SECTION_NAME (vnode->decl) == NULL)
666 if (dump_file)
667 fprintf (dump_file, " %s (read-only)", vnode->name ());
668 TREE_READONLY (vnode->decl) = 1;
671 if (dump_file)
672 fprintf (dump_file, "\n");
675 /* Return true when there is a reference to node and it is not vtable. */
676 static bool
677 address_taken_from_non_vtable_p (symtab_node *node)
679 int i;
680 struct ipa_ref *ref;
681 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list,
682 i, ref); i++)
683 if (ref->use == IPA_REF_ADDR)
685 varpool_node *node;
686 if (is_a <cgraph_node> (ref->referring))
687 return true;
688 node = ipa_ref_referring_varpool_node (ref);
689 if (!DECL_VIRTUAL_P (node->decl))
690 return true;
692 return false;
695 /* A helper for comdat_can_be_unshared_p. */
697 static bool
698 comdat_can_be_unshared_p_1 (symtab_node *node)
700 /* When address is taken, we don't know if equality comparison won't
701 break eventually. Exception are virutal functions, C++
702 constructors/destructors and vtables, where this is not possible by
703 language standard. */
704 if (!DECL_VIRTUAL_P (node->decl)
705 && (TREE_CODE (node->decl) != FUNCTION_DECL
706 || (!DECL_CXX_CONSTRUCTOR_P (node->decl)
707 && !DECL_CXX_DESTRUCTOR_P (node->decl)))
708 && address_taken_from_non_vtable_p (node))
709 return false;
711 /* If the symbol is used in some weird way, better to not touch it. */
712 if (node->force_output)
713 return false;
715 /* Explicit instantiations needs to be output when possibly
716 used externally. */
717 if (node->forced_by_abi
718 && TREE_PUBLIC (node->decl)
719 && (node->resolution != LDPR_PREVAILING_DEF_IRONLY
720 && !flag_whole_program))
721 return false;
723 /* Non-readonly and volatile variables can not be duplicated. */
724 if (is_a <varpool_node> (node)
725 && (!TREE_READONLY (node->decl)
726 || TREE_THIS_VOLATILE (node->decl)))
727 return false;
728 return true;
731 /* COMDAT functions must be shared only if they have address taken,
732 otherwise we can produce our own private implementation with
733 -fwhole-program.
734 Return true when turning COMDAT functoin static can not lead to wrong
735 code when the resulting object links with a library defining same COMDAT.
737 Virtual functions do have their addresses taken from the vtables,
738 but in C++ there is no way to compare their addresses for equality. */
740 static bool
741 comdat_can_be_unshared_p (symtab_node *node)
743 if (!comdat_can_be_unshared_p_1 (node))
744 return false;
745 if (node->same_comdat_group)
747 symtab_node *next;
749 /* If more than one function is in the same COMDAT group, it must
750 be shared even if just one function in the comdat group has
751 address taken. */
752 for (next = node->same_comdat_group;
753 next != node; next = next->same_comdat_group)
754 if (!comdat_can_be_unshared_p_1 (next))
755 return false;
757 return true;
760 /* Return true when function NODE should be considered externally visible. */
762 static bool
763 cgraph_externally_visible_p (struct cgraph_node *node,
764 bool whole_program)
766 if (!node->definition)
767 return false;
768 if (!TREE_PUBLIC (node->decl)
769 || DECL_EXTERNAL (node->decl))
770 return false;
772 /* Do not try to localize built-in functions yet. One of problems is that we
773 end up mangling their asm for WHOPR that makes it impossible to call them
774 using the implicit built-in declarations anymore. Similarly this enables
775 us to remove them as unreachable before actual calls may appear during
776 expansion or folding. */
777 if (DECL_BUILT_IN (node->decl))
778 return true;
780 /* If linker counts on us, we must preserve the function. */
781 if (symtab_used_from_object_file_p (node))
782 return true;
783 if (DECL_PRESERVE_P (node->decl))
784 return true;
785 if (lookup_attribute ("externally_visible",
786 DECL_ATTRIBUTES (node->decl)))
787 return true;
788 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
789 && lookup_attribute ("dllexport",
790 DECL_ATTRIBUTES (node->decl)))
791 return true;
792 if (node->resolution == LDPR_PREVAILING_DEF_IRONLY)
793 return false;
794 /* When doing LTO or whole program, we can bring COMDAT functoins static.
795 This improves code quality and we know we will duplicate them at most twice
796 (in the case that we are not using plugin and link with object file
797 implementing same COMDAT) */
798 if ((in_lto_p || whole_program)
799 && DECL_COMDAT (node->decl)
800 && comdat_can_be_unshared_p (node))
801 return false;
803 /* When doing link time optimizations, hidden symbols become local. */
804 if (in_lto_p
805 && (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
806 || DECL_VISIBILITY (node->decl) == VISIBILITY_INTERNAL)
807 /* Be sure that node is defined in IR file, not in other object
808 file. In that case we don't set used_from_other_object_file. */
809 && node->definition)
811 else if (!whole_program)
812 return true;
814 if (MAIN_NAME_P (DECL_NAME (node->decl)))
815 return true;
817 return false;
820 /* Return true when variable VNODE should be considered externally visible. */
822 bool
823 varpool_externally_visible_p (varpool_node *vnode)
825 if (DECL_EXTERNAL (vnode->decl))
826 return true;
828 if (!TREE_PUBLIC (vnode->decl))
829 return false;
831 /* If linker counts on us, we must preserve the function. */
832 if (symtab_used_from_object_file_p (vnode))
833 return true;
835 if (DECL_HARD_REGISTER (vnode->decl))
836 return true;
837 if (DECL_PRESERVE_P (vnode->decl))
838 return true;
839 if (lookup_attribute ("externally_visible",
840 DECL_ATTRIBUTES (vnode->decl)))
841 return true;
842 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
843 && lookup_attribute ("dllexport",
844 DECL_ATTRIBUTES (vnode->decl)))
845 return true;
847 /* See if we have linker information about symbol not being used or
848 if we need to make guess based on the declaration.
850 Even if the linker clams the symbol is unused, never bring internal
851 symbols that are declared by user as used or externally visible.
852 This is needed for i.e. references from asm statements. */
853 if (symtab_used_from_object_file_p (vnode))
854 return true;
855 if (vnode->resolution == LDPR_PREVAILING_DEF_IRONLY)
856 return false;
858 /* As a special case, the COMDAT virtual tables can be unshared.
859 In LTO mode turn vtables into static variables. The variable is readonly,
860 so this does not enable more optimization, but referring static var
861 is faster for dynamic linking. Also this match logic hidding vtables
862 from LTO symbol tables. */
863 if ((in_lto_p || flag_whole_program)
864 && DECL_COMDAT (vnode->decl)
865 && comdat_can_be_unshared_p (vnode))
866 return false;
868 /* When doing link time optimizations, hidden symbols become local. */
869 if (in_lto_p
870 && (DECL_VISIBILITY (vnode->decl) == VISIBILITY_HIDDEN
871 || DECL_VISIBILITY (vnode->decl) == VISIBILITY_INTERNAL)
872 /* Be sure that node is defined in IR file, not in other object
873 file. In that case we don't set used_from_other_object_file. */
874 && vnode->definition)
876 else if (!flag_whole_program)
877 return true;
879 /* Do not attempt to privatize COMDATS by default.
880 This would break linking with C++ libraries sharing
881 inline definitions.
883 FIXME: We can do so for readonly vars with no address taken and
884 possibly also for vtables since no direct pointer comparsion is done.
885 It might be interesting to do so to reduce linking overhead. */
886 if (DECL_COMDAT (vnode->decl) || DECL_WEAK (vnode->decl))
887 return true;
888 return false;
891 /* Return true if reference to NODE can be replaced by a local alias.
892 Local aliases save dynamic linking overhead and enable more optimizations.
895 bool
896 can_replace_by_local_alias (symtab_node *node)
898 return (symtab_node_availability (node) > AVAIL_OVERWRITABLE
899 && !symtab_can_be_discarded (node));
902 /* Mark visibility of all functions.
904 A local function is one whose calls can occur only in the current
905 compilation unit and all its calls are explicit, so we can change
906 its calling convention. We simply mark all static functions whose
907 address is not taken as local.
909 We also change the TREE_PUBLIC flag of all declarations that are public
910 in language point of view but we want to overwrite this default
911 via visibilities for the backend point of view. */
913 static unsigned int
914 function_and_variable_visibility (bool whole_program)
916 struct cgraph_node *node;
917 varpool_node *vnode;
919 /* All aliases should be procssed at this point. */
920 gcc_checking_assert (!alias_pairs || !alias_pairs->length ());
922 FOR_EACH_FUNCTION (node)
924 int flags = flags_from_decl_or_type (node->decl);
926 /* Optimize away PURE and CONST constructors and destructors. */
927 if (optimize
928 && (flags & (ECF_CONST | ECF_PURE))
929 && !(flags & ECF_LOOPING_CONST_OR_PURE))
931 DECL_STATIC_CONSTRUCTOR (node->decl) = 0;
932 DECL_STATIC_DESTRUCTOR (node->decl) = 0;
935 /* Frontends and alias code marks nodes as needed before parsing is finished.
936 We may end up marking as node external nodes where this flag is meaningless
937 strip it. */
938 if (DECL_EXTERNAL (node->decl) || !node->definition)
940 node->force_output = 0;
941 node->forced_by_abi = 0;
944 /* C++ FE on lack of COMDAT support create local COMDAT functions
945 (that ought to be shared but can not due to object format
946 limitations). It is necessary to keep the flag to make rest of C++ FE
947 happy. Clear the flag here to avoid confusion in middle-end. */
948 if (DECL_COMDAT (node->decl) && !TREE_PUBLIC (node->decl))
949 DECL_COMDAT (node->decl) = 0;
951 /* For external decls stop tracking same_comdat_group. It doesn't matter
952 what comdat group they are in when they won't be emitted in this TU. */
953 if (node->same_comdat_group && DECL_EXTERNAL (node->decl))
955 #ifdef ENABLE_CHECKING
956 symtab_node *n;
958 for (n = node->same_comdat_group;
959 n != node;
960 n = n->same_comdat_group)
961 /* If at least one of same comdat group functions is external,
962 all of them have to be, otherwise it is a front-end bug. */
963 gcc_assert (DECL_EXTERNAL (n->decl));
964 #endif
965 symtab_dissolve_same_comdat_group_list (node);
967 gcc_assert ((!DECL_WEAK (node->decl)
968 && !DECL_COMDAT (node->decl))
969 || TREE_PUBLIC (node->decl)
970 || node->weakref
971 || DECL_EXTERNAL (node->decl));
972 if (cgraph_externally_visible_p (node, whole_program))
974 gcc_assert (!node->global.inlined_to);
975 node->externally_visible = true;
977 else
979 node->externally_visible = false;
980 node->forced_by_abi = false;
982 if (!node->externally_visible
983 && node->definition && !node->weakref
984 && !DECL_EXTERNAL (node->decl))
986 gcc_assert (whole_program || in_lto_p
987 || !TREE_PUBLIC (node->decl));
988 node->unique_name = ((node->resolution == LDPR_PREVAILING_DEF_IRONLY
989 || node->unique_name
990 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
991 && TREE_PUBLIC (node->decl));
992 node->resolution = LDPR_PREVAILING_DEF_IRONLY;
993 if (node->same_comdat_group && TREE_PUBLIC (node->decl))
995 symtab_node *next = node;
997 /* Set all members of comdat group local. */
998 if (node->same_comdat_group)
999 for (next = node->same_comdat_group;
1000 next != node;
1001 next = next->same_comdat_group)
1003 symtab_make_decl_local (next->decl);
1004 next->unique_name = ((next->resolution == LDPR_PREVAILING_DEF_IRONLY
1005 || next->unique_name
1006 || next->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1007 && TREE_PUBLIC (next->decl));
1009 /* cgraph_externally_visible_p has already checked all other nodes
1010 in the group and they will all be made local. We need to
1011 dissolve the group at once so that the predicate does not
1012 segfault though. */
1013 symtab_dissolve_same_comdat_group_list (node);
1015 symtab_make_decl_local (node->decl);
1018 if (node->thunk.thunk_p
1019 && TREE_PUBLIC (node->decl))
1021 struct cgraph_node *decl_node = node;
1023 decl_node = cgraph_function_node (decl_node->callees->callee, NULL);
1025 /* Thunks have the same visibility as function they are attached to.
1026 Make sure the C++ front end set this up properly. */
1027 if (DECL_ONE_ONLY (decl_node->decl))
1029 gcc_checking_assert (DECL_COMDAT (node->decl)
1030 == DECL_COMDAT (decl_node->decl));
1031 gcc_checking_assert (DECL_COMDAT_GROUP (node->decl)
1032 == DECL_COMDAT_GROUP (decl_node->decl));
1033 gcc_checking_assert (node->same_comdat_group);
1035 node->forced_by_abi = decl_node->forced_by_abi;
1036 if (DECL_EXTERNAL (decl_node->decl))
1037 DECL_EXTERNAL (node->decl) = 1;
1040 /* If whole comdat group is used only within LTO code, we can dissolve it,
1041 we handle the unification ourselves.
1042 We keep COMDAT and weak so visibility out of DSO does not change.
1043 Later we may bring the symbols static if they are not exported. */
1044 if (DECL_ONE_ONLY (node->decl)
1045 && (node->resolution == LDPR_PREVAILING_DEF_IRONLY
1046 || node->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP))
1048 symtab_node *next = node;
1050 if (node->same_comdat_group)
1051 for (next = node->same_comdat_group;
1052 next != node;
1053 next = next->same_comdat_group)
1054 if (next->externally_visible
1055 && (next->resolution != LDPR_PREVAILING_DEF_IRONLY
1056 && next->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP))
1057 break;
1058 if (node == next)
1060 if (node->same_comdat_group)
1061 for (next = node->same_comdat_group;
1062 next != node;
1063 next = next->same_comdat_group)
1065 DECL_COMDAT_GROUP (next->decl) = NULL;
1066 DECL_WEAK (next->decl) = false;
1068 DECL_COMDAT_GROUP (node->decl) = NULL;
1069 symtab_dissolve_same_comdat_group_list (node);
1073 FOR_EACH_DEFINED_FUNCTION (node)
1075 node->local.local |= cgraph_local_node_p (node);
1077 /* If we know that function can not be overwritten by a different semantics
1078 and moreover its section can not be discarded, replace all direct calls
1079 by calls to an nonoverwritable alias. This make dynamic linking
1080 cheaper and enable more optimization.
1082 TODO: We can also update virtual tables. */
1083 if (node->callers && can_replace_by_local_alias (node))
1085 struct cgraph_node *alias = cgraph (symtab_nonoverwritable_alias (node));
1087 if (alias && alias != node)
1089 while (node->callers)
1091 struct cgraph_edge *e = node->callers;
1093 cgraph_redirect_edge_callee (e, alias);
1094 if (gimple_has_body_p (e->caller->decl))
1096 push_cfun (DECL_STRUCT_FUNCTION (e->caller->decl));
1097 cgraph_redirect_edge_call_stmt_to_callee (e);
1098 pop_cfun ();
1104 FOR_EACH_VARIABLE (vnode)
1106 /* weak flag makes no sense on local variables. */
1107 gcc_assert (!DECL_WEAK (vnode->decl)
1108 || vnode->weakref
1109 || TREE_PUBLIC (vnode->decl)
1110 || DECL_EXTERNAL (vnode->decl));
1111 /* In several cases declarations can not be common:
1113 - when declaration has initializer
1114 - when it is in weak
1115 - when it has specific section
1116 - when it resides in non-generic address space.
1117 - if declaration is local, it will get into .local common section
1118 so common flag is not needed. Frontends still produce these in
1119 certain cases, such as for:
1121 static int a __attribute__ ((common))
1123 Canonicalize things here and clear the redundant flag. */
1124 if (DECL_COMMON (vnode->decl)
1125 && (!(TREE_PUBLIC (vnode->decl)
1126 || DECL_EXTERNAL (vnode->decl))
1127 || (DECL_INITIAL (vnode->decl)
1128 && DECL_INITIAL (vnode->decl) != error_mark_node)
1129 || DECL_WEAK (vnode->decl)
1130 || DECL_SECTION_NAME (vnode->decl) != NULL
1131 || ! (ADDR_SPACE_GENERIC_P
1132 (TYPE_ADDR_SPACE (TREE_TYPE (vnode->decl))))))
1133 DECL_COMMON (vnode->decl) = 0;
1135 FOR_EACH_DEFINED_VARIABLE (vnode)
1137 if (!vnode->definition)
1138 continue;
1139 if (varpool_externally_visible_p (vnode))
1140 vnode->externally_visible = true;
1141 else
1143 vnode->externally_visible = false;
1144 vnode->forced_by_abi = false;
1146 if (!vnode->externally_visible
1147 && !vnode->weakref)
1149 gcc_assert (in_lto_p || whole_program || !TREE_PUBLIC (vnode->decl));
1150 vnode->unique_name = ((vnode->resolution == LDPR_PREVAILING_DEF_IRONLY
1151 || vnode->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP)
1152 && TREE_PUBLIC (vnode->decl));
1153 symtab_make_decl_local (vnode->decl);
1154 if (vnode->same_comdat_group)
1155 symtab_dissolve_same_comdat_group_list (vnode);
1156 vnode->resolution = LDPR_PREVAILING_DEF_IRONLY;
1160 if (dump_file)
1162 fprintf (dump_file, "\nMarking local functions:");
1163 FOR_EACH_DEFINED_FUNCTION (node)
1164 if (node->local.local)
1165 fprintf (dump_file, " %s", node->name ());
1166 fprintf (dump_file, "\n\n");
1167 fprintf (dump_file, "\nMarking externally visible functions:");
1168 FOR_EACH_DEFINED_FUNCTION (node)
1169 if (node->externally_visible)
1170 fprintf (dump_file, " %s", node->name ());
1171 fprintf (dump_file, "\n\n");
1172 fprintf (dump_file, "\nMarking externally visible variables:");
1173 FOR_EACH_DEFINED_VARIABLE (vnode)
1174 if (vnode->externally_visible)
1175 fprintf (dump_file, " %s", vnode->name ());
1176 fprintf (dump_file, "\n\n");
1178 cgraph_function_flags_ready = true;
1179 return 0;
1182 /* Local function pass handling visibilities. This happens before LTO streaming
1183 so in particular -fwhole-program should be ignored at this level. */
1185 static unsigned int
1186 local_function_and_variable_visibility (void)
1188 return function_and_variable_visibility (flag_whole_program && !flag_lto);
1191 namespace {
1193 const pass_data pass_data_ipa_function_and_variable_visibility =
1195 SIMPLE_IPA_PASS, /* type */
1196 "visibility", /* name */
1197 OPTGROUP_NONE, /* optinfo_flags */
1198 false, /* has_gate */
1199 true, /* has_execute */
1200 TV_CGRAPHOPT, /* tv_id */
1201 0, /* properties_required */
1202 0, /* properties_provided */
1203 0, /* properties_destroyed */
1204 0, /* todo_flags_start */
1205 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1208 class pass_ipa_function_and_variable_visibility : public simple_ipa_opt_pass
1210 public:
1211 pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1212 : simple_ipa_opt_pass (pass_data_ipa_function_and_variable_visibility,
1213 ctxt)
1216 /* opt_pass methods: */
1217 unsigned int execute () {
1218 return local_function_and_variable_visibility ();
1221 }; // class pass_ipa_function_and_variable_visibility
1223 } // anon namespace
1225 simple_ipa_opt_pass *
1226 make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt)
1228 return new pass_ipa_function_and_variable_visibility (ctxt);
1231 /* Free inline summary. */
1233 static unsigned
1234 free_inline_summary (void)
1236 inline_free_summary ();
1237 return 0;
1240 namespace {
1242 const pass_data pass_data_ipa_free_inline_summary =
1244 SIMPLE_IPA_PASS, /* type */
1245 "*free_inline_summary", /* name */
1246 OPTGROUP_NONE, /* optinfo_flags */
1247 false, /* has_gate */
1248 true, /* has_execute */
1249 TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */
1250 0, /* properties_required */
1251 0, /* properties_provided */
1252 0, /* properties_destroyed */
1253 0, /* todo_flags_start */
1254 0, /* todo_flags_finish */
1257 class pass_ipa_free_inline_summary : public simple_ipa_opt_pass
1259 public:
1260 pass_ipa_free_inline_summary (gcc::context *ctxt)
1261 : simple_ipa_opt_pass (pass_data_ipa_free_inline_summary, ctxt)
1264 /* opt_pass methods: */
1265 unsigned int execute () { return free_inline_summary (); }
1267 }; // class pass_ipa_free_inline_summary
1269 } // anon namespace
1271 simple_ipa_opt_pass *
1272 make_pass_ipa_free_inline_summary (gcc::context *ctxt)
1274 return new pass_ipa_free_inline_summary (ctxt);
1277 /* Do not re-run on ltrans stage. */
1279 static bool
1280 gate_whole_program_function_and_variable_visibility (void)
1282 return !flag_ltrans;
1285 /* Bring functionss local at LTO time with -fwhole-program. */
1287 static unsigned int
1288 whole_program_function_and_variable_visibility (void)
1290 function_and_variable_visibility (flag_whole_program);
1291 if (optimize)
1292 ipa_discover_readonly_nonaddressable_vars ();
1293 return 0;
1296 namespace {
1298 const pass_data pass_data_ipa_whole_program_visibility =
1300 IPA_PASS, /* type */
1301 "whole-program", /* name */
1302 OPTGROUP_NONE, /* optinfo_flags */
1303 true, /* has_gate */
1304 true, /* has_execute */
1305 TV_CGRAPHOPT, /* tv_id */
1306 0, /* properties_required */
1307 0, /* properties_provided */
1308 0, /* properties_destroyed */
1309 0, /* todo_flags_start */
1310 ( TODO_remove_functions | TODO_dump_symtab ), /* todo_flags_finish */
1313 class pass_ipa_whole_program_visibility : public ipa_opt_pass_d
1315 public:
1316 pass_ipa_whole_program_visibility (gcc::context *ctxt)
1317 : ipa_opt_pass_d (pass_data_ipa_whole_program_visibility, ctxt,
1318 NULL, /* generate_summary */
1319 NULL, /* write_summary */
1320 NULL, /* read_summary */
1321 NULL, /* write_optimization_summary */
1322 NULL, /* read_optimization_summary */
1323 NULL, /* stmt_fixup */
1324 0, /* function_transform_todo_flags_start */
1325 NULL, /* function_transform */
1326 NULL) /* variable_transform */
1329 /* opt_pass methods: */
1330 bool gate () {
1331 return gate_whole_program_function_and_variable_visibility ();
1333 unsigned int execute () {
1334 return whole_program_function_and_variable_visibility ();
1337 }; // class pass_ipa_whole_program_visibility
1339 } // anon namespace
1341 ipa_opt_pass_d *
1342 make_pass_ipa_whole_program_visibility (gcc::context *ctxt)
1344 return new pass_ipa_whole_program_visibility (ctxt);
1347 /* Generate and emit a static constructor or destructor. WHICH must
1348 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1349 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1350 initialization priority for this constructor or destructor.
1352 FINAL specify whether the externally visible name for collect2 should
1353 be produced. */
1355 static void
1356 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final)
1358 static int counter = 0;
1359 char which_buf[16];
1360 tree decl, name, resdecl;
1362 /* The priority is encoded in the constructor or destructor name.
1363 collect2 will sort the names and arrange that they are called at
1364 program startup. */
1365 if (final)
1366 sprintf (which_buf, "%c_%.5d_%d", which, priority, counter++);
1367 else
1368 /* Proudce sane name but one not recognizable by collect2, just for the
1369 case we fail to inline the function. */
1370 sprintf (which_buf, "sub_%c_%.5d_%d", which, priority, counter++);
1371 name = get_file_function_name (which_buf);
1373 decl = build_decl (input_location, FUNCTION_DECL, name,
1374 build_function_type_list (void_type_node, NULL_TREE));
1375 current_function_decl = decl;
1377 resdecl = build_decl (input_location,
1378 RESULT_DECL, NULL_TREE, void_type_node);
1379 DECL_ARTIFICIAL (resdecl) = 1;
1380 DECL_RESULT (decl) = resdecl;
1381 DECL_CONTEXT (resdecl) = decl;
1383 allocate_struct_function (decl, false);
1385 TREE_STATIC (decl) = 1;
1386 TREE_USED (decl) = 1;
1387 DECL_ARTIFICIAL (decl) = 1;
1388 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1389 DECL_SAVED_TREE (decl) = body;
1390 if (!targetm.have_ctors_dtors && final)
1392 TREE_PUBLIC (decl) = 1;
1393 DECL_PRESERVE_P (decl) = 1;
1395 DECL_UNINLINABLE (decl) = 1;
1397 DECL_INITIAL (decl) = make_node (BLOCK);
1398 TREE_USED (DECL_INITIAL (decl)) = 1;
1400 DECL_SOURCE_LOCATION (decl) = input_location;
1401 cfun->function_end_locus = input_location;
1403 switch (which)
1405 case 'I':
1406 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1407 decl_init_priority_insert (decl, priority);
1408 break;
1409 case 'D':
1410 DECL_STATIC_DESTRUCTOR (decl) = 1;
1411 decl_fini_priority_insert (decl, priority);
1412 break;
1413 default:
1414 gcc_unreachable ();
1417 gimplify_function_tree (decl);
1419 cgraph_add_new_function (decl, false);
1421 set_cfun (NULL);
1422 current_function_decl = NULL;
1425 /* Generate and emit a static constructor or destructor. WHICH must
1426 be one of 'I' (for a constructor) or 'D' (for a destructor). BODY
1427 is a STATEMENT_LIST containing GENERIC statements. PRIORITY is the
1428 initialization priority for this constructor or destructor. */
1430 void
1431 cgraph_build_static_cdtor (char which, tree body, int priority)
1433 cgraph_build_static_cdtor_1 (which, body, priority, false);
1436 /* A vector of FUNCTION_DECLs declared as static constructors. */
1437 static vec<tree> static_ctors;
1438 /* A vector of FUNCTION_DECLs declared as static destructors. */
1439 static vec<tree> static_dtors;
1441 /* When target does not have ctors and dtors, we call all constructor
1442 and destructor by special initialization/destruction function
1443 recognized by collect2.
1445 When we are going to build this function, collect all constructors and
1446 destructors and turn them into normal functions. */
1448 static void
1449 record_cdtor_fn (struct cgraph_node *node)
1451 if (DECL_STATIC_CONSTRUCTOR (node->decl))
1452 static_ctors.safe_push (node->decl);
1453 if (DECL_STATIC_DESTRUCTOR (node->decl))
1454 static_dtors.safe_push (node->decl);
1455 node = cgraph_get_node (node->decl);
1456 DECL_DISREGARD_INLINE_LIMITS (node->decl) = 1;
1459 /* Define global constructors/destructor functions for the CDTORS, of
1460 which they are LEN. The CDTORS are sorted by initialization
1461 priority. If CTOR_P is true, these are constructors; otherwise,
1462 they are destructors. */
1464 static void
1465 build_cdtor (bool ctor_p, vec<tree> cdtors)
1467 size_t i,j;
1468 size_t len = cdtors.length ();
1470 i = 0;
1471 while (i < len)
1473 tree body;
1474 tree fn;
1475 priority_type priority;
1477 priority = 0;
1478 body = NULL_TREE;
1479 j = i;
1482 priority_type p;
1483 fn = cdtors[j];
1484 p = ctor_p ? DECL_INIT_PRIORITY (fn) : DECL_FINI_PRIORITY (fn);
1485 if (j == i)
1486 priority = p;
1487 else if (p != priority)
1488 break;
1489 j++;
1491 while (j < len);
1493 /* When there is only one cdtor and target supports them, do nothing. */
1494 if (j == i + 1
1495 && targetm.have_ctors_dtors)
1497 i++;
1498 continue;
1500 /* Find the next batch of constructors/destructors with the same
1501 initialization priority. */
1502 for (;i < j; i++)
1504 tree call;
1505 fn = cdtors[i];
1506 call = build_call_expr (fn, 0);
1507 if (ctor_p)
1508 DECL_STATIC_CONSTRUCTOR (fn) = 0;
1509 else
1510 DECL_STATIC_DESTRUCTOR (fn) = 0;
1511 /* We do not want to optimize away pure/const calls here.
1512 When optimizing, these should be already removed, when not
1513 optimizing, we want user to be able to breakpoint in them. */
1514 TREE_SIDE_EFFECTS (call) = 1;
1515 append_to_statement_list (call, &body);
1517 gcc_assert (body != NULL_TREE);
1518 /* Generate a function to call all the function of like
1519 priority. */
1520 cgraph_build_static_cdtor_1 (ctor_p ? 'I' : 'D', body, priority, true);
1524 /* Comparison function for qsort. P1 and P2 are actually of type
1525 "tree *" and point to static constructors. DECL_INIT_PRIORITY is
1526 used to determine the sort order. */
1528 static int
1529 compare_ctor (const void *p1, const void *p2)
1531 tree f1;
1532 tree f2;
1533 int priority1;
1534 int priority2;
1536 f1 = *(const tree *)p1;
1537 f2 = *(const tree *)p2;
1538 priority1 = DECL_INIT_PRIORITY (f1);
1539 priority2 = DECL_INIT_PRIORITY (f2);
1541 if (priority1 < priority2)
1542 return -1;
1543 else if (priority1 > priority2)
1544 return 1;
1545 else
1546 /* Ensure a stable sort. Constructors are executed in backwarding
1547 order to make LTO initialize braries first. */
1548 return DECL_UID (f2) - DECL_UID (f1);
1551 /* Comparison function for qsort. P1 and P2 are actually of type
1552 "tree *" and point to static destructors. DECL_FINI_PRIORITY is
1553 used to determine the sort order. */
1555 static int
1556 compare_dtor (const void *p1, const void *p2)
1558 tree f1;
1559 tree f2;
1560 int priority1;
1561 int priority2;
1563 f1 = *(const tree *)p1;
1564 f2 = *(const tree *)p2;
1565 priority1 = DECL_FINI_PRIORITY (f1);
1566 priority2 = DECL_FINI_PRIORITY (f2);
1568 if (priority1 < priority2)
1569 return -1;
1570 else if (priority1 > priority2)
1571 return 1;
1572 else
1573 /* Ensure a stable sort. */
1574 return DECL_UID (f1) - DECL_UID (f2);
1577 /* Generate functions to call static constructors and destructors
1578 for targets that do not support .ctors/.dtors sections. These
1579 functions have magic names which are detected by collect2. */
1581 static void
1582 build_cdtor_fns (void)
1584 if (!static_ctors.is_empty ())
1586 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1587 static_ctors.qsort (compare_ctor);
1588 build_cdtor (/*ctor_p=*/true, static_ctors);
1591 if (!static_dtors.is_empty ())
1593 gcc_assert (!targetm.have_ctors_dtors || in_lto_p);
1594 static_dtors.qsort (compare_dtor);
1595 build_cdtor (/*ctor_p=*/false, static_dtors);
1599 /* Look for constructors and destructors and produce function calling them.
1600 This is needed for targets not supporting ctors or dtors, but we perform the
1601 transformation also at linktime to merge possibly numerous
1602 constructors/destructors into single function to improve code locality and
1603 reduce size. */
1605 static unsigned int
1606 ipa_cdtor_merge (void)
1608 struct cgraph_node *node;
1609 FOR_EACH_DEFINED_FUNCTION (node)
1610 if (DECL_STATIC_CONSTRUCTOR (node->decl)
1611 || DECL_STATIC_DESTRUCTOR (node->decl))
1612 record_cdtor_fn (node);
1613 build_cdtor_fns ();
1614 static_ctors.release ();
1615 static_dtors.release ();
1616 return 0;
1619 /* Perform the pass when we have no ctors/dtors support
1620 or at LTO time to merge multiple constructors into single
1621 function. */
1623 static bool
1624 gate_ipa_cdtor_merge (void)
1626 return !targetm.have_ctors_dtors || (optimize && in_lto_p);
1629 namespace {
1631 const pass_data pass_data_ipa_cdtor_merge =
1633 IPA_PASS, /* type */
1634 "cdtor", /* name */
1635 OPTGROUP_NONE, /* optinfo_flags */
1636 true, /* has_gate */
1637 true, /* has_execute */
1638 TV_CGRAPHOPT, /* tv_id */
1639 0, /* properties_required */
1640 0, /* properties_provided */
1641 0, /* properties_destroyed */
1642 0, /* todo_flags_start */
1643 0, /* todo_flags_finish */
1646 class pass_ipa_cdtor_merge : public ipa_opt_pass_d
1648 public:
1649 pass_ipa_cdtor_merge (gcc::context *ctxt)
1650 : ipa_opt_pass_d (pass_data_ipa_cdtor_merge, ctxt,
1651 NULL, /* generate_summary */
1652 NULL, /* write_summary */
1653 NULL, /* read_summary */
1654 NULL, /* write_optimization_summary */
1655 NULL, /* read_optimization_summary */
1656 NULL, /* stmt_fixup */
1657 0, /* function_transform_todo_flags_start */
1658 NULL, /* function_transform */
1659 NULL) /* variable_transform */
1662 /* opt_pass methods: */
1663 bool gate () { return gate_ipa_cdtor_merge (); }
1664 unsigned int execute () { return ipa_cdtor_merge (); }
1666 }; // class pass_ipa_cdtor_merge
1668 } // anon namespace
1670 ipa_opt_pass_d *
1671 make_pass_ipa_cdtor_merge (gcc::context *ctxt)
1673 return new pass_ipa_cdtor_merge (ctxt);